Jump to content


dx11 and D3D_SHADER_MACRO


2 replies to this topic

#1 David Gallagher

    New Member

  • Members
  • PipPip
  • 33 posts

Posted 19 January 2012 - 03:35 AM

I was just wondering whether to use D3D_SHADER_MACRO or 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 bool as 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 Reedbeta

    DevMaster Staff

  • Administrators
  • 4974 posts
  • LocationBellevue, WA

Posted 19 January 2012 - 05:23 AM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 David Gallagher

    New Member

  • Members
  • PipPip
  • 33 posts

Posted 19 January 2012 - 08:46 AM

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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users