Batch.h 10 KB

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