diff --git a/gn/sksl_tests.gni b/gn/sksl_tests.gni index 00c6950d82..034bed0cff 100644 --- a/gn/sksl_tests.gni +++ b/gn/sksl_tests.gni @@ -175,6 +175,7 @@ sksl_metal_tests = [ "$_tests/sksl/metal/NumericGlobals.sksl", "$_tests/sksl/metal/OpaqueTypeInInterfaceBlock.sksl", "$_tests/sksl/metal/OpaqueTypeInStruct.sksl", + "$_tests/sksl/metal/OutVarsRequireLocation.sksl", "$_tests/sksl/metal/SamplerGlobals.sksl", ] diff --git a/src/sksl/SkSLMetalCodeGenerator.cpp b/src/sksl/SkSLMetalCodeGenerator.cpp index 96286ebcb1..fcc37b6062 100644 --- a/src/sksl/SkSLMetalCodeGenerator.cpp +++ b/src/sksl/SkSLMetalCodeGenerator.cpp @@ -1559,12 +1559,15 @@ void MetalCodeGenerator::writeOutputStruct() { this->writeType(var.type()); this->write(" "); this->writeName(var.name()); - if (fProgram.fKind == Program::kVertex_Kind) { - this->write(" [[user(locn" + - to_string(var.modifiers().fLayout.fLocation) + ")]]"); + + int location = var.modifiers().fLayout.fLocation; + if (location < 0) { + fErrors.error(var.fOffset, + "Metal out variables must have 'layout(location=...)'"); + } else if (fProgram.fKind == Program::kVertex_Kind) { + this->write(" [[user(locn" + to_string(location) + ")]]"); } else if (fProgram.fKind == Program::kFragment_Kind) { - this->write(" [[color(" + - to_string(var.modifiers().fLayout.fLocation) +")"); + this->write(" [[color(" + to_string(location) + ")"); int colorIndex = var.modifiers().fLayout.fIndex; if (colorIndex) { this->write(", index(" + to_string(colorIndex) + ")"); diff --git a/tests/sksl/metal/OutVarsRequireLocation.sksl b/tests/sksl/metal/OutVarsRequireLocation.sksl new file mode 100644 index 0000000000..68c6caed60 --- /dev/null +++ b/tests/sksl/metal/OutVarsRequireLocation.sksl @@ -0,0 +1 @@ +out int noLocation; diff --git a/tests/sksl/metal/golden/OutVarsRequireLocation.metal b/tests/sksl/metal/golden/OutVarsRequireLocation.metal new file mode 100644 index 0000000000..5b6bc64fbb --- /dev/null +++ b/tests/sksl/metal/golden/OutVarsRequireLocation.metal @@ -0,0 +1,4 @@ +### Compilation failed: + +error: 1: Metal out variables must have 'layout(location=...)' +1 error diff --git a/tests/sksl/shared/InstanceID.vert b/tests/sksl/shared/InstanceID.vert index 0fdf87008b..351ea2eba8 100644 --- a/tests/sksl/shared/InstanceID.vert +++ b/tests/sksl/shared/InstanceID.vert @@ -1,4 +1,4 @@ -out int id; +layout(location=1) out int id; void main() { id = sk_InstanceID; diff --git a/tests/sksl/shared/VertexID.vert b/tests/sksl/shared/VertexID.vert index 8ca9d95ff4..39e2d76391 100644 --- a/tests/sksl/shared/VertexID.vert +++ b/tests/sksl/shared/VertexID.vert @@ -1,4 +1,4 @@ -out int id; +layout(location=1) out int id; void main() { id = sk_VertexID; diff --git a/tests/sksl/shared/golden/InstanceID.asm.vert b/tests/sksl/shared/golden/InstanceID.asm.vert index 2289321b72..0a268a3c90 100644 --- a/tests/sksl/shared/golden/InstanceID.asm.vert +++ b/tests/sksl/shared/golden/InstanceID.asm.vert @@ -6,6 +6,7 @@ OpName %sk_InstanceID "sk_InstanceID" OpName %id "id" OpName %main "main" OpDecorate %sk_InstanceID BuiltIn InstanceIndex +OpDecorate %id Location 1 %int = OpTypeInt 32 1 %_ptr_Input_int = OpTypePointer Input %int %sk_InstanceID = OpVariable %_ptr_Input_int Input diff --git a/tests/sksl/shared/golden/InstanceID.glsl b/tests/sksl/shared/golden/InstanceID.glsl index b265e30d4e..a1f21043ca 100644 --- a/tests/sksl/shared/golden/InstanceID.glsl +++ b/tests/sksl/shared/golden/InstanceID.glsl @@ -1,5 +1,5 @@ -out int id; +layout (location = 1) out int id; void main() { id = gl_InstanceID; } diff --git a/tests/sksl/shared/golden/InstanceID.metal b/tests/sksl/shared/golden/InstanceID.metal index 1ac4820d24..33c35ea2a2 100644 --- a/tests/sksl/shared/golden/InstanceID.metal +++ b/tests/sksl/shared/golden/InstanceID.metal @@ -5,7 +5,7 @@ struct Inputs { }; struct Outputs { float4 sk_Position [[position]]; - int id [[user(locn-1)]]; + int id [[user(locn1)]]; float sk_PointSize [[point_size]]; }; diff --git a/tests/sksl/shared/golden/VertexID.asm.vert b/tests/sksl/shared/golden/VertexID.asm.vert index 8659720fd4..3ef3ed35d1 100644 --- a/tests/sksl/shared/golden/VertexID.asm.vert +++ b/tests/sksl/shared/golden/VertexID.asm.vert @@ -6,6 +6,7 @@ OpName %sk_VertexID "sk_VertexID" OpName %id "id" OpName %main "main" OpDecorate %sk_VertexID BuiltIn VertexIndex +OpDecorate %id Location 1 %int = OpTypeInt 32 1 %_ptr_Input_int = OpTypePointer Input %int %sk_VertexID = OpVariable %_ptr_Input_int Input diff --git a/tests/sksl/shared/golden/VertexID.glsl b/tests/sksl/shared/golden/VertexID.glsl index f529fb22f9..75f8f5139a 100644 --- a/tests/sksl/shared/golden/VertexID.glsl +++ b/tests/sksl/shared/golden/VertexID.glsl @@ -1,5 +1,5 @@ -out int id; +layout (location = 1) out int id; void main() { id = gl_VertexID; } diff --git a/tests/sksl/shared/golden/VertexID.metal b/tests/sksl/shared/golden/VertexID.metal index f0f5bf5362..4a164c32c8 100644 --- a/tests/sksl/shared/golden/VertexID.metal +++ b/tests/sksl/shared/golden/VertexID.metal @@ -5,7 +5,7 @@ struct Inputs { }; struct Outputs { float4 sk_Position [[position]]; - int id [[user(locn-1)]]; + int id [[user(locn1)]]; float sk_PointSize [[point_size]]; };