Skip to content

Commit 284d8fe

Browse files
committed
add check access fully mapped
1 parent 2d371ed commit 284d8fe

File tree

2 files changed

+81
-44
lines changed

2 files changed

+81
-44
lines changed

lib/API/DX/Device.cpp

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -549,73 +549,83 @@ class DXDevice : public offloadtest::Device {
549549
"Failed to create reserved resource (buffer)."))
550550
return Err;
551551

552-
// Committed Upload Buffer
552+
// Committed Upload Buffer (CPU visible)
553553
ComPtr<ID3D12Resource> UploadBuffer;
554554
const D3D12_HEAP_PROPERTIES UploadHeapProps =
555555
CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
556556

557557
if (auto Err = HR::toError(
558558
Device->CreateCommittedResource(
559559
&UploadHeapProps, D3D12_HEAP_FLAG_NONE, &UploadResDesc,
560-
D3D12_RESOURCE_STATE_GENERIC_READ, // upload state for CPU
561-
// writes
562-
nullptr, IID_PPV_ARGS(&UploadBuffer)),
560+
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
561+
IID_PPV_ARGS(&UploadBuffer)),
563562
"Failed to create committed resource (upload buffer)."))
564563
return Err;
565564

566-
// Tile mapping setup
567-
UINT numTiles = R.TilesMapped;
568-
std::vector<D3D12_TILED_RESOURCE_COORDINATE> startCoords(numTiles);
569-
std::vector<D3D12_TILE_REGION_SIZE> regionSizes(numTiles);
570-
std::vector<D3D12_TILE_RANGE_FLAGS> rangeFlags(
571-
numTiles, D3D12_TILE_RANGE_FLAG_NONE);
572-
std::vector<UINT> heapRangeStartOffsets(numTiles);
573-
std::vector<UINT> rangeTileCounts(numTiles, 1);
574-
575-
// Create a heap large enough for all mapped tiles
576-
D3D12_HEAP_DESC heapDesc = {};
577-
heapDesc.Properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
578-
heapDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
579-
heapDesc.SizeInBytes =
580-
numTiles * D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
581-
heapDesc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS;
582-
583-
ComPtr<ID3D12Heap> heap;
584-
if (auto Err =
585-
HR::toError(Device->CreateHeap(&heapDesc, IID_PPV_ARGS(&heap)),
586-
"Failed to create heap for tiled SRV resource."))
587-
return Err;
565+
// Tile mapping setup (optional if numTiles > 0)
566+
UINT numTiles = static_cast<UINT>(R.TilesMapped);
567+
ComPtr<ID3D12Heap> heap; // optional, only created if numTiles > 0
588568

589-
// Fill tile coordinates and region sizes
590-
for (UINT i = 0; i < numTiles; ++i) {
591-
startCoords[i] = {i, 0, 0, 0};
592-
regionSizes[i].NumTiles = 1;
593-
regionSizes[i].UseBox = FALSE;
594-
heapRangeStartOffsets[i] = i;
595-
}
569+
if (numTiles > 0) {
570+
std::vector<D3D12_TILED_RESOURCE_COORDINATE> startCoords(numTiles);
571+
std::vector<D3D12_TILE_REGION_SIZE> regionSizes(numTiles);
572+
std::vector<D3D12_TILE_RANGE_FLAGS> rangeFlags(
573+
numTiles, D3D12_TILE_RANGE_FLAG_NONE);
574+
std::vector<UINT> heapRangeStartOffsets(numTiles);
575+
std::vector<UINT> rangeTileCounts(numTiles, 1);
576+
577+
// Create a heap large enough for the mapped tiles
578+
D3D12_HEAP_DESC heapDesc = {};
579+
heapDesc.Properties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
580+
heapDesc.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
581+
heapDesc.SizeInBytes = static_cast<UINT64>(numTiles) *
582+
D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
583+
heapDesc.Flags = D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS;
584+
585+
if (auto Err =
586+
HR::toError(Device->CreateHeap(&heapDesc, IID_PPV_ARGS(&heap)),
587+
"Failed to create heap for tiled SRV resource."))
588+
return Err;
596589

597-
// Retrieve a command queue from InvocationState
598-
ID3D12CommandQueue *CommandQueue = IS.Queue.Get();
590+
// Fill tile coordinates and region sizes
591+
for (UINT i = 0; i < numTiles; ++i) {
592+
startCoords[i] = {i, 0, 0, 0};
593+
regionSizes[i].NumTiles = 1;
594+
regionSizes[i].UseBox = FALSE;
595+
heapRangeStartOffsets[i] = i;
596+
}
597+
598+
// Retrieve a command queue from InvocationState
599+
ID3D12CommandQueue *CommandQueue = IS.Queue.Get();
599600

600-
// Map the first numTiles tiles in the Buffer
601-
CommandQueue->UpdateTileMappings(
602-
Buffer.Get(), numTiles, startCoords.data(), regionSizes.data(),
603-
heap.Get(), numTiles, rangeFlags.data(), heapRangeStartOffsets.data(),
604-
rangeTileCounts.data(), D3D12_TILE_MAPPING_FLAG_NONE);
601+
// Map the first numTiles tiles in the Buffer
602+
CommandQueue->UpdateTileMappings(
603+
Buffer.Get(), numTiles, startCoords.data(), regionSizes.data(),
604+
heap.Get(), numTiles, rangeFlags.data(),
605+
heapRangeStartOffsets.data(), rangeTileCounts.data(),
606+
D3D12_TILE_MAPPING_FLAG_NONE);
607+
}
605608

606609
// Upload data initialization
607610
void *ResDataPtr = nullptr;
608611
D3D12_RANGE range = {0, 0}; // no reads expected
609612
if (SUCCEEDED(UploadBuffer->Map(0, &range, &ResDataPtr))) {
610613
memcpy(ResDataPtr, ResData.get(), R.size());
614+
// Zero remaining bytes if the buffer is padded
615+
if (R.size() < BufferSize) {
616+
memset(static_cast<char *>(ResDataPtr) + R.size(), 0,
617+
BufferSize - R.size());
618+
}
611619
UploadBuffer->Unmap(0, nullptr);
612620
} else {
613621
return llvm::createStringError(std::errc::io_error,
614622
"Failed to map SRV upload buffer.");
615623
}
616624

625+
// Add GPU upload commands
617626
addResourceUploadCommands(R, IS, Buffer, UploadBuffer);
618627

628+
// Store resource bundle (heap optional)
619629
Bundle.emplace_back(UploadBuffer, Buffer, nullptr, heap);
620630
RegOffset++;
621631
}

test/Feature/HLSLLib/Mapped.test

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ StructuredBuffer<S> X : register(t0);
88
StructuredBuffer<S> Y : register(t1);
99

1010
RWStructuredBuffer<int> Out : register(u2);
11+
RWStructuredBuffer<int> CAFM : register(u3);
1112

1213

1314
[numthreads(1,1,1)]
@@ -21,21 +22,25 @@ void main() {
2122
S Out0 = X.Load(0, status);
2223
Out[idx] = Out0.data[0];
2324
Out[idx + 4] = status;
25+
CAFM[idx] = CheckAccessFullyMapped(status) ? 1 : 0;
2426
idx += 1;
2527

2628
S Out1 = X.Load(50, status);
2729
Out[idx] = Out1.data[0];
2830
Out[idx + 4] = status;
31+
CAFM[idx] = CheckAccessFullyMapped(status) ? 1 : 0;
2932
idx += 1;
3033

3134
S Out2 = Y.Load(0, status);
3235
Out[idx] = Out2.data[0];
3336
Out[idx + 4] = status;
37+
CAFM[idx] = CheckAccessFullyMapped(status) ? 1 : 0;
3438
idx += 1;
3539

3640
S Out3 = Y.Load(50, status);
3741
Out[idx] = Out3.data[0];
38-
Out[idx + 4] = status;
42+
Out[idx + 4] = status;
43+
CAFM[idx] = CheckAccessFullyMapped(status) ? 1 : 0;
3944
}
4045
//--- pipeline.yaml
4146

@@ -64,13 +69,27 @@ Buffers:
6469
Stride: 4
6570
# first 4 values are the actual data retrieved. For non-resident loads, 0 is expected.
6671
# last 4 values are the status. 1 is expected for resident memory, 0 for non-resident
67-
Data: [1, 1, 2, 0, 1, 1, 1, 0]
72+
Data: [1, 0, 0, 0, 1, 0, 0, 0]
73+
- Name: CAFM
74+
Format: Int32
75+
Stride: 4
76+
FillSize: 16
77+
FillValue: 0
78+
- Name: ExpectedCAFM
79+
Format: Int32
80+
Stride: 4
81+
# Only the first data access should be accessing fully mapped memory
82+
Data: [1, 0, 0, 0]
6883

6984
Results:
7085
- Result: Test
71-
Rule: BufferExact
86+
Rule: BufferExact
7287
Actual: Out
7388
Expected: ExpectedOut
89+
- Result: TestCAFM
90+
Rule: BufferExact
91+
Actual: CAFM
92+
Expected: ExpectedCAFM
7493
DescriptorSets:
7594
- Resources:
7695
- Name: X
@@ -80,21 +99,29 @@ DescriptorSets:
8099
Space: 0
81100
VulkanBinding:
82101
Binding: 0
102+
TilesMapped: 1
83103
- Name: Y
84104
Kind: StructuredBuffer
85105
DirectXBinding:
86106
Register: 1
87107
Space: 0
88108
VulkanBinding:
89109
Binding: 1
90-
TilesMapped: 1
110+
TilesMapped: 0
91111
- Name: Out
92112
Kind: RWStructuredBuffer
93113
DirectXBinding:
94114
Register: 2
95115
Space: 0
96116
VulkanBinding:
97117
Binding: 2
118+
- Name: CAFM
119+
Kind: RWStructuredBuffer
120+
DirectXBinding:
121+
Register: 3
122+
Space: 0
123+
VulkanBinding:
124+
Binding: 3
98125
#--- end
99126

100127
# XFAIL: Clang

0 commit comments

Comments
 (0)