• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
cozzie

Shader 'defines' (effects)

7 posts in this topic

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

0

Share this post


Link to post
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!

 

[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
1

Share this post


Link to post
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
0

Share this post


Link to post
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.

1

Share this post


Link to post
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.

0

Share this post


Link to post
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...

1

Share this post


Link to post
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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0