Jump to content
  • Advertisement
Sign in to follow this  
Vilem Otte

OpenGL [OpenGL/GLSL]Weird deferred shading bug

This topic is 2853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


right now I'm trying to write OpenGL/GLSL fallback for my raytracer, and I got stuck with reflections - it must be position from depth reconstruction stuff (this worked with RGB32F on positions, but switching to R32F depth killed it (I just wanted 64 bits per pixel less :( ) ... and I'm trying to solve this for about 6 hours without success :( ).
Here is thing, I'm trying to create water reflections (with shadows, and such) inside them, here are my vertex and fragment shaders of MRTs:

// Vertex shader
#version 150
#define GL_core_profile 1
#extension GL_EXT_gpu_shader4 : enable
#extension GL_ARB_shader_texture_lod : enable

uniform mat4 Gpu_ModelViewMatrix;
uniform mat4 Gpu_FlippingMatrix;
uniform mat4 Gpu_ProjectionMatrix;
uniform mat3 Gpu_NormalMatrix;

in vec3 Gpu_Vertex;
in vec3 Gpu_Normal;
in vec4 Gpu_MultiTexCoord0;
in vec4 Gpu_MultiTexCoord1;

out vec3 vert_vout;
out vec3 norm_vout;
out vec2 texCoord_vout;
out vec3 tangent_vout;
out vec3 bitangent_vout;

void main()
vert_vout = vec3(Gpu_ModelViewMatrix * vec4(Gpu_Vertex, 1.0));
norm_vout = Gpu_NormalMatrix * Gpu_Normal;
tangent_vout = Gpu_NormalMatrix * Gpu_MultiTexCoord1.xyz;
bitangent_vout = Gpu_NormalMatrix * normalize(cross(Gpu_Normal, Gpu_MultiTexCoord1.xyz));
texCoord_vout = vec2(Gpu_MultiTexCoord0.x, 1.0 - Gpu_MultiTexCoord0.y);

gl_Position = Gpu_ProjectionMatrix * (Gpu_ModelViewMatrix * Gpu_FlippingMatrix) * vec4(Gpu_Vertex, 1.0);

// Fragment shader
#version 150
#define GL_core_profile 1
#extension GL_EXT_gpu_shader4 : enable

precision highp float;

uniform mat4 Gpu_ModelViewMatrix;
uniform mat4 Gpu_ProjectionMatrix;
uniform mat3 Gpu_NormalMatrix;

uniform sampler2D buffer_diffuse;
uniform sampler2D buffer_normals;

uniform float Glossiness;
uniform float Anisotropy;

in vec3 vert_vout;
in vec3 norm_vout;
in vec2 texCoord_vout;
in vec3 tangent_vout;
in vec3 bitangent_vout;

out vec4 Gpu_FragColor[3];

void main()
vec3 vertex = vert_vout;
float depth = vertex.z * -1.0;

vec4 diffuseTex = texture(buffer_diffuse, texCoord_vout);

mat3 tangentFrame = mat3(normalize(tangent_vout), -normalize(bitangent_vout), norm_vout);
vec3 normalTex = texture(buffer_normals, texCoord_vout).xyz;
normalTex = normalTex * 2.0 - 1.0;
vec3 normal_vs = normalize(tangentFrame * normalTex);

gl_FragDepth = log(1.0 * depth + 1.0) / log(1.0 * 10000.0 + 1.0) * 1.0;
Gpu_FragColor[0] = vec4(depth, 0.0, 0.0, 1.0);
Gpu_FragColor[1] = vec4(normal_vs.xyz * 0.5 + 0.5, Anisotropy);
Gpu_FragColor[2] = vec4(diffuseTex.xyz, Glossiness);

Strange thing is, as long as Gpu_FlippingMatrix is identity, everything works fine, and as soon as it is flipped (e.g. scaled by 1, -1, 1 factor) all shadows are down and are definitely not correct. Although as I'm using depth variable from which I'm reconstructing position this way:

float depth_tex = texture(buffer_depth, texCoord_vout).x;
float fovy = radians(45.0);
float invFocalLenX = tan(fovy * 0.5) * aspect;
float invFocalLenY = tan(fovy * 0.5);
vec2 uv = texCoord_vout * 2.0 - 1.0;
vec3 position = vec3(uv * vec2(invFocalLenX, invFocalLenY) * depth_tex, -depth_tex);

The gl_Position should not affect it at all (it should just flip all vertices around plane (currently at height = 0.0, with normal 0.0 1.0 0.0), but it does! Is this AMD driver bug ? Well maybe, but I think it is just some bad variable here and/or there :(

Can anyone see issue I just don't get?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!