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