Unified unboxing

This commit is contained in:
Dzmitry Malyshau 2018-04-12 09:24:47 -04:00
parent cfe1f11ad3
commit 670de5f155
3 changed files with 29 additions and 28 deletions

View file

@ -46,8 +46,9 @@ binding: $(BINDING)
$(BINDING): $(VULKAN_DIR)/vulkan/*.h $(BINDING): $(VULKAN_DIR)/vulkan/*.h
bindgen --no-layout-tests --rustfmt-bindings $(VULKAN_DIR)/vulkan/vulkan.h -o $(BINDING) bindgen --no-layout-tests --rustfmt-bindings $(VULKAN_DIR)/vulkan/vulkan.h -o $(BINDING)
$(LIBRARY): libportability/src/*.rs libportability-gfx/src/*.rs Cargo.toml libportability-gfx/Cargo.toml $(wildcard Cargo.lock) $(LIBRARY): libportability*/src/*.rs libportability*/Cargo.toml Cargo.lock
cargo build --manifest-path libportability/Cargo.toml --features $(BACKEND) cargo build --manifest-path libportability/Cargo.toml --features $(BACKEND)
cargo build --manifest-path libportability-icd/Cargo.toml --features $(BACKEND)
mkdir -p target/native mkdir -p target/native
$(NATIVE_DIR)/%.o: native/%.cpp $(DEPS) Makefile $(NATIVE_DIR)/%.o: native/%.cpp $(DEPS) Makefile

View file

