Each time I render, I loop through several textures and draw them in a back-to-front order by, for each of them:
1) Calling PSSetShaderResources() to update the shader's single Texture2D resource to the next ShaderResourceView
2) Calling DrawIndex() on the appropriate vertex buffer segment.
I have a simple pixel shader that is really just one basic texture sampling line:
ps.color = myOneTexture2D.Sample(samLinear, IN.texcoord0);
I've enabled alpha blending so that the alpha channel controls transparency, and everything is fine with that, except:
The Phantom Lingers.
Which is to say that each sampled texture is for some reason alpha-blended with the previously sampled texture.
So for totally opaque images, it's fine. For the first texture drawn -- such as this opaque double jellyfish I've drawn onto the background -- it's fine:
texture1.jpg 192.22KB 50 downloads
But the second texture (which I've made 50% transparent) drawn onto the scene shows the first texture (the double jellyfish) blended with it. Behind it, you can see a piece of the large background jellyfish previously drawn, which is the only thing that should be showing behind Snidely Whiplash.
texture2.jpg 61.1KB 47 downloads
A third texture showing fog (also set to 50% transparency) has that entire previous texture (Snidely + the first texture) mixed with it. It again shows the background portion behind it (though you can't see it well here), like I want it to, but also the previous texture, like I don't want it to.
texture3.jpg 44.65KB 42 downloads
So can any kind soul take a stab at telling me what's going on, here?
If I create multiple Texture2D resources in the shader, and have a separate one for each texture, then it works fine. The problem is, I want to be able to draw dozens of textures each time I render the scene, and the shader/my hardware doesn't support having, say, 50 different textures with a long sequence of "if (index == 3) texture.Sample..." etc.
I can't use TextureArrays because the textures have different dimensions.
I'm sure I'm missing something fundamental in the way shaders and samplers work, but for the life of me I haven't been able to discover it over days of research and experimentation. I know it's happening before the output merger stage, but I can't figure out why the Texture2D resource is apparently not being completely overriden by PSSetShaderResource(), like one would expect, and is instead somehow alpha-blended with it. Or the previous incarnation lingers, and is blended when Sampled? I can't tell, but perhaps one of you generous individuals can!
Many thanks for any insight you can offer.