Batch.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Zone.h"
  38. #include "DebugNew.h"
  39. inline bool CompareBatchesFrontToBack(Batch* lhs, Batch* rhs)
  40. {
  41. if (lhs->sortKey_ == rhs->sortKey_)
  42. return lhs->distance_ < rhs->distance_;
  43. else
  44. return lhs->sortKey_ > rhs->sortKey_;
  45. }
  46. inline bool CompareBatchesBackToFront(Batch* lhs, Batch* rhs)
  47. {
  48. if (lhs->distance_ == rhs->distance_)
  49. return lhs->sortKey_ > rhs->sortKey_;
  50. else
  51. return lhs->distance_ > rhs->distance_;
  52. }
  53. inline bool CompareInstancesFrontToBack(const InstanceData& lhs, const InstanceData& rhs)
  54. {
  55. return lhs.distance_ < rhs.distance_;
  56. }
  57. inline bool CompareBatchGroupsFrontToBack(BatchGroup* lhs, BatchGroup* rhs)
  58. {
  59. return lhs->instances_[0].distance_ < rhs->instances_[0].distance_;
  60. }
  61. void CalculateShadowMatrix(Matrix4& dest, LightBatchQueue* queue, unsigned split, Renderer* renderer, const Vector3& translation)
  62. {
  63. Camera* shadowCamera = queue->shadowSplits_[split].shadowCamera_;
  64. const IntRect& viewport = queue->shadowSplits_[split].shadowViewport_;
  65. Matrix3x4 posAdjust(translation, Quaternion::IDENTITY, 1.0f);
  66. Matrix3x4 shadowView(shadowCamera->GetInverseWorldTransform());
  67. Matrix4 shadowProj(shadowCamera->GetProjection());
  68. Matrix4 texAdjust(Matrix4::IDENTITY);
  69. Texture2D* shadowMap = queue->shadowMap_;
  70. if (!shadowMap)
  71. return;
  72. float width = (float)shadowMap->GetWidth();
  73. float height = (float)shadowMap->GetHeight();
  74. Vector2 offset(
  75. (float)viewport.left_ / width,
  76. (float)viewport.top_ / height
  77. );
  78. Vector2 scale(
  79. 0.5f * (float)(viewport.right_ - viewport.left_) / width,
  80. 0.5f * (float)(viewport.bottom_ - viewport.top_) / height
  81. );
  82. #ifdef USE_OPENGL
  83. offset.x_ += scale.x_;
  84. offset.y_ += scale.y_;
  85. offset.y_ = 1.0f - offset.y_;
  86. // If using 4 shadow samples, offset the position diagonally by half pixel
  87. if (renderer->GetShadowQuality() & SHADOWQUALITY_HIGH_16BIT)
  88. {
  89. offset.x_ -= 0.5f / width;
  90. offset.y_ -= 0.5f / height;
  91. }
  92. texAdjust.SetTranslation(Vector3(offset.x_, offset.y_, 0.5f));
  93. texAdjust.SetScale(Vector3(scale.x_, scale.y_, 0.5f));
  94. #else
  95. offset.x_ += scale.x_ + 0.5f / width;
  96. offset.y_ += scale.y_ + 0.5f / height;
  97. if (renderer->GetShadowQuality() & SHADOWQUALITY_HIGH_16BIT)
  98. {
  99. offset.x_ -= 0.5f / width;
  100. offset.y_ -= 0.5f / height;
  101. }
  102. scale.y_ = -scale.y_;
  103. texAdjust.SetTranslation(Vector3(offset.x_, offset.y_, 0.0f));
  104. texAdjust.SetScale(Vector3(scale.x_, scale.y_, 1.0f));
  105. #endif
  106. dest = texAdjust * shadowProj * shadowView * posAdjust;
  107. }
  108. void CalculateSpotMatrix(Matrix4& dest, Light* light, const Vector3& translation)
  109. {
  110. Matrix3x4 posAdjust(translation, Quaternion::IDENTITY, 1.0f);
  111. Matrix3x4 spotView(light->GetWorldPosition(), light->GetWorldRotation(), 1.0f);
  112. Matrix4 spotProj(Matrix4::ZERO);
  113. Matrix4 texAdjust(Matrix4::IDENTITY);
  114. // Make the projected light slightly smaller than the shadow map to prevent light spill
  115. float h = 1.005f / tanf(light->GetFov() * M_DEGTORAD * 0.5f);
  116. float w = h / light->GetAspectRatio();
  117. spotProj.m00_ = w;
  118. spotProj.m11_ = h;
  119. spotProj.m22_ = 1.0f / Max(light->GetRange(), M_EPSILON);
  120. spotProj.m32_ = 1.0f;
  121. #ifdef USE_OPENGL
  122. texAdjust.SetTranslation(Vector3(0.5f, 0.5f, 0.5f));
  123. texAdjust.SetScale(Vector3(0.5f, -0.5f, 0.5f));
  124. #else
  125. texAdjust.SetTranslation(Vector3(0.5f, 0.5f, 0.0f));
  126. texAdjust.SetScale(Vector3(0.5f, -0.5f, 1.0f));
  127. #endif
  128. dest = texAdjust * spotProj * spotView.Inverse() * posAdjust;
  129. }
  130. void Batch::CalculateSortKey()
  131. {
  132. unsigned lightQueue = (*((unsigned*)&lightQueue_) / sizeof(LightBatchQueue)) & 0x7fff;
  133. unsigned pass = (*((unsigned*)&pass_) / sizeof(Pass)) & 0xffff;
  134. unsigned material = (*((unsigned*)&material_) / sizeof(Material)) & 0xffff;
  135. unsigned geometry = (*((unsigned*)&geometry_) / sizeof(Geometry)) & 0xffff;
  136. if (isBase_)
  137. lightQueue |= 0x8000;
  138. sortKey_ = (((unsigned long long)lightQueue) << 48) | (((unsigned long long)pass) << 32) |
  139. (((unsigned long long)material) << 16) | geometry;
  140. }
  141. void Batch::Prepare(Graphics* graphics, Renderer* renderer, bool setModelTransform) const
  142. {
  143. if (!vertexShader_ || !pixelShader_)
  144. return;
  145. // Set pass / material-specific renderstates
  146. if (pass_ && material_)
  147. {
  148. if (pass_->GetAlphaTest())
  149. graphics->SetAlphaTest(true, CMP_GREATEREQUAL, 0.5f);
  150. else
  151. graphics->SetAlphaTest(false);
  152. graphics->SetBlendMode(pass_->GetBlendMode());
  153. renderer->SetCullMode(pass_->GetType() != PASS_SHADOW ? material_->GetCullMode() : material_->GetShadowCullMode(),
  154. camera_);
  155. graphics->SetDepthTest(pass_->GetDepthTestMode());
  156. graphics->SetDepthWrite(pass_->GetDepthWrite());
  157. }
  158. // Set shaders
  159. graphics->SetShaders(vertexShader_, pixelShader_);
  160. // Set viewport and camera shader parameters
  161. if (graphics->NeedParameterUpdate(VSP_CAMERAPOS, camera_))
  162. graphics->SetShaderParameter(VSP_CAMERAPOS, camera_->GetWorldPosition());
  163. if (graphics->NeedParameterUpdate(VSP_CAMERAROT, camera_))
  164. graphics->SetShaderParameter(VSP_CAMERAROT, camera_->GetWorldTransform().RotationMatrix());
  165. if (graphics->NeedParameterUpdate(VSP_DEPTHMODE, camera_))
  166. {
  167. Vector4 depthMode = Vector4::ZERO;
  168. if (camera_->IsOrthographic())
  169. {
  170. depthMode.x_ = 1.0f;
  171. #ifdef USE_OPENGL
  172. depthMode.z_ = 0.5f;
  173. depthMode.w_ = 0.5f;
  174. #else
  175. depthMode.z_ = 1.0f;
  176. #endif
  177. }
  178. else
  179. depthMode.w_ = 1.0f / camera_->GetFarClip();
  180. graphics->SetShaderParameter(VSP_DEPTHMODE, depthMode);
  181. }
  182. if (graphics->NeedParameterUpdate(VSP_FRUSTUMSIZE, camera_))
  183. {
  184. Vector3 nearVector, farVector;
  185. camera_->GetFrustumSize(nearVector, farVector);
  186. Vector4 viewportParams(farVector.x_, farVector.y_, farVector.z_, 0.0f);
  187. graphics->SetShaderParameter(VSP_FRUSTUMSIZE, viewportParams);
  188. }
  189. IntVector2 rtSize = graphics->GetRenderTargetDimensions();
  190. IntRect viewport = graphics->GetViewport();
  191. unsigned viewportHash = (viewport.left_) | (viewport.top_ << 8) | (viewport.right_ << 16) | (viewport.bottom_ << 24);
  192. float rtWidth = (float)rtSize.x_;
  193. float rtHeight = (float)rtSize.y_;
  194. if (graphics->NeedParameterUpdate(VSP_GBUFFEROFFSETS, (const void*)viewportHash))
  195. {
  196. float widthRange = 0.5f * (viewport.right_ - viewport.left_) / rtWidth;
  197. float heightRange = 0.5f * (viewport.bottom_ - viewport.top_) / rtHeight;
  198. #ifdef USE_OPENGL
  199. Vector4 bufferUVOffset(((float)viewport.left_) / rtWidth + widthRange,
  200. 1.0f - (((float)viewport.top_) / rtHeight + heightRange), widthRange, heightRange);
  201. #else
  202. Vector4 bufferUVOffset((0.5f + (float)viewport.left_) / rtWidth + widthRange,
  203. (0.5f + (float)viewport.top_) / rtHeight + heightRange, widthRange, heightRange);
  204. #endif
  205. graphics->SetShaderParameter(VSP_GBUFFEROFFSETS, bufferUVOffset);
  206. }
  207. if (graphics->NeedParameterUpdate(PSP_GBUFFERINVSIZE, (const void*)viewportHash))
  208. {
  209. float sizeX = 1.0f / rtWidth;
  210. float sizeY = 1.0f / rtHeight;
  211. graphics->SetShaderParameter(PSP_GBUFFERINVSIZE, Vector4(sizeX, sizeY, 0.0f, 0.0f));
  212. }
  213. if (overrideView_)
  214. {
  215. if (graphics->NeedParameterUpdate(VSP_VIEWPROJ, ((unsigned char*)camera_) + 4))
  216. graphics->SetShaderParameter(VSP_VIEWPROJ, camera_->GetProjection());
  217. }
  218. else
  219. {
  220. if (graphics->NeedParameterUpdate(VSP_VIEWPROJ, camera_))
  221. graphics->SetShaderParameter(VSP_VIEWPROJ, camera_->GetProjection() *
  222. camera_->GetInverseWorldTransform());
  223. }
  224. if (graphics->NeedParameterUpdate(VSP_VIEWRIGHTVECTOR, camera_))
  225. graphics->SetShaderParameter(VSP_VIEWRIGHTVECTOR, camera_->GetRightVector());
  226. if (graphics->NeedParameterUpdate(VSP_VIEWUPVECTOR, camera_))
  227. graphics->SetShaderParameter(VSP_VIEWUPVECTOR, camera_->GetUpVector());
  228. // Set model transform
  229. if (setModelTransform && graphics->NeedParameterUpdate(VSP_MODEL, worldTransform_))
  230. graphics->SetShaderParameter(VSP_MODEL, *worldTransform_);
  231. // Set skinning transforms
  232. if (shaderData_ && shaderDataSize_)
  233. {
  234. if (graphics->NeedParameterUpdate(VSP_SKINMATRICES, shaderData_))
  235. graphics->SetShaderParameter(VSP_SKINMATRICES, shaderData_, shaderDataSize_);
  236. }
  237. // Set zone-related shader parameters
  238. if (zone_)
  239. {
  240. if (graphics->NeedParameterUpdate(VSP_AMBIENTSTARTCOLOR, zone_))
  241. graphics->SetShaderParameter(VSP_AMBIENTSTARTCOLOR, zone_->GetAmbientStartColor());
  242. if (graphics->NeedParameterUpdate(VSP_AMBIENTENDCOLOR, zone_))
  243. graphics->SetShaderParameter(VSP_AMBIENTENDCOLOR, zone_->GetAmbientEndColor().ToVector4() - zone_->GetAmbientStartColor().ToVector4());
  244. if (graphics->NeedParameterUpdate(VSP_ZONE, zone_))
  245. {
  246. const BoundingBox& box = zone_->GetBoundingBox();
  247. Vector3 boxSize = box.Size();
  248. Matrix3x4 adjust(Matrix3x4::IDENTITY);
  249. adjust.SetScale(Vector3(1.0f / boxSize.x_, 1.0f / boxSize.y_, 1.0f / boxSize.z_));
  250. adjust.SetTranslation(Vector3(0.5f, 0.5f, 0.5f));
  251. Matrix3x4 zoneTransform = adjust * zone_->GetWorldTransform().Inverse();
  252. graphics->SetShaderParameter(VSP_ZONE, zoneTransform);
  253. }
  254. if (graphics->NeedParameterUpdate(PSP_AMBIENTCOLOR, zone_))
  255. graphics->SetShaderParameter(PSP_AMBIENTCOLOR, zone_->GetAmbientColor());
  256. // If the pass is additive, override fog color to black so that shaders do not need a separate additive path
  257. BlendMode blend = pass_->GetBlendMode();
  258. Zone* fogColorZone = (blend == BLEND_ADD || blend == BLEND_ADDALPHA) ? renderer->GetDefaultZone() : zone_;
  259. if (graphics->NeedParameterUpdate(PSP_FOGCOLOR, fogColorZone))
  260. graphics->SetShaderParameter(PSP_FOGCOLOR, fogColorZone->GetFogColor());
  261. if (graphics->NeedParameterUpdate(PSP_FOGPARAMS, zone_))
  262. {
  263. float farClip = camera_->GetFarClip();
  264. float nearClip = camera_->GetNearClip();
  265. float fogStart = Min(zone_->GetFogStart(), farClip);
  266. float fogEnd = Min(zone_->GetFogEnd(), farClip);
  267. if (fogStart >= fogEnd * (1.0f - M_LARGE_EPSILON))
  268. fogStart = fogEnd * (1.0f - M_LARGE_EPSILON);
  269. float fogRange = Max(fogEnd - fogStart, M_EPSILON);
  270. Vector4 fogParams(fogEnd / farClip, farClip / fogRange, 0.0f, 0.0f);
  271. graphics->SetShaderParameter(PSP_FOGPARAMS, fogParams);
  272. }
  273. }
  274. if (graphics->NeedParameterUpdate(PSP_DEPTHRECONSTRUCT, camera_))
  275. {
  276. float farClip = camera_->GetFarClip();
  277. float nearClip = camera_->GetNearClip();
  278. Vector4 depthReconstruct(farClip / (farClip - nearClip), -nearClip / (farClip - nearClip), 0.0f, 0.0f);
  279. graphics->SetShaderParameter(PSP_DEPTHRECONSTRUCT, depthReconstruct);
  280. }
  281. // Set light-related shader parameters
  282. Light* light = 0;
  283. Texture2D* shadowMap = 0;
  284. if (lightQueue_)
  285. {
  286. light = lightQueue_->light_;
  287. shadowMap = lightQueue_->shadowMap_;
  288. if (graphics->NeedParameterUpdate(VSP_LIGHTDIR, light))
  289. graphics->SetShaderParameter(VSP_LIGHTDIR, light->GetWorldRotation() * Vector3::BACK);
  290. if (graphics->NeedParameterUpdate(VSP_LIGHTPOS, light))
  291. {
  292. float atten = 1.0f / Max(light->GetRange(), M_EPSILON);
  293. graphics->SetShaderParameter(VSP_LIGHTPOS, Vector4(light->GetWorldPosition(), atten));
  294. }
  295. if (graphics->NeedParameterUpdate(VSP_LIGHTMATRICES, light))
  296. {
  297. switch (light->GetLightType())
  298. {
  299. case LIGHT_DIRECTIONAL:
  300. {
  301. Matrix4 shadowMatrices[MAX_CASCADE_SPLITS];
  302. unsigned numSplits = lightQueue_->shadowSplits_.Size();
  303. for (unsigned i = 0; i < numSplits; ++i)
  304. CalculateShadowMatrix(shadowMatrices[i], lightQueue_, i, renderer, Vector3::ZERO);
  305. graphics->SetShaderParameter(VSP_LIGHTMATRICES, shadowMatrices[0].Data(), 16 * numSplits);
  306. }
  307. break;
  308. case LIGHT_SPOT:
  309. {
  310. Matrix4 shadowMatrices[2];
  311. CalculateSpotMatrix(shadowMatrices[0], light, Vector3::ZERO);
  312. bool isShadowed = shadowMap && graphics->NeedTextureUnit(TU_SHADOWMAP);
  313. if (isShadowed)
  314. CalculateShadowMatrix(shadowMatrices[1], lightQueue_, 0, renderer, Vector3::ZERO);
  315. graphics->SetShaderParameter(VSP_LIGHTMATRICES, shadowMatrices[0].Data(), isShadowed ? 32 : 16);
  316. }
  317. break;
  318. case LIGHT_POINT:
  319. {
  320. Matrix4 lightVecRot(light->GetWorldRotation().RotationMatrix());
  321. // HLSL compiler will pack the parameters as if the matrix is only 3x4, so must be careful to not overwrite
  322. // the next parameter
  323. #ifdef USE_OPENGL
  324. graphics->SetShaderParameter(VSP_LIGHTMATRICES, lightVecRot.Data(), 16);
  325. #else
  326. graphics->SetShaderParameter(VSP_LIGHTMATRICES, lightVecRot.Data(), 12);
  327. #endif
  328. }
  329. break;
  330. }
  331. }
  332. if (graphics->NeedParameterUpdate(VSP_VERTEXLIGHTS, lightQueue_))
  333. {
  334. Vector4 vertexLights[MAX_VERTEX_LIGHTS * 3];
  335. const PODVector<Light*>& lights = lightQueue_->vertexLights_;
  336. for (unsigned i = 0; i < lights.Size(); ++i)
  337. {
  338. Light* vertexLight = lights[i];
  339. LightType type = vertexLight->GetLightType();
  340. // Attenuation
  341. float invRange, cutoff, invCutoff;
  342. if (type == LIGHT_DIRECTIONAL)
  343. invRange = 0.0f;
  344. else
  345. invRange = 1.0f / Max(vertexLight->GetRange(), M_EPSILON);
  346. if (type == LIGHT_SPOT)
  347. {
  348. cutoff = cosf(vertexLight->GetFov() * 0.5f * M_DEGTORAD);
  349. invCutoff = 1.0f / (1.0f - cutoff);
  350. }
  351. else
  352. {
  353. cutoff = -1.0f;
  354. invCutoff = 1.0f;
  355. }
  356. // Color
  357. float fade = 1.0f;
  358. float fadeEnd = vertexLight->GetDrawDistance();
  359. float fadeStart = vertexLight->GetFadeDistance();
  360. // Do fade calculation for light if both fade & draw distance defined
  361. if (vertexLight->GetLightType() != LIGHT_DIRECTIONAL && fadeEnd > 0.0f && fadeStart > 0.0f && fadeStart < fadeEnd)
  362. fade = Min(1.0f - (vertexLight->GetDistance() - fadeStart) / (fadeEnd - fadeStart), 1.0f);
  363. Color color = vertexLight->GetColor() * fade;
  364. vertexLights[i * 3] = Vector4(color.r_, color.g_, color.b_, invRange);
  365. // Direction
  366. vertexLights[i * 3 + 1] = Vector4(-(vertexLight->GetNode()->GetWorldDirection()), cutoff);
  367. // Position
  368. vertexLights[i * 3 + 2] = Vector4(vertexLight->GetWorldPosition(), invCutoff);
  369. }
  370. if (lights.Size())
  371. graphics->SetShaderParameter(VSP_VERTEXLIGHTS, vertexLights[0].Data(), lights.Size() * 3 * 4);
  372. }
  373. if (graphics->NeedParameterUpdate(PSP_LIGHTCOLOR, light))
  374. {
  375. float fade = 1.0f;
  376. float fadeEnd = light->GetDrawDistance();
  377. float fadeStart = light->GetFadeDistance();
  378. // Do fade calculation for light if both fade & draw distance defined
  379. if (light->GetLightType() != LIGHT_DIRECTIONAL && fadeEnd > 0.0f && fadeStart > 0.0f && fadeStart < fadeEnd)
  380. fade = Min(1.0f - (light->GetDistance() - fadeStart) / (fadeEnd - fadeStart), 1.0f);
  381. graphics->SetShaderParameter(PSP_LIGHTCOLOR, Vector4(light->GetColor().RGBValues(),
  382. light->GetSpecularIntensity()) * fade);
  383. }
  384. if (graphics->NeedParameterUpdate(PSP_LIGHTDIR, light))
  385. graphics->SetShaderParameter(PSP_LIGHTDIR, light->GetWorldRotation() * Vector3::BACK);
  386. if (graphics->NeedParameterUpdate(PSP_LIGHTPOS, light))
  387. {
  388. float atten = 1.0f / Max(light->GetRange(), M_EPSILON);
  389. graphics->SetShaderParameter(PSP_LIGHTPOS, Vector4(light->GetWorldPosition() - camera_->GetWorldPosition(), atten));
  390. }
  391. // Set shadow mapping shader parameters
  392. if (shadowMap)
  393. {
  394. if (graphics->NeedParameterUpdate(PSP_SHADOWCUBEADJUST, light))
  395. {
  396. unsigned faceWidth = shadowMap->GetWidth() / 2;
  397. unsigned faceHeight = shadowMap->GetHeight() / 3;
  398. float width = (float)shadowMap->GetWidth();
  399. float height = (float)shadowMap->GetHeight();
  400. #ifdef USE_OPENGL
  401. float mulX = (float)(faceWidth - 3) / width;
  402. float mulY = (float)(faceHeight - 3) / height;
  403. float addX = 1.5f / width;
  404. float addY = 1.5f / height;
  405. #else
  406. float mulX = (float)(faceWidth - 4) / width;
  407. float mulY = (float)(faceHeight - 4) / height;
  408. float addX = 2.5f / width;
  409. float addY = 2.5f / height;
  410. #endif
  411. // If using 4 shadow samples, offset the position diagonally by half pixel
  412. if (renderer->GetShadowQuality() & SHADOWQUALITY_HIGH_16BIT)
  413. {
  414. addX -= 0.5f / width;
  415. addY -= 0.5f / height;
  416. }
  417. graphics->SetShaderParameter(PSP_SHADOWCUBEADJUST, Vector4(mulX, mulY, addX, addY));
  418. }
  419. if (graphics->NeedParameterUpdate(PSP_SHADOWDEPTHFADE, light))
  420. {
  421. Camera* shadowCamera = lightQueue_->shadowSplits_[0].shadowCamera_;
  422. float nearClip = shadowCamera->GetNearClip();
  423. float farClip = shadowCamera->GetFarClip();
  424. float q = farClip / (farClip - nearClip);
  425. float r = -q * nearClip;
  426. const CascadeParameters& parameters = light->GetShadowCascade();
  427. float viewFarClip = camera_->GetFarClip();
  428. float shadowRange = parameters.GetShadowRange();
  429. float fadeStart = parameters.fadeStart_ * shadowRange / viewFarClip;
  430. float fadeEnd = shadowRange / viewFarClip;
  431. float fadeRange = fadeEnd - fadeStart;
  432. graphics->SetShaderParameter(PSP_SHADOWDEPTHFADE, Vector4(q, r, fadeStart, 1.0f / fadeRange));
  433. }
  434. if (graphics->NeedParameterUpdate(PSP_SHADOWINTENSITY, light))
  435. {
  436. float intensity = light->GetShadowIntensity();
  437. float fadeStart = light->GetShadowFadeDistance();
  438. float fadeEnd = light->GetShadowDistance();
  439. if (fadeStart > 0.0f && fadeEnd > 0.0f && fadeEnd > fadeStart)
  440. intensity = Lerp(intensity, 1.0f, Clamp((light->GetDistance() - fadeStart) / (fadeEnd - fadeStart), 0.0f, 1.0f));
  441. float pcfValues = (1.0f - intensity);
  442. float samples = renderer->GetShadowQuality() >= SHADOWQUALITY_HIGH_16BIT ? 4.0f : 1.0f;
  443. graphics->SetShaderParameter(PSP_SHADOWINTENSITY, Vector4(pcfValues / samples, intensity, 0.0f, 0.0f));
  444. }
  445. if (graphics->NeedParameterUpdate(PSP_SHADOWMAPINVSIZE, shadowMap))
  446. {
  447. float sizeX = 1.0f / (float)shadowMap->GetWidth();
  448. float sizeY = 1.0f / (float)shadowMap->GetHeight();
  449. graphics->SetShaderParameter(PSP_SHADOWMAPINVSIZE, Vector4(sizeX, sizeY, 0.0f, 0.0f));
  450. }
  451. if (graphics->NeedParameterUpdate(PSP_SHADOWSPLITS, light))
  452. {
  453. Vector4 lightSplits(M_LARGE_VALUE, M_LARGE_VALUE, M_LARGE_VALUE, M_LARGE_VALUE);
  454. if (lightQueue_->shadowSplits_.Size() > 1)
  455. lightSplits.x_ = lightQueue_->shadowSplits_[0].farSplit_ / camera_->GetFarClip();
  456. if (lightQueue_->shadowSplits_.Size() > 2)
  457. lightSplits.y_ = lightQueue_->shadowSplits_[1].farSplit_ / camera_->GetFarClip();
  458. if (lightQueue_->shadowSplits_.Size() > 3)
  459. lightSplits.z_ = lightQueue_->shadowSplits_[2].farSplit_ / camera_->GetFarClip();
  460. graphics->SetShaderParameter(PSP_SHADOWSPLITS, lightSplits);
  461. }
  462. }
  463. if (graphics->NeedParameterUpdate(PSP_LIGHTMATRICES, light))
  464. {
  465. switch (light->GetLightType())
  466. {
  467. case LIGHT_DIRECTIONAL:
  468. {
  469. Matrix4 shadowMatrices[MAX_CASCADE_SPLITS];
  470. unsigned numSplits = lightQueue_->shadowSplits_.Size();
  471. for (unsigned i = 0; i < numSplits; ++i)
  472. CalculateShadowMatrix(shadowMatrices[i], lightQueue_, i, renderer, camera_->GetWorldPosition());
  473. graphics->SetShaderParameter(PSP_LIGHTMATRICES, shadowMatrices[0].Data(), 16 * numSplits);
  474. }
  475. break;
  476. case LIGHT_SPOT:
  477. {
  478. Matrix4 shadowMatrices[2];
  479. CalculateSpotMatrix(shadowMatrices[0], light, camera_->GetWorldPosition());
  480. bool isShadowed = lightQueue_->shadowMap_ != 0;
  481. if (isShadowed)
  482. CalculateShadowMatrix(shadowMatrices[1], lightQueue_, 0, renderer, camera_->GetWorldPosition());
  483. graphics->SetShaderParameter(PSP_LIGHTMATRICES, shadowMatrices[0].Data(), isShadowed ? 32 : 16);
  484. }
  485. break;
  486. case LIGHT_POINT:
  487. {
  488. Matrix4 lightVecRot(light->GetWorldRotation().RotationMatrix());
  489. // HLSL compiler will pack the parameters as if the matrix is only 3x4, so must be careful to not overwrite
  490. // the next parameter
  491. #ifdef USE_OPENGL
  492. graphics->SetShaderParameter(PSP_LIGHTMATRICES, lightVecRot.Data(), 16);
  493. #else
  494. graphics->SetShaderParameter(PSP_LIGHTMATRICES, lightVecRot.Data(), 12);
  495. #endif
  496. }
  497. break;
  498. }
  499. }
  500. }
  501. // Set material-specific shader parameters and textures
  502. if (material_)
  503. {
  504. const HashMap<StringHash, MaterialShaderParameter>& parameters = material_->GetShaderParameters();
  505. for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = parameters.Begin(); i != parameters.End(); ++i)
  506. {
  507. if (graphics->NeedParameterUpdate(i->first_, material_))
  508. graphics->SetShaderParameter(i->first_, i->second_.value_);
  509. }
  510. const SharedPtr<Texture>* textures = material_->GetTextures();
  511. if (graphics->NeedTextureUnit(TU_DIFFUSE))
  512. graphics->SetTexture(TU_DIFFUSE, textures[TU_DIFFUSE]);
  513. if (graphics->NeedTextureUnit(TU_NORMAL))
  514. graphics->SetTexture(TU_NORMAL, textures[TU_NORMAL]);
  515. if (graphics->NeedTextureUnit(TU_SPECULAR))
  516. graphics->SetTexture(TU_NORMAL, textures[TU_SPECULAR]);
  517. if (graphics->NeedTextureUnit(TU_DETAIL))
  518. graphics->SetTexture(TU_DETAIL, textures[TU_DETAIL]);
  519. if (graphics->NeedTextureUnit(TU_ENVIRONMENT))
  520. graphics->SetTexture(TU_ENVIRONMENT, textures[TU_ENVIRONMENT]);
  521. }
  522. // Set light-related textures
  523. if (light)
  524. {
  525. if (shadowMap && graphics->NeedTextureUnit(TU_SHADOWMAP))
  526. graphics->SetTexture(TU_SHADOWMAP, shadowMap);
  527. if (graphics->NeedTextureUnit(TU_LIGHTRAMP))
  528. {
  529. Texture* rampTexture = light->GetRampTexture();
  530. if (!rampTexture)
  531. rampTexture = renderer->GetDefaultLightRamp();
  532. graphics->SetTexture(TU_LIGHTRAMP, rampTexture);
  533. }
  534. if (graphics->NeedTextureUnit(TU_LIGHTSHAPE))
  535. {
  536. Texture* shapeTexture = light->GetShapeTexture();
  537. if (!shapeTexture && light->GetLightType() == LIGHT_SPOT)
  538. shapeTexture = renderer->GetDefaultLightSpot();
  539. graphics->SetTexture(TU_LIGHTSHAPE, shapeTexture);
  540. }
  541. }
  542. }
  543. void Batch::Draw(Graphics* graphics, Renderer* renderer) const
  544. {
  545. Prepare(graphics, renderer);
  546. geometry_->Draw(graphics);
  547. }
  548. void BatchGroup::SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex)
  549. {
  550. // Do not use up buffer space if not going to draw as instanced
  551. if (geometry_->GetIndexCount() > (unsigned)renderer->GetMaxInstanceTriangles() * 3)
  552. return;
  553. startIndex_ = freeIndex;
  554. Matrix3x4* dest = (Matrix3x4*)lockedData;
  555. dest += freeIndex;
  556. for (unsigned i = 0; i < instances_.Size(); ++i)
  557. *dest++ = *instances_[i].worldTransform_;
  558. freeIndex += instances_.Size();
  559. }
  560. void BatchGroup::Draw(Graphics* graphics, Renderer* renderer) const
  561. {
  562. if (!instances_.Size())
  563. return;
  564. // Draw as individual objects if instancing not supported
  565. VertexBuffer* instanceBuffer = renderer->GetInstancingBuffer();
  566. if (!instanceBuffer || geometry_->GetIndexCount() > (unsigned)renderer->GetMaxInstanceTriangles() * 3)
  567. {
  568. Batch::Prepare(graphics, renderer, false);
  569. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  570. graphics->SetVertexBuffers(geometry_->GetVertexBuffers(), geometry_->GetVertexElementMasks());
  571. for (unsigned i = 0; i < instances_.Size(); ++i)
  572. {
  573. graphics->SetShaderParameter(VSP_MODEL, *instances_[i].worldTransform_);
  574. graphics->Draw(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  575. geometry_->GetVertexStart(), geometry_->GetVertexCount());
  576. }
  577. graphics->ClearTransformSources();
  578. }
  579. else
  580. {
  581. Batch::Prepare(graphics, renderer, false);
  582. // Get the geometry vertex buffers, then add the instancing stream buffer
  583. // Hack: use a const_cast to avoid dynamic allocation of new temp vectors
  584. Vector<SharedPtr<VertexBuffer> >& vertexBuffers = const_cast<Vector<SharedPtr<VertexBuffer> >&>
  585. (geometry_->GetVertexBuffers());
  586. PODVector<unsigned>& elementMasks = const_cast<PODVector<unsigned>&>(geometry_->GetVertexElementMasks());
  587. vertexBuffers.Push(SharedPtr<VertexBuffer>(instanceBuffer));
  588. elementMasks.Push(instanceBuffer->GetElementMask());
  589. // No stream offset support, instancing buffer not pre-filled with transforms: have to lock and fill now
  590. if (startIndex_ == M_MAX_UNSIGNED)
  591. {
  592. unsigned startIndex = 0;
  593. while (startIndex < instances_.Size())
  594. {
  595. unsigned instances = instances_.Size() - startIndex;
  596. if (instances > instanceBuffer->GetVertexCount())
  597. instances = instanceBuffer->GetVertexCount();
  598. // Lock the instance stream buffer and copy the transforms
  599. void* data = instanceBuffer->Lock(0, instances, LOCK_DISCARD);
  600. if (!data)
  601. {
  602. // Remember to remove the instancing buffer and element mask
  603. vertexBuffers.Pop();
  604. elementMasks.Pop();
  605. return;
  606. }
  607. Matrix3x4* dest = (Matrix3x4*)data;
  608. for (unsigned i = 0; i < instances; ++i)
  609. dest[i] = *instances_[i + startIndex].worldTransform_;
  610. instanceBuffer->Unlock();
  611. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  612. graphics->SetVertexBuffers(vertexBuffers, elementMasks);
  613. graphics->DrawInstanced(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  614. geometry_->GetVertexStart(), geometry_->GetVertexCount(), instances);
  615. startIndex += instances;
  616. }
  617. }
  618. // Stream offset supported, and instancing buffer has been already filled, so just draw
  619. else
  620. {
  621. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  622. graphics->SetVertexBuffers(vertexBuffers, elementMasks, startIndex_);
  623. graphics->DrawInstanced(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  624. geometry_->GetVertexStart(), geometry_->GetVertexCount(), instances_.Size());
  625. }
  626. // Remove the instancing buffer & element mask now
  627. vertexBuffers.Pop();
  628. elementMasks.Pop();
  629. }
  630. }
  631. void BatchQueue::Clear()
  632. {
  633. batches_.Clear();
  634. sortedBaseBatches_.Clear();
  635. sortedBatches_.Clear();
  636. baseBatchGroups_.Clear();
  637. batchGroups_.Clear();
  638. }
  639. void BatchQueue::AddBatch(const Batch& batch)
  640. {
  641. // Important: this function does not check whether the batch can actually be instanced. It must have been checked before,
  642. // including setting the correct vertex shader (non-instanced or instanced)
  643. if (batch.geometryType_ != GEOM_INSTANCED)
  644. batches_.Push(batch);
  645. else
  646. {
  647. Map<BatchGroupKey, BatchGroup>* groups = batch.isBase_ ? &baseBatchGroups_ : &batchGroups_;
  648. BatchGroupKey key(batch);
  649. Map<BatchGroupKey, BatchGroup>::Iterator i = groups->Find(key);
  650. if (i == groups->End())
  651. {
  652. // Create a new group based on the batch
  653. BatchGroup newGroup(batch);
  654. newGroup.instances_.Push(InstanceData(batch.worldTransform_, batch.distance_));
  655. groups->Insert(MakePair(key, newGroup));
  656. }
  657. else
  658. i->second_.instances_.Push(InstanceData(batch.worldTransform_, batch.distance_));
  659. }
  660. }
  661. void BatchQueue::SortBackToFront()
  662. {
  663. sortedBaseBatches_.Clear();
  664. sortedBatches_.Resize(batches_.Size());
  665. for (unsigned i = 0; i < batches_.Size(); ++i)
  666. sortedBatches_[i] = &batches_[i];
  667. Sort(sortedBatches_.Begin(), sortedBatches_.End(), CompareBatchesBackToFront);
  668. // Do not actually sort batch groups, just list them
  669. sortedBaseBatchGroups_.Resize(baseBatchGroups_.Size());
  670. sortedBatchGroups_.Resize(batchGroups_.Size());
  671. unsigned index = 0;
  672. for (Map<BatchGroupKey, BatchGroup>::Iterator i = baseBatchGroups_.Begin(); i != baseBatchGroups_.End(); ++i)
  673. sortedBaseBatchGroups_[index++] = &i->second_;
  674. index = 0;
  675. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  676. sortedBatchGroups_[index++] = &i->second_;
  677. }
  678. void BatchQueue::SortFrontToBack()
  679. {
  680. sortedBaseBatches_.Clear();
  681. sortedBatches_.Clear();
  682. // Must explicitly divide into base and non-base batches, so that priorities do not get mixed up between
  683. // instanced and non-instanced batches
  684. for (unsigned i = 0; i < batches_.Size(); ++i)
  685. {
  686. if (batches_[i].isBase_)
  687. sortedBaseBatches_.Push(&batches_[i]);
  688. else
  689. sortedBatches_.Push(&batches_[i]);
  690. }
  691. Sort(sortedBaseBatches_.Begin(), sortedBaseBatches_.End(), CompareBatchesFrontToBack);
  692. Sort(sortedBatches_.Begin(), sortedBatches_.End(), CompareBatchesFrontToBack);
  693. // Sort each group front to back
  694. for (Map<BatchGroupKey, BatchGroup>::Iterator i = baseBatchGroups_.Begin(); i != baseBatchGroups_.End(); ++i)
  695. Sort(i->second_.instances_.Begin(), i->second_.instances_.End(), CompareInstancesFrontToBack);
  696. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  697. Sort(i->second_.instances_.Begin(), i->second_.instances_.End(), CompareInstancesFrontToBack);
  698. // Now sort batch groups by the distance of the first batch
  699. sortedBaseBatchGroups_.Resize(baseBatchGroups_.Size());
  700. sortedBatchGroups_.Resize(batchGroups_.Size());
  701. unsigned index = 0;
  702. for (Map<BatchGroupKey, BatchGroup>::Iterator i = baseBatchGroups_.Begin(); i != baseBatchGroups_.End(); ++i)
  703. sortedBaseBatchGroups_[index++] = &i->second_;
  704. index = 0;
  705. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  706. sortedBatchGroups_[index++] = &i->second_;
  707. Sort(sortedBaseBatchGroups_.Begin(), sortedBaseBatchGroups_.End(), CompareBatchGroupsFrontToBack);
  708. Sort(sortedBatchGroups_.Begin(), sortedBatchGroups_.End(), CompareBatchGroupsFrontToBack);
  709. }
  710. void BatchQueue::SetTransforms(Renderer* renderer, void* lockedData, unsigned& freeIndex)
  711. {
  712. for (Map<BatchGroupKey, BatchGroup>::Iterator i = baseBatchGroups_.Begin(); i != baseBatchGroups_.End(); ++i)
  713. i->second_.SetTransforms(renderer, lockedData, freeIndex);
  714. for (Map<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  715. i->second_.SetTransforms(renderer, lockedData, freeIndex);
  716. }
  717. void BatchQueue::Draw(Graphics* graphics, Renderer* renderer, bool useScissor, bool markToStencil) const
  718. {
  719. graphics->SetScissorTest(false);
  720. // During G-buffer rendering, mark opaque pixels to stencil buffer
  721. if (!markToStencil)
  722. graphics->SetStencilTest(false);
  723. // Base instanced
  724. for (PODVector<BatchGroup*>::ConstIterator i = sortedBaseBatchGroups_.Begin(); i != sortedBaseBatchGroups_.End(); ++i)
  725. {
  726. BatchGroup* group = *i;
  727. if (markToStencil)
  728. graphics->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_KEEP, OP_KEEP, group->lightMask_);
  729. group->Draw(graphics, renderer);
  730. }
  731. // Base non-instanced
  732. for (PODVector<Batch*>::ConstIterator i = sortedBaseBatches_.Begin(); i != sortedBaseBatches_.End(); ++i)
  733. {
  734. Batch* batch = *i;
  735. if (markToStencil)
  736. graphics->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_KEEP, OP_KEEP, batch->lightMask_);
  737. batch->Draw(graphics, renderer);
  738. }
  739. // Non-base instanced
  740. for (PODVector<BatchGroup*>::ConstIterator i = sortedBatchGroups_.Begin(); i != sortedBatchGroups_.End(); ++i)
  741. {
  742. BatchGroup* group = *i;
  743. if (useScissor && group->lightQueue_)
  744. renderer->OptimizeLightByScissor(group->lightQueue_->light_, group->camera_);
  745. if (markToStencil)
  746. graphics->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_KEEP, OP_KEEP, group->lightMask_);
  747. group->Draw(graphics, renderer);
  748. }
  749. // Non-base non-instanced
  750. for (PODVector<Batch*>::ConstIterator i = sortedBatches_.Begin(); i != sortedBatches_.End(); ++i)
  751. {
  752. Batch* batch = *i;
  753. if (useScissor)
  754. {
  755. if (!batch->isBase_ && batch->lightQueue_)
  756. renderer->OptimizeLightByScissor(batch->lightQueue_->light_, batch->camera_);
  757. else
  758. graphics->SetScissorTest(false);
  759. }
  760. if (markToStencil)
  761. graphics->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_KEEP, OP_KEEP, batch->lightMask_);
  762. batch->Draw(graphics, renderer);
  763. }
  764. }
  765. void BatchQueue::Draw(Light* light, Graphics* graphics, Renderer* renderer) const
  766. {
  767. graphics->SetScissorTest(false);
  768. graphics->SetStencilTest(false);
  769. // Base instanced
  770. for (PODVector<BatchGroup*>::ConstIterator i = sortedBaseBatchGroups_.Begin(); i != sortedBaseBatchGroups_.End(); ++i)
  771. {
  772. BatchGroup* group = *i;
  773. group->Draw(graphics, renderer);
  774. }
  775. // Base non-instanced
  776. for (PODVector<Batch*>::ConstIterator i = sortedBaseBatches_.Begin(); i != sortedBaseBatches_.End(); ++i)
  777. {
  778. Batch* batch = *i;
  779. batch->Draw(graphics, renderer);
  780. }
  781. // All base passes have been drawn. Optimize at this point by both stencil volume and scissor
  782. bool optimized = false;
  783. // Non-base instanced
  784. for (PODVector<BatchGroup*>::ConstIterator i = sortedBatchGroups_.Begin(); i != sortedBatchGroups_.End(); ++i)
  785. {
  786. BatchGroup* group = *i;
  787. if (!optimized)
  788. {
  789. renderer->OptimizeLightByStencil(light, group->camera_);
  790. renderer->OptimizeLightByScissor(light, group->camera_);
  791. optimized = true;
  792. }
  793. group->Draw(graphics, renderer);
  794. }
  795. // Non-base non-instanced
  796. for (PODVector<Batch*>::ConstIterator i = sortedBatches_.Begin(); i != sortedBatches_.End(); ++i)
  797. {
  798. Batch* batch = *i;
  799. if (!optimized)
  800. {
  801. renderer->OptimizeLightByStencil(light, batch->camera_);
  802. renderer->OptimizeLightByScissor(light, batch->camera_);
  803. optimized = true;
  804. }
  805. batch->Draw(graphics, renderer);
  806. }
  807. }
  808. unsigned BatchQueue::GetNumInstances(Renderer* renderer) const
  809. {
  810. unsigned total = 0;
  811. unsigned maxIndexCount = renderer->GetMaxInstanceTriangles() * 3;
  812. // This is for the purpose of calculating how much space is needed in the instancing buffer. Do not add groups
  813. // that have too many triangles
  814. for (Map<BatchGroupKey, BatchGroup>::ConstIterator i = baseBatchGroups_.Begin(); i != baseBatchGroups_.End(); ++i)
  815. {
  816. if (i->second_.geometry_->GetIndexCount() <= maxIndexCount)
  817. total += i->second_.instances_.Size();
  818. }
  819. for (Map<BatchGroupKey, BatchGroup>::ConstIterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  820. {
  821. if (i->second_.geometry_->GetIndexCount() <= maxIndexCount)
  822. total += i->second_.instances_.Size();
  823. }
  824. return total;
  825. }