在DirectX 11中通过一遍渲染为多个纹理 [英] Rendering to multiple textures with one pass in directx 11

查看:547
本文介绍了在DirectX 11中通过一遍渲染为多个纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C ++ Directx 11 SDK一次渲染到两个纹理.我希望一个纹理包含结果图像每个像素的颜色(渲染3D场景时通常在屏幕上看到的颜色),另一个纹理包含每个像素和深度的法线(法线3个浮点,1个浮点)深度).现在,我能想到的是创建两个渲染目标,并将第一遍渲染为颜色,第二遍将法线和深度分别传递给每个渲染目标.但是,这似乎很浪费时间,因为我可以在第一遍中获得每个像素的颜色,法线和深度的信息.那么有没有办法用像素着色器以某种方式输出两个纹理?

I'm trying to render to two textures with one pass using C++ directx 11 SDK. I want one texture to contain the color of each pixel of the result image (what I normally see on the screen when rendering a 3D scene), and another texture to contain the normal of each pixel and depth (3 float for normal and 1 float for depth). Right now, what I can think of is to create two rendering targets and render the first pass as the colors and the second pass the normals and depth to each rendering target respectively. However, this seems a waste of time because I can get the information of each pixel's color, normal, and depth in the first pass. So is there a way to somehow output two textures with the pixel shader?

任何帮助将不胜感激.

P.S.我在考虑像素着色器中的RWTexture2D或RWStructuredBuffer.一些背景知识:我将需要两张图像在计算着色器中进行进一步处理.这带来了一个同步的副问题:由于像素着色器(与计算着色器不同)一次写入每个像素,我如何知道像素着色器何时完成并告诉计算着色器开始图像后处理?/p>

P.S. I'm thinking something along the lines of RWTexture2D or RWStructuredBuffer in the pixel shader. A little background: I will need the two images for further processing in the compute shader. Which brings up a side question of synchronization: since the pixel shader (unlike the compute shader) writes each pixel one at a time, how would I know when the pixel shader is finished and tell the compute shader to start image post-processing?

推荐答案

您需要使用MRT(多个渲染目标)来一次性渲染.

You need to use MRT (Multiple Render Targets) to render this in one pass.

您可以使用OMSetRenderTargets将两个目标绑定为输出

You can bind both targets as output using OMSetRenderTargets

http://msdn.microsoft.com/zh-CN/library/windows/desktop/ff476464(v=vs.85).aspx

http://hieroglyph3.codeplex.com/(DefferedRendering)中有一个示例,然后显示如何一次写入两个纹理.

There's an example in http://hieroglyph3.codeplex.com/ (DefferedRendering) which then shows how to write to both textures at once.

这是一个小样本:

ID3D11DeviceContext* deviceContext; //Your immediate context

ID3D11RenderTargetView* m_pRenderViews[2]; //Not more than D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT (8)
m_pRenderViews[0] = pRTV1; //First target
m_pRenderViews[1] = pRTV2; //second target

deviceContext->OMSetRenderTargets(2, &m_pRenderViews[0], NULL); //NULL means no depth stencil attached

然后,您的像素着色器将需要输出结构而不是单色:

Then your pixel shader will need to output a structure instead of a single color:

struct PS_OUTPUT
{
    float4 Color: SV_Target0;
    float4 Normal: SV_Target1;
};

PS_OUTPUT PS(float4 p: SV_Position, float2 uv : TEXCOORD0)
{
      PS_OUTPUT output;
      output.Color = //Set first output
      output.Normal= //Set second output
      return output;
}

在DirectX11中,您也不需要向常规缓冲区写入深度,只需使用深度缓冲区即可.

Also in DirectX11 you shouldn't need to write depth to your normal buffer, you can just use the depth buffer.

对于Pixel/Compute着色器同步,不能在同一设备上同时运行像素着色器和计算着色器,因此,当绘制调用完成时,纹理可以用于计算中以进行分发.

For Pixel/Compute shader sync, you can't run a pixel shader and a compute shader at the same time on the same device, so when your draw calls are finished, the textures are ready to use in compute for dispatch.

这篇关于在DirectX 11中通过一遍渲染为多个纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