Batch.cpp 37 KB

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