Skip to content

Commit de4aaad

Browse files
Simplify examples (#353)
1 parent 5c59cd5 commit de4aaad

File tree

4 files changed

+135
-238
lines changed

4 files changed

+135
-238
lines changed

examples/capture/main.c

Lines changed: 47 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
#define LOG_PREFIX "[capture]"
1111

12-
#define COPY_BYTES_PER_ROW_ALIGNMENT 256
12+
const size_t IMAGE_WIDTH = 100;
13+
const size_t IMAGE_HEIGHT = 200;
14+
const size_t COPY_BYTES_PER_ROW_ALIGNMENT = 256;
1315

1416
static void handle_request_adapter(WGPURequestAdapterStatus status,
1517
WGPUAdapter adapter, char const *message,
@@ -43,9 +45,10 @@ static void buffer_dimensions_init(BufferDimensions *r, size_t width,
4345

4446
const size_t bytes_per_pixel = sizeof(uint32_t);
4547
const size_t unpadded_bytes_per_row = width * bytes_per_pixel;
46-
const size_t align = COPY_BYTES_PER_ROW_ALIGNMENT;
4748
const size_t padded_bytes_per_row_padding =
48-
(align - unpadded_bytes_per_row % align) % align;
49+
(COPY_BYTES_PER_ROW_ALIGNMENT -
50+
unpadded_bytes_per_row % COPY_BYTES_PER_ROW_ALIGNMENT) %
51+
COPY_BYTES_PER_ROW_ALIGNMENT;
4952
const size_t padded_bytes_per_row =
5053
unpadded_bytes_per_row + padded_bytes_per_row_padding;
5154

@@ -58,69 +61,45 @@ static void buffer_dimensions_init(BufferDimensions *r, size_t width,
5861
int main(int argc, char *argv[]) {
5962
UNUSED(argc)
6063
UNUSED(argv)
61-
WGPUInstance instance = NULL;
62-
WGPUAdapter adapter = NULL;
63-
WGPUDevice device = NULL;
64-
WGPUQueue queue = NULL;
65-
WGPUBuffer output_buffer = NULL;
66-
WGPUTexture texture = NULL;
67-
WGPUTextureView texture_view = NULL;
68-
WGPUCommandEncoder command_encoder = NULL;
69-
WGPURenderPassEncoder render_pass_encoder = NULL;
70-
WGPUCommandBuffer command_buffer = NULL;
71-
uint8_t *buf = NULL;
72-
int ret = EXIT_SUCCESS;
73-
74-
#define ASSERT_CHECK(expr) \
75-
do { \
76-
if (!(expr)) { \
77-
ret = EXIT_FAILURE; \
78-
printf(LOG_PREFIX " assert failed (%s): %s:%d\n", #expr, __FILE__, \
79-
__LINE__); \
80-
goto cleanup_and_exit; \
81-
} \
82-
} while (0)
83-
84-
const size_t width = 100;
85-
const size_t height = 200;
86-
8764
frmwrk_setup_logging(WGPULogLevel_Warn);
8865

89-
instance = wgpuCreateInstance(NULL);
90-
ASSERT_CHECK(instance);
66+
WGPUInstance instance = wgpuCreateInstance(NULL);
67+
assert(instance);
9168

69+
WGPUAdapter adapter = NULL;
9270
wgpuInstanceRequestAdapter(instance, NULL, handle_request_adapter,
9371
(void *)&adapter);
94-
ASSERT_CHECK(adapter);
72+
assert(adapter);
9573

74+
WGPUDevice device = NULL;
9675
wgpuAdapterRequestDevice(adapter, NULL, handle_request_device,
9776
(void *)&device);
98-
ASSERT_CHECK(device);
99-
queue = wgpuDeviceGetQueue(device);
100-
ASSERT_CHECK(queue);
77+
assert(device);
78+
WGPUQueue queue = wgpuDeviceGetQueue(device);
79+
assert(queue);
10180

10281
BufferDimensions buffer_dimensions = {0};
103-
buffer_dimensions_init(&buffer_dimensions, width, height);
82+
buffer_dimensions_init(&buffer_dimensions, IMAGE_WIDTH, IMAGE_HEIGHT);
10483

10584
const size_t buffer_size =
10685
(buffer_dimensions.padded_bytes_per_row * buffer_dimensions.height);
10786

108-
output_buffer = wgpuDeviceCreateBuffer(
87+
WGPUBuffer output_buffer = wgpuDeviceCreateBuffer(
10988
device, &(const WGPUBufferDescriptor){
11089
.label = "output_buffer",
11190
.size = buffer_size,
11291
.usage = WGPUBufferUsage_MapRead | WGPUBufferUsage_CopyDst,
11392
.mappedAtCreation = false,
11493
});
115-
ASSERT_CHECK(output_buffer);
94+
assert(output_buffer);
11695

11796
const WGPUExtent3D texture_extent = (const WGPUExtent3D){
11897
.width = buffer_dimensions.width,
11998
.height = buffer_dimensions.height,
12099
.depthOrArrayLayers = 1,
121100
};
122101

123-
texture = wgpuDeviceCreateTexture(
102+
WGPUTexture texture = wgpuDeviceCreateTexture(
124103
device,
125104
&(const WGPUTextureDescriptor){
126105
.label = "texture",
@@ -131,18 +110,17 @@ int main(int argc, char *argv[]) {
131110
.format = WGPUTextureFormat_RGBA8UnormSrgb,
132111
.usage = WGPUTextureUsage_RenderAttachment | WGPUTextureUsage_CopySrc,
133112
});
134-
ASSERT_CHECK(texture);
135-
136-
texture_view = wgpuTextureCreateView(texture, NULL);
137-
ASSERT_CHECK(texture_view);
113+
assert(texture);
114+
WGPUTextureView texture_view = wgpuTextureCreateView(texture, NULL);
115+
assert(texture_view);
138116

139-
command_encoder = wgpuDeviceCreateCommandEncoder(
117+
WGPUCommandEncoder command_encoder = wgpuDeviceCreateCommandEncoder(
140118
device, &(const WGPUCommandEncoderDescriptor){
141119
.label = "command_encoder",
142120
});
143-
ASSERT_CHECK(command_encoder);
121+
assert(command_encoder);
144122

145-
render_pass_encoder = wgpuCommandEncoderBeginRenderPass(
123+
WGPURenderPassEncoder render_pass_encoder = wgpuCommandEncoderBeginRenderPass(
146124
command_encoder, &(const WGPURenderPassDescriptor){
147125
.label = "rende_pass_encoder",
148126
.colorAttachmentCount = 1,
@@ -162,7 +140,7 @@ int main(int argc, char *argv[]) {
162140
},
163141
},
164142
});
165-
ASSERT_CHECK(render_pass_encoder);
143+
assert(render_pass_encoder);
166144

167145
wgpuRenderPassEncoderEnd(render_pass_encoder);
168146

@@ -185,51 +163,36 @@ int main(int argc, char *argv[]) {
185163
},
186164
&texture_extent);
187165

188-
command_buffer = wgpuCommandEncoderFinish(
166+
WGPUCommandBuffer command_buffer = wgpuCommandEncoderFinish(
189167
command_encoder, &(const WGPUCommandBufferDescriptor){
190168
.label = "command_buffer",
191169
});
192-
ASSERT_CHECK(command_buffer);
170+
assert(command_buffer);
193171

194172
wgpuQueueSubmit(queue, 1, (const WGPUCommandBuffer[]){command_buffer});
195173

196174
wgpuBufferMapAsync(output_buffer, WGPUMapMode_Read, 0, buffer_size,
197175
handle_buffer_map, NULL);
198176
wgpuDevicePoll(device, true, NULL);
199177

200-
buf = (uint8_t *)wgpuBufferGetConstMappedRange(output_buffer, 0, buffer_size);
201-
ASSERT_CHECK(buf);
202-
203-
ASSERT_CHECK(stbi_write_png("red.png", buffer_dimensions.width,
204-
buffer_dimensions.height, 4, buf,
205-
buffer_dimensions.padded_bytes_per_row));
206-
207-
cleanup_and_exit:
208-
if (buf) {
209-
wgpuBufferUnmap(output_buffer);
210-
// mapped buf is unusable after wgpuBufferUnmap()
211-
buf = NULL;
212-
}
213-
if (command_buffer)
214-
wgpuCommandBufferRelease(command_buffer);
215-
if (render_pass_encoder)
216-
wgpuRenderPassEncoderRelease(render_pass_encoder);
217-
if (command_encoder)
218-
wgpuCommandEncoderRelease(command_encoder);
219-
if (texture_view)
220-
wgpuTextureViewRelease(texture_view);
221-
if (texture)
222-
wgpuTextureRelease(texture);
223-
if (output_buffer)
224-
wgpuBufferRelease(output_buffer);
225-
if (queue)
226-
wgpuQueueRelease(queue);
227-
if (device)
228-
wgpuDeviceRelease(device);
229-
if (adapter)
230-
wgpuAdapterRelease(adapter);
231-
if (instance)
232-
wgpuInstanceRelease(instance);
233-
234-
return ret;
178+
uint8_t *buf =
179+
(uint8_t *)wgpuBufferGetConstMappedRange(output_buffer, 0, buffer_size);
180+
assert(buf);
181+
182+
assert(stbi_write_png("red.png", buffer_dimensions.width,
183+
buffer_dimensions.height, 4, buf,
184+
buffer_dimensions.padded_bytes_per_row));
185+
186+
wgpuBufferUnmap(output_buffer);
187+
wgpuCommandBufferRelease(command_buffer);
188+
wgpuRenderPassEncoderRelease(render_pass_encoder);
189+
wgpuCommandEncoderRelease(command_encoder);
190+
wgpuTextureViewRelease(texture_view);
191+
wgpuTextureRelease(texture);
192+
wgpuBufferRelease(output_buffer);
193+
wgpuQueueRelease(queue);
194+
wgpuDeviceRelease(device);
195+
wgpuAdapterRelease(adapter);
196+
wgpuInstanceRelease(instance);
197+
return EXIT_SUCCESS;
235198
}

0 commit comments

Comments
 (0)