Batch.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Drawable.h"
  25. #include "MathDefs.h"
  26. #include "Ptr.h"
  27. #include "Rect.h"
  28. #include "Vector4.h"
  29. namespace Urho3D
  30. {
  31. class Camera;
  32. class Drawable;
  33. class Geometry;
  34. class Graphics;
  35. class Light;
  36. class Material;
  37. class Matrix3x4;
  38. class Pass;
  39. class Renderer;
  40. class ShaderVariation;
  41. class Texture2D;
  42. class VertexBuffer;
  43. class Zone;
  44. struct LightBatchQueue;
  45. /// Queued 3D geometry draw call.
  46. struct Batch
  47. {
  48. /// Construct with defaults.
  49. Batch() :
  50. lightQueue_(0),
  51. isBase_(false)
  52. {
  53. }
  54. /// Construct from a drawable's source batch.
  55. Batch(const SourceBatch& rhs) :
  56. distance_(rhs.distance_),
  57. geometry_(rhs.geometry_),
  58. material_(rhs.material_),
  59. worldTransform_(rhs.worldTransform_),
  60. lightQueue_(0),
  61. shaderData_(rhs.shaderData_),
  62. shaderDataSize_(rhs.shaderDataSize_),
  63. geometryType_(rhs.geometryType_),
  64. overrideView_(rhs.overrideView_),
  65. isBase_(false)
  66. {
  67. }
  68. /// Calculate state sorting key, which consists of base pass flag, light, pass and geometry.
  69. void CalculateSortKey();
  70. /// Prepare for rendering.
  71. void Prepare(Graphics* graphics, Renderer* renderer, bool setModelTransform = true) const;
  72. /// Prepare and draw.
  73. void Draw(Graphics* graphics, Renderer* renderer) const;
  74. /// State sorting key.
  75. unsigned long long sortKey_;
  76. /// Distance from camera.
  77. float distance_;
  78. /// Geometry.
  79. Geometry* geometry_;
  80. /// Material.
  81. Material* material_;
  82. /// %Object's world transform.
  83. const Matrix3x4* worldTransform_;
  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. /// Vertex shader data.
  97. const float* shaderData_;
  98. /// Vertex shader data size in floats.
  99. unsigned shaderDataSize_;
  100. /// %Geometry type.
  101. GeometryType geometryType_;
  102. /// Override view transform flag.
  103. bool overrideView_;
  104. /// Base batch flag. This tells to draw the object fully without light optimizations.
  105. bool isBase_;
  106. /// 8-bit light mask for stencil marking in deferred rendering.
  107. unsigned char lightMask_;
  108. };
  109. /// Data for one geometry instance.
  110. struct InstanceData
  111. {
  112. /// Construct undefined.
  113. InstanceData()
  114. {
  115. }
  116. /// Construct with transform and distance.
  117. InstanceData(const Matrix3x4* worldTransform, float distance) :
  118. worldTransform_(worldTransform),
  119. distance_(distance)
  120. {
  121. }
  122. /// World transform.
  123. const Matrix3x4* worldTransform_;
  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. /// Pre-set the instance transforms. Buffer must be big enough to hold all transforms.
  146. void SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex);
  147. /// Prepare and draw.
  148. void Draw(Graphics* graphics, Renderer* renderer) const;
  149. /// Instance data.
  150. PODVector<InstanceData> instances_;
  151. /// Instance stream start index, or M_MAX_UNSIGNED if transforms not pre-set.
  152. unsigned startIndex_;
  153. };
  154. /// Instanced draw call grouping key.
  155. struct BatchGroupKey
  156. {
  157. /// Construct undefined.
  158. BatchGroupKey()
  159. {
  160. }
  161. /// Construct from a batch.
  162. BatchGroupKey(const Batch& batch) :
  163. zone_(batch.zone_),
  164. lightQueue_(batch.lightQueue_),
  165. pass_(batch.pass_),
  166. material_(batch.material_),
  167. geometry_(batch.geometry_)
  168. {
  169. }
  170. /// Zone.
  171. Zone* zone_;
  172. /// Light properties.
  173. LightBatchQueue* lightQueue_;
  174. /// Material pass.
  175. Pass* pass_;
  176. /// Material.
  177. Material* material_;
  178. /// Geometry.
  179. Geometry* geometry_;
  180. /// Test for equality 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. /// Test for inequality with another batch group key.
  183. bool operator != (const BatchGroupKey& rhs) const { return zone_ != rhs.zone_ || lightQueue_ != rhs.lightQueue_ || pass_ != rhs.pass_ || material_ != rhs.material_ || geometry_ != rhs.geometry_; }
  184. /// Return hash value.
  185. unsigned ToHash() const;
  186. };
  187. /// Queue that contains both instanced and non-instanced draw calls.
  188. struct BatchQueue
  189. {
  190. public:
  191. /// Clear for new frame by clearing all groups and batches.
  192. void Clear(int maxSortedInstances);
  193. /// Sort non-instanced draw calls back to front.
  194. void SortBackToFront();
  195. /// Sort instanced and non-instanced draw calls front to back.
  196. void SortFrontToBack();
  197. /// Sort batches front to back while also maintaining state sorting.
  198. void SortFrontToBack2Pass(PODVector<Batch*>& batches);
  199. /// Pre-set instance transforms of all groups. The vertex buffer must be big enough to hold all transforms.
  200. void SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex);
  201. /// Draw.
  202. void Draw(Graphics* graphics, Renderer* renderer, bool useScissor = false, bool markToStencil = false) const;
  203. /// Draw with forward light optimizations.
  204. void Draw(Light* light, Graphics* graphics, Renderer* renderer) const;
  205. /// Return the combined amount of instances.
  206. unsigned GetNumInstances(Renderer* renderer) const;
  207. /// Return whether the batch group is empty.
  208. bool IsEmpty() const { return batches_.Empty() && baseBatchGroups_.Empty() && batchGroups_.Empty(); }
  209. /// Instanced draw calls with base flag.
  210. HashMap<BatchGroupKey, BatchGroup> baseBatchGroups_;
  211. /// Instanced draw calls.
  212. HashMap<BatchGroupKey, BatchGroup> batchGroups_;
  213. /// Shader remapping table for 2-pass state and distance sort.
  214. HashMap<unsigned, unsigned> shaderRemapping_;
  215. /// Material remapping table for 2-pass state and distance sort.
  216. HashMap<unsigned short, unsigned short> materialRemapping_;
  217. /// Geometry remapping table for 2-pass state and distance sort.
  218. HashMap<unsigned short, unsigned short> geometryRemapping_;
  219. /// Unsorted non-instanced draw calls.
  220. PODVector<Batch> batches_;
  221. /// Sorted non-instanced draw calls with base flag.
  222. PODVector<Batch*> sortedBaseBatches_;
  223. /// Sorted non-instanced draw calls.
  224. PODVector<Batch*> sortedBatches_;
  225. /// Sorted instanced draw calls with base flag.
  226. PODVector<BatchGroup*> sortedBaseBatchGroups_;
  227. /// Sorted instanced draw calls.
  228. PODVector<BatchGroup*> sortedBatchGroups_;
  229. /// Maximum sorted instances.
  230. unsigned maxSortedInstances_;
  231. };
  232. /// Queue for shadow map draw calls
  233. struct ShadowBatchQueue
  234. {
  235. /// Shadow map camera.
  236. Camera* shadowCamera_;
  237. /// Shadow map viewport.
  238. IntRect shadowViewport_;
  239. /// Shadow caster draw calls.
  240. BatchQueue shadowBatches_;
  241. /// Directional light cascade near split distance.
  242. float nearSplit_;
  243. /// Directional light cascade far split distance.
  244. float farSplit_;
  245. };
  246. /// Queue for light related draw calls.
  247. struct LightBatchQueue
  248. {
  249. /// Per-pixel light.
  250. Light* light_;
  251. /// Shadow map depth texture.
  252. Texture2D* shadowMap_;
  253. /// Lit geometry draw calls.
  254. BatchQueue litBatches_;
  255. /// Shadow map split queues.
  256. Vector<ShadowBatchQueue> shadowSplits_;
  257. /// Per-vertex lights.
  258. PODVector<Light*> vertexLights_;
  259. /// Light volume draw calls.
  260. PODVector<Batch> volumeBatches_;
  261. };
  262. }