From 32efb52cd1d7b3f4907efb1b4f8dc151e8a1a5d0 Mon Sep 17 00:00:00 2001 From: Ptolemy Date: Thu, 14 Jul 2022 18:12:19 -0700 Subject: SL-17764: PBR: Add light shaders to class 3 deferred --- .../shaders/class1/deferred/deferredUtil.glsl | 43 +++ .../shaders/class3/deferred/multiPointLightF.glsl | 157 +++++++++++ .../shaders/class3/deferred/multiPointLightV.glsl | 39 +++ .../shaders/class3/deferred/multiSpotLightF.glsl | 300 +++++++++++++++++++++ .../shaders/class3/deferred/pointLightF.glsl | 145 ++++++++++ .../shaders/class3/deferred/pointLightV.glsl | 45 ++++ .../shaders/class3/deferred/spotLightF.glsl | 287 ++++++++++++++++++++ 7 files changed, 1016 insertions(+) create mode 100644 indra/newview/app_settings/shaders/class3/deferred/multiPointLightF.glsl create mode 100644 indra/newview/app_settings/shaders/class3/deferred/multiPointLightV.glsl create mode 100644 indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl create mode 100644 indra/newview/app_settings/shaders/class3/deferred/pointLightF.glsl create mode 100644 indra/newview/app_settings/shaders/class3/deferred/pointLightV.glsl create mode 100644 indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl (limited to 'indra/newview') diff --git a/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl b/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl index e27bbce094..5dfdd2006e 100644 --- a/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl +++ b/indra/newview/app_settings/shaders/class1/deferred/deferredUtil.glsl @@ -29,6 +29,13 @@ uniform sampler2DRect depthMap; uniform mat4 inv_proj; uniform vec2 screen_res; +void calcHalfVectors(vec3 h, vec3 n, vec3 v, out float nh, out float nv, out float vh) +{ + nh = dot(n, h); + nv = dot(n, v); + vh = dot(v, h); +} + vec2 getScreenCoordinate(vec2 screenpos) { vec2 sc = screenpos.xy * 2.0; @@ -39,6 +46,8 @@ vec2 getScreenCoordinate(vec2 screenpos) return sc - vec2(1.0, 1.0); } +// See: https://aras-p.info/texts/CompactNormalStorage.html +// Method #4: Spheremap Transform, Lambert Azimuthal Equal-Area projection vec3 getNorm(vec2 screenpos) { vec2 enc = texture2DRect(normalMap, screenpos.xy).xy; @@ -51,6 +60,28 @@ vec3 getNorm(vec2 screenpos) return n; } +vec3 getNormalFromPacked(vec4 packedNormalEnvIntensityFlags) +{ + vec2 enc = packedNormalEnvIntensityFlags.xy; + vec2 fenc = enc*4-2; + float f = dot(fenc,fenc); + float g = sqrt(1-f/4); + vec3 n; + n.xy = fenc*g; + n.z = 1-f/2; + return normalize(n); // TODO: Is this normalize redundant? +} + +// return packedNormalEnvIntensityFlags since GBUFFER_FLAG_HAS_PBR needs .w +// See: C++: addDeferredAttachments(), GLSL: softenLightF +vec4 getNormalEnvIntensityFlags(vec2 screenpos, out vec3 n, out float envIntensity) +{ + vec4 packedNormalEnvIntensityFlags = texture2DRect(normalMap, screenpos.xy); + n = getNormalFromPacked( packedNormalEnvIntensityFlags ); + envIntensity = packedNormalEnvIntensityFlags.z; + return packedNormalEnvIntensityFlags; +} + float getDepth(vec2 pos_screen) { float depth = texture2DRect(depthMap, pos_screen).r; @@ -77,3 +108,15 @@ vec4 getPositionWithDepth(vec2 pos_screen, float depth) pos.w = 1.0; return pos; } + +vec2 getScreenXY(vec4 clip) +{ + vec4 ndc = clip; + ndc.xyz /= clip.w; + vec2 screen = vec2( ndc.xy * 0.5 ); + screen += 0.5; + screen *= screen_res; + return screen; +} + +// PBR Utils diff --git a/indra/newview/app_settings/shaders/class3/deferred/multiPointLightF.glsl b/indra/newview/app_settings/shaders/class3/deferred/multiPointLightF.glsl new file mode 100644 index 0000000000..5aec75ff3d --- /dev/null +++ b/indra/newview/app_settings/shaders/class3/deferred/multiPointLightF.glsl @@ -0,0 +1,157 @@ +/** + * @file class3\deferred\multiPointLightF.glsl + * + * $LicenseInfo:firstyear=2022&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2022, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ + +#extension GL_ARB_texture_rectangle : enable + +/*[EXTRA_CODE_HERE]*/ + +#ifdef DEFINE_GL_FRAGCOLOR +out vec4 frag_color; +#else +#define frag_color gl_FragColor +#endif + +uniform sampler2DRect depthMap; +uniform sampler2DRect diffuseRect; +uniform sampler2DRect specularRect; +uniform sampler2D noiseMap; +uniform sampler2D lightFunc; + +uniform vec3 env_mat[3]; +uniform float sun_wash; +uniform int light_count; +uniform vec4 light[LIGHT_COUNT]; +uniform vec4 light_col[LIGHT_COUNT]; + +uniform vec2 screen_res; +uniform float far_z; +uniform mat4 inv_proj; + +VARYING vec4 vary_fragcoord; + +void calcHalfVectors(vec3 h, vec3 n, vec3 npos, out float nh, out float nv, out float vh); +vec4 getPosition(vec2 pos_screen); +vec4 getNormalEnvIntensityFlags(vec2 screenpos, out vec3 n, out float envIntensity); +vec2 getScreenXY(vec4 clip); +vec3 srgb_to_linear(vec3 c); + +void main() +{ +#if defined(LOCAL_LIGHT_KILL) + discard; // Bail immediately +#else + vec3 final_color = vec3(0, 0, 0); + vec2 tc = getScreenXY(vary_fragcoord); + vec3 pos = getPosition(tc).xyz; + if (pos.z < far_z) + { + discard; + } + + float envIntensity; // not used for this shader + vec3 n; + vec4 norm = getNormalEnvIntensityFlags(tc, n, envIntensity); // need `norm.w` for GET_GBUFFER_FLAG() + + vec4 spec = texture2DRect(specularRect, tc); + vec3 diffuse = texture2DRect(diffuseRect, tc).rgb; + + float noise = texture2D(noiseMap, tc/128.0).b; + vec3 v = -normalize(pos); + + if (GET_GBUFFER_FLAG(GBUFFER_FLAG_HAS_PBR)) + { + vec3 colorDiffuse = vec3(0); + vec3 colorSpec = vec3(0); + + final_color = colorDiffuse + colorSpec; + } + else + { + // As of OSX 10.6.7 ATI Apple's crash when using a variable size loop + for (int i = 0; i < LIGHT_COUNT; ++i) + { + vec3 lv = light[i].xyz - pos; + float dist = length(lv); + dist /= light[i].w; + if (dist <= 1.0) + { + float da = dot(n, lv); + if (da > 0.0) + { + lv = normalize(lv); + da = dot(n, lv); + + float fa = light_col[i].a + 1.0; + float dist_atten = clamp(1.0 - (dist - 1.0 * (1.0 - fa)) / fa, 0.0, 1.0); + dist_atten *= dist_atten; + + // Tweak falloff slightly to match pre-EEP attenuation + // NOTE: this magic number also shows up in a great many other places, search for dist_atten *= to audit + dist_atten *= 2.0; + + dist_atten *= noise; + + float lit = da * dist_atten; + + vec3 col = light_col[i].rgb * lit * diffuse; + + if (spec.a > 0.0) + { + lit = min(da * 6.0, 1.0) * dist_atten; + vec3 h = normalize(lv + v); + float nh, nv, vh; + calcHalfVectors(h, n, v, nh, nv, vh); + float sa = nh; + float fres = pow(1 - dot(h, v), 5) * 0.4 + 0.5; + + float gtdenom = 2 * nh; + float gt = max(0, min(gtdenom * nv / vh, gtdenom * da / vh)); + + if (nh > 0.0) + { + float scol = fres * texture2D(lightFunc, vec2(nh, spec.a)).r * gt / (nh * da); + col += lit * scol * light_col[i].rgb * spec.rgb; + } + } + + final_color += col; + } + } + } + } + + frag_color.rgb = final_color; + frag_color.a = 0.0; +#endif // LOCAL_LIGHT_KILL + +#ifdef IS_AMD_CARD + // If it's AMD make sure the GLSL compiler sees the arrays referenced once by static index. Otherwise it seems to optimise the storage + // away which leads to unfun crashes and artifacts. + vec4 dummy1 = light[0]; + vec4 dummy2 = light_col[0]; + vec4 dummy3 = light[LIGHT_COUNT - 1]; + vec4 dummy4 = light_col[LIGHT_COUNT - 1]; +#endif +} diff --git a/indra/newview/app_settings/shaders/class3/deferred/multiPointLightV.glsl b/indra/newview/app_settings/shaders/class3/deferred/multiPointLightV.glsl new file mode 100644 index 0000000000..ad6a0fa752 --- /dev/null +++ b/indra/newview/app_settings/shaders/class3/deferred/multiPointLightV.glsl @@ -0,0 +1,39 @@ +/** + * @file class3\deferred\multiPointLightV.glsl + * + * $LicenseInfo:firstyear=2022&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2022, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ + +uniform mat4 modelview_projection_matrix; + +ATTRIBUTE vec3 position; + +VARYING vec4 vary_fragcoord; + +void main() +{ + //transform vertex + vec4 pos = modelview_projection_matrix * vec4(position.xyz, 1.0); + vary_fragcoord = pos; + + gl_Position = pos; +} diff --git a/indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl b/indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl new file mode 100644 index 0000000000..f1b06fe1ad --- /dev/null +++ b/indra/newview/app_settings/shaders/class3/deferred/multiSpotLightF.glsl @@ -0,0 +1,300 @@ +/** + * @file class3\deferred\multiSpotLightF.glsl + * + * $LicenseInfo:firstyear=2022&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2022, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ + +#extension GL_ARB_texture_rectangle : enable +#extension GL_ARB_shader_texture_lod : enable + +/*[EXTRA_CODE_HERE]*/ + +#ifdef DEFINE_GL_FRAGCOLOR +out vec4 frag_color; +#else +#define frag_color gl_FragColor +#endif + +uniform sampler2DRect diffuseRect; +uniform sampler2DRect specularRect; +uniform sampler2DRect depthMap; +uniform sampler2DRect normalMap; +uniform samplerCube environmentMap; +uniform sampler2DRect lightMap; +uniform sampler2D noiseMap; +uniform sampler2D projectionMap; +uniform sampler2D lightFunc; + +uniform mat4 proj_mat; //screen space to light space +uniform float proj_near; //near clip for projection +uniform vec3 proj_p; //plane projection is emitting from (in screen space) +uniform vec3 proj_n; +uniform float proj_focus; //distance from plane to begin blurring +uniform float proj_lod; //(number of mips in proj map) +uniform float proj_range; //range between near clip and far clip plane of projection +uniform float proj_ambient_lod; +uniform float proj_ambiance; +uniform float near_clip; +uniform float far_clip; + +uniform vec3 proj_origin; //origin of projection to be used for angular attenuation +uniform float sun_wash; +uniform int proj_shadow_idx; +uniform float shadow_fade; + +uniform vec3 center; +uniform float size; +uniform vec3 color; +uniform float falloff; + +VARYING vec4 vary_fragcoord; +uniform vec2 screen_res; + +uniform mat4 inv_proj; + +void calcHalfVectors(vec3 h, vec3 n, vec3 v, out float nh, out float nv, out float vh); +vec4 getNormalEnvIntensityFlags(vec2 screenpos, out vec3 n, out float envIntensity); +vec2 getScreenXY(vec4 clip); +vec3 srgb_to_linear(vec3 cs); + +vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod) +{ + vec4 ret = texture2DLod(projectionMap, tc, lod); + ret.rgb = srgb_to_linear(ret.rgb); + vec2 dist = vec2(0.5) - abs(tc-vec2(0.5)); + + float det = min(lod/(proj_lod*0.5), 1.0); + + float d = min(dist.x, dist.y); + + d *= min(1, d * (proj_lod - lod)); + + float edge = 0.25*det; + + ret *= clamp(d/edge, 0.0, 1.0); + + return ret; +} + +vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod) +{ + vec4 ret = texture2DLod(projectionMap, tc, lod); + ret.rgb = srgb_to_linear(ret.rgb); + + vec2 dist = vec2(0.5) - abs(tc-vec2(0.5)); + + float det = min(lod/(proj_lod*0.5), 1.0); + + float d = min(dist.x, dist.y); + + float edge = 0.25*det; + + ret *= clamp(d/edge, 0.0, 1.0); + + return ret; +} + +vec4 texture2DLodAmbient(sampler2D projectionMap, vec2 tc, float lod) +{ + vec4 ret = texture2DLod(projectionMap, tc, lod); + ret.rgb = srgb_to_linear(ret.rgb); + + vec2 dist = tc-vec2(0.5); + + float d = dot(dist,dist); + + ret *= min(clamp((0.25-d)/0.25, 0.0, 1.0), 1.0); + + return ret; +} + +vec4 getPosition(vec2 pos_screen); + +void main() +{ +#if defined(LOCAL_LIGHT_KILL) + discard; +#else + vec3 final_color = vec3(0,0,0); + vec2 tc = getScreenXY(vary_fragcoord); + vec3 pos = getPosition(tc).xyz; + + vec3 lv = center.xyz-pos.xyz; + float dist = length(lv); + + if (dist >= size) + { + discard; + } + dist /= size; + + float shadow = 1.0; + + if (proj_shadow_idx >= 0) + { + vec4 shd = texture2DRect(lightMap, tc); + shadow = (proj_shadow_idx==0)?shd.b:shd.a; + shadow += shadow_fade; + shadow = clamp(shadow, 0.0, 1.0); + } + + float envIntensity; + vec3 n; + vec4 norm = getNormalEnvIntensityFlags(tc, n, envIntensity); + + float l_dist = -dot(lv, proj_n); + + vec4 proj_tc = (proj_mat * vec4(pos.xyz, 1.0)); + if (proj_tc.z < 0.0) + { + discard; + } + + proj_tc.xyz /= proj_tc.w; + + float fa = falloff+1.0; + float dist_atten = min(1.0-(dist-1.0*(1.0-fa))/fa, 1.0); + dist_atten *= dist_atten; + dist_atten *= 2.0; + if (dist_atten <= 0.0) + { + discard; + } + + lv = proj_origin-pos.xyz; + lv = normalize(lv); + float da = dot(n, lv); + + vec3 diffuse = texture2DRect(diffuseRect, tc).rgb; + vec4 spec = texture2DRect(specularRect, tc); + vec3 dlit = vec3(0, 0, 0); + + if (GET_GBUFFER_FLAG(GBUFFER_FLAG_HAS_PBR)) + { + vec3 colorDiffuse = vec3(0); + vec3 colorSpec = vec3(0); + + final_color = colorDiffuse + colorSpec; + } + else + { + float noise = texture2D(noiseMap, tc/128.0).b; + if (proj_tc.z > 0.0 && + proj_tc.x < 1.0 && + proj_tc.y < 1.0 && + proj_tc.x > 0.0 && + proj_tc.y > 0.0) + { + float amb_da = proj_ambiance; + float lit = 0.0; + + if (da > 0.0) + { + lit = da * dist_atten * noise; + + float diff = clamp((l_dist-proj_focus)/proj_range, 0.0, 1.0); + float lod = diff * proj_lod; + + vec4 plcol = texture2DLodDiffuse(projectionMap, proj_tc.xy, lod); + + dlit = color.rgb * plcol.rgb * plcol.a; + + final_color = dlit*lit*diffuse*shadow; + + // unshadowed for consistency between forward and deferred? + amb_da += (da*0.5+0.5) /* * (1.0-shadow) */ * proj_ambiance; + } + + //float diff = clamp((proj_range-proj_focus)/proj_range, 0.0, 1.0); + vec4 amb_plcol = texture2DLodAmbient(projectionMap, proj_tc.xy, proj_lod); + + // use unshadowed for consistency between forward and deferred? + amb_da += (da*da*0.5+0.5) /* * (1.0-shadow) */ * proj_ambiance; + amb_da *= dist_atten * noise; + amb_da = min(amb_da, 1.0-lit); + + final_color += amb_da*color.rgb*diffuse.rgb*amb_plcol.rgb*amb_plcol.a; + } + + + if (spec.a > 0.0) + { + vec3 v = -normalize(pos); + dlit *= min(da*6.0, 1.0) * dist_atten; + + //vec3 ref = dot(pos+lv, n); + vec3 h = normalize(lv + v); + float nh, nv, vh; + calcHalfVectors(h, n, v, nh, nv, vh); + float sa = nh; + float fres = pow(1 - dot(h, v), 5)*0.4+0.5; + + float gtdenom = 2 * nh; + float gt = max(0, min(gtdenom * nv / vh, gtdenom * da / vh)); + + if (nh > 0.0) + { + float scol = fres*texture2D(lightFunc, vec2(nh, spec.a)).r*gt/(nh*da); + vec3 speccol = dlit*scol*spec.rgb*shadow; + speccol = clamp(speccol, vec3(0), vec3(1)); + final_color += speccol; + } + } + + if (envIntensity > 0.0) + { + vec3 ref = reflect(normalize(pos), n); + + //project from point pos in direction ref to plane proj_p, proj_n + vec3 pdelta = proj_p-pos; + float ds = dot(ref, proj_n); + + if (ds < 0.0) + { + vec3 pfinal = pos + ref * dot(pdelta, proj_n)/ds; + + vec4 stc = (proj_mat * vec4(pfinal.xyz, 1.0)); + + if (stc.z > 0.0) + { + stc /= stc.w; + + if (stc.x < 1.0 && + stc.y < 1.0 && + stc.x > 0.0 && + stc.y > 0.0) + { + final_color += color.rgb * texture2DLodSpecular(projectionMap, stc.xy, (1 - spec.a) * (proj_lod * 0.6)).rgb * shadow * envIntensity; + } + } + } + } + } + + //not sure why, but this line prevents MATBUG-194 + final_color = max(final_color, vec3(0.0)); + + //output linear + frag_color.rgb = final_color; + frag_color.a = 0.0; +#endif // LOCAL_LIGHT_KILL +} diff --git a/indra/newview/app_settings/shaders/class3/deferred/pointLightF.glsl b/indra/newview/app_settings/shaders/class3/deferred/pointLightF.glsl new file mode 100644 index 0000000000..10d8c5e87f --- /dev/null +++ b/indra/newview/app_settings/shaders/class3/deferred/pointLightF.glsl @@ -0,0 +1,145 @@ +/** + * @file class3\deferred\pointLightF.glsl + * + * $LicenseInfo:firstyear=2022&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2022, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ + +#extension GL_ARB_texture_rectangle : enable + +/*[EXTRA_CODE_HERE]*/ + +#ifdef DEFINE_GL_FRAGCOLOR +out vec4 frag_color; +#else +#define frag_color gl_FragColor +#endif + +uniform sampler2DRect diffuseRect; +uniform sampler2DRect specularRect; +uniform sampler2DRect normalMap; +uniform sampler2D noiseMap; +uniform sampler2D lightFunc; +uniform sampler2DRect depthMap; + +uniform vec3 env_mat[3]; +uniform float sun_wash; + +// light params +uniform vec3 color; +uniform float falloff; +uniform float size; + +VARYING vec4 vary_fragcoord; +VARYING vec3 trans_center; + +uniform vec2 screen_res; + +uniform mat4 inv_proj; +uniform vec4 viewport; + +void calcHalfVectors(vec3 h, vec3 n, vec3 v, out float nh, out float nv, out float vh); +vec4 getNormalEnvIntensityFlags(vec2 screenpos, out vec3 n, out float envIntensity); +vec4 getPosition(vec2 pos_screen); +vec2 getScreenXY(vec4 clip); +vec3 srgb_to_linear(vec3 c); + +void main() +{ + vec3 final_color = vec3(0); + vec2 tc = getScreenXY(vary_fragcoord); + vec3 pos = getPosition(tc).xyz; + + vec3 lv = trans_center.xyz-pos; + float dist = length(lv); + if (dist >= size) + { + discard; + } + dist /= size; + + float envIntensity; + vec3 n; + vec4 norm = getNormalEnvIntensityFlags(tc, n, envIntensity); // need `norm.w` for GET_GBUFFER_FLAG() + + float da = dot(n, lv); + if (da < 0.0) + { + discard; + } + + lv = normalize(lv); + da = dot(n, lv); + + float noise = texture2D(noiseMap, tc/128.0).b; + + vec3 diffuse = texture2DRect(diffuseRect, tc).rgb; + vec4 spec = texture2DRect(specularRect, tc); + + // Common half vectors calcs + vec3 v = -normalize(pos); + vec3 h = normalize(lv + v); + float nh, nv, vh; + calcHalfVectors(h, n, v, nh, nv, vh); + + if (GET_GBUFFER_FLAG(GBUFFER_FLAG_HAS_PBR)) + { + vec3 colorDiffuse = vec3(0); + vec3 colorSpec = vec3(0); + + final_color = colorDiffuse + colorSpec; + } + else + { + float fa = falloff+1.0; + float dist_atten = clamp(1.0-(dist-1.0*(1.0-fa))/fa, 0.0, 1.0); + dist_atten *= dist_atten; + dist_atten *= 2.0; + + float lit = da * dist_atten * noise; + + final_color = color.rgb*lit*diffuse; + + if (spec.a > 0.0) + { + lit = min(da*6.0, 1.0) * dist_atten; + + float sa = nh; + float fres = pow(1 - dot(h, v), 5) * 0.4+0.5; + float gtdenom = 2 * nh; + float gt = max(0,(min(gtdenom * nv / vh, gtdenom * da / vh))); + + if (nh > 0.0) + { + float scol = fres*texture2D(lightFunc, vec2(nh, spec.a)).r*gt/(nh*da); + final_color += lit*scol*color.rgb*spec.rgb; + } + } + + if (dot(final_color, final_color) <= 0.0) + { + discard; + } + } + + frag_color.rgb = final_color; + frag_color.a = 0.0; +} diff --git a/indra/newview/app_settings/shaders/class3/deferred/pointLightV.glsl b/indra/newview/app_settings/shaders/class3/deferred/pointLightV.glsl new file mode 100644 index 0000000000..d42c8f6cf6 --- /dev/null +++ b/indra/newview/app_settings/shaders/class3/deferred/pointLightV.glsl @@ -0,0 +1,45 @@ +/** + * @file class3\deferred\pointLightV.glsl + * + * $LicenseInfo:firstyear=2022&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2022, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ + +uniform mat4 modelview_projection_matrix; +uniform mat4 modelview_matrix; + +ATTRIBUTE vec3 position; + +uniform vec3 center; +uniform float size; + +VARYING vec4 vary_fragcoord; +VARYING vec3 trans_center; + +void main() +{ + //transform vertex + vec3 p = position*size+center; + vec4 pos = modelview_projection_matrix * vec4(p.xyz, 1.0); + vary_fragcoord = pos; + trans_center = (modelview_matrix*vec4(center.xyz, 1.0)).xyz; + gl_Position = pos; +} diff --git a/indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl b/indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl new file mode 100644 index 0000000000..8768500682 --- /dev/null +++ b/indra/newview/app_settings/shaders/class3/deferred/spotLightF.glsl @@ -0,0 +1,287 @@ +/** + * @file class3\deferred\spotLightF.glsl + * + * $LicenseInfo:firstyear=2022&license=viewerlgpl$ + * Second Life Viewer Source Code + * Copyright (C) 2022, Linden Research, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License only. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA + * $/LicenseInfo$ + */ + +#extension GL_ARB_texture_rectangle : enable +#extension GL_ARB_shader_texture_lod : enable + +/*[EXTRA_CODE_HERE]*/ + +#ifdef DEFINE_GL_FRAGCOLOR +out vec4 frag_color; +#else +#define frag_color gl_FragColor +#endif + +uniform sampler2DRect diffuseRect; +uniform sampler2DRect specularRect; +uniform sampler2DRect depthMap; +uniform sampler2DRect normalMap; +uniform samplerCube environmentMap; +uniform sampler2DRect lightMap; +uniform sampler2D noiseMap; +uniform sampler2D projectionMap; +uniform sampler2D lightFunc; + +uniform mat4 proj_mat; //screen space to light space +uniform float proj_near; //near clip for projection +uniform vec3 proj_p; //plane projection is emitting from (in screen space) +uniform vec3 proj_n; +uniform float proj_focus; //distance from plane to begin blurring +uniform float proj_lod; //(number of mips in proj map) +uniform float proj_range; //range between near clip and far clip plane of projection +uniform float proj_ambient_lod; +uniform float proj_ambiance; +uniform float near_clip; +uniform float far_clip; + +uniform vec3 proj_origin; //origin of projection to be used for angular attenuation +uniform float sun_wash; +uniform int proj_shadow_idx; +uniform float shadow_fade; + +uniform float size; +uniform vec3 color; +uniform float falloff; + +VARYING vec3 trans_center; +VARYING vec4 vary_fragcoord; +uniform vec2 screen_res; + +uniform mat4 inv_proj; + +void calcHalfVectors(vec3 h, vec3 n, vec3 v, out float nh, out float nv, out float vh); +vec2 getScreenXY(vec4 clip_point); +vec4 getNormalEnvIntensityFlags(vec2 screenpos, out vec3 n, out float envIntensity); +vec3 srgb_to_linear(vec3 c); + +vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod) +{ + vec4 ret = texture2DLod(projectionMap, tc, lod); + ret.rgb = srgb_to_linear(ret.rgb); + + vec2 dist = vec2(0.5) - abs(tc-vec2(0.5)); + float det = min(lod/(proj_lod*0.5), 1.0); + float d = min(dist.x, dist.y); + d *= min(1, d * (proj_lod - lod)); + float edge = 0.25*det; + ret *= clamp(d/edge, 0.0, 1.0); + + return ret; +} + +vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod) +{ + vec4 ret = texture2DLod(projectionMap, tc, lod); + ret.rgb = srgb_to_linear(ret.rgb); + + vec2 dist = vec2(0.5) - abs(tc-vec2(0.5)); + float det = min(lod/(proj_lod*0.5), 1.0); + float d = min(dist.x, dist.y); + float edge = 0.25*det; + ret *= clamp(d/edge, 0.0, 1.0); + + return ret; +} + +vec4 texture2DLodAmbient(sampler2D projectionMap, vec2 tc, float lod) +{ + vec4 ret = texture2DLod(projectionMap, tc, lod); + ret.rgb = srgb_to_linear(ret.rgb); + + vec2 dist = tc-vec2(0.5); + float d = dot(dist,dist); + ret *= min(clamp((0.25-d)/0.25, 0.0, 1.0), 1.0); + + return ret; +} + +vec4 getPosition(vec2 pos_screen); + +void main() +{ +#if defined(LOCAL_LIGHT_KILL) + discard; +#else + vec3 final_color = vec3(0,0,0); + vec2 tc = getScreenXY(vary_fragcoord); + vec3 pos = getPosition(tc).xyz; + + vec3 lv = trans_center.xyz-pos.xyz; + float dist = length(lv); + if (dist >= size) + { + discard; + } + dist /= size; + + float shadow = 1.0; + + if (proj_shadow_idx >= 0) + { + vec4 shd = texture2DRect(lightMap, tc); + shadow = (proj_shadow_idx == 0) ? shd.b : shd.a; + shadow += shadow_fade; + shadow = clamp(shadow, 0.0, 1.0); + } + + float envIntensity; + vec3 n; + vec4 norm = getNormalEnvIntensityFlags(tc, n, envIntensity); // need `norm.w` for GET_GBUFFER_FLAG() + + float l_dist = -dot(lv, proj_n); + + vec4 proj_tc = (proj_mat * vec4(pos.xyz, 1.0)); + if (proj_tc.z < 0.0) + { + discard; + } + + proj_tc.xyz /= proj_tc.w; + + float fa = falloff+1.0; + float dist_atten = min(1.0-(dist-1.0*(1.0-fa))/fa, 1.0); + dist_atten *= dist_atten; + dist_atten *= 2.0; + + if (dist_atten <= 0.0) + { + discard; + } + + lv = proj_origin-pos.xyz; + lv = normalize(lv); + float da = dot(n, lv); + + vec3 diffuse = texture2DRect(diffuseRect, tc).rgb; + vec4 spec = texture2DRect(specularRect, tc); + vec3 dlit = vec3(0, 0, 0); + + if (GET_GBUFFER_FLAG(GBUFFER_FLAG_HAS_PBR)) + { + vec3 colorDiffuse = vec3(0); + vec3 colorSpec = vec3(0); + + final_color = colorDiffuse + colorSpec; + } + else + { + float noise = texture2D(noiseMap, tc/128.0).b; + if (proj_tc.z > 0.0 && + proj_tc.x < 1.0 && + proj_tc.y < 1.0 && + proj_tc.x > 0.0 && + proj_tc.y > 0.0) + { + float amb_da = proj_ambiance; + float lit = 0.0; + + if (da > 0.0) + { + lit = da * dist_atten * noise; + + float diff = clamp((l_dist-proj_focus)/proj_range, 0.0, 1.0); + float lod = diff * proj_lod; + + vec4 plcol = texture2DLodDiffuse(projectionMap, proj_tc.xy, lod); + + dlit = color.rgb * plcol.rgb * plcol.a; + + final_color = dlit*lit*diffuse*shadow; + + amb_da += (da*0.5+0.5) /* * (1.0-shadow) */ * proj_ambiance; + } + + //float diff = clamp((proj_range-proj_focus)/proj_range, 0.0, 1.0); + vec4 amb_plcol = texture2DLodAmbient(projectionMap, proj_tc.xy, proj_lod); + + amb_da += (da*da*0.5+0.5) /* * (1.0-shadow) */ * proj_ambiance; + amb_da *= dist_atten * noise; + amb_da = min(amb_da, 1.0-lit); + + final_color += amb_da*color.rgb*diffuse.rgb*amb_plcol.rgb*amb_plcol.a; + } + + if (spec.a > 0.0) + { + dlit *= min(da*6.0, 1.0) * dist_atten; + vec3 v = -normalize(pos); + + //vec3 ref = dot(pos+lv, n); + vec3 h = normalize(lv + v); + float nh, nv, vh; + calcHalfVectors(h, n, v, nh, nv, vh); + float sa = nh; + float fres = pow(1 - dot(h, v), 5)*0.4+0.5; + + float gtdenom = 2 * nh; + float gt = max(0, min(gtdenom * nv / vh, gtdenom * da / vh)); + + if (nh > 0.0) + { + float scol = fres*texture2D(lightFunc, vec2(nh, spec.a)).r*gt/(nh*da); + vec3 speccol = dlit*scol*spec.rgb*shadow; + speccol = clamp(speccol, vec3(0), vec3(1)); + final_color += speccol; + } + } + + if (envIntensity > 0.0) + { + vec3 ref = reflect(normalize(pos), n); + + //project from point pos in direction ref to plane proj_p, proj_n + vec3 pdelta = proj_p-pos; + float ds = dot(ref, proj_n); + + if (ds < 0.0) + { + vec3 pfinal = pos + ref * dot(pdelta, proj_n)/ds; + + vec4 stc = (proj_mat * vec4(pfinal.xyz, 1.0)); + + if (stc.z > 0.0) + { + stc /= stc.w; + + if (stc.x < 1.0 && + stc.y < 1.0 && + stc.x > 0.0 && + stc.y > 0.0) + { + final_color += color.rgb * texture2DLodSpecular(projectionMap, stc.xy, (1 - spec.a) * (proj_lod * 0.6)).rgb * shadow * envIntensity; + } + } + } + } + } + + //not sure why, but this line prevents MATBUG-194 + final_color = max(final_color, vec3(0.0)); + + //output linear colors as gamma correction happens down stream + frag_color.rgb = final_color; + frag_color.a = 0.0; +#endif // LOCAL_LIGHT_KILL +} -- cgit v1.2.3