-
Notifications
You must be signed in to change notification settings - Fork 570
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2418 from Try/msl-mesh-shader-patch1
Fixes for Metal mesh-shader
- Loading branch information
Showing
8 changed files
with
275 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
reference/opt/shaders-msl/mesh/mesh-shader-flat-varying.msl3.spv14.vk.nocompat.mesh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#pragma clang diagnostic ignored "-Wmissing-prototypes" | ||
#pragma clang diagnostic ignored "-Wmissing-braces" | ||
|
||
#include <metal_stdlib> | ||
#include <simd/simd.h> | ||
|
||
using namespace metal; | ||
|
||
template<typename T, size_t Num> | ||
struct spvUnsafeArray | ||
{ | ||
T elements[Num ? Num : 1]; | ||
|
||
thread T& operator [] (size_t pos) thread | ||
{ | ||
return elements[pos]; | ||
} | ||
constexpr const thread T& operator [] (size_t pos) const thread | ||
{ | ||
return elements[pos]; | ||
} | ||
|
||
device T& operator [] (size_t pos) device | ||
{ | ||
return elements[pos]; | ||
} | ||
constexpr const device T& operator [] (size_t pos) const device | ||
{ | ||
return elements[pos]; | ||
} | ||
|
||
constexpr const constant T& operator [] (size_t pos) const constant | ||
{ | ||
return elements[pos]; | ||
} | ||
|
||
threadgroup T& operator [] (size_t pos) threadgroup | ||
{ | ||
return elements[pos]; | ||
} | ||
constexpr const threadgroup T& operator [] (size_t pos) const threadgroup | ||
{ | ||
return elements[pos]; | ||
} | ||
|
||
object_data T& operator [] (size_t pos) object_data | ||
{ | ||
return elements[pos]; | ||
} | ||
constexpr const object_data T& operator [] (size_t pos) const object_data | ||
{ | ||
return elements[pos]; | ||
} | ||
}; | ||
|
||
void spvSetMeshOutputsEXT(uint gl_LocalInvocationIndex, threadgroup uint2& spvMeshSizes, uint vertexCount, uint primitiveCount) | ||
{ | ||
if (gl_LocalInvocationIndex == 0) | ||
{ | ||
spvMeshSizes.x = vertexCount; | ||
spvMeshSizes.y = primitiveCount; | ||
} | ||
} | ||
|
||
struct gl_MeshPerVertexEXT | ||
{ | ||
float4 gl_Position [[position]]; | ||
float gl_ClipDistance [[clip_distance]] [1]; | ||
}; | ||
|
||
constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(2u, 3u, 4u); | ||
|
||
struct spvPerVertex | ||
{ | ||
float4 gl_Position [[position]]; | ||
float gl_ClipDistance [[clip_distance]] [1]; | ||
float gl_ClipDistance_0 [[user(clip0)]]; | ||
float fOut [[user(locn0)]]; | ||
uint uiOut [[user(locn1)]]; | ||
}; | ||
|
||
using spvMesh_t = mesh<spvPerVertex, void, 24, 22, topology::triangle>; | ||
|
||
static inline __attribute__((always_inline)) | ||
void _4(threadgroup spvUnsafeArray<gl_MeshPerVertexEXT, 24>& gl_MeshVerticesEXT, thread uint& gl_LocalInvocationIndex, thread uint3& gl_GlobalInvocationID, threadgroup spvUnsafeArray<float, 24>& fOut, threadgroup spvUnsafeArray<uint, 24>& uiOut, threadgroup uint2& spvMeshSizes) | ||
{ | ||
spvSetMeshOutputsEXT(gl_LocalInvocationIndex, spvMeshSizes, 24u, 22u); | ||
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position = float4(float3(gl_GlobalInvocationID), 1.0); | ||
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_ClipDistance[0] = 4.0; | ||
fOut[gl_LocalInvocationIndex] = float(gl_GlobalInvocationID.x); | ||
uiOut[gl_LocalInvocationIndex] = gl_GlobalInvocationID.y; | ||
} | ||
|
||
[[mesh]] void main0(uint gl_LocalInvocationIndex [[thread_index_in_threadgroup]], uint3 gl_GlobalInvocationID [[thread_position_in_grid]], spvMesh_t spvMesh) | ||
{ | ||
threadgroup uint2 spvMeshSizes; | ||
threadgroup spvUnsafeArray<gl_MeshPerVertexEXT, 24> gl_MeshVerticesEXT; | ||
threadgroup spvUnsafeArray<float, 24> fOut; | ||
threadgroup spvUnsafeArray<uint, 24> uiOut; | ||
if (gl_LocalInvocationIndex == 0) spvMeshSizes.y = 0u; | ||
_4(gl_MeshVerticesEXT, gl_LocalInvocationIndex, gl_GlobalInvocationID, fOut, uiOut, spvMeshSizes); | ||
threadgroup_barrier(mem_flags::mem_threadgroup); | ||
if (spvMeshSizes.y == 0) | ||
{ | ||
return; | ||
} | ||
spvMesh.set_primitive_count(spvMeshSizes.y); | ||
const uint spvThreadCount [[maybe_unused]] = (gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z); | ||
const uint spvVI = gl_LocalInvocationIndex; | ||
if (gl_LocalInvocationIndex < spvMeshSizes.x) | ||
{ | ||
spvPerVertex spvV = {}; | ||
spvV.gl_Position = gl_MeshVerticesEXT[spvVI].gl_Position; | ||
spvV.gl_ClipDistance[0] = gl_MeshVerticesEXT[spvVI].gl_ClipDistance[0]; | ||
spvV.gl_ClipDistance_0 = gl_MeshVerticesEXT[spvVI].gl_ClipDistance[0]; | ||
spvV.fOut = fOut[spvVI]; | ||
spvV.uiOut = uiOut[spvVI]; | ||
spvMesh.set_vertex(spvVI, spvV); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
reference/shaders-msl/mesh/mesh-shader-flat-varying.msl3.spv14.vk.nocompat.mesh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#pragma clang diagnostic ignored "-Wmissing-prototypes" | ||
#pragma clang diagnostic ignored "-Wmissing-braces" | ||
|
||
#include <metal_stdlib> | ||
#include <simd/simd.h> | ||
|
||
using namespace metal; | ||
|
||
template<typename T, size_t Num> | ||
struct spvUnsafeArray | ||
{ | ||
T elements[Num ? Num : 1]; | ||
|
||
thread T& operator [] (size_t pos) thread | ||
{ | ||
return elements[pos]; | ||
} | ||
constexpr const thread T& operator [] (size_t pos) const thread | ||
{ | ||
return elements[pos]; | ||
} | ||
|
||
device T& operator [] (size_t pos) device | ||
{ | ||
return elements[pos]; | ||
} | ||
constexpr const device T& operator [] (size_t pos) const device | ||
{ | ||
return elements[pos]; | ||
} | ||
|
||
constexpr const constant T& operator [] (size_t pos) const constant | ||
{ | ||
return elements[pos]; | ||
} | ||
|
||
threadgroup T& operator [] (size_t pos) threadgroup | ||
{ | ||
return elements[pos]; | ||
} | ||
constexpr const threadgroup T& operator [] (size_t pos) const threadgroup | ||
{ | ||
return elements[pos]; | ||
} | ||
|
||
object_data T& operator [] (size_t pos) object_data | ||
{ | ||
return elements[pos]; | ||
} | ||
constexpr const object_data T& operator [] (size_t pos) const object_data | ||
{ | ||
return elements[pos]; | ||
} | ||
}; | ||
|
||
void spvSetMeshOutputsEXT(uint gl_LocalInvocationIndex, threadgroup uint2& spvMeshSizes, uint vertexCount, uint primitiveCount) | ||
{ | ||
if (gl_LocalInvocationIndex == 0) | ||
{ | ||
spvMeshSizes.x = vertexCount; | ||
spvMeshSizes.y = primitiveCount; | ||
} | ||
} | ||
|
||
struct gl_MeshPerVertexEXT | ||
{ | ||
float4 gl_Position [[position]]; | ||
float gl_ClipDistance [[clip_distance]] [1]; | ||
}; | ||
|
||
constant uint3 gl_WorkGroupSize [[maybe_unused]] = uint3(2u, 3u, 4u); | ||
|
||
struct spvPerVertex | ||
{ | ||
float4 gl_Position [[position]]; | ||
float gl_ClipDistance [[clip_distance]] [1]; | ||
float gl_ClipDistance_0 [[user(clip0)]]; | ||
float fOut [[user(locn0)]]; | ||
uint uiOut [[user(locn1)]]; | ||
}; | ||
|
||
using spvMesh_t = mesh<spvPerVertex, void, 24, 22, topology::triangle>; | ||
|
||
static inline __attribute__((always_inline)) | ||
void _4(threadgroup spvUnsafeArray<gl_MeshPerVertexEXT, 24>& gl_MeshVerticesEXT, thread uint& gl_LocalInvocationIndex, thread uint3& gl_GlobalInvocationID, threadgroup spvUnsafeArray<float, 24>& fOut, threadgroup spvUnsafeArray<uint, 24>& uiOut, threadgroup uint2& spvMeshSizes) | ||
{ | ||
spvSetMeshOutputsEXT(gl_LocalInvocationIndex, spvMeshSizes, 24u, 22u); | ||
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position = float4(float3(gl_GlobalInvocationID), 1.0); | ||
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_ClipDistance[0] = 4.0; | ||
fOut[gl_LocalInvocationIndex] = float(gl_GlobalInvocationID.x); | ||
uiOut[gl_LocalInvocationIndex] = gl_GlobalInvocationID.y; | ||
} | ||
|
||
[[mesh]] void main0(uint gl_LocalInvocationIndex [[thread_index_in_threadgroup]], uint3 gl_GlobalInvocationID [[thread_position_in_grid]], spvMesh_t spvMesh) | ||
{ | ||
threadgroup uint2 spvMeshSizes; | ||
threadgroup spvUnsafeArray<gl_MeshPerVertexEXT, 24> gl_MeshVerticesEXT; | ||
threadgroup spvUnsafeArray<float, 24> fOut; | ||
threadgroup spvUnsafeArray<uint, 24> uiOut; | ||
if (gl_LocalInvocationIndex == 0) spvMeshSizes.y = 0u; | ||
_4(gl_MeshVerticesEXT, gl_LocalInvocationIndex, gl_GlobalInvocationID, fOut, uiOut, spvMeshSizes); | ||
threadgroup_barrier(mem_flags::mem_threadgroup); | ||
if (spvMeshSizes.y == 0) | ||
{ | ||
return; | ||
} | ||
spvMesh.set_primitive_count(spvMeshSizes.y); | ||
const uint spvThreadCount [[maybe_unused]] = (gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z); | ||
const uint spvVI = gl_LocalInvocationIndex; | ||
if (gl_LocalInvocationIndex < spvMeshSizes.x) | ||
{ | ||
spvPerVertex spvV = {}; | ||
spvV.gl_Position = gl_MeshVerticesEXT[spvVI].gl_Position; | ||
spvV.gl_ClipDistance[0] = gl_MeshVerticesEXT[spvVI].gl_ClipDistance[0]; | ||
spvV.gl_ClipDistance_0 = gl_MeshVerticesEXT[spvVI].gl_ClipDistance[0]; | ||
spvV.fOut = fOut[spvVI]; | ||
spvV.uiOut = uiOut[spvVI]; | ||
spvMesh.set_vertex(spvVI, spvV); | ||
} | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
shaders-msl/mesh/mesh-shader-flat-varying.msl3.spv14.vk.nocompat.mesh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#version 450 | ||
#extension GL_EXT_mesh_shader : require | ||
#extension GL_EXT_fragment_shading_rate : require | ||
layout(local_size_x = 2, local_size_y = 3, local_size_z = 4) in; | ||
layout(triangles, max_vertices = 24, max_primitives = 22) out; | ||
|
||
out gl_MeshPerVertexEXT | ||
{ | ||
vec4 gl_Position; | ||
float gl_ClipDistance[1]; | ||
} gl_MeshVerticesEXT[]; | ||
|
||
layout(location = 0) out float fOut[]; | ||
layout(location = 1) out flat uint uiOut[]; | ||
|
||
void main() | ||
{ | ||
SetMeshOutputsEXT(24, 22); | ||
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_Position = vec4(gl_GlobalInvocationID, 1.0); | ||
gl_MeshVerticesEXT[gl_LocalInvocationIndex].gl_ClipDistance[0] = 4.0; | ||
fOut[gl_LocalInvocationIndex] = float(gl_GlobalInvocationID.x); | ||
uiOut[gl_LocalInvocationIndex] = gl_GlobalInvocationID.y; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters