4
$\begingroup$

I'm playing around with directX 12 and I'm having some problems loading geometry in. I have a std::vector vertices definition and I do exactly what the dx12 template does with it.

  const UINT vertexBufferSize = sizeof(vertices);

  // Create the vertex buffer resource in the GPU's default heap and copy vertex data into it using the upload heap.
  // The upload resource must not be released until after the GPU has finished using it.
  Microsoft::WRL::ComPtr<ID3D12Resource> vertexBufferUpload;

  CD3DX12_HEAP_PROPERTIES defaultHeapProperties(D3D12_HEAP_TYPE_DEFAULT);
  CD3DX12_RESOURCE_DESC vertexBufferDesc = CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize);
  DX::ThrowIfFailed(d3dDevice->CreateCommittedResource(
     &defaultHeapProperties,
     D3D12_HEAP_FLAG_NONE,
     &vertexBufferDesc,
     D3D12_RESOURCE_STATE_COPY_DEST,
     nullptr,
     IID_PPV_ARGS(&mVertexBuffer)));

  CD3DX12_HEAP_PROPERTIES uploadHeapProperties(D3D12_HEAP_TYPE_UPLOAD);
  DX::ThrowIfFailed(d3dDevice->CreateCommittedResource(
     &uploadHeapProperties,
     D3D12_HEAP_FLAG_NONE,
     &vertexBufferDesc,
     D3D12_RESOURCE_STATE_GENERIC_READ,
     nullptr,
     IID_PPV_ARGS(&vertexBufferUpload)));

  mVertexBuffer->SetName(L"Vertex Buffer Resource");
  vertexBufferUpload->SetName(L"Vertex Buffer Upload Resource");

  // Upload the vertex buffer to the GPU.
  {
     D3D12_SUBRESOURCE_DATA vertexData = {};
     vertexData.pData = reinterpret_cast<BYTE*>(vertices.data());
     vertexData.RowPitch = vertexBufferSize;
     vertexData.SlicePitch = vertexData.RowPitch;

     UpdateSubresources(mCommandList.Get(), mVertexBuffer.Get(), vertexBufferUpload.Get(), 0, 0, 1, &vertexData);

     CD3DX12_RESOURCE_BARRIER vertexBufferResourceBarrier =
        CD3DX12_RESOURCE_BARRIER::Transition(mVertexBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
     mCommandList->ResourceBarrier(1, &vertexBufferResourceBarrier);
  }

But this is what I get in the graphics debugger when it was suppossed to be a sphere. I can confirm that the data in vertices is correct. enter image description here

If I change vertices to be what the template code had it works and renders the default cube. The only real difference mine is using a vector instead of an array.

VertexPositionColor cubeVertices[] =
{
    { XMFLOAT3(-0.5f, -0.5f, -0.5f), XMFLOAT3(0.0f, 0.0f, 0.0f) },
    { XMFLOAT3(-0.5f, -0.5f,  0.5f), XMFLOAT3(0.0f, 0.0f, 1.0f) },
    { XMFLOAT3(-0.5f,  0.5f, -0.5f), XMFLOAT3(0.0f, 1.0f, 0.0f) },
    { XMFLOAT3(-0.5f,  0.5f,  0.5f), XMFLOAT3(0.0f, 1.0f, 1.0f) },
    { XMFLOAT3(0.5f, -0.5f, -0.5f), XMFLOAT3(1.0f, 0.0f, 0.0f) },
    { XMFLOAT3(0.5f, -0.5f,  0.5f), XMFLOAT3(1.0f, 0.0f, 1.0f) },
    { XMFLOAT3(0.5f,  0.5f, -0.5f), XMFLOAT3(1.0f, 1.0f, 0.0f) },
    { XMFLOAT3(0.5f,  0.5f,  0.5f), XMFLOAT3(1.0f, 1.0f, 1.0f) },
};
$\endgroup$
5
$\begingroup$

Vector has a few different semantics from static arrays. For one it's a struct containing a pointer a capacity and a length (at the very least). That means that sizeof will not reflect how much data is actually stored in there (that only works on static arrays).

If you need to get the size in bytes of the data then you need

const UINT vertexBufferSize = sizeof(vertices::value_type) * vertices.size();
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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