Batch.cpp 32 KB

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