CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
GPUQuery.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Common.hpp"
11
13
16
17#include <chrono>
18
19namespace CeresEngine {
20
21 class GPUQuery {};
22
23 // ---------------------------------------------------------------------------------------------
24
27
35
36 public: // Reflection
39 template<typename Processor> static constexpr void reflect(Processor&& RTTI) {}
40 };
41
44 class GPUTimerQuery : public GPUQuery, public TDeviceObject<GPUTimerQueryDescriptor> {
45 public:
48
50 GPUTimerQuery(const GPUTimerQuery&) = delete;
52
56
57 public: // Events
60
61 public:
71 virtual void begin(GPUCommandBuffer& commandBuffer, UInt32 timerIndex = 0) = 0;
72
78 virtual void end(GPUCommandBuffer& commandBuffer, UInt32 timerIndex = 0) = 0;
79
83 [[nodiscard]] virtual bool isComplete(UInt32 timerIndex = 0) const = 0;
84
89 virtual void wait(UInt32 timerIndex = 0) const = 0;
90
97
100 virtual void reset() = 0;
101
102 public:
104 class Scope {
105 private:
108
111
114
115 public:
120 explicit Scope(GPUCommandBuffer& commandBuffer, GPUTimerQuery& query, const UInt32 timerIndex = 0)
121 : mCommandBuffer(&commandBuffer), mQuery(&query), mTimerIndex(timerIndex) {
123 }
124
126 explicit Scope(const GPUCommandBufferPtr& commandBuffer, GPUTimerQuery& query, const UInt32 timerIndex = 0) : Scope(*commandBuffer, query, timerIndex) {}
127
129 explicit Scope(GPUCommandBuffer& commandBuffer, const GPUTimerQueryPtr& query, const UInt32 timerIndex = 0) : Scope(commandBuffer, *query, timerIndex) {}
130
132 explicit Scope(const GPUCommandBufferPtr& commandBuffer, const GPUTimerQueryPtr& query, const UInt32 timerIndex = 0)
133 : Scope(*commandBuffer, *query, timerIndex) {}
134
137
138 public:
140 void reset() {
141 if(mQuery != nullptr) {
143 mQuery = nullptr;
144 mCommandBuffer = nullptr;
145 }
146 }
147 };
148
157 template<typename Func> decltype(auto) with(GPUCommandBuffer& commandBuffer, Func&& func) {
158 const Scope scope(commandBuffer, *this);
159 return func();
160 }
161
163 template<typename Func> decltype(auto) with(GPUCommandBuffer& commandBuffer, const UInt32 timerIndex, Func&& func) {
164 const Scope scope(commandBuffer, *this, timerIndex);
165 return func();
166 }
167 };
168
169 // ---------------------------------------------------------------------------------------------
170
172 public: // Reflection
175 template<typename Processor> static constexpr void reflect(Processor&& RTTI) {}
176 };
177
178 class GPUOcclusionQuery : public GPUQuery, public TDeviceObject<GPUOcclusionQueryDescriptor> {
179 public:
182
186
190
191 public:
201 virtual void begin(GPUCommandBuffer& commandBuffer) = 0;
202
208 virtual void end(GPUCommandBuffer& commandBuffer) = 0;
209
210 public:
212 class Scope {
213 private:
216
219
220 public:
224 explicit Scope(GPUCommandBuffer& commandBuffer, GPUOcclusionQuery& query)
225 : mCommandBuffer(&commandBuffer), mQuery(&query) {
227 }
228
230 explicit Scope(const GPUCommandBufferPtr& commandBuffer, GPUOcclusionQuery& query) : Scope(*commandBuffer, query) {}
231
233 explicit Scope(GPUCommandBuffer& commandBuffer, const GPUOcclusionQueryPtr& query) : Scope(commandBuffer, *query) {}
234
236 explicit Scope(const GPUCommandBufferPtr& commandBuffer, const GPUOcclusionQueryPtr& query)
237 : Scope(*commandBuffer, *query) {}
238
241
242 public:
244 void reset() {
245 if(mQuery != nullptr) {
247 mQuery = nullptr;
248 mCommandBuffer = nullptr;
249 }
250 }
251 };
252
261 template<typename Func> decltype(auto) with(GPUCommandBuffer& commandBuffer, Func&& func) {
262 const Scope scope(commandBuffer, *this);
263 return func();
264 }
265 };
266
267} // namespace CeresEngine
268
271
#define CE_REFLECT_HASH(T)
Definition Hash.hpp:89
A retain-release type of smart pointer.
Definition SmartPtr.hpp:132
Definition GPUCommandBuffer.hpp:77
A helper class that begins a scoped occlusion query.
Definition GPUQuery.hpp:212
Scope(const GPUCommandBufferPtr &commandBuffer, GPUOcclusionQuery &query)
Definition GPUQuery.hpp:230
Scope(const GPUCommandBufferPtr &commandBuffer, const GPUOcclusionQueryPtr &query)
Creates a new scope and calls begin() on the query.
Definition GPUQuery.hpp:236
GPUCommandBuffer * mCommandBuffer
The command buffer to which commands are being recorded on.
Definition GPUQuery.hpp:215
void reset()
Unbound the pipeline from the command buffer.
Definition GPUQuery.hpp:244
Scope(GPUCommandBuffer &commandBuffer, const GPUOcclusionQueryPtr &query)
Creates a new scope and calls begin() on the query.
Definition GPUQuery.hpp:233
~Scope() noexcept
Unbound the pipeline from the command buffer.
Definition GPUQuery.hpp:240
Scope(GPUCommandBuffer &commandBuffer, GPUOcclusionQuery &query)
Creates a new scope and calls begin() on the query.
Definition GPUQuery.hpp:224
GPUOcclusionQuery * mQuery
The occlusion query to be bound within the scope.
Definition GPUQuery.hpp:218
Definition GPUQuery.hpp:178
virtual void end(GPUCommandBuffer &commandBuffer)=0
Ends the occlusion query.
virtual void begin(GPUCommandBuffer &commandBuffer)=0
Begins the occlusion query.
GPUOcclusionQuery(GPUOcclusionQuery &&)=delete
Deleted move constructor.
decltype(auto) with(GPUCommandBuffer &commandBuffer, Func &&func)
Measures the time the GPU takes to run the commands run in commandBuffer from inside the given func.
Definition GPUQuery.hpp:261
GPUOcclusionQuery(const GPUOcclusionQuery &)=delete
Deleted copy constructor.
GPUOcclusionQuery & operator=(GPUOcclusionQuery &&)=delete
GPUOcclusionQuery & operator=(const GPUOcclusionQuery &)=delete
Definition GPUQuery.hpp:21
A helper class that begins a scoped timer query.
Definition GPUQuery.hpp:104
UInt32 mTimerIndex
The index of the timer to be started.
Definition GPUQuery.hpp:113
Scope(GPUCommandBuffer &commandBuffer, GPUTimerQuery &query, const UInt32 timerIndex=0)
Creates a new scope and calls begin() on the query.
Definition GPUQuery.hpp:120
Scope(const GPUCommandBufferPtr &commandBuffer, GPUTimerQuery &query, const UInt32 timerIndex=0)
Creates a new scope and calls begin() on the query.
Definition GPUQuery.hpp:126
void reset()
Unbound the pipeline from the command buffer.
Definition GPUQuery.hpp:140
~Scope() noexcept
Unbound the pipeline from the command buffer.
Definition GPUQuery.hpp:136
Scope(GPUCommandBuffer &commandBuffer, const GPUTimerQueryPtr &query, const UInt32 timerIndex=0)
Definition GPUQuery.hpp:129
GPUTimerQuery * mQuery
The timer query to be bound within the scope.
Definition GPUQuery.hpp:110
Scope(const GPUCommandBufferPtr &commandBuffer, const GPUTimerQueryPtr &query, const UInt32 timerIndex=0)
Definition GPUQuery.hpp:132
GPUCommandBuffer * mCommandBuffer
The command buffer to which commands are being recorded on.
Definition GPUQuery.hpp:107
A GPU query that allows measuring the time a GPU device takes execute a set of commands on a command ...
Definition GPUQuery.hpp:44
GPUTimerQuery & operator=(const GPUTimerQuery &)=delete
Event< void(GPUTimerQueryResult)> didComplete
An event triggered whenever the query result becomes available.
Definition GPUQuery.hpp:59
GPUTimerQuery(GPUTimerQuery &&)=delete
Deleted move constructor.
virtual bool isComplete(UInt32 timerIndex=0) const =0
Checks if the timer query is complete and ready to get the value.
decltype(auto) with(GPUCommandBuffer &commandBuffer, Func &&func)
Measures the time the GPU takes to run the commands run in commandBuffer from inside the given func.
Definition GPUQuery.hpp:157
virtual GPUTimerQueryResult getValue(UInt32 timerIndex=0) const =0
Gets the amount of seconds the GPU took to run the commands between the begin() and the end() cals.
decltype(auto) with(GPUCommandBuffer &commandBuffer, const UInt32 timerIndex, Func &&func)
Definition GPUQuery.hpp:163
virtual void end(GPUCommandBuffer &commandBuffer, UInt32 timerIndex=0)=0
Ends the timer query.
virtual void begin(GPUCommandBuffer &commandBuffer, UInt32 timerIndex=0)=0
Begins the timer query.
GPUTimerQuery(const GPUTimerQuery &)=delete
Deleted copy constructor.
virtual void reset()=0
Resets the timer query so that it can be re-used by another command buffer.
GPUTimerQuery & operator=(GPUTimerQuery &&)=delete
virtual void wait(UInt32 timerIndex=0) const =0
Waits until the timer query is complete.
Definition Common.hpp:62
TDeviceObject(GPUDevice &device, const Descriptor &descriptor)
Initializes a new RenderAPI device object instance.
Definition Common.hpp:73
Base template for the event class.
Definition Event.hpp:27
Definition Application.hpp:19
TTimeInterval< double > TimeInterval
Represents a time interval.
Definition Chrono.hpp:32
TimeInterval GPUTimerQueryResult
The type returned by a TimerQuery.
Definition GPUQuery.hpp:26
std::uint32_t UInt32
Definition DataTypes.hpp:23
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition GPUQuery.hpp:171
static constexpr void reflect(Processor &&RTTI)
Executes the given processor for every field of the struct.
Definition GPUQuery.hpp:175
A descriptor structure for TimerQuery.
Definition GPUQuery.hpp:29
UInt32 numberOfTimers
The number of timers to be created in a single TimerQuery.
Definition GPUQuery.hpp:34
static constexpr void reflect(Processor &&RTTI)
Executes the given processor for every field of the struct.
Definition GPUQuery.hpp:39