From c45f618852dfeeae44cf68f934f8d57609b661a5 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 12 Jan 2015 15:00:35 -0800 Subject: [PATCH] Factorising the shader code to unpack data from the deferred buffer --- libraries/render-utils/src/DeferredBuffer.slh | 65 +++++++++++++++++++ .../render-utils/src/directional_light.slf | 47 +++----------- .../directional_light_cascaded_shadow_map.slf | 54 ++++----------- .../src/directional_light_shadow_map.slf | 48 +++----------- 4 files changed, 96 insertions(+), 118 deletions(-) create mode 100755 libraries/render-utils/src/DeferredBuffer.slh diff --git a/libraries/render-utils/src/DeferredBuffer.slh b/libraries/render-utils/src/DeferredBuffer.slh new file mode 100755 index 0000000000..c5630c3564 --- /dev/null +++ b/libraries/render-utils/src/DeferredBuffer.slh @@ -0,0 +1,65 @@ + +<@if not DEFERRED_BUFFER_SLH@> +<@def DEFERRED_BUFFER_SLH@> + + +// the diffuse texture +uniform sampler2D diffuseMap; + +// the normal texture +uniform sampler2D normalMap; + +// the specular texture +uniform sampler2D specularMap; + +// the depth texture +uniform sampler2D depthMap; + +// the distance to the near clip plane +uniform float near; + +// scale factor for depth: (far - near) / far +uniform float depthScale; + +// offset for depth texture coordinates +uniform vec2 depthTexCoordOffset; + +// scale for depth texture coordinates +uniform vec2 depthTexCoordScale; + +struct DeferredFragment { + float depthVal; + vec4 normalVal; + vec4 diffuseVal; + vec4 specularVal; + vec4 position; + vec3 normal; +}; + +DeferredFragment unpackDeferredFragment(vec2 texcoord) { + DeferredFragment frag; + frag.depthVal = texture2D(depthMap, texcoord).r; + frag.normalVal = texture2D(normalMap, texcoord); + frag.diffuseVal = texture2D(diffuseMap, texcoord); + frag.specularVal = texture2D(specularMap, texcoord); + + // compute the view space position using the depth + float z = near / (frag.depthVal * depthScale - 1.0); + frag.position = vec4((depthTexCoordOffset + texcoord * depthTexCoordScale) * z, z, 1.0); + + // Unpack the normal from the map + frag.normal = normalize(frag.normalVal.xyz * 2.0 - vec3(1.0)); + + return frag; +} + +<@endif@> diff --git a/libraries/render-utils/src/directional_light.slf b/libraries/render-utils/src/directional_light.slf index 7a02e3c3a8..db963be913 100644 --- a/libraries/render-utils/src/directional_light.slf +++ b/libraries/render-utils/src/directional_light.slf @@ -12,56 +12,29 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// the diffuse texture -uniform sampler2D diffuseMap; - -// the normal texture -uniform sampler2D normalMap; - -// the specular texture -uniform sampler2D specularMap; - -// the depth texture -uniform sampler2D depthMap; - -// the distance to the near clip plane -uniform float near; - -// scale factor for depth: (far - near) / far -uniform float depthScale; - -// offset for depth texture coordinates -uniform vec2 depthTexCoordOffset; - -// scale for depth texture coordinates -uniform vec2 depthTexCoordScale; +// Everything about deferred buffer +<@include DeferredBuffer.slh@> void main(void) { - float depthVal = texture2D(depthMap, gl_TexCoord[0].st).r; - vec4 normalVal = texture2D(normalMap, gl_TexCoord[0].st); - vec4 diffuseVal = texture2D(diffuseMap, gl_TexCoord[0].st); - vec4 specularVal = texture2D(specularMap, gl_TexCoord[0].st); + DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); + + vec4 normalVal = frag.normalVal; + vec4 diffuseVal = frag.diffuseVal; + vec4 specularVal = frag.specularVal; - // compute the view space position using the depth - float z = near / (depthVal * depthScale - 1.0); - vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 0.0); - // Light mapped or not ? if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { gl_FragColor = vec4(diffuseVal.rgb * specularVal.rgb, 1.0); } else { - // get the normal from the map - vec3 normalizedNormal = normalize(normalVal.xyz * 2.0 - vec3(1.0)); - // compute the base color based on OpenGL lighting model - float diffuse = dot(normalizedNormal, gl_LightSource[0].position.xyz); + float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz); float facingLight = step(0.0, diffuse); vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)); // compute the specular multiplier (sans exponent) - float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(position.xyz)), - normalizedNormal)); + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(frag.position.xyz)), + frag.normal)); // add specular contribution vec4 specularColor = specularVal; diff --git a/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf b/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf index 08fdc3a908..f7e21252fb 100644 --- a/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf +++ b/libraries/render-utils/src/directional_light_cascaded_shadow_map.slf @@ -12,52 +12,24 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// the diffuse texture -uniform sampler2D diffuseMap; - -// the normal texture -uniform sampler2D normalMap; - -// the specular texture -uniform sampler2D specularMap; - -// the depth texture -uniform sampler2D depthMap; +// Everything about deferred buffer +<@include DeferredBuffer.slh@> // Everything about shadow <@include Shadow.slh@> -// the distance to the near clip plane -uniform float near; - -// scale factor for depth: (far - near) / far -uniform float depthScale; - -// offset for depth texture coordinates -uniform vec2 depthTexCoordOffset; - -// scale for depth texture coordinates -uniform vec2 depthTexCoordScale; - void main(void) { - float depthVal = texture2D(depthMap, gl_TexCoord[0].st).r; - vec4 normalVal = texture2D(normalMap, gl_TexCoord[0].st); - vec4 diffuseVal = texture2D(diffuseMap, gl_TexCoord[0].st); - vec4 specularVal = texture2D(specularMap, gl_TexCoord[0].st); - - // compute the view space position using the depth - float z = near / (depthVal * depthScale - 1.0); - vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 1.0); + DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); + vec4 normalVal = frag.normalVal; + vec4 diffuseVal = frag.diffuseVal; + vec4 specularVal = frag.specularVal; // Eval shadow Texcoord and then Attenuation - vec4 shadowTexcoord = evalCascadedShadowTexcoord(position); + vec4 shadowTexcoord = evalCascadedShadowTexcoord(frag.position); float shadowAttenuation = evalShadowAttenuation(shadowTexcoord); - // get the normal from the map - vec3 normalizedNormal = normalize(normalVal.xyz * 2.0 - vec3(1.0)); - // how much this fragment faces the light direction - float diffuse = dot(normalizedNormal, gl_LightSource[0].position.xyz); + float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz); // Light mapped or not ? if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { @@ -83,19 +55,15 @@ void main(void) { float facingLight = step(0.0, diffuse) * shadowAttenuation; // compute the base color based on OpenGL lighting model - vec3 baseColor = diffuseVal.rgb * (/*gl_FrontLightModelProduct.sceneColor.rgb + */ + vec3 baseColor = diffuseVal.rgb * (gl_FrontLightModelProduct.sceneColor.rgb + gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)); // compute the specular multiplier (sans exponent) - float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(position.xyz)), - normalizedNormal)); + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(frag.position.xyz)), + frag.normal)); // add specular contribution vec4 specularColor = specularVal; gl_FragColor = vec4(baseColor.rgb + pow(specular, specularColor.a * 128.0) * specularColor.rgb, normalVal.a); - - if (gl_FragCoord.x > 1024) { - gl_FragColor = vec4( (gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)), normalVal.a); - } } } diff --git a/libraries/render-utils/src/directional_light_shadow_map.slf b/libraries/render-utils/src/directional_light_shadow_map.slf index 7a34f56f93..1a781af131 100644 --- a/libraries/render-utils/src/directional_light_shadow_map.slf +++ b/libraries/render-utils/src/directional_light_shadow_map.slf @@ -12,52 +12,24 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// the diffuse texture -uniform sampler2D diffuseMap; - -// the normal texture -uniform sampler2D normalMap; - -// the specular texture -uniform sampler2D specularMap; - -// the depth texture -uniform sampler2D depthMap; +// Everything about deferred buffer +<@include DeferredBuffer.slh@> // Everything about shadow <@include Shadow.slh@> -// the distance to the near clip plane -uniform float near; - -// scale factor for depth: (far - near) / far -uniform float depthScale; - -// offset for depth texture coordinates -uniform vec2 depthTexCoordOffset; - -// scale for depth texture coordinates -uniform vec2 depthTexCoordScale; - void main(void) { - float depthVal = texture2D(depthMap, gl_TexCoord[0].st).r; - vec4 normalVal = texture2D(normalMap, gl_TexCoord[0].st); - vec4 diffuseVal = texture2D(diffuseMap, gl_TexCoord[0].st); - vec4 specularVal = texture2D(specularMap, gl_TexCoord[0].st); - - // compute the view space position using the depth - float z = near / (depthVal * depthScale - 1.0); - vec4 position = vec4((depthTexCoordOffset + gl_TexCoord[0].st * depthTexCoordScale) * z, z, 1.0); + DeferredFragment frag = unpackDeferredFragment(gl_TexCoord[0].st); + vec4 normalVal = frag.normalVal; + vec4 diffuseVal = frag.diffuseVal; + vec4 specularVal = frag.specularVal; // Eval shadow Texcoord and then Attenuation - vec4 shadowTexcoord = evalShadowTexcoord(position); + vec4 shadowTexcoord = evalShadowTexcoord(frag.position); float shadowAttenuation = evalShadowAttenuation(shadowTexcoord); - // get the normal from the map - vec3 normalizedNormal = normalize(normalVal.xyz * 2.0 - vec3(1.0)); - // how much this fragment faces the light direction - float diffuse = dot(normalizedNormal, gl_LightSource[0].position.xyz); + float diffuse = dot(frag.normal, gl_LightSource[0].position.xyz); // Light mapped or not ? if ((normalVal.a >= 0.45) && (normalVal.a <= 0.55)) { @@ -86,8 +58,8 @@ void main(void) { gl_FrontLightProduct[0].ambient.rgb + gl_FrontLightProduct[0].diffuse.rgb * (diffuse * facingLight)); // compute the specular multiplier (sans exponent) - float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(position.xyz)), - normalizedNormal)); + float specular = facingLight * max(0.0, dot(normalize(gl_LightSource[0].position.xyz - normalize(frag.position.xyz)), + frag.normal)); // add specular contribution vec4 specularColor = specularVal;