Skip to content

Commit 9c6664b

Browse files
committed
make all 3 new functions more consistent between each other
1 parent 284d8fe commit 9c6664b

File tree

1 file changed

+45
-55
lines changed

1 file changed

+45
-55
lines changed

lib/API/DX/Device.cpp

Lines changed: 45 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ class DXDevice : public offloadtest::Device {
608608

609609
// Upload data initialization
610610
void *ResDataPtr = nullptr;
611-
D3D12_RANGE range = {0, 0}; // no reads expected
612-
if (SUCCEEDED(UploadBuffer->Map(0, &range, &ResDataPtr))) {
611+
D3D12_RANGE Range = {0, 0}; // no reads expected
612+
if (SUCCEEDED(UploadBuffer->Map(0, &Range, &ResDataPtr))) {
613613
memcpy(ResDataPtr, ResData.get(), R.size());
614614
// Zero remaining bytes if the buffer is padded
615615
if (R.size() < BufferSize) {
@@ -625,7 +625,7 @@ class DXDevice : public offloadtest::Device {
625625
// Add GPU upload commands
626626
addResourceUploadCommands(R, IS, Buffer, UploadBuffer);
627627

628-
// Store resource bundle (heap optional)
628+
// Store heap in Bundle so it lives until caller releases the Bundle
629629
Bundle.emplace_back(UploadBuffer, Buffer, nullptr, heap);
630630
RegOffset++;
631631
}
@@ -743,7 +743,7 @@ class DXDevice : public offloadtest::Device {
743743
<< ", HasCounter = " << R.HasCounter
744744
<< ", TilesMapped = " << R.TilesMapped << " }\n";
745745

746-
// Reserved destination buffer (UAV target)
746+
// Reserved UAV resource
747747
ComPtr<ID3D12Resource> Buffer;
748748
if (auto Err =
749749
HR::toError(Device->CreateReservedResource(
@@ -752,7 +752,7 @@ class DXDevice : public offloadtest::Device {
752752
"Failed to create reserved resource (buffer)."))
753753
return Err;
754754

755-
// Committed upload buffer (CPU visible)
755+
// Committed Upload Buffer (CPU visible)
756756
ComPtr<ID3D12Resource> UploadBuffer;
757757
const D3D12_HEAP_PROPERTIES UploadHeapProps =
758758
CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
@@ -775,9 +775,9 @@ class DXDevice : public offloadtest::Device {
775775
"Failed to create committed resource (readback buffer)."))
776776
return Err;
777777

778-
// Tile mapping setup (map first R.TilesMapped tiles)
778+
// Tile mapping setup (optional if numTiles > 0)
779779
UINT numTiles = static_cast<UINT>(R.TilesMapped);
780-
ComPtr<ID3D12Heap> heap; // optional backing heap for mapped tiles
780+
ComPtr<ID3D12Heap> heap; // optional, only created if numTiles > 0
781781

782782
if (numTiles > 0) {
783783
std::vector<D3D12_TILED_RESOURCE_COORDINATE> startCoords(numTiles);
@@ -787,7 +787,7 @@ class DXDevice : public offloadtest::Device {
787787
std::vector<UINT> heapRangeStartOffsets(numTiles);
788788
std::vector<UINT> rangeTileCounts(numTiles, 1);
789789

790-
// Create a heap large enough for the requested mapped tiles
790+
// Create a heap large enough for the mapped tiles
791791
D3D12_HEAP_DESC heapDesc = {};
792792
heapDesc.Properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
793793
heapDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
@@ -797,10 +797,10 @@ class DXDevice : public offloadtest::Device {
797797

798798
if (auto Err =
799799
HR::toError(Device->CreateHeap(&heapDesc, IID_PPV_ARGS(&heap)),
800-
"Failed to create heap for tiled resource."))
800+
"Failed to create heap for tiled UAV resource."))
801801
return Err;
802802

803-
// Fill tile coordinates and region sizes (map tiles starting at tile 0)
803+
// Fill tile coordinates and region sizes
804804
for (UINT i = 0; i < numTiles; ++i) {
805805
startCoords[i] = {i, 0, 0, 0};
806806
regionSizes[i].NumTiles = 1;
@@ -821,10 +821,10 @@ class DXDevice : public offloadtest::Device {
821821

822822
// Upload data initialization
823823
void *ResDataPtr = nullptr;
824-
D3D12_RANGE mapRange = {0, 0}; // no reads expected
825-
if (SUCCEEDED(UploadBuffer->Map(0, &mapRange, &ResDataPtr))) {
824+
D3D12_RANGE Range = {0, 0}; // no reads expected
825+
if (SUCCEEDED(UploadBuffer->Map(0, &Range, &ResDataPtr))) {
826826
memcpy(ResDataPtr, ResData.get(), R.size());
827-
// Zero any remaining bytes if buffer element is larger than R.size()
827+
// Zero remaining bytes if the buffer is padded
828828
if (R.size() < BufferSize) {
829829
memset(static_cast<char *>(ResDataPtr) + R.size(), 0,
830830
BufferSize - R.size());
@@ -835,8 +835,7 @@ class DXDevice : public offloadtest::Device {
835835
"Failed to map upload buffer.");
836836
}
837837

838-
// Issue copy/upload commands to transfer UploadBuffer -> Buffer (tile
839-
// region)
838+
// Add GPU upload commands
840839
addResourceUploadCommands(R, IS, Buffer, UploadBuffer);
841840

842841
// Store heap in Bundle so it lives until caller releases the Bundle
@@ -964,14 +963,11 @@ class DXDevice : public offloadtest::Device {
964963
InvocationState &IS) {
965964
ResourceBundle Bundle;
966965

967-
const size_t CBVSize = getCBVSize(R.size());
968-
969-
// Create a buffer description for a reserved buffer (no physical memory
970-
// yet)
966+
const size_t BufferSize = getCBVSize(R.size());
971967
const D3D12_RESOURCE_DESC ResDesc = {
972968
D3D12_RESOURCE_DIMENSION_BUFFER,
973969
0,
974-
CBVSize,
970+
BufferSize,
975971
1,
976972
1,
977973
1,
@@ -980,19 +976,17 @@ class DXDevice : public offloadtest::Device {
980976
D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
981977
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS};
982978

983-
const D3D12_HEAP_PROPERTIES UploadHeapProp =
984-
CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
985979
const D3D12_RESOURCE_DESC UploadResDesc =
986-
CD3DX12_RESOURCE_DESC::Buffer(CBVSize);
980+
CD3DX12_RESOURCE_DESC::Buffer(BufferSize);
987981

988982
uint32_t RegOffset = 0;
989983
for (const auto &ResData : R.BufferPtr->Data) {
990-
llvm::outs() << "Creating CBV: { Size = " << CBVSize << ", Register = b"
991-
<< R.DXBinding.Register + RegOffset
984+
llvm::outs() << "Creating CBV: { Size = " << BufferSize
985+
<< ", Register = b" << R.DXBinding.Register + RegOffset
992986
<< ", Space = " << R.DXBinding.Space
993987
<< ", TilesMapped = " << R.TilesMapped << " }\n";
994988

995-
// Create the GPU-side reserved buffer (no physical memory yet)
989+
// Reserved CBV resource
996990
ComPtr<ID3D12Resource> Buffer;
997991
if (auto Err =
998992
HR::toError(Device->CreateReservedResource(
@@ -1001,20 +995,21 @@ class DXDevice : public offloadtest::Device {
1001995
"Failed to create reserved resource (buffer)."))
1002996
return Err;
1003997

1004-
// Create an upload buffer (committed, CPU-visible) for copying the CBV
1005-
// contents
998+
// Committed Upload Buffer (CPU visible)
1006999
ComPtr<ID3D12Resource> UploadBuffer;
1000+
const D3D12_HEAP_PROPERTIES UploadHeapProps =
1001+
CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
10071002
if (auto Err = HR::toError(
10081003
Device->CreateCommittedResource(
1009-
&UploadHeapProp, D3D12_HEAP_FLAG_NONE, &UploadResDesc,
1004+
&UploadHeapProps, D3D12_HEAP_FLAG_NONE, &UploadResDesc,
10101005
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
10111006
IID_PPV_ARGS(&UploadBuffer)),
10121007
"Failed to create committed resource (upload buffer)."))
10131008
return Err;
10141009

1015-
// Tile mapping setup (map first R.TilesMapped tiles)
1010+
// Tile mapping setup (optional if numTiles > 0)
10161011
UINT numTiles = static_cast<UINT>(R.TilesMapped);
1017-
ComPtr<ID3D12Heap> heap; // keep alive in bundle if created
1012+
ComPtr<ID3D12Heap> heap; // optional, only created if numTiles > 0
10181013

10191014
if (numTiles > 0) {
10201015
std::vector<D3D12_TILED_RESOURCE_COORDINATE> startCoords(numTiles);
@@ -1024,7 +1019,7 @@ class DXDevice : public offloadtest::Device {
10241019
std::vector<UINT> heapRangeStartOffsets(numTiles);
10251020
std::vector<UINT> rangeTileCounts(numTiles, 1);
10261021

1027-
// Create a heap large enough for all mapped tiles
1022+
// Create a heap large enough for the mapped tiles
10281023
D3D12_HEAP_DESC heapDesc = {};
10291024
heapDesc.Properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
10301025
heapDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
@@ -1034,11 +1029,10 @@ class DXDevice : public offloadtest::Device {
10341029

10351030
if (auto Err =
10361031
HR::toError(Device->CreateHeap(&heapDesc, IID_PPV_ARGS(&heap)),
1037-
"Failed to create heap for tiled resource."))
1032+
"Failed to create heap for tiled CBV resource."))
10381033
return Err;
10391034

1040-
// Fill tile coordinates and region sizes (map tiles starting from tile
1041-
// 0)
1035+
// Fill tile coordinates and region sizes
10421036
for (UINT i = 0; i < numTiles; ++i) {
10431037
startCoords[i] = {i, 0, 0, 0};
10441038
regionSizes[i].NumTiles = 1;
@@ -1049,38 +1043,34 @@ class DXDevice : public offloadtest::Device {
10491043
// Retrieve a command queue from InvocationState
10501044
ID3D12CommandQueue *CommandQueue = IS.Queue.Get();
10511045

1052-
// Map the first numtiles Tiles
1046+
// Map the first numTiles tiles in the Buffer
10531047
CommandQueue->UpdateTileMappings(
10541048
Buffer.Get(), numTiles, startCoords.data(), regionSizes.data(),
10551049
heap.Get(), numTiles, rangeFlags.data(),
10561050
heapRangeStartOffsets.data(), rangeTileCounts.data(),
10571051
D3D12_TILE_MAPPING_FLAG_NONE);
1058-
} // end if numTiles > 0
1052+
}
10591053

1060-
// Initialize the CBV data into the upload buffer
1054+
// Upload data initialization
10611055
void *ResDataPtr = nullptr;
1062-
D3D12_RANGE mapRange = {
1063-
0, 0}; // We don't intend to read from the upload buffer on CPU
1064-
if (FAILED(UploadBuffer->Map(0, &mapRange, &ResDataPtr)))
1056+
D3D12_RANGE range = {0, 0}; // no reads expected
1057+
if (SUCCEEDED(UploadBuffer->Map(0, &range, &ResDataPtr))) {
1058+
memcpy(ResDataPtr, ResData.get(), R.size());
1059+
// Zero remaining bytes if the buffer is padded
1060+
if (R.size() < BufferSize) {
1061+
memset(static_cast<char *>(ResDataPtr) + R.size(), 0,
1062+
BufferSize - R.size());
1063+
}
1064+
UploadBuffer->Unmap(0, nullptr);
1065+
} else {
10651066
return llvm::createStringError(std::errc::io_error,
1066-
"Failed to map upload buffer.");
1067-
1068-
// Copy CBV payload
1069-
memcpy(ResDataPtr, ResData.get(), R.size());
1070-
1071-
// Zero any remaining bytes (correct length CBVSize - R.size())
1072-
if (R.size() < CBVSize) {
1073-
void *ExtraData = static_cast<char *>(ResDataPtr) + R.size();
1074-
memset(ExtraData, 0, CBVSize - R.size());
1067+
"Failed to map CBV upload buffer.");
10751068
}
10761069

1077-
UploadBuffer->Unmap(0, nullptr);
1078-
1079-
// Issue GPU-side copy/upload commands to transfer UploadBuffer -> Buffer
1070+
// Add GPU upload commands
10801071
addResourceUploadCommands(R, IS, Buffer, UploadBuffer);
10811072

1082-
// Save bundle (store heap so backing memory stays alive while GPU uses
1083-
// it)
1073+
// Store resource bundle (heap optional)
10841074
Bundle.emplace_back(UploadBuffer, Buffer, nullptr, heap);
10851075
RegOffset++;
10861076
}

0 commit comments

Comments
 (0)