Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Replicate D3D's div by 0 behavior for RCP/RSQ (GLSL Edition)
  • Loading branch information
TheSpydog committed Dec 5, 2019
1 parent 2a78300 commit 135aa18
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions profiles/mojoshader_profile.h
Expand Up @@ -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.

Expand Down
8 changes: 6 additions & 2 deletions profiles/mojoshader_profile_glsl.c
Expand Up @@ -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

Expand Down Expand Up @@ -1199,15 +1201,17 @@ 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

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

Expand Down

0 comments on commit 135aa18

Please sign in to comment.