12 #ifndef hifi_gpu_Context_h
13 #define hifi_gpu_Context_h
22 #include "PointerStorage.h"
29 using Size = Resource::Size;
30 typedef BackendPointer (*CreateBackend)();
35 std::call_once(_initialized, [] {
36 _createBackendCallback = T::createBackend;
45 const std::string& getBackendVersion()
const;
47 void beginFrame(
const glm::mat4& renderView = glm::mat4(),
const glm::mat4& renderPose = glm::mat4());
48 void appendFrameBatch(
const BatchPointer& batch);
49 FramePointer endFrame();
51 static BatchPointer acquireBatch(
const char* name =
nullptr);
52 static void releaseBatch(Batch* batch);
62 void executeBatch(Batch& batch)
const;
67 void executeBatch(
const char* name, std::function<
void(Batch&)> lambda)
const;
74 void executeFrame(
const FramePointer& frame)
const;
92 void consumeFrameUpdates(
const FramePointer& frame)
const;
94 const BackendPointer& getBackend()
const {
return _backend; }
96 void enableStereo(
bool enable =
true);
98 void setStereoProjections(
const mat4 eyeProjections[2]);
99 void setStereoViews(
const mat4 eyeViews[2]);
100 void getStereoProjections(mat4* eyeProjections)
const;
101 void getStereoViews(mat4* eyeViews)
const;
105 void downloadFramebuffer(
const FramebufferPointer& srcFramebuffer,
const Vec4i& region, QImage& destImage);
108 void resetStats()
const;
109 void getStats(ContextStats& stats)
const;
112 void getFrameStats(ContextStats& stats)
const;
114 static PipelinePointer createMipGenerationPipeline(
const ShaderPointer& pixelShader);
116 double getFrameTimerGPUAverage()
const;
117 double getFrameTimerBatchAverage()
const;
119 static Size getFreeGPUMemSize();
120 static Size getUsedGPUMemSize();
122 static uint32_t getBufferGPUCount();
123 static Size getBufferGPUMemSize();
125 static uint32_t getTextureGPUCount();
126 static uint32_t getTextureResidentGPUCount();
127 static uint32_t getTextureFramebufferGPUCount();
128 static uint32_t getTextureResourceGPUCount();
129 static uint32_t getTextureExternalGPUCount();
131 static Size getTextureGPUMemSize();
132 static Size getTextureResidentGPUMemSize();
133 static Size getTextureFramebufferGPUMemSize();
134 static Size getTextureResourceGPUMemSize();
135 static Size getTextureExternalGPUMemSize();
137 static uint32_t getTexturePendingGPUTransferCount();
138 static Size getTexturePendingGPUTransferMemSize();
140 static Size getTextureResourcePopulatedGPUMemSize();
141 static Size getTextureResourceIdealGPUMemSize();
143 struct ProgramsToSync {
144 ProgramsToSync(
const std::vector<gpu::ShaderPointer>& programs, std::function<
void()> callback,
size_t rate) :
145 programs(programs), callback(callback), rate(rate) {}
147 std::vector<gpu::ShaderPointer> programs;
148 std::function<void()> callback;
152 void pushProgramsToSync(
const std::vector<uint32_t>& programIDs, std::function<
void()> callback,
size_t rate = 0);
153 void pushProgramsToSync(
const std::vector<gpu::ShaderPointer>& programs, std::function<
void()> callback,
size_t rate = 0);
155 void processProgramsToSync();
158 Context(
const Context& context);
160 std::shared_ptr<Backend> _backend;
161 bool _frameActive{
false };
162 FramePointer _currentFrame;
163 RangeTimerPointer _frameRangeTimer;
166 std::mutex _programsToSyncMutex;
167 std::queue<ProgramsToSync> _programsToSyncQueue;
168 gpu::Shaders _syncedPrograms;
169 size_t _nextProgramToSyncIndex { 0 };
172 mutable ContextStats _frameStats;
174 static CreateBackend _createBackendCallback;
175 static std::once_flag _initialized;
178 static void clearBatches();
179 static std::mutex _batchPoolMutex;
180 static std::list<Batch*> _batchPool;
183 friend class Backend;
185 typedef std::shared_ptr<Context> ContextPointer;
187 void doInBatch(
const char* name,
const std::shared_ptr<gpu::Context>& context,
const std::function<
void(Batch& batch)>& f);