Skip to content

Commit

Permalink
Merge pull request #2394 from KhronosGroup/msl-bary-decoration-fix
Browse files Browse the repository at this point in the history
MSL: Allow Centroid and Sample decorations on bary coord.
  • Loading branch information
HansKristian-Work authored Oct 15, 2024
2 parents c82f1cc + df234e8 commit 1f4b24e
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct main0_out
{
float3 value [[color(0)]];
};

struct main0_in
{
float3 gl_BaryCoordNoPerspEXT [[barycentric_coord, centroid_no_perspective]];
};

fragment main0_out main0(main0_in in [[stage_in]])
{
main0_out out = {};
out.value = in.gl_BaryCoordNoPerspEXT;
return out;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct main0_out
{
float3 value [[color(0)]];
};

struct main0_in
{
float3 gl_BaryCoordEXT [[barycentric_coord, centroid_perspective]];
};

fragment main0_out main0(main0_in in [[stage_in]])
{
main0_out out = {};
out.value = in.gl_BaryCoordEXT;
return out;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct main0_out
{
float3 value [[color(0)]];
};

struct main0_in
{
float3 gl_BaryCoordNoPerspEXT [[barycentric_coord, sample_no_perspective]];
};

fragment main0_out main0(main0_in in [[stage_in]])
{
main0_out out = {};
out.value = in.gl_BaryCoordNoPerspEXT;
return out;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <metal_stdlib>
#include <simd/simd.h>

using namespace metal;

struct main0_out
{
float3 value [[color(0)]];
};

struct main0_in
{
float3 gl_BaryCoordEXT [[barycentric_coord, sample_perspective]];
};

fragment main0_out main0(main0_in in [[stage_in]])
{
main0_out out = {};
out.value = in.gl_BaryCoordEXT;
return out;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 11
; Bound: 13
; Schema: 0
OpCapability Shader
OpCapability FragmentBarycentricKHR
OpExtension "SPV_KHR_fragment_shader_barycentric"
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %value %gl_BaryCoordEXT
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpSourceExtension "GL_EXT_fragment_shader_barycentric"
OpName %main "main"
OpName %value "value"
OpName %gl_BaryCoordEXT "gl_BaryCoordEXT"
OpDecorate %value Location 0
OpDecorate %gl_BaryCoordEXT BuiltIn BaryCoordNoPerspKHR
OpDecorate %gl_BaryCoordEXT Centroid
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v3float = OpTypeVector %float 3
%_ptr_Output_v3float = OpTypePointer Output %v3float
%value = OpVariable %_ptr_Output_v3float Output
%_ptr_Input_v3float = OpTypePointer Input %v3float
%gl_BaryCoordEXT = OpVariable %_ptr_Input_v3float Input
%main = OpFunction %void None %3
%5 = OpLabel
%12 = OpLoad %v3float %gl_BaryCoordEXT
OpStore %value %12
OpReturn
OpFunctionEnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 11
; Bound: 13
; Schema: 0
OpCapability Shader
OpCapability FragmentBarycentricKHR
OpExtension "SPV_KHR_fragment_shader_barycentric"
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %value %gl_BaryCoordEXT
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpSourceExtension "GL_EXT_fragment_shader_barycentric"
OpName %main "main"
OpName %value "value"
OpName %gl_BaryCoordEXT "gl_BaryCoordEXT"
OpDecorate %value Location 0
OpDecorate %gl_BaryCoordEXT BuiltIn BaryCoordKHR
OpDecorate %gl_BaryCoordEXT Centroid
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v3float = OpTypeVector %float 3
%_ptr_Output_v3float = OpTypePointer Output %v3float
%value = OpVariable %_ptr_Output_v3float Output
%_ptr_Input_v3float = OpTypePointer Input %v3float
%gl_BaryCoordEXT = OpVariable %_ptr_Input_v3float Input
%main = OpFunction %void None %3
%5 = OpLabel
%12 = OpLoad %v3float %gl_BaryCoordEXT
OpStore %value %12
OpReturn
OpFunctionEnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 11
; Bound: 13
; Schema: 0
OpCapability Shader
OpCapability SampleRateShading
OpCapability FragmentBarycentricKHR
OpExtension "SPV_KHR_fragment_shader_barycentric"
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %value %gl_BaryCoordEXT
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpSourceExtension "GL_EXT_fragment_shader_barycentric"
OpName %main "main"
OpName %value "value"
OpName %gl_BaryCoordEXT "gl_BaryCoordEXT"
OpDecorate %value Location 0
OpDecorate %gl_BaryCoordEXT BuiltIn BaryCoordNoPerspKHR
OpDecorate %gl_BaryCoordEXT Sample
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v3float = OpTypeVector %float 3
%_ptr_Output_v3float = OpTypePointer Output %v3float
%value = OpVariable %_ptr_Output_v3float Output
%_ptr_Input_v3float = OpTypePointer Input %v3float
%gl_BaryCoordEXT = OpVariable %_ptr_Input_v3float Input
%main = OpFunction %void None %3
%5 = OpLabel
%12 = OpLoad %v3float %gl_BaryCoordEXT
OpStore %value %12
OpReturn
OpFunctionEnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 11
; Bound: 13
; Schema: 0
OpCapability Shader
OpCapability SampleRateShading
OpCapability FragmentBarycentricKHR
OpExtension "SPV_KHR_fragment_shader_barycentric"
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %value %gl_BaryCoordEXT
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpSourceExtension "GL_EXT_fragment_shader_barycentric"
OpName %main "main"
OpName %value "value"
OpName %gl_BaryCoordEXT "gl_BaryCoordEXT"
OpDecorate %value Location 0
OpDecorate %gl_BaryCoordEXT BuiltIn BaryCoordKHR
OpDecorate %gl_BaryCoordEXT Sample
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v3float = OpTypeVector %float 3
%_ptr_Output_v3float = OpTypePointer Output %v3float
%value = OpVariable %_ptr_Output_v3float Output
%_ptr_Input_v3float = OpTypePointer Input %v3float
%gl_BaryCoordEXT = OpVariable %_ptr_Input_v3float Input
%main = OpFunction %void None %3
%5 = OpLabel
%12 = OpLoad %v3float %gl_BaryCoordEXT
OpStore %value %12
OpReturn
OpFunctionEnd
33 changes: 13 additions & 20 deletions spirv_msl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12769,17 +12769,10 @@ string CompilerMSL::member_attribute_qualifier(const SPIRType &type, uint32_t in
else
quals = member_location_attribute_qualifier(type, index);

if (builtin == BuiltInBaryCoordKHR || builtin == BuiltInBaryCoordNoPerspKHR)
if (builtin == BuiltInBaryCoordKHR && has_member_decoration(type.self, index, DecorationNoPerspective))
{
if (has_member_decoration(type.self, index, DecorationFlat) ||
has_member_decoration(type.self, index, DecorationCentroid) ||
has_member_decoration(type.self, index, DecorationSample) ||
has_member_decoration(type.self, index, DecorationNoPerspective))
{
// NoPerspective is baked into the builtin type.
SPIRV_CROSS_THROW(
"Flat, Centroid, Sample, NoPerspective decorations are not supported for BaryCoord inputs.");
}
// NoPerspective is baked into the builtin type.
SPIRV_CROSS_THROW("NoPerspective decorations are not supported for BaryCoord inputs.");
}

// Don't bother decorating integers with the 'flat' attribute; it's
Expand All @@ -12797,7 +12790,7 @@ string CompilerMSL::member_attribute_qualifier(const SPIRType &type, uint32_t in
{
if (!quals.empty())
quals += ", ";
if (has_member_decoration(type.self, index, DecorationNoPerspective))
if (has_member_decoration(type.self, index, DecorationNoPerspective) || builtin == BuiltInBaryCoordNoPerspKHR)
quals += "centroid_no_perspective";
else
quals += "centroid_perspective";
Expand All @@ -12806,17 +12799,23 @@ string CompilerMSL::member_attribute_qualifier(const SPIRType &type, uint32_t in
{
if (!quals.empty())
quals += ", ";
if (has_member_decoration(type.self, index, DecorationNoPerspective))
if (has_member_decoration(type.self, index, DecorationNoPerspective) || builtin == BuiltInBaryCoordNoPerspKHR)
quals += "sample_no_perspective";
else
quals += "sample_perspective";
}
else if (has_member_decoration(type.self, index, DecorationNoPerspective))
else if (has_member_decoration(type.self, index, DecorationNoPerspective) || builtin == BuiltInBaryCoordNoPerspKHR)
{
if (!quals.empty())
quals += ", ";
quals += "center_no_perspective";
}
else if (builtin == BuiltInBaryCoordKHR)
{
if (!quals.empty())
quals += ", ";
quals += "center_perspective";
}
}

if (!quals.empty())
Expand Down Expand Up @@ -16819,18 +16818,12 @@ string CompilerMSL::builtin_qualifier(BuiltIn builtin)
SPIRV_CROSS_THROW("Subgroup ballot masks are handled specially in MSL.");

case BuiltInBaryCoordKHR:
if (msl_options.is_ios() && !msl_options.supports_msl_version(2, 3))
SPIRV_CROSS_THROW("Barycentrics are only supported in MSL 2.3 and above on iOS.");
else if (!msl_options.supports_msl_version(2, 2))
SPIRV_CROSS_THROW("Barycentrics are only supported in MSL 2.2 and above on macOS.");
return "barycentric_coord, center_perspective";

case BuiltInBaryCoordNoPerspKHR:
if (msl_options.is_ios() && !msl_options.supports_msl_version(2, 3))
SPIRV_CROSS_THROW("Barycentrics are only supported in MSL 2.3 and above on iOS.");
else if (!msl_options.supports_msl_version(2, 2))
SPIRV_CROSS_THROW("Barycentrics are only supported in MSL 2.2 and above on macOS.");
return "barycentric_coord, center_no_perspective";
return "barycentric_coord";

default:
return "unsupported-built-in";
Expand Down

0 comments on commit 1f4b24e

Please sign in to comment.