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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2005 Blender Foundation.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): Brecht Van Lommel.
*
* ***** END GPL LICENSE BLOCK *****
*/
#include "MEM_guardedalloc.h"
#include "DNA_image_types.h"
#include "BLI_blenlib.h"
#include "BLI_utildefines.h"
#include "BLI_math_base.h"
#include "BKE_global.h"
#include "GPU_debug.h"
#include "GPU_draw.h"
#include "GPU_extensions.h"
#include "GPU_framebuffer.h"
#include "GPU_glew.h"
#include "GPU_texture.h"
static struct GPUTextureGlobal {
GPUTexture *invalid_tex_1D; /* texture used in place of invalid textures (not loaded correctly, missing) */
GPUTexture *invalid_tex_2D;
GPUTexture *invalid_tex_3D;
} GG = {NULL, NULL, NULL};
/* GPUTexture */
struct GPUTexture {
int w, h, d; /* width/height/depth */
int number; /* number for multitexture binding */
int refcount; /* reference count */
GLenum target; /* GL_TEXTURE_* */
GLenum target_base; /* same as target, (but no multisample)
* use it for unbinding */
GLuint bindcode; /* opengl identifier for texture */
int fromblender; /* we got the texture from Blender */
GPUFrameBuffer *fb; /* GPUFramebuffer this texture is attached to */
int fb_attachment; /* slot the texture is attached to */
bool depth; /* is a depth texture? */
bool stencil; /* is a stencil texture? */
};
static GLenum gpu_texture_get_format(
int components, GPUTextureFormat data_type,
GLenum *format, GLenum *data_format, bool *is_depth, bool *is_stencil)
{
if (data_type == GPU_DEPTH_COMPONENT24 ||
data_type == GPU_DEPTH_COMPONENT16 ||
data_type == GPU_DEPTH_COMPONENT32F)
{
*is_depth = true;
*is_stencil = false;
*data_format = GL_FLOAT;
*format = GL_DEPTH_COMPONENT;
}
else if (data_type == GPU_DEPTH24_STENCIL8) {
*is_depth = true;
*is_stencil = true;
*data_format = GL_UNSIGNED_INT_24_8;
*format = GL_DEPTH_STENCIL;
}
else {
*is_depth = false;
*is_stencil = false;
*data_format = GL_FLOAT;
switch (components) {
case 1: *format = GL_RED; break;
case 2: *format = GL_RG; break;
case 3: *format = GL_RGB; break;
case 4: *format = GL_RGBA; break;
default: break;
}
}
/* You can add any of the available type to this list
* For available types see GPU_texture.h */
switch (data_type) {
/* Formats texture & renderbuffer */
case GPU_RGBA16F: return GL_RGBA16F;
case GPU_RG32F: return GL_RG32F;
case GPU_RGB16F: return GL_RGB16F;
case GPU_RG16F: return GL_RG16F;
case GPU_RGBA8: return GL_RGBA8;
case GPU_R16F: return GL_R16F;
case GPU_R8: return GL_R8;
/* Special formats texture & renderbuffer */
case GPU_DEPTH24_STENCIL8: return GL_DEPTH24_STENCIL8;
/* Texture only format */
/* ** Add Format here **/
/* Special formats texture only */
/* ** Add Format here **/
/* Depth Formats */
case GPU_DEPTH_COMPONENT32F: return GL_DEPTH_COMPONENT32F;
case GPU_DEPTH_COMPONENT24: return GL_DEPTH_COMPONENT24;
case GPU_DEPTH_COMPONENT16: return GL_DEPTH_COMPONENT16;
default:
fprintf(stderr, "Texture format incorrect or unsupported\n");
return 0;
}
}
static float *GPU_texture_3D_rescale(GPUTexture *tex, int w, int h, int d, int channels, const float *fpixels)
{
const unsigned int xf = w / tex->w, yf = h / tex->h, zf = d / tex->d;
float *nfpixels = MEM_mallocN(channels * sizeof(float) * tex->w * tex->h * tex->d, "GPUTexture Rescaled 3Dtex");
if (nfpixels) {
GPU_print_error_debug("You need to scale a 3D texture, feel the pain!");
for (unsigned k = 0; k < tex->d; k++) {
for (unsigned j = 0; j < tex->h; j++) {
for (unsigned i = 0; i < tex->w; i++) {
/* obviously doing nearest filtering here,
* it's going to be slow in any case, let's not make it worse */
float xb = i * xf;
float yb = j * yf;
float zb = k * zf;
unsigned int offset = k * (tex->w * tex->h) + i * tex->h + j;
unsigned int offset_orig = (zb) * (w * h) + (xb) * h + (yb);
if (channels == 4) {
nfpixels[offset * 4] = fpixels[offset_orig * 4];
nfpixels[offset * 4 + 1] = fpixels[offset_orig * 4 + 1];
nfpixels[offset * 4 + 2] = fpixels[offset_orig * 4 + 2];
nfpixels[offset * 4 + 3] = fpixels[offset_orig * 4 + 3];
}
else
nfpixels[offset] = fpixels[offset_orig];
}
}
}
}
return nfpixels;
}
/* This tries to allocate video memory for a given texture
* If alloc fails, lower the resolution until it fits. */
static bool gpu_texture_try_alloc(
GPUTexture *tex, GLenum proxy, GLenum internalformat, GLenum format, GLenum data_format,
int channels, bool try_rescale, const float *fpixels, float **rescaled_fpixels)
{
int r_width;
switch (proxy) {
case GL_PROXY_TEXTURE_1D:
glTexImage1D(proxy, 0, internalformat, tex->w, 0, format, data_format, NULL);
break;
case GL_PROXY_TEXTURE_1D_ARRAY:
case GL_PROXY_TEXTURE_2D:
glTexImage2D(proxy, 0, internalformat, tex->w, tex->h, 0, format, data_format, NULL);
break;
case GL_PROXY_TEXTURE_2D_ARRAY:
case GL_PROXY_TEXTURE_3D:
glTexImage3D(proxy, 0, internalformat, tex->w, tex->h, tex->d, 0, format, data_format, NULL);
break;
}
glGetTexLevelParameteriv(proxy, 0, GL_TEXTURE_WIDTH, &r_width);
if (r_width == 0 && try_rescale) {
const int w = tex->w, h = tex->h, d = tex->d;
/* Find largest texture possible */
while (r_width == 0) {
tex->w /= 2;
tex->h /= 2;
tex->d /= 2;
/* really unlikely to happen but keep this just in case */
if (tex->w == 0) break;
if (tex->h == 0 && proxy != GL_PROXY_TEXTURE_1D) break;
if (tex->d == 0 && proxy == GL_PROXY_TEXTURE_3D) break;
if (proxy == GL_PROXY_TEXTURE_1D)
glTexImage1D(proxy, 0, internalformat, tex->w, 0, format, data_format, NULL);
else if (proxy == GL_PROXY_TEXTURE_2D)
glTexImage2D(proxy, 0, internalformat, tex->w, tex->h, 0, format, data_format, NULL);
else if (proxy == GL_PROXY_TEXTURE_3D)
glTexImage3D(proxy, 0, internalformat, tex->w, tex->h, tex->d, 0, format, data_format, NULL);
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &r_width);
}
/* Rescale */
if (r_width > 0) {
switch (proxy) {
case GL_PROXY_TEXTURE_1D:
case GL_PROXY_TEXTURE_2D:
/* Do nothing for now */
return false;
case GL_PROXY_TEXTURE_3D:
*rescaled_fpixels = GPU_texture_3D_rescale(tex, w, h, d, channels, fpixels);
return (bool)*rescaled_fpixels;
}
}
}
return (r_width > 0);
}
static GPUTexture *GPU_texture_create_nD(
int w, int h, int d, int n, const float *fpixels,
GPUTextureFormat data_type, int components, int samples,
const bool can_rescale, char err_out[256])
{
if (samples) {
CLAMP_MAX(samples, GPU_max_color_texture_samples());
}
GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture");
tex->w = w;
tex->h = h;
tex->d = d;
tex->number = -1;
tex->refcount = 1;
tex->fb_attachment = -1;
if (n == 2) {
if (d == 0)
tex->target_base = tex->target = GL_TEXTURE_2D;
else
tex->target_base = tex->target = GL_TEXTURE_2D_ARRAY;
}
else if (n == 1) {
if (h == 0)
tex->target_base = tex->target = GL_TEXTURE_1D;
else
tex->target_base = tex->target = GL_TEXTURE_1D_ARRAY;
}
else if (n == 3) {
tex->target_base = tex->target = GL_TEXTURE_3D;
}
else {
/* should never happen */
MEM_freeN(tex);
return NULL;
}
if (samples && n == 2 && d == 0)
tex->target = GL_TEXTURE_2D_MULTISAMPLE;
GLenum format, internalformat, data_format;
internalformat = gpu_texture_get_format(components, data_type, &format, &data_format, &tex->depth, &tex->stencil);
/* Generate Texture object */
glGenTextures(1, &tex->bindcode);
if (!tex->bindcode) {
if (err_out)
BLI_snprintf(err_out, 256, "GPUTexture: texture create failed");
else
fprintf(stderr, "GPUTexture: texture create failed");
GPU_texture_free(tex);
return NULL;
}
tex->number = 0;
glBindTexture(tex->target, tex->bindcode);
/* Check if texture fit in VRAM */
GLenum proxy = GL_PROXY_TEXTURE_2D;
if (n == 2) {
if (d > 0)
proxy = GL_PROXY_TEXTURE_2D_ARRAY;
}
else if (n == 1) {
if (h == 0)
proxy = GL_PROXY_TEXTURE_1D;
else
proxy = GL_PROXY_TEXTURE_1D_ARRAY;
}
else if (n == 3) {
proxy = GL_PROXY_TEXTURE_3D;
}
float *rescaled_fpixels = NULL;
bool valid = gpu_texture_try_alloc(tex, proxy, internalformat, format, data_format, components, can_rescale,
fpixels, &rescaled_fpixels);
if (!valid) {
if (err_out)
BLI_snprintf(err_out, 256, "GPUTexture: texture alloc failed");
else
fprintf(stderr, "GPUTexture: texture alloc failed. Not enough Video Memory.");
GPU_texture_free(tex);
return NULL;
}
/* Upload Texture */
const float *pix = (rescaled_fpixels) ? rescaled_fpixels : fpixels;
if (tex->target == GL_TEXTURE_2D ||
tex->target == GL_TEXTURE_2D_MULTISAMPLE ||
tex->target == GL_TEXTURE_1D_ARRAY)
{
if (samples) {
glTexImage2DMultisample(tex->target, samples, internalformat, tex->w, tex->h, true);
if (pix)
glTexSubImage2D(tex->target, 0, 0, 0, tex->w, tex->h, format, data_format, pix);
}
else {
glTexImage2D(tex->target, 0, internalformat, tex->w, tex->h, 0, format, data_format, pix);
}
}
else if (tex->target == GL_TEXTURE_1D) {
glTexImage1D(tex->target, 0, internalformat, tex->w, 0, format, data_format, pix);
}
else {
glTexImage3D(tex->target, 0, internalformat, tex->w, tex->h, tex->d, 0, format, data_format, pix);
}
if (rescaled_fpixels)
MEM_freeN(rescaled_fpixels);
/* Texture Parameters */
if (tex->depth) {
glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
#ifdef WITH_LEGACY_OPENGL
glTexParameteri(tex->target_base, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
#endif
}
else {
glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
if (n > 1) {
glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
if (n > 2) {
glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
GPU_texture_unbind(tex);
return tex;
}
static GPUTexture *GPU_texture_cube_create(
int w, int d,
const float *fpixels_px, const float *fpixels_py, const float *fpixels_pz,
const float *fpixels_nx, const float *fpixels_ny, const float *fpixels_nz,
GPUTextureFormat data_type, int components,
char err_out[256])
{
GLenum format, internalformat, data_format;
GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture");
tex->w = w;
tex->h = w;
tex->d = d;
tex->number = -1;
tex->refcount = 1;
tex->fb_attachment = -1;
if (d == 0) {
tex->target_base = tex->target = GL_TEXTURE_CUBE_MAP;
}
else {
BLI_assert(false && "Cubemap array Not implemented yet");
// tex->target_base = tex->target = GL_TEXTURE_CUBE_MAP_ARRAY;
}
internalformat = gpu_texture_get_format(components, data_type, &format, &data_format, &tex->depth, &tex->stencil);
/* Generate Texture object */
glGenTextures(1, &tex->bindcode);
if (!tex->bindcode) {
if (err_out)
BLI_snprintf(err_out, 256, "GPUTexture: texture create failed");
else
fprintf(stderr, "GPUTexture: texture create failed");
GPU_texture_free(tex);
return NULL;
}
tex->number = 0;
glBindTexture(tex->target, tex->bindcode);
/* Upload Texture */
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, internalformat, tex->w, tex->h, 0, format, data_format, fpixels_px);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, internalformat, tex->w, tex->h, 0, format, data_format, fpixels_py);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, internalformat, tex->w, tex->h, 0, format, data_format, fpixels_pz);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, internalformat, tex->w, tex->h, 0, format, data_format, fpixels_nx);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, internalformat, tex->w, tex->h, 0, format, data_format, fpixels_ny);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, internalformat, tex->w, tex->h, 0, format, data_format, fpixels_nz);
/* Texture Parameters */
if (tex->depth) {
glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(tex->target_base, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
}
else {
glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
GPU_texture_unbind(tex);
return tex;
}
GPUTexture *GPU_texture_from_blender(Image *ima, ImageUser *iuser, int textarget, bool is_data, double time, int mipmap)
{
int gputt;
/* this binds a texture, so that's why to restore it to 0 */
GLint bindcode = GPU_verify_image(ima, iuser, textarget, 0, 0, mipmap, is_data);
GPU_update_image_time(ima, time);
/* see GPUInput::textarget: it can take two values - GL_TEXTURE_2D and GL_TEXTURE_CUBE_MAP
* these values are correct for glDisable, so textarget can be safely used in
* GPU_texture_bind/GPU_texture_unbind through tex->target_base */
/* (is any of this obsolete now that we don't glEnable/Disable textures?) */
if (textarget == GL_TEXTURE_2D)
gputt = TEXTARGET_TEXTURE_2D;
else
gputt = TEXTARGET_TEXTURE_CUBE_MAP;
if (ima->gputexture[gputt]) {
ima->gputexture[gputt]->bindcode = bindcode;
glBindTexture(textarget, 0);
return ima->gputexture[gputt];
}
GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture");
tex->bindcode = bindcode;
tex->number = -1;
tex->refcount = 1;
tex->target = textarget;
tex->target_base = textarget;
tex->fromblender = 1;
ima->gputexture[gputt] = tex;
if (!glIsTexture(tex->bindcode)) {
GPU_print_error_debug("Blender Texture Not Loaded");
}
else {
GLint w, h, border;
GLenum gettarget;
if (textarget == GL_TEXTURE_2D)
gettarget = GL_TEXTURE_2D;
else
gettarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X;
glBindTexture(textarget, tex->bindcode);
glGetTexLevelParameteriv(gettarget, 0, GL_TEXTURE_WIDTH, &w);
glGetTexLevelParameteriv(gettarget, 0, GL_TEXTURE_HEIGHT, &h);
glGetTexLevelParameteriv(gettarget, 0, GL_TEXTURE_BORDER, &border);
tex->w = w - border;
tex->h = h - border;
}
glBindTexture(textarget, 0);
return tex;
}
GPUTexture *GPU_texture_from_preview(PreviewImage *prv, int mipmap)
{
GPUTexture *tex = prv->gputexture[0];
GLuint bindcode = 0;
if (tex)
bindcode = tex->bindcode;
/* this binds a texture, so that's why we restore it to 0 */
if (bindcode == 0) {
GPU_create_gl_tex(&bindcode, prv->rect[0], NULL, prv->w[0], prv->h[0], GL_TEXTURE_2D, mipmap, 0, NULL);
}
if (tex) {
tex->bindcode = bindcode;
glBindTexture(GL_TEXTURE_2D, 0);
return tex;
}
tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture");
tex->bindcode = bindcode;
tex->number = -1;
tex->refcount = 1;
tex->target = GL_TEXTURE_2D;
tex->target_base = GL_TEXTURE_2D;
prv->gputexture[0] = tex;
if (!glIsTexture(tex->bindcode)) {
GPU_print_error_debug("Blender Texture Not Loaded");
}
else {
GLint w, h;
glBindTexture(GL_TEXTURE_2D, tex->bindcode);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
tex->w = w;
tex->h = h;
}
glBindTexture(GL_TEXTURE_2D, 0);
return tex;
}
GPUTexture *GPU_texture_create_1D(int w, const float *pixels, char err_out[256])
{
return GPU_texture_create_nD(w, 0, 0, 1, pixels, GPU_RGBA8, 4, 0, false, err_out);
}
GPUTexture *GPU_texture_create_1D_custom(
int w, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256])
{
return GPU_texture_create_nD(w, 0, 0, 1, pixels, data_type, channels, 0, false, err_out);
}
GPUTexture *GPU_texture_create_2D(int w, int h, const float *pixels, char err_out[256])
{
return GPU_texture_create_nD(w, h, 0, 2, pixels, GPU_RGBA8, 4, 0, false, err_out);
}
GPUTexture *GPU_texture_create_2D_custom(
int w, int h, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256])
{
return GPU_texture_create_nD(w, h, 0, 2, pixels, data_type, channels, 0, false, err_out);
}
GPUTexture *GPU_texture_create_2D_multisample(int w, int h, const float *pixels, int samples, char err_out[256])
{
return GPU_texture_create_nD(w, h, 0, 2, pixels, GPU_RGBA8, 4, samples, false, err_out);
}
GPUTexture *GPU_texture_create_2D_array_custom(int w, int h, int d, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256])
{
return GPU_texture_create_nD(w, h, d, 2, pixels, data_type, channels, 0, false, err_out);
}
GPUTexture *GPU_texture_create_3D(int w, int h, int d, const float *pixels, char err_out[256])
{
return GPU_texture_create_nD(w, h, d, 3, pixels, GPU_RGBA8, 4, 0, true, err_out);
}
GPUTexture *GPU_texture_create_3D_custom(int w, int h, int d, int channels, GPUTextureFormat data_type, const float *pixels, char err_out[256])
{
return GPU_texture_create_nD(w, h, d, 3, pixels, data_type, channels, 0, true, err_out);
}
GPUTexture *GPU_texture_create_cube_custom(int w, int channels, GPUTextureFormat data_type, const float *fpixels, char err_out[256])
{
const float *fpixels_px, *fpixels_py, *fpixels_pz, *fpixels_nx, *fpixels_ny, *fpixels_nz;
if (fpixels) {
fpixels_px = fpixels + 0 * w * w * channels;
fpixels_nx = fpixels + 1 * w * w * channels;
fpixels_py = fpixels + 2 * w * w * channels;
fpixels_ny = fpixels + 3 * w * w * channels;
fpixels_pz = fpixels + 4 * w * w * channels;
fpixels_nz = fpixels + 5 * w * w * channels;
}
else {
fpixels_px = fpixels_py = fpixels_pz = fpixels_nx = fpixels_ny = fpixels_nz = NULL;
}
return GPU_texture_cube_create(w, 0, fpixels_px, fpixels_py, fpixels_pz, fpixels_nx, fpixels_ny, fpixels_nz, data_type, channels, err_out);
}
GPUTexture *GPU_texture_create_depth(int w, int h, char err_out[256])
{
return GPU_texture_create_nD(w, h, 0, 2, NULL, GPU_DEPTH_COMPONENT24, 1, 0, false, err_out);
}
GPUTexture *GPU_texture_create_depth_with_stencil(int w, int h, char err_out[256])
{
return GPU_texture_create_nD(w, h, 0, 2, NULL, GPU_DEPTH24_STENCIL8, 1, 0, false, err_out);
}
GPUTexture *GPU_texture_create_depth_multisample(int w, int h, int samples, char err_out[256])
{
return GPU_texture_create_nD(w, h, 0, 2, NULL, GPU_DEPTH_COMPONENT24, 1, samples, false, err_out);
}
void GPU_invalid_tex_init(void)
{
const float color[4] = {1.0f, 0.0f, 1.0f, 1.0f};
GG.invalid_tex_1D = GPU_texture_create_1D(1, color, NULL);
GG.invalid_tex_2D = GPU_texture_create_2D(1, 1, color, NULL);
GG.invalid_tex_3D = GPU_texture_create_3D(1, 1, 1, color, NULL);
}
void GPU_invalid_tex_bind(int mode)
{
switch (mode) {
case GL_TEXTURE_1D:
glBindTexture(GL_TEXTURE_1D, GG.invalid_tex_1D->bindcode);
break;
case GL_TEXTURE_2D:
glBindTexture(GL_TEXTURE_2D, GG.invalid_tex_2D->bindcode);
break;
case GL_TEXTURE_3D:
glBindTexture(GL_TEXTURE_3D, GG.invalid_tex_3D->bindcode);
break;
}
}
void GPU_invalid_tex_free(void)
{
if (GG.invalid_tex_1D)
GPU_texture_free(GG.invalid_tex_1D);
if (GG.invalid_tex_2D)
GPU_texture_free(GG.invalid_tex_2D);
if (GG.invalid_tex_3D)
GPU_texture_free(GG.invalid_tex_3D);
}
void GPU_texture_bind(GPUTexture *tex, int number)
{
if (number >= GPU_max_textures()) {
fprintf(stderr, "Not enough texture slots.\n");
return;
}
if ((G.debug & G_DEBUG)) {
if (tex->fb && GPU_framebuffer_bound(tex->fb)) {
fprintf(stderr, "Feedback loop warning!: Attempting to bind texture attached to current framebuffer!\n");
}
}
if (number < 0)
return;
if (number != 0)
glActiveTexture(GL_TEXTURE0 + number);
if (tex->bindcode != 0)
glBindTexture(tex->target_base, tex->bindcode);
else
GPU_invalid_tex_bind(tex->target_base);
if (number != 0)
glActiveTexture(GL_TEXTURE0);
tex->number = number;
}
void GPU_texture_unbind(GPUTexture *tex)
{
if (tex->number >= GPU_max_textures()) {
fprintf(stderr, "Not enough texture slots.\n");
return;
}
if (tex->number == -1)
return;
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0 + tex->number);
glBindTexture(tex->target_base, 0);
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0);
tex->number = -1;
}
int GPU_texture_bound_number(GPUTexture *tex)
{
return tex->number;
}
void GPU_texture_generate_mipmap(GPUTexture *tex)
{
if (tex->number >= GPU_max_textures()) {
fprintf(stderr, "Not enough texture slots.\n");
return;
}
if (tex->number == -1)
return;
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0 + tex->number);
glGenerateMipmap(tex->target_base);
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0);
}
void GPU_texture_compare_mode(GPUTexture *tex, bool use_compare)
{
if (tex->number >= GPU_max_textures()) {
fprintf(stderr, "Not enough texture slots.\n");
return;
}
if (tex->number == -1)
return;
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0 + tex->number);
/* TODO viewport: use GL_COMPARE_REF_TO_TEXTURE after we switch to core profile */
if (tex->depth)
glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_MODE, use_compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE);
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0);
}
void GPU_texture_filter_mode(GPUTexture *tex, bool use_filter)
{
if (tex->number >= GPU_max_textures()) {
fprintf(stderr, "Not enough texture slots.\n");
return;
}
if (tex->number == -1)
return;
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0 + tex->number);
GLenum filter = use_filter ? GL_LINEAR : GL_NEAREST;
glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, filter);
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0);
}
void GPU_texture_mipmap_mode(GPUTexture *tex, bool use_mipmap)
{
if (tex->number >= GPU_max_textures()) {
fprintf(stderr, "Not enough texture slots.\n");
return;
}
if (tex->number == -1)
return;
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0 + tex->number);
GLenum mipmap = use_mipmap ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR;
glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, mipmap);
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0);
}
void GPU_texture_wrap_mode(GPUTexture *tex, bool use_repeat)
{
if (tex->number >= GPU_max_textures()) {
fprintf(stderr, "Not enough texture slots.\n");
return;
}
if (tex->number == -1)
return;
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0 + tex->number);
GLenum repeat = use_repeat ? GL_REPEAT : GL_CLAMP_TO_EDGE;
glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_S, repeat);
if (tex->target_base != GL_TEXTURE_1D)
glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_T, repeat);
if (tex->target_base == GL_TEXTURE_3D)
glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_R, repeat);
if (tex->number != 0)
glActiveTexture(GL_TEXTURE0);
}
void GPU_texture_free(GPUTexture *tex)
{
tex->refcount--;
if (tex->refcount < 0)
fprintf(stderr, "GPUTexture: negative refcount\n");
if (tex->refcount == 0) {
if (tex->fb)
GPU_framebuffer_texture_detach(tex);
if (tex->bindcode && !tex->fromblender)
glDeleteTextures(1, &tex->bindcode);
MEM_freeN(tex);
}
}
void GPU_texture_ref(GPUTexture *tex)
{
tex->refcount++;
}
int GPU_texture_target(const GPUTexture *tex)
{
return tex->target;
}
int GPU_texture_width(const GPUTexture *tex)
{
return tex->w;
}
int GPU_texture_height(const GPUTexture *tex)
{
return tex->h;
}
bool GPU_texture_depth(const GPUTexture *tex)
{
return tex->depth;
}
bool GPU_texture_stencil(const GPUTexture *tex)
{
return tex->stencil;
}
int GPU_texture_opengl_bindcode(const GPUTexture *tex)
{
return tex->bindcode;
}
GPUFrameBuffer *GPU_texture_framebuffer(GPUTexture *tex)
{
return tex->fb;
}
int GPU_texture_framebuffer_attachment(GPUTexture *tex)
{
return tex->fb_attachment;
}
void GPU_texture_framebuffer_set(GPUTexture *tex, GPUFrameBuffer *fb, int attachment)
{
tex->fb = fb;
tex->fb_attachment = attachment;
}
|