|
| 1 | +/* |
| 2 | + * Copyright 2026 Diligent Graphics LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * In no event and under no legal theory, whether in tort (including negligence), |
| 17 | + * contract, or otherwise, unless required by applicable law (such as deliberate |
| 18 | + * and grossly negligent acts) or agreed to in writing, shall any Contributor be |
| 19 | + * liable for any damages, including any direct, indirect, special, incidental, |
| 20 | + * or consequential damages of any character arising as a result of this License or |
| 21 | + * out of the use or inability to use the software (including but not limited to damages |
| 22 | + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and |
| 23 | + * all other commercial damages or losses), even if such Contributor has been advised |
| 24 | + * of the possibility of such damages. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <string> |
| 28 | + |
| 29 | +namespace |
| 30 | +{ |
| 31 | + |
| 32 | +namespace MSL |
| 33 | +{ |
| 34 | + |
| 35 | +// clang-format off |
| 36 | +const std::string MeshShaderTest{ |
| 37 | +R"( |
| 38 | +#include <metal_stdlib> |
| 39 | +using namespace metal; |
| 40 | +
|
| 41 | +struct VertexOut |
| 42 | +{ |
| 43 | + float4 position [[position]]; |
| 44 | + float3 color; |
| 45 | +}; |
| 46 | +
|
| 47 | +using TriMesh = metal::mesh<VertexOut, void, 4, 2, metal::topology::triangle>; |
| 48 | +
|
| 49 | +[[mesh]] |
| 50 | +void MSmain(uint tid [[thread_index_in_threadgroup]], |
| 51 | + TriMesh output) |
| 52 | +{ |
| 53 | + if (tid == 0) |
| 54 | + output.set_primitive_count(2); |
| 55 | +
|
| 56 | + const float3 colors[4] = {float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), |
| 57 | + float3(0.0, 0.0, 1.0), float3(1.0, 1.0, 1.0)}; |
| 58 | +
|
| 59 | + VertexOut v; |
| 60 | + v.position = float4(float(tid >> 1) * 2.0 - 1.0, float(tid & 1) * 2.0 - 1.0, 0.0, 1.0); |
| 61 | + v.color = colors[tid]; |
| 62 | + output.set_vertex(tid, v); |
| 63 | +
|
| 64 | + // Triangle 0: (0, 1, 2) |
| 65 | + if (tid == 0) |
| 66 | + { |
| 67 | + output.set_index(0, 0); |
| 68 | + output.set_index(1, 1); |
| 69 | + output.set_index(2, 2); |
| 70 | + } |
| 71 | + // Triangle 1: (2, 1, 3) |
| 72 | + if (tid == 3) |
| 73 | + { |
| 74 | + output.set_index(3, 2); |
| 75 | + output.set_index(4, 1); |
| 76 | + output.set_index(5, 3); |
| 77 | + } |
| 78 | +} |
| 79 | +
|
| 80 | +struct FSOut |
| 81 | +{ |
| 82 | + float4 color [[color(0)]]; |
| 83 | +}; |
| 84 | +
|
| 85 | +fragment FSOut PSmain(VertexOut in [[stage_in]]) |
| 86 | +{ |
| 87 | + FSOut out; |
| 88 | + out.color = float4(in.color, 1.0); |
| 89 | + return out; |
| 90 | +} |
| 91 | +)" |
| 92 | +}; |
| 93 | + |
| 94 | +const std::string AmplificationShaderTest{ |
| 95 | +R"( |
| 96 | +#include <metal_stdlib> |
| 97 | +using namespace metal; |
| 98 | +
|
| 99 | +struct VertexOut |
| 100 | +{ |
| 101 | + float4 position [[position]]; |
| 102 | + float3 color; |
| 103 | +}; |
| 104 | +
|
| 105 | +struct Payload |
| 106 | +{ |
| 107 | + uint baseID; |
| 108 | + uint subIDs[8]; |
| 109 | +}; |
| 110 | +
|
| 111 | +// Object (amplification) shader |
| 112 | +[[object]] |
| 113 | +void OBJmain(uint tid [[thread_index_in_threadgroup]], |
| 114 | + uint gid [[threadgroup_position_in_grid]], |
| 115 | + object_data Payload& payload [[payload]], |
| 116 | + mesh_grid_properties mgp) |
| 117 | +{ |
| 118 | + if (tid == 0) |
| 119 | + payload.baseID = gid * 8; |
| 120 | + payload.subIDs[tid] = tid; |
| 121 | +
|
| 122 | + if (tid == 0) |
| 123 | + mgp.set_threadgroups_per_grid(uint3(8, 1, 1)); |
| 124 | +} |
| 125 | +
|
| 126 | +using SmallTriMesh = metal::mesh<VertexOut, void, 3, 1, metal::topology::triangle>; |
| 127 | +
|
| 128 | +// Mesh shader for amplification test |
| 129 | +[[mesh]] |
| 130 | +void AmpMSmain(uint gid [[threadgroup_position_in_grid]], |
| 131 | + const object_data Payload& payload [[payload]], |
| 132 | + SmallTriMesh output) |
| 133 | +{ |
| 134 | + output.set_primitive_count(1); |
| 135 | +
|
| 136 | + uint meshletID = payload.baseID + payload.subIDs[gid]; |
| 137 | +
|
| 138 | + const float3 colors[4] = {float3(1.0, 0.0, 0.0), float3(0.0, 1.0, 0.0), |
| 139 | + float3(0.0, 0.0, 1.0), float3(1.0, 0.0, 1.0)}; |
| 140 | +
|
| 141 | + float2 center; |
| 142 | + center.x = (float((meshletID % 9) + 1) / 10.0) * 2.0 - 1.0; |
| 143 | + center.y = (float((meshletID / 9) + 1) / 10.0) * 2.0 - 1.0; |
| 144 | +
|
| 145 | + VertexOut v; |
| 146 | + v.color = colors[meshletID & 3]; |
| 147 | +
|
| 148 | + v.position = float4(center.x, center.y + 0.09, 0.0, 1.0); |
| 149 | + output.set_vertex(0, v); |
| 150 | +
|
| 151 | + v.position = float4(center.x - 0.09, center.y - 0.09, 0.0, 1.0); |
| 152 | + output.set_vertex(1, v); |
| 153 | +
|
| 154 | + v.position = float4(center.x + 0.09, center.y - 0.09, 0.0, 1.0); |
| 155 | + output.set_vertex(2, v); |
| 156 | +
|
| 157 | + output.set_index(0, 2); |
| 158 | + output.set_index(1, 1); |
| 159 | + output.set_index(2, 0); |
| 160 | +} |
| 161 | +
|
| 162 | +struct FSOut |
| 163 | +{ |
| 164 | + float4 color [[color(0)]]; |
| 165 | +}; |
| 166 | +
|
| 167 | +fragment FSOut AmpPSmain(VertexOut in [[stage_in]]) |
| 168 | +{ |
| 169 | + FSOut out; |
| 170 | + out.color = float4(in.color, 1.0); |
| 171 | + return out; |
| 172 | +} |
| 173 | +)" |
| 174 | +}; |
| 175 | +// clang-format on |
| 176 | + |
| 177 | +} // namespace MSL |
| 178 | + |
| 179 | +} // namespace |
0 commit comments