Batch.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Graphics/Camera.h"
  24. #include "../Graphics/Geometry.h"
  25. #include "../Graphics/Graphics.h"
  26. #include "../Graphics/GraphicsImpl.h"
  27. #include "../Graphics/Material.h"
  28. #include "../Graphics/Renderer.h"
  29. #include "../Graphics/ShaderVariation.h"
  30. #include "../Graphics/Technique.h"
  31. #include "../Graphics/Texture2D.h"
  32. #include "../Graphics/VertexBuffer.h"
  33. #include "../Graphics/View.h"
  34. #include "../Scene/Scene.h"
  35. #include "../DebugNew.h"
  36. namespace Atomic
  37. {
  38. inline bool CompareBatchesState(Batch* lhs, Batch* rhs)
  39. {
  40. if (lhs->sortKey_ != rhs->sortKey_)
  41. return lhs->sortKey_ < rhs->sortKey_;
  42. else
  43. return lhs->distance_ < rhs->distance_;
  44. }
  45. inline bool CompareBatchesFrontToBack(Batch* lhs, Batch* rhs)
  46. {
  47. if (lhs->distance_ != rhs->distance_)
  48. return lhs->distance_ < rhs->distance_;
  49. else
  50. return lhs->sortKey_ < rhs->sortKey_;
  51. }
  52. inline bool CompareBatchesBackToFront(Batch* lhs, Batch* rhs)
  53. {
  54. if (lhs->distance_ != rhs->distance_)
  55. return lhs->distance_ > rhs->distance_;
  56. else
  57. return lhs->sortKey_ < rhs->sortKey_;
  58. }
  59. inline bool CompareInstancesFrontToBack(const InstanceData& lhs, const InstanceData& rhs)
  60. {
  61. return lhs.distance_ < rhs.distance_;
  62. }
  63. void CalculateShadowMatrix(Matrix4& dest, LightBatchQueue* queue, unsigned split, Renderer* renderer, const Vector3& translation)
  64. {
  65. Camera* shadowCamera = queue->shadowSplits_[split].shadowCamera_;
  66. const IntRect& viewport = queue->shadowSplits_[split].shadowViewport_;
  67. Matrix3x4 posAdjust(translation, Quaternion::IDENTITY, 1.0f);
  68. Matrix3x4 shadowView(shadowCamera->GetView());
  69. Matrix4 shadowProj(shadowCamera->GetProjection());
  70. Matrix4 texAdjust(Matrix4::IDENTITY);
  71. Texture2D* shadowMap = queue->shadowMap_;
  72. if (!shadowMap)
  73. return;
  74. float width = (float)shadowMap->GetWidth();
  75. float height = (float)shadowMap->GetHeight();
  76. Vector3 offset(
  77. (float)viewport.left_ / width,
  78. (float)viewport.top_ / height,
  79. 0.0f
  80. );
  81. Vector3 scale(
  82. 0.5f * (float)viewport.Width() / width,
  83. 0.5f * (float)viewport.Height() / height,
  84. 1.0f
  85. );
  86. // Add pixel-perfect offset if needed by the graphics API
  87. const Vector2& pixelUVOffset = Graphics::GetPixelUVOffset();
  88. offset.x_ += scale.x_ + pixelUVOffset.x_ / width;
  89. offset.y_ += scale.y_ + pixelUVOffset.y_ / height;
  90. #ifdef ATOMIC_OPENGL
  91. offset.z_ = 0.5f;
  92. scale.z_ = 0.5f;
  93. offset.y_ = 1.0f - offset.y_;
  94. #else
  95. scale.y_ = -scale.y_;
  96. #endif
  97. // If using 4 shadow samples, offset the position diagonally by half pixel
  98. if (renderer->GetShadowQuality() & SHADOWQUALITY_HIGH_16BIT)
  99. {
  100. offset.x_ -= 0.5f / width;
  101. offset.y_ -= 0.5f / height;
  102. }
  103. texAdjust.SetTranslation(offset);
  104. texAdjust.SetScale(scale);
  105. dest = texAdjust * shadowProj * shadowView * posAdjust;
  106. }
  107. void CalculateSpotMatrix(Matrix4& dest, Light* light, const Vector3& translation)
  108. {
  109. Node* lightNode = light->GetNode();
  110. Matrix3x4 posAdjust(translation, Quaternion::IDENTITY, 1.0f);
  111. Matrix3x4 spotView = Matrix3x4(lightNode->GetWorldPosition(), lightNode->GetWorldRotation(), 1.0f).Inverse();
  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 ATOMIC_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 * posAdjust;
  129. }
  130. void Batch::CalculateSortKey()
  131. {
  132. unsigned shaderID = (unsigned)(
  133. ((*((unsigned*)&vertexShader_) / sizeof(ShaderVariation)) + (*((unsigned*)&pixelShader_) / sizeof(ShaderVariation))) &
  134. 0x3fff);
  135. if (!isBase_)
  136. shaderID |= 0x8000;
  137. if (pass_ && pass_->GetAlphaMask())
  138. shaderID |= 0x4000;
  139. unsigned lightQueueID = (unsigned)((*((unsigned*)&lightQueue_) / sizeof(LightBatchQueue)) & 0xffff);
  140. unsigned materialID = (unsigned)((*((unsigned*)&material_) / sizeof(Material)) & 0xffff);
  141. unsigned geometryID = (unsigned)((*((unsigned*)&geometry_) / sizeof(Geometry)) & 0xffff);
  142. sortKey_ = (((unsigned long long)shaderID) << 48) | (((unsigned long long)lightQueueID) << 32) |
  143. (((unsigned long long)materialID) << 16) | geometryID;
  144. }
  145. void Batch::Prepare(View* view, bool setModelTransform, bool allowDepthWrite) const
  146. {
  147. if (!vertexShader_ || !pixelShader_)
  148. return;
  149. Graphics* graphics = view->GetGraphics();
  150. Renderer* renderer = view->GetRenderer();
  151. Node* cameraNode = camera_ ? camera_->GetNode() : 0;
  152. Light* light = lightQueue_ ? lightQueue_->light_ : 0;
  153. Texture2D* shadowMap = lightQueue_ ? lightQueue_->shadowMap_ : 0;
  154. // Set shaders first. The available shader parameters and their register/uniform positions depend on the currently set shaders
  155. graphics->SetShaders(vertexShader_, pixelShader_);
  156. // Set pass / material-specific renderstates
  157. if (pass_ && material_)
  158. {
  159. BlendMode blend = pass_->GetBlendMode();
  160. // Turn additive blending into subtract if the light is negative
  161. if (light && light->IsNegative())
  162. {
  163. if (blend == BLEND_ADD)
  164. blend = BLEND_SUBTRACT;
  165. else if (blend == BLEND_ADDALPHA)
  166. blend = BLEND_SUBTRACTALPHA;
  167. }
  168. graphics->SetBlendMode(blend);
  169. bool isShadowPass = pass_->GetIndex() == Technique::shadowPassIndex;
  170. renderer->SetCullMode(isShadowPass ? material_->GetShadowCullMode() : material_->GetCullMode(), camera_);
  171. if (!isShadowPass)
  172. {
  173. const BiasParameters& depthBias = material_->GetDepthBias();
  174. graphics->SetDepthBias(depthBias.constantBias_, depthBias.slopeScaledBias_);
  175. }
  176. // Use the "least filled" fill mode combined from camera & material
  177. graphics->SetFillMode((FillMode)(Max(camera_->GetFillMode(), material_->GetFillMode())));
  178. graphics->SetDepthTest(pass_->GetDepthTestMode());
  179. graphics->SetDepthWrite(pass_->GetDepthWrite() && allowDepthWrite);
  180. }
  181. // Set global (per-frame) shader parameters
  182. if (graphics->NeedParameterUpdate(SP_FRAME, (void*)0))
  183. view->SetGlobalShaderParameters();
  184. // Set camera & viewport shader parameters
  185. unsigned cameraHash = (unsigned)(size_t)camera_;
  186. IntRect viewport = graphics->GetViewport();
  187. IntVector2 viewSize = IntVector2(viewport.Width(), viewport.Height());
  188. unsigned viewportHash = (unsigned)(viewSize.x_ | (viewSize.y_ << 16));
  189. if (graphics->NeedParameterUpdate(SP_CAMERA, reinterpret_cast<const void*>(cameraHash + viewportHash)))
  190. {
  191. view->SetCameraShaderParameters(camera_, true);
  192. // During renderpath commands the G-Buffer or viewport texture is assumed to always be viewport-sized
  193. view->SetGBufferShaderParameters(viewSize, IntRect(0, 0, viewSize.x_, viewSize.y_));
  194. }
  195. // Set model or skinning transforms
  196. if (setModelTransform && graphics->NeedParameterUpdate(SP_OBJECT, worldTransform_))
  197. {
  198. if (geometryType_ == GEOM_SKINNED)
  199. {
  200. graphics->SetShaderParameter(VSP_SKINMATRICES, reinterpret_cast<const float*>(worldTransform_),
  201. 12 * numWorldTransforms_);
  202. }
  203. else
  204. graphics->SetShaderParameter(VSP_MODEL, *worldTransform_);
  205. // Set the orientation for billboards, either from the object itself or from the camera
  206. if (geometryType_ == GEOM_BILLBOARD)
  207. {
  208. if (numWorldTransforms_ > 1)
  209. graphics->SetShaderParameter(VSP_BILLBOARDROT, worldTransform_[1].RotationMatrix());
  210. else
  211. graphics->SetShaderParameter(VSP_BILLBOARDROT, cameraNode->GetWorldRotation().RotationMatrix());
  212. }
  213. }
  214. // Set zone-related shader parameters
  215. BlendMode blend = graphics->GetBlendMode();
  216. // If the pass is additive, override fog color to black so that shaders do not need a separate additive path
  217. bool overrideFogColorToBlack = blend == BLEND_ADD || blend == BLEND_ADDALPHA;
  218. unsigned zoneHash = (unsigned)(size_t)zone_;
  219. if (overrideFogColorToBlack)
  220. zoneHash += 0x80000000;
  221. if (zone_ && graphics->NeedParameterUpdate(SP_ZONE, reinterpret_cast<const void*>(zoneHash)))
  222. {
  223. graphics->SetShaderParameter(VSP_AMBIENTSTARTCOLOR, zone_->GetAmbientStartColor());
  224. graphics->SetShaderParameter(VSP_AMBIENTENDCOLOR,
  225. zone_->GetAmbientEndColor().ToVector4() - zone_->GetAmbientStartColor().ToVector4());
  226. const BoundingBox& box = zone_->GetBoundingBox();
  227. Vector3 boxSize = box.Size();
  228. Matrix3x4 adjust(Matrix3x4::IDENTITY);
  229. adjust.SetScale(Vector3(1.0f / boxSize.x_, 1.0f / boxSize.y_, 1.0f / boxSize.z_));
  230. adjust.SetTranslation(Vector3(0.5f, 0.5f, 0.5f));
  231. Matrix3x4 zoneTransform = adjust * zone_->GetInverseWorldTransform();
  232. graphics->SetShaderParameter(VSP_ZONE, zoneTransform);
  233. graphics->SetShaderParameter(PSP_AMBIENTCOLOR, zone_->GetAmbientColor());
  234. graphics->SetShaderParameter(PSP_FOGCOLOR, overrideFogColorToBlack ? Color::BLACK : zone_->GetFogColor());
  235. float farClip = camera_->GetFarClip();
  236. float fogStart = Min(zone_->GetFogStart(), farClip);
  237. float fogEnd = Min(zone_->GetFogEnd(), farClip);
  238. if (fogStart >= fogEnd * (1.0f - M_LARGE_EPSILON))
  239. fogStart = fogEnd * (1.0f - M_LARGE_EPSILON);
  240. float fogRange = Max(fogEnd - fogStart, M_EPSILON);
  241. Vector4 fogParams(fogEnd / farClip, farClip / fogRange, 0.0f, 0.0f);
  242. Node* zoneNode = zone_->GetNode();
  243. if (zone_->GetHeightFog() && zoneNode)
  244. {
  245. Vector3 worldFogHeightVec = zoneNode->GetWorldTransform() * Vector3(0.0f, zone_->GetFogHeight(), 0.0f);
  246. fogParams.z_ = worldFogHeightVec.y_;
  247. fogParams.w_ = zone_->GetFogHeightScale() / Max(zoneNode->GetWorldScale().y_, M_EPSILON);
  248. }
  249. graphics->SetShaderParameter(PSP_FOGPARAMS, fogParams);
  250. }
  251. // Set light-related shader parameters
  252. if (lightQueue_)
  253. {
  254. if (light && graphics->NeedParameterUpdate(SP_LIGHT, lightQueue_))
  255. {
  256. // Deferred light volume batches operate in a camera-centered space. Detect from material, zone & pass all being null
  257. bool isLightVolume = !material_ && !pass_ && !zone_;
  258. Matrix3x4 cameraEffectiveTransform = camera_->GetEffectiveWorldTransform();
  259. Vector3 cameraEffectivePos = cameraEffectiveTransform.Translation();
  260. Node* lightNode = light->GetNode();
  261. Matrix3 lightWorldRotation = lightNode->GetWorldRotation().RotationMatrix();
  262. graphics->SetShaderParameter(VSP_LIGHTDIR, lightWorldRotation * Vector3::BACK);
  263. float atten = 1.0f / Max(light->GetRange(), M_EPSILON);
  264. graphics->SetShaderParameter(VSP_LIGHTPOS, Vector4(lightNode->GetWorldPosition(), atten));
  265. if (graphics->HasShaderParameter(VSP_LIGHTMATRICES))
  266. {
  267. switch (light->GetLightType())
  268. {
  269. case LIGHT_DIRECTIONAL:
  270. {
  271. Matrix4 shadowMatrices[MAX_CASCADE_SPLITS];
  272. unsigned numSplits = (unsigned)Min(MAX_CASCADE_SPLITS, (int)lightQueue_->shadowSplits_.Size());
  273. for (unsigned i = 0; i < numSplits; ++i)
  274. CalculateShadowMatrix(shadowMatrices[i], lightQueue_, i, renderer, Vector3::ZERO);
  275. graphics->SetShaderParameter(VSP_LIGHTMATRICES, shadowMatrices[0].Data(), 16 * numSplits);
  276. }
  277. break;
  278. case LIGHT_SPOT:
  279. {
  280. Matrix4 shadowMatrices[2];
  281. CalculateSpotMatrix(shadowMatrices[0], light, Vector3::ZERO);
  282. bool isShadowed = shadowMap && graphics->HasTextureUnit(TU_SHADOWMAP);
  283. if (isShadowed)
  284. CalculateShadowMatrix(shadowMatrices[1], lightQueue_, 0, renderer, Vector3::ZERO);
  285. graphics->SetShaderParameter(VSP_LIGHTMATRICES, shadowMatrices[0].Data(), isShadowed ? 32 : 16);
  286. }
  287. break;
  288. case LIGHT_POINT:
  289. {
  290. Matrix4 lightVecRot(lightNode->GetWorldRotation().RotationMatrix());
  291. // HLSL compiler will pack the parameters as if the matrix is only 3x4, so must be careful to not overwrite
  292. // the next parameter
  293. #ifdef ATOMIC_OPENGL
  294. graphics->SetShaderParameter(VSP_LIGHTMATRICES, lightVecRot.Data(), 16);
  295. #else
  296. graphics->SetShaderParameter(VSP_LIGHTMATRICES, lightVecRot.Data(), 12);
  297. #endif
  298. }
  299. break;
  300. }
  301. }
  302. float fade = 1.0f;
  303. float fadeEnd = light->GetDrawDistance();
  304. float fadeStart = light->GetFadeDistance();
  305. // Do fade calculation for light if both fade & draw distance defined
  306. if (light->GetLightType() != LIGHT_DIRECTIONAL && fadeEnd > 0.0f && fadeStart > 0.0f && fadeStart < fadeEnd)
  307. fade = Min(1.0f - (light->GetDistance() - fadeStart) / (fadeEnd - fadeStart), 1.0f);
  308. // Negative lights will use subtract blending, so write absolute RGB values to the shader parameter
  309. graphics->SetShaderParameter(PSP_LIGHTCOLOR, Color(light->GetEffectiveColor().Abs(),
  310. light->GetEffectiveSpecularIntensity()) * fade);
  311. graphics->SetShaderParameter(PSP_LIGHTDIR, lightWorldRotation * Vector3::BACK);
  312. graphics->SetShaderParameter(PSP_LIGHTPOS,
  313. Vector4((isLightVolume ? (lightNode->GetWorldPosition() - cameraEffectivePos) : lightNode->GetWorldPosition()),
  314. atten));
  315. if (graphics->HasShaderParameter(PSP_LIGHTMATRICES))
  316. {
  317. switch (light->GetLightType())
  318. {
  319. case LIGHT_DIRECTIONAL:
  320. {
  321. Matrix4 shadowMatrices[MAX_CASCADE_SPLITS];
  322. unsigned numSplits = (unsigned)Min(MAX_CASCADE_SPLITS, (int)lightQueue_->shadowSplits_.Size());
  323. for (unsigned i = 0; i < numSplits; ++i)
  324. {
  325. CalculateShadowMatrix(shadowMatrices[i], lightQueue_, i, renderer, isLightVolume ? cameraEffectivePos :
  326. Vector3::ZERO);
  327. }
  328. graphics->SetShaderParameter(PSP_LIGHTMATRICES, shadowMatrices[0].Data(), 16 * numSplits);
  329. }
  330. break;
  331. case LIGHT_SPOT:
  332. {
  333. Matrix4 shadowMatrices[2];
  334. CalculateSpotMatrix(shadowMatrices[0], light, cameraEffectivePos);
  335. bool isShadowed = lightQueue_->shadowMap_ != 0;
  336. if (isShadowed)
  337. {
  338. CalculateShadowMatrix(shadowMatrices[1], lightQueue_, 0, renderer, isLightVolume ? cameraEffectivePos :
  339. Vector3::ZERO);
  340. }
  341. graphics->SetShaderParameter(PSP_LIGHTMATRICES, shadowMatrices[0].Data(), isShadowed ? 32 : 16);
  342. }
  343. break;
  344. case LIGHT_POINT:
  345. {
  346. Matrix4 lightVecRot(lightNode->GetWorldRotation().RotationMatrix());
  347. // HLSL compiler will pack the parameters as if the matrix is only 3x4, so must be careful to not overwrite
  348. // the next parameter
  349. #ifdef ATOMIC_OPENGL
  350. graphics->SetShaderParameter(PSP_LIGHTMATRICES, lightVecRot.Data(), 16);
  351. #else
  352. graphics->SetShaderParameter(PSP_LIGHTMATRICES, lightVecRot.Data(), 12);
  353. #endif
  354. }
  355. break;
  356. }
  357. }
  358. // Set shadow mapping shader parameters
  359. if (shadowMap)
  360. {
  361. {
  362. // Calculate point light shadow sampling offsets (unrolled cube map)
  363. unsigned faceWidth = (unsigned)(shadowMap->GetWidth() / 2);
  364. unsigned faceHeight = (unsigned)(shadowMap->GetHeight() / 3);
  365. float width = (float)shadowMap->GetWidth();
  366. float height = (float)shadowMap->GetHeight();
  367. #ifdef ATOMIC_OPENGL
  368. float mulX = (float)(faceWidth - 3) / width;
  369. float mulY = (float)(faceHeight - 3) / height;
  370. float addX = 1.5f / width;
  371. float addY = 1.5f / height;
  372. #else
  373. float mulX = (float)(faceWidth - 4) / width;
  374. float mulY = (float)(faceHeight - 4) / height;
  375. float addX = 2.5f / width;
  376. float addY = 2.5f / height;
  377. #endif
  378. // If using 4 shadow samples, offset the position diagonally by half pixel
  379. if (renderer->GetShadowQuality() & SHADOWQUALITY_HIGH_16BIT)
  380. {
  381. addX -= 0.5f / width;
  382. addY -= 0.5f / height;
  383. }
  384. graphics->SetShaderParameter(PSP_SHADOWCUBEADJUST, Vector4(mulX, mulY, addX, addY));
  385. }
  386. {
  387. // Calculate shadow camera depth parameters for point light shadows and shadow fade parameters for
  388. // directional light shadows, stored in the same uniform
  389. Camera* shadowCamera = lightQueue_->shadowSplits_[0].shadowCamera_;
  390. float nearClip = shadowCamera->GetNearClip();
  391. float farClip = shadowCamera->GetFarClip();
  392. float q = farClip / (farClip - nearClip);
  393. float r = -q * nearClip;
  394. const CascadeParameters& parameters = light->GetShadowCascade();
  395. float viewFarClip = camera_->GetFarClip();
  396. float shadowRange = parameters.GetShadowRange();
  397. float fadeStart = parameters.fadeStart_ * shadowRange / viewFarClip;
  398. float fadeEnd = shadowRange / viewFarClip;
  399. float fadeRange = fadeEnd - fadeStart;
  400. graphics->SetShaderParameter(PSP_SHADOWDEPTHFADE, Vector4(q, r, fadeStart, 1.0f / fadeRange));
  401. }
  402. {
  403. float intensity = light->GetShadowIntensity();
  404. float fadeStart = light->GetShadowFadeDistance();
  405. float fadeEnd = light->GetShadowDistance();
  406. if (fadeStart > 0.0f && fadeEnd > 0.0f && fadeEnd > fadeStart)
  407. intensity =
  408. Lerp(intensity, 1.0f, Clamp((light->GetDistance() - fadeStart) / (fadeEnd - fadeStart), 0.0f, 1.0f));
  409. float pcfValues = (1.0f - intensity);
  410. float samples = renderer->GetShadowQuality() >= SHADOWQUALITY_HIGH_16BIT ? 4.0f : 1.0f;
  411. graphics->SetShaderParameter(PSP_SHADOWINTENSITY, Vector4(pcfValues / samples, intensity, 0.0f, 0.0f));
  412. }
  413. float sizeX = 1.0f / (float)shadowMap->GetWidth();
  414. float sizeY = 1.0f / (float)shadowMap->GetHeight();
  415. graphics->SetShaderParameter(PSP_SHADOWMAPINVSIZE, Vector4(sizeX, sizeY, 0.0f, 0.0f));
  416. Vector4 lightSplits(M_LARGE_VALUE, M_LARGE_VALUE, M_LARGE_VALUE, M_LARGE_VALUE);
  417. if (lightQueue_->shadowSplits_.Size() > 1)
  418. lightSplits.x_ = lightQueue_->shadowSplits_[0].farSplit_ / camera_->GetFarClip();
  419. if (lightQueue_->shadowSplits_.Size() > 2)
  420. lightSplits.y_ = lightQueue_->shadowSplits_[1].farSplit_ / camera_->GetFarClip();
  421. if (lightQueue_->shadowSplits_.Size() > 3)
  422. lightSplits.z_ = lightQueue_->shadowSplits_[2].farSplit_ / camera_->GetFarClip();
  423. graphics->SetShaderParameter(PSP_SHADOWSPLITS, lightSplits);
  424. }
  425. }
  426. else if (lightQueue_->vertexLights_.Size() && graphics->HasShaderParameter(VSP_VERTEXLIGHTS) &&
  427. graphics->NeedParameterUpdate(SP_LIGHT, lightQueue_))
  428. {
  429. Vector4 vertexLights[MAX_VERTEX_LIGHTS * 3];
  430. const PODVector<Light*>& lights = lightQueue_->vertexLights_;
  431. for (unsigned i = 0; i < lights.Size(); ++i)
  432. {
  433. Light* vertexLight = lights[i];
  434. Node* vertexLightNode = vertexLight->GetNode();
  435. LightType type = vertexLight->GetLightType();
  436. // Attenuation
  437. float invRange, cutoff, invCutoff;
  438. if (type == LIGHT_DIRECTIONAL)
  439. invRange = 0.0f;
  440. else
  441. invRange = 1.0f / Max(vertexLight->GetRange(), M_EPSILON);
  442. if (type == LIGHT_SPOT)
  443. {
  444. cutoff = Cos(vertexLight->GetFov() * 0.5f);
  445. invCutoff = 1.0f / (1.0f - cutoff);
  446. }
  447. else
  448. {
  449. cutoff = -1.0f;
  450. invCutoff = 1.0f;
  451. }
  452. // Color
  453. float fade = 1.0f;
  454. float fadeEnd = vertexLight->GetDrawDistance();
  455. float fadeStart = vertexLight->GetFadeDistance();
  456. // Do fade calculation for light if both fade & draw distance defined
  457. if (vertexLight->GetLightType() != LIGHT_DIRECTIONAL && fadeEnd > 0.0f && fadeStart > 0.0f && fadeStart < fadeEnd)
  458. fade = Min(1.0f - (vertexLight->GetDistance() - fadeStart) / (fadeEnd - fadeStart), 1.0f);
  459. Color color = vertexLight->GetEffectiveColor() * fade;
  460. vertexLights[i * 3] = Vector4(color.r_, color.g_, color.b_, invRange);
  461. // Direction
  462. vertexLights[i * 3 + 1] = Vector4(-(vertexLightNode->GetWorldDirection()), cutoff);
  463. // Position
  464. vertexLights[i * 3 + 2] = Vector4(vertexLightNode->GetWorldPosition(), invCutoff);
  465. }
  466. graphics->SetShaderParameter(VSP_VERTEXLIGHTS, vertexLights[0].Data(), lights.Size() * 3 * 4);
  467. }
  468. }
  469. // Set material-specific shader parameters and textures
  470. if (material_)
  471. {
  472. if (graphics->NeedParameterUpdate(SP_MATERIAL, reinterpret_cast<const void*>(material_->GetShaderParameterHash())))
  473. {
  474. const HashMap<StringHash, MaterialShaderParameter>& parameters = material_->GetShaderParameters();
  475. for (HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = parameters.Begin(); i != parameters.End(); ++i)
  476. graphics->SetShaderParameter(i->first_, i->second_.value_);
  477. }
  478. const HashMap<TextureUnit, SharedPtr<Texture> >& textures = material_->GetTextures();
  479. for (HashMap<TextureUnit, SharedPtr<Texture> >::ConstIterator i = textures.Begin(); i != textures.End(); ++i)
  480. {
  481. if (graphics->HasTextureUnit(i->first_))
  482. graphics->SetTexture(i->first_, i->second_.Get());
  483. }
  484. }
  485. // Set light-related textures
  486. if (light)
  487. {
  488. if (shadowMap && graphics->HasTextureUnit(TU_SHADOWMAP))
  489. graphics->SetTexture(TU_SHADOWMAP, shadowMap);
  490. if (graphics->HasTextureUnit(TU_LIGHTRAMP))
  491. {
  492. Texture* rampTexture = light->GetRampTexture();
  493. if (!rampTexture)
  494. rampTexture = renderer->GetDefaultLightRamp();
  495. graphics->SetTexture(TU_LIGHTRAMP, rampTexture);
  496. }
  497. if (graphics->HasTextureUnit(TU_LIGHTSHAPE))
  498. {
  499. Texture* shapeTexture = light->GetShapeTexture();
  500. if (!shapeTexture && light->GetLightType() == LIGHT_SPOT)
  501. shapeTexture = renderer->GetDefaultLightSpot();
  502. graphics->SetTexture(TU_LIGHTSHAPE, shapeTexture);
  503. }
  504. }
  505. // Set zone texture if necessary
  506. #ifdef DESKTOP_GRAPHICS
  507. if (zone_ && graphics->HasTextureUnit(TU_ZONE))
  508. graphics->SetTexture(TU_ZONE, zone_->GetZoneTexture());
  509. #endif
  510. }
  511. void Batch::Draw(View* view, bool allowDepthWrite) const
  512. {
  513. if (!geometry_->IsEmpty())
  514. {
  515. Prepare(view, true, allowDepthWrite);
  516. geometry_->Draw(view->GetGraphics());
  517. }
  518. }
  519. void BatchGroup::SetTransforms(void* lockedData, unsigned& freeIndex)
  520. {
  521. // Do not use up buffer space if not going to draw as instanced
  522. if (geometryType_ != GEOM_INSTANCED)
  523. return;
  524. startIndex_ = freeIndex;
  525. Matrix3x4* dest = (Matrix3x4*)lockedData;
  526. dest += freeIndex;
  527. for (unsigned i = 0; i < instances_.Size(); ++i)
  528. *dest++ = *instances_[i].worldTransform_;
  529. freeIndex += instances_.Size();
  530. }
  531. void BatchGroup::Draw(View* view, bool allowDepthWrite) const
  532. {
  533. Graphics* graphics = view->GetGraphics();
  534. Renderer* renderer = view->GetRenderer();
  535. if (instances_.Size() && !geometry_->IsEmpty())
  536. {
  537. // Draw as individual objects if instancing not supported or could not fill the instancing buffer
  538. VertexBuffer* instanceBuffer = renderer->GetInstancingBuffer();
  539. if (!instanceBuffer || geometryType_ != GEOM_INSTANCED || startIndex_ == M_MAX_UNSIGNED)
  540. {
  541. Batch::Prepare(view, false, allowDepthWrite);
  542. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  543. graphics->SetVertexBuffers(geometry_->GetVertexBuffers(), geometry_->GetVertexElementMasks());
  544. for (unsigned i = 0; i < instances_.Size(); ++i)
  545. {
  546. if (graphics->NeedParameterUpdate(SP_OBJECT, instances_[i].worldTransform_))
  547. graphics->SetShaderParameter(VSP_MODEL, *instances_[i].worldTransform_);
  548. graphics->Draw(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  549. geometry_->GetVertexStart(), geometry_->GetVertexCount());
  550. }
  551. }
  552. else
  553. {
  554. Batch::Prepare(view, false, allowDepthWrite);
  555. // Get the geometry vertex buffers, then add the instancing stream buffer
  556. // Hack: use a const_cast to avoid dynamic allocation of new temp vectors
  557. Vector<SharedPtr<VertexBuffer> >& vertexBuffers = const_cast<Vector<SharedPtr<VertexBuffer> >&>(
  558. geometry_->GetVertexBuffers());
  559. PODVector<unsigned>& elementMasks = const_cast<PODVector<unsigned>&>(geometry_->GetVertexElementMasks());
  560. vertexBuffers.Push(SharedPtr<VertexBuffer>(instanceBuffer));
  561. elementMasks.Push(instanceBuffer->GetElementMask());
  562. graphics->SetIndexBuffer(geometry_->GetIndexBuffer());
  563. graphics->SetVertexBuffers(vertexBuffers, elementMasks, startIndex_);
  564. graphics->DrawInstanced(geometry_->GetPrimitiveType(), geometry_->GetIndexStart(), geometry_->GetIndexCount(),
  565. geometry_->GetVertexStart(), geometry_->GetVertexCount(), instances_.Size());
  566. // Remove the instancing buffer & element mask now
  567. vertexBuffers.Pop();
  568. elementMasks.Pop();
  569. }
  570. }
  571. }
  572. unsigned BatchGroupKey::ToHash() const
  573. {
  574. return (unsigned)((size_t)zone_ / sizeof(Zone) + (size_t)lightQueue_ / sizeof(LightBatchQueue) + (size_t)pass_ / sizeof(Pass) +
  575. (size_t)material_ / sizeof(Material) + (size_t)geometry_ / sizeof(Geometry));
  576. }
  577. void BatchQueue::Clear(int maxSortedInstances)
  578. {
  579. batches_.Clear();
  580. sortedBatches_.Clear();
  581. batchGroups_.Clear();
  582. maxSortedInstances_ = (unsigned)maxSortedInstances;
  583. }
  584. void BatchQueue::SortBackToFront()
  585. {
  586. sortedBatches_.Resize(batches_.Size());
  587. for (unsigned i = 0; i < batches_.Size(); ++i)
  588. sortedBatches_[i] = &batches_[i];
  589. Sort(sortedBatches_.Begin(), sortedBatches_.End(), CompareBatchesBackToFront);
  590. // Do not actually sort batch groups, just list them
  591. sortedBatchGroups_.Resize(batchGroups_.Size());
  592. unsigned index = 0;
  593. for (HashMap<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  594. sortedBatchGroups_[index++] = &i->second_;
  595. }
  596. void BatchQueue::SortFrontToBack()
  597. {
  598. sortedBatches_.Clear();
  599. for (unsigned i = 0; i < batches_.Size(); ++i)
  600. sortedBatches_.Push(&batches_[i]);
  601. SortFrontToBack2Pass(sortedBatches_);
  602. // Sort each group front to back
  603. for (HashMap<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  604. {
  605. if (i->second_.instances_.Size() <= maxSortedInstances_)
  606. {
  607. Sort(i->second_.instances_.Begin(), i->second_.instances_.End(), CompareInstancesFrontToBack);
  608. if (i->second_.instances_.Size())
  609. i->second_.distance_ = i->second_.instances_[0].distance_;
  610. }
  611. else
  612. {
  613. float minDistance = M_INFINITY;
  614. for (PODVector<InstanceData>::ConstIterator j = i->second_.instances_.Begin(); j != i->second_.instances_.End(); ++j)
  615. minDistance = Min(minDistance, j->distance_);
  616. i->second_.distance_ = minDistance;
  617. }
  618. }
  619. sortedBatchGroups_.Resize(batchGroups_.Size());
  620. unsigned index = 0;
  621. for (HashMap<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  622. sortedBatchGroups_[index++] = &i->second_;
  623. SortFrontToBack2Pass(reinterpret_cast<PODVector<Batch*>& >(sortedBatchGroups_));
  624. }
  625. void BatchQueue::SortFrontToBack2Pass(PODVector<Batch*>& batches)
  626. {
  627. // Mobile devices likely use a tiled deferred approach, with which front-to-back sorting is irrelevant. The 2-pass
  628. // method is also time consuming, so just sort with state having priority
  629. #ifdef GL_ES_VERSION_2_0
  630. Sort(batches.Begin(), batches.End(), CompareBatchesState);
  631. #else
  632. // For desktop, first sort by distance and remap shader/material/geometry IDs in the sort key
  633. Sort(batches.Begin(), batches.End(), CompareBatchesFrontToBack);
  634. unsigned freeShaderID = 0;
  635. unsigned short freeMaterialID = 0;
  636. unsigned short freeGeometryID = 0;
  637. for (PODVector<Batch*>::Iterator i = batches.Begin(); i != batches.End(); ++i)
  638. {
  639. Batch* batch = *i;
  640. unsigned shaderID = (unsigned)(batch->sortKey_ >> 32);
  641. HashMap<unsigned, unsigned>::ConstIterator j = shaderRemapping_.Find(shaderID);
  642. if (j != shaderRemapping_.End())
  643. shaderID = j->second_;
  644. else
  645. {
  646. shaderID = shaderRemapping_[shaderID] = freeShaderID | (shaderID & 0xc0000000);
  647. ++freeShaderID;
  648. }
  649. unsigned short materialID = (unsigned short)(batch->sortKey_ & 0xffff0000);
  650. HashMap<unsigned short, unsigned short>::ConstIterator k = materialRemapping_.Find(materialID);
  651. if (k != materialRemapping_.End())
  652. materialID = k->second_;
  653. else
  654. {
  655. materialID = materialRemapping_[materialID] = freeMaterialID;
  656. ++freeMaterialID;
  657. }
  658. unsigned short geometryID = (unsigned short)(batch->sortKey_ & 0xffff);
  659. HashMap<unsigned short, unsigned short>::ConstIterator l = geometryRemapping_.Find(geometryID);
  660. if (l != geometryRemapping_.End())
  661. geometryID = l->second_;
  662. else
  663. {
  664. geometryID = geometryRemapping_[geometryID] = freeGeometryID;
  665. ++freeGeometryID;
  666. }
  667. batch->sortKey_ = (((unsigned long long)shaderID) << 32) | (((unsigned long long)materialID) << 16) | geometryID;
  668. }
  669. shaderRemapping_.Clear();
  670. materialRemapping_.Clear();
  671. geometryRemapping_.Clear();
  672. // Finally sort again with the rewritten ID's
  673. Sort(batches.Begin(), batches.End(), CompareBatchesState);
  674. #endif
  675. }
  676. void BatchQueue::SetTransforms(void* lockedData, unsigned& freeIndex)
  677. {
  678. for (HashMap<BatchGroupKey, BatchGroup>::Iterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  679. i->second_.SetTransforms(lockedData, freeIndex);
  680. }
  681. void BatchQueue::Draw(View* view, bool markToStencil, bool usingLightOptimization, bool allowDepthWrite) const
  682. {
  683. Graphics* graphics = view->GetGraphics();
  684. Renderer* renderer = view->GetRenderer();
  685. // If View has set up its own light optimizations, do not disturb the stencil/scissor test settings
  686. if (!usingLightOptimization)
  687. {
  688. graphics->SetScissorTest(false);
  689. // During G-buffer rendering, mark opaque pixels' lightmask to stencil buffer if requested
  690. if (!markToStencil)
  691. graphics->SetStencilTest(false);
  692. }
  693. // Instanced
  694. for (PODVector<BatchGroup*>::ConstIterator i = sortedBatchGroups_.Begin(); i != sortedBatchGroups_.End(); ++i)
  695. {
  696. BatchGroup* group = *i;
  697. if (markToStencil)
  698. graphics->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_KEEP, OP_KEEP, group->lightMask_);
  699. group->Draw(view, allowDepthWrite);
  700. }
  701. // Non-instanced
  702. for (PODVector<Batch*>::ConstIterator i = sortedBatches_.Begin(); i != sortedBatches_.End(); ++i)
  703. {
  704. Batch* batch = *i;
  705. if (markToStencil)
  706. graphics->SetStencilTest(true, CMP_ALWAYS, OP_REF, OP_KEEP, OP_KEEP, batch->lightMask_);
  707. if (!usingLightOptimization)
  708. {
  709. // If drawing an alpha batch, we can optimize fillrate by scissor test
  710. if (!batch->isBase_ && batch->lightQueue_)
  711. renderer->OptimizeLightByScissor(batch->lightQueue_->light_, batch->camera_);
  712. else
  713. graphics->SetScissorTest(false);
  714. }
  715. batch->Draw(view, allowDepthWrite);
  716. }
  717. }
  718. unsigned BatchQueue::GetNumInstances() const
  719. {
  720. unsigned total = 0;
  721. for (HashMap<BatchGroupKey, BatchGroup>::ConstIterator i = batchGroups_.Begin(); i != batchGroups_.End(); ++i)
  722. {
  723. if (i->second_.geometryType_ == GEOM_INSTANCED)
  724. total += i->second_.instances_.Size();
  725. }
  726. return total;
  727. }
  728. }