yog_gfx/gl/mod.rs
1//! Low-level GPU resource wrappers: [`Buffer`], [`VertexArray`], [`ShaderProgram`], [`Texture`].
2//!
3//! All types are plain handles (`u32`). Create them via [`GfxContext`] methods;
4//! delete them explicitly with `ctx.delete_*` when no longer needed.
5
6use yog_abi::YogStr;
7
8use crate::core::DataType;
9use crate::GfxContext;
10
11// ── Buffer ────────────────────────────────────────────────────────────────────
12
13/// A GPU buffer (VBO or EBO).
14///
15/// Created by [`GfxContext::create_buffer`]. Must be deleted with
16/// [`GfxContext::delete_buffer`] when done.
17#[derive(Copy, Clone, Debug, PartialEq, Eq)]
18pub struct Buffer {
19 pub handle: u32,
20}
21
22impl Buffer {
23 /// Upload bytes to this buffer.
24 /// `dynamic`: hint that the buffer will be updated frequently.
25 pub fn upload_bytes(&self, ctx: &GfxContext, data: &[u8], dynamic: bool) {
26 unsafe { (ctx.api().buf_data)(self.handle, data.as_ptr(), data.len() as u32, dynamic) }
27 }
28
29 /// Upload typed data to this buffer. `T` must be a plain-old-data type
30 /// (the bytes are used as-is, in the layout `T` has in memory).
31 ///
32 /// # Safety
33 /// The byte representation of `T` must be well-defined (no padding traps,
34 /// no floats encoding NaN, etc.). Use `#[repr(C)]` structs or primitive types.
35 pub unsafe fn upload<T: Sized>(&self, ctx: &GfxContext, data: &[T], dynamic: bool) {
36 let bytes = std::slice::from_raw_parts(
37 data.as_ptr() as *const u8,
38 std::mem::size_of_val(data),
39 );
40 self.upload_bytes(ctx, bytes, dynamic);
41 }
42
43 /// Overwrite a sub-range of this buffer starting at `offset` bytes.
44 pub fn subdata_bytes(&self, ctx: &GfxContext, offset: u32, data: &[u8]) {
45 unsafe { (ctx.api().buf_subdata)(self.handle, offset, data.as_ptr(), data.len() as u32) }
46 }
47}
48
49// ── VertexArray ───────────────────────────────────────────────────────────────
50
51/// A vertex array object (VAO).
52///
53/// Created by [`GfxContext::create_vao`]. Must be deleted with
54/// [`GfxContext::delete_vao`].
55#[derive(Copy, Clone, Debug, PartialEq, Eq)]
56pub struct VertexArray {
57 pub handle: u32,
58}
59
60impl VertexArray {
61 /// Declare one vertex attribute in this VAO, sourced from `vbo`.
62 ///
63 /// - `index`: attribute location in the shader (`layout(location = N)`).
64 /// - `components`: number of components (1–4).
65 /// - `dtype`: component data type.
66 /// - `normalized`: if `true` and `dtype` is `U8`, values are mapped 0→1.
67 /// - `stride`: byte distance between consecutive vertices in `vbo`.
68 /// - `offset`: byte offset of this attribute within each vertex.
69 pub fn attrib(
70 &self, ctx: &GfxContext, vbo: &Buffer,
71 index: u32, components: u8, dtype: DataType,
72 normalized: bool, stride: u32, offset: u32,
73 ) {
74 unsafe {
75 (ctx.api().vao_attrib)(
76 self.handle, vbo.handle, index, components,
77 dtype as u8, normalized, stride, offset,
78 )
79 }
80 }
81
82 /// Bind an index buffer (EBO) to this VAO.
83 pub fn set_ebo(&self, ctx: &GfxContext, ebo: &Buffer) {
84 unsafe { (ctx.api().vao_set_ebo)(self.handle, ebo.handle) }
85 }
86}
87
88// ── ShaderProgram ─────────────────────────────────────────────────────────────
89
90/// A compiled and linked GLSL shader program.
91///
92/// Created by [`GfxContext::create_shader`]. Must be deleted with
93/// [`GfxContext::delete_shader`].
94#[derive(Copy, Clone, Debug, PartialEq, Eq)]
95pub struct ShaderProgram {
96 pub handle: u32,
97}
98
99impl ShaderProgram {
100 pub fn uniform_1i(&self, ctx: &GfxContext, name: &str, v: i32) {
101 unsafe { (ctx.api().prog_uniform_1i)(self.handle, YogStr::from_str(name), v) }
102 }
103 pub fn uniform_1f(&self, ctx: &GfxContext, name: &str, v: f32) {
104 unsafe { (ctx.api().prog_uniform_1f)(self.handle, YogStr::from_str(name), v) }
105 }
106 pub fn uniform_2f(&self, ctx: &GfxContext, name: &str, x: f32, y: f32) {
107 unsafe { (ctx.api().prog_uniform_2f)(self.handle, YogStr::from_str(name), x, y) }
108 }
109 pub fn uniform_3f(&self, ctx: &GfxContext, name: &str, x: f32, y: f32, z: f32) {
110 unsafe { (ctx.api().prog_uniform_3f)(self.handle, YogStr::from_str(name), x, y, z) }
111 }
112 pub fn uniform_4f(&self, ctx: &GfxContext, name: &str, x: f32, y: f32, z: f32, w: f32) {
113 unsafe { (ctx.api().prog_uniform_4f)(self.handle, YogStr::from_str(name), x, y, z, w) }
114 }
115 /// Upload a column-major 4×4 matrix (16 contiguous `f32` values).
116 pub fn uniform_mat4(&self, ctx: &GfxContext, name: &str, col_major: &[f32; 16]) {
117 unsafe { (ctx.api().prog_uniform_mat4)(self.handle, YogStr::from_str(name), col_major.as_ptr()) }
118 }
119}
120
121// ── Texture ───────────────────────────────────────────────────────────────────
122
123/// A 2-D GPU texture.
124///
125/// Created by [`GfxContext::create_texture_rgba`] or [`GfxContext::texture_from_mc`].
126/// Must be deleted with [`GfxContext::delete_texture`] when you own it
127/// (do **not** delete handles from `texture_from_mc` — Minecraft owns those).
128#[derive(Copy, Clone, Debug, PartialEq, Eq)]
129pub struct Texture {
130 pub handle: u32,
131}