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
59 using Index = uint16_t;
80 static_assert(
sizeof(DrawCallInfo) == 4,
"DrawCallInfo size is incorrect.");
82 using DrawCallInfoBuffer = std::vector<DrawCallInfo>;
88 using BufferPointers = std::vector<BufferPointer>;
89 using Function = std::function<void(gpu::Batch&,
NamedBatchData&)>;
118 function(batch, *
this);
123 using NamedBatchDataMap = std::map<std::string, NamedBatchData>;
126 DrawCallInfoBuffer _drawCallInfos;
129 static size_t _drawCallInfosMax;
138 mutable std::string _currentNamedCall;
147 const DrawCallInfoBuffer& getDrawCallInfoBuffer()
const;
156 DrawCallInfoBuffer& getDrawCallInfoBuffer();
163 void captureDrawCallInfo();
170 void captureNamedDrawCallInfo(std::string name);
175 Batch(
const std::string& name =
"");
177 Batch(
const Batch& batch) =
delete;
178 Batch& operator=(
const Batch& batch) =
delete;
184 void setName(
const std::string& name);
189 const std::string& getName()
const {
return _name; }
206 void enableStereo(
bool enable =
true);
213 bool isStereoEnabled()
const;
224 void enableSkybox(
bool enable =
true);
229 bool isSkyboxEnabled()
const;
238 void setDrawcallUniform(uint16 uniform);
249 void setDrawcallUniformReset(uint16 resetUniform);
259 void draw(Primitive primitiveType, uint32 numVertices, uint32 startVertex = 0);
267 void drawIndexed(Primitive primitiveType, uint32 numIndices, uint32 startIndex = 0);
277 void drawInstanced(uint32 numInstances, Primitive primitiveType, uint32 numVertices, uint32 startVertex = 0, uint32 startInstance = 0);
287 void drawIndexedInstanced(uint32 numInstances, Primitive primitiveType, uint32 numIndices, uint32 startIndex = 0, uint32 startInstance = 0);
295 void multiDrawIndirect(uint32 numCommands, Primitive primitiveType);
303 void multiDrawIndexedIndirect(uint32 numCommands, Primitive primitiveType);
314 void setupNamedCalls(
const std::string& instanceName, NamedBatchData::Function
function);
324 const BufferPointer& getNamedBuffer(
const std::string& instanceName, uint8_t index = 0);
337 void setInputFormat(
const Stream::FormatPointer& format);
347 void setInputBuffer(Slot channel,
const BufferPointer& buffer, Offset offset, Offset stride);
355 void setInputBuffer(Slot channel,
const BufferView& buffer);
363 void setInputStream(Slot startChannel,
const BufferStream& stream);
372 void setIndexBuffer(Type type,
const BufferPointer& buffer, Offset offset);
379 void setIndexBuffer(
const BufferView& buffer);
392 void setIndirectBuffer(
const BufferPointer& buffer, Offset offset = 0, Offset stride = 0);
395 class DrawIndirectCommand {
398 uint _instanceCount { 0 };
399 uint _firstIndex { 0 };
400 uint _baseInstance { 0 };
404 class DrawIndexedIndirectCommand {
407 uint _instanceCount { 0 };
408 uint _firstIndex { 0 };
409 uint _baseVertex { 0 };
410 uint _baseInstance { 0 };
423 void setModelTransform(
const Transform& model);
430 void setModelTransform(
const Transform& model,
const Transform& previousModel);
435 void resetViewTransform() { setViewTransform(Transform(),
false); }
445 void setViewTransform(
const Transform& view,
bool camera =
true);
454 void setProjectionTransform(
const Mat4& proj);
463 void setProjectionJitterEnabled(
bool isProjectionEnabled);
471 void setProjectionJitterSequence(
const Vec2* sequence,
size_t count);
478 void setProjectionJitterScale(
float scale);
487 void pushProjectionJitterEnabled(
bool isProjectionEnabled);
494 void popProjectionJitterEnabled();
503 void setViewportTransform(
const Vec4i& viewport);
514 void setDepthRangeTransform(
float nearDepth,
float farDepth);
525 void saveViewProjectionTransform(uint saveSlot);
532 void setSavedViewProjectionTransform(uint saveSlot);
543 void copySavedViewProjectionTransformToBuffer(uint saveSlot,
const BufferPointer& buffer, Offset offset);
551 void setPipeline(
const PipelinePointer& pipeline);
561 void setStateBlendFactor(
const Vec4& factor);
571 void setStateScissorRect(
const Vec4i& rect);
583 void setUniformBuffer(uint32 slot,
const BufferPointer& buffer, Offset offset, Offset size);
591 void setUniformBuffer(uint32 slot,
const BufferView& view);
602 void setResourceBuffer(uint32 slot,
const BufferPointer& buffer);
612 void setResourceTexture(uint32 slot,
const TexturePointer& texture);
620 void setResourceTexture(uint32 slot,
const TextureView& view);
628 void setResourceTextureTable(
const TextureTablePointer& table, uint32 slot = 0);
640 void setResourceFramebufferSwapChainTexture(uint32 slot,
const FramebufferSwapChainPointer& framebuffer,
unsigned int swapChainIndex,
unsigned int renderBufferSlot = 0U);
648 void setFramebuffer(
const FramebufferPointer& framebuffer);
658 void setFramebufferSwapChain(
const FramebufferSwapChainPointer& framebuffer,
unsigned int swapChainIndex);
667 void advance(
const SwapChainPointer& swapChain);
681 void clearFramebuffer(Framebuffer::Masks targets,
const Vec4& color,
float depth,
int stencil,
bool enableScissor =
false);
692 void clearColorFramebuffer(Framebuffer::Masks targets,
const Vec4& color,
bool enableScissor =
false);
702 void clearDepthFramebuffer(
float depth,
bool enableScissor =
false);
712 void clearStencilFramebuffer(
int stencil,
bool enableScissor =
false);
723 void clearDepthStencilFramebuffer(
float depth,
int stencil,
bool enableScissor =
false);
736 void blit(
const FramebufferPointer& src,
const Vec4i& srcRect,
const FramebufferPointer& dst,
const Vec4i& dstRect);
745 void generateTextureMips(
const TexturePointer& texture);
756 void generateTextureMipsWithPipeline(
const TexturePointer& destTexture,
int numMips = -1);
767 void beginQuery(
const QueryPointer& query);
774 void endQuery(
const QueryPointer& query);
781 void getQuery(
const QueryPointer& query);
794 void disableContextViewCorrection();
801 void restoreContextViewCorrection();
813 void setContextMirrorViewCorrection(
bool shouldMirror);
820 void disableContextStereo();
827 void restoreContextStereo();
838 void pushProfileRange(
const char* name);
843 void popProfileRange();
858 void _glUniform1i(
int location,
int v0);
866 void _glUniform1f(
int location,
float v0);
875 void _glUniform2f(
int location,
float v0,
float v1);
885 void _glUniform3f(
int location,
float v0,
float v1,
float v2);
896 void _glUniform4f(
int location,
float v0,
float v1,
float v2,
float v3);
905 void _glUniform3fv(
int location,
int count,
const float* value);
914 void _glUniform4fv(
int location,
int count,
const float* value);
923 void _glUniform4iv(
int location,
int count,
const int* value);
933 void _glUniformMatrix3fv(
int location,
int count,
unsigned char transpose,
const float* value);
943 void _glUniformMatrix4fv(
int location,
int count,
unsigned char transpose,
const float* value);
951 void _glUniform(
int location,
int v0) {
952 _glUniform1i(location, v0);
961 void _glUniform(
int location,
float v0) {
962 _glUniform1f(location, v0);
971 void _glUniform(
int location,
const glm::vec2& v) {
972 _glUniform2f(location, v.x, v.y);
981 void _glUniform(
int location,
const glm::vec3& v) {
982 _glUniform3f(location, v.x, v.y, v.z);
991 void _glUniform(
int location,
const glm::vec4& v) {
992 _glUniform4f(location, v.x, v.y, v.z, v.w);
1001 void _glUniform(
int location,
const glm::mat3& v) {
1002 _glUniformMatrix3fv(location, 1,
false, glm::value_ptr(v));
1011 void _glUniform(
int location,
const glm::mat4& v) {
1012 _glUniformMatrix4fv(location, 1,
false, glm::value_ptr(v));
1024 void runLambda(std::function<
void()> f);
1028 COMMAND_drawIndexed,
1029 COMMAND_drawInstanced,
1030 COMMAND_drawIndexedInstanced,
1031 COMMAND_multiDrawIndirect,
1032 COMMAND_multiDrawIndexedIndirect,
1034 COMMAND_setInputFormat,
1035 COMMAND_setInputBuffer,
1036 COMMAND_setIndexBuffer,
1037 COMMAND_setIndirectBuffer,
1039 COMMAND_setModelTransform,
1040 COMMAND_setViewTransform,
1041 COMMAND_setProjectionTransform,
1042 COMMAND_setProjectionJitterEnabled,
1043 COMMAND_setProjectionJitterSequence,
1044 COMMAND_setProjectionJitterScale,
1045 COMMAND_setViewportTransform,
1046 COMMAND_setDepthRangeTransform,
1048 COMMAND_saveViewProjectionTransform,
1049 COMMAND_setSavedViewProjectionTransform,
1050 COMMAND_copySavedViewProjectionTransformToBuffer,
1052 COMMAND_setPipeline,
1053 COMMAND_setStateBlendFactor,
1054 COMMAND_setStateScissorRect,
1056 COMMAND_setUniformBuffer,
1057 COMMAND_setResourceBuffer,
1058 COMMAND_setResourceTexture,
1059 COMMAND_setResourceTextureTable,
1060 COMMAND_setResourceFramebufferSwapChainTexture,
1062 COMMAND_setFramebuffer,
1063 COMMAND_setFramebufferSwapChain,
1064 COMMAND_clearFramebuffer,
1066 COMMAND_generateTextureMips,
1067 COMMAND_generateTextureMipsWithPipeline,
1075 COMMAND_resetStages,
1077 COMMAND_disableContextViewCorrection,
1078 COMMAND_restoreContextViewCorrection,
1079 COMMAND_setContextMirrorViewCorrection,
1081 COMMAND_disableContextStereo,
1082 COMMAND_restoreContextStereo,
1086 COMMAND_startNamedCall,
1087 COMMAND_stopNamedCall,
1092 COMMAND_glUniform1i,
1093 COMMAND_glUniform1f,
1094 COMMAND_glUniform2f,
1095 COMMAND_glUniform3f,
1096 COMMAND_glUniform4f,
1097 COMMAND_glUniform3fv,
1098 COMMAND_glUniform4fv,
1099 COMMAND_glUniform4iv,
1100 COMMAND_glUniformMatrix3fv,
1101 COMMAND_glUniformMatrix4fv,
1103 COMMAND_pushProfileRange,
1104 COMMAND_popProfileRange,
1108 typedef std::vector<Command> Commands;
1109 typedef std::vector<size_t> CommandOffsets;
1116 const Commands& getCommands()
const {
return _commands; }
1125 const CommandOffsets& getCommandOffsets()
const {
return _commandOffsets; }
1133 #if (QT_POINTER_SIZE == 8)
1139 char _chars[
sizeof(size_t)];
1141 #if (QT_POINTER_SIZE == 8)
1142 Param(
size_t val) : _size(val) {}
1144 Param(int32 val) : _int(val) {}
1145 Param(uint32 val) : _uint(val) {}
1146 Param(
float val) : _float(val) {}
1148 typedef std::vector<Param> Params;
1157 const Params& getParams()
const {
return _params; }
1166 template <
typename T>
1171 Cache(
const Data& data) : _data(data) {}
1179 std::vector< Cache<T> > _items;
1182 _items.reserve(
_max);
1186 _max = std::max(_items.size(),
_max);
1193 size_t size()
const {
return _items.size(); }
1202 size_t offset = _items.size();
1203 _items.emplace_back(data);
1213 const Data&
get(uint32 offset)
const {
1214 assert((offset < _items.size()));
1215 return (_items.data() + offset)->_data;
1227 using CommandHandler = std::function<void(Command,
const Param*)>;
1236 void forEachCommand(
const CommandHandler& handler)
const {
1237 size_t count = _commands.size();
1238 for (
size_t i = 0; i < count; ++i) {
1239 const auto command = _commands[i];
1240 const auto offset = _commandOffsets[i];
1241 const Param* params = _params.data() + offset;
1242 handler(command, params);
1246 typedef Cache<BufferPointer>::Vector BufferCaches;
1247 typedef Cache<TexturePointer>::Vector TextureCaches;
1248 typedef Cache<TextureTablePointer>::Vector TextureTableCaches;
1249 typedef Cache<Sampler>::Vector SamplerCaches;
1250 typedef Cache<Stream::FormatPointer>::Vector StreamFormatCaches;
1251 typedef Cache<Transform>::Vector TransformCaches;
1252 typedef Cache<PipelinePointer>::Vector PipelineCaches;
1253 typedef Cache<FramebufferPointer>::Vector FramebufferCaches;
1254 typedef Cache<SwapChainPointer>::Vector SwapChainCaches;
1255 typedef Cache<QueryPointer>::Vector QueryCaches;
1256 typedef Cache<std::string>::Vector StringCaches;
1257 typedef Cache<std::function<void()>>::Vector LambdaCache;
1261 typedef unsigned char Byte;
1262 typedef std::vector<Byte> Bytes;
1273 size_t cacheData(
size_t size,
const void* data);
1283 Byte* editData(
size_t offset) {
1284 if (offset >= _data.size()) {
1287 return (_data.data() + offset);
1296 const Byte* readData(
size_t offset)
const {
1297 if (offset >= _data.size()) {
1300 return (_data.data() + offset);
1304 static size_t _commandsMax;
1306 CommandOffsets _commandOffsets;
1307 static size_t _commandOffsetsMax;
1310 static size_t _paramsMax;
1313 static size_t _dataMax;
1315 #include "TransformObject_shared.slh"
1317 using TransformObjects = std::vector<TransformObject>;
1323 bool _invalidModel {
true };
1328 Transform _currentModel;
1335 Transform _previousModel;
1343 mutable bool _mustUpdatePreviousModels;
1348 mutable TransformObjects _objects;
1349 static size_t _objectsMax;
1351 Stream::FormatPointer _currentStreamFormat;
1352 PipelinePointer _currentPipeline;
1356 BufferCaches _buffers;
1357 TextureCaches _textures;
1358 TextureTableCaches _textureTables;
1359 SamplerCaches _samplers;
1360 StreamFormatCaches _streamFormats;
1361 TransformCaches _transforms;
1362 PipelineCaches _pipelines;
1363 FramebufferCaches _framebuffers;
1364 SwapChainCaches _swapChains;
1365 QueryCaches _queries;
1366 LambdaCache _lambdas;
1367 StringCaches _profileRanges;
1372 StringCaches _names;
1377 NamedBatchDataMap _namedData;
1382 bool _isJitterOnProjectionEnabled {
false };
1390 uint16_t _drawcallUniform { 0 };
1398 uint16_t _drawcallUniformReset { 0 };
1404 bool _enableStereo {
true };
1409 bool _enableSkybox {
false };
1414 friend class Context;
1424 void finishFrame(BufferUpdates& updates);
1442 void validateDrawState()
const;
1451 void startNamedCall(
const std::string& name);
1458 void stopNamedCall();
1465 void captureDrawCallInfoImpl();
1472 template <
typename T>
1477 #if defined(NSIGHT_FOUND)
1501 #define PROFILE_RANGE_BATCH(batch, name) ProfileRangeBatch profileRangeThis(batch, name);
1505 #define PROFILE_RANGE_BATCH(batch, name)
Provides the Mat4 scripting interface.
Definition: Mat4.h:44
~ProfileRangeBatch()
Adds popProfileRange command.
Definition: Batch.cpp:33
ProfileRangeBatch(gpu::Batch &batch, const char *name)
Adds pushProfileRange command.
Definition: Batch.cpp:29
Templated container for objects inheriting from gpu::Object.
Definition: Batch.h:1177
const Data & get(uint32 offset) const
Retrieve object with given index.
Definition: Batch.h:1213
size_t cache(const Data &data)
Adds a new object to the container.
Definition: Batch.h:1201
void clear()
Clear contents of this container.
Definition: Batch.h:1221
size_t size() const
Get the current size of the container.
Definition: Batch.h:1193
The template cache mechanism for the gpu::Object passed to the gpu::Batch.
Definition: Batch.h:1167
static size_t _max
Definition: Batch.h:1172
DrawCallInfo(Index idx, Index user)
Create DrawCallInfo object with user data. It's used when drawing meshes. First bit of user data enab...
Definition: Batch.h:73
DrawCallInfo(Index idx)
Create DrawCallInfo object with no user data.
Definition: Batch.h:65
A variant-like class representing single renderer command parameter.
Definition: Batch.h:1130
void process(Batch &batch)
Adds commands for drawInstanced call for this set of instanced objects.
Definition: Batch.h:116
BufferPointers buffers
Definition: Batch.h:96
DrawCallInfoBuffer drawCallInfos
Definition: Batch.h:108