Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLSL: Add option to emulate alpha test #2412

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions spirv_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3563,6 +3563,12 @@ void CompilerGLSL::emit_resources()
fixup_implicit_builtin_block_names(execution.model);
break;

case ExecutionModelFragment:
if (options.fragment.emulate_alpha_test_func != Options::Always &&
options.fragment.emulate_alpha_test_func != Options::Never)
statement("uniform float SPIRV_Cross_AlphaTestRef;");
break;

default:
break;
}
Expand Down Expand Up @@ -16613,6 +16619,28 @@ void CompilerGLSL::emit_fixup()
if (options.vertex.flip_vert_y)
statement("gl_Position.y = -gl_Position.y;");
}
else if (get_entry_point().model == ExecutionModelFragment)
{
if (options.fragment.emulate_alpha_test_func == Options::Never)
{
statement("discard;");
}
else if (options.fragment.emulate_alpha_test_func != Options::Always)
{
auto *output_var = find_color_output_by_location(0);
if (output_var && this->get<SPIRType>(output_var->basetype).vecsize >= 4)
{
// We check for test fail, so we use the opposite operator
static const char *ops[Options::Always] = {
"", ">=", "!=", ">", "<=", "==", "<",
};
assert(options.fragment.emulate_alpha_test_func < Options::Always);
const char *op = ops[options.fragment.emulate_alpha_test_func];
statement("if (", to_expression(output_var->self), ".a ", op,
" SPIRV_Cross_AlphaTestRef) discard;");
}
}
}
}

void CompilerGLSL::flush_phi(BlockID from, BlockID to)
Expand Down
15 changes: 15 additions & 0 deletions spirv_glsl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ class CompilerGLSL : public Compiler
Highp
};

enum CompareFunc
{
Never,
Less,
Equal,
LessEqual,
Greater,
NotEqual,
GreaterEqual,
Always,
};

struct VertexOptions
{
// "Vertex-like shader" here is any shader stage that can write BuiltInPosition.
Expand All @@ -189,6 +201,9 @@ class CompilerGLSL : public Compiler
// Add precision highp int in ES targets when emitting GLES source.
Precision default_float_precision = Mediump;
Precision default_int_precision = Highp;

// If this is not Always, injects an alpha test for the output with location 0.
CompareFunc emulate_alpha_test_func = Always;
} fragment;
};

Expand Down
Loading