Przeglądaj źródła

Optimize forward lighting when material has zero specular strength.

Lasse Öörni 14 lat temu
rodzic
commit
5a64681f0b

+ 15 - 0
Engine/Graphics/Material.cpp

@@ -94,6 +94,8 @@ Material::Material(Context* context) :
     SetShaderParameter("MatDiffColor", Vector4::UNITY);
     SetShaderParameter("MatEmissiveColor", Vector4::ZERO);
     SetShaderParameter("MatSpecProperties", Vector4::ZERO);
+    
+    CheckSpecular();
 }
 
 Material::~Material()
@@ -197,6 +199,7 @@ bool Material::Load(Deserializer& source)
     
     SetMemoryUse(memoryUse);
     CheckOcclusion();
+    CheckSpecular();
     return true;
 }
 
@@ -264,6 +267,8 @@ void Material::SetShaderParameter(const String& name, const Vector4& value)
     newParam.name_ = name;
     newParam.value_ = value;
     shaderParameters_[StringHash(name)] = newParam;
+    
+    CheckSpecular();
 }
 
 void Material::SetTexture(TextureUnit unit, Texture* texture)
@@ -321,6 +326,7 @@ void Material::SetShadowCullMode(CullMode mode)
 void Material::RemoveShaderParameter(const String& name)
 {
     shaderParameters_.Erase(StringHash(name));
+    CheckSpecular();
 }
 
 void Material::ReleaseShaders()
@@ -403,3 +409,12 @@ void Material::CheckOcclusion()
         }
     }
 }
+
+void Material::CheckSpecular()
+{
+    HashMap<StringHash, MaterialShaderParameter>::ConstIterator i = shaderParameters_.Find(PSP_MATSPECPROPERTIES);
+    if (i != shaderParameters_.End())
+        specular_ = i->second_.value_.x_ > 0.0f;
+    else
+        specular_ = false;
+}

+ 6 - 0
Engine/Graphics/Material.h

@@ -128,6 +128,8 @@ public:
     unsigned GetAuxViewFrameNumber() const { return auxViewFrameNumber_; }
     /// Return whether should render occlusion.
     bool GetOcclusion() const { return occlusion_; }
+    /// Return whether should render specular.
+    bool GetSpecular() const { return specular_; }
     
     /// Return name for texture unit.
     static const String& GetTextureUnitName(TextureUnit unit);
@@ -135,6 +137,8 @@ public:
 private:
     /// Re-evaluate occlusion rendering.
     void CheckOcclusion();
+    /// Re-evaluate specular lighting.
+    void CheckSpecular();
     
     /// Techniques.
     Vector<TechniqueEntry> techniques_;
@@ -150,4 +154,6 @@ private:
     unsigned auxViewFrameNumber_;
     /// Render occlusion flag.
     bool occlusion_;
+    /// Specular lighting flag.
+    bool specular_;
 };

+ 2 - 1
Engine/Graphics/Renderer.cpp

@@ -905,7 +905,8 @@ void Renderer::SetBatchShaders(Batch& batch, Technique* technique, Pass* pass, b
             unsigned psi = 0;
             vsi = batch.geometryType_ * MAX_LIGHT_VS_VARIATIONS;
             
-            if (specularLighting_ && light->GetSpecularIntensity() > 0.0f)
+            bool materialHasSpecular = batch.material_ ? batch.material_->GetSpecular() : true;
+            if (specularLighting_ && light->GetSpecularIntensity() > 0.0f && materialHasSpecular)
                 psi += LPS_SPEC;
             if (allowShadows && light->GetShadowMap())
             {

+ 0 - 1
Engine/Graphics/View.cpp

@@ -1930,7 +1930,6 @@ void View::CalculateShaderParameters()
     shaderParameters_[PSP_FOGPARAMS] = fogParams;
 }
 
-
 void View::SetupLightBatch(Batch& batch, bool firstSplit)
 {
     Matrix3x4 view(batch.camera_->GetInverseWorldTransform());