seperate device functions
This commit is contained in:
parent
606abdd86f
commit
7c0b1a4046
159
ash/src/device.rs
Normal file
159
ash/src/device.rs
Normal file
|
@ -0,0 +1,159 @@
|
||||||
|
use std::mem;
|
||||||
|
use vk_loader as vk;
|
||||||
|
use std::ptr;
|
||||||
|
use feature;
|
||||||
|
use surface;
|
||||||
|
use extensions::*;
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct PhysicalDevice {
|
||||||
|
pub ip: vk::InstancePointers,
|
||||||
|
pub handle: vk::PhysicalDevice,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PhysicalDevice {
|
||||||
|
pub fn has_surface_support(&self, index: u32, surface: &surface::Surface) -> bool {
|
||||||
|
unsafe {
|
||||||
|
let mut output: u32 = mem::uninitialized();
|
||||||
|
self.ip.GetPhysicalDeviceSurfaceSupportKHR(self.handle,
|
||||||
|
index,
|
||||||
|
surface.handle,
|
||||||
|
&mut output);
|
||||||
|
output != 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_physical_device_infos(&self) -> PhysicalDeviceInfos {
|
||||||
|
PhysicalDeviceInfos {
|
||||||
|
properties: self.get_physical_device_properties(),
|
||||||
|
queue_families: self.get_queue_families(),
|
||||||
|
memory: self.get_memory_properties(),
|
||||||
|
features: self.get_device_features(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_physical_device_properties(&self) -> vk::PhysicalDeviceProperties {
|
||||||
|
unsafe {
|
||||||
|
let mut device_prop: vk::PhysicalDeviceProperties = mem::uninitialized();
|
||||||
|
self.ip.GetPhysicalDeviceProperties(self.handle, &mut device_prop);
|
||||||
|
device_prop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_queue_families(&self) -> Vec<vk::QueueFamilyProperties> {
|
||||||
|
unsafe {
|
||||||
|
let mut queue_count = 0;
|
||||||
|
self.ip
|
||||||
|
.GetPhysicalDeviceQueueFamilyProperties(self.handle,
|
||||||
|
&mut queue_count,
|
||||||
|
ptr::null_mut());
|
||||||
|
let mut queue_families_vec = Vec::with_capacity(queue_count as usize);
|
||||||
|
self.ip
|
||||||
|
.GetPhysicalDeviceQueueFamilyProperties(self.handle,
|
||||||
|
&mut queue_count,
|
||||||
|
queue_families_vec.as_mut_ptr());
|
||||||
|
queue_families_vec.set_len(queue_count as usize);
|
||||||
|
queue_families_vec
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_memory_properties(&self) -> vk::PhysicalDeviceMemoryProperties {
|
||||||
|
unsafe {
|
||||||
|
let mut output = mem::uninitialized();
|
||||||
|
self.ip.GetPhysicalDeviceMemoryProperties(self.handle, &mut output);
|
||||||
|
output
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_device_features(&self) -> feature::Features {
|
||||||
|
let available_features: vk::PhysicalDeviceFeatures = unsafe {
|
||||||
|
let mut output = mem::uninitialized();
|
||||||
|
self.ip.GetPhysicalDeviceFeatures(self.handle, &mut output);
|
||||||
|
output
|
||||||
|
};
|
||||||
|
feature::Features::from(available_features)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_device(&self,
|
||||||
|
present: u32,
|
||||||
|
ext: &DeviceExtension,
|
||||||
|
features: &feature::Features)
|
||||||
|
-> Device {
|
||||||
|
let f = vk::PhysicalDeviceFeatures::from(*features);
|
||||||
|
let priorities = [1.0];
|
||||||
|
let queue_info = vk::DeviceQueueCreateInfo {
|
||||||
|
sType: vk::STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
|
||||||
|
pNext: ptr::null(),
|
||||||
|
flags: 0,
|
||||||
|
queueFamilyIndex: present,
|
||||||
|
pQueuePriorities: priorities.as_ptr(),
|
||||||
|
queueCount: priorities.len() as u32,
|
||||||
|
};
|
||||||
|
|
||||||
|
let create_info = vk::DeviceCreateInfo {
|
||||||
|
sType: vk::STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
||||||
|
pNext: ptr::null(),
|
||||||
|
flags: 0,
|
||||||
|
queueCreateInfoCount: 1,
|
||||||
|
pQueueCreateInfos: &queue_info,
|
||||||
|
enabledLayerCount: 0,
|
||||||
|
ppEnabledLayerNames: ptr::null(),
|
||||||
|
enabledExtensionCount: 0,
|
||||||
|
ppEnabledExtensionNames: ptr::null(),
|
||||||
|
pEnabledFeatures: ptr::null(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut vk_device = unsafe { mem::uninitialized() };
|
||||||
|
unsafe {
|
||||||
|
assert!(self.ip.CreateDevice(self.handle, &create_info, ptr::null(), &mut vk_device) ==
|
||||||
|
0,
|
||||||
|
"device");
|
||||||
|
}
|
||||||
|
let dp = vk::DevicePointers::load(|name| {
|
||||||
|
unsafe { self.ip.GetDeviceProcAddr(vk_device, name.as_ptr()) as *const _ }
|
||||||
|
});
|
||||||
|
Device {
|
||||||
|
dp: dp,
|
||||||
|
device: vk_device,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct PhysicalDeviceInfos {
|
||||||
|
pub properties: vk::PhysicalDeviceProperties,
|
||||||
|
pub queue_families: Vec<vk::QueueFamilyProperties>,
|
||||||
|
pub memory: vk::PhysicalDeviceMemoryProperties,
|
||||||
|
pub features: feature::Features,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PhysicalDeviceInfos {
|
||||||
|
fn has_surface_support(&self) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Queue {
|
||||||
|
handle: vk::Queue,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Device {
|
||||||
|
dp: vk::DevicePointers,
|
||||||
|
device: vk::Device,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Device {
|
||||||
|
pub fn get_device_queue(&self, family: u32, index: u32) -> Queue {
|
||||||
|
unsafe {
|
||||||
|
let mut queue = mem::uninitialized();
|
||||||
|
self.dp.GetDeviceQueue(self.device, family, index, &mut queue);
|
||||||
|
Queue { handle: queue }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Device {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe {
|
||||||
|
self.dp.DestroyDevice(self.device, ptr::null());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -14,139 +14,14 @@ use extensions::*;
|
||||||
use std::os::raw::*;
|
use std::os::raw::*;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use surface;
|
use surface;
|
||||||
|
use device::*;
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct PhysicalDevice {
|
|
||||||
ip: vk::InstancePointers,
|
|
||||||
handle: vk::PhysicalDevice,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct DebugCallback {
|
pub struct DebugCallback {
|
||||||
handle: vk::DebugReportCallbackEXT,
|
handle: vk::DebugReportCallbackEXT,
|
||||||
f: *mut Fn(String),
|
f: *mut Fn(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PhysicalDevice {
|
|
||||||
pub fn has_surface_support(&self, index: u32, surface: &Surface) -> bool {
|
|
||||||
unsafe {
|
|
||||||
let mut output: u32 = mem::uninitialized();
|
|
||||||
self.ip.GetPhysicalDeviceSurfaceSupportKHR(self.handle,
|
|
||||||
index,
|
|
||||||
surface.handle,
|
|
||||||
&mut output);
|
|
||||||
output != 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_physical_device_infos(&self) -> PhysicalDeviceInfos {
|
|
||||||
PhysicalDeviceInfos {
|
|
||||||
properties: self.get_physical_device_properties(),
|
|
||||||
queue_families: self.get_queue_families(),
|
|
||||||
memory: self.get_memory_properties(),
|
|
||||||
features: self.get_device_features(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_physical_device_properties(&self) -> vk::PhysicalDeviceProperties {
|
|
||||||
unsafe {
|
|
||||||
let mut device_prop: vk::PhysicalDeviceProperties = mem::uninitialized();
|
|
||||||
self.ip.GetPhysicalDeviceProperties(self.handle, &mut device_prop);
|
|
||||||
device_prop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_queue_families(&self) -> Vec<vk::QueueFamilyProperties> {
|
|
||||||
unsafe {
|
|
||||||
let mut queue_count = 0;
|
|
||||||
self.ip
|
|
||||||
.GetPhysicalDeviceQueueFamilyProperties(self.handle,
|
|
||||||
&mut queue_count,
|
|
||||||
ptr::null_mut());
|
|
||||||
let mut queue_families_vec = Vec::with_capacity(queue_count as usize);
|
|
||||||
self.ip
|
|
||||||
.GetPhysicalDeviceQueueFamilyProperties(self.handle,
|
|
||||||
&mut queue_count,
|
|
||||||
queue_families_vec.as_mut_ptr());
|
|
||||||
queue_families_vec.set_len(queue_count as usize);
|
|
||||||
queue_families_vec
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_memory_properties(&self) -> vk::PhysicalDeviceMemoryProperties {
|
|
||||||
unsafe {
|
|
||||||
let mut output = mem::uninitialized();
|
|
||||||
self.ip.GetPhysicalDeviceMemoryProperties(self.handle, &mut output);
|
|
||||||
output
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_device_features(&self) -> feature::Features {
|
|
||||||
let available_features: vk::PhysicalDeviceFeatures = unsafe {
|
|
||||||
let mut output = mem::uninitialized();
|
|
||||||
self.ip.GetPhysicalDeviceFeatures(self.handle, &mut output);
|
|
||||||
output
|
|
||||||
};
|
|
||||||
feature::Features::from(available_features)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_device(&self,
|
|
||||||
present: u32,
|
|
||||||
ext: &DeviceExtension,
|
|
||||||
features: &feature::Features)
|
|
||||||
-> Device {
|
|
||||||
let f = vk::PhysicalDeviceFeatures::from(*features);
|
|
||||||
let priorities = [1.0];
|
|
||||||
let queue_info = vk::DeviceQueueCreateInfo {
|
|
||||||
sType: vk::STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
|
|
||||||
pNext: ptr::null(),
|
|
||||||
flags: 0,
|
|
||||||
queueFamilyIndex: present,
|
|
||||||
pQueuePriorities: priorities.as_ptr(),
|
|
||||||
queueCount: priorities.len() as u32,
|
|
||||||
};
|
|
||||||
|
|
||||||
let create_info = vk::DeviceCreateInfo {
|
|
||||||
sType: vk::STRUCTURE_TYPE_DEVICE_CREATE_INFO,
|
|
||||||
pNext: ptr::null(),
|
|
||||||
flags: 0,
|
|
||||||
queueCreateInfoCount: 1,
|
|
||||||
pQueueCreateInfos: &queue_info,
|
|
||||||
enabledLayerCount: 0,
|
|
||||||
ppEnabledLayerNames: ptr::null(),
|
|
||||||
enabledExtensionCount: 0,
|
|
||||||
ppEnabledExtensionNames: ptr::null(),
|
|
||||||
pEnabledFeatures: ptr::null(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut vk_device = unsafe { mem::uninitialized() };
|
|
||||||
unsafe {
|
|
||||||
assert!(self.ip.CreateDevice(self.handle, &create_info, ptr::null(), &mut vk_device) ==
|
|
||||||
0,
|
|
||||||
"device");
|
|
||||||
}
|
|
||||||
let dp = vk::DevicePointers::load(|name| {
|
|
||||||
unsafe { self.ip.GetDeviceProcAddr(vk_device, name.as_ptr()) as *const _ }
|
|
||||||
});
|
|
||||||
Device {
|
|
||||||
dp: dp,
|
|
||||||
device: vk_device,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct PhysicalDeviceInfos {
|
|
||||||
pub properties: vk::PhysicalDeviceProperties,
|
|
||||||
pub queue_families: Vec<vk::QueueFamilyProperties>,
|
|
||||||
pub memory: vk::PhysicalDeviceMemoryProperties,
|
|
||||||
pub features: feature::Features,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PhysicalDeviceInfos {
|
|
||||||
fn has_surface_support(&self) -> bool {
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Instance {
|
pub struct Instance {
|
||||||
pub instance: vk::Instance,
|
pub instance: vk::Instance,
|
||||||
pub ip: vk::InstancePointers,
|
pub ip: vk::InstancePointers,
|
||||||
|
@ -172,21 +47,10 @@ pub struct ApplicationInfo {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Surface<'r> {
|
|
||||||
instance: &'r Instance,
|
|
||||||
handle: vk::SurfaceKHR,
|
|
||||||
}
|
|
||||||
impl<'r> Drop for Surface<'r> {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
unsafe {
|
|
||||||
self.instance.ip.DestroySurfaceKHR(self.instance.instance, self.handle, ptr::null());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
pub fn create_surface<S: surface::Surface>(&self, s: &S) -> Surface {
|
pub fn create_surface<S: surface::VulkanSurface>(&self, s: &S) -> surface::Surface {
|
||||||
Surface {
|
surface::Surface {
|
||||||
instance: self,
|
instance: self,
|
||||||
handle: s.create_surface(self),
|
handle: s.create_surface(self),
|
||||||
}
|
}
|
||||||
|
@ -342,30 +206,3 @@ impl Instance {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Queue {
|
|
||||||
handle: vk::Queue,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Device {
|
|
||||||
dp: vk::DevicePointers,
|
|
||||||
device: vk::Device,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Device {
|
|
||||||
pub fn get_device_queue(&self, family: u32, index: u32) -> Queue {
|
|
||||||
unsafe {
|
|
||||||
let mut queue = mem::uninitialized();
|
|
||||||
self.dp.GetDeviceQueue(self.device, family, index, &mut queue);
|
|
||||||
Queue { handle: queue }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for Device {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
unsafe {
|
|
||||||
self.dp.DestroyDevice(self.device, ptr::null());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -9,3 +9,4 @@ pub mod extensions;
|
||||||
pub mod surface;
|
pub mod surface;
|
||||||
pub mod instance;
|
pub mod instance;
|
||||||
pub mod feature;
|
pub mod feature;
|
||||||
|
pub mod device;
|
||||||
|
|
|
@ -4,11 +4,11 @@ use std::mem;
|
||||||
use instance::Instance;
|
use instance::Instance;
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
use std::os::raw::c_void;
|
use std::os::raw::c_void;
|
||||||
pub trait Surface {
|
pub trait VulkanSurface {
|
||||||
fn create_surface(&self, inst: &Instance) -> vk::SurfaceKHR;
|
fn create_surface(&self, inst: &Instance) -> vk::SurfaceKHR;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Surface for Window {
|
impl VulkanSurface for Window {
|
||||||
fn create_surface(&self, inst: &Instance) -> vk::SurfaceKHR {
|
fn create_surface(&self, inst: &Instance) -> vk::SurfaceKHR {
|
||||||
unsafe {
|
unsafe {
|
||||||
let x11_display = self.glfw.get_x11_display();
|
let x11_display = self.glfw.get_x11_display();
|
||||||
|
@ -26,3 +26,15 @@ impl Surface for Window {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Surface<'r> {
|
||||||
|
pub instance: &'r Instance,
|
||||||
|
pub handle: vk::SurfaceKHR,
|
||||||
|
}
|
||||||
|
impl<'r> Drop for Surface<'r> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe {
|
||||||
|
self.instance.ip.DestroySurfaceKHR(self.instance.instance, self.handle, ptr::null());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue