Skip to content

Commit

Permalink
misc: Squash numerous bugs in C and Rust ABIs
Browse files Browse the repository at this point in the history
  • Loading branch information
caseif committed Aug 29, 2024
1 parent da53d70 commit b39cf8d
Show file tree
Hide file tree
Showing 44 changed files with 421 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

use std::ffi;
use std::ffi::CStr;

use crate::lowlevel_cabi::*;
Expand All @@ -24,18 +24,21 @@ use crate::lowlevel_cabi;
pub use crate::lowlevel_cabi::StringArray;
pub use crate::lowlevel_cabi::StringArrayConst;

pub fn string_array_to_vec(sa: StringArray) -> Vec<String> {
pub trait FfiWrapper {
fn of(ptr: *mut ffi::c_void) -> Self;
}

pub unsafe fn string_array_to_vec(sa: StringArray) -> Vec<String> {
unsafe {
let count = string_array_get_count(sa);
let mut vec = Vec::<String>::new();
vec.reserve(count);
let mut vec = Vec::<String>::with_capacity(count);

for i in 0..count {
let s = string_array_get_element(sa, i);
vec[i] = CStr::from_ptr(s).to_str().unwrap().to_string();
}

return vec;
vec
}
}

Expand Down
28 changes: 14 additions & 14 deletions engine/auxiliary/lowlevel_rustabi/src/argus/lowlevel/math/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ use crate::lowlevel_cabi::*;

#[repr(C)]
#[ffi_repr(argus_vector_2d_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Vector2d {
pub x: f64,
pub y: f64,
}

