Skip to content

Commit 0f0a669

Browse files
Samples: 005_MeshShaders is now bilingual (GLSL + Slang)
1 parent 4581549 commit 0f0a669

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

samples/005_MeshShaders.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,65 @@
1010
// we are going to use raw Vulkan here to initialize VK_EXT_mesh_shader
1111
#include <lvk/vulkan/VulkanUtils.h>
1212

13+
const char* codeSlang = R"(
14+
struct VertexOutput {
15+
float4 sv_Position : SV_Position;
16+
float3 color : COLOR0;
17+
};
18+
19+
struct PrimitiveOutput {
20+
bool sv_CullPrimitive : SV_CullPrimitive;
21+
};
22+
23+
struct MeshPayload {
24+
uint unused;
25+
};
26+
27+
[shader("amplification")]
28+
[numthreads(1, 1, 1)]
29+
void taskMain() {
30+
MeshPayload p;
31+
DispatchMesh(1, 1, 1, p); // Slang requires the payload arg
32+
}
33+
34+
[shader("mesh")]
35+
[numthreads(1, 1, 1)]
36+
[outputtopology("triangle")]
37+
void meshMain(
38+
in payload MeshPayload meshPayload,
39+
OutputVertices<VertexOutput, 3> verts,
40+
OutputIndices<uint3, 1> triangles,
41+
OutputPrimitives<PrimitiveOutput, 1> primitives)
42+
{
43+
const float2 pos[3] = {
44+
float2(-0.6, -0.4),
45+
float2( 0.6, -0.4),
46+
float2( 0.0, 0.6)
47+
};
48+
49+
const float3 col[3] = {
50+
float3(1.0, 0.0, 0.0),
51+
float3(0.0, 1.0, 0.0),
52+
float3(0.0, 0.0, 1.0)
53+
};
54+
55+
SetMeshOutputCounts(3, 1);
56+
57+
for (uint i = 0; i < 3; i++) {
58+
verts[i].sv_Position = float4(pos[i], 0.0, 1.0);
59+
verts[i].color = col[i];
60+
}
61+
62+
primitives[0].sv_CullPrimitive = false;
63+
triangles[0] = uint3(0, 1, 2);
64+
}
65+
66+
[shader("fragment")]
67+
float4 fragmentMain(VertexOutput input) : SV_Target {
68+
return float4(input.color, 1.0);
69+
}
70+
)";
71+
1372
const char* codeTask = R"(
1473
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
1574
@@ -83,9 +142,15 @@ VULKAN_APP_MAIN {
83142

84143
lvk::IContext* ctx = app.ctx_.get();
85144

145+
#if defined(LVK_DEMO_WITH_SLANG)
146+
res.task_ = ctx->createShaderModule({codeSlang, lvk::Stage_Task, "Shader Module: main (task)"});
147+
res.mesh_ = ctx->createShaderModule({codeSlang, lvk::Stage_Mesh, "Shader Module: main (mesh)"});
148+
res.frag_ = ctx->createShaderModule({codeSlang, lvk::Stage_Frag, "Shader Module: main (frag)"});
149+
#else
86150
res.task_ = ctx->createShaderModule({codeTask, lvk::Stage_Task, "Shader Module: main (task)"});
87151
res.mesh_ = ctx->createShaderModule({codeMesh, lvk::Stage_Mesh, "Shader Module: main (mesh)"});
88152
res.frag_ = ctx->createShaderModule({codeFrag, lvk::Stage_Frag, "Shader Module: main (frag)"});
153+
#endif // defined(LVK_DEMO_WITH_SLANG)
89154

90155
res.renderPipelineState_Triangle_ = ctx->createRenderPipeline(
91156
{

samples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ ADD_DEMO("003_RenderToCubeMapSinglePass" LVK_WITH_SLANG)
153153
ADD_DEMO("004_YUV" LVK_WITH_SLANG)
154154
if(WIN32 OR UNIX AND NOT (APPLE))
155155
# Windows and Linux
156-
ADD_DEMO("005_MeshShaders")
156+
ADD_DEMO("005_MeshShaders" LVK_WITH_SLANG)
157157
endif()
158158
ADD_DEMO("006_SwapchainHDR")
159159
ADD_DEMO("009_TriplanarMapping" LVK_WITH_SLANG)

0 commit comments

Comments
 (0)