2
0

Batch.cpp 41 KB

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