Added option loading example

This commit is contained in:
maik klein 2016-12-23 21:41:16 +01:00
parent c6a13fbe08
commit 03d44a42df

138
README.md
View file

@ -17,95 +17,101 @@ No.
Functions return a `type VkResult<T> = Result<T, vk::Result>` instead of an error code. No mutable references for the output are required. Functions return a `type VkResult<T> = Result<T, vk::Result>` instead of an error code. No mutable references for the output are required.
```Rust ```Rust
pub fn create_swapchain_khr(&self, pub fn create_swapchain_khr(&self,
create_info: &vk::SwapchainCreateInfoKHR) create_info: &vk::SwapchainCreateInfoKHR)
-> VkResult<vk::SwapchainKHR>; -> VkResult<vk::SwapchainKHR>;
let swapchain = device.create_swapchain_khr(&swapchain_create_info).unwrap(); let swapchain = device.create_swapchain_khr(&swapchain_create_info).unwrap();
``` ```
Always returns a `Vec<T>` for functions that output multiple values. Always returns a `Vec<T>` for functions that output multiple values.
```Rust ```Rust
pub fn get_swapchain_images_khr(&self, pub fn get_swapchain_images_khr(&self,
swapchain: vk::SwapchainKHR) swapchain: vk::SwapchainKHR)
-> VkResult<Vec<vk::Image>>; -> VkResult<Vec<vk::Image>>;
let present_images = device.get_swapchain_images_khr(swapchain).unwrap(); let present_images = device.get_swapchain_images_khr(swapchain).unwrap();
``` ```
Ash always uses slices in functions. Ash always uses slices in functions.
```Rust ```Rust
// C // C
void vkCmdPipelineBarrier( void vkCmdPipelineBarrier(
VkCommandBuffer commandBuffer, VkCommandBuffer commandBuffer,
VkPipelineStageFlags srcStageMask, VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask, VkPipelineStageFlags dstStageMask,
VkDependencyFlags dependencyFlags, VkDependencyFlags dependencyFlags,
uint32_t memoryBarrierCount, uint32_t memoryBarrierCount,
const VkMemoryBarrier* pMemoryBarriers, const VkMemoryBarrier* pMemoryBarriers,
uint32_t bufferMemoryBarrierCount, uint32_t bufferMemoryBarrierCount,
const VkBufferMemoryBarrier* pBufferMemoryBarriers, const VkBufferMemoryBarrier* pBufferMemoryBarriers,
uint32_t imageMemoryBarrierCount, uint32_t imageMemoryBarrierCount,
const VkImageMemoryBarrier* pImageMemoryBarriers); const VkImageMemoryBarrier* pImageMemoryBarriers);
// Rust // Rust
pub fn cmd_pipeline_barrier(&self, pub fn cmd_pipeline_barrier(&self,
command_buffer: vk::CommandBuffer, command_buffer: vk::CommandBuffer,
src_stage_mask: vk::PipelineStageFlags, src_stage_mask: vk::PipelineStageFlags,
dst_stage_mask: vk::PipelineStageFlags, dst_stage_mask: vk::PipelineStageFlags,
dependency_flags: vk::DependencyFlags, dependency_flags: vk::DependencyFlags,
memory_barriers: &[vk::MemoryBarrier], memory_barriers: &[vk::MemoryBarrier],
buffer_memory_barriers: &[vk::BufferMemoryBarrier], buffer_memory_barriers: &[vk::BufferMemoryBarrier],
image_memory_barriers: &[vk::ImageMemoryBarrier]); image_memory_barriers: &[vk::ImageMemoryBarrier]);
device.cmd_pipeline_barrier(setup_command_buffer, device.cmd_pipeline_barrier(setup_command_buffer,
vk::PIPELINE_STAGE_TOP_OF_PIPE_BIT, vk::PIPELINE_STAGE_TOP_OF_PIPE_BIT,
vk::PIPELINE_STAGE_TOP_OF_PIPE_BIT, vk::PIPELINE_STAGE_TOP_OF_PIPE_BIT,
vk::DependencyFlags::empty(), vk::DependencyFlags::empty(),
&[], &[],
&[], &[],
&[layout_transition_barrier]); &[layout_transition_barrier]);
// or // or
let slice = device.map_memory::<Vertex>(vertex_input_buffer_memory, let slice = device.map_memory::<Vertex>(vertex_input_buffer_memory,
0, 0,
vertex_input_buffer_info.size, vertex_input_buffer_info.size,
vk::MemoryMapFlags::empty()) vk::MemoryMapFlags::empty())
.unwrap(); .unwrap();
slice.copy_from_slice(&vertices); slice.copy_from_slice(&vertices);
``` ```
Ash still uses raw Vulkan structs. The only difference is type safety. Everything that can be an enum is an enum like `vk::StructureType`, flags are implemented similar to the `Bitflags` crate. Ash also follows the Rust style guide. The reason that Ash uses raw Vulkan structs is to be extensible, just like the Vulkan spec. Ash still uses raw Vulkan structs. The only difference is type safety. Everything that can be an enum is an enum like `vk::StructureType`, flags are implemented similar to the `Bitflags` crate. Ash also follows the Rust style guide. The reason that Ash uses raw Vulkan structs is to be extensible, just like the Vulkan spec.
```Rust ```Rust
let pool_create_info = vk::CommandPoolCreateInfo { let pool_create_info = vk::CommandPoolCreateInfo {
s_type: vk::StructureType::CommandPoolCreateInfo, s_type: vk::StructureType::CommandPoolCreateInfo,
p_next: ptr::null(), p_next: ptr::null(),
flags: vk::COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, flags: vk::COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT,
queue_family_index: queue_family_index, queue_family_index: queue_family_index,
}; };
let pool = device.create_command_pool(&pool_create_info).unwrap(); let pool = device.create_command_pool(&pool_create_info).unwrap();
``` ```
Ash also takes care of loading the function pointers. Function pointers are split into 3 categories. Entry, Instance and Device. The reason for not loading it into a global is that in Vulkan you can have multiple devices and each device must load its own function pointers. Ash also takes care of loading the function pointers. Function pointers are split into 3 categories. Entry, Instance and Device. The reason for not loading it into a global is that in Vulkan you can have multiple devices and each device must load its own function pointers.
```Rust ```Rust
// Looks for the vulkan lib in your path, alternatively you can supply the path explicitly. // Looks for the vulkan lib in your path, alternatively you can supply the path explicitly.
let entry = Entry::load_vulkan().unwrap(); let entry = Entry::load_vulkan().unwrap();
let instance: Instance = entry.create_instance(&create_info).expect("Instance creation error"); let instance: Instance = entry.create_instance(&create_info).expect("Instance creation error");
let device: Device = instance.create_device(pdevice, &device_create_info) let device: Device = instance.create_device(pdevice, &device_create_info)
.unwrap(); .unwrap();
```
Additionally, every Vulkan extensions has to be loaded explicity. You can find all extensions under [ash::extensions](https://github.com/MaikKlein/ash/tree/master/src/extensions). You still have to tell Vulkan which instance or device extensions you want to load.
```Rust
use ash::extensions::Swapchain;
let swapchain_loader = Swapchain::new(&instance, &device).expect("Unable to load swapchain");
let swapchain = swapchain_loader.create_swapchain_khr(&swapchain_create_info).unwrap();
``` ```
You don't have to pass an Instance or Device handle anymore, this is done implicitly for you. You don't have to pass an Instance or Device handle anymore, this is done implicitly for you.
```Rust ```Rust
// C // C
VkResult vkCreateCommandPool( VkResult vkCreateCommandPool(
VkDevice device, VkDevice device,
const VkCommandPoolCreateInfo* pCreateInfo, const VkCommandPoolCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator, const VkAllocationCallbacks* pAllocator,
VkCommandPool* pCommandPool); VkCommandPool* pCommandPool);
// Rust // Rust
pub fn create_command_pool(&self, pub fn create_command_pool(&self,
create_info: &vk::CommandPoolCreateInfo) create_info: &vk::CommandPoolCreateInfo)
-> VkResult<vk::CommandPool>; -> VkResult<vk::CommandPool>;
let pool = device.create_command_pool(&pool_create_info).unwrap(); let pool = device.create_command_pool(&pool_create_info).unwrap();
``` ```
## Example ## Example
You can find the examples [here](https://github.com/MaikKlein/ash/tree/master/examples). You can find the examples [here](https://github.com/MaikKlein/ash/tree/master/examples).