-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmesh_triangle.cc
More file actions
84 lines (63 loc) · 2.05 KB
/
mesh_triangle.cc
File metadata and controls
84 lines (63 loc) · 2.05 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
// A triangle generated via Mesh Shaders.
#include "rafx.h"
#include <stddef.h>
const char* shaderSource = R"(
struct VertexOut {
float4 position : SV_Position;
float4 color : COLOR;
};
// numthreads: Number of threads per workgroup (meshlet)
// outputtopology: "triangle" or "line"
[shader("mesh")]
[numthreads(1, 1, 1)]
[outputtopology("triangle")]
void meshMain(
out indices uint3 primIndices[1],
out vertices VertexOut verts[3],
uint3 dispatchThreadID : SV_DispatchThreadID
) {
SetMeshOutputCounts(3, 1);
// top
verts[0].position = float4(0.0, 0.5, 0.0, 1.0);
verts[0].color = float4(1.0, 0.0, 0.0, 1.0);
// bottom right
verts[1].position = float4(0.5, -0.5, 0.0, 1.0);
verts[1].color = float4(0.0, 1.0, 0.0, 1.0);
// bottom left
verts[2].position = float4(-0.5, -0.5, 0.0, 1.0);
verts[2].color = float4(0.0, 0.0, 1.0, 1.0);
// indices
primIndices[0] = uint3(0, 1, 2);
}
[shader("fragment")]
float4 fragmentMain(VertexOut input) : SV_Target {
return input.color;
}
)";
int main(void) {
if (!rfxOpenWindow("Rafx Mesh Shader Triangle", 1280, 720))
return 1;
RfxShader shader = rfxCompileShaderMem(shaderSource, NULL, 0, NULL, 0);
if (!shader)
return 1;
RfxPipelineDesc pipelineDesc = {};
pipelineDesc.shader = shader;
pipelineDesc.topology = RFX_TOPOLOGY_TRIANGLE_LIST;
pipelineDesc.cullMode = RFX_CULL_NONE;
pipelineDesc.colorFormat = rfxGetSwapChainFormat();
RfxPipeline pipeline = rfxCreatePipeline(&pipelineDesc);
while (!rfxWindowShouldClose()) {
rfxBeginFrame();
RfxCommandList cmd = rfxGetCommandList();
rfxCmdBeginSwapchainRenderPass(cmd, RFX_FORMAT_UNKNOWN, RFX_COLOR(20, 20, 20, 255));
rfxCmdBindPipeline(cmd, pipeline);
// emit exactly one triangle ([numthreads(1,1,1)] => x=1 y=1 z=1)
rfxCmdDrawMeshTasks(cmd, 1, 1, 1);
rfxCmdEndRenderPass(cmd);
rfxEndFrame();
}
// cleanup
rfxDestroyPipeline(pipeline);
rfxDestroyShader(shader);
return 0;
}