DirectX 11:如何在使用多个顶点缓冲区时定义输入布局?

0 c++ directx

使用 Direct3D 11,我希望能够为位置和颜色使用单独的顶点缓冲区。当位置在运行时很少改变时,我想这样做,而颜色可能会经常改变。该主题并不新鲜,但我找不到正确处理许多顶点缓冲区/着色器的综合答案。如果我对如何创建和设置顶点缓冲区非常有信心,我不确定如何定义布局。为了进行实验,我从 MSDN 教程开始,该教程仅使用一个顶点着色器绘制单个三角形。我将着色器分成两部分,位置和颜色。

以下是我的 HLSL 和 C++ 代码的主要内容。

// ******着色器文件*****

struct VS_OUTPUT
{
    float4 Pos : SV_POSITION;
    float4 Color : COLOR0;
};

// Position shader
VS_OUTPUT VPS( float4 Pos : POSITION )
{
    VS_OUTPUT output = (VS_OUTPUT)0;
    output.Pos = Pos;
    return output;
}

// Color shader
VS_OUTPUT VCS( float4 Color : COLOR )
{
    VS_OUTPUT output = (VS_OUTPUT)0;
    output.Color = Color;
    return output;
}

// Pixel shader
float4 PS( VS_OUTPUT input ) : SV_Target
{
    return input.Color;
}
Run Code Online (Sandbox Code Playgroud)

// ********** C++ 文件摘要**********

// Position shader
ID3DBlob* pVPSBlob
ID3D11VertexShader* g_pVertexShader

// Color shader
ID3DBlob* pVCSBlob
ID3D11VertexShader* g_pVertexColorShader

// Pixel shader
ID3DBlob* pPSBlob
ID3D11InputLayout* g_pVertexLayout

// Vertex buffers
ID3D11Buffer* g_pVertexBuffer = nullptr;
ID3D11Buffer* g_pVertexColorBuffer = nullptr;

// I compile and create the shaders with:
D3DCompileFromFile(), CreateVertexShader()



// I define the input vertex position layout in slot 0
D3D11_INPUT_ELEMENT_DESC positionLayout[] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
UINT numElements = ARRAYSIZE( positionLayout );

// Create the input vertex layout
hr = g_pd3dDevice->CreateInputLayout( positionLayout, numElements, pVPSBlob->GetBufferPointer(),
                                      pVPSBlob->GetBufferSize(), &g_pVertexLayout );

// Set the input vertex layout
g_pImmediateContext->IASetInputLayout( g_pVertexLayout );


 // I define the input color layout in slot 1
D3D11_INPUT_ELEMENT_DESC colorsLayout[] =
{
    { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
numElements = ARRAYSIZE( colorsLayout );

// Create the input colors layout
hr = g_pd3dDevice->CreateInputLayout( colorsLayout, numElements, pVCSBlob->GetBufferPointer(),
                                      pVCSBlob->GetBufferSize(), &g_pVertexColorLayout );

// Set the input color layout
g_pImmediateContext->IASetInputLayout( g_pVertexColorLayout );

// I set the 3 shaders:
g_pImmediateContext->VSSetShader( g_pVertexShader, nullptr, 0 );
g_pImmediateContext->VSSetShader( g_pVertexColorShader, nullptr, 0 );
g_pImmediateContext->PSSetShader( g_pPixelShader, nullptr, 0 );


// I create 2 vertex buffers for positions and colors and set them

// Set the positions & colors vertex buffers
ID3D11Buffer *vertexBuffers[] = { g_pVertexBuffer, g_pVertexColorBuffer };
UINT strides[] = { sizeof( SimpleVertex ), sizeof( ColorVertex ) };
UINT offsets[] = { 0, 0};
g_pImmediateContext->IASetVertexBuffers( 0, 2, vertexBuffers, strides, offsets );
Run Code Online (Sandbox Code Playgroud)

我还尝试将布局元素定义为单个数组,位于两个不同的插槽中,但它也不起作用。在这种情况下,我应该使用什么着色器?

我所能得到的只是一个空屏幕,我被卡住了......如果有人可以提供帮助,请提前致谢。

Ros*_*dge 6

您一次只能在设备上下文中设置一个输入布局和一个顶点着色器。每次调用时IASetInputLayoutVSSetShader之前的布局/着色器都会替换为新的布局/着色器。将两个输入布局结构合并为一个结构,将 HLSL 顶点着色器合并为一个着色器。的InputSlot字段成员D3D11_INPUT_ELEMENT_DESC将确定哪个顶点缓冲区用于该元素。

就像是:

VS_OUTPUT VS( float4 Pos : POSITION, float4 Color : COLOR)
{
    VS_OUTPUT 输出 = (VS_OUTPUT)0;
    output.Pos = Pos;
    output.Color = 颜色;
    返回输出;
}
D3D11_INPUT_ELEMENT_DESC positionLayout[] =
{
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
Run Code Online (Sandbox Code Playgroud)