Directx10,实例渲染,顶点缓冲输入

时间:2012-02-02 17:06:43

标签: c++ directx hlsl vertex-buffer

所以,我试图用一个简单的着色器在DirectX10中进行硬件实例化,其中顶点结构采用位置,纹理坐标和顶点的法线以及实例的模型矩阵。问题是创建输入布局,将数据发送到着色器中的模型矩阵。使用PIX进行调试表明矩阵只能获得无意义的值。

相关代码:

输入布局

m_Tech = m_Effect->GetTechniqueByName("SimpleTech");

//Position-normal-texture vertex
D3D10_INPUT_ELEMENT_DESC posNormalTexVertexDesc[] =
{
    {"POSITION",    0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0,                            D3D10_INPUT_PER_VERTEX_DATA,   0},
    {"NORMAL",      0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA,   0},
    {"TEXCOORD",    0, DXGI_FORMAT_R32G32_FLOAT,       0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA,   0},
    {"mTransform",  0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0,                            D3D10_INPUT_PER_INSTANCE_DATA, 1},
    {"mTransform",  1, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_INSTANCE_DATA, 1},
    {"mTransform",  2, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_INSTANCE_DATA, 1},
    {"mTransform",  3, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_INSTANCE_DATA, 1}
};
D3D10_PASS_DESC PassDesc;

m_Effect->GetTechniqueByName("SimpleTech")->GetPassByIndex(0)->GetDesc(&PassDesc);
HR(mp_D3DDevice->CreateInputLayout(posNormalTexVertexDesc, 7, PassDesc.pIAInputSignature,
    PassDesc.IAInputSignatureSize, &m_InputLayout));

Shader struct

struct VS_IN
{
    float4 Pos : POSITION;
    float2 TexCoord : TEXCOORD0;
    float4 Normal : NORMAL;
    row_major float4x4 mTransform : mTransform;
};

缓冲器

void Model::BuildVertexBuffer()
{
    D3D10_BUFFER_DESC vbd;
    vbd.Usage = D3D10_USAGE_DEFAULT;
    vbd.ByteWidth = sizeof(MeshVertex) * (UINT)m_Mesh->VertexCount();
    vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    vbd.CPUAccessFlags = 0;
    vbd.MiscFlags = 0;
    D3D10_SUBRESOURCE_DATA vinitData;
    vinitData.pSysMem = m_Mesh->VertexData();
    m_D3DDevice->CreateBuffer(&vbd, &vinitData, &m_VertexBuffer);
}

void Model::BuildInstanceBuffer()
{
    D3D10_BUFFER_DESC ibd;
    ibd.Usage = D3D10_USAGE_DEFAULT;
    ibd.ByteWidth = sizeof(D3DXMATRIX) * (UINT)m_instanceData->size();
    ibd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    ibd.CPUAccessFlags = 0;
    ibd.MiscFlags = 0;
    D3D10_SUBRESOURCE_DATA vinitData;
    vinitData.pSysMem = &m_instanceData[0];
    m_D3DDevice->CreateBuffer(&ibd, &vinitData, &m_InstanceBuffer);
}

绘制电话

void Model::Draw(Camera& camera)
{
    UINT strides[2];
    UINT offsets[2];
    ID3D10Buffer* bufferPointers[2];

    //Set buffer strides
    strides[0] = sizeof(MeshVertex);
    strides[1] = sizeof(D3DXMATRIX);

    //Set buffer offsets
    offsets[0] = 0;
    offsets[1] = 0;

    //Set the array of pointers to the vertex and instance buffers
    bufferPointers[0] = m_VertexBuffer;
    bufferPointers[1] = m_InstanceBuffer;

    //Set the vertex and instance buffers to active in the input assembler so it may be rendered
    m_D3DDevice->IASetVertexBuffers(0,2,bufferPointers, strides, offsets);
    m_D3DDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

    //Set input layout
    m_D3DDevice->IASetInputLayout(m_Effect->GetLayout());

    //Set
    m_Effect->SetResources(camera, NULL);

    D3D10_TECHNIQUE_DESC techDesc;
    ID3D10EffectTechnique* tech = m_Effect->GetTech();
    tech->GetDesc( &techDesc );

    for(UINT i = 0; i < techDesc.Passes; ++i)
    {
        ID3D10EffectPass* pass = tech->GetPassByIndex(i);
        pass->Apply(0);

        m_D3DDevice->DrawInstanced(m_Mesh->VertexCount(), m_instanceData->size(),0,0);
    }   
}

m_instanceData是一个向每个实例保存D3DXMATRIX的向量。看过SDK附带的示例,我们的输入布局似乎看起来一样,但矩阵没有正确传输?

我目前只是发送一个单位矩阵作为模型矩阵用于测试目的,但它显示为完全不同的东西。我已经检查过,一旦初始化实例缓冲区,数据就在向量中。

EDIT1: 将BuildInstanceBuffer()方法更改为

void Model::BuildInstanceBuffer()
{
    D3D10_BUFFER_DESC ibd;
    ibd.Usage = D3D10_USAGE_DYNAMIC;
    ibd.ByteWidth = sizeof(D3DXMATRIX) * (UINT)m_instanceData->size();
    ibd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    ibd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
    ibd.MiscFlags = 0;
    D3D10_SUBRESOURCE_DATA vinitData;
    vinitData.pSysMem = &m_instanceData[0];
    m_D3DDevice->CreateBuffer(&ibd, &vinitData, &m_InstanceBuffer);

    D3DXMATRIX* pMatrices = NULL;
    m_InstanceBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, ( void** )&pMatrices );

    memcpy( pMatrices, m_InstanceBuffer, m_instanceData->size() * sizeof( D3DXMATRIX ) );

    m_InstanceBuffer->Unmap();

}

第二次编辑:猜猜有点早熟。似乎我的模型矩阵在着色器中被“正确”填充,因为每一行在正确的点获得一个值。但这个价值仍然不是我传递的价值。

这一点真的让人感到陌生。当我在visual studio中调试程序时,我得到一个绿色网格。在PIX中,它以黑色显示。我真的可以在这个上使用帮助,我不知道现在发生了什么。

1 个答案:

答案 0 :(得分:0)

最好迟到然后我猜,以防其他人偶然发现这个问题。 将BuildInstanceBuffer更改为:

void Model::BuildInstanceBuffer()
{
    D3D10_BUFFER_DESC ibd;
    ibd.Usage = D3D10_USAGE_DYNAMIC;
    ibd.ByteWidth = sizeof(D3DXMATRIX) * (UINT)m_instanceData->size();
    ibd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    ibd.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
    ibd.MiscFlags = 0;

    m_D3DDevice->CreateBuffer(&ibd, NULL, &m_InstanceBuffer);
    D3DXMATRIX* pMatrices = NULL;
    m_InstanceBuffer->Map( D3D10_MAP_WRITE_DISCARD, NULL, ( void** )&pMatrices );
    memcpy( pMatrices, m_instanceData->data(), m_instanceData->size() * sizeof( D3DXMATRIX ) );
    m_InstanceBuffer->Unmap();
}

基本上,那里的小菜一碟。