Batch.h 9.2 KB

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