Browse Source

Fixed missing depth reconstruction in deferred rendering ambient quad on OpenGL.

Lasse Öörni 14 years ago
parent
commit
2290441ea2

+ 1 - 6
Engine/Graphics/OpenGL/OGLGraphics.cpp

@@ -877,12 +877,7 @@ void Graphics::SetShaderParameter(ShaderParameter param, float value)
 
 
 void Graphics::SetShaderParameter(ShaderParameter param, const Color& color)
 void Graphics::SetShaderParameter(ShaderParameter param, const Color& color)
 {
 {
-    if (shaderProgram_)
-    {
-        const UniformInfo* info = shaderProgram_->GetUniformInfo(param);
-        if (info)
-            glUniform4fv(info->location_, 1, color.GetData());
-    }
+    SetShaderParameter(param, color.GetData(), 4);
 }
 }
 
 
 void Graphics::SetShaderParameter(ShaderParameter param, const Matrix3& matrix)
 void Graphics::SetShaderParameter(ShaderParameter param, const Matrix3& matrix)

+ 10 - 4
Engine/Graphics/View.cpp

@@ -857,7 +857,7 @@ void View::RenderBatchesDeferred()
     shaderParameters_[PSP_GBUFFERVIEWPORT] = viewportSize;
     shaderParameters_[PSP_GBUFFERVIEWPORT] = viewportSize;
     
     
     {
     {
-        // Render G-buffer
+        // Clear and render the G-buffer
         PROFILE(RenderGBuffer);
         PROFILE(RenderGBuffer);
         
         
         graphics_->ClearLastParameterSources();
         graphics_->ClearLastParameterSources();
@@ -866,7 +866,7 @@ void View::RenderBatchesDeferred()
         graphics_->SetScissorTest(false);
         graphics_->SetScissorTest(false);
         graphics_->SetStencilTest(false);
         graphics_->SetStencilTest(false);
         #ifdef USE_OPENGL
         #ifdef USE_OPENGL
-        // On OpenGL, clear the depth and diffuse albedo buffers normally
+        // On OpenGL, clear the diffuse and depth buffers normally
         if (deferred)
         if (deferred)
         {
         {
             graphics_->SetRenderTarget(0, diffBuffer);
             graphics_->SetRenderTarget(0, diffBuffer);
@@ -882,6 +882,7 @@ void View::RenderBatchesDeferred()
             graphics_->SetRenderTarget(0, normalBuffer);
             graphics_->SetRenderTarget(0, normalBuffer);
         }
         }
         #else
         #else
+        // On Direct3D9, clear only depth and stencil at first (fillrate optimization)
         if (deferred)
         if (deferred)
         {
         {
             graphics_->SetRenderTarget(0, diffBuffer);
             graphics_->SetRenderTarget(0, diffBuffer);
@@ -893,7 +894,6 @@ void View::RenderBatchesDeferred()
             graphics_->SetRenderTarget(0, normalBuffer);
             graphics_->SetRenderTarget(0, normalBuffer);
             graphics_->SetRenderTarget(1, depthBuffer);
             graphics_->SetRenderTarget(1, depthBuffer);
         }
         }
-        // On Direct3D9, clear only depth and stencil at first (fillrate optimization)
         graphics_->SetDepthStencil(lightDepthStencil);
         graphics_->SetDepthStencil(lightDepthStencil);
         graphics_->SetViewport(screenRect_);
         graphics_->SetViewport(screenRect_);
         graphics_->Clear(CLEAR_DEPTH | CLEAR_STENCIL);
         graphics_->Clear(CLEAR_DEPTH | CLEAR_STENCIL);
@@ -947,8 +947,14 @@ void View::RenderBatchesDeferred()
         graphics_->SetTexture(TU_DEPTHBUFFER, depthBuffer);
         graphics_->SetTexture(TU_DEPTHBUFFER, depthBuffer);
         graphics_->SetViewport(screenRect_);
         graphics_->SetViewport(screenRect_);
         
         
+        String pixelShaderName = "Deferred/Ambient";
+        #ifdef USE_OPENGL
+        if (camera_->IsOrthographic())
+            pixelShaderName += "Ortho";
+        #endif
+        
         renderer_->DrawFullScreenQuad(*camera_, renderer_->GetVertexShader("Deferred/Ambient"),
         renderer_->DrawFullScreenQuad(*camera_, renderer_->GetVertexShader("Deferred/Ambient"),
-            renderer_->GetPixelShader("Deferred/Ambient"), false, shaderParameters_);
+            renderer_->GetPixelShader(pixelShaderName), false, shaderParameters_);
     }
     }
     else
     else
     {
     {

+ 6 - 1
Engine/Math/Color.h

@@ -123,7 +123,12 @@ public:
         unsigned b = (unsigned)(Clamp(b_ * 255.0f, 0.0f, 255.0f));
         unsigned b = (unsigned)(Clamp(b_ * 255.0f, 0.0f, 255.0f));
         unsigned a = (unsigned)(Clamp(a_ * 255.0f, 0.0f, 255.0f));
         unsigned a = (unsigned)(Clamp(a_ * 255.0f, 0.0f, 255.0f));
         
         
-        return (((a) & 0xff) << 24) | (((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff);
+        /// \todo Model data will need to be converted on OpenGL if it has vertex colors
+        #ifdef USE_OPENGL
+            return (((a) & 0xff) << 24) | (((b) & 0xff) << 16) | (((g) & 0xff) << 8) | ((r) & 0xff);
+        #else
+            return (((a) & 0xff) << 24) | (((r) & 0xff) << 16) | (((g) & 0xff) << 8) | ((b) & 0xff);
+        #endif
     }
     }
     
     
     /// Return as a four-dimensional vector
     /// Return as a four-dimensional vector

+ 8 - 3
Engine/UI/UI.cpp

@@ -245,11 +245,13 @@ void UI::Render()
     projection.m33_ = 1.0f;
     projection.m33_ = 1.0f;
     
     
     graphics_->ResetRenderTargets();
     graphics_->ResetRenderTargets();
+    graphics_->SetAlphaTest(false);
     graphics_->SetCullMode(CULL_CCW);
     graphics_->SetCullMode(CULL_CCW);
     graphics_->SetDepthTest(CMP_ALWAYS);
     graphics_->SetDepthTest(CMP_ALWAYS);
     graphics_->SetDepthWrite(false);
     graphics_->SetDepthWrite(false);
     graphics_->SetFillMode(FILL_SOLID);
     graphics_->SetFillMode(FILL_SOLID);
     graphics_->SetStencilTest(false);
     graphics_->SetStencilTest(false);
+    graphics_->ClearLastParameterSources();
     
     
     ShaderVariation* ps = 0;
     ShaderVariation* ps = 0;
     ShaderVariation* vs = 0;
     ShaderVariation* vs = 0;
@@ -276,9 +278,12 @@ void UI::Render()
         }
         }
         
         
         graphics_->SetShaders(vs, ps);
         graphics_->SetShaders(vs, ps);
