Batch.h 9.9 KB

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