From 135aa1838ed57f41e2818f75a79326786d2d72a2 Mon Sep 17 00:00:00 2001 From: Caleb Cornett Date: Thu, 5 Dec 2019 12:11:01 -0500 Subject: [PATCH] Replicate D3D's div by 0 behavior for RCP/RSQ (GLSL Edition) --- profiles/mojoshader_profile.h | 1 + profiles/mojoshader_profile_glsl.c | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/profiles/mojoshader_profile.h b/profiles/mojoshader_profile.h index 34bcd6b3..4b13b05a 100644 --- a/profiles/mojoshader_profile.h +++ b/profiles/mojoshader_profile.h @@ -160,6 +160,7 @@ typedef struct Context int glsl_generated_lit_helper; int glsl_generated_texlod_setup; int glsl_generated_texm3x3spec_helper; + int glsl_need_max_float; int arb1_wrote_position; // !!! FIXME: move these into SUPPORT_PROFILE sections. diff --git a/profiles/mojoshader_profile_glsl.c b/profiles/mojoshader_profile_glsl.c index d9336a8d..6ea81de3 100644 --- a/profiles/mojoshader_profile_glsl.c +++ b/profiles/mojoshader_profile_glsl.c @@ -641,6 +641,8 @@ void emit_GLSL_finalize(Context *ctx) if (shader_is_vertex(ctx)) output_line(ctx, "uniform float vpFlip;"); #endif + if (ctx->glsl_need_max_float) + output_line(ctx, "const float FLT_MAX = 1e38;"); pop_output(ctx); } // emit_GLSL_finalize @@ -1199,7 +1201,8 @@ void emit_GLSL_RCP(Context *ctx) { char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); char code[128]; - make_GLSL_destarg_assign(ctx, code, sizeof (code), "1.0 / %s", src0); + ctx->glsl_need_max_float = 1; + make_GLSL_destarg_assign(ctx, code, sizeof (code), "(%s == 0.0) ? FLT_MAX : 1.0 / %s", src0, src0); output_line(ctx, "%s", code); } // emit_GLSL_RCP @@ -1207,7 +1210,8 @@ void emit_GLSL_RSQ(Context *ctx) { char src0[64]; make_GLSL_srcarg_string_masked(ctx, 0, src0, sizeof (src0)); char code[128]; - make_GLSL_destarg_assign(ctx, code, sizeof (code), "inversesqrt(%s)", src0); + ctx->glsl_need_max_float = 1; + make_GLSL_destarg_assign(ctx, code, sizeof (code), "(%s == 0.0) ? FLT_MAX : inversesqrt(abs(%s))", src0, src0); output_line(ctx, "%s", code); } // emit_GLSL_RSQ