@ -15,8 +15,8 @@ impl<T> Handle<T> {
Handle(VK_NULL_HANDLE as *mut _) Handle(VK_NULL_HANDLE as *mut _)
} }
pub fn unwrap(self) -> Box<T> { pub fn unbox(self) -> T {
unsafe { Box::from_raw(self.0) } *unsafe { Box::from_raw(self.0) }
} }
pub fn is_null(&self) -> bool { pub fn is_null(&self) -> bool {
@ -82,8 +82,8 @@ mod dispatch {
DispatchHandle(VK_NULL_HANDLE as *mut _) DispatchHandle(VK_NULL_HANDLE as *mut _)
} }
pub fn unwrap(self) -> Box<(u64, T)> { pub fn unbox(self) -> T {
unsafe { Box::from_raw(self.0) } unsafe { Box::from_raw(self.0) }.1
} }
pub fn is_null(&self) -> bool { pub fn is_null(&self) -> bool {

View file

@ -59,7 +59,7 @@ pub extern "C" fn gfxDestroyInstance(
instance: VkInstance, instance: VkInstance,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
instance.unwrap(); let _ = instance.unbox();
//let it drop //let it drop
} }
@ -550,9 +550,9 @@ pub extern "C" fn gfxCreateDevice(
#[inline] #[inline]
pub extern "C" fn gfxDestroyDevice(gpu: VkDevice, _pAllocator: *const VkAllocationCallbacks) { pub extern "C" fn gfxDestroyDevice(gpu: VkDevice, _pAllocator: *const VkAllocationCallbacks) {
// release all the owned command queues // release all the owned command queues
for (_, family) in gpu.unwrap().queues { for (_, family) in gpu.unbox().queues {
for queue in family { for queue in family {
let _ = queue.unwrap(); let _ = queue.unbox();
} }
} }
} }
@ -737,7 +737,7 @@ pub extern "C" fn gfxFreeMemory(
memory: VkDeviceMemory, memory: VkDeviceMemory,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.free_memory(*memory.unwrap()); gpu.device.free_memory(memory.unbox());
} }
#[inline] #[inline]
pub extern "C" fn gfxMapMemory( pub extern "C" fn gfxMapMemory(
@ -938,7 +938,7 @@ pub extern "C" fn gfxDestroyFence(
fence: VkFence, fence: VkFence,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.destroy_fence(*fence.unwrap()); gpu.device.destroy_fence(fence.unbox());
} }
#[inline] #[inline]
pub extern "C" fn gfxResetFences( pub extern "C" fn gfxResetFences(
@ -999,7 +999,7 @@ pub extern "C" fn gfxDestroySemaphore(
semaphore: VkSemaphore, semaphore: VkSemaphore,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.destroy_semaphore(*semaphore.unwrap()); gpu.device.destroy_semaphore(semaphore.unbox());
} }
#[inline] #[inline]
pub extern "C" fn gfxCreateEvent( pub extern "C" fn gfxCreateEvent(
@ -1087,7 +1087,7 @@ pub extern "C" fn gfxDestroyBuffer(
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
if !buffer.is_null() { if !buffer.is_null() {
match *buffer.unwrap() { match buffer.unbox() {
Buffer::Buffer(buffer) => gpu.device.destroy_buffer(buffer), Buffer::Buffer(buffer) => gpu.device.destroy_buffer(buffer),
Buffer::Unbound(_) => { Buffer::Unbound(_) => {
warn!("Trying to destroy a non-bound buffer, ignoring"); warn!("Trying to destroy a non-bound buffer, ignoring");
@ -1127,7 +1127,7 @@ pub extern "C" fn gfxDestroyBufferView(
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
if !view.is_null() { if !view.is_null() {
gpu.device.destroy_buffer_view(*view.unwrap()); gpu.device.destroy_buffer_view(view.unbox());
} }
} }
#[inline] #[inline]
@ -1170,7 +1170,7 @@ pub extern "C" fn gfxDestroyImage(
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
if !image.is_null() { if !image.is_null() {
match *image.unwrap() { match image.unbox() {
Image::Image(image) => gpu.device.destroy_image(image), Image::Image(image) => gpu.device.destroy_image(image),
Image::Unbound(_) => { Image::Unbound(_) => {
warn!("Trying to destroy a non-bound image, ignoring"); warn!("Trying to destroy a non-bound image, ignoring");
@ -1226,7 +1226,7 @@ pub extern "C" fn gfxDestroyImageView(
imageView: VkImageView, imageView: VkImageView,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.destroy_image_view(*imageView.unwrap()) gpu.device.destroy_image_view(imageView.unbox())
} }
#[inline] #[inline]
pub extern "C" fn gfxCreateShaderModule( pub extern "C" fn gfxCreateShaderModule(
@ -1256,7 +1256,7 @@ pub extern "C" fn gfxDestroyShaderModule(
shaderModule: VkShaderModule, shaderModule: VkShaderModule,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.destroy_shader_module(*shaderModule.unwrap()); gpu.device.destroy_shader_module(shaderModule.unbox());
} }
#[inline] #[inline]
pub extern "C" fn gfxCreatePipelineCache( pub extern "C" fn gfxCreatePipelineCache(
@ -1684,7 +1684,7 @@ pub extern "C" fn gfxDestroyPipeline(
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
if !pipeline.is_null() { if !pipeline.is_null() {
match *pipeline.unwrap() { match pipeline.unbox() {
Pipeline::Graphics(pipeline) => gpu.device.destroy_graphics_pipeline(pipeline), Pipeline::Graphics(pipeline) => gpu.device.destroy_graphics_pipeline(pipeline),
Pipeline::Compute(pipeline) => gpu.device.destroy_compute_pipeline(pipeline), Pipeline::Compute(pipeline) => gpu.device.destroy_compute_pipeline(pipeline),
} }
@ -1731,7 +1731,7 @@ pub extern "C" fn gfxDestroyPipelineLayout(
pipelineLayout: VkPipelineLayout, pipelineLayout: VkPipelineLayout,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.destroy_pipeline_layout(*pipelineLayout.unwrap()); gpu.device.destroy_pipeline_layout(pipelineLayout.unbox());
} }
#[inline] #[inline]
pub extern "C" fn gfxCreateSampler( pub extern "C" fn gfxCreateSampler(
@ -1767,7 +1767,7 @@ pub extern "C" fn gfxDestroySampler(
sampler: VkSampler, sampler: VkSampler,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.destroy_sampler(*sampler.unwrap()); gpu.device.destroy_sampler(sampler.unbox());
} }
#[inline] #[inline]
pub extern "C" fn gfxCreateDescriptorSetLayout( pub extern "C" fn gfxCreateDescriptorSetLayout(
@ -1810,7 +1810,7 @@ pub extern "C" fn gfxDestroyDescriptorSetLayout(
descriptorSetLayout: VkDescriptorSetLayout, descriptorSetLayout: VkDescriptorSetLayout,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.destroy_descriptor_set_layout(*descriptorSetLayout.unwrap()); gpu.device.destroy_descriptor_set_layout(descriptorSetLayout.unbox());
} }
#[inline] #[inline]
pub extern "C" fn gfxCreateDescriptorPool( pub extern "C" fn gfxCreateDescriptorPool(
@ -1850,7 +1850,7 @@ pub extern "C" fn gfxDestroyDescriptorPool(
descriptorPool: VkDescriptorPool, descriptorPool: VkDescriptorPool,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.destroy_descriptor_pool(*descriptorPool.unwrap()); gpu.device.destroy_descriptor_pool(descriptorPool.unbox());
} }
#[inline] #[inline]
pub extern "C" fn gfxResetDescriptorPool( pub extern "C" fn gfxResetDescriptorPool(
@ -2060,7 +2060,7 @@ pub extern "C" fn gfxDestroyFramebuffer(
framebuffer: VkFramebuffer, framebuffer: VkFramebuffer,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.destroy_framebuffer(*framebuffer.unwrap()); gpu.device.destroy_framebuffer(framebuffer.unbox());
} }
#[inline] #[inline]
pub extern "C" fn gfxCreateRenderPass( pub extern "C" fn gfxCreateRenderPass(
@ -2232,7 +2232,7 @@ pub extern "C" fn gfxDestroyRenderPass(
renderPass: VkRenderPass, renderPass: VkRenderPass,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.destroy_render_pass(*renderPass.unwrap()); gpu.device.destroy_render_pass(renderPass.unbox());
} }
#[inline] #[inline]
pub extern "C" fn gfxGetRenderAreaGranularity( pub extern "C" fn gfxGetRenderAreaGranularity(
@ -2277,7 +2277,7 @@ pub extern "C" fn gfxDestroyCommandPool(
commandPool: VkCommandPool, commandPool: VkCommandPool,
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
gpu.device.destroy_command_pool(*commandPool.unwrap()); gpu.device.destroy_command_pool(commandPool.unbox());
} }
#[inline] #[inline]
@ -2325,7 +2325,7 @@ pub extern "C" fn gfxFreeCommandBuffers(
let slice = unsafe { let slice = unsafe {
slice::from_raw_parts(pCommandBuffers, commandBufferCount as _) slice::from_raw_parts(pCommandBuffers, commandBufferCount as _)
}; };
let buffers = slice.iter().map(|buffer| *buffer.unwrap()).collect(); let buffers = slice.iter().map(|buffer| buffer.unbox()).collect();
unsafe { commandPool.free(buffers) }; unsafe { commandPool.free(buffers) };
} }
@ -2968,7 +2968,7 @@ pub extern "C" fn gfxDestroySurfaceKHR(
surface: VkSurfaceKHR, surface: VkSurfaceKHR,
_: *const VkAllocationCallbacks, _: *const VkAllocationCallbacks,
) { ) {
let _ = surface.unwrap(); //TODO let _ = surface.unbox(); //TODO
} }
#[inline] #[inline]
@ -3119,9 +3119,9 @@ pub extern "C" fn gfxDestroySwapchainKHR(
_pAllocator: *const VkAllocationCallbacks, _pAllocator: *const VkAllocationCallbacks,
) { ) {
for image in &mut swapchain.images { for image in &mut swapchain.images {
let _ = image.unwrap(); let _ = image.unbox();
} }
let _ = swapchain.unwrap(); let _ = swapchain.unbox();
} }
#[inline] #[inline]
pub extern "C" fn gfxGetSwapchainImagesKHR( pub extern "C" fn gfxGetSwapchainImagesKHR(