#[repr(C)]
#[ffi_repr(argus_vector_3d_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Vector3d {
pub x: f64,
pub y: f64,
Expand All @@ -39,7 +39,7 @@ pub struct Vector3d {

#[repr(C)]
#[ffi_repr(argus_vector_4d_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Vector4d {
pub x: f64,
pub y: f64,
Expand All @@ -49,15 +49,15 @@ pub struct Vector4d {

#[repr(C)]
#[ffi_repr(argus_vector_2f_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Vector2f {
pub x: f32,
pub y: f32,
}

#[repr(C)]
#[ffi_repr(argus_vector_3f_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Vector3f {
pub x: f32,
pub y: f32,
Expand All @@ -66,7 +66,7 @@ pub struct Vector3f {

#[repr(C)]
#[ffi_repr(argus_vector_4f_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, PartialEq, PartialOrd)]
pub struct Vector4f {
pub x: f32,
pub y: f32,
Expand All @@ -76,15 +76,15 @@ pub struct Vector4f {

#[repr(C)]
#[ffi_repr(argus_vector_2i_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Vector2i {
pub x: f32,
pub y: f32,
pub x: i32,
pub y: i32,
}

#[repr(C)]
#[ffi_repr(argus_vector_3i_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Vector3i {
pub x: i32,
pub y: i32,
Expand All @@ -93,7 +93,7 @@ pub struct Vector3i {

#[repr(C)]
#[ffi_repr(argus_vector_4i_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Vector4i {
pub x: i32,
pub y: i32,
Expand All @@ -103,15 +103,15 @@ pub struct Vector4i {

#[repr(C)]
#[ffi_repr(argus_vector_2u_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Vector2u {
pub x: u32,
pub y: u32,
}

#[repr(C)]
#[ffi_repr(argus_vector_3u_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Vector3u {
pub x: u32,
pub y: u32,
Expand All @@ -120,7 +120,7 @@ pub struct Vector3u {

#[repr(C)]
#[ffi_repr(argus_vector_4u_t)]
#[derive(Clone, Copy, Debug, Default)]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Vector4u {
pub x: u32,
pub y: u32,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,47 +17,53 @@
*/

use std::ffi::c_char;

use std::ptr;
use num_enum::UnsafeFromPrimitive;

use lowlevel_rustabi::argus::lowlevel::Handle;
use lowlevel_rustabi::util::{cstr_to_string, str_to_cstring};

use crate::argus::render::{AttachedViewport2d, SceneType, Viewport};
use crate::render_cabi::*;

#[derive(Clone)]
pub struct AttachedViewport {
handle: argus_attached_viewport_t,
ffi_handle: argus_attached_viewport_t,
}

impl AttachedViewport {
pub(crate) fn of(handle: argus_attached_viewport_t) -> Self {
Self { handle }
Self { ffi_handle: handle }
}

pub fn as_2d(&self) -> AttachedViewport2d {
AttachedViewport2d::of(self.handle)
AttachedViewport2d::of(self.ffi_handle)
}

pub fn get_id(&self) -> u32 {
unsafe { argus_attached_viewport_get_id(self.ffi_handle) }
}

pub fn get_type(&self) -> SceneType {
unsafe {
SceneType::unchecked_transmute_from(argus_attached_viewport_get_type(self.handle))
SceneType::unchecked_transmute_from(argus_attached_viewport_get_type(self.ffi_handle))
}
}

pub fn get_viewport(&self) -> Viewport {
unsafe { argus_attached_viewport_get_viewport(self.handle).into() }
unsafe { argus_attached_viewport_get_viewport(self.ffi_handle).into() }
}

pub fn get_z_index(&self) -> u32 {
unsafe { argus_attached_viewport_get_z_index(self.handle) }
unsafe { argus_attached_viewport_get_z_index(self.ffi_handle) }
}

pub fn get_postprocessing_shaders(&self) -> Vec<String> {
unsafe {
let count = argus_attached_viewport_get_postprocessing_shaders_count(self.handle);
let mut shader_uids: Vec<*const c_char> = Vec::new();
let count = argus_attached_viewport_get_postprocessing_shaders_count(self.ffi_handle);
let mut shader_uids: Vec<*const c_char> = Vec::with_capacity(count);
shader_uids.resize(count, ptr::null());
argus_attached_viewport_get_postprocessing_shaders(
self.handle,
self.ffi_handle,
shader_uids.as_mut_ptr(),
count,
);
Expand All @@ -68,14 +74,14 @@ impl AttachedViewport {
pub fn add_postprocessing_shader(&mut self, shader_uid: &str) {
unsafe {
let uid_c = str_to_cstring(shader_uid);
argus_attached_viewport_add_postprocessing_shader(self.handle, uid_c.as_ptr())
argus_attached_viewport_add_postprocessing_shader(self.ffi_handle, uid_c.as_ptr())
}
}

pub fn remove_postprocessing_shader(&mut self, shader_uid: &str) {
unsafe {
let uid_c = str_to_cstring(shader_uid);
argus_attached_viewport_remove_postprocessing_shader(self.handle, uid_c.as_ptr())
argus_attached_viewport_remove_postprocessing_shader(self.ffi_handle, uid_c.as_ptr())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

use std::ptr;
use crate::argus::render::{AttachedViewport, AttachedViewport2d, Camera2d, Viewport};
use crate::render_cabi::*;
use lowlevel_rustabi::util::str_to_cstring;
Expand All @@ -27,6 +27,10 @@ pub struct Canvas {
}

impl Canvas {
pub fn of(handle: argus_canvas_t) -> Self {
Self { handle }
}

pub fn get_window(&self) -> Window {
unsafe { Window::of(argus_canvas_get_window(self.handle)) }
}
Expand All @@ -36,6 +40,7 @@ impl Canvas {
let count = argus_canvas_get_viewports_2d_count(self.handle);

let mut viewport_handles: Vec<argus_attached_viewport_2d_t> = Vec::with_capacity(count);
viewport_handles.resize(count, ptr::null_mut());
argus_canvas_get_viewports_2d(self.handle, viewport_handles.as_mut_ptr(), count);

viewport_handles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@
*/
use lowlevel_rustabi::util::cstr_to_str;
use std::ffi::c_char;

use std::ptr;
use lowlevel_rustabi::argus::lowlevel::FfiWrapper;
use crate::render_cabi::*;

pub struct Material {
handle: argus_material_t,
}

impl Material {
pub fn of(handle: argus_material_t) -> Self {
impl FfiWrapper for Material {
fn of(handle: argus_material_t) -> Self {
Self { handle }
}
}

impl Material {
pub fn get_texture_uid(&self) -> &str {
unsafe { cstr_to_str(argus_material_get_texture_uid(self.handle)) }
}
Expand All @@ -37,7 +40,8 @@ impl Material {
unsafe {
let count = argus_material_get_shader_uids_count(self.handle);

let mut cstrs: Vec<*const c_char> = Vec::new();
let mut cstrs: Vec<*const c_char> = Vec::with_capacity(count);
cstrs.resize(count, ptr::null());
argus_material_get_shader_uids(self.handle, cstrs.as_mut_ptr(), count);

cstrs.into_iter().map(|s| cstr_to_str(s)).collect()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::{ptr, slice};
use lowlevel_rustabi::util::{cstr_to_string, str_to_cstring};

use num_enum::{IntoPrimitive, UnsafeFromPrimitive};
use lowlevel_rustabi::argus::lowlevel::FfiWrapper;

pub struct Shader {
handle: argus_shader_t,
Expand All @@ -37,11 +38,13 @@ pub enum ShaderStage {
Fragment = ARGUS_SHADER_STAGE_FRAGMENT,
}

impl Shader {
pub(crate) fn of(handle: argus_shader_t) -> Self {
impl FfiWrapper for Shader {
fn of(handle: argus_shader_t) -> Self {
Self { handle }
}
}

impl Shader {
pub(crate) fn get_handle(&self) -> argus_shader_const_t {
self.handle
}
Expand All @@ -61,7 +64,7 @@ impl Shader {
}

pub fn copy(&self) -> Self {
unsafe { Self { handle: argus_shader_copy(self.handle) } }
unsafe { Self::of(argus_shader_copy(self.handle)) }
}

pub fn destroy(&mut self) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
use std::ptr;
use std::ptr::null_mut;

use lowlevel_rustabi::argus::lowlevel::FfiWrapper;
use crate::argus::render::{Shader, ShaderReflectionInfo};

use crate::render_cabi::*;
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn compile_glsl_to_spirv(
(
compiled_shaders
.into_iter()
.map(|handle| Shader::of(handle))
.map(Shader::of)
.collect(),
ShaderReflectionInfo::of(refl_info),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@
*/

use std::slice;

use lowlevel_rustabi::argus::lowlevel::FfiWrapper;
use crate::render_cabi::*;

pub struct TextureData {
handle: argus_texture_data_t,
}

impl FfiWrapper for TextureData {
fn of(handle: argus_texture_data_t) -> Self {
Self { handle }
}
}

impl TextureData {
pub fn get_width(&self) -> u32 {
unsafe { argus_texture_data_get_width(self.handle) }
Expand Down
Loading

0 comments on commit b39cf8d

Please sign in to comment.