This repository has been archived by the owner on Jul 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhello_triangle.c
195 lines (163 loc) · 5.41 KB
/
hello_triangle.c
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
#include <playsys.h>
#include <playwgpu.h>
#include "hello.h"
#ifdef __wasm__
// stub out this for wasm to get the build working sooner
void hello_triangle_set_device(WGPUDevice device) {}
void hello_triangle_set_surface(WGPUSurface surface) {}
void hello_triangle_render() {}
#else
// we have no libc, but llvm has most math functions built-in
#define fabs __builtin_fabs
#define sinf __builtin_sinf
#define cosf __builtin_cosf
static WGPUDevice g_device = NULL;
static WGPUSurface g_surface = NULL;
static WGPURenderPipeline g_pipeline = NULL;
static WGPUSwapChain g_swapchain = NULL;
static WGPUShaderModule create_wgsl_shader(WGPUDevice device, const char* source) {
WGPUShaderModuleWGSLDescriptor wgsl = {
.chain = { .sType = WGPUSType_ShaderModuleWGSLDescriptor },
.source = source,
};
WGPUShaderModuleDescriptor d = { .nextInChain = &wgsl.chain };
return wgpuDeviceCreateShaderModule(device, &d);
}
static WGPURenderPipeline create_pipeline(
WGPUDevice device, WGPUShaderModule vsmod, WGPUShaderModule fsmod)
{
check_notnull(device);
check_notnull(vsmod);
check_notnull(fsmod);
WGPUTextureFormat swapChainFormat = WGPUTextureFormat_BGRA8Unorm; // FIXME
// Fragment state
WGPUBlendState blend = {
.color = {
.operation = WGPUBlendOperation_Add,
.srcFactor = WGPUBlendFactor_One,
.dstFactor = WGPUBlendFactor_One,
},
.alpha = {
.operation = WGPUBlendOperation_Add,
.srcFactor = WGPUBlendFactor_One,
.dstFactor = WGPUBlendFactor_One,
},
};
WGPUColorTargetState colorTarget = {
.format = swapChainFormat,
.blend = &blend,
.writeMask = WGPUColorWriteMask_All,
};
WGPUFragmentState fragment = {
.module = fsmod,
.entryPoint = "main",
.targetCount = 1,
.targets = &colorTarget,
};
WGPURenderPipelineDescriptor pd = {
.vertex = {
.module = vsmod,
.entryPoint = "main",
},
.primitive = {
.frontFace = WGPUFrontFace_CCW,
.cullMode = WGPUCullMode_None,
.topology = WGPUPrimitiveTopology_TriangleList,
.stripIndexFormat = WGPUIndexFormat_Undefined,
},
.multisample = {
.count = 1,
.mask = 0xFFFFFFFF,
.alphaToCoverageEnabled = false,
},
.fragment = &fragment,
};
return wgpuDeviceCreateRenderPipeline(device, &pd);
}
void hello_triangle_set_device(WGPUDevice device) {
wgpuDeviceReference(device);
if (g_device)
wgpuDeviceRelease(g_device);
g_device = device;
WGPUShaderModule vsmod = create_wgsl_shader(device,
"[[stage(vertex)]] fn main(\n"
" [[builtin(vertex_index)]] VertexIndex : u32\n"
") -> [[builtin(position)]] vec4<f32>\n"
"{\n"
" var pos = array<vec2<f32>, 3>(\n"
" vec2<f32>( 0.0, 0.5),\n"
" vec2<f32>(-0.5, -0.5),\n"
" vec2<f32>( 0.5, -0.5));\n"
" return vec4<f32>(pos[VertexIndex], 0.0, 1.0);\n"
"}\n");
WGPUShaderModule fsmod = create_wgsl_shader(device,
"[[stage(fragment)]] fn main() -> [[location(0)]] vec4<f32> {\n"
" return vec4<f32>(1.0, 0.8, 0.0, 1.0);\n"
"}\n");
if (g_pipeline)
wgpuRenderPipelineRelease(g_pipeline);
g_pipeline = create_pipeline(device, vsmod, fsmod);
wgpuShaderModuleRelease(vsmod);
wgpuShaderModuleRelease(fsmod);
}
void hello_triangle_set_surface(WGPUSurface surface) {
wgpuSurfaceReference(surface);
if (g_surface)
wgpuSurfaceRelease(g_surface);
g_surface = surface;
static WGPUSwapChainDescriptor scdesc = {
.usage = WGPUTextureUsage_RenderAttachment,
.format = WGPUTextureFormat_BGRA8Unorm,
.width = 400*2,
.height = 300*2,
.presentMode = WGPUPresentMode_Mailbox,
};
if (g_swapchain)
wgpuSwapChainRelease(g_swapchain);
g_swapchain = wgpuDeviceCreateSwapChain(g_device, surface, &scdesc);
}
void hello_triangle_render() {
#ifdef __wasm__
// no __builtin_{sinf,cosf} in llvm wasm
float RED = 0.0f;
float GREEN = 0.1f;
float BLUE = 0.1f;
#else
static u32 fc = 0;
fc++;
float RED = fabs(sinf((float)(fc*2) / 100.0f));
float GREEN = fabs(sinf((float)(fc*2) / 50.0f));
float BLUE = fabs(cosf((float)(fc*2) / 80.0f));
#endif
WGPUTextureView backbufferView = wgpuSwapChainGetCurrentTextureView(g_swapchain);
WGPURenderPassColorAttachment colorAttachment = {
.view = backbufferView,
.clearColor = (WGPUColor){RED, GREEN, BLUE, 0.0f},
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
};
WGPURenderPassDescriptor renderpassInfo = {
.colorAttachmentCount = 1,
.colorAttachments = &colorAttachment,
.depthStencilAttachment = NULL,
};
// acquire a command encoder
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(g_device, NULL);
// execute a render pass, writing commands to encoder
WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &renderpassInfo);
wgpuRenderPassEncoderSetPipeline(pass, g_pipeline);
wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0);
wgpuRenderPassEncoderEndPass(pass);
wgpuRenderPassEncoderRelease(pass);
// get commands buffered in the encoder
WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, NULL);
wgpuCommandEncoderRelease(encoder);
// send commands to device
WGPUQueue queue = wgpuDeviceGetQueue(g_device);
wgpuQueueSubmit(queue, 1, &commands);
wgpuCommandBufferRelease(commands);
// show the new frame we drew on screen
wgpuSwapChainPresent(g_swapchain);
wgpuTextureViewRelease(backbufferView);
}
#endif // __wasm__