| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2011 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Geometry.h"
- #include "GeometryNode.h"
- #include "Light.h"
- #include "Material.h"
- #include "Pipeline.h"
- #include "PixelShader.h"
- #include "Profiler.h"
- #include "Renderer.h"
- #include "Texture2D.h"
- #include "VertexShader.h"
- #include "View.h"
- #include "DebugNew.h"
- void View::getBatchesForward()
- {
- // Go through geometries for ambient batches
- {
- PROFILE(View_GetAmbientBatches);
- for (unsigned i = 0; i < mGeometries.size(); ++i)
- {
- GeometryNode* node = mGeometries[i];
- unsigned numBatches = node->getNumBatches();
-
- for (unsigned j = 0; j < numBatches; ++j)
- {
- Geometry* geom = node->getBatchGeometry(j);
- if (!geom)
- continue;
- MaterialTechnique* tech = getMaterialTechnique(node, j);
- if (!tech)
- continue;
-
- // Check here if the material technique refers to a render target texture with camera(s) attached
- // Only check this for the main view (null rendertarget)
- if ((!mRenderTarget) && (tech->getAuxViewFrameNumber() != mFrame.mFrameNumber))
- checkTechniqueForAuxView(tech);
-
- Batch newBatch;
- newBatch.mNode = node;
- newBatch.mCamera = mCamera;
- newBatch.mDistance = node->getDistance();
- newBatch.mGeometry = geom;
- newBatch.mBatchIndex = j;
-
- MaterialPass* pass = tech->getPass(PASS_AMBIENT);
- if (pass)
- {
- mPipeline->setBatchShaders(newBatch, tech, pass);
- if (pass->getBlendMode() == BLEND_REPLACE)
- {
- newBatch.mHasPriority = !(pass->getAlphaTest() || pass->getAlphaMask());
- newBatch.calculateSortKey(true, true);
- mAmbientQueue.push_back(newBatch);
- }
- else
- {
- newBatch.mHasPriority = true;
- newBatch.calculateSortKey(false, false);
- mTransparentQueue.push_back(newBatch);
- }
- continue;
- }
- else
- {
- // If no ambient pass, check for postopaque (custom) pass
- pass = tech->getPass(PASS_POSTOPAQUE);
- if (pass)
- {
- newBatch.mHasPriority = false;
- mPipeline->setBatchShaders(newBatch, tech, pass);
- newBatch.calculateSortKey(true, true);
- mPostOpaqueQueue.push_back(newBatch);
- }
- }
- }
- }
- }
-
- // Go through lights
- {
- PROFILE_MULTIPLE(Pipeline_GetLightBatches, mLights.size());
-
- unsigned lightQueueCount = 0;
- for (unsigned i = 0; i < mLights.size(); ++i)
- {
- Light* light = mLights[i];
- unsigned splits = processLight(light);
-
- if (!splits)
- continue;
-
- // For split point lights, check that a transparent object is not lit multiple times
- bool splitPointLight = sSplitLights[0]->getLightType() == LIGHT_SPLITPOINT;
- static std::set<GeometryNode*> litTransparencies;
- litTransparencies.clear();
-
- // Negative (darkening) lighting flag
- bool negative = mLights[i]->isNegative();
-
- // Prepare lit object + shadow caster queues for each split
- if (mLightQueues.size() < lightQueueCount + splits)
- mLightQueues.resize(lightQueueCount + splits);
- unsigned prevLightQueueCount = lightQueueCount;
-
- for (unsigned j = 0; j < splits; ++j)
- {
- LightBatchQueue& lightQueue = mLightQueues[lightQueueCount];
- lightQueue.mLight = sSplitLights[j];
- lightQueue.mShadowBatches.clear();
- lightQueue.mBatches.clear();
- lightQueue.mLastSplit = false;
-
- // Loop through shadow casters
- Camera& shadowCamera = sSplitLights[j]->getShadowCamera();
- for (unsigned k = 0; k < sShadowCasters[j].size(); ++k)
- {
- GeometryNode* node = sShadowCasters[j][k];
- unsigned numBatches = node->getNumBatches();
-
- for (unsigned l = 0; l < numBatches; ++l)
- {
- MaterialTechnique* tech = getMaterialTechnique(node, l);
- if (!tech)
- continue;
- MaterialPass* pass = tech->getPass(PASS_SHADOW);
- // Skip if material has no shadow pass
- if (!pass)
- continue;
- Geometry* geom = node->getBatchGeometry(l);
- if (!geom)
- continue;
-
- // Build the shadow batch
- // Note: shadow cameras are never parented, so can simply use getPosition()
- Batch shadowBatch;
- shadowBatch.mNode = node;
- shadowBatch.mCamera = &shadowCamera;
- shadowBatch.mDistance = (node->getWorldPosition() - shadowCamera.getPosition()).getLengthFast();
- shadowBatch.mGeometry = geom;
- shadowBatch.mBatchIndex = l;
- shadowBatch.mForwardLight = sSplitLights[j];
- shadowBatch.mHasPriority = !(pass->getAlphaTest() || pass->getAlphaMask());
-
- mPipeline->setBatchShaders(shadowBatch, tech, pass);
- shadowBatch.calculateSortKey(true, true);
- lightQueue.mShadowBatches.push_back(shadowBatch);
- }
- }
-
- // Loop through lit geometries
- for (unsigned k = 0; k < sLitGeometries[j].size(); ++k)
- {
- GeometryNode* node = sLitGeometries[j][k];
- unsigned numBatches = node->getNumBatches();
-
- for (unsigned l = 0; l < numBatches; ++l)
- {
- MaterialTechnique* tech = getMaterialTechnique(node, l);
- if (!tech)
- continue;
-
- MaterialPass* pass = 0;
- if (!negative)
- pass = tech->getPass(PASS_LIGHT);
- else
- pass = tech->getPass(PASS_NEGATIVE);
- // Skip if material does not receive light
- if (!pass)
- continue;
-
- Geometry* geom = node->getBatchGeometry(l);
- if (!geom)
- continue;
-
- // Build the lit batch
- Batch newBatch;
- newBatch.mNode = node;
- newBatch.mCamera = mCamera;
- newBatch.mDistance = node->getDistance();
- newBatch.mGeometry = geom;
- newBatch.mBatchIndex = l;
- newBatch.mForwardLight = sSplitLights[j];
-
- // Check from the ambient pass whether the object is opaque
- MaterialPass* ambientPass = tech->getPass(PASS_AMBIENT);
- if ((!ambientPass) || (ambientPass->getBlendMode() == BLEND_REPLACE))
- {
- mPipeline->setBatchShaders(newBatch, tech, pass);
- newBatch.calculateSortKey(true, true);
- if (!negative)
- lightQueue.mBatches.push_back(newBatch);
- else
- mNegativeQueue.push_back(newBatch);
- }
- else
- {
- // Transparent: disable shadows
- // Prevent multi-lighting by a split point light
- // (transparent rendering can not handle the needed stencil masking)
- if (splitPointLight)
- {
- if (litTransparencies.find(node) != litTransparencies.end())
- continue;
- // Use the original light instead of the split one, to choose correct scissor
- newBatch.mForwardLight = mLights[i];
- }
-
- // If light is negative, bias the distance slightly to ensure it has priority
- if (negative)
- newBatch.mDistance -= 0.001f;
-
- mPipeline->setBatchShaders(newBatch, tech, pass, false);
- newBatch.calculateSortKey(false, false);
- mTransparentQueue.push_back(newBatch);
- }
- }
-
- if (splitPointLight)
- litTransparencies.insert(node);
- }
-
- // Store light & its shadow batches only if per-pixel lit geometries exist
- if (sLitGeometries[j].size())
- lightQueueCount++;
- }
-
- // Mark the last split
- if (lightQueueCount != prevLightQueueCount)
- mLightQueues[lightQueueCount - 1].mLastSplit = true;
- }
-
- // Resize the light queue vector now that final size is known
- mLightQueues.resize(lightQueueCount);
- }
-
- // Finally sort the batches
- {
- PROFILE(View_SortBatches);
-
- sortBatches(mAmbientQueue, mAmbientQueueSorted);
- sortBatches(mNegativeQueue, mNegativeQueueSorted);
- sortBatches(mPostOpaqueQueue, mPostOpaqueQueueSorted);
- sortBatches(mTransparentQueue, mTransparentQueueSorted);
-
- for (unsigned i = 0; i < mLightQueues.size(); ++i)
- {
- sortBatches(mLightQueues[i].mShadowBatches, mLightQueues[i].mSortedShadowBatches);
- sortBatches(mLightQueues[i].mBatches, mLightQueues[i].mSortedBatches);
- }
- }
- }
- void View::renderBatchesForward()
- {
- Renderer* renderer = mPipeline->getRenderer();
-
- {
- // Render opaque objects' ambient & negative lighting
- PROFILE(View_RenderAmbient);
-
- clearLastParameterSources();
-
- renderer->setColorWrite(true);
- renderer->setFillMode(FILL_SOLID);
- renderer->setStencilTest(false);
- renderer->setRenderTarget(0, mRenderTarget);
- renderer->setDepthStencil(mDepthStencil);
- renderer->clear(CLEAR_COLOR | CLEAR_DEPTH | CLEAR_STENCIL, mZone->getFogColor());
-
- for (unsigned i = 0; i < mAmbientQueueSorted.size(); ++i)
- mAmbientQueueSorted[i]->draw(renderer);
-
- for (unsigned i = 0; i < mNegativeQueueSorted.size(); ++i)
- {
- optimizeLightByScissor(mNegativeQueueSorted[i]->mForwardLight);
- mNegativeQueueSorted[i]->draw(renderer);
- }
- }
-
- {
- // Render shadow maps + opaque objects' shadowed additive lighting
- PROFILE(View_RenderLights);
-
- for (unsigned i = 0; i < mLightQueues.size(); ++i)
- {
- LightBatchQueue& queue = mLightQueues[i];
-
- if (queue.mLight->getShadowMap())
- {
- PROFILE(View_RenderShadowMap);
-
- Texture2D* shadowMap = queue.mLight->getShadowMap();
- clearLastParameterSources();
-
- renderer->setColorWrite(false);
- renderer->setStencilTest(false);
- renderer->setTexture(TU_SHADOWMAP, 0);
- renderer->setRenderTarget(0, shadowMap->getRenderSurface()->getLinkedRenderTarget());
- renderer->setDepthStencil(shadowMap);
- renderer->clear(CLEAR_DEPTH);
-
- // Set shadow depth bias
- BiasParameters parameters = queue.mLight->getShadowBias();
- renderer->setDepthBias(parameters.mConstantBias, parameters.mSlopeScaledBias);
-
- // Set a scissor rectangle to match possible shadow map size reduction by out-zooming
- // However, do not do this for point lights
- if (queue.mLight->getLightType() != LIGHT_SPLITPOINT)
- {
- float zoom = min(queue.mLight->getShadowCamera().getZoom(),
- (float)(shadowMap->getWidth() - 2) / (float)shadowMap->getWidth());
- Rect zoomRect(Vector2(-1.0f, -1.0f) * zoom, Vector2(1.0f, 1.0f) * zoom);
- renderer->setScissorTest(true, zoomRect, false);
- }
- else
- renderer->setScissorTest(false);
-
- std::vector<Batch*>& sortedBatches = queue.mSortedShadowBatches;
- if (sortedBatches.size())
- {
- for (unsigned j = 0; j < sortedBatches.size(); ++j)
- sortedBatches[j]->draw(renderer);
- }
-
- renderer->setColorWrite(true);
- renderer->setDepthBias(0.0f, 0.0f);
- }
-
- std::vector<Batch*>& sortedBatches = queue.mSortedBatches;
- LightType type = queue.mLight->getLightType();
- if (sortedBatches.size())
- {
- clearLastParameterSources();
-
- renderer->setRenderTarget(0, mRenderTarget);
- renderer->setDepthStencil(mDepthStencil);
- optimizeLightByScissor(queue.mLight);
-
- // If this is a split point or dir. light, mark the split volume to the stencil
- if ((type == LIGHT_DIRECTIONAL) || (type == LIGHT_SPLITPOINT))
- mPipeline->drawSplitLightToStencil(*mCamera, queue.mLight);
-
- for (unsigned j = 0; j < sortedBatches.size(); ++j)
- sortedBatches[j]->draw(renderer);
- }
-
- // Clear the stencil buffer after the last split
- if ((queue.mLastSplit) && ((type == LIGHT_DIRECTIONAL) || (type == LIGHT_SPLITPOINT)))
- {
- renderer->setScissorTest(false);
- mPipeline->drawSplitLightToStencil(*mCamera, queue.mLight, true);
- }
- }
- }
-
- {
- // Render post-opaque passes
- PROFILE(View_RenderForward);
-
- clearLastParameterSources();
-
- renderer->setScissorTest(false);
- renderer->setStencilTest(false);
- renderer->setRenderTarget(0, mRenderTarget);
- renderer->setDepthStencil(mDepthStencil);
-
- for (unsigned i = 0; i < mPostOpaqueQueueSorted.size(); ++i)
- mPostOpaqueQueueSorted[i]->draw(renderer);
- }
-
- {
- // Render transparent objects last (both ambient & additive lighting)
- PROFILE(View_RenderTransparent);
-
- for (unsigned i = 0; i < mTransparentQueueSorted.size(); ++i)
- {
- optimizeLightByScissor(mTransparentQueueSorted[i]->mForwardLight);
- mTransparentQueueSorted[i]->draw(renderer);
- }
- }
- }
|