define_api_id!(0xc711_14ac_a9a4_ae3b, "render-v0");
use crate::FFIResult;
use crate::PodBool;
use bytemuck::CheckedBitPattern;
use bytemuck::NoUninit;
use bytemuck::Pod;
use bytemuck::Zeroable;
pub type TextureHandle = u64;
#[derive(Clone, Copy, Debug, Default, Pod, Zeroable)]
#[repr(C)]
pub struct Rectangle {
pub min_x: f32,
pub min_y: f32,
pub max_x: f32,
pub max_y: f32,
}
impl Rectangle {
#[inline(always)]
pub fn width(&self) -> f32 {
self.max_x - self.min_x
}
#[inline(always)]
pub fn height(&self) -> f32 {
self.max_y - self.min_y
}
pub fn from_resolution(resolution: [u32; 2]) -> Self {
Self {
min_x: 0.0,
min_y: 0.0,
max_x: resolution[0] as f32,
max_y: resolution[1] as f32,
}
}
}
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
#[repr(C)]
pub struct BoundingBox {
pub min: [f32; 3],
pub max: [f32; 3],
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, NoUninit, CheckedBitPattern)]
#[repr(u32)]
#[non_exhaustive]
#[allow(non_camel_case_types)]
pub enum TextureFormat {
R8G8B8A8_SRGB = 1,
R8G8B8A8_UNORM = 2,
}
impl TextureFormat {
#[inline]
pub fn bytes_per_pixel(&self) -> u64 {
match self {
TextureFormat::R8G8B8A8_SRGB | TextureFormat::R8G8B8A8_UNORM => 4,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, NoUninit, CheckedBitPattern)]
#[repr(u32)]
#[non_exhaustive]
pub enum TextureType {
D2 = 1,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, NoUninit, CheckedBitPattern)]
#[repr(C)]
pub struct TextureDescription {
pub texture_type: TextureType,
pub format: TextureFormat,
pub width: u64,
pub height: u64,
pub depth: u64,
pub mipmaps: u32,
pub array_len: u32,
}
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
#[repr(C)]
pub struct BoneTransform {
pub pos: [f32; 3],
pub _padding: f32,
pub rot: [f32; 4],
}
impl PartialEq for BoneTransform {
fn eq(&self, other: &Self) -> bool {
self.pos == other.pos && self.rot == other.rot
}
}
impl BoneTransform {
pub fn zero() -> Self {
Self {
pos: [0.0; 3],
_padding: 0.0,
rot: [0.0; 4],
}
}
}
#[derive(Debug, Copy, Clone, Pod, Zeroable)]
#[repr(C)]
pub struct SdfInstanceData {
pub world_from_instance: [f32; 16],
pub bounding_box_index: u32,
pub dynamic_data_offset: u32,
pub dynamic_data_length: u32,
pub detail_bias: f32,
pub opacity: f32,
pub lighting: f32,
pub depth_test: PodBool,
pub depth_write: PodBool,
pub _pad: [u8; 2],
}
#[derive(Debug, Copy, Clone, Pod, Zeroable)]
#[repr(C)]
pub struct SkinnedSdfInstanceData {
pub detail_bias: f32,
pub opacity: f32,
pub lighting: f32,
}
pub type SdfHandle = u64;
#[ark_api_macros::ark_bindgen(imports = "ark-render-v0")]
mod render {
use super::*;
extern "C" {
pub fn create_texture(
name: &str,
description: &TextureDescription,
data: &[u8],
) -> FFIResult<TextureHandle>;
pub fn update_texture(
handle: TextureHandle,
pos_x: u32,
pos_y: u32,
width: u32,
height: u32,
data: &[u8],
);
#[deprecated_infallible]
pub fn destroy_texture(handle: TextureHandle);
#[deprecated_infallible]
pub fn draw_triangles_2d(
clip_rect: &Rectangle,
indices: &[u32], positions: &[f32], colors: &[u8], );
#[deprecated_infallible]
pub fn draw_textured_triangles_2d(
clip_rect: &Rectangle,
handle: TextureHandle,
indices: &[u32], positions: &[f32], colors: &[u8], uvs: &[f32], );
pub fn create_sdf_model(
opcodes: &[u32],
constants: &[f32],
bounding_box: &BoundingBox,
) -> FFIResult<u64>;
#[deprecated_infallible]
pub fn destroy_sdf_model(sdf: u64);
#[deprecated_infallible]
pub fn draw_sdf_model(
sdf: SdfHandle,
instances: &[SdfInstanceData],
constants: &[f32],
bounding_boxes: &[BoundingBox],
);
}
}
pub use render::*;