Surface creation/destruction

This commit is contained in:
Dzmitry Malyshau 2017-11-13 22:55:43 -05:00
parent fcdebb5160
commit 432331abce
5 changed files with 59 additions and 32 deletions

View file

@ -7,6 +7,9 @@ authors = ["Dzmitry Malyshau <kvark@mozilla.com>"]
name = "portability" name = "portability"
crate-type = ["staticlib"] crate-type = ["staticlib"]
[dependencies]
winit = "0.7"
[dependencies.gfx-hal] [dependencies.gfx-hal]
#path = "../gfx/src/hal" #path = "../gfx/src/hal"
git = "https://github.com/kvark/gfx-rs" git = "https://github.com/kvark/gfx-rs"

View file

@ -6,7 +6,7 @@ OBJECTS=$(NATIVE_DIR)/test.o
LIBRARY=target/debug/libportability.a LIBRARY=target/debug/libportability.a
CC=gcc CC=gcc
CFLAGS=-I$(VULKAN_DIR) CFLAGS=-ggdb -O0 -I$(VULKAN_DIR)
DEPS= DEPS=
LDFLAGS=-lpthread -ldl -lm -lX11 LDFLAGS=-lpthread -ldl -lm -lX11
@ -23,7 +23,7 @@ $(LIBRARY): src/*.rs Cargo.toml Cargo.lock
cargo build cargo build
mkdir -p target/native mkdir -p target/native
$(NATIVE_DIR)/%.o: native/%.c $(DEPS) $(NATIVE_DIR)/%.o: native/%.c $(DEPS) Makefile
$(CC) -c -o $@ $< $(CFLAGS) $(CC) -c -o $@ $< $(CFLAGS)
$(TARGET): $(LIBRARY) $(OBJECTS) Makefile $(TARGET): $(LIBRARY) $(OBJECTS) Makefile

View file

@ -4,16 +4,17 @@
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
VkSurfaceKHR vkCreateSurfaceGFX(VkInstance);
int main() { int main() {
printf("starting the portability test\n"); printf("starting the portability test\n");
VkInstanceCreateInfo inst_info = {};
inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
VkInstance instance; VkInstance instance;
VkResult res; VkResult res = 0;
unsigned int i; unsigned int i;
VkInstanceCreateInfo inst_info = {};
inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
res = vkCreateInstance(&inst_info, NULL, &instance); res = vkCreateInstance(&inst_info, NULL, &instance);
if (res == VK_ERROR_INCOMPATIBLE_DRIVER) { if (res == VK_ERROR_INCOMPATIBLE_DRIVER) {
printf("cannot find a compatible Vulkan ICD\n"); printf("cannot find a compatible Vulkan ICD\n");
@ -23,6 +24,9 @@ int main() {
return -1; return -1;
} }
VkSurfaceKHR surface = vkCreateSurfaceGFX(instance);
printf("\tvkCreateSurfaceGFX\n");
uint32_t adapter_count = 1; uint32_t adapter_count = 1;
VkPhysicalDevice physical_devices[1] = {}; VkPhysicalDevice physical_devices[1] = {};
res = vkEnumeratePhysicalDevices(instance, &adapter_count, physical_devices); res = vkEnumeratePhysicalDevices(instance, &adapter_count, physical_devices);
@ -91,6 +95,7 @@ int main() {
vkFreeCommandBuffers(device, cmd_pool, 1, &cmd_buffer); vkFreeCommandBuffers(device, cmd_pool, 1, &cmd_buffer);
vkDestroyCommandPool(device, cmd_pool, NULL); vkDestroyCommandPool(device, cmd_pool, NULL);
vkDestroySurfaceKHR(instance, surface, NULL);
vkDestroyDevice(device, NULL); vkDestroyDevice(device, NULL);
vkDestroyInstance(instance, NULL); vkDestroyInstance(instance, NULL);

View file

@ -1,32 +1,23 @@
use std::{fmt, ops}; use std::{fmt, ops};
use std::marker::PhantomData;
#[repr(C)] #[repr(C)]
pub struct Handle<T> { pub struct Handle<T>(*mut T);
pointer: *mut u8,
marker: PhantomData<T>,
}
impl<T> Handle<T> { impl<T> Handle<T> {
pub fn new(value: T) -> Self { pub fn new(value: T) -> Self {
Handle { let ptr = Box::into_raw(Box::new(value));
pointer: Box::into_raw(Box::new(value)) as _, Handle(ptr)
marker: PhantomData,
}
} }
pub fn unwrap(self) -> Box<T> { pub fn unwrap(self) -> Box<T> {
unsafe { Box::from_raw(self.pointer as _) } unsafe { Box::from_raw(self.0) }
} }
} }
impl<T> Clone for Handle<T> { impl<T> Clone for Handle<T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Handle { Handle(self.0)
pointer: self.pointer,
marker: PhantomData,
}
} }
} }
@ -35,19 +26,18 @@ impl<T> Copy for Handle<T> {}
impl<T> ops::Deref for Handle<T> { impl<T> ops::Deref for Handle<T> {
type Target = T; type Target = T;
fn deref(&self) -> &T { fn deref(&self) -> &T {
unsafe { &*(self.pointer as *mut _) } unsafe { & *self.0 }
} }
} }
impl<T> ops::DerefMut for Handle<T> { impl<T> ops::DerefMut for Handle<T> {
fn deref_mut(&mut self) -> &mut T { fn deref_mut(&mut self) -> &mut T {
unsafe { &mut*(self.pointer as *mut _) } unsafe { &mut *self.0 }
} }
} }
impl<T> fmt::Debug for Handle<T> { impl<T> fmt::Debug for Handle<T> {
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
//TODO write!(formatter, "Handle({:p})", self.0)
Ok(())
} }
} }

View file

@ -1,9 +1,11 @@
#![allow(non_snake_case)] #![allow(non_snake_case)]
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![allow(non_upper_case_globals)] #![allow(non_upper_case_globals)]
#![allow(improper_ctypes)] //TEMP: buggy Rustc FFI analysis
extern crate gfx_hal as hal; extern crate gfx_hal as hal;
extern crate gfx_backend_vulkan as back; extern crate gfx_backend_vulkan as back;
extern crate winit;
mod handle; mod handle;
@ -5197,12 +5199,17 @@ extern "C" {
commandBufferCount: u32, commandBufferCount: u32,
pCommandBuffers: *const VkCommandBuffer); pCommandBuffers: *const VkCommandBuffer);
} }
//NOTE: all *KHR types have to be pure `Handle` things for compatibility with
//`VK_DEFINE_NON_DISPATCHABLE_HANDLE` used in `vulkan.h`
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] pub struct VkSurfaceInner<Backend: hal::Backend> {
pub struct VkSurfaceKHR_T { raw: Backend::Surface,
_unused: [u8; 0], window: winit::Window,
events_loop: winit::EventsLoop,
} }
pub type VkSurfaceKHR = *mut VkSurfaceKHR_T; pub type VkSurfaceKHR = Handle<VkSurfaceInner<B>>;
pub const VkColorSpaceKHR_VK_COLOR_SPACE_BEGIN_RANGE_KHR: VkColorSpaceKHR = pub const VkColorSpaceKHR_VK_COLOR_SPACE_BEGIN_RANGE_KHR: VkColorSpaceKHR =
VkColorSpaceKHR::VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; VkColorSpaceKHR::VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
pub const VkColorSpaceKHR_VK_COLOR_SPACE_END_RANGE_KHR: VkColorSpaceKHR = pub const VkColorSpaceKHR_VK_COLOR_SPACE_END_RANGE_KHR: VkColorSpaceKHR =
@ -5314,10 +5321,16 @@ pub type PFN_vkGetPhysicalDeviceSurfacePresentModesKHR =
pPresentModes: pPresentModes:
*mut VkPresentModeKHR) *mut VkPresentModeKHR)
-> VkResult>; -> VkResult>;
extern "C" {
pub fn vkDestroySurfaceKHR(instance: VkInstance, surface: VkSurfaceKHR, #[no_mangle]
pAllocator: *const VkAllocationCallbacks); pub extern fn vkDestroySurfaceKHR(
_instance: VkInstance,
surface: VkSurfaceKHR,
_: *const VkAllocationCallbacks,
) {
let _ = surface.unwrap(); //TODO
} }
extern "C" { extern "C" {
pub fn vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice: pub fn vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice:
VkPhysicalDevice, VkPhysicalDevice,
@ -5353,6 +5366,22 @@ extern "C" {
*mut VkPresentModeKHR) *mut VkPresentModeKHR)
-> VkResult; -> VkResult;
} }
/// This is an EXTRA function not in original vulkan.h
#[no_mangle]
pub extern fn vkCreateSurfaceGFX(instance: VkInstance) -> VkSurfaceKHR {
let events_loop = winit::EventsLoop::new();
let window = winit::Window::new(&events_loop).unwrap();
let inner = VkSurfaceInner {
raw: instance.create_surface(&window),
window: window,
events_loop: events_loop,
};
Handle::new(inner)
}
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct VkSwapchainKHR_T { pub struct VkSwapchainKHR_T {