Batch.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. void Batch::CalculateSortKey()
  57. {
  58. unsigned light = (*((unsigned*)&light_) / sizeof(Light)) & 0x7fff;
  59. unsigned pass = (*((unsigned*)&pass_) / sizeof(Pass)) & 0xffff;
  60. unsigned material = (*((unsigned*)&material_) / sizeof(Material)) & 0xffff;
  61. unsigned geometry = (*((unsigned*)&geometry_) / sizeof(Geometry)) & 0xffff;
  62. if (hasPriority_)
  63. light |= 0x8000;
  64. sortKey_ = (((unsigned long long)light) << 48) || (((unsigned long long)pass) << 32) ||
  65. (((unsigned long long)material) << 16) || geometry;
  66. }
  67. void Batch::Prepare(Graphics* graphics, bool SetModelTransform) const
  68. {
  69. if ((!vertexShader_) || (!pixelShader_))
  70. return;
  71. // Set shaders
  72. graphics->SetShaders(vertexShader_, pixelShader_);
  73. // Set pass / material-specific renderstates
  74. if ((pass_) && (material_))
  75. {
  76. if (pass_->GetAlphaTest())
  77. graphics->SetAlphaTest(true, CMP_GREATEREQUAL, 0.5f);
  78. else
  79. graphics->SetAlphaTest(false);
  80. graphics->SetBlendMode(pass_->GetBlendMode());
  81. graphics->SetCullMode(pass_->GetType() != PASS_SHADOW ? material_->GetCullMode() : material_->GetShadowCullMode());
  82. graphics->SetDepthTest(pass_->GetDepthTestMode());
  83. graphics->SetDepthWrite(pass_->GetDepthWrite());
  84. const Vector<SharedPtr<Texture> >& textures = material_->GetTextures();
  85. if (graphics->NeedTextureUnit(TU_DIFFUSE))
  86. graphics->SetTexture(TU_DIFFUSE, textures[TU_DIFFUSE]);
  87. if (graphics->NeedTextureUnit(TU_NORMAL))
  88. graphics->SetTexture(TU_NORMAL, textures[TU_NORMAL]);
  89. if (graphics->NeedTextureUnit(TU_SPECULAR))
  90. graphics->SetTexture(TU_SPECULAR, textures[TU_SPECULAR]);
  91. if (graphics->NeedTextureUnit(TU_DETAIL))
  92. graphics->SetTexture(TU_DETAIL, textures[TU_DETAIL]);
  93. if (graphics->NeedTextureUnit(TU_ENVIRONMENT))
  94. graphics->SetTexture(TU_ENVIRONMENT, textures[TU_ENVIRONMENT]);
  95. if (!light_)
  96. {
  97. if (graphics->NeedTextureUnit(TU_EMISSIVE))
  98. graphics->SetTexture(TU_EMISSIVE, textures[TU_EMISSIVE]);
  99. }
  100. }
  101. // Set light-related textures
  102. Texture2D* shadowMap = 0;
  103. if (light_)
  104. {
  105. shadowMap = light_->GetShadowMap();
  106. if ((shadowMap) && (graphics->NeedTextureUnit(TU_SHADOWMAP)))
  107. graphics->SetTexture(TU_SHADOWMAP, shadowMap);
  108. if (graphics->NeedTextureUnit(TU_LIGHTRAMP))
  109. graphics->SetTexture(TU_LIGHTRAMP, light_->GetRampTexture());
  110. if (graphics->NeedTextureUnit(TU_LIGHTSPOT))
  111. graphics->SetTexture(TU_LIGHTSPOT, light_->GetShapeTexture());
  112. }
  113. // Set viewport parameters
  114. if (graphics->NeedParameterUpdate(VSP_CAMERAPOS, camera_))
  115. graphics->SetShaderParameter(VSP_CAMERAPOS, camera_->GetWorldPosition());
  116. if (graphics->NeedParameterUpdate(VSP_CAMERAROT, camera_))
  117. graphics->SetShaderParameter(VSP_CAMERAROT, camera_->GetWorldTransform().RotationMatrix());
  118. if (graphics->NeedParameterUpdate(VSP_DEPTHMODE, camera_))
  119. {
  120. Vector4 depthMode = Vector4::ZERO;
  121. if (camera_->IsOrthographic())
  122. depthMode.z_ = 1.0f;
  123. else
  124. depthMode.w_ = 1.0f / camera_->GetFarClip();
  125. graphics->SetShaderParameter(VSP_DEPTHMODE, depthMode);
  126. }
  127. // Set transforms
  128. if ((SetModelTransform) && (graphics->NeedParameterUpdate(VSP_MODEL, worldTransform_)))
  129. graphics->SetShaderParameter(VSP_MODEL, *worldTransform_);
  130. if ((shadowMap) && (graphics->NeedParameterUpdate(VSP_SHADOWPROJ, light_)))
  131. {
  132. Camera* shadowCamera = light_->GetShadowCamera();
  133. Matrix3x4 shadowView(shadowCamera->InverseWorldTransform());
  134. Matrix4 shadowProj(shadowCamera->GetProjection());
  135. Matrix4 texAdjust(Matrix4::IDENTITY);
  136. float offset = 0.5f + 0.5f / (float)shadowMap->GetWidth();
  137. texAdjust.SetTranslation(Vector3(offset, offset, 0.0f));
  138. texAdjust.SetScale(Vector3(0.5f, -0.5f, 1.0f));
  139. graphics->SetShaderParameter(VSP_SHADOWPROJ, texAdjust * shadowProj * shadowView);
  140. }
  141. if ((light_) && (graphics->NeedParameterUpdate(VSP_SPOTPROJ, light_)))
  142. {
  143. const Matrix3x4& transform = light_->GetWorldTransform();
  144. Matrix3x4 spotView(transform.Translation(), transform.Rotation(), 1.0f);
  145. Matrix4 spotProj(Matrix4::ZERO);
  146. // Make the projected light slightly smaller than the shadow map to prevent light spill
  147. float h = 1.005f / tanf(light_->GetFov() * M_DEGTORAD * 0.5f);
  148. float w = h / light_->GetAspectRatio();
  149. spotProj.m00_ = w;
  150. spotProj.m11_ = h;
  151. spotProj.m22_ = 1.0f / Max(light_->GetRange(), M_EPSILON);
  152. spotProj.m32_ = 1.0f;
  153. Matrix4 texAdjust(Matrix4::IDENTITY);
  154. texAdjust.SetTranslation(Vector3(0.5f, 0.5f, 0.0f));
  155. texAdjust.SetScale(Vector3(0.5f, -0.5f, 1.0f));
  156. graphics->SetShaderParameter(VSP_SPOTPROJ, texAdjust * spotProj * spotView.Inverse());
  157. }
  158. if (overrideView_)
  159. {
  160. // If we override the view matrix, also disable any projection jittering
  161. /// \todo This may not be correct in all cases (skybox rendering?)
  162. if (graphics->NeedParameterUpdate(VSP_VIEWPROJ, &Matrix3x4::IDENTITY))
  163. graphics->SetShaderParameter(VSP_VIEWPROJ, camera_->GetProjection(false));
  164. }
  165. else
  166. {
  167. if (graphics->NeedParameterUpdate(VSP_VIEWPROJ, camera_))
  168. graphics->SetShaderParameter(VSP_VIEWPROJ, camera_->GetProjection() *
  169. camera_->InverseWorldTransform());
  170. }
  171. if (graphics->NeedParameterUpdate(VSP_VIEWRIGHTVECTOR, camera_))
  172. graphics->SetShaderParameter(VSP_VIEWRIGHTVECTOR, camera_->GetRightVector());
  173. if (graphics->NeedParameterUpdate(VSP_VIEWUPVECTOR, camera_))
  174. graphics->SetShaderParameter(VSP_VIEWUPVECTOR, camera_->GetUpVector());
  175. // Set skinning transforms
  176. if ((shaderData_) && (shaderDataSize_))
  177. {
  178. if (graphics->NeedParameterUpdate(VSP_SKINMATRICES, shaderData_))
  179. graphics->SetShaderParameter(VSP_SKINMATRICES, (const float*)shaderData_, shaderDataSize_);
  180. }
  181. // Set light-related parameters
  182. if (light_)
  183. {
  184. if (graphics->NeedParameterUpdate(PSP_LIGHTATTEN, light_))
  185. {
  186. Vector4 light_Atten(1.0f / Max(light_->GetRange(), M_EPSILON), 0.0f, 0.0f, 0.0f);
  187. graphics->SetShaderParameter(PSP_LIGHTATTEN, light_Atten);
  188. }
  189. if (graphics->NeedParameterUpdate(PSP_LIGHTCOLOR, light_))
  190. {
  191. float fade = 1.0f;
  192. float fadeEnd = light_->GetDrawDistance();
  193. float fadeStart = light_->GetFadeDistance();
  194. // Do fade calculation for light if both fade & draw distance defined
  195. if ((light_->GetLightType() != LIGHT_DIRECTIONAL) && (fadeEnd > 0.0f) && (fadeStart > 0.0f) && (fadeStart < fadeEnd))
  196. fade = Min(1.0f - (light_->GetDistance() - fadeStart) / (fadeEnd - fadeStart), 1.0f);
  197. graphics->SetShaderParameter(PSP_LIGHTCOLOR, Vector4(light_->GetColor().RGBValues(),
  198. light_->GetSpecularIntensity()) * fade);
  199. }
  200. if (graphics->NeedParameterUpdate(PSP_LIGHTDIR, light_))
  201. graphics->SetShaderParameter(PSP_LIGHTDIR, light_->GetWorldRotation() * Vector3::BACK);
  202. if (graphics->NeedParameterUpdate(PSP_LIGHTPOS, light_))
  203. graphics->SetShaderParameter(PSP_LIGHTPOS, light_->GetWorldPosition() - camera_->GetWorldPosition());
  204. if (graphics->NeedParameterUpdate(PSP_LIGHTSPLITS, light_))
  205. {
  206. float nearFadeRange = light_->GetNearFadeRange();
  207. float farFadeRange = light_->GetFarFadeRange();
  208. float nearFadeStart = light_->GetNearSplit() - nearFadeRange;
  209. float farFadeStart = light_->GetFarSplit() - farFadeRange;
  210. float depthRange = camera_->GetFarClip();
  211. graphics->SetShaderParameter(PSP_LIGHTSPLITS, Vector4(nearFadeStart / depthRange, 1.0f / (nearFadeRange / depthRange),
  212. farFadeStart / depthRange, 1.0f / (farFadeRange / depthRange)));
  213. }
  214. if (graphics->NeedParameterUpdate(PSP_LIGHTVECROT, light_))
  215. {
  216. Matrix3x4 light_VecRot;
  217. // Use original light if available (split lights)
  218. Light* original = light_->GetOriginalLight();
  219. if (!original)
  220. light_VecRot = Matrix3x4(Vector3::ZERO, light_->GetWorldRotation(), Vector3::UNITY);
  221. else
  222. light_VecRot = Matrix3x4(Vector3::ZERO, original->GetWorldRotation(), Vector3::UNITY);
  223. graphics->SetShaderParameter(PSP_LIGHTVECROT, light_VecRot);
  224. }
  225. }
  226. // Set shadow & spotlight projection parameters
  227. if (shadowMap)
  228. {
  229. if (graphics->NeedParameterUpdate(PSP_SAMPLEOFFSETS, shadowMap))
  230. {
  231. float invWidth = 1.0f / (float)shadowMap->GetWidth();
  232. graphics->SetShaderParameter(PSP_SAMPLEOFFSETS, Vector4(0.5f * invWidth, -0.5f * invWidth, 0.0f, 0.0f));
  233. }
  234. if (graphics->NeedParameterUpdate(PSP_SHADOWINTENSITY, light_))
  235. {
  236. float intensity = light_->GetShadowIntensity();
  237. float fadeStart = light_->GetShadowFadeDistance();
  238. float fadeEnd = light_->GetShadowDistance();
  239. if ((fadeStart > 0.0f) && (fadeEnd > 0.0f) && (fadeEnd > fadeStart))
  240. intensity = Lerp(intensity, 1.0f, Clamp((light_->GetDistance() - fadeStart) / (fadeEnd - fadeStart), 0.0f, 1.0f));
  241. float pcfValues = (1.0f - intensity) * 0.25f;
  242. graphics->SetShaderParameter(PSP_SHADOWINTENSITY, Vector4(pcfValues, intensity, 0.0f, 0.0f));
  243. }
  244. if (graphics->NeedParameterUpdate(PSP_SHADOWPROJ, light_))
  245. {
  246. Camera* shadowCamera = light_->GetShadowCamera();
  247. Matrix3x4 shadowView(shadowCamera->InverseWorldTransform());
  248. Matrix4 shadowProj(shadowCamera->GetProjection());
  249. Matrix3x4 viewPos(Matrix3x4::IDENTITY);
  250. viewPos.SetTranslation(camera_->GetWorldPosition());
  251. Matrix4 texAdjust(Matrix4::IDENTITY);
  252. float offset = 0.5f + 0.5f / (float)shadowMap->GetWidth();
  253. texAdjust.SetTranslation(Vector3(offset, offset, 0.0f));
  254. texAdjust.SetScale(Vector3(0.5f, -0.5f, 1.0f));
  255. graphics->SetShaderParameter(PSP_SHADOWPROJ, texAdjust * shadowProj * shadowView * viewPos);
  256. }
  257. }
  258. if ((light_) && (graphics->NeedParameterUpdate(PSP_SPOTPROJ, light_)))
  259. {
  260. Matrix3x4 spotView(light_->GetWorldPosition(), light_->GetWorldRotation(), 1.0f);
  261. Matrix4 spotProj(Matrix4::IDENTITY);
  262. // Make the projected light slightly smaller than the shadow map to prevent light spill
  263. float h = 1.005f / tanf(light_->GetFov() * M_DEGTORAD * 0.5f);
  264. float w = h / light_->GetAspectRatio();
  265. spotProj.m00_ = w;
  266. spotProj.m11_ = h;
  267. spotProj.m22_ = 1.0f / Max(light_->GetRange(), M_EPSILON);
  268. spotProj.m32_ = 1.0f;
  269. Matrix3x4 viewPos(Matrix3x4::IDENTITY);
  270. viewPos.SetTranslation(camera_->GetWorldPosition());
  271. Matrix4 texAdjust(Matrix4::IDENTITY);
  272. texAdjust.SetTranslation(Vector3(0.5f, 0.5f, 0.0f));
  273. texAdjust.SetScale(Vector3(0.5f, -0.5f, 1.0f));
  274. graphics->SetShaderParameter(PSP_SPOTPROJ, texAdjust * spotProj * spotView.Inverse() * viewPos);
  275. }
  276. // Set material's shader parameters
  277. if (material_)
  278. {
  279. const Map<ShaderParameter, Vector4>& parameters = material_->GetShaderParameters();
  280. for (Map<ShaderParameter, Vector4>::ConstIterator i = parameters.Begin(); i != parameters.End(); ++i)
  281. {
  282. if (graphics->NeedParameterUpdate(i->first_, material_))
  283. graphics->SetShaderParameter(i->first_, i->second_);
  284. }
  285. }
  286. }
  287. void Batch::Draw(Graphics* graphics) const
  288. {
  289. Prepare(graphics);
  290. geometry_->Draw(graphics);
  291. }
  292. void BatchGroup::SetTransforms(void* lockedData, unsigned& freeIndex)
  293. {
  294. // Do not use up buffer space if not going to draw as instanced
  295. if (instances_.Size() < MIN_INSTANCES)
  296. return;
  297. startIndex_ = freeIndex;
  298. Matrix3x4* dest = (Matrix3x4*)lockedData;
  299. dest += freeIndex;
  300. for (unsigned i = 0; i < instances_.Size(); ++i)
  301. *dest++ = *instances_[i].worldTransform_;
  302. freeIndex += instances_.Size();
  303. }
  304. void BatchGroup::Draw(Graphics* graphics, VertexBuffer* instanceBuffer) const
  305. {
  306. if (!instances_.Size())
  307. return;
  308. // Construct a temporary batch for rendering
  309. Batch batch;
  310. batch.geometry_ = geometry_;
  311. batch.material_ = material_;
  312. batch.pass_ = pass_;
  313. batch.vertexShader_ = vertexShader_;
  314. batch.pixelShader_ = pixelShader_;
  315. batch.camera_ = camera_;
  316. batch.light_ = light_;
  317. batch.vertexShaderIndex_ = vertexShaderIndex_;
  318. // Draw as individual instances if below minimum size, or if instancing not supported
  319. if ((instances_.Size() < MIN_INSTANCES) || (!instanceBuffer))
  320. {
  321. batch.Prepare(graphics, false);
  322. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  323. graphics->SetVertexBuffers(geometry_->GetVertexBuffers(), geometry_->GetVertexElementMasks());
  324. for (unsigned i = 0; i < instances_.Size(); ++i)
  325. {
  326. if (graphics->NeedParameterUpdate(VSP_MODEL, instances_[i].worldTransform_))
  327. graphics->SetShaderParameter(VSP_MODEL, *instances_[i].worldTransform_);
  328. graphics->Draw(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  329. geometry_->GetVertexStart(), geometry_->GetVertexCount());
  330. }
  331. }
  332. else
  333. {
  334. // Switch to the instancing vertex shader
  335. // The indexing is different in the forward lit passes
  336. Vector<SharedPtr<ShaderVariation> >& vertexShaders = pass_->GetVertexShaders();
  337. Vector<SharedPtr<ShaderVariation> >& pixelShaders = pass_->GetPixelShaders();
  338. PassType type = pass_->GetType();
  339. if ((type != PASS_LITBASE) && (type != PASS_LIGHT))
  340. batch.vertexShader_ = vertexShaders[vertexShaderIndex_ + GEOM_INSTANCED];
  341. else
  342. batch.vertexShader_ = vertexShaders[vertexShaderIndex_ + GEOM_INSTANCED * MAX_LIGHT_VS_VARIATIONS];
  343. batch.Prepare(graphics, false);
  344. // Get the geometry vertex buffers, then add the instancing stream buffer
  345. Vector<SharedPtr<VertexBuffer> > vertexBuffers = geometry_->GetVertexBuffers();
  346. PODVector<unsigned> elementMasks = geometry_->GetVertexElementMasks();
  347. vertexBuffers.Push(SharedPtr<VertexBuffer>(instanceBuffer));
  348. elementMasks.Push(instanceBuffer->GetElementMask());
  349. // No stream offset support, instancing buffer not pre-filled with transforms: have to lock and fill now
  350. if (startIndex_ == M_MAX_UNSIGNED)
  351. {
  352. unsigned startIndex = 0;
  353. while (startIndex < instances_.Size())
  354. {
  355. unsigned instances = instances_.Size() - startIndex;
  356. if (instances > instanceBuffer->GetVertexCount())
  357. instances = instanceBuffer->GetVertexCount();
  358. // Lock the instance stream buffer and copy the transforms
  359. void* data = instanceBuffer->Lock(0, instances, LOCK_DISCARD);
  360. if (!data)
  361. return;
  362. Matrix3x4* dest = (Matrix3x4*)data;
  363. for (unsigned i = 0; i < instances; ++i)
  364. dest[i] = *instances_[i + startIndex].worldTransform_;
  365. instanceBuffer->Unlock();
  366. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  367. graphics->SetVertexBuffers(vertexBuffers, elementMasks);
  368. graphics->DrawInstanced(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  369. geometry_->GetVertexStart(), geometry_->GetVertexCount(), instances);
  370. startIndex += instances;
  371. }
  372. }
  373. // Stream offset supported, and instancing buffer has been already filled, so just draw
  374. else
  375. {
  376. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  377. graphics->SetVertexBuffers(vertexBuffers, elementMasks, startIndex_);
  378. graphics->DrawInstanced(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  379. geometry_->GetVertexStart(), geometry_->GetVertexCount(), instances_.Size());
  380. }
  381. }
  382. }
  383. void BatchQueue::Clear()
  384. {
  385. batches_.Clear();
  386. sortedPriorityBatches_.Clear();
  387. sortedBatches_.Clear();
  388. priorityBatchGroups_.Clear();
  389. batchGroups_.Clear();
  390. }
  391. void BatchQueue::AddBatch(const Batch& batch, bool noInstancing)
  392. {
  393. // If batch is something else than static, has custom view, or has per-instance shader data defined, can not instance
  394. if ((noInstancing) || (batch.geometryType_ != GEOM_STATIC) || (batch.overrideView_) || (batch.shaderData_))
  395. batches_.Push(batch);
  396. else
  397. {
  398. BatchGroupKey key;
  399. key.light_ = batch.light_;
  400. key.pass_ = batch.pass_;
  401. key.material_ = batch.material_;
  402. key.geometry_ = batch.geometry_;
  403. Map<BatchGroupKey, BatchGroup>* groups = batch.hasPriority_ ? &priorityBatchGroups_ : &batchGroups_;
  404. Map<BatchGroupKey, BatchGroup>::Iterator i = groups->Find(key);
  405. if (i == groups->End())
  406. {
  407. // Create new group
  408. BatchGroup newGroup;
  409. newGroup.geometry_ = batch.geometry_;
  410. newGroup.material_ = batch.material_;
  411. newGroup.pass_ = batch.pass_;
  412. newGroup.vertexShader_ = batch.vertexShader_;
  413. newGroup.pixelShader_ = batch.pixelShader_;
  414. newGroup.camera_ = batch.camera_;
  415. newGroup.light_ = batch.light_;
  416. newGroup.vertexShaderIndex_ = batch.vertexShaderIndex_;
  417. newGroup.instances_.Push(InstanceData(batch.worldTransform_, batch.distance_));
  418. groups->Insert(MakePair(key, newGroup));
  419. }
  420. else
  421. i->second_.instances_.Push(InstanceData(batch.worldTransform_, batch.distance_));
  422. }
  423. }
  424. void BatchQueue::SortBackToFront()
  425. {
  426. sortedPriorityBatches_.Clear();
  427. sortedBatches_.Resize(batches_.Size());
  428. for (unsigned i = 0; i < batches_.Size(); ++i)
  429. sortedBatches_[i] = &batches_[i];
  430. Sort(sortedBatches_.Begin(), sortedBatches_.End(), CompareBatchesBackToFront);
  431. }
  432. void BatchQueue::SortFrontToBack()
  433. {
  434. sortedPriorityBatches_.Clear();
  435. sortedBatches_.Clear();
  436. // Must explicitly divide into priority batches and non-priority, so that priorities do not get mixed up between
  437. // instanced and non-instanced batches
  438. for (unsigned i = 0; i < batches_.Size(); ++i)
  439. {
  440. if (batches_[i].hasPriority_)
  441. sortedPriorityBatches_.Push(&batches_[i]);
  442. else
  443. sortedBatches_.Push(&batches_[i]);
  444. }
  445. Sort(sortedPriorityBatches_.Begin(), sortedPriorityBatches_.End(), CompareBatchesFrontToBack);
  446. Sort(sortedBatches_.Begin(), sortedBatches_.End(), CompareBatchesFrontToBack);
  447. // Sort each group front to back
  448. for (Map<BatchGroupKey, BatchGroup>::Iterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  449. Sort(i->second_.instances_.Begin(), i->second_.instances_.End(), CompareInstancesFrontToBack);
  450. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  451. Sort(i->second_.instances_.Begin(), i->second_.instances_.End(), CompareInstancesFrontToBack);
  452. }
  453. void BatchQueue::SetTransforms(void* lockedData, unsigned& freeIndex)
  454. {
  455. for (Map<BatchGroupKey, BatchGroup>::Iterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  456. i->second_.SetTransforms(lockedData, freeIndex);
  457. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  458. i->second_.SetTransforms(lockedData, freeIndex);
  459. }
  460. unsigned BatchQueue::GetNumInstances() const
  461. {
  462. unsigned total = 0;
  463. // This is for the purpose of calculating how much space is needed in the instancing buffer. Do not add instance counts
  464. // that are below the minimum threshold for instancing
  465. for (Map<BatchGroupKey, BatchGroup>::ConstIterator i = priorityBatchGroups_.Begin(); i != priorityBatchGroups_.End(); ++i)
  466. {
  467. unsigned instances = i->second_.instances_.Size();
  468. if (instances >= MIN_INSTANCES)
  469. total += instances;
  470. }
  471. for (Map<BatchGroupKey, BatchGroup>::ConstIterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  472. {
  473. unsigned instances = i->second_.instances_.Size();
  474. if (instances >= MIN_INSTANCES)
  475. total += instances;
  476. }
  477. return total;
  478. }