Stop HLSL from optimizing out unused textures

Started by
10 comments, last by L. Spiro 8 years, 6 months ago

Say I have a shader that uses 4 textures as input. For debugging purposes, I just want to check what's in the 4th texture, so I comment out the majority of the shader and just sample that one. I get all black, because apparently the shader has optimized that texture to slot t0 instead of t3 where i declared it.

I'm compiling with D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION, and yet this optimization still happens. Is there a way to disable it, for debug builds? It's not a problem I can't work around, but it's a nuisance.

Advertisement


I get all black, because apparently the shader has optimized that texture to slot t0 instead of t3 where i declared it.
That's a different issue to the compiler optimizing out unused resources... Are you saying that it's changed the register assignments of your resources?

What does your code look like where you declare the textures?

TextureCube tex1 : register(t0);
Texture2D tex2 : register(t1);
Texture2D tex3 : register(t2);
TextureCube tex4 : register(t3);
If I don't sample from tex1-3, and only sample tex4, I get all black.
I've never come across this but just wondered, if you change your commented shader to use t0 for the texture, does it then work?

it does not. I'm still assigning 4 textures on the CPU side, and regardless of if I put t0 or t3, it samples from the first-assigned cubemap, not the 4th.

It's probably easier if you just sample all the textures, and just throw away the results smile.png Maybe use them in the output but multiplied by 0 or whatever it takes for the compiler not to optimize them out. Don't know if that will work though, depends on how smart the compiler is smile.png

Otherwise, move the t3 texture to t0, and of course reflect that change in the texture assigning....

Edit: Still is a bit weird though, isn't that the purpose of register() to have control over which slot to use regardless of any optimization?

.:vinterberg:.


and regardless of if I put t0 or t3, it samples from the first-assigned cubemap


I get all black

This is... odd. Is the first bound cubemap actually black, or is there some calculation going on that would turn black if you enter a texture with different content than what it would expect? Because you shouldn't really get black in any other case if it really samples just from the first cubemap. So try to, instead of executing the rest of the shader, just output the actual content of the t3 declared cubemap and see if it matches your t0-bound texture first.

I have never seen such behavior from the shader compiler, which makes me very skeptical that the compiler is actually re-assigning it to register 0. You can check this pretty easily by using fxc.exe to dump the disassembly, which includes the register assignments for shader resources. Alternatively, you can also use a program like RenderDoc to capture a frame and then inspect the disassembly. If you do this, you can also easily confirm that you are in fact binding your shader resource view to the correct slot.

If all else fails, try running with the reference device. If it works correctly with the reference device, then you may have hit a driver bug.

Have you tried using something like PIX or RenderDoc to see what is actually set in the pipeline during runtime.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Say I have a shader that uses 4 textures as input. For debugging purposes, I just want to check what's in the 4th texture, so I comment out the majority of the shader and just sample that one. I get all black, because apparently the shader has optimized that texture to slot t0 instead of t3 where i declared it.

I'm compiling with D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION, and yet this optimization still happens. Is there a way to disable it, for debug builds? It's not a problem I can't work around, but it's a nuisance.

What dx are you targeting?

This topic is closed to new replies.

Advertisement