From 241aef131ae7c2b9edf9ae1e452d6e7acc101684 Mon Sep 17 00:00:00 2001 From: Joshua Groves Date: Fri, 28 Sep 2018 07:16:42 -0600 Subject: [PATCH] Move `register` inside guard --- examples/Makefile | 6 ++++-- examples/hello_triangle_c/main.c | 2 +- wgpu-bindings/wgpu.h | 9 ++++---- wgpu-native/src/device.rs | 16 +++++++-------- wgpu-native/src/instance.rs | 6 +++--- wgpu-native/src/registry.rs | 35 ++++++++++++++++---------------- 6 files changed, 38 insertions(+), 36 deletions(-) diff --git a/examples/Makefile b/examples/Makefile index 9bd128145..bdb489dc4 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -1,5 +1,3 @@ -all: hello_triangle_c - CC=gcc CFLAGS=-I. DEPS=./../wgpu-bindings/wgpu.h @@ -12,3 +10,7 @@ LINK_ARGS=-L ./../target/debug -lwgpu_native hello_triangle_c: hello_triangle_c/main.c mkdir -p $(OUTDIR) $(CC) $(LINK_ARGS) -o $(OUTDIR)/$@ $^ $(CFLAGS) + +.PHONY: all + +all: hello_triangle_c diff --git a/examples/hello_triangle_c/main.c b/examples/hello_triangle_c/main.c index c1d4c9bd8..d3ad4c54c 100644 --- a/examples/hello_triangle_c/main.c +++ b/examples/hello_triangle_c/main.c @@ -132,7 +132,7 @@ int main() WGPUCommandBufferDescriptor cmd_buf_desc = { }; WGPUCommandBufferId cmd_buf = wgpu_device_create_command_buffer(device, cmd_buf_desc); WGPUQueueId queue = wgpu_device_get_queue(device); - wgpu_queue_submit(queue, &cmd_buf, 1); + /*wgpu_queue_submit(queue, &cmd_buf, 1);*/ return 0; } diff --git a/wgpu-bindings/wgpu.h b/wgpu-bindings/wgpu.h index 8a1b918fb..b0cad59a2 100644 --- a/wgpu-bindings/wgpu.h +++ b/wgpu-bindings/wgpu.h @@ -1,13 +1,14 @@ +#include +#include +#include + +#define WGPU_REMOTE 1 #ifdef WGPU_REMOTE typedef uint32_t WGPUId; #else typedef void *WGPUId; #endif -#include -#include -#include - #define WGPUColorWriteFlags_ALL 15 #define WGPUColorWriteFlags_ALPHA 8 diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 31bdeeb2d..acf987cb0 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -56,7 +56,7 @@ pub extern "C" fn wgpu_device_create_bind_group_layout( }), &[], ); - registry::BIND_GROUP_LAYOUT_REGISTRY.register(binding_model::BindGroupLayout { + registry::BIND_GROUP_LAYOUT_REGISTRY.lock().register(binding_model::BindGroupLayout { raw: descriptor_set_layout, }) } @@ -76,7 +76,7 @@ pub extern "C" fn wgpu_device_create_pipeline_layout( let device = &device_guard.get(device_id).device; let pipeline_layout = device.create_pipeline_layout(descriptor_set_layouts.iter().map(|d| &d.raw), &[]); // TODO: push constants - registry::PIPELINE_LAYOUT_REGISTRY.register(binding_model::PipelineLayout { + registry::PIPELINE_LAYOUT_REGISTRY.lock().register(binding_model::PipelineLayout { raw: pipeline_layout, }) } @@ -86,7 +86,7 @@ pub extern "C" fn wgpu_device_create_blend_state( _device_id: DeviceId, desc: pipeline::BlendStateDescriptor, ) -> BlendStateId { - registry::BLEND_STATE_REGISTRY.register(pipeline::BlendState { + registry::BLEND_STATE_REGISTRY.lock().register(pipeline::BlendState { raw: conv::map_blend_state_descriptor(desc), }) } @@ -96,7 +96,7 @@ pub extern "C" fn wgpu_device_create_depth_stencil_state( device_id: DeviceId, desc: pipeline::DepthStencilStateDescriptor, ) -> DepthStencilStateId { - registry::DEPTH_STENCIL_STATE_REGISTRY.register(pipeline::DepthStencilState { + registry::DEPTH_STENCIL_STATE_REGISTRY.lock().register(pipeline::DepthStencilState { raw: conv::map_depth_stencil_state(desc), }) } @@ -111,7 +111,7 @@ pub extern "C" fn wgpu_device_create_shader_module( let shader = device .create_shader_module(unsafe { slice::from_raw_parts(desc.code.bytes, desc.code.length) }) .unwrap(); - registry::SHADER_MODULE_REGISTRY.register(ShaderModule { raw: shader }) + registry::SHADER_MODULE_REGISTRY.lock().register(ShaderModule { raw: shader }) } #[no_mangle] @@ -122,7 +122,7 @@ pub extern "C" fn wgpu_device_create_command_buffer( let mut device_guard = registry::DEVICE_REGISTRY.lock(); let device = device_guard.get_mut(device_id); let cmd_buf = device.com_allocator.allocate(&device.device); - registry::COMMAND_BUFFER_REGISTRY.register(cmd_buf) + registry::COMMAND_BUFFER_REGISTRY.lock().register(cmd_buf) } #[no_mangle] @@ -188,7 +188,7 @@ pub extern "C" fn wgpu_device_create_attachment_state( layouts: hal::image::Layout::Undefined..hal::image::Layout::Present, // TODO map } }).collect(); - registry::ATTACHMENT_STATE_REGISTRY.register(pipeline::AttachmentState { raw: attachments }) + registry::ATTACHMENT_STATE_REGISTRY.lock().register(pipeline::AttachmentState { raw: attachments }) } #[no_mangle] @@ -364,5 +364,5 @@ pub extern "C" fn wgpu_device_create_render_pipeline( .create_graphics_pipeline(&pipeline_desc, None) .unwrap(); - registry::RENDER_PIPELINE_REGISTRY.register(pipeline::RenderPipeline { raw: pipeline }) + registry::RENDER_PIPELINE_REGISTRY.lock().register(pipeline::RenderPipeline { raw: pipeline }) } diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 94466d373..5e57487db 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -35,7 +35,7 @@ pub extern "C" fn wgpu_create_instance() -> InstanceId { ))] { let inst = ::back::Instance::create("wgpu", 1); - registry::INSTANCE_REGISTRY.register(inst) + registry::INSTANCE_REGISTRY.lock().register(inst) } #[cfg(not(any( feature = "gfx-backend-vulkan", @@ -67,7 +67,7 @@ pub extern "C" fn wgpu_instance_get_adapter( PowerPreference::LowPower => low.or(high), PowerPreference::HighPerformance | PowerPreference::Default => high.or(low), }; - registry::ADAPTER_REGISTRY.register(some.or(other).unwrap()) + registry::ADAPTER_REGISTRY.lock().register(some.or(other).unwrap()) } #[no_mangle] @@ -79,5 +79,5 @@ pub extern "C" fn wgpu_adapter_create_device( let adapter = adapter_guard.get_mut(adapter_id); let (device, queue_group) = adapter.open_with::<_, hal::General>(1, |_qf| true).unwrap(); let mem_props = adapter.physical_device.memory_properties(); - registry::DEVICE_REGISTRY.register(Device::new(device, queue_group, mem_props)) + registry::DEVICE_REGISTRY.lock().register(Device::new(device, queue_group, mem_props)) } diff --git a/wgpu-native/src/registry.rs b/wgpu-native/src/registry.rs index 0016363b6..44184997d 100644 --- a/wgpu-native/src/registry.rs +++ b/wgpu-native/src/registry.rs @@ -30,11 +30,11 @@ type ItemsGuard<'a, T> = MutexGuard<'a, RemoteItems>; pub(crate) trait Registry { fn new() -> Self; - fn register(&self, handle: T) -> Id; fn lock(&self) -> ItemsGuard; } pub(crate) trait Items { + fn register(&mut self, handle: T) -> Id; fn get(&self, id: Id) -> Item; fn get_mut(&mut self, id: Id) -> ItemMut; fn take(&mut self, id: Id) -> T; @@ -47,6 +47,10 @@ pub(crate) struct LocalItems { #[cfg(not(feature = "remote"))] impl Items for LocalItems { + fn register(&mut self, handle: T) -> Id { + Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void + } + fn get(&self, id: Id) -> Item { unsafe { (id as *mut T).as_ref() }.unwrap() } @@ -75,10 +79,6 @@ impl Registry for LocalRegistry { } } - fn register(&self, handle: T) -> Id { - Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void - } - fn lock(&self) -> ItemsGuard { LocalItems { marker: PhantomData, @@ -106,6 +106,18 @@ impl RemoteItems { #[cfg(feature = "remote")] impl Items for RemoteItems { + fn register(&mut self, handle: T) -> Id { + let id = match self.free.pop() { + Some(id) => id, + None => { + self.next_id += 1; + self.next_id - 1 + } + }; + self.tracked.insert(id, handle); + id + } + fn get(&self, id: Id) -> Item { self.tracked.get(&id).unwrap() } @@ -133,19 +145,6 @@ impl Registry for RemoteRegistry { } } - fn register(&self, handle: T) -> Id { - let mut items = self.items.lock(); - let id = match items.free.pop() { - Some(id) => id, - None => { - items.next_id += 1; - items.next_id - 1 - } - }; - items.tracked.insert(id, handle); - id - } - fn lock(&self) -> ItemsGuard { self.items.lock() }