12 #ifndef hifi_gpu_Batch_h
13 #define hifi_gpu_Batch_h
18 #include <glm/gtc/type_ptr.hpp>
20 #include <shared/NsightHelpers.h>
22 #include "Framebuffer.h"
27 #include "Transform.h"
28 #include "ShaderConstants.h"
31 #define BATCH_PREALLOCATE_MIN 128
44 typedef Stream::Slot Slot;
51 MAX_TRANSFORM_SAVE_SLOT_COUNT = 160
56 using Index = uint16_t;
58 DrawCallInfo(Index idx) : index(idx) {}
59 DrawCallInfo(Index idx, Index user) : index(idx), unused(user) {}
62 uint16_t unused { 0 };
66 static_assert(
sizeof(DrawCallInfo) == 4,
"DrawCallInfo size is incorrect.");
68 using DrawCallInfoBuffer = std::vector<DrawCallInfo>;
70 struct NamedBatchData {
71 using BufferPointers = std::vector<BufferPointer>;
72 using Function = std::function<void(gpu::Batch&, NamedBatchData&)>;
74 BufferPointers buffers;
76 DrawCallInfoBuffer drawCallInfos;
78 size_t count()
const {
return drawCallInfos.size(); }
80 void process(Batch& batch) {
82 function(batch, *
this);
87 using NamedBatchDataMap = std::map<std::string, NamedBatchData>;
89 DrawCallInfoBuffer _drawCallInfos;
90 static size_t _drawCallInfosMax;
92 mutable std::string _currentNamedCall;
94 const DrawCallInfoBuffer& getDrawCallInfoBuffer()
const;
95 DrawCallInfoBuffer& getDrawCallInfoBuffer();
97 void captureDrawCallInfo();
98 void captureNamedDrawCallInfo(std::string name);
100 Batch(
const std::string& name =
"");
102 Batch(
const Batch& batch) =
delete;
103 Batch& operator=(
const Batch& batch) =
delete;
106 void setName(
const std::string& name);
107 const std::string& getName()
const {
return _name; }
113 void enableStereo(
bool enable =
true);
114 bool isStereoEnabled()
const;
120 void enableSkybox(
bool enable =
true);
121 bool isSkyboxEnabled()
const;
126 void setDrawcallUniform(uint16 uniform);
129 void setDrawcallUniformReset(uint16 resetUniform);
132 void draw(Primitive primitiveType, uint32 numVertices, uint32 startVertex = 0);
133 void drawIndexed(Primitive primitiveType, uint32 numIndices, uint32 startIndex = 0);
134 void drawInstanced(uint32 numInstances, Primitive primitiveType, uint32 numVertices, uint32 startVertex = 0, uint32 startInstance = 0);
135 void drawIndexedInstanced(uint32 numInstances, Primitive primitiveType, uint32 numIndices, uint32 startIndex = 0, uint32 startInstance = 0);
136 void multiDrawIndirect(uint32 numCommands, Primitive primitiveType);
137 void multiDrawIndexedIndirect(uint32 numCommands, Primitive primitiveType);
139 void setupNamedCalls(
const std::string& instanceName, NamedBatchData::Function
function);
140 const BufferPointer& getNamedBuffer(
const std::string& instanceName, uint8_t index = 0);
146 void setInputFormat(
const Stream::FormatPointer& format);
148 void setInputBuffer(Slot channel,
const BufferPointer& buffer, Offset offset, Offset stride);
149 void setInputBuffer(Slot channel,
const BufferView& buffer);
150 void setInputStream(Slot startChannel,
const BufferStream& stream);
152 void setIndexBuffer(Type type,
const BufferPointer& buffer, Offset offset);
153 void setIndexBuffer(
const BufferView& buffer);
157 void setIndirectBuffer(
const BufferPointer& buffer, Offset offset = 0, Offset stride = 0);
160 class DrawIndirectCommand {
163 uint _instanceCount { 0 };
164 uint _firstIndex { 0 };
165 uint _baseInstance { 0 };
169 class DrawIndexedIndirectCommand {
172 uint _instanceCount { 0 };
173 uint _firstIndex { 0 };
174 uint _baseVertex { 0 };
175 uint _baseInstance { 0 };
184 void setModelTransform(
const Transform& model);
185 void setModelTransform(
const Transform& model,
const Transform& previousModel);
186 void resetViewTransform() { setViewTransform(Transform(),
false); }
187 void setViewTransform(
const Transform& view,
bool camera =
true);
188 void setProjectionTransform(
const Mat4& proj);
189 void setProjectionJitterEnabled(
bool isProjectionEnabled);
190 void setProjectionJitterSequence(
const Vec2* sequence,
size_t count);
191 void setProjectionJitterScale(
float scale);
193 void pushProjectionJitterEnabled(
bool isProjectionEnabled);
194 void popProjectionJitterEnabled();
196 void setViewportTransform(
const Vec4i& viewport);
197 void setDepthRangeTransform(
float nearDepth,
float farDepth);
199 void saveViewProjectionTransform(uint saveSlot);
200 void setSavedViewProjectionTransform(uint saveSlot);
201 void copySavedViewProjectionTransformToBuffer(uint saveSlot,
const BufferPointer& buffer, Offset offset);
204 void setPipeline(
const PipelinePointer& pipeline);
206 void setStateBlendFactor(
const Vec4& factor);
210 void setStateScissorRect(
const Vec4i& rect);
212 void setUniformBuffer(uint32 slot,
const BufferPointer& buffer, Offset offset, Offset size);
213 void setUniformBuffer(uint32 slot,
const BufferView& view);
215 void setResourceBuffer(uint32 slot,
const BufferPointer& buffer);
217 void setResourceTexture(uint32 slot,
const TexturePointer& texture);
218 void setResourceTexture(uint32 slot,
const TextureView& view);
219 void setResourceTextureTable(
const TextureTablePointer& table, uint32 slot = 0);
220 void setResourceFramebufferSwapChainTexture(uint32 slot,
const FramebufferSwapChainPointer& framebuffer,
unsigned int swapChainIndex,
unsigned int renderBufferSlot = 0U);
223 void setFramebuffer(
const FramebufferPointer& framebuffer);
224 void setFramebufferSwapChain(
const FramebufferSwapChainPointer& framebuffer,
unsigned int swapChainIndex);
226 void advance(
const SwapChainPointer& swapChain);
231 void clearFramebuffer(Framebuffer::Masks targets,
const Vec4& color,
float depth,
int stencil,
bool enableScissor =
false);
232 void clearColorFramebuffer(Framebuffer::Masks targets,
const Vec4& color,
bool enableScissor =
false);
233 void clearDepthFramebuffer(
float depth,
bool enableScissor =
false);
234 void clearStencilFramebuffer(
int stencil,
bool enableScissor =
false);
235 void clearDepthStencilFramebuffer(
float depth,
int stencil,
bool enableScissor =
false);
240 void blit(
const FramebufferPointer& src,
const Vec4i& srcRect,
const FramebufferPointer& dst,
const Vec4i& dstRect);
243 void generateTextureMips(
const TexturePointer& texture);
245 void generateTextureMipsWithPipeline(
const TexturePointer& destTexture,
int numMips = -1);
248 void beginQuery(
const QueryPointer& query);
249 void endQuery(
const QueryPointer& query);
250 void getQuery(
const QueryPointer& query);
255 void disableContextViewCorrection();
256 void restoreContextViewCorrection();
257 void setContextMirrorViewCorrection(
bool shouldMirror);
259 void disableContextStereo();
260 void restoreContextStereo();
263 void pushProfileRange(
const char* name);
264 void popProfileRange();
271 void _glUniform1i(
int location,
int v0);
272 void _glUniform1f(
int location,
float v0);
273 void _glUniform2f(
int location,
float v0,
float v1);
274 void _glUniform3f(
int location,
float v0,
float v1,
float v2);
275 void _glUniform4f(
int location,
float v0,
float v1,
float v2,
float v3);
276 void _glUniform3fv(
int location,
int count,
const float* value);
277 void _glUniform4fv(
int location,
int count,
const float* value);
278 void _glUniform4iv(
int location,
int count,
const int* value);
279 void _glUniformMatrix3fv(
int location,
int count,
unsigned char transpose,
const float* value);
280 void _glUniformMatrix4fv(
int location,
int count,
unsigned char transpose,
const float* value);
282 void _glUniform(
int location,
int v0) {
283 _glUniform1i(location, v0);
286 void _glUniform(
int location,
float v0) {
287 _glUniform1f(location, v0);
290 void _glUniform(
int location,
const glm::vec2& v) {
291 _glUniform2f(location, v.x, v.y);
294 void _glUniform(
int location,
const glm::vec3& v) {
295 _glUniform3f(location, v.x, v.y, v.z);
298 void _glUniform(
int location,
const glm::vec4& v) {
299 _glUniform4f(location, v.x, v.y, v.z, v.w);
302 void _glUniform(
int location,
const glm::mat3& v) {
303 _glUniformMatrix3fv(location, 1,
false, glm::value_ptr(v));
306 void _glUniform(
int location,
const glm::mat4& v) {
307 _glUniformMatrix4fv(location, 1,
false, glm::value_ptr(v));
312 void runLambda(std::function<
void()> f);
317 COMMAND_drawInstanced,
318 COMMAND_drawIndexedInstanced,
319 COMMAND_multiDrawIndirect,
320 COMMAND_multiDrawIndexedIndirect,
322 COMMAND_setInputFormat,
323 COMMAND_setInputBuffer,
324 COMMAND_setIndexBuffer,
325 COMMAND_setIndirectBuffer,
327 COMMAND_setModelTransform,
328 COMMAND_setViewTransform,
329 COMMAND_setProjectionTransform,
330 COMMAND_setProjectionJitterEnabled,
331 COMMAND_setProjectionJitterSequence,
332 COMMAND_setProjectionJitterScale,
333 COMMAND_setViewportTransform,
334 COMMAND_setDepthRangeTransform,
336 COMMAND_saveViewProjectionTransform,
337 COMMAND_setSavedViewProjectionTransform,
338 COMMAND_copySavedViewProjectionTransformToBuffer,
341 COMMAND_setStateBlendFactor,
342 COMMAND_setStateScissorRect,
344 COMMAND_setUniformBuffer,
345 COMMAND_setResourceBuffer,
346 COMMAND_setResourceTexture,
347 COMMAND_setResourceTextureTable,
348 COMMAND_setResourceFramebufferSwapChainTexture,
350 COMMAND_setFramebuffer,
351 COMMAND_setFramebufferSwapChain,
352 COMMAND_clearFramebuffer,
354 COMMAND_generateTextureMips,
355 COMMAND_generateTextureMipsWithPipeline,
365 COMMAND_disableContextViewCorrection,
366 COMMAND_restoreContextViewCorrection,
367 COMMAND_setContextMirrorViewCorrection,
369 COMMAND_disableContextStereo,
370 COMMAND_restoreContextStereo,
374 COMMAND_startNamedCall,
375 COMMAND_stopNamedCall,
385 COMMAND_glUniform3fv,
386 COMMAND_glUniform4fv,
387 COMMAND_glUniform4iv,
388 COMMAND_glUniformMatrix3fv,
389 COMMAND_glUniformMatrix4fv,
391 COMMAND_pushProfileRange,
392 COMMAND_popProfileRange,
396 typedef std::vector<Command> Commands;
397 typedef std::vector<size_t> CommandOffsets;
399 const Commands& getCommands()
const {
return _commands; }
400 const CommandOffsets& getCommandOffsets()
const {
return _commandOffsets; }
405 #if (QT_POINTER_SIZE == 8)
411 char _chars[
sizeof(size_t)];
413 #if (QT_POINTER_SIZE == 8)
414 Param(
size_t val) : _size(val) {}
416 Param(int32 val) : _int(val) {}
417 Param(uint32 val) : _uint(val) {}
418 Param(
float val) : _float(val) {}
420 typedef std::vector<Param> Params;
422 const Params& getParams()
const {
return _params; }
427 template <
typename T>
432 Cache(
const Data& data) : _data(data) {}
437 std::vector< Cache<T> > _items;
440 _items.reserve(_max);
444 _max = std::max(_items.size(), _max);
448 size_t size()
const {
return _items.size(); }
449 size_t cache(
const Data& data) {
450 size_t offset = _items.size();
451 _items.emplace_back(data);
455 const Data& get(uint32 offset)
const {
456 assert((offset < _items.size()));
457 return (_items.data() + offset)->_data;
466 using CommandHandler = std::function<void(Command,
const Param*)>;
468 void forEachCommand(
const CommandHandler& handler)
const {
469 size_t count = _commands.size();
470 for (
size_t i = 0; i < count; ++i) {
471 const auto command = _commands[i];
472 const auto offset = _commandOffsets[i];
473 const Param* params = _params.data() + offset;
474 handler(command, params);
478 typedef Cache<BufferPointer>::Vector BufferCaches;
479 typedef Cache<TexturePointer>::Vector TextureCaches;
480 typedef Cache<TextureTablePointer>::Vector TextureTableCaches;
481 typedef Cache<Sampler>::Vector SamplerCaches;
482 typedef Cache<Stream::FormatPointer>::Vector StreamFormatCaches;
483 typedef Cache<Transform>::Vector TransformCaches;
484 typedef Cache<PipelinePointer>::Vector PipelineCaches;
485 typedef Cache<FramebufferPointer>::Vector FramebufferCaches;
486 typedef Cache<SwapChainPointer>::Vector SwapChainCaches;
487 typedef Cache<QueryPointer>::Vector QueryCaches;
488 typedef Cache<std::string>::Vector StringCaches;
489 typedef Cache<std::function<void()>>::Vector LambdaCache;
493 typedef unsigned char Byte;
494 typedef std::vector<Byte> Bytes;
495 size_t cacheData(
size_t size,
const void* data);
496 Byte* editData(
size_t offset) {
497 if (offset >= _data.size()) {
500 return (_data.data() + offset);
503 const Byte* readData(
size_t offset)
const {
504 if (offset >= _data.size()) {
507 return (_data.data() + offset);
511 static size_t _commandsMax;
513 CommandOffsets _commandOffsets;
514 static size_t _commandOffsetsMax;
517 static size_t _paramsMax;
520 static size_t _dataMax;
522 #include "TransformObject_shared.slh"
524 using TransformObjects = std::vector<TransformObject>;
525 bool _invalidModel {
true };
526 Transform _currentModel;
527 Transform _previousModel;
528 mutable bool _mustUpdatePreviousModels;
529 mutable TransformObjects _objects;
530 static size_t _objectsMax;
532 Stream::FormatPointer _currentStreamFormat;
533 PipelinePointer _currentPipeline;
535 BufferCaches _buffers;
536 TextureCaches _textures;
537 TextureTableCaches _textureTables;
538 SamplerCaches _samplers;
539 StreamFormatCaches _streamFormats;
540 TransformCaches _transforms;
541 PipelineCaches _pipelines;
542 FramebufferCaches _framebuffers;
543 SwapChainCaches _swapChains;
544 QueryCaches _queries;
545 LambdaCache _lambdas;
546 StringCaches _profileRanges;
549 NamedBatchDataMap _namedData;
551 bool _isJitterOnProjectionEnabled {
false };
553 uint16_t _drawcallUniform { 0 };
554 uint16_t _drawcallUniformReset { 0 };
556 bool _enableStereo {
true };
557 bool _enableSkybox {
false };
562 friend class Context;
567 void finishFrame(BufferUpdates& updates);
574 void validateDrawState()
const;
576 void startNamedCall(
const std::string& name);
577 void stopNamedCall();
581 void captureDrawCallInfoImpl();
584 template <
typename T>
585 size_t Batch::Cache<T>::_max = BATCH_PREALLOCATE_MIN;
589 #if defined(NSIGHT_FOUND)
591 class ProfileRangeBatch {
593 ProfileRangeBatch(gpu::Batch& batch,
const char *name);
594 ~ProfileRangeBatch();
600 #define PROFILE_RANGE_BATCH(batch, name) ProfileRangeBatch profileRangeThis(batch, name);
604 #define PROFILE_RANGE_BATCH(batch, name)
Provides the Mat4 scripting interface.
Definition: Mat4.h:44