Cleanup: GPU headers
[blender.git] / source / blender / gpu / intern / gpu_texture.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (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, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Brecht Van Lommel.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include "MEM_guardedalloc.h"
29
30 #include "DNA_image_types.h"
31
32 #include "BLI_blenlib.h"
33 #include "BLI_utildefines.h"
34 #include "BLI_math_base.h"
35
36 #include "BKE_global.h"
37
38 #include "GPU_debug.h"
39 #include "GPU_draw.h"
40 #include "GPU_extensions.h"
41 #include "GPU_framebuffer.h"
42 #include "GPU_glew.h"
43 #include "GPU_texture.h"
44
45 static struct GPUTextureGlobal {
46         GPUTexture *invalid_tex_1D; /* texture used in place of invalid textures (not loaded correctly, missing) */
47         GPUTexture *invalid_tex_2D;
48         GPUTexture *invalid_tex_3D;
49 } GG = {NULL, NULL, NULL};
50
51 /* GPUTexture */
52
53 struct GPUTexture {
54         int w, h;           /* width/height */
55         int number;         /* number for multitexture binding */
56         int refcount;       /* reference count */
57         GLenum target;      /* GL_TEXTURE_* */
58         GLenum target_base; /* same as target, (but no multisample) */
59         GLuint bindcode;    /* opengl identifier for texture */
60         int fromblender;    /* we got the texture from Blender */
61
62         GPUFrameBuffer *fb; /* GPUFramebuffer this texture is attached to */
63         int fb_attachment;  /* slot the texture is attached to */
64         int depth;          /* is a depth texture? if 3D how deep? */
65 };
66
67 static unsigned char *GPU_texture_convert_pixels(int length, const float *fpixels)
68 {
69         unsigned char *pixels, *p;
70         const float *fp = fpixels;
71         const int len = 4 * length;
72
73         p = pixels = MEM_callocN(sizeof(unsigned char) * len, "GPUTexturePixels");
74
75         for (int a = 0; a < len; a++, p++, fp++)
76                 *p = FTOCHAR((*fp));
77
78         return pixels;
79 }
80
81 static void GPU_glTexSubImageEmpty(GLenum target, GLenum format, int x, int y, int w, int h)
82 {
83         void *pixels = MEM_callocN(sizeof(char) * 4 * w * h, "GPUTextureEmptyPixels");
84
85         if (target == GL_TEXTURE_1D)
86                 glTexSubImage1D(target, 0, x, w, format, GL_UNSIGNED_BYTE, pixels);
87         else
88                 glTexSubImage2D(target, 0, x, y, w, h, format, GL_UNSIGNED_BYTE, pixels);
89         
90         MEM_freeN(pixels);
91 }
92
93 static GPUTexture *GPU_texture_create_nD(
94         int w, int h, int n, const float *fpixels, int depth,
95         GPUHDRType hdr_type, int components, int samples,
96         char err_out[256])
97 {
98         GLenum type, format, internalformat;
99         void *pixels = NULL;
100
101         if (samples) {
102                 CLAMP_MAX(samples, GPU_max_color_texture_samples());
103         }
104
105         GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture");
106         tex->w = w;
107         tex->h = h;
108         tex->number = -1;
109         tex->refcount = 1;
110         tex->target = (n == 1) ? GL_TEXTURE_1D : (samples ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D);
111         tex->target_base = (n == 1) ? GL_TEXTURE_1D : GL_TEXTURE_2D;
112         tex->depth = depth;
113         tex->fb_attachment = -1;
114
115         glGenTextures(1, &tex->bindcode);
116
117         if (!tex->bindcode) {
118                 if (err_out) {
119                         BLI_snprintf(err_out, 256, "GPUTexture: texture create failed: %d",
120                                 (int)glGetError());
121                 }
122                 else {
123                         fprintf(stderr, "GPUTexture: texture create failed: %d\n",
124                                 (int)glGetError());
125                 }
126                 GPU_texture_free(tex);
127                 return NULL;
128         }
129
130         if (!GPU_full_non_power_of_two_support()) {
131                 tex->w = power_of_2_max_i(tex->w);
132                 tex->h = power_of_2_max_i(tex->h);
133         }
134
135         tex->number = 0;
136         glBindTexture(tex->target, tex->bindcode);
137
138         if (depth) {
139                 type = GL_UNSIGNED_BYTE;
140                 format = GL_DEPTH_COMPONENT;
141                 internalformat = GL_DEPTH_COMPONENT;
142         }
143         else {
144                 type = GL_FLOAT;
145
146                 if (components == 4) {
147                         format = GL_RGBA;
148                         switch (hdr_type) {
149                                 case GPU_HDR_NONE:
150                                         internalformat = GL_RGBA8;
151                                         break;
152                                 /* the following formats rely on ARB_texture_float or OpenGL 3.0 */
153                                 case GPU_HDR_HALF_FLOAT:
154                                         internalformat = GL_RGBA16F_ARB;
155                                         break;
156                                 case GPU_HDR_FULL_FLOAT:
157                                         internalformat = GL_RGBA32F_ARB;
158                                         break;
159                                 default:
160                                         break;
161                         }
162                 }
163                 else if (components == 2) {
164                         /* these formats rely on ARB_texture_rg or OpenGL 3.0 */
165                         format = GL_RG;
166                         switch (hdr_type) {
167                                 case GPU_HDR_NONE:
168                                         internalformat = GL_RG8;
169                                         break;
170                                 case GPU_HDR_HALF_FLOAT:
171                                         internalformat = GL_RG16F;
172                                         break;
173                                 case GPU_HDR_FULL_FLOAT:
174                                         internalformat = GL_RG32F;
175                                         break;
176                                 default:
177                                         break;
178                         }
179                 }
180
181                 if (fpixels && hdr_type == GPU_HDR_NONE) {
182                         type = GL_UNSIGNED_BYTE;
183                         pixels = GPU_texture_convert_pixels(w * h, fpixels);
184                 }
185         }
186
187         if (tex->target == GL_TEXTURE_1D) {
188                 glTexImage1D(tex->target, 0, internalformat, tex->w, 0, format, type, NULL);
189
190                 if (fpixels) {
191                         glTexSubImage1D(tex->target, 0, 0, w, format, type,
192                                 pixels ? pixels : fpixels);
193
194                         if (tex->w > w) {
195                                 GPU_glTexSubImageEmpty(tex->target, format, w, 0, tex->w - w, 1);
196                         }
197                 }
198         }
199         else {
200                 if (samples) {
201                         glTexImage2DMultisample(tex->target, samples, internalformat, tex->w, tex->h, true);
202                 }
203                 else {
204                         glTexImage2D(tex->target, 0, internalformat, tex->w, tex->h, 0,
205                                      format, type, NULL);
206                 }
207
208                 if (fpixels) {
209                         glTexSubImage2D(tex->target, 0, 0, 0, w, h,
210                                 format, type, pixels ? pixels : fpixels);
211
212                         if (tex->w > w)
213                                 GPU_glTexSubImageEmpty(tex->target, format, w, 0, tex->w - w, tex->h);
214                         if (tex->h > h)
215                                 GPU_glTexSubImageEmpty(tex->target, format, 0, h, w, tex->h - h);
216                 }
217         }
218
219         if (pixels)
220                 MEM_freeN(pixels);
221
222         if (depth) {
223                 glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
224                 glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
225                 glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
226                 glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
227                 glTexParameteri(tex->target_base, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
228         }
229         else {
230                 glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
231                 glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
232         }
233
234         if (tex->target_base != GL_TEXTURE_1D) {
235                 glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
236                 glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
237         }
238         else
239                 glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
240
241         return tex;
242 }
243
244
245 GPUTexture *GPU_texture_create_3D(int w, int h, int depth, int channels, const float *fpixels)
246 {
247         GLenum type, format, internalformat;
248         void *pixels = NULL;
249
250         GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture");
251         tex->w = w;
252         tex->h = h;
253         tex->depth = depth;
254         tex->number = -1;
255         tex->refcount = 1;
256         tex->target = GL_TEXTURE_3D;
257         tex->target_base = GL_TEXTURE_3D;
258
259         glGenTextures(1, &tex->bindcode);
260
261         if (!tex->bindcode) {
262                 fprintf(stderr, "GPUTexture: texture create failed: %d\n",
263                         (int)glGetError());
264                 GPU_texture_free(tex);
265                 return NULL;
266         }
267
268         tex->number = 0;
269         glBindTexture(tex->target, tex->bindcode);
270
271         GPU_ASSERT_NO_GL_ERRORS("3D glBindTexture");
272
273         type = GL_FLOAT;
274         if (channels == 4) {
275                 format = GL_RGBA;
276                 internalformat = GL_RGBA8;
277         }
278         else {
279                 format = GL_RED;
280                 internalformat = GL_INTENSITY8;
281         }
282
283         /* 3D textures are quite heavy, test if it's possible to create them first */
284         glTexImage3D(GL_PROXY_TEXTURE_3D, 0, internalformat, tex->w, tex->h, tex->depth, 0, format, type, NULL);
285
286         bool rescale = false;
287         int r_width;
288
289         glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &r_width);
290
291         while (r_width == 0) {
292                 rescale = true;
293                 tex->w /= 2;
294                 tex->h /= 2;
295                 tex->depth /= 2;
296                 glTexImage3D(GL_PROXY_TEXTURE_3D, 0, internalformat, tex->w, tex->h, tex->depth, 0, format, type, NULL);
297                 glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &r_width);
298         }
299
300         /* really unlikely to happen but keep this just in case */
301         tex->w = max_ii(tex->w, 1);
302         tex->h = max_ii(tex->h, 1);
303         tex->depth = max_ii(tex->depth, 1);
304
305 #if 0
306         if (fpixels)
307                 pixels = GPU_texture_convert_pixels(w*h*depth, fpixels);
308 #endif
309
310         GPU_ASSERT_NO_GL_ERRORS("3D glTexImage3D");
311
312         /* hardcore stuff, 3D texture rescaling - warning, this is gonna hurt your performance a lot, but we need it
313          * for gooseberry */
314         if (rescale && fpixels) {
315                 /* FIXME: should these be floating point? */
316                 const unsigned int xf = w / tex->w, yf = h / tex->h, zf = depth / tex->depth;
317                 float *tex3d = MEM_mallocN(channels * sizeof(float) * tex->w * tex->h * tex->depth, "tex3d");
318
319                 GPU_print_error_debug("You need to scale a 3D texture, feel the pain!");
320
321                 for (unsigned k = 0; k < tex->depth; k++) {
322                         for (unsigned j = 0; j < tex->h; j++) {
323                                 for (unsigned i = 0; i < tex->w; i++) {
324                                         /* obviously doing nearest filtering here,
325                                          * it's going to be slow in any case, let's not make it worse */
326                                         float xb = i * xf;
327                                         float yb = j * yf;
328                                         float zb = k * zf;
329                                         unsigned int offset = k * (tex->w * tex->h) + i * tex->h + j;
330                                         unsigned int offset_orig = (zb) * (w * h) + (xb) * h + (yb);
331
332                                         if (channels == 4) {
333                                                 tex3d[offset * 4] = fpixels[offset_orig * 4];
334                                                 tex3d[offset * 4 + 1] = fpixels[offset_orig * 4 + 1];
335                                                 tex3d[offset * 4 + 2] = fpixels[offset_orig * 4 + 2];
336                                                 tex3d[offset * 4 + 3] = fpixels[offset_orig * 4 + 3];
337                                         }
338                                         else
339                                                 tex3d[offset] = fpixels[offset_orig];
340                                 }
341                         }
342                 }
343
344                 glTexImage3D(tex->target, 0, internalformat, tex->w, tex->h, tex->depth, 0, format, type, tex3d);
345
346                 MEM_freeN(tex3d);
347         }
348         else {
349                 if (fpixels) {
350                         glTexImage3D(tex->target, 0, internalformat, tex->w, tex->h, tex->depth, 0, format, type, fpixels);
351                         GPU_ASSERT_NO_GL_ERRORS("3D glTexSubImage3D");
352                 }
353         }
354
355
356         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
357         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
358         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
359         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
360         glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
361
362         if (pixels)
363                 MEM_freeN(pixels);
364
365         GPU_texture_unbind(tex);
366
367         return tex;
368 }
369
370 GPUTexture *GPU_texture_from_blender(Image *ima, ImageUser *iuser, int textarget, bool is_data, double time, int mipmap)
371 {
372         int gputt;
373         /* this binds a texture, so that's why to restore it to 0 */
374         GLint bindcode = GPU_verify_image(ima, iuser, textarget, 0, 0, mipmap, is_data);
375         GPU_update_image_time(ima, time);
376
377         if (textarget == GL_TEXTURE_2D)
378                 gputt = TEXTARGET_TEXTURE_2D;
379         else
380                 gputt = TEXTARGET_TEXTURE_CUBE_MAP;
381
382         if (ima->gputexture[gputt]) {
383                 ima->gputexture[gputt]->bindcode = bindcode;
384                 glBindTexture(textarget, 0);
385                 return ima->gputexture[gputt];
386         }
387
388         GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture");
389         tex->bindcode = bindcode;
390         tex->number = -1;
391         tex->refcount = 1;
392         tex->target = textarget;
393         tex->target_base = GL_TEXTURE_2D;
394         tex->fromblender = 1;
395
396         ima->gputexture[gputt] = tex;
397
398         if (!glIsTexture(tex->bindcode)) {
399                 GPU_ASSERT_NO_GL_ERRORS("Blender Texture Not Loaded");
400         }
401         else {
402                 GLint w, h, border;
403
404                 GLenum gettarget;
405
406                 if (textarget == GL_TEXTURE_2D)
407                         gettarget = GL_TEXTURE_2D;
408                 else
409                         gettarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
410
411                 glBindTexture(textarget, tex->bindcode);
412                 glGetTexLevelParameteriv(gettarget, 0, GL_TEXTURE_WIDTH, &w);
413                 glGetTexLevelParameteriv(gettarget, 0, GL_TEXTURE_HEIGHT, &h);
414                 glGetTexLevelParameteriv(gettarget, 0, GL_TEXTURE_BORDER, &border);
415
416                 tex->w = w - border;
417                 tex->h = h - border;
418         }
419
420         glBindTexture(textarget, 0);
421
422         return tex;
423 }
424
425 GPUTexture *GPU_texture_from_preview(PreviewImage *prv, int mipmap)
426 {
427         GPUTexture *tex = prv->gputexture[0];
428         GLuint bindcode = 0;
429         
430         if (tex)
431                 bindcode = tex->bindcode;
432         
433         /* this binds a texture, so that's why we restore it to 0 */
434         if (bindcode == 0) {
435                 GPU_create_gl_tex(&bindcode, prv->rect[0], NULL, prv->w[0], prv->h[0], GL_TEXTURE_2D, mipmap, 0, NULL);
436         }
437         if (tex) {
438                 tex->bindcode = bindcode;
439                 glBindTexture(GL_TEXTURE_2D, 0);
440                 return tex;
441         }
442
443         tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture");
444         tex->bindcode = bindcode;
445         tex->number = -1;
446         tex->refcount = 1;
447         tex->target = GL_TEXTURE_2D;
448         tex->target_base = GL_TEXTURE_2D;
449         
450         prv->gputexture[0] = tex;
451         
452         if (!glIsTexture(tex->bindcode)) {
453                 GPU_ASSERT_NO_GL_ERRORS("Blender Texture Not Loaded");
454         }
455         else {
456                 GLint w, h;
457
458                 glBindTexture(GL_TEXTURE_2D, tex->bindcode);
459                 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
460                 glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
461                 
462                 tex->w = w;
463                 tex->h = h;
464         }
465         
466         glBindTexture(GL_TEXTURE_2D, 0);
467         
468         return tex;
469
470 }
471
472 GPUTexture *GPU_texture_create_1D(int w, const float *fpixels, char err_out[256])
473 {
474         GPUTexture *tex = GPU_texture_create_nD(w, 1, 1, fpixels, 0, GPU_HDR_NONE, 4, 0, err_out);
475
476         if (tex)
477                 GPU_texture_unbind(tex);
478         
479         return tex;
480 }
481
482 GPUTexture *GPU_texture_create_2D(int w, int h, const float *fpixels, GPUHDRType hdr, char err_out[256])
483 {
484         GPUTexture *tex = GPU_texture_create_nD(w, h, 2, fpixels, 0, hdr, 4, 0, err_out);
485
486         if (tex)
487                 GPU_texture_unbind(tex);
488         
489         return tex;
490 }
491 GPUTexture *GPU_texture_create_2D_multisample(
492         int w, int h, const float *fpixels, GPUHDRType hdr, int samples, char err_out[256])
493 {
494         GPUTexture *tex = GPU_texture_create_nD(w, h, 2, fpixels, 0, hdr, 4, samples, err_out);
495
496         if (tex)
497                 GPU_texture_unbind(tex);
498
499         return tex;
500 }
501
502 GPUTexture *GPU_texture_create_depth(int w, int h, char err_out[256])
503 {
504         GPUTexture *tex = GPU_texture_create_nD(w, h, 2, NULL, 1, GPU_HDR_NONE, 1, 0, err_out);
505
506         if (tex)
507                 GPU_texture_unbind(tex);
508         
509         return tex;
510 }
511 GPUTexture *GPU_texture_create_depth_multisample(int w, int h, int samples, char err_out[256])
512 {
513         GPUTexture *tex = GPU_texture_create_nD(w, h, 2, NULL, 1, GPU_HDR_NONE, 1, samples, err_out);
514
515         if (tex)
516                 GPU_texture_unbind(tex);
517
518         return tex;
519 }
520
521 /**
522  * A shadow map for VSM needs two components (depth and depth^2)
523  */
524 GPUTexture *GPU_texture_create_vsm_shadow_map(int size, char err_out[256])
525 {
526         GPUTexture *tex = GPU_texture_create_nD(size, size, 2, NULL, 0, GPU_HDR_FULL_FLOAT, 2, 0, err_out);
527
528         if (tex) {
529                 /* Now we tweak some of the settings */
530                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
531                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
532
533                 GPU_texture_unbind(tex);
534         }
535
536         return tex;
537 }
538
539 GPUTexture *GPU_texture_create_2D_procedural(int w, int h, const float *pixels, bool repeat, char err_out[256])
540 {
541         GPUTexture *tex = GPU_texture_create_nD(w, h, 2, pixels, 0, GPU_HDR_HALF_FLOAT, 2, 0, err_out);
542
543         if (tex) {
544                 /* Now we tweak some of the settings */
545                 if (repeat) {
546                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
547                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
548                 }
549                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
550                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
551
552                 GPU_texture_unbind(tex);
553         }
554
555         return tex;
556 }
557
558 GPUTexture *GPU_texture_create_1D_procedural(int w, const float *pixels, char err_out[256])
559 {
560         GPUTexture *tex = GPU_texture_create_nD(w, 0, 1, pixels, 0, GPU_HDR_HALF_FLOAT, 2, 0, err_out);
561
562         if (tex) {
563                 /* Now we tweak some of the settings */
564                 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
565                 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
566                 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
567
568                 GPU_texture_unbind(tex);
569         }
570
571         return tex;
572 }
573
574 void GPU_invalid_tex_init(void)
575 {
576         const float color[4] = {1.0f, 0.0f, 1.0f, 1.0f};
577         GG.invalid_tex_1D = GPU_texture_create_1D(1, color, NULL);
578         GG.invalid_tex_2D = GPU_texture_create_2D(1, 1, color, GPU_HDR_NONE, NULL);
579         GG.invalid_tex_3D = GPU_texture_create_3D(1, 1, 1, 4, color);
580 }
581
582 void GPU_invalid_tex_bind(int mode)
583 {
584         switch (mode) {
585                 case GL_TEXTURE_1D:
586                         glBindTexture(GL_TEXTURE_1D, GG.invalid_tex_1D->bindcode);
587                         break;
588                 case GL_TEXTURE_2D:
589                         glBindTexture(GL_TEXTURE_2D, GG.invalid_tex_2D->bindcode);
590                         break;
591                 case GL_TEXTURE_3D:
592                         glBindTexture(GL_TEXTURE_3D, GG.invalid_tex_3D->bindcode);
593                         break;
594         }
595 }
596
597 void GPU_invalid_tex_free(void)
598 {
599         if (GG.invalid_tex_1D)
600                 GPU_texture_free(GG.invalid_tex_1D);
601         if (GG.invalid_tex_2D)
602                 GPU_texture_free(GG.invalid_tex_2D);
603         if (GG.invalid_tex_3D)
604                 GPU_texture_free(GG.invalid_tex_3D);
605 }
606
607
608 void GPU_texture_bind(GPUTexture *tex, int number)
609 {
610         if (number >= GPU_max_textures()) {
611                 fprintf(stderr, "Not enough texture slots.\n");
612                 return;
613         }
614
615         if ((G.debug & G_DEBUG)) {
616                 if (tex->fb && GPU_framebuffer_bound(tex->fb)) {
617                         fprintf(stderr, "Feedback loop warning!: Attempting to bind texture attached to current framebuffer!\n");
618                 }
619         }
620
621         if (number < 0)
622                 return;
623
624         GPU_ASSERT_NO_GL_ERRORS("Pre Texture Bind");
625
626         GLenum arbnumber = (GLenum)((GLuint)GL_TEXTURE0 + number);
627         if (number != 0) glActiveTexture(arbnumber);
628         if (tex->bindcode != 0) {
629                 glBindTexture(tex->target, tex->bindcode);
630         }
631         else
632                 GPU_invalid_tex_bind(tex->target);
633         glEnable(tex->target);
634         if (number != 0) glActiveTexture(GL_TEXTURE0);
635
636         tex->number = number;
637
638         GPU_ASSERT_NO_GL_ERRORS("Post Texture Bind");
639 }
640
641 void GPU_texture_unbind(GPUTexture *tex)
642 {
643         if (tex->number >= GPU_max_textures()) {
644                 fprintf(stderr, "Not enough texture slots.\n");
645                 return;
646         }
647
648         if (tex->number == -1)
649                 return;
650         
651         GPU_ASSERT_NO_GL_ERRORS("Pre Texture Unbind");
652
653         GLenum arbnumber = (GLenum)((GLuint)GL_TEXTURE0 + tex->number);
654         if (tex->number != 0) glActiveTexture(arbnumber);
655         glBindTexture(tex->target, 0);
656         glDisable(tex->target);
657         glBindTexture(tex->target_base, 0);
658         glDisable(tex->target_base);
659         if (tex->number != 0) glActiveTexture(GL_TEXTURE0);
660
661         tex->number = -1;
662
663         GPU_ASSERT_NO_GL_ERRORS("Post Texture Unbind");
664 }
665
666 int GPU_texture_bound_number(GPUTexture *tex)
667 {
668         return tex->number;
669 }
670
671 void GPU_texture_filter_mode(GPUTexture *tex, bool compare, bool use_filter)
672 {
673         if (tex->number >= GPU_max_textures()) {
674                 fprintf(stderr, "Not enough texture slots.\n");
675                 return;
676         }
677
678         if (tex->number == -1)
679                 return;
680
681         GPU_ASSERT_NO_GL_ERRORS("Pre Texture Unbind");
682
683         GLenum arbnumber = (GLenum)((GLuint)GL_TEXTURE0 + tex->number);
684         if (tex->number != 0) glActiveTexture(arbnumber);
685
686         if (tex->depth) {
687                 if (compare)
688                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
689                 else
690                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
691         }
692
693         if (use_filter) {
694                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
695                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
696         }
697         else {
698                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
699                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
700         }
701         if (tex->number != 0) glActiveTexture(GL_TEXTURE0);
702
703         GPU_ASSERT_NO_GL_ERRORS("Post Texture Unbind");
704 }
705
706 void GPU_texture_free(GPUTexture *tex)
707 {
708         tex->refcount--;
709
710         if (tex->refcount < 0)
711                 fprintf(stderr, "GPUTexture: negative refcount\n");
712         
713         if (tex->refcount == 0) {
714                 if (tex->fb)
715                         GPU_framebuffer_texture_detach(tex);
716                 if (tex->bindcode && !tex->fromblender)
717                         glDeleteTextures(1, &tex->bindcode);
718
719                 MEM_freeN(tex);
720         }
721 }
722
723 void GPU_texture_ref(GPUTexture *tex)
724 {
725         tex->refcount++;
726 }
727
728 int GPU_texture_target(const GPUTexture *tex)
729 {
730         return tex->target;
731 }
732
733 int GPU_texture_width(const GPUTexture *tex)
734 {
735         return tex->w;
736 }
737
738 int GPU_texture_height(const GPUTexture *tex)
739 {
740         return tex->h;
741 }
742
743 int GPU_texture_depth(const GPUTexture *tex)
744 {
745         return tex->depth;
746 }
747
748 int GPU_texture_opengl_bindcode(const GPUTexture *tex)
749 {
750         return tex->bindcode;
751 }
752
753 GPUFrameBuffer *GPU_texture_framebuffer(GPUTexture *tex)
754 {
755         return tex->fb;
756 }
757
758 int GPU_texture_framebuffer_attachment(GPUTexture *tex)
759 {
760         return tex->fb_attachment;
761 }
762
763 void GPU_texture_framebuffer_set(GPUTexture *tex, GPUFrameBuffer *fb, int attachment)
764 {
765         tex->fb = fb;
766         tex->fb_attachment = attachment;
767 }
768