2
0

Batch.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. #include "Precompiled.h"
  24. #include "Camera.h"
  25. #include "Geometry.h"
  26. #include "Graphics.h"
  27. #include "GraphicsImpl.h"
  28. #include "Light.h"
  29. #include "Material.h"
  30. #include "Renderer.h"
  31. #include "Profiler.h"
  32. #include "ShaderVariation.h"
  33. #include "Sort.h"
  34. #include "Technique.h"
  35. #include "Texture2D.h"
  36. #include "VertexBuffer.h"
  37. #include "DebugNew.h"
  38. inline bool CompareBatchesFrontToBack(Batch* lhs, Batch* rhs)
  39. {
  40. if (lhs->sortKey_ == rhs->sortKey_)
  41. return lhs->distance_ < rhs->distance_;
  42. else
  43. return lhs->sortKey_ > rhs->sortKey_;
  44. }
  45. inline bool CompareBatchesBackToFront(Batch* lhs, Batch* rhs)
  46. {
  47. if (lhs->distance_ == rhs->distance_)
  48. return lhs->sortKey_ > rhs->sortKey_;
  49. else
  50. return lhs->distance_ > rhs->distance_;
  51. }
  52. inline bool CompareInstancesFrontToBack(const InstanceData& lhs, const InstanceData& rhs)
  53. {
  54. return lhs.distance_ < rhs.distance_;
  55. }
  56. inline bool CompareBatchGroupsFrontToBack(BatchGroup* lhs, BatchGroup* rhs)
  57. {
  58. return lhs->instances_[0].distance_ < rhs->instances_[0].distance_;
  59. }
  60. void Batch::CalculateSortKey()
  61. {
  62. unsigned lightQueue = (*((unsigned*)&lightQueue_) / sizeof(LightBatchQueue)) & 0x7fff;
  63. unsigned pass = (*((unsigned*)&pass_) / sizeof(Pass)) & 0xffff;
  64. unsigned material = (*((unsigned*)&material_) / sizeof(Material)) & 0xffff;
  65. unsigned geometry = (*((unsigned*)&geometry_) / sizeof(Geometry)) & 0xffff;
  66. if (hasPriority_)
  67. lightQueue |= 0x8000;
  68. sortKey_ = (((unsigned long long)lightQueue) << 48) | (((unsigned long long)pass) << 32) |
  69. (((unsigned long long)material) << 16) | geometry;
  70. }
  71. void Batch::Prepare(Graphics* graphics, Renderer* renderer, const HashMap<StringHash, Vector4>& shaderParameters, bool setModelTransform) const
  72. {
  73. if (!vertexShader_ || !pixelShader_)
  74. return;
  75. // Set pass / material-specific renderstates
  76. if (pass_ && material_)
  77. {
  78. if (pass_->GetAlphaTest())
  79. graphics->SetAlphaTest(true, CMP_GREATEREQUAL, 0.5f);
  80. else
  81. graphics->SetAlphaTest(false);
  82. graphics->SetBlendMode(pass_->GetBlendMode());
  83. graphics->SetCullMode(pass_->GetType() != PASS_SHADOW ? material_->GetCullMode() : material_->GetShadowCullMode());
  84. graphics->SetDepthTest(pass_->GetDepthTestMode());
  85. graphics->SetDepthWrite(pass_->GetDepthWrite());
  86. }
  87. // Set shaders
  88. graphics->SetShaders(vertexShader_, pixelShader_);
  89. // Set global shader parameters
  90. for (HashMap<StringHash, Vector4>::ConstIterator i = shaderParameters.Begin(); i != shaderParameters.End(); ++i)
  91. {
  92. if (graphics->NeedParameterUpdate(i->first_, &shaderParameters))
  93. graphics->SetShaderParameter(i->first_, i->second_);
  94. }
  95. // Set viewport and camera shader parameters
  96. if (graphics->NeedParameterUpdate(VSP_CAMERAPOS, camera_))
  97. graphics->SetShaderParameter(VSP_CAMERAPOS, camera_->GetWorldPosition());
  98. if (graphics->NeedParameterUpdate(VSP_CAMERAROT, camera_))
  99. graphics->SetShaderParameter(VSP_CAMERAROT, camera_->GetWorldTransform().RotationMatrix());
  100. if (overrideView_)
  101. {
  102. if (graphics->NeedParameterUpdate(VSP_VIEWPROJ, ((unsigned char*)camera_) + 4))
  103. graphics->SetShaderParameter(VSP_VIEWPROJ, camera_->GetProjection());
  104. }
  105. else
  106. {
  107. if (graphics->NeedParameterUpdate(VSP_VIEWPROJ, camera_))
  108. graphics->SetShaderParameter(VSP_VIEWPROJ, camera_->GetProjection() *
  109. camera_->GetInverseWorldTransform());
  110. }
  111. if (graphics->NeedParameterUpdate(VSP_VIEWRIGHTVECTOR, camera_))
  112. graphics->SetShaderParameter(VSP_VIEWRIGHTVECTOR, camera_->GetRightVector());
  113. if (graphics->NeedParameterUpdate(VSP_VIEWUPVECTOR, camera_))
  114. graphics->SetShaderParameter(VSP_VIEWUPVECTOR, camera_->GetUpVector());
  115. // Set model transform
  116. if (setModelTransform && graphics->NeedParameterUpdate(VSP_MODEL, worldTransform_))
  117. graphics->SetShaderParameter(VSP_MODEL, *worldTransform_);
  118. // Set skinning transforms
  119. if (shaderData_ && shaderDataSize_)
  120. {
  121. if (graphics->NeedParameterUpdate(VSP_SKINMATRICES, shaderData_))
  122. graphics->SetShaderParameter(VSP_SKINMATRICES, shaderData_, shaderDataSize_);
  123. }
  124. // Set light-related shader parameters
  125. Light* light = 0;
  126. Texture2D* shadowMap = 0;
  127. if (lightQueue_)
  128. {
  129. light = lightQueue_->light_;
  130. shadowMap = lightQueue_->shadowMap_;
  131. if (graphics->NeedParameterUpdate(VSP_LIGHTATTEN, light))
  132. {
  133. Vector4 lightAtten(1.0f / Max(light->GetRange(), M_EPSILON), 0.0f, 0.0f, 0.0f);
  134. graphics->SetShaderParameter(VSP_LIGHTATTEN, lightAtten);
  135. }
  136. if (graphics->NeedParameterUpdate(VSP_LIGHTDIR, light))
  137. graphics->SetShaderParameter(VSP_LIGHTDIR, light->GetWorldRotation() * Vector3::BACK);
  138. if (graphics->NeedParameterUpdate(VSP_LIGHTPOS, light))
  139. graphics->SetShaderParameter(VSP_LIGHTPOS, light->GetWorldPosition() - camera_->GetWorldPosition());
  140. if (graphics->NeedParameterUpdate(VSP_LIGHTVECROT, light))
  141. {
  142. Matrix3x4 lightVecRot(Vector3::ZERO, light->GetWorldRotation(), Vector3::UNITY);
  143. graphics->SetShaderParameter(VSP_LIGHTVECROT, lightVecRot);
  144. }
  145. if (graphics->NeedParameterUpdate(VSP_SPOTPROJ, light))
  146. {
  147. Matrix3x4 spotView(light->GetWorldPosition(), light->GetWorldRotation(), 1.0f);
  148. Matrix4 spotProj(Matrix4::ZERO);
  149. Matrix4 texAdjust(Matrix4::IDENTITY);
  150. // Make the projected light slightly smaller than the shadow map to prevent light spill
  151. float h = 1.005f / tanf(light->GetFov() * M_DEGTORAD * 0.5f);
  152. float w = h / light->GetAspectRatio();
  153. spotProj.m00_ = w;
  154. spotProj.m11_ = h;
  155. spotProj.m22_ = 1.0f / Max(light->GetRange(), M_EPSILON);
  156. spotProj.m32_ = 1.0f;
  157. #ifdef USE_OPENGL
  158. texAdjust.SetTranslation(Vector3(0.5f, 0.5f, 0.5f));
  159. texAdjust.SetScale(Vector3(0.5f, -0.5f, 0.5f));
  160. #else
  161. texAdjust.SetTranslation(Vector3(0.5f, 0.5f, 0.0f));
  162. texAdjust.SetScale(Vector3(0.5f, -0.5f, 1.0f));
  163. #endif
  164. graphics->SetShaderParameter(VSP_SPOTPROJ, texAdjust * spotProj * spotView.Inverse());
  165. }
  166. if (graphics->NeedParameterUpdate(PSP_LIGHTCOLOR, light))
  167. {
  168. float fade = 1.0f;
  169. float fadeEnd = light->GetDrawDistance();
  170. float fadeStart = light->GetFadeDistance();
  171. // Do fade calculation for light if both fade & draw distance defined
  172. if (light->GetLightType() != LIGHT_DIRECTIONAL && fadeEnd > 0.0f && fadeStart > 0.0f && fadeStart < fadeEnd)
  173. fade = Min(1.0f - (light->GetDistance() - fadeStart) / (fadeEnd - fadeStart), 1.0f);
  174. graphics->SetShaderParameter(PSP_LIGHTCOLOR, Vector4(light->GetColor().RGBValues(),
  175. light->GetSpecularIntensity()) * fade);
  176. }
  177. // Set shadow mapping shader parameters
  178. if (shadowMap)
  179. {
  180. if (graphics->NeedParameterUpdate(VSP_SHADOWPROJ, light))
  181. {
  182. Matrix4 shadowMatrices[MAX_CASCADE_SPLITS];
  183. unsigned numSplits = 1;
  184. if (light->GetLightType() == LIGHT_DIRECTIONAL)
  185. numSplits = lightQueue_->shadowSplits_.Size();
  186. for (unsigned i = 0; i < numSplits; ++i)
  187. {
  188. Camera* shadowCamera = lightQueue_->shadowSplits_[i].shadowCamera_;
  189. const IntRect& viewport = lightQueue_->shadowSplits_[i].shadowViewport_;
  190. Matrix3x4 shadowView(shadowCamera->GetInverseWorldTransform());
  191. Matrix4 shadowProj(shadowCamera->GetProjection());
  192. Matrix4 texAdjust(Matrix4::IDENTITY);
  193. float width = (float)shadowMap->GetWidth();
  194. float height = (float)shadowMap->GetHeight();
  195. Vector2 offset(
  196. (float)viewport.left_ / width,
  197. (float)viewport.top_ / height
  198. );
  199. Vector2 scale(
  200. 0.5f * (float)(viewport.right_ - viewport.left_) / width,
  201. 0.5f * (float)(viewport.bottom_ - viewport.top_) / height
  202. );
  203. #ifdef USE_OPENGL
  204. offset.x_ += scale.x_;
  205. offset.y_ += scale.y_;
  206. offset.y_ = 1.0f - offset.y_;
  207. // If using 4 shadow samples, offset the position diagonally by half pixel
  208. if (renderer->GetShadowQuality() & SHADOWQUALITY_HIGH_16BIT)
  209. {
  210. offset.x_ -= 0.5f / width;
  211. offset.y_ -= 0.5f / height;
  212. }
  213. texAdjust.SetTranslation(Vector3(offset.x_, offset.y_, 0.5f));
  214. texAdjust.SetScale(Vector3(scale.x_, scale.y_, 0.5f));
  215. #else
  216. offset.x_ += scale.x_ + 0.5f / width;
  217. offset.y_ += scale.y_ + 0.5f / height;
  218. if (renderer->GetShadowQuality() & SHADOWQUALITY_HIGH_16BIT)
  219. {
  220. offset.x_ -= 0.5f / width;
  221. offset.y_ -= 0.5f / height;
  222. }
  223. // If using 2 shadow samples (fallback mode), offset the position horizontally only
  224. if (graphics->GetFallback())
  225. offset.x_ -= 0.5f / width;
  226. scale.y_ = -scale.y_;
  227. texAdjust.SetTranslation(Vector3(offset.x_, offset.y_, 0.0f));
  228. texAdjust.SetScale(Vector3(scale.x_, scale.y_, 1.0f));
  229. #endif
  230. shadowMatrices[i] = texAdjust * shadowProj * shadowView;
  231. }
  232. graphics->SetShaderParameter(VSP_SHADOWPROJ, shadowMatrices[0].GetData(), 16 * numSplits);
  233. }
  234. if (graphics->NeedParameterUpdate(PSP_SAMPLEOFFSETS, shadowMap))
  235. {
  236. float addX = 1.0f / (float)shadowMap->GetWidth();
  237. float addY = 1.0f / (float)shadowMap->GetHeight();
  238. graphics->SetShaderParameter(PSP_SAMPLEOFFSETS, Vector4(addX, addY, 0.0f, 0.0f));
  239. }
  240. if (graphics->NeedParameterUpdate(PSP_SHADOWCUBEADJUST, light))
  241. {
  242. unsigned faceWidth = shadowMap->GetWidth() / 2;
  243. unsigned faceHeight = shadowMap->GetHeight() / 3;
  244. float width = (float)shadowMap->GetWidth();
  245. float height = (float)shadowMap->GetHeight();
  246. #ifdef USE_OPENGL
  247. float mulX = (float)(faceWidth - 3) / width;
  248. float mulY = (float)(faceHeight - 3) / height;
  249. float addX = 1.5f / width;
  250. float addY = 1.5f / height;
  251. #else
  252. float mulX = (float)(faceWidth - 4) / width;
  253. float mulY = (float)(faceHeight - 4) / height;
  254. float addX = 2.5f / width;
  255. float addY = 2.5f / height;
  256. #endif
  257. // If using 4 shadow samples, offset the position diagonally by half pixel
  258. if (renderer->GetShadowQuality() & SHADOWQUALITY_HIGH_16BIT)
  259. {
  260. addX -= 0.5f / width;
  261. addY -= 0.5f / height;
  262. }
  263. // If using 2 shadow samples (fallback mode), offset the position horizontally only
  264. if (graphics->GetFallback())
  265. addX -= 0.5f / width;
  266. graphics->SetShaderParameter(PSP_SHADOWCUBEADJUST, Vector4(mulX, mulY, addX, addY));
  267. }
  268. if (graphics->NeedParameterUpdate(PSP_SHADOWCUBEPROJ, light))
  269. {
  270. // Note: we use the shadow camera of the first cube face. All are assumed to use the same projection
  271. Camera* shadowCamera = lightQueue_->shadowSplits_[0].shadowCamera_;
  272. float nearClip = shadowCamera->GetNearClip();
  273. float farClip = shadowCamera->GetFarClip();
  274. float q = farClip / (farClip - nearClip);
  275. float r = -q * nearClip;
  276. graphics->SetShaderParameter(PSP_SHADOWCUBEPROJ, Vector4(q, r, 0.0f, 0.0f));
  277. }
  278. if (graphics->NeedParameterUpdate(PSP_SHADOWFADE, light))
  279. {
  280. const CascadeParameters& parameters = light->GetShadowCascade();
  281. float farClip = camera_->GetFarClip();
  282. float shadowRange = parameters.GetShadowRange();
  283. float fadeStart = parameters.fadeStart_ * shadowRange / farClip;
  284. float fadeEnd = shadowRange / farClip;
  285. float fadeRange = fadeEnd - fadeStart;
  286. graphics->SetShaderParameter(PSP_SHADOWFADE, Vector4(fadeStart, 1.0f / fadeRange, 0.0f, 0.0f));
  287. }
  288. if (graphics->NeedParameterUpdate(PSP_SHADOWINTENSITY, light))
  289. {
  290. float intensity = light->GetShadowIntensity();
  291. float fadeStart = light->GetShadowFadeDistance();
  292. float fadeEnd = light->GetShadowDistance();
  293. if (fadeStart > 0.0f && fadeEnd > 0.0f && fadeEnd > fadeStart)
  294. intensity = Lerp(intensity, 1.0f, Clamp((light->GetDistance() - fadeStart) / (fadeEnd - fadeStart), 0.0f, 1.0f));
  295. float pcfValues = (1.0f - intensity);
  296. float samples = 4.0f;
  297. float fallbackBias = 0.0f;
  298. // Fallback mode requires manual depth biasing. We do not do proper slope scale biasing,
  299. // instead just fudge the bias values together
  300. if (graphics->GetFallback())
  301. {
  302. samples = 2.0f;
  303. fallbackBias = graphics->GetDepthConstantBias() + 2.0f * graphics->GetDepthSlopeScaledBias() *
  304. graphics->GetDepthConstantBias();
  305. }
  306. graphics->SetShaderParameter(PSP_SHADOWINTENSITY, Vector4(pcfValues, pcfValues / samples, intensity, fallbackBias));
  307. }
  308. if (graphics->NeedParameterUpdate(PSP_SHADOWSPLITS, light))
  309. {
  310. Vector4 lightSplits(M_LARGE_VALUE, M_LARGE_VALUE, M_LARGE_VALUE, M_LARGE_VALUE);
  311. if (lightQueue_->shadowSplits_.Size() > 1)
  312. lightSplits.x_ = lightQueue_->shadowSplits_[0].farSplit_ / camera_->GetFarClip();
  313. if (lightQueue_->shadowSplits_.Size() > 2)
  314. lightSplits.y_ = lightQueue_->shadowSplits_[1].farSplit_ / camera_->GetFarClip();
  315. if (lightQueue_->shadowSplits_.Size() > 3)
  316. lightSplits.z_ = lightQueue_->shadowSplits_[2].farSplit_ / camera_->GetFarClip();
  317. graphics->SetShaderParameter(PSP_SHADOWSPLITS, lightSplits);
  318. }
  319. }
  320. }
  321. // Set material-specific shader parameters and textures
  322. if (material_)
  323. {
  324. const HashMap<StringHash, MaterialShaderParameter>& parameters = material_->GetShaderParameters();
  325. for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = parameters.Begin(); i != parameters.End(); ++i)
  326. {
  327. if (graphics->NeedParameterUpdate(i->first_, material_))
  328. graphics->SetShaderParameter(i->first_, i->second_.value_);
  329. }
  330. const Vector<SharedPtr<Texture> >& textures = material_->GetTextures();
  331. if (graphics->NeedTextureUnit(TU_DIFFUSE))
  332. graphics->SetTexture(TU_DIFFUSE, textures[TU_DIFFUSE]);
  333. if (graphics->NeedTextureUnit(TU_NORMAL))
  334. graphics->SetTexture(TU_NORMAL, textures[TU_NORMAL]);
  335. if (graphics->NeedTextureUnit(TU_DETAIL))
  336. graphics->SetTexture(TU_DETAIL, textures[TU_DETAIL]);
  337. if (graphics->NeedTextureUnit(TU_ENVIRONMENT))
  338. graphics->SetTexture(TU_ENVIRONMENT, textures[TU_ENVIRONMENT]);
  339. }
  340. // Set light-related textures
  341. if (light)
  342. {
  343. if (shadowMap && graphics->NeedTextureUnit(TU_SHADOWMAP))
  344. graphics->SetTexture(TU_SHADOWMAP, shadowMap);
  345. if (graphics->NeedTextureUnit(TU_LIGHTRAMP))
  346. {
  347. Texture* rampTexture = light->GetRampTexture();
  348. if (!rampTexture)
  349. rampTexture = renderer->GetDefaultLightRamp();
  350. graphics->SetTexture(TU_LIGHTRAMP, rampTexture);
  351. }
  352. if (graphics->NeedTextureUnit(TU_LIGHTSHAPE))
  353. {
  354. Texture* shapeTexture = light->GetShapeTexture();
  355. if (!shapeTexture && light->GetLightType() == LIGHT_SPOT)
  356. shapeTexture = renderer->GetDefaultLightSpot();
  357. graphics->SetTexture(TU_LIGHTSHAPE, shapeTexture);
  358. }
  359. }
  360. }
  361. void Batch::Draw(Graphics* graphics, Renderer* renderer, const HashMap<StringHash, Vector4>& shaderParameters) const
  362. {
  363. Prepare(graphics, renderer, shaderParameters);
  364. geometry_->Draw(graphics);
  365. }
  366. void BatchGroup::SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex)
  367. {
  368. // Do not use up buffer space if not going to draw as instanced
  369. unsigned minGroupSize = renderer->GetMinInstanceGroupSize();
  370. unsigned maxIndexCount = renderer->GetMaxInstanceTriangles() * 3;
  371. if (instances_.Size() < minGroupSize || geometry_->GetIndexCount() > maxIndexCount)
  372. return;
  373. startIndex_ = freeIndex;
  374. Matrix3x4* dest = (Matrix3x4*)lockedData;
  375. dest += freeIndex;
  376. for (unsigned i = 0; i < instances_.Size(); ++i)
  377. *dest++ = *instances_[i].worldTransform_;
  378. freeIndex += instances_.Size();
  379. }
  380. void BatchGroup::Draw(Graphics* graphics, Renderer* renderer, const HashMap<StringHash, Vector4>& shaderParameters) const
  381. {
  382. if (!instances_.Size())
  383. return;
  384. // Construct a temporary batch for rendering
  385. Batch batch;
  386. batch.geometry_ = geometry_;
  387. batch.material_ = material_;
  388. batch.pass_ = pass_;
  389. batch.vertexShader_ = vertexShader_;
  390. batch.pixelShader_ = pixelShader_;
  391. batch.camera_ = camera_;
  392. batch.lightQueue_ = lightQueue_;
  393. batch.vertexShaderIndex_ = vertexShaderIndex_;
  394. unsigned minGroupSize = renderer->GetMinInstanceGroupSize();
  395. unsigned maxIndexCount = renderer->GetMaxInstanceTriangles() * 3;
  396. // Draw as individual instances if below minimum size, or if instancing not supported
  397. VertexBuffer* instanceBuffer = renderer->GetInstancingBuffer();
  398. if (!instanceBuffer || instances_.Size() < minGroupSize || geometry_->GetIndexCount() > maxIndexCount)
  399. {
  400. batch.Prepare(graphics, renderer, shaderParameters, false);
  401. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  402. graphics->SetVertexBuffers(geometry_->GetVertexBuffers(), geometry_->GetVertexElementMasks());
  403. for (unsigned i = 0; i < instances_.Size(); ++i)
  404. {
  405. graphics->SetShaderParameter(VSP_MODEL, *instances_[i].worldTransform_);
  406. graphics->Draw(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  407. geometry_->GetVertexStart(), geometry_->GetVertexCount());
  408. }
  409. graphics->ClearTransformSources();
  410. }
  411. else
  412. {
  413. // Switch to the instancing vertex shader
  414. // The indexing is different in the forward lit passes
  415. Vector<SharedPtr<ShaderVariation> >& vertexShaders = pass_->GetVertexShaders();
  416. Vector<SharedPtr<ShaderVariation> >& pixelShaders = pass_->GetPixelShaders();
  417. PassType type = pass_->GetType();
  418. if (type == PASS_LIGHT || type == PASS_LITBASE)
  419. batch.vertexShader_ = vertexShaders[vertexShaderIndex_ + GEOM_INSTANCED * MAX_LIGHT_VS_VARIATIONS];
  420. else
  421. batch.vertexShader_ = vertexShaders[vertexShaderIndex_ + GEOM_INSTANCED];
  422. batch.Prepare(graphics, renderer, shaderParameters, false);
  423. // Get the geometry vertex buffers, then add the instancing stream buffer
  424. // Hack: use a const_cast to avoid dynamic allocation of new temp vectors
  425. Vector<SharedPtr<VertexBuffer> >& vertexBuffers = const_cast<Vector<SharedPtr<VertexBuffer> >&>
  426. (geometry_->GetVertexBuffers());
  427. PODVector<unsigned>& elementMasks = const_cast<PODVector<unsigned>&>(geometry_->GetVertexElementMasks());
  428. vertexBuffers.Push(SharedPtr<VertexBuffer>(instanceBuffer));
  429. elementMasks.Push(instanceBuffer->GetElementMask());
  430. // No stream offset support, instancing buffer not pre-filled with transforms: have to lock and fill now
  431. if (startIndex_ == M_MAX_UNSIGNED)
  432. {
  433. unsigned startIndex = 0;
  434. while (startIndex < instances_.Size())
  435. {
  436. unsigned instances = instances_.Size() - startIndex;
  437. if (instances > instanceBuffer->GetVertexCount())
  438. instances = instanceBuffer->GetVertexCount();
  439. // Lock the instance stream buffer and copy the transforms
  440. void* data = instanceBuffer->Lock(0, instances, LOCK_DISCARD);
  441. if (!data)
  442. {
  443. // Remember to remove the instancing buffer and element mask
  444. vertexBuffers.Pop();
  445. elementMasks.Pop();
  446. return;
  447. }
  448. Matrix3x4* dest = (Matrix3x4*)data;
  449. for (unsigned i = 0; i < instances; ++i)
  450. dest[i] = *instances_[i + startIndex].worldTransform_;
  451. instanceBuffer->Unlock();
  452. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  453. graphics->SetVertexBuffers(vertexBuffers, elementMasks);
  454. graphics->DrawInstanced(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  455. geometry_->GetVertexStart(), geometry_->GetVertexCount(), instances);
  456. startIndex += instances;
  457. }
  458. }
  459. // Stream offset supported, and instancing buffer has been already filled, so just draw
  460. else
  461. {
  462. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  463. graphics->SetVertexBuffers(vertexBuffers, elementMasks, startIndex_);
  464. graphics->DrawInstanced(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  465. geometry_->GetVertexStart(), geometry_->GetVertexCount(), instances_.Size());
  466. }
  467. // Remove the instancing buffer & element mask now
  468. vertexBuffers.Pop();
  469. elementMasks.Pop();
  470. }
  471. }
  472. void BatchQueue::Clear()
  473. {
  474. batches_.Clear();
  475. sortedPriorityBatches_.Clear();
  476. sortedBatches_.Clear();
  477. priorityBatchGroups_.Clear();
  478. batchGroups_.Clear();
  479. }
  480. void BatchQueue::AddBatch(const Batch& batch, bool noInstancing)
  481. {
  482. // If batch is something else than static, has custom view, or has per-instance shader data defined, can not instance
  483. if (noInstancing || batch.geometryType_ != GEOM_STATIC || batch.overrideView_ || batch.shaderData_)
  484. batches_.Push(batch);
  485. else
  486. {
  487. BatchGroupKey key;
  488. key.lightQueue_ = batch.lightQueue_;
  489. key.pass_ = batch.pass_;
  490. key.material_ = batch.material_;
  491. key.geometry_ = batch.geometry_;
  492. Map<BatchGroupKey, BatchGroup>* groups = batch.hasPriority_ ? &priorityBatchGroups_ : &batchGroups_;
  493. Map<BatchGroupKey, BatchGroup>::Iterator i = groups->Find(key);
  494. if (i == groups->End())
  495. {
  496. // Create new group
  497. BatchGroup newGroup;
  498. newGroup.geometry_ = batch.geometry_;
  499. newGroup.material_ = batch.material_;
  500. newGroup.pass_ = batch.pass_;
  501. newGroup.vertexShader_ = batch.vertexShader_;
  502. newGroup.pixelShader_ = batch.pixelShader_;
  503. newGroup.camera_ = batch.camera_;
  504. newGroup.lightQueue_ = batch.lightQueue_;
  505. newGroup.vertexShaderIndex_ = batch.vertexShaderIndex_;
  506. newGroup.instances_.Push(InstanceData(batch.worldTransform_, batch.distance_));
  507. groups->Insert(MakePair(key, newGroup));
  508. }
  509. else
  510. i->second_.instances_.Push(InstanceData(batch.worldTransform_, batch.distance_));
  511. }
  512. }
  513. void BatchQueue::SortBackToFront()
  514. {
  515. sortedPriorityBatches_.Clear();
  516. sortedBatches_.Resize(batches_.Size());
  517. for (unsigned i = 0; i < batches_.Size(); ++i)
  518. sortedBatches_[i] = &batches_[i];
  519. Sort(sortedBatches_.Begin(), sortedBatches_.End(), CompareBatchesBackToFront);
  520. // Do not actually sort batch groups, just list them
  521. sortedPriorityBatchGroups_.Resize(priorityBatchGroups_.Size());
  522. sortedBatchGroups_.Resize(batchGroups_.Size());
  523. unsigned index = 0;
  524. for (Map<BatchGroupKey, BatchGroup>::Iterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  525. sortedPriorityBatchGroups_[index++] = &i->second_;
  526. index = 0;
  527. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  528. sortedBatchGroups_[index++] = &i->second_;
  529. }
  530. void BatchQueue::SortFrontToBack()
  531. {
  532. sortedPriorityBatches_.Clear();
  533. sortedBatches_.Clear();
  534. // Must explicitly divide into priority batches and non-priority, so that priorities do not get mixed up between
  535. // instanced and non-instanced batches
  536. for (unsigned i = 0; i < batches_.Size(); ++i)
  537. {
  538. if (batches_[i].hasPriority_)
  539. sortedPriorityBatches_.Push(&batches_[i]);
  540. else
  541. sortedBatches_.Push(&batches_[i]);
  542. }
  543. Sort(sortedPriorityBatches_.Begin(), sortedPriorityBatches_.End(), CompareBatchesFrontToBack);
  544. Sort(sortedBatches_.Begin(), sortedBatches_.End(), CompareBatchesFrontToBack);
  545. // Sort each group front to back
  546. for (Map<BatchGroupKey, BatchGroup>::Iterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  547. Sort(i->second_.instances_.Begin(), i->second_.instances_.End(), CompareInstancesFrontToBack);
  548. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  549. Sort(i->second_.instances_.Begin(), i->second_.instances_.End(), CompareInstancesFrontToBack);
  550. // Now sort batch groups by the distance of the first batch
  551. sortedPriorityBatchGroups_.Resize(priorityBatchGroups_.Size());
  552. sortedBatchGroups_.Resize(batchGroups_.Size());
  553. unsigned index = 0;
  554. for (Map<BatchGroupKey, BatchGroup>::Iterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  555. sortedPriorityBatchGroups_[index++] = &i->second_;
  556. index = 0;
  557. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  558. sortedBatchGroups_[index++] = &i->second_;
  559. Sort(sortedPriorityBatchGroups_.Begin(), sortedPriorityBatchGroups_.End(), CompareBatchGroupsFrontToBack);
  560. Sort(sortedBatchGroups_.Begin(), sortedBatchGroups_.End(), CompareBatchGroupsFrontToBack);
  561. }
  562. void BatchQueue::SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex)
  563. {
  564. for (Map<BatchGroupKey, BatchGroup>::Iterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  565. i->second_.SetTransforms(renderer, lockedData, freeIndex);
  566. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  567. i->second_.SetTransforms(renderer, lockedData, freeIndex);
  568. }
  569. unsigned BatchQueue::GetNumInstances(Renderer* renderer) const
  570. {
  571. unsigned total = 0;
  572. unsigned minGroupSize = renderer->GetMinInstanceGroupSize();
  573. unsigned maxIndexCount = renderer->GetMaxInstanceTriangles() * 3;
  574. // This is for the purpose of calculating how much space is needed in the instancing buffer. Do not add instance counts
  575. // that are below the minimum threshold for instancing
  576. for (Map<BatchGroupKey, BatchGroup>::ConstIterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  577. {
  578. unsigned instances = i->second_.instances_.Size();
  579. if (instances >= minGroupSize && i->second_.geometry_->GetIndexCount() <= maxIndexCount)
  580. total += instances;
  581. }
  582. for (Map<BatchGroupKey, BatchGroup>::ConstIterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  583. {
  584. unsigned instances = i->second_.instances_.Size();
  585. if (instances >= minGroupSize && i->second_.geometry_->GetIndexCount() <= maxIndexCount)
  586. total += instances;
  587. }
  588. return total;
  589. }