7

使用 DirectX 11,我创建了一个可以绑定为渲染目标的 3D 体积纹理:

D3D11_TEXTURE3D_DESC texDesc3d;
// ...
texDesc3d.Usage     = D3D11_USAGE_DEFAULT;
texDesc3d.BindFlags = D3D11_BIND_RENDER_TARGET;

// Create volume texture and views
m_dxDevice->CreateTexture3D(&texDesc3d, nullptr, &m_tex3d);
m_dxDevice->CreateRenderTargetView(m_tex3d, nullptr, &m_tex3dRTView);

我现在想更新整个渲染目标并用像素着色器中生成的程序数据填充它,类似于使用“全屏通道”更新 2D 渲染目标。生成数据所需的一切都是相关像素的 UVW 坐标。

对于 2D,可以构建一个简单的渲染全屏三角形的顶点着色器:

struct VS_OUTPUT
{
    float4 position : SV_Position;
    float2 uv: TexCoord;
};

// input: three empty vertices
VS_OUTPUT main( uint vertexID : SV_VertexID )
{
    VS_OUTPUT result;
    result.uv = float2((vertexID << 1) & 2, vertexID & 2);
    result.position = float4(result.uv * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f);
    return result;
}

我很难思考如何将这一原则用于 3D。这在 DirectX 11 中是否可行,或者我是否必须按照此处所述渲染到体积纹理的各个切片?

4

1 回答 1

3

这是一些使用管道版本执行的示例代码。您基本上批处理 N 个三角形并使用几何着色器将每个实例路由到一个体积切片。

struct VS_OUTPUT
{
    float4 position : SV_Position;
    float2 uv: TexCoord;
    uint index: SLICEINDEX;
};

VS_OUTPUT main( uint vertexID : SV_VertexID, uint ii : SV_InstanceID )
{
    VS_OUTPUT result;
    result.uv = float2((vertexID << 1) & 2, vertexID & 2);
    result.position = float4(result.uv * float2(2.0f, -2.0f) + float2(-1.0f, 1.0f), 0.0f, 1.0f);
    result.index= ii;
    return result;
}

现在您需要使用 3 个顶点和 N 个实例调用 DrawInstanced,其中 N 是您的体积切片数

然后将三角形分配给 GS,如下所示:

struct psInput
{
    float4 pos : SV_POSITION;
    float2 uv: TEXCOORD0;
uint index : SV_RenderTargetArrayIndex; //This will write your vertex to a specific slice, which you can read in pixel shader too
};

[maxvertexcount(3)] 
void GS( triangle VS_OUTPUT input[3], inout TriangleStream<psInput> gsout )
{       
psInput output;
for (uint i = 0; i < 3; i++)
{
    output.pos = input[i].pos;
    output.uv = input[i].uv;
    output.index= input[0].index; //Use 0 as we need to push a full triangle to the slice
    gsout.Append(output);
}
gsout.RestartStrip();
}

现在您可以访问像素着色器中的切片索引:

float4 PS(psInput input) : SV_Target
{
//Do something with uvs, and use slice input as Z
}

计算着色器版本(不要忘记为您的体积创建 UAV),这里的 numthreads 是完全任意的

[numthreads(8,8,8)]
void CS(uint3 tid : SV_DispatchThreadID)
{
     //Standard overflow safeguards

     //Generate data using tid coordinates
} 

现在你需要用 width/8, height/8, depth/8 调用 dispatch

于 2015-04-09T16:41:40.613 回答