This topic is 1490 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

Someone triggered me to dig into using #defines (#ifdef) in my HLSL shaders/effects.

And I actually think it's very interesting, it can potentially bring me that I don't need 3 to the power of 8 number of effect files for adding 1 or 2 simply features in my shaders.

What I've learned so far is that this works:

// in the FX file

#ifdef SPECULAR
uniform extern shared float3		CameraPos		: CAMERAPOS;
#endif

struct VS_OUTPUT
{
float4 	Pos			: POSITION0;
float3	TangentWorld	: TEXCOORD1;
float3	BiNormalWorld	: TEXCOORD2;
float3 	NormalWorld		: TEXCOORD3;
float2 	TexCoord		: TEXCOORD4;
float3 	wPos			: TEXCOORD5;
#ifdef SPECULAR
float3 	ViewDir		: TEXCOORD6;
#endif
};

VS_OUTPUT VS_function(VS_INPUT input)
{
VS_OUTPUT Out = (VS_OUTPUT)0;

float4 worldPosition = mul(input.Pos, World);
Out.Pos = mul(worldPosition, ViewProj);

Out.TexCoord = input.TexCoord;
Out.wPos = worldPosition.xyz;

#ifdef SPECULAR
Out.ViewDir = normalize(CameraPos - Out.wPos.xyz);
#endif

// and in my code

std::vector<D3DXMACRO> defines;
defines.resize(0);

D3DXMACRO myDefine;
myDefine.Name = "SPECULAR";
myDefine.Definition = 0;
defines.push_back(myDefine);

myDefine.Name = NULL;
myDefine.Definition = NULL;
defines.push_back(myDefine);

HRESULT hr = D3DXCreateEffectFromFileA(pD3ddev, mFilename.c_str(), &defines[0], NULL, 0, pEffectPool, &mEffect, &errorBuffer);



But now let's say I want my effect file to be able to handle 'x' point lights.

Till now I had 2 different FX files (with a PS and VS), having the following constant:

#define MaxPointLights 4

extern float3		PointLightPos[MaxPointLights];
extern float2		PointLightRange[MaxPointLights];	// x = range, y = FP range
extern float3		PointLightColInt[MaxPointLights];



?

?

What I would like to do is set the max pointlights to 0 or 1 in the FX file, and through a define, compile the effect with 'x' maxpointlights.

Like this:

	std::vector<D3DXMACRO> defines;
defines.resize(0);

D3DXMACRO myDefine;
myDefine.Name = "MaxPointLights";
myDefine.Definition = "4";
defines.push_back(myDefine);

myDefine.Name = NULL;
myDefine.Definition = NULL;
defines.push_back(myDefine);

HRESULT hr = D3DXCreateEffectFromFileA(pD3ddev, mFilename.c_str(), &defines[0], NULL, 0, pEffectPool, &mEffect, &errorBuffer);



But I run into the following challenges:

- the effect will not compile with "MaxPointLights 0", because the arrays can then not be created (with size = 0)

- if I set MaxPointLights to 1 in the effect file, and try to compile it using the code above, the size of the arrays still stays 1 (and not 4)

So my question is, is it possible to have a define like MAX_LIGHTS set in the effect file, and adjust if through defines/d3dxmacro's like a do above?

Note: I did find a way to solve it, but that doesn't help in readability:

#ifdef FOURPOINTLIGHTS
#define MaxPointLights 4
extern float3		PointLightPos[MaxPointLights];
extern float2		PointLightRange[MaxPointLights];	// x = range, y = FP range
extern float3		PointLightColInt[MaxPointLights];
#endif

#ifdef EIGHTPOINTLIGHTS
#define MaxPointLights 8
extern float3		PointLightPos[MaxPointLights];
extern float2		PointLightRange[MaxPointLights];	// x = range, y = FP range
extern float3		PointLightColInt[MaxPointLights];
#endif



##### Share on other sites

Instead of changing the array size, you could fix the array to some maximum size and then use the macro to define how many times the lighting loop is executed.

Cheers!

 but of course that will mean that you'll have quite a few shader permutations, have you tried a bit more dynamic solution (ie. pass the amount lights as a constant buffer variable and execute the lighting loop based on it).

Edited by kauna

##### Share on other sites

Thanks, I didn't think of that before, sounds like a good idea.

I don't use 'constant buffers' yet (I think), I'm using dx9/d3d9 for now.

Do you mean something like this:

#define MaxPointLights 8

extern float3		PointLightPos[MaxPointLights];
extern float2		PointLightRange[MaxPointLights];	// x = range, y = FP range
extern float3		PointLightColInt[MaxPointLights];

int				NumPointLights;

#ifdef PLIGHT2 NumPointLights=2 #endif
#ifdef PLIGHT4 NumPointLights=4 #endif
#ifdef PLIGHT8 NumPointLights=8 #endif

// then somewhere in the PS:

for(int i=0;i<NumPointLights;++i)
{


Edited by cozzie

##### Share on other sites

For what it's worth, we do something like your original attempt, but we just wrap our array declarations with an extra check:

#if NUM_POINT_LIGHTS > 0
float4 pointLightPosition[NUM_POINT_LIGHTS];
// etc...
#endif


As long as your other code that does the lighting is also wrapped in a similar check, then the version of the shader with zero point lights will still compile, because the arrays won't exist, and nothing will try to access them.

##### Share on other sites

Thanks.

I didn't manage to do it like that, because NUM_POINT_LIGHTS in your example has to be a "#define" to be able to use it as an array size.

And if I use a #define NUM_POINT_LIGHTS, I cannot change it's value through passing a define when compiling.

##### Share on other sites

No, you can define the value when you run the compiler. Whether you specify a preprocessor token's value using #define in the source, or using the D3DXMACRO API when compiling makes no difference - it works exactly the same way. Our code does what you were originally suggesting - we set MANY defines using the macro API when we compile, and then use those to influence the generated shader, including as array sizes, etc...

##### Share on other sites

Hmm, OK. Then I'm either not understanding, didn't try hard enough or made a mistake.

Do you mean something like this should work:

In the Effect/ HLSL:

extern float3		PointLightPos[NUM_LIGHTS];
extern float2		PointLightRange[NUM_LIGHTS];	// x = range, y = FP range
extern float3		PointLightColInt[NUM_LIGHTS];



(where I don't define NUM_LIGHTS in the HLSL/ Effect file)

And then in the code:

	std::vector<D3DXMACRO> defines;
defines.resize(0);

myDefine.Name = "NUM_LIGHTS";
myDefine.Definition = "4";
defines.push_back(myDefine);

myDefine.Name = NULL;
myDefine.Definition = NULL;
defines.push_back(myDefine);

HRESULT hr = D3DXCreateEffectFromFileA(pD3ddev, mFilename.c_str(), &defines[0], NULL, 0, pEffectPool, &mEffect, &errorBuffer);



Till now I only used 'defines' when compiling, where I only set a name (without definition/ Always with value 0).

If the above should work theoretically, then I'll get into it again.

Especially because my current approach doesn't help readability. An example:

#define MaxPointLights 8

extern float3		PointLightPos[MaxPointLights];
extern float2		PointLightRange[MaxPointLights];	// x = range, y = FP range
extern float3		PointLightColInt[MaxPointLights];

// more stuff
// and then in the PS

float4 PS_function(VS_OUTPUT input): COLOR0
{
#if defined(PLIGHT4)
int NumPointLights = 4;
#elif defined(PLIGHT8)
int NumPointLights = 8;
#else
int NumPointLights = 0;
#endif

// I currently use this int for looping through the lights

Edited by cozzie

##### Share on other sites
Update: I tried it out correctly and it works great. Thanks

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631431
• Total Posts
3000039
×