use yog_abi::YogStr;
use crate::core::DataType;
use crate::GfxContext;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Buffer {
pub handle: u32,
}
impl Buffer {
pub fn upload_bytes(&self, ctx: &GfxContext, data: &[u8], dynamic: bool) {
unsafe { (ctx.api().buf_data)(self.handle, data.as_ptr(), data.len() as u32, dynamic) }
}
pub unsafe fn upload<T: Sized>(&self, ctx: &GfxContext, data: &[T], dynamic: bool) {
let bytes = std::slice::from_raw_parts(
data.as_ptr() as *const u8,
std::mem::size_of_val(data),
);
self.upload_bytes(ctx, bytes, dynamic);
}
pub fn subdata_bytes(&self, ctx: &GfxContext, offset: u32, data: &[u8]) {
unsafe { (ctx.api().buf_subdata)(self.handle, offset, data.as_ptr(), data.len() as u32) }
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct VertexArray {
pub handle: u32,
}
impl VertexArray {
pub fn attrib(
&self, ctx: &GfxContext, vbo: &Buffer,
index: u32, components: u8, dtype: DataType,
normalized: bool, stride: u32, offset: u32,
) {
unsafe {
(ctx.api().vao_attrib)(
self.handle, vbo.handle, index, components,
dtype as u8, normalized, stride, offset,
)
}
}
pub fn set_ebo(&self, ctx: &GfxContext, ebo: &Buffer) {
unsafe { (ctx.api().vao_set_ebo)(self.handle, ebo.handle) }
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct ShaderProgram {
pub handle: u32,
}
impl ShaderProgram {
pub fn uniform_1i(&self, ctx: &GfxContext, name: &str, v: i32) {
unsafe { (ctx.api().prog_uniform_1i)(self.handle, YogStr::from_str(name), v) }
}
pub fn uniform_1f(&self, ctx: &GfxContext, name: &str, v: f32) {
unsafe { (ctx.api().prog_uniform_1f)(self.handle, YogStr::from_str(name), v) }
}
pub fn uniform_2f(&self, ctx: &GfxContext, name: &str, x: f32, y: f32) {
unsafe { (ctx.api().prog_uniform_2f)(self.handle, YogStr::from_str(name), x, y) }
}
pub fn uniform_3f(&self, ctx: &GfxContext, name: &str, x: f32, y: f32, z: f32) {
unsafe { (ctx.api().prog_uniform_3f)(self.handle, YogStr::from_str(name), x, y, z) }
}
pub fn uniform_4f(&self, ctx: &GfxContext, name: &str, x: f32, y: f32, z: f32, w: f32) {
unsafe { (ctx.api().prog_uniform_4f)(self.handle, YogStr::from_str(name), x, y, z, w) }
}
pub fn uniform_mat4(&self, ctx: &GfxContext, name: &str, col_major: &[f32; 16]) {
unsafe { (ctx.api().prog_uniform_mat4)(self.handle, YogStr::from_str(name), col_major.as_ptr()) }
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Texture {
pub handle: u32,
}