Batch.h 9.0 KB

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