Search code examples
includeshaderdirect3dhlslpragma

HLSL #pragma once equivalent?


What's the equivalent to C/C++-like #pragma once in HLSL includes?

I want (as a contrived example):

// ./dependency.hlsl
float x() { return 0; }

// ./shader.hlsl
#include "./dependency.hlsl" // (./ unnecessary; for question readability)
#include "./dependency.hlsl"

to not fail with error X3003: redefinition of 'x'. #pragma once at the top of my file yields a non-error warning X3568: 'once' : unknown pragma ignored and does nothing!


Solution

  • Use C/C++ macro-like include guards. The contrived dependency.hlsl would look as follows:

    #ifndef __DEPENDENCY_HLSL__
    #define __DEPENDENCY_HLSL__
    
    float x() { return 0; }
    
    #endif // __DEPENDENCY_HLSL__