11 #ifndef hifi_gpu_Context_h
12 #define hifi_gpu_Context_h
18 #include <GLMHelpers.h>
25 #include "Framebuffer.h"
27 #include "PointerStorage.h"
35 uint32_t _ISNumFormatChanges { 0 };
36 uint32_t _ISNumInputBufferChanges { 0 };
37 uint32_t _ISNumIndexBufferChanges { 0 };
39 uint32_t _RSNumResourceBufferBounded { 0 };
40 uint32_t _RSNumTextureBounded { 0 };
41 uint64_t _RSAmountTextureMemoryBounded { 0 };
43 uint32_t _DSNumAPIDrawcalls { 0 };
44 uint32_t _DSNumDrawcalls { 0 };
45 uint32_t _DSNumTriangles { 0 };
47 uint32_t _PSNumSetPipelines { 0 };
50 ContextStats(
const ContextStats& stats) =
default;
52 void evalDelta(
const ContextStats& begin,
const ContextStats& end);
59 virtual void shutdown() {}
60 virtual const std::string& getVersion()
const = 0;
62 void setStereoState(
const StereoState& stereo) { _stereo = stereo; }
64 virtual void render(
const Batch& batch) = 0;
65 virtual void syncCache() = 0;
66 virtual void syncProgram(
const gpu::ShaderPointer& program) = 0;
67 virtual void recycle()
const = 0;
68 virtual void downloadFramebuffer(
const FramebufferPointer& srcFramebuffer,
const Vec4i& region, QImage& destImage) = 0;
69 virtual void setCameraCorrection(
const Mat4& correction,
const Mat4& prevRenderView,
bool reset =
false) {}
71 virtual bool supportedTextureFormat(
const gpu::Element& format) = 0;
74 #include "TransformCamera_shared.slh"
76 class TransformCamera :
public _TransformCamera {
78 const Backend::TransformCamera& recomputeDerived(
const Transform& xformView)
const;
80 TransformCamera getMonoCamera(
const Transform& xformView, Vec2 normalizedJitter)
const;
82 TransformCamera getEyeCamera(
int eye,
const StereoState& stereo,
const Transform& xformView, Vec2 normalizedJitter)
const;
85 template <
typename T,
typename U>
86 static void setGPUObject(
const U&
object, T* gpuObject) {
87 object.gpuObject.setGPUObject(gpuObject);
89 template <
typename T,
typename U>
90 static T* getGPUObject(
const U&
object) {
91 return reinterpret_cast<T*
>(
object.gpuObject.getGPUObject());
94 void resetStats()
const { _stats = ContextStats(); }
95 void getStats(ContextStats& stats)
const { stats = _stats; }
97 virtual bool isTextureManagementSparseEnabled()
const = 0;
101 static ContextMetricSize freeGPUMemSize;
103 static ContextMetricCount bufferCount;
104 static ContextMetricSize bufferGPUMemSize;
106 static ContextMetricCount textureResidentCount;
107 static ContextMetricCount textureFramebufferCount;
108 static ContextMetricCount textureResourceCount;
109 static ContextMetricCount textureExternalCount;
111 static ContextMetricSize textureResidentGPUMemSize;
112 static ContextMetricSize textureFramebufferGPUMemSize;
113 static ContextMetricSize textureResourceGPUMemSize;
114 static ContextMetricSize textureExternalGPUMemSize;
116 static ContextMetricCount texturePendingGPUTransferCount;
117 static ContextMetricSize texturePendingGPUTransferMemSize;
118 static ContextMetricSize textureResourcePopulatedGPUMemSize;
119 static ContextMetricSize textureResourceIdealGPUMemSize;
121 virtual bool isStereo()
const {
122 return _stereo.isStereo();
125 void getStereoProjections(mat4* eyeProjections)
const {
126 for (
int i = 0; i < 2; ++i) {
127 eyeProjections[i] = _stereo._eyeProjections[i];
132 void getStereoViews(mat4* eyeViews)
const {
133 for (
int i = 0; i < 2; ++i) {
134 eyeViews[i] = _stereo._eyeViews[i];
138 friend class Context;
139 mutable ContextStats _stats;
145 using Size = Resource::Size;
146 typedef BackendPointer (*CreateBackend)();
151 std::call_once(_initialized, [] {
152 _createBackendCallback = T::createBackend;
161 const std::string& getBackendVersion()
const;
163 void beginFrame(
const glm::mat4& renderView = glm::mat4(),
const glm::mat4& renderPose = glm::mat4());
164 void appendFrameBatch(
const BatchPointer& batch);
165 FramePointer endFrame();
167 static BatchPointer acquireBatch(
const char* name =
nullptr);
168 static void releaseBatch(Batch* batch);
173 void recycle()
const;
178 void executeBatch(Batch& batch)
const;
183 void executeBatch(
const char* name, std::function<
void(Batch&)> lambda)
const;
190 void executeFrame(
const FramePointer& frame)
const;
208 void consumeFrameUpdates(
const FramePointer& frame)
const;
210 const BackendPointer& getBackend()
const {
return _backend; }
212 void enableStereo(
bool enable =
true);
214 void setStereoProjections(
const mat4 eyeProjections[2]);
215 void setStereoViews(
const mat4 eyeViews[2]);
216 void getStereoProjections(mat4* eyeProjections)
const;
217 void getStereoViews(mat4* eyeViews)
const;
221 void downloadFramebuffer(
const FramebufferPointer& srcFramebuffer,
const Vec4i& region, QImage& destImage);
224 void resetStats()
const;
225 void getStats(ContextStats& stats)
const;
228 void getFrameStats(ContextStats& stats)
const;
230 static PipelinePointer createMipGenerationPipeline(
const ShaderPointer& pixelShader);
232 double getFrameTimerGPUAverage()
const;
233 double getFrameTimerBatchAverage()
const;
235 static Size getFreeGPUMemSize();
236 static Size getUsedGPUMemSize();
238 static uint32_t getBufferGPUCount();
239 static Size getBufferGPUMemSize();
241 static uint32_t getTextureGPUCount();
242 static uint32_t getTextureResidentGPUCount();
243 static uint32_t getTextureFramebufferGPUCount();
244 static uint32_t getTextureResourceGPUCount();
245 static uint32_t getTextureExternalGPUCount();
247 static Size getTextureGPUMemSize();
248 static Size getTextureResidentGPUMemSize();
249 static Size getTextureFramebufferGPUMemSize();
250 static Size getTextureResourceGPUMemSize();
251 static Size getTextureExternalGPUMemSize();
253 static uint32_t getTexturePendingGPUTransferCount();
254 static Size getTexturePendingGPUTransferMemSize();
256 static Size getTextureResourcePopulatedGPUMemSize();
257 static Size getTextureResourceIdealGPUMemSize();
259 struct ProgramsToSync {
260 ProgramsToSync(
const std::vector<gpu::ShaderPointer>& programs, std::function<
void()> callback,
size_t rate) :
261 programs(programs), callback(callback), rate(rate) {}
263 std::vector<gpu::ShaderPointer> programs;
264 std::function<void()> callback;
268 void pushProgramsToSync(
const std::vector<uint32_t>& programIDs, std::function<
void()> callback,
size_t rate = 0);
269 void pushProgramsToSync(
const std::vector<gpu::ShaderPointer>& programs, std::function<
void()> callback,
size_t rate = 0);
271 void processProgramsToSync();
274 Context(
const Context& context);
276 std::shared_ptr<Backend> _backend;
277 bool _frameActive{
false };
278 FramePointer _currentFrame;
279 RangeTimerPointer _frameRangeTimer;
282 std::mutex _programsToSyncMutex;
283 std::queue<ProgramsToSync> _programsToSyncQueue;
284 gpu::Shaders _syncedPrograms;
285 size_t _nextProgramToSyncIndex { 0 };
288 mutable ContextStats _frameStats;
290 static CreateBackend _createBackendCallback;
291 static std::once_flag _initialized;
294 static void clearBatches();
295 static std::mutex _batchPoolMutex;
296 static std::list<Batch*> _batchPool;
299 friend class Backend;
301 typedef std::shared_ptr<Context> ContextPointer;
303 void doInBatch(
const char* name,
const std::shared_ptr<gpu::Context>& context,
const std::function<
void(Batch& batch)>& f);
Provides the Mat4 scripting interface.
Definition: Mat4.h:44