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 
58  virtual void executeFrame(const FramePointer& frame) = 0;
59  virtual void render(const Batch& batch) = 0;
60  virtual void syncCache() = 0;
61  virtual void syncProgram(const gpu::ShaderPointer& program) = 0;
62  virtual void recycle() const = 0;
63  virtual void downloadFramebuffer(const FramebufferPointer& srcFramebuffer, const Vec4i& region, QImage& destImage) = 0;
64  virtual void updatePresentFrame(const Mat4& correction = Mat4(), bool primary = true) = 0;
65 
66  virtual bool supportedTextureFormat(const gpu::Element& format) const = 0;
67 
68  // Shared header between C++ and GLSL
69 #include "TransformCamera_shared.slh"
70 
71  class TransformCamera : public _TransformCamera {
72  public:
73  const Backend::TransformCamera& recomputeDerived(const Transform& view, const Transform& previousView, const Mat4& previousProjection) const;
74  // Jitter should be divided by framebuffer size
75  TransformCamera getMonoCamera(bool isSkybox, const Transform& view, Transform previousView, Mat4 previousProjection, Vec2 normalizedJitter) const;
76  // Jitter should be divided by framebuffer size
77  TransformCamera getEyeCamera(int eye, const StereoState& stereo, const StereoState& prevStereo, const Transform& view, const Transform& previousView,
78  Vec2 normalizedJitter) const;
79  };
80 
81  template <typename T, typename U>
82  static void setGPUObject(const U& object, T* gpuObject) {
83  object.gpuObject.setGPUObject(gpuObject);
84  }
85  template <typename T, typename U>
86  static T* getGPUObject(const U& object) {
87  return reinterpret_cast<T*>(object.gpuObject.getGPUObject());
88  }
89 
90  void resetStats() const { _stats = ContextStats(); }
91  void getStats(ContextStats& stats) const { stats = _stats; }
92 
93  virtual bool isTextureManagementSparseEnabled() const = 0;
94 
95  // These should only be accessed by Backend implementation to report the buffer and texture allocations,
96  // they are NOT public objects
97  static ContextMetricSize freeGPUMemSize;
98 
99  static ContextMetricCount bufferCount;
100  static ContextMetricSize bufferGPUMemSize;
101 
102  static ContextMetricCount textureResidentCount;
103  static ContextMetricCount textureFramebufferCount;
104  static ContextMetricCount textureResourceCount;
105  static ContextMetricCount textureExternalCount;
106 
107  static ContextMetricSize textureResidentGPUMemSize;
108  static ContextMetricSize textureFramebufferGPUMemSize;
109  static ContextMetricSize textureResourceGPUMemSize;
110  static ContextMetricSize textureExternalGPUMemSize;
111 
112  static ContextMetricCount texturePendingGPUTransferCount;
113  static ContextMetricSize texturePendingGPUTransferMemSize;
114  static ContextMetricSize textureResourcePopulatedGPUMemSize;
115  static ContextMetricSize textureResourceIdealGPUMemSize;
116 
117 protected:
118  virtual bool isStereo() const {
119  return _stereo.isStereo();
120  }
121 
122  void getStereoProjections(mat4* eyeProjections) const {
123  for (int i = 0; i < 2; ++i) {
124  eyeProjections[i] = _stereo._eyeProjections[i];
125  }
126  }
127 
128  void getStereoViews(mat4* eyeViews) const {
129  for (int i = 0; i < 2; ++i) {
130  eyeViews[i] = _stereo._eyeViews[i];
131  }
132  }
133 
134  friend class Context;
135  mutable ContextStats _stats;
136  StereoState _stereo;
137  StereoState _prevStereo;
138 };
139 
140 }
141 
142 #endif
Provides the Mat4 scripting interface.
Definition: Mat4.h:44