Plasma Engine  2.0
Loading...
Searching...
No Matches
QueryPoolDX11.h
1#pragma once
2
3#include <RendererDX11/RendererDX11DLL.h>
4#include <d3d11.h>
5
6struct ID3D11Query;
8
10class PL_RENDERERDX11_DLL plQueryPoolDX11
11{
12public:
14
16 plResult Initialize();
17 void DeInitialize();
18
19 void BeginFrame();
20 void EndFrame();
21
22 plGALTimestampHandle InsertTimestamp();
23
28 plEnum<plGALAsyncResult> GetTimestampResult(plGALTimestampHandle hTimestamp, plTime& out_result);
29
30 plGALPoolHandle BeginOcclusionQuery(plEnum<plGALQueryType> type);
31 void EndOcclusionQuery(plGALPoolHandle hPool);
32 plEnum<plGALAsyncResult> GetOcclusionQueryResult(plGALPoolHandle hPool, plUInt64& out_uiQueryResult);
33
34private:
35 static constexpr plUInt32 s_uiRetainFrames = 4;
36 static constexpr plUInt64 s_uiPredicateFlag = PL_BIT(19);
37 static constexpr double s_fInvalid = -1.0;
38
39 struct PerFrameData
40 {
41 plGALFenceHandle m_hFence;
42 ID3D11Query* m_pDisjointTimerQuery = nullptr;
43 double m_fInvTicksPerSecond = s_fInvalid;
44 plUInt32 m_uiReadyFrames = 0;
45 plUInt64 m_uiFrameCounter = plUInt64(-1);
46 };
47
48private:
49 PerFrameData GetFreeFrame();
50
51private:
52 plGALDeviceDX11* m_pDevice = nullptr;
53
54 struct Pool
55 {
56 Pool(plAllocator* pAllocator);
57
58 plResult Initialize(plGALDeviceDX11* pDevice, D3D11_QUERY queryType, plUInt32 uiCount);
59 void DeInitialize();
60
61 plGALPoolHandle CreateQuery();
62 ID3D11Query* GetQuery(plGALPoolHandle hPool);
63 template <typename T>
64 plEnum<plGALAsyncResult> GetResult(plGALPoolHandle hPool, T& out_uiResult)
65 {
66 ID3D11Query* pQuery = GetQuery(hPool);
67 HRESULT res = m_pDevice->GetDXImmediateContext()->GetData(pQuery, &out_uiResult, sizeof(out_uiResult), D3D11_ASYNC_GETDATA_DONOTFLUSH);
68 if (res == S_FALSE)
69 {
71 }
72 else if (res == S_OK)
73 {
75 }
76 else
77 {
79 }
80 }
81
82 // #TODO_DX11 Replace ring buffer with proper pool like in Vulkan to prevent buffer overrun.
84 plUInt32 m_uiNextTimestamp = 0;
85 plGALDeviceDX11* m_pDevice = nullptr;
86 };
87
88 // Pools
89 Pool m_TimestampPool;
90 Pool m_OcclusionPool;
91 Pool m_OcclusionPredicatePool;
92
93 // Disjoint timer and frame meta data needed for timestamps
94 plDeque<PerFrameData> m_PendingFrames;
95 plDeque<PerFrameData> m_FreeFrames;
96 plUInt64 m_uiFirstFrameIndex = 0;
97
98 plTime m_SyncTimeDiff;
99 bool m_bSyncTimeNeeded = true;
100};
Base class for all memory allocators.
Definition Allocator.h:23
Definition Deque.h:270
Definition DynamicArray.h:81
The DX11 device implementation of the graphics abstraction layer.
Definition DeviceDX11.h:32
Pool for GPU queries.
Definition QueryPoolDX11.h:11
A custom enum implementation that allows to define the underlying storage type to control its memory ...
Definition Enum.h:37
@ Pending
The async operation is still running, retry later.
Definition RendererFoundationDLL.h:351
@ Ready
The async operation has finished and the result is ready.
Definition RendererFoundationDLL.h:350
@ Expired
The async operation is too old and the result was thrown away. Pending results should be queried ever...
Definition RendererFoundationDLL.h:352
A generic id class that holds an id combined of an instance index and a generation counter.
Definition Id.h:52
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
The time class encapsulates a double value storing the time in seconds.
Definition Time.h:12