27#include <d3d12sdklayers.h>
31#include "D3D12MemAlloc.h"
39#ifdef __ID3D12Device2_INTERFACE_DEFINED__
40#define QRHI_D3D12_AVAILABLE
44#ifdef __ID3D12GraphicsCommandList5_INTERFACE_DEFINED__
45#define QRHI_D3D12_CL5_AVAILABLE
46using D3D12GraphicsCommandList = ID3D12GraphicsCommandList5;
48using D3D12GraphicsCommandList = ID3D12GraphicsCommandList1;
51#ifdef __ID3D12InfoQueue1_INTERFACE_DEFINED__
52#define QRHI_D3D12_INFOQUEUE1_AVAILABLE
55#ifdef __ID3D12PipelineLibrary1_INTERFACE_DEFINED__
56#define QRHI_D3D12_PIPELINE_LIBRARY_AVAILABLE
61static const int QD3D12_FRAMES_IN_FLIGHT = 2;
65struct QD3D12Descriptor
67 D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle = {};
68 D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle = {};
70 bool isValid()
const {
return cpuHandle.ptr != 0; }
73struct QD3D12ReleaseQueue;
75struct QD3D12DescriptorHeap
77 bool isValid()
const {
return heap && capacity; }
78 bool create(ID3D12Device *device,
79 quint32 descriptorCount,
80 D3D12_DESCRIPTOR_HEAP_TYPE heapType,
81 D3D12_DESCRIPTOR_HEAP_FLAGS heapFlags);
82 void createWithExisting(
const QD3D12DescriptorHeap &other,
83 quint32 offsetInDescriptors,
84 quint32 descriptorCount);
86 void destroyWithDeferredRelease(QD3D12ReleaseQueue *releaseQueue);
88 QD3D12Descriptor get(quint32 count);
89 QD3D12Descriptor at(quint32 index)
const;
90 quint32 remainingCapacity()
const {
return capacity - head; }
92 QD3D12Descriptor incremented(
const QD3D12Descriptor &descriptor, quint32 offsetInDescriptors)
const
94 D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle = descriptor.cpuHandle;
95 cpuHandle.ptr += offsetInDescriptors * descriptorByteSize;
96 D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle = descriptor.gpuHandle;
98 gpuHandle.ptr += offsetInDescriptors * descriptorByteSize;
99 return { cpuHandle, gpuHandle };
102 ID3D12DescriptorHeap *heap =
nullptr;
103 quint32 capacity = 0;
104 QD3D12Descriptor heapStart;
106 quint32 descriptorByteSize = 0;
107 D3D12_DESCRIPTOR_HEAP_TYPE heapType;
108 D3D12_DESCRIPTOR_HEAP_FLAGS heapFlags;
111struct QD3D12CpuDescriptorPool
113 bool isValid()
const {
return !heaps.isEmpty(); }
114 bool create(ID3D12Device *device, D3D12_DESCRIPTOR_HEAP_TYPE heapType,
const char *debugName =
"");
117 QD3D12Descriptor allocate(quint32 count);
118 void release(
const QD3D12Descriptor &descriptor, quint32 count);
120 static const int DESCRIPTORS_PER_HEAP = 256;
123 QD3D12DescriptorHeap heap;
125 static HeapWithMap init(
const QD3D12DescriptorHeap &heap, quint32 descriptorCount) {
128 result.map.resize(descriptorCount);
133 ID3D12Device *device;
134 quint32 descriptorByteSize;
135 QVector<HeapWithMap> heaps;
136 const char *debugName;
139struct QD3D12QueryHeap
141 bool isValid()
const {
return heap && capacity; }
142 bool create(ID3D12Device *device,
144 D3D12_QUERY_HEAP_TYPE heapType);
147 ID3D12QueryHeap *heap =
nullptr;
148 quint32 capacity = 0;
151struct QD3D12StagingArea
153 static const quint32 ALIGNMENT = D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT;
157 D3D12_GPU_VIRTUAL_ADDRESS gpuAddr = 0;
158 ID3D12Resource *buffer =
nullptr;
159 quint32 bufferOffset = 0;
160 bool isValid()
const {
return p !=
nullptr; }
163 bool isValid()
const {
return allocation && mem.isValid(); }
164 bool create(QRhiD3D12 *rhi, quint32 capacity, D3D12_HEAP_TYPE heapType);
166 void destroyWithDeferredRelease(QD3D12ReleaseQueue *releaseQueue);
168 Allocation get(quint32 byteSize);
170 quint32 remainingCapacity()
const
172 return capacity - head;
175 static quint32 allocSizeForArray(quint32 size,
int count = 1)
177 return count * ((size + ALIGNMENT - 1) & ~(ALIGNMENT - 1));
181 ID3D12Resource *resource =
nullptr;
182 D3D12MA::Allocation *allocation =
nullptr;
187struct QD3D12ObjectHandle
190 quint32 generation = 0;
193 bool isNull()
const {
return index == 0 && generation == 0; }
196inline bool operator==(
const QD3D12ObjectHandle &a,
const QD3D12ObjectHandle &b)
noexcept
198 return a.index == b.index && a.generation == b.generation;
201inline bool operator!=(
const QD3D12ObjectHandle &a,
const QD3D12ObjectHandle &b)
noexcept
207struct QD3D12ObjectPool
209 void create(
const char *debugName =
"")
211 this->debugName = debugName;
212 Q_ASSERT(data.isEmpty());
218 for (Data &d : data) {
219 if (d.object.has_value()) {
221 d.object->releaseResources();
227 static bool leakCheck =
true;
230 static bool leakCheck = qEnvironmentVariableIntValue(
"QT_RHI_LEAK_CHECK");
234 qWarning(
"QD3D12ObjectPool::destroy(): Pool %p '%s' had %d unreleased objects",
235 this, debugName, leakCount);
240 bool isValid(
const QD3D12ObjectHandle &handle)
const
242 return handle.index > 0
243 && handle.index < quint32(data.count())
244 && handle.generation > 0
245 && handle.generation == data[handle.index].generation
246 && data[handle.index].object.has_value();
249 T lookup(
const QD3D12ObjectHandle &handle)
const
251 return isValid(handle) ? *data[handle.index].object : T();
254 const T *lookupRef(
const QD3D12ObjectHandle &handle)
const
256 return isValid(handle) ? &*data[handle.index].object :
nullptr;
259 T *lookupRef(
const QD3D12ObjectHandle &handle)
261 return isValid(handle) ? &*data[handle.index].object :
nullptr;
264 QD3D12ObjectHandle add(
const T &object)
266 Q_ASSERT(!data.isEmpty());
267 const quint32 count = quint32(data.count());
269 for (; index < count; ++index) {
270 if (!data[index].object.has_value())
274 data[index].object = object;
275 quint32 &generation = data[index].generation;
277 return { index, generation };
279 data.append({ object, 1 });
284 void remove(
const QD3D12ObjectHandle &handle)
286 if (T *object = lookupRef(handle)) {
287 object->releaseResources();
288 data[handle.index].object.reset();
292 const char *debugName;
294 std::optional<T> object;
295 quint32 generation = 0;
302 ID3D12Resource *resource;
303 D3D12_RESOURCE_STATES state;
304 D3D12_RESOURCE_DESC desc;
305 D3D12MA::Allocation *allocation;
307 enum { UavUsageRead = 0x01, UavUsageWrite = 0x02 };
313 static QD3D12ObjectHandle addToPool(QD3D12ObjectPool<QD3D12Resource> *pool,
314 ID3D12Resource *resource,
315 D3D12_RESOURCE_STATES state,
316 D3D12MA::Allocation *allocation =
nullptr,
317 void *cpuMapPtr =
nullptr)
320 return pool->add({ resource, state, resource->GetDesc(), allocation, cpuMapPtr, 0,
true });
324 static QD3D12ObjectHandle addNonOwningToPool(QD3D12ObjectPool<QD3D12Resource> *pool,
325 ID3D12Resource *resource,
326 D3D12_RESOURCE_STATES state)
329 return pool->add({ resource, state, resource->GetDesc(),
nullptr,
nullptr, 0,
false });
332 void releaseResources()
338 allocation->Release();
350 ID3D12PipelineState *pso;
352 static QD3D12ObjectHandle addToPool(QD3D12ObjectPool<QD3D12Pipeline> *pool,
354 ID3D12PipelineState *pso)
356 return pool->add({ type, pso });
359 void releaseResources()
365struct QD3D12RootSignature
367 ID3D12RootSignature *rootSig;
369 static QD3D12ObjectHandle addToPool(QD3D12ObjectPool<QD3D12RootSignature> *pool,
370 ID3D12RootSignature *rootSig)
372 return pool->add({ rootSig });
375 void releaseResources()
381struct QD3D12ReleaseQueue
383 void create(QD3D12ObjectPool<QD3D12Resource> *resourcePool,
384 QD3D12ObjectPool<QD3D12Pipeline> *pipelinePool,
385 QD3D12ObjectPool<QD3D12RootSignature> *rootSignaturePool)
387 this->resourcePool = resourcePool;
388 this->pipelinePool = pipelinePool;
389 this->rootSignaturePool = rootSignaturePool;
392 void deferredReleaseResource(
const QD3D12ObjectHandle &handle);
393 void deferredReleaseResourceWithViews(
const QD3D12ObjectHandle &handle,
394 QD3D12CpuDescriptorPool *pool,
395 const QD3D12Descriptor &viewsStart,
397 void deferredReleasePipeline(
const QD3D12ObjectHandle &handle);
398 void deferredReleaseRootSignature(
const QD3D12ObjectHandle &handle);
399 void deferredReleaseCallback(std::function<
void(
void*)> callback,
void *userData);
400 void deferredReleaseResourceAndAllocation(ID3D12Resource *resource,
401 D3D12MA::Allocation *allocation);
402 void deferredReleaseDescriptorHeap(ID3D12DescriptorHeap *heap);
403 void deferredReleaseViews(QD3D12CpuDescriptorPool *pool,
404 const QD3D12Descriptor &viewsStart,
407 void activatePendingDeferredReleaseRequests(
int frameSlot);
408 void executeDeferredReleases(
int frameSlot,
bool forced =
false);
411 struct DeferredReleaseEntry {
417 ResourceAndAllocation,
421 Type type = Resource;
422 std::optional<
int> frameSlotToBeReleasedIn;
423 QD3D12ObjectHandle handle;
424 QD3D12CpuDescriptorPool *poolForViews =
nullptr;
425 QD3D12Descriptor viewsStart;
427 std::function<
void(
void*)> callback =
nullptr;
428 void *callbackUserData =
nullptr;
429 std::pair<ID3D12Resource *, D3D12MA::Allocation *> resourceAndAllocation = {};
430 ID3D12DescriptorHeap *descriptorHeap =
nullptr;
432 QVector<DeferredReleaseEntry> queue;
433 QD3D12ObjectPool<QD3D12Resource> *resourcePool =
nullptr;
434 QD3D12ObjectPool<QD3D12Pipeline> *pipelinePool =
nullptr;
435 QD3D12ObjectPool<QD3D12RootSignature> *rootSignaturePool =
nullptr;
438struct QD3D12CommandBuffer;
440struct QD3D12ResourceBarrierGenerator
442 static const int PREALLOC = 16;
444 void create(QD3D12ObjectPool<QD3D12Resource> *resourcePool)
446 this->resourcePool = resourcePool;
449 void addTransitionBarrier(
const QD3D12ObjectHandle &resourceHandle, D3D12_RESOURCE_STATES stateAfter);
450 void enqueueBufferedTransitionBarriers(QD3D12CommandBuffer *cbD);
451 void enqueueSubresourceTransitionBarrier(QD3D12CommandBuffer *cbD,
452 const QD3D12ObjectHandle &resourceHandle,
454 D3D12_RESOURCE_STATES stateBefore,
455 D3D12_RESOURCE_STATES stateAfter);
456 void enqueueUavBarrier(QD3D12CommandBuffer *cbD,
const QD3D12ObjectHandle &resourceHandle);
458 struct TransitionResourceBarrier {
459 QD3D12ObjectHandle resourceHandle;
460 D3D12_RESOURCE_STATES stateBefore;
461 D3D12_RESOURCE_STATES stateAfter;
463 QVarLengthArray<TransitionResourceBarrier, PREALLOC> transitionResourceBarriers;
464 QD3D12ObjectPool<QD3D12Resource> *resourcePool =
nullptr;
467struct QD3D12ShaderBytecode
469 QD3D12ShaderBytecode() =
default;
470 QD3D12ShaderBytecode(
const QByteArray &bytecode,
const QShader::NativeResourceBindingMap &rbm,
471 int pushConstantRegister = -1, quint32 pushConstantSize = 0)
472 : bytecode(bytecode), nativeResourceBindingMap(rbm),
473 pushConstantRegister(pushConstantRegister), pushConstantSize(pushConstantSize)
476 QShader::NativeResourceBindingMap nativeResourceBindingMap;
477 int pushConstantRegister = -1;
478 quint32 pushConstantSize = 0;
481inline quint32 qrhiBinaryCacheEntryByteSize(
const QD3D12ShaderBytecode &s)
483 return quint32(s.bytecode.size());
486using QD3D12ShaderBytecodeCache = QRhiBinaryCache<QRhiShaderStage, QD3D12ShaderBytecode>;
488struct QD3D12ShaderVisibleDescriptorHeap
490 bool create(ID3D12Device *device, D3D12_DESCRIPTOR_HEAP_TYPE type, quint32 perFrameDescriptorCount);
492 void destroyWithDeferredRelease(QD3D12ReleaseQueue *releaseQueue);
494 QD3D12DescriptorHeap heap;
495 QD3D12DescriptorHeap perFrameHeapSlice[QD3D12_FRAMES_IN_FLIGHT];
499struct Q_D3D12_SAMPLER_DESC
501 D3D12_SAMPLER_DESC desc;
503 friend bool operator==(
const Q_D3D12_SAMPLER_DESC &lhs,
const Q_D3D12_SAMPLER_DESC &rhs)
noexcept
505 return lhs.desc.Filter == rhs.desc.Filter
506 && lhs.desc.AddressU == rhs.desc.AddressU
507 && lhs.desc.AddressV == rhs.desc.AddressV
508 && lhs.desc.AddressW == rhs.desc.AddressW
509 && lhs.desc.MipLODBias == rhs.desc.MipLODBias
510 && lhs.desc.MaxAnisotropy == rhs.desc.MaxAnisotropy
511 && lhs.desc.ComparisonFunc == rhs.desc.ComparisonFunc
513 && lhs.desc.MinLOD == rhs.desc.MinLOD
514 && lhs.desc.MaxLOD == rhs.desc.MaxLOD;
517 friend bool operator!=(
const Q_D3D12_SAMPLER_DESC &lhs,
const Q_D3D12_SAMPLER_DESC &rhs)
noexcept
519 return !(lhs == rhs);
522 friend size_t qHash(
const Q_D3D12_SAMPLER_DESC &key, size_t seed = 0)
noexcept
524 QtPrivate::QHashCombine hash(seed);
525 seed = hash(seed, key.desc.Filter);
526 seed = hash(seed, key.desc.AddressU);
527 seed = hash(seed, key.desc.AddressV);
528 seed = hash(seed, key.desc.AddressW);
529 seed = hash(seed, key.desc.MipLODBias);
530 seed = hash(seed, key.desc.MaxAnisotropy);
531 seed = hash(seed, key.desc.ComparisonFunc);
533 seed = hash(seed, key.desc.MinLOD);
534 seed = hash(seed, key.desc.MaxLOD);
541struct Q_D3D12_SAMPLER_DESC_LIST
543 QVarLengthArray<Q_D3D12_SAMPLER_DESC, 8> descs;
545 friend bool operator==(
const Q_D3D12_SAMPLER_DESC_LIST &lhs,
const Q_D3D12_SAMPLER_DESC_LIST &rhs)
noexcept
547 return lhs.descs == rhs.descs;
550 friend bool operator!=(
const Q_D3D12_SAMPLER_DESC_LIST &lhs,
const Q_D3D12_SAMPLER_DESC_LIST &rhs)
noexcept
552 return !(lhs == rhs);
555 friend size_t qHash(
const Q_D3D12_SAMPLER_DESC_LIST &key, size_t seed = 0)
noexcept
557 return qHashRange(key.descs.cbegin(), key.descs.cend(), seed);
561struct QD3D12SamplerManager
563 const quint32 MAX_SAMPLERS = 512;
565 bool create(ID3D12Device *device);
568 QD3D12Descriptor getShaderVisibleDescriptor(
const D3D12_SAMPLER_DESC &desc);
572 QD3D12Descriptor getShaderVisibleDescriptors(
const QVarLengthArray<Q_D3D12_SAMPLER_DESC, 8> &descs);
574 ID3D12Device *device =
nullptr;
575 QD3D12ShaderVisibleDescriptorHeap shaderVisibleSamplerHeap;
586 QHash<Q_D3D12_SAMPLER_DESC, QD3D12Descriptor> gpuMap;
587 QHash<Q_D3D12_SAMPLER_DESC_LIST, QD3D12Descriptor> gpuArrayMap;
590enum QD3D12Stage { VS = 0, HS, DS, GS, PS, CS };
592static inline QD3D12Stage qd3d12_stage(QRhiShaderStage::Type type)
595 case QRhiShaderStage::Vertex:
597 case QRhiShaderStage::TessellationControl:
599 case QRhiShaderStage::TessellationEvaluation:
601 case QRhiShaderStage::Geometry:
603 case QRhiShaderStage::Fragment:
605 case QRhiShaderStage::Compute:
608 Q_UNREACHABLE_RETURN(VS);
611static inline D3D12_SHADER_VISIBILITY qd3d12_stageToVisibility(QD3D12Stage s)
615 return D3D12_SHADER_VISIBILITY_VERTEX;
617 return D3D12_SHADER_VISIBILITY_HULL;
619 return D3D12_SHADER_VISIBILITY_DOMAIN;
621 return D3D12_SHADER_VISIBILITY_GEOMETRY;
623 return D3D12_SHADER_VISIBILITY_PIXEL;
625 return D3D12_SHADER_VISIBILITY_ALL;
627 Q_UNREACHABLE_RETURN(D3D12_SHADER_VISIBILITY_ALL);
630static inline QRhiShaderResourceBinding::StageFlag qd3d12_stageToSrb(QD3D12Stage s)
634 return QRhiShaderResourceBinding::VertexStage;
636 return QRhiShaderResourceBinding::TessellationControlStage;
638 return QRhiShaderResourceBinding::TessellationEvaluationStage;
640 return QRhiShaderResourceBinding::GeometryStage;
642 return QRhiShaderResourceBinding::FragmentStage;
644 return QRhiShaderResourceBinding::ComputeStage;
646 Q_UNREACHABLE_RETURN(QRhiShaderResourceBinding::VertexStage);
649struct QD3D12ShaderStageData
652 QD3D12Stage stage = VS;
653 QShader::NativeResourceBindingMap nativeResourceBindingMap;
654 int pushConstantRegister = -1;
655 quint32 pushConstantSize = 0;
658struct QD3D12ShaderResourceBindings;
660struct QD3D12ShaderResourceVisitor
662 enum StorageOp { Load = 0, Store, LoadStore };
664 QD3D12ShaderResourceVisitor(
const QD3D12ShaderResourceBindings *srb,
665 const QD3D12ShaderStageData *stageData,
668 stageData(stageData),
669 stageCount(stageCount)
673 std::function<
void(QD3D12Stage,
const QRhiShaderResourceBinding::Data::UniformBufferData &,
int,
int)> uniformBuffer =
nullptr;
680 std::function<
void(QD3D12Stage,
const QRhiShaderResourceBinding::TextureAndSampler *,
int,
int)> textures =
nullptr;
681 std::function<
void(QD3D12Stage,
const QRhiShaderResourceBinding::TextureAndSampler *,
int,
int)> samplers =
nullptr;
682 std::function<
void(QD3D12Stage,
const QRhiShaderResourceBinding::Data::StorageImageData &, StorageOp,
int)> storageImage =
nullptr;
683 std::function<
void(QD3D12Stage,
const QRhiShaderResourceBinding::Data::StorageBufferData &, StorageOp,
int)> storageBuffer =
nullptr;
687 const QD3D12ShaderResourceBindings *srb;
688 const QD3D12ShaderStageData *stageData;
696 QRhiReadbackResult *result =
nullptr;
697 QD3D12StagingArea staging;
698 quint32 byteSize = 0;
700 quint32 bytesPerLine = 0;
702 QRhiTexture::Format format = QRhiTexture::UnknownFormat;
703 quint32 stagingRowPitch = 0;
709 quint32 stencilStagingOffset = 0;
710 quint32 stencilStagingRowPitch = 0;
714 quint32 bytesPerPixel = 0;
717struct QD3D12MipmapGenerator
719 void create(QRhiD3D12 *rhiD);
721 void generate(QD3D12CommandBuffer *cbD,
const QD3D12ObjectHandle &textureHandle);
723 bool ensureCreated();
724 bool buildPipeline();
726 QRhiD3D12 *rhiD =
nullptr;
727 QD3D12ObjectHandle rootSigHandle;
728 QD3D12ObjectHandle pipelineHandle;
729 bool createFailed =
false;
732struct QD3D12MipmapGenerator3D
734 void create(QRhiD3D12 *rhiD);
736 void generate(QD3D12CommandBuffer *cbD,
const QD3D12ObjectHandle &textureHandle);
738 bool ensureCreated();
739 bool buildPipeline();
741 QRhiD3D12 *rhiD =
nullptr;
742 QD3D12ObjectHandle rootSigHandle;
743 QD3D12ObjectHandle pipelineHandle;
744 bool createFailed =
false;
747struct QD3D12MemoryAllocator
749 bool create(ID3D12Device *device, IDXGIAdapter1 *adapter);
752 HRESULT createResource(D3D12_HEAP_TYPE heapType,
753 const D3D12_RESOURCE_DESC *resourceDesc,
754 D3D12_RESOURCE_STATES initialState,
755 const D3D12_CLEAR_VALUE *optimizedClearValue,
756 D3D12MA::Allocation **maybeAllocation,
760 void getBudget(D3D12MA::Budget *localBudget, D3D12MA::Budget *nonLocalBudget);
762 bool isUsingD3D12MA()
const {
return allocator !=
nullptr; }
764 ID3D12Device *device =
nullptr;
765 D3D12MA::Allocator *allocator =
nullptr;
768struct QD3D12Buffer :
public QRhiBuffer
770 QD3D12Buffer(QRhiImplementation *rhi, Type type, UsageFlags usage, quint32 size);
773 void destroy() override;
774 bool create() override;
775 QRhiBuffer::NativeBuffer nativeBuffer() override;
776 char *beginFullDynamicBufferUpdateForCurrentFrame() override;
777 void endFullDynamicBufferUpdateForCurrentFrame() override;
779 void executeHostWritesForFrameSlot(
int frameSlot);
781 QD3D12ObjectHandle handles[QD3D12_FRAMES_IN_FLIGHT] = {};
786 QVarLengthArray<HostWrite, 16> pendingHostWrites[QD3D12_FRAMES_IN_FLIGHT];
788 friend class QRhiD3D12;
789 friend struct QD3D12ShaderResourceBindings;
792struct QD3D12RenderBuffer :
public QRhiRenderBuffer
794 QD3D12RenderBuffer(QRhiImplementation *rhi,
796 const QSize &pixelSize,
799 QRhiTexture::Format backingFormatHint);
800 ~QD3D12RenderBuffer();
801 void destroy() override;
802 bool create() override;
803 QRhiTexture::Format backingFormat()
const override;
805 static const DXGI_FORMAT DS_FORMAT = DXGI_FORMAT_D24_UNORM_S8_UINT;
807 QD3D12ObjectHandle handle;
808 QD3D12Descriptor rtv;
809 QD3D12Descriptor dsv;
810 DXGI_FORMAT dxgiFormat;
811 DXGI_SAMPLE_DESC sampleDesc;
813 friend class QRhiD3D12;
816struct QD3D12Texture :
public QRhiTexture
818 QD3D12Texture(QRhiImplementation *rhi, Format format,
const QSize &pixelSize,
int depth,
819 int arraySize,
int sampleCount, Flags flags);
821 void destroy() override;
822 bool create() override;
823 bool createFrom(NativeTexture src) override;
824 NativeTexture nativeTexture() override;
825 void setNativeLayout(
int layout) override;
827 bool prepareCreate(QSize *adjustedSize =
nullptr);
830 QD3D12ObjectHandle handle;
831 QD3D12Descriptor srv;
832 DXGI_FORMAT dxgiFormat;
833 DXGI_FORMAT srvFormat;
834 DXGI_FORMAT rtFormat;
836 DXGI_SAMPLE_DESC sampleDesc;
837 QBitArray subresourceInitialized;
839 friend class QRhiD3D12;
840 friend struct QD3D12ShaderResourceBindings;
843struct QD3D12Sampler :
public QRhiSampler
845 QD3D12Sampler(QRhiImplementation *rhi, Filter magFilter, Filter minFilter, Filter mipmapMode,
846 AddressMode u, AddressMode v, AddressMode w);
848 void destroy() override;
849 bool create() override;
851 QD3D12Descriptor lookupOrCreateShaderVisibleDescriptor();
853 D3D12_SAMPLER_DESC desc = {};
854 QD3D12Descriptor shaderVisibleDescriptor;
856 friend class QRhiD3D12;
859struct QD3D12ShadingRateMap :
public QRhiShadingRateMap
861 QD3D12ShadingRateMap(QRhiImplementation *rhi);
862 ~QD3D12ShadingRateMap();
863 void destroy() override;
864 bool createFrom(QRhiTexture *src) override;
866 QD3D12ObjectHandle handle;
867 friend class QRhiD3D12;
870struct QD3D12RenderPassDescriptor :
public QRhiRenderPassDescriptor
872 QD3D12RenderPassDescriptor(QRhiImplementation *rhi);
873 ~QD3D12RenderPassDescriptor();
874 void destroy() override;
875 bool isCompatible(
const QRhiRenderPassDescriptor *other)
const override;
876 QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor()
const override;
877 QVector<quint32> serializedFormat()
const override;
879 void updateSerializedFormat();
881 static const int MAX_COLOR_ATTACHMENTS = 8;
882 int colorAttachmentCount = 0;
883 bool hasDepthStencil =
false;
884 int colorFormat[MAX_COLOR_ATTACHMENTS];
886 bool hasShadingRateMap =
false;
887 QVector<quint32> serializedFormatData;
890struct QD3D12RenderTargetData
892 QD3D12RenderTargetData(QRhiImplementation *) { }
894 QD3D12RenderPassDescriptor *rp =
nullptr;
898 int colorAttCount = 0;
900 QRhiRenderTargetAttachmentTracker::ResIdList currentResIdList;
901 static const int MAX_COLOR_ATTACHMENTS = QD3D12RenderPassDescriptor::MAX_COLOR_ATTACHMENTS;
902 D3D12_CPU_DESCRIPTOR_HANDLE rtv[MAX_COLOR_ATTACHMENTS];
903 D3D12_CPU_DESCRIPTOR_HANDLE dsv;
906struct QD3D12SwapChainRenderTarget :
public QRhiSwapChainRenderTarget
908 QD3D12SwapChainRenderTarget(QRhiImplementation *rhi, QRhiSwapChain *swapchain);
909 ~QD3D12SwapChainRenderTarget();
910 void destroy() override;
912 QSize pixelSize()
const override;
913 float devicePixelRatio()
const override;
914 int sampleCount()
const override;
916 QD3D12RenderTargetData d;
919struct QD3D12TextureRenderTarget :
public QRhiTextureRenderTarget
921 QD3D12TextureRenderTarget(QRhiImplementation *rhi,
922 const QRhiTextureRenderTargetDescription &desc,
924 ~QD3D12TextureRenderTarget();
925 void destroy() override;
927 QSize pixelSize()
const override;
928 float devicePixelRatio()
const override;
929 int sampleCount()
const override;
931 QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() override;
932 bool create() override;
934 QD3D12RenderTargetData d;
935 bool ownsRtv[QD3D12RenderTargetData::MAX_COLOR_ATTACHMENTS];
936 QD3D12Descriptor rtv[QD3D12RenderTargetData::MAX_COLOR_ATTACHMENTS];
937 bool ownsDsv =
false;
938 QD3D12Descriptor dsv;
939 friend class QRhiD3D12;
942struct QD3D12GraphicsPipeline;
943struct QD3D12ComputePipeline;
945struct QD3D12ShaderResourceBindings :
public QRhiShaderResourceBindings
947 QD3D12ShaderResourceBindings(QRhiImplementation *rhi);
948 ~QD3D12ShaderResourceBindings();
949 void destroy() override;
950 bool create() override;
951 void updateResources(UpdateFlags flags) override;
953 QD3D12ObjectHandle createRootSignature(
const QD3D12ShaderStageData *stageData,
int stageCount);
956 int pushConstantRootParamIndex = -1;
959 QVarLengthArray<D3D12_ROOT_PARAMETER1, 2> cbParams[6];
961 D3D12_ROOT_PARAMETER1 srvTables[6] = {};
962 QVarLengthArray<D3D12_DESCRIPTOR_RANGE1, 4> srvRanges[6];
963 quint32 currentSrvRangeOffset[6] = {};
965 QVarLengthArray<D3D12_ROOT_PARAMETER1, 4> samplerTables[6];
966 std::array<D3D12_DESCRIPTOR_RANGE1, 16> samplerRanges[6] = {};
967 int samplerRangeHeads[6] = {};
969 D3D12_ROOT_PARAMETER1 uavTables[6] = {};
970 QVarLengthArray<D3D12_DESCRIPTOR_RANGE1, 4> uavRanges[6];
971 quint32 currentUavRangeOffset[6] = {};
975 void visitUniformBuffer(QD3D12Stage s,
976 const QRhiShaderResourceBinding::Data::UniformBufferData &d,
979 void visitTextures(QD3D12Stage s,
980 const QRhiShaderResourceBinding::TextureAndSampler *d,
982 int baseShaderRegister);
983 void visitSamplers(QD3D12Stage s,
984 const QRhiShaderResourceBinding::TextureAndSampler *d,
986 int baseShaderRegister);
987 void visitStorageBuffer(QD3D12Stage s,
988 const QRhiShaderResourceBinding::Data::StorageBufferData &d,
989 QD3D12ShaderResourceVisitor::StorageOp op,
991 void visitStorageImage(QD3D12Stage s,
992 const QRhiShaderResourceBinding::Data::StorageImageData &d,
993 QD3D12ShaderResourceVisitor::StorageOp op,
1000 struct BindingCache {
1008 QVarLengthArray<CBuf, 2> cbufs[6];
1009 QVarLengthArray<D3D12_CPU_DESCRIPTOR_HANDLE, 8> srvs[6];
1010 QVarLengthArray<D3D12_GPU_DESCRIPTOR_HANDLE, 8> samplers[6];
1012 QD3D12ObjectHandle handle;
1013 D3D12_UNORDERED_ACCESS_VIEW_DESC desc;
1015 QVarLengthArray<Uav, 4> uavs[6];
1016 quint32 srvUavCount = 0;
1019 for (
int s = 0; s < 6; ++s) {
1022 samplers[s].clear();
1029 bool bindingCacheValid =
false;
1030 uint bindingCacheGeneration = 0;
1032 void rebuildBindingCache(
const QD3D12ShaderStageData *stageData,
int stageCount);
1036 void cacheUniformBuffer(QD3D12Stage s,
1037 const QRhiShaderResourceBinding::Data::UniformBufferData &d,
1040 void cacheTextures(QD3D12Stage s,
1041 const QRhiShaderResourceBinding::TextureAndSampler *d,
1043 int baseShaderRegister);
1044 void cacheSamplers(QD3D12Stage s,
1045 const QRhiShaderResourceBinding::TextureAndSampler *d,
1047 int baseShaderRegister);
1048 void cacheStorageBuffer(QD3D12Stage s,
1049 const QRhiShaderResourceBinding::Data::StorageBufferData &d,
1050 QD3D12ShaderResourceVisitor::StorageOp op,
1051 int shaderRegister);
1052 void cacheStorageImage(QD3D12Stage s,
1053 const QRhiShaderResourceBinding::Data::StorageImageData &d,
1054 QD3D12ShaderResourceVisitor::StorageOp op,
1055 int shaderRegister);
1057 struct BoundUniformBufferData {
1061 struct BoundSampledTextureData {
1064 uint texGeneration = 0;
1065 quint64 samplerId = 0;
1066 uint samplerGeneration = 0;
1068 QVarLengthArray<Entry, 1> d;
1070 struct BoundStorageImageData {
1074 struct BoundStorageBufferData {
1078 struct BoundResourceData {
1079 BoundUniformBufferData ubuf = {};
1080 BoundSampledTextureData stex;
1081 BoundStorageImageData simage = {};
1082 BoundStorageBufferData sbuf = {};
1084 QVarLengthArray<BoundResourceData, 8> boundResourceData;
1086 bool hasDynamicOffset =
false;
1087 uint generation = 0;
1088 QD3D12GraphicsPipeline *lastUsedGraphicsPipeline =
nullptr;
1089 QD3D12ComputePipeline *lastUsedComputePipeline =
nullptr;
1090 uint lastUsedPipelineGeneration = 0;
1092 friend class QRhiD3D12;
1093 friend struct QD3D12ShaderResourceVisitor;
1096struct QD3D12GraphicsPipeline :
public QRhiGraphicsPipeline
1098 QD3D12GraphicsPipeline(QRhiImplementation *rhi);
1099 ~QD3D12GraphicsPipeline();
1100 void destroy() override;
1101 bool create() override;
1103 QD3D12ObjectHandle handle;
1104 QD3D12ObjectHandle rootSigHandle;
1105 int pushConstantRootParamIndex = -1;
1106 std::array<QD3D12ShaderStageData, 5> stageData;
1107 D3D12_PRIMITIVE_TOPOLOGY topology;
1108 UINT viewInstanceMask = 0;
1109 uint generation = 0;
1110 friend class QRhiD3D12;
1113struct QD3D12ComputePipeline :
public QRhiComputePipeline
1115 QD3D12ComputePipeline(QRhiImplementation *rhi);
1116 ~QD3D12ComputePipeline();
1117 void destroy() override;
1118 bool create() override;
1120 QD3D12ObjectHandle handle;
1121 QD3D12ObjectHandle rootSigHandle;
1122 int pushConstantRootParamIndex = -1;
1123 QD3D12ShaderStageData stageData;
1124 uint generation = 0;
1125 friend class QRhiD3D12;
1128struct QD3D12CommandBuffer :
public QRhiCommandBuffer
1130 QD3D12CommandBuffer(QRhiImplementation *rhi);
1131 ~QD3D12CommandBuffer();
1132 void destroy() override;
1134 const QRhiNativeHandles *nativeHandles();
1136 D3D12GraphicsCommandList *cmdList =
nullptr;
1137 QRhiD3D12CommandBufferNativeHandles nativeHandlesStruct;
1147 recordingPass = NoPass;
1148 currentTarget =
nullptr;
1150 resetPerPassState();
1153 void resetPerPassState()
1155 currentGraphicsPipeline =
nullptr;
1156 currentComputePipeline =
nullptr;
1157 currentPipelineGeneration = 0;
1158 currentGraphicsSrb =
nullptr;
1159 currentComputeSrb =
nullptr;
1160 currentSrbGeneration = 0;
1161 currentIndexBuffer = {};
1162 currentIndexOffset = 0;
1163 currentIndexFormat = DXGI_FORMAT_R16_UINT;
1164 currentVertexBuffers = {};
1165 currentVertexOffsets = {};
1166 hasShadingRateSet =
false;
1167 hasShadingRateMapSet =
false;
1168 hasCustomScissorSet =
false;
1169 currentViewport = {};
1173 PassType recordingPass;
1174 QRhiRenderTarget *currentTarget;
1177 QD3D12GraphicsPipeline *currentGraphicsPipeline;
1178 QD3D12ComputePipeline *currentComputePipeline;
1179 uint currentPipelineGeneration;
1180 QRhiShaderResourceBindings *currentGraphicsSrb;
1181 QRhiShaderResourceBindings *currentComputeSrb;
1182 uint currentSrbGeneration;
1183 QD3D12ObjectHandle currentIndexBuffer;
1184 quint32 currentIndexOffset;
1185 DXGI_FORMAT currentIndexFormat;
1186 std::array<QD3D12ObjectHandle, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> currentVertexBuffers;
1187 std::array<quint32, D3D12_IA_VERTEX_INPUT_RESOURCE_SLOT_COUNT> currentVertexOffsets;
1188 bool hasShadingRateSet;
1189 bool hasShadingRateMapSet;
1190 bool hasCustomScissorSet;
1191 QRhiViewport currentViewport;
1194 double lastGpuTime = 0;
1197struct QD3D12SwapChain :
public QRhiSwapChain
1199 QD3D12SwapChain(QRhiImplementation *rhi);
1201 void destroy() override;
1203 QRhiCommandBuffer *currentFrameCommandBuffer() override;
1204 QRhiRenderTarget *currentFrameRenderTarget() override;
1205 QRhiRenderTarget *currentFrameRenderTarget(StereoTargetBuffer targetBuffer) override;
1207 QSize surfacePixelSize() override;
1208 bool isFormatSupported(Format f) override;
1209 QRhiSwapChainHdrInfo hdrInfo() override;
1211 QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() override;
1212 bool createOrResize() override;
1214 void releaseBuffers();
1215 void waitCommandCompletionForFrameSlot(
int frameSlot);
1216 void addCommandCompletionSignalForCurrentFrameSlot();
1217 void chooseFormats();
1219 QWindow *window =
nullptr;
1220 IDXGISwapChain1 *sourceSwapChain1 =
nullptr;
1221 IDXGISwapChain3 *swapChain =
nullptr;
1223 UINT swapInterval = 1;
1224 UINT swapChainFlags = 0;
1225 BOOL stereo =
false;
1226 DXGI_FORMAT colorFormat;
1227 DXGI_FORMAT srgbAdjustedColorFormat;
1228 DXGI_COLOR_SPACE_TYPE hdrColorSpace;
1229 IDCompositionTarget *dcompTarget =
nullptr;
1230 IDCompositionVisual *dcompVisual =
nullptr;
1231 static const UINT BUFFER_COUNT = 3;
1232 QD3D12ObjectHandle colorBuffers[BUFFER_COUNT];
1233 QD3D12Descriptor rtvs[BUFFER_COUNT];
1234 QD3D12Descriptor rtvsRight[BUFFER_COUNT];
1235 DXGI_SAMPLE_DESC sampleDesc;
1236 QD3D12ObjectHandle msaaBuffers[QD3D12_FRAMES_IN_FLIGHT];
1237 QD3D12Descriptor msaaRtvs[QD3D12_FRAMES_IN_FLIGHT];
1238 QD3D12RenderBuffer *ds =
nullptr;
1239 UINT currentBackBufferIndex = 0;
1240 QD3D12SwapChainRenderTarget rtWrapper;
1241 QD3D12SwapChainRenderTarget rtWrapperRight;
1242 QD3D12CommandBuffer cbWrapper;
1243 HANDLE frameLatencyWaitableObject =
nullptr;
1245 struct FrameResources {
1246 ID3D12Fence *fence =
nullptr;
1247 HANDLE fenceEvent =
nullptr;
1248 UINT64 fenceCounter = 0;
1249 D3D12GraphicsCommandList *cmdList =
nullptr;
1250 } frameRes[QD3D12_FRAMES_IN_FLIGHT];
1252 int currentFrameSlot = 0;
1253 int lastFrameLatencyWaitSlot = -1;
1256template<
typename T, D3D12_PIPELINE_STATE_SUBOBJECT_TYPE Type>
1257struct alignas(
void*) QD3D12PipelineStateSubObject
1259 D3D12_PIPELINE_STATE_SUBOBJECT_TYPE type = Type;
1263class QD3D12Adapter :
public QRhiAdapter
1266 QRhiDriverInfo info()
const override;
1269 QRhiDriverInfo adapterInfo;
1272class QRhiD3D12 :
public QRhiImplementation
1276 static constexpr quint32 SMALL_STAGING_AREA_BYTES_PER_FRAME_START = 512 * 1024;
1277 static constexpr quint32 SMALL_STAGING_AREA_BYTES_PER_FRAME_MAX = 16 * 1024 * 1024;
1280 static constexpr int SMALL_STAGING_AREA_LOW_DEMAND_FRAMES = 60;
1282 static constexpr quint32 SHADER_VISIBLE_CBV_SRV_UAV_HEAP_PER_FRAME_START_SIZE = 16384;
1284 QRhiD3D12(QRhiD3D12InitParams *params, QRhiD3D12NativeHandles *importDevice =
nullptr);
1286 bool create(QRhi::Flags flags) override;
1287 void destroy() override;
1288 QRhi::AdapterList enumerateAdaptersBeforeCreate(QRhiNativeHandles *nativeHandles)
const override;
1290 QRhiGraphicsPipeline *createGraphicsPipeline() override;
1291 QRhiComputePipeline *createComputePipeline() override;
1292 QRhiShaderResourceBindings *createShaderResourceBindings() override;
1293 QRhiBuffer *createBuffer(QRhiBuffer::Type type,
1294 QRhiBuffer::UsageFlags usage,
1295 quint32 size) override;
1296 QRhiRenderBuffer *createRenderBuffer(QRhiRenderBuffer::Type type,
1297 const QSize &pixelSize,
1299 QRhiRenderBuffer::Flags flags,
1300 QRhiTexture::Format backingFormatHint) override;
1301 QRhiTexture *createTexture(QRhiTexture::Format format,
1302 const QSize &pixelSize,
1306 QRhiTexture::Flags flags) override;
1307 QRhiSampler *createSampler(QRhiSampler::Filter magFilter,
1308 QRhiSampler::Filter minFilter,
1309 QRhiSampler::Filter mipmapMode,
1310 QRhiSampler:: AddressMode u,
1311 QRhiSampler::AddressMode v,
1312 QRhiSampler::AddressMode w) override;
1314 QRhiTextureRenderTarget *createTextureRenderTarget(
const QRhiTextureRenderTargetDescription &desc,
1315 QRhiTextureRenderTarget::Flags flags) override;
1317 QRhiShadingRateMap *createShadingRateMap() override;
1319 QRhiSwapChain *createSwapChain() override;
1320 QRhi::FrameOpResult beginFrame(QRhiSwapChain *swapChain, QRhi::BeginFrameFlags flags) override;
1321 QRhi::FrameOpResult endFrame(QRhiSwapChain *swapChain, QRhi::EndFrameFlags flags) override;
1322 QRhi::FrameOpResult beginOffscreenFrame(QRhiCommandBuffer **cb, QRhi::BeginFrameFlags flags) override;
1323 QRhi::FrameOpResult endOffscreenFrame(QRhi::EndFrameFlags flags) override;
1324 QRhi::FrameOpResult finish() override;
1326 void resourceUpdate(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) override;
1328 void beginPass(QRhiCommandBuffer *cb,
1329 QRhiRenderTarget *rt,
1330 const QColor &colorClearValue,
1331 const QRhiDepthStencilClearValue &depthStencilClearValue,
1332 QRhiResourceUpdateBatch *resourceUpdates,
1333 QRhiCommandBuffer::BeginPassFlags flags) override;
1334 void endPass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) override;
1336 void setGraphicsPipeline(QRhiCommandBuffer *cb,
1337 QRhiGraphicsPipeline *ps) override;
1339 void setShaderResources(QRhiCommandBuffer *cb,
1340 QRhiShaderResourceBindings *srb,
1341 int dynamicOffsetCount,
1342 const QRhiCommandBuffer::DynamicOffset *dynamicOffsets) override;
1344 void setVertexInput(QRhiCommandBuffer *cb,
1345 int startBinding,
int bindingCount,
const QRhiCommandBuffer::VertexInput *bindings,
1346 QRhiBuffer *indexBuf, quint32 indexOffset,
1347 QRhiCommandBuffer::IndexFormat indexFormat) override;
1349 void setViewport(QRhiCommandBuffer *cb,
const QRhiViewport &viewport) override;
1350 void setScissor(QRhiCommandBuffer *cb,
const QRhiScissor &scissor) override;
1351 void setBlendConstants(QRhiCommandBuffer *cb,
const QColor &c) override;
1352 void setStencilRef(QRhiCommandBuffer *cb, quint32 refValue) override;
1353 void setPushConstants(QRhiCommandBuffer *cb, quint32 offset, quint32 size,
const void *data) override;
1354 void setShadingRate(QRhiCommandBuffer *cb,
const QSize &coarsePixelSize) override;
1356 void draw(QRhiCommandBuffer *cb, quint32 vertexCount,
1357 quint32 instanceCount, quint32 firstVertex, quint32 firstInstance) override;
1359 void drawIndexed(QRhiCommandBuffer *cb, quint32 indexCount,
1360 quint32 instanceCount, quint32 firstIndex,
1361 qint32 vertexOffset, quint32 firstInstance) override;
1363 void drawIndirect(QRhiCommandBuffer *cb, QRhiBuffer *indirectBuffer,
1364 quint32 indirectBufferOffset, quint32 drawCount, quint32 stride) override;
1366 void drawIndexedIndirect(QRhiCommandBuffer *cb, QRhiBuffer *indirectBuffer,
1367 quint32 indirectBufferOffset, quint32 drawCount, quint32 stride) override;
1369 void debugMarkBegin(QRhiCommandBuffer *cb,
const QByteArray &name) override;
1370 void debugMarkEnd(QRhiCommandBuffer *cb) override;
1371 void debugMarkMsg(QRhiCommandBuffer *cb,
const QByteArray &msg) override;
1373 void beginComputePass(QRhiCommandBuffer *cb,
1374 QRhiResourceUpdateBatch *resourceUpdates,
1375 QRhiCommandBuffer::BeginPassFlags flags) override;
1376 void endComputePass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates) override;
1377 void setComputePipeline(QRhiCommandBuffer *cb, QRhiComputePipeline *ps) override;
1378 void dispatch(QRhiCommandBuffer *cb,
int x,
int y,
int z) override;
1379 void dispatchIndirect(QRhiCommandBuffer *cb, QRhiBuffer *indirectBuffer,
1380 quint32 indirectBufferOffset) override;
1382 void drawIndirectCount(QRhiCommandBuffer *cb,
1383 QRhiBuffer *indirectBuffer, quint32 indirectBufferOffset,
1384 QRhiBuffer *countBuffer, quint32 countBufferOffset,
1385 quint32 maxDrawCount, quint32 stride) override;
1386 void drawIndexedIndirectCount(QRhiCommandBuffer *cb,
1387 QRhiBuffer *indirectBuffer, quint32 indirectBufferOffset,
1388 QRhiBuffer *countBuffer, quint32 countBufferOffset,
1389 quint32 maxDrawCount, quint32 stride) override;
1390 ID3D12CommandSignature *indirectDrawCommandSignature(
bool indexed, quint32 stride);
1392 const QRhiNativeHandles *nativeHandles(QRhiCommandBuffer *cb) override;
1393 void beginExternal(QRhiCommandBuffer *cb) override;
1394 void endExternal(QRhiCommandBuffer *cb) override;
1395 double lastCompletedGpuTime(QRhiCommandBuffer *cb) override;
1397 QList<
int> supportedSampleCounts()
const override;
1398 QList<QSize> supportedShadingRates(
int sampleCount)
const override;
1399 int ubufAlignment()
const override;
1400 bool isYUpInFramebuffer()
const override;
1401 bool isYUpInNDC()
const override;
1402 bool isClipDepthZeroToOne()
const override;
1403 QMatrix4x4 clipSpaceCorrMatrix()
const override;
1404 bool isTextureFormatSupported(QRhiTexture::Format format, QRhiTexture::Flags flags)
const override;
1405 bool isFeatureSupported(QRhi::Feature feature)
const override;
1406 int resourceLimit(QRhi::ResourceLimit limit)
const override;
1407 const QRhiNativeHandles *nativeHandles() override;
1408 QRhiDriverInfo driverInfo()
const override;
1409 QRhiStats statistics() override;
1410 bool makeThreadLocalNativeContextCurrent() override;
1411 void setQueueSubmitParams(QRhiNativeHandles *params) override;
1412 void releaseCachedResources() override;
1413 bool isDeviceLost()
const override;
1415 QByteArray pipelineCacheData() override;
1416 void setPipelineCacheData(
const QByteArray &data) override;
1424 void recordSmallStagingAreaDemand(quint32 byteSize)
1426 smallStagingAreaBytesNeeded[currentFrameSlot] += byteSize;
1428 void resetAndResizeSmallStagingArea(
int frameSlot);
1430 ID3D12PipelineState *loadOrCreatePipelineState(
const D3D12_PIPELINE_STATE_STREAM_DESC *streamDesc,
1431 const QByteArray &cacheKey,
1433 bool ensurePipelineLibrary();
1434 bool createPipelineLibrary(
const QByteArray &blob);
1435 void destroyPipelineLibrary();
1438 DXGI_SAMPLE_DESC effectiveSampleDesc(
int sampleCount, DXGI_FORMAT format)
const;
1439 bool ensureDirectCompositionDevice();
1440 bool startCommandListForCurrentFrameSlot(D3D12GraphicsCommandList **cmdList);
1441 void enqueueResourceUpdates(QD3D12CommandBuffer *cbD, QRhiResourceUpdateBatch *resourceUpdates);
1442 void finishActiveReadbacks(
bool forced =
false);
1443 bool ensureShaderVisibleDescriptorHeapCapacity(QD3D12ShaderVisibleDescriptorHeap *h,
1444 D3D12_DESCRIPTOR_HEAP_TYPE type,
1446 quint32 neededDescriptorCount,
1448 void bindShaderVisibleHeaps(QD3D12CommandBuffer *cbD);
1449 void setDefaultScissor(QD3D12CommandBuffer *cbD);
1451 bool debugLayer =
false;
1452#ifdef QRHI_D3D12_INFOQUEUE1_AVAILABLE
1453 ID3D12InfoQueue1 *infoQueue1 =
nullptr;
1454 DWORD infoQueueCallbackCookie = 0;
1456 UINT maxFrameLatency = 2;
1457 ID3D12Device2 *dev =
nullptr;
1458 D3D_FEATURE_LEVEL minimumFeatureLevel = D3D_FEATURE_LEVEL(0);
1459 LUID adapterLuid = {};
1460 bool importedDevice =
false;
1461 bool importedCommandQueue =
false;
1462 QRhi::Flags rhiFlags;
1463 IDXGIFactory2 *dxgiFactory =
nullptr;
1464 bool supportsAllowTearing =
false;
1465 IDXGIAdapter1 *activeAdapter =
nullptr;
1466 QRhiDriverInfo driverInfoStruct;
1467 QRhiD3D12NativeHandles nativeHandlesStruct;
1468 bool deviceLost =
false;
1469 ID3D12CommandQueue *cmdQueue =
nullptr;
1470 ID3D12Fence *fullFence =
nullptr;
1471 HANDLE fullFenceEvent =
nullptr;
1472 UINT64 fullFenceCounter = 0;
1473 ID3D12CommandAllocator *cmdAllocators[QD3D12_FRAMES_IN_FLIGHT] = {};
1474 QD3D12MemoryAllocator vma;
1475 QD3D12CpuDescriptorPool rtvPool;
1476 QD3D12CpuDescriptorPool dsvPool;
1477 QD3D12CpuDescriptorPool cbvSrvUavPool;
1478 QD3D12ObjectPool<QD3D12Resource> resourcePool;
1479 QD3D12ObjectPool<QD3D12Pipeline> pipelinePool;
1480 QD3D12ObjectPool<QD3D12RootSignature> rootSignaturePool;
1481 QD3D12ReleaseQueue releaseQueue;
1482 QD3D12ResourceBarrierGenerator barrierGen;
1483 QD3D12SamplerManager samplerMgr;
1484 QD3D12MipmapGenerator mipmapGen;
1485 QD3D12MipmapGenerator3D mipmapGen3D;
1486 QD3D12StagingArea smallStagingAreas[QD3D12_FRAMES_IN_FLIGHT];
1487 quint32 smallStagingAreaBytesNeeded[QD3D12_FRAMES_IN_FLIGHT] = {};
1488 int smallStagingAreaLowDemandFrames[QD3D12_FRAMES_IN_FLIGHT] = {};
1489 QD3D12ShaderVisibleDescriptorHeap shaderVisibleCbvSrvUavHeap;
1490 UINT64 timestampTicksPerSecond = 0;
1491 QD3D12QueryHeap timestampQueryHeap;
1492 QD3D12StagingArea timestampReadbackArea;
1493 IDCompositionDevice *dcompDevice =
nullptr;
1494 QD3D12SwapChain *currentSwapChain =
nullptr;
1495 QSet<QD3D12SwapChain *> swapchains;
1496 QD3D12ShaderBytecodeCache shaderBytecodeCache {
"Shader bytecode cache" };
1497 QVarLengthArray<QD3D12Readback, 4> activeReadbacks;
1498 bool offscreenActive =
false;
1499 QD3D12CommandBuffer *offscreenCb[QD3D12_FRAMES_IN_FLIGHT] = {};
1500 UINT shadingRateImageTileSize = 0;
1501 ID3D12CommandSignature *drawCommandSignature =
nullptr;
1502 ID3D12CommandSignature *drawIndexedCommandSignature =
nullptr;
1503 ID3D12CommandSignature *dispatchCommandSignature =
nullptr;
1504#ifdef QRHI_D3D12_PIPELINE_LIBRARY_AVAILABLE
1505 ID3D12PipelineLibrary1 *pipelineLibrary =
nullptr;
1508 QByteArray pipelineLibraryBlob;
1510 QSet<QByteArray> pipelineLibraryNames;
1512 QHash<quint32, ID3D12CommandSignature *> drawCommandSignaturesByStride;
1513 QHash<quint32, ID3D12CommandSignature *> drawIndexedCommandSignaturesByStride;
1516 bool multiView =
false;
1517 bool textureViewFormat =
false;
1519 bool vrsMap =
false;
1520 bool vrsAdditionalRates =
false;