DX11丢失实例缓冲区数据

时间:2015-11-23 04:51:31

标签: c++ directx-11 hlsl

我有一个函数,它基本上为数组创建了不同的实例缓冲区供我在DrawIndexedInstanced调用中使用。

但是当我将顶点缓冲区和实例缓冲区传递给我的着色器时,当着色器使用它时,我的实例数据完全丢失,因此我的所有对象都没有被重新定位,因此都在同一个地方渲染。

我已经看了好几个小时,实际上找不到任何有用的东西。

创建顶点着色器输入布局:

D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
{
    //Vertex Buffer
    { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
    { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },

    //Instance buffer
    { "INSTANCEPOS", 0, DXGI_FORMAT_R32G32B32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
    { "INSTANCEROT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 1, 12, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
    { "INSTANCESCA", 0, DXGI_FORMAT_R32G32B32_FLOAT, 1, 24, D3D11_INPUT_PER_INSTANCE_DATA, 1 },
    { "INSTANCETEX", 0, DXGI_FORMAT_R32_FLOAT, 1, 36, D3D11_INPUT_PER_INSTANCE_DATA, 1 }
};

创建实例缓冲区(每帧多次调用,以创建所有必需的缓冲区):

void GameManager::CreateInstanceBuffer(ID3D11Buffer** buffer, Mesh* mesh, std::vector<Instance> instances)
{

    D3D11_BUFFER_DESC instBuffDesc;
    ZeroMemory(&instBuffDesc, sizeof(instBuffDesc));

    instBuffDesc.Usage = D3D11_USAGE_DEFAULT;
    instBuffDesc.ByteWidth = sizeof(Instance) * instances.size();
    instBuffDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    instBuffDesc.CPUAccessFlags = 0;
    instBuffDesc.MiscFlags = 0;
    instBuffDesc.StructureByteStride = 0;

    int i = sizeof(Instance);

    D3D11_SUBRESOURCE_DATA instData;
    ZeroMemory(&instData, sizeof(instData));

    instData.pSysMem = &instances;
    instData.SysMemPitch = 0;
    instData.SysMemSlicePitch = 0;

    CheckFailWithError(dxManager.GetDevice()->CreateBuffer(&instBuffDesc, &instData, buffer),
        "An error occurred whilst building an instance buffer",
        "[GameManager]");


    meshBuffers.push_back(mesh->GetBuffer(VERTEX_BUFFER));
}

绘制命令:

dxManager.GetContext()->DrawIndexedInstanced(instanceIndexCounts[buffer], instanceCounts[buffer], 0, 0, 0);

着色器:

cbuffer cbChangesEveryFrame : register(b0)
{
    matrix worldMatrix;
};
cbuffer cbNeverChanges : register(b1)
{
    matrix viewMatrix;
};
cbuffer cbChangeOnResize : register(b2)
{
    matrix projMatrix;
};

struct VS_Input
{
    float4 pos : POSITION;
    float2 tex0 : TEXCOORD0;

    float4 instancePos : INSTANCEPOS;
    float4 instanceRot : INSTANCEROT;
    float4 instanceSca : INSTANCESCA;
    float instanceTex : INSTANCETEX;
};

PS_Input VS_Main(VS_Input vertex)
{
    PS_Input vsOut = (PS_Input)0;
    vsOut.pos = mul(vertex.pos + vertex.instancePos, worldMatrix);
    vsOut.pos = mul(vsOut.pos, viewMatrix);
    vsOut.pos = mul(vsOut.pos, projMatrix);
    vsOut.tex0 = vertex.tex0;
    return vsOut;
}

我使用了Visual Studio内置的图形调试器。最初它似乎是在顶点着色器中将变量分配回到前面,但是从APPEND_ALIGNED_ELEMENT中删除AlignedByteOffset已经修复了这个问题,但是每个实例的数据似乎已损坏并且没有收到。< / p>

如果您还有其他任何需要通知我,我会根据需要更新帖子。

0 个答案:

没有答案