diff --git a/libportability-gfx/src/conv.rs b/libportability-gfx/src/conv.rs index 0677279..c10dacb 100644 --- a/libportability-gfx/src/conv.rs +++ b/libportability-gfx/src/conv.rs @@ -346,7 +346,7 @@ pub fn map_stage_flags(stages: VkShaderStageFlags) -> pso::ShaderStageFlags { pub fn map_pipeline_stage_flags(stages: VkPipelineStageFlags) -> pso::PipelineStage { let max_flag = VkPipelineStageFlagBits::VK_PIPELINE_STAGE_HOST_BIT as u32; - if (stages & 1 << (max_flag + 1) - 1) == 0 { + if (stages & !((max_flag << 1) - 1)) == 0 { // HAL flags have the same numeric representation as Vulkan flags unsafe { mem::transmute(stages) } } else { diff --git a/libportability-gfx/src/impls.rs b/libportability-gfx/src/impls.rs index f7b3254..e9be61b 100644 --- a/libportability-gfx/src/impls.rs +++ b/libportability-gfx/src/impls.rs @@ -5,7 +5,7 @@ use hal::{ }; use hal::device::WaitFor; use hal::pool::RawCommandPool; -use hal::command::{ClearValueRaw, RawCommandBuffer, Rect}; +use hal::command::{ClearValueRaw, RawCommandBuffer, Rect, Viewport}; use hal::queue::RawCommandQueue; use std::ffi::{CStr, CString}; @@ -919,7 +919,10 @@ pub extern "C" fn gfxCreatePipelineCache( pAllocator: *const VkAllocationCallbacks, pPipelineCache: *mut VkPipelineCache, ) -> VkResult { - unimplemented!() + // unimplemented!() + // TODO + + VkResult::VK_SUCCESS } #[inline] pub extern "C" fn gfxDestroyPipelineCache( @@ -1275,15 +1278,16 @@ pub extern "C" fn gfxCreateGraphicsPipelines( let pipelines = gpu.device.create_graphics_pipelines(&descs); - unsafe { + let pipelines = unsafe { slice::from_raw_parts_mut(pPipelines, descs.len()) .into_iter() .zip(pipelines.into_iter()) - .map(|(pipeline, raw)| { - if let Ok(raw) = raw { - *pipeline = Handle::new(Pipeline::Graphics(raw)); - } - }); + }; + + for (pipeline, raw) in pipelines { + if let Ok(raw) = raw { + *pipeline = Handle::new(Pipeline::Graphics(raw)); + } } VkResult::VK_SUCCESS @@ -1930,29 +1934,65 @@ pub extern "C" fn gfxResetCommandBuffer( } #[inline] pub extern "C" fn gfxCmdBindPipeline( - commandBuffer: VkCommandBuffer, - pipelineBindPoint: VkPipelineBindPoint, + mut commandBuffer: VkCommandBuffer, + _pipelineBindPoint: VkPipelineBindPoint, // ignore, needs to match by spec pipeline: VkPipeline, ) { - unimplemented!() + match *pipeline { + Pipeline::Graphics(ref pipeline) => commandBuffer.bind_graphics_pipeline(pipeline), + Pipeline::Compute(ref pipeline) => commandBuffer.bind_compute_pipeline(pipeline), + } } #[inline] pub extern "C" fn gfxCmdSetViewport( - commandBuffer: VkCommandBuffer, + mut commandBuffer: VkCommandBuffer, firstViewport: u32, viewportCount: u32, pViewports: *const VkViewport, ) { - unimplemented!() + assert_eq!(firstViewport, 0); // TODO + + let viewports = unsafe { + slice::from_raw_parts(pViewports, viewportCount as _) + .into_iter() + .map(|viewport| { + Viewport { + rect: Rect { + x: viewport.x as _, + y: viewport.y as _, + w: viewport.width as _, + h: viewport.height as _, + }, + depth: viewport.minDepth .. viewport.maxDepth, + } + }) + }; + + commandBuffer.set_viewports(viewports); } #[inline] pub extern "C" fn gfxCmdSetScissor( - commandBuffer: VkCommandBuffer, + mut commandBuffer: VkCommandBuffer, firstScissor: u32, scissorCount: u32, pScissors: *const VkRect2D, ) { - unimplemented!() + assert_eq!(firstScissor, 0); // TODO + + let scissors = unsafe { + slice::from_raw_parts(pScissors, scissorCount as _) + .into_iter() + .map(|scissor| { + Rect { + x: scissor.offset.x as _, + y: scissor.offset.y as _, + w: scissor.extent.width as _, + h: scissor.extent.height as _, + } + }) + }; + + commandBuffer.set_scissors(scissors); } #[inline] pub extern "C" fn gfxCmdSetLineWidth(commandBuffer: VkCommandBuffer, lineWidth: f32) { @@ -2008,7 +2048,7 @@ pub extern "C" fn gfxCmdSetStencilReference( } #[inline] pub extern "C" fn gfxCmdBindDescriptorSets( - commandBuffer: VkCommandBuffer, + mut commandBuffer: VkCommandBuffer, pipelineBindPoint: VkPipelineBindPoint, layout: VkPipelineLayout, firstSet: u32, @@ -2017,7 +2057,31 @@ pub extern "C" fn gfxCmdBindDescriptorSets( dynamicOffsetCount: u32, pDynamicOffsets: *const u32, ) { - unimplemented!() + assert_eq!(dynamicOffsetCount, 0); // TODO + + let descriptor_sets = unsafe { + slice::from_raw_parts(pDescriptorSets, descriptorSetCount as _) + .into_iter() + .map(|set| set.deref()) + }; + + match pipelineBindPoint { + VkPipelineBindPoint::VK_PIPELINE_BIND_POINT_GRAPHICS => { + commandBuffer.bind_graphics_descriptor_sets( + layout.deref(), + firstSet as _, + descriptor_sets, + ); + } + VkPipelineBindPoint::VK_PIPELINE_BIND_POINT_COMPUTE => { + commandBuffer.bind_compute_descriptor_sets( + layout.deref(), + firstSet as _, + descriptor_sets, + ); + } + _ => panic!("Unexpected pipeline bind point: {:?}", pipelineBindPoint), + } } #[inline] pub extern "C" fn gfxCmdBindIndexBuffer( @@ -2063,13 +2127,16 @@ pub extern "C" fn gfxCmdBindVertexBuffers( } #[inline] pub extern "C" fn gfxCmdDraw( - commandBuffer: VkCommandBuffer, + mut commandBuffer: VkCommandBuffer, vertexCount: u32, instanceCount: u32, firstVertex: u32, firstInstance: u32, ) { - unimplemented!() + commandBuffer.draw( + firstVertex .. firstVertex + vertexCount, + firstInstance .. firstInstance + instanceCount, + ) } #[inline] pub extern "C" fn gfxCmdDrawIndexed( @@ -2784,8 +2851,23 @@ pub extern "C" fn gfxAcquireNextImageKHR( } #[inline] pub extern "C" fn gfxQueuePresentKHR( - queue: VkQueue, + mut queue: VkQueue, pPresentInfo: *const VkPresentInfoKHR, ) -> VkResult { - unimplemented!() + let info = unsafe { &*pPresentInfo }; + + let swapchains = unsafe { + slice::from_raw_parts_mut(info.pSwapchains as *mut VkSwapchainKHR, info.swapchainCount as _) + .into_iter() + .map(|swapchain| &mut swapchain.raw) + }; + let wait_semaphores = unsafe { + slice::from_raw_parts(info.pWaitSemaphores, info.waitSemaphoreCount as _) + .into_iter() + .map(|semaphore| semaphore.deref()) + }; + + queue.present(swapchains, wait_semaphores); + + VkResult::VK_SUCCESS } diff --git a/libportability/src/lib.rs b/libportability/src/lib.rs index ddcc9a4..139b6d7 100644 --- a/libportability/src/lib.rs +++ b/libportability/src/lib.rs @@ -906,7 +906,7 @@ pub extern "C" fn vkResetCommandBuffer( commandBuffer: VkCommandBuffer, flags: VkCommandBufferResetFlags, ) -> VkResult { - unimplemented!() + gfxResetCommandBuffer(commandBuffer, flags) } #[no_mangle] pub extern "C" fn vkCmdBindPipeline( @@ -914,7 +914,7 @@ pub extern "C" fn vkCmdBindPipeline( pipelineBindPoint: VkPipelineBindPoint, pipeline: VkPipeline, ) { - unimplemented!() + gfxCmdBindPipeline(commandBuffer, pipelineBindPoint, pipeline) } #[no_mangle] pub extern "C" fn vkCmdSetViewport( @@ -923,7 +923,7 @@ pub extern "C" fn vkCmdSetViewport( viewportCount: u32, pViewports: *const VkViewport, ) { - unimplemented!() + gfxCmdSetViewport(commandBuffer, firstViewport, viewportCount, pViewports) } #[no_mangle] pub extern "C" fn vkCmdSetScissor( @@ -932,11 +932,11 @@ pub extern "C" fn vkCmdSetScissor( scissorCount: u32, pScissors: *const VkRect2D, ) { - unimplemented!() + gfxCmdSetScissor(commandBuffer, firstScissor, scissorCount, pScissors) } #[no_mangle] pub extern "C" fn vkCmdSetLineWidth(commandBuffer: VkCommandBuffer, lineWidth: f32) { - unimplemented!() + gfxCmdSetLineWidth(commandBuffer, lineWidth) } #[no_mangle] pub extern "C" fn vkCmdSetDepthBias( @@ -945,14 +945,19 @@ pub extern "C" fn vkCmdSetDepthBias( depthBiasClamp: f32, depthBiasSlopeFactor: f32, ) { - unimplemented!() + gfxCmdSetDepthBias( + commandBuffer, + depthBiasConstantFactor, + depthBiasClamp, + depthBiasSlopeFactor, + ) } #[no_mangle] pub extern "C" fn vkCmdSetBlendConstants( commandBuffer: VkCommandBuffer, blendConstants: *const f32, ) { - unimplemented!() + gfxCmdSetBlendConstants(commandBuffer, blendConstants) } #[no_mangle] pub extern "C" fn vkCmdSetDepthBounds( @@ -960,7 +965,7 @@ pub extern "C" fn vkCmdSetDepthBounds( minDepthBounds: f32, maxDepthBounds: f32, ) { - unimplemented!() + gfxCmdSetDepthBounds(commandBuffer, minDepthBounds, maxDepthBounds) } #[no_mangle] pub extern "C" fn vkCmdSetStencilCompareMask( @@ -968,7 +973,7 @@ pub extern "C" fn vkCmdSetStencilCompareMask( faceMask: VkStencilFaceFlags, compareMask: u32, ) { - unimplemented!() + gfxCmdSetStencilCompareMask(commandBuffer, faceMask, compareMask) } #[no_mangle] pub extern "C" fn vkCmdSetStencilWriteMask( @@ -997,7 +1002,16 @@ pub extern "C" fn vkCmdBindDescriptorSets( dynamicOffsetCount: u32, pDynamicOffsets: *const u32, ) { - unimplemented!() + gfxCmdBindDescriptorSets( + commandBuffer, + pipelineBindPoint, + layout, + firstSet, + descriptorSetCount, + pDescriptorSets, + dynamicOffsetCount, + pDynamicOffsets, + ) } #[no_mangle] pub extern "C" fn vkCmdBindIndexBuffer( @@ -1006,7 +1020,7 @@ pub extern "C" fn vkCmdBindIndexBuffer( offset: VkDeviceSize, indexType: VkIndexType, ) { - unimplemented!() + gfxCmdBindIndexBuffer(commandBuffer, buffer, offset, indexType) } #[no_mangle] pub extern "C" fn vkCmdBindVertexBuffers( @@ -1032,7 +1046,7 @@ pub extern "C" fn vkCmdDraw( firstVertex: u32, firstInstance: u32, ) { - unimplemented!() + gfxCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance) } #[no_mangle] pub extern "C" fn vkCmdDrawIndexed( @@ -1043,7 +1057,14 @@ pub extern "C" fn vkCmdDrawIndexed( vertexOffset: i32, firstInstance: u32, ) { - unimplemented!() + gfxCmdDrawIndexed( + commandBuffer, + indexCount, + instanceCount, + firstIndex, + vertexOffset, + firstInstance, + ) } #[no_mangle] pub extern "C" fn vkCmdDrawIndirect( @@ -1365,7 +1386,7 @@ pub extern "C" fn vkQueuePresentKHR( queue: VkQueue, pPresentInfo: *const VkPresentInfoKHR, ) -> VkResult { - unimplemented!() + gfxQueuePresentKHR(queue, pPresentInfo) } #[no_mangle] pub extern "C" fn vkEnumerateInstanceExtensionProperties(