"# 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",