4

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!

Warty
  • 7,237
  • 1
  • 31
  • 49
  • "hlsl" "include guard" brings up only 135 results on google, none of which are relevant and "hlsl" "pragma once" brings up C++ stuff. Hopefully the Google gods catch this. Also, not sure if there's a better approach (I'm fairly newbie) so maybe my provided answer is nonoptimal. – Warty Dec 26 '17 at 12:00

1 Answers1

9

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__
Warty
  • 7,237
  • 1
  • 31
  • 49