Unify C headers

Both headers are actually included at the same time by glfw3.h .
This commit is contained in:
Stephen Seo 2024-03-04 12:07:33 +09:00
parent 92e0e0fdec
commit 9bbc5e156a
2 changed files with 29 additions and 47 deletions

View file

@ -7,24 +7,14 @@ fn main() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let vk_bindings = bindgen::Builder::default()
.header("/usr/include/vulkan/vulkan.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate vulkan bindings");
vk_bindings
.write_to_file(out_path.join("vk_bindings.rs"))
.expect("Couldn't write vk bindings!");
let glfw_bindings = bindgen::Builder::default()
let glfw_vk_bindings = bindgen::Builder::default()
.header_contents("glfw_defines", "#define GLFW_INCLUDE_VULKAN")
.header("/usr/include/GLFW/glfw3.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.generate()
.expect("Unable to generate glfw bindings");
glfw_bindings
.write_to_file(out_path.join("glfw_bindings.rs"))
glfw_vk_bindings
.write_to_file(out_path.join("glfw_vk_bindings.rs"))
.expect("Couldn't write glfw bindings!");
}

View file

@ -1,36 +1,28 @@
mod ffi_vk {
mod ffi {
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(unused_imports)]
#![allow(dead_code)]
include!(concat!(env!("OUT_DIR"), "/vk_bindings.rs"));
include!(concat!(env!("OUT_DIR"), "/glfw_vk_bindings.rs"));
pub fn VK_MAKE_VERSION(major: u32, minor: u32, patch: u32) -> u32 {
(major << 22) | (minor << 12) | patch
}
pub fn VK_MAKE_API_VERSION(variant: u32, major: u32, minor: u32, patch: u32) -> u32 {
(variant << 29) | (major << 22) | (minor << 12) | patch
}
}
mod ffi_glfw {
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(unused_imports)]
#![allow(dead_code)]
include!(concat!(env!("OUT_DIR"), "/glfw_bindings.rs"));
}
use std::ffi::CString;
const WINDOW_WIDTH: i32 = 800;
const WINDOW_HEIGHT: i32 = 600;
struct VulkanApp {
window: *mut ffi_glfw::GLFWwindow,
vk_instance: ffi_vk::VkInstance,
window: *mut ffi::GLFWwindow,
vk_instance: ffi::VkInstance,
}
impl VulkanApp {
@ -44,13 +36,13 @@ impl VulkanApp {
fn init_glfw(&mut self) {
let app_title = CString::new("Vulkan").unwrap();
unsafe {
ffi_glfw::glfwInit();
ffi_glfw::glfwWindowHint(
ffi_glfw::GLFW_CLIENT_API as i32,
ffi_glfw::GLFW_NO_API as i32,
ffi::glfwInit();
ffi::glfwWindowHint(
ffi::GLFW_CLIENT_API as i32,
ffi::GLFW_NO_API as i32,
);
ffi_glfw::glfwWindowHint(ffi_glfw::GLFW_RESIZABLE as i32, ffi_glfw::GLFW_FALSE as i32);
self.window = ffi_glfw::glfwCreateWindow(
ffi::glfwWindowHint(ffi::GLFW_RESIZABLE as i32, ffi::GLFW_FALSE as i32);
self.window = ffi::glfwCreateWindow(
WINDOW_WIDTH,
WINDOW_HEIGHT,
app_title.as_ptr(),
@ -67,14 +59,14 @@ impl VulkanApp {
// Create instance.
let app_name = CString::new("Vulkan Triangle").unwrap();
let engine_name = CString::new("No Engine").unwrap();
let app_info = ffi_vk::VkApplicationInfo {
sType: ffi_vk::VkStructureType_VK_STRUCTURE_TYPE_APPLICATION_INFO,
let app_info = ffi::VkApplicationInfo {
sType: ffi::VkStructureType_VK_STRUCTURE_TYPE_APPLICATION_INFO,
pNext: std::ptr::null_mut(),
pApplicationName: app_name.as_ptr(),
applicationVersion: ffi_vk::VK_MAKE_VERSION(1, 0, 0),
applicationVersion: ffi::VK_MAKE_VERSION(1, 0, 0),
pEngineName: engine_name.as_ptr(),
engineVersion: ffi_vk::VK_MAKE_VERSION(1, 0, 0),
apiVersion: ffi_vk::VK_MAKE_API_VERSION(0, 1, 0, 0),
engineVersion: ffi::VK_MAKE_VERSION(1, 0, 0),
apiVersion: ffi::VK_MAKE_API_VERSION(0, 1, 0, 0),
};
// Populate VkInstanceCreateInfo.
@ -83,12 +75,12 @@ impl VulkanApp {
let mut ext_count: u32 = 0;
let exts: *mut *const std::ffi::c_char;
unsafe {
exts = ffi_glfw::glfwGetRequiredInstanceExtensions(std::ptr::addr_of_mut!(ext_count));
exts = ffi::glfwGetRequiredInstanceExtensions(std::ptr::addr_of_mut!(ext_count));
}
// Second populate the struct with necessary info.
let create_info = ffi_vk::VkInstanceCreateInfo {
sType: ffi_vk::VkStructureType_VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
let create_info = ffi::VkInstanceCreateInfo {
sType: ffi::VkStructureType_VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
pNext: std::ptr::null(),
flags: 0,
pApplicationInfo: std::ptr::addr_of!(app_info),
@ -99,14 +91,14 @@ impl VulkanApp {
};
let vk_result = unsafe {
ffi_vk::vkCreateInstance(
ffi::vkCreateInstance(
std::ptr::addr_of!(create_info),
std::ptr::null(),
std::ptr::addr_of_mut!(self.vk_instance),
)
};
if vk_result != ffi_vk::VkResult_VK_SUCCESS {
if vk_result != ffi::VkResult_VK_SUCCESS {
panic!("ERROR: Failed to create vk instance!");
}
}
@ -122,10 +114,10 @@ impl VulkanApp {
loop {
unsafe {
if ffi_glfw::glfwWindowShouldClose(self.window) != 0 {
if ffi::glfwWindowShouldClose(self.window) != 0 {
return;
}
ffi_glfw::glfwPollEvents();
ffi::glfwPollEvents();
}
}
}
@ -135,18 +127,18 @@ impl Drop for VulkanApp {
fn drop(&mut self) {
if !self.vk_instance.is_null() {
unsafe {
ffi_vk::vkDestroyInstance(self.vk_instance, std::ptr::null());
ffi::vkDestroyInstance(self.vk_instance, std::ptr::null());
}
}
if !self.window.is_null() {
unsafe {
ffi_glfw::glfwDestroyWindow(self.window);
ffi::glfwDestroyWindow(self.window);
}
}
unsafe {
ffi_glfw::glfwTerminate();
ffi::glfwTerminate();
}
}
}