Batch.cpp 41 KB

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