Overte C++ Documentation
Backend.h
1 //
2 // Backend.h
3 // interface/src/gpu
4 //
5 // Created by Olivier Prat on 05/18/2018.
6 // Copyright 2018 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_Backend_h
13 #define hifi_gpu_Backend_h
14 
15 #include <GLMHelpers.h>
16 
17 #include "Forward.h"
18 #include "Batch.h"
19 #include "Buffer.h"
20 #include "Framebuffer.h"
21 
22 class QImage;
23 
24 namespace gpu {
25 class Context;
26 
27 struct ContextStats {
28 public:
29  int _ISNumFormatChanges = 0;
30  int _ISNumInputBufferChanges = 0;
31  int _ISNumIndexBufferChanges = 0;
32 
33  int _RSNumResourceBufferBounded = 0;
34  int _RSNumTextureBounded = 0;
35  int _RSAmountTextureMemoryBounded = 0;
36 
37  int _DSNumAPIDrawcalls = 0;
38  int _DSNumDrawcalls = 0;
39  int _DSNumTriangles = 0;
40 
41  int _PSNumSetPipelines = 0;
42 
43  ContextStats() {}
44  ContextStats(const ContextStats& stats) = default;
45 
46  void evalDelta(const ContextStats& begin, const ContextStats& end);
47 };
48 
49 class Backend {
50 public:
51  virtual ~Backend() {}
52 
53  virtual void shutdown() {}
54  virtual const std::string& getVersion() const = 0;
55 
56  void setStereoState(const StereoState& stereo);
57 
65  virtual void executeFrame(const FramePointer& frame) = 0;
66 
74  virtual void render(const Batch& batch) = 0;
75 
80  virtual void syncCache() = 0;
81 
87  virtual void syncProgram(const gpu::ShaderPointer& program) = 0;
88 
93  virtual void recycle() const = 0;
94 
102  virtual void downloadFramebuffer(const FramebufferPointer& srcFramebuffer, const Vec4i& region, QImage& destImage) = 0;
103 
114  virtual void updatePresentFrame(const Mat4& correction = Mat4(), bool primary = true) = 0;
115 
122  virtual bool supportedTextureFormat(const gpu::Element& format) const = 0;
123 
124  // Shared header between C++ and GLSL
125 #include "TransformCamera_shared.slh"
126 
130  class TransformCamera : public _TransformCamera {
131  public:
141  const Backend::TransformCamera& recomputeDerived(const Transform& view, const Transform& previousView, const Mat4& previousProjection) const;
142 
143  // Jitter should be divided by framebuffer size
154  TransformCamera getMonoCamera(bool isSkybox, const Transform& view, Transform previousView, Mat4 previousProjection, Vec2 normalizedJitter) const;
155 
166  TransformCamera getEyeCamera(int eye, const StereoState& stereo, const StereoState& prevStereo, const Transform& view, const Transform& previousView,
167  Vec2 normalizedJitter) const;
168  };
169 
177  template <typename T, typename U>
178  static void setGPUObject(const U& object, T* gpuObject) {
179  object.gpuObject.setGPUObject(gpuObject);
180  }
181 
189  template <typename T, typename U>
190  static T* getGPUObject(const U& object) {
191  return reinterpret_cast<T*>(object.gpuObject.getGPUObject());
192  }
193 
198  void resetStats() const { _stats = ContextStats(); }
199 
204  void getStats(ContextStats& stats) const { stats = _stats; }
205 
210  virtual bool isTextureManagementSparseEnabled() const = 0;
211 
212  // These should only be accessed by Backend implementation to report the buffer and texture allocations,
213  // they are NOT public objects
214  static ContextMetricSize freeGPUMemSize;
215 
216  static ContextMetricCount bufferCount;
217  static ContextMetricSize bufferGPUMemSize;
218 
219  static ContextMetricCount textureResidentCount;
220  static ContextMetricCount textureFramebufferCount;
221  static ContextMetricCount textureResourceCount;
222  static ContextMetricCount textureExternalCount;
223 
224  static ContextMetricSize textureResidentGPUMemSize;
225  static ContextMetricSize textureFramebufferGPUMemSize;
226  static ContextMetricSize textureResourceGPUMemSize;
227  static ContextMetricSize textureExternalGPUMemSize;
228 
229  static ContextMetricCount texturePendingGPUTransferCount;
230  static ContextMetricSize texturePendingGPUTransferMemSize;
231  static ContextMetricSize textureResourcePopulatedGPUMemSize;
232  static ContextMetricSize textureResourceIdealGPUMemSize;
233 
234 protected:
235  virtual bool isStereo() const {
236  return _stereo.isStereo();
237  }
238 
239  void getStereoProjections(mat4* eyeProjections) const {
240  for (int i = 0; i < 2; ++i) {
241  eyeProjections[i] = _stereo._eyeProjections[i];
242  }
243  }
244 
245  void getStereoViews(mat4* eyeViews) const {
246  for (int i = 0; i < 2; ++i) {
247  eyeViews[i] = _stereo._eyeViews[i];
248  }
249  }
250 
251  friend class Context;
252  mutable ContextStats _stats;
253  StereoState _stereo;
254  StereoState _prevStereo;
255 };
256 
257 }
258 
259 #endif
Provides the Mat4 scripting interface.
Definition: Mat4.h:44
Definition: Backend.h:130
TransformCamera getMonoCamera(bool isSkybox, const Transform &view, Transform previousView, Mat4 previousProjection, Vec2 normalizedJitter) const
Definition: Backend.cpp:87
TransformCamera getEyeCamera(int eye, const StereoState &stereo, const StereoState &prevStereo, const Transform &view, const Transform &previousView, Vec2 normalizedJitter) const
Definition: Backend.cpp:44
const Backend::TransformCamera & recomputeDerived(const Transform &view, const Transform &previousView, const Mat4 &previousProjection) const
Calculates helper matrices that are derived from ones passed to this object. Is called only from insi...
Definition: Backend.cpp:106
Definition: Format.h:295
Definition: gpu/src/gpu/Forward.h:105