<p>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 <code>sizeof</code> will not reflect how much data is actually stored in there (that only works on static arrays).</p>

<p>If you need to get the size in bytes of the data then you need</p>

<pre><code>const UINT vertexBufferSize = sizeof(vertices::value_type) * vertices.size();
</code></pre>

CC BY-SA 3.0
2015-10-24T03:58:23.510Z
2016-01-16T16:32:24.677Z
2016-01-16T16:32:24.677Z
ratchet freak
<p>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.</p>

<pre><code>  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&lt;ID3D12Resource&gt; vertexBufferUpload;

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

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

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

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

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

     CD3DX12_RESOURCE_BARRIER vertexBufferResourceBarrier =
        CD3DX12_RESOURCE_BARRIER::Transition(mVertexBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
     mCommandList-&gt;ResourceBarrier(1, &amp;vertexBufferResourceBarrier);
  }
</code></pre>

<p>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.
<a href="https://i.sstatic.net/a6cL7.png" rel="noreferrer"><img src="https://i.sstatic.net/a6cL7.png" alt="enter image description here"></a></p>

<p>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.</p>

<pre><code>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) },
};
</code></pre>

<directx12><vertex-buffer-object><c++>
Dx12 Vertex Buffer incorrect
Andrew Wilson
# Dx12 Vertex Buffer incorrect

(Asked by: username_0 on 2015-10-24)

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](https://i.sstatic.net/a6cL7.png)](https://i.sstatic.net/a6cL7.png)

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

```

**Tags:** directx12, vertex-buffer-object, c++


---

## 1 answers

### Answer by username_1 on 2015-10-24. Score: 5 | Accepted answer

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

```


---

