// // Copyright (c) 2008-2017 the Urho3D project. // // 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 "../Core/CoreEvents.h" #include "../Core/Profiler.h" #include "../Graphics/Camera.h" #include "../Graphics/DebugRenderer.h" #include "../Graphics/Geometry.h" #include "../Graphics/Graphics.h" #include "../Graphics/GraphicsEvents.h" #include "../Graphics/GraphicsImpl.h" #include "../Graphics/IndexBuffer.h" #include "../Graphics/Material.h" #include "../Graphics/OcclusionBuffer.h" #include "../Graphics/Octree.h" #include "../Graphics/Renderer.h" #include "../Graphics/RenderPath.h" #include "../Graphics/ShaderVariation.h" #include "../Graphics/Technique.h" #include "../Graphics/Texture2D.h" #include "../Graphics/TextureCube.h" #include "../Graphics/VertexBuffer.h" #include "../Graphics/View.h" #include "../Graphics/Zone.h" #include "../IO/Log.h" #include "../Resource/ResourceCache.h" #include "../Resource/XMLFile.h" #include "../Scene/Scene.h" #include "../DebugNew.h" #ifdef _MSC_VER #pragma warning(disable:6293) #endif namespace Atomic { 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[] = { 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 char* geometryVSVariations[] = { "", "SKINNED ", "INSTANCED ", "BILLBOARD ", "DIRBILLBOARD ", "TRAILFACECAM ", "TRAILBONE " }; static const char* lightVSVariations[] = { "PERPIXEL DIRLIGHT ", "PERPIXEL SPOTLIGHT ", "PERPIXEL POINTLIGHT ", "PERPIXEL DIRLIGHT SHADOW ", "PERPIXEL SPOTLIGHT SHADOW ", "PERPIXEL POINTLIGHT SHADOW ", "PERPIXEL DIRLIGHT SHADOW NORMALOFFSET ", "PERPIXEL SPOTLIGHT SHADOW NORMALOFFSET ", "PERPIXEL POINTLIGHT SHADOW NORMALOFFSET " }; static const char* vertexLightVSVariations[] = { "", "NUMVERTEXLIGHTS=1 ", "NUMVERTEXLIGHTS=2 ", "NUMVERTEXLIGHTS=3 ", "NUMVERTEXLIGHTS=4 ", }; static const char* deferredLightVSVariations[] = { "", "DIRLIGHT ", "ORTHO ", "DIRLIGHT ORTHO " }; static const char* lightPSVariations[] = { "PERPIXEL DIRLIGHT ", "PERPIXEL SPOTLIGHT ", "PERPIXEL POINTLIGHT ", "PERPIXEL POINTLIGHT CUBEMASK ", "PERPIXEL DIRLIGHT SPECULAR ", "PERPIXEL SPOTLIGHT SPECULAR ", "PERPIXEL POINTLIGHT SPECULAR ", "PERPIXEL POINTLIGHT CUBEMASK SPECULAR ", "PERPIXEL DIRLIGHT SHADOW ", "PERPIXEL SPOTLIGHT SHADOW ", "PERPIXEL POINTLIGHT SHADOW ", "PERPIXEL POINTLIGHT CUBEMASK SHADOW ", "PERPIXEL DIRLIGHT SPECULAR SHADOW ", "PERPIXEL SPOTLIGHT SPECULAR SHADOW ", "PERPIXEL POINTLIGHT SPECULAR SHADOW ", "PERPIXEL POINTLIGHT CUBEMASK SPECULAR SHADOW ", "PERPIXEL DIRLIGHT SHADOW NORMALOFFSET ", "PERPIXEL SPOTLIGHT SHADOW NORMALOFFSET ", "PERPIXEL POINTLIGHT SHADOW NORMALOFFSET ", "PERPIXEL POINTLIGHT CUBEMASK SHADOW NORMALOFFSET ", "PERPIXEL DIRLIGHT SPECULAR SHADOW NORMALOFFSET ", "PERPIXEL SPOTLIGHT SPECULAR SHADOW NORMALOFFSET ", "PERPIXEL POINTLIGHT SPECULAR SHADOW NORMALOFFSET ", "PERPIXEL POINTLIGHT CUBEMASK SPECULAR SHADOW NORMALOFFSET " }; static const char* heightFogVariations[] = { "", "HEIGHTFOG " }; static const unsigned MAX_BUFFER_AGE = 1000; static const int MAX_EXTRA_INSTANCING_BUFFER_ELEMENTS = 4; inline PODVector CreateInstancingBufferElements(unsigned numExtraElements) { static const unsigned NUM_INSTANCEMATRIX_ELEMENTS = 3; static const unsigned FIRST_UNUSED_TEXCOORD = 4; PODVector elements; for (unsigned i = 0; i < NUM_INSTANCEMATRIX_ELEMENTS + numExtraElements; ++i) elements.Push(VertexElement(TYPE_VECTOR4, SEM_TEXCOORD, FIRST_UNUSED_TEXCOORD + i, true)); return elements; } Renderer::Renderer(Context* context) : Object(context), defaultZone_(new Zone(context)), shadowMapFilterInstance_(0), shadowMapFilter_(0), textureAnisotropy_(4), textureFilterMode_(FILTER_TRILINEAR), textureQuality_(QUALITY_HIGH), materialQuality_(QUALITY_HIGH), shadowMapSize_(1024), shadowQuality_(SHADOWQUALITY_PCF_16BIT), shadowSoftness_(1.0f), vsmShadowParams_(0.0000001f, 0.9f), vsmMultiSample_(1), maxShadowMaps_(1), minInstances_(2), maxSortedInstances_(1000), maxOccluderTriangles_(5000), occlusionBufferSize_(256), occluderSizeThreshold_(0.025f), mobileShadowBiasMul_(1.0f), mobileShadowBiasAdd_(0.0f), mobileNormalOffsetMul_(1.0f), numOcclusionBuffers_(0), numShadowCameras_(0), shadersChangedFrameNumber_(M_MAX_UNSIGNED), hdrRendering_(false), specularLighting_(true), drawShadows_(true), reuseShadowMaps_(true), dynamicInstancing_(true), numExtraInstancingBufferElements_(0), threadedOcclusion_(false), shadersDirty_(true), initialized_(false), resetViews_(false) { SubscribeToEvent(E_SCREENMODE, ATOMIC_HANDLER(Renderer, HandleScreenMode)); // 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, Viewport* viewport) { if (index >= viewports_.Size()) viewports_.Resize(index + 1); viewports_[index] = viewport; } void Renderer::SetDefaultRenderPath(RenderPath* renderPath) { if (renderPath) defaultRenderPath_ = renderPath; } void Renderer::SetDefaultRenderPath(XMLFile* xmlFile) { SharedPtr newRenderPath(new RenderPath()); if (newRenderPath->Load(xmlFile)) defaultRenderPath_ = newRenderPath; } void Renderer::SetDefaultTechnique(Technique* technique) { defaultTechnique_ = technique; } void Renderer::SetHDRRendering(bool enable) { hdrRendering_ = enable; } void Renderer::SetSpecularLighting(bool enable) { specularLighting_ = enable; } 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) { quality = Clamp(quality, QUALITY_LOW, QUALITY_MAX); if (quality != materialQuality_) { materialQuality_ = quality; shadersDirty_ = true; // Reallocate views to not store eg. pass information that might be unnecessary on the new material quality level resetViews_ = true; } } void Renderer::SetDrawShadows(bool enable) { if (!graphics_ || !graphics_->GetShadowMapFormat()) return; drawShadows_ = enable; if (!drawShadows_) ResetShadowMaps(); } void Renderer::SetShadowMapSize(int size) { if (!graphics_) return; size = NextPowerOfTwo((unsigned)Max(size, SHADOW_MIN_PIXELS)); if (size != shadowMapSize_) { shadowMapSize_ = size; ResetShadowMaps(); } } void Renderer::SetShadowQuality(ShadowQuality quality) { if (!graphics_) return; // If no hardware PCF, do not allow to select one-sample quality if (!graphics_->GetHardwareShadowSupport()) { if (quality == SHADOWQUALITY_SIMPLE_16BIT) quality = SHADOWQUALITY_PCF_16BIT; if (quality == SHADOWQUALITY_SIMPLE_24BIT) quality = SHADOWQUALITY_PCF_24BIT; } // if high resolution is not allowed if (!graphics_->GetHiresShadowMapFormat()) { if (quality == SHADOWQUALITY_SIMPLE_24BIT) quality = SHADOWQUALITY_SIMPLE_16BIT; if (quality == SHADOWQUALITY_PCF_24BIT) quality = SHADOWQUALITY_PCF_16BIT; } if (quality != shadowQuality_) { shadowQuality_ = quality; shadersDirty_ = true; if (quality == SHADOWQUALITY_BLUR_VSM) SetShadowMapFilter(this, static_cast(&Renderer::BlurShadowMap)); else SetShadowMapFilter(0, 0); ResetShadowMaps(); } } void Renderer::SetShadowSoftness(float shadowSoftness) { shadowSoftness_ = Max(shadowSoftness, 0.0f); } void Renderer::SetVSMShadowParameters(float minVariance, float lightBleedingReduction) { vsmShadowParams_.x_ = Max(minVariance, 0.0f); vsmShadowParams_.y_ = Clamp(lightBleedingReduction, 0.0f, 1.0f); } void Renderer::SetVSMMultiSample(int multiSample) { multiSample = Clamp(multiSample, 1, 16); if (multiSample != vsmMultiSample_) { vsmMultiSample_ = multiSample; ResetShadowMaps(); } } void Renderer::SetShadowMapFilter(Object* instance, ShadowMapFilter functionPtr) { shadowMapFilterInstance_ = instance; shadowMapFilter_ = functionPtr; } void Renderer::SetReuseShadowMaps(bool enable) { reuseShadowMaps_ = enable; } void Renderer::SetMaxShadowMaps(int shadowMaps) { if (shadowMaps < 1) return; maxShadowMaps_ = shadowMaps; for (HashMap > >::Iterator i = shadowMaps_.Begin(); i != shadowMaps_.End(); ++i) { if ((int)i->second_.Size() > maxShadowMaps_) i->second_.Resize((unsigned)maxShadowMaps_); } } void Renderer::SetDynamicInstancing(bool enable) { if (!instancingBuffer_) enable = false; dynamicInstancing_ = enable; } void Renderer::SetNumExtraInstancingBufferElements(int elements) { if (numExtraInstancingBufferElements_ != elements) { numExtraInstancingBufferElements_ = Clamp(elements, 0, MAX_EXTRA_INSTANCING_BUFFER_ELEMENTS); CreateInstancingBuffer(); } } void Renderer::SetMinInstances(int instances) { minInstances_ = Max(instances, 1); } void Renderer::SetMaxSortedInstances(int instances) { maxSortedInstances_ = Max(instances, 0); } void Renderer::SetMaxOccluderTriangles(int triangles) { maxOccluderTriangles_ = Max(triangles, 0); } void Renderer::SetOcclusionBufferSize(int size) { occlusionBufferSize_ = Max(size, 1); occlusionBuffers_.Clear(); } void Renderer::SetMobileShadowBiasMul(float mul) { mobileShadowBiasMul_ = mul; } void Renderer::SetMobileShadowBiasAdd(float add) { mobileShadowBiasAdd_ = add; } void Renderer::SetMobileNormalOffsetMul(float mul) { mobileNormalOffsetMul_ = mul; } void Renderer::SetOccluderSizeThreshold(float screenSize) { occluderSizeThreshold_ = Max(screenSize, 0.0f); } void Renderer::SetThreadedOcclusion(bool enable) { if (enable != threadedOcclusion_) { threadedOcclusion_ = enable; occlusionBuffers_.Clear(); } } void Renderer::ReloadShaders() { shadersDirty_ = true; } void Renderer::ApplyShadowMapFilter(View* view, Texture2D* shadowMap, float blurScale) { if (shadowMapFilterInstance_ && shadowMapFilter_) (shadowMapFilterInstance_->*shadowMapFilter_)(view, shadowMap, blurScale); } Viewport* Renderer::GetViewport(unsigned index) const { return index < viewports_.Size() ? viewports_[index] : (Viewport*)0; } RenderPath* Renderer::GetDefaultRenderPath() const { return defaultRenderPath_; } Technique* Renderer::GetDefaultTechnique() const { // Assign default when first asked if not assigned yet if (!defaultTechnique_) const_cast& >(defaultTechnique_) = GetSubsystem()->GetResource("Techniques/NoTexture.xml"); return defaultTechnique_; } unsigned Renderer::GetNumGeometries(bool allViews) const { unsigned numGeometries = 0; unsigned lastView = allViews ? views_.Size() : 1; for (unsigned i = 0; i < lastView; ++i) { // Use the source view's statistics if applicable View* view = GetActualView(views_[i]); if (!view) continue; numGeometries += view->GetGeometries().Size(); } return numGeometries; } unsigned Renderer::GetNumLights(bool allViews) const { unsigned numLights = 0; unsigned lastView = allViews ? views_.Size() : 1; for (unsigned i = 0; i < lastView; ++i) { View* view = GetActualView(views_[i]); if (!view) continue; numLights += view->GetLights().Size(); } return numLights; } unsigned Renderer::GetNumShadowMaps(bool allViews) const { unsigned numShadowMaps = 0; unsigned lastView = allViews ? views_.Size() : 1; for (unsigned i = 0; i < lastView; ++i) { View* view = GetActualView(views_[i]); if (!view) continue; const Vector& lightQueues = view->GetLightQueues(); for (Vector::ConstIterator i = lightQueues.Begin(); i != lightQueues.End(); ++i) { if (i->shadowMap_) ++numShadowMaps; } } return numShadowMaps; } unsigned Renderer::GetNumOccluders(bool allViews) const { unsigned numOccluders = 0; unsigned lastView = allViews ? views_.Size() : 1; for (unsigned i = 0; i < lastView; ++i) { View* view = GetActualView(views_[i]); if (!view) continue; numOccluders += view->GetNumActiveOccluders(); } return numOccluders; } void Renderer::Update(float timeStep) { ATOMIC_PROFILE(UpdateViews); views_.Clear(); preparedViews_.Clear(); // 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; // Set up the frameinfo structure for this frame frame_.frameNumber_ = GetSubsystem