Ivan Belyavtsev
04.03.2022
THROW_IF_FAILED(device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
// ...
IID_PPV_ARGS(&vertex_buffers[s])));
UINT8* vertex_data_begin;
CD3DX12_RANGE read_range(0, 0);
THROW_IF_FAILED(vertex_buffers[s]->Map(0, &read_range, reinterpret_cast<void **>(&vertex_data_begin)));
memcpy(vertex_data_begin, vertex_buffer_data->get_data(), vertex_buffer_size);
vertex_buffers[s]->Unmap(0, nullptr);
Upload heap type has CPU access optimized for uploading to the GPU, but does not experience the maximum amount of bandwidth for the GPU. Resources in this heap must be created with D3D12_RESOURCE_STATE_GENERIC_READ
and cannot be changed away from this [1]
UpdateSubresources
function to load data to the upload heap resource and then copy it to the default heap resourceTHROW_IF_FAILED(device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
// ...
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
IID_PPV_ARGS(&upload_vertex_buffer[s])));
THROW_IF_FAILED(device->CreateCommittedResource(
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
// ...
D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&vertex_buffer[s])));
command_list->ResourceBarrier(
1, &CD3DX12_RESOURCE_BARRIER::Transition(
vertex_buffer[s].Get(), D3D12_RESOURCE_STATE_COPY_DEST,
D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER));
// Or D3D12_RESOURCE_STATE_INDEX_BUFFER for index buffers
Don’t forget about executing the command list before