-        graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
-        graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
-        graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
+        if (graphics_->NeedParameterUpdate(VSP_MODEL, this))
+            graphics_->SetShaderParameter(VSP_MODEL, Matrix3x4::IDENTITY);
+        if (graphics_->NeedParameterUpdate(VSP_VIEWPROJ, this))
+            graphics_->SetShaderParameter(VSP_VIEWPROJ, projection);
+        if (graphics_->NeedParameterUpdate(PSP_MATDIFFCOLOR, this))
+            graphics_->SetShaderParameter(PSP_MATDIFFCOLOR, Color(1.0f, 1.0f, 1.0f, 1.0f));
         
         
         batches_[i].Draw(graphics_);
         batches_[i].Draw(graphics_);
     }
     }

+ 8 - 1
SourceAssets/GLSLShaders/Deferred/Ambient.frag

@@ -11,8 +11,15 @@ void main()
 
 
     vec3 ambientColor = cAmbientColor * diffInput.rgb;
     vec3 ambientColor = cAmbientColor * diffInput.rgb;
 
 
+    #ifdef ORTHO
+        float linearDepth = depth;
+    #else
+        float linearDepth = ReconstructDepth(depth);
+    #endif
+
     // Store coarse depth to alpha channel for deferred antialiasing
     // Store coarse depth to alpha channel for deferred antialiasing
-    gl_FragColor = vec4(ambientColor + GetFogFactor(depth) * cFogColor, depth);
+    gl_FragColor = vec4(ambientColor + GetFogFactor(linearDepth) * cFogColor, linearDepth);
+    
     // Copy a slightly biased depth value because of possible inaccuracy
     // Copy a slightly biased depth value because of possible inaccuracy
     gl_FragDepth = min(texture2D(sDepthBuffer, vScreenPos).r + 0.000001, 1.0);
     gl_FragDepth = min(texture2D(sDepthBuffer, vScreenPos).r + 0.000001, 1.0);
 }
 }

+ 3 - 1
SourceAssets/GLSLShaders/Deferred/Ambient.xml

@@ -1,4 +1,6 @@
 <shaders>
 <shaders>
     <shader name="Ambient" type="vs" />
     <shader name="Ambient" type="vs" />
-    <shader name="Ambient" type="ps" />
+    <shader name="Ambient" type="ps">
+        <option name="Ortho" define="ORTHO" />
+    </shader>
 </shaders>
 </shaders>

+ 0 - 5
SourceAssets/GLSLShaders/Lighting.frag

@@ -1,8 +1,3 @@
-float ReconstructDepth(float hwDepth)
-{
-    return cDepthReconstruct.y / (hwDepth - cDepthReconstruct.x);
-}
-
 float GetDiffuseDir(vec3 normal, out vec3 lightDir)
 float GetDiffuseDir(vec3 normal, out vec3 lightDir)
 {
 {
     lightDir = cLightDir;
     lightDir = cLightDir;

+ 6 - 1
SourceAssets/GLSLShaders/Samplers.frag

@@ -26,4 +26,9 @@ vec3 UnpackNormal(vec4 normalInput)
 float GetIntensity(vec3 color)
 float GetIntensity(vec3 color)
 {
 {
     return dot(color, vec3(0.333, 0.333, 0.333));
     return dot(color, vec3(0.333, 0.333, 0.333));
-}
+}
+
+float ReconstructDepth(float hwDepth)
+{
+    return cDepthReconstruct.y / (hwDepth - cDepthReconstruct.x);
+}