Batch.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "GraphicsDefs.h"
  25. #include "HashMap.h"
  26. #include "MathDefs.h"
  27. #include "Ptr.h"
  28. #include "Rect.h"
  29. #include "Vector4.h"
  30. class Camera;
  31. class Drawable;
  32. class Geometry;
  33. class Graphics;
  34. class Light;
  35. class Material;
  36. class Matrix3x4;
  37. class Pass;
  38. class Renderer;
  39. class ShaderVariation;
  40. class Texture2D;
  41. class VertexBuffer;
  42. class Zone;
  43. struct LightBatchQueue;
  44. /// Description of a 3D geometry draw call.
  45. struct Batch
  46. {
  47. /// Construct with defaults.
  48. Batch() :
  49. lightQueue_(0),
  50. shaderData_(0),
  51. shaderDataSize_(0),
  52. geometryType_(GEOM_STATIC),
  53. overrideView_(false),
  54. isBase_(false)
  55. {
  56. }
  57. /// Calculate state sorting key, which consists of base pass flag, light, pass and geometry.
  58. void CalculateSortKey();
  59. /// Prepare for rendering.
  60. void Prepare(Graphics* graphics, Renderer* renderer, bool setModelTransform = true) const;
  61. /// Prepare and draw.
  62. void Draw(Graphics* graphics, Renderer* renderer) const;
  63. /// State sorting key.
  64. unsigned long long sortKey_;
  65. /// Distance from camera.
  66. float distance_;
  67. /// Geometry.
  68. Geometry* geometry_;
  69. /// Model world transform.
  70. const Matrix3x4* worldTransform_;
  71. /// Camera.
  72. Camera* camera_;
  73. /// Zone.
  74. Zone* zone_;
  75. /// Light properties.
  76. LightBatchQueue* lightQueue_;
  77. /// Material.
  78. Material* material_;
  79. /// Material pass.
  80. Pass* pass_;
  81. /// Vertex shader.
  82. ShaderVariation* vertexShader_;
  83. /// Pixel shader.
  84. ShaderVariation* pixelShader_;
  85. /// Vertex shader data.
  86. const float* shaderData_;
  87. /// Vertex shader data size in floats.
  88. unsigned shaderDataSize_;
  89. /// Geometry type.
  90. GeometryType geometryType_;
  91. /// Override view transform flag.
  92. bool overrideView_;
  93. /// Base batch flag. This tells to draw the object fully without light optimizations.
  94. bool isBase_;
  95. /// 8-bit light mask for stencil marking in deferred rendering.
  96. unsigned char lightMask_;
  97. };
  98. /// Data for one geometry instance.
  99. struct InstanceData
  100. {
  101. /// Construct undefined.
  102. InstanceData()
  103. {
  104. }
  105. /// Construct with transform and distance.
  106. InstanceData(const Matrix3x4* worldTransform, float distance) :
  107. worldTransform_(worldTransform),
  108. distance_(distance)
  109. {
  110. }
  111. /// World transform.
  112. const Matrix3x4* worldTransform_;
  113. /// Distance from camera.
  114. float distance_;
  115. };
  116. /// Instanced 3D geometry draw call.
  117. struct BatchGroup : public Batch
  118. {
  119. /// Construct with defaults.
  120. BatchGroup() :
  121. startIndex_(M_MAX_UNSIGNED)
  122. {
  123. }
  124. /// Construct from a batch.
  125. BatchGroup(const Batch& batch) :
  126. Batch(batch),
  127. startIndex_(M_MAX_UNSIGNED)
  128. {
  129. }
  130. /// Destruct.
  131. ~BatchGroup()
  132. {
  133. }
  134. /// Pre-set the instance transforms. Buffer must be big enough to hold all transforms.
  135. void SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex);
  136. /// Prepare and draw.
  137. void Draw(Graphics* graphics, Renderer* renderer) const;
  138. /// Instance data.
  139. PODVector<InstanceData> instances_;
  140. /// Instance stream start index, or M_MAX_UNSIGNED if transforms not pre-set.
  141. unsigned startIndex_;
  142. };
  143. /// Instanced draw call key.
  144. struct BatchGroupKey
  145. {
  146. /// Construct undefined.
  147. BatchGroupKey()
  148. {
  149. }
  150. /// Construct from a batch.
  151. BatchGroupKey(const Batch& batch) :
  152. zone_(batch.zone_),
  153. lightQueue_(batch.lightQueue_),
  154. pass_(batch.pass_),
  155. material_(batch.material_),
  156. geometry_(batch.geometry_)
  157. {
  158. }
  159. /// Zone.
  160. Zone* zone_;
  161. /// Light properties.
  162. LightBatchQueue* lightQueue_;
  163. /// Material pass.
  164. Pass* pass_;
  165. /// Material.
  166. Material* material_;
  167. /// Geometry.
  168. Geometry* geometry_;
  169. /// Test for equality with another batch group key.
  170. bool operator == (const BatchGroupKey& rhs) const { return zone_ == rhs.zone_ && lightQueue_ == rhs.lightQueue_ && pass_ == rhs.pass_ && material_ == rhs.material_ && geometry_ == rhs.geometry_; }
  171. /// Test for inequality with another batch group key.
  172. bool operator != (const BatchGroupKey& rhs) const { return zone_ != rhs.zone_ || lightQueue_ != rhs.lightQueue_ || pass_ != rhs.pass_ || material_ != rhs.material_ || geometry_ != rhs.geometry_; }
  173. /// Return hash value.
  174. unsigned ToHash() const { return (unsigned)zone_ + (unsigned)lightQueue_ + (unsigned)pass_ + (unsigned)material_ + (unsigned)geometry_; }
  175. };
  176. /// Queue that contains both instanced and non-instanced draw calls.
  177. struct BatchQueue
  178. {
  179. public:
  180. /// Clear everything.
  181. void Clear();
  182. /// Sort non-instanced draw calls back to front.
  183. void SortBackToFront();
  184. /// Sort instanced and non-instanced draw calls front to back.
  185. void SortFrontToBack();
  186. /// Pre-set instance transforms of all groups. The vertex buffer must be big enough to hold all transforms.
  187. void SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex);
  188. /// Draw.
  189. void Draw(Graphics* graphics, Renderer* renderer, bool useScissor = false, bool markToStencil = false) const;
  190. /// Draw with forward light optimizations.
  191. void Draw(Light* light, Graphics* graphics, Renderer* renderer) const;
  192. /// Return the combined amount of instances.
  193. unsigned GetNumInstances(Renderer* renderer) const;
  194. /// Return whether the batch group is empty.
  195. bool IsEmpty() const { return batches_.Empty() && baseBatchGroups_.Empty() && batchGroups_.Empty(); }
  196. /// Instanced draw calls with base flag.
  197. HashMap<BatchGroupKey, BatchGroup> baseBatchGroups_;
  198. /// Instanced draw calls.
  199. HashMap<BatchGroupKey, BatchGroup> batchGroups_;
  200. /// Unsorted non-instanced draw calls.
  201. PODVector<Batch> batches_;
  202. /// Sorted non-instanced draw calls with base flag.
  203. PODVector<Batch*> sortedBaseBatches_;
  204. /// Sorted non-instanced draw calls.
  205. PODVector<Batch*> sortedBatches_;
  206. /// Sorted instanced draw calls with base flag.
  207. PODVector<BatchGroup*> sortedBaseBatchGroups_;
  208. /// Sorted instanced draw calls.
  209. PODVector<BatchGroup*> sortedBatchGroups_;
  210. };
  211. /// Queue for shadow map draw calls
  212. struct ShadowBatchQueue
  213. {
  214. /// Shadow map camera.
  215. Camera* shadowCamera_;
  216. /// Shadow map viewport.
  217. IntRect shadowViewport_;
  218. /// Shadow caster draw calls.
  219. BatchQueue shadowBatches_;
  220. /// Directional light cascade near split distance.
  221. float nearSplit_;
  222. /// Directional light cascade far split distance.
  223. float farSplit_;
  224. };
  225. /// Queue for light related draw calls
  226. struct LightBatchQueue
  227. {
  228. /// Per-pixel light.
  229. Light* light_;
  230. /// Lit geometry draw calls.
  231. BatchQueue litBatches_;
  232. /// Shadow map depth texture.
  233. Texture2D* shadowMap_;
  234. /// Shadow map split queues.
  235. Vector<ShadowBatchQueue> shadowSplits_;
  236. /// Per-vertex lights.
  237. PODVector<Light*> vertexLights_;
  238. /// Light volume draw calls.
  239. PODVector<Batch> volumeBatches_;
  240. };