Sign in to follow this  
Sock5

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

Recommended Posts

Sock5    162

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?

Share this post


Link to post
Share on other sites
kauna    2922

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!

Share this post


Link to post
Share on other sites
Matias Goldberg    9581
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.

Share this post


Link to post
Share on other sites
MJP    19786

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.

Share this post


Link to post
Share on other sites
Jason Z    6436

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.

Share this post


Link to post
Share on other sites
eppo    4877

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

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