Repository navigation
Expand file tree
/
Copy pathMeshShaderTriangleSession.cpp
More file actions
351 lines (291 loc) · 10.2 KB
/
Copy pathMeshShaderTriangleSession.cpp
File metadata and controls
351 lines (291 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
// @fb-only
#include <shell/renderSessions/MeshShaderTriangleSession.h>
#include <shell/shared/renderSession/ShellParams.h>
#include <igl/NameHandle.h>
#include <igl/ShaderCreator.h>
namespace igl::shell {
namespace {
std::string getMetalTaskShaderSource() {
return R"(
using namespace metal;
[[object]]
void taskMain(mesh_grid_properties meshGridProperties) {
meshGridProperties.set_threadgroups_per_grid(uint3(1, 1, 1));
}
)";
}
std::string getMetalMeshShaderSource() {
return R"(
using namespace metal;
struct VertexOut{
float4 position [[position]];
float4 color [[user(locn0)]];
};
struct UniformBlock {
float4x4 mvpMatrix;
};
using TriangleMeshType = metal::mesh<VertexOut, void, 64, 64, metal::topology::triangle>;
constant float4 vertexData[3] = {{-0.6f, -0.4f, 0.0, 1.0}, {0.6f, -0.4f, 0.0, 1.0}, {0.0f, 0.6f, 0.0, 1.0}};
constant float4 colorData[3] = {{1.0, 0.0, 0.0, 1.0}, {0.0, 1.0, 0.0, 1.0}, {0.0, 0.0, 1.0, 1.0}};
[[mesh]]
void meshMain(TriangleMeshType output, constant UniformBlock &vUniform[[buffer(1)]]) {
output.set_primitive_count(1);
for (int i = 0; i != 3; ++i){
VertexOut v;
v.position = vUniform.mvpMatrix * vertexData[i];
v.color = colorData[i];
output.set_vertex(i, v);
}
output.set_index(0, 0);
output.set_index(1, 1);
output.set_index(2, 2);
}
)";
}
std::string getMetalFragmentShaderSource() {
return R"(
using namespace metal;
struct FS_IN{
float4 color [[user(locn0)]];
};
fragment float4 fragmentMain(FS_IN in [[stage_in]]) {
return in.color;
}
)";
}
std::string getVulkanTaskShaderSource() {
return R"(
#version 460
#extension GL_EXT_mesh_shader : enable
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main(){
EmitMeshTasksEXT(1,1,1);
}
)";
}
std::string getVulkanMeshShaderSource() {
return R"(
#version 460
#extension GL_EXT_mesh_shader : enable
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
layout(set = 1, binding = 1, std140) uniform UniformBlock {
mat4 mvpMatrix;
};
layout(location = 0) out PerVertexData {vec4 color; } v_out[];
layout(triangles, max_vertices = 3, max_primitives = 1) out;
const vec4 vertexData[3] = {{-0.6f, -0.4f, 0.0, 1.0}, {0.6f, -0.4f, 0.0, 1.0}, {0.0f, 0.6f, 0.0, 1.0}};
const vec4 colorData[3] = {{1.0, 0.0, 0.0, 1.0}, {0.0, 1.0, 0.0, 1.0}, {0.0, 0.0, 1.0, 1.0}};
void main(){
SetMeshOutputsEXT(3, 1);
for (int i = 0; i != 3; ++i) {
gl_MeshVerticesEXT[i].gl_Position = mvpMatrix * vertexData[i];
v_out[i].color = colorData[i];
}
gl_PrimitiveTriangleIndicesEXT[0] = uvec3(0, 1, 2);
}
)";
}
std::string getVulkanFragmentShaderSource() {
return R"(
#version 460
layout(location = 0) in vec4 color;
layout(location = 0) out vec4 out_FragColor;
void main() {
out_FragColor = color;
}
)";
}
std::unique_ptr<IShaderStages> getShaderStagesForBackend(IDevice& device) {
std::string taskShader, meshShader, fragmentShader;
std::string taskShaderEntryPoint, meshShaderEntryPoint, fragmentShaderEntryPoint;
// NOLINTNEXTLINE(clang-diagnostic-switch-enum)
switch (device.getBackendType()) {
case igl::BackendType::Metal:
taskShader = getMetalTaskShaderSource();
meshShader = getMetalMeshShaderSource();
fragmentShader = getMetalFragmentShaderSource();
taskShaderEntryPoint = "taskMain";
meshShaderEntryPoint = "meshMain";
fragmentShaderEntryPoint = "fragmentMain";
break;
case igl::BackendType::Vulkan:
taskShader = getVulkanTaskShaderSource();
meshShader = getVulkanMeshShaderSource();
fragmentShader = getVulkanFragmentShaderSource();
taskShaderEntryPoint = "main";
meshShaderEntryPoint = "main";
fragmentShaderEntryPoint = "main";
break;
default:
break;
}
if (taskShader.empty() || meshShader.empty() || fragmentShader.empty()) {
IGL_DEBUG_ASSERT_NOT_IMPLEMENTED();
return nullptr;
}
auto taskModule = igl::ShaderModuleCreator::fromStringInput(
device,
taskShader.c_str(),
{.stage = igl::ShaderStage::Task, .entryPoint = taskShaderEntryPoint},
"task shader",
nullptr);
auto meshModule = igl::ShaderModuleCreator::fromStringInput(
device,
meshShader.c_str(),
{.stage = igl::ShaderStage::Mesh, .entryPoint = meshShaderEntryPoint},
"mesh shader",
nullptr);
auto fragmentModule = igl::ShaderModuleCreator::fromStringInput(
device,
fragmentShader.c_str(),
{.stage = igl::ShaderStage::Fragment, .entryPoint = fragmentShaderEntryPoint},
"fragment shader",
nullptr);
Result result;
auto shaderStages = igl::ShaderStagesCreator::fromMeshRenderModules(
device, taskModule, meshModule, fragmentModule, &result);
IGL_DEBUG_ASSERT(shaderStages);
return shaderStages;
}
// WebGPU has no mesh shaders: a vertex shader emits the same triangle from the vertex index.
const char* getWgslVertexFallbackShaderSource() {
return R"(
struct UniformBlock {
mvpMatrix : mat4x4f,
};
@group(1) @binding(1) var<uniform> vUniform : UniformBlock;
struct VertexOut {
@builtin(position) position : vec4f,
@location(0) color : vec4f,
};
@vertex
fn vertexMain(@builtin(vertex_index) i : u32) -> VertexOut {
var vertexData = array<vec4f, 3>(
vec4f(-0.6, -0.4, 0.0, 1.0), vec4f(0.6, -0.4, 0.0, 1.0), vec4f(0.0, 0.6, 0.0, 1.0));
var colorData = array<vec4f, 3>(
vec4f(1.0, 0.0, 0.0, 1.0), vec4f(0.0, 1.0, 0.0, 1.0), vec4f(0.0, 0.0, 1.0, 1.0));
return VertexOut(vUniform.mvpMatrix * vertexData[i], colorData[i]);
}
@fragment
fn fragmentMain(v : VertexOut) -> @location(0) vec4f {
return v.color;
}
)";
}
} // namespace
// NOLINTNEXTLINE(bugprone-exception-escape)
void MeshShaderTriangleSession::initialize() noexcept {
auto& device = getPlatform().getDevice();
useMeshShaders_ = device.hasFeature(DeviceFeatures::MeshShaders);
if (useMeshShaders_) {
shaderStages_ = getShaderStagesForBackend(device);
} else if (device.getShaderVersion().family == ShaderFamily::Wgsl) {
shaderStages_ = ShaderStagesCreator::fromLibraryStringInput(
device, getWgslVertexFallbackShaderSource(), "vertexMain", "fragmentMain", "", nullptr);
} else {
IGL_LOG_INFO("MeshShaderTriangleSession: mesh shaders are not supported; only clearing\n");
}
const BufferDesc uboDesc{
.type = igl::BufferDesc::BufferTypeBits::Uniform,
.length = sizeof(glm::mat4),
.storage = igl::ResourceStorage::Shared,
};
ubo_ = device.createBuffer(uboDesc, nullptr);
IGL_DEBUG_ASSERT(ubo_ != nullptr);
// Command queue
commandQueue_ = device.createCommandQueue({}, nullptr);
IGL_DEBUG_ASSERT(commandQueue_ != nullptr);
renderPass_.colorAttachments = {{
.loadAction = LoadAction::Clear,
.storeAction = StoreAction::Store,
.clearColor = getPreferredClearColor(),
}};
renderPass_.depthAttachment = {
.loadAction = LoadAction::Clear,
.clearDepth = 1.0,
};
}
// NOLINTNEXTLINE(bugprone-exception-escape)
void MeshShaderTriangleSession::update(SurfaceTextures surfaceTextures) noexcept {
// Per IGL guidelines, surfaceTextures.color may be null on some platforms
// before the surface is ready (e.g., during window resize on Android/iOS).
if (!surfaceTextures.color) {
return;
}
Result ret;
if (!framebuffer_) {
const FramebufferDesc framebufferDesc = {
.colorAttachments = {{.texture = surfaceTextures.color}},
.depthAttachment = {.texture = surfaceTextures.depth},
.stencilAttachment =
(surfaceTextures.depth && surfaceTextures.depth->getProperties().hasStencil())
? igl::FramebufferDesc::AttachmentDesc{.texture = surfaceTextures.depth}
: igl::FramebufferDesc::AttachmentDesc{},
};
framebuffer_ = getPlatform().getDevice().createFramebuffer(framebufferDesc, &ret);
IGL_DEBUG_ASSERT(ret.isOk());
IGL_DEBUG_ASSERT(framebuffer_ != nullptr);
} else {
framebuffer_->updateDrawable(surfaceTextures);
}
// Graphics pipeline
if (!pipelineState_ && shaderStages_) {
const RenderPipelineDesc graphicsDesc = {
.shaderStages = shaderStages_,
.targetDesc =
{
.colorAttachments = {{.textureFormat =
framebuffer_->getColorAttachment(0)->getFormat()}},
.depthAttachmentFormat = framebuffer_->getDepthAttachment()->getFormat(),
.stencilAttachmentFormat = framebuffer_->getStencilAttachment()
? framebuffer_->getStencilAttachment()->getFormat()
: igl::TextureFormat::Invalid,
},
.cullMode = igl::CullMode::Disabled,
.frontFaceWinding = igl::WindingMode::Clockwise,
};
pipelineState_ = getPlatform().getDevice().createRenderPipeline(graphicsDesc, &ret);
IGL_DEBUG_ASSERT(ret.isOk());
IGL_DEBUG_ASSERT(pipelineState_ != nullptr);
}
// Command Buffers
const auto buffer = commandQueue_->createCommandBuffer({}, &ret);
IGL_DEBUG_ASSERT(ret.isOk());
IGL_DEBUG_ASSERT(buffer != nullptr);
auto drawableSurface = framebuffer_->getColorAttachment(0);
frameNum_ = (++frameNum_) % 360;
const float angle = static_cast<float>(frameNum_) * M_PI / 180.0f;
const glm::mat4 matrix = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0.0f, 0.0f, 1.0f));
if (pipelineState_ && ubo_) {
ubo_->upload(&matrix, {sizeof(matrix)});
}
// Submit commands
const std::shared_ptr<IRenderCommandEncoder> commands =
buffer->createRenderCommandEncoder(renderPass_, framebuffer_);
IGL_DEBUG_ASSERT(commands != nullptr);
if (pipelineState_) {
commands->bindRenderPipelineState(pipelineState_);
if (useMeshShaders_) {
commands->bindBuffer(1, BindTarget::kMesh, ubo_.get());
commands->drawMeshTasks({1, 1, 1}, {1, 1, 1}, {1, 1, 1});
} else {
commands->bindBuffer(1, BindTarget::kVertex, ubo_.get());
commands->draw(3);
}
}
commands->endEncoding();
IGL_DEBUG_ASSERT(buffer != nullptr);
if (shellParams().shouldPresent) {
buffer->present(drawableSurface);
}
IGL_DEBUG_ASSERT(commandQueue_ != nullptr);
commandQueue_->submit(*buffer);
RenderSession::update(surfaceTextures);
}
} // namespace igl::shell