CommandBuffer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright (C) 2009-2015, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include "anki/gr/GrObject.h"
  7. namespace anki {
  8. /// @addtogroup graphics
  9. /// @{
  10. /// The draw indirect structure for index drawing, also the parameters of a
  11. /// regular drawcall
  12. class DrawElementsIndirectInfo
  13. {
  14. public:
  15. DrawElementsIndirectInfo()
  16. {}
  17. DrawElementsIndirectInfo(U32 count, U32 instanceCount, U32 firstIndex,
  18. U32 baseVertex, U32 baseInstance)
  19. : m_count(count)
  20. , m_instanceCount(instanceCount)
  21. , m_firstIndex(firstIndex)
  22. , m_baseVertex(baseVertex)
  23. , m_baseInstance(baseInstance)
  24. {}
  25. U32 m_count = MAX_U32;
  26. U32 m_instanceCount = 1;
  27. U32 m_firstIndex = 0;
  28. U32 m_baseVertex = 0;
  29. U32 m_baseInstance = 0;
  30. };
  31. /// The draw indirect structure for arrays drawing, also the parameters of a
  32. /// regular drawcall
  33. class DrawArraysIndirectInfo
  34. {
  35. public:
  36. DrawArraysIndirectInfo()
  37. {}
  38. DrawArraysIndirectInfo(U32 count, U32 instanceCount, U32 first,
  39. U32 baseInstance)
  40. : m_count(count)
  41. , m_instanceCount(instanceCount)
  42. , m_first(first)
  43. , m_baseInstance(baseInstance)
  44. {}
  45. U32 m_count = MAX_U32;
  46. U32 m_instanceCount = 1;
  47. U32 m_first = 0;
  48. U32 m_baseInstance = 0;
  49. };
  50. /// Command buffer initialization hints. They are used to optimize the
  51. /// allocators of a command buffer.
  52. class CommandBufferInitHints
  53. {
  54. friend class CommandBufferImpl;
  55. private:
  56. enum
  57. {
  58. MAX_CHUNK_SIZE = 4 * 1024 * 1024 // 4MB
  59. };
  60. PtrSize m_chunkSize = 1024;
  61. };
  62. /// Command buffer.
  63. class CommandBuffer: public GrObject
  64. {
  65. public:
  66. /// Construct.
  67. CommandBuffer(GrManager* manager);
  68. /// Destroy.
  69. ~CommandBuffer();
  70. /// Access the implementation.
  71. CommandBufferImpl& getImplementation()
  72. {
  73. return *m_impl;
  74. }
  75. /// Create command buffer.
  76. void create(CommandBufferInitHints hints = CommandBufferInitHints());
  77. /// Compute initialization hints.
  78. CommandBufferInitHints computeInitHints() const;
  79. /// Flush command buffer for deferred execution.
  80. void flush();
  81. /// Flush and wait to finish.
  82. void finish();
  83. /// @name State manipulation
  84. /// @{
  85. /// Set the viewport.
  86. void setViewport(U16 minx, U16 miny, U16 maxx, U16 maxy);
  87. /// Bind pipeline.
  88. void bindPipeline(PipelinePtr ppline);
  89. /// Bind framebuffer.
  90. void bindFramebuffer(FramebufferPtr fb);
  91. /// Bind resources.
  92. void bindResourceGroup(ResourceGroupPtr rc, U slot);
  93. /// @}
  94. /// @name Drawcalls
  95. /// @{
  96. void drawElements(U32 count, U32 instanceCount = 1, U32 firstIndex = 0,
  97. U32 baseVertex = 0, U32 baseInstance = 0);
  98. void drawArrays(U32 count, U32 instanceCount = 1, U32 first = 0,
  99. U32 baseInstance = 0);
  100. void drawElementsConditional(OcclusionQueryPtr query, U32 count,
  101. U32 instanceCount = 1, U32 firstIndex = 0, U32 baseVertex = 0,
  102. U32 baseInstance = 0);
  103. void drawArraysConditional(OcclusionQueryPtr query, U32 count,
  104. U32 instanceCount = 1, U32 first = 0, U32 baseInstance = 0);
  105. void dispatchCompute(U32 groupCountX, U32 groupCountY, U32 groupCountZ);
  106. void generateMipmaps(TexturePtr tex);
  107. /// @}
  108. /// @name Resource upload
  109. /// @{
  110. /// Used to upload data to a texture.
  111. template<typename Type>
  112. void textureUpload(TexturePtr tex, U32 mipmap, U32 slice, PtrSize dataSize,
  113. Type*& data)
  114. {
  115. void* vdata = nullptr;
  116. textureUploadInternal(tex, mipmap, slice, dataSize, vdata);
  117. data = static_cast<Type*>(vdata);
  118. }
  119. /// Write data to a buffer.
  120. template<typename Type>
  121. void writeBuffer(BufferPtr buff, PtrSize offset, PtrSize range,
  122. Type*& data)
  123. {
  124. void* vdata = nullptr;
  125. writeBufferInternal(buff, offset, range, vdata);
  126. data = static_cast<Type*>(vdata);
  127. }
  128. /// Update dynamic uniforms.
  129. template<typename Type>
  130. void updateDynamicUniforms(U32 size, Type*& data)
  131. {
  132. void* vdata = nullptr;
  133. updateDynamicUniformsInternal(size, vdata);
  134. data = static_cast<Type*>(vdata);
  135. }
  136. /// @}
  137. /// @name Other
  138. /// @{
  139. /// Begin query.
  140. void beginOcclusionQuery(OcclusionQueryPtr query);
  141. /// End query.
  142. void endOcclusionQuery(OcclusionQueryPtr query);
  143. /// Append a second level command buffer.
  144. void pushSecondLevelCommandBuffer(CommandBufferPtr cmdb);
  145. Bool isEmpty() const;
  146. /// @}
  147. private:
  148. UniquePtr<CommandBufferImpl> m_impl;
  149. void textureUploadInternal(TexturePtr tex, U32 mipmap, U32 slice,
  150. PtrSize dataSize, void*& data);
  151. void writeBufferInternal(BufferPtr buff, PtrSize offset, PtrSize range,
  152. void*& data);
  153. void updateDynamicUniformsInternal(U32 size, void*& data);
  154. };
  155. /// @}
  156. } // end namespace anki