Move register inside guard

This commit is contained in:
Joshua Groves 2018-09-28 07:16:42 -06:00
parent eb80057e11
commit 241aef131a
6 changed files with 38 additions and 36 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -1,13 +1,14 @@
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#define WGPU_REMOTE 1
#ifdef WGPU_REMOTE
typedef uint32_t WGPUId;
#else
typedef void *WGPUId;
#endif
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#define WGPUColorWriteFlags_ALL 15
#define WGPUColorWriteFlags_ALPHA 8

View File

@ -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 })
}

View File

@ -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))
}

View File

@ -30,11 +30,11 @@ type ItemsGuard<'a, T> = MutexGuard<'a, RemoteItems<T>>;
pub(crate) trait Registry<T> {
fn new() -> Self;
fn register(&self, handle: T) -> Id;
fn lock(&self) -> ItemsGuard<T>;
}
pub(crate) trait Items<T> {
fn register(&mut self, handle: T) -> Id;
fn get(&self, id: Id) -> Item<T>;
fn get_mut(&mut self, id: Id) -> ItemMut<T>;
fn take(&mut self, id: Id) -> T;
@ -47,6 +47,10 @@ pub(crate) struct LocalItems<T> {
#[cfg(not(feature = "remote"))]
impl<T> Items<T> for LocalItems<T> {
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<T> {
unsafe { (id as *mut T).as_ref() }.unwrap()
}
@ -75,10 +79,6 @@ impl<T> Registry<T> for LocalRegistry<T> {
}
}
fn register(&self, handle: T) -> Id {
Box::into_raw(Box::new(handle)) as *mut _ as *mut c_void
}
fn lock(&self) -> ItemsGuard<T> {
LocalItems {
marker: PhantomData,
@ -106,6 +106,18 @@ impl<T> RemoteItems<T> {
#[cfg(feature = "remote")]
impl<T> Items<T> for RemoteItems<T> {
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<T> {
self.tracked.get(&id).unwrap()
}
@ -133,19 +145,6 @@ impl<T> Registry<T> for RemoteRegistry<T> {
}
}
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<T> {
self.items.lock()
}