DeferredView.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 "Geometry.h"
  25. #include "GeometryNode.h"
  26. #include "Light.h"
  27. #include "Material.h"
  28. #include "Pipeline.h"
  29. #include "PixelShader.h"
  30. #include "Profiler.h"
  31. #include "Renderer.h"
  32. #include "RendererImpl.h"
  33. #include "Texture2D.h"
  34. #include "VertexShader.h"
  35. #include "View.h"
  36. #include "DebugNew.h"
  37. static const std::string hwDepthVariations[] =
  38. {
  39. "",
  40. "HW"
  41. };
  42. static const std::string linearVariations[] =
  43. {
  44. "",
  45. "Linear"
  46. };
  47. void View::getBatchesDeferred()
  48. {
  49. Renderer* renderer = mPipeline->getRenderer();
  50. bool deferred = renderer->getRenderMode() == RENDER_DEFERRED;
  51. // G-Buffer pass types depend on whether deferred shading or light prepass is in use
  52. PassType gBufferPass, additionalPass;
  53. if (deferred)
  54. {
  55. gBufferPass = PASS_DEFERRED;
  56. additionalPass = PASS_EMISSIVE;
  57. }
  58. else
  59. {
  60. gBufferPass = PASS_PREPASS;
  61. additionalPass = PASS_MATERIAL;
  62. }
  63. // Go through geometries for GBuffer and ambient batches
  64. {
  65. PROFILE(View_GetAmbientBatches);
  66. for (unsigned i = 0; i < mGeometries.size(); ++i)
  67. {
  68. GeometryNode* node = mGeometries[i];
  69. unsigned numBatches = node->getNumBatches();
  70. for (unsigned j = 0; j < numBatches; ++j)
  71. {
  72. Geometry* geom = node->getBatchGeometry(j);
  73. if (!geom)
  74. continue;
  75. MaterialTechnique* tech = getMaterialTechnique(node, j);
  76. if (!tech)
  77. continue;
  78. // Check here if the material technique refers to a render target texture with camera(s) attached
  79. // Only check this for the main view (null rendertarget)
  80. if ((!mRenderTarget) && (tech->getAuxViewFrameNumber() != mFrame.mFrameNumber))
  81. checkTechniqueForAuxView(tech);
  82. Batch newBatch;
  83. newBatch.mNode = node;
  84. newBatch.mCamera = mCamera;
  85. newBatch.mDistance = node->getDistance();
  86. newBatch.mGeometry = geom;
  87. newBatch.mBatchIndex = j;
  88. // GBuffer pass has priority over ambient pass
  89. MaterialPass* pass = tech->getPass(gBufferPass);
  90. if (pass)
  91. {
  92. // Render opaque objects without alpha masking first
  93. newBatch.mHasPriority = !(pass->getAlphaMask() || pass->getAlphaTest());
  94. mPipeline->setBatchShaders(newBatch, tech, pass);
  95. newBatch.calculateSortKey(true, true);
  96. mGBufferQueue.push_back(newBatch);
  97. // Check for additional emissive pass with deferred rendering, and material pass with light prepass
  98. pass = tech->getPass(additionalPass);
  99. if (pass)
  100. {
  101. newBatch.mHasPriority = false;
  102. mPipeline->setBatchShaders(newBatch, tech, pass);
  103. newBatch.calculateSortKey(true, true);
  104. mForwardQueue.push_back(newBatch);
  105. }
  106. }
  107. else
  108. {
  109. pass = tech->getPass(PASS_AMBIENT);
  110. if (pass)
  111. {
  112. mPipeline->setBatchShaders(newBatch, tech, pass);
  113. if (pass->getBlendMode() == BLEND_REPLACE)
  114. {
  115. newBatch.mHasPriority = !(pass->getAlphaTest() || pass->getAlphaMask());
  116. newBatch.calculateSortKey(true, true);
  117. mAmbientQueue.push_back(newBatch);
  118. }
  119. else
  120. {
  121. newBatch.mHasPriority = true;
  122. newBatch.calculateSortKey(false, false);
  123. mTransparentQueue.push_back(newBatch);
  124. }
  125. }
  126. else
  127. {
  128. // If no other pass yet, check for post-opaque (custom) pass
  129. pass = tech->getPass(PASS_POSTOPAQUE);
  130. if (pass)
  131. {
  132. newBatch.mHasPriority = false;
  133. mPipeline->setBatchShaders(newBatch, tech, pass);
  134. newBatch.calculateSortKey(true, true);
  135. mPostOpaqueQueue.push_back(newBatch);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. // Go through lights
  143. {
  144. PROFILE_MULTIPLE(Pipeline_GetLightBatches, mLights.size());
  145. unsigned lightQueueCount = 0;
  146. for (unsigned i = 0; i < mLights.size(); ++i)
  147. {
  148. Light* light = mLights[i];
  149. unsigned splits = processLight(light);
  150. if (!splits)
  151. continue;
  152. // For split point lights, check that a transparent object is not lit multiple times
  153. bool splitPointLight = sSplitLights[0]->getLightType() == LIGHT_SPLITPOINT;
  154. static std::set<GeometryNode*> litTransparencies;
  155. litTransparencies.clear();
  156. // Negative (darkening) lighting flag
  157. bool negative = mLights[i]->isNegative();
  158. // Prepare lit object + shadow caster queues for each split
  159. if (mLightQueues.size() < lightQueueCount + splits)
  160. mLightQueues.resize(lightQueueCount + splits);
  161. unsigned prevLightQueueCount = lightQueueCount;
  162. for (unsigned j = 0; j < splits; ++j)
  163. {
  164. LightBatchQueue& lightQueue = mLightQueues[lightQueueCount];
  165. lightQueue.mLight = sSplitLights[j];
  166. lightQueue.mShadowBatches.clear();
  167. lightQueue.mBatches.clear();
  168. lightQueue.mLastSplit = false;
  169. // Loop through shadow casters
  170. Camera& shadowCamera = sSplitLights[j]->getShadowCamera();
  171. for (unsigned k = 0; k < sShadowCasters[j].size(); ++k)
  172. {
  173. GeometryNode* node = sShadowCasters[j][k];
  174. unsigned numBatches = node->getNumBatches();
  175. for (unsigned l = 0; l < numBatches; ++l)
  176. {
  177. MaterialTechnique* tech = getMaterialTechnique(node, l);
  178. if (!tech)
  179. continue;
  180. MaterialPass* pass = tech->getPass(PASS_SHADOW);
  181. // Skip if material has no shadow pass
  182. if (!pass)
  183. continue;
  184. Geometry* geom = node->getBatchGeometry(l);
  185. if (!geom)
  186. continue;
  187. // Build the shadow batch
  188. // Note: shadow cameras are never parented, so can simply use getPosition()
  189. Batch shadowBatch;
  190. shadowBatch.mNode = node;
  191. shadowBatch.mCamera = &shadowCamera;
  192. shadowBatch.mDistance = (node->getWorldPosition() - shadowCamera.getPosition()).getLengthFast();
  193. shadowBatch.mGeometry = geom;
  194. shadowBatch.mBatchIndex = l;
  195. shadowBatch.mForwardLight = sSplitLights[j];
  196. shadowBatch.mHasPriority = !(pass->getAlphaTest() || pass->getAlphaMask());
  197. mPipeline->setBatchShaders(shadowBatch, tech, pass);
  198. shadowBatch.calculateSortKey(true, true);
  199. lightQueue.mShadowBatches.push_back(shadowBatch);
  200. }
  201. }
  202. // Loop through lit geometries to get forward lit objects
  203. for (unsigned k = 0; k < sLitGeometries[j].size(); ++k)
  204. {
  205. GeometryNode* node = sLitGeometries[j][k];
  206. unsigned numBatches = node->getNumBatches();
  207. for (unsigned l = 0; l < numBatches; ++l)
  208. {
  209. MaterialTechnique* tech = getMaterialTechnique(node, l);
  210. if (!tech)
  211. continue;
  212. // Skip if material uses deferred rendering
  213. if (tech->hasPass(gBufferPass))
  214. continue;
  215. MaterialPass* pass;
  216. if (!negative)
  217. pass = tech->getPass(PASS_LIGHT);
  218. else
  219. pass = tech->getPass(PASS_NEGATIVE);
  220. // Skip if material does not receive light
  221. if (!pass)
  222. continue;
  223. Geometry* geom = node->getBatchGeometry(l);
  224. if (!geom)
  225. continue;
  226. // Build the lit batch
  227. Batch newBatch;
  228. newBatch.mNode = node;
  229. newBatch.mCamera = mCamera;
  230. newBatch.mDistance = node->getDistance();
  231. newBatch.mGeometry = geom;
  232. newBatch.mBatchIndex = l;
  233. newBatch.mForwardLight = sSplitLights[j];
  234. // No shadows in any forward lighting
  235. mPipeline->setBatchShaders(newBatch, tech, pass, false);
  236. // Check from the ambient pass whether the object is opaque
  237. MaterialPass* ambientPass = tech->getPass(PASS_AMBIENT);
  238. if ((!ambientPass) || (ambientPass->getBlendMode() == BLEND_REPLACE))
  239. {
  240. // Render negative lights first in the forward lighting queue
  241. newBatch.mHasPriority = negative;
  242. newBatch.calculateSortKey(true, true);
  243. mForwardQueue.push_back(newBatch);
  244. }
  245. else
  246. {
  247. // Prevent multi-lighting by a split point light
  248. // (transparent rendering can not handle the needed stencil masking)
  249. if (splitPointLight)
  250. {
  251. if (litTransparencies.find(node) != litTransparencies.end())
  252. continue;
  253. // Use the original light instead of the split one, to choose correct scissor
  254. newBatch.mForwardLight = mLights[i];
  255. }
  256. // If light is negative, bias the distance slightly to ensure it has priority
  257. if (negative)
  258. newBatch.mDistance -= 0.001f;
  259. newBatch.calculateSortKey(false, false);
  260. mTransparentQueue.push_back(newBatch);
  261. }
  262. }
  263. if (splitPointLight)
  264. litTransparencies.insert(node);
  265. }
  266. // Build the light volume batch
  267. // Check amount of lit geometries
  268. if (sLitGeometries[j].size())
  269. {
  270. Batch lightBatch;
  271. lightBatch.mNode = sSplitLights[j];
  272. lightBatch.mCamera = mCamera;
  273. lightBatch.mDistance = sSplitLights[j]->getDistance();
  274. lightBatch.mGeometry = mPipeline->getLightGeometry(sSplitLights[j]);
  275. mPipeline->setLightVolumeShaders(lightBatch);
  276. lightBatch.calculateSortKey(true, true);
  277. // Non-shadow casting light can go into the state-sorted light queue
  278. if (sSplitLights[j]->getShadowMap())
  279. {
  280. lightQueue.mBatches.push_back(lightBatch);
  281. lightQueueCount++;
  282. }
  283. else
  284. {
  285. if (!negative)
  286. mNoShadowLightQueue.push_back(lightBatch);
  287. else
  288. mNegativeLightQueue.push_back(lightBatch);
  289. }
  290. }
  291. }
  292. // Mark the last split
  293. if (lightQueueCount != prevLightQueueCount)
  294. mLightQueues[lightQueueCount - 1].mLastSplit = true;
  295. }
  296. // Resize the shadowed light queue vector now that final size is known
  297. mLightQueues.resize(lightQueueCount);
  298. }
  299. // Finally sort the batches
  300. {
  301. PROFILE(View_SortBatches);
  302. sortBatches(mGBufferQueue, mGBufferQueueSorted);
  303. sortBatches(mNegativeLightQueue, mNegativeLightQueueSorted);
  304. sortBatches(mNoShadowLightQueue, mNoShadowLightQueueSorted);
  305. sortBatches(mAmbientQueue, mAmbientQueueSorted);
  306. sortBatches(mForwardQueue, mForwardQueueSorted);
  307. sortBatches(mPostOpaqueQueue, mPostOpaqueQueueSorted);
  308. sortBatches(mTransparentQueue, mTransparentQueueSorted);
  309. for (unsigned i = 0; i < mLightQueues.size(); ++i)
  310. sortBatches(mLightQueues[i].mShadowBatches, mLightQueues[i].mSortedShadowBatches);
  311. }
  312. }
  313. void View::renderBatchesDeferred()
  314. {
  315. Renderer* renderer = mPipeline->getRenderer();
  316. bool deferred = renderer->getRenderMode() == RENDER_DEFERRED;
  317. Texture2D* diffBuffer = renderer->getDiffBuffer();
  318. Texture2D* normalBuffer = renderer->getNormalBuffer();
  319. Texture2D* depthBuffer = renderer->getDepthBuffer();
  320. bool hwDepth = renderer->getHardwareDepthSupport();
  321. // Check for edge filter. Only use it on the main view (null rendertarget)
  322. const EdgeFilterParameters& filterParams = mPipeline->getEdgeFilter();
  323. bool edgeFilter = (!mRenderTarget) && renderer->getMultiSample() && renderer->getScreenBuffer() &&
  324. (filterParams.mFilterStep > 0.0f) && (filterParams.mMaxFilter > 0.0f);
  325. // Use screen buffer only with edge filtering, else render direct to rendertarget or backbuffer
  326. RenderSurface* renderBuffer = edgeFilter ? renderer->getScreenBuffer()->getRenderSurface() : mRenderTarget;
  327. // Set the G-buffer UV coord adjustment here
  328. int gBufferWidth = diffBuffer->getWidth();
  329. int gBufferHeight = diffBuffer->getHeight();
  330. float widthRange = 0.5f * mWidth / gBufferWidth;
  331. float heightRange = 0.5f * mHeight / gBufferHeight;
  332. Vector4 bufferUVOffset(0.5f / gBufferWidth, 0.5f / gBufferHeight, widthRange, heightRange);
  333. renderer->setVertexShaderConstant(getVSRegister(VSP_GBUFFEROFFSETS), bufferUVOffset);
  334. {
  335. // Render G-buffer
  336. PROFILE(View_RenderGBuffer);
  337. clearLastParameterSources();
  338. // Use always the default depth stencil, so that it matches the G-buffer size, and is in the expected format
  339. // Enable depth rendertarget only if hardware depth not supported
  340. renderer->setColorWrite(true);
  341. renderer->setFillMode(FILL_SOLID);
  342. renderer->setScissorTest(false);
  343. renderer->setStencilTest(false);
  344. renderer->resetDepthStencil();
  345. renderer->setViewport(IntRect(0, 0, mWidth, mHeight));
  346. if (deferred)
  347. {
  348. renderer->setRenderTarget(0, diffBuffer);
  349. renderer->setRenderTarget(1, normalBuffer);
  350. if (!hwDepth)
  351. renderer->setRenderTarget(2, depthBuffer);
  352. }
  353. else
  354. {
  355. renderer->setRenderTarget(0, normalBuffer);
  356. if (!hwDepth)
  357. renderer->setRenderTarget(1, depthBuffer);
  358. }
  359. // Clear only depth and stencil at first, render the G-buffer batches
  360. renderer->clear(CLEAR_DEPTH | CLEAR_STENCIL);
  361. for (unsigned i = 0; i < mGBufferQueueSorted.size(); ++i)
  362. mGBufferQueueSorted[i]->draw(renderer);
  363. // Then fill the untouched parts of the G-buffer with defaults: black diffuse + specular (deferred only), far depth
  364. renderer->setAlphaTest(false);
  365. renderer->setBlendMode(BLEND_REPLACE);
  366. renderer->setDepthTest(CMP_LESSEQUAL);
  367. renderer->setDepthWrite(false);
  368. if (deferred)
  369. {
  370. renderer->resetRenderTarget(2);
  371. if (!hwDepth)
  372. renderer->setRenderTarget(1, depthBuffer);
  373. else
  374. renderer->resetRenderTarget(1);
  375. mPipeline->drawFullScreenQuad(*mCamera, mPipeline->getVertexShader("Deferred/GBufferFill"),
  376. mPipeline->getPixelShader("Deferred/GBufferFill_" + hwDepthVariations[hwDepth ? 1 : 0]), false);
  377. }
  378. else if (!hwDepth)
  379. {
  380. renderer->resetRenderTarget(1);
  381. renderer->setRenderTarget(0, depthBuffer);
  382. // The stencil shader writes color 1.0, which equals far depth
  383. mPipeline->drawFullScreenQuad(*mCamera, mPipeline->getVertexShader("Stencil"),
  384. mPipeline->getPixelShader("Stencil"), false);
  385. }
  386. }
  387. if (deferred)
  388. {
  389. // Render ambient color & fog
  390. clearLastParameterSources();
  391. renderer->setDepthTest(CMP_ALWAYS);
  392. renderer->setRenderTarget(0, renderBuffer);
  393. renderer->resetRenderTarget(1);
  394. renderer->resetRenderTarget(2);
  395. renderer->setTexture(TU_DIFFBUFFER, diffBuffer);
  396. renderer->setTexture(TU_DEPTHBUFFER, depthBuffer);
  397. // Use depth reconstruction only if necessary
  398. bool linear = mCamera->isOrthographic() || (!hwDepth);
  399. mPipeline->drawFullScreenQuad(*mCamera, mPipeline->getVertexShader("Deferred/Ambient"),
  400. mPipeline->getPixelShader("Deferred/Ambient_" + linearVariations[linear ? 1 : 0]), false);
  401. }
  402. else
  403. {
  404. // Light prepass: reset the light accumulation buffer with ambient light (half intensity to allow 2x "overburn")
  405. renderer->setRenderTarget(0, diffBuffer);
  406. renderer->resetRenderTarget(1);
  407. renderer->clear(CLEAR_COLOR, mZone->getAmbientColor() * 0.5f);
  408. }
  409. {
  410. // Render lights
  411. PROFILE(View_RenderLights);
  412. // Negative lights
  413. if (mNegativeLightQueueSorted.size())
  414. {
  415. clearLastParameterSources();
  416. if (deferred)
  417. renderer->setTexture(TU_DIFFBUFFER, diffBuffer);
  418. renderer->setTexture(TU_NORMALBUFFER, normalBuffer);
  419. renderer->setTexture(TU_DEPTHBUFFER, depthBuffer);
  420. for (unsigned i = 0; i < mNegativeLightQueueSorted.size(); ++i)
  421. {
  422. mPipeline->setupLightBatch(*mNegativeLightQueueSorted[i]);
  423. mNegativeLightQueueSorted[i]->draw(renderer);
  424. }
  425. }
  426. // Shadowed lights
  427. for (unsigned i = 0; i < mLightQueues.size(); ++i)
  428. {
  429. LightBatchQueue& queue = mLightQueues[i];
  430. {
  431. PROFILE(View_RenderShadowMap);
  432. Texture2D* shadowMap = queue.mLight->getShadowMap();
  433. clearLastParameterSources();
  434. renderer->setColorWrite(false);
  435. renderer->setStencilTest(false);
  436. renderer->setTexture(TU_SHADOWMAP, 0);
  437. renderer->setRenderTarget(0, shadowMap->getRenderSurface()->getLinkedRenderTarget());
  438. renderer->setDepthStencil(shadowMap);
  439. renderer->clear(CLEAR_DEPTH);
  440. // Set shadow depth bias
  441. BiasParameters parameters = queue.mLight->getShadowBias();
  442. renderer->setDepthBias(parameters.mConstantBias, parameters.mSlopeScaledBias);
  443. // Set a scissor rectangle to match possible shadow map size reduction by out-zooming
  444. // However, do not do this for point lights
  445. if (queue.mLight->getLightType() != LIGHT_SPLITPOINT)
  446. {
  447. Texture2D* shadowMap = queue.mLight->getShadowMap();
  448. float zoom = min(queue.mLight->getShadowCamera().getZoom(),
  449. (float)(shadowMap->getWidth() - 2) / (float)shadowMap->getWidth());
  450. Rect zoomRect(Vector2(-1.0f, -1.0f) * zoom, Vector2(1.0f, 1.0f) * zoom);
  451. renderer->setScissorTest(true, zoomRect, false);
  452. }
  453. else
  454. renderer->setScissorTest(false);
  455. const std::vector<Batch*>& sortedBatches = queue.mSortedShadowBatches;
  456. if (sortedBatches.size())
  457. {
  458. for (unsigned j = 0; j < sortedBatches.size(); ++j)
  459. sortedBatches[j]->draw(renderer);
  460. }
  461. renderer->setColorWrite(true);
  462. renderer->setDepthBias(0.0f, 0.0f);
  463. }
  464. // Light volume batches are not sorted as there should be only one of them
  465. if (queue.mBatches.size())
  466. {
  467. clearLastParameterSources();
  468. if (deferred)
  469. {
  470. renderer->setRenderTarget(0, renderBuffer);
  471. renderer->setTexture(TU_DIFFBUFFER, diffBuffer);
  472. }
  473. else
  474. renderer->setRenderTarget(0, diffBuffer);
  475. renderer->setTexture(TU_NORMALBUFFER, normalBuffer);
  476. renderer->setTexture(TU_DEPTHBUFFER, depthBuffer);
  477. renderer->resetDepthStencil();
  478. for (unsigned j = 0; j < queue.mBatches.size(); ++j)
  479. {
  480. mPipeline->setupLightBatch(queue.mBatches[j]);
  481. queue.mBatches[j].draw(renderer);
  482. }
  483. // If was the last split of a shadowed point light, clear the stencil by rendering the point light again
  484. if ((queue.mLastSplit) && (queue.mLight->getLightType() == LIGHT_SPLITPOINT))
  485. mPipeline->drawSplitLightToStencil(*mCamera, queue.mLight, true);
  486. }
  487. }
  488. // Non-shadowed lights
  489. if (mNoShadowLightQueueSorted.size())
  490. {
  491. clearLastParameterSources();
  492. if (deferred)
  493. {
  494. renderer->setRenderTarget(0, renderBuffer);
  495. renderer->setTexture(TU_DIFFBUFFER, diffBuffer);
  496. }
  497. else
  498. renderer->setRenderTarget(0, diffBuffer);
  499. renderer->setTexture(TU_NORMALBUFFER, normalBuffer);
  500. renderer->setTexture(TU_DEPTHBUFFER, depthBuffer);
  501. renderer->resetDepthStencil();
  502. for (unsigned i = 0; i < mNoShadowLightQueueSorted.size(); ++i)
  503. {
  504. mPipeline->setupLightBatch(*mNoShadowLightQueueSorted[i]);
  505. mNoShadowLightQueueSorted[i]->draw(renderer);
  506. }
  507. }
  508. }
  509. {
  510. // Render forward passes
  511. PROFILE(View_RenderForward);
  512. clearLastParameterSources();
  513. renderer->setStencilTest(false);
  514. renderer->setRenderTarget(0, renderBuffer);
  515. renderer->setTexture(TU_DIFFBUFFER, 0);
  516. renderer->setTexture(TU_NORMALBUFFER, 0);
  517. renderer->setTexture(TU_DEPTHBUFFER, 0);
  518. if (!deferred)
  519. renderer->clear(CLEAR_COLOR, mZone->getFogColor());
  520. for (unsigned i = 0; i < mAmbientQueueSorted.size(); ++i)
  521. mAmbientQueueSorted[i]->draw(renderer);
  522. for (unsigned i = 0; i < mForwardQueueSorted.size(); ++i)
  523. {
  524. if (mForwardQueueSorted[i]->mForwardLight)
  525. optimizeLightByScissor(mForwardQueueSorted[i]->mForwardLight);
  526. else
  527. {
  528. renderer->setScissorTest(false);
  529. // Bind the light accumulation buffer for material pass of prepass mode
  530. if (!deferred)
  531. renderer->setTexture(TU_LIGHTBUFFER, diffBuffer);
  532. }
  533. mForwardQueueSorted[i]->draw(renderer);
  534. }
  535. renderer->setScissorTest(false);
  536. for (unsigned i = 0; i < mPostOpaqueQueueSorted.size(); ++i)
  537. mPostOpaqueQueueSorted[i]->draw(renderer);
  538. }
  539. {
  540. // Render transparent objects last (both ambient & additive lighting)
  541. PROFILE(View_RenderTransparent);
  542. for (unsigned i = 0; i < mTransparentQueueSorted.size(); ++i)
  543. {
  544. optimizeLightByScissor(mTransparentQueueSorted[i]->mForwardLight);
  545. mTransparentQueueSorted[i]->draw(renderer);
  546. }
  547. }
  548. // Render edge filter now if enabled
  549. if (edgeFilter)
  550. {
  551. PROFILE(View_RenderEdgeFilter);
  552. float invWidth = 0.5f / gBufferWidth;
  553. float invHeight = 0.5f / gBufferHeight;
  554. renderer->setAlphaTest(false);
  555. renderer->setBlendMode(BLEND_REPLACE);
  556. renderer->setDepthTest(CMP_ALWAYS);
  557. renderer->setDepthWrite(false);
  558. renderer->setScissorTest(false);
  559. renderer->setStencilTest(false);
  560. renderer->setRenderTarget(0, mRenderTarget);
  561. renderer->setTexture(TU_DIFFBUFFER, renderer->getScreenBuffer());
  562. renderer->setPixelShaderConstant(getPSRegister(PSP_SAMPLEOFFSETS), Vector4(invWidth, -invWidth, invHeight, -invHeight));
  563. renderer->setPixelShaderConstant(getPSRegister(PSP_EDGEFILTERPARAMS), Vector4(filterParams.mThreshold,
  564. filterParams.mFilterStep, filterParams.mMaxFilter, filterParams.mMaxScale));
  565. mPipeline->drawFullScreenQuad(*mCamera, mPipeline->getVertexShader("EdgeFilter"),
  566. mPipeline->getPixelShader("EdgeFilter"), false);
  567. }
  568. }