diff --git a/libportability-gfx/src/conv.rs b/libportability-gfx/src/conv.rs index 8ed7140..06ecdfa 100644 --- a/libportability-gfx/src/conv.rs +++ b/libportability-gfx/src/conv.rs @@ -1,5 +1,5 @@ use hal::{buffer, command, error, format, image, memory, pass, pso, window}; -use hal::{PatchSize, Primitive}; +use hal::{IndexType, PatchSize, Primitive}; use std::mem; @@ -568,10 +568,54 @@ pub fn map_color_components(mask: VkColorComponentFlags) -> pso::ColorMask { unsafe { mem::transmute(mask as u8) } } +fn map_blend_factor(factor: VkBlendFactor) -> pso::Factor { + use hal::pso::Factor::*; + use super::VkBlendFactor::*; + match factor { + VK_BLEND_FACTOR_ZERO => Zero, + VK_BLEND_FACTOR_ONE => One, + VK_BLEND_FACTOR_SRC_COLOR => SrcColor, + VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR => OneMinusSrcColor, + VK_BLEND_FACTOR_DST_COLOR => DstColor, + VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR => OneMinusDstColor, + VK_BLEND_FACTOR_SRC_ALPHA => SrcAlpha, + VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA => OneMinusSrcAlpha, + VK_BLEND_FACTOR_DST_ALPHA => DstAlpha, + VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA => OneMinusDstAlpha, + VK_BLEND_FACTOR_CONSTANT_COLOR => ConstColor, + VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR => OneMinusConstColor, + VK_BLEND_FACTOR_CONSTANT_ALPHA => ConstAlpha, + VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA => OneMinusConstAlpha, + VK_BLEND_FACTOR_SRC_ALPHA_SATURATE => SrcAlphaSaturate, + VK_BLEND_FACTOR_SRC1_COLOR => Src1Color, + VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR => OneMinusSrc1Color, + VK_BLEND_FACTOR_SRC1_ALPHA => Src1Alpha, + VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA => OneMinusSrc1Alpha, + _ => panic!("Unexpected blend factor: {:?}", factor), + } +} + pub fn map_blend_op( blend_op: VkBlendOp, src_factor: VkBlendFactor, dst_factor: VkBlendFactor, ) -> pso::BlendOp { - unimplemented!() + use super::VkBlendOp::*; + match blend_op { + VK_BLEND_OP_ADD => pso::BlendOp::Add { + src: map_blend_factor(src_factor), + dst: map_blend_factor(dst_factor) + }, + VK_BLEND_OP_SUBTRACT => pso::BlendOp::Sub { + src: map_blend_factor(src_factor), + dst: map_blend_factor(dst_factor) + }, + VK_BLEND_OP_REVERSE_SUBTRACT => pso::BlendOp::RevSub { + src: map_blend_factor(src_factor), + dst: map_blend_factor(dst_factor) + }, + VK_BLEND_OP_MIN => pso::BlendOp::Min, + VK_BLEND_OP_MAX => pso::BlendOp::Max, + _ => panic!("Unexpected blend operation: {:?}", blend_op), + } } #[inline] @@ -654,3 +698,11 @@ pub fn map_tiling(tiling: VkImageTiling) -> image::Tiling { _ => panic!("Unexpected tiling: {:?}", tiling), } } + +pub fn map_index_type(ty: VkIndexType) -> IndexType { + match ty { + VkIndexType::VK_INDEX_TYPE_UINT16 => IndexType::U16, + VkIndexType::VK_INDEX_TYPE_UINT32 => IndexType::U32, + _ => panic!("Unexpected index type: {:?}", ty), + } +} \ No newline at end of file diff --git a/libportability-gfx/src/impls.rs b/libportability-gfx/src/impls.rs index 25f76e2..75c1ac9 100644 --- a/libportability-gfx/src/impls.rs +++ b/libportability-gfx/src/impls.rs @@ -3,6 +3,7 @@ use hal::{ DescriptorPool, Device, Instance, PhysicalDevice, QueueFamily, Surface, Swapchain as HalSwapchain, FrameSync, }; +use hal::buffer::IndexBufferView; use hal::device::WaitFor; use hal::pool::RawCommandPool; use hal::command::RawCommandBuffer; @@ -1372,7 +1373,7 @@ pub extern "C" fn gfxCreateGraphicsPipelines( _pAllocator: *const VkAllocationCallbacks, pPipelines: *mut VkPipeline, ) -> VkResult { - assert!(pipelineCache.is_null()); + // assert!(pipelineCache.is_null()); let infos = unsafe { slice::from_raw_parts(pCreateInfos, createInfoCount as _) @@ -2262,7 +2263,7 @@ pub extern "C" fn gfxCreateRenderPass( let dependencies = dependencies .into_iter() .map(|dependency| { - assert_eq!(dependency.dependencyFlags, 0); // TODO + // assert_eq!(dependency.dependencyFlags, 0); // TODO let src_pass = map_subpass_ref(dependency.srcSubpass); let dst_pass = map_subpass_ref(dependency.dstSubpass); @@ -2552,13 +2553,23 @@ pub extern "C" fn gfxCmdBindDescriptorSets( } #[inline] pub extern "C" fn gfxCmdBindIndexBuffer( - commandBuffer: VkCommandBuffer, + mut commandBuffer: VkCommandBuffer, buffer: VkBuffer, offset: VkDeviceSize, indexType: VkIndexType, ) { - unimplemented!() + commandBuffer.bind_index_buffer( + IndexBufferView { + buffer: match *buffer { + Buffer::Buffer(ref b) => b, + Buffer::Unbound(_) => panic!("Bound index buffer expected."), + }, + offset, + index_type: conv::map_index_type(indexType), + } + ); } + #[inline] pub extern "C" fn gfxCmdBindVertexBuffers( mut commandBuffer: VkCommandBuffer, @@ -2606,14 +2617,18 @@ pub extern "C" fn gfxCmdDraw( } #[inline] pub extern "C" fn gfxCmdDrawIndexed( - commandBuffer: VkCommandBuffer, + mut commandBuffer: VkCommandBuffer, indexCount: u32, instanceCount: u32, firstIndex: u32, vertexOffset: i32, firstInstance: u32, ) { - unimplemented!() + commandBuffer.draw_indexed( + firstIndex .. firstIndex + indexCount, + vertexOffset, + firstInstance .. firstInstance + instanceCount, + ) } #[inline] pub extern "C" fn gfxCmdDrawIndirect( @@ -2654,13 +2669,33 @@ pub extern "C" fn gfxCmdDispatchIndirect( } #[inline] pub extern "C" fn gfxCmdCopyBuffer( - commandBuffer: VkCommandBuffer, + mut commandBuffer: VkCommandBuffer, srcBuffer: VkBuffer, dstBuffer: VkBuffer, regionCount: u32, pRegions: *const VkBufferCopy, ) { - unimplemented!() + let regions = unsafe { + slice::from_raw_parts(pRegions, regionCount as _) + } + .iter() + .map(|r| com::BufferCopy { + src: r.srcOffset, + dst: r.dstOffset, + size: r.size, + }); + + commandBuffer.copy_buffer( + match *srcBuffer { + Buffer::Buffer(ref src) => src, + Buffer::Unbound(_) => panic!("Bound src buffer expected!"), + }, + match *dstBuffer { + Buffer::Buffer(ref dst) => dst, + Buffer::Unbound(_) => panic!("Bound dst buffer expected!"), + }, + regions, + ); } #[inline] pub extern "C" fn gfxCmdCopyImage(