359: Make examples work again r=kvark a=GabrielMajeri

This PR fixes the C example code to not crash and actually run.

I've also added a few assertions to ensure a warning is emitted next time somebody forgots to increase `max_bind_groups` to something non-zero on device creation.

To help with debugging the examples, I've configured CMake to include debug info in the builds. Some new Makefile targets for the examples have been added to automate running them.

Co-authored-by: Gabriel Majeri <gabriel.majeri6@gmail.com>
This commit is contained in:
bors[bot] 2019-10-26 21:08:16 +00:00 committed by GitHub
commit b71fc33c72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 26 deletions

View File

@ -26,7 +26,9 @@ else
CREATE_BUILD_DIR=mkdir -p $(BUILD_DIR) CREATE_BUILD_DIR=mkdir -p $(BUILD_DIR)
endif 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 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 rustup run nightly cbindgen -o $(FFI_DIR)/wgpu-remote.h wgpu-remote
example-compute: lib-native $(FFI_DIR)/wgpu.h examples/compute/main.c 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 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 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

View File

@ -35,22 +35,17 @@ int main(
WGPUAdapterId adapter = wgpu_request_adapter(NULL); WGPUAdapterId adapter = wgpu_request_adapter(NULL);
WGPUDeviceId device = wgpu_adapter_request_device(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){ &(WGPUBufferDescriptor){
.size = size, .size = size,
.usage = WGPUBufferUsage_MAP_READ}, .usage = WGPUBufferUsage_STORAGE | WGPUBufferUsage_MAP_READ},
&staging_memory); &staging_memory);
memcpy((uint32_t *) staging_memory, numbers, size); memcpy((uint32_t *) staging_memory, numbers, size);
wgpu_buffer_unmap(staging_buffer); wgpu_buffer_unmap(buffer);
WGPUBufferId storage_buffer = wgpu_device_create_buffer(device,
&(WGPUBufferDescriptor){
.size = size,
.usage = WGPUBufferUsage_STORAGE});
WGPUBindGroupLayoutId bind_group_layout = WGPUBindGroupLayoutId bind_group_layout =
wgpu_device_create_bind_group_layout(device, wgpu_device_create_bind_group_layout(device,
@ -63,10 +58,10 @@ int main(
WGPUBindingResource resource = { WGPUBindingResource resource = {
.tag = WGPUBindingResource_Buffer, .tag = WGPUBindingResource_Buffer,
.buffer = (WGPUBufferBinding){ .buffer = {(WGPUBufferBinding){
.buffer = storage_buffer, .buffer = buffer,
.size = size, .size = size,
.offset = 0}}; .offset = 0}}};
WGPUBindGroupId bind_group = wgpu_device_create_bind_group(device, WGPUBindGroupId bind_group = wgpu_device_create_bind_group(device,
&(WGPUBindGroupDescriptor){.layout = bind_group_layout, &(WGPUBindGroupDescriptor){.layout = bind_group_layout,
@ -102,9 +97,6 @@ int main(
.todo = 0 .todo = 0
}); });
wgpu_command_encoder_copy_buffer_to_buffer(
encoder, staging_buffer, 0, storage_buffer, 0, size);
WGPUComputePassId command_pass = WGPUComputePassId command_pass =
wgpu_command_encoder_begin_compute_pass(encoder, NULL); wgpu_command_encoder_begin_compute_pass(encoder, NULL);
wgpu_compute_pass_set_pipeline(command_pass, compute_pipeline); 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_dispatch(command_pass, numbers_length, 1, 1);
wgpu_compute_pass_end_pass(command_pass); 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); WGPUQueueId queue = wgpu_device_get_queue(device);
WGPUCommandBufferId command_buffer = wgpu_command_encoder_finish(encoder, NULL); WGPUCommandBufferId command_buffer = wgpu_command_encoder_finish(encoder, NULL);
wgpu_queue_submit(queue, &command_buffer, 1); 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); wgpu_device_poll(device, true);

View File

@ -45,6 +45,10 @@ int main() {
{ {
.anisotropic_filtering = false, .anisotropic_filtering = false,
}, },
.limits =
{
.max_bind_groups = 1,
},
}); });
WGPUShaderModuleId vertex_shader = wgpu_device_create_shader_module(device, WGPUShaderModuleId vertex_shader = wgpu_device_create_shader_module(device,

View File

@ -1211,9 +1211,13 @@ pub fn device_create_pipeline_layout<B: GfxBackend>(
let mut token = Token::root(); let mut token = Token::root();
let (device_guard, mut token) = hub.devices.read(&mut token); let (device_guard, mut token) = hub.devices.read(&mut token);
let device = &device_guard[device_id];
let bind_group_layout_ids = let bind_group_layout_ids =
unsafe { slice::from_raw_parts(desc.bind_group_layouts, desc.bind_group_layouts_length) }; 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 // TODO: push constants
let pipeline_layout = { let pipeline_layout = {
let (bind_group_layout_guard, _) = hub.bind_group_layouts.read(&mut token); let (bind_group_layout_guard, _) = hub.bind_group_layouts.read(&mut token);
@ -1221,9 +1225,7 @@ pub fn device_create_pipeline_layout<B: GfxBackend>(
.iter() .iter()
.map(|&id| &bind_group_layout_guard[id].raw); .map(|&id| &bind_group_layout_guard[id].raw);
unsafe { unsafe {
device_guard[device_id] device.raw.create_pipeline_layout(descriptor_set_layouts, &[])
.raw
.create_pipeline_layout(descriptor_set_layouts, &[])
} }
.unwrap() .unwrap()
}; };