-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathclear_screen.c
More file actions
122 lines (102 loc) · 2.84 KB
/
Copy pathclear_screen.c
File metadata and controls
122 lines (102 loc) · 2.84 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
#include "webgpu/wgpu_common.h"
#include <math.h>
#ifdef __WAJIC__
#define WAJIC_TIME_IMPL
#include <wajic_time.h>
#else
#define SOKOL_TIME_IMPL
#include <sokol_time.h>
#endif
/* -------------------------------------------------------------------------- *
* WebGPU Example - Clear Screen
*
* This example shows how to set up a swap chain and clearing the screen.
*
* Ref:
* https://tsherif.github.io/webgpu-examples
* https://github.com/tsherif/webgpu-examples/blob/gh-pages/blank.html
* -------------------------------------------------------------------------- */
static struct {
WGPURenderPassColorAttachment color_attachment;
WGPURenderPassDescriptor render_pass_descriptor;
WGPUBool initialized;
} state = {
.color_attachment = {
.loadOp = WGPULoadOp_Clear,
.storeOp = WGPUStoreOp_Store,
.clearValue = {1.0, 1.0, 1.0, 1.0},
.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED,
},
.render_pass_descriptor = {
.colorAttachmentCount = 1,
.colorAttachments = &state.color_attachment,
},
};
static int init(struct wgpu_context_t* wgpu_context)
{
if (wgpu_context) {
stm_setup();
state.initialized = 1;
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
static WGPUColor lerp(WGPUColor* a, WGPUColor* b, float t)
{
return (WGPUColor){
.r = (1 - t) * a->r + t * b->r,
.g = (1 - t) * a->g + t * b->g,
.b = (1 - t) * a->b + t * b->b,
};
}
static int frame(struct wgpu_context_t* wgpu_context)
{
if (!state.initialized) {
return EXIT_FAILURE;
}
WGPUDevice device = wgpu_context->device;
WGPUQueue queue = wgpu_context->queue;
state.color_attachment.view = wgpu_context->swapchain_view;
/* Figure out how far along duration we are, between 0.0 and 1.0 */
const float t = cos(stm_sec(stm_now())) * 0.5f + 0.5f;
/* Interpolate between two colors */
state.color_attachment.clearValue = lerp(
&(WGPUColor){
.r = 0.0,
.g = 0.0,
.b = 0.0,
},
&(WGPUColor){
.r = 1.0,
.g = 1.0,
.b = 1.0,
},
t);
WGPUCommandEncoder cmd_enc = wgpuDeviceCreateCommandEncoder(device, NULL);
WGPURenderPassEncoder rpass_enc
= wgpuCommandEncoderBeginRenderPass(cmd_enc, &state.render_pass_descriptor);
/* Record render commands. */
wgpuRenderPassEncoderEnd(rpass_enc);
WGPUCommandBuffer cmd_buffer = wgpuCommandEncoderFinish(cmd_enc, NULL);
/* Submit and present. */
wgpuQueueSubmit(queue, 1, &cmd_buffer);
/* Cleanup */
wgpuRenderPassEncoderRelease(rpass_enc);
wgpuCommandBufferRelease(cmd_buffer);
wgpuCommandEncoderRelease(cmd_enc);
return EXIT_SUCCESS;
}
static void shutdown(struct wgpu_context_t* wgpu_context)
{
UNUSED_VAR(wgpu_context);
}
int main(void)
{
wgpu_start(&(wgpu_desc_t){
.title = "Clear Screen",
.init_cb = init,
.frame_cb = frame,
.shutdown_cb = shutdown,
});
return EXIT_SUCCESS;
}