I've been planning to refactor my shader library and put all possible constant buffers and textures in one .hlsli file and then include that file to every shader that uses a constant buffer or a texture or whatever.My question is - for instance if I have 20 constant buffers in the .hlsli file, but I onclude it in one pixel shader and that pixel shader references values from only one cbuffer and only one of the textures and one of the samplers, all the other ones will not get compiled, right?And I won't be uploading to them from the application, so it would be if they don't exist, right?
Does the HLSL compiler in Visual Studio 2012 remove unreferenced constant buffers and shader resources for sure?
Crossbones+ - Reputation: 2918
Posted 09 April 2013 - 01:41 PM
As far as I know, any unreferenced resource (texture, sampler, buffet etc) won't have any effect on the final shader. You may use some shader tools to verify this in the compiled shader code.
The cbuffers and resources defined inside the shader files are more like declarations that "this kind of data with this name may exist and it is formatted this way", so the shader file alone and the compiler don't create any of those resources for you.
Otherwise, the shader compiler will go through all the file and all the included files, so longer shader files you create, more time it will take to compile them. I think that the compiler parses all the functions (for syntax errors and such) even if the functions aren't used.
Crossbones+ - Reputation: 8327
Posted 09 April 2013 - 05:23 PM
I know for sure that if you use only a part of the cbuffer (i.e. the first 12 bytes out of 32), the compiler will still compile as if it were 32 (the full size).
However, I don't know about fully unused cbuffers. Granted, it will take more time to compile, which in bigger projects this can be a major problem, particularly for fast game iteration (as in development); which is very important. I wouldn't do it if I were you.
Moderators - Reputation: 17554
Posted 09 April 2013 - 07:26 PM
The compile will absolutely strip unused resources from the compiled shader. You can verify this very easily by checking the resulting assembly dumped from fxc.exe.
Constant buffers are either stripped entirely, or left intact. The compiler won't selectively strip out parts of the constant buffer that aren't used, because this would change the layout the buffer.
Crossbones+ - Reputation: 6355
Posted 09 April 2013 - 07:27 PM
The compiler will remove unused objects from the shader file. However, you should consider that each object will be located at a different register if there are different numbers of textures and constant buffers being bound (this assumes you don't explicitly set the register by hand). That really isn't too big of a deal, but you have to consider it anyways.
If you set your build chain up properly, then you should build your shader as soon as you save it to disk, and then just use the compiled shader at runtime. This is the strategy imposed on you by the Windows Store style apps, and it actually makes sense to follow this path. So in this case, it probably won't make to much of a difference if the compilation takes a bit longer to remove the unnecessary objects.
Jason Zink :: DirectX MVP
Direct3D 11 engine on CodePlex: Hieroglyph 3
Direct3D Books: Practical Rendering and Computation with Direct3D 11, Programming Vertex, Geometry, and Pixel Shaders
Articles: Dual-Paraboloid Mapping Article :: Parallax Occlusion Mapping Article (original):: Fast Silhouettes Article
Games: Lunar Rift
Crossbones+ - Reputation: 4332
Posted 10 April 2013 - 03:32 AM
you should consider that each object will be located at a different register if there are different numbers of textures and constant buffers being bound
True, the compiler will optimize unused resources away, and re-index the used slots. If you manually bind a resource to a slot (e.g.: <float> texture : register(t)), the shader is guaranteed to use that specific slot/register index.
Edited by eppo, 10 April 2013 - 03:33 AM.