diff options
Diffstat (limited to 'src/demos/hdr/shaders/cg')
-rwxr-xr-x | src/demos/hdr/shaders/cg/hdr.cg | 130 | ||||
-rwxr-xr-x | src/demos/hdr/shaders/cg/object.cg | 30 | ||||
-rwxr-xr-x | src/demos/hdr/shaders/cg/object_hilo.cg | 31 | ||||
-rwxr-xr-x | src/demos/hdr/shaders/cg/object_vp.cg | 43 | ||||
-rwxr-xr-x | src/demos/hdr/shaders/cg/shrink.cg | 17 | ||||
-rwxr-xr-x | src/demos/hdr/shaders/cg/skybox.cg | 8 | ||||
-rwxr-xr-x | src/demos/hdr/shaders/cg/skybox_hilo.cg | 9 | ||||
-rwxr-xr-x | src/demos/hdr/shaders/cg/tonemap.cg | 37 |
8 files changed, 305 insertions, 0 deletions
diff --git a/src/demos/hdr/shaders/cg/hdr.cg b/src/demos/hdr/shaders/cg/hdr.cg new file mode 100755 index 0000000..3a0cafd --- /dev/null +++ b/src/demos/hdr/shaders/cg/hdr.cg @@ -0,0 +1,130 @@ +/* + Cg functions to decode and filter textures in Radiance (RGBE) high dynamic range format + sgg 2/15/02 + + http://www.graphics.cornell.edu/~bjw/rgbe.html +*/ + +#if 0 +typedef float4 vec4; +typedef float3 vec3; +typedef float2 vec2; +typedef float real; +#define texRECT f4texRECT +#define texCUBE f4texCUBE +#else +typedef half4 vec4; +typedef half3 vec3; +typedef half2 vec2; +typedef half real; +#define texRECT h4texRECT +#define texCUBE h4texCUBE +#endif + +struct fragin +{ + float4 wpos : WPOS; + float3 tex0 : TEXCOORD0; + float3 tex1 : TEXCOORD1; + float4 col0 : COLOR0; +}; + +// Lookup in RGBE-encoded rectangle texture +vec3 texRECT_RGBE(uniform samplerRECT tex, float2 t) +{ + vec4 rgbe = texRECT(tex, t); + real e = (rgbe[3] * 255) - 128; + return rgbe.xyz * exp2(e); +} + +// Lookup in RGBE-encoded cube map texture +vec3 texCUBE_RGBE(uniform samplerCUBE tex, float3 t) +{ + vec4 rgbe = texCUBE(tex, t); + real e = (rgbe[3] * 255) - 128; + return rgbe.xyz * exp2(e); +} + +// Lookup in RGBE-encoded rectangle texture with filtering +vec3 texRECT_RGBE_Bilinear(uniform samplerRECT tex, half2 t) +{ + float2 f = frac(t); + vec3 t0 = texRECT_RGBE(tex, t); + vec3 t1 = texRECT_RGBE(tex, t + half2(1,0) ); + vec3 t2 = lerp(t0, t1, f[0]); + t0 = texRECT_RGBE(tex, t + half2(0,1) ); + t1 = texRECT_RGBE(tex, t + half2(1,1) ); + t0 = lerp(t0, t1, f[0]); + t0 = lerp(t2, t0, f[1]); + return t0; +} + +// Lookup in cubemap encoded as two HILO cube maps +vec3 texCUBE_hilo(uniform samplerCUBE rg_tex : TEXUNIT0, uniform samplerCUBE b_tex : TEXUNIT1, float3 t) +{ + vec3 c; + c.xy = texCUBE(rg_tex, t).xy; + c.z = texCUBE(b_tex, t).x; +// c = c * c; + return c; +} + +// Lookup in rectangle texture encoded as two HILO cube maps +vec3 texRECT_hilo(uniform samplerRECT rg_tex : TEXUNIT0, uniform samplerRECT b_tex : TEXUNIT1, float2 t) +{ + vec3 c; + c.xy = texRECT(rg_tex, t).xy; + c.z = texRECT(b_tex, t).x; + return c; +} + +// bilinear lookup in float texture +vec4 texRECT_bilinear(uniform samplerRECT tex, half2 t) +{ + float2 f = frac(t); + vec4 t0 = texRECT(tex, t); + vec4 t1 = texRECT(tex, t + half2(1,0) ); + vec4 t2 = lerp(t0, t1, f[0]); + t0 = texRECT(tex, t + half2(0,1) ); + t1 = texRECT(tex, t + half2(1,1) ); + t0 = lerp(t0, t1, f[0]); + t0 = lerp(t2, t0, f[1]); + return t0; +} + +// applying vignetting based on window coordinates +void vignette(inout float3 c, float4 wpos, const float2 win_bias, const float2 win_scale) +{ + // convert window coord to [-1, 1] range + wpos.xy = (wpos.xy - win_bias) * win_scale; + // calculate distance from origin + float r = length(wpos.xy); + r = 1.0 - smoothstep(0.8, 1.5, r); + c = c * r; +} + +// refraction function from Renderman spec +// I = incident direction, N = normal, eta = relative index of refraction +half3 my_refract(half3 I, half3 N, half eta) +{ + half IdotN = dot(I,N); + half k = 1 - eta*eta*(1 - IdotN*IdotN); + + return eta*I - (eta*IdotN + sqrt(k))*N; +} + +// fresnel approximation +half my_fresnel(half3 I, half3 N, half power, half scale, half bias) +{ + return bias + (pow(max(0.0, 1.0 - dot(I, N)), power) * scale); +} + +// transform a direction vector by a 4x4 matrix +float3 transform_dir(float4x4 m, float3 v) +{ + float3 o; + o.x = dot(v, m._11_12_13); + o.y = dot(v, m._21_22_23); + o.z = dot(v, m._31_32_33); + return o; +} diff --git a/src/demos/hdr/shaders/cg/object.cg b/src/demos/hdr/shaders/cg/object.cg new file mode 100755 index 0000000..a242bc6 --- /dev/null +++ b/src/demos/hdr/shaders/cg/object.cg @@ -0,0 +1,30 @@ +// object shader + +#include "hdr.cg" + +struct v2f +{ + float4 HPosition : POSITION; + float4 P : TEXCOORD0; // position + float3 N : TEXCOORD1; // normal + float3 I : TEXCOORD2; // incident vector +}; + +half4 main(v2f In, + uniform samplerCUBE envMap : TEXUNIT0) : COLOR +{ + half3 I = normalize(half3(In.I)); + half3 N = normalize(half3(In.N)); + + half3 R = reflect(I, N); +// half3 T = refract(I, N, 0.9); + half fresnel = my_fresnel(-I, N, 5.0, 0.98, 0.02); + + half3 Creflect = texCUBE(envMap, R).rgb; // lookup reflection in HDR cube map +// half3 Crefract = texCUBE(envMap, T).rgb; // refraction + +// half3 Cout = lerp(Crefract, Creflect, fresnel); +// return half4(Cout, fresnel*0.5 + 0.5); + + return half4(Creflect * fresnel, 1.0); +} diff --git a/src/demos/hdr/shaders/cg/object_hilo.cg b/src/demos/hdr/shaders/cg/object_hilo.cg new file mode 100755 index 0000000..88a3348 --- /dev/null +++ b/src/demos/hdr/shaders/cg/object_hilo.cg @@ -0,0 +1,31 @@ +// object shader + +#include "hdr.cg" + +struct v2f +{ + float4 HPosition : POSITION; + float4 P : TEXCOORD0; // position + float3 N : TEXCOORD1; // normal + float3 I : TEXCOORD2; // incident vector +}; + +half4 main(v2f In, + uniform samplerCUBE envMap_rg : TEXUNIT0, + uniform samplerCUBE envMap_b : TEXUNIT1) : COLOR +{ + half3 I = normalize(half3(In.I)); + half3 N = normalize(half3(In.N)); + + half3 R = reflect(I, N); +// half3 T = refract(I, N, 0.9); + half fresnel = my_fresnel(-I, N, 5.0, 0.98, 0.02); + + half3 Creflect = texCUBE_hilo(envMap_rg, envMap_b, R).rgb; // lookup reflection in HDR cube map +// half3 Crefract = texCUBE_hilo(envMap_rg, envMap_b, T).rgb; // refraction + +// half3 Cout = lerp(Crefract, Creflect, fresnel); +// return half4(Cout, 1.0); + + return half4(Creflect * fresnel, 1.0); +} diff --git a/src/demos/hdr/shaders/cg/object_vp.cg b/src/demos/hdr/shaders/cg/object_vp.cg new file mode 100755 index 0000000..895dc28 --- /dev/null +++ b/src/demos/hdr/shaders/cg/object_vp.cg @@ -0,0 +1,43 @@ +#include "hdr.cg" + +// application to vertex shader +struct a2v +{ + float4 Position : POSITION; + float4 Normal : NORMAL; +}; + +// vertex shader to fragment shader +struct v2f +{ + float4 HPosition : POSITION; + float4 P : TEXCOORD0; // position + float3 N : TEXCOORD1; // normal + float3 I : TEXCOORD2; // incident vector +}; + +v2f main(a2v In, + uniform float4x4 modelViewProj, + uniform float4x4 model, + uniform float3 eyePos + ) +{ + v2f Out; + + // transform position + float4 P = mul(model, In.Position); + + // transform normal + float3 N = transform_dir(model, In.Normal.xyz); + N = normalize(N); + + // calculate incident vector + float3 I = P.xyz - eyePos; + + Out.P = P; + Out.N = N; + Out.I = I; + + Out.HPosition = mul(modelViewProj, In.Position); + return Out; +} diff --git a/src/demos/hdr/shaders/cg/shrink.cg b/src/demos/hdr/shaders/cg/shrink.cg new file mode 100755 index 0000000..7e01947 --- /dev/null +++ b/src/demos/hdr/shaders/cg/shrink.cg @@ -0,0 +1,17 @@ +// downsample float image by half + +#include "hdr.cg" + +half4 main(fragin In, + uniform samplerRECT sceneTex : TEXUNIT0 + ) : COLOR +{ + // should calculate texcoords in vertex shader here: + half4 c; + c = texRECT(sceneTex, In.tex0.xy); + c = c + texRECT(sceneTex, In.tex0.xy + float2(1, 0)); + c = c + texRECT(sceneTex, In.tex0.xy + float2(0, 1)); + c = c + texRECT(sceneTex, In.tex0.xy + float2(1, 1)); + c = c * 0.25; + return c; +} diff --git a/src/demos/hdr/shaders/cg/skybox.cg b/src/demos/hdr/shaders/cg/skybox.cg new file mode 100755 index 0000000..5ca6e6f --- /dev/null +++ b/src/demos/hdr/shaders/cg/skybox.cg @@ -0,0 +1,8 @@ +#include "hdr.cg" + +half4 main(fragin In, + uniform samplerCUBE envMap : TEXUNIT0) : COLOR +{ + half3 c = texCUBE(envMap, In.tex0).rgb; + return half4(c, 1.0); +} diff --git a/src/demos/hdr/shaders/cg/skybox_hilo.cg b/src/demos/hdr/shaders/cg/skybox_hilo.cg new file mode 100755 index 0000000..d7392bf --- /dev/null +++ b/src/demos/hdr/shaders/cg/skybox_hilo.cg @@ -0,0 +1,9 @@ +#include "hdr.cg" + +half4 main(fragin In, + uniform samplerCUBE envMap_rg : TEXUNIT0, + uniform samplerCUBE envMap_b : TEXUNIT1) : COLOR +{ + half3 c = texCUBE_hilo(envMap_rg, envMap_b, In.tex0).rgb; + return half4(c, 1.0); +} diff --git a/src/demos/hdr/shaders/cg/tonemap.cg b/src/demos/hdr/shaders/cg/tonemap.cg new file mode 100755 index 0000000..c3d218f --- /dev/null +++ b/src/demos/hdr/shaders/cg/tonemap.cg @@ -0,0 +1,37 @@ +// Tone mapping pass + +#include "hdr.cg" + +half4 main(fragin In, + uniform samplerRECT sceneTex : TEXUNIT0, + uniform samplerRECT blurTex : TEXUNIT1, + uniform sampler1D gammaTex : TEXUNIT2, + uniform samplerRECT vignetteTex : TEXUNIT3, + uniform float blurAmount, + uniform float4 windowSize, + uniform float exposure + ) : COLOR +{ + // sum original and blurred image + half3 c = lerp(texRECT(sceneTex, In.tex0.xy), texRECT_bilinear(blurTex, In.tex1.xy), blurAmount).xyz; + + // exposure + c = c * half(exposure); + + // vignette effect (makes brightness drop off with distance from center) +// vignette(c, In.wpos, windowSize.xy, windowSize.zw); + c = c * texRECT(vignetteTex, In.tex0.xy).rgb; + + // gamma correction +#if 0 + // use math + c = pow(c, 1.0 / 2.2); +#else + // use lut + c.r = h1tex1D(gammaTex, c.r); + c.g = h1tex1D(gammaTex, c.g); + c.b = h1tex1D(gammaTex, c.b); +#endif + + return half4(c, 1.0); +} |