Skip to content

Instantly share code, notes, and snippets.

View Arlorean's full-sized avatar

Adam Davidson Arlorean

View GitHub Profile
@Arlorean
Arlorean / UsingCsCodeInHLSL.cs
Created January 5, 2025 14:34 — forked from kraj0t/UsingCsCodeInHLSL.cs
How to include C# code in an HLSL file - useful for sharing code between CPU and shader without duplicating any files
// Share code between HLSL (shaders) and C#
//
// Limitations:
// - Cannot use #include
// - Must use defines to hide certain keywords, such as public, private, protected, or any other object-oriented programming keywords
// - Use defines to convert half or fixed to your desired C# equivalent
// - Must always write f after float literals
// - Use #if !MY_DEFINE instead of #ifndef MY_DEFINE
// - #define cannot be used with a value in C#, not even inside an '#if SHADER_TARGET' block. Therefore, you have two options for declaring valued constants:
// a. Declare each constant separately in HLSL (using 'static const float MyConstant = 1.0f') and in C# (using 'const float MyConstant = 1.0f'). C# does not support 'static const'.