UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
D3D12Query.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "D3D12RHICommon.h"
6#include "D3D12Residency.h"
7#include "D3D12Resources.h"
8#include "RHIResources.h"
9
10class FD3D12SyncPoint;
13
15{
16 None,
20#if RHI_NEW_GPU_PROFILER
21
22 // Top-of-pipe GPU timestamp, converted to the CPU clock domain (i.e. relative to FPlatformTime::Cycles64())
24
25 // Bottom-of-pipe GPU timestamp, converted to the CPU clock domain (i.e. relative to FPlatformTime::Cycles64())
27
28#else
32 IdleEnd,
33#endif
35};
36
38{
39 // Query result should be written before any future command list work is started.
41
42 // Query result should be written after all prior command list work has completed.
44};
45
46// Wraps an ID3D12QueryHeap and its readback buffer. Used by command contexts to create timestamp and occlusion queries.
47// Ref-counting is used to recycle the heaps on parent device when all refering command lists have completed on the GPU.
49{
50 // All query heaps are allocated to fill a single 64KB page
51 static constexpr uint32 MaxHeapSize = 65536;
52
53private:
54 friend FD3D12Device;
56
57 FD3D12QueryHeap(FD3D12QueryHeap const&) = delete;
59
61
62public:
64
69
70 // The byte size of a result for a single query
72 {
73 switch (QueryType)
74 {
75 default: checkNoEntry(); [[fallthrough]];
78 return sizeof(uint64);
79
82 }
83 }
84
85 ID3D12QueryHeap* GetD3DQueryHeap() const { return D3DQueryHeap; }
86 FD3D12Resource* GetResultBuffer() const { return ResultBuffer; }
87
88 FD3D12ResidencyHandle& GetHeapResidencyHandle() { return ResidencyHandle; }
89
90 // Ref-counting used for object pool recycling.
91 uint32 AddRef() { return uint32(NumRefs.Increment()); }
93
94private:
96 uint8 const* ResultPtr = nullptr;
98 FD3D12ResidencyHandle ResidencyHandle;
99
100 FThreadSafeCounter NumRefs;
101};
102
104{
105 uint32 Start = 0, End = 0;
106
107 FD3D12QueryRange() = default;
112
113 inline bool IsFull(FD3D12QueryHeap* Heap) const;
114
115 bool operator == (FD3D12QueryRange const& RHS) const
116 {
117 return Start == RHS.Start
118 && End == RHS.End;
119 }
120
121 bool operator < (FD3D12QueryRange const& RHS) const
122 {
123 return Start < RHS.Start;
124 }
125};
126
127// The location of a single (timestamp or occlusion) query result.
129{
130 // The heap in which the result is contained.
132
133 // The index of the query within the heap.
135
137
138 // The location into which the result is written by the interrupt thread.
139 void* Target = nullptr;
140
141 // Reads the query result from the heap
142 inline void CopyResultTo(void* Dst) const;
143
144 template <typename TValueType>
145 inline TValueType GetResult() const;
146
154
155 operator bool() const { return Heap != nullptr; }
156};
157
158inline void FD3D12QueryLocation::CopyResultTo(void* Dst) const
159{
160 check(Dst);
161 check(Index < Heap->NumQueries);
162 check(Heap->ResultPtr);
163
164 void const* Src = Heap->ResultPtr + Index * Heap->GetResultSize();
165 FMemory::Memcpy(Dst, Src, Heap->GetResultSize());
166}
167
168template <typename TValueType>
170{
171 check(sizeof(TValueType) >= Heap->GetResultSize());
172
175 return Value;
176}
177
179{
180 return End >= Heap->NumQueries;
181}
182
184{
185public:
191
195
196 // Allocate a query on a query heap, returning its location.
197 // The "target" is where the interrupt thread will write the result when completed by the GPU.
198 FD3D12QueryLocation Allocate(ED3D12QueryType Type, void* Target);
199
200 // Resets the allocator and returns the used query ranges
202
203 bool HasQueries() const
204 {
205 return !(CurrentRange == nullptr || CurrentRange->Start == CurrentRange->End);
206 }
207
208private:
210
211 FD3D12QueryHeap* CurrentHeap = nullptr;
212 FD3D12QueryRange* CurrentRange = nullptr;
213};
214
216class FD3D12RenderQuery : public FRHIRenderQuery, public FD3D12DeviceChild, public FD3D12LinkedAdapterObject<FD3D12RenderQuery>
217{
218public:
221
223
224 // Signaled when the result is available. Nullptr if the query has never been used.
226
227 // The query result, read from the GPU. Heap allocated since it is
228 // accessed by the interrupt thread, and needs to outlive the RHI object.
230
231 // The current query location for occlusion queries.
233};
234
235template<>
#define check(expr)
Definition AssertionMacros.h:314
#define checkNoEntry()
Definition AssertionMacros.h:316
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
ED3D12QueryType
Definition D3D12Query.h:15
ED3D12QueryPosition
Definition D3D12Query.h:38
ED3D12QueueType
Definition D3D12Queue.h:11
const bool
Definition NetworkReplayStreaming.h:178
ERenderQueryType
Definition RHIDefinitions.h:258
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition D3D12RHICommon.h:78
FD3D12Device * Parent
Definition D3D12RHICommon.h:80
Definition D3D12Device.h:176
Definition D3D12RHICommon.h:164
Definition D3D12Query.h:184
void CloseAndReset(TMap< TRefCountPtr< FD3D12QueryHeap >, TArray< FD3D12QueryRange > > &OutRanges)
Definition D3D12CommandContext.cpp:531
D3D12_QUERY_TYPE const QueryType
Definition D3D12Query.h:194
bool HasQueries() const
Definition D3D12Query.h:203
FD3D12QueryAllocator(FD3D12Device *Device, ED3D12QueueType const QueueType, D3D12_QUERY_TYPE QueryType)
Definition D3D12Query.h:186
FD3D12QueryLocation Allocate(ED3D12QueryType Type, void *Target)
Definition D3D12CommandContext.cpp:505
ED3D12QueueType const QueueType
Definition D3D12Query.h:193
FD3D12Device *const Device
Definition D3D12Query.h:192
Definition D3D12Query.h:49
FD3D12ResidencyHandle & GetHeapResidencyHandle()
Definition D3D12Query.h:88
FD3D12Device *const Device
Definition D3D12Query.h:65
D3D12_QUERY_TYPE const QueryType
Definition D3D12Query.h:66
ID3D12QueryHeap * GetD3DQueryHeap() const
Definition D3D12Query.h:85
uint32 GetResultSize() const
Definition D3D12Query.h:71
D3D12_QUERY_HEAP_TYPE const HeapType
Definition D3D12Query.h:67
FD3D12Resource * GetResultBuffer() const
Definition D3D12Query.h:86
uint32 AddRef()
Definition D3D12Query.h:91
uint32 Release()
Definition D3D12Query.cpp:145
~FD3D12QueryHeap()
Definition D3D12Query.cpp:127
uint32 const NumQueries
Definition D3D12Query.h:68
Definition D3D12Query.h:217
~FD3D12RenderQuery()
Definition D3D12Query.cpp:36
uint64 * Result
Definition D3D12Query.h:229
FD3D12QueryLocation ActiveLocation
Definition D3D12Query.h:232
ERenderQueryType const Type
Definition D3D12Query.h:222
FD3D12SyncPointRef SyncPoint
Definition D3D12Query.h:225
Definition D3D12Resources.h:181
Definition D3D12RHICommon.h:128
Definition D3D12Submission.h:76
Definition RHIResources.h:2444
Definition ThreadSafeCounter.h:14
int32 Increment()
Definition ThreadSafeCounter.h:52
Definition Array.h:670
Definition UnrealString.h.inl:34
U16 Index
Definition radfft.cpp:71
Definition D3D12Query.h:129
FD3D12QueryLocation(FD3D12QueryHeap *Heap, uint32 Index, ED3D12QueryType Type, void *Target)
Definition D3D12Query.h:148
FD3D12QueryLocation()=default
FD3D12QueryHeap * Heap
Definition D3D12Query.h:131
TValueType GetResult() const
Definition D3D12Query.h:169
ED3D12QueryType Type
Definition D3D12Query.h:136
void CopyResultTo(void *Dst) const
Definition D3D12Query.h:158
void * Target
Definition D3D12Query.h:139
Definition D3D12Query.h:104
uint32 Start
Definition D3D12Query.h:105
FD3D12QueryRange()=default
bool IsFull(FD3D12QueryHeap *Heap) const
Definition D3D12Query.h:178
bool operator<(FD3D12QueryRange const &RHS) const
Definition D3D12Query.h:121
bool operator==(FD3D12QueryRange const &RHS) const
Definition D3D12Query.h:115
uint32 End
Definition D3D12Query.h:105
FD3D12QueryRange(uint32 Start, uint32 End)
Definition D3D12Query.h:108
Definition D3D12Residency.h:46
static UE_FORCEINLINE_HINT void * Memcpy(void *Dest, const void *Src, SIZE_T Count)
Definition UnrealMemory.h:160
FD3D12RenderQuery TConcreteType
Definition D3D12Query.h:238
Definition D3D12RHICommon.h:374