Overte C++ Documentation
gpu/src/gpu/Context.h
1 //
2 // Context.h
3 // interface/src/gpu
4 //
5 // Created by Sam Gateau on 10/27/2014.
6 // Copyright 2014 High Fidelity, Inc.
7 // Copyright 2024 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 //
12 #ifndef hifi_gpu_Context_h
13 #define hifi_gpu_Context_h
14 
15 #include <assert.h>
16 #include <mutex>
17 #include <queue>
18 
19 #include "Texture.h"
20 #include "Pipeline.h"
21 #include "Frame.h"
22 #include "PointerStorage.h"
23 #include "Backend.h"
24 
25 namespace gpu {
29 class Context {
30 public:
31  using Size = Resource::Size;
32  typedef BackendPointer (*CreateBackend)();
33 
41  template <class T>
42  static void init() {
43  std::call_once(_initialized, [] {
44  _createBackendCallback = T::createBackend;
45  T::init();
46  });
47  }
48 
49  Context();
50  ~Context();
51 
55  void shutdown();
56 
60  const std::string& getBackendVersion() const;
61 
68  void beginFrame(const glm::mat4& renderView = glm::mat4(), const glm::mat4& renderPose = glm::mat4());
69 
74  void appendFrameBatch(const BatchPointer& batch);
75 
82  FramePointer endFrame();
83 
91  static BatchPointer acquireBatch(const char* name = nullptr);
92 
98  static void releaseBatch(Batch* batch);
99 
104  void recycle() const;
105 
112  void executeBatch(Batch& batch) const;
113 
121  void executeBatch(const char* name, std::function<void(Batch&)> lambda) const;
122 
130  void executeFrame(const FramePointer& frame) const;
131 
150  void consumeFrameUpdates(const FramePointer& frame) const;
151 
155  const BackendPointer& getBackend() const { return _backend; }
156 
163  void enableStereo(bool enable = true);
164 
168  bool isStereo();
169 
176  void setStereoProjections(const mat4 eyeProjections[2]);
177 
185  void setStereoViews(const mat4 eyeViews[2]);
186 
191  void getStereoProjections(mat4* eyeProjections) const;
192 
197  void getStereoViews(mat4* eyeViews) const;
198 
208  void downloadFramebuffer(const FramebufferPointer& srcFramebuffer, const Vec4i& region, QImage& destImage);
209 
210  // Reporting stats of the context
214  void resetStats() const;
215 
220  void getStats(ContextStats& stats) const;
221 
228  void getFrameStats(ContextStats& stats) const;
229 
237  static PipelinePointer createMipGenerationPipeline(const ShaderPointer& pixelShader);
238 
242  double getFrameTimerGPUAverage() const;
243 
247  double getFrameTimerBatchAverage() const;
248 
252  static Size getFreeGPUMemSize();
253 
257  static Size getUsedGPUMemSize();
258 
262  static uint32_t getBufferGPUCount();
263 
267  static Size getBufferGPUMemSize();
268 
272  static uint32_t getTextureGPUCount();
273 
277  static uint32_t getTextureResidentGPUCount();
278 
282  static uint32_t getTextureFramebufferGPUCount();
283 
287  static uint32_t getTextureResourceGPUCount();
288 
292  static uint32_t getTextureExternalGPUCount();
293 
297  static Size getTextureGPUMemSize();
298 
302  static Size getTextureResidentGPUMemSize();
303 
307  static Size getTextureFramebufferGPUMemSize();
308 
312  static Size getTextureResourceGPUMemSize();
313 
317  static Size getTextureExternalGPUMemSize();
318 
323  static uint32_t getTexturePendingGPUTransferCount();
324 
329 
334 
338  static Size getTextureResourceIdealGPUMemSize();
339 
344  struct ProgramsToSync {
350  ProgramsToSync(const std::vector<gpu::ShaderPointer>& programs, std::function<void()> callback, size_t rate) :
351  programs(programs), callback(callback), rate(rate) {}
352 
353  std::vector<gpu::ShaderPointer> programs;
354  std::function<void()> callback;
355  size_t rate;
356  };
357 
365  void pushProgramsToSync(const std::vector<uint32_t>& programIDs, std::function<void()> callback, size_t rate = 0);
366 
374  void pushProgramsToSync(const std::vector<gpu::ShaderPointer>& programs, std::function<void()> callback, size_t rate = 0);
375 
381  void processProgramsToSync();
382 
383 protected:
389  Context(const Context& context) = delete;
390 
394  std::shared_ptr<Backend> _backend;
395 
399  bool _frameActive{ false };
400 
405  FramePointer _currentFrame;
406 
410  RangeTimerPointer _frameRangeTimer;
411 
416 
421 
425  std::queue<ProgramsToSync> _programsToSyncQueue;
426 
430  gpu::Shaders _syncedPrograms;
431 
436 
440  mutable ContextStats _frameStats;
441 
445  static CreateBackend _createBackendCallback;
446 
450  static std::once_flag _initialized;
451 
459  static void clearBatches();
460 
464  static std::mutex _batchPoolMutex;
465 
469  static std::list<Batch*> _batchPool;
470 
471  friend class Shader;
472  friend class Backend;
473 };
474 typedef std::shared_ptr<Context> ContextPointer;
475 
483 void doInBatch(const char* name, const std::shared_ptr<gpu::Context>& context, const std::function<void(Batch& batch)>& f);
484 
485 }; // namespace gpu
486 
487 #endif
Definition: gpu/src/gpu/Context.h:29
static uint32_t getTextureResidentGPUCount()
Definition: gpu/src/gpu/Context.cpp:240
static void clearBatches()
Deletes all batches in the batch pool.
Definition: gpu/src/gpu/Context.cpp:332
static void init()
Creates graphics-API specific Backend object.
Definition: gpu/src/gpu/Context.h:42
static Size getTextureFramebufferGPUMemSize()
Definition: gpu/src/gpu/Context.cpp:259
void downloadFramebuffer(const FramebufferPointer &srcFramebuffer, const Vec4i &region, QImage &destImage)
Writes a region of a given framebuffer to a QImage.
Definition: gpu/src/gpu/Context.cpp:191
FramePointer endFrame()
Finalizes the currently recorded frame.
Definition: gpu/src/gpu/Context.cpp:93
std::shared_ptr< Backend > _backend
Definition: gpu/src/gpu/Context.h:394
static void releaseBatch(Batch *batch)
Clears the batch and adds it batch to the pool.
Definition: gpu/src/gpu/Context.cpp:357
Context(const Context &context)=delete
static Size getTextureResourceIdealGPUMemSize()
Definition: gpu/src/gpu/Context.cpp:291
bool isStereo()
Definition: gpu/src/gpu/Context.cpp:163
const BackendPointer & getBackend() const
Definition: gpu/src/gpu/Context.h:155
void enableStereo(bool enable=true)
Enables stereo for the context.
Definition: gpu/src/gpu/Context.cpp:159
static Size getTextureResidentGPUMemSize()
Definition: gpu/src/gpu/Context.cpp:256
void resetStats() const
Resets render statistics such as draw call count.
Definition: gpu/src/gpu/Context.cpp:195
static Size getTextureGPUMemSize()
Definition: gpu/src/gpu/Context.cpp:253
static BatchPointer acquireBatch(const char *name=nullptr)
Acquires batch from a batch pool and sets its name.
Definition: gpu/src/gpu/Context.cpp:339
void executeBatch(Batch &batch) const
Definition: gpu/src/gpu/Context.cpp:111
void recycle() const
Definition: gpu/src/gpu/Context.cpp:121
StereoState _stereo
Definition: gpu/src/gpu/Context.h:415
static Size getTextureResourcePopulatedGPUMemSize()
Definition: gpu/src/gpu/Context.cpp:277
std::mutex _programsToSyncMutex
Definition: gpu/src/gpu/Context.h:420
gpu::Shaders _syncedPrograms
Definition: gpu/src/gpu/Context.h:430
void executeFrame(const FramePointer &frame) const
Definition: gpu/src/gpu/Context.cpp:131
static Size getBufferGPUMemSize()
Definition: gpu/src/gpu/Context.cpp:233
void pushProgramsToSync(const std::vector< uint32_t > &programIDs, std::function< void()> callback, size_t rate=0)
Schedule shaders for loading.
Definition: gpu/src/gpu/Context.cpp:295
static std::once_flag _initialized
Definition: gpu/src/gpu/Context.h:450
void shutdown()
Definition: gpu/src/gpu/Context.cpp:62
void getStereoViews(mat4 *eyeViews) const
Writes current stereo view matrices to a given mat4 array.
Definition: gpu/src/gpu/Context.cpp:185
static std::mutex _batchPoolMutex
Definition: gpu/src/gpu/Context.h:464
void getStereoProjections(mat4 *eyeProjections) const
Writes current stereo projection matrices to a given mat4 array.
Definition: gpu/src/gpu/Context.cpp:179
void setStereoViews(const mat4 eyeViews[2])
Sets view matrices for each eye.
Definition: gpu/src/gpu/Context.cpp:173
void beginFrame(const glm::mat4 &renderView=glm::mat4(), const glm::mat4 &renderPose=glm::mat4())
Creates and sets up new Frame object to which batches of renderer commands will be written.
Definition: gpu/src/gpu/Context.cpp:73
FramePointer _currentFrame
Definition: gpu/src/gpu/Context.h:405
static uint32_t getBufferGPUCount()
Definition: gpu/src/gpu/Context.cpp:229
static uint32_t getTextureFramebufferGPUCount()
Definition: gpu/src/gpu/Context.cpp:243
double getFrameTimerGPUAverage() const
Definition: gpu/src/gpu/Context.cpp:207
static uint32_t getTextureExternalGPUCount()
Definition: gpu/src/gpu/Context.cpp:249
std::queue< ProgramsToSync > _programsToSyncQueue
Definition: gpu/src/gpu/Context.h:425
const std::string & getBackendVersion() const
Definition: gpu/src/gpu/Context.cpp:69
static Size getUsedGPUMemSize()
Definition: gpu/src/gpu/Context.cpp:225
static uint32_t getTexturePendingGPUTransferCount()
Definition: gpu/src/gpu/Context.cpp:269
static std::list< Batch * > _batchPool
Definition: gpu/src/gpu/Context.h:469
static Size getTexturePendingGPUTransferMemSize()
Definition: gpu/src/gpu/Context.cpp:273
size_t _nextProgramToSyncIndex
Definition: gpu/src/gpu/Context.h:435
static PipelinePointer createMipGenerationPipeline(const ShaderPointer &pixelShader)
Creates a custom pipeline for generating mipmaps.
Definition: gpu/src/gpu/Context.cpp:281
void consumeFrameUpdates(const FramePointer &frame) const
Definition: gpu/src/gpu/Context.cpp:126
void getFrameStats(ContextStats &stats) const
Retrieves most recent frame render statistics such as draw call count.
Definition: gpu/src/gpu/Context.cpp:203
RangeTimerPointer _frameRangeTimer
Definition: gpu/src/gpu/Context.h:410
static Size getFreeGPUMemSize()
Definition: gpu/src/gpu/Context.cpp:221
void setStereoProjections(const mat4 eyeProjections[2])
Sets projection matrices for each eye.
Definition: gpu/src/gpu/Context.cpp:167
static CreateBackend _createBackendCallback
Definition: gpu/src/gpu/Context.h:445
void getStats(ContextStats &stats) const
Retrieves current render statistics such as draw call count.
Definition: gpu/src/gpu/Context.cpp:199
static uint32_t getTextureResourceGPUCount()
Definition: gpu/src/gpu/Context.cpp:246
static uint32_t getTextureGPUCount()
Definition: gpu/src/gpu/Context.cpp:237
ContextStats _frameStats
Definition: gpu/src/gpu/Context.h:440
static Size getTextureResourceGPUMemSize()
Definition: gpu/src/gpu/Context.cpp:262
void processProgramsToSync()
Loads and compiles shaders.
Definition: gpu/src/gpu/Context.cpp:308
void appendFrameBatch(const BatchPointer &batch)
Appends a batch at the end of the current frame.
Definition: gpu/src/gpu/Context.cpp:85
static Size getTextureExternalGPUMemSize()
Definition: gpu/src/gpu/Context.cpp:265
double getFrameTimerBatchAverage() const
Definition: gpu/src/gpu/Context.cpp:214
Definition: gpu/src/gpu/Context.h:344
ProgramsToSync(const std::vector< gpu::ShaderPointer > &programs, std::function< void()> callback, size_t rate)
Definition: gpu/src/gpu/Context.h:350
Definition: gpu/src/gpu/Forward.h:105