| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512 |
- //
- // 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 "Camera.h"
- #include "CoreEvents.h"
- #include "DebugRenderer.h"
- #include "Geometry.h"
- #include "Graphics.h"
- #include "GraphicsEvents.h"
- #include "GraphicsImpl.h"
- #include "IndexBuffer.h"
- #include "Light.h"
- #include "Log.h"
- #include "Material.h"
- #include "OcclusionBuffer.h"
- #include "Octree.h"
- #include "OctreeQuery.h"
- #include "Profiler.h"
- #include "Renderer.h"
- #include "ResourceCache.h"
- #include "Scene.h"
- #include "Shader.h"
- #include "ShaderVariation.h"
- #include "Technique.h"
- #include "Texture2D.h"
- #include "TextureCube.h"
- #include "VertexBuffer.h"
- #include "View.h"
- #include "XMLFile.h"
- #include "Zone.h"
- #include "DebugNew.h"
- static const float dirLightVertexData[] =
- {
- -1, 1, 0,
- 1, 1, 0,
- 1, -1, 0,
- -1, -1, 0,
- };
- static const unsigned short dirLightIndexData[] =
- {
- 0, 1, 2,
- 2, 3, 0,
- };
- static const float pointLightVertexData[] =
- {
- -0.423169f, -1.000000f, 0.423169f,
- -0.423169f, -1.000000f, -0.423169f,
- 0.423169f, -1.000000f, -0.423169f,
- 0.423169f, -1.000000f, 0.423169f,
- 0.423169f, 1.000000f, -0.423169f,
- -0.423169f, 1.000000f, -0.423169f,
- -0.423169f, 1.000000f, 0.423169f,
- 0.423169f, 1.000000f, 0.423169f,
- -1.000000f, 0.423169f, -0.423169f,
- -1.000000f, -0.423169f, -0.423169f,
- -1.000000f, -0.423169f, 0.423169f,
- -1.000000f, 0.423169f, 0.423169f,
- 0.423169f, 0.423169f, -1.000000f,
- 0.423169f, -0.423169f, -1.000000f,
- -0.423169f, -0.423169f, -1.000000f,
- -0.423169f, 0.423169f, -1.000000f,
- 1.000000f, 0.423169f, 0.423169f,
- 1.000000f, -0.423169f, 0.423169f,
- 1.000000f, -0.423169f, -0.423169f,
- 1.000000f, 0.423169f, -0.423169f,
- 0.423169f, -0.423169f, 1.000000f,
- 0.423169f, 0.423169f, 1.000000f,
- -0.423169f, 0.423169f, 1.000000f,
- -0.423169f, -0.423169f, 1.000000f
- };
- static const unsigned short pointLightIndexData[] =
- {
- 0, 1, 2,
- 0, 2, 3,
- 4, 5, 6,
- 4, 6, 7,
- 8, 9, 10,
- 8, 10, 11,
- 12, 13, 14,
- 12, 14, 15,
- 16, 17, 18,
- 16, 18, 19,
- 20, 21, 22,
- 20, 22, 23,
- 0, 10, 9,
- 0, 9, 1,
- 13, 2, 1,
- 13, 1, 14,
- 23, 0, 3,
- 23, 3, 20,
- 17, 3, 2,
- 17, 2, 18,
- 21, 7, 6,
- 21, 6, 22,
- 7, 16, 19,
- 7, 19, 4,
- 5, 8, 11,
- 5, 11, 6,
- 4, 12, 15,
- 4, 15, 5,
- 22, 11, 10,
- 22, 10, 23,
- 8, 15, 14,
- 8, 14, 9,
- 12, 19, 18,
- 12, 18, 13,
- 16, 21, 20,
- 16, 20, 17,
- 0, 23, 10,
- 1, 9, 14,
- 2, 13, 18,
- 3, 17, 20,
- 6, 11, 22,
- 5, 15, 8,
- 4, 19, 12,
- 7, 21, 16
- };
- static const float spotLightVertexData[] =
- {
- // Use slightly clamped Z-range so that shadowed point light splits line up nicely
- 0.00001f, 0.00001f, 0.00001f,
- 0.00001f, -0.00001f, 0.00001f,
- -0.00001f, -0.00001f, 0.00001f,
- -0.00001f, 0.00001f, 0.00001f,
- 1.00000f, 1.00000f, 0.99999f,
- 1.00000f, -1.00000f, 0.99999f,
- -1.00000f, -1.00000f, 0.99999f,
- -1.00000f, 1.00000f, 0.99999f,
- };
- static const unsigned short spotLightIndexData[] =
- {
- 3, 0, 1,
- 3, 1, 2,
- 0, 4, 5,
- 0, 5, 1,
- 3, 7, 4,
- 3, 4, 0,
- 7, 3, 2,
- 7, 2, 6,
- 6, 2, 1,
- 6, 1, 5,
- 7, 5, 4,
- 7, 6, 5
- };
- static const String hwVariations[] =
- {
- "",
- "HW"
- };
- static const String geometryVSVariations[] =
- {
- "",
- "Skinned",
- "Instanced",
- "Billboard"
- };
- static const String lightVSVariations[] =
- {
- "",
- "Spot",
- "Shadow",
- "SpotShadow"
- };
- static const String deferredLightVSVariations[] =
- {
- "",
- "Dir",
- "Ortho",
- "OrthoDir"
- };
- static const String lightPSVariations[] =
- {
- "Dir",
- "DirSpec",
- "DirShadow",
- "DirShadowSpec",
- "Spot",
- "SpotSpec",
- "SpotShadow",
- "SpotShadowSpec",
- "Point",
- "PointSpec",
- "PointShadow",
- "PointShadowSpec",
- "PointMask",
- "PointMaskSpec",
- "PointMaskShadow",
- "PointMaskShadowSpec",
- "OrthoDir",
- "OrthoDirSpec",
- "OrthoDirShadow",
- "OrthoDirShadowSpec",
- "OrthoSpot",
- "OrthoSpotSpec",
- "OrthoSpotShadow",
- "OrthoSpotShadowSpec",
- "OrthoPoint",
- "OrthoPointSpec",
- "OrthoPointShadow",
- "OrthoPointShadowSpec",
- "OrthoPointMask",
- "OrthoPointMaskSpec",
- "OrthoPointMaskShadow",
- "OrthoPointMaskShadowSpec"
- };
- static const unsigned INSTANCING_BUFFER_MASK = MASK_INSTANCEMATRIX1 | MASK_INSTANCEMATRIX2 | MASK_INSTANCEMATRIX3;
- static const Viewport noViewport;
- void EdgeFilterParameters::Validate()
- {
- radius_ = Max(radius_, 0.0f);
- threshold_ = Max(threshold_, 0.0f);
- strength_ = Max(strength_, 0.0f);
- }
- OBJECTTYPESTATIC(Renderer);
- Renderer::Renderer(Context* context) :
- Object(context),
- defaultZone_(new Zone(context)),
- numViews_(0),
- numShadowCameras_(0),
- numSplitLights_(0),
- numTempNodes_(0),
- specularLighting_(true),
- drawShadows_(true),
- textureAnisotropy_(4),
- textureFilterMode_(FILTER_TRILINEAR),
- textureQuality_(QUALITY_HIGH),
- materialQuality_(QUALITY_HIGH),
- shadowMapSize_(1024),
- shadowMapHiresDepth_(false),
- reuseShadowMaps_(true),
- dynamicInstancing_(true),
- edgeFilter_(EdgeFilterParameters(0.4f, 0.5f, 0.9f)),
- maxOccluderTriangles_(5000),
- occlusionBufferSize_(256),
- occluderSizeThreshold_(0.1f),
- shadersChangedFrameNumber_(M_MAX_UNSIGNED),
- shadersDirty_(true),
- initialized_(false)
- {
- SubscribeToEvent(E_SCREENMODE, HANDLER(Renderer, HandleScreenMode));
- SubscribeToEvent(E_RENDERUPDATE, HANDLER(Renderer, HandleRenderUpdate));
-
- // Default to one of each shadow map resolution
- for (unsigned i = 0; i < NUM_SHADOWMAP_RESOLUTIONS; ++i)
- shadowMaps_[i].Resize(1);
-
- // Try to initialize right now, but skip if screen mode is not yet set
- Initialize();
- }
- Renderer::~Renderer()
- {
- }
- void Renderer::SetNumViewports(unsigned num)
- {
- viewports_.Resize(num);
- }
- void Renderer::SetViewport(unsigned index, const Viewport& viewport)
- {
- if (index >= viewports_.Size())
- {
- LOGERROR("Viewport index out of bounds");
- return;
- }
-
- viewports_[index] = viewport;
- }
- void Renderer::SetSpecularLighting(bool enable)
- {
- specularLighting_ = enable;
- }
- void Renderer::SetDrawShadows(bool enable)
- {
- if (!graphics_)
- return;
-
- drawShadows_ = enable;
-
- if (!CreateShadowMaps())
- drawShadows_ = false;
- }
- void Renderer::SetTextureAnisotropy(int level)
- {
- textureAnisotropy_ = Max(level, 1);
- }
- void Renderer::SetTextureFilterMode(TextureFilterMode mode)
- {
- textureFilterMode_ = mode;
- }
- void Renderer::SetTextureQuality(int quality)
- {
- quality = Clamp(quality, QUALITY_LOW, QUALITY_HIGH);
-
- if (quality != textureQuality_)
- {
- textureQuality_ = quality;
- ReloadTextures();
- }
- }
- void Renderer::SetMaterialQuality(int quality)
- {
- materialQuality_ = Clamp(quality, QUALITY_LOW, QUALITY_MAX);
- shadersDirty_ = true;
- ResetViews();
- }
- void Renderer::SetShadowMapSize(int size)
- {
- if (!graphics_)
- return;
-
- shadowMapSize_ = Max(size, SHADOW_MIN_PIXELS);
-
- if (!CreateShadowMaps())
- {
- shadowMapSize_ = 1024;
- if (!CreateShadowMaps())
- drawShadows_ = false;
- }
- }
- void Renderer::SetShadowMapHiresDepth(bool enable)
- {
- if (!graphics_)
- return;
-
- if (!graphics_->GetHiresShadowSupport())
- enable = false;
-
- shadowMapHiresDepth_ = enable;
- if (!CreateShadowMaps())
- drawShadows_ = false;
- }
- void Renderer::SetReuseShadowMaps(bool enable)
- {
- if (enable == reuseShadowMaps_)
- return;
-
- reuseShadowMaps_ = enable;
- if (reuseShadowMaps_)
- {
- for (unsigned i = 0; i < NUM_SHADOWMAP_RESOLUTIONS; ++i)
- shadowMaps_[i].Resize(1);
- }
- if (!CreateShadowMaps())
- drawShadows_ = false;
- shadersDirty_ = true;
- }
- void Renderer::SetNumShadowMaps(unsigned full, unsigned half, unsigned quarter)
- {
- shadowMaps_[0].Resize(full ? full : 1);
- shadowMaps_[1].Resize(half ? half : 1);
- shadowMaps_[2].Resize(quarter ? quarter : 1);
- if (!CreateShadowMaps())
- drawShadows_ = false;
- }
- void Renderer::SetDynamicInstancing(bool enable)
- {
- if (!instancingBuffer_)
- enable = false;
-
- dynamicInstancing_ = enable;
- }
- void Renderer::SetEdgeFilter(const EdgeFilterParameters& parameters)
- {
- edgeFilter_ = parameters;
- edgeFilter_.Validate();
- }
- void Renderer::SetMaxOccluderTriangles(int triangles)
- {
- maxOccluderTriangles_ = Max(triangles, 0);
- }
- void Renderer::SetOcclusionBufferSize(int size)
- {
- occlusionBufferSize_ = Max(size, 1);
- occlusionBuffers_.Clear();
- }
- void Renderer::SetOccluderSizeThreshold(float screenSize)
- {
- occluderSizeThreshold_ = Max(screenSize, 0.0f);
- }
- const Viewport& Renderer::GetViewport(unsigned index) const
- {
- return index < viewports_.Size() ? viewports_[index] : noViewport;
- }
- ShaderVariation* Renderer::GetVertexShader(const String& name, bool checkExists) const
- {
- return GetShader(name, vsFormat_, checkExists);
- }
- ShaderVariation* Renderer::GetPixelShader(const String& name, bool checkExists) const
- {
- return GetShader(name, psFormat_, checkExists);
- }
- unsigned Renderer::GetNumGeometries(bool allViews) const
- {
- unsigned numGeometries = 0;
- unsigned lastView = allViews ? numViews_ : 1;
-
- for (unsigned i = 0; i < lastView; ++i)
- numGeometries += views_[i]->GetGeometries().Size();
-
- return numGeometries;
- }
- unsigned Renderer::GetNumLights(bool allViews) const
- {
- unsigned nulights_ = 0;
- unsigned lastView = allViews ? numViews_ : 1;
-
- for (unsigned i = 0; i < lastView; ++i)
- nulights_ += views_[i]->GetLights().Size();
-
- return nulights_;
- }
- unsigned Renderer::GetNumShadowMaps(bool allViews) const
- {
- unsigned numShadowMaps = 0;
- unsigned lastView = allViews ? numViews_ : 1;
-
- for (unsigned i = 0; i < lastView; ++i)
- {
- const Vector<LightBatchQueue>& lightQueues = views_[i]->GetLightQueues();
-
- for (unsigned j = 0; j < lightQueues.Size(); ++j)
- {
- Light* light = lightQueues[j].light_;
- if (light && light->GetShadowMap())
- ++numShadowMaps;
- }
- }
-
- return numShadowMaps;
- }
- unsigned Renderer::GetNumOccluders(bool allViews) const
- {
- unsigned numOccluders = 0;
- unsigned lastView = allViews ? numViews_ : 1;
-
- for (unsigned i = 0; i < lastView; ++i)
- numOccluders += views_[i]->GetOccluders().Size();
-
- return numOccluders;
- }
- unsigned Renderer::GetNumShadowOccluders(bool allViews) const
- {
- unsigned numShadowOccluders = 0;
- unsigned lastView = allViews ? numViews_ : 1;
-
- for (unsigned i = 0; i < lastView; ++i)
- numShadowOccluders += views_[i]->GetShadowOccluders().Size();
-
- return numShadowOccluders;
- }
- const OcclusionBuffer* Renderer::GetOcclusionBuffer(float aspectRatio, bool halfResolution)
- {
- // Return an occlusion buffer for debug output purposes. Do not allocate new
- int width = occlusionBufferSize_;
- int height = (int)(occlusionBufferSize_ / aspectRatio);
- if (halfResolution)
- {
- width >>= 1;
- height >>= 1;
- }
- int searchKey = (width << 16) | height;
-
- HashMap<int, SharedPtr<OcclusionBuffer> >::Iterator i = occlusionBuffers_.Find(searchKey);
- if (i != occlusionBuffers_.End())
- return i->second_;
- else
- return 0;
- }
- void Renderer::Update(float timeStep)
- {
- PROFILE(UpdateViewports);
-
- numViews_ = 0;
-
- // If device lost, do not perform update. This is because any dynamic vertex/index buffer updates happen already here,
- // and if the device is lost, the updates queue up, causing memory use to rise constantly
- if (!graphics_ || !graphics_->IsInitialized() || graphics_->IsDeviceLost())
- return;
-
- // Advance frame number & time, set up the frameinfo structure, and reset views & stats
- frame_.frameNumber_ = GetSubsystem<Time>()->GetFrameNumber();
- frame_.timeStep_ = timeStep;
- frame_.camera_ = 0;
- numShadowCameras_ = 0;
- numSplitLights_ = 0;
- numTempNodes_ = 0;
- updateOctrees_.Clear();
-
- // Reload shaders if needed
- if (shadersDirty_)
- LoadShaders();
-
- // Process all viewports. Use reverse order, because during rendering the order will be reversed again to handle auxiliary
- // view dependencies correctly
- for (unsigned i = viewports_.Size() - 1; i < viewports_.Size(); --i)
- {
- unsigned mainView = numViews_;
- Viewport& viewport = viewports_[i];
- if (!AddView(0, viewport))
- continue;
-
- // Update octree (perform early update for nodes which need that, and reinsert moved nodes.)
- // However, if the same scene is viewed from multiple cameras, update the octree only once
- Octree* octree = viewport.scene_->GetComponent<Octree>();
- DebugRenderer* debug = viewport.scene_->GetComponent<DebugRenderer>();
- if (updateOctrees_.Find(octree) == updateOctrees_.End())
- {
- frame_.camera_ = viewport.camera_;
- frame_.viewSize_ = IntVector2(viewport.rect_.right_ - viewport.rect_.left_, viewport.rect_.bottom_ - viewport.rect_.top_);
- if (frame_.viewSize_ == IntVector2::ZERO)
- frame_.viewSize_ = IntVector2(graphics_->GetWidth(), graphics_->GetHeight());
- octree->Update(frame_);
- updateOctrees_.Insert(octree);
-
- // Set also the view for the debug graphics already here, so that it can use culling
- /// \todo May result in incorrect debug geometry culling if the same scene is drawn from multiple viewports
- if (debug)
- debug->SetView(viewport.camera_);
- }
-
- // Update the viewport's main view and any auxiliary views it Creates
- for (unsigned i = mainView; i < numViews_; ++i)
- views_[i]->Update(frame_);
- }
-
- return;
- }
- void Renderer::Render()
- {
- if (!graphics_)
- return;
-
- PROFILE(RenderViewports);
-
- graphics_->SetDefaultTextureFilterMode(textureFilterMode_);
- graphics_->SetTextureAnisotropy(textureAnisotropy_);
-
- // If no views, just clear the screen
- if (!numViews_)
- {
- numPrimitives_ = 0;
- numBatches_ = 0;
- graphics_->SetAlphaTest(false);
- graphics_->SetBlendMode(BLEND_REPLACE);
- graphics_->SetColorWrite(true);
- graphics_->SetDepthWrite(true);
- graphics_->SetFillMode(FILL_SOLID);
- graphics_->SetScissorTest(false);
- graphics_->SetStencilTest(false);
- graphics_->Clear(CLEAR_COLOR | CLEAR_DEPTH | CLEAR_STENCIL);
- return;
- }
-
- // Render views from last to first (each main view is rendered after the auxiliary views it depends on)
- for (unsigned i = numViews_ - 1; i < numViews_; --i)
- views_[i]->Render();
-
- // Disable scissor/stencil tests if left on by lights, and reset stream frequencies
- graphics_->SetScissorTest(false);
- graphics_->SetStencilTest(false);
- graphics_->ResetStreamFrequencies();
-
- // Copy the number of batches & primitives from Graphics so that we can account for 3D geometry only
- numPrimitives_ = graphics_->GetNumPrimitives();
- numBatches_ = graphics_->GetNumBatches();
- }
- void Renderer::DrawDebugGeometry(bool depthTest)
- {
- PROFILE(RendererDrawDebug);
-
- /// \todo Because debug geometry is per-scene, if two cameras show views of the same area, occlusion is not shown correctly
- HashSet<Drawable*> processedGeometries;
- HashSet<Light*> processedLights;
-
- for (unsigned i = 0; i < numViews_; ++i)
- {
- // Make sure it's a main view, and process each node only once
- View* view = views_[i];
- if (view->GetRenderTarget())
- continue;
- Octree* octree = view->GetOctree();
- if (!octree)
- continue;
- Scene* scene = static_cast<Scene*>(octree->GetNode());
- if (!scene)
- continue;
- DebugRenderer* debug = scene->GetComponent<DebugRenderer>();
- if (!debug)
- continue;
-
- const PODVector<Drawable*>& geometries = view->GetGeometries();
- const PODVector<Light*>& lights = view->GetLights();
-
- for (unsigned i = 0; i < geometries.Size(); ++i)
- {
- if (processedGeometries.Find(geometries[i]) == processedGeometries.End())
- {
- geometries[i]->DrawDebugGeometry(debug, depthTest);
- processedGeometries.Insert(geometries[i]);
- }
- }
- for (unsigned i = 0; i < lights.Size(); ++i)
- {
- if (processedLights.Find(lights[i]) == processedLights.End())
- {
- lights[i]->DrawDebugGeometry(debug, depthTest);
- processedLights.Insert(lights[i]);
- }
- }
- }
- }
- void Renderer::Initialize()
- {
- Graphics* graphics = GetSubsystem<Graphics>();
- ResourceCache* cache = GetSubsystem<ResourceCache>();
-
- if (!graphics || !graphics->IsInitialized() || !cache)
- return;
-
- PROFILE(InitRenderer);
-
- graphics_ = graphics;
- cache_ = cache;
-
- // Check shader model support
- #ifndef USE_OPENGL
- if (graphics_->GetSM3Support())
- {
- shaderPath_ = "Shaders/SM3/";
- vsFormat_ = ".vs3";
- psFormat_ = ".ps3";
- }
- else
- {
- shaderPath_ = "Shaders/SM2/";
- vsFormat_ = ".vs2";
- psFormat_ = ".ps2";
- }
- #else
- {
- shaderPath_ = "Shaders/GLSL/";
- vsFormat_ = ".vert";
- psFormat_ = ".frag";
- }
- #endif
-
- defaultLightRamp_ = cache->GetResource<Texture2D>("Textures/Ramp.png");
- defaultLightSpot = cache->GetResource<Texture2D>("Textures/Spot.png");
- defaultMaterial_ = cache->GetResource<Material>("Materials/Default.xml");
-
- CreateGeometries();
- CreateInstancingBuffer();
-
- if (!CreateShadowMaps())
- drawShadows_ = false;
-
- viewports_.Resize(1);
- ResetViews();
-
- LOGINFO("Initialized renderer");
- initialized_ = true;
- }
- void Renderer::ResetViews()
- {
- views_.Clear();
- numViews_ = 0;
- }
- bool Renderer::AddView(RenderSurface* renderTarget, const Viewport& viewport)
- {
- // If using a render target texture, make sure it is supported, and will not be rendered to multiple times
- if (renderTarget)
- {
- if (!graphics_->GetRenderTargetSupport())
- return false;
-
- for (unsigned i = 0; i < numViews_; ++i)
- {
- if (views_[i]->GetRenderTarget() == renderTarget)
- return false;
- }
- }
-
- if (views_.Size() <= numViews_)
- views_.Resize(numViews_ + 1);
- if (!views_[numViews_])
- views_[numViews_] = new View(context_);
-
- if (views_[numViews_]->Define(renderTarget, viewport))
- {
- ++numViews_;
- return true;
- }
- else
- return false;
- }
- OcclusionBuffer* Renderer::GetOrCreateOcclusionBuffer(Camera* camera, int maxOccluderTriangles, bool halfResolution)
- {
- // Get an occlusion buffer matching the aspect ratio. If not found, allocate new
- int width = occlusionBufferSize_;
- int height = (int)(occlusionBufferSize_ / camera->GetAspectRatio());
- if (halfResolution)
- {
- width >>= 1;
- height >>= 1;
- }
- int searchKey = (width << 16) | height;
-
- SharedPtr<OcclusionBuffer> buffer;
- HashMap<int, SharedPtr<OcclusionBuffer> >::Iterator i = occlusionBuffers_.Find(searchKey);
- if (i != occlusionBuffers_.End())
- buffer = i->second_;
- else
- {
- buffer = new OcclusionBuffer(context_);
- buffer->SetSize(width, height);
- occlusionBuffers_[searchKey] = buffer;
- }
-
- buffer->SetView(camera);
- buffer->SetMaxTriangles(maxOccluderTriangles);
- buffer->Clear();
-
- return buffer;
- }
- Geometry* Renderer::GetLightGeometry(Light* light)
- {
- switch (light->GetLightType())
- {
- case LIGHT_POINT:
- return pointLightGeometry_;
-
- case LIGHT_SPOT:
- case LIGHT_SPLITPOINT:
- return spotLightGeometry_;
-
- default:
- return dirLightGeometry_;
- }
- }
- Texture2D* Renderer::GetShadowMap(float resolution)
- {
- unsigned index = 0;
- if (resolution < 0.75f)
- index = (resolution >= 0.375f) ? 1 : 2;
-
- if (reuseShadowMaps_)
- return shadowMaps_[index][0];
- else
- {
- // If higher resolution shadow maps already used up, fall back to lower resolutions
- while (index < NUM_SHADOWMAP_RESOLUTIONS)
- {
- if (shadowMapUseCount_[index] < shadowMaps_[index].Size())
- return shadowMaps_[index][shadowMapUseCount_[index]++];
- ++index;
- }
- return 0;
- }
- }
- void Renderer::ResetShadowMapUseCount()
- {
- for (unsigned i = 0; i < NUM_SHADOWMAP_RESOLUTIONS; ++i)
- shadowMapUseCount_[i] = 0;
- }
- ShaderVariation* Renderer::GetShader(const String& name, const String& extension, bool checkExists) const
- {
- String shaderName = shaderPath_;
- String variationName;
-
- unsigned split = name.Find('_');
- if (split != String::NPOS)
- {
- shaderName += name.Substring(0, split) + extension;
- variationName = name.Substring(split + 1);
- }
- else
- shaderName += name + extension;
-
- if (checkExists)
- {
- if (!cache_->Exists(shaderName))
- return 0;
- }
-
- Shader* shader = cache_->GetResource<Shader>(shaderName);
- if (shader)
- return shader->GetVariation(variationName);
- else
- return 0;
- }
- void Renderer::SetBatchShaders(Batch& batch, Technique* technique, Pass* pass, bool allowShadows)
- {
- batch.pass_ = pass;
-
- // Check if shaders are unloaded or need reloading
- Vector<SharedPtr<ShaderVariation> >& vertexShaders = pass->GetVertexShaders();
- Vector<SharedPtr<ShaderVariation> >& pixelShaders = pass->GetPixelShaders();
- if (!vertexShaders.Size() || !pixelShaders.Size() || technique->GetShadersLoadedFrameNumber() !=
- shadersChangedFrameNumber_)
- {
- // First release all previous shaders, then load
- technique->ReleaseShaders();
- LoadMaterialShaders(technique);
- }
-
- // Make sure shaders are loaded now
- if (vertexShaders.Size() && pixelShaders.Size())
- {
- // Check whether is a forward lit pass. If not, there is only one pixel shader
- PassType type = pass->GetType();
- if (type != PASS_LITBASE && type != PASS_LIGHT)
- {
- unsigned vsi = batch.geometryType_;
- batch.vertexShader_ = vertexShaders[vsi];
- batch.pixelShader_ = pixelShaders[0];
- batch.vertexShaderIndex_ = vsi;
- }
- else
- {
- Light* light = batch.light_;
- if (!light)
- {
- // Do not log error, as it would result in a lot of spam
- batch.vertexShader_ = 0;
- batch.pixelShader_ = 0;
- return;
- }
-
- unsigned vsi = 0;
- unsigned psi = 0;
- vsi = batch.geometryType_ * MAX_LIGHT_VS_VARIATIONS;
-
- if (specularLighting_ && light->GetSpecularIntensity() > 0.0f)
- psi += LPS_SPEC;
- if (allowShadows && light->GetShadowMap())
- {
- vsi += LVS_SHADOW;
- psi += LPS_SHADOW;
- }
-
- switch (light->GetLightType())
- {
- case LIGHT_POINT:
- case LIGHT_SPLITPOINT:
- if (light->GetShapeTexture())
- psi += LPS_POINTMASK;
- else
- psi += LPS_POINT;
- break;
-
- case LIGHT_SPOT:
- psi += LPS_SPOT;
- vsi += LVS_SPOT;
- break;
- }
-
- batch.vertexShader_ = vertexShaders[vsi];
- batch.pixelShader_ = pixelShaders[psi];
- batch.vertexShaderIndex_ = vsi;
- }
- }
-
- batch.CalculateSortKey();
-
- // Log error if shaders could not be assigned, but only once per technique
- if (!batch.vertexShader_ || !batch.pixelShader_)
- {
- if (shaderErrorDisplayed_.Find(technique) == shaderErrorDisplayed_.End())
- {
- shaderErrorDisplayed_.Insert(technique);
- LOGERROR("Technique " + technique->GetName() + " has missing shaders");
- }
- }
- }
- void Renderer::SetLightVolumeShaders(Batch& batch)
- {
- unsigned vsi = DLVS_NONE;
- unsigned psi = DLPS_NONE;
- Light* light = batch.light_;
-
- switch(light->GetLightType())
- {
- case LIGHT_DIRECTIONAL:
- vsi += DLVS_DIR;
- break;
-
- case LIGHT_POINT:
- case LIGHT_SPLITPOINT:
- if (light->GetShapeTexture())
- psi += DLPS_POINTMASK;
- else
- psi += DLPS_POINT;
- break;
-
- case LIGHT_SPOT:
- psi += DLPS_SPOT;
- break;
- }
-
- if (light->GetShadowMap())
- psi += DLPS_SHADOW;
-
- if (specularLighting_ && light->GetSpecularIntensity() > 0.0)
- psi += DLPS_SPEC;
-
- if (batch.camera_->IsOrthographic())
- {
- vsi += DLVS_ORTHO;
- psi += DLPS_ORTHO;
- }
-
- batch.material_ = 0;
- batch.pass_ = 0;
- batch.vertexShader_ = lightVS_[vsi];
- batch.pixelShader_ = lightPS_[psi];
- batch.vertexShaderIndex_ = vsi;
- batch.CalculateSortKey();
- }
- void Renderer::LoadShaders()
- {
- LOGINFO("Reloading shaders");
-
- // Release old material shaders, mark them for reload
- ReleaseMaterialShaders();
- shadersChangedFrameNumber_ = GetSubsystem<Time>()->GetFrameNumber();
-
- // Load inbuilt shaders
- stencilVS_ = GetVertexShader("Stencil");
- stencilPS_ = GetPixelShader("Stencil");
- lightVS_.Clear();
- lightPS_.Clear();
-
- RenderMode mode = graphics_->GetRenderMode();
- if (mode != RENDER_FORWARD)
- {
- lightVS_.Resize(MAX_DEFERRED_LIGHT_VS_VARIATIONS);
- lightPS_.Resize(MAX_DEFERRED_LIGHT_PS_VARIATIONS);
-
- unsigned hwShadows = graphics_->GetHardwareShadowSupport() ? 1 : 0;
-
- for (unsigned i = 0; i < MAX_DEFERRED_LIGHT_VS_VARIATIONS; ++i)
- lightVS_[i] = GetVertexShader("Light_" + deferredLightVSVariations[i]);
-
- for (unsigned i = 0; i < MAX_DEFERRED_LIGHT_PS_VARIATIONS; ++i)
- {
- unsigned variation = i % DLPS_SPOT;
- if (variation == DLPS_SHADOW || variation == DLPS_SHADOWSPEC)
- lightPS_[i] = GetPixelShader("Light_" + lightPSVariations[i] + hwVariations[hwShadows]);
- else
- lightPS_[i] = GetPixelShader("Light_" + lightPSVariations[i]);
- }
- }
-
- // Remove shaders that are no longer referenced from the cache
- cache_->ReleaseResources(Shader::GetTypeStatic());
-
- shadersDirty_ = false;
- }
- void Renderer::LoadMaterialShaders(Technique* technique)
- {
- LoadPassShaders(technique, PASS_SHADOW);
- LoadPassShaders(technique, PASS_EXTRA);
-
- RenderMode mode = graphics_->GetRenderMode();
- if (mode == RENDER_FORWARD)
- {
- LoadPassShaders(technique, PASS_BASE);
- LoadPassShaders(technique, PASS_LITBASE);
- LoadPassShaders(technique, PASS_LIGHT);
- }
- else
- {
- if (technique->HasPass(PASS_GBUFFER))
- LoadPassShaders(technique, PASS_GBUFFER);
- else
- {
- LoadPassShaders(technique, PASS_BASE);
- // If shadow maps are not reused, transparencies can be rendered shadowed
- LoadPassShaders(technique, PASS_LITBASE, !reuseShadowMaps_);
- LoadPassShaders(technique, PASS_LIGHT, !reuseShadowMaps_);
- }
- }
- }
- void Renderer::LoadPassShaders(Technique* technique, PassType pass, bool allowShadows)
- {
- Map<PassType, Pass>::Iterator i = technique->passes_.Find(pass);
- if (i == technique->passes_.End())
- return;
-
- String vertexShaderName = i->second_.GetVertexShaderName();
- String pixelShaderName = i->second_.GetPixelShaderName();
-
- // Check if the shader name is already a variation in itself
- if (vertexShaderName.Find('_') == String::NPOS)
- vertexShaderName += "_";
- if (pixelShaderName.Find('_') == String::NPOS)
- pixelShaderName += "_";
-
- // If ambient pass is transparent, and shadow maps are reused, do not load shadow variations
- if (reuseShadowMaps_ && (pass == PASS_LIGHT || pass == PASS_LITBASE))
- {
- if (!technique->HasPass(PASS_BASE) || technique->GetPass(PASS_BASE)->GetBlendMode() != BLEND_REPLACE)
- allowShadows = false;
- }
-
- unsigned hwShadows = graphics_->GetHardwareShadowSupport() ? 1 : 0;
-
- Vector<SharedPtr<ShaderVariation> >& vertexShaders = i->second_.GetVertexShaders();
- Vector<SharedPtr<ShaderVariation> >& pixelShaders = i->second_.GetPixelShaders();
-
- // Forget all the old shaders
- vertexShaders.Clear();
- pixelShaders.Clear();
-
- switch (i->first_)
- {
- default:
- vertexShaders.Resize(MAX_GEOMETRYTYPES);
- pixelShaders.Resize(1);
- for (unsigned j = 0; j < MAX_GEOMETRYTYPES; ++j)
- vertexShaders[j] = GetVertexShader(vertexShaderName + geometryVSVariations[j], j != 0);
- pixelShaders[0] = GetPixelShader(pixelShaderName);
- break;
-
- case PASS_LITBASE:
- case PASS_LIGHT:
- {
- // In first light pass, load only directional light shaders
- unsigned numPS = i->first_ == PASS_LIGHT ? MAX_LIGHT_PS_VARIATIONS : LPS_SPOT;
-
- vertexShaders.Resize(MAX_GEOMETRYTYPES * MAX_LIGHT_VS_VARIATIONS);
- pixelShaders.Resize(numPS);
-
- for (unsigned j = 0; j < MAX_GEOMETRYTYPES * MAX_LIGHT_VS_VARIATIONS; ++j)
- {
- unsigned g = j / MAX_LIGHT_VS_VARIATIONS;
- unsigned l = j % MAX_LIGHT_VS_VARIATIONS;
- if (!(l & LVS_SHADOW) || allowShadows)
- vertexShaders[j] = GetVertexShader(vertexShaderName + lightVSVariations[l] + geometryVSVariations[g], g != 0);
- else
- vertexShaders[j].Reset();
- }
- for (unsigned j = 0; j < numPS; ++j)
- {
- unsigned variation = j % LPS_SPOT;
- if (variation == LPS_SHADOW || variation == LPS_SHADOWSPEC)
- {
- if (allowShadows)
- pixelShaders[j] = GetPixelShader(pixelShaderName + lightPSVariations[j] +
- hwVariations[hwShadows]);
- else
- pixelShaders[j].Reset();
- }
- else
- pixelShaders[j] = GetPixelShader(pixelShaderName + lightPSVariations[j]);
- }
- break;
- }
- }
-
- technique->MarkShadersLoaded(shadersChangedFrameNumber_);
- }
- void Renderer::ReleaseMaterialShaders()
- {
- PODVector<Material*> materials;
- cache_->GetResources<Material>(materials);
-
- for (unsigned i = 0; i < materials.Size(); ++i)
- materials[i]->ReleaseShaders();
- }
- void Renderer::ReloadTextures()
- {
- PODVector<Resource*> textures;
-
- cache_->GetResources(textures, Texture2D::GetTypeStatic());
- for (unsigned i = 0; i < textures.Size(); ++i)
- cache_->ReloadResource(textures[i]);
-
- cache_->GetResources(textures, TextureCube::GetTypeStatic());
- for (unsigned i = 0; i < textures.Size(); ++i)
- cache_->ReloadResource(textures[i]);
- }
- void Renderer::CreateGeometries()
- {
- SharedPtr<VertexBuffer> dlvb(new VertexBuffer(context_));
- dlvb->SetSize(4, MASK_POSITION);
- dlvb->SetData(dirLightVertexData);
-
- SharedPtr<IndexBuffer> dlib(new IndexBuffer(context_));
- dlib->SetSize(6, false);
- dlib->SetData(dirLightIndexData);
-
- dirLightGeometry_ = new Geometry(context_);
- dirLightGeometry_->SetVertexBuffer(0, dlvb);
- dirLightGeometry_->SetIndexBuffer(dlib);
- dirLightGeometry_->SetDrawRange(TRIANGLE_LIST, 0, dlib->GetIndexCount());
-
- SharedPtr<VertexBuffer> plvb(new VertexBuffer(context_));
- plvb->SetSize(24, MASK_POSITION);
- plvb->SetData(pointLightVertexData);
-
- SharedPtr<IndexBuffer> plib(new IndexBuffer(context_));
- plib->SetSize(132, false);
- plib->SetData(pointLightIndexData);
-
- pointLightGeometry_ = new Geometry(context_);
- pointLightGeometry_->SetVertexBuffer(0, plvb);
- pointLightGeometry_->SetIndexBuffer(plib);
- pointLightGeometry_->SetDrawRange(TRIANGLE_LIST, 0, plib->GetIndexCount());
-
- SharedPtr<VertexBuffer> slvb(new VertexBuffer(context_));
- slvb->SetSize(8, MASK_POSITION);
- slvb->SetData(spotLightVertexData);
-
- SharedPtr<IndexBuffer> slib(new IndexBuffer(context_));
- slib->SetSize(36, false);
- slib->SetData(spotLightIndexData);
-
- spotLightGeometry_ = new Geometry(context_);
- spotLightGeometry_->SetVertexBuffer(0, slvb);
- spotLightGeometry_->SetIndexBuffer(slib);
- spotLightGeometry_->SetDrawRange(TRIANGLE_LIST, 0, slib->GetIndexCount());
- }
- void Renderer::CreateInstancingBuffer()
- {
- // Do not create buffer if instancing not supported
- if (!graphics_->GetSM3Support())
- {
- dynamicInstancing_ = false;
- return;
- }
-
- // If must lock the buffer for each batch group, set a smaller size
- unsigned defaultSize = graphics_->GetStreamOffsetSupport() ? INSTANCING_BUFFER_DEFAULT_SIZE : INSTANCING_BUFFER_DEFAULT_SIZE / 4;
-
- instancingBuffer_ = new VertexBuffer(context_);
- if (!instancingBuffer_->SetSize(defaultSize, INSTANCING_BUFFER_MASK, true))
- {
- instancingBuffer_.Reset();
- dynamicInstancing_ = false;
- }
- }
- bool Renderer::ResizeInstancingBuffer(unsigned numInstances)
- {
- if (!instancingBuffer_)
- return false;
-
- unsigned oldSize = instancingBuffer_->GetVertexCount();
- if (numInstances <= oldSize)
- return true;
-
- unsigned newSize = INSTANCING_BUFFER_DEFAULT_SIZE;
- while (newSize < numInstances)
- newSize <<= 1;
-
- if (!instancingBuffer_->SetSize(newSize, INSTANCING_BUFFER_MASK, true))
- {
- LOGERROR("Failed to resize instancing buffer to " + String(newSize));
- // If failed, try to restore the old size
- instancingBuffer_->SetSize(oldSize, INSTANCING_BUFFER_MASK, true);
- return false;
- }
-
- LOGDEBUG("Resized instancing buffer to " + String(newSize));
- return true;
- }
- bool Renderer::CreateShadowMaps()
- {
- unsigned shadowMapFormat = shadowMapHiresDepth_ ? graphics_->GetHiresShadowMapFormat() : graphics_->GetShadowMapFormat();
- unsigned dummyColorFormat = graphics_->GetDummyColorFormat();
- bool hardwarePCF = graphics_->GetHardwareShadowSupport();
-
- if (!shadowMapFormat)
- return false;
-
- if (!drawShadows_)
- {
- for (unsigned i = 0; i < NUM_SHADOWMAP_RESOLUTIONS; ++i)
- {
- for (unsigned j = 0; j < shadowMaps_[i].Size(); ++j)
- shadowMaps_[i][j].Reset();
- }
- return true;
- }
-
- #ifdef USE_OPENGL
-
- // Create shadow maps only. Color rendertargets are not needed
- unsigned size = shadowMapSize_;
- for (unsigned i = 0; i < NUM_SHADOWMAP_RESOLUTIONS; ++i)
- {
- for (unsigned j = 0; j < shadowMaps_[i].Size(); ++j)
- {
- if (!shadowMaps_[i][j])
- shadowMaps_[i][j] = new Texture2D(context_);
- if (!shadowMaps_[i][j]->SetSize(size, size, shadowMapFormat, TEXTURE_DEPTHSTENCIL))
- return false;
- shadowMaps_[i][j]->SetFilterMode(FILTER_BILINEAR);
- shadowMaps_[i][j]->SetShadowCompare(true);
- }
- size >>= 1;
- }
-
- #else
-
- // Create shadow maps and dummy color rendertargets
- unsigned size = shadowMapSize_;
- for (unsigned i = 0; i < NUM_SHADOWMAP_RESOLUTIONS; ++i)
- {
- if (!colorShadowMaps_[i])
- colorShadowMaps_[i] = new Texture2D(context_);
- if (!colorShadowMaps_[i]->SetSize(size, size, dummyColorFormat, TEXTURE_RENDERTARGET))
- return false;
-
- for (unsigned j = 0; j < shadowMaps_[i].Size(); ++j)
- {
- if (!shadowMaps_[i][j])
- shadowMaps_[i][j] = new Texture2D(context_);
- if (!shadowMaps_[i][j]->SetSize(size, size, shadowMapFormat, TEXTURE_DEPTHSTENCIL))
- return false;
- shadowMaps_[i][j]->SetFilterMode(hardwarePCF ? FILTER_BILINEAR : FILTER_NEAREST);
-
- // Link the color rendertarget to depth rendertarget
- shadowMaps_[i][j]->GetRenderSurface()->SetLinkedRenderTarget(colorShadowMaps_[i]->GetRenderSurface());
- }
- size >>= 1;
- }
-
- #endif
-
- return true;
- }
- Camera* Renderer::CreateShadowCamera()
- {
- if (numShadowCameras_ >= shadowCameraStore_.Size())
- shadowCameraStore_.Push(SharedPtr<Camera>(new Camera(context_)));
- Camera* camera = shadowCameraStore_[numShadowCameras_];
- camera->SetNode(CreateTempNode());
-
- ++numShadowCameras_;
- return camera;
- }
- Light* Renderer::CreateSplitLight(Light* original)
- {
- if (numSplitLights_ >= splitLightStore_.Size())
- splitLightStore_.Push(SharedPtr<Light>(new Light(context_)));
- Light* light = splitLightStore_[numSplitLights_];
- light->SetNode(CreateTempNode());
- light->CopyFrom(original);
-
- ++numSplitLights_;
- return light;
- }
- Node* Renderer::CreateTempNode()
- {
- if (numTempNodes_ >= tempNodeStore_.Size())
- tempNodeStore_.Push(SharedPtr<Node>(new Node(context_)));
- Node* node = tempNodeStore_[numTempNodes_];
-
- ++numTempNodes_;
- return node;
- }
- void Renderer::SetupLightBatch(Batch& batch)
- {
- Matrix3x4 view(batch.camera_->GetInverseWorldTransform());
-
- Light* light = batch.light_;
- float lightExtent = light->GetVolumeExtent();
- float lightViewDist = (light->GetWorldPosition() - batch.camera_->GetWorldPosition()).LengthFast();
-
- graphics_->SetAlphaTest(false);
- graphics_->SetBlendMode(BLEND_ADD);
- graphics_->SetDepthWrite(false);
-
- if (light->GetLightType() == LIGHT_DIRECTIONAL)
- {
- // Get projection without jitter offset to ensure the whole screen is filled
- Matrix4 projection(batch.camera_->GetProjection(false));
-
- // If the light does not extend to the near plane, use a stencil test. Else just draw with depth fail
- if (light->GetNearSplit() <= batch.camera_->GetNearClip())
- {
- graphics_->SetCullMode(CULL_NONE);
- graphics_->SetDepthTest(CMP_GREATER);
- graphics_->SetStencilTest(false);
- }
- else
- {
- Matrix3x4 nearTransform = light->GetDirLightTransform(*batch.camera_, true);
-
- // Set state for stencil rendering
- graphics_->SetColorWrite(false);
- graphics_->SetCullMode(CULL_NONE);
- graphics_->SetDepthTest(CMP_LESSEQUAL);
- graphics_->SetStencilTest(true, CMP_ALWAYS, OP_INCR, OP_KEEP, OP_KEEP, 1);
- graphics_->SetShaders(stencilVS_, stencilPS_);
- graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
- graphics_->SetShaderParameter(VSP_MODEL, nearTransform);
- graphics_->ClearTransformSources();
-
- // Draw to stencil
- batch.geometry_->Draw(graphics_);
-
- // Re-enable color write, set test for rendering the actual light
- graphics_->SetColorWrite(true);
- graphics_->SetDepthTest(CMP_GREATER);
- graphics_->SetStencilTest(true, CMP_EQUAL, OP_ZERO, OP_KEEP, OP_ZERO, 1);
- }
- }
- else
- {
- Matrix4 projection(batch.camera_->GetProjection());
- const Matrix3x4& model = light->GetVolumeTransform(*batch.camera_);
-
- if (light->GetLightType() == LIGHT_SPLITPOINT)
- {
- // Shadowed point light, split in 6 frustums: mask out overlapping pixels to prevent overlighting
- // Check whether we should draw front or back faces
- bool drawBackFaces = lightViewDist < (lightExtent + batch.camera_->GetNearClip());
- graphics_->SetColorWrite(false);
- graphics_->SetCullMode(drawBackFaces ? CULL_CCW : CULL_CW);
- graphics_->SetDepthTest(drawBackFaces ? CMP_GREATER : CMP_LESS);
- graphics_->SetStencilTest(true, CMP_EQUAL, OP_INCR, OP_KEEP, OP_KEEP, 0);
- graphics_->SetShaders(stencilVS_, stencilPS_);
- graphics_->SetShaderParameter(VSP_VIEWPROJ, projection * view);
- graphics_->SetShaderParameter(VSP_MODEL, model);
-
- // Draw the other faces to stencil to mark where we should not draw
- batch.geometry_->Draw(graphics_);
-
- graphics_->SetColorWrite(true);
- graphics_->SetCullMode(drawBackFaces ? CULL_CW : CULL_CCW);
- graphics_->SetStencilTest(true, CMP_EQUAL, OP_DECR, OP_DECR, OP_KEEP, 0);
- }
- else
- {
- // If light is close to near clip plane, we might be inside light volume
- if (lightViewDist < (lightExtent + batch.camera_->GetNearClip()))
- {
- // In this case reverse cull mode & depth test and render back faces
- graphics_->SetCullMode(CULL_CW);
- graphics_->SetDepthTest(CMP_GREATER);
- graphics_->SetStencilTest(false);
- }
- else
- {
- // If not too close to far clip plane, write the back faces to stencil for optimization,
- // then render front faces. Else just render front faces.
- if (lightViewDist < (batch.camera_->GetFarClip() - lightExtent))
- {
- // Set state for stencil rendering
- graphics_->SetColorWrite(false);
- graphics_->SetCullMode(CULL_CW);
- graphics_->SetDepthTest(CMP_GREATEREQUAL);
- graphics_->SetStencilTest(true, CMP_ALWAYS, OP_INCR, OP_KEEP, OP_KEEP, 1);
- graphics_->SetShaders(stencilVS_, stencilPS_);
- graphics_->SetShaderParameter(VSP_VIEWPROJ, projection * view);
- graphics_->SetShaderParameter(VSP_MODEL, model);
-
- // Draw to stencil
- batch.geometry_->Draw(graphics_);
-
- // Re-enable color write, set test for rendering the actual light
- graphics_->SetColorWrite(true);
- graphics_->SetStencilTest(true, CMP_EQUAL, OP_ZERO, OP_KEEP, OP_ZERO, 1);
- graphics_->SetCullMode(CULL_CCW);
- graphics_->SetDepthTest(CMP_LESS);
- }
- else
- {
- graphics_->SetStencilTest(false);
- graphics_->SetCullMode(CULL_CCW);
- graphics_->SetDepthTest(CMP_LESS);
- }
- }
- }
- }
- }
- void Renderer::DrawFullScreenQuad(Camera& camera, ShaderVariation* vs, ShaderVariation* ps, bool nearQuad, const HashMap<StringHash, Vector4>& shaderParameters)
- {
- Light quadDirLight(context_);
- Matrix3x4 model(quadDirLight.GetDirLightTransform(camera, nearQuad));
-
- graphics_->SetCullMode(CULL_NONE);
- graphics_->SetShaders(vs, ps);
- graphics_->SetShaderParameter(VSP_MODEL, model);
- // Get projection without jitter offset to ensure the whole screen is filled
- graphics_->SetShaderParameter(VSP_VIEWPROJ, camera.GetProjection(false));
- graphics_->ClearTransformSources();
-
- // Set global shader parameters as needed
- for (HashMap<StringHash, Vector4>::ConstIterator i = shaderParameters.Begin(); i != shaderParameters.End(); ++i)
- {
- if (graphics_->NeedParameterUpdate(i->first_, &shaderParameters))
- graphics_->SetShaderParameter(i->first_, i->second_);
- }
-
- dirLightGeometry_->Draw(graphics_);
- }
- void Renderer::HandleScreenMode(StringHash eventType, VariantMap& eventData)
- {
- if (!initialized_)
- Initialize();
- else
- {
- // When screen mode changes, reload shaders and purge old views and occlusion buffers
- shadersDirty_ = true;
- occlusionBuffers_.Clear();
- ResetViews();
- }
- }
- void Renderer::HandleRenderUpdate(StringHash eventType, VariantMap& eventData)
- {
- if (initialized_)
- {
- using namespace RenderUpdate;
-
- Update(eventData[P_TIMESTEP].GetFloat());
- }
- }
|