Batch.h 8.9 KB

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