瀏覽代碼

Fix GLSL deprecation warnings. OpenGL 3 clip plane support. Use R32F format on OpenGL 3 for deferred linear depth instead of manual RGB encoding & decoding.

Lasse Öörni 10 年之前
父節點
當前提交
992cbcc73f

+ 22 - 10
Source/Urho3D/Graphics/OpenGL/OGLGraphics.cpp

@@ -1222,6 +1222,9 @@ void Graphics::SetShaders(ShaderVariation* vs, ShaderVariation* ps)
         }
     }
     
+    // Update the clip plane uniform on GL3
+    if (gl3Support)
+        SetShaderParameter(VSP_CLIPPLANE, useClipPlane_ ? clipPlane_ : Vector4(0.0f, 0.0f, 0.0f, 1.0f));
 
     // Store shader combination if shader dumping in progress
     if (shaderPrecache_)
@@ -1913,21 +1916,25 @@ void Graphics::SetClipPlane(bool enable, const Plane& clipPlane, const Matrix3x4
             glEnable(GL_CLIP_PLANE0);
         else
             glDisable(GL_CLIP_PLANE0);
+
         useClipPlane_ = enable;
     }
     
     if (enable)
     {
         Matrix4 viewProj = projection * view;
-        Vector4 planeVec =  clipPlane.Transformed(viewProj).ToVector4();
-        
-        GLdouble planeData[4];
-        planeData[0] = planeVec.x_;
-        planeData[1] = planeVec.y_;
-        planeData[2] = planeVec.z_;
-        planeData[3] = planeVec.w_;
+        clipPlane_ =  clipPlane.Transformed(viewProj).ToVector4();
         
-        glClipPlane(GL_CLIP_PLANE0, &planeData[0]);
+        if (!gl3Support)
+        {
+            GLdouble planeData[4];
+            planeData[0] = clipPlane_.x_;
+            planeData[1] = clipPlane_.y_;
+            planeData[2] = clipPlane_.z_;
+            planeData[3] = clipPlane_.w_;
+            
+            glClipPlane(GL_CLIP_PLANE0, &planeData[0]);
+        }
     }
     #endif
 }
@@ -2621,8 +2628,13 @@ unsigned Graphics::GetFloat32Format()
 
 unsigned Graphics::GetLinearDepthFormat()
 {
-    // OpenGL FBO specs state that color attachments must have the same format; therefore must encode linear depth to RGBA
-    // manually if not using a readable hardware depth texture
+#ifndef GL_ES_VERSION_2_0
+    // OpenGL 3 can use different color attachment formats
+    if (gl3Support)
+        return GL_R32F;
+#endif
+    // OpenGL 2 requires color attachments to have the same format, therefore encode deferred depth to RGBA manually
+    // if not using a readable hardware depth texture
     return GL_RGBA;
 }
 

+ 2 - 0
Source/Urho3D/Graphics/OpenGL/OGLGraphics.h

@@ -559,6 +559,8 @@ private:
     IntRect scissorRect_;
     /// Scissor test enable flag.
     bool scissorTest_;
+    /// Current custom clip plane in post-projection space.
+    Vector4 clipPlane_;
     /// Stencil test compare mode.
     CompareMode stencilTestMode_;
     /// Stencil operation on pass.

+ 3 - 2
Source/Urho3D/Graphics/OpenGL/OGLTexture.cpp

@@ -310,6 +310,7 @@ unsigned Texture::GetRowDataSize(int width) const
     case GL_LUMINANCE32F_ARB:
     case GL_DEPTH24_STENCIL8_EXT:
     case GL_RG16:
+    case GL_R32F:
     #endif
         return width * 4;
         
@@ -364,7 +365,7 @@ unsigned Texture::GetExternalFormat(unsigned format)
         return GL_LUMINANCE;
     else if (format == GL_SLUMINANCE_ALPHA_EXT)
         return GL_LUMINANCE_ALPHA;
-    else if (format == GL_R8)
+    else if (format == GL_R8 || format == GL_R32F)
         return GL_RED;
     else if (format == GL_RG8 || format == GL_RG16 || format == GL_RG16F || format == GL_RG32F)
         return GL_RG;
@@ -387,7 +388,7 @@ unsigned Texture::GetDataType(unsigned format)
     else if (format == GL_RG16 || format == GL_RGBA16)
         return GL_UNSIGNED_SHORT;
     else if (format == GL_LUMINANCE16F_ARB || format == GL_LUMINANCE32F_ARB || format == GL_RGBA16F_ARB ||
-        format == GL_RGBA32F_ARB || format == GL_RG16F || format == GL_RG32F)
+        format == GL_RGBA32F_ARB || format == GL_RG16F || format == GL_RG32F || format == GL_R32F)
         return GL_FLOAT;
     else
         return GL_UNSIGNED_BYTE;

+ 20 - 10
bin/CoreData/Shaders/GLSL/Samplers.glsl

@@ -44,20 +44,30 @@ vec3 DecodeNormal(vec4 normalInput)
 
 vec3 EncodeDepth(float depth)
 {
-    vec3 ret;
-    depth *= 255.0;
-    ret.x = floor(depth);
-    depth = (depth - ret.x) * 255.0;
-    ret.y = floor(depth);
-    ret.z = (depth - ret.y);
-    ret.xy *= 1.0 / 255.0;
-    return ret;
+    #ifndef GL3
+        vec3 ret;
+        depth *= 255.0;
+        ret.x = floor(depth);
+        depth = (depth - ret.x) * 255.0;
+        ret.y = floor(depth);
+        ret.z = (depth - ret.y);
+        ret.xy *= 1.0 / 255.0;
+        return ret;
+    #else
+        // OpenGL 3 can use different MRT formats, so no need for encoding
+        return vec3(depth, 0.0, 0.0);
+    #endif
 }
 
 float DecodeDepth(vec3 depth)
 {
-    const vec3 dotValues = vec3(1.0, 1.0 / 255.0, 1.0 / (255.0 * 255.0));
-    return dot(depth, dotValues);
+    #ifndef GL3
+        const vec3 dotValues = vec3(1.0, 1.0 / 255.0, 1.0 / (255.0 * 255.0));
+        return dot(depth, dotValues);
+    #else
+        // OpenGL 3 can use different MRT formats, so no need for encoding
+        return depth.r;
+    #endif
 }
 
 float ReconstructDepth(float hwDepth)

+ 29 - 0
bin/CoreData/Shaders/GLSL/Transform.glsl

@@ -1,4 +1,11 @@
 #ifdef COMPILEVS
+
+// Silence GLSL 150 deprecation warnings
+#ifdef GL3
+#define attribute in
+#define varying out
+#endif
+
 attribute vec4 iPos;
 attribute vec3 iNormal;
 attribute vec4 iColor;
@@ -51,6 +58,8 @@ vec4 GetClipPos(vec3 worldPos)
     // While getting the clip coordinate, also automatically set gl_ClipVertex for user clip planes
     #if !defined(GL_ES) && !defined(GL3)
         gl_ClipVertex = ret;
+    #elif defined(GL3)
+        gl_ClipDistance[0] = dot(cClipPlane, ret);
     #endif
     return ret;
 }
@@ -105,4 +114,24 @@ vec3 GetWorldTangent(mat4 modelMatrix)
 {
     return normalize(iTangent.xyz * GetNormalMatrix(modelMatrix));
 }
+
+#else
+
+// Silence GLSL 150 deprecation warnings
+#ifdef GL3
+#define varying in
+
+// \todo: should not hardcode the number of MRT outputs according to defines
+#if defined(DEFERRED)
+out vec4 fragData[4];
+#elif defined(PREPASS)
+out vec4 fragData[2];
+#else
+out vec4 fragData[1];
 #endif
+
+#define gl_FragColor fragData[0]
+#define gl_FragData fragData
+#endif
+
+#endif

+ 3 - 0
bin/CoreData/Shaders/GLSL/Uniforms.glsl

@@ -33,6 +33,9 @@ uniform mat4 cZone;
 #ifdef NUMVERTEXLIGHTS
     uniform vec4 cVertexLights[4*3];
 #endif
+#ifdef GL3
+    uniform vec4 cClipPlane;
+#endif
 #endif
 
 #ifdef COMPILEPS