Skip to content

Commit 8d9cbbb

Browse files
committed
attempt to unify SRV
1 parent d3cbf91 commit 8d9cbbb

File tree

1 file changed

+24
-79
lines changed

1 file changed

+24
-79
lines changed

lib/API/DX/Device.cpp

Lines changed: 24 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,12 @@ static D3D12_RESOURCE_DESC getResourceDescription(const Resource &R) {
149149
const uint32_t Width =
150150
R.isTexture() ? B.OutputProps.Width : getUAVBufferSize(R);
151151
const uint32_t Height = R.isTexture() ? B.OutputProps.Height : 1;
152-
const D3D12_TEXTURE_LAYOUT Layout = R.isTexture()
153-
? D3D12_TEXTURE_LAYOUT_UNKNOWN
154-
: D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
152+
D3D12_TEXTURE_LAYOUT Layout;
153+
if (R.isTexture() && getDXKind(R.Kind) == SRV)
154+
Layout = D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE;
155+
else
156+
Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
157+
155158
const D3D12_RESOURCE_FLAGS Flags =
156159
R.isReadWrite() ? D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS
157160
: D3D12_RESOURCE_FLAG_NONE;
@@ -523,25 +526,25 @@ class DXDevice : public offloadtest::Device {
523526
addUploadEndBarrier(IS, Destination, R.isReadWrite());
524527
}
525528

526-
llvm::Expected<ResourceBundle> createReservedSRV(Resource &R,
527-
InvocationState &IS) {
529+
llvm::Expected<ResourceBundle> createSRV(Resource &R, InvocationState &IS) {
528530
ResourceBundle Bundle;
529-
const uint32_t BufferSize = R.size();
530531
const D3D12_RESOURCE_DESC ResDesc = getResourceDescription(R);
531-
532532
const D3D12_RESOURCE_DESC UploadResDesc =
533-
CD3DX12_RESOURCE_DESC::Buffer(BufferSize);
533+
CD3DX12_RESOURCE_DESC::Buffer(R.size());
534+
const D3D12_HEAP_PROPERTIES UploadHeapProps =
535+
CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
534536

535537
uint32_t RegOffset = 0;
538+
536539
for (const auto &ResData : R.BufferPtr->Data) {
537-
llvm::outs() << "Creating SRV: { Size = " << BufferSize
538-
<< ", Register = t" << R.DXBinding.Register + RegOffset
540+
llvm::outs() << "Creating SRV: { Size = " << R.size() << ", Register = t"
541+
<< R.DXBinding.Register + RegOffset
539542
<< ", Space = " << R.DXBinding.Space;
543+
540544
if (R.TilesMapped)
541545
llvm::outs() << ", TilesMapped = " << *R.TilesMapped;
542546
llvm::outs() << " }\n";
543547

544-
// Reserved SRV resource
545548
ComPtr<ID3D12Resource> Buffer;
546549
if (auto Err =
547550
HR::toError(Device->CreateReservedResource(
@@ -550,11 +553,8 @@ class DXDevice : public offloadtest::Device {
550553
"Failed to create reserved resource (buffer)."))
551554
return Err;
552555

553-
// Committed Upload Buffer (CPU visible)
556+
// Committed upload buffer
554557
ComPtr<ID3D12Resource> UploadBuffer;
555-
const D3D12_HEAP_PROPERTIES UploadHeapProps =
556-
CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
557-
558558
if (auto Err = HR::toError(
559559
Device->CreateCommittedResource(
560560
&UploadHeapProps, D3D12_HEAP_FLAG_NONE, &UploadResDesc,
@@ -564,7 +564,15 @@ class DXDevice : public offloadtest::Device {
564564
return Err;
565565

566566
// Tile mapping setup (optional if NumTiles > 0)
567-
const UINT NumTiles = static_cast<UINT>(*R.TilesMapped);
567+
UINT NumTiles = 0;
568+
if (R.TilesMapped.has_value()) {
569+
NumTiles = static_cast<UINT>(*R.TilesMapped);
570+
} else {
571+
// Map the entire buffer by computing how many 64KB tiles cover it
572+
NumTiles = static_cast<UINT>(
573+
(ResDesc.Width + D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT - 1) /
574+
D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT);
575+
}
568576
ComPtr<ID3D12Heap> Heap; // optional, only created if NumTiles > 0
569577

570578
if (NumTiles > 0) {
@@ -612,11 +620,6 @@ class DXDevice : public offloadtest::Device {
612620
const D3D12_RANGE Range = {0, 0}; // no reads expected
613621
if (SUCCEEDED(UploadBuffer->Map(0, &Range, &ResDataPtr))) {
614622
memcpy(ResDataPtr, ResData.get(), R.size());
615-
// Zero remaining bytes if the buffer is padded
616-
if (R.size() < BufferSize) {
617-
memset(static_cast<char *>(ResDataPtr) + R.size(), 0,
618-
BufferSize - R.size());
619-
}
620623
UploadBuffer->Unmap(0, nullptr);
621624
} else {
622625
return llvm::createStringError(std::errc::io_error,
@@ -630,67 +633,9 @@ class DXDevice : public offloadtest::Device {
630633
Bundle.emplace_back(UploadBuffer, Buffer, nullptr, Heap);
631634
RegOffset++;
632635
}
633-
634636
return Bundle;
635637
}
636638

637-
llvm::Expected<ResourceBundle> createCommittedSRV(Resource &R,
638-
InvocationState &IS) {
639-
ResourceBundle Bundle;
640-
641-
const D3D12_HEAP_PROPERTIES HeapProp =
642-
CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
643-
const D3D12_RESOURCE_DESC ResDesc = getResourceDescription(R);
644-
const D3D12_HEAP_PROPERTIES UploadHeapProp =
645-
CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
646-
const D3D12_RESOURCE_DESC UploadResDesc =
647-
CD3DX12_RESOURCE_DESC::Buffer(R.size());
648-
649-
uint32_t RegOffset = 0;
650-
for (const auto &ResData : R.BufferPtr->Data) {
651-
llvm::outs() << "Creating SRV: { Size = " << R.size() << ", Register = t"
652-
<< R.DXBinding.Register + RegOffset
653-
<< ", Space = " << R.DXBinding.Space << " }\n";
654-
655-
ComPtr<ID3D12Resource> Buffer;
656-
if (auto Err = HR::toError(
657-
Device->CreateCommittedResource(
658-
&HeapProp, D3D12_HEAP_FLAG_NONE, &ResDesc,
659-
D3D12_RESOURCE_STATE_COMMON, nullptr, IID_PPV_ARGS(&Buffer)),
660-
"Failed to create committed resource (buffer)."))
661-
return Err;
662-
663-
ComPtr<ID3D12Resource> UploadBuffer;
664-
if (auto Err = HR::toError(
665-
Device->CreateCommittedResource(
666-
&UploadHeapProp, D3D12_HEAP_FLAG_NONE, &UploadResDesc,
667-
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr,
668-
IID_PPV_ARGS(&UploadBuffer)),
669-
"Failed to create committed resource (upload buffer)."))
670-
return Err;
671-
672-
// Initialize the SRV data
673-
void *ResDataPtr = nullptr;
674-
if (auto Err = HR::toError(UploadBuffer->Map(0, nullptr, &ResDataPtr),
675-
"Failed to acquire UAV data pointer."))
676-
return Err;
677-
memcpy(ResDataPtr, ResData.get(), R.size());
678-
UploadBuffer->Unmap(0, nullptr);
679-
680-
addResourceUploadCommands(R, IS, Buffer, UploadBuffer);
681-
682-
Bundle.emplace_back(UploadBuffer, Buffer, nullptr);
683-
RegOffset++;
684-
}
685-
return Bundle;
686-
}
687-
688-
llvm::Expected<ResourceBundle> createSRV(Resource &R, InvocationState &IS) {
689-
if (R.TilesMapped)
690-
return createReservedSRV(R, IS);
691-
return createCommittedSRV(R, IS);
692-
}
693-
694639
// returns the next available HeapIdx
695640
uint32_t bindSRV(Resource &R, InvocationState &IS, uint32_t HeapIdx,
696641
ResourceBundle ResBundle) {

0 commit comments

Comments
 (0)