forked from servo/rust-layers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexturegl.rs
180 lines (159 loc) · 5.57 KB
/
texturegl.rs
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
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! OpenGL-specific implementation of texturing.
use layers::{ARGB32Format, Format, RGB24Format};
use geom::size::Size2D;
use opengles::gl2::{BGRA, CLAMP_TO_EDGE, GLenum, GLint, GLsizei, GLuint, LINEAR, RGB, RGBA};
use opengles::gl2::{TEXTURE_MAG_FILTER, TEXTURE_MIN_FILTER, TEXTURE_2D, TEXTURE_RECTANGLE_ARB};
use opengles::gl2::{TEXTURE_WRAP_S, TEXTURE_WRAP_T, UNSIGNED_BYTE, UNSIGNED_INT_8_8_8_8_REV};
use opengles::gl2;
use std::num::Zero;
/// Image data used when uploading to a texture.
pub struct TextureImageData<'self> {
size: Size2D<uint>,
stride: uint,
format: Format,
data: &'self [u8],
}
/// The texture target.
pub enum TextureTarget {
/// TEXTURE_2D.
TextureTarget2D,
/// TEXTURE_RECTANGLE_ARB, with the size included.
TextureTargetRectangle(Size2D<uint>),
}
impl TextureTarget {
fn as_gl_target(self) -> GLenum {
match self {
TextureTarget2D => TEXTURE_2D,
TextureTargetRectangle(*) => TEXTURE_RECTANGLE_ARB,
}
}
}
/// A texture.
///
/// TODO: Include client storage here for `GL_CLIENT_STORAGE_APPLE`.
pub struct Texture {
/// The OpenGL texture ID.
priv id: GLuint,
/// The texture target.
target: TextureTarget,
/// Whether this texture is weak. Weak textures will not be cleaned up by
/// the destructor.
priv weak: bool,
}
impl Drop for Texture {
fn drop(&mut self) {
if !self.weak {
gl2::delete_textures([ self.id ])
}
}
}
impl Zero for Texture {
fn zero() -> Texture {
Texture {
id: 0,
target: TextureTarget2D,
weak: true,
}
}
fn is_zero(&self) -> bool {
self.id == 0
}
}
/// Encapsulates a bound texture. This ensures that the texture is unbound
/// properly.
struct BoundTexture {
target: TextureTarget
}
impl Drop for BoundTexture {
fn drop(&mut self) {
gl2::bind_texture(self.target.as_gl_target(), 0)
}
}
impl Texture {
/// Creates a new blank texture.
pub fn new(target: TextureTarget) -> Texture {
let this = Texture {
id: gl2::gen_textures(1)[0],
target: target,
weak: false,
};
this.set_default_params();
this
}
/// Creates a texture from an existing OpenGL texture. The texture will be deleted when this
/// `Texture` object goes out of scope.
pub fn adopt_native_texture(native_texture_id: GLuint, target: TextureTarget) -> Texture {
let this = Texture {
id: native_texture_id,
target: target,
weak: false,
};
this
}
/// Creates a texture from an existing OpenGL texture. The texture will *not* be deleted when
/// this `Texture` object goes out of scope.
pub fn wrap_native_texture(native_texture_id: GLuint, target: TextureTarget) -> Texture {
let this = Texture {
id: native_texture_id,
target: target,
weak: true,
};
this.set_default_params();
this
}
/// Returns the raw OpenGL texture underlying this texture.
pub fn native_texture(&self) -> GLuint {
self.id
}
/// Sets default parameters for this texture.
fn set_default_params(&self) {
let _bound_texture = self.bind();
gl2::tex_parameter_i(self.target.as_gl_target(), TEXTURE_MAG_FILTER, LINEAR as GLint);
gl2::tex_parameter_i(self.target.as_gl_target(), TEXTURE_MIN_FILTER, LINEAR as GLint);
gl2::tex_parameter_i(self.target.as_gl_target(), TEXTURE_WRAP_S, CLAMP_TO_EDGE as GLint);
gl2::tex_parameter_i(self.target.as_gl_target(), TEXTURE_WRAP_T, CLAMP_TO_EDGE as GLint);
}
/// Binds the texture to the current context.
pub fn bind(&self) -> BoundTexture {
gl2::bind_texture(self.target.as_gl_target(), self.id);
BoundTexture {
target: self.target,
}
}
/// Uploads raw image data to the texture.
pub fn upload_image<'a>(&self, texture_image_data: &TextureImageData<'a>) {
let _bound_texture = self.bind();
match texture_image_data.format {
RGB24Format => {
gl2::tex_image_2d(self.target.as_gl_target(),
0,
RGB as GLint,
texture_image_data.size.width as GLsizei,
texture_image_data.size.height as GLsizei,
0,
RGB,
UNSIGNED_BYTE,
Some(texture_image_data.data))
}
ARGB32Format => {
gl2::tex_image_2d(self.target.as_gl_target(),
0,
RGBA as GLint,
texture_image_data.size.width as GLsizei,
texture_image_data.size.height as GLsizei,
0,
BGRA,
UNSIGNED_INT_8_8_8_8_REV,
Some(texture_image_data.data))
}
}
}
}