更换 Direct3D 11 着色器时管理状态的好方法是什么?
我目前有两个着色器,一个处理着色器(顶点和像素) - 用于计算照明和投影变换。然后将其渲染为纹理。然后我有第二个着色器,一个后处理着色器,它读取渲染的纹理并将其输出到屏幕(同样是顶点和像素着色器)。
将场景渲染为纹理后,我将立即上下文的顶点和像素着色器与后处理着色器交换,但我不确定应该如何管理状态(例如我的纹理参数和常量缓冲区)。交换着色器,然后每帧手动重置常量缓冲区和纹理两次似乎非常浪费,并且一开始就违背了常量缓冲区的要点,但据我所知,您无法在着色器对象上设置数据,它必须传递到上下文。
对于在换入和换出着色器时管理变量和纹理的相当简单且有效的方法,其他人有何建议?
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 managing variables and textures when swapping in and out shaders?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您没有从 D3D11 专家那里得到答案,因此我提供我有限的 D3D9 经验,无论其价值如何。
“似乎”是一个可疑的词。您测量过性能影响吗?每帧两次听起来还不错。
对于纹理,我根据用途分配纹理寄存器。我使用寄存器< s4 用于特定于材质的纹理,>= s4 用于渲染目标纹理,这些纹理仅在游戏开始时分配一次。例如,我的主场景着色器具有以下内容:
因此,我的粒子着色器具有缩减集,但 DepthMap 纹理仍位于同一插槽中...
我不知道其中有多少适用于 D3D11,但我希望这是有一定用处的。
Since you've had no answers from D3D11 experts, I offer my limited D3D9 experience, for what it's worth.
"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:
So my particle shader has a reduced set, but the DepthMap texture is still in the same slot...
I don't know how much of this is applicable to D3D11, but I hope it's of some use.