From c0243eb8122b34988831843a05d7ce9cb09921ed Mon Sep 17 00:00:00 2001 From: uis Date: Wed, 3 Jan 2024 20:50:17 +0300 Subject: [PATCH] Use saturated math and explicitly replace multiplication with negative value --- shader_glsl.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/shader_glsl.h b/shader_glsl.h index 10c4347b..8f205e0b 100644 --- a/shader_glsl.h +++ b/shader_glsl.h @@ -21,19 +21,23 @@ "# endif\n", "#endif\n", "\n", +"#define sat(x) clamp(x, 0, 1)\n", +"#define possat(x) sat(x)\n",//As replacement of max(x, 0) when x<=1, better for gpus that can't do 0-cycle max(x, 0) +"#define possatdot(x, y) possat(dot(x, y))\n", +"\n", "#ifdef USECELSHADING\n", -"# define SHADEDIFFUSE myhalf diffuse = cast_myhalf(min(max(float(dot(surfacenormal, lightnormal)) * 2.0, 0.0), 1.0));\n", +"# define SHADEDIFFUSE myhalf diffuse = cast_myhalf(sat(float(dot(surfacenormal, lightnormal)) * 2.0));\n", "# ifdef USEEXACTSPECULARMATH\n", -"# define SHADESPECULAR(specpow) myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), eyenormal))*-1.0, 0.0)), 1.0 + specpow);specular = max(0.0, specular * 10.0 - 9.0);\n", +"# define SHADESPECULAR(specpow) myhalf specular = pow(cast_myhalf(float(possatdot(reflect(lightnormal, surfacenormal), -eyenormal))), 1.0 + specpow);specular = possat(specular * 10.0 - 9.0);\n", "# else\n", -"# define SHADESPECULAR(specpow) myhalf3 specularnormal = normalize(lightnormal + eyenormal);myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + specpow);specular = max(0.0, specular * 10.0 - 9.0);\n", +"# define SHADESPECULAR(specpow) myhalf3 specularnormal = normalize(lightnormal + eyenormal);myhalf specular = pow(cast_myhalf(float(possatdot(surfacenormal, specularnormal))), 1.0 + specpow);specular = possat(specular * 10.0 - 9.0);\n", "# endif\n", "#else\n", -"# define SHADEDIFFUSE myhalf diffuse = cast_myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n", +"# define SHADEDIFFUSE myhalf diffuse = cast_myhalf(float(possatdot(surfacenormal, lightnormal)));\n", "# ifdef USEEXACTSPECULARMATH\n", -"# define SHADESPECULAR(specpow) myhalf specular = pow(cast_myhalf(max(float(dot(reflect(lightnormal, surfacenormal), eyenormal))*-1.0, 0.0)), 1.0 + specpow);\n", +"# define SHADESPECULAR(specpow) myhalf specular = pow(cast_myhalf(float(possatdot(reflect(lightnormal, surfacenormal), -eyenormal))), 1.0 + specpow);\n", "# else\n", -"# define SHADESPECULAR(specpow) myhalf3 specularnormal = normalize(lightnormal + eyenormal);myhalf specular = pow(cast_myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), 1.0 + specpow);\n", +"# define SHADESPECULAR(specpow) myhalf3 specularnormal = normalize(lightnormal + eyenormal);myhalf specular = pow(cast_myhalf(float(possatdot(surfacenormal, specularnormal))), 1.0 + specpow);\n", "# endif\n", "#endif\n", "\n", -- 2.39.2