@@ -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 }
0 commit comments