Batch.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  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 "GraphicsDefs.h"
  25. #include "HashMap.h"
  26. #include "Map.h"
  27. #include "MathDefs.h"
  28. #include "Ptr.h"
  29. #include "Rect.h"
  30. #include "Vector4.h"
  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. /// Description of a 3D geometry draw call.
  46. struct Batch
  47. {
  48. /// Construct with defaults.
  49. Batch() :
  50. lightQueue_(0),
  51. shaderData_(0),
  52. shaderDataSize_(0),
  53. geometryType_(GEOM_STATIC),
  54. overrideView_(false),
  55. isBase_(false)
  56. {
  57. }
  58. /// Calculate state sorting key, which consists of base pass flag, light, pass and geometry.
  59. void CalculateSortKey();
  60. /// Prepare for rendering.
  61. void Prepare(Graphics* graphics, Renderer* renderer, bool setModelTransform = true) const;
  62. /// Prepare and draw.
  63. void Draw(Graphics* graphics, Renderer* renderer) const;
  64. /// State sorting key.
  65. unsigned long long sortKey_;
  66. /// Distance from camera.
  67. float distance_;
  68. /// Geometry.
  69. Geometry* geometry_;
  70. /// Model world transform.
  71. const Matrix3x4* worldTransform_;
  72. /// Camera.
  73. Camera* camera_;
  74. /// Zone.
  75. Zone* zone_;
  76. /// Light properties.
  77. LightBatchQueue* lightQueue_;
  78. /// Material.
  79. Material* material_;
  80. /// Material pass.
  81. Pass* pass_;
  82. /// Vertex shader.
  83. ShaderVariation* vertexShader_;
  84. /// Pixel shader.
  85. ShaderVariation* pixelShader_;
  86. /// Vertex shader data.
  87. const float* shaderData_;
  88. /// Vertex shader data size in floats.
  89. unsigned shaderDataSize_;
  90. /// Geometry type.
  91. GeometryType geometryType_;
  92. /// Override view transform flag.
  93. bool overrideView_;
  94. /// Base batch flag. This tells to draw the object fully without light optimizations.
  95. bool isBase_;
  96. };
  97. /// Data for one geometry instance.
  98. struct InstanceData
  99. {
  100. /// Construct undefined.
  101. InstanceData()
  102. {
  103. }
  104. /// Construct with transform and distance.
  105. InstanceData(const Matrix3x4* worldTransform, float distance) :
  106. worldTransform_(worldTransform),
  107. distance_(distance)
  108. {
  109. }
  110. /// World transform.
  111. const Matrix3x4* worldTransform_;
  112. /// Distance from camera.
  113. float distance_;
  114. };
  115. /// Instanced 3D geometry draw call.
  116. struct BatchGroup : public Batch
  117. {
  118. /// Construct with defaults.
  119. BatchGroup() :
  120. startIndex_(M_MAX_UNSIGNED)
  121. {
  122. }
  123. /// Construct from a batch.
  124. BatchGroup(const Batch& batch) :
  125. Batch(batch),
  126. startIndex_(M_MAX_UNSIGNED)
  127. {
  128. }
  129. /// Destruct.
  130. ~BatchGroup()
  131. {
  132. }
  133. /// Pre-set the instance transforms. Buffer must be big enough to hold all transforms.
  134. void SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex);
  135. /// Prepare and draw.
  136. void Draw(Graphics* graphics, Renderer* renderer) const;
  137. /// Instance data.
  138. PODVector<InstanceData> instances_;
  139. /// Instance stream start index, or M_MAX_UNSIGNED if transforms not pre-set.
  140. unsigned startIndex_;
  141. };
  142. /// Instanced draw call key.
  143. struct BatchGroupKey
  144. {
  145. /// Construct undefined.
  146. BatchGroupKey()
  147. {
  148. }
  149. /// Construct from a batch.
  150. BatchGroupKey(const Batch& batch) :
  151. zone_(batch.zone_),
  152. lightQueue_(batch.lightQueue_),
  153. pass_(batch.pass_),
  154. material_(batch.material_),
  155. geometry_(batch.geometry_)
  156. {
  157. }
  158. /// Zone.
  159. Zone* zone_;
  160. /// Light properties.
  161. LightBatchQueue* lightQueue_;
  162. /// Material pass.
  163. Pass* pass_;
  164. /// Material.
  165. Material* material_;
  166. /// Geometry.
  167. Geometry* geometry_;
  168. /// Test for equality with another batch group key.
  169. bool operator == (const BatchGroupKey& rhs) const { return zone_ == rhs.zone_ && lightQueue_ == rhs.lightQueue_ && pass_ == rhs.pass_ && material_ == rhs.material_ && geometry_ == rhs.geometry_; }
  170. /// Test for inequality with another batch group key.
  171. bool operator != (const BatchGroupKey& rhs) const { return zone_ != rhs.zone_ || lightQueue_ != rhs.lightQueue_ || pass_ != rhs.pass_ || material_ != rhs.material_ || geometry_ != rhs.geometry_; }
  172. /// Test if less than another batch group key.
  173. bool operator < (const BatchGroupKey& rhs) const
  174. {
  175. if (zone_ == rhs.zone_)
  176. {
  177. if (lightQueue_ == rhs.lightQueue_)
  178. {
  179. if (pass_ == rhs.pass_)
  180. {
  181. if (material_ == rhs.material_)
  182. return geometry_ < rhs.geometry_;
  183. else
  184. return material_ < rhs.material_;
  185. }
  186. else
  187. return pass_ < rhs.pass_;
  188. }
  189. else
  190. return lightQueue_ < rhs.lightQueue_;
  191. }
  192. else
  193. return zone_ < rhs.zone_;
  194. }
  195. /// Test if greater than another batch group key.
  196. bool operator > (const BatchGroupKey& rhs) const
  197. {
  198. if (zone_ == rhs.zone_)
  199. {
  200. if (lightQueue_ == rhs.lightQueue_)
  201. {
  202. if (pass_ == rhs.pass_)
  203. {
  204. if (material_ == rhs.material_)
  205. return geometry_ > rhs.geometry_;
  206. else
  207. return material_ > rhs.material_;
  208. }
  209. else
  210. return pass_ > rhs.pass_;
  211. }
  212. else
  213. return lightQueue_ > rhs.lightQueue_;
  214. }
  215. else
  216. return zone_ > rhs.zone_;
  217. }
  218. };
  219. /// Queue that contains both instanced and non-instanced draw calls.
  220. struct BatchQueue
  221. {
  222. public:
  223. /// Clear everything.
  224. void Clear();
  225. /// Add a batch.
  226. void AddBatch(const Batch& batch);
  227. /// Sort non-instanced draw calls back to front.
  228. void SortBackToFront();
  229. /// Sort instanced and non-instanced draw calls front to back.
  230. void SortFrontToBack();
  231. /// Pre-set instance transforms of all groups. The vertex buffer must be big enough to hold all transforms.
  232. void SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex);
  233. /// Return the combined amount of instances.
  234. unsigned GetNumInstances(Renderer* renderer) const;
  235. /// Return whether the batch group is empty.
  236. bool IsEmpty() const { return batches_.Empty() && baseBatchGroups_.Empty() && batchGroups_.Empty(); }
  237. /// Unsorted non-instanced draw calls.
  238. PODVector<Batch> batches_;
  239. /// Instanced draw calls with base flag.
  240. Map<BatchGroupKey, BatchGroup> baseBatchGroups_;
  241. /// Instanced draw calls.
  242. Map<BatchGroupKey, BatchGroup> batchGroups_;
  243. /// Sorted non-instanced draw calls with base flag.
  244. PODVector<Batch*> sortedBaseBatches_;
  245. /// Sorted non-instanced draw calls.
  246. PODVector<Batch*> sortedBatches_;
  247. /// Sorted instanced draw calls with base flag.
  248. PODVector<BatchGroup*> sortedBaseBatchGroups_;
  249. /// Sorted instanced draw calls.
  250. PODVector<BatchGroup*> sortedBatchGroups_;
  251. };
  252. /// Queue for shadow map draw calls
  253. struct ShadowBatchQueue
  254. {
  255. /// Shadow map camera.
  256. Camera* shadowCamera_;
  257. /// Shadow map viewport.
  258. IntRect shadowViewport_;
  259. /// Shadow caster draw calls.
  260. BatchQueue shadowBatches_;
  261. /// Directional light cascade near split distance.
  262. float nearSplit_;
  263. /// Directional light cascade far split distance.
  264. float farSplit_;
  265. };
  266. /// Queue for light related draw calls
  267. struct LightBatchQueue
  268. {
  269. /// Per-pixel light.
  270. Light* light_;
  271. /// Lit geometry draw calls.
  272. BatchQueue litBatches_;
  273. /// Shadow map depth texture.
  274. Texture2D* shadowMap_;
  275. /// Shadow map split queues.
  276. Vector<ShadowBatchQueue> shadowSplits_;
  277. /// Per-vertex lights.
  278. PODVector<Light*> vertexLights_;
  279. /// Light volume draw calls.
  280. PODVector<Batch> volumeBatches_;
  281. };