GLSL: fix Position built-in variable.

Position maps to gl_Position in vertex shaders, and gl_FragCoord in
fragment shaders.

Bug: tint:1296
Change-Id: I22069af2c82bb03521f2721d16c29ba98c85f3f2
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/69280
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
Commit-Queue: Stephen White <senorblanco@chromium.org>
This commit is contained in:
Stephen White 2021-11-15 13:33:58 +00:00 committed by Tint LUCI CQ
parent 4cfa725fb3
commit e1761d5852
4 changed files with 20 additions and 9 deletions

View File

@ -1758,10 +1758,20 @@ sem::Type* GeneratorImpl::builtin_type(ast::Builtin builtin) {
} }
} }
const char* GeneratorImpl::builtin_to_string(ast::Builtin builtin) const { const char* GeneratorImpl::builtin_to_string(ast::Builtin builtin,
ast::PipelineStage stage) {
switch (builtin) { switch (builtin) {
case ast::Builtin::kPosition: case ast::Builtin::kPosition:
switch (stage) {
case ast::PipelineStage::kVertex:
return "gl_Position"; return "gl_Position";
case ast::PipelineStage::kFragment:
return "gl_FragCoord";
default:
TINT_ICE(Writer, builder_.Diagnostics())
<< "position builtin unexpected in this pipeline stage";
return "";
}
case ast::Builtin::kVertexIndex: case ast::Builtin::kVertexIndex:
return "gl_VertexID"; return "gl_VertexID";
case ast::Builtin::kInstanceIndex: case ast::Builtin::kInstanceIndex:
@ -1959,10 +1969,10 @@ bool GeneratorImpl::EmitEntryPointFunction(const ast::Function* func) {
return false; return false;
} }
out << "("; out << "(";
out << builtin_to_string(builtin->builtin); out << builtin_to_string(builtin->builtin, func->PipelineStage());
out << ")"; out << ")";
} else { } else {
out << builtin_to_string(builtin->builtin); out << builtin_to_string(builtin->builtin, func->PipelineStage());
} }
} else { } else {
out << name; out << name;
@ -1992,7 +2002,7 @@ bool GeneratorImpl::EmitEntryPointFunction(const ast::Function* func) {
out << " "; out << " ";
if (auto* builtin = ast::GetDecoration<ast::BuiltinDecoration>( if (auto* builtin = ast::GetDecoration<ast::BuiltinDecoration>(
member->Declaration()->decorations)) { member->Declaration()->decorations)) {
out << builtin_to_string(builtin->builtin); out << builtin_to_string(builtin->builtin, func->PipelineStage());
} else { } else {
out << name; out << name;
} }

View File

@ -347,8 +347,9 @@ class GeneratorImpl : public TextGenerator {
std::string generate_builtin_name(const sem::Intrinsic* intrinsic); std::string generate_builtin_name(const sem::Intrinsic* intrinsic);
/// Converts a builtin to a gl_ string /// Converts a builtin to a gl_ string
/// @param builtin the builtin to convert /// @param builtin the builtin to convert
/// @param stage pipeline stage in which this builtin is used
/// @returns the string name of the builtin or blank on error /// @returns the string name of the builtin or blank on error
const char* builtin_to_string(ast::Builtin builtin) const; const char* builtin_to_string(ast::Builtin builtin, ast::PipelineStage stage);
/// Converts a builtin to a sem::Type appropriate for GLSL. /// Converts a builtin to a sem::Type appropriate for GLSL.
/// @param builtin the builtin to convert /// @param builtin the builtin to convert
/// @returns the appropriate semantic type or null on error. /// @returns the appropriate semantic type or null on error.

View File

@ -208,7 +208,7 @@ tint_symbol_2 frag_main(tint_symbol_1 tint_symbol) {
} }
void main() { void main() {
tint_symbol_1 inputs; tint_symbol_1 inputs;
inputs.coord = gl_Position; inputs.coord = gl_FragCoord;
tint_symbol_2 outputs; tint_symbol_2 outputs;
outputs = frag_main(inputs); outputs = frag_main(inputs);
gl_FragDepth = outputs.value; gl_FragDepth = outputs.value;
@ -320,7 +320,7 @@ void main() {
tint_symbol_2 inputs; tint_symbol_2 inputs;
inputs.col1 = col1; inputs.col1 = col1;
inputs.col2 = col2; inputs.col2 = col2;
inputs.pos = gl_Position; inputs.pos = gl_FragCoord;
frag_main(inputs); frag_main(inputs);
} }

View File

@ -59,7 +59,7 @@ TEST_P(GlslBuiltinConversionTest, Emit) {
auto params = GetParam(); auto params = GetParam();
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
EXPECT_EQ(gen.builtin_to_string(params.builtin), EXPECT_EQ(gen.builtin_to_string(params.builtin, ast::PipelineStage::kVertex),
std::string(params.attribute_name)); std::string(params.attribute_name));
} }
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(