开发者

What is a good way to manage state when swapping out Direct3D 11 shaders?

I currently have two shaders, a processing shader (both vertex and pixel) - which calculates lighting and projection transformations. This is then rendered to a texture. I then have my second shader, a postprocessing shader, which reads the rendered texture and outputs it to the screen (again both vertex and pixel shaders).

Once I've rendered my scene to a texture I swap the Immediate Context's Vertex and Pixel shaders with my postprocessing ones, but I'm not sure how I should manage the state (e.g. my texture parameters and my constant buffers). Swapping shaders and then manually resetting the constant buffers and textures twice each frame seems incredibly wasteful, and kind of defeats the point of constant buffers in the first place, but as far as I can see you can't set the data on the shader object, it has to be passed to the context.

What do other people suggest for fairly simple and efficient ways of ma开发者_如何学Pythonnaging variables and textures when swapping in and out shaders?


Since you've had no answers from D3D11 experts, I offer my limited D3D9 experience, for what it's worth.

... twice each frame seems incredibly wasteful ...

"Seems" is a suspicious word there. Have you measured the performance hit? Twice per frame doesn't sound too bad.

For textures, I assign my texture registers according to purpose. I use registers < s4 for material-specific textures, and >= s4 for render target textures which are assigned only once at the beginning of the game. So for example my main scene shader has the following:

sampler2D DiffuseMap : register(s0);
sampler2D DetailMap : register(s1);

sampler2D DepthMap : register(s4);
sampler2D ShadowMap : register(s5);
sampler2D LightMap : register(s6);
sampler2D PrevFrame : register(s7);

So my particle shader has a reduced set, but the DepthMap texture is still in the same slot...

sampler2D Tex : register(s0);
sampler2D DepthMap : register(s4);

I don't know how much of this is applicable to D3D11, but I hope it's of some use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