Jump to content

  • Log In with Google      Sign In   
  • Create Account

Does the HLSL compiler in Visual Studio 2012 remove unreferenced constant buffers and shader resources for sure?


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
5 replies to this topic

#1 Sock5   Members   -  Reputation: 162

Like
0Likes
Like

Posted 09 April 2013 - 11:39 AM

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?


>removed<


Sponsor:

#2 kauna   Crossbones+   -  Reputation: 2852

Like
0Likes
Like

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. 

 

 

Cheers!



#3 Matias Goldberg   Crossbones+   -  Reputation: 3696

Like
0Likes
Like

Posted 09 April 2013 - 05:23 PM

It's a touchy subject. There's a lot the HLSL compiler doesn't know that you would assume it should.

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.

#4 MJP   Moderators   -  Reputation: 11751

Like
2Likes
Like

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.



#5 Jason Z   Crossbones+   -  Reputation: 5308

Like
1Likes
Like

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.



#6 eppo   Crossbones+   -  Reputation: 2621

Like
0Likes
Like

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[4])), the shader is guaranteed to use that specific slot/register index.


Edited by eppo, 10 April 2013 - 03:33 AM.





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