gpu/data/textures/
texture.rs

1use crate::prelude::*;
2use crate::{Context, GLContext};
3use crate::TextureFormat;
4
5type TextureResource = <glow::Context as HasContext>::Texture;
6
7/// A `Texture` representation.
8pub struct Texture {
9    pub(crate) gl      : GLContext,
10    resource           : TextureResource,
11    pub(crate) format  : TextureFormat,
12    typ                : u32
13}
14
15impl Texture {
16    /// Creates a new `Texture` with the specified `TextureFormat` and the internal OpenGL `typ`.
17    pub fn new(context:&Context, format:TextureFormat, typ:u32) -> Self {
18        let gl = context.gl_context();
19        let resource = unsafe {
20            gl.create_texture().expect("Couldn't create texture")
21        };
22        Self { gl, resource, format, typ }
23    }
24
25    /// Gets the internal OpenGL type.
26    pub fn typ(&self) -> u32 {
27        self.typ
28    }
29
30    /// Gets the `TextureFormat`.
31    pub fn format(&self) -> &TextureFormat {
32        &self.format
33    }
34
35    pub(crate) fn bind(&self) {
36        unsafe {
37            self.gl.bind_texture(self.typ(), Some(self.resource()));
38        }
39    }
40
41    /// Gets `TextureResource`.
42    pub fn resource(&self) -> TextureResource {
43        self.resource
44    }
45}
46
47impl Drop for Texture {
48    fn drop(&mut self) {
49        unsafe {
50            self.gl.delete_texture(self.resource());
51        }
52    }
53}