GOOENGINE: Fix double include issues

Safeguards various functions that double include as a result of the #pragma BLENDER_REQUIRE workaround
This commit is contained in:
2025-12-01 22:13:25 -06:00
parent 719d50aad7
commit 3dc6c87740
20 changed files with 51 additions and 43 deletions
@@ -407,7 +407,7 @@ float specular_occlusion(
occlusion_eval(data, V, N, N, 0.0, visibility, visibility_error, visibility_dir);
/* Correct visibility error for very sharp surfaces. */
visibility *= mix(goo_safe_rcp(visibility_error), 1.0, roughness);
visibility *= mix(safe_rcp(visibility_error), 1.0, roughness);
specular_dir = normalize(mix(specular_dir, visibility_dir, roughness * (1.0 - visibility)));
@@ -168,11 +168,11 @@ vec3 ensure_valid_specular_reflection(vec3 Ng, vec3 I, vec3 N)
float b = 2.0 * (a + Iz * threshold);
float c = sqr(threshold + Iz);
float Nz2 = (Ix < 0.0) ? 0.25 * (b + goo_safe_sqrt(sqr(b) - 4.0 * a * c)) / a :
0.25 * (b - goo_safe_sqrt(sqr(b) - 4.0 * a * c)) / a;
float Nz2 = (Ix < 0.0) ? 0.25 * (b + safe_sqrt(sqr(b) - 4.0 * a * c)) / a :
0.25 * (b - safe_sqrt(sqr(b) - 4.0 * a * c)) / a;
float Nx = goo_safe_sqrt(1.0 - Nz2);
float Nz = goo_safe_sqrt(Nz2);
float Nx = safe_sqrt(1.0 - Nz2);
float Nz = safe_sqrt(Nz2);
return Nx * X + Nz * Ng;
}
@@ -82,7 +82,7 @@ void main()
}
}
else {
uv *= goo_safe_rcp(length(uv));
uv *= safe_rcp(length(uv));
}
/* For gather store the normalized UV. */
@@ -28,7 +28,7 @@ void main()
vec4 weights = dof_downsample_bilateral_coc_weights(cocs);
/* Normalize so that the sum is 1. */
weights *= goo_safe_rcp(sum(weights));
weights *= safe_rcp(sum(weights));
outColor = weighted_sum_array(colors, weights);
}
@@ -581,7 +581,7 @@ void dof_gather_accumulate_resolve(int total_sample_count,
out float out_weight,
out vec2 out_occlusion)
{
float weight_inv = goo_safe_rcp(accum_data.weight);
float weight_inv = safe_rcp(accum_data.weight);
out_col = accum_data.color * weight_inv;
out_occlusion = vec2(abs(accum_data.coc), accum_data.coc_sqr) * weight_inv;
@@ -97,7 +97,7 @@ vec3 dof_neighborhood_clamping(vec3 color)
float clamped_luma = min(upper_bound, c11);
return color * clamped_luma * goo_safe_rcp(c11);
return color * clamped_luma * safe_rcp(c11);
}
/* Simple copy pass where we select what pixels to scatter. Also the resolution might change.
@@ -152,7 +152,7 @@ void main()
vec4 weights = dof_downsample_bilateral_coc_weights(cocs);
weights *= dof_downsample_bilateral_color_weights(colors);
/* Normalize so that the sum is 1. */
weights *= goo_safe_rcp(sum(weights));
weights *= safe_rcp(sum(weights));
outColor = weighted_sum_array(colors, weights);
outCoc = dot(cocs, weights);
@@ -142,20 +142,20 @@ void main(void)
if (!no_holefill_pass && prediction.do_holefill) {
dof_resolve_load_layer(holefillColorBuffer, holefillWeightBuffer, layer_color, layer_weight);
FragColor = layer_color * goo_safe_rcp(layer_weight);
FragColor = layer_color * safe_rcp(layer_weight);
weight = float(layer_weight > 0.0);
}
if (!no_background_pass && prediction.do_background) {
dof_resolve_load_layer(bgColorBuffer, bgWeightBuffer, layer_color, layer_weight);
/* Always prefer background to holefill pass. */
layer_color *= goo_safe_rcp(layer_weight);
layer_color *= safe_rcp(layer_weight);
layer_weight = float(layer_weight > 0.0);
/* Composite background. */
FragColor = FragColor * (1.0 - layer_weight) + layer_color;
weight = weight * (1.0 - layer_weight) + layer_weight;
/* Fill holes with the composited background. */
FragColor *= goo_safe_rcp(weight);
FragColor *= safe_rcp(weight);
weight = float(weight > 0.0);
}
@@ -57,7 +57,7 @@ void main(void)
* using a VSM like chebychev test (slide 85). */
float mean = occlusion_data.x;
float variance = occlusion_data.y;
shapes *= variance * goo_safe_rcp(variance + sqr(max(cocs * correction_fac - mean, 0.0)));
shapes *= variance * safe_rcp(variance + sqr(max(cocs * correction_fac - mean, 0.0)));
}
FragColor = color1 * shapes.x;
@@ -33,7 +33,7 @@ void main()
vec4 weights = dof_downsample_bilateral_coc_weights(cocs);
weights *= dof_downsample_bilateral_color_weights(colors);
/* Normalize so that the sum is 1. */
weights *= goo_safe_rcp(sum(weights));
weights *= safe_rcp(sum(weights));
outColor = weighted_sum_array(colors, weights);
outCoc.x = dot(cocs, weights);
@@ -53,7 +53,7 @@ vec4 ssr_get_scene_color_and_mask(vec3 hit_vP, int planar_index, float mip)
/* Clamped brightness. */
float luma = max_v3(color);
color *= 1.0 - max(0.0, luma - ssrFireflyFac) * goo_safe_rcp(luma);
color *= 1.0 - max(0.0, luma - ssrFireflyFac) * safe_rcp(luma);
float mask = screen_border_mask(uv);
return vec4(color, mask);
@@ -234,7 +234,7 @@ void raytrace_resolve(ClosureInputGlossy cl_in,
}
/* Compute SSR contribution */
ssr_accum *= goo_safe_rcp(weight_acc);
ssr_accum *= safe_rcp(weight_acc);
/* fade between 0.5 and 1.0 roughness */
ssr_accum.a *= smoothstep(ssrMaxRoughness + 0.2, ssrMaxRoughness, roughness);
@@ -136,7 +136,7 @@ void main()
else {
data.is_hit = raytrace(ray, params, true, false, hit_sP);
}
data.ray_pdf_inv = goo_safe_rcp(pdf);
data.ray_pdf_inv = safe_rcp(pdf);
encode_hit_data(data, hit_sP, ray.origin, hitData, hitDepth);
}
@@ -354,7 +354,7 @@ float light_contact_shadows(LightData ld, vec3 P, vec3 vP, vec3 vNg, float rand_
}
else {
ray.direction = shadows_cube_data[int(sd.sh_data_index)].position.xyz - P;
ray.direction *= saturate(sd.sh_contact_dist * goo_safe_rcp(length(ray.direction)));
ray.direction *= saturate(sd.sh_contact_dist * safe_rcp(length(ray.direction)));
}
ray.direction = transform_direction(ViewMatrix, ray.direction);
@@ -61,7 +61,7 @@ void raytrace_screenspace_ray_finalize(inout ScreenSpaceRay ray)
ray.direction /= (is_more_vertical) ? abs(ray.direction.y) : abs(ray.direction.x);
ray.direction *= (is_more_vertical) ? ssrPixelSize.y : ssrPixelSize.x;
/* Clip to segment's end. */
ray.max_time = sqrt(ray_len_sqr * goo_safe_rcp(len_squared(ray.direction.xyz)));
ray.max_time = sqrt(ray_len_sqr * safe_rcp(len_squared(ray.direction.xyz)));
/* Clipping to frustum sides. */
float clip_dist = line_unit_box_intersect_dist_safe(ray.origin.xyz, ray.direction.xyz);
ray.max_time = min(ray.max_time, clip_dist);
@@ -142,7 +142,7 @@ void main()
}
#ifndef USE_ALPHA_BLEND
float alpha_div = goo_safe_rcp(alpha);
float alpha_div = safe_rcp(alpha);
outRadiance.rgb *= alpha_div;
ssrData.rgb *= alpha_div;
sssAlbedo.rgb *= alpha_div;
@@ -172,7 +172,7 @@ vec4 attr_load_color_post(vec4 attr)
{
#ifdef MESH_SHADER
/* Density is premultiplied for interpolation, divide it out here. */
attr.rgb *= goo_safe_rcp(attr.a);
attr.rgb *= safe_rcp(attr.a);
attr.rgb *= drw_volume.color_mul.rgb;
attr.a = 1.0;
#endif
@@ -113,14 +113,14 @@ float avg(vec4 v) { return dot(vec4(1.0 / 4.0), v); }
/* WORKAROUND: To be removed once we port all code to use gpu_shader_math_base_lib.glsl. */
#ifndef GPU_SHADER_MATH_BASE_LIB_GLSL
float goo_safe_rcp(float a) { return (a != 0.0) ? (1.0 / a) : 0.0; }
float goo_safe_sqrt(float a) { return sqrt(max(a, 0.0)); }
float goo_safe_acos(float a) { return acos(clamp(a, -1.0, 1.0)); }
float safe_rcp(float a) { return (a != 0.0) ? (1.0 / a) : 0.0; }
float safe_sqrt(float a) { return sqrt(max(a, 0.0)); }
float safe_acos(float a) { return acos(clamp(a, -1.0, 1.0)); }
#endif
#ifndef GPU_SHADER_MATH_VECTOR_LIB_GLSL
vec2 goo_safe_rcp(vec2 a) { return select(vec2(0.0), (1.0 / a), notEqual(a, vec2(0.0))); }
vec3 goo_safe_rcp(vec3 a) { return select(vec3(0.0), (1.0 / a), notEqual(a, vec3(0.0))); }
vec4 goo_safe_rcp(vec4 a) { return select(vec4(0.0), (1.0 / a), notEqual(a, vec4(0.0))); }
vec2 safe_rcp(vec2 a) { return select(vec2(0.0), (1.0 / a), notEqual(a, vec2(0.0))); }
vec3 safe_rcp(vec3 a) { return select(vec3(0.0), (1.0 / a), notEqual(a, vec3(0.0))); }
vec4 safe_rcp(vec4 a) { return select(vec4(0.0), (1.0 / a), notEqual(a, vec4(0.0))); }
#endif
float sqr(float a) { return a * a; }
@@ -145,8 +145,8 @@ bool flag_test(int flag, int val) { return (flag & val) != 0; }
void set_flag_from_test(inout uint value, bool test, uint flag) { if (test) { value |= flag; } else { value &= ~flag; } }
void set_flag_from_test(inout int value, bool test, int flag) { if (test) { value |= flag; } else { value &= ~flag; } }
#define weighted_sum(val0, val1, val2, val3, weights) ((val0 * weights[0] + val1 * weights[1] + val2 * weights[2] + val3 * weights[3]) * goo_safe_rcp(sum(weights)))
#define weighted_sum_array(val, weights) ((val[0] * weights[0] + val[1] * weights[1] + val[2] * weights[2] + val[3] * weights[3]) * goo_safe_rcp(sum(weights)))
#define weighted_sum(val0, val1, val2, val3, weights) ((val0 * weights[0] + val1 * weights[1] + val2 * weights[2] + val3 * weights[3]) * safe_rcp(sum(weights)))
#define weighted_sum_array(val, weights) ((val[0] * weights[0] + val[1] * weights[1] + val[2] * weights[2] + val[3] * weights[3]) * safe_rcp(sum(weights)))
#endif
/* clang-format on */
@@ -153,11 +153,12 @@ void min_max(float value, inout float min_v, inout float max_v)
max_v = max(value, max_v);
}
#ifndef GOO_COMMON_MATH_LIB_GLSL
/**
* Safe divide `a` by `b`.
* If `b` equal 0 the result will be 0.
*/
float safe_divide_new(float a, float b)
float safe_divide(float a, float b)
{
return (b != 0.0) ? (a / b) : 0.0;
}
@@ -195,6 +196,7 @@ float safe_acos(float a)
}
return acos(a);
}
#endif
/**
* Return true if the difference between`a` and `b` is below the `epsilon` value.
@@ -1291,6 +1291,7 @@ void to_loc_rot_scale(mat4x4 mat,
to_rot_scale(to_float3x3(mat), r_rotation, r_scale, allow_negative_scale);
}
#ifndef GOO_COMMON_MATH_LIB_GLSL
vec3 transform_point(mat3x3 mat, vec3 point)
{
return mat * point;
@@ -1323,6 +1324,7 @@ vec3 project_point(mat4x4 mat, vec3 point)
/* Absolute value to not flip the frustum upside down behind the camera. */
return tmp.xyz / abs(tmp.w);
}
#endif
mat4x4 interpolate_fast(mat4x4 a, mat4x4 b, float t)
{
@@ -80,12 +80,12 @@ void min_max(vec4 vector, inout vec4 min, inout vec4 max);
* Safe divide `a` by `b`.
* If `b` equal 0 the result will be 0.
*/
vec2 safe_divide_new(vec2 a, vec2 b);
vec3 safe_divide_new(vec3 a, vec3 b);
vec4 safe_divide_new(vec4 a, vec4 b);
vec2 safe_divide_new(vec2 a, float b);
vec3 safe_divide_new(vec3 a, float b);
vec4 safe_divide_new(vec4 a, float b);
vec2 safe_divide(vec2 a, vec2 b);
vec3 safe_divide(vec3 a, vec3 b);
vec4 safe_divide(vec4 a, vec4 b);
vec2 safe_divide(vec2 a, float b);
vec3 safe_divide(vec3 a, float b);
vec4 safe_divide(vec4 a, float b);
/**
* Return the manhattan length of `a`.
@@ -388,28 +388,28 @@ void min_max(vec4 vector, inout vec4 min_v, inout vec4 max_v)
max_v = max(vector, max_v);
}
vec2 safe_divide_new(vec2 a, vec2 b)
vec2 safe_divide(vec2 a, vec2 b)
{
return select(vec2(0), a / b, notEqual(b, vec2(0)));
}
vec3 safe_divide_new(vec3 a, vec3 b)
vec3 safe_divide(vec3 a, vec3 b)
{
return select(vec3(0), a / b, notEqual(b, vec3(0)));
}
vec4 safe_divide_new(vec4 a, vec4 b)
vec4 safe_divide(vec4 a, vec4 b)
{
return select(vec4(0), a / b, notEqual(b, vec4(0)));
}
vec2 safe_divide_new(vec2 a, float b)
vec2 safe_divide(vec2 a, float b)
{
return (b != 0.0) ? (a / b) : vec2(0);
}
vec3 safe_divide_new(vec3 a, float b)
vec3 safe_divide(vec3 a, float b)
{
return (b != 0.0) ? (a / b) : vec3(0);
}
vec4 safe_divide_new(vec4 a, float b)
vec4 safe_divide(vec4 a, float b)
{
return (b != 0.0) ? (a / b) : vec4(0);
}
@@ -548,6 +548,7 @@ vec4 safe_normalize_and_get_length(vec4 vector, out float out_length)
return vec4(1.0, 0.0, 0.0, 0.0);
}
#ifndef GOO_COMMON_MATH_LIB_GLSL
vec2 safe_normalize(vec2 vector)
{
float unused_length = 0.0;
@@ -576,6 +577,7 @@ vec4 safe_rcp(vec4 a)
{
return select(vec4(0.0), (1.0 / a), notEqual(a, vec4(0.0)));
}
#endif
vec2 interpolate(vec2 a, vec2 b, float t)
{
@@ -43,6 +43,7 @@
#define in_texture_range(texel, tex) (all(greaterThanEqual(texel, ivec2(0))) && all(lessThan(texel, textureSize(tex, 0).xy)))
#define in_image_range(texel, tex) (all(greaterThanEqual(texel, ivec2(0))) && all(lessThan(texel, imageSize(tex).xy)))
#ifndef GOO_COMMON_MATH_LIB_GLSL
#define weighted_sum(val0, val1, val2, val3, weights) ((val0 * weights[0] + val1 * weights[1] + val2 * weights[2] + val3 * weights[3]) * safe_rcp(weights[0] + weights[1] + weights[2] + weights[3]))
#define weighted_sum_array(val, weights) ((val[0] * weights[0] + val[1] * weights[1] + val[2] * weights[2] + val[3] * weights[3]) * safe_rcp(weights[0] + weights[1] + weights[2] + weights[3]))
/* clang-format on */
@@ -78,6 +79,7 @@ void set_flag_from_test(inout int value, bool test, int flag)
value &= ~flag;
}
}
#endif
/* Keep define to match C++ implementation. */
# define SET_FLAG_FROM_TEST(value, test, flag) set_flag_from_test(value, test, flag)