Batch.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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. texAdjust.SetTranslation(Vector3(offset.x_, offset.y_, 0.5f));
  208. texAdjust.SetScale(Vector3(scale.x_, scale.y_, 0.5f));
  209. #else
  210. offset.x_ += scale.x_ + 0.5f / (float)shadowMap->GetWidth();
  211. offset.y_ += scale.y_ + 0.5f / (float)shadowMap->GetHeight();
  212. scale.y_ = -scale.y_;
  213. texAdjust.SetTranslation(Vector3(offset.x_, offset.y_, 0.0f));
  214. texAdjust.SetScale(Vector3(scale.x_, scale.y_, 1.0f));
  215. #endif
  216. shadowMatrices[i] = texAdjust * shadowProj * shadowView;
  217. }
  218. graphics->SetShaderParameter(VSP_SHADOWPROJ, shadowMatrices[0].GetData(), 16 * numSplits);
  219. }
  220. if (graphics->NeedParameterUpdate(PSP_SAMPLEOFFSETS, shadowMap))
  221. {
  222. float invWidth = 1.0f / (float)shadowMap->GetWidth();
  223. float invHeight = 1.0f / (float)shadowMap->GetHeight();
  224. graphics->SetShaderParameter(PSP_SAMPLEOFFSETS, Vector4(0.5f * invWidth, 0.5f * invHeight, 0.0f, 0.0f));
  225. }
  226. if (graphics->NeedParameterUpdate(PSP_SHADOWCUBEADJUST, light))
  227. {
  228. unsigned faceWidth = shadowMap->GetWidth() / 2;
  229. unsigned faceHeight = shadowMap->GetHeight() / 3;
  230. float width = (float)shadowMap->GetWidth();
  231. float height = (float)shadowMap->GetHeight();
  232. #ifdef USE_OPENGL
  233. float mulX = (float)(faceWidth - 3) / width;
  234. float mulY = (float)(faceHeight - 3) / height;
  235. float addX = 1.5f / width;
  236. float addY = 1.5f / height;
  237. #else
  238. float mulX = (float)(faceWidth - 4) / width;
  239. float mulY = (float)(faceHeight - 4) / height;
  240. float addX = 2.5f / width;
  241. float addY = 2.5f / height;
  242. #endif
  243. graphics->SetShaderParameter(PSP_SHADOWCUBEADJUST, Vector4(mulX, mulY, addX, addY));
  244. }
  245. if (graphics->NeedParameterUpdate(PSP_SHADOWCUBEPROJ, light))
  246. {
  247. // Note: we use the shadow camera of the first cube face. All are assumed to use the same projection
  248. Camera* shadowCamera = lightQueue_->shadowSplits_[0].shadowCamera_;
  249. float nearClip = shadowCamera->GetNearClip();
  250. float farClip = shadowCamera->GetFarClip();
  251. float q = farClip / (farClip - nearClip);
  252. float r = -q * nearClip;
  253. graphics->SetShaderParameter(PSP_SHADOWCUBEPROJ, Vector4(q, r, 0.0f, 0.0f));
  254. }
  255. if (graphics->NeedParameterUpdate(PSP_SHADOWFADE, light))
  256. {
  257. const CascadeParameters& parameters = light->GetShadowCascade();
  258. float farClip = camera_->GetFarClip();
  259. float shadowRange = parameters.GetShadowRange();
  260. float fadeStart = parameters.fadeStart_ * shadowRange / farClip;
  261. float fadeEnd = shadowRange / farClip;
  262. float fadeRange = fadeEnd - fadeStart;
  263. graphics->SetShaderParameter(PSP_SHADOWFADE, Vector4(fadeStart, 1.0f / fadeRange, 0.0f, 0.0f));
  264. }
  265. if (graphics->NeedParameterUpdate(PSP_SHADOWINTENSITY, light))
  266. {
  267. float intensity = light->GetShadowIntensity();
  268. float fadeStart = light->GetShadowFadeDistance();
  269. float fadeEnd = light->GetShadowDistance();
  270. if (fadeStart > 0.0f && fadeEnd > 0.0f && fadeEnd > fadeStart)
  271. intensity = Lerp(intensity, 1.0f, Clamp((light->GetDistance() - fadeStart) / (fadeEnd - fadeStart), 0.0f, 1.0f));
  272. float pcfValues = (1.0f - intensity);
  273. // Fallback mode requires manual depth biasing. We do not do proper slope scale biasing,
  274. // instead just fudge the bias values together
  275. float constantBias = graphics->GetDepthConstantBias();
  276. float slopeScaledBias = graphics->GetDepthSlopeScaledBias();
  277. graphics->SetShaderParameter(PSP_SHADOWINTENSITY, Vector4(pcfValues, pcfValues * 0.25f, intensity, constantBias +
  278. slopeScaledBias * constantBias));
  279. }
  280. if (graphics->NeedParameterUpdate(PSP_SHADOWSPLITS, light))
  281. {
  282. Vector4 lightSplits(M_LARGE_VALUE, M_LARGE_VALUE, M_LARGE_VALUE, M_LARGE_VALUE);
  283. if (lightQueue_->shadowSplits_.Size() > 1)
  284. lightSplits.x_ = lightQueue_->shadowSplits_[0].farSplit_ / camera_->GetFarClip();
  285. if (lightQueue_->shadowSplits_.Size() > 2)
  286. lightSplits.y_ = lightQueue_->shadowSplits_[1].farSplit_ / camera_->GetFarClip();
  287. if (lightQueue_->shadowSplits_.Size() > 3)
  288. lightSplits.z_ = lightQueue_->shadowSplits_[2].farSplit_ / camera_->GetFarClip();
  289. graphics->SetShaderParameter(PSP_SHADOWSPLITS, lightSplits);
  290. }
  291. }
  292. }
  293. // Set material-specific shader parameters and textures
  294. if (material_)
  295. {
  296. const HashMap<StringHash, MaterialShaderParameter>& parameters = material_->GetShaderParameters();
  297. for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = parameters.Begin(); i != parameters.End(); ++i)
  298. {
  299. if (graphics->NeedParameterUpdate(i->first_, material_))
  300. graphics->SetShaderParameter(i->first_, i->second_.value_);
  301. }
  302. const Vector<SharedPtr<Texture> >& textures = material_->GetTextures();
  303. if (graphics->NeedTextureUnit(TU_DIFFUSE))
  304. graphics->SetTexture(TU_DIFFUSE, textures[TU_DIFFUSE]);
  305. if (graphics->NeedTextureUnit(TU_NORMAL))
  306. graphics->SetTexture(TU_NORMAL, textures[TU_NORMAL]);
  307. if (graphics->NeedTextureUnit(TU_DETAIL))
  308. graphics->SetTexture(TU_DETAIL, textures[TU_DETAIL]);
  309. if (graphics->NeedTextureUnit(TU_ENVIRONMENT))
  310. graphics->SetTexture(TU_ENVIRONMENT, textures[TU_ENVIRONMENT]);
  311. }
  312. // Set light-related textures
  313. if (light)
  314. {
  315. if (shadowMap && graphics->NeedTextureUnit(TU_SHADOWMAP))
  316. graphics->SetTexture(TU_SHADOWMAP, shadowMap);
  317. if (graphics->NeedTextureUnit(TU_LIGHTRAMP))
  318. {
  319. Texture* rampTexture = light->GetRampTexture();
  320. if (!rampTexture)
  321. rampTexture = renderer->GetDefaultLightRamp();
  322. graphics->SetTexture(TU_LIGHTRAMP, rampTexture);
  323. }
  324. if (graphics->NeedTextureUnit(TU_LIGHTSHAPE))
  325. {
  326. Texture* shapeTexture = light->GetShapeTexture();
  327. if (!shapeTexture && light->GetLightType() == LIGHT_SPOT)
  328. shapeTexture = renderer->GetDefaultLightSpot();
  329. graphics->SetTexture(TU_LIGHTSHAPE, shapeTexture);
  330. }
  331. }
  332. }
  333. void Batch::Draw(Graphics* graphics, Renderer* renderer, const HashMap<StringHash, Vector4>& shaderParameters) const
  334. {
  335. Prepare(graphics, renderer, shaderParameters);
  336. geometry_->Draw(graphics);
  337. }
  338. void BatchGroup::SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex)
  339. {
  340. // Do not use up buffer space if not going to draw as instanced
  341. unsigned minGroupSize = renderer->GetMinInstanceGroupSize();
  342. unsigned maxIndexCount = renderer->GetMaxInstanceTriangles() * 3;
  343. if (instances_.Size() < minGroupSize || geometry_->GetIndexCount() > maxIndexCount)
  344. return;
  345. startIndex_ = freeIndex;
  346. Matrix3x4* dest = (Matrix3x4*)lockedData;
  347. dest += freeIndex;
  348. for (unsigned i = 0; i < instances_.Size(); ++i)
  349. *dest++ = *instances_[i].worldTransform_;
  350. freeIndex += instances_.Size();
  351. }
  352. void BatchGroup::Draw(Graphics* graphics, Renderer* renderer, const HashMap<StringHash, Vector4>& shaderParameters) const
  353. {
  354. if (!instances_.Size())
  355. return;
  356. // Construct a temporary batch for rendering
  357. Batch batch;
  358. batch.geometry_ = geometry_;
  359. batch.material_ = material_;
  360. batch.pass_ = pass_;
  361. batch.vertexShader_ = vertexShader_;
  362. batch.pixelShader_ = pixelShader_;
  363. batch.camera_ = camera_;
  364. batch.lightQueue_ = lightQueue_;
  365. batch.vertexShaderIndex_ = vertexShaderIndex_;
  366. unsigned minGroupSize = renderer->GetMinInstanceGroupSize();
  367. unsigned maxIndexCount = renderer->GetMaxInstanceTriangles() * 3;
  368. // Draw as individual instances if below minimum size, or if instancing not supported
  369. VertexBuffer* instanceBuffer = renderer->GetInstancingBuffer();
  370. if (!instanceBuffer || instances_.Size() < minGroupSize || geometry_->GetIndexCount() > maxIndexCount)
  371. {
  372. batch.Prepare(graphics, renderer, shaderParameters, false);
  373. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  374. graphics->SetVertexBuffers(geometry_->GetVertexBuffers(), geometry_->GetVertexElementMasks());
  375. for (unsigned i = 0; i < instances_.Size(); ++i)
  376. {
  377. if (graphics->NeedParameterUpdate(VSP_MODEL, instances_[i].worldTransform_))
  378. graphics->SetShaderParameter(VSP_MODEL, *instances_[i].worldTransform_);
  379. graphics->Draw(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  380. geometry_->GetVertexStart(), geometry_->GetVertexCount());
  381. }
  382. }
  383. else
  384. {
  385. // Switch to the instancing vertex shader
  386. // The indexing is different in the forward lit passes
  387. Vector<SharedPtr<ShaderVariation> >& vertexShaders = pass_->GetVertexShaders();
  388. Vector<SharedPtr<ShaderVariation> >& pixelShaders = pass_->GetPixelShaders();
  389. PassType type = pass_->GetType();
  390. if (type == PASS_LIGHT || type == PASS_LITBASE)
  391. batch.vertexShader_ = vertexShaders[vertexShaderIndex_ + GEOM_INSTANCED * MAX_LIGHT_VS_VARIATIONS];
  392. else
  393. batch.vertexShader_ = vertexShaders[vertexShaderIndex_ + GEOM_INSTANCED];
  394. batch.Prepare(graphics, renderer, shaderParameters, false);
  395. // Get the geometry vertex buffers, then add the instancing stream buffer
  396. // Hack: use a const_cast to avoid dynamic allocation of new temp vectors
  397. Vector<SharedPtr<VertexBuffer> >& vertexBuffers = const_cast<Vector<SharedPtr<VertexBuffer> >&>
  398. (geometry_->GetVertexBuffers());
  399. PODVector<unsigned>& elementMasks = const_cast<PODVector<unsigned>&>(geometry_->GetVertexElementMasks());
  400. vertexBuffers.Push(SharedPtr<VertexBuffer>(instanceBuffer));
  401. elementMasks.Push(instanceBuffer->GetElementMask());
  402. // No stream offset support, instancing buffer not pre-filled with transforms: have to lock and fill now
  403. if (startIndex_ == M_MAX_UNSIGNED)
  404. {
  405. unsigned startIndex = 0;
  406. while (startIndex < instances_.Size())
  407. {
  408. unsigned instances = instances_.Size() - startIndex;
  409. if (instances > instanceBuffer->GetVertexCount())
  410. instances = instanceBuffer->GetVertexCount();
  411. // Lock the instance stream buffer and copy the transforms
  412. void* data = instanceBuffer->Lock(0, instances, LOCK_DISCARD);
  413. if (!data)
  414. {
  415. // Remember to remove the instancing buffer and element mask
  416. vertexBuffers.Pop();
  417. elementMasks.Pop();
  418. return;
  419. }
  420. Matrix3x4* dest = (Matrix3x4*)data;
  421. for (unsigned i = 0; i < instances; ++i)
  422. dest[i] = *instances_[i + startIndex].worldTransform_;
  423. instanceBuffer->Unlock();
  424. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  425. graphics->SetVertexBuffers(vertexBuffers, elementMasks);
  426. graphics->DrawInstanced(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  427. geometry_->GetVertexStart(), geometry_->GetVertexCount(), instances);
  428. startIndex += instances;
  429. }
  430. }
  431. // Stream offset supported, and instancing buffer has been already filled, so just draw
  432. else
  433. {
  434. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  435. graphics->SetVertexBuffers(vertexBuffers, elementMasks, startIndex_);
  436. graphics->DrawInstanced(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  437. geometry_->GetVertexStart(), geometry_->GetVertexCount(), instances_.Size());
  438. }
  439. // Remove the instancing buffer & element mask now
  440. vertexBuffers.Pop();
  441. elementMasks.Pop();
  442. }
  443. }
  444. void BatchQueue::Clear()
  445. {
  446. batches_.Clear();
  447. sortedPriorityBatches_.Clear();
  448. sortedBatches_.Clear();
  449. priorityBatchGroups_.Clear();
  450. batchGroups_.Clear();
  451. }
  452. void BatchQueue::AddBatch(const Batch& batch, bool noInstancing)
  453. {
  454. // If batch is something else than static, has custom view, or has per-instance shader data defined, can not instance
  455. if (noInstancing || batch.geometryType_ != GEOM_STATIC || batch.overrideView_ || batch.shaderData_)
  456. batches_.Push(batch);
  457. else
  458. {
  459. BatchGroupKey key;
  460. key.lightQueue_ = batch.lightQueue_;
  461. key.pass_ = batch.pass_;
  462. key.material_ = batch.material_;
  463. key.geometry_ = batch.geometry_;
  464. Map<BatchGroupKey, BatchGroup>* groups = batch.hasPriority_ ? &priorityBatchGroups_ : &batchGroups_;
  465. Map<BatchGroupKey, BatchGroup>::Iterator i = groups->Find(key);
  466. if (i == groups->End())
  467. {
  468. // Create new group
  469. BatchGroup newGroup;
  470. newGroup.geometry_ = batch.geometry_;
  471. newGroup.material_ = batch.material_;
  472. newGroup.pass_ = batch.pass_;
  473. newGroup.vertexShader_ = batch.vertexShader_;
  474. newGroup.pixelShader_ = batch.pixelShader_;
  475. newGroup.camera_ = batch.camera_;
  476. newGroup.lightQueue_ = batch.lightQueue_;
  477. newGroup.vertexShaderIndex_ = batch.vertexShaderIndex_;
  478. newGroup.instances_.Push(InstanceData(batch.worldTransform_, batch.distance_));
  479. groups->Insert(MakePair(key, newGroup));
  480. }
  481. else
  482. i->second_.instances_.Push(InstanceData(batch.worldTransform_, batch.distance_));
  483. }
  484. }
  485. void BatchQueue::SortBackToFront()
  486. {
  487. sortedPriorityBatches_.Clear();
  488. sortedBatches_.Resize(batches_.Size());
  489. for (unsigned i = 0; i < batches_.Size(); ++i)
  490. sortedBatches_[i] = &batches_[i];
  491. Sort(sortedBatches_.Begin(), sortedBatches_.End(), CompareBatchesBackToFront);
  492. // Do not actually sort batch groups, just list them
  493. sortedPriorityBatchGroups_.Resize(priorityBatchGroups_.Size());
  494. sortedBatchGroups_.Resize(batchGroups_.Size());
  495. unsigned index = 0;
  496. for (Map<BatchGroupKey, BatchGroup>::Iterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  497. sortedPriorityBatchGroups_[index++] = &i->second_;
  498. index = 0;
  499. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  500. sortedBatchGroups_[index++] = &i->second_;
  501. }
  502. void BatchQueue::SortFrontToBack()
  503. {
  504. sortedPriorityBatches_.Clear();
  505. sortedBatches_.Clear();
  506. // Must explicitly divide into priority batches and non-priority, so that priorities do not get mixed up between
  507. // instanced and non-instanced batches
  508. for (unsigned i = 0; i < batches_.Size(); ++i)
  509. {
  510. if (batches_[i].hasPriority_)
  511. sortedPriorityBatches_.Push(&batches_[i]);
  512. else
  513. sortedBatches_.Push(&batches_[i]);
  514. }
  515. Sort(sortedPriorityBatches_.Begin(), sortedPriorityBatches_.End(), CompareBatchesFrontToBack);
  516. Sort(sortedBatches_.Begin(), sortedBatches_.End(), CompareBatchesFrontToBack);
  517. // Sort each group front to back
  518. for (Map<BatchGroupKey, BatchGroup>::Iterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  519. Sort(i->second_.instances_.Begin(), i->second_.instances_.End(), CompareInstancesFrontToBack);
  520. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  521. Sort(i->second_.instances_.Begin(), i->second_.instances_.End(), CompareInstancesFrontToBack);
  522. // Now sort batch groups by the distance of the first batch
  523. sortedPriorityBatchGroups_.Resize(priorityBatchGroups_.Size());
  524. sortedBatchGroups_.Resize(batchGroups_.Size());
  525. unsigned index = 0;
  526. for (Map<BatchGroupKey, BatchGroup>::Iterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  527. sortedPriorityBatchGroups_[index++] = &i->second_;
  528. index = 0;
  529. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  530. sortedBatchGroups_[index++] = &i->second_;
  531. Sort(sortedPriorityBatchGroups_.Begin(), sortedPriorityBatchGroups_.End(), CompareBatchGroupsFrontToBack);
  532. Sort(sortedBatchGroups_.Begin(), sortedBatchGroups_.End(), CompareBatchGroupsFrontToBack);
  533. }
  534. void BatchQueue::SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex)
  535. {
  536. for (Map<BatchGroupKey, BatchGroup>::Iterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  537. i->second_.SetTransforms(renderer, lockedData, freeIndex);
  538. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  539. i->second_.SetTransforms(renderer, lockedData, freeIndex);
  540. }
  541. unsigned BatchQueue::GetNumInstances(Renderer* renderer) const
  542. {
  543. unsigned total = 0;
  544. unsigned minGroupSize = renderer->GetMinInstanceGroupSize();
  545. unsigned maxIndexCount = renderer->GetMaxInstanceTriangles() * 3;
  546. // This is for the purpose of calculating how much space is needed in the instancing buffer. Do not add instance counts
  547. // that are below the minimum threshold for instancing
  548. for (Map<BatchGroupKey, BatchGroup>::ConstIterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  549. {
  550. unsigned instances = i->second_.instances_.Size();
  551. if (instances >= minGroupSize && i->second_.geometry_->GetIndexCount() <= maxIndexCount)
  552. total += instances;
  553. }
  554. for (Map<BatchGroupKey, BatchGroup>::ConstIterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  555. {
  556. unsigned instances = i->second_.instances_.Size();
  557. if (instances >= minGroupSize && i->second_.geometry_->GetIndexCount() <= maxIndexCount)
  558. total += instances;
  559. }
  560. return total;
  561. }