11 #ifndef hifi_gpu_Batch_h
12 #define hifi_gpu_Batch_h
17 #include <glm/gtc/type_ptr.hpp>
19 #include <shared/NsightHelpers.h>
21 #include "Framebuffer.h"
26 #include "Transform.h"
27 #include "ShaderConstants.h"
30 #define BATCH_PREALLOCATE_MIN 128
43 typedef Stream::Slot Slot;
48 using Index = uint16_t;
50 DrawCallInfo(Index idx) : index(idx) {}
51 DrawCallInfo(Index idx, Index user) : index(idx), unused(user) {}
54 uint16_t unused { 0 };
58 static_assert(
sizeof(DrawCallInfo) == 4,
"DrawCallInfo size is incorrect.");
60 using DrawCallInfoBuffer = std::vector<DrawCallInfo>;
62 struct NamedBatchData {
63 using BufferPointers = std::vector<BufferPointer>;
64 using Function = std::function<void(gpu::Batch&, NamedBatchData&)>;
66 BufferPointers buffers;
68 DrawCallInfoBuffer drawCallInfos;
70 size_t count()
const {
return drawCallInfos.size(); }
72 void process(Batch& batch) {
74 function(batch, *
this);
79 using NamedBatchDataMap = std::map<std::string, NamedBatchData>;
81 DrawCallInfoBuffer _drawCallInfos;
82 static size_t _drawCallInfosMax;
84 mutable std::string _currentNamedCall;
86 const DrawCallInfoBuffer& getDrawCallInfoBuffer()
const;
87 DrawCallInfoBuffer& getDrawCallInfoBuffer();
89 void captureDrawCallInfo();
90 void captureNamedDrawCallInfo(std::string name);
92 Batch(
const std::string& name =
"");
94 Batch(
const Batch& batch) =
delete;
95 Batch& operator=(
const Batch& batch) =
delete;
98 void setName(
const std::string& name);
99 const std::string& getName()
const {
return _name; }
105 void enableStereo(
bool enable =
true);
106 bool isStereoEnabled()
const;
112 void enableSkybox(
bool enable =
true);
113 bool isSkyboxEnabled()
const;
118 void setDrawcallUniform(uint16 uniform);
121 void setDrawcallUniformReset(uint16 resetUniform);
124 void draw(Primitive primitiveType, uint32 numVertices, uint32 startVertex = 0);
125 void drawIndexed(Primitive primitiveType, uint32 numIndices, uint32 startIndex = 0);
126 void drawInstanced(uint32 numInstances, Primitive primitiveType, uint32 numVertices, uint32 startVertex = 0, uint32 startInstance = 0);
127 void drawIndexedInstanced(uint32 numInstances, Primitive primitiveType, uint32 numIndices, uint32 startIndex = 0, uint32 startInstance = 0);
128 void multiDrawIndirect(uint32 numCommands, Primitive primitiveType);
129 void multiDrawIndexedIndirect(uint32 numCommands, Primitive primitiveType);
131 void setupNamedCalls(
const std::string& instanceName, NamedBatchData::Function
function);
132 const BufferPointer& getNamedBuffer(
const std::string& instanceName, uint8_t index = 0);
138 void setInputFormat(
const Stream::FormatPointer& format);
140 void setInputBuffer(Slot channel,
const BufferPointer& buffer, Offset offset, Offset stride);
141 void setInputBuffer(Slot channel,
const BufferView& buffer);
142 void setInputStream(Slot startChannel,
const BufferStream& stream);
144 void setIndexBuffer(Type type,
const BufferPointer& buffer, Offset offset);
145 void setIndexBuffer(
const BufferView& buffer);
149 void setIndirectBuffer(
const BufferPointer& buffer, Offset offset = 0, Offset stride = 0);
152 class DrawIndirectCommand {
155 uint _instanceCount{ 0 };
156 uint _firstIndex{ 0 };
157 uint _baseInstance{ 0 };
161 class DrawIndexedIndirectCommand {
164 uint _instanceCount{ 0 };
165 uint _firstIndex{ 0 };
166 uint _baseVertex{ 0 };
167 uint _baseInstance{ 0 };
176 void setModelTransform(
const Transform& model);
177 void resetViewTransform() { setViewTransform(Transform(),
false); }
178 void setViewTransform(
const Transform& view,
bool camera =
true);
179 void setProjectionTransform(
const Mat4& proj);
180 void setProjectionJitter(
float jx = 0.0f,
float jy = 0.0f);
182 void pushProjectionJitter(
float jx = 0.0f,
float jy = 0.0f);
183 void popProjectionJitter();
185 void setViewportTransform(
const Vec4i& viewport);
186 void setDepthRangeTransform(
float nearDepth,
float farDepth);
189 void setPipeline(
const PipelinePointer& pipeline);
191 void setStateBlendFactor(
const Vec4& factor);
195 void setStateScissorRect(
const Vec4i& rect);
197 void setUniformBuffer(uint32 slot,
const BufferPointer& buffer, Offset offset, Offset size);
198 void setUniformBuffer(uint32 slot,
const BufferView& view);
200 void setResourceBuffer(uint32 slot,
const BufferPointer& buffer);
202 void setResourceTexture(uint32 slot,
const TexturePointer& texture);
203 void setResourceTexture(uint32 slot,
const TextureView& view);
204 void setResourceTextureTable(
const TextureTablePointer& table, uint32 slot = 0);
205 void setResourceFramebufferSwapChainTexture(uint32 slot,
const FramebufferSwapChainPointer& framebuffer,
unsigned int swpaChainIndex,
unsigned int renderBufferSlot = 0U);
208 void setFramebuffer(
const FramebufferPointer& framebuffer);
209 void setFramebufferSwapChain(
const FramebufferSwapChainPointer& framebuffer,
unsigned int swapChainIndex);
211 void advance(
const SwapChainPointer& swapChain);
216 void clearFramebuffer(Framebuffer::Masks targets,
const Vec4& color,
float depth,
int stencil,
bool enableScissor =
false);
217 void clearColorFramebuffer(Framebuffer::Masks targets,
const Vec4& color,
bool enableScissor =
false);
218 void clearDepthFramebuffer(
float depth,
bool enableScissor =
false);
219 void clearStencilFramebuffer(
int stencil,
bool enableScissor =
false);
220 void clearDepthStencilFramebuffer(
float depth,
int stencil,
bool enableScissor =
false);
225 void blit(
const FramebufferPointer& src,
const Vec4i& srcRect,
const FramebufferPointer& dst,
const Vec4i& dstRect);
228 void generateTextureMips(
const TexturePointer& texture);
230 void generateTextureMipsWithPipeline(
const TexturePointer& destTexture,
int numMips = -1);
233 void beginQuery(
const QueryPointer& query);
234 void endQuery(
const QueryPointer& query);
235 void getQuery(
const QueryPointer& query);
240 void disableContextViewCorrection();
241 void restoreContextViewCorrection();
243 void disableContextStereo();
244 void restoreContextStereo();
247 void pushProfileRange(
const char* name);
248 void popProfileRange();
255 void _glUniform1i(
int location,
int v0);
256 void _glUniform1f(
int location,
float v0);
257 void _glUniform2f(
int location,
float v0,
float v1);
258 void _glUniform3f(
int location,
float v0,
float v1,
float v2);
259 void _glUniform4f(
int location,
float v0,
float v1,
float v2,
float v3);
260 void _glUniform3fv(
int location,
int count,
const float* value);
261 void _glUniform4fv(
int location,
int count,
const float* value);
262 void _glUniform4iv(
int location,
int count,
const int* value);
263 void _glUniformMatrix3fv(
int location,
int count,
unsigned char transpose,
const float* value);
264 void _glUniformMatrix4fv(
int location,
int count,
unsigned char transpose,
const float* value);
266 void _glUniform(
int location,
int v0) {
267 _glUniform1i(location, v0);
270 void _glUniform(
int location,
float v0) {
271 _glUniform1f(location, v0);
274 void _glUniform(
int location,
const glm::vec2& v) {
275 _glUniform2f(location, v.x, v.y);
278 void _glUniform(
int location,
const glm::vec3& v) {
279 _glUniform3f(location, v.x, v.y, v.z);
282 void _glUniform(
int location,
const glm::vec4& v) {
283 _glUniform4f(location, v.x, v.y, v.z, v.w);
286 void _glUniform(
int location,
const glm::mat3& v) {
287 _glUniformMatrix3fv(location, 1,
false, glm::value_ptr(v));
292 void runLambda(std::function<
void()> f);
297 COMMAND_drawInstanced,
298 COMMAND_drawIndexedInstanced,
299 COMMAND_multiDrawIndirect,
300 COMMAND_multiDrawIndexedIndirect,
302 COMMAND_setInputFormat,
303 COMMAND_setInputBuffer,
304 COMMAND_setIndexBuffer,
305 COMMAND_setIndirectBuffer,
307 COMMAND_setModelTransform,
308 COMMAND_setViewTransform,
309 COMMAND_setProjectionTransform,
310 COMMAND_setProjectionJitter,
311 COMMAND_setViewportTransform,
312 COMMAND_setDepthRangeTransform,
315 COMMAND_setStateBlendFactor,
316 COMMAND_setStateScissorRect,
318 COMMAND_setUniformBuffer,
319 COMMAND_setResourceBuffer,
320 COMMAND_setResourceTexture,
321 COMMAND_setResourceTextureTable,
322 COMMAND_setResourceFramebufferSwapChainTexture,
324 COMMAND_setFramebuffer,
325 COMMAND_setFramebufferSwapChain,
326 COMMAND_clearFramebuffer,
328 COMMAND_generateTextureMips,
329 COMMAND_generateTextureMipsWithPipeline,
339 COMMAND_disableContextViewCorrection,
340 COMMAND_restoreContextViewCorrection,
342 COMMAND_disableContextStereo,
343 COMMAND_restoreContextStereo,
347 COMMAND_startNamedCall,
348 COMMAND_stopNamedCall,
358 COMMAND_glUniform3fv,
359 COMMAND_glUniform4fv,
360 COMMAND_glUniform4iv,
361 COMMAND_glUniformMatrix3fv,
362 COMMAND_glUniformMatrix4fv,
364 COMMAND_pushProfileRange,
365 COMMAND_popProfileRange,
369 typedef std::vector<Command> Commands;
370 typedef std::vector<size_t> CommandOffsets;
372 const Commands& getCommands()
const {
return _commands; }
373 const CommandOffsets& getCommandOffsets()
const {
return _commandOffsets; }
378 #if (QT_POINTER_SIZE == 8)
384 char _chars[
sizeof(size_t)];
386 #if (QT_POINTER_SIZE == 8)
387 Param(
size_t val) : _size(val) {}
389 Param(int32 val) : _int(val) {}
390 Param(uint32 val) : _uint(val) {}
391 Param(
float val) : _float(val) {}
393 typedef std::vector<Param> Params;
395 const Params& getParams()
const {
return _params; }
400 template <
typename T>
405 Cache(
const Data& data) : _data(data) {}
410 std::vector< Cache<T> > _items;
413 _items.reserve(_max);
417 _max = std::max(_items.size(), _max);
421 size_t size()
const {
return _items.size(); }
422 size_t cache(
const Data& data) {
423 size_t offset = _items.size();
424 _items.emplace_back(data);
428 const Data& get(uint32 offset)
const {
429 assert((offset < _items.size()));
430 return (_items.data() + offset)->_data;
439 using CommandHandler = std::function<void(Command,
const Param*)>;
441 void forEachCommand(
const CommandHandler& handler)
const {
442 size_t count = _commands.size();
443 for (
size_t i = 0; i < count; ++i) {
444 const auto command = _commands[i];
445 const auto offset = _commandOffsets[i];
446 const Param* params = _params.data() + offset;
447 handler(command, params);
451 typedef Cache<BufferPointer>::Vector BufferCaches;
452 typedef Cache<TexturePointer>::Vector TextureCaches;
453 typedef Cache<TextureTablePointer>::Vector TextureTableCaches;
454 typedef Cache<Stream::FormatPointer>::Vector StreamFormatCaches;
455 typedef Cache<Transform>::Vector TransformCaches;
456 typedef Cache<PipelinePointer>::Vector PipelineCaches;
457 typedef Cache<FramebufferPointer>::Vector FramebufferCaches;
458 typedef Cache<SwapChainPointer>::Vector SwapChainCaches;
459 typedef Cache<QueryPointer>::Vector QueryCaches;
460 typedef Cache<std::string>::Vector StringCaches;
461 typedef Cache<std::function<void()>>::Vector LambdaCache;
465 typedef unsigned char Byte;
466 typedef std::vector<Byte> Bytes;
467 size_t cacheData(
size_t size,
const void* data);
468 Byte* editData(
size_t offset) {
469 if (offset >= _data.size()) {
472 return (_data.data() + offset);
475 const Byte* readData(
size_t offset)
const {
476 if (offset >= _data.size()) {
479 return (_data.data() + offset);
483 static size_t _commandsMax;
485 CommandOffsets _commandOffsets;
486 static size_t _commandOffsetsMax;
489 static size_t _paramsMax;
492 static size_t _dataMax;
495 class TransformObject {
501 using TransformObjects = std::vector<TransformObject>;
502 bool _invalidModel {
true };
503 Transform _currentModel;
504 TransformObjects _objects;
505 static size_t _objectsMax;
507 BufferCaches _buffers;
508 TextureCaches _textures;
509 TextureTableCaches _textureTables;
510 StreamFormatCaches _streamFormats;
511 TransformCaches _transforms;
512 PipelineCaches _pipelines;
513 FramebufferCaches _framebuffers;
514 SwapChainCaches _swapChains;
515 QueryCaches _queries;
516 LambdaCache _lambdas;
517 StringCaches _profileRanges;
520 NamedBatchDataMap _namedData;
522 uint16_t _drawcallUniform{ 0 };
523 uint16_t _drawcallUniformReset{ 0 };
525 glm::vec2 _projectionJitter{ 0.0f, 0.0f };
526 bool _enableStereo{
true };
527 bool _enableSkybox {
false };
532 friend class Context;
537 void finishFrame(BufferUpdates& updates);
544 void startNamedCall(
const std::string& name);
545 void stopNamedCall();
549 void captureDrawCallInfoImpl();
552 template <
typename T>
553 size_t Batch::Cache<T>::_max = BATCH_PREALLOCATE_MIN;
557 #if defined(NSIGHT_FOUND)
559 class ProfileRangeBatch {
561 ProfileRangeBatch(gpu::Batch& batch,
const char *name);
562 ~ProfileRangeBatch();
568 #define PROFILE_RANGE_BATCH(batch, name) ProfileRangeBatch profileRangeThis(batch, name);
572 #define PROFILE_RANGE_BATCH(batch, name)
Provides the Mat4 scripting interface.
Definition: Mat4.h:44