Add check for backend specific shader IO. The ability to return the vertex position through a `var` instead of the vertex return is a backend specific feature. It is enabled in the SPIR-V reader and SPIR-V lower shader-IO already. Correctly guard the structural validation for this feature. Change-Id: Ica357ddd9055d72ef992d728f586c9465a9b664b Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/322699 Commit-Queue: dan sinclair <dsinclair@chromium.org> Auto-Submit: dan sinclair <dsinclair@chromium.org> Reviewed-by: Ryan Harrison <rharrison@chromium.org> Commit-Queue: Ryan Harrison <rharrison@chromium.org>
diff --git a/src/tint/lang/core/ir/structural_validator.cc b/src/tint/lang/core/ir/structural_validator.cc index b0964b4..8d6569c 100644 --- a/src/tint/lang/core/ir/structural_validator.cc +++ b/src/tint/lang/core/ir/structural_validator.cc
@@ -1893,6 +1893,11 @@ const auto attr = var->Attributes(); if (IsPositionPresent(attr, ty)) { + if (!ir_.properties.Contains(Property::kAllowBackendSpecificShaderIO)) { + AddError(var) << "position as part of a `var`, it must be part of the return"; + AddNote(ep) << "used in entry point here"; + return; + } return; } }
diff --git a/src/tint/lang/core/ir/validator_function_test.cc b/src/tint/lang/core/ir/validator_function_test.cc index 0ed4096..62209f1 100644 --- a/src/tint/lang/core/ir/validator_function_test.cc +++ b/src/tint/lang/core/ir/validator_function_test.cc
@@ -34,15 +34,8 @@ #include "src/tint/lang/core/ir/validator.h" #include "src/tint/lang/core/ir/validator_test.h" #include "src/tint/lang/core/number.h" -#include "src/tint/lang/core/type/abstract_float.h" -#include "src/tint/lang/core/type/abstract_int.h" -#include "src/tint/lang/core/type/function.h" #include "src/tint/lang/core/type/manager.h" -#include "src/tint/lang/core/type/matrix.h" -#include "src/tint/lang/core/type/memory_view.h" -#include "src/tint/lang/core/type/reference.h" -#include "src/tint/lang/core/type/storage_texture.h" -#include "src/tint/lang/core/type/struct.h" +#include "src/tint/lang/core/type/reference.h" // IWYU pragma: export namespace tint::core::ir { @@ -3350,6 +3343,61 @@ )")) << res.Failure(); } +TEST_F(IR_ValidatorTest, Function_Vertex_PositionOnVarWithProperty) { + auto pos_ty = ty.vec4f(); + auto pos_attr = IOAttributes(); + pos_attr.builtin = BuiltinValue::kPosition; + + auto* str_ty = + ty.Struct(mod.symbols.New("MyStruct"), { + {mod.symbols.New("pos"), pos_ty, pos_attr}, + }); + + auto* v = b.Var(ty.ptr(AddressSpace::kOut, str_ty, core::Access::kReadWrite)); + mod.root_block->Append(v); + + auto* f = b.Function("my_func", ty.void_(), Function::PipelineStage::kVertex); + b.Append(f->Block(), [&] { + b.Phony(v); + b.Return(f); + }); + + mod.properties.Add(ir::Property::kAllowPhonyInstructions); + mod.properties.Add(ir::Property::kAllowBackendSpecificShaderIO); + + auto res = ir::Validate(mod); + ASSERT_EQ(res, Success); +} + +TEST_F(IR_ValidatorTest, Function_Vertex_PositionOnVarWithoutProperty) { + auto pos_ty = ty.vec4f(); + auto pos_attr = IOAttributes(); + pos_attr.builtin = BuiltinValue::kPosition; + + auto* str_ty = + ty.Struct(mod.symbols.New("MyStruct"), { + {mod.symbols.New("pos"), pos_ty, pos_attr}, + }); + + auto* v = b.Var(ty.ptr(AddressSpace::kOut, str_ty, core::Access::kReadWrite)); + mod.root_block->Append(v); + + auto* f = b.Function("my_func", ty.void_(), Function::PipelineStage::kVertex); + b.Append(f->Block(), [&] { + b.Phony(v); + b.Return(f); + }); + + mod.properties.Add(ir::Property::kAllowPhonyInstructions); + auto res = ir::Validate(mod); + ASSERT_NE(res, Success); + EXPECT_THAT(res.Failure().reason, + testing::HasSubstr( + R"(:6:41 error: var: position as part of a `var`, it must be part of the return + %1:ptr<__out, MyStruct, read_write> = var undef +)")) << res.Failure(); +} + TEST_F(IR_ValidatorTest, Function_NonFragment_BoolInput) { auto* f = VertexEntryPoint(); auto* p = b.FunctionParam("invalid", ty.bool_());