简体   繁体   中英

DX11 Losing Instance Buffer Data

I've got a function that basically creates different instance buffers into an array for me to use in my DrawIndexedInstanced call.

But when I pass the vertex buffer and instance buffer through to my shader, my instance data is completely lost when the shader goes to use it, so none of my objects are being relocated and are thus all rendering in the same place.

I've been looking at this for hours and literally cannot find anything that is helpful.

Creating the Vertex shader input layout:

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 }
};

Creating an instance buffer (called multiple times per frame, to create all necessary buffers):

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));
}

The draw command:

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

The shader:

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;
}

I've used the graphics debugger built into Visual Studio. Initially it appeared to be assigning variables in the Vertex shader back to front, however removing APPEND_ALIGNED_ELEMENT from the AlignedByteOffset has fixed that, however the per-instance data seems to be corrupt and is not getting recieved.

If there is anything else you need let me know and I'll update the post as necessary.

The problem lies in your subresource data.

instData.pSysMem = &instances;

You are not specifying which offset to read the memory from. Try using

instData.pSysMem = &instances[0];

or

instData.pSysMem = &instances.at(0);

That clarifies where to start reading memory from and will hopefully fix your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM