Batch.cpp 41 KB

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