mirror of
https://github.com/gfx-rs/wgpu.git
synced 2024-11-22 06:44:14 +00:00
Merge #359
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:
commit
b71fc33c72
19
Makefile
19
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
|
||||
|
@ -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);
|
||||
|
||||
|
@ -45,6 +45,10 @@ int main() {
|
||||
{
|
||||
.anisotropic_filtering = false,
|
||||
},
|
||||
.limits =
|
||||
{
|
||||
.max_bind_groups = 1,
|
||||
},
|
||||
});
|
||||
|
||||
WGPUShaderModuleId vertex_shader = wgpu_device_create_shader_module(device,
|
||||
|
@ -1211,9 +1211,13 @@ pub fn device_create_pipeline_layout<B: GfxBackend>(
|
||||
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);
|
||||
@ -1221,9 +1225,7 @@ pub fn device_create_pipeline_layout<B: GfxBackend>(
|
||||
.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()
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user