2
\$\begingroup\$

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

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.

\$\endgroup\$

closed as off-topic by Lars Viklund, MAnd, Josh Feb 26 '16 at 16:43

  • This question does not appear to be about game development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ I'm voting to close this question as it's a duplicate of stackoverflow.com/questions/33863911/… \$\endgroup\$ – Lars Viklund Feb 26 '16 at 10:00
  • \$\begingroup\$ They're both my questions. I asked it in multiple places and have already directed anyone hitting this page to that URL. See comment to answer below. Its more relevant on this site than stack overflow, but the answer was given on stack overflow, so both questions are valid. \$\endgroup\$ – Zach Ross-Clyne Feb 26 '16 at 16:23
  • \$\begingroup\$ I'm voting to close this question as off-topic because it is a cross-post. Please don't cross-post questions across multiple SE sites. \$\endgroup\$ – Josh Feb 26 '16 at 16:43
  • \$\begingroup\$ @ZachRoss-Clyne Yes, your comment on the answer prompted me to close the question properly to avoid it popping up in the feed. Housekeeping, nothing personal. \$\endgroup\$ – Lars Viklund Feb 26 '16 at 20:54
0
\$\begingroup\$

This is how I do it, vb.net and sharpDX but u should be able to port it over. I also found that I hade to put the sort the elements but there size eg.

vect4 vect3 vect2 float

    Dim elements As InputElement() = New InputElement() {New InputElement("POSITION", 0, DXGI.Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
                                                              New InputElement("NORMAL", 0, DXGI.Format.R32G32B32_Float, InputElement.AppendAligned, 0, InputClassification.PerVertexData, 0),
                                                               New InputElement("BINORMAL", 0, DXGI.Format.R32G32B32_Float, InputElement.AppendAligned, 0, InputClassification.PerVertexData, 0),
                                                               New InputElement("TANGENT", 0, DXGI.Format.R32G32B32_Float, InputElement.AppendAligned, 0, InputClassification.PerVertexData, 0),
                                                                New InputElement("COLOR", 0, DXGI.Format.R32G32B32_Float, InputElement.AppendAligned, 0, InputClassification.PerVertexData, 0),
                                                                New InputElement("TEXCOORD", 0, DXGI.Format.R32G32_Float, InputElement.AppendAligned, 0, InputClassification.PerVertexData, 0),
                                                                 New InputElement("WORLD", 0, DXGI.Format.R32G32B32A32_Float, 0, 1, InputClassification.PerInstanceData, 1),
                                                                 New InputElement("WORLD", 1, DXGI.Format.R32G32B32A32_Float, InputElement.AppendAligned, 1, InputClassification.PerInstanceData, 1),
                                                                 New InputElement("WORLD", 2, DXGI.Format.R32G32B32A32_Float, InputElement.AppendAligned, 1, InputClassification.PerInstanceData, 1),
                                                                 New InputElement("WORLD", 3, DXGI.Format.R32G32B32A32_Float, InputElement.AppendAligned, 1, InputClassification.PerInstanceData, 1)
                                                         }
\$\endgroup\$
  • \$\begingroup\$ Hi Sorry, I've actually solved this issue, I posted this question on both SO and GameDev to try to expand my reach and thankfully someone replied on SO... Here is the question for anyone else who would like the answer stackoverflow.com/questions/33863911/… \$\endgroup\$ – Zach Ross-Clyne Nov 29 '15 at 0:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.