dx11 and D3D_SHADER_MACRO

5000fdaaeeeb9f038be93cf67eb3b130
0
David_Gallagher 102 Jan 19, 2012 at 03:35

I was just wondering whether to use D3D_SHADER_MACROor not to control different effects in the same shader. unfortunatley in order to change the effect via macro the shader needs to be rebuilt. Is there any reason (performance wise) why I shouldn’t just use a boolas it would provide more flexibility as far as changing effects runtime where different meshes share the same effect though different parts of the shader (i.e turning on\off culling or different light counts in use etc…), or should I just create a seperate VS,HS etc… for each effect, though that would mean more/repeating code being built at startup with only slight changes however I’m assuming faster runtime.

also if wanting to precompile my shaders would I just need to use the 2 following functions, D3DCompile() and D3DWriteBlobToFile().
then for loading is it just D3DReadFileToBlob() before calling CreateVertexShader().

thanks for any insight into the best way to go about this.

2 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Jan 19, 2012 at 05:23

You can use bools for this, however you probably want to make sure they’re uniform bools to ensure that you don’t end up creating a dynamic branch in the shader - which can be quite slow even if all of the threads (vertices/pixels) are branching the same direction. You’d set up multiple techniques, each of which calls the shaders with a different combination of bools, then select the correct technique to render with. The shader compiler will optimize away the uniform bools in this case, producing separate compiled versions of the shader for each technique.

As for the functions to call, yes, I think you’ve got it right. You can also use D3DCompileFromFile if your HLSL source is in a file.

5000fdaaeeeb9f038be93cf67eb3b130
0
David_Gallagher 102 Jan 19, 2012 at 08:46

Thanks Reedbeta for the explanation, sounds like it will work out great using that method. turns out I’ll have to pass on D3DWriteBlobToFile() and D3DReadFileToBlob() as I just realised they are part of the windows developer preview, but D3DCompileFromFile() works great, thanks again!