Skip to content

Commit

Permalink
D3D12: Upload data from mStagingBuffer with CopyResource()
Browse files Browse the repository at this point in the history
This patch uses `CopyResource()` in the code that uploads data from
staging buffer to the destination buffer when handling
`mappedAtCreation`.

Bug: chromium:42242066
Test: dawn_end2end_tests
Change-Id: I60b845507792c682ab2cf33da9ed368f52700143
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/213001
Reviewed-by: Loko Kung <[email protected]>
Reviewed-by: Corentin Wallez <[email protected]>
Commit-Queue: Jiawei Shao <[email protected]>
  • Loading branch information
Jiawei-Shao authored and Dawn LUCI CQ committed Nov 1, 2024
1 parent f78a5e2 commit b355a39
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
9 changes: 2 additions & 7 deletions src/dawn/native/d3d12/CommandBufferD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,6 @@ bool CanUseCopyResource(const TextureCopy& src, const TextureCopy& dst, const Ex
copySize.depthOrArrayLayers == srcSize.depthOrArrayLayers;
}

bool CanUseCopyResource(CopyBufferToBufferCmd* copy) {
return copy->sourceOffset == 0 && copy->destinationOffset == 0 &&
copy->size == copy->source->GetSize() && copy->size == copy->destination->GetSize() &&
copy->source->GetAllocatedSize() == copy->destination->GetAllocatedSize();
}

void RecordWriteTimestampCmd(ID3D12GraphicsCommandList* commandList,
QuerySetBase* querySet,
uint32_t queryIndex) {
Expand Down Expand Up @@ -862,7 +856,8 @@ MaybeError CommandBuffer::RecordCommands(CommandRecordingContext* commandContext
srcBuffer->TrackUsageAndTransitionNow(commandContext, wgpu::BufferUsage::CopySrc);
dstBuffer->TrackUsageAndTransitionNow(commandContext, wgpu::BufferUsage::CopyDst);

if (CanUseCopyResource(copy)) {
if (CanCopyBufferToBufferWithCopyResource(srcBuffer, copy->sourceOffset, dstBuffer,
copy->destinationOffset, copy->size)) {
commandList->CopyResource(dstBuffer->GetD3D12Resource(),
srcBuffer->GetD3D12Resource());
} else {
Expand Down
12 changes: 9 additions & 3 deletions src/dawn/native/d3d12/DeviceD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,15 @@ void Device::CopyFromStagingToBufferHelper(CommandRecordingContext* commandConte
Buffer* srcBuffer = ToBackend(source);
dstBuffer->TrackUsageAndTransitionNow(commandContext, wgpu::BufferUsage::CopyDst);

commandContext->GetCommandList()->CopyBufferRegion(
dstBuffer->GetD3D12Resource(), destinationOffset, srcBuffer->GetD3D12Resource(),
sourceOffset, size);
if (CanCopyBufferToBufferWithCopyResource(srcBuffer, sourceOffset, dstBuffer, destinationOffset,
size)) {
commandContext->GetCommandList()->CopyResource(dstBuffer->GetD3D12Resource(),
srcBuffer->GetD3D12Resource());
} else {
commandContext->GetCommandList()->CopyBufferRegion(
dstBuffer->GetD3D12Resource(), destinationOffset, srcBuffer->GetD3D12Resource(),
sourceOffset, size);
}
}

MaybeError Device::CopyFromStagingToTextureImpl(const BufferBase* source,
Expand Down
10 changes: 10 additions & 0 deletions src/dawn/native/d3d12/UtilsD3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,14 @@ void SetDebugName(Device* device, ID3D12Object* object, const char* prefix, std:
object->SetPrivateData(WKPDID_D3DDebugObjectName, objectName.length(), objectName.c_str());
}

bool CanCopyBufferToBufferWithCopyResource(Buffer* sourceBuffer,
uint64_t sourceOffset,
Buffer* destinationBuffer,
uint64_t destinationOffset,
uint64_t copySize) {
return sourceOffset == 0 && destinationOffset == 0 && sourceBuffer->GetSize() == copySize &&
destinationBuffer->GetSize() == copySize &&
sourceBuffer->GetAllocatedSize() == destinationBuffer->GetAllocatedSize();
}

} // namespace dawn::native::d3d12
6 changes: 6 additions & 0 deletions src/dawn/native/d3d12/UtilsD3D12.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ void RecordBufferTextureCopy(BufferTextureCopyDirection direction,

void SetDebugName(Device* device, ID3D12Object* object, const char* prefix, std::string label = "");

bool CanCopyBufferToBufferWithCopyResource(Buffer* sourceBuffer,
uint64_t sourceOffset,
Buffer* destinationBuffer,
uint64_t destinationOffset,
uint64_t copySize);

} // namespace dawn::native::d3d12

#endif // SRC_DAWN_NATIVE_D3D12_UTILSD3D12_H_

0 comments on commit b355a39

Please sign in to comment.