forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkLineIntegralConvolution2D.cxx
2152 lines (1878 loc) · 64.4 KB
/
vtkLineIntegralConvolution2D.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkLineIntegralConvolution2D.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkLineIntegralConvolution2D.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLExtensionManager.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkShader2.h"
#include "vtkShaderProgram2.h"
#include "vtkUniformVariables.h"
#include "vtkShader2Collection.h"
#include "vtkTextureObject.h"
#include "vtkPixelBufferObject.h"
#include "vtkFrameBufferObject2.h"
#include "vtkPixelExtent.h"
#include "vtkPainterCommunicator.h"
#include "vtkFloatArray.h"
#include "vtkTimerLog.h"
#include "vtkMath.h"
#include "vtkgl.h"
#include "vtkOpenGLError.h"
#include <vector>
#include <string>
#include <algorithm>
using std::deque;
using std::vector;
using std::string;
// Enable stream min/max computations. Streaming is accomplished
// via PBO+glReadPixels to read just the regions we are updating.
// Without streaming PBO+glGetTexImage is used to uplaod the entire
// screen sized texture, of which (in parallel) we are updating only
// a small part of.
#define STREAMING_MIN_MAX
// if you don't explicitly bind to 0 before swapping on some
// systems (intel hd4000) then things get whacky. nvidia devices
// are fine without this.
#define NOT_NVIDIA
// here have to setup the activate textures *before* calling use
// program looks like it's a bug in the intel driver.
// Intel GL 4.0.0 - Build 9.17.10.2932 GLSL 4.00 - Build 9.17.10.2932
#define INTEL_BUG
// if defined write intermediate results to disk
// for debugging. (1 results, 2 +steps, 3 +fbo status )
#define vtkLineIntegralConvolution2DDEBUG 0
#if (vtkLineIntegralConvolution2DDEBUG >= 1)
#include "vtkTextureIO.h"
#include <sstream>
using std::ostringstream;
//----------------------------------------------------------------------------
static
string mpifn(int rank, const char *fn)
{
ostringstream oss;
oss << rank << "_" << fn;
return oss.str();
}
#endif
#if vtkLineIntegralConvolution2DDEBUG >= 3
#define vtkLICCheckFrameBufferStatusMacro(mode) vtkCheckFrameBufferStatusMacro(mode)
#else
#define vtkLICCheckFrameBufferStatusMacro(mode)
#endif
// shader sources
extern const char *vtkLineIntegralConvolution2D_VT; // normalized image space transform
extern const char *vtkLineIntegralConvolution2D_LIC0; // initialization for lic
extern const char *vtkLineIntegralConvolution2D_LICI; // compute i'th lic step
extern const char *vtkLineIntegralConvolution2D_LICN; // finalize lic
extern const char *vtkLineIntegralConvolution2D_EE; // Laplace edge-enhance
extern const char *vtkLineIntegralConvolution2D_CE; // contrast enhance
extern const char *vtkLineIntegralConvolution2D_AAH; // horizontal part of anti-alias filter
extern const char *vtkLineIntegralConvolution2D_AAV; // vertical part of anti-alias filter
#if defined(NDEBUG) || (vtkLineIntegralConvolution2DDEBUG < 3)
# define DEBUG3CheckFrameBufferStatusMacro(mode)
#else
# define DEBUG3CheckFrameBufferStatusMacro(mode) \
vtkStaticCheckFrameBufferStatusMacro(mode)
#endif
/// vtkLICPingPongBufferManager -- gpgpu buffer manager
/**
Helper that manages state for the ping-pong buffer strategy
employed during LIC integration. This class encapsulates all
of the knowledge of our use of the FBO and texture units. Care
is taken to avoid feedback loops.
*/
class vtkLICPingPongBufferManager
{
public:
vtkLICPingPongBufferManager(
vtkFrameBufferObject2 *fbo,
unsigned int *bufSize,
vtkTextureObject *vectorTexture,
vtkTextureObject *maskVectorTexture,
vtkTextureObject *noiseTexture,
int doEEPass,
int doVTPass)
{
this->VectorTexture = vectorTexture;
this->MaskVectorTexture = maskVectorTexture;
this->NoiseTexture = noiseTexture;
// allocate buffers
vtkRenderWindow *context = fbo->GetContext();
this->LICTexture0 = this->AllocateLICBuffer(context, bufSize);
this->SeedTexture0 = this->AllocateLICBuffer(context, bufSize);
this->LICTexture1 = this->AllocateLICBuffer(context, bufSize);
this->SeedTexture1 = this->AllocateLICBuffer(context, bufSize);
this->EETexture = doEEPass ? this->AllocateNoiseBuffer(context, bufSize) : NULL;
this->ImageVectorTexture = doVTPass ? this->AllocateVectorBuffer(context, bufSize) : NULL;
this->DettachBuffers();
// setup pairs for buffer ping-pong
this->PingTextures[0] = this->LICTexture0->GetHandle();
this->PingTextures[1] = this->SeedTexture0->GetHandle();
this->PongTextures[0] = this->LICTexture1->GetHandle();
this->PongTextures[1] = this->SeedTexture1->GetHandle();
this->Textures[0] = this->PingTextures;
this->Textures[1] = this->PongTextures;
this->ReadIndex = 0;
#if vtkLineIntegralConvolution2DDEBUG >= 3
this->Print(cerr);
#endif
}
~vtkLICPingPongBufferManager()
{
// free buffers
this->LICTexture0->Delete();
this->SeedTexture0->Delete();
this->LICTexture1->Delete();
this->SeedTexture1->Delete();
if (this->EETexture)
{
this->EETexture->Delete();
}
if (this->ImageVectorTexture)
{
this->ImageVectorTexture->Delete();
}
}
// Description:
// Get the unit/unit id for the given texture
// Here is how we use tetxure units.
// name | unit
// -------------+--------
// vectors | 0
// mask vectors | 0/1
// noise | 2
// lic | 3
// seeds | 4
int GetVectorTextureUnit(){ return 0; }
int GetMaskVectorTextureUnit(){ return this->MaskVectorUnit; }
int GetNoiseTextureUnit(){ return 2; }
int GetLICTextureUnit(){ return 3; }
int GetSeedTextureUnit(){ return 4; }
// Description:
// Switch input and output buffers
void Swap(){ this->ReadIndex = 1 - this->ReadIndex; }
// Description:
// Get the last output (assumes a swap has been done).
vtkTextureObject *GetLastLICBuffer()
{
return this->ReadIndex == 0 ? this->LICTexture0 : this->LICTexture1;
}
// Description:
// Get the last output (assumes a swap has been done).
vtkTextureObject *GetLastSeedBuffer()
{
return this->ReadIndex == 0 ? this->SeedTexture0 : this->SeedTexture1;
}
// Description:
// Get the last output (assumes a swap has been done).
vtkTextureObject *GetLICBuffer()
{
return 1-this->ReadIndex == 0 ? this->LICTexture0 : this->LICTexture1;
}
// Description:
// Get the last output (assumes a swap has been done).
vtkTextureObject *GetSeedBuffer()
{
return 1-this->ReadIndex == 0 ? this->SeedTexture0 : this->SeedTexture1;
}
// Description:
// Clear all the buffers used for writing.
void ClearBuffers(
vtkFrameBufferObject2 *fbo,
const vtkPixelExtent &viewExt,
const deque<vtkPixelExtent> &extents,
int clearEETex = 0)
{
//attach
fbo->AddColorAttachment(vtkgl::DRAW_FRAMEBUFFER_EXT, 0U, this->LICTexture0);
fbo->AddColorAttachment(vtkgl::DRAW_FRAMEBUFFER_EXT, 1U, this->SeedTexture0);
fbo->AddColorAttachment(vtkgl::DRAW_FRAMEBUFFER_EXT, 2U, this->LICTexture1);
fbo->AddColorAttachment(vtkgl::DRAW_FRAMEBUFFER_EXT, 3U, this->SeedTexture1);
unsigned int num = 4U;
if (clearEETex)
{
fbo->AddColorAttachment(vtkgl::DRAW_FRAMEBUFFER_EXT, 4U, this->EETexture);
num = 5U;
}
fbo->ActivateDrawBuffers(num);
DEBUG3CheckFrameBufferStatusMacro(vtkgl::DRAW_FRAMEBUFFER_EXT);
// clear the parts of the screen which we will modify
// initially mask all fragments
glClearColor(0.0, 1.0, 0.0, 0.0);
#if 0
glClear(GL_COLOR_BUFFER_BIT);
#else
glEnable(GL_SCISSOR_TEST);
size_t nBlocks = extents.size();
for (size_t e=0; e<nBlocks; ++e)
{
vtkPixelExtent ext = extents[e];
// add halo for linear filtering
// since at most linear filtering requires
// 4 pixels , clearing an extra 4 here
// ensures we never access uninitialized
// memory.
ext.Grow(4);
ext &= viewExt;
unsigned int extSize[2];
ext.Size(extSize);
glScissor(ext[0], ext[2], extSize[0], extSize[1]);
glClear(GL_COLOR_BUFFER_BIT);
}
glDisable(GL_SCISSOR_TEST);
#endif
// detach
fbo->RemoveTexColorAttachments(vtkgl::DRAW_FRAMEBUFFER_EXT, num);
fbo->DeactivateDrawBuffers();
}
// Description:
// Clear the given buffer
void ClearBuffer(
vtkFrameBufferObject2 *fbo,
vtkTextureObject *tex,
const vtkPixelExtent &viewExt,
const deque<vtkPixelExtent> &extents)
{
//attach
fbo->AddColorAttachment(vtkgl::DRAW_FRAMEBUFFER_EXT, 0U, tex);
fbo->ActivateDrawBuffers(1);
DEBUG3CheckFrameBufferStatusMacro(vtkgl::DRAW_FRAMEBUFFER_EXT);
// clear the parts of the screen which we will modify
// initially mask all fragments
glClearColor(0.0, 1.0, 0.0, 0.0);
#if 0
glClear(GL_COLOR_BUFFER_BIT);
#else
glEnable(GL_SCISSOR_TEST);
size_t nBlocks = extents.size();
for (size_t e=0; e<nBlocks; ++e)
{
vtkPixelExtent ext = extents[e];
// add halo for linear filtering
// since at most linear filtering requires
// 4 pixels , clearing an extra 4 here
// ensures we never access uninitialized
// memory.
ext.Grow(4);
ext &= viewExt;
unsigned int extSize[2];
ext.Size(extSize);
glScissor(ext[0], ext[2], extSize[0], extSize[1]);
glClear(GL_COLOR_BUFFER_BIT);
}
glDisable(GL_SCISSOR_TEST);
#endif
// detach
fbo->RemoveTexColorAttachments(vtkgl::DRAW_FRAMEBUFFER_EXT, 1);
fbo->DeactivateDrawBuffers();
}
// Description:
// Activates the input textures. These are read only.
void AttachVectorTextures()
{
// vector
vtkgl::ActiveTexture(vtkgl::TEXTURE0);
if (this->ImageVectorTexture)
{
glBindTexture( GL_TEXTURE_2D, this->ImageVectorTexture->GetHandle());
}
else
{
glBindTexture( GL_TEXTURE_2D, this->VectorTexture->GetHandle());
}
vtkOpenGLStaticCheckErrorMacro("failed at glBindTexture");
// mask vectors (optional)
vtkgl::ActiveTexture(vtkgl::TEXTURE1);
if (this->MaskVectorTexture)
{
glBindTexture(GL_TEXTURE_2D, this->MaskVectorTexture->GetHandle());
vtkOpenGLStaticCheckErrorMacro("failed at glBindTexture");
this->MaskVectorUnit = 1;
}
else
{
glBindTexture(GL_TEXTURE_2D, 0);
this->MaskVectorUnit = 0;
}
}
// Description:
// Deactivates the input noise textures.
void DettachVectorTextures()
{
vtkgl::ActiveTexture(vtkgl::TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
vtkgl::ActiveTexture(vtkgl::TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);
}
// Description:
// Activate the read only noise texture. It's active for
// the entirety of each LIC pass.
void AttachNoiseTexture(int LICPassNum = 0)
{
switch (LICPassNum)
{
case 0:
this->NoiseTexture->Activate(vtkgl::TEXTURE2);
break;
case 1:
this->EETexture->Activate(vtkgl::TEXTURE2);
break;
}
}
// Dsecription:
// Deactivate the inpyt noise tetxure.
void DettachNoiseTexture()
{
vtkgl::ActiveTexture(vtkgl::TEXTURE2);
glBindTexture(GL_TEXTURE_2D, 0);
}
// Description:
// Setup read/write from/to the active lic/seed buffer texture pair
// for LIC pass.
void AttachLICBuffers()
{
unsigned int *readTex = this->Textures[this->ReadIndex];
vtkgl::ActiveTexture(vtkgl::TEXTURE3);
glBindTexture(GL_TEXTURE_2D, readTex[0]);
vtkOpenGLStaticCheckErrorMacro("failed at glBindTexture");
vtkgl::ActiveTexture(vtkgl::TEXTURE4);
glBindTexture(GL_TEXTURE_2D, readTex[1]);
vtkOpenGLStaticCheckErrorMacro("failed at glBindTexture");
unsigned int *writeTex = this->Textures[1-this->ReadIndex];
vtkgl::FramebufferTexture2DEXT(
vtkgl::DRAW_FRAMEBUFFER_EXT,
vtkgl::COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
writeTex[0],
0);
vtkOpenGLStaticCheckErrorMacro("failed at glFramebuffereadTexture2D");
vtkgl::FramebufferTexture2DEXT(
vtkgl::DRAW_FRAMEBUFFER_EXT,
vtkgl::COLOR_ATTACHMENT1,
GL_TEXTURE_2D,
writeTex[1],
0);
vtkOpenGLStaticCheckErrorMacro("failed at glFramebuffereadTexture2D");
GLenum atts[2] = {
vtkgl::COLOR_ATTACHMENT0,
vtkgl::COLOR_ATTACHMENT1
};
vtkgl::DrawBuffers(2, atts);
vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
DEBUG3CheckFrameBufferStatusMacro(vtkgl::DRAW_FRAMEBUFFER_EXT);
}
// Description:
// Remove input/output bufers used for computing the LIC.
void DettachLICBuffers()
{
vtkgl::ActiveTexture(vtkgl::TEXTURE3);
glBindTexture(GL_TEXTURE_2D, 0);
vtkgl::ActiveTexture(vtkgl::TEXTURE4);
glBindTexture(GL_TEXTURE_2D, 0);
vtkgl::FramebufferTexture2DEXT(
vtkgl::DRAW_FRAMEBUFFER_EXT,
vtkgl::COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
0U,
0);
vtkgl::FramebufferTexture2DEXT(
vtkgl::DRAW_FRAMEBUFFER_EXT,
vtkgl::COLOR_ATTACHMENT1,
GL_TEXTURE_2D,
0U,
0);
GLenum atts[1] = {GL_NONE};
vtkgl::DrawBuffers(1, atts);
vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
}
// Description:
// Attach read/write buffers for transform pass.
void AttachImageVectorBuffer()
{
vtkgl::ActiveTexture(vtkgl::TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->VectorTexture->GetHandle());
vtkOpenGLStaticCheckErrorMacro("failed at glBindTexture");
vtkgl::FramebufferTexture2DEXT(
vtkgl::DRAW_FRAMEBUFFER_EXT,
vtkgl::COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
this->ImageVectorTexture->GetHandle(),
0);
vtkOpenGLStaticCheckErrorMacro("failed at glFramebufferTexture2D");
GLenum atts[1] = {vtkgl::COLOR_ATTACHMENT0};
vtkgl::DrawBuffers(1, atts);
vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
DEBUG3CheckFrameBufferStatusMacro(vtkgl::DRAW_FRAMEBUFFER_EXT);
}
// Description:
// Attach read/write buffers for transform pass.
void DettachImageVectorBuffer()
{
vtkgl::ActiveTexture(vtkgl::TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
vtkgl::FramebufferTexture2DEXT(
vtkgl::DRAW_FRAMEBUFFER_EXT,
vtkgl::COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
0U,
0);
GLenum atts[1] = {GL_NONE};
vtkgl::DrawBuffers(1, atts);
vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
}
// Description:
// Attach read/write buffers for EE pass.
void AttachEEBuffer()
{
unsigned int *readTex = this->Textures[this->ReadIndex];
vtkgl::ActiveTexture(vtkgl::TEXTURE3);
glBindTexture(GL_TEXTURE_2D, readTex[0]);
vtkOpenGLStaticCheckErrorMacro("failed at glBindTexture");
vtkgl::FramebufferTexture2DEXT(
vtkgl::DRAW_FRAMEBUFFER_EXT,
vtkgl::COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
this->EETexture->GetHandle(),
0);
vtkOpenGLStaticCheckErrorMacro("failed at glFramebufferTexture2D");
GLenum atts[1] = {vtkgl::COLOR_ATTACHMENT0};
vtkgl::DrawBuffers(1, atts);
vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
DEBUG3CheckFrameBufferStatusMacro(vtkgl::DRAW_FRAMEBUFFER_EXT);
}
// Description:
// Attach read/write buffers for EE pass.
void DettachEEBuffer()
{
vtkgl::ActiveTexture(vtkgl::TEXTURE3);
glBindTexture(GL_TEXTURE_2D, 0);
vtkgl::FramebufferTexture2DEXT(
vtkgl::DRAW_FRAMEBUFFER_EXT,
vtkgl::COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
0U,
0);
GLenum atts[1] = {GL_NONE};
vtkgl::DrawBuffers(1, atts);
vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
}
// Description:
// Deactivates and removes all read/write buffers that were in
// use during the run, restoring a pristine FBO/texture unit state.
void DettachBuffers()
{
vtkgl::ActiveTexture(vtkgl::TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0U);
vtkOpenGLStaticCheckErrorMacro("failed at glBindTexture");
vtkgl::ActiveTexture(vtkgl::TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0U);
vtkOpenGLStaticCheckErrorMacro("failed at glBindTexture");
vtkgl::ActiveTexture(vtkgl::TEXTURE2);
glBindTexture(GL_TEXTURE_2D, 0U);
vtkOpenGLStaticCheckErrorMacro("failed at glBindTexture");
vtkgl::ActiveTexture(vtkgl::TEXTURE3);
glBindTexture(GL_TEXTURE_2D, 0U);
vtkOpenGLStaticCheckErrorMacro("failed at glBindTexture");
vtkgl::ActiveTexture(vtkgl::TEXTURE4);
glBindTexture(GL_TEXTURE_2D, 0U);
vtkOpenGLStaticCheckErrorMacro("failed at glBindTexture");
vtkgl::FramebufferTexture2DEXT(
vtkgl::DRAW_FRAMEBUFFER_EXT,
vtkgl::COLOR_ATTACHMENT0,
GL_TEXTURE_2D,
0U,
0);
vtkOpenGLStaticCheckErrorMacro("failed at glFramebufferTexture2D");
vtkgl::FramebufferTexture2DEXT(
vtkgl::DRAW_FRAMEBUFFER_EXT,
vtkgl::COLOR_ATTACHMENT1,
GL_TEXTURE_2D,
0U,
0);
vtkOpenGLStaticCheckErrorMacro("failed at glFramebufferTexture2D");
GLenum none = GL_NONE;
vtkgl::DrawBuffers(1, &none);
vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
}
// Description:
// Get the read/write ids
int GetReadIndex(){ return this->ReadIndex; }
int GetWriteIndex(){ return 1 - this->ReadIndex; }
// Description:
// Allocate a texture of the given size.
// with parameters for LIC lookups
vtkTextureObject *AllocateLICBuffer(
vtkRenderWindow *context,
unsigned int texSize[2])
{
float border[4] = {0.0f, 1.0f, 0.0f, 0.0f};
return this->AllocateBuffer(
context,
texSize,
vtkTextureObject::Nearest,
vtkTextureObject::ClampToBorder,
border);
}
// Description:
// Allocate a texture of the given size.
// with parameters for noise lookups
vtkTextureObject *AllocateNoiseBuffer(
vtkRenderWindow *context,
unsigned int texSize[2])
{
float border[4] = {0.0f, 0.0f, 0.0f, 0.0f};
return this->AllocateBuffer(
context,
texSize,
vtkTextureObject::Nearest,
vtkTextureObject::ClampToEdge,
border);
}
// Description:
// Allocate a texture of the given size.
// with parameters for LIC
vtkTextureObject *AllocateVectorBuffer(
vtkRenderWindow *context,
unsigned int texSize[2])
{
float border[4] = {0.0f, 0.0f, 0.0f, 0.0f};
return this->AllocateBuffer(
context,
texSize,
vtkTextureObject::Linear,
vtkTextureObject::ClampToBorder,
border);
}
// Description:
// Allocate a texture of the given size.
vtkTextureObject *AllocateBuffer(
vtkRenderWindow *context,
unsigned int texSize[2],
int filter,
int wrapping,
float *borderColor)
{
vtkTextureObject *tex = vtkTextureObject::New();
tex->SetContext(context);
tex->SetBaseLevel(0);
tex->SetMaxLevel(0);
tex->SetBorderColor(borderColor);
tex->SetWrapS(wrapping);
tex->SetWrapT(wrapping);
tex->SetMinificationFilter(filter); // no guard pixels
tex->SetMagnificationFilter(filter); // no guard pixels
tex->Create2D(texSize[0], texSize[1], 4, VTK_FLOAT, false);
tex->SetAutoParameters(0);
return tex;
}
// Description:
// Render screen aligned quad. Texture coordinates are
// always assigned on TEXTURE0, it's hardcoded in the
// shaders.
void RenderQuad(
float computeBoundsPt0[2],
float computeBoundsPt1[2],
vtkPixelExtent computeExtent)
{
float computeBounds[4] = {
computeBoundsPt0[0], computeBoundsPt1[0],
computeBoundsPt0[1], computeBoundsPt1[1]
};
this->RenderQuad(computeBounds, computeExtent);
}
// Description:
// Render screen aligned quad. Texture coordinates are
// always assigned on TEXTURE0, it's hardcoded in the
// shaders.
void RenderQuad(
float computeBounds[4],
vtkPixelExtent computeExtent)
{
int quadPtIds[8] = {0,2, 1,2, 1,3, 0,3};
float quadBounds[4];
computeExtent.CellToNode();
computeExtent.GetData(quadBounds);
glBegin(GL_QUADS);
for (int q=0; q<4; ++q)
{
int qq = 2*q;
vtkgl::MultiTexCoord2f(
vtkgl::TEXTURE0,
computeBounds[quadPtIds[qq]],
computeBounds[quadPtIds[qq+1]]);
glVertex2f(
quadBounds[quadPtIds[qq]],
quadBounds[quadPtIds[qq+1]]);
}
glEnd();
vtkOpenGLStaticCheckErrorMacro("failed at render quad");
}
#if (vtkLineIntegralConvolution2DDEBUG >= 1)
// Description:
// Write the last output buffers to disk (assumes a swap has
// already been done)
void WriteBuffers(
int rank,
const char *licFileName,
const char *seedFileName,
const deque<vtkPixelExtent>& exts)
{
if (licFileName)
{
vtkTextureIO::Write(
mpifn(rank, licFileName),
this->GetLastLICBuffer(),
exts);
}
if (seedFileName)
{
vtkTextureIO::Write(
mpifn(rank, seedFileName),
this->GetLastSeedBuffer(),
exts);
}
}
void WriteEEBuffer(int rank, const deque<vtkPixelExtent> &exts)
{
vtkTextureIO::Write(
mpifn(rank,"lic2d_ee.vtm"),
this->EETexture,
exts);
}
void WriteImageVectorBuffer(int rank, const deque<vtkPixelExtent> &exts)
{
vtkTextureIO::Write(
mpifn(rank,"lic2d_ivec.vtm"),
this->ImageVectorTexture,
exts);
}
void WriteInputs(int rank, const deque<vtkPixelExtent>& exts)
{
vtkTextureIO::Write(
mpifn(rank,"lic2d_vec.vtm"),
this->VectorTexture,
exts);
if (this->MaskVectorTexture)
{
vtkTextureIO::Write(
mpifn(rank,"lic2d_mask.vtm"),
this->MaskVectorTexture,
exts);
}
vtkTextureIO::Write(
mpifn(rank,"lic2d_noise.vtk"),
this->NoiseTexture);
}
// Description:
// Print current state to the given stream
void Print(ostream &os)
{
os
<< "Vectors = " << this->VectorTexture->GetHandle() << endl
<< "ImageVectors = " << this->ImageVectorTexture->GetHandle() << endl
<< "MaskVectors = " << (this->MaskVectorTexture ? this->MaskVectorTexture->GetHandle() : 0U) << endl
<< "Noise = " << this->NoiseTexture->GetHandle() << endl
<< "EE = " << (this->EETexture ? this->EETexture->GetHandle() : 0U) << endl
<< "LIC0 = " << this->LICTexture0->GetHandle() << endl
<< "Seed0 = " << this->SeedTexture0->GetHandle() << endl
<< "LIC1 = " << this->LICTexture1->GetHandle() << endl
<< "Seed1 = " << this->SeedTexture1->GetHandle() << endl
<< "ReadIndex=" << this->ReadIndex << endl
<< "PingTextures[0]=" << this->Textures[0][0] << ", " << this->Textures[0][1] << endl
<< "PongTextures[1]=" << this->Textures[1][0] << ", " << this->Textures[1][1] << endl;
}
#endif
private:
vtkTextureObject *VectorTexture;
vtkTextureObject *ImageVectorTexture;
vtkTextureObject *MaskVectorTexture;
vtkTextureObject *NoiseTexture;
vtkTextureObject *EETexture;
vtkTextureObject *LICTexture0;
vtkTextureObject *SeedTexture0;
vtkTextureObject *LICTexture1;
vtkTextureObject *SeedTexture1;
int MaskVectorUnit;
int ReadIndex;
unsigned int PingTextures[2];
unsigned int PongTextures[2];
unsigned int *Textures[2];
};
namespace vtkLineIntegralConvolution2DUtil
{
/**
glsl shader code for selecting vector comps.
*/
string GetComponentSelectionProgram(int *compIds)
{
// swizles at 45,46
string srcCode("vec2 getSelectedComponents(vec4 V){ return V.$$; }");
const char *compNames = "xyzw";
srcCode[45] = compNames[compIds[0]];
srcCode[46] = compNames[compIds[1]];
return srcCode;
}
/*
Shader code for looking up vectors
*/
const char *GetVectorLookupProgram(int normalize)
{
// lookup the vector and normalize
const char *getNormVecSrc = " \
uniform sampler2D texVectors;\n \
vec2 getVector( vec2 vectc )\n \
{\n \
vec2 V = texture2D( texVectors, vectc ).xy;\n \
// normalize if |V| not 0\n \
float lenV = length( V );\n \
if ( lenV > 1.0e-8 )\n \
{\n \
return V/lenV;\n \
}\n \
else\n \
{\n \
return vec2( 0.0, 0.0 );\n \
}\n \
}\n \
";
// lookup the vector
const char *getVecSrc = " \
uniform sampler2D texVectors;\n \
vec2 getVector( vec2 vectc )\n \
{\n \
return texture2D( texVectors, vectc ).xy;\n \
}\n \
";
if ( normalize )
{
return getNormVecSrc;
}
return getVecSrc;
}
// Description
// find min/max of unmasked fragments across all regions
// download the entire screen then search each region
void FindMinMax(
vtkTextureObject *tex,
const deque<vtkPixelExtent> &extents,
float &min,
float &max)
{
// download entire screen
int size0 = tex->GetWidth();
vtkPixelBufferObject *colors = tex->Download();
float *pColors = static_cast<float*>(colors->MapPackedBuffer());
// search each region
size_t nExtents = extents.size();
for (size_t q=0; q<nExtents; ++q)
{
const vtkPixelExtent &extent = extents[q];
for (int j=extent[2]; j<=extent[3]; ++j)
{
for (int i=extent[0]; i<=extent[1]; ++i)
{
int id = 4*(j*size0+i);
bool masked = pColors[id+1] != 0.0f;
bool ceskip = pColors[id+2] != 0.0f;
if ( !masked && !ceskip )
{
float color = pColors[id];
min = min > color ? color : min;
max = max < color ? color : max;
}
}
}
}
colors->UnmapPackedBuffer();
colors->Delete();
#if vtkLineIntegralConvolution2DDEBUG>=1
cerr << "min=" << min << " max=" << max << endl;
#endif
}
// Description
// find min/max of unmasked fragments across all regions
// download each search each region individually
void StreamingFindMinMax(
vtkFrameBufferObject2 *fbo,
vtkTextureObject *tex,
const deque<vtkPixelExtent> &extents,
float &min,
float &max)
{
size_t nExtents = extents.size();
// initiate download of each region
fbo->AddColorAttachment(vtkgl::DRAW_FRAMEBUFFER_EXT, 0U, tex);
fbo->AddColorAttachment(vtkgl::READ_FRAMEBUFFER_EXT, 0U, tex);
fbo->ActivateDrawBuffer(0U);
fbo->ActivateReadBuffer(0U);
fbo->CheckFrameBufferStatus(vtkgl::FRAMEBUFFER_EXT);
vector<vtkPixelBufferObject*> pbos(nExtents, NULL);
for (size_t q=0; q<nExtents; ++q)
{
pbos[q] = fbo->Download(
const_cast<int*>(extents[q].GetData()),
VTK_FLOAT,
4,
GL_FLOAT,
GL_RGBA);
}
fbo->DeactivateDrawBuffers();
fbo->DeactivateReadBuffer();
fbo->RemoveTexColorAttachment(vtkgl::DRAW_FRAMEBUFFER_EXT, 0U);
fbo->RemoveTexColorAttachment(vtkgl::READ_FRAMEBUFFER_EXT, 0U);
// search each region
for (size_t q=0; q<nExtents; ++q)
{
vtkPixelBufferObject *&pbo = pbos[q];
float *pColors = (float*)pbo->MapPackedBuffer();
size_t n = extents[q].Size();
for (size_t i = 0; i<n; ++i)
{
bool masked = pColors[4*i+1] != 0.0f;
bool ceskip = pColors[4*i+2] != 0.0f;
if ( !masked && !ceskip )
{
float color = pColors[4*i];
min = min > color ? color : min;
max = max < color ? color : max;
}
}
pbo->UnmapPackedBuffer();
pbo->Delete();
pbo = NULL;
}
pbos.clear();
#if vtkLineIntegralConvolution2DDEBUG >= 1
cerr << "min=" << min << " max=" << max << endl;
#endif
}
};
using namespace vtkLineIntegralConvolution2DUtil;
// ----------------------------------------------------------------------------
vtkObjectFactoryNewMacro(vtkLineIntegralConvolution2D);
// ----------------------------------------------------------------------------
vtkLineIntegralConvolution2D::vtkLineIntegralConvolution2D()
{
this->Comm = NULL;
this->Context = NULL;
this->FBO = vtkFrameBufferObject2::New();
this->ShadersNeedBuild = 1;
this->VTShader = NULL;
this->LIC0Shader = NULL;
this->LICIShader = NULL;
this->LICNShader = NULL;
this->EEShader = NULL;
this->CEShader = NULL;
this->AAHShader = NULL;
this->AAVShader = NULL;
this->StepSize = 0.01;
this->NumberOfSteps = 1;
this->NormalizeVectors = 1;
this->ComponentIds[0] = 0;
this->ComponentIds[1] = 1;
this->EnhancedLIC = 1;
this->EnhanceContrast = 0;
this->LowContrastEnhancementFactor = 0.0;
this->HighContrastEnhancementFactor = 0.0;
this->AntiAlias = 0;
this->MaskThreshold = 0.0;
this->TransformVectors = 1;
}
// ----------------------------------------------------------------------------
vtkLineIntegralConvolution2D::~vtkLineIntegralConvolution2D()
{
delete this->Comm;
this->SetContext(NULL);
this->SetVTShader(NULL);
this->SetLIC0Shader(NULL);
this->SetLICIShader(NULL);