[英]DirectX 11: how to define input layout when using more than one vertex buffer?
使用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;
}
// ********** 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 );
我还尝试将layout元素定义为一个数组,在两个不同的插槽中,但是它也不起作用。 在那种情况下,我应该使用什么着色器?
我所能得到的只是一个空白的屏幕,而且我陷入了困境……如果有人可以帮助,请先谢谢。
一次只能在设备上下文上设置一个输入布局和一个顶点着色器。 每次调用IASetInputLayout
和VSSetShader
,以前的布局/着色器都会被新的替换。 将两个输入布局结构合并为一个结构,并将HLSL顶点着色器合并为一个着色器。 所述InputSlot
的场构件D3D11_INPUT_ELEMENT_DESC
将决定哪些顶点缓冲器用于该元素。
就像是:
VS_OUTPUT VS( float4 Pos : POSITION, float4 Color : COLOR) { VS_OUTPUT output = (VS_OUTPUT)0; output.Pos = Pos; output.Color = Color; return output; }
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 },
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.