From eb198a9c3ef63c555754cd446feddc9ccb3d9c13 Mon Sep 17 00:00:00 2001 From: Gabriel Majeri Date: Fri, 25 Oct 2019 21:18:18 +0300 Subject: [PATCH] Fix C examples Get the examples running again. The compute example has been simplified to use only one buffer. A new check has been added to ensure the `max_bind_groups` device limit is properly set. --- Makefile | 19 +++++++++++++++---- examples/compute/main.c | 27 ++++++++------------------- examples/triangle/main.c | 4 ++++ wgpu-native/src/device.rs | 8 +++++--- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 9830795cc..c98578c0a 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,9 @@ else CREATE_BUILD_DIR=mkdir -p $(BUILD_DIR) endif -.PHONY: all check test doc clear lib-native lib-remote example-compute example-triangle example-remote +.PHONY: all check test doc clear lib-native lib-remote \ + example-compute example-triangle example-remote \ + run-example-compute run-example-triangle run-example-remote all: example-compute example-triangle example-remote @@ -56,10 +58,19 @@ $(FFI_DIR)/wgpu-remote.h: wgpu-remote/cbindgen.toml $(WILDCARD_WGPU_NATIVE_AND_R rustup run nightly cbindgen -o $(FFI_DIR)/wgpu-remote.h wgpu-remote example-compute: lib-native $(FFI_DIR)/wgpu.h examples/compute/main.c - cd examples/compute && $(CREATE_BUILD_DIR) && cd build && cmake .. $(GENERATOR_PLATFORM) && cmake --build . + cd examples/compute && $(CREATE_BUILD_DIR) && cd build && cmake -DCMAKE_BUILD_TYPE=Debug .. $(GENERATOR_PLATFORM) && cmake --build . + +run-example-compute: example-compute + cd examples/compute/build && ./compute 1 2 3 4 example-triangle: lib-native $(FFI_DIR)/wgpu.h examples/triangle/main.c - cd examples/triangle && $(CREATE_BUILD_DIR) && cd build && cmake .. $(GENERATOR_PLATFORM) && cmake --build . + cd examples/triangle && $(CREATE_BUILD_DIR) && cd build && cmake -DCMAKE_BUILD_TYPE=Debug .. $(GENERATOR_PLATFORM) && cmake --build . + +run-example-triangle: example-triangle + cd examples/triangle/build && ./triangle example-remote: lib-remote $(FFI_DIR)/wgpu-remote.h examples/remote/main.c - cd examples/remote && $(CREATE_BUILD_DIR) && cd build && cmake .. && cmake --build . + cd examples/remote && $(CREATE_BUILD_DIR) && cd build && cmake -DCMAKE_BUILD_TYPE=Debug .. $(GENERATOR_PLATFORM) && cmake --build . + +run-example-remote: example-remote + cd examples/remote/build && ./remote diff --git a/examples/compute/main.c b/examples/compute/main.c index 35d6a0f09..ad17bf150 100644 --- a/examples/compute/main.c +++ b/examples/compute/main.c @@ -35,22 +35,17 @@ int main( WGPUAdapterId adapter = wgpu_request_adapter(NULL); WGPUDeviceId device = wgpu_adapter_request_device(adapter, NULL); - uint8_t *staging_memory; + uint8_t *staging_memory; - WGPUBufferId staging_buffer = wgpu_device_create_buffer_mapped(device, + WGPUBufferId buffer = wgpu_device_create_buffer_mapped(device, &(WGPUBufferDescriptor){ .size = size, - .usage = WGPUBufferUsage_MAP_READ}, + .usage = WGPUBufferUsage_STORAGE | WGPUBufferUsage_MAP_READ}, &staging_memory); memcpy((uint32_t *) staging_memory, numbers, size); - wgpu_buffer_unmap(staging_buffer); - - WGPUBufferId storage_buffer = wgpu_device_create_buffer(device, - &(WGPUBufferDescriptor){ - .size = size, - .usage = WGPUBufferUsage_STORAGE}); + wgpu_buffer_unmap(buffer); WGPUBindGroupLayoutId bind_group_layout = wgpu_device_create_bind_group_layout(device, @@ -63,10 +58,10 @@ int main( WGPUBindingResource resource = { .tag = WGPUBindingResource_Buffer, - .buffer = (WGPUBufferBinding){ - .buffer = storage_buffer, + .buffer = {(WGPUBufferBinding){ + .buffer = buffer, .size = size, - .offset = 0}}; + .offset = 0}}}; WGPUBindGroupId bind_group = wgpu_device_create_bind_group(device, &(WGPUBindGroupDescriptor){.layout = bind_group_layout, @@ -102,9 +97,6 @@ int main( .todo = 0 }); - wgpu_command_encoder_copy_buffer_to_buffer( - encoder, staging_buffer, 0, storage_buffer, 0, size); - WGPUComputePassId command_pass = wgpu_command_encoder_begin_compute_pass(encoder, NULL); wgpu_compute_pass_set_pipeline(command_pass, compute_pipeline); @@ -113,16 +105,13 @@ int main( wgpu_compute_pass_dispatch(command_pass, numbers_length, 1, 1); wgpu_compute_pass_end_pass(command_pass); - wgpu_command_encoder_copy_buffer_to_buffer( - encoder, storage_buffer, 0, staging_buffer, 0, size); - WGPUQueueId queue = wgpu_device_get_queue(device); WGPUCommandBufferId command_buffer = wgpu_command_encoder_finish(encoder, NULL); wgpu_queue_submit(queue, &command_buffer, 1); - wgpu_buffer_map_read_async(staging_buffer, 0, size, read_buffer_map, NULL); + wgpu_buffer_map_read_async(buffer, 0, size, read_buffer_map, NULL); wgpu_device_poll(device, true); diff --git a/examples/triangle/main.c b/examples/triangle/main.c index 0de981262..db711c62a 100644 --- a/examples/triangle/main.c +++ b/examples/triangle/main.c @@ -45,6 +45,10 @@ int main() { { .anisotropic_filtering = false, }, + .limits = + { + .max_bind_groups = 1, + }, }); WGPUShaderModuleId vertex_shader = wgpu_device_create_shader_module(device, diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index c8bebc958..20da86f13 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -1172,9 +1172,13 @@ pub fn device_create_pipeline_layout( let mut token = Token::root(); let (device_guard, mut token) = hub.devices.read(&mut token); + let device = &device_guard[device_id]; let bind_group_layout_ids = unsafe { slice::from_raw_parts(desc.bind_group_layouts, desc.bind_group_layouts_length) }; + assert!(desc.bind_group_layouts_length <= (device.features.max_bind_groups as usize), + "Cannot set a bind group which is beyond the `max_bind_groups` limit requested on device creation"); + // TODO: push constants let pipeline_layout = { let (bind_group_layout_guard, _) = hub.bind_group_layouts.read(&mut token); @@ -1182,9 +1186,7 @@ pub fn device_create_pipeline_layout( .iter() .map(|&id| &bind_group_layout_guard[id].raw); unsafe { - device_guard[device_id] - .raw - .create_pipeline_layout(descriptor_set_layouts, &[]) + device.raw.create_pipeline_layout(descriptor_set_layouts, &[]) } .unwrap() };