diff options
Diffstat (limited to 'source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl')
-rw-r--r-- | source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl | 1004 |
1 files changed, 647 insertions, 357 deletions
diff --git a/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl b/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl index 5b6f30a116b..f63a9665810 100644 --- a/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl +++ b/source/blender/draw/engines/eevee/shaders/lit_surface_frag.glsl @@ -3,45 +3,17 @@ uniform int light_count; uniform int probe_count; uniform int grid_count; uniform int planar_count; -uniform mat4 ViewMatrix; -uniform mat4 ViewMatrixInverse; -uniform sampler2DArray probePlanars; - -uniform sampler2DArray probeCubes; -uniform float lodMax; uniform bool specToggle; +uniform bool ssrToggle; + +uniform float refractionDepth; #ifndef UTIL_TEX #define UTIL_TEX uniform sampler2DArray utilTex; #endif /* UTIL_TEX */ -uniform sampler2DArray shadowCubes; -uniform sampler2DArrayShadow shadowCascades; - -layout(std140) uniform probe_block { - ProbeData probes_data[MAX_PROBE]; -}; - -layout(std140) uniform grid_block { - GridData grids_data[MAX_GRID]; -}; - -layout(std140) uniform planar_block { - PlanarData planars_data[MAX_PLANAR]; -}; - -layout(std140) uniform light_block { - LightData lights_data[MAX_LIGHT]; -}; - -layout(std140) uniform shadow_block { - ShadowCubeData shadows_cube_data[MAX_SHADOW_CUBE]; - ShadowMapData shadows_map_data[MAX_SHADOW_MAP]; - ShadowCascadeData shadows_cascade_data[MAX_SHADOW_CASCADE]; -}; - in vec3 worldPosition; in vec3 viewPosition; @@ -53,470 +25,788 @@ in vec3 worldNormal; in vec3 viewNormal; #endif -#define cameraForward normalize(ViewMatrixInverse[2].xyz) -#define cameraPos ViewMatrixInverse[3].xyz +uniform float maxRoughness; +uniform int rayCount; -/* type */ -#define POINT 0.0 -#define SUN 1.0 -#define SPOT 2.0 -#define HEMI 3.0 -#define AREA 4.0 +/* ----------- default ----------- */ -#ifdef HAIR_SHADER -vec3 light_diffuse(LightData ld, ShadingData sd, vec3 albedo) +vec3 eevee_surface_lit(vec3 N, vec3 albedo, vec3 f0, float roughness, float ao, int ssr_id, out vec3 ssr_spec) { - if (ld.l_type == SUN) { - return direct_diffuse_sun(ld, sd) * albedo; - } - else if (ld.l_type == AREA) { - return direct_diffuse_rectangle(ld, sd) * albedo; - } - else { - return direct_diffuse_sphere(ld, sd) * albedo; - } -} + /* Zero length vectors cause issues, see: T51979. */ +#if 0 + N = normalize(N); +#else + { + float len = length(N); + if (isnan(len)) { + return vec3(0.0); + } + N /= len; + } +#endif -vec3 light_specular(LightData ld, ShadingData sd, float roughness, vec3 f0) -{ - if (ld.l_type == SUN) { - return direct_ggx_sun(ld, sd, roughness, f0); - } - else if (ld.l_type == AREA) { - return direct_ggx_rectangle(ld, sd, roughness, f0); - } - else { - return direct_ggx_sphere(ld, sd, roughness, f0); - } -} + roughness = clamp(roughness, 1e-8, 0.9999); + float roughnessSquared = roughness * roughness; -void light_shade( - LightData ld, ShadingData sd, vec3 albedo, float roughness, vec3 f0, - out vec3 diffuse, out vec3 specular) -{ - const float transmission = 0.3; /* Uniform internal scattering factor */ - ShadingData sd_new = sd; + vec3 V = cameraVec; - vec3 lamp_vec; + /* ---------------- SCENE LAMPS LIGHTING ----------------- */ - if (ld.l_type == SUN || ld.l_type == AREA) { - lamp_vec = ld.l_forward; - } - else { - lamp_vec = -sd.l_vector; - } +#ifdef HAIR_SHADER + vec3 norm_view = cross(V, N); + norm_view = normalize(cross(norm_view, N)); /* Normal facing view */ +#endif - vec3 norm_view = cross(sd.V, sd.N); - norm_view = normalize(cross(norm_view, sd.N)); /* Normal facing view */ + vec3 diff = vec3(0.0); + vec3 spec = vec3(0.0); + for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) { + LightData ld = lights_data[i]; - vec3 norm_lamp = cross(lamp_vec, sd.N); - norm_lamp = normalize(cross(sd.N, norm_lamp)); /* Normal facing lamp */ + vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ + l_vector.xyz = ld.l_position - worldPosition; + l_vector.w = length(l_vector.xyz); - /* Rotate view vector onto the cross(tangent, light) plane */ - vec3 view_vec = normalize(norm_lamp * dot(norm_view, sd.V) + sd.N * dot(sd.N, sd.V)); + vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, viewPosition, viewNormal, l_vector); - float occlusion = (dot(norm_view, norm_lamp) * 0.5 + 0.5); - float occltrans = transmission + (occlusion * (1.0 - transmission)); /* Includes transmission component */ +#ifdef HAIR_SHADER + vec3 norm_lamp, view_vec; + float occlu_trans, occlu; + light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec); - sd_new.N = -norm_lamp; + diff += l_color_vis * light_diffuse(ld, -norm_lamp, V, l_vector) * occlu_trans; + spec += l_color_vis * light_specular(ld, N, view_vec, l_vector, roughnessSquared, f0) * occlu; +#else + diff += l_color_vis * light_diffuse(ld, N, V, l_vector); + spec += l_color_vis * light_specular(ld, N, V, l_vector, roughnessSquared, f0); +#endif + } - diffuse = light_diffuse(ld, sd_new, albedo) * occltrans; + /* Accumulate outgoing radiance */ + vec3 out_light = diff * albedo + spec * float(specToggle); - sd_new.V = view_vec; +#ifdef HAIR_SHADER + N = -norm_view; +#endif - specular = light_specular(ld, sd_new, roughness, f0) * occlusion; -} -#else -void light_shade( - LightData ld, ShadingData sd, vec3 albedo, float roughness, vec3 f0, - out vec3 diffuse, out vec3 specular) -{ -#ifdef USE_LTC - if (ld.l_type == SUN) { - /* TODO disk area light */ - diffuse = direct_diffuse_sun(ld, sd) * albedo; - specular = direct_ggx_sun(ld, sd, roughness, f0); + /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ + + /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ + vec4 spec_accum = vec4(0.0); + + /* SSR lobe is applied later in a defered style */ + if (!(ssrToggle && ssr_id == outputSsrId)) { + /* Planar Reflections */ + for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999; ++i) { + PlanarData pd = planars_data[i]; + + float fade = probe_attenuation_planar(pd, worldPosition, N, roughness); + + if (fade > 0.0) { + vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); + accumulate_light(spec, fade, spec_accum); + } + } + + /* Specular probes */ + vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); + + /* Starts at 1 because 0 is world probe */ + for (int i = 1; i < MAX_PROBE && i < probe_count && spec_accum.a < 0.999; ++i) { + CubeData cd = probes_data[i]; + + float fade = probe_attenuation_cube(cd, worldPosition); + + if (fade > 0.0) { + vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness); + accumulate_light(spec, fade, spec_accum); + } + } + + /* World Specular */ + if (spec_accum.a < 0.999) { + vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); + accumulate_light(spec, 1.0, spec_accum); + } } - else if (ld.l_type == AREA) { - diffuse = direct_diffuse_rectangle(ld, sd) * albedo; - specular = direct_ggx_rectangle(ld, sd, roughness, f0); + + vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0)); + + /* Ambient Occlusion */ + vec3 bent_normal; + float final_ao = occlusion_compute(N, viewPosition, ao, rand.rg, bent_normal); + + /* Get Brdf intensity */ + vec2 uv = lut_coords(dot(N, V), roughness); + vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; + + ssr_spec = F_ibl(f0, brdf_lut); + if (!(ssrToggle && ssr_id == outputSsrId)) { + ssr_spec *= specular_occlusion(dot(N, V), final_ao, roughness); } - else { - diffuse = direct_diffuse_sphere(ld, sd) * albedo; - specular = direct_ggx_sphere(ld, sd, roughness, f0); + out_light += spec_accum.rgb * ssr_spec * float(specToggle); + + /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ----------------- */ + + /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ + vec4 diff_accum = vec4(0.0); + + /* Start at 1 because 0 is world irradiance */ + for (int i = 1; i < MAX_GRID && i < grid_count && diff_accum.a < 0.999; ++i) { + GridData gd = grids_data[i]; + + vec3 localpos; + float fade = probe_attenuation_grid(gd, worldPosition, localpos); + + if (fade > 0.0) { + vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); + accumulate_light(diff, fade, diff_accum); + } } -#else - if (ld.l_type == SUN) { - diffuse = direct_diffuse_sun(ld, sd) * albedo; - specular = direct_ggx_sun(ld, sd, roughness, f0); + + /* World Diffuse */ + if (diff_accum.a < 0.999 && grid_count > 0) { + vec3 diff = probe_evaluate_world_diff(bent_normal); + accumulate_light(diff, 1.0, diff_accum); } - else { - diffuse = direct_diffuse_point(ld, sd) * albedo; - specular = direct_ggx_point(sd, roughness, f0); + + out_light += diff_accum.rgb * albedo * gtao_multibounce(final_ao, albedo); + + return out_light; +} + +/* ----------- CLEAR COAT ----------- */ + +vec3 eevee_surface_clearcoat_lit( + vec3 N, vec3 albedo, vec3 f0, float roughness, + vec3 C_N, float C_intensity, float C_roughness, /* Clearcoat params */ + float ao, int ssr_id, out vec3 ssr_spec) +{ + roughness = clamp(roughness, 1e-8, 0.9999); + float roughnessSquared = roughness * roughness; + C_roughness = clamp(C_roughness, 1e-8, 0.9999); + float C_roughnessSquared = C_roughness * C_roughness; + + /* Zero length vectors cause issues, see: T51979. */ +#if 0 + N = normalize(N); + C_N = normalize(C_N); +#else + { + float len = length(N); + if (isnan(len)) { + return vec3(0.0); + } + N /= len; + + len = length(C_N); + if (isnan(len)) { + return vec3(0.0); + } + C_N /= len; } #endif - specular *= float(specToggle); -} + vec3 V = cameraVec; + + /* ---------------- SCENE LAMPS LIGHTING ----------------- */ + +#ifdef HAIR_SHADER + vec3 norm_view = cross(V, N); + norm_view = normalize(cross(norm_view, N)); /* Normal facing view */ #endif -void light_visibility(LightData ld, ShadingData sd, out float vis) -{ - vis = 1.0; + vec3 diff = vec3(0.0); + vec3 spec = vec3(0.0); + for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) { + LightData ld = lights_data[i]; - if (ld.l_type == SPOT) { - float z = dot(ld.l_forward, sd.l_vector); - vec3 lL = sd.l_vector / z; - float x = dot(ld.l_right, lL) / ld.l_sizex; - float y = dot(ld.l_up, lL) / ld.l_sizey; + vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ + l_vector.xyz = ld.l_position - worldPosition; + l_vector.w = length(l_vector.xyz); - float ellipse = 1.0 / sqrt(1.0 + x * x + y * y); + vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, viewPosition, viewNormal, l_vector); - float spotmask = smoothstep(0.0, 1.0, (ellipse - ld.l_spot_size) / ld.l_spot_blend); +#ifdef HAIR_SHADER + vec3 norm_lamp, view_vec; + float occlu_trans, occlu; + light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec); - vis *= spotmask; - vis *= step(0.0, -dot(sd.l_vector, ld.l_forward)); - } - else if (ld.l_type == AREA) { - vis *= step(0.0, -dot(sd.l_vector, ld.l_forward)); + diff += l_color_vis * light_diffuse(ld, -norm_lamp, V, l_vector) * occlu_trans; + spec += l_color_vis * light_specular(ld, N, view_vec, l_vector, roughnessSquared, f0) * occlu; + spec += l_color_vis * light_specular(ld, C_N, view_vec, l_vector, C_roughnessSquared, f0) * C_intensity * occlu; +#else + diff += l_color_vis * light_diffuse(ld, N, V, l_vector); + spec += l_color_vis * light_specular(ld, N, V, l_vector, roughnessSquared, f0); + spec += l_color_vis * light_specular(ld, C_N, V, l_vector, C_roughnessSquared, f0) * C_intensity; +#endif } - /* shadowing */ - if (ld.l_shadowid >= (MAX_SHADOW_MAP + MAX_SHADOW_CUBE)) { - /* Shadow Cascade */ - float shid = ld.l_shadowid - (MAX_SHADOW_CUBE + MAX_SHADOW_MAP); - ShadowCascadeData smd = shadows_cascade_data[int(shid)]; + /* Accumulate outgoing radiance */ + vec3 out_light = diff * albedo + spec * float(specToggle); - /* Finding Cascade index */ - vec4 z = vec4(-dot(cameraPos - worldPosition, cameraForward)); - vec4 comp = step(z, smd.split_distances); - float cascade = dot(comp, comp); - mat4 shadowmat; - float bias; +#ifdef HAIR_SHADER + N = -norm_view; +#endif - /* Manual Unrolling of a loop for better performance. - * Doing fetch directly with cascade index leads to - * major performance impact. (0.27ms -> 10.0ms for 1 light) */ - if (cascade == 0.0) { - shadowmat = smd.shadowmat[0]; - bias = smd.bias[0]; - } - else if (cascade == 1.0) { - shadowmat = smd.shadowmat[1]; - bias = smd.bias[1]; - } - else if (cascade == 2.0) { - shadowmat = smd.shadowmat[2]; - bias = smd.bias[2]; - } - else { - shadowmat = smd.shadowmat[3]; - bias = smd.bias[3]; - } + /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ + + /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ + vec4 spec_accum = vec4(0.0); + vec4 C_spec_accum = vec4(0.0); - vec4 shpos = shadowmat * vec4(sd.W, 1.0); - shpos.z -= bias * shpos.w; - shpos.xyz /= shpos.w; + /* Planar Reflections */ + for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999; ++i) { + PlanarData pd = planars_data[i]; - vis *= texture(shadowCascades, vec4(shpos.xy, shid * float(MAX_CASCADE_NUM) + cascade, shpos.z)); + /* Fade on geometric normal. */ + float fade = probe_attenuation_planar(pd, worldPosition, worldNormal, roughness); + + if (fade > 0.0) { + if (!(ssrToggle && ssr_id == outputSsrId)) { + vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); + accumulate_light(spec, fade, spec_accum); + } + + vec3 C_spec = probe_evaluate_planar(float(i), pd, worldPosition, C_N, V, C_roughness, fade); + accumulate_light(C_spec, fade, C_spec_accum); + } } - else if (ld.l_shadowid >= 0.0) { - /* Shadow Cube */ - float shid = ld.l_shadowid; - ShadowCubeData scd = shadows_cube_data[int(shid)]; - vec3 cubevec = sd.W - ld.l_position; - float dist = length(cubevec) - scd.sh_cube_bias; + /* Specular probes */ + vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); + vec3 C_spec_dir = get_specular_reflection_dominant_dir(C_N, V, C_roughnessSquared); - float z = texture_octahedron(shadowCubes, vec4(cubevec, shid)).r; + /* Starts at 1 because 0 is world probe */ + for (int i = 1; i < MAX_PROBE && i < probe_count && spec_accum.a < 0.999; ++i) { + CubeData cd = probes_data[i]; - float esm_test = saturate(exp(scd.sh_cube_exp * (z - dist))); - float sh_test = step(0, z - dist); + float fade = probe_attenuation_cube(cd, worldPosition); - vis *= esm_test; + if (fade > 0.0) { + if (!(ssrToggle && ssr_id == outputSsrId)) { + vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness); + accumulate_light(spec, fade, spec_accum); + } + + vec3 C_spec = probe_evaluate_cube(float(i), cd, worldPosition, C_spec_dir, C_roughness); + accumulate_light(C_spec, fade, C_spec_accum); + } } -} -vec3 probe_parallax_correction(vec3 W, vec3 spec_dir, ProbeData pd, inout float roughness) -{ - vec3 localpos = (pd.parallaxmat * vec4(W, 1.0)).xyz; - vec3 localray = (pd.parallaxmat * vec4(spec_dir, 0.0)).xyz; + /* World Specular */ + if (spec_accum.a < 0.999) { + if (!(ssrToggle && ssr_id == outputSsrId)) { + vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); + accumulate_light(spec, 1.0, spec_accum); + } - float dist; - if (pd.p_parallax_type == PROBE_PARALLAX_BOX) { - dist = line_unit_box_intersect_dist(localpos, localray); + vec3 C_spec = probe_evaluate_world_spec(C_spec_dir, C_roughness); + accumulate_light(C_spec, 1.0, C_spec_accum); } - else { - dist = line_unit_sphere_intersect_dist(localpos, localray); + + vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0)); + + /* Ambient Occlusion */ + vec3 bent_normal; + float final_ao = occlusion_compute(N, viewPosition, ao, rand.rg, bent_normal); + + /* Get Brdf intensity */ + vec2 uv = lut_coords(dot(N, V), roughness); + vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; + + ssr_spec = F_ibl(f0, brdf_lut); + if (!(ssrToggle && ssr_id == outputSsrId)) { + ssr_spec *= specular_occlusion(dot(N, V), final_ao, roughness); } + out_light += spec_accum.rgb * ssr_spec * float(specToggle); - /* Use Distance in WS directly to recover intersection */ - vec3 intersection = W + spec_dir * dist - pd.p_position; + uv = lut_coords(dot(C_N, V), C_roughness); + brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; - /* From Frostbite PBR Course - * Distance based roughness - * http://www.frostbite.com/wp-content/uploads/2014/11/course_notes_moving_frostbite_to_pbr.pdf */ - float original_roughness = roughness; - float linear_roughness = sqrt(roughness); - float distance_roughness = saturate(dist * linear_roughness / length(intersection)); - linear_roughness = mix(distance_roughness, linear_roughness, linear_roughness); - roughness = linear_roughness * linear_roughness; + out_light += C_spec_accum.rgb * F_ibl(vec3(0.04), brdf_lut) * specular_occlusion(dot(C_N, V), final_ao, C_roughness) * float(specToggle) * C_intensity; - float fac = saturate(original_roughness * 2.0 - 1.0); - return mix(intersection, spec_dir, fac * fac); -} + /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ----------------- */ -float probe_attenuation(vec3 W, ProbeData pd) -{ - vec3 localpos = (pd.influencemat * vec4(W, 1.0)).xyz; + /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ + vec4 diff_accum = vec4(0.0); + + /* Start at 1 because 0 is world irradiance */ + for (int i = 1; i < MAX_GRID && i < grid_count && diff_accum.a < 0.999; ++i) { + GridData gd = grids_data[i]; - float fac; - if (pd.p_atten_type == PROBE_ATTENUATION_BOX) { - vec3 axes_fac = saturate(pd.p_atten_fac - pd.p_atten_fac * abs(localpos)); - fac = min_v3(axes_fac); + vec3 localpos; + float fade = probe_attenuation_grid(gd, worldPosition, localpos); + + if (fade > 0.0) { + vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); + accumulate_light(diff, fade, diff_accum); + } } - else { - fac = saturate(pd.p_atten_fac - pd.p_atten_fac * length(localpos)); + + /* World Diffuse */ + if (diff_accum.a < 0.999 && grid_count > 0) { + vec3 diff = probe_evaluate_world_diff(bent_normal); + accumulate_light(diff, 1.0, diff_accum); } - return fac; + out_light += diff_accum.rgb * albedo * gtao_multibounce(final_ao, albedo); + + return out_light; } -float planar_attenuation(vec3 W, vec3 N, PlanarData pd) +/* ----------- Diffuse ----------- */ + +vec3 eevee_surface_diffuse_lit(vec3 N, vec3 albedo, float ao) { - float fac; + vec3 V = cameraVec; - /* Normal Facing */ - fac = saturate(dot(pd.pl_normal, N) * pd.pl_facing_scale + pd.pl_facing_bias); + /* Zero length vectors cause issues, see: T51979. */ +#if 0 + N = normalize(N); +#else + { + float len = length(N); + if (isnan(len)) { + return vec3(0.0); + } + N /= len; + } +#endif - /* Distance from plane */ - fac *= saturate(abs(dot(pd.pl_plane_eq, vec4(W, 1.0))) * pd.pl_fade_scale + pd.pl_fade_bias); + /* ---------------- SCENE LAMPS LIGHTING ----------------- */ - /* Fancy fast clipping calculation */ - vec2 dist_to_clip; - dist_to_clip.x = dot(pd.pl_clip_pos_x, W); - dist_to_clip.y = dot(pd.pl_clip_pos_y, W); - fac *= step(2.0, dot(step(pd.pl_clip_edges, dist_to_clip.xxyy), vec2(-1.0, 1.0).xyxy)); /* compare and add all tests */ +#ifdef HAIR_SHADER + vec3 norm_view = cross(V, N); + norm_view = normalize(cross(norm_view, N)); /* Normal facing view */ +#endif - return fac; -} + vec3 diff = vec3(0.0); + for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) { + LightData ld = lights_data[i]; -float compute_occlusion(vec3 N, float micro_occlusion, vec2 randuv, out vec3 bent_normal) -{ -#ifdef USE_AO /* Screen Space Occlusion */ + vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ + l_vector.xyz = ld.l_position - worldPosition; + l_vector.w = length(l_vector.xyz); + + vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, viewPosition, viewNormal, l_vector); - float macro_occlusion; - vec3 vnor = mat3(ViewMatrix) * N; +#ifdef HAIR_SHADER + vec3 norm_lamp, view_vec; + float occlu_trans, occlu; + light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec); -#ifdef USE_BENT_NORMAL - gtao(vnor, viewPosition, randuv, macro_occlusion, bent_normal); - bent_normal = mat3(ViewMatrixInverse) * bent_normal; + diff += l_color_vis * light_diffuse(ld, -norm_lamp, V, l_vector) * occlu_trans; #else - gtao(vnor, viewPosition, randuv, macro_occlusion); - bent_normal = N; + diff += l_color_vis * light_diffuse(ld, N, V, l_vector); #endif - return min(macro_occlusion, micro_occlusion); - -#else /* No added Occlusion. */ + } - bent_normal = N; - return micro_occlusion; + /* Accumulate outgoing radiance */ + vec3 out_light = diff * albedo; +#ifdef HAIR_SHADER + N = -norm_view; #endif + + /* ---------------- DIFFUSE ENVIRONMENT LIGHTING ----------------- */ + + vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0)); + + /* Ambient Occlusion */ + vec3 bent_normal; + float final_ao = occlusion_compute(N, viewPosition, ao, rand.rg, bent_normal); + + /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ + vec4 diff_accum = vec4(0.0); + + /* Start at 1 because 0 is world irradiance */ + for (int i = 1; i < MAX_GRID && i < grid_count && diff_accum.a < 0.999; ++i) { + GridData gd = grids_data[i]; + + vec3 localpos; + float fade = probe_attenuation_grid(gd, worldPosition, localpos); + + if (fade > 0.0) { + vec3 diff = probe_evaluate_grid(gd, worldPosition, bent_normal, localpos); + accumulate_light(diff, fade, diff_accum); + } + } + + /* World Diffuse */ + if (diff_accum.a < 0.999 && grid_count > 0) { + vec3 diff = probe_evaluate_world_diff(bent_normal); + accumulate_light(diff, 1.0, diff_accum); + } + + out_light += diff_accum.rgb * albedo * gtao_multibounce(final_ao, albedo); + + return out_light; } -vec3 eevee_surface_lit(vec3 world_normal, vec3 albedo, vec3 f0, float roughness, float ao) +/* ----------- Glossy ----------- */ + +vec3 eevee_surface_glossy_lit(vec3 N, vec3 f0, float roughness, float ao, int ssr_id, out vec3 ssr_spec) { roughness = clamp(roughness, 1e-8, 0.9999); float roughnessSquared = roughness * roughness; - ShadingData sd; - sd.N = normalize(world_normal); - sd.V = (ProjectionMatrix[3][3] == 0.0) /* if perspective */ - ? normalize(cameraPos - worldPosition) - : cameraForward; - sd.W = worldPosition; + vec3 V = cameraVec; - vec3 radiance = vec3(0.0); + /* Zero length vectors cause issues, see: T51979. */ +#if 0 + N = normalize(N); +#else + { + float len = length(N); + if (isnan(len)) { + return vec3(0.0); + } + N /= len; + } +#endif + + /* ---------------- SCENE LAMPS LIGHTING ----------------- */ #ifdef HAIR_SHADER - /* View facing normal */ - vec3 norm_view = cross(sd.V, sd.N); - norm_view = normalize(cross(norm_view, sd.N)); /* Normal facing view */ + vec3 norm_view = cross(V, N); + norm_view = normalize(cross(norm_view, N)); /* Normal facing view */ #endif - - /* Analytic Lights */ + vec3 spec = vec3(0.0); for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) { LightData ld = lights_data[i]; - vec3 diff, spec; - float vis = 1.0; - sd.l_vector = ld.l_position - worldPosition; + vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ + l_vector.xyz = ld.l_position - worldPosition; + l_vector.w = length(l_vector.xyz); -#ifndef HAIR_SHADER - light_visibility(ld, sd, vis); -#endif - light_shade(ld, sd, albedo, roughnessSquared, f0, diff, spec); + vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, viewPosition, viewNormal, l_vector); - radiance += vis * (diff + spec) * ld.l_color; +#ifdef HAIR_SHADER + vec3 norm_lamp, view_vec; + float occlu_trans, occlu; + light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec); + + spec += l_color_vis * light_specular(ld, N, view_vec, l_vector, roughnessSquared, f0) * occlu; +#else + spec += l_color_vis * light_specular(ld, N, V, l_vector, roughnessSquared, f0); +#endif } + /* Accumulate outgoing radiance */ + vec3 out_light = spec * float(specToggle); + #ifdef HAIR_SHADER - sd.N = -norm_view; + N = -norm_view; #endif - vec3 bent_normal; - vec4 rand = textureLod(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0), 0.0).rgba; - float final_ao = compute_occlusion(sd.N, ao, rand.rg, bent_normal); - - /* Envmaps */ - vec3 R = reflect(-sd.V, sd.N); - vec3 spec_dir = get_specular_dominant_dir(sd.N, R, roughnessSquared); - vec2 uv = lut_coords(dot(sd.N, sd.V), roughness); - vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; + /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ + /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ vec4 spec_accum = vec4(0.0); - vec4 diff_accum = vec4(0.0); - /* Planar Reflections */ - for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999; ++i) { - PlanarData pd = planars_data[i]; + if (!(ssrToggle && ssr_id == outputSsrId)) { + /* Planar Reflections */ + for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999; ++i) { + PlanarData pd = planars_data[i]; + + float fade = probe_attenuation_planar(pd, worldPosition, N, roughness); + + if (fade > 0.0) { + vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); + accumulate_light(spec, fade, spec_accum); + } + } + + /* Specular probes */ + vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); - float influence = planar_attenuation(sd.W, sd.N, pd); + /* Starts at 1 because 0 is world probe */ + for (int i = 1; i < MAX_PROBE && i < probe_count && spec_accum.a < 0.999; ++i) { + CubeData cd = probes_data[i]; - if (influence > 0.0) { - float influ_spec = min(influence, (1.0 - spec_accum.a)); + float fade = probe_attenuation_cube(cd, worldPosition); - /* Sample reflection depth. */ - vec4 refco = pd.reflectionmat * vec4(sd.W, 1.0); - refco.xy /= refco.w; - float ref_depth = textureLod(probePlanars, vec3(refco.xy, i), 0.0).a; + if (fade > 0.0) { + vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness); + accumulate_light(spec, fade, spec_accum); + } + } + + /* World Specular */ + if (spec_accum.a < 0.999) { + vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); + accumulate_light(spec, 1.0, spec_accum); + } + } - /* Find view vector / reflection plane intersection. (dist_to_plane is negative) */ - float dist_to_plane = line_plane_intersect_dist(cameraPos, sd.V, pd.pl_plane_eq); - vec3 point_on_plane = cameraPos + sd.V * dist_to_plane; + vec4 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0)); - /* How far the pixel is from the plane. */ - ref_depth = ref_depth + dist_to_plane; + /* Get Brdf intensity */ + vec2 uv = lut_coords(dot(N, V), roughness); + vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; - /* Compute distorded reflection vector based on the distance to the reflected object. - * In other words find intersection between reflection vector and the sphere center - * around point_on_plane. */ - vec3 proj_ref = reflect(R * ref_depth, pd.pl_normal); + ssr_spec = F_ibl(f0, brdf_lut); + if (!(ssrToggle && ssr_id == outputSsrId)) { + /* Ambient Occlusion */ + vec3 bent_normal; + float final_ao = occlusion_compute(N, viewPosition, ao, rand.rg, bent_normal); - /* Final point in world space. */ - vec3 ref_pos = point_on_plane + proj_ref; + ssr_spec *= specular_occlusion(dot(N, V), final_ao, roughness); + } + out_light += spec_accum.rgb * ssr_spec * float(specToggle); - /* Reproject to find texture coords. */ - refco = pd.reflectionmat * vec4(ref_pos, 1.0); - refco.xy /= refco.w; + return out_light; +} - vec3 sample = textureLod(probePlanars, vec3(refco.xy, i), 0.0).rgb; +/* ----------- Transmission ----------- */ - spec_accum.rgb += sample * influ_spec; - spec_accum.a += influ_spec; +vec3 eevee_surface_refraction(vec3 N, vec3 f0, float roughness, float ior) +{ + /* Zero length vectors cause issues, see: T51979. */ +#if 0 + N = normalize(N); +#else + { + float len = length(N); + if (isnan(len)) { + return vec3(0.0); } + N /= len; } +#endif + vec3 V = cameraVec; + ior = (gl_FrontFacing) ? ior : 1.0 / ior; + + roughness = clamp(roughness, 1e-8, 0.9999); + float roughnessSquared = roughness * roughness; + + /* ---------------- SCENE LAMPS LIGHTING ----------------- */ + + /* No support for now. Supporting LTCs mean having a 3D LUT. + * We could support point lights easily though. */ + + /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ + + /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ + vec4 trans_accum = vec4(0.0); + + /* Refract the view vector using the depth heuristic. + * Then later Refract a second time the already refracted + * ray using the inverse ior. */ + float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; + vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; + vec3 refr_pos = (refractionDepth > 0.0) ? line_plane_intersect(worldPosition, refr_V, worldPosition - N * refractionDepth, N) : worldPosition; + +#ifdef USE_REFRACTION + /* Screen Space Refraction */ + if (ssrToggle && roughness < maxRoughness + 0.2) { + vec3 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0)).xzw; + + /* Find approximated position of the 2nd refraction event. */ + vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : viewPosition; + + float ray_ofs = 1.0 / float(rayCount); + vec4 spec = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand, 0.0); + if (rayCount > 1) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xyz * vec3(1.0, -1.0, -1.0), 1.0 * ray_ofs); + if (rayCount > 2) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xzy * vec3(1.0, 1.0, -1.0), 2.0 * ray_ofs); + if (rayCount > 3) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xzy * vec3(1.0, -1.0, 1.0), 3.0 * ray_ofs); + spec /= float(rayCount); + spec.a *= smoothstep(maxRoughness + 0.2, maxRoughness, roughness); + accumulate_light(spec.rgb, spec.a, trans_accum); + } +#endif /* Specular probes */ - /* Start at 1 because 0 is world probe */ - for (int i = 1; i < MAX_PROBE && i < probe_count && spec_accum.a < 0.999; ++i) { - ProbeData pd = probes_data[i]; + /* NOTE: This bias the IOR */ + vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); - float dist_attenuation = probe_attenuation(sd.W, pd); + /* Starts at 1 because 0 is world probe */ + for (int i = 1; i < MAX_PROBE && i < probe_count && trans_accum.a < 0.999; ++i) { + CubeData cd = probes_data[i]; - if (dist_attenuation > 0.0) { - float roughness_copy = roughness; + float fade = probe_attenuation_cube(cd, worldPosition); - vec3 sample_vec = probe_parallax_correction(sd.W, spec_dir, pd, roughness_copy); - vec4 sample = textureLod_octahedron(probeCubes, vec4(sample_vec, i), roughness_copy * lodMax, lodMax).rgba; + if (fade > 0.0) { + vec3 spec = probe_evaluate_cube(float(i), cd, refr_pos, refr_dir, roughnessSquared); + accumulate_light(spec, fade, trans_accum); + } + } + + /* World Specular */ + if (trans_accum.a < 0.999) { + vec3 spec = probe_evaluate_world_spec(refr_dir, roughnessSquared); + accumulate_light(spec, 1.0, trans_accum); + } + + float btdf = get_btdf_lut(utilTex, dot(N, V), roughness, ior); - float influ_spec = min(dist_attenuation, (1.0 - spec_accum.a)); + return trans_accum.rgb * btdf; +} - spec_accum.rgb += sample.rgb * influ_spec; - spec_accum.a += influ_spec; +vec3 eevee_surface_glass(vec3 N, vec3 transmission_col, float roughness, float ior, int ssr_id, out vec3 ssr_spec) +{ + /* Zero length vectors cause issues, see: T51979. */ +#if 0 + N = normalize(N); +#else + { + float len = length(N); + if (isnan(len)) { + return vec3(0.0); } + N /= len; } +#endif + vec3 V = cameraVec; + ior = (gl_FrontFacing) ? ior : 1.0 / ior; - /* Start at 1 because 0 is world irradiance */ - for (int i = 1; i < MAX_GRID && i < grid_count && diff_accum.a < 0.999; ++i) { - GridData gd = grids_data[i]; + if (!specToggle) return vec3(0.0); - vec3 localpos = (gd.localmat * vec4(sd.W, 1.0)).xyz; + roughness = clamp(roughness, 1e-8, 0.9999); + float roughnessSquared = roughness * roughness; - float fade = min(1.0, min_v3(1.0 - abs(localpos))); - fade = saturate(fade * gd.g_atten_scale + gd.g_atten_bias); + /* ---------------- SCENE LAMPS LIGHTING ----------------- */ - if (fade > 0.0) { - localpos = localpos * 0.5 + 0.5; - localpos = localpos * vec3(gd.g_resolution) - 0.5; +#ifdef HAIR_SHADER + vec3 norm_view = cross(V, N); + norm_view = normalize(cross(norm_view, N)); /* Normal facing view */ +#endif - vec3 localpos_floored = floor(localpos); - vec3 trilinear_weight = fract(localpos); + vec3 spec = vec3(0.0); + for (int i = 0; i < MAX_LIGHT && i < light_count; ++i) { + LightData ld = lights_data[i]; - float weight_accum = 0.0; - vec3 irradiance_accum = vec3(0.0); + vec4 l_vector; /* Non-Normalized Light Vector with length in last component. */ + l_vector.xyz = ld.l_position - worldPosition; + l_vector.w = length(l_vector.xyz); - /* For each neighboor cells */ - for (int i = 0; i < 8; ++i) { - ivec3 offset = ivec3(i, i >> 1, i >> 2) & ivec3(1); - vec3 cell_cos = clamp(localpos_floored + vec3(offset), vec3(0.0), vec3(gd.g_resolution) - 1.0); + vec3 l_color_vis = ld.l_color * light_visibility(ld, worldPosition, viewPosition, viewNormal, l_vector); - /* We need this because we render probes in world space (so we need light vector in WS). - * And rendering them in local probe space is too much problem. */ - vec3 ws_cell_location = gd.g_corner + - (gd.g_increment_x * cell_cos.x + - gd.g_increment_y * cell_cos.y + - gd.g_increment_z * cell_cos.z); - vec3 ws_point_to_cell = ws_cell_location - sd.W; - vec3 ws_light = normalize(ws_point_to_cell); +#ifdef HAIR_SHADER + vec3 norm_lamp, view_vec; + float occlu_trans, occlu; + light_hair_common(ld, N, V, l_vector, norm_view, occlu_trans, occlu, norm_lamp, view_vec); - vec3 trilinear = mix(1 - trilinear_weight, trilinear_weight, offset); - float weight = trilinear.x * trilinear.y * trilinear.z; + spec += l_color_vis * light_specular(ld, N, view_vec, l_vector, roughnessSquared, vec3(1.0)) * occlu; +#else + spec += l_color_vis * light_specular(ld, N, V, l_vector, roughnessSquared, vec3(1.0)); +#endif + } - /* Smooth backface test */ - // weight *= sqrt(max(0.002, dot(ws_light, sd.N))); + /* Accumulate outgoing radiance */ + vec3 out_light = spec; - /* Avoid zero weight */ - weight = max(0.00001, weight); +#ifdef HAIR_SHADER + N = -norm_view; +#endif + + + /* ---------------- SPECULAR ENVIRONMENT LIGHTING ----------------- */ + + /* Accumulate light from all sources until accumulator is full. Then apply Occlusion and BRDF. */ + vec4 spec_accum = vec4(0.0); + + /* Planar Reflections */ + if (!(ssrToggle && ssr_id == outputSsrId)) { + for (int i = 0; i < MAX_PLANAR && i < planar_count && spec_accum.a < 0.999 && roughness < 0.1; ++i) { + PlanarData pd = planars_data[i]; - vec3 color = get_cell_color(ivec3(cell_cos), gd.g_resolution, gd.g_offset, bent_normal); + float fade = probe_attenuation_planar(pd, worldPosition, N, roughness); - weight_accum += weight; - irradiance_accum += color * weight; + if (fade > 0.0) { + vec3 spec = probe_evaluate_planar(float(i), pd, worldPosition, N, V, roughness, fade); + accumulate_light(spec, fade, spec_accum); } + } + } + + /* Refract the view vector using the depth heuristic. + * Then later Refract a second time the already refracted + * ray using the inverse ior. */ + float final_ior = (refractionDepth > 0.0) ? 1.0 / ior : ior; + vec3 refr_V = (refractionDepth > 0.0) ? -refract(-V, N, final_ior) : V; + vec3 refr_pos = (refractionDepth > 0.0) ? line_plane_intersect(worldPosition, refr_V, worldPosition - N * refractionDepth, N) : worldPosition; + + vec4 trans_accum = vec4(0.0); + +#ifdef USE_REFRACTION + /* Screen Space Refraction */ + if (ssrToggle && roughness < maxRoughness + 0.2) { + vec3 rand = texture(utilTex, vec3(gl_FragCoord.xy / LUT_SIZE, 2.0)).xzw; + + /* Find approximated position of the 2nd refraction event. */ + vec3 refr_vpos = (refractionDepth > 0.0) ? transform_point(ViewMatrix, refr_pos) : viewPosition; + + float ray_ofs = 1.0 / float(rayCount); + vec4 spec = screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand, 0.0); + if (rayCount > 1) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xyz * vec3(1.0, -1.0, -1.0), 1.0 * ray_ofs); + if (rayCount > 2) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xzy * vec3(1.0, 1.0, -1.0), 2.0 * ray_ofs); + if (rayCount > 3) spec += screen_space_refraction(refr_vpos, N, refr_V, final_ior, roughnessSquared, rand.xzy * vec3(1.0, -1.0, 1.0), 3.0 * ray_ofs); + spec /= float(rayCount); + spec.a *= smoothstep(maxRoughness + 0.2, maxRoughness, roughness); + accumulate_light(spec.rgb, spec.a, trans_accum); + } +#endif - vec3 indirect_diffuse = irradiance_accum / weight_accum; + /* Specular probes */ + vec3 refr_dir = get_specular_refraction_dominant_dir(N, refr_V, roughness, final_ior); + vec3 spec_dir = get_specular_reflection_dominant_dir(N, V, roughnessSquared); + + /* Starts at 1 because 0 is world probe */ + for (int i = 1; i < MAX_PROBE && i < probe_count && (spec_accum.a < 0.999 || trans_accum.a < 0.999); ++i) { + CubeData cd = probes_data[i]; - float influ_diff = min(fade, (1.0 - diff_accum.a)); + float fade = probe_attenuation_cube(cd, worldPosition); - diff_accum.rgb += indirect_diffuse * influ_diff; - diff_accum.a += influ_diff; + if (fade > 0.0) { + if (!(ssrToggle && ssr_id == outputSsrId)) { + vec3 spec = probe_evaluate_cube(float(i), cd, worldPosition, spec_dir, roughness); + accumulate_light(spec, fade, spec_accum); + } - /* For Debug purpose */ - // return texture(irradianceGrid, sd.W.xy).rgb; + spec = probe_evaluate_cube(float(i), cd, refr_pos, refr_dir, roughnessSquared); + accumulate_light(spec, fade, trans_accum); } } - /* World probe */ - if (diff_accum.a < 1.0 && grid_count > 0) { - IrradianceData ir_data = load_irradiance_cell(0, bent_normal); + /* World Specular */ + if (spec_accum.a < 0.999) { + if (!(ssrToggle && ssr_id == outputSsrId)) { + vec3 spec = probe_evaluate_world_spec(spec_dir, roughness); + accumulate_light(spec, 1.0, spec_accum); + } + } - vec3 diff = compute_irradiance(bent_normal, ir_data); - diff_accum.rgb += diff * (1.0 - diff_accum.a); + if (trans_accum.a < 0.999) { + spec = probe_evaluate_world_spec(refr_dir, roughnessSquared); + accumulate_light(spec, 1.0, trans_accum); } - if (spec_accum.a < 1.0) { - ProbeData pd = probes_data[0]; + /* Ambient Occlusion */ + /* TODO : when AO will be cheaper */ + float final_ao = 1.0; + + float NV = dot(N, V); + /* Get Brdf intensity */ + vec2 uv = lut_coords(NV, roughness); + vec2 brdf_lut = texture(utilTex, vec3(uv, 1.0)).rg; + + float fresnel = F_eta(ior, NV); + + /* Apply fresnel on lamps. */ + out_light *= vec3(fresnel); - vec3 spec = textureLod_octahedron(probeCubes, vec4(spec_dir, 0), roughness * lodMax, lodMax).rgb; - spec_accum.rgb += spec * (1.0 - spec_accum.a); + ssr_spec = vec3(fresnel) * F_ibl(vec3(1.0), brdf_lut); + if (!(ssrToggle && ssr_id == outputSsrId)) { + ssr_spec *= specular_occlusion(dot(N, V), final_ao, roughness); } + out_light += spec_accum.rgb * ssr_spec; + + + float btdf = get_btdf_lut(utilTex, NV, roughness, ior); - vec3 indirect_radiance = - spec_accum.rgb * F_ibl(f0, brdf_lut) * float(specToggle) * specular_occlusion(dot(sd.N, sd.V), final_ao, roughness) + - diff_accum.rgb * albedo * gtao_multibounce(final_ao, albedo); + out_light += vec3(1.0 - fresnel) * transmission_col * trans_accum.rgb * btdf; - return radiance + indirect_radiance; + return out_light; } |