1 /******************************************************************************
2 Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17
18 #include "gl-subsystem.h"
19
upload_texture_2d(struct gs_texture_2d * tex,const uint8_t ** data)20 static bool upload_texture_2d(struct gs_texture_2d *tex, const uint8_t **data)
21 {
22 uint32_t row_size = tex->width * gs_get_format_bpp(tex->base.format);
23 uint32_t tex_size = tex->height * row_size / 8;
24 uint32_t num_levels = tex->base.levels;
25 bool compressed = gs_is_compressed_format(tex->base.format);
26 bool success;
27
28 if (!num_levels)
29 num_levels = gs_get_total_levels(tex->width, tex->height, 1);
30
31 if (!gl_bind_texture(GL_TEXTURE_2D, tex->base.texture))
32 return false;
33
34 success = gl_init_face(GL_TEXTURE_2D, tex->base.gl_type, num_levels,
35 tex->base.gl_format,
36 tex->base.gl_internal_format, compressed,
37 tex->width, tex->height, tex_size, &data);
38
39 if (!gl_tex_param_i(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL,
40 num_levels - 1))
41 success = false;
42 if (!gl_bind_texture(GL_TEXTURE_2D, 0))
43 success = false;
44
45 return success;
46 }
47
create_pixel_unpack_buffer(struct gs_texture_2d * tex)48 static bool create_pixel_unpack_buffer(struct gs_texture_2d *tex)
49 {
50 GLsizeiptr size;
51 bool success = true;
52
53 if (!gl_gen_buffers(1, &tex->unpack_buffer))
54 return false;
55
56 if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex->unpack_buffer))
57 return false;
58
59 size = tex->width * gs_get_format_bpp(tex->base.format);
60 if (!gs_is_compressed_format(tex->base.format)) {
61 size /= 8;
62 size = (size + 3) & 0xFFFFFFFC;
63 size *= tex->height;
64 } else {
65 size *= tex->height;
66 size /= 8;
67 }
68
69 glBufferData(GL_PIXEL_UNPACK_BUFFER, size, 0, GL_DYNAMIC_DRAW);
70 if (!gl_success("glBufferData"))
71 success = false;
72
73 if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0))
74 success = false;
75
76 return success;
77 }
78
device_texture_create(gs_device_t * device,uint32_t width,uint32_t height,enum gs_color_format color_format,uint32_t levels,const uint8_t ** data,uint32_t flags)79 gs_texture_t *device_texture_create(gs_device_t *device, uint32_t width,
80 uint32_t height,
81 enum gs_color_format color_format,
82 uint32_t levels, const uint8_t **data,
83 uint32_t flags)
84 {
85 struct gs_texture_2d *tex = bzalloc(sizeof(struct gs_texture_2d));
86 tex->base.device = device;
87 tex->base.type = GS_TEXTURE_2D;
88 tex->base.format = color_format;
89 tex->base.levels = levels;
90 tex->base.gl_format = convert_gs_format(color_format);
91 tex->base.gl_internal_format = convert_gs_internal_format(color_format);
92 tex->base.gl_type = get_gl_format_type(color_format);
93 tex->base.gl_target = GL_TEXTURE_2D;
94 tex->base.is_dynamic = (flags & GS_DYNAMIC) != 0;
95 tex->base.is_render_target = (flags & GS_RENDER_TARGET) != 0;
96 tex->base.is_dummy = (flags & GS_GL_DUMMYTEX) != 0;
97 tex->base.gen_mipmaps = (flags & GS_BUILD_MIPMAPS) != 0;
98 tex->width = width;
99 tex->height = height;
100
101 if (!gl_gen_textures(1, &tex->base.texture))
102 goto fail;
103
104 if (!tex->base.is_dummy) {
105 if (tex->base.is_dynamic && !create_pixel_unpack_buffer(tex))
106 goto fail;
107 if (!upload_texture_2d(tex, data))
108 goto fail;
109 } else {
110 if (!gl_bind_texture(GL_TEXTURE_2D, tex->base.texture))
111 goto fail;
112
113 uint32_t row_size =
114 tex->width * gs_get_format_bpp(tex->base.format);
115 uint32_t tex_size = tex->height * row_size / 8;
116 bool compressed = gs_is_compressed_format(tex->base.format);
117 bool did_init = gl_init_face(GL_TEXTURE_2D, tex->base.gl_type,
118 1, tex->base.gl_format,
119 tex->base.gl_internal_format,
120 compressed, tex->width,
121 tex->height, tex_size, NULL);
122 did_init =
123 gl_tex_param_i(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
124
125 bool did_unbind = gl_bind_texture(GL_TEXTURE_2D, 0);
126 if (!did_init || !did_unbind)
127 goto fail;
128 }
129
130 return (gs_texture_t *)tex;
131
132 fail:
133 gs_texture_destroy((gs_texture_t *)tex);
134 blog(LOG_ERROR, "device_texture_create (GL) failed");
135 return NULL;
136 }
137
is_texture_2d(const gs_texture_t * tex,const char * func)138 static inline bool is_texture_2d(const gs_texture_t *tex, const char *func)
139 {
140 bool is_tex2d = tex->type == GS_TEXTURE_2D;
141 if (!is_tex2d)
142 blog(LOG_ERROR, "%s (GL) failed: Not a 2D texture", func);
143 return is_tex2d;
144 }
145
gs_texture_destroy(gs_texture_t * tex)146 void gs_texture_destroy(gs_texture_t *tex)
147 {
148 if (!tex)
149 return;
150
151 if (tex->cur_sampler)
152 gs_samplerstate_destroy(tex->cur_sampler);
153
154 if (!tex->is_dummy && tex->is_dynamic) {
155 if (tex->type == GS_TEXTURE_2D) {
156 struct gs_texture_2d *tex2d =
157 (struct gs_texture_2d *)tex;
158 if (tex2d->unpack_buffer)
159 gl_delete_buffers(1, &tex2d->unpack_buffer);
160 } else if (tex->type == GS_TEXTURE_3D) {
161 struct gs_texture_3d *tex3d =
162 (struct gs_texture_3d *)tex;
163 if (tex3d->unpack_buffer)
164 gl_delete_buffers(1, &tex3d->unpack_buffer);
165 }
166 }
167
168 if (tex->texture)
169 gl_delete_textures(1, &tex->texture);
170
171 if (tex->fbo)
172 fbo_info_destroy(tex->fbo);
173
174 bfree(tex);
175 }
176
gs_texture_get_width(const gs_texture_t * tex)177 uint32_t gs_texture_get_width(const gs_texture_t *tex)
178 {
179 const struct gs_texture_2d *tex2d = (const struct gs_texture_2d *)tex;
180 if (!is_texture_2d(tex, "gs_texture_get_width"))
181 return 0;
182
183 return tex2d->width;
184 }
185
gs_texture_get_height(const gs_texture_t * tex)186 uint32_t gs_texture_get_height(const gs_texture_t *tex)
187 {
188 const struct gs_texture_2d *tex2d = (const struct gs_texture_2d *)tex;
189 if (!is_texture_2d(tex, "gs_texture_get_height"))
190 return 0;
191
192 return tex2d->height;
193 }
194
gs_texture_get_color_format(const gs_texture_t * tex)195 enum gs_color_format gs_texture_get_color_format(const gs_texture_t *tex)
196 {
197 return tex->format;
198 }
199
gs_texture_map(gs_texture_t * tex,uint8_t ** ptr,uint32_t * linesize)200 bool gs_texture_map(gs_texture_t *tex, uint8_t **ptr, uint32_t *linesize)
201 {
202 struct gs_texture_2d *tex2d = (struct gs_texture_2d *)tex;
203
204 if (!is_texture_2d(tex, "gs_texture_map"))
205 goto fail;
206
207 if (!tex2d->base.is_dynamic) {
208 blog(LOG_ERROR, "Texture is not dynamic");
209 goto fail;
210 }
211
212 if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex2d->unpack_buffer))
213 goto fail;
214
215 *ptr = glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
216 if (!gl_success("glMapBuffer"))
217 goto fail;
218
219 gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
220
221 *linesize = tex2d->width * gs_get_format_bpp(tex->format) / 8;
222 *linesize = (*linesize + 3) & 0xFFFFFFFC;
223 return true;
224
225 fail:
226 blog(LOG_ERROR, "gs_texture_map (GL) failed");
227 return false;
228 }
229
gs_texture_unmap(gs_texture_t * tex)230 void gs_texture_unmap(gs_texture_t *tex)
231 {
232 struct gs_texture_2d *tex2d = (struct gs_texture_2d *)tex;
233 if (!is_texture_2d(tex, "gs_texture_unmap"))
234 goto failed;
235
236 if (!gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, tex2d->unpack_buffer))
237 goto failed;
238
239 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
240 if (!gl_success("glUnmapBuffer"))
241 goto failed;
242
243 if (!gl_bind_texture(GL_TEXTURE_2D, tex2d->base.texture))
244 goto failed;
245
246 glTexImage2D(GL_TEXTURE_2D, 0, tex->gl_internal_format, tex2d->width,
247 tex2d->height, 0, tex->gl_format, tex->gl_type, 0);
248 if (!gl_success("glTexImage2D"))
249 goto failed;
250
251 gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
252 gl_bind_texture(GL_TEXTURE_2D, 0);
253 return;
254
255 failed:
256 gl_bind_buffer(GL_PIXEL_UNPACK_BUFFER, 0);
257 gl_bind_texture(GL_TEXTURE_2D, 0);
258 blog(LOG_ERROR, "gs_texture_unmap (GL) failed");
259 }
260
gs_texture_is_rect(const gs_texture_t * tex)261 bool gs_texture_is_rect(const gs_texture_t *tex)
262 {
263 if (tex->type == GS_TEXTURE_3D)
264 return false;
265
266 if (!is_texture_2d(tex, "gs_texture_is_rect")) {
267 blog(LOG_ERROR, "gs_texture_is_rect (GL) failed");
268 return false;
269 }
270
271 const struct gs_texture_2d *tex2d = (const struct gs_texture_2d *)tex;
272 return tex2d->base.gl_target == GL_TEXTURE_RECTANGLE;
273 }
274
gs_texture_get_obj(gs_texture_t * tex)275 void *gs_texture_get_obj(gs_texture_t *tex)
276 {
277 struct gs_texture_2d *tex2d = (struct gs_texture_2d *)tex;
278 if (!is_texture_2d(tex, "gs_texture_get_obj")) {
279 blog(LOG_ERROR, "gs_texture_get_obj (GL) failed");
280 return NULL;
281 }
282
283 return &tex2d->base.texture;
284 }
285