Created
August 21, 2016 11:49
-
-
Save yorung/ed518ddee3efccd716da4fccbb1e1393 to your computer and use it in GitHub Desktop.
[DX12] Upload texture.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void afWriteTexture(ComPtr<ID3D12Resource> tex, const TexDesc& desc, int mipCount, const AFTexSubresourceData datas[]) | |
| { | |
| const int maxSubresources = 100; | |
| const UINT subResources = mipCount * desc.arraySize; | |
| assert(subResources <= maxSubresources); | |
| D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprints[maxSubresources]; | |
| UINT64 rowSizeInBytes[maxSubresources], uploadSize; | |
| UINT numRows[maxSubresources]; | |
| D3D12_RESOURCE_BARRIER transitions1[maxSubresources], transitions2[maxSubresources]; | |
| deviceMan.GetDevice()->GetCopyableFootprints(&tex->GetDesc(), 0, subResources, 0, footprints, numRows, rowSizeInBytes, &uploadSize); | |
| ComPtr<ID3D12Resource> uploadBuf = afCreateBuffer((int)uploadSize); | |
| assert(uploadBuf); | |
| uploadBuf->SetName(__FUNCTIONW__ L" intermediate buffer"); | |
| D3D12_RANGE readRange = {}; | |
| BYTE* ptr; | |
| HRESULT hr = uploadBuf->Map(0, &readRange, (void**)&ptr); | |
| assert(ptr); | |
| for (UINT i = 0; i < subResources; i++) | |
| { | |
| transitions1[i] = { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, D3D12_RESOURCE_BARRIER_FLAG_NONE,{ tex.Get(), i, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_COPY_DEST } }; | |
| transitions2[i] = { D3D12_RESOURCE_BARRIER_TYPE_TRANSITION, D3D12_RESOURCE_BARRIER_FLAG_NONE,{ tex.Get(), i, D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE } }; | |
| } | |
| ID3D12GraphicsCommandList* list = deviceMan.GetCommandList(); | |
| list->ResourceBarrier(subResources, transitions1); | |
| for (UINT i = 0; i < subResources; i++) | |
| { | |
| assert(datas[i].pitch == rowSizeInBytes[i]); | |
| assert(datas[i].pitch <= footprints[i].Footprint.RowPitch); | |
| for (UINT row = 0; row < numRows[i]; row++) { | |
| memcpy(ptr + footprints[i].Offset + footprints[i].Footprint.RowPitch * row, (BYTE*)datas[i].ptr + datas[i].pitch * row, datas[i].pitch); | |
| } | |
| D3D12_TEXTURE_COPY_LOCATION uploadBufLocation = { uploadBuf.Get(), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, footprints[i] }; | |
| D3D12_TEXTURE_COPY_LOCATION nativeBufLocation = { tex.Get(), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, i }; | |
| list->CopyTextureRegion(&nativeBufLocation, 0, 0, 0, &uploadBufLocation, nullptr); | |
| } | |
| list->ResourceBarrier(subResources, transitions2); | |
| uploadBuf->Unmap(0, nullptr); | |
| deviceMan.AddIntermediateCommandlistDependentResource(uploadBuf); | |
| deviceMan.AddIntermediateCommandlistDependentResource(tex); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment