Shader: Only clamp undefined or unsupported inputs of Principled BSDF #112895

Merged
Lukas Stockner merged 39 commits from Alaska/blender:clamp-tint into blender-v4.0-release 2023-10-31 03:14:14 +01:00
3 changed files with 13 additions and 6 deletions
Showing only changes of commit b46aff1f7e - Show all commits

View File

@ -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;
Alaska marked this conversation as resolved Outdated

As above - having all the clamping together at the start makes it more maintainable (except for cases where the same parameter gets clamped differently depending on context).

As above - having all the clamping together at the start makes it more maintainable (except for cases where the same parameter gets clamped differently depending on context).
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);
Alaska marked this conversation as resolved Outdated

Shouldn't this be [0..inf]?

Shouldn't this be [0..inf]?

Clamping to [0..infinity] is handled by if SheenWeight > CLOSURE_WEIGHT_CUTOFF

However I have added back explicit clamping for code clarity.

Clamping to [0..infinity] is handled by `if SheenWeight > CLOSURE_WEIGHT_CUTOFF` However I have added back explicit clamping for code clarity.
}

View File

@ -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);
Alaska marked this conversation as resolved Outdated

Might as well do it explicitly here I guess.

Might as well do it explicitly here I guess.
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));
Alaska marked this conversation as resolved Outdated

I'm not sure where?

I'm not sure where?

Sheen weight is clamped to [0..infinity] with if (sheen_weight > CLOSURE_WEIGHT_CUTOFF) later on.

However I re-added the full clamping for code clarity.

Sheen weight is clamped to [0..infinity] with `if (sheen_weight > CLOSURE_WEIGHT_CUTOFF)` later on. However I re-added the full clamping for code clarity.
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);

View File

@ -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);