3737#include " Support/Pipeline.h"
3838#include " Support/WinError.h"
3939
40+ #include " DXResources.h"
41+
4042#include " llvm/ADT/SmallVector.h"
4143#include " llvm/Object/DXContainer.h"
4244#include " llvm/Support/Error.h"
@@ -282,6 +284,17 @@ class DXBuffer : public offloadtest::Buffer {
282284 : Buffer(Buffer), Name(Name), Desc(Desc), SizeInBytes(SizeInBytes) {}
283285};
284286
287+ class DXTexture : public offloadtest ::Texture {
288+ public:
289+ ComPtr<ID3D12Resource> Resource;
290+ std::string Name;
291+ TextureCreateDesc Desc;
292+
293+ DXTexture (ComPtr<ID3D12Resource> Resource, llvm::StringRef Name,
294+ TextureCreateDesc Desc)
295+ : Resource(Resource), Name(Name), Desc(Desc) {}
296+ };
297+
285298class DXQueue : public offloadtest ::Queue {
286299public:
287300 ComPtr<ID3D12CommandQueue> Queue;
@@ -370,19 +383,7 @@ class DXDevice : public offloadtest::Device {
370383 llvm::Expected<std::shared_ptr<offloadtest::Buffer>>
371384 createBuffer (std::string Name, BufferCreateDesc &Desc,
372385 size_t SizeInBytes) override {
373-
374- D3D12_HEAP_TYPE HeapType = D3D12_HEAP_TYPE_DEFAULT ;
375- switch (Desc.Location ) {
376- case MemoryLocation::GpuOnly:
377- HeapType = D3D12_HEAP_TYPE_DEFAULT ;
378- break ;
379- case MemoryLocation::CpuToGpu:
380- HeapType = D3D12_HEAP_TYPE_UPLOAD ;
381- break ;
382- case MemoryLocation::GpuToCpu:
383- HeapType = D3D12_HEAP_TYPE_READBACK ;
384- break ;
385- }
386+ const D3D12_HEAP_TYPE HeapType = getDXHeapType (Desc.Location );
386387
387388 const D3D12_RESOURCE_FLAGS Flags =
388389 D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS ;
@@ -402,6 +403,40 @@ class DXDevice : public offloadtest::Device {
402403 return std::make_shared<DXBuffer>(DeviceBuffer, Name, Desc, SizeInBytes);
403404 }
404405
406+ llvm::Expected<std::shared_ptr<offloadtest::Texture>>
407+ createTexture (llvm::StringRef Name, TextureCreateDesc &Desc) override {
408+ if (!isValidTextureUsageAndFormat (Desc.Usage , Desc.Format ))
409+ return llvm::createStringError (
410+ std::errc::invalid_argument,
411+ " Invalid texture usage/format combination: usage '%s' is not "
412+ " compatible with format '%s'." ,
413+ getTextureUsageName (Desc.Usage ).c_str (),
414+ getTextureFormatName (Desc.Format ).data ());
415+
416+ const D3D12_HEAP_PROPERTIES HeapProps =
417+ CD3DX12_HEAP_PROPERTIES (getDXHeapType (Desc.Location ));
418+
419+ D3D12_RESOURCE_DESC TexDesc = {};
420+ TexDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D ;
421+ TexDesc.Width = Desc.Width , TexDesc.Height = Desc.Height ,
422+ TexDesc.DepthOrArraySize = 1 ;
423+ TexDesc.MipLevels = static_cast <UINT16 >(Desc.MipLevels );
424+ TexDesc.Format = getDXGIFormat (Desc.Format );
425+ TexDesc.SampleDesc .Count = 1 ;
426+ TexDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN ;
427+ TexDesc.Flags = getDXResourceFlags (Desc.Usage );
428+
429+ ComPtr<ID3D12Resource> DeviceTexture;
430+ if (auto Err = HR::toError (Device->CreateCommittedResource (
431+ &HeapProps, D3D12_HEAP_FLAG_NONE , &TexDesc,
432+ D3D12_RESOURCE_STATE_COMMON , nullptr ,
433+ IID_PPV_ARGS (&DeviceTexture)),
434+ " Failed to create texture." ))
435+ return Err;
436+
437+ return std::make_shared<DXTexture>(DeviceTexture, Name, Desc);
438+ }
439+
405440 static llvm::Expected<DXDevice> create (ComPtr<IDXCoreAdapter> Adapter,
406441 const DeviceConfig &Config) {
407442 ComPtr<ID3D12Device> Device;
0 commit comments