Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Shader 'defines' (effects)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 cozzie   Members   -  Reputation: 2700

Like
0Likes
Like

Posted 24 May 2014 - 05:38 AM

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



Sponsor:

#2 kauna   Crossbones+   -  Reputation: 2888

Like
1Likes
Like

Posted 24 May 2014 - 01:40 PM

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!

 

[edit] 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, 24 May 2014 - 01:41 PM.


#3 cozzie   Members   -  Reputation: 2700

Like
0Likes
Like

Posted 24 May 2014 - 01:50 PM

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, 24 May 2014 - 01:50 PM.


#4 osmanb   Crossbones+   -  Reputation: 1759

Like
1Likes
Like

Posted 24 May 2014 - 02:31 PM

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.



#5 cozzie   Members   -  Reputation: 2700

Like
0Likes
Like

Posted 24 May 2014 - 03:52 PM

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.



#6 osmanb   Crossbones+   -  Reputation: 1759

Like
1Likes
Like

Posted 25 May 2014 - 10:33 AM

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...



#7 cozzie   Members   -  Reputation: 2700

Like
0Likes
Like

Posted 25 May 2014 - 11:53 AM

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, 25 May 2014 - 11:53 AM.


#8 cozzie   Members   -  Reputation: 2700

Like
0Likes
Like

Posted 26 May 2014 - 01:55 PM

Update: I tried it out correctly and it works great. Thanks




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS