Skip to content

Commit

Permalink
Update C examples to new headers
Browse files Browse the repository at this point in the history
  • Loading branch information
PJB3005 committed Sep 19, 2024
1 parent 880cca0 commit 1e19d06
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 63 deletions.
37 changes: 26 additions & 11 deletions examples/capture/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ const size_t COPY_BYTES_PER_ROW_ALIGNMENT = 256;

static void handle_request_adapter(WGPURequestAdapterStatus status,
WGPUAdapter adapter, char const *message,
void *userdata) {
void *userdata1, void *userdata2) {
UNUSED(status)
UNUSED(message)
*(WGPUAdapter *)userdata = adapter;
UNUSED(userdata2)
*(WGPUAdapter *)userdata1 = adapter;
}
static void handle_request_device(WGPURequestDeviceStatus status,
WGPUDevice device, char const *message,
void *userdata) {
void *userdata1, void *userdata2) {
UNUSED(status)
UNUSED(message)
*(WGPUDevice *)userdata = device;
UNUSED(userdata2)
*(WGPUDevice *)userdata1 = device;
}
static void handle_buffer_map(WGPUBufferMapAsyncStatus status, void *userdata) {
UNUSED(userdata)
static void handle_buffer_map(WGPUBufferMapAsyncStatus status,
char const *message,
void *userdata1, void *userdata2) {
UNUSED(message)
UNUSED(userdata1)
UNUSED(userdata2)
printf(LOG_PREFIX " buffer_map status=%#.8x\n", status);
}

Expand Down Expand Up @@ -67,14 +73,21 @@ int main(int argc, char *argv[]) {
assert(instance);

WGPUAdapter adapter = NULL;
wgpuInstanceRequestAdapter(instance, NULL, handle_request_adapter,
(void *)&adapter);
wgpuInstanceRequestAdapter(instance, NULL,
(const WGPURequestAdapterCallbackInfo){
.callback = handle_request_adapter,
.userdata1 = &adapter
});
assert(adapter);

WGPUDevice device = NULL;
wgpuAdapterRequestDevice(adapter, NULL, handle_request_device,
(void *)&device);
wgpuAdapterRequestDevice(adapter, NULL,
(const WGPURequestDeviceCallbackInfo){
.callback = handle_request_device,
.userdata1 = &device
});
assert(device);

WGPUQueue queue = wgpuDeviceGetQueue(device);
assert(queue);

Expand Down Expand Up @@ -174,7 +187,9 @@ int main(int argc, char *argv[]) {
wgpuQueueSubmit(queue, 1, (const WGPUCommandBuffer[]){command_buffer});

wgpuBufferMapAsync(output_buffer, WGPUMapMode_Read, 0, buffer_size,
handle_buffer_map, NULL);
(const WGPUBufferMapCallbackInfo){
.callback = handle_buffer_map
});
wgpuDevicePoll(device, true, NULL);

uint8_t *buf =
Expand Down
35 changes: 24 additions & 11 deletions examples/compute/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@

static void handle_request_adapter(WGPURequestAdapterStatus status,
WGPUAdapter adapter, char const *message,
void *userdata) {
void *userdata1, void *userdata2) {
UNUSED(status)
UNUSED(message)
*(WGPUAdapter *)userdata = adapter;
UNUSED(userdata2)
*(WGPUAdapter *)userdata1 = adapter;
}
static void handle_request_device(WGPURequestDeviceStatus status,
WGPUDevice device, char const *message,
void *userdata) {
void *userdata1, void *userdata2) {
UNUSED(status)
UNUSED(message)
*(WGPUDevice *)userdata = device;
UNUSED(userdata2)
*(WGPUDevice *)userdata1 = device;
}
static void handle_buffer_map(WGPUBufferMapAsyncStatus status, void *userdata) {
UNUSED(userdata)
static void handle_buffer_map(WGPUBufferMapAsyncStatus status,
char const * message,
void *userdata1, void *userdata2) {
UNUSED(userdata1)
UNUSED(userdata2)
printf(LOG_PREFIX " buffer_map status=%#.8x\n", status);
}

Expand All @@ -38,13 +43,19 @@ int main(int argc, char *argv[]) {
assert(instance);

WGPUAdapter adapter = NULL;
wgpuInstanceRequestAdapter(instance, NULL, handle_request_adapter,
(void *)&adapter);
wgpuInstanceRequestAdapter(instance, NULL,
(const WGPURequestAdapterCallbackInfo){
.callback = handle_request_adapter,
.userdata1 = &adapter
});
assert(adapter);

WGPUDevice device = NULL;
wgpuAdapterRequestDevice(adapter, NULL, handle_request_device,
(void *)&device);
wgpuAdapterRequestDevice(adapter, NULL,
(const WGPURequestDeviceCallbackInfo){
.callback = handle_request_device,
.userdata1 = &device
});
assert(device);

WGPUQueue queue = wgpuDeviceGetQueue(device);
Expand Down Expand Up @@ -139,7 +150,9 @@ int main(int argc, char *argv[]) {
wgpuQueueSubmit(queue, 1, &command_buffer);

wgpuBufferMapAsync(staging_buffer, WGPUMapMode_Read, 0, numbers_size,
handle_buffer_map, NULL);
(const WGPUBufferMapCallbackInfo){
.callback = handle_buffer_map
});
wgpuDevicePoll(device, true, NULL);

uint32_t *buf =
Expand Down
4 changes: 2 additions & 2 deletions examples/framework/framework.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ WGPUShaderModule frmwrk_load_shader_module(WGPUDevice device,
.label = name,
.nextInChain =
(const WGPUChainedStruct *)&(
const WGPUShaderModuleWGSLDescriptor){
const WGPUShaderSourceWGSL){
.chain =
(const WGPUChainedStruct){
.sType = WGPUSType_ShaderModuleWGSLDescriptor,
.sType = WGPUSType_ShaderSourceWGSL,
},
.code = buf,
},
Expand Down
2 changes: 1 addition & 1 deletion examples/framework/framework.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

typedef struct frmwrk_buffer_init_descriptor {
WGPU_NULLABLE char const *label;
WGPUBufferUsageFlags usage;
WGPUBufferUsage usage;
void *content;
size_t content_size;
} frmwrk_buffer_init_descriptor;
Expand Down
52 changes: 30 additions & 22 deletions examples/texture_arrays/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ struct demo {

static void handle_request_adapter(WGPURequestAdapterStatus status,
WGPUAdapter adapter, char const *message,
void *userdata) {
void *userdata1, void *userdata2) {
UNUSED(userdata2)
if (status == WGPURequestAdapterStatus_Success) {
struct demo *demo = userdata;
struct demo *demo = userdata1;
demo->adapter = adapter;
} else {
printf(LOG_PREFIX " request_adapter status=%#.8x message=%s\n", status,
Expand All @@ -38,9 +39,10 @@ static void handle_request_adapter(WGPURequestAdapterStatus status,
}
static void handle_request_device(WGPURequestDeviceStatus status,
WGPUDevice device, char const *message,
void *userdata) {
void *userdata1, void *userdata2) {
UNUSED(userdata2)
if (status == WGPURequestDeviceStatus_Success) {
struct demo *demo = userdata;
struct demo *demo = userdata1;
demo->device = device;
} else {
printf(LOG_PREFIX " request_device status=%#.8x message=%s\n", status,
Expand Down Expand Up @@ -147,10 +149,10 @@ int main(int argc, char *argv[]) {
&(const WGPUSurfaceDescriptor){
.nextInChain =
(const WGPUChainedStruct *)&(
const WGPUSurfaceDescriptorFromMetalLayer){
const WGPUSurfaceSourceromMetalLayer){
.chain =
(const WGPUChainedStruct){
.sType = WGPUSType_SurfaceDescriptorFromMetalLayer,
.sType = WGPUSType_SurfaceSourceMetalLayer,
},
.layer = metal_layer,
},
Expand All @@ -165,10 +167,10 @@ int main(int argc, char *argv[]) {
&(const WGPUSurfaceDescriptor){
.nextInChain =
(const WGPUChainedStruct *)&(
const WGPUSurfaceDescriptorFromXlibWindow){
const WGPUSurfaceSourceXlibWindow){
.chain =
(const WGPUChainedStruct){
.sType = WGPUSType_SurfaceDescriptorFromXlibWindow,
.sType = WGPUSType_SurfaceSourceXlibWindow,
},
.display = x11_display,
.window = x11_window,
Expand All @@ -183,11 +185,11 @@ int main(int argc, char *argv[]) {
&(const WGPUSurfaceDescriptor){
.nextInChain =
(const WGPUChainedStruct *)&(
const WGPUSurfaceDescriptorFromWaylandSurface){
const WGPUSurfaceSourceWaylandSurface){
.chain =
(const WGPUChainedStruct){
.sType =
WGPUSType_SurfaceDescriptorFromWaylandSurface,
WGPUSType_SurfaceSourceWaylandSurface,
},
.display = wayland_display,
.surface = wayland_surface,
Expand All @@ -203,10 +205,10 @@ int main(int argc, char *argv[]) {
&(const WGPUSurfaceDescriptor){
.nextInChain =
(const WGPUChainedStruct *)&(
const WGPUSurfaceDescriptorFromWindowsHWND){
const WGPUSurfaceSourceWindowsHWND){
.chain =
(const WGPUChainedStruct){
.sType = WGPUSType_SurfaceDescriptorFromWindowsHWND,
.sType = WGPUSType_SurfaceSourceWindowsHWND,
},
.hinstance = hinstance,
.hwnd = hwnd,
Expand All @@ -222,7 +224,10 @@ int main(int argc, char *argv[]) {
&(const WGPURequestAdapterOptions){
.compatibleSurface = demo.surface,
},
handle_request_adapter, &demo);
(const WGPURequestAdapterCallbackInfo){
.callback = handle_request_adapter,
.userdata1 = &demo
});
assert(demo.adapter);

WGPUSurfaceCapabilities surface_capabilities = {0};
Expand Down Expand Up @@ -260,13 +265,15 @@ int main(int argc, char *argv[]) {
required_device_feature_count++;
}

wgpuAdapterRequestDevice(
demo.adapter,
&(const WGPUDeviceDescriptor){
.requiredFeatureCount = required_device_feature_count,
.requiredFeatures = required_device_features,
},
handle_request_device, &demo);
wgpuAdapterRequestDevice(demo.adapter,
&(const WGPUDeviceDescriptor){
.requiredFeatureCount = required_device_feature_count,
.requiredFeatures = required_device_features,
},
(const WGPURequestDeviceCallbackInfo){
.callback = handle_request_device,
.userdata1 = &demo
});
assert(demo.device);

WGPUQueue queue = wgpuDeviceGetQueue(demo.device);
Expand Down Expand Up @@ -648,8 +655,9 @@ int main(int argc, char *argv[]) {
WGPUSurfaceTexture surface_texture;
wgpuSurfaceGetCurrentTexture(demo.surface, &surface_texture);
switch (surface_texture.status) {
case WGPUSurfaceGetCurrentTextureStatus_Success:
// All good, could check for `surface_texture.suboptimal` here.
case WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal:
case WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal:
// All good, could handle suboptimal here
break;
case WGPUSurfaceGetCurrentTextureStatus_Timeout:
case WGPUSurfaceGetCurrentTextureStatus_Outdated:
Expand Down
42 changes: 26 additions & 16 deletions examples/triangle/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ struct demo {

static void handle_request_adapter(WGPURequestAdapterStatus status,
WGPUAdapter adapter, char const *message,
void *userdata) {
void *userdata1, void *userdata2) {
UNUSED(userdata2)
if (status == WGPURequestAdapterStatus_Success) {
struct demo *demo = userdata;
struct demo *demo = userdata1;
demo->adapter = adapter;
} else {
printf(LOG_PREFIX " request_adapter status=%#.8x message=%s\n", status,
Expand All @@ -38,9 +39,10 @@ static void handle_request_adapter(WGPURequestAdapterStatus status,
}
static void handle_request_device(WGPURequestDeviceStatus status,
WGPUDevice device, char const *message,
void *userdata) {
void *userdata1, void *userdata2) {
UNUSED(userdata2)
if (status == WGPURequestDeviceStatus_Success) {
struct demo *demo = userdata;
struct demo *demo = userdata1;
demo->device = device;
} else {
printf(LOG_PREFIX " request_device status=%#.8x message=%s\n", status,
Expand Down Expand Up @@ -110,10 +112,10 @@ int main(int argc, char *argv[]) {
&(const WGPUSurfaceDescriptor){
.nextInChain =
(const WGPUChainedStruct *)&(
const WGPUSurfaceDescriptorFromMetalLayer){
const WGPUSurfaceSourceromMetalLayer){
.chain =
(const WGPUChainedStruct){
.sType = WGPUSType_SurfaceDescriptorFromMetalLayer,
.sType = WGPUSType_SurfaceSourceMetalLayer,
},
.layer = metal_layer,
},
Expand All @@ -128,10 +130,10 @@ int main(int argc, char *argv[]) {
&(const WGPUSurfaceDescriptor){
.nextInChain =
(const WGPUChainedStruct *)&(
const WGPUSurfaceDescriptorFromXlibWindow){
const WGPUSurfaceSourceXlibWindow){
.chain =
(const WGPUChainedStruct){
.sType = WGPUSType_SurfaceDescriptorFromXlibWindow,
.sType = WGPUSType_SurfaceSourceXlibWindow,
},
.display = x11_display,
.window = x11_window,
Expand All @@ -146,11 +148,11 @@ int main(int argc, char *argv[]) {
&(const WGPUSurfaceDescriptor){
.nextInChain =
(const WGPUChainedStruct *)&(
const WGPUSurfaceDescriptorFromWaylandSurface){
const WGPUSurfaceSourceWaylandSurface){
.chain =
(const WGPUChainedStruct){
.sType =
WGPUSType_SurfaceDescriptorFromWaylandSurface,
WGPUSType_SurfaceSourceWaylandSurface,
},
.display = wayland_display,
.surface = wayland_surface,
Expand All @@ -166,10 +168,10 @@ int main(int argc, char *argv[]) {
&(const WGPUSurfaceDescriptor){
.nextInChain =
(const WGPUChainedStruct *)&(
const WGPUSurfaceDescriptorFromWindowsHWND){
const WGPUSurfaceSourceWindowsHWND){
.chain =
(const WGPUChainedStruct){
.sType = WGPUSType_SurfaceDescriptorFromWindowsHWND,
.sType = WGPUSType_SurfaceSourceWindowsHWND,
},
.hinstance = hinstance,
.hwnd = hwnd,
Expand All @@ -185,12 +187,19 @@ int main(int argc, char *argv[]) {
&(const WGPURequestAdapterOptions){
.compatibleSurface = demo.surface,
},
handle_request_adapter, &demo);
(const WGPURequestAdapterCallbackInfo){
.callback = handle_request_adapter,
.userdata1 = &demo
});
assert(demo.adapter);

frmwrk_print_adapter_info(demo.adapter);

wgpuAdapterRequestDevice(demo.adapter, NULL, handle_request_device, &demo);
wgpuAdapterRequestDevice(demo.adapter, NULL,
(const WGPURequestDeviceCallbackInfo){
.callback = handle_request_device,
.userdata1 = &demo
});
assert(demo.device);

WGPUQueue queue = wgpuDeviceGetQueue(demo.device);
Expand Down Expand Up @@ -267,8 +276,9 @@ int main(int argc, char *argv[]) {
WGPUSurfaceTexture surface_texture;
wgpuSurfaceGetCurrentTexture(demo.surface, &surface_texture);
switch (surface_texture.status) {
case WGPUSurfaceGetCurrentTextureStatus_Success:
// All good, could check for `surface_texture.suboptimal` here.
case WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal:
case WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal:
// All good, could handle suboptimal here
break;
case WGPUSurfaceGetCurrentTextureStatus_Timeout:
case WGPUSurfaceGetCurrentTextureStatus_Outdated:
Expand Down

0 comments on commit 1e19d06

Please sign in to comment.