Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be more generic, for Rc and Arc to replace &. #62

Merged
merged 23 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "vendor/VulkanMemoryAllocator"]
path = vendor/VulkanMemoryAllocator
url = https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator.git
url = https://github.com/dust-engine/VulkanMemoryAllocator.git
[submodule "vendor/Vulkan-Headers"]
path = vendor/Vulkan-Headers
url = https://github.com/KhronosGroup/Vulkan-Headers
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ travis-ci = { repository = "gwihlidal/vk-mem-rs" }
maintenance = { status = "actively-developed" }

[dependencies]
ash = ">= 0.35"
ash = { version = "0.37", default-features = false }
bitflags = "1.2.1"

[build-dependencies]
cc = "1.0.50"
cc = "1.0"

[build-dependencies.bindgen]
version = "0.59.1"
version = "0.60"
optional = true

[profile.release]
Expand All @@ -45,7 +45,8 @@ opt-level = 3
codegen-units = 1

[features]
default = []
default = ["loaded"]
generate_bindings=["bindgen"]
link_vulkan=[]
linked=["ash/linked"]
loaded=["ash/loaded"]
recording=[]
54 changes: 7 additions & 47 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
let mut build = cc::Build::new();

build.include("vendor/VulkanMemoryAllocator/include");
build.include("vendor/Vulkan-Headers/include/vulkan");
build.include("vendor/Vulkan-Headers/include");
build.include("wrapper");

// Disable VMA_ASSERT when rust assertions are disabled
Expand Down Expand Up @@ -102,55 +102,11 @@ fn main() {
.cpp(true);
}

build.compile("vma_cpp");
build.compile("vma");

link_vulkan();
generate_bindings("gen/bindings.rs");
generate_bindings("src/ffi.rs");
}

#[cfg(feature = "link_vulkan")]
fn link_vulkan() {
use std::path::PathBuf;
let target = env::var("TARGET").unwrap();
if target.contains("windows") {
if let Ok(vulkan_sdk) = env::var("VULKAN_SDK") {
let mut vulkan_sdk_path = PathBuf::from(vulkan_sdk);

if target.contains("x86_64") {
vulkan_sdk_path.push("Lib");
} else {
vulkan_sdk_path.push("Lib32");
}

println!(
"cargo:rustc-link-search=native={}",
vulkan_sdk_path.to_str().unwrap()
);
}

println!("cargo:rustc-link-lib=dylib=vulkan-1");
} else {
if target.contains("apple") {
if let Ok(vulkan_sdk) = env::var("VULKAN_SDK") {
let mut vulkan_sdk_path = PathBuf::from(vulkan_sdk);
vulkan_sdk_path.push("macOS/lib");
println!(
"cargo:rustc-link-search=native={}",
vulkan_sdk_path.to_str().unwrap()
);
} else {
let lib_path = "wrapper/macOS/lib";
println!("cargo:rustc-link-search=native={}", lib_path);
}

println!("cargo:rustc-link-lib=dylib=vulkan");
}
}
}

#[cfg(not(feature = "link_vulkan"))]
fn link_vulkan() {}

#[cfg(feature = "generate_bindings")]
fn generate_bindings(output_file: &str) {
let bindings = bindgen::Builder::default()
Expand All @@ -166,9 +122,13 @@ fn generate_bindings(output_file: &str) {
.parse_callbacks(Box::new(FixAshTypes))
.blocklist_type("Vk.*")
.blocklist_type("PFN_vk.*")
.raw_line("#![allow(non_camel_case_types)]")
.raw_line("#![allow(non_snake_case)]")
.raw_line("#![allow(dead_code)]")
.raw_line("use ash::vk::*;")
.trust_clang_mangling(false)
.layout_tests(false)
.rustified_enum("Vma.*")
.generate()
.expect("Unable to generate bindings!");

Expand Down
1,871 changes: 0 additions & 1,871 deletions gen/bindings.rs

This file was deleted.

443 changes: 290 additions & 153 deletions src/definitions.rs

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions src/defragmentation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use crate::ffi;
use crate::Allocator;
use ash::prelude::VkResult;
use ash::vk;

pub use ffi::VmaDefragmentationMove as DefragmentationMove;
pub use ffi::VmaDefragmentationStats as DefragmentationStats;
pub struct DefragmentationContext<'a> {
allocator: &'a Allocator,
raw: ffi::VmaDefragmentationContext,
}

impl<'a> Drop for DefragmentationContext<'a> {
fn drop(&mut self) {
unsafe {
ffi::vmaEndDefragmentation(self.allocator.internal, self.raw, std::ptr::null_mut());
}
}
}

impl<'a> DefragmentationContext<'a> {
/// Ends defragmentation process.
pub fn end(self) -> DefragmentationStats {
let mut stats = DefragmentationStats {
bytesMoved: 0,
bytesFreed: 0,
allocationsMoved: 0,
deviceMemoryBlocksFreed: 0,
};
unsafe {
ffi::vmaEndDefragmentation(self.allocator.internal, self.raw, &mut stats);
}
std::mem::forget(self);
stats
}

/// Returns `false` if no more moves are possible or `true` if more defragmentations are possible.
pub fn begin_pass(&self, mover: impl FnOnce(&mut [DefragmentationMove]) -> ()) -> bool {
let mut pass_info = ffi::VmaDefragmentationPassMoveInfo {
moveCount: 0,
pMoves: std::ptr::null_mut(),
};
let result = unsafe {
ffi::vmaBeginDefragmentationPass(self.allocator.internal, self.raw, &mut pass_info)
};
if result == vk::Result::SUCCESS {
return false;
}
debug_assert_eq!(result, vk::Result::INCOMPLETE);
let moves = unsafe {
std::slice::from_raw_parts_mut(pass_info.pMoves, pass_info.moveCount as usize)
};
mover(moves);

let result = unsafe {
ffi::vmaEndDefragmentationPass(self.allocator.internal, self.raw, &mut pass_info)
};

return result == vk::Result::INCOMPLETE;
}
}

impl Allocator {
/// Begins defragmentation process.
///
/// ## Returns
/// `VK_SUCCESS` if defragmentation can begin.
/// `VK_ERROR_FEATURE_NOT_PRESENT` if defragmentation is not supported.
pub unsafe fn begin_defragmentation(
&self,
info: &ffi::VmaDefragmentationInfo,
) -> VkResult<DefragmentationContext> {
let mut context: ffi::VmaDefragmentationContext = std::ptr::null_mut();

ffi::vmaBeginDefragmentation(self.internal, info, &mut context).result()?;

Ok(DefragmentationContext {
allocator: self,
raw: context,
})
}
}
Loading