Skip to content

Commit 9e8a3b6

Browse files
committed
GLSL: Add option to emulate alpha test
1 parent a018347 commit 9e8a3b6

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

spirv_glsl.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -3563,6 +3563,11 @@ void CompilerGLSL::emit_resources()
35633563
fixup_implicit_builtin_block_names(execution.model);
35643564
break;
35653565

3566+
case ExecutionModelFragment:
3567+
if (options.fragment.emulate_alpha_test_func)
3568+
statement("uniform float SPIRV_Cross_AlphaTestRef;");
3569+
break;
3570+
35663571
default:
35673572
break;
35683573
}
@@ -16613,6 +16618,28 @@ void CompilerGLSL::emit_fixup()
1661316618
if (options.vertex.flip_vert_y)
1661416619
statement("gl_Position.y = -gl_Position.y;");
1661516620
}
16621+
else if (get_entry_point().model == ExecutionModelFragment)
16622+
{
16623+
if (options.fragment.emulate_alpha_test_func == Options::Never)
16624+
{
16625+
statement("discard;");
16626+
}
16627+
else if (options.fragment.emulate_alpha_test_func != Options::Always)
16628+
{
16629+
auto *output_var = find_color_output_by_location(0);
16630+
if (output_var && this->get<SPIRType>(output_var->basetype).vecsize >= 4)
16631+
{
16632+
// We check for test fail, so we use the opposite operator
16633+
static const char *ops[Options::Always] = {
16634+
"", ">=", "!=", ">", "<=", "==", "<",
16635+
};
16636+
assert(options.fragment.emulate_alpha_test_func < Options::Always);
16637+
const char *op = ops[options.fragment.emulate_alpha_test_func];
16638+
statement("if (", to_expression(output_var->self), ".a ", op,
16639+
" SPIRV_Cross_AlphaTestRef) discard;");
16640+
}
16641+
}
16642+
}
1661616643
}
1661716644

1661816645
void CompilerGLSL::flush_phi(BlockID from, BlockID to)

spirv_glsl.hpp

+15
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,18 @@ class CompilerGLSL : public Compiler
164164
Highp
165165
};
166166

167+
enum CompareFunc
168+
{
169+
Never,
170+
Less,
171+
Equal,
172+
LessEqual,
173+
Greater,
174+
NotEqual,
175+
GreaterEqual,
176+
Always,
177+
};
178+
167179
struct VertexOptions
168180
{
169181
// "Vertex-like shader" here is any shader stage that can write BuiltInPosition.
@@ -189,6 +201,9 @@ class CompilerGLSL : public Compiler
189201
// Add precision highp int in ES targets when emitting GLES source.
190202
Precision default_float_precision = Mediump;
191203
Precision default_int_precision = Highp;
204+
205+
// If this is not Always, injects an alpha test for the output with location 0.
206+
CompareFunc emulate_alpha_test_func = Always;
192207
} fragment;
193208
};
194209

0 commit comments

Comments
 (0)