Skip to main content

edgefirst_gl/
apis.rs

1//! Safe-ish wrappers over the raw `gl` bindings.
2//!
3//! Trimmed from upstream `gls` to only the functions the EdgeFirst HAL uses.
4
5// The texture-image wrappers mirror the GL entry points' arity by design.
6#![allow(clippy::too_many_arguments)]
7
8use crate::{gl, Error, GLchar, GLeglImageOES, GLenum, GLint, GLsizei, GLuint};
9use std::ffi::CStr;
10
11/// Select active texture unit.
12pub fn active_texture(texture: GLenum) {
13    unsafe { gl::ActiveTexture(texture) }
14}
15
16/// Bind a named texture to a texturing target.
17pub fn bind_texture(target: GLenum, texture: GLuint) {
18    unsafe { gl::BindTexture(target, texture) }
19}
20
21/// Specify pixel arithmetic for RGB and alpha components separately.
22pub fn blend_func_separate(
23    sfactor_rgb: GLenum,
24    dfactor_rgb: GLenum,
25    sfactor_alpha: GLenum,
26    dfactor_alpha: GLenum,
27) {
28    unsafe { gl::BlendFuncSeparate(sfactor_rgb, dfactor_rgb, sfactor_alpha, dfactor_alpha) }
29}
30
31/// Disable a server-side GL capability.
32pub fn disable(cap: GLenum) {
33    unsafe { gl::Disable(cap) }
34}
35
36/// Define a 2D texture image from an `EGLImage` (zero-copy import).
37// `image` is an opaque EGL handle passed straight to the driver; keeping this a
38// safe wrapper matches upstream `gls` and the HAL's call sites.
39#[allow(clippy::not_unsafe_ptr_arg_deref)]
40pub fn egl_image_target_texture_2d_oes(target: GLenum, image: GLeglImageOES) {
41    unsafe { gl::EGLImageTargetTexture2DOES(target, image) }
42}
43
44/// Enable a server-side GL capability.
45pub fn enable(cap: GLenum) {
46    unsafe { gl::Enable(cap) }
47}
48
49/// Block until all GL execution is complete.
50pub fn finish() {
51    unsafe { gl::Finish() }
52}
53
54/// Return a string describing the current GL connection.
55pub fn get_string(name: GLenum) -> Result<String, Error> {
56    unsafe {
57        let name: *const GLchar = gl::GetString(name) as *const GLchar;
58        if name.is_null() {
59            Err(Error::new())
60        } else {
61            Ok(CStr::from_ptr(name).to_string_lossy().into_owned())
62        }
63    }
64}
65
66/// Specify a two-dimensional texture image.
67pub fn tex_image2d<T>(
68    target: GLenum,
69    level: GLint,
70    internalformat: GLint,
71    width: GLsizei,
72    height: GLsizei,
73    border: GLint,
74    format: GLenum,
75    type_: GLenum,
76    pixels: Option<&[T]>,
77) where
78    T: Sized,
79{
80    let ptr = match pixels {
81        Some(v) => v.as_ptr() as *const core::ffi::c_void,
82        None => std::ptr::null(),
83    };
84    unsafe {
85        gl::TexImage2D(
86            target,
87            level,
88            internalformat,
89            width,
90            height,
91            border,
92            format,
93            type_,
94            ptr,
95        );
96    }
97}
98
99/// Specify a three-dimensional texture image.
100pub fn tex_image3d<T>(
101    target: GLenum,
102    level: GLint,
103    internalformat: GLint,
104    width: GLsizei,
105    height: GLsizei,
106    depth: GLsizei,
107    border: GLint,
108    format: GLenum,
109    type_: GLenum,
110    pixels: Option<&[T]>,
111) where
112    T: Sized,
113{
114    let ptr = match pixels {
115        Some(v) => v.as_ptr() as *const core::ffi::c_void,
116        None => std::ptr::null(),
117    };
118    unsafe {
119        gl::TexImage3D(
120            target,
121            level,
122            internalformat,
123            width,
124            height,
125            depth,
126            border,
127            format,
128            type_,
129            ptr,
130        );
131    }
132}
133
134/// Set an integer texture parameter.
135pub fn tex_parameteri(target: GLenum, pname: GLenum, param: GLint) {
136    unsafe {
137        gl::TexParameteri(target, pname, param);
138    }
139}
140
141/// Install a program object as part of current rendering state.
142pub fn use_program(program: GLuint) {
143    unsafe {
144        gl::UseProgram(program);
145    }
146}