diff --git a/src/writer/glsl/generator_impl.cc b/src/writer/glsl/generator_impl.cc index df24aaae06..7668b28af4 100644 --- a/src/writer/glsl/generator_impl.cc +++ b/src/writer/glsl/generator_impl.cc @@ -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) { case ast::Builtin::kPosition: - return "gl_Position"; + switch (stage) { + case ast::PipelineStage::kVertex: + 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: return "gl_VertexID"; case ast::Builtin::kInstanceIndex: @@ -1959,10 +1969,10 @@ bool GeneratorImpl::EmitEntryPointFunction(const ast::Function* func) { return false; } out << "("; - out << builtin_to_string(builtin->builtin); + out << builtin_to_string(builtin->builtin, func->PipelineStage()); out << ")"; } else { - out << builtin_to_string(builtin->builtin); + out << builtin_to_string(builtin->builtin, func->PipelineStage()); } } else { out << name; @@ -1992,7 +2002,7 @@ bool GeneratorImpl::EmitEntryPointFunction(const ast::Function* func) { out << " "; if (auto* builtin = ast::GetDecoration( member->Declaration()->decorations)) { - out << builtin_to_string(builtin->builtin); + out << builtin_to_string(builtin->builtin, func->PipelineStage()); } else { out << name; } diff --git a/src/writer/glsl/generator_impl.h b/src/writer/glsl/generator_impl.h index 6155f0b93a..85522b8e14 100644 --- a/src/writer/glsl/generator_impl.h +++ b/src/writer/glsl/generator_impl.h @@ -347,8 +347,9 @@ class GeneratorImpl : public TextGenerator { std::string generate_builtin_name(const sem::Intrinsic* intrinsic); /// Converts a builtin to a gl_ string /// @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 - 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. /// @param builtin the builtin to convert /// @returns the appropriate semantic type or null on error. diff --git a/src/writer/glsl/generator_impl_function_test.cc b/src/writer/glsl/generator_impl_function_test.cc index 9f82acb94a..2324dd1e6f 100644 --- a/src/writer/glsl/generator_impl_function_test.cc +++ b/src/writer/glsl/generator_impl_function_test.cc @@ -208,7 +208,7 @@ tint_symbol_2 frag_main(tint_symbol_1 tint_symbol) { } void main() { tint_symbol_1 inputs; - inputs.coord = gl_Position; + inputs.coord = gl_FragCoord; tint_symbol_2 outputs; outputs = frag_main(inputs); gl_FragDepth = outputs.value; @@ -320,7 +320,7 @@ void main() { tint_symbol_2 inputs; inputs.col1 = col1; inputs.col2 = col2; - inputs.pos = gl_Position; + inputs.pos = gl_FragCoord; frag_main(inputs); } diff --git a/src/writer/glsl/generator_impl_test.cc b/src/writer/glsl/generator_impl_test.cc index 30758857b0..c89041f019 100644 --- a/src/writer/glsl/generator_impl_test.cc +++ b/src/writer/glsl/generator_impl_test.cc @@ -59,7 +59,7 @@ TEST_P(GlslBuiltinConversionTest, Emit) { auto params = GetParam(); 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)); } INSTANTIATE_TEST_SUITE_P(