From 0679abdc8a95f96ed6743e4eb9aa083e657699fe Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 13:31:43 +1300 Subject: [PATCH 01/36] Remove many input clamps --- .../osl/shaders/node_principled_bsdf.osl | 30 +++++++++---------- intern/cycles/kernel/svm/closure.h | 28 ++++++++--------- .../gpu_shader_material_principled.glsl | 16 ---------- 3 files changed, 27 insertions(+), 47 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 816592513bd..0e51bdaca3b 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -36,9 +36,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", normal Tangent = normalize(dPdu), output closure color BSDF = 0) { - color specular_tint = max(SpecularTint, color(0.0)); - - float r2 = clamp(Roughness, 0.0, 1.0); + float r2 = Roughness; r2 = r2 * r2; float alpha_x = r2, alpha_y = r2; @@ -57,7 +55,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float eta = IOR; float f0 = F0_from_ior(eta); if (SpecularIORLevel != 0.5) { - f0 *= 2.0 * max(SpecularIORLevel, 0.0); + f0 *= 2.0 * SpecularIORLevel; eta = ior_from_F0(f0); if (IOR < 1.0) { eta = 1.0 / eta; @@ -66,7 +64,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", BSDF = BaseColor * diffuse(Normal); if (SubsurfaceWeight > 1e-5) { - float subsurface_weight = min(SubsurfaceWeight, 1.0); + float subsurface_weight = SubsurfaceWeight; vector radius = SubsurfaceScale * SubsurfaceRadius; float subsurface_ior = (subsurface_method == "random_walk_skin") ? SubsurfaceIOR : eta; closure color SubsurfBSDF = bssrdf(subsurface_method, @@ -84,7 +82,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", if (eta != 1.0) { /* Apply specular tint */ - color F0 = f0 * specular_tint; + color F0 = f0 * SpecularTint; color F90 = color(1.0); BSDF = layer( @@ -99,20 +97,20 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float eta = max(IOR, 1e-5); eta = backfacing() ? 1.0 / eta : eta; - color F0 = F0_from_ior(eta) * specular_tint; + color F0 = F0_from_ior(eta) * SpecularTint; color F90 = color(1.0); TransmissionBSDF = generalized_schlick_bsdf( Normal, vector(0.0), color(1.0), sqrt(BaseColor), r2, r2, F0, F90, -eta, distribution), - BSDF = mix(BSDF, TransmissionBSDF, clamp(TransmissionWeight, 0.0, 1.0)); + BSDF = mix(BSDF, TransmissionBSDF, TransmissionWeight); } closure color MetallicBSDF = 0; if (Metallic > 0.0) { color F0 = BaseColor; - color F82 = specular_tint; + color F82 = SpecularTint; MetallicBSDF = microfacet_f82_tint(distribution, Normal, T, alpha_x, alpha_y, F0, F82); - BSDF = mix(BSDF, MetallicBSDF, clamp(Metallic, 0.0, 1.0)); + BSDF = mix(BSDF, MetallicBSDF, Metallic); } if (EmissionStrength > 0.0 && EmissionColor != color(0.0)) { @@ -127,19 +125,19 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float cosNT = sqrt(1.0 - coat_neta * coat_neta * (1 - cosNI * cosNI)); BSDF *= pow(CoatTint, CoatWeight / cosNT); } - float coat_r2 = clamp(CoatRoughness, 0.0, 1.0); + float coat_r2 = CoatRoughness; coat_r2 = coat_r2 * coat_r2; closure color CoatBSDF = dielectric_bsdf( CoatNormal, vector(0.0), color(1.0), color(0.0), coat_r2, coat_r2, coat_ior, "ggx"); - BSDF = layer(clamp(CoatWeight, 0.0, 1.0) * CoatBSDF, BSDF); + BSDF = layer(CoatWeight * CoatBSDF, BSDF); } if (SheenWeight > 1e-5) { - normal sheen_normal = normalize(mix(Normal, CoatNormal, clamp(CoatWeight, 0.0, 1.0))); - closure color SheenBSDF = sheen(sheen_normal, clamp(SheenRoughness, 0.0, 1.0)); - BSDF = layer(clamp(SheenWeight, 0.0, 1.0) * SheenTint * SheenBSDF, BSDF); + normal sheen_normal = normalize(mix(Normal, CoatNormal, CoatWeight)); + closure color SheenBSDF = sheen(sheen_normal, SheenRoughness); + BSDF = layer(SheenWeight * SheenTint * SheenBSDF, BSDF); } - BSDF = mix(transparent(), BSDF, clamp(Alpha, 0.0, 1.0)); + BSDF = mix(transparent(), BSDF, Alpha); } diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 9569587f9b6..1472cdf3ee3 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -107,21 +107,20 @@ ccl_device &coat_tint_offset); // get Disney principled parameters - float metallic = saturatef(param1); - float subsurface_weight = saturatef(param2); - float specular_ior_level = fmaxf(stack_load_float(stack, specular_ior_level_offset), 0.0f); - float roughness = saturatef(stack_load_float(stack, roughness_offset)); - Spectrum specular_tint = rgb_to_spectrum( - max(stack_load_float3(stack, specular_tint_offset), zero_float3())); - float anisotropic = saturatef(stack_load_float(stack, anisotropic_offset)); - float sheen_weight = saturatef(stack_load_float(stack, sheen_weight_offset)); + float metallic = param1; + float subsurface_weight = param2; + float specular_ior_level = stack_load_float(stack, specular_ior_level_offset); + float roughness = stack_load_float(stack, roughness_offset); + Spectrum specular_tint = rgb_to_spectrum(stack_load_float3(stack, specular_tint_offset), zero_float3()); + float anisotropic = stack_load_float(stack, anisotropic_offset); + float sheen_weight = stack_load_float(stack, sheen_weight_offset); float3 sheen_tint = stack_load_float3(stack, sheen_tint_offset); - float sheen_roughness = saturatef(stack_load_float(stack, sheen_roughness_offset)); - float coat_weight = saturatef(stack_load_float(stack, coat_weight_offset)); - float coat_roughness = saturatef(stack_load_float(stack, coat_roughness_offset)); + float sheen_roughness = stack_load_float(stack, sheen_roughness_offset); + float coat_weight = stack_load_float(stack, coat_weight_offset); + float coat_roughness = stack_load_float(stack, coat_roughness_offset); float coat_ior = fmaxf(stack_load_float(stack, coat_ior_offset), 1.0f); float3 coat_tint = stack_load_float3(stack, coat_tint_offset); - float transmission_weight = saturatef(stack_load_float(stack, transmission_weight_offset)); + float transmission_weight = stack_load_float(stack, transmission_weight_offset); float anisotropic_rotation = stack_load_float(stack, anisotropic_rotation_offset); float ior = fmaxf(stack_load_float(stack, eta_offset), 1e-5f); @@ -152,12 +151,11 @@ ccl_device &dummy); float alpha = stack_valid(alpha_offset) ? stack_load_float(stack, alpha_offset) : __uint_as_float(data_alpha_emission.y); - alpha = saturatef(alpha); float emission_strength = stack_valid(emission_strength_offset) ? stack_load_float(stack, emission_strength_offset) : __uint_as_float(data_alpha_emission.z); - float3 emission = stack_load_float3(stack, emission_offset) * fmaxf(emission_strength, 0.0f); + float3 emission = stack_load_float3(stack, emission_offset) * emission_strength; Spectrum weight = closure_weight * mix_weight; @@ -193,7 +191,7 @@ ccl_device sd, sizeof(SheenBsdf), sheen_weight * rgb_to_spectrum(sheen_tint) * weight); if (bsdf) { - bsdf->N = safe_normalize(mix(N, coat_normal, saturatef(coat_weight))); + bsdf->N = safe_normalize(mix(N, coat_normal, coat_weight)); bsdf->roughness = sheen_roughness; /* setup bsdf */ diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index c99356e920c..3c4eb4dae28 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -60,22 +60,6 @@ void node_bsdf_principled(vec4 base_color, out Closure result) { /* Match cycles. */ - metallic = clamp(metallic, 0.0, 1.0); - roughness = clamp(roughness, 0.0, 1.0); - ior = max(ior, 1e-5); - transmission_weight = clamp(transmission_weight, 0.0, 1.0); - subsurface_weight = clamp(subsurface_weight, 0.0, 1.0); - specular_ior_level = max(specular_ior_level, 0.0); - specular_tint = max(specular_tint, vec4(0.0)); - /* Not used by EEVEE */ - /* anisotropic = clamp(anisotropic, 0.0, 1.0) */ - coat_weight = clamp(coat_weight, 0.0, 1.0); - coat_roughness = clamp(coat_roughness, 0.0, 1.0); - coat_ior = max(coat_ior, 1.0); - sheen_weight = clamp(sheen_weight, 0.0, 1.0); - sheen_roughness = clamp(sheen_roughness, 0.0, 1.0); - emission_strength = max(emission_strength, 0.0); - alpha = clamp(alpha, 0.0, 1.0); N = safe_normalize(N); CN = safe_normalize(CN); -- 2.30.2 From 3519786abec824f7c1c84145c90139d04e7ce0b1 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 13:39:28 +1300 Subject: [PATCH 02/36] Fix build issues --- intern/cycles/kernel/svm/closure.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 1472cdf3ee3..83bd9128a2b 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -111,7 +111,7 @@ ccl_device float subsurface_weight = param2; float specular_ior_level = stack_load_float(stack, specular_ior_level_offset); float roughness = stack_load_float(stack, roughness_offset); - Spectrum specular_tint = rgb_to_spectrum(stack_load_float3(stack, specular_tint_offset), zero_float3()); + Spectrum specular_tint = rgb_to_spectrum(stack_load_float3(stack, specular_tint_offset)); float anisotropic = stack_load_float(stack, anisotropic_offset); float sheen_weight = stack_load_float(stack, sheen_weight_offset); float3 sheen_tint = stack_load_float3(stack, sheen_tint_offset); -- 2.30.2 From 2dba935f423f0ee784cae19874e42e157f935a5c Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 13:59:18 +1300 Subject: [PATCH 03/36] Clamp base color to 0..infinity --- .../kernel/osl/shaders/node_principled_bsdf.osl | 15 ++++++++++----- intern/cycles/kernel/svm/closure.h | 1 + .../material/gpu_shader_material_principled.glsl | 4 +++- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 0e51bdaca3b..3c31c326a4e 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -36,6 +36,11 @@ shader node_principled_bsdf(string distribution = "multi_ggx", normal Tangent = normalize(dPdu), output closure color BSDF = 0) { + /* Clamping to match SVM */ + color base_color = max(BaseColor, color(0.0)); + + + float r2 = Roughness; r2 = r2 * r2; @@ -62,7 +67,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", } } - BSDF = BaseColor * diffuse(Normal); + BSDF = base_color * diffuse(Normal); if (SubsurfaceWeight > 1e-5) { float subsurface_weight = SubsurfaceWeight; vector radius = SubsurfaceScale * SubsurfaceRadius; @@ -70,14 +75,14 @@ shader node_principled_bsdf(string distribution = "multi_ggx", closure color SubsurfBSDF = bssrdf(subsurface_method, Normal, SubsurfaceScale * SubsurfaceRadius, - BaseColor, + base_color, "roughness", r2, "ior", subsurface_ior, "anisotropy", SubsurfaceAnisotropy); - BSDF = mix(BSDF, BaseColor * SubsurfBSDF, subsurface_weight); + BSDF = mix(BSDF, base_color * SubsurfBSDF, subsurface_weight); } if (eta != 1.0) { @@ -101,13 +106,13 @@ shader node_principled_bsdf(string distribution = "multi_ggx", color F90 = color(1.0); TransmissionBSDF = generalized_schlick_bsdf( - Normal, vector(0.0), color(1.0), sqrt(BaseColor), r2, r2, F0, F90, -eta, distribution), + Normal, vector(0.0), color(1.0), sqrt(base_color), r2, r2, F0, F90, -eta, distribution), BSDF = mix(BSDF, TransmissionBSDF, TransmissionWeight); } closure color MetallicBSDF = 0; if (Metallic > 0.0) { - color F0 = BaseColor; + color F0 = base_color; color F82 = SpecularTint; MetallicBSDF = microfacet_f82_tint(distribution, Normal, T, alpha_x, alpha_y, F0, F82); BSDF = mix(BSDF, MetallicBSDF, Metallic); diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 83bd9128a2b..5bd9cc78c53 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -139,6 +139,7 @@ ccl_device make_float3(__uint_as_float(data_base_color.y), __uint_as_float(data_base_color.z), __uint_as_float(data_base_color.w)); + base_color = max(base_color, zero_float3()); // get the subsurface scattering data uint4 data_subsurf = read_node(kg, &offset); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 3c4eb4dae28..200a0a9907f 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -60,7 +60,9 @@ void node_bsdf_principled(vec4 base_color, out Closure result) { /* Match cycles. */ - + base_color = max(base_color, vec4(0.0)); + + N = safe_normalize(N); CN = safe_normalize(CN); vec3 V = cameraVec(g_data.P); -- 2.30.2 From 6e0d74045dbf41caeaa583393b590cebd4c17c7f Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 14:26:17 +1300 Subject: [PATCH 04/36] Clamp base color for Metallic and Subsurface --- .../osl/shaders/node_principled_bsdf.osl | 25 ++++++++++++------- intern/cycles/kernel/svm/closure.h | 12 ++++++--- .../gpu_shader_material_principled.glsl | 9 +++++-- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 3c31c326a4e..7c8911cc5a0 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -36,9 +36,16 @@ shader node_principled_bsdf(string distribution = "multi_ggx", normal Tangent = normalize(dPdu), output closure color BSDF = 0) { + float CLOSURE_WEIGHT_CUTOFF = 1e-5; + /* Clamping to match SVM */ color base_color = max(BaseColor, color(0.0)); - + color clamped_base_color = min(base_color, color(1.0)); + float clamped_color_weight = max(Metallic, SubsurfaceWeight); + if (clamped_color_weight > CLOSURE_WEIGHT_CUTOFF) { + /* Metallic and Subsurface Scattering materials behave unpredictably with values greater than 1.0. */ + base_color = mix(base_color, clamped_base_color, clamped_color_weight); + } float r2 = Roughness; @@ -68,21 +75,21 @@ shader node_principled_bsdf(string distribution = "multi_ggx", } BSDF = base_color * diffuse(Normal); - if (SubsurfaceWeight > 1e-5) { + if (SubsurfaceWeight > CLOSURE_WEIGHT_CUTOFF) { float subsurface_weight = SubsurfaceWeight; vector radius = SubsurfaceScale * SubsurfaceRadius; float subsurface_ior = (subsurface_method == "random_walk_skin") ? SubsurfaceIOR : eta; closure color SubsurfBSDF = bssrdf(subsurface_method, Normal, SubsurfaceScale * SubsurfaceRadius, - base_color, + clamped_base_color, "roughness", r2, "ior", subsurface_ior, "anisotropy", SubsurfaceAnisotropy); - BSDF = mix(BSDF, base_color * SubsurfBSDF, subsurface_weight); + BSDF = mix(BSDF, clamped_base_color * SubsurfBSDF, subsurface_weight); } if (eta != 1.0) { @@ -98,7 +105,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", } closure color TransmissionBSDF = 0; - if (Metallic < 1.0 && TransmissionWeight > 0.0) { + if (Metallic < 1.0 && TransmissionWeight > CLOSURE_WEIGHT_CUTOFF) { float eta = max(IOR, 1e-5); eta = backfacing() ? 1.0 / eta : eta; @@ -111,8 +118,8 @@ shader node_principled_bsdf(string distribution = "multi_ggx", } closure color MetallicBSDF = 0; - if (Metallic > 0.0) { - color F0 = base_color; + if (Metallic > CLOSURE_WEIGHT_CUTOFF) { + color F0 = clamped_base_color; color F82 = SpecularTint; MetallicBSDF = microfacet_f82_tint(distribution, Normal, T, alpha_x, alpha_y, F0, F82); BSDF = mix(BSDF, MetallicBSDF, Metallic); @@ -122,7 +129,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", BSDF += EmissionStrength * EmissionColor * emission(); } - if (CoatWeight > 1e-5) { + if (CoatWeight > CLOSURE_WEIGHT_CUTOFF) { float coat_ior = max(CoatIOR, 1.0); if (CoatTint != color(1.0)) { float coat_neta = 1.0 / coat_ior; @@ -138,7 +145,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", BSDF = layer(CoatWeight * CoatBSDF, BSDF); } - if (SheenWeight > 1e-5) { + if (SheenWeight > CLOSURE_WEIGHT_CUTOFF) { normal sheen_normal = normalize(mix(Normal, CoatNormal, CoatWeight)); closure color SheenBSDF = sheen(sheen_normal, SheenRoughness); BSDF = layer(SheenWeight * SheenTint * SheenBSDF, BSDF); diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 5bd9cc78c53..28e3e3e1f78 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -140,6 +140,12 @@ ccl_device __uint_as_float(data_base_color.z), __uint_as_float(data_base_color.w)); base_color = max(base_color, zero_float3()); + const float3 clamped_base_color = min(base_color, one_float3()); + const float clamped_color_weight = max(metallic, subsurface_weight); + if (clamped_color_weight > CLOSURE_WEIGHT_CUTOFF) { + /* Metallic and Subsurface Scattering materials behave unpredictably with values greater than 1.0. */ + base_color = mix(base_color, clamped_base_color, clamped_color_weight); + } // get the subsurface scattering data uint4 data_subsurf = read_node(kg, &offset); @@ -281,7 +287,7 @@ ccl_device bsdf->alpha_x = alpha_x; bsdf->alpha_y = alpha_y; - fresnel->f0 = rgb_to_spectrum(base_color); + fresnel->f0 = rgb_to_spectrum(clamped_base_color); const Spectrum f82 = specular_tint; /* setup bsdf */ @@ -373,13 +379,13 @@ ccl_device /* Diffuse/Subsurface component */ #ifdef __SUBSURFACE__ ccl_private Bssrdf *bssrdf = bssrdf_alloc( - sd, rgb_to_spectrum(base_color) * subsurface_weight * weight); + sd, rgb_to_spectrum(clamped_base_color) * subsurface_weight * weight); if (bssrdf) { float3 subsurface_radius = stack_load_float3(stack, data_subsurf.y); float subsurface_scale = stack_load_float(stack, data_subsurf.z); bssrdf->radius = rgb_to_spectrum(subsurface_radius * subsurface_scale); - bssrdf->albedo = rgb_to_spectrum(base_color); + bssrdf->albedo = rgb_to_spectrum(clamped_base_color); bssrdf->N = maybe_ensure_valid_specular_reflection(sd, N); bssrdf->alpha = sqr(roughness); bssrdf->ior = eta; diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 200a0a9907f..c814fb1ea83 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -61,7 +61,12 @@ void node_bsdf_principled(vec4 base_color, { /* Match cycles. */ base_color = max(base_color, vec4(0.0)); - + vec4 clamped_base_color = min(base_color, vec4(1.0)); + float clamped_color_weight = max(metallic, subsurface_weight); + if (clamped_color_weight > 0.0) { + /* Metallic and Subsurface Scattering materials behave unpredictably with values greater than 1.0. */ + base_color = mix(base_color, clamped_base_color, clamped_color_weight); + } N = safe_normalize(N); CN = safe_normalize(CN); @@ -125,7 +130,7 @@ void node_bsdf_principled(vec4 base_color, reflection_data.roughness = roughness; vec3 reflection_tint = specular_tint.rgb; if (metallic > 0.0) { - vec3 F0 = base_color.rgb; + vec3 F0 = clamped_base_color.rgb; vec3 F82 = reflection_tint; vec3 metallic_brdf; brdf_f82_tint_lut(F0, F82, NV, roughness, do_multiscatter != 0.0, metallic_brdf); -- 2.30.2 From b46aff1f7e8fed8f09eb4ea02da29049f117a172 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 14:34:43 +1300 Subject: [PATCH 05/36] Clamp roughness inputs --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 6 +++--- intern/cycles/kernel/svm/closure.h | 6 +++--- .../shaders/material/gpu_shader_material_principled.glsl | 7 +++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 7c8911cc5a0..1bd2256ed63 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -48,7 +48,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", } - float r2 = Roughness; + float r2 = clamp(Roughness, 0.0, 1.0); r2 = r2 * r2; float alpha_x = r2, alpha_y = r2; @@ -137,7 +137,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float cosNT = sqrt(1.0 - coat_neta * coat_neta * (1 - cosNI * cosNI)); BSDF *= pow(CoatTint, CoatWeight / cosNT); } - float coat_r2 = CoatRoughness; + float coat_r2 = clamp(CoatRoughness, 0.0, 1.0); coat_r2 = coat_r2 * coat_r2; closure color CoatBSDF = dielectric_bsdf( @@ -147,7 +147,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", if (SheenWeight > CLOSURE_WEIGHT_CUTOFF) { normal sheen_normal = normalize(mix(Normal, CoatNormal, CoatWeight)); - closure color SheenBSDF = sheen(sheen_normal, SheenRoughness); + closure color SheenBSDF = sheen(sheen_normal, clamp(SheenRoughness, 0.0, 1.0)); BSDF = layer(SheenWeight * SheenTint * SheenBSDF, BSDF); } diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 28e3e3e1f78..0b9193516e6 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -110,14 +110,14 @@ ccl_device float metallic = param1; float subsurface_weight = param2; float specular_ior_level = stack_load_float(stack, specular_ior_level_offset); - float roughness = stack_load_float(stack, roughness_offset); + float roughness = saturatef(stack_load_float(stack, roughness_offset)); Spectrum specular_tint = rgb_to_spectrum(stack_load_float3(stack, specular_tint_offset)); float anisotropic = stack_load_float(stack, anisotropic_offset); float sheen_weight = stack_load_float(stack, sheen_weight_offset); float3 sheen_tint = stack_load_float3(stack, sheen_tint_offset); - float sheen_roughness = stack_load_float(stack, sheen_roughness_offset); + float sheen_roughness = saturatef(stack_load_float(stack, sheen_roughness_offset)); float coat_weight = stack_load_float(stack, coat_weight_offset); - float coat_roughness = stack_load_float(stack, coat_roughness_offset); + float coat_roughness = saturatef(stack_load_float(stack, coat_roughness_offset)); float coat_ior = fmaxf(stack_load_float(stack, coat_ior_offset), 1.0f); float3 coat_tint = stack_load_float3(stack, coat_tint_offset); float transmission_weight = stack_load_float(stack, transmission_weight_offset); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index c814fb1ea83..e7a7761d880 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -67,6 +67,13 @@ void node_bsdf_principled(vec4 base_color, /* Metallic and Subsurface Scattering materials behave unpredictably with values greater than 1.0. */ base_color = mix(base_color, clamped_base_color, clamped_color_weight); } + roughness = saturate(roughness); + + coat_roughness = saturate(coat_roughness); + + sheen_roughness = saturate(sheen_roughness); + + N = safe_normalize(N); CN = safe_normalize(CN); -- 2.30.2 From 43ae9949ac922efd567f23efd95f4587571ad1ed Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 14:37:21 +1300 Subject: [PATCH 06/36] Clamp IOR --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 2 +- .../gpu/shaders/material/gpu_shader_material_principled.glsl | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 1bd2256ed63..325d4349570 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -64,7 +64,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", } if (Metallic < 1.0 && TransmissionWeight < 1.0) { - float eta = IOR; + float eta = max(IOR, 1e-5); float f0 = F0_from_ior(eta); if (SpecularIORLevel != 0.5) { f0 *= 2.0 * SpecularIORLevel; diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index e7a7761d880..03bab00404c 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -68,6 +68,7 @@ void node_bsdf_principled(vec4 base_color, base_color = mix(base_color, clamped_base_color, clamped_color_weight); } roughness = saturate(roughness); + ior = max(ior, 1e-5); coat_roughness = saturate(coat_roughness); -- 2.30.2 From 5247f635931f7fa90ac7211786d8159c65062cac Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 14:42:22 +1300 Subject: [PATCH 07/36] Clamp Alpha --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 2 +- intern/cycles/kernel/svm/closure.h | 1 + .../gpu/shaders/material/gpu_shader_material_principled.glsl | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 325d4349570..2f709e4333c 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -151,5 +151,5 @@ shader node_principled_bsdf(string distribution = "multi_ggx", BSDF = layer(SheenWeight * SheenTint * SheenBSDF, BSDF); } - BSDF = mix(transparent(), BSDF, Alpha); + BSDF = mix(transparent(), BSDF, clamp(Alpha, 0.0, 1.0)); } diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 0b9193516e6..5dd8338785c 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -158,6 +158,7 @@ ccl_device &dummy); float alpha = stack_valid(alpha_offset) ? stack_load_float(stack, alpha_offset) : __uint_as_float(data_alpha_emission.y); + alpha = saturatef(alpha); float emission_strength = stack_valid(emission_strength_offset) ? stack_load_float(stack, emission_strength_offset) : diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 03bab00404c..008fa945c0c 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -69,7 +69,7 @@ void node_bsdf_principled(vec4 base_color, } roughness = saturate(roughness); ior = max(ior, 1e-5); - + alpha = saturate(alpha); coat_roughness = saturate(coat_roughness); sheen_roughness = saturate(sheen_roughness); -- 2.30.2 From 39f3bbf9b0d32cf57b7a6cdc3dfe7b52c83395d5 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 14:49:18 +1300 Subject: [PATCH 08/36] Clamp Subsurface Weight --- .../kernel/osl/shaders/node_principled_bsdf.osl | 7 ++++--- intern/cycles/kernel/svm/closure.h | 2 +- .../material/gpu_shader_material_principled.glsl | 15 ++++++++++----- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 2f709e4333c..5dd7af71cb6 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -39,9 +39,11 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float CLOSURE_WEIGHT_CUTOFF = 1e-5; /* Clamping to match SVM */ + float subsurface_weight = clamp(SubsurfaceWeight, 0.0, 1.0); + color base_color = max(BaseColor, color(0.0)); color clamped_base_color = min(base_color, color(1.0)); - float clamped_color_weight = max(Metallic, SubsurfaceWeight); + float clamped_color_weight = max(Metallic, subsurface_weight); if (clamped_color_weight > CLOSURE_WEIGHT_CUTOFF) { /* Metallic and Subsurface Scattering materials behave unpredictably with values greater than 1.0. */ base_color = mix(base_color, clamped_base_color, clamped_color_weight); @@ -75,8 +77,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", } BSDF = base_color * diffuse(Normal); - if (SubsurfaceWeight > CLOSURE_WEIGHT_CUTOFF) { - float subsurface_weight = SubsurfaceWeight; + if (subsurface_weight > CLOSURE_WEIGHT_CUTOFF) { vector radius = SubsurfaceScale * SubsurfaceRadius; float subsurface_ior = (subsurface_method == "random_walk_skin") ? SubsurfaceIOR : eta; closure color SubsurfBSDF = bssrdf(subsurface_method, diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 5dd8338785c..cdfb88d093b 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -108,7 +108,7 @@ ccl_device // get Disney principled parameters float metallic = param1; - float subsurface_weight = param2; + float subsurface_weight = saturatef(param2); float specular_ior_level = stack_load_float(stack, specular_ior_level_offset); float roughness = saturatef(stack_load_float(stack, roughness_offset)); Spectrum specular_tint = rgb_to_spectrum(stack_load_float3(stack, specular_tint_offset)); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 008fa945c0c..48a8a09e676 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -60,6 +60,15 @@ void node_bsdf_principled(vec4 base_color, out Closure result) { /* Match cycles. */ + subsurface_weight = saturate(subsurface_weight); + + roughness = saturate(roughness); + ior = max(ior, 1e-5); + alpha = saturate(alpha); + coat_roughness = saturate(coat_roughness); + + sheen_roughness = saturate(sheen_roughness); + base_color = max(base_color, vec4(0.0)); vec4 clamped_base_color = min(base_color, vec4(1.0)); float clamped_color_weight = max(metallic, subsurface_weight); @@ -67,12 +76,8 @@ void node_bsdf_principled(vec4 base_color, /* Metallic and Subsurface Scattering materials behave unpredictably with values greater than 1.0. */ base_color = mix(base_color, clamped_base_color, clamped_color_weight); } - roughness = saturate(roughness); - ior = max(ior, 1e-5); - alpha = saturate(alpha); - coat_roughness = saturate(coat_roughness); - sheen_roughness = saturate(sheen_roughness); + -- 2.30.2 From b4efa20bd67746bd7975ba21f660ea98753286d8 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 14:59:44 +1300 Subject: [PATCH 09/36] Clamp metallic --- .../kernel/osl/shaders/node_principled_bsdf.osl | 11 ++++++----- intern/cycles/kernel/svm/closure.h | 2 +- .../material/gpu_shader_material_principled.glsl | 1 + 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 5dd7af71cb6..88f039b6cb8 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -39,11 +39,12 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float CLOSURE_WEIGHT_CUTOFF = 1e-5; /* Clamping to match SVM */ + float metallic = clamp(Metallic, 0.0, 1.0); float subsurface_weight = clamp(SubsurfaceWeight, 0.0, 1.0); color base_color = max(BaseColor, color(0.0)); color clamped_base_color = min(base_color, color(1.0)); - float clamped_color_weight = max(Metallic, subsurface_weight); + float clamped_color_weight = max(metallic, subsurface_weight); if (clamped_color_weight > CLOSURE_WEIGHT_CUTOFF) { /* Metallic and Subsurface Scattering materials behave unpredictably with values greater than 1.0. */ base_color = mix(base_color, clamped_base_color, clamped_color_weight); @@ -65,7 +66,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", T = rotate(T, AnisotropicRotation * M_2PI, point(0.0, 0.0, 0.0), Normal); } - if (Metallic < 1.0 && TransmissionWeight < 1.0) { + if (metallic < 1.0 && TransmissionWeight < 1.0) { float eta = max(IOR, 1e-5); float f0 = F0_from_ior(eta); if (SpecularIORLevel != 0.5) { @@ -106,7 +107,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", } closure color TransmissionBSDF = 0; - if (Metallic < 1.0 && TransmissionWeight > CLOSURE_WEIGHT_CUTOFF) { + if (metallic < 1.0 && TransmissionWeight > CLOSURE_WEIGHT_CUTOFF) { float eta = max(IOR, 1e-5); eta = backfacing() ? 1.0 / eta : eta; @@ -119,11 +120,11 @@ shader node_principled_bsdf(string distribution = "multi_ggx", } closure color MetallicBSDF = 0; - if (Metallic > CLOSURE_WEIGHT_CUTOFF) { + if (metallic > CLOSURE_WEIGHT_CUTOFF) { color F0 = clamped_base_color; color F82 = SpecularTint; MetallicBSDF = microfacet_f82_tint(distribution, Normal, T, alpha_x, alpha_y, F0, F82); - BSDF = mix(BSDF, MetallicBSDF, Metallic); + BSDF = mix(BSDF, MetallicBSDF, metallic); } if (EmissionStrength > 0.0 && EmissionColor != color(0.0)) { diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index cdfb88d093b..543d80e9973 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -107,7 +107,7 @@ ccl_device &coat_tint_offset); // get Disney principled parameters - float metallic = param1; + float metallic = saturatef(param1); float subsurface_weight = saturatef(param2); float specular_ior_level = stack_load_float(stack, specular_ior_level_offset); float roughness = saturatef(stack_load_float(stack, roughness_offset)); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 48a8a09e676..7bdd1c073d6 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -60,6 +60,7 @@ void node_bsdf_principled(vec4 base_color, out Closure result) { /* Match cycles. */ + metallic = saturate(metallic); subsurface_weight = saturate(subsurface_weight); roughness = saturate(roughness); -- 2.30.2 From db3899f6142c3d8ac2c949314392638044b8dd0d Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 15:04:26 +1300 Subject: [PATCH 10/36] Clamp subsurface radius --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 4 ++-- intern/cycles/kernel/svm/closure.h | 2 +- .../gpu/shaders/material/gpu_shader_material_principled.glsl | 5 +++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 88f039b6cb8..4b49f2585d6 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -79,11 +79,11 @@ shader node_principled_bsdf(string distribution = "multi_ggx", BSDF = base_color * diffuse(Normal); if (subsurface_weight > CLOSURE_WEIGHT_CUTOFF) { - vector radius = SubsurfaceScale * SubsurfaceRadius; + vector radius = SubsurfaceScale * max(SubsurfaceRadius, vector(0.0)); float subsurface_ior = (subsurface_method == "random_walk_skin") ? SubsurfaceIOR : eta; closure color SubsurfBSDF = bssrdf(subsurface_method, Normal, - SubsurfaceScale * SubsurfaceRadius, + radius, clamped_base_color, "roughness", r2, diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 543d80e9973..655eb0caaa8 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -382,7 +382,7 @@ ccl_device ccl_private Bssrdf *bssrdf = bssrdf_alloc( sd, rgb_to_spectrum(clamped_base_color) * subsurface_weight * weight); if (bssrdf) { - float3 subsurface_radius = stack_load_float3(stack, data_subsurf.y); + float3 subsurface_radius = max(stack_load_float3(stack, data_subsurf.y), zero_float3()); float subsurface_scale = stack_load_float(stack, data_subsurf.z); bssrdf->radius = rgb_to_spectrum(subsurface_radius * subsurface_scale); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 7bdd1c073d6..d7804cce460 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -61,11 +61,12 @@ void node_bsdf_principled(vec4 base_color, { /* Match cycles. */ metallic = saturate(metallic); - subsurface_weight = saturate(subsurface_weight); - roughness = saturate(roughness); ior = max(ior, 1e-5); alpha = saturate(alpha); + subsurface_weight = saturate(subsurface_weight); + subsurface_radius = max(subsurface_radius, vec3(0.0)); + coat_roughness = saturate(coat_roughness); sheen_roughness = saturate(sheen_roughness); -- 2.30.2 From 270a6ec0ffeabeca4957eb2d73b3b62e72332f0f Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 15:08:23 +1300 Subject: [PATCH 11/36] Clamp subsurface scale --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 2 +- intern/cycles/kernel/svm/closure.h | 2 +- .../gpu/shaders/material/gpu_shader_material_principled.glsl | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 4b49f2585d6..42a04e84b29 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -79,7 +79,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", BSDF = base_color * diffuse(Normal); if (subsurface_weight > CLOSURE_WEIGHT_CUTOFF) { - vector radius = SubsurfaceScale * max(SubsurfaceRadius, vector(0.0)); + vector radius = max(SubsurfaceScale, 0.0) * max(SubsurfaceRadius, vector(0.0)); float subsurface_ior = (subsurface_method == "random_walk_skin") ? SubsurfaceIOR : eta; closure color SubsurfBSDF = bssrdf(subsurface_method, Normal, diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 655eb0caaa8..600e1e3f624 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -383,7 +383,7 @@ ccl_device sd, rgb_to_spectrum(clamped_base_color) * subsurface_weight * weight); if (bssrdf) { float3 subsurface_radius = max(stack_load_float3(stack, data_subsurf.y), zero_float3()); - float subsurface_scale = stack_load_float(stack, data_subsurf.z); + float subsurface_scale = max(stack_load_float(stack, data_subsurf.z), 0.0f); bssrdf->radius = rgb_to_spectrum(subsurface_radius * subsurface_scale); bssrdf->albedo = rgb_to_spectrum(clamped_base_color); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index d7804cce460..c31cc1acf45 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -66,7 +66,8 @@ void node_bsdf_principled(vec4 base_color, alpha = saturate(alpha); subsurface_weight = saturate(subsurface_weight); subsurface_radius = max(subsurface_radius, vec3(0.0)); - + subsurface_scale = max(subsurface_scale, 0.0); + coat_roughness = saturate(coat_roughness); sheen_roughness = saturate(sheen_roughness); -- 2.30.2 From 61402741586aa5df846df04856b28c93d60199aa Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 15:11:06 +1300 Subject: [PATCH 12/36] Add comments about subsurface anisotropy clamping --- intern/cycles/kernel/svm/closure.h | 1 + .../gpu/shaders/material/gpu_shader_material_principled.glsl | 2 ++ 2 files changed, 3 insertions(+) diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 600e1e3f624..c5da4c2f945 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -390,6 +390,7 @@ ccl_device bssrdf->N = maybe_ensure_valid_specular_reflection(sd, N); bssrdf->alpha = sqr(roughness); bssrdf->ior = eta; + /* Anisotropy is clamped to [0.0..0.9] inside bssrdf_setup */ bssrdf->anisotropy = stack_load_float(stack, data_subsurf.w); if (subsurface_method == CLOSURE_BSSRDF_RANDOM_WALK_SKIN_ID) { bssrdf->ior = stack_load_float(stack, data_subsurf.x); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index c31cc1acf45..0b80668b7e3 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -67,6 +67,8 @@ void node_bsdf_principled(vec4 base_color, subsurface_weight = saturate(subsurface_weight); subsurface_radius = max(subsurface_radius, vec3(0.0)); subsurface_scale = max(subsurface_scale, 0.0); + /* Not used by EEVEE */ + /* subsurface_anisotropy = clamp(subsurface_anisotropy, 0.0, 0.9); */ coat_roughness = saturate(coat_roughness); -- 2.30.2 From 472b163bec2a5a5bb38481dfe8d79ebca69f4104 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 15:13:13 +1300 Subject: [PATCH 13/36] Add comments about subsurface IOR clamps --- intern/cycles/kernel/svm/closure.h | 1 + .../gpu/shaders/material/gpu_shader_material_principled.glsl | 1 + 2 files changed, 2 insertions(+) diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index c5da4c2f945..53ccd93057d 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -389,6 +389,7 @@ ccl_device bssrdf->albedo = rgb_to_spectrum(clamped_base_color); bssrdf->N = maybe_ensure_valid_specular_reflection(sd, N); bssrdf->alpha = sqr(roughness); + /* IOR is clamped to [1.01..3.8] inside bssrdf_setup */ bssrdf->ior = eta; /* Anisotropy is clamped to [0.0..0.9] inside bssrdf_setup */ bssrdf->anisotropy = stack_load_float(stack, data_subsurf.w); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 0b80668b7e3..e270976386e 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -69,6 +69,7 @@ void node_bsdf_principled(vec4 base_color, subsurface_scale = max(subsurface_scale, 0.0); /* Not used by EEVEE */ /* subsurface_anisotropy = clamp(subsurface_anisotropy, 0.0, 0.9); */ + /* subsurface_ior = clamp(subsurface_ior, 1.01, 3.8); */ coat_roughness = saturate(coat_roughness); -- 2.30.2 From 496a1082972baad0a068a0785a0becf1811e9b80 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 15:22:40 +1300 Subject: [PATCH 14/36] Clamp Specular IOR level --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 2 +- intern/cycles/kernel/svm/closure.h | 2 +- .../gpu/shaders/material/gpu_shader_material_principled.glsl | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 42a04e84b29..92d0fa66ae9 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -70,7 +70,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float eta = max(IOR, 1e-5); float f0 = F0_from_ior(eta); if (SpecularIORLevel != 0.5) { - f0 *= 2.0 * SpecularIORLevel; + f0 *= 2.0 * max(SpecularIORLevel, 0.0); eta = ior_from_F0(f0); if (IOR < 1.0) { eta = 1.0 / eta; diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 53ccd93057d..09d29a6047e 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -109,7 +109,7 @@ ccl_device // get Disney principled parameters float metallic = saturatef(param1); float subsurface_weight = saturatef(param2); - float specular_ior_level = stack_load_float(stack, specular_ior_level_offset); + float specular_ior_level = max(stack_load_float(stack, specular_ior_level_offset), 0.0f); float roughness = saturatef(stack_load_float(stack, roughness_offset)); Spectrum specular_tint = rgb_to_spectrum(stack_load_float3(stack, specular_tint_offset)); float anisotropic = stack_load_float(stack, anisotropic_offset); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index e270976386e..5e376314a65 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -70,6 +70,7 @@ void node_bsdf_principled(vec4 base_color, /* Not used by EEVEE */ /* subsurface_anisotropy = clamp(subsurface_anisotropy, 0.0, 0.9); */ /* subsurface_ior = clamp(subsurface_ior, 1.01, 3.8); */ + specular_ior_level = max(specular_ior_level, 0.0); coat_roughness = saturate(coat_roughness); -- 2.30.2 From 131d7db0b562af91e7fe78310c445b2bd4eba6f7 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 15:43:13 +1300 Subject: [PATCH 15/36] Clamp specular tint --- intern/cycles/kernel/closure/bsdf_microfacet.h | 2 +- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 8 ++++---- intern/cycles/kernel/svm/closure.h | 4 ++-- .../shaders/material/gpu_shader_material_principled.glsl | 3 ++- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/intern/cycles/kernel/closure/bsdf_microfacet.h b/intern/cycles/kernel/closure/bsdf_microfacet.h index 8744b55703a..d21b6ecaf41 100644 --- a/intern/cycles/kernel/closure/bsdf_microfacet.h +++ b/intern/cycles/kernel/closure/bsdf_microfacet.h @@ -827,7 +827,7 @@ ccl_device void bsdf_microfacet_setup_fresnel_f82_tint(KernelGlobals kg, * Therefore, the factor follows by setting F82Tint(cosI) = FSchlick(cosI) - b*cosI*(1-cosI)^6 * and F82Tint(acos(1/7)) = FSchlick(acos(1/7)) * f82_tint and solving for b. */ const float f = 6.0f / 7.0f; - const float f5 = sqr(sqr(f)) * f; + const float f5 = sqr(sqr(f)) * f; /* 0.4626643659 */ const Spectrum F_schlick = mix(fresnel->f0, one_spectrum(), f5); fresnel->b = F_schlick * (7.0f / (f5 * f)) * (one_spectrum() - f82_tint); } diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 92d0fa66ae9..c6c33f70f54 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -41,7 +41,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", /* Clamping to match SVM */ float metallic = clamp(Metallic, 0.0, 1.0); float subsurface_weight = clamp(SubsurfaceWeight, 0.0, 1.0); - + color specular_tint = max(SpecularTint, color(0.0)); color base_color = max(BaseColor, color(0.0)); color clamped_base_color = min(base_color, color(1.0)); float clamped_color_weight = max(metallic, subsurface_weight); @@ -96,7 +96,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", if (eta != 1.0) { /* Apply specular tint */ - color F0 = f0 * SpecularTint; + color F0 = f0 * specular_tint; color F90 = color(1.0); BSDF = layer( @@ -111,7 +111,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float eta = max(IOR, 1e-5); eta = backfacing() ? 1.0 / eta : eta; - color F0 = F0_from_ior(eta) * SpecularTint; + color F0 = F0_from_ior(eta) * specular_tint; color F90 = color(1.0); TransmissionBSDF = generalized_schlick_bsdf( @@ -122,7 +122,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", closure color MetallicBSDF = 0; if (metallic > CLOSURE_WEIGHT_CUTOFF) { color F0 = clamped_base_color; - color F82 = SpecularTint; + color F82 = min(specular_tint, color(1.0)); MetallicBSDF = microfacet_f82_tint(distribution, Normal, T, alpha_x, alpha_y, F0, F82); BSDF = mix(BSDF, MetallicBSDF, metallic); } diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 09d29a6047e..3e2a7acb223 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -111,7 +111,7 @@ ccl_device float subsurface_weight = saturatef(param2); float specular_ior_level = max(stack_load_float(stack, specular_ior_level_offset), 0.0f); float roughness = saturatef(stack_load_float(stack, roughness_offset)); - Spectrum specular_tint = rgb_to_spectrum(stack_load_float3(stack, specular_tint_offset)); + Spectrum specular_tint = rgb_to_spectrum(max(stack_load_float3(stack, specular_tint_offset), zero_float3())); float anisotropic = stack_load_float(stack, anisotropic_offset); float sheen_weight = stack_load_float(stack, sheen_weight_offset); float3 sheen_tint = stack_load_float3(stack, sheen_tint_offset); @@ -289,7 +289,7 @@ ccl_device bsdf->alpha_y = alpha_y; fresnel->f0 = rgb_to_spectrum(clamped_base_color); - const Spectrum f82 = specular_tint; + const Spectrum f82 = min(specular_tint, one_spectrum()); /* setup bsdf */ sd->flag |= bsdf_microfacet_ggx_setup(bsdf); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 5e376314a65..c0acaecb96e 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -71,6 +71,7 @@ void node_bsdf_principled(vec4 base_color, /* subsurface_anisotropy = clamp(subsurface_anisotropy, 0.0, 0.9); */ /* subsurface_ior = clamp(subsurface_ior, 1.01, 3.8); */ specular_ior_level = max(specular_ior_level, 0.0); + specular_tint = max(specular_tint, vec4(0.0)); coat_roughness = saturate(coat_roughness); @@ -151,7 +152,7 @@ void node_bsdf_principled(vec4 base_color, vec3 reflection_tint = specular_tint.rgb; if (metallic > 0.0) { vec3 F0 = clamped_base_color.rgb; - vec3 F82 = reflection_tint; + vec3 F82 = min(reflection_tint, vec3(1.0)); vec3 metallic_brdf; brdf_f82_tint_lut(F0, F82, NV, roughness, do_multiscatter != 0.0, metallic_brdf); reflection_data.color = weight * metallic * metallic_brdf; -- 2.30.2 From e0f59887d149e05a05537464bee7f9bda0292fd5 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 16:00:10 +1300 Subject: [PATCH 16/36] Clamp anisotropic --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 2 +- intern/cycles/kernel/svm/closure.h | 3 ++- .../gpu/shaders/material/gpu_shader_material_principled.glsl | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index c6c33f70f54..459010761dc 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -59,7 +59,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", /* Handle anisotropy. */ vector T = Tangent; if (Anisotropic > 0.0) { - float aspect = sqrt(1.0 - clamp(Anisotropic, 0.0, 1.0) * 0.9); + float aspect = sqrt(1.0 - min(Anisotropic, 1.0) * 0.9); alpha_x /= aspect; alpha_y *= aspect; if (AnisotropicRotation != 0.0) diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 3e2a7acb223..c0335cb3d39 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -112,7 +112,8 @@ ccl_device float specular_ior_level = max(stack_load_float(stack, specular_ior_level_offset), 0.0f); float roughness = saturatef(stack_load_float(stack, roughness_offset)); Spectrum specular_tint = rgb_to_spectrum(max(stack_load_float3(stack, specular_tint_offset), zero_float3())); - float anisotropic = stack_load_float(stack, anisotropic_offset); + /* anisotropic is clamped to > 0 later on resulting in [0..1] clamping. */ + float anisotropic = min(stack_load_float(stack, anisotropic_offset), 1.0f); float sheen_weight = stack_load_float(stack, sheen_weight_offset); float3 sheen_tint = stack_load_float3(stack, sheen_tint_offset); float sheen_roughness = saturatef(stack_load_float(stack, sheen_roughness_offset)); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index c0acaecb96e..c2e0e13377a 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -72,6 +72,8 @@ void node_bsdf_principled(vec4 base_color, /* subsurface_ior = clamp(subsurface_ior, 1.01, 3.8); */ specular_ior_level = max(specular_ior_level, 0.0); specular_tint = max(specular_tint, vec4(0.0)); + /* Not used by EEVEE */ + /* anisotropic = saturate(anisotropic); */ coat_roughness = saturate(coat_roughness); -- 2.30.2 From 6213c50e67595646ad56dd83212d31e128ef9755 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 16:31:35 +1300 Subject: [PATCH 17/36] Clamp Transmission Weight --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 2 +- intern/cycles/kernel/svm/closure.h | 2 +- .../gpu/shaders/material/gpu_shader_material_principled.glsl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 459010761dc..3df55509d6b 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -116,7 +116,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", TransmissionBSDF = generalized_schlick_bsdf( Normal, vector(0.0), color(1.0), sqrt(base_color), r2, r2, F0, F90, -eta, distribution), - BSDF = mix(BSDF, TransmissionBSDF, TransmissionWeight); + BSDF = mix(BSDF, TransmissionBSDF, min(TransmissionWeight, 1.0)); } closure color MetallicBSDF = 0; diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index c0335cb3d39..1138a691ac6 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -121,7 +121,7 @@ ccl_device float coat_roughness = saturatef(stack_load_float(stack, coat_roughness_offset)); float coat_ior = fmaxf(stack_load_float(stack, coat_ior_offset), 1.0f); float3 coat_tint = stack_load_float3(stack, coat_tint_offset); - float transmission_weight = stack_load_float(stack, transmission_weight_offset); + float transmission_weight = saturatef(stack_load_float(stack, transmission_weight_offset)); float anisotropic_rotation = stack_load_float(stack, anisotropic_rotation_offset); float ior = fmaxf(stack_load_float(stack, eta_offset), 1e-5f); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index c2e0e13377a..8937867d429 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -74,7 +74,7 @@ void node_bsdf_principled(vec4 base_color, specular_tint = max(specular_tint, vec4(0.0)); /* Not used by EEVEE */ /* anisotropic = saturate(anisotropic); */ - + transmission_weight = saturate(transmission_weight); coat_roughness = saturate(coat_roughness); sheen_roughness = saturate(sheen_roughness); -- 2.30.2 From 8743beec8fd18fb46586c72db29817f0d9fed687 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 16:37:40 +1300 Subject: [PATCH 18/36] Clamp coat weight --- .../cycles/kernel/osl/shaders/node_principled_bsdf.osl | 9 +++++---- intern/cycles/kernel/svm/closure.h | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 3df55509d6b..fe02a42b812 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -42,6 +42,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float metallic = clamp(Metallic, 0.0, 1.0); float subsurface_weight = clamp(SubsurfaceWeight, 0.0, 1.0); color specular_tint = max(SpecularTint, color(0.0)); + float coat_weight = clamp(CoatWeight, 0.0, 1.0); color base_color = max(BaseColor, color(0.0)); color clamped_base_color = min(base_color, color(1.0)); float clamped_color_weight = max(metallic, subsurface_weight); @@ -131,24 +132,24 @@ shader node_principled_bsdf(string distribution = "multi_ggx", BSDF += EmissionStrength * EmissionColor * emission(); } - if (CoatWeight > CLOSURE_WEIGHT_CUTOFF) { + if (coat_weight > CLOSURE_WEIGHT_CUTOFF) { float coat_ior = max(CoatIOR, 1.0); if (CoatTint != color(1.0)) { float coat_neta = 1.0 / coat_ior; float cosNI = dot(I, CoatNormal); float cosNT = sqrt(1.0 - coat_neta * coat_neta * (1 - cosNI * cosNI)); - BSDF *= pow(CoatTint, CoatWeight / cosNT); + BSDF *= pow(CoatTint, coat_weight / cosNT); } float coat_r2 = clamp(CoatRoughness, 0.0, 1.0); coat_r2 = coat_r2 * coat_r2; closure color CoatBSDF = dielectric_bsdf( CoatNormal, vector(0.0), color(1.0), color(0.0), coat_r2, coat_r2, coat_ior, "ggx"); - BSDF = layer(CoatWeight * CoatBSDF, BSDF); + BSDF = layer(coat_weight * CoatBSDF, BSDF); } if (SheenWeight > CLOSURE_WEIGHT_CUTOFF) { - normal sheen_normal = normalize(mix(Normal, CoatNormal, CoatWeight)); + normal sheen_normal = normalize(mix(Normal, CoatNormal, coat_weight)); closure color SheenBSDF = sheen(sheen_normal, clamp(SheenRoughness, 0.0, 1.0)); BSDF = layer(SheenWeight * SheenTint * SheenBSDF, BSDF); } diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 1138a691ac6..2c2b3c728f1 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -117,7 +117,7 @@ ccl_device float sheen_weight = stack_load_float(stack, sheen_weight_offset); float3 sheen_tint = stack_load_float3(stack, sheen_tint_offset); float sheen_roughness = saturatef(stack_load_float(stack, sheen_roughness_offset)); - float coat_weight = stack_load_float(stack, coat_weight_offset); + float coat_weight = saturatef(stack_load_float(stack, coat_weight_offset)); float coat_roughness = saturatef(stack_load_float(stack, coat_roughness_offset)); float coat_ior = fmaxf(stack_load_float(stack, coat_ior_offset), 1.0f); float3 coat_tint = stack_load_float3(stack, coat_tint_offset); -- 2.30.2 From b52682017fe5602d479de160ddf0d4e59d055a4d Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 17:02:02 +1300 Subject: [PATCH 19/36] Clamp coat tint --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 2 +- intern/cycles/kernel/svm/closure.h | 2 +- .../gpu/shaders/material/gpu_shader_material_principled.glsl | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index fe02a42b812..1b3fc463846 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -138,7 +138,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float coat_neta = 1.0 / coat_ior; float cosNI = dot(I, CoatNormal); float cosNT = sqrt(1.0 - coat_neta * coat_neta * (1 - cosNI * cosNI)); - BSDF *= pow(CoatTint, coat_weight / cosNT); + BSDF *= pow(max(CoatTint, color(0.0)), coat_weight / cosNT); } float coat_r2 = clamp(CoatRoughness, 0.0, 1.0); coat_r2 = coat_r2 * coat_r2; diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 2c2b3c728f1..ad0b5fb5179 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -120,7 +120,7 @@ ccl_device float coat_weight = saturatef(stack_load_float(stack, coat_weight_offset)); float coat_roughness = saturatef(stack_load_float(stack, coat_roughness_offset)); float coat_ior = fmaxf(stack_load_float(stack, coat_ior_offset), 1.0f); - float3 coat_tint = stack_load_float3(stack, coat_tint_offset); + float3 coat_tint = max(stack_load_float3(stack, coat_tint_offset), zero_float3()); float transmission_weight = saturatef(stack_load_float(stack, transmission_weight_offset)); float anisotropic_rotation = stack_load_float(stack, anisotropic_rotation_offset); float ior = fmaxf(stack_load_float(stack, eta_offset), 1e-5f); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 8937867d429..00f38299d5d 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -76,7 +76,7 @@ void node_bsdf_principled(vec4 base_color, /* anisotropic = saturate(anisotropic); */ transmission_weight = saturate(transmission_weight); coat_roughness = saturate(coat_roughness); - + coat_tint = max(coat_tint, vec4(0.0)); sheen_roughness = saturate(sheen_roughness); base_color = max(base_color, vec4(0.0)); -- 2.30.2 From 0ae4621a25117e1bac1573428f89bcb9efb15745 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 17:20:25 +1300 Subject: [PATCH 20/36] Clamp coat ior --- .../gpu/shaders/material/gpu_shader_material_principled.glsl | 1 + 1 file changed, 1 insertion(+) diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 00f38299d5d..83cddc0205d 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -76,6 +76,7 @@ void node_bsdf_principled(vec4 base_color, /* anisotropic = saturate(anisotropic); */ transmission_weight = saturate(transmission_weight); coat_roughness = saturate(coat_roughness); + coat_ior = max(coat_ior, 1.0); coat_tint = max(coat_tint, vec4(0.0)); sheen_roughness = saturate(sheen_roughness); -- 2.30.2 From 48e21bff9d859a4bd6d36cd69566799ea13f5f9a Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 17:44:13 +1300 Subject: [PATCH 21/36] Clamp sheen weight --- intern/cycles/kernel/svm/closure.h | 1 + .../gpu/shaders/material/gpu_shader_material_principled.glsl | 1 + 2 files changed, 2 insertions(+) diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index ad0b5fb5179..447d8903ac3 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -114,6 +114,7 @@ ccl_device Spectrum specular_tint = rgb_to_spectrum(max(stack_load_float3(stack, specular_tint_offset), zero_float3())); /* anisotropic is clamped to > 0 later on resulting in [0..1] clamping. */ float anisotropic = min(stack_load_float(stack, anisotropic_offset), 1.0f); + /* sheen_weight is clamped to > 0 later on. */ float sheen_weight = stack_load_float(stack, sheen_weight_offset); float3 sheen_tint = stack_load_float3(stack, sheen_tint_offset); float sheen_roughness = saturatef(stack_load_float(stack, sheen_roughness_offset)); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 83cddc0205d..f49dbfb7bc5 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -78,6 +78,7 @@ void node_bsdf_principled(vec4 base_color, coat_roughness = saturate(coat_roughness); coat_ior = max(coat_ior, 1.0); coat_tint = max(coat_tint, vec4(0.0)); + sheen_weight = max(sheen_weight, 0.0); sheen_roughness = saturate(sheen_roughness); base_color = max(base_color, vec4(0.0)); -- 2.30.2 From f77e2d5bb3dd5bbd7de2bb0f18c470623a014187 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 17:46:24 +1300 Subject: [PATCH 22/36] Clamp coat weight in EEVEE --- .../gpu/shaders/material/gpu_shader_material_principled.glsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index f49dbfb7bc5..ecc4a585d54 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -75,10 +75,10 @@ void node_bsdf_principled(vec4 base_color, /* Not used by EEVEE */ /* anisotropic = saturate(anisotropic); */ transmission_weight = saturate(transmission_weight); + coat_weight = max(coat_weight, 0.0); coat_roughness = saturate(coat_roughness); coat_ior = max(coat_ior, 1.0); coat_tint = max(coat_tint, vec4(0.0)); - sheen_weight = max(sheen_weight, 0.0); sheen_roughness = saturate(sheen_roughness); base_color = max(base_color, vec4(0.0)); -- 2.30.2 From f4231d6a6c5501b483221b9008c2157bc211a297 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 18:04:17 +1300 Subject: [PATCH 23/36] Clamp sheen tint --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 2 +- intern/cycles/kernel/svm/closure.h | 2 +- .../gpu/shaders/material/gpu_shader_material_principled.glsl | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 1b3fc463846..538961512db 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -151,7 +151,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", if (SheenWeight > CLOSURE_WEIGHT_CUTOFF) { normal sheen_normal = normalize(mix(Normal, CoatNormal, coat_weight)); closure color SheenBSDF = sheen(sheen_normal, clamp(SheenRoughness, 0.0, 1.0)); - BSDF = layer(SheenWeight * SheenTint * SheenBSDF, BSDF); + BSDF = layer(SheenWeight * max(SheenTint, color(0.0)) * SheenBSDF, BSDF); } BSDF = mix(transparent(), BSDF, clamp(Alpha, 0.0, 1.0)); diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 447d8903ac3..3c61c1dcc53 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -116,7 +116,7 @@ ccl_device float anisotropic = min(stack_load_float(stack, anisotropic_offset), 1.0f); /* sheen_weight is clamped to > 0 later on. */ float sheen_weight = stack_load_float(stack, sheen_weight_offset); - float3 sheen_tint = stack_load_float3(stack, sheen_tint_offset); + float3 sheen_tint = max(stack_load_float3(stack, sheen_tint_offset), zero_float3()); float sheen_roughness = saturatef(stack_load_float(stack, sheen_roughness_offset)); float coat_weight = saturatef(stack_load_float(stack, coat_weight_offset)); float coat_roughness = saturatef(stack_load_float(stack, coat_roughness_offset)); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index ecc4a585d54..628aab81a44 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -80,6 +80,7 @@ void node_bsdf_principled(vec4 base_color, coat_ior = max(coat_ior, 1.0); coat_tint = max(coat_tint, vec4(0.0)); sheen_roughness = saturate(sheen_roughness); + sheen_tint = max(sheen_tint, vec4(0.0)); base_color = max(base_color, vec4(0.0)); vec4 clamped_base_color = min(base_color, vec4(1.0)); -- 2.30.2 From 79cf86d96452b04af50fb3fbeebc2f8a98357d31 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 18:12:31 +1300 Subject: [PATCH 24/36] Fix Emission --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 2 +- intern/cycles/kernel/svm/closure.h | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 538961512db..8b42ef8723e 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -128,7 +128,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", BSDF = mix(BSDF, MetallicBSDF, metallic); } - if (EmissionStrength > 0.0 && EmissionColor != color(0.0)) { + if (EmissionStrength != 0.0 && EmissionColor != color(0.0)) { BSDF += EmissionStrength * EmissionColor * emission(); } diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 3c61c1dcc53..d175dade3f0 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -269,8 +269,9 @@ ccl_device } } - /* Emission (attenuated by sheen and coat) */ - if (!is_zero(emission)) { + /* Emission (attenuated by sheen and coat) */ + weight = max(weight, zero_spectrum()); + if (!is_zero(emission) && !isequal(weight, zero_spectrum())) { emission_setup(sd, rgb_to_spectrum(emission) * weight); } -- 2.30.2 From 55a2f8e450c32c6b227e1e5f88b6781d39a764ac Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 18:14:21 +1300 Subject: [PATCH 25/36] make format --- .../osl/shaders/node_principled_bsdf.osl | 32 +++++++++---------- intern/cycles/kernel/svm/closure.h | 24 +++++++------- .../gpu_shader_material_principled.glsl | 13 +++----- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 8b42ef8723e..d10dfa5faa7 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -36,22 +36,22 @@ shader node_principled_bsdf(string distribution = "multi_ggx", normal Tangent = normalize(dPdu), output closure color BSDF = 0) { - float CLOSURE_WEIGHT_CUTOFF = 1e-5; - - /* Clamping to match SVM */ - float metallic = clamp(Metallic, 0.0, 1.0); - float subsurface_weight = clamp(SubsurfaceWeight, 0.0, 1.0); - color specular_tint = max(SpecularTint, color(0.0)); - float coat_weight = clamp(CoatWeight, 0.0, 1.0); - color base_color = max(BaseColor, color(0.0)); - color clamped_base_color = min(base_color, color(1.0)); - float clamped_color_weight = max(metallic, subsurface_weight); - if (clamped_color_weight > CLOSURE_WEIGHT_CUTOFF) { - /* Metallic and Subsurface Scattering materials behave unpredictably with values greater than 1.0. */ - base_color = mix(base_color, clamped_base_color, clamped_color_weight); - } - - + float CLOSURE_WEIGHT_CUTOFF = 1e-5; + + /* Clamping to match SVM */ + float metallic = clamp(Metallic, 0.0, 1.0); + float subsurface_weight = clamp(SubsurfaceWeight, 0.0, 1.0); + color specular_tint = max(SpecularTint, color(0.0)); + float coat_weight = clamp(CoatWeight, 0.0, 1.0); + color base_color = max(BaseColor, color(0.0)); + color clamped_base_color = min(base_color, color(1.0)); + float clamped_color_weight = max(metallic, subsurface_weight); + if (clamped_color_weight > CLOSURE_WEIGHT_CUTOFF) { + /* Metallic and Subsurface Scattering materials behave unpredictably with values greater + * than 1.0. */ + base_color = mix(base_color, clamped_base_color, clamped_color_weight); + } + float r2 = clamp(Roughness, 0.0, 1.0); r2 = r2 * r2; diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index d175dade3f0..4ea46fdc45d 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -111,7 +111,8 @@ ccl_device float subsurface_weight = saturatef(param2); float specular_ior_level = max(stack_load_float(stack, specular_ior_level_offset), 0.0f); float roughness = saturatef(stack_load_float(stack, roughness_offset)); - Spectrum specular_tint = rgb_to_spectrum(max(stack_load_float3(stack, specular_tint_offset), zero_float3())); + Spectrum specular_tint = rgb_to_spectrum( + max(stack_load_float3(stack, specular_tint_offset), zero_float3())); /* anisotropic is clamped to > 0 later on resulting in [0..1] clamping. */ float anisotropic = min(stack_load_float(stack, anisotropic_offset), 1.0f); /* sheen_weight is clamped to > 0 later on. */ @@ -141,13 +142,14 @@ ccl_device make_float3(__uint_as_float(data_base_color.y), __uint_as_float(data_base_color.z), __uint_as_float(data_base_color.w)); - base_color = max(base_color, zero_float3()); - const float3 clamped_base_color = min(base_color, one_float3()); - const float clamped_color_weight = max(metallic, subsurface_weight); - if (clamped_color_weight > CLOSURE_WEIGHT_CUTOFF) { - /* Metallic and Subsurface Scattering materials behave unpredictably with values greater than 1.0. */ - base_color = mix(base_color, clamped_base_color, clamped_color_weight); - } + base_color = max(base_color, zero_float3()); + const float3 clamped_base_color = min(base_color, one_float3()); + const float clamped_color_weight = max(metallic, subsurface_weight); + if (clamped_color_weight > CLOSURE_WEIGHT_CUTOFF) { + /* Metallic and Subsurface Scattering materials behave unpredictably with values greater + * than 1.0. */ + base_color = mix(base_color, clamped_base_color, clamped_color_weight); + } // get the subsurface scattering data uint4 data_subsurf = read_node(kg, &offset); @@ -160,7 +162,7 @@ ccl_device &dummy); float alpha = stack_valid(alpha_offset) ? stack_load_float(stack, alpha_offset) : __uint_as_float(data_alpha_emission.y); - alpha = saturatef(alpha); + alpha = saturatef(alpha); float emission_strength = stack_valid(emission_strength_offset) ? stack_load_float(stack, emission_strength_offset) : @@ -269,8 +271,8 @@ ccl_device } } - /* Emission (attenuated by sheen and coat) */ - weight = max(weight, zero_spectrum()); + /* Emission (attenuated by sheen and coat) */ + weight = max(weight, zero_spectrum()); if (!is_zero(emission) && !isequal(weight, zero_spectrum())) { emission_setup(sd, rgb_to_spectrum(emission) * weight); } diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 628aab81a44..4fb75ae9f53 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -81,19 +81,16 @@ void node_bsdf_principled(vec4 base_color, coat_tint = max(coat_tint, vec4(0.0)); sheen_roughness = saturate(sheen_roughness); sheen_tint = max(sheen_tint, vec4(0.0)); - + base_color = max(base_color, vec4(0.0)); vec4 clamped_base_color = min(base_color, vec4(1.0)); float clamped_color_weight = max(metallic, subsurface_weight); if (clamped_color_weight > 0.0) { - /* Metallic and Subsurface Scattering materials behave unpredictably with values greater than 1.0. */ - base_color = mix(base_color, clamped_base_color, clamped_color_weight); + /* Metallic and Subsurface Scattering materials behave unpredictably with values greater + * than 1.0. */ + base_color = mix(base_color, clamped_base_color, clamped_color_weight); } - - - - - + N = safe_normalize(N); CN = safe_normalize(CN); vec3 V = cameraVec(g_data.P); -- 2.30.2 From 118339d7fc76c3594d85af8dd30414fe71818778 Mon Sep 17 00:00:00 2001 From: Alaska Date: Wed, 11 Oct 2023 19:16:10 +1300 Subject: [PATCH 26/36] Remove random code comment --- intern/cycles/kernel/closure/bsdf_microfacet.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/intern/cycles/kernel/closure/bsdf_microfacet.h b/intern/cycles/kernel/closure/bsdf_microfacet.h index d21b6ecaf41..8744b55703a 100644 --- a/intern/cycles/kernel/closure/bsdf_microfacet.h +++ b/intern/cycles/kernel/closure/bsdf_microfacet.h @@ -827,7 +827,7 @@ ccl_device void bsdf_microfacet_setup_fresnel_f82_tint(KernelGlobals kg, * Therefore, the factor follows by setting F82Tint(cosI) = FSchlick(cosI) - b*cosI*(1-cosI)^6 * and F82Tint(acos(1/7)) = FSchlick(acos(1/7)) * f82_tint and solving for b. */ const float f = 6.0f / 7.0f; - const float f5 = sqr(sqr(f)) * f; /* 0.4626643659 */ + const float f5 = sqr(sqr(f)) * f; const Spectrum F_schlick = mix(fresnel->f0, one_spectrum(), f5); fresnel->b = F_schlick * (7.0f / (f5 * f)) * (one_spectrum() - f82_tint); } -- 2.30.2 From 0cbb81e54c592fa5ec1c5bb3062a223bbf51c7d2 Mon Sep 17 00:00:00 2001 From: Alaska Date: Thu, 19 Oct 2023 16:56:09 +1300 Subject: [PATCH 27/36] Add code comment about something that will be removed in the future --- intern/cycles/kernel/svm/closure.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 4ea46fdc45d..f252037528b 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -272,7 +272,12 @@ ccl_device } /* Emission (attenuated by sheen and coat) */ + + /* This section of code will be removed after merging some changes from Lukas. */ weight = max(weight, zero_spectrum()); + /* End of region being removed. (Note: The if statement will probably change to the old + * one.)*/ + if (!is_zero(emission) && !isequal(weight, zero_spectrum())) { emission_setup(sd, rgb_to_spectrum(emission) * weight); } -- 2.30.2 From 52b61b37f43dff511a182ddae3e2bc4cde9aaebf Mon Sep 17 00:00:00 2001 From: Alaska Date: Thu, 19 Oct 2023 17:19:22 +1300 Subject: [PATCH 28/36] Address review - Explicitly clamp things to make the code clearer - Clamp the product of subsurface radius instead of individual components - Group clamps in OSL script --- .../kernel/osl/shaders/node_principled_bsdf.osl | 10 ++++++---- intern/cycles/kernel/svm/closure.h | 12 +++++------- .../material/gpu_shader_material_principled.glsl | 5 ++--- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index d10dfa5faa7..1505c892bcf 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -40,9 +40,11 @@ shader node_principled_bsdf(string distribution = "multi_ggx", /* Clamping to match SVM */ float metallic = clamp(Metallic, 0.0, 1.0); + float transmission = clamp(TransmissionWeight, 0.0, 1.0); float subsurface_weight = clamp(SubsurfaceWeight, 0.0, 1.0); color specular_tint = max(SpecularTint, color(0.0)); float coat_weight = clamp(CoatWeight, 0.0, 1.0); + color coat_tint = max(CoatTint, color(0.0)); color base_color = max(BaseColor, color(0.0)); color clamped_base_color = min(base_color, color(1.0)); float clamped_color_weight = max(metallic, subsurface_weight); @@ -60,7 +62,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", /* Handle anisotropy. */ vector T = Tangent; if (Anisotropic > 0.0) { - float aspect = sqrt(1.0 - min(Anisotropic, 1.0) * 0.9); + float aspect = sqrt(1.0 - clamp(Anisotropic, 0.0, 1.0) * 0.9); alpha_x /= aspect; alpha_y *= aspect; if (AnisotropicRotation != 0.0) @@ -80,7 +82,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", BSDF = base_color * diffuse(Normal); if (subsurface_weight > CLOSURE_WEIGHT_CUTOFF) { - vector radius = max(SubsurfaceScale, 0.0) * max(SubsurfaceRadius, vector(0.0)); + vector radius = max(SubsurfaceScale * SubsurfaceRadius, vector(0.0)); float subsurface_ior = (subsurface_method == "random_walk_skin") ? SubsurfaceIOR : eta; closure color SubsurfBSDF = bssrdf(subsurface_method, Normal, @@ -117,7 +119,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", TransmissionBSDF = generalized_schlick_bsdf( Normal, vector(0.0), color(1.0), sqrt(base_color), r2, r2, F0, F90, -eta, distribution), - BSDF = mix(BSDF, TransmissionBSDF, min(TransmissionWeight, 1.0)); + BSDF = mix(BSDF, TransmissionBSDF, transmission); } closure color MetallicBSDF = 0; @@ -138,7 +140,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float coat_neta = 1.0 / coat_ior; float cosNI = dot(I, CoatNormal); float cosNT = sqrt(1.0 - coat_neta * coat_neta * (1 - cosNI * cosNI)); - BSDF *= pow(max(CoatTint, color(0.0)), coat_weight / cosNT); + BSDF *= pow(coat_tint, coat_weight / cosNT); } float coat_r2 = clamp(CoatRoughness, 0.0, 1.0); coat_r2 = coat_r2 * coat_r2; diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index f252037528b..6f73d357ccc 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -113,10 +113,8 @@ ccl_device float roughness = saturatef(stack_load_float(stack, roughness_offset)); Spectrum specular_tint = rgb_to_spectrum( max(stack_load_float3(stack, specular_tint_offset), zero_float3())); - /* anisotropic is clamped to > 0 later on resulting in [0..1] clamping. */ - float anisotropic = min(stack_load_float(stack, anisotropic_offset), 1.0f); - /* sheen_weight is clamped to > 0 later on. */ - float sheen_weight = stack_load_float(stack, sheen_weight_offset); + float anisotropic = saturatef(stack_load_float(stack, anisotropic_offset)); + float sheen_weight = max(stack_load_float(stack, sheen_weight_offset), 0.0f); float3 sheen_tint = max(stack_load_float3(stack, sheen_tint_offset), zero_float3()); float sheen_roughness = saturatef(stack_load_float(stack, sheen_roughness_offset)); float coat_weight = saturatef(stack_load_float(stack, coat_weight_offset)); @@ -392,10 +390,10 @@ ccl_device ccl_private Bssrdf *bssrdf = bssrdf_alloc( sd, rgb_to_spectrum(clamped_base_color) * subsurface_weight * weight); if (bssrdf) { - float3 subsurface_radius = max(stack_load_float3(stack, data_subsurf.y), zero_float3()); - float subsurface_scale = max(stack_load_float(stack, data_subsurf.z), 0.0f); + float3 subsurface_radius = stack_load_float3(stack, data_subsurf.y); + float subsurface_scale = stack_load_float(stack, data_subsurf.z); - bssrdf->radius = rgb_to_spectrum(subsurface_radius * subsurface_scale); + bssrdf->radius = rgb_to_spectrum(max(subsurface_radius * subsurface_scale, zero_float3())); bssrdf->albedo = rgb_to_spectrum(clamped_base_color); bssrdf->N = maybe_ensure_valid_specular_reflection(sd, N); bssrdf->alpha = sqr(roughness); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 4fb75ae9f53..420cc94eca1 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -65,8 +65,6 @@ void node_bsdf_principled(vec4 base_color, ior = max(ior, 1e-5); alpha = saturate(alpha); subsurface_weight = saturate(subsurface_weight); - subsurface_radius = max(subsurface_radius, vec3(0.0)); - subsurface_scale = max(subsurface_scale, 0.0); /* Not used by EEVEE */ /* subsurface_anisotropy = clamp(subsurface_anisotropy, 0.0, 0.9); */ /* subsurface_ior = clamp(subsurface_ior, 1.01, 3.8); */ @@ -79,6 +77,7 @@ void node_bsdf_principled(vec4 base_color, coat_roughness = saturate(coat_roughness); coat_ior = max(coat_ior, 1.0); coat_tint = max(coat_tint, vec4(0.0)); + sheen_weight = max(sheen_weight, 0.0); sheen_roughness = saturate(sheen_roughness); sheen_tint = max(sheen_tint, vec4(0.0)); @@ -221,7 +220,7 @@ void node_bsdf_principled(vec4 base_color, /* Diffuse component */ if (true) { - diffuse_data.sss_radius = subsurface_radius * subsurface_scale; + diffuse_data.sss_radius = max(subsurface_radius * subsurface_scale, vec3(0.0)); diffuse_data.sss_id = uint(do_sss); diffuse_data.color += weight * base_color.rgb * coat_tint.rgb; } -- 2.30.2 From 912b667feaeca6a310595f113cd27cb6674381dd Mon Sep 17 00:00:00 2001 From: Alaska Date: Thu, 19 Oct 2023 17:39:58 +1300 Subject: [PATCH 29/36] OSL explicitly clamp sheen weight --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 1505c892bcf..8c60fc26b4d 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -45,6 +45,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", color specular_tint = max(SpecularTint, color(0.0)); float coat_weight = clamp(CoatWeight, 0.0, 1.0); color coat_tint = max(CoatTint, color(0.0)); + float sheen_weight = max(SheenWeight, 0.0); color base_color = max(BaseColor, color(0.0)); color clamped_base_color = min(base_color, color(1.0)); float clamped_color_weight = max(metallic, subsurface_weight); @@ -153,7 +154,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", if (SheenWeight > CLOSURE_WEIGHT_CUTOFF) { normal sheen_normal = normalize(mix(Normal, CoatNormal, coat_weight)); closure color SheenBSDF = sheen(sheen_normal, clamp(SheenRoughness, 0.0, 1.0)); - BSDF = layer(SheenWeight * max(SheenTint, color(0.0)) * SheenBSDF, BSDF); + BSDF = layer(sheen_weight * max(SheenTint, color(0.0)) * SheenBSDF, BSDF); } BSDF = mix(transparent(), BSDF, clamp(Alpha, 0.0, 1.0)); -- 2.30.2 From d35fc7bc249666313a73a978c8e38848cbebe427 Mon Sep 17 00:00:00 2001 From: Alaska Date: Fri, 20 Oct 2023 13:04:07 +1300 Subject: [PATCH 30/36] Remove old emission code --- intern/cycles/kernel/svm/closure.h | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 0123488a6dc..fe7d198f14e 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -270,13 +270,7 @@ ccl_device } /* Emission (attenuated by sheen and coat) */ - - /* This section of code will be removed after merging some changes from Lukas. */ - weight = max(weight, zero_spectrum()); - /* End of region being removed. (Note: The if statement will probably change to the old - * one.)*/ - - if (!is_zero(emission) && !isequal(weight, zero_spectrum())) { + if (!is_zero(emission)) { emission_setup(sd, rgb_to_spectrum(emission) * weight); } -- 2.30.2 From 7dc05adbb4d1b9080c7182a90bccee1d7e582204 Mon Sep 17 00:00:00 2001 From: Alaska Date: Fri, 20 Oct 2023 13:20:57 +1300 Subject: [PATCH 31/36] Adjust coat weight clamping --- intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl | 6 +++--- intern/cycles/kernel/svm/closure.h | 2 +- .../shaders/material/gpu_shader_material_principled.glsl | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 88f463c8559..442e165bac4 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -43,7 +43,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float transmission = clamp(TransmissionWeight, 0.0, 1.0); float subsurface_weight = clamp(SubsurfaceWeight, 0.0, 1.0); color specular_tint = max(SpecularTint, color(0.0)); - float coat_weight = clamp(CoatWeight, 0.0, 1.0); + float coat_weight = max(CoatWeight, 0.0); color coat_tint = max(CoatTint, color(0.0)); float sheen_weight = max(SheenWeight, 0.0); color base_color = max(BaseColor, color(0.0)); @@ -141,7 +141,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float coat_neta = 1.0 / coat_ior; float cosNI = dot(I, CoatNormal); float cosNT = sqrt(1.0 - coat_neta * coat_neta * (1 - cosNI * cosNI)); - BSDF *= mix(color(1.0), pow(CoatTint, 1.0 / cosNT), coat_weight); + BSDF *= mix(color(1.0), pow(CoatTint, 1.0 / cosNT), clamp(coat_weight, 0.0, 1.0)); } float coat_r2 = clamp(CoatRoughness, 0.0, 1.0); coat_r2 = coat_r2 * coat_r2; @@ -152,7 +152,7 @@ shader node_principled_bsdf(string distribution = "multi_ggx", } if (SheenWeight > CLOSURE_WEIGHT_CUTOFF) { - normal sheen_normal = normalize(mix(Normal, CoatNormal, coat_weight)); + normal sheen_normal = normalize(mix(Normal, CoatNormal, clamp(coat_weight, 0.0, 1.0))); closure color SheenBSDF = sheen(sheen_normal, clamp(SheenRoughness, 0.0, 1.0)); BSDF = layer(sheen_weight * max(SheenTint, color(0.0)) * SheenBSDF, BSDF); } diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index fe7d198f14e..cb3968b07c4 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -117,7 +117,7 @@ ccl_device float sheen_weight = max(stack_load_float(stack, sheen_weight_offset), 0.0f); float3 sheen_tint = max(stack_load_float3(stack, sheen_tint_offset), zero_float3()); float sheen_roughness = saturatef(stack_load_float(stack, sheen_roughness_offset)); - float coat_weight = saturatef(stack_load_float(stack, coat_weight_offset)); + float coat_weight = fmaxf(stack_load_float(stack, coat_weight_offset), 0.0f); float coat_roughness = saturatef(stack_load_float(stack, coat_roughness_offset)); float coat_ior = fmaxf(stack_load_float(stack, coat_ior_offset), 1.0f); float3 coat_tint = max(stack_load_float3(stack, coat_tint_offset), zero_float3()); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 44323599a91..f2bb4f373fc 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -133,7 +133,7 @@ void node_bsdf_principled(vec4 base_color, float coat_neta = 1.0 / coat_ior; float NT = fast_sqrt(1.0 - coat_neta * coat_neta * (1 - NV * NV)); /* Tint lower layers. */ - coat_tint.rgb = mix(vec3(1.0), pow(coat_tint.rgb, vec3(1.0 / NT)), coat_weight); + coat_tint.rgb = mix(vec3(1.0), pow(coat_tint.rgb, vec3(1.0 / NT)), saturate(coat_weight)); } } else { -- 2.30.2 From cf25219d430e3ac019107b97c1787723db51f07a Mon Sep 17 00:00:00 2001 From: Alaska Date: Fri, 20 Oct 2023 13:21:19 +1300 Subject: [PATCH 32/36] Apply layer weight clamping to EEVEE --- .../material/gpu_shader_material_principled.glsl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index f2bb4f373fc..86d1b3ee594 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -110,7 +110,7 @@ void node_bsdf_principled(vec4 base_color, vec3 sheen_color = sheen_weight * sheen_tint.rgb * principled_sheen(NV, sheen_roughness); diffuse_data.color = weight * sheen_color; /* Attenuate lower layers */ - weight *= (1.0 - max_v3(sheen_color)); + weight *= max((1.0 - max_v3(sheen_color)), 0.0); } else { diffuse_data.color = vec3(0.0); @@ -127,7 +127,7 @@ void node_bsdf_principled(vec4 base_color, float reflectance = bsdf_lut(coat_NV, coat_data.roughness, coat_ior, false).x; coat_data.weight = weight * coat_weight * reflectance; /* Attenuate lower layers */ - weight *= (1.0 - reflectance * coat_weight); + weight *= max((1.0 - reflectance * coat_weight), 0.0); if (!all(equal(coat_tint.rgb, vec3(1.0)))) { float coat_neta = 1.0 / coat_ior; @@ -158,7 +158,7 @@ void node_bsdf_principled(vec4 base_color, brdf_f82_tint_lut(F0, F82, NV, roughness, do_multiscatter != 0.0, metallic_brdf); reflection_data.color = weight * metallic * metallic_brdf; /* Attenuate lower layers */ - weight *= (1.0 - metallic); + weight *= max((1.0 - metallic), 0.0); } else { reflection_data.color = vec3(0.0); @@ -188,7 +188,7 @@ void node_bsdf_principled(vec4 base_color, refraction_data.weight = weight * transmission_weight; refraction_data.color = transmittance * coat_tint.rgb; /* Attenuate lower layers */ - weight *= (1.0 - transmission_weight); + weight *= max((1.0 - transmission_weight), 0.0); } else { refraction_data.weight = 0.0; @@ -215,7 +215,7 @@ void node_bsdf_principled(vec4 base_color, reflection_data.color += weight * reflectance; /* Attenuate lower layers */ - weight *= (1.0 - max_v3(reflectance)); + weight *= max((1.0 - max_v3(reflectance)), 0.0); } /* Diffuse component */ -- 2.30.2 From 086eece53eba8d067dd7d84da1993b4a084682cd Mon Sep 17 00:00:00 2001 From: Alaska Date: Fri, 20 Oct 2023 13:34:18 +1300 Subject: [PATCH 33/36] Clamp coat weight mix for sheen normal SVM --- intern/cycles/kernel/svm/closure.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index cb3968b07c4..c1e72e7b566 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -201,7 +201,7 @@ ccl_device sd, sizeof(SheenBsdf), sheen_weight * rgb_to_spectrum(sheen_tint) * weight); if (bsdf) { - bsdf->N = safe_normalize(mix(N, coat_normal, coat_weight)); + bsdf->N = safe_normalize(mix(N, coat_normal, saturatef(coat_weight))); bsdf->roughness = sheen_roughness; /* setup bsdf */ -- 2.30.2 From a05a4128402b351574c50e0a68950738359749db Mon Sep 17 00:00:00 2001 From: Alaska Date: Tue, 24 Oct 2023 00:51:35 +1300 Subject: [PATCH 34/36] Remove mix between clamped and non-clamped base colours --- .../kernel/osl/shaders/node_principled_bsdf.osl | 6 ------ intern/cycles/kernel/svm/closure.h | 6 ------ .../material/gpu_shader_material_principled.glsl | 13 +++++++------ 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 442e165bac4..777e0e9fba7 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -48,12 +48,6 @@ shader node_principled_bsdf(string distribution = "multi_ggx", float sheen_weight = max(SheenWeight, 0.0); color base_color = max(BaseColor, color(0.0)); color clamped_base_color = min(base_color, color(1.0)); - float clamped_color_weight = max(metallic, subsurface_weight); - if (clamped_color_weight > CLOSURE_WEIGHT_CUTOFF) { - /* Metallic and Subsurface Scattering materials behave unpredictably with values greater - * than 1.0. */ - base_color = mix(base_color, clamped_base_color, clamped_color_weight); - } float r2 = clamp(Roughness, 0.0, 1.0); r2 = r2 * r2; diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index c1e72e7b566..916a4663a1d 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -142,12 +142,6 @@ ccl_device __uint_as_float(data_base_color.w)); base_color = max(base_color, zero_float3()); const float3 clamped_base_color = min(base_color, one_float3()); - const float clamped_color_weight = max(metallic, subsurface_weight); - if (clamped_color_weight > CLOSURE_WEIGHT_CUTOFF) { - /* Metallic and Subsurface Scattering materials behave unpredictably with values greater - * than 1.0. */ - base_color = mix(base_color, clamped_base_color, clamped_color_weight); - } // get the subsurface scattering data uint4 data_subsurf = read_node(kg, &offset); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 86d1b3ee594..61cb8a9b387 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -83,11 +83,12 @@ void node_bsdf_principled(vec4 base_color, base_color = max(base_color, vec4(0.0)); vec4 clamped_base_color = min(base_color, vec4(1.0)); - float clamped_color_weight = max(metallic, subsurface_weight); - if (clamped_color_weight > 0.0) { - /* Metallic and Subsurface Scattering materials behave unpredictably with values greater - * than 1.0. */ - base_color = mix(base_color, clamped_base_color, clamped_color_weight); + + vec4 diffuse_sss_base_color = base_color; + if (subsurface_weight > 0.0) { + /* Subsurface Scattering materials behave unpredictably with values greater than 1.0 in Cycles. + * So it's clamped there and we clamp here for consistency with Cycles. */ + diffuse_sss_base_color = mix(diffuse_sss_base_color, clamped_base_color, subsurface_weight); } N = safe_normalize(N); @@ -222,7 +223,7 @@ void node_bsdf_principled(vec4 base_color, if (true) { diffuse_data.sss_radius = max(subsurface_radius * subsurface_scale, vec3(0.0)); diffuse_data.sss_id = uint(do_sss); - diffuse_data.color += weight * base_color.rgb * coat_tint.rgb; + diffuse_data.color += weight * diffuse_sss_base_color.rgb * coat_tint.rgb; } /* Adjust the weight of picking the closure. */ -- 2.30.2 From 32888eeb7d6403748aa4301f520a1268dd480843 Mon Sep 17 00:00:00 2001 From: Alaska Date: Tue, 24 Oct 2023 00:59:53 +1300 Subject: [PATCH 35/36] Clamp transmission color Turns out the transmission behaves unpredicatbly when they are rough and have high base colours --- .../kernel/osl/shaders/node_principled_bsdf.osl | 12 ++++++++++-- intern/cycles/kernel/svm/closure.h | 2 +- .../material/gpu_shader_material_principled.glsl | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl index 777e0e9fba7..25e46afa964 100644 --- a/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl +++ b/intern/cycles/kernel/osl/shaders/node_principled_bsdf.osl @@ -112,8 +112,16 @@ shader node_principled_bsdf(string distribution = "multi_ggx", color F0 = F0_from_ior(eta) * specular_tint; color F90 = color(1.0); - TransmissionBSDF = generalized_schlick_bsdf( - Normal, vector(0.0), color(1.0), sqrt(base_color), r2, r2, F0, F90, -eta, distribution), + TransmissionBSDF = generalized_schlick_bsdf(Normal, + vector(0.0), + color(1.0), + sqrt(clamped_base_color), + r2, + r2, + F0, + F90, + -eta, + distribution), BSDF = mix(BSDF, TransmissionBSDF, transmission); } diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 916a4663a1d..9688529cb21 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -317,7 +317,7 @@ ccl_device fresnel->f90 = one_spectrum(); fresnel->exponent = -ior; fresnel->reflection_tint = one_spectrum(); - fresnel->transmission_tint = sqrt(rgb_to_spectrum(base_color)); + fresnel->transmission_tint = sqrt(rgb_to_spectrum(clamped_base_color)); /* setup bsdf */ sd->flag |= bsdf_microfacet_ggx_glass_setup(bsdf); diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl index 61cb8a9b387..3af6ba4cdca 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_principled.glsl @@ -176,7 +176,7 @@ void node_bsdf_principled(vec4 base_color, vec3 reflectance, transmittance; bsdf_lut(F0, F90, - base_color.rgb, + clamped_base_color.rgb, NV, roughness, ior, -- 2.30.2 From 079c590f4c6c5138500b1eb1963dd157c7ee642b Mon Sep 17 00:00:00 2001 From: Alaska Date: Tue, 24 Oct 2023 01:00:02 +1300 Subject: [PATCH 36/36] Make format --- intern/cycles/kernel/svm/closure.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/intern/cycles/kernel/svm/closure.h b/intern/cycles/kernel/svm/closure.h index 9688529cb21..717b5826940 100644 --- a/intern/cycles/kernel/svm/closure.h +++ b/intern/cycles/kernel/svm/closure.h @@ -259,7 +259,8 @@ ccl_device * TIR is no concern here since we're always coming from the outside. */ float cosNT = sqrtf(1.0f - sqr(1.0f / coat_ior) * (1 - sqr(cosNI))); float optical_depth = 1.0f / cosNT; - weight *= mix(one_spectrum(), power(rgb_to_spectrum(coat_tint), optical_depth), coat_weight); + weight *= mix( + one_spectrum(), power(rgb_to_spectrum(coat_tint), optical_depth), coat_weight); } } -- 2.30.2