forked from LexiBigCheese/vita_gl_helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.rs
More file actions
352 lines (316 loc) · 9.72 KB
/
texture.rs
File metadata and controls
352 lines (316 loc) · 9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//! Texture Management
//!
//! This module provides safe wrappers for OpenGL texture objects with
//! automatic cleanup via RAII.
//!
//! # Example
//! ```ignore
//! use vita_gl_helpers::texture::Texture;
//!
//! // Create a new texture - automatically cleaned up when dropped
//! let texture = Texture::new();
//!
//! // Bind and configure
//! texture.bind_then(gl::TEXTURE_2D, |bound| {
//! bound.image_2d(0, gl::RGBA as i32, 256, 256, gl::RGBA, gl::UNSIGNED_BYTE, pixels);
//! bound.parameter_i(gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32);
//! bound.parameter_i(gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32);
//! });
//! ```
use std::ffi::c_void;
use gl::types::{GLenum, GLint, GLuint};
use crate::handle::{GpuResource, Handle};
/// Marker type for texture resources.
pub struct TextureResource;
impl GpuResource for TextureResource {
type Id = GLuint;
unsafe fn delete(id: Self::Id) {
gl::DeleteTextures(1, &id);
}
fn is_null(id: Self::Id) -> bool {
id == 0
}
fn resource_name() -> &'static str {
"Texture"
}
}
/// A GPU texture object with automatic cleanup.
///
/// When dropped, the texture is automatically deleted via `glDeleteTextures`.
///
/// # Example
/// ```ignore
/// let texture = Texture::new();
/// texture.bind(gl::TEXTURE_2D);
/// // texture is automatically deleted when it goes out of scope
/// ```
pub struct Texture(Handle<TextureResource>);
impl Texture {
/// Create a new GPU texture.
///
/// # Example
/// ```ignore
/// let texture = Texture::new();
/// assert!(texture.is_valid());
/// ```
pub fn new() -> Self {
let mut id: GLuint = 0;
unsafe {
gl::GenTextures(1, &mut id);
}
Texture(unsafe { Handle::from_raw(id) })
}
/// Create multiple textures at once (more efficient than individual creation).
///
/// # Example
/// ```ignore
/// let textures = Texture::new_batch(5);
/// assert_eq!(textures.len(), 5);
/// ```
pub fn new_batch(count: usize) -> Vec<Self> {
let mut ids = vec![0 as GLuint; count];
unsafe {
gl::GenTextures(count as i32, ids.as_mut_ptr());
}
ids.into_iter()
.map(|id| Texture(unsafe { Handle::from_raw(id) }))
.collect()
}
/// Create a texture from a raw OpenGL ID. Takes ownership.
///
/// # Safety
/// The caller must ensure the ID is a valid texture created with `glGenTextures`
/// and that ownership is being transferred.
pub unsafe fn from_raw(id: GLuint) -> Self {
Texture(Handle::from_raw(id))
}
/// Get the raw OpenGL texture ID.
///
/// Returns `None` if the texture is invalid.
#[inline]
pub fn id(&self) -> Option<GLuint> {
self.0.id()
}
/// Get the raw OpenGL texture ID, panicking if invalid.
///
/// # Panics
/// Panics if the texture is invalid.
#[inline]
pub fn id_unwrap(&self) -> GLuint {
self.0.id_unwrap()
}
/// Check if this texture is valid.
#[inline]
pub fn is_valid(&self) -> bool {
self.0.is_valid()
}
/// Take ownership of the raw ID, preventing automatic deletion.
///
/// After calling this, you are responsible for deleting the texture.
pub fn into_raw(self) -> Option<GLuint> {
self.0.into_raw()
}
/// Bind this texture to the specified target.
///
/// # Arguments
/// * `target` - The texture target (e.g., `gl::TEXTURE_2D`)
///
/// # Example
/// ```ignore
/// texture.bind(gl::TEXTURE_2D);
/// ```
pub fn bind(&self, target: GLenum) {
if let Some(id) = self.0.id() {
unsafe {
gl::BindTexture(target, id);
}
}
}
/// Bind this texture, execute a closure, then optionally unbind.
///
/// This pattern ensures the texture is bound during the operation.
///
/// # Example
/// ```ignore
/// texture.bind_then(gl::TEXTURE_2D, |bound| {
/// bound.parameter_i(gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32);
/// });
/// ```
pub fn bind_then<R>(&self, target: GLenum, then: impl FnOnce(BoundTexture) -> R) -> R {
self.bind(target);
then(BoundTexture(target))
}
/// Activate a texture unit and bind this texture.
///
/// # Arguments
/// * `unit` - The texture unit (0, 1, 2, etc.)
/// * `target` - The texture target
///
/// # Example
/// ```ignore
/// texture.bind_to_unit(0, gl::TEXTURE_2D);
/// ```
pub fn bind_to_unit(&self, unit: u32, target: GLenum) {
unsafe {
gl::ActiveTexture(gl::TEXTURE0 + unit);
}
self.bind(target);
}
}
impl Default for Texture {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for Texture {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Texture").field(&self.0).finish()
}
}
/// A temporarily bound texture for operations.
///
/// This type is returned by [`Texture::bind_then`] and represents
/// a texture that is currently bound to a target.
#[non_exhaustive]
pub struct BoundTexture(GLenum);
impl BoundTexture {
/// Upload 2D image data to the bound texture.
///
/// # Arguments
/// * `level` - Mipmap level (0 for base level)
/// * `internalformat` - Internal format (e.g., `gl::RGBA`)
/// * `width` - Image width
/// * `height` - Image height
/// * `format` - Pixel format (e.g., `gl::RGBA`)
/// * `type_` - Pixel type (e.g., `gl::UNSIGNED_BYTE`)
/// * `pixels` - Pointer to pixel data (can be null for allocation only)
pub fn image_2d(
&self,
level: impl Into<GLint>,
internalformat: impl Into<GLint>,
width: impl Into<GLint>,
height: impl Into<GLint>,
format: impl Into<GLenum>,
type_: impl Into<GLenum>,
pixels: *const c_void,
) {
unsafe {
gl::TexImage2D(
self.0,
level.into(),
internalformat.into(),
width.into(),
height.into(),
0, // border (must be 0)
format.into(),
type_.into(),
pixels,
);
}
}
/// Upload 2D image data from a slice.
///
/// # Arguments
/// * `level` - Mipmap level
/// * `internalformat` - Internal format
/// * `width` - Image width
/// * `height` - Image height
/// * `format` - Pixel format
/// * `type_` - Pixel type
/// * `data` - Pixel data slice
pub fn image_2d_data<T>(
&self,
level: impl Into<GLint>,
internalformat: impl Into<GLint>,
width: impl Into<GLint>,
height: impl Into<GLint>,
format: impl Into<GLenum>,
type_: impl Into<GLenum>,
data: &[T],
) {
self.image_2d(
level,
internalformat,
width,
height,
format,
type_,
data.as_ptr() as *const c_void,
);
}
/// Generate mipmaps for the bound texture.
pub fn gen_mipmap(&self) {
unsafe {
gl::GenerateMipmap(self.0);
}
}
/// Set a texture parameter (integer).
///
/// # Arguments
/// * `pname` - Parameter name (e.g., `gl::TEXTURE_MIN_FILTER`)
/// * `param` - Parameter value
pub fn parameter_i(&self, pname: impl Into<GLenum>, param: impl Into<GLint>) {
unsafe {
gl::TexParameteri(self.0, pname.into(), param.into());
}
}
/// Set a texture parameter (float).
///
/// # Arguments
/// * `pname` - Parameter name
/// * `param` - Parameter value
pub fn parameter_f(&self, pname: impl Into<GLenum>, param: f32) {
unsafe {
gl::TexParameterf(self.0, pname.into(), param);
}
}
/// Set common texture parameters for linear filtering.
pub fn set_linear_filtering(&self) {
self.parameter_i(gl::TEXTURE_MIN_FILTER, gl::LINEAR as i32);
self.parameter_i(gl::TEXTURE_MAG_FILTER, gl::LINEAR as i32);
}
/// Set common texture parameters for nearest (pixel) filtering.
pub fn set_nearest_filtering(&self) {
self.parameter_i(gl::TEXTURE_MIN_FILTER, gl::NEAREST as i32);
self.parameter_i(gl::TEXTURE_MAG_FILTER, gl::NEAREST as i32);
}
/// Set texture wrap mode for both S and T coordinates.
pub fn set_wrap(&self, wrap_mode: impl Into<GLint>) {
let mode = wrap_mode.into();
self.parameter_i(gl::TEXTURE_WRAP_S, mode);
self.parameter_i(gl::TEXTURE_WRAP_T, mode);
}
}
// ============================================================================
// Legacy API (deprecated, for backwards compatibility)
// ============================================================================
/// Legacy trait for generating and deleting textures.
///
/// # Deprecated
/// Use [`Texture::new`] and [`Texture::new_batch`] instead.
/// Textures are now automatically deleted when dropped.
#[deprecated(since = "0.2.0", note = "Use Texture::new() instead - textures auto-delete on drop")]
pub trait GenDelTexturesExt {
/// Generate texture IDs.
fn gen_textures(&mut self);
/// Delete texture IDs.
fn delete_textures(&mut self);
}
#[allow(deprecated)]
impl GenDelTexturesExt for [GLuint] {
fn gen_textures(&mut self) {
unsafe { gl::GenTextures(self.len() as i32, self.as_mut_ptr()) }
}
fn delete_textures(&mut self) {
unsafe { gl::DeleteTextures(self.len() as i32, self.as_mut_ptr()) }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_texture_batch_creation() {
// Note: This test won't actually create GL textures without a context,
// but it verifies the API compiles correctly
}
}