Batch.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "../Graphics/Drawable.h"
  24. #include "../Math/MathDefs.h"
  25. #include "../Math/Matrix3x4.h"
  26. #include "../Container/Ptr.h"
  27. #include "../Math/Rect.h"
  28. namespace Urho3D
  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 { return zone_ == rhs.zone_ && lightQueue_ == rhs.lightQueue_ && pass_ == rhs.pass_ && material_ == rhs.material_ && geometry_ == rhs.geometry_; }
  185. /// Test for inequality with another batch group key.
  186. bool operator != (const BatchGroupKey& rhs) const { return zone_ != rhs.zone_ || lightQueue_ != rhs.lightQueue_ || pass_ != rhs.pass_ || material_ != rhs.material_ || geometry_ != rhs.geometry_; }
  187. /// Return hash value.
  188. unsigned ToHash() const;
  189. };
  190. /// Queue that contains both instanced and non-instanced draw calls.
  191. struct BatchQueue
  192. {
  193. public:
  194. /// Clear for new frame by clearing all groups and batches.
  195. void Clear(int maxSortedInstances);
  196. /// Sort non-instanced draw calls back to front.
  197. void SortBackToFront();
  198. /// Sort instanced and non-instanced draw calls front to back.
  199. void SortFrontToBack();
  200. /// Sort batches front to back while also maintaining state sorting.
  201. void SortFrontToBack2Pass(PODVector<Batch*>& batches);
  202. /// Pre-set instance transforms of all groups. The vertex buffer must be big enough to hold all transforms.
  203. void SetTransforms(void* lockedData, unsigned& freeIndex);
  204. /// Draw.
  205. void Draw(View* view, bool markToStencil, bool usingLightOptimization, bool allowDepthWrite) const;
  206. /// Return the combined amount of instances.
  207. unsigned GetNumInstances() const;
  208. /// Return whether the batch group is empty.
  209. bool IsEmpty() const { return batches_.Empty() && batchGroups_.Empty(); }
  210. /// Instanced draw calls.
  211. HashMap<BatchGroupKey, BatchGroup> batchGroups_;
  212. /// Shader remapping table for 2-pass state and distance sort.
  213. HashMap<unsigned, unsigned> shaderRemapping_;
  214. /// Material remapping table for 2-pass state and distance sort.
  215. HashMap<unsigned short, unsigned short> materialRemapping_;
  216. /// Geometry remapping table for 2-pass state and distance sort.
  217. HashMap<unsigned short, unsigned short> geometryRemapping_;
  218. /// Unsorted non-instanced draw calls.
  219. PODVector<Batch> batches_;
  220. /// Sorted non-instanced draw calls.
  221. PODVector<Batch*> sortedBatches_;
  222. /// Sorted instanced draw calls.
  223. PODVector<BatchGroup*> sortedBatchGroups_;
  224. /// Maximum sorted instances.
  225. unsigned maxSortedInstances_;
  226. };
  227. /// Queue for shadow map draw calls
  228. struct ShadowBatchQueue
  229. {
  230. /// Shadow map camera.
  231. Camera* shadowCamera_;
  232. /// Shadow map viewport.
  233. IntRect shadowViewport_;
  234. /// Shadow caster draw calls.
  235. BatchQueue shadowBatches_;
  236. /// Directional light cascade near split distance.
  237. float nearSplit_;
  238. /// Directional light cascade far split distance.
  239. float farSplit_;
  240. };
  241. /// Queue for light related draw calls.
  242. struct LightBatchQueue
  243. {
  244. /// Per-pixel light.
  245. Light* light_;
  246. /// Light negative flag.
  247. bool negative_;
  248. /// Shadow map depth texture.
  249. Texture2D* shadowMap_;
  250. /// Lit geometry draw calls, base (replace blend mode)
  251. BatchQueue litBaseBatches_;
  252. /// Lit geometry draw calls, non-base (additive)
  253. BatchQueue litBatches_;
  254. /// Shadow map split queues.
  255. Vector<ShadowBatchQueue> shadowSplits_;
  256. /// Per-vertex lights.
  257. PODVector<Light*> vertexLights_;
  258. /// Light volume draw calls.
  259. PODVector<Batch> volumeBatches_;
  260. };
  261. }