Attempt to enforce repr(C) with safer_ffi

This commit is contained in:
hodasemi 2023-01-27 12:07:41 +01:00
parent fe75b39d01
commit 0b44972394
11 changed files with 53 additions and 43 deletions

View file

@ -9,6 +9,6 @@ image = "0.24.5"
vulkan-sys = { path = "../vulkan-sys" } vulkan-sys = { path = "../vulkan-sys" }
vma-rs = { path = "../vma-rs" } vma-rs = { path = "../vma-rs" }
anyhow = { version = "1.0.68", features = ["backtrace"] } anyhow = { version = "1.0.68", features = ["backtrace"] }
cgmath = "0.18.0"
assetpath = { path = "../assetpath" } assetpath = { path = "../assetpath" }
safer-ffi = "0.0.10" cgmath = { git = "https://gavania.de/hodasemi/cgmath.git" }
safer-ffi = { version = "0.0.10", features = ["proc_macros"] }

View file

@ -56,7 +56,7 @@ impl AccelerationStructureBuilder {
self self
} }
pub fn add_vertices<T: Send + Sync + 'static>( pub fn add_vertices<T: ReprC + Send + Sync + 'static>(
mut self, mut self,
vertex_buffer: &Arc<Buffer<T>>, vertex_buffer: &Arc<Buffer<T>>,
transform: Option<Arc<Buffer<Matrix4<f32>>>>, transform: Option<Arc<Buffer<Matrix4<f32>>>>,

View file

@ -6,7 +6,7 @@ use std;
use std::mem; use std::mem;
use std::sync::Arc; use std::sync::Arc;
pub struct BufferBuilder<'a, T> { pub struct BufferBuilder<'a, T: ReprC> {
flags: VkBufferCreateFlagBits, flags: VkBufferCreateFlagBits,
usage: VkBufferUsageFlagBits, usage: VkBufferUsageFlagBits,
memory_usage: Option<MemoryUsage>, memory_usage: Option<MemoryUsage>,
@ -17,7 +17,7 @@ pub struct BufferBuilder<'a, T> {
alignment: Option<VkDeviceSize>, alignment: Option<VkDeviceSize>,
} }
impl<'a, T> BufferBuilder<'a, T> { impl<'a, T: ReprC> BufferBuilder<'a, T> {
pub fn set_memory_usage(mut self, usage: MemoryUsage) -> Self { pub fn set_memory_usage(mut self, usage: MemoryUsage) -> Self {
self.memory_usage = Some(usage); self.memory_usage = Some(usage);
@ -61,7 +61,7 @@ impl<'a, T> BufferBuilder<'a, T> {
} }
} }
impl<'a, T: Clone + Send + Sync + 'static> BufferBuilder<'a, T> { impl<'a, T: ReprC + Clone + Send + Sync + 'static> BufferBuilder<'a, T> {
pub fn build(self, device: Arc<Device>) -> Result<Arc<Buffer<T>>> { pub fn build(self, device: Arc<Device>) -> Result<Arc<Buffer<T>>> {
let size = match self.data { let size = match self.data {
Some(data) => data.len() as VkDeviceSize, Some(data) => data.len() as VkDeviceSize,
@ -122,7 +122,7 @@ impl<'a, T: Clone + Send + Sync + 'static> BufferBuilder<'a, T> {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Buffer<T> { pub struct Buffer<T: ReprC> {
device: Arc<Device>, device: Arc<Device>,
buffer: VkBuffer, buffer: VkBuffer,
@ -134,7 +134,7 @@ pub struct Buffer<T> {
size: VkDeviceSize, size: VkDeviceSize,
} }
impl<T: Clone + Send + Sync + 'static> Buffer<T> { impl<T: ReprC + Clone + Send + Sync + 'static> Buffer<T> {
pub fn fill(&self, data: &[T]) -> Result<()> { pub fn fill(&self, data: &[T]) -> Result<()> {
let mut buffer_map = self.map(data.len() as VkDeviceSize)?; let mut buffer_map = self.map(data.len() as VkDeviceSize)?;
@ -190,7 +190,7 @@ impl<T: Clone + Send + Sync + 'static> Buffer<T> {
} }
} }
impl<T> Buffer<T> { impl<T: ReprC> Buffer<T> {
pub fn builder<'a>() -> BufferBuilder<'a, T> { pub fn builder<'a>() -> BufferBuilder<'a, T> {
BufferBuilder { BufferBuilder {
flags: 0u32.into(), flags: 0u32.into(),
@ -217,39 +217,39 @@ impl<T> Buffer<T> {
} }
} }
impl<T> VulkanDevice for Buffer<T> { impl<T: ReprC> VulkanDevice for Buffer<T> {
fn device(&self) -> &Arc<Device> { fn device(&self) -> &Arc<Device> {
&self.device &self.device
} }
} }
impl_vk_handle_t!(Buffer, VkBuffer, buffer); impl_vk_handle_t!(Buffer[ReprC], VkBuffer, buffer);
impl<T> VkHandle<VkDeviceMemory> for Buffer<T> { impl<T: ReprC> VkHandle<VkDeviceMemory> for Buffer<T> {
fn vk_handle(&self) -> VkDeviceMemory { fn vk_handle(&self) -> VkDeviceMemory {
self.memory.vk_handle() self.memory.vk_handle()
} }
} }
impl<'a, T> VkHandle<VkDeviceMemory> for &'a Buffer<T> { impl<'a, T: ReprC> VkHandle<VkDeviceMemory> for &'a Buffer<T> {
fn vk_handle(&self) -> VkDeviceMemory { fn vk_handle(&self) -> VkDeviceMemory {
self.memory.vk_handle() self.memory.vk_handle()
} }
} }
impl<T> VkHandle<VkDeviceMemory> for Arc<Buffer<T>> { impl<T: ReprC> VkHandle<VkDeviceMemory> for Arc<Buffer<T>> {
fn vk_handle(&self) -> VkDeviceMemory { fn vk_handle(&self) -> VkDeviceMemory {
self.memory.vk_handle() self.memory.vk_handle()
} }
} }
impl<'a, T> VkHandle<VkDeviceMemory> for &'a Arc<Buffer<T>> { impl<'a, T: ReprC> VkHandle<VkDeviceMemory> for &'a Arc<Buffer<T>> {
fn vk_handle(&self) -> VkDeviceMemory { fn vk_handle(&self) -> VkDeviceMemory {
self.memory.vk_handle() self.memory.vk_handle()
} }
} }
impl<T> Drop for Buffer<T> { impl<T: ReprC> Drop for Buffer<T> {
fn drop(&mut self) { fn drop(&mut self) {
self.device.destroy_buffer(self.buffer); self.device.destroy_buffer(self.buffer);
} }
@ -257,7 +257,7 @@ impl<T> Drop for Buffer<T> {
// use crate::{ffi::*, handle_ffi_result}; // use crate::{ffi::*, handle_ffi_result};
impl<T> FFIBufferTrait for Buffer<T> { impl<T: ReprC> FFIBufferTrait for Buffer<T> {
fn byte_size(&self) -> VkDeviceSize { fn byte_size(&self) -> VkDeviceSize {
self.byte_size() self.byte_size()
} }

View file

@ -226,7 +226,7 @@ impl<'a> CommandBufferRecorder<'a> {
); );
} }
pub fn buffer_barrier<T: Send + Sync + 'static>( pub fn buffer_barrier<T: ReprC + Send + Sync + 'static>(
&mut self, &mut self,
buffer: &Arc<Buffer<T>>, buffer: &Arc<Buffer<T>>,
src_access_mask: impl Into<VkAccessFlagBits>, src_access_mask: impl Into<VkAccessFlagBits>,
@ -446,7 +446,10 @@ impl<'a> CommandBufferRecorder<'a> {
); );
} }
pub fn bind_vertex_buffer<T: Send + Sync + 'static>(&mut self, buffer: &Arc<Buffer<T>>) { pub fn bind_vertex_buffer<T: ReprC + Send + Sync + 'static>(
&mut self,
buffer: &Arc<Buffer<T>>,
) {
self.calls.fetch_add(1, SeqCst); self.calls.fetch_add(1, SeqCst);
self.handles_lock.push(buffer.clone()); self.handles_lock.push(buffer.clone());
@ -455,7 +458,7 @@ impl<'a> CommandBufferRecorder<'a> {
.cmd_bind_vertex_buffers(self.buffer, 0, &[buffer.vk_handle()], &[0]); .cmd_bind_vertex_buffers(self.buffer, 0, &[buffer.vk_handle()], &[0]);
} }
pub fn bind_vertex_buffers_minimal<T: Send + Sync + 'static>( pub fn bind_vertex_buffers_minimal<T: ReprC + Send + Sync + 'static>(
&mut self, &mut self,
buffers: &[&Arc<Buffer<T>>], buffers: &[&Arc<Buffer<T>>],
) { ) {
@ -480,7 +483,7 @@ impl<'a> CommandBufferRecorder<'a> {
); );
} }
pub fn bind_index_buffer<T: Send + Sync + 'static>( pub fn bind_index_buffer<T: ReprC + Send + Sync + 'static>(
&mut self, &mut self,
buffer: &Arc<Buffer<T>>, buffer: &Arc<Buffer<T>>,
offset: VkDeviceSize, offset: VkDeviceSize,
@ -673,7 +676,7 @@ impl<'a> CommandBufferRecorder<'a> {
unimplemented!(); unimplemented!();
} }
pub fn copy_buffer<T: Send + Sync + 'static, U: Send + Sync + 'static>( pub fn copy_buffer<T: ReprC + Send + Sync + 'static, U: ReprC + Send + Sync + 'static>(
&mut self, &mut self,
src_buffer: &Arc<Buffer<T>>, src_buffer: &Arc<Buffer<T>>,
dst_buffer: &Arc<Buffer<U>>, dst_buffer: &Arc<Buffer<U>>,
@ -780,7 +783,7 @@ impl<'a> CommandBufferRecorder<'a> {
); );
} }
pub fn copy_buffer_to_image<T: Send + Sync + 'static>( pub fn copy_buffer_to_image<T: ReprC + Send + Sync + 'static>(
&mut self, &mut self,
src_buffer: &Arc<Buffer<T>>, src_buffer: &Arc<Buffer<T>>,
dst_image: &Arc<Image>, dst_image: &Arc<Image>,
@ -801,7 +804,7 @@ impl<'a> CommandBufferRecorder<'a> {
); );
} }
pub fn copy_image_to_buffer<T: Send + Sync + 'static>( pub fn copy_image_to_buffer<T: ReprC + Send + Sync + 'static>(
&mut self, &mut self,
src_image: &Arc<Image>, src_image: &Arc<Image>,
image_layout: VkImageLayout, image_layout: VkImageLayout,
@ -920,7 +923,7 @@ impl<'a> CommandBufferRecorder<'a> {
pub fn build_acceleration_structure_indirect( pub fn build_acceleration_structure_indirect(
&mut self, &mut self,
infos: &[VkAccelerationStructureBuildGeometryInfoKHR], infos: &[VkAccelerationStructureBuildGeometryInfoKHR],
indirect_buffers: &[Arc<Buffer<impl Send + Sync + 'static>>], indirect_buffers: &[Arc<Buffer<impl ReprC + Send + Sync + 'static>>],
indirect_strides: &[u32], indirect_strides: &[u32],
max_primitive_counts: &[&u32], max_primitive_counts: &[&u32],
) { ) {
@ -995,7 +998,7 @@ impl<'a> CommandBufferRecorder<'a> {
pub fn trace_rays_indirect( pub fn trace_rays_indirect(
&mut self, &mut self,
sbt: ShaderBindingTable, sbt: ShaderBindingTable,
buffer: Arc<Buffer<impl Send + Sync + 'static>>, buffer: Arc<Buffer<impl ReprC + Send + Sync + 'static>>,
) { ) {
self.handles_lock.push(buffer.clone()); self.handles_lock.push(buffer.clone());

View file

@ -28,7 +28,7 @@ enum InnerWrite {
} }
impl DescriptorWrite { impl DescriptorWrite {
pub fn uniform_buffers<T: Send + Sync + 'static>( pub fn uniform_buffers<T: ReprC + Send + Sync + 'static>(
binding: u32, binding: u32,
buffers: &[&Arc<Buffer<T>>], buffers: &[&Arc<Buffer<T>>],
) -> Self { ) -> Self {
@ -52,7 +52,7 @@ impl DescriptorWrite {
} }
} }
pub fn storage_buffers<T: Send + Sync + 'static>( pub fn storage_buffers<T: ReprC + Send + Sync + 'static>(
binding: u32, binding: u32,
buffers: &[&Arc<Buffer<T>>], buffers: &[&Arc<Buffer<T>>],
) -> Self { ) -> Self {

View file

@ -928,7 +928,7 @@ fn into_layout(image: &Arc<Image>, layout: VkImageLayout) -> Result<()> {
Ok(()) Ok(())
} }
fn copy_buffer_to_image<T: Send + Sync + 'static>( fn copy_buffer_to_image<T: ReprC + Send + Sync + 'static>(
device: &Arc<Device>, device: &Arc<Device>,
queue: &Arc<Mutex<Queue>>, queue: &Arc<Mutex<Queue>>,
buffer: &Arc<Buffer<T>>, buffer: &Arc<Buffer<T>>,

View file

@ -27,26 +27,26 @@ macro_rules! impl_vk_handle {
} }
macro_rules! impl_vk_handle_t { macro_rules! impl_vk_handle_t {
($struct_name:ident, $target_name:ident, $value:ident) => { ($struct_name:ident $([$($t:ident $(,)? )* ])?, $target_name:ident, $value:ident) => {
impl<T> VkHandle<$target_name> for $struct_name<T> { impl<T $(: $($t,)* )?> VkHandle<$target_name> for $struct_name<T> {
fn vk_handle(&self) -> $target_name { fn vk_handle(&self) -> $target_name {
self.$value self.$value
} }
} }
impl<'a, T> VkHandle<$target_name> for &'a $struct_name<T> { impl<'a, T $(: $($t,)* )?> VkHandle<$target_name> for &'a $struct_name<T> {
fn vk_handle(&self) -> $target_name { fn vk_handle(&self) -> $target_name {
self.$value self.$value
} }
} }
impl<T> VkHandle<$target_name> for Arc<$struct_name<T>> { impl<T $(: $($t,)* )?> VkHandle<$target_name> for Arc<$struct_name<T>> {
fn vk_handle(&self) -> $target_name { fn vk_handle(&self) -> $target_name {
self.$value self.$value
} }
} }
impl<'a, T> VkHandle<$target_name> for &'a Arc<$struct_name<T>> { impl<'a, T $(: $($t,)* )?> VkHandle<$target_name> for &'a Arc<$struct_name<T>> {
fn vk_handle(&self) -> $target_name { fn vk_handle(&self) -> $target_name {
self.$value self.$value
} }

View file

@ -36,7 +36,7 @@ impl Into<VmaMemoryUsage> for MemoryUsage {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Memory<T> { pub struct Memory<T: ReprC> {
device: Arc<Device>, device: Arc<Device>,
allocation: Allocation, allocation: Allocation,
@ -44,7 +44,7 @@ pub struct Memory<T> {
data_type: PhantomData<T>, data_type: PhantomData<T>,
} }
impl<T> Memory<T> { impl<T: ReprC> Memory<T> {
pub(crate) fn forced_requirements( pub(crate) fn forced_requirements(
device: &Arc<Device>, device: &Arc<Device>,
memory_requirements: VkMemoryRequirements, memory_requirements: VkMemoryRequirements,
@ -85,7 +85,7 @@ impl<T> Memory<T> {
} }
} }
trait MemoryBinder<T, K> { trait MemoryBinder<T, K: ReprC> {
fn create_and_bind( fn create_and_bind(
device: &Arc<Device>, device: &Arc<Device>,
memory_requirements: VkMemoryRequirements, memory_requirements: VkMemoryRequirements,
@ -94,7 +94,7 @@ trait MemoryBinder<T, K> {
) -> Result<Arc<Memory<K>>>; ) -> Result<Arc<Memory<K>>>;
} }
impl<K> MemoryBinder<(), K> for Memory<K> { impl<K: ReprC> MemoryBinder<(), K> for Memory<K> {
fn create_and_bind( fn create_and_bind(
device: &Arc<Device>, device: &Arc<Device>,
memory_requirements: VkMemoryRequirements, memory_requirements: VkMemoryRequirements,
@ -118,7 +118,7 @@ impl<K> MemoryBinder<(), K> for Memory<K> {
} }
} }
impl<K> MemoryBinder<VkImage, K> for Memory<K> { impl<K: ReprC> MemoryBinder<VkImage, K> for Memory<K> {
fn create_and_bind( fn create_and_bind(
device: &Arc<Device>, device: &Arc<Device>,
memory_requirements: VkMemoryRequirements, memory_requirements: VkMemoryRequirements,
@ -142,7 +142,7 @@ impl<K> MemoryBinder<VkImage, K> for Memory<K> {
} }
} }
impl<K> MemoryBinder<VkBuffer, K> for Memory<K> { impl<K: ReprC> MemoryBinder<VkBuffer, K> for Memory<K> {
fn create_and_bind( fn create_and_bind(
device: &Arc<Device>, device: &Arc<Device>,
memory_requirements: VkMemoryRequirements, memory_requirements: VkMemoryRequirements,
@ -166,13 +166,13 @@ impl<K> MemoryBinder<VkBuffer, K> for Memory<K> {
} }
} }
impl<T> VulkanDevice for Memory<T> { impl<T: ReprC> VulkanDevice for Memory<T> {
fn device(&self) -> &Arc<Device> { fn device(&self) -> &Arc<Device> {
&self.device &self.device
} }
} }
impl<T: Clone> Memory<T> { impl<T: ReprC + Clone> Memory<T> {
pub fn map(&self, length: VkDeviceSize) -> Result<VkMappedMemory<'_, T>> { pub fn map(&self, length: VkDeviceSize) -> Result<VkMappedMemory<'_, T>> {
self.allocation.map(length) self.allocation.map(length)
} }

View file

@ -8,4 +8,5 @@ edition = "2021"
library_loader = { path = "../library_loader" } library_loader = { path = "../library_loader" }
paste = "1.0.11" paste = "1.0.11"
shared_library = "0.1.9" shared_library = "0.1.9"
anyhow = { version = "1.0.68", features = ["backtrace"] } anyhow = { version = "1.0.68", features = ["backtrace"] }
safer-ffi = { version = "0.0.10", features = ["proc_macros"] }

View file

@ -1,6 +1,8 @@
use crate::prelude::*; use crate::prelude::*;
use safer_ffi::prelude::*;
use std::fmt; use std::fmt;
#[derive_ReprC]
#[repr(C)] #[repr(C)]
#[derive(Copy, Clone, PartialEq)] #[derive(Copy, Clone, PartialEq)]
pub struct VkAccelerationStructureInstanceKHR { pub struct VkAccelerationStructureInstanceKHR {

View file

@ -1,3 +1,7 @@
use safer_ffi::prelude::*;
#[derive_ReprC]
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
pub struct VkTransformMatrixKHR { pub struct VkTransformMatrixKHR {