Batch.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "Drawable.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. /// Queued 3D geometry draw call.
  45. struct Batch
  46. {
  47. /// Construct with defaults.
  48. Batch() :
  49. lightQueue_(0),
  50. isBase_(false)
  51. {
  52. }
  53. /// Construct from a drawable's source batch.
  54. Batch(const SourceBatch& rhs) :
  55. distance_(rhs.distance_),
  56. geometry_(rhs.geometry_),
  57. material_(rhs.material_),
  58. worldTransform_(rhs.worldTransform_),
  59. lightQueue_(0),
  60. shaderData_(rhs.shaderData_),
  61. shaderDataSize_(rhs.shaderDataSize_),
  62. geometryType_(rhs.geometryType_),
  63. overrideView_(rhs.overrideView_),
  64. isBase_(false)
  65. {
  66. }
  67. /// Calculate state sorting key, which consists of base pass flag, light, pass and geometry.
  68. void CalculateSortKey();
  69. /// Prepare for rendering.
  70. void Prepare(Graphics* graphics, Renderer* renderer, bool setModelTransform = true) const;
  71. /// Prepare and draw.
  72. void Draw(Graphics* graphics, Renderer* renderer) const;
  73. /// State sorting key.
  74. unsigned long long sortKey_;
  75. /// Distance from camera.
  76. float distance_;
  77. /// Geometry.
  78. Geometry* geometry_;
  79. /// Material.
  80. Material* material_;
  81. /// %Object's world transform.
  82. const Matrix3x4* worldTransform_;
  83. /// Camera.
  84. Camera* camera_;
  85. /// Zone.
  86. Zone* zone_;
  87. /// Light properties.
  88. LightBatchQueue* lightQueue_;
  89. /// Material pass.
  90. Pass* pass_;
  91. /// Vertex shader.
  92. ShaderVariation* vertexShader_;
  93. /// Pixel shader.
  94. ShaderVariation* pixelShader_;
  95. /// Vertex shader data.
  96. const float* shaderData_;
  97. /// Vertex shader data size in floats.
  98. unsigned shaderDataSize_;
  99. /// %Geometry type.
  100. GeometryType geometryType_;
  101. /// Override view transform flag.
  102. bool overrideView_;
  103. /// Base batch flag. This tells to draw the object fully without light optimizations.
  104. bool isBase_;
  105. /// 8-bit light mask for stencil marking in deferred rendering.
  106. unsigned char lightMask_;
  107. };
  108. /// Data for one geometry instance.
  109. struct InstanceData
  110. {
  111. /// Construct undefined.
  112. InstanceData()
  113. {
  114. }
  115. /// Construct with transform and distance.
  116. InstanceData(const Matrix3x4* worldTransform, float distance) :
  117. worldTransform_(worldTransform),
  118. distance_(distance)
  119. {
  120. }
  121. /// World transform.
  122. const Matrix3x4* worldTransform_;
  123. /// Distance from camera.
  124. float distance_;
  125. };
  126. /// Instanced 3D geometry draw call.
  127. struct BatchGroup : public Batch
  128. {
  129. /// Construct with defaults.
  130. BatchGroup() :
  131. startIndex_(M_MAX_UNSIGNED)
  132. {
  133. }
  134. /// Construct from a batch.
  135. BatchGroup(const Batch& batch) :
  136. Batch(batch),
  137. startIndex_(M_MAX_UNSIGNED)
  138. {
  139. }
  140. /// Destruct.
  141. ~BatchGroup()
  142. {
  143. }
  144. /// Pre-set the instance transforms. Buffer must be big enough to hold all transforms.
  145. void SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex);
  146. /// Prepare and draw.
  147. void Draw(Graphics* graphics, Renderer* renderer) const;
  148. /// Instance data.
  149. PODVector<InstanceData> instances_;
  150. /// Instance stream start index, or M_MAX_UNSIGNED if transforms not pre-set.
  151. unsigned startIndex_;
  152. };
  153. /// Instanced draw call grouping key.
  154. struct BatchGroupKey
  155. {
  156. /// Construct undefined.
  157. BatchGroupKey()
  158. {
  159. }
  160. /// Construct from a batch.
  161. BatchGroupKey(const Batch& batch) :
  162. zone_(batch.zone_),
  163. lightQueue_(batch.lightQueue_),
  164. pass_(batch.pass_),
  165. material_(batch.material_),
  166. geometry_(batch.geometry_)
  167. {
  168. }
  169. /// Zone.
  170. Zone* zone_;
  171. /// Light properties.
  172. LightBatchQueue* lightQueue_;
  173. /// Material pass.
  174. Pass* pass_;
  175. /// Material.
  176. Material* material_;
  177. /// Geometry.
  178. Geometry* geometry_;
  179. /// Test for equality with another batch group key.
  180. bool operator == (const BatchGroupKey& rhs) const { return zone_ == rhs.zone_ && lightQueue_ == rhs.lightQueue_ && pass_ == rhs.pass_ && material_ == rhs.material_ && geometry_ == rhs.geometry_; }
  181. /// Test for inequality with another batch group key.
  182. bool operator != (const BatchGroupKey& rhs) const { return zone_ != rhs.zone_ || lightQueue_ != rhs.lightQueue_ || pass_ != rhs.pass_ || material_ != rhs.material_ || geometry_ != rhs.geometry_; }
  183. /// Return hash value.
  184. unsigned ToHash() const;
  185. };
  186. /// Queue that contains both instanced and non-instanced draw calls.
  187. struct BatchQueue
  188. {
  189. public:
  190. /// Clear for new frame by clearing all groups and batches.
  191. void Clear(int maxSortedInstances);
  192. /// Sort non-instanced draw calls back to front.
  193. void SortBackToFront();
  194. /// Sort instanced and non-instanced draw calls front to back.
  195. void SortFrontToBack();
  196. /// Pre-set instance transforms of all groups. The vertex buffer must be big enough to hold all transforms.
  197. void SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex);
  198. /// Draw.
  199. void Draw(Graphics* graphics, Renderer* renderer, bool useScissor = false, bool markToStencil = false) const;
  200. /// Draw with forward light optimizations.
  201. void Draw(Light* light, Graphics* graphics, Renderer* renderer) const;
  202. /// Return the combined amount of instances.
  203. unsigned GetNumInstances(Renderer* renderer) const;
  204. /// Return whether the batch group is empty.
  205. bool IsEmpty() const { return batches_.Empty() && baseBatchGroups_.Empty() && batchGroups_.Empty(); }
  206. /// Instanced draw calls with base flag.
  207. HashMap<BatchGroupKey, BatchGroup> baseBatchGroups_;
  208. /// Instanced draw calls.
  209. HashMap<BatchGroupKey, BatchGroup> batchGroups_;
  210. /// Unsorted non-instanced draw calls.
  211. PODVector<Batch> batches_;
  212. /// Sorted non-instanced draw calls with base flag.
  213. PODVector<Batch*> sortedBaseBatches_;
  214. /// Sorted non-instanced draw calls.
  215. PODVector<Batch*> sortedBatches_;
  216. /// Sorted instanced draw calls with base flag.
  217. PODVector<BatchGroup*> sortedBaseBatchGroups_;
  218. /// Sorted instanced draw calls.
  219. PODVector<BatchGroup*> sortedBatchGroups_;
  220. /// Maximum sorted instances.
  221. unsigned maxSortedInstances_;
  222. };
  223. /// Queue for shadow map draw calls
  224. struct ShadowBatchQueue
  225. {
  226. /// Shadow map camera.
  227. Camera* shadowCamera_;
  228. /// Shadow map viewport.
  229. IntRect shadowViewport_;
  230. /// Shadow caster draw calls.
  231. BatchQueue shadowBatches_;
  232. /// Directional light cascade near split distance.
  233. float nearSplit_;
  234. /// Directional light cascade far split distance.
  235. float farSplit_;
  236. };
  237. /// Queue for light related draw calls.
  238. struct LightBatchQueue
  239. {
  240. /// Per-pixel light.
  241. Light* light_;
  242. /// Shadow map depth texture.
  243. Texture2D* shadowMap_;
  244. /// Lit geometry draw calls.
  245. BatchQueue litBatches_;
  246. /// Shadow map split queues.
  247. Vector<ShadowBatchQueue> shadowSplits_;
  248. /// Per-vertex lights.
  249. PODVector<Light*> vertexLights_;
  250. /// Light volume draw calls.
  251. PODVector<Batch> volumeBatches_;
  252. };