diff --git a/source/blender/gpu/shaders/common/gpu_shader_common_math_utils.glsl b/source/blender/gpu/shaders/common/gpu_shader_common_math_utils.glsl index 29d5e6c928c..a60915e1079 100644 --- a/source/blender/gpu/shaders/common/gpu_shader_common_math_utils.glsl +++ b/source/blender/gpu/shaders/common/gpu_shader_common_math_utils.glsl @@ -8,6 +8,10 @@ #include "gpu_glsl_cpp_stubs.hh" +/* WORKAROUND: to guard against double include in EEVEE. */ +#ifndef GPU_SHADER_MATH_COMMON_UTILS_GLSL +# define GPU_SHADER_MATH_COMMON_UTILS_GLSL + /* WORKAROUND: To be removed once we port all code to use `gpu_shader_math_base_lib.glsl`. */ #ifndef GPU_SHADER_MATH_BASE_LIB_GLSL @@ -97,6 +101,16 @@ vec3 wrap(vec3 a, vec3 b, vec3 c) return vec3(wrap(a.x, b.x, c.x), wrap(a.y, b.y, c.y), wrap(a.z, b.z, c.z)); } +/* 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 hypot(float x, float y) +{ + return sqrt(x * x + y * y); +} + +#endif + int floor_to_int(float x) { return int(floor(x)); @@ -207,3 +221,5 @@ mat3 euler_to_mat3(vec3 euler) mat[2][2] = cy * cx; return mat; } + +#endif /* GPU_SHADER_MATH_COMMON_UTILS_GLSL */ diff --git a/source/blender/gpu/shaders/common/gpu_shader_math_base_lib.glsl b/source/blender/gpu/shaders/common/gpu_shader_math_base_lib.glsl index 14102417401..12cf41e0dae 100644 --- a/source/blender/gpu/shaders/common/gpu_shader_math_base_lib.glsl +++ b/source/blender/gpu/shaders/common/gpu_shader_math_base_lib.glsl @@ -94,11 +94,16 @@ float cube_f(float v) return v * v * v; } +/* WORKAROUND: To be removed once we port all code to use gpu_shader_math_base_lib.glsl. */ +#ifndef GPU_SHADER_MATH_COMMON_UTILS_GLSL + float hypot(float x, float y) { return sqrt(x * x + y * y); } +#endif + /* Declared as _atan2 to prevent errors with `WITH_GPU_SHADER_CPP_COMPILATION` on VS2019 due * to `corecrt_math` conflicting functions. */ diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_sdf_primitive.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_sdf_primitive.glsl index bc308a0c6e5..aeccdd609eb 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_sdf_primitive.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_sdf_primitive.glsl @@ -513,7 +513,7 @@ float sdf_2d_star_x(vec2 p, float r, float sides, float inset) vec2 acs = vec2(cos(an), sin(an)); vec2 ecs = vec2(cos(en), sin(en)); // ecs=vec2(0,1) for regular polygon, - float bn = safe_mod(atan(p.x, p.y), 2.0 * an) - an; + float bn = sdf_safe_mod(atan(p.x, p.y), 2.0 * an) - an; p = length(p) * vec2(cos(bn), abs(sin(bn))); p -= r * acs; p += ecs * clamp(-dot(p, ecs), 0.0, safe_divide(r * acs.y, ecs.y)); @@ -557,7 +557,7 @@ float sdf_2d_star(vec2 p, float r, float sides, float inset, float inradius) // vec2 ecs = vec2(cos(en), sin(en)); // ecs=vec2(0,1) and simplify, for regular polygon, // reduce to first sector - float bn = safe_mod(atan(p.x, p.y) * sign(p.x), 2.0 * an) - an; + float bn = sdf_safe_mod(atan(p.x, p.y) * sign(p.x), 2.0 * an) - an; p = length(p) * vec2(cos(bn), abs(sin(bn))); // line sdf diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_sdf_util.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_sdf_util.glsl index c640507979e..e2360f16473 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_sdf_util.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_sdf_util.glsl @@ -28,31 +28,31 @@ float sdf_safe_sqrt(float a) return sqrt(max(0.0, a)); } -float safe_mod(float a, float b) +float sdf_safe_mod(float a, float b) { return (b != 0.0) ? a - b * floor(a / b) : 0.0; } -vec3 safe_mod(vec3 a, float b) +vec3 sdf_safe_mod(vec3 a, float b) { - a.x = safe_mod(a.x, b); - a.y = safe_mod(a.y, b); - a.z = safe_mod(a.z, b); + a.x = sdf_safe_mod(a.x, b); + a.y = sdf_safe_mod(a.y, b); + a.z = sdf_safe_mod(a.z, b); return a; } -vec3 safe_mod(vec3 a, vec3 b) +vec3 sdf_safe_mod(vec3 a, vec3 b) { - a.x = safe_mod(a.x, b.x); - a.y = safe_mod(a.y, b.y); - a.z = safe_mod(a.z, b.z); + a.x = sdf_safe_mod(a.x, b.x); + a.y = sdf_safe_mod(a.y, b.y); + a.z = sdf_safe_mod(a.z, b.z); return a; } -vec2 safe_mod(vec2 a, vec2 b) +vec2 sdf_safe_mod(vec2 a, vec2 b) { - a.x = safe_mod(a.x, b.x); - a.y = safe_mod(a.y, b.y); + a.x = sdf_safe_mod(a.x, b.x); + a.y = sdf_safe_mod(a.y, b.y); return a; } @@ -170,7 +170,7 @@ float p_mod1(inout float p, float size) { float halfsize = size * 0.5; float c = floor((p + halfsize) / size); - p = safe_mod(p + halfsize, size) - halfsize; + p = sdf_safe_mod(p + halfsize, size) - halfsize; return c; } @@ -185,16 +185,16 @@ vec3 p_mod_mirror3(inout vec3 p, vec3 size) { vec3 halfsize = size * 0.5; vec3 c = floor(safe_divide((p + halfsize), size)); - p = safe_mod(p + halfsize, size) - halfsize; - p = p * (safe_mod(c, vec3(2.0)) * 2.0 - vec3(1.0)); + p = sdf_safe_mod(p + halfsize, size) - halfsize; + p = p * (sdf_safe_mod(c, vec3(2.0)) * 2.0 - vec3(1.0)); return c; } vec2 p_mod_grid2(inout vec2 p, vec2 size) { vec2 c = floor(safe_divide((p + size * 0.5), size)); - p = safe_mod(p + size * 0.5, size) - size * 0.5; - p = p * (safe_mod(c, vec2(2.0)) * 2.0 - vec2(1.0)); + p = sdf_safe_mod(p + size * 0.5, size) - size * 0.5; + p = p * (sdf_safe_mod(c, vec2(2.0)) * 2.0 - vec2(1.0)); p -= size / 2.0; if (p.x > p.y) { p.xy = p.yx; @@ -283,7 +283,7 @@ float sdf_op_union_columns(float a, float b, float r, float n) p = rotate_45(p); p.x -= M_SQRT2 / 2.0 * r; p.x += columnradius * M_SQRT2; - if (safe_mod(n, 2.0) == 1.0) { + if (sdf_safe_mod(n, 2.0) == 1.0) { p.y += columnradius; } // At this point, we have turned 45 degrees and moved at a point on the @@ -313,7 +313,7 @@ float sdf_op_diff_columns(float a, float b, float r, float n) p.x -= M_SQRT2 / 2.0 * r; p.x += -columnradius * M_SQRT2 / 2.0; - if (safe_mod(n, 2.0) == 1.0) { + if (sdf_safe_mod(n, 2.0) == 1.0) { p.y += columnradius; } p_mod1(p.y, columnradius * 2.0); @@ -404,7 +404,7 @@ float sdf_op_union_stairs(float a, float b, float r, float n) { float s = r / n; float u = b - r; - return min(min(a, b), 0.5 * (u + a + abs((safe_mod(u - a + s, 2.0 * s)) - s))); + return min(min(a, b), 0.5 * (u + a + abs((sdf_safe_mod(u - a + s, 2.0 * s)) - s))); } float sdf_op_intersect_stairs(float a, float b, float r, float n) @@ -480,7 +480,7 @@ float sdf_op_polar(inout vec2 p, float repetitions) float a = atan(p.y, p.x) + angle / 2.0; float r = length(p); float c = floor(a / angle); - a = safe_mod(a, angle) - angle / 2.0; + a = sdf_safe_mod(a, angle) - angle / 2.0; p = vec2(cos(a), sin(a)) * r; // For an odd number of repetitions, fix cell index of the cell in -x direction // (cell index would be e.g. -5 and 5 in the two halves of the cell): diff --git a/source/blender/gpu/shaders/material/gpu_shader_material_sdf_vector_op.glsl b/source/blender/gpu/shaders/material/gpu_shader_material_sdf_vector_op.glsl index 1c231be1b58..85d6200c356 100644 --- a/source/blender/gpu/shaders/material/gpu_shader_material_sdf_vector_op.glsl +++ b/source/blender/gpu/shaders/material/gpu_shader_material_sdf_vector_op.glsl @@ -336,7 +336,7 @@ void node_sdf_vector_op_repeat_inf(vec3 p, out vec3 pos, out float d) { - vout = safe_mod(p + 0.5 * p2, p2) - 0.5 * p2; + vout = sdf_safe_mod(p + 0.5 * p2, p2) - 0.5 * p2; } void node_sdf_vector_op_repeat(vec3 p,