Batch.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //
  2. // Copyright (c) 2008-2020 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 "../Graphics/Material.h"
  26. #include "../Math/MathDefs.h"
  27. #include "../Math/Matrix3x4.h"
  28. #include "../Math/Rect.h"
  29. namespace Urho3D
  30. {
  31. class Camera;
  32. class Drawable;
  33. class Geometry;
  34. class Light;
  35. class Material;
  36. class Matrix3x4;
  37. class Pass;
  38. class ShaderVariation;
  39. class Texture2D;
  40. class VertexBuffer;
  41. class View;
  42. class Zone;
  43. struct LightBatchQueue;
  44. /// Queued 3D geometry draw call.
  45. struct Batch
  46. {
  47. /// Construct with defaults.
  48. Batch() = default;
  49. /// Construct from a drawable's source batch.
  50. explicit Batch(const SourceBatch& rhs) :
  51. distance_(rhs.distance_),
  52. renderOrder_(rhs.material_ ? rhs.material_->GetRenderOrder() : DEFAULT_RENDER_ORDER),
  53. isBase_(false),
  54. geometry_(rhs.geometry_),
  55. material_(rhs.material_),
  56. worldTransform_(rhs.worldTransform_),
  57. numWorldTransforms_(rhs.numWorldTransforms_),
  58. instancingData_(rhs.instancingData_),
  59. lightQueue_(nullptr),
  60. geometryType_(rhs.geometryType_)
  61. {
  62. }
  63. /// Calculate state sorting key, which consists of base pass flag, light, pass and geometry.
  64. void CalculateSortKey();
  65. /// Prepare for rendering.
  66. void Prepare(View* view, Camera* camera, bool setModelTransform, bool allowDepthWrite) const;
  67. /// Prepare and draw.
  68. void Draw(View* view, Camera* camera, bool allowDepthWrite) const;
  69. /// State sorting key.
  70. unsigned long long sortKey_{};
  71. /// Distance from camera.
  72. float distance_{};
  73. /// 8-bit render order modifier from material.
  74. unsigned char renderOrder_{};
  75. /// 8-bit light mask for stencil marking in deferred rendering.
  76. unsigned char lightMask_{};
  77. /// Base batch flag. This tells to draw the object fully without light optimizations.
  78. bool isBase_{};
  79. /// Geometry.
  80. Geometry* geometry_{};
  81. /// Material.
  82. Material* material_{};
  83. /// World transform(s). For a skinned model, these are the bone transforms.
  84. const Matrix3x4* worldTransform_{};
  85. /// Number of world transforms.
  86. unsigned numWorldTransforms_{};
  87. /// Per-instance data. If not null, must contain enough data to fill instancing buffer.
  88. void* instancingData_{};
  89. /// Zone.
  90. Zone* zone_{};
  91. /// Light properties.
  92. LightBatchQueue* lightQueue_{};
  93. /// Material pass.
  94. Pass* pass_{};
  95. /// Vertex shader.
  96. ShaderVariation* vertexShader_{};
  97. /// Pixel shader.
  98. ShaderVariation* pixelShader_{};
  99. /// %Geometry type.
  100. GeometryType geometryType_{};
  101. };
  102. /// Data for one geometry instance.
  103. struct InstanceData
  104. {
  105. /// Construct undefined.
  106. InstanceData() = default;
  107. /// Construct with transform, instancing data and distance.
  108. InstanceData(const Matrix3x4* worldTransform, const void* instancingData, float distance) :
  109. worldTransform_(worldTransform),
  110. instancingData_(instancingData),
  111. distance_(distance)
  112. {
  113. }
  114. /// World transform.
  115. const Matrix3x4* worldTransform_{};
  116. /// Instancing data buffer.
  117. const void* instancingData_{};
  118. /// Distance from camera.
  119. float distance_{};
  120. };
  121. /// Instanced 3D geometry draw call.
  122. struct BatchGroup : public Batch
  123. {
  124. /// Construct with defaults.
  125. BatchGroup() :
  126. startIndex_(M_MAX_UNSIGNED)
  127. {
  128. }
  129. /// Construct from a batch.
  130. explicit BatchGroup(const Batch& batch) :
  131. Batch(batch),
  132. startIndex_(M_MAX_UNSIGNED)
  133. {
  134. }
  135. /// Destruct.
  136. ~BatchGroup() = default;
  137. /// Add world transform(s) from a batch.
  138. void AddTransforms(const Batch& batch)
  139. {
  140. InstanceData newInstance;
  141. newInstance.distance_ = batch.distance_;
  142. newInstance.instancingData_ = batch.instancingData_;
  143. for (unsigned i = 0; i < batch.numWorldTransforms_; ++i)
  144. {
  145. newInstance.worldTransform_ = &batch.worldTransform_[i];
  146. instances_.Push(newInstance);
  147. }
  148. }
  149. /// Pre-set the instance data. Buffer must be big enough to hold all data.
  150. void SetInstancingData(void* lockedData, unsigned stride, unsigned& freeIndex);
  151. /// Prepare and draw.
  152. void Draw(View* view, Camera* camera, bool allowDepthWrite) const;
  153. /// Instance data.
  154. PODVector<InstanceData> instances_;
  155. /// Instance stream start index, or M_MAX_UNSIGNED if transforms not pre-set.
  156. unsigned startIndex_;
  157. };
  158. /// Instanced draw call grouping key.
  159. struct BatchGroupKey
  160. {
  161. /// Construct undefined.
  162. BatchGroupKey() = default;
  163. /// Construct from a batch.
  164. explicit BatchGroupKey(const Batch& batch) :
  165. zone_(batch.zone_),
  166. lightQueue_(batch.lightQueue_),
  167. pass_(batch.pass_),
  168. material_(batch.material_),
  169. geometry_(batch.geometry_),
  170. renderOrder_(batch.renderOrder_)
  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. /// 8-bit render order modifier from material.
  184. unsigned char renderOrder_;
  185. /// Test for equality with another batch group key.
  186. bool operator ==(const BatchGroupKey& rhs) const
  187. {
  188. return zone_ == rhs.zone_ && lightQueue_ == rhs.lightQueue_ && pass_ == rhs.pass_ && material_ == rhs.material_ &&
  189. geometry_ == rhs.geometry_ && renderOrder_ == rhs.renderOrder_;
  190. }
  191. /// Test for inequality with another batch group key.
  192. bool operator !=(const BatchGroupKey& rhs) const
  193. {
  194. return zone_ != rhs.zone_ || lightQueue_ != rhs.lightQueue_ || pass_ != rhs.pass_ || material_ != rhs.material_ ||
  195. geometry_ != rhs.geometry_ || renderOrder_ != rhs.renderOrder_;
  196. }
  197. /// Return hash value.
  198. unsigned ToHash() const;
  199. };
  200. /// Queue that contains both instanced and non-instanced draw calls.
  201. struct BatchQueue
  202. {
  203. public:
  204. /// Clear for new frame by clearing all groups and batches.
  205. void Clear(int maxSortedInstances);
  206. /// Sort non-instanced draw calls back to front.
  207. void SortBackToFront();
  208. /// Sort instanced and non-instanced draw calls front to back.
  209. void SortFrontToBack();
  210. /// Sort batches front to back while also maintaining state sorting.
  211. void SortFrontToBack2Pass(PODVector<Batch*>& batches);
  212. /// Pre-set instance data of all groups. The vertex buffer must be big enough to hold all data.
  213. void SetInstancingData(void* lockedData, unsigned stride, unsigned& freeIndex);
  214. /// Draw.
  215. void Draw(View* view, Camera* camera, bool markToStencil, bool usingLightOptimization, bool allowDepthWrite) const;
  216. /// Return the combined amount of instances.
  217. unsigned GetNumInstances() const;
  218. /// Return whether the batch group is empty.
  219. bool IsEmpty() const { return batches_.Empty() && batchGroups_.Empty(); }
  220. /// Instanced draw calls.
  221. HashMap<BatchGroupKey, BatchGroup> batchGroups_;
  222. /// Shader remapping table for 2-pass state and distance sort.
  223. HashMap<unsigned, unsigned> shaderRemapping_;
  224. /// Material remapping table for 2-pass state and distance sort.
  225. HashMap<unsigned short, unsigned short> materialRemapping_;
  226. /// Geometry remapping table for 2-pass state and distance sort.
  227. HashMap<unsigned short, unsigned short> geometryRemapping_;
  228. /// Unsorted non-instanced draw calls.
  229. PODVector<Batch> batches_;
  230. /// Sorted non-instanced draw calls.
  231. PODVector<Batch*> sortedBatches_;
  232. /// Sorted instanced draw calls.
  233. PODVector<BatchGroup*> sortedBatchGroups_;
  234. /// Maximum sorted instances.
  235. unsigned maxSortedInstances_;
  236. /// Whether the pass command contains extra shader defines.
  237. bool hasExtraDefines_;
  238. /// Vertex shader extra defines.
  239. String vsExtraDefines_;
  240. /// Pixel shader extra defines.
  241. String psExtraDefines_;
  242. /// Hash for vertex shader extra defines.
  243. StringHash vsExtraDefinesHash_;
  244. /// Hash for pixel shader extra defines.
  245. StringHash psExtraDefinesHash_;
  246. };
  247. /// Queue for shadow map draw calls.
  248. struct ShadowBatchQueue
  249. {
  250. /// Shadow map camera.
  251. Camera* shadowCamera_{};
  252. /// Shadow map viewport.
  253. IntRect shadowViewport_;
  254. /// Shadow caster draw calls.
  255. BatchQueue shadowBatches_;
  256. /// Directional light cascade near split distance.
  257. float nearSplit_{};
  258. /// Directional light cascade far split distance.
  259. float farSplit_{};
  260. };
  261. /// Queue for light related draw calls.
  262. struct LightBatchQueue
  263. {
  264. /// Per-pixel light.
  265. Light* light_;
  266. /// Light negative flag.
  267. bool negative_;
  268. /// Shadow map depth texture.
  269. Texture2D* shadowMap_;
  270. /// Lit geometry draw calls, base (replace blend mode).
  271. BatchQueue litBaseBatches_;
  272. /// Lit geometry draw calls, non-base (additive).
  273. BatchQueue litBatches_;
  274. /// Shadow map split queues.
  275. Vector<ShadowBatchQueue> shadowSplits_;
  276. /// Per-vertex lights.
  277. PODVector<Light*> vertexLights_;
  278. /// Light volume draw calls.
  279. PODVector<Batch> volumeBatches_;
  280. };
  281. }