-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
vtkTextureObject.cxx
2278 lines (1930 loc) · 68.7 KB
/
vtkTextureObject.cxx
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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkTextureObject.h"
#include "vtk_glew.h"
#include "vtkObjectFactory.h"
#include "vtkNew.h"
#include "vtkOpenGLBufferObject.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLFramebufferObject.h"
#include "vtkOpenGLRenderUtilities.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLResourceFreeCallback.h"
#include "vtkOpenGLShaderCache.h"
#include "vtkOpenGLState.h"
#include "vtkOpenGLTexture.h"
#include "vtkOpenGLVertexArrayObject.h"
#include "vtkPixelBufferObject.h"
#include "vtkRenderer.h"
#include "vtkShaderProgram.h"
#include "vtkOpenGLHelper.h"
#include <cassert>
// #define VTK_TO_DEBUG
// #define VTK_TO_TIMING
#ifdef VTK_TO_TIMING
#include "vtkTimerLog.h"
#endif
#include "vtkTextureObjectFS.h"
#include "vtkTextureObjectVS.h" // a pass through shader
#define BUFFER_OFFSET(i) (reinterpret_cast<char*>(i))
// Mapping from DepthTextureCompareFunction values to OpenGL values.
//------------------------------------------------------------------------------
VTK_ABI_NAMESPACE_BEGIN
static GLint OpenGLDepthTextureCompareFunction[8] = { GL_LEQUAL, GL_GEQUAL, GL_LESS, GL_GREATER,
GL_EQUAL, GL_NOTEQUAL, GL_ALWAYS, GL_NEVER };
//------------------------------------------------------------------------------
static const char* DepthTextureCompareFunctionAsString[8] = { "Lequal", "Gequal", "Less", "Greater",
"Equal", "NotEqual", "AlwaysTrue", "Never" };
// Mapping from Wrap values to OpenGL values
#ifndef GL_ES_VERSION_3_0
//------------------------------------------------------------------------------
static GLint OpenGLWrap[4] = { GL_CLAMP_TO_EDGE, GL_REPEAT, GL_MIRRORED_REPEAT,
GL_CLAMP_TO_BORDER };
//------------------------------------------------------------------------------
static const char* WrapAsString[4] = { "ClampToEdge", "Repeat", "MirroredRepeat", "ClampToBorder" };
#else
//------------------------------------------------------------------------------
static GLint OpenGLWrap[3] = { GL_CLAMP_TO_EDGE, GL_REPEAT, GL_MIRRORED_REPEAT };
//------------------------------------------------------------------------------
static const char* WrapAsString[3] = { "ClampToEdge", "Repeat", "MirroredRepeat" };
#endif
// Mapping MinificationFilter values to OpenGL values.
//------------------------------------------------------------------------------
static GLint OpenGLMinFilter[6] = { GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST,
GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR };
// Mapping MagnificationFilter values to OpenGL values.
//------------------------------------------------------------------------------
static GLint OpenGLMagFilter[6] = { GL_NEAREST, GL_LINEAR };
//------------------------------------------------------------------------------
static const char* MinMagFilterAsString[6] = { "Nearest", "Linear", "NearestMipmapNearest",
"NearestMipmapLinear", "LinearMipmapNearest", "LinearMipmapLinear" };
//------------------------------------------------------------------------------
static GLenum OpenGLDepthInternalFormat[7] = {
GL_DEPTH_COMPONENT, // native
GL_DEPTH_COMPONENT, // fixed8
GL_DEPTH_COMPONENT16, // fixed16
#ifdef GL_DEPTH_COMPONENT24
GL_DEPTH_COMPONENT24, // fixed24
#else
GL_DEPTH_COMPONENT16,
#endif
#ifdef GL_DEPTH_COMPONENT32
GL_DEPTH_COMPONENT32, // fixed32
#else
GL_DEPTH_COMPONENT16,
#endif
#ifdef GL_DEPTH_COMPONENT32F
GL_DEPTH_COMPONENT32F, // float16
GL_DEPTH_COMPONENT32F // float32
#else
GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT16
#endif
};
//------------------------------------------------------------------------------
static GLenum OpenGLDepthInternalFormatType[7] = {
GL_UNSIGNED_INT, GL_UNSIGNED_INT, GL_UNSIGNED_INT, GL_UNSIGNED_INT, GL_UNSIGNED_INT,
#ifdef GL_DEPTH_COMPONENT32F
GL_FLOAT, GL_FLOAT
#else
GL_UNSIGNED_INT, GL_UNSIGNED_INT
#endif
};
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkTextureObject);
//------------------------------------------------------------------------------
vtkTextureObject::vtkTextureObject()
{
this->Context = nullptr;
this->Handle = 0;
this->OwnHandle = false;
this->NumberOfDimensions = 0;
this->Target = 0;
this->Components = 0;
this->Width = 0;
this->Height = 0;
this->Depth = 0;
this->Samples = 0;
this->RequireTextureInteger = false;
this->SupportsTextureInteger = false;
this->RequireTextureFloat = false;
this->SupportsTextureFloat = false;
this->RequireDepthBufferFloat = false;
this->SupportsDepthBufferFloat = false;
this->AutoParameters = 1;
this->WrapS = Repeat;
this->WrapT = Repeat;
this->WrapR = Repeat;
this->MinificationFilter = Nearest;
this->MagnificationFilter = Nearest;
this->MinLOD = -1000.0f;
this->MaxLOD = 1000.0f;
this->BaseLevel = 0;
this->MaxLevel = 0;
this->DepthTextureCompare = false;
this->DepthTextureCompareFunction = Lequal;
this->GenerateMipmap = false;
this->ShaderProgram = nullptr;
this->BorderColor[0] = 0.0f;
this->BorderColor[1] = 0.0f;
this->BorderColor[2] = 0.0f;
this->BorderColor[3] = 0.0f;
this->MaximumAnisotropicFiltering = 1.0;
this->BufferObject = nullptr;
this->UseSRGBColorSpace = false;
this->ResourceCallback = new vtkOpenGLResourceFreeCallback<vtkTextureObject>(
this, &vtkTextureObject::ReleaseGraphicsResources);
this->ResetFormatAndType();
}
//------------------------------------------------------------------------------
vtkTextureObject::~vtkTextureObject()
{
if (this->ResourceCallback)
{
this->ResourceCallback->Release();
delete this->ResourceCallback;
this->ResourceCallback = nullptr;
}
if (this->ShaderProgram)
{
delete this->ShaderProgram;
this->ShaderProgram = nullptr;
}
}
//----------------------------------------------------------------------------
bool vtkTextureObject::IsSupported(vtkOpenGLRenderWindow* vtkNotUsed(win), bool requireTexFloat,
bool requireDepthFloat, bool requireTexInt)
{
#ifdef GL_ES_VERSION_3_0
(void)requireTexFloat;
(void)requireDepthFloat;
(void)requireTexInt;
return true;
#elif defined(__APPLE__)
// Cannot trust glew on apple systems
(void)requireTexFloat;
(void)requireDepthFloat;
(void)requireTexInt;
return true;
#else
bool texFloat = true;
if (requireTexFloat)
{
texFloat =
(glewIsSupported("GL_ARB_texture_float") != 0 && glewIsSupported("GL_ARB_texture_rg") != 0);
}
bool depthFloat = true;
if (requireDepthFloat)
{
depthFloat = (glewIsSupported("GL_ARB_depth_buffer_float") != 0);
}
bool texInt = true;
if (requireTexInt)
{
texInt = (glewIsSupported("GL_EXT_texture_integer") != 0);
}
return texFloat && depthFloat && texInt;
#endif
}
//----------------------------------------------------------------------------
bool vtkTextureObject::LoadRequiredExtensions(vtkOpenGLRenderWindow* renWin)
{
#ifdef GL_ES_VERSION_3_0
this->SupportsTextureInteger = true;
this->SupportsTextureFloat = true;
this->SupportsDepthBufferFloat = true;
#elif defined(__APPLE__)
// Cannot trust glew on apple systems. OpenGL 3.2 on apple supports these features.
this->SupportsTextureInteger = true;
this->SupportsTextureFloat = true;
this->SupportsDepthBufferFloat = true;
#else
this->SupportsTextureInteger = (glewIsSupported("GL_EXT_texture_integer") != 0);
this->SupportsTextureFloat =
(glewIsSupported("GL_ARB_texture_float") != 0 && glewIsSupported("GL_ARB_texture_rg") != 0);
this->SupportsDepthBufferFloat = (glewIsSupported("GL_ARB_depth_buffer_float") != 0);
#endif
return this->IsSupported(
renWin, this->RequireTextureFloat, this->RequireDepthBufferFloat, this->RequireTextureInteger);
}
//------------------------------------------------------------------------------
void vtkTextureObject::SetContext(vtkOpenGLRenderWindow* renWin)
{
this->ResourceCallback->RegisterGraphicsResources(renWin);
// avoid pointless reassignment
if (this->Context == renWin)
{
return;
}
this->ResetFormatAndType();
this->Context = nullptr;
this->Modified();
// all done if assigned null
if (!renWin)
{
return;
}
if (!this->LoadRequiredExtensions(renWin))
{
vtkErrorMacro("Required OpenGL extensions not supported by the context.");
return;
}
// initialize
this->Context = renWin;
this->Context->MakeCurrent();
}
//------------------------------------------------------------------------------
vtkOpenGLRenderWindow* vtkTextureObject::GetContext()
{
return this->Context;
}
//------------------------------------------------------------------------------
void vtkTextureObject::DestroyTexture()
{
// deactivate it first
this->Deactivate();
// because we don't hold a reference to the render
// context we don't have any control on when it is
// destroyed. In fact it may be destroyed before
// we are(eg smart pointers), in which case we should
// do nothing.
if (this->Context && this->Handle)
{
GLuint tex = this->Handle;
glDeleteTextures(1, &tex);
vtkOpenGLCheckErrorMacro("failed at glDeleteTexture");
}
this->Handle = 0;
this->NumberOfDimensions = 0;
this->Target = 0;
this->Components = 0;
this->Width = this->Height = this->Depth = 0;
this->ResetFormatAndType();
}
void vtkTextureObject::AssignToExistingTexture(unsigned int handle, unsigned int target)
{
if (this->Handle == handle && this->Target == target)
{
return;
}
this->Handle = handle;
this->Target = target;
this->OwnHandle = false;
this->Modified();
}
//------------------------------------------------------------------------------
void vtkTextureObject::CreateTexture()
{
assert(this->Context);
this->ResourceCallback->RegisterGraphicsResources(this->Context);
// reuse the existing handle if we have one
if (!this->Handle)
{
GLuint tex = 0;
glGenTextures(1, &tex);
this->OwnHandle = true;
vtkOpenGLCheckErrorMacro("failed at glGenTextures");
this->Handle = tex;
#if defined(GL_TEXTURE_BUFFER)
if (this->Target && this->Target != GL_TEXTURE_BUFFER)
#else
if (this->Target)
#endif
{
glBindTexture(this->Target, this->Handle);
vtkOpenGLCheckErrorMacro("failed at glBindTexture");
// See: http://www.opengl.org/wiki/Common_Mistakes#Creating_a_complete_texture
// turn off mip map filter or set the base and max level correctly. here
// both are done.
#ifdef GL_TEXTURE_2D_MULTISAMPLE
if (this->Target != GL_TEXTURE_2D_MULTISAMPLE)
#endif
{
glTexParameteri(this->Target, GL_TEXTURE_MIN_FILTER,
this->GetMinificationFilterMode(this->MinificationFilter));
glTexParameteri(this->Target, GL_TEXTURE_MAG_FILTER,
this->GetMagnificationFilterMode(this->MagnificationFilter));
glTexParameteri(this->Target, GL_TEXTURE_WRAP_S, this->GetWrapSMode(this->WrapS));
glTexParameteri(this->Target, GL_TEXTURE_WRAP_T, this->GetWrapTMode(this->WrapT));
#if defined(GL_TEXTURE_3D)
if (this->Target == GL_TEXTURE_3D)
{
glTexParameteri(this->Target, GL_TEXTURE_WRAP_R, this->GetWrapRMode(this->WrapR));
}
#endif
}
if (this->Target == GL_TEXTURE_2D) // maybe expand later on
{
glTexParameteri(this->Target, GL_TEXTURE_BASE_LEVEL, this->BaseLevel);
glTexParameteri(this->Target, GL_TEXTURE_MAX_LEVEL, this->MaxLevel);
}
glBindTexture(this->Target, 0);
}
}
}
//------------------------------------------------------------------------------
int vtkTextureObject::GetTextureUnit()
{
if (this->Context)
{
return this->Context->GetTextureUnitForTexture(this);
}
return -1;
}
//------------------------------------------------------------------------------
void vtkTextureObject::Activate()
{
// activate a free texture unit for this texture
this->Context->ActivateTexture(this);
this->Bind();
}
//------------------------------------------------------------------------------
void vtkTextureObject::Deactivate()
{
if (this->Context)
{
this->Context->DeactivateTexture(this);
}
}
//------------------------------------------------------------------------------
void vtkTextureObject::ReleaseGraphicsResources(vtkWindow* win)
{
if (!this->ResourceCallback->IsReleasing())
{
this->ResourceCallback->Release();
return;
}
// Ensure that the context is current before releasing any graphics
// resources tied to it.
if (this->Handle)
{
vtkOpenGLRenderWindow* rwin = vtkOpenGLRenderWindow::SafeDownCast(win);
// you can commewnt out the next line to look for textures left active
rwin->DeactivateTexture(this);
if (this->OwnHandle)
{
GLuint tex = this->Handle;
glDeleteTextures(1, &tex);
this->OwnHandle = false;
}
this->Handle = 0;
this->NumberOfDimensions = 0;
this->Target = 0;
this->InternalFormat = 0;
this->Format = 0;
this->Type = 0;
this->Components = 0;
this->Width = this->Height = this->Depth = 0;
}
if (this->ShaderProgram)
{
this->ShaderProgram->ReleaseGraphicsResources(win);
delete this->ShaderProgram;
this->ShaderProgram = nullptr;
}
}
//------------------------------------------------------------------------------
void vtkTextureObject::Bind()
{
assert(this->Context);
assert(this->Handle);
glBindTexture(this->Target, this->Handle);
vtkOpenGLCheckErrorMacro("failed at glBindTexture");
if (this->AutoParameters && (this->GetMTime() > this->SendParametersTime))
{
this->SendParameters();
}
}
//------------------------------------------------------------------------------
bool vtkTextureObject::IsBound()
{
bool result = false;
if (this->Context && this->Handle)
{
GLenum target = 0; // to avoid warnings.
switch (this->Target)
{
#if defined(GL_TEXTURE_1D) && defined(GL_TEXTURE_BINDING_1D)
case GL_TEXTURE_1D:
target = GL_TEXTURE_BINDING_1D;
break;
#endif
case GL_TEXTURE_2D:
target = GL_TEXTURE_BINDING_2D;
break;
#if defined(GL_TEXTURE_2D_MULTISAMPLE) && defined(GL_TEXTURE_BINDING_2D_MULTISAMPLE)
case GL_TEXTURE_2D_MULTISAMPLE:
target = GL_TEXTURE_BINDING_2D_MULTISAMPLE;
break;
#endif
#if defined(GL_TEXTURE_3D) && defined(GL_TEXTURE_BINDING_3D)
case GL_TEXTURE_3D:
target = GL_TEXTURE_BINDING_3D;
break;
#endif
#if defined(GL_TEXTURE_BUFFER) && defined(GL_TEXTURE_BINDING_BUFFER)
case GL_TEXTURE_BUFFER:
target = GL_TEXTURE_BINDING_BUFFER;
break;
#endif
#if defined(GL_TEXTURE_CUBE_MAP) && defined(GL_TEXTURE_BINDING_CUBE_MAP)
case GL_TEXTURE_CUBE_MAP:
target = GL_TEXTURE_BINDING_CUBE_MAP;
break;
#endif
default:
assert("check: impossible case" && 0);
break;
}
GLint objectId;
glGetIntegerv(target, &objectId);
result = static_cast<GLuint>(objectId) == this->Handle;
}
return result;
}
//------------------------------------------------------------------------------
void vtkTextureObject::SendParameters()
{
assert("pre: is_bound" && this->IsBound());
#if defined(GL_TEXTURE_BUFFER)
if (this->Target == GL_TEXTURE_BUFFER)
{
return;
}
#endif
#ifdef GL_TEXTURE_2D_MULTISAMPLE
if (this->Target == GL_TEXTURE_2D_MULTISAMPLE)
{
return;
}
#endif
glTexParameteri(this->Target, GL_TEXTURE_WRAP_S, OpenGLWrap[this->WrapS]);
glTexParameteri(this->Target, GL_TEXTURE_WRAP_T, OpenGLWrap[this->WrapT]);
#ifdef GL_TEXTURE_WRAP_R
glTexParameteri(this->Target, GL_TEXTURE_WRAP_R, OpenGLWrap[this->WrapR]);
#endif
#ifdef __EMSCRIPTEN__
// Web browsers are over-eager in validating texture completeness.
// Even though spec says that depth textures can be filterable by treating
// them as red textures, browsers do not seem to implement it.
// This section of code ignores requests to enable linear filtering for depth textures. Otherwise,
// 1. In firefox, sampling this texture in a shader will return 0 and console throws a warning
// saying that texture is incomplete.
// 2. In chromium, this texture cannot be sampled.
// See https://groups.google.com/g/webgl-dev-list/c/T4_bKNzEhqk
if (this->Format == GL_DEPTH_COMPONENT)
{
if (this->MinificationFilter != Nearest && this->MinificationFilter != NearestMipmapNearest)
{
vtkDebugMacro(<< "Ignoring request to enable linear minification filtering for texture with "
"format=GL_DEPTH_COMPONENT");
}
else
{
glTexParameteri(
this->Target, GL_TEXTURE_MIN_FILTER, OpenGLMinFilter[this->MinificationFilter]);
}
if (this->MagnificationFilter != Nearest && this->MagnificationFilter != NearestMipmapNearest)
{
vtkDebugMacro(<< "Ignoring request to enable linear magnification filtering for texture with "
"format=GL_DEPTH_COMPONENT");
}
else
{
glTexParameteri(
this->Target, GL_TEXTURE_MAG_FILTER, OpenGLMagFilter[this->MagnificationFilter]);
}
}
else
{
glTexParameteri(this->Target, GL_TEXTURE_MIN_FILTER, OpenGLMinFilter[this->MinificationFilter]);
glTexParameteri(
this->Target, GL_TEXTURE_MAG_FILTER, OpenGLMagFilter[this->MagnificationFilter]);
}
#else
glTexParameteri(this->Target, GL_TEXTURE_MIN_FILTER, OpenGLMinFilter[this->MinificationFilter]);
glTexParameteri(this->Target, GL_TEXTURE_MAG_FILTER, OpenGLMagFilter[this->MagnificationFilter]);
#endif
#ifndef GL_ES_VERSION_3_0
glTexParameterfv(this->Target, GL_TEXTURE_BORDER_COLOR, this->BorderColor);
#endif
if (this->DepthTextureCompare)
{
glTexParameteri(this->Target, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
}
else
{
glTexParameteri(this->Target, GL_TEXTURE_COMPARE_MODE, GL_NONE);
}
// if mipmaps are requested also turn on anisotropic if available
#ifdef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
if (GLEW_EXT_texture_filter_anisotropic)
{
float aniso = 0.0f;
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &aniso);
if (aniso > this->MaximumAnisotropicFiltering)
{
aniso = this->MaximumAnisotropicFiltering;
}
glTexParameterf(this->Target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
}
#endif
glTexParameterf(this->Target, GL_TEXTURE_MIN_LOD, this->MinLOD);
glTexParameterf(this->Target, GL_TEXTURE_MAX_LOD, this->MaxLOD);
glTexParameteri(this->Target, GL_TEXTURE_BASE_LEVEL, this->BaseLevel);
glTexParameteri(this->Target, GL_TEXTURE_MAX_LEVEL, this->MaxLevel);
glTexParameteri(this->Target, GL_TEXTURE_COMPARE_FUNC,
OpenGLDepthTextureCompareFunction[this->DepthTextureCompareFunction]);
vtkOpenGLCheckErrorMacro("failed after SendParameters");
this->SendParametersTime.Modified();
}
//------------------------------------------------------------------------------
unsigned int vtkTextureObject::GetInternalFormat(
int vtktype, int numComps, bool shaderSupportsTextureInt)
{
if (this->InternalFormat)
{
return this->InternalFormat;
}
// pre-condition
if (vtktype == VTK_VOID && numComps != 1)
{
vtkErrorMacro(
"Depth component texture must have 1 component only (" << numComps << " requested");
this->InternalFormat = 0;
return this->InternalFormat;
}
this->InternalFormat =
this->GetDefaultInternalFormat(vtktype, numComps, shaderSupportsTextureInt);
if (!this->InternalFormat)
{
vtkDebugMacro("Unable to find suitable internal format for T="
<< vtktype << " NC=" << numComps << " SSTI=" << shaderSupportsTextureInt);
}
return this->InternalFormat;
}
//------------------------------------------------------------------------------
unsigned int vtkTextureObject::GetDefaultInternalFormat(
int vtktype, int numComps, bool shaderSupportsTextureInt)
{
GLenum result = 0;
// if shader supports int textures try that first
if (shaderSupportsTextureInt)
{
result = this->Context->GetDefaultTextureInternalFormat(
vtktype, numComps, true, false, this->UseSRGBColorSpace);
if (!result)
{
vtkDebugMacro("Unsupported internal texture type!");
}
return result;
}
// try default next
result = this->Context->GetDefaultTextureInternalFormat(
vtktype, numComps, false, false, this->UseSRGBColorSpace);
if (result)
{
return result;
}
// try floating point
result = this->Context->GetDefaultTextureInternalFormat(
vtktype, numComps, false, true, this->UseSRGBColorSpace);
if (!result)
{
vtkDebugMacro("Unsupported internal texture type!");
vtkDebugMacro("Unable to find suitable internal format for T="
<< vtktype << " NC=" << numComps << " SSTI=" << shaderSupportsTextureInt);
}
return result;
}
//------------------------------------------------------------------------------
void vtkTextureObject::SetInternalFormat(unsigned int glInternalFormat)
{
if (this->InternalFormat != glInternalFormat)
{
this->InternalFormat = glInternalFormat;
this->Modified();
}
}
//------------------------------------------------------------------------------
static int vtkGetVTKType(GLenum gltype)
{
// DON'T DEAL with VTK_CHAR as this is platform dependent.
switch (gltype)
{
case GL_BYTE:
return VTK_SIGNED_CHAR;
case GL_UNSIGNED_BYTE:
return VTK_UNSIGNED_CHAR;
case GL_SHORT:
return VTK_SHORT;
case GL_UNSIGNED_SHORT:
return VTK_UNSIGNED_SHORT;
case GL_INT:
return VTK_INT;
case GL_UNSIGNED_INT:
return VTK_UNSIGNED_INT;
case GL_FLOAT:
return VTK_FLOAT;
}
return 0;
}
void vtkTextureObject::GetShiftAndScale(float& shift, float& scale)
{
shift = 1.0;
scale = 1.0;
// check to see if this is an int format
GLenum iresult = this->Context->GetDefaultTextureInternalFormat(
vtkGetVTKType(this->Type), this->Components, true, false, this->UseSRGBColorSpace);
// using an int texture format, no shift scale
if (iresult == this->InternalFormat)
{
return;
}
// for all float type internal formats
switch (this->Type)
{
case GL_BYTE:
scale = (VTK_SIGNED_CHAR_MAX - VTK_SIGNED_CHAR_MIN) / 2.0;
shift = scale + VTK_SIGNED_CHAR_MIN;
break;
case GL_UNSIGNED_BYTE:
scale = VTK_UNSIGNED_CHAR_MAX;
shift = 0.0;
break;
case GL_SHORT:
// this may be off a tad
scale = (VTK_SHORT_MAX - VTK_SHORT_MIN) / 2.0;
shift = scale + VTK_SHORT_MIN;
break;
case GL_UNSIGNED_SHORT:
scale = VTK_UNSIGNED_SHORT_MAX;
shift = 0.0;
break;
case GL_INT:
// this may be off a tad
scale = (1.0 * VTK_INT_MAX - VTK_INT_MIN) / 2.0;
shift = scale + VTK_INT_MIN;
break;
case GL_UNSIGNED_INT:
scale = static_cast<float>(VTK_UNSIGNED_INT_MAX);
shift = 0.0;
break;
case GL_FLOAT:
default:
break;
}
}
//------------------------------------------------------------------------------
unsigned int vtkTextureObject::GetFormat(int vtktype, int numComps, bool shaderSupportsTextureInt)
{
if (!this->Format)
{
this->Format = this->GetDefaultFormat(vtktype, numComps, shaderSupportsTextureInt);
}
return this->Format;
}
//------------------------------------------------------------------------------
unsigned int vtkTextureObject::GetDefaultFormat(
int vtktype, int numComps, bool shaderSupportsTextureInt)
{
if (vtktype == VTK_VOID)
{
return GL_DEPTH_COMPONENT;
}
if (this->SupportsTextureInteger && shaderSupportsTextureInt &&
(vtktype == VTK_SIGNED_CHAR || vtktype == VTK_UNSIGNED_CHAR || vtktype == VTK_SHORT ||
vtktype == VTK_UNSIGNED_SHORT || vtktype == VTK_INT || vtktype == VTK_UNSIGNED_INT))
{
switch (numComps)
{
case 1:
return GL_RED_INTEGER;
case 2:
return GL_RG_INTEGER;
#ifdef GL_ES_VERSION_3_0
case 3:
return GL_RGB_INTEGER;
case 4:
return GL_RGBA_INTEGER;
#else
case 3:
return GL_RGB_INTEGER_EXT;
case 4:
return GL_RGBA_INTEGER_EXT;
#endif
}
}
else
{
switch (numComps)
{
case 1:
return GL_RED;
case 2:
return GL_RG;
case 3:
return GL_RGB;
case 4:
return GL_RGBA;
}
}
return GL_RGB;
}
//------------------------------------------------------------------------------
void vtkTextureObject::SetFormat(unsigned int glFormat)
{
if (this->Format != glFormat)
{
this->Format = glFormat;
this->Modified();
}
}
//------------------------------------------------------------------------------
void vtkTextureObject::ResetFormatAndType()
{
this->Format = 0;
this->InternalFormat = 0;
this->Type = 0;
}
//------------------------------------------------------------------------------
int vtkTextureObject::GetDefaultDataType(int vtk_scalar_type)
{
// DON'T DEAL with VTK_CHAR as this is platform dependent.
switch (vtk_scalar_type)
{
case VTK_SIGNED_CHAR:
return GL_BYTE;
case VTK_UNSIGNED_CHAR:
return GL_UNSIGNED_BYTE;
case VTK_SHORT:
return GL_SHORT;
case VTK_UNSIGNED_SHORT:
return GL_UNSIGNED_SHORT;
case VTK_INT:
return GL_INT;
case VTK_UNSIGNED_INT:
return GL_UNSIGNED_INT;
case VTK_FLOAT:
case VTK_VOID: // used for depth component textures.
return GL_FLOAT;
}
return 0;
}
//------------------------------------------------------------------------------
int vtkTextureObject::GetVTKDataType()
{
return ::vtkGetVTKType(this->Type);
}
//------------------------------------------------------------------------------
int vtkTextureObject::GetDataType(int vtk_scalar_type)
{
if (!this->Type)
{
this->Type = this->GetDefaultDataType(vtk_scalar_type);
}
return this->Type;
}
//------------------------------------------------------------------------------
void vtkTextureObject::SetDataType(unsigned int glType)
{
if (this->Type != glType)
{
this->Type = glType;
this->Modified();
}
}
//------------------------------------------------------------------------------
unsigned int vtkTextureObject::GetMinificationFilterMode(int vtktype)
{
switch (vtktype)
{
case Nearest:
return GL_NEAREST;
case Linear:
return GL_LINEAR;
case NearestMipmapNearest:
return GL_NEAREST_MIPMAP_NEAREST;
case NearestMipmapLinear:
return GL_NEAREST_MIPMAP_LINEAR;
case LinearMipmapNearest:
return GL_LINEAR_MIPMAP_NEAREST;
case LinearMipmapLinear:
return GL_LINEAR_MIPMAP_LINEAR;
default:
return GL_NEAREST;
}
}
//------------------------------------------------------------------------------
unsigned int vtkTextureObject::GetMagnificationFilterMode(int vtktype)
{
switch (vtktype)
{
case Nearest:
return GL_NEAREST;
case Linear:
return GL_LINEAR;
default:
return GL_NEAREST;
}
}
//------------------------------------------------------------------------------
unsigned int vtkTextureObject::GetWrapSMode(int vtktype)
{
switch (vtktype)
{
case ClampToEdge:
return GL_CLAMP_TO_EDGE;
case Repeat:
return GL_REPEAT;
#ifdef GL_CLAMP_TO_BORDER
case ClampToBorder:
return GL_CLAMP_TO_BORDER;
#endif
case MirroredRepeat:
return GL_MIRRORED_REPEAT;
default:
return GL_CLAMP_TO_EDGE;
}
}
//------------------------------------------------------------------------------
unsigned int vtkTextureObject::GetWrapTMode(int vtktype)
{
return this->GetWrapSMode(vtktype);
}
//------------------------------------------------------------------------------
unsigned int vtkTextureObject::GetWrapRMode(int vtktype)
{
return this->GetWrapSMode(vtktype);
}
// 1D textures are not supported in ES 2.0 or 3.0
#ifndef GL_ES_VERSION_3_0
//------------------------------------------------------------------------------
bool vtkTextureObject::Create1D(
int numComps, vtkPixelBufferObject* pbo, bool shaderSupportsTextureInt)
{
assert(this->Context);
assert(pbo->GetContext() == this->Context.GetPointer());
GLenum target = GL_TEXTURE_1D;
// Now, determine texture parameters using the information from the pbo.
// * internalFormat depends on number of components and the data type.
GLenum internalFormat =
this->GetInternalFormat(pbo->GetType(), numComps, shaderSupportsTextureInt);
// * format depends on the number of components.
GLenum format = this->GetFormat(pbo->GetType(), numComps, shaderSupportsTextureInt);