I just finished my first DirectX 11 program. But I have several questions on memory transfer between CPU and GPU.
In my program, I create a vertex buffer first:
bool InitializeGeometry() { VertexType vertices[] = { XMFLOAT3(0.0f, 0.0f, 0.5f), XMFLOAT3(0.5f, 0.0f, 0.5f), XMFLOAT3(0.0f, 0.5f, 0.5f) }; D3D11_BUFFER_DESC vbDesc; ZeroMemory(&vbDesc, sizeof(vbDesc)); vbDesc.Usage = D3D11_USAGE_IMMUTABLE; vbDesc.ByteWidth = sizeof(vertices); vbDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vbDesc.CPUAccessFlags = 0; D3D11_SUBRESOURCE_DATA vbData; ZeroMemory(&vbData, sizeof(vbData)); vbData.pSysMem = vertices; if (FAILED(gDevice->CreateBuffer(&vbDesc, &vbData, &gVB))) return false; // ... } Later I will use this vertex buffer for rendering. Note that the vertices array is a stack variable and it will be destroyed on after the return of InitializeGeometry.
Does actual memory transfer from CPU to GPU happen in CreateBuffer? Or CreateBuffer just takes a reference to the vertices so I should give a longer lifetime to the vertices?
If the transfer does starts in CreateBuffer(or Unmap for dynamic buffers), can I expect the GPU driver save a copy of my vertices? I am afraid that the transfer takes so much time that InitializeGeometry function returns early, hence leaves transferring data incomplete.