Skip to content

About

Automatically generates .hlsl equivalent structs/enums/constants from your C# structs/enums/readonly structs

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

15 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Shared Structs C#/HLSL (Unity)

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.

Installation

Simply add the entire folder into your project's Assets folder.


Example Structs

FileA.cs:

[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;
}

FileB.cs:

[SharedStruct]
public struct MyStructC
{
	public int a;
	public int b;
	public MyStructD c;
}

[SharedStruct]
public struct MyStructD
{
	public int a;
	public int b;
}

FileA_AutoGenerated.hlsl:

#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

FileB_AutoGenerated.hlsl:

#ifndef MyFileB_AutoGenerated
#define MyFileB_AutoGenerated

struct MyStructD {
	int a;
	int b;
};

struct MyStructC {
	int a;
	int b;
	MyStructD c;
};

#endif

Example Enums

FileA.cs:

[SharedStruct]
public enum MyEnum
{
	MY_VAL = 10,
	MY_OTHER_VAL = 11,
	THIRD_VAL = 12 
}

FileA_AutoGenerated.hlsl:

#ifndef MyFileA_AutoGenerated
#define MyFileA_AutoGenerated

#define MyEnum_MY_VAL 10
#define MyEnum_MY_OTHER_VAL 11
#define MyEnum_THIRD_VAL 12

#endif

Example ReadOnly Structs

FileA.cs:

[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) };
}

FileA_AutoGenerated.hlsl:

#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

Twitter: @aelstraz

About

Automatically generates .hlsl equivalent structs/enums/constants from your C# structs/enums/readonly structs

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages