Automatically generates HLSL structs/enums/constants from C# structs/enums/readonly structs marked with the [SharedStruct] attribute. The equivalent .hlsl files will be automatically generated with no extra work. Structs are automatically re-ordered in order of dependency, and automatically '#includes' any referenced external struct also marked with the [SharedStruct] attribute. Enums are automatically converted to '#define' symbols (e.g '#define MyEnumName_FieldName FieldValue'). To generate constants, use a readonly struct.
Generated files are written to 'Assets/Auto Generated HLSL Structs'. When declaring arrays in a standard struct, either add the fixed keyword and specify a length, or [MarshalAs] the type with a constant length value (note: readonly structs use regular arrays). Only 32-bit built-in types, vectors and other shared structs are valid types. Struct functions/constructors are not converted, only variables/fields.
Simply add the entire folder into your project's Assets folder.
[SharedStruct]
public struct MyStructA
{
public int a;
public uint b;
public Vector3 c;
public Vector2Int d;
public MyStructB e;
}
[SharedStruct]
public struct MyStructB
{
public Vector3 a;
public Vector3 b;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
public Vector3 c[];
public MyStructC d;
}[SharedStruct]
public struct MyStructC
{
public int a;
public int b;
public MyStructD c;
}
[SharedStruct]
public struct MyStructD
{
public int a;
public int b;
}#ifndef MyFileA_AutoGenerated
#define MyFileA_AutoGenerated
#include "MyFileB_AutoGenerated.hlsl"
struct MyStructB {
float3 a;
float3 b;
float3 c[6];
MyStructC d;
};
struct MyStructA {
int a;
uint b;
float3 c;
int2 d;
MyStructB e;
};
#endif#ifndef MyFileB_AutoGenerated
#define MyFileB_AutoGenerated
struct MyStructD {
int a;
int b;
};
struct MyStructC {
int a;
int b;
MyStructD c;
};
#endif[SharedStruct]
public enum MyEnum
{
MY_VAL = 10,
MY_OTHER_VAL = 11,
THIRD_VAL = 12
}#ifndef MyFileA_AutoGenerated
#define MyFileA_AutoGenerated
#define MyEnum_MY_VAL 10
#define MyEnum_MY_OTHER_VAL 11
#define MyEnum_THIRD_VAL 12
#endif[SharedStruct]
public readonly struct MyConstants
{
public const int MY_INT = 10;
public static readonly Vector3 MY_VECTOR = new Vector3(0, 10, 0);
public static readonly Vector2 MY_VECTOR_ARRAY[] = new Vector2[] { new Vector2(1, 10), new Vector2(4, 10), new Vector2(6, 10) };
}#ifndef MyFileA_AutoGenerated
#define MyFileA_AutoGenerated
const int MY_INT = 10;
const float3 MY_VECTOR = float3(0.00, 10.00, 0.00);
const float2 MY_VECTOR_ARRAY[3] = {float2(1.00, 10.00), float2(4.00, 10.00), float2(6.00, 10.00)};
#endif