From 8df62847f40ab9f10df98709bdbd490b51e95d61 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 15 Dec 2020 12:36:08 +0000 Subject: [PATCH] spirv-writer: Emit the DepthReplacing execution mode ... if the kFragDepth builtin is referenced. Bug: dawn:399 Change-Id: I17a4a50d7aa03fd341dec4787a93566c61b59320 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/35500 Commit-Queue: Ben Clayton Reviewed-by: David Neto --- src/writer/spirv/builder.cc | 8 +++++ .../spirv/builder_function_decoration_test.cc | 29 +++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index 2143b579af..e1cc57b71f 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -477,6 +477,14 @@ bool Builder::GenerateExecutionModes(ast::Function* func, uint32_t id) { Operand::Int(x), Operand::Int(y), Operand::Int(z)}); } + for (auto builtin : func->referenced_builtin_variables()) { + if (builtin.second->value() == ast::Builtin::kFragDepth) { + push_execution_mode( + spv::Op::OpExecutionMode, + {Operand::Int(id), Operand::Int(SpvExecutionModeDepthReplacing)}); + } + } + return true; } diff --git a/src/writer/spirv/builder_function_decoration_test.cc b/src/writer/spirv/builder_function_decoration_test.cc index fe8fc51e55..4f28dc5123 100644 --- a/src/writer/spirv/builder_function_decoration_test.cc +++ b/src/writer/spirv/builder_function_decoration_test.cc @@ -271,7 +271,7 @@ TEST_F(BuilderTest, FunctionDecoration_ExecutionMode_Fragment_OriginUpperLeft) { )"); } -TEST_F(BuilderTest, FunctionDecoration_WorkgroupSize_Default) { +TEST_F(BuilderTest, FunctionDecoration_ExecutionMode_WorkgroupSize_Default) { ast::type::Void void_type; ast::Function func( @@ -287,7 +287,7 @@ TEST_F(BuilderTest, FunctionDecoration_WorkgroupSize_Default) { )"); } -TEST_F(BuilderTest, FunctionDecoration_WorkgroupSize) { +TEST_F(BuilderTest, FunctionDecoration_ExecutionMode_WorkgroupSize) { ast::type::Void void_type; ast::Function func( @@ -343,6 +343,31 @@ OpFunctionEnd )"); } +TEST_F(BuilderTest, FunctionDecoration_ExecutionMode_FragDepth) { + auto* fragdepth = + Var("fragdepth", ast::StorageClass::kOutput, create(), + nullptr, + ast::VariableDecorationList{ + create(ast::Builtin::kFragDepth), + }); + mod->AddGlobalVariable(fragdepth); + + auto* body = create(ast::StatementList{ + create(Expr("fragdepth"), Expr(1.f)), + }); + + auto* func = create( + Source{}, mod->RegisterSymbol("main"), "main", ast::VariableList{}, + create(), body, ast::FunctionDecorationList{}); + + func->add_referenced_module_variable(fragdepth); + + ASSERT_TRUE(b.GenerateExecutionModes(func, 3)) << b.error(); + EXPECT_EQ(DumpInstructions(b.execution_modes()), + R"(OpExecutionMode %3 DepthReplacing +)"); +} + } // namespace } // namespace spirv } // namespace writer