vtkLineIntegralConvolution2D.cxx 64 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*=========================================================================

  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 "vtk_glew.h"

Ken Martin's avatar
Ken Martin committed
19 20
#include "vtkOpenGLHelper.h"

Ken Martin's avatar
Ken Martin committed
21 22

#include "vtkFloatArray.h"
23
#include "vtkOpenGLFramebufferObject.h"
Ken Martin's avatar
Ken Martin committed
24 25 26
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLError.h"
27
#include "vtkOpenGLRenderUtilities.h"
Ken Martin's avatar
Ken Martin committed
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
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLShaderCache.h"
#include "vtkPainterCommunicator.h"
#include "vtkPixelBufferObject.h"
#include "vtkPixelExtent.h"
#include "vtkShaderProgram.h"
#include "vtkTextureObject.h"
#include "vtkTextureObjectVS.h"
#include "vtkTimerLog.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
#include "vtkLineIntegralConvolution2D_VT.h" // normalized image space transform
#include "vtkLineIntegralConvolution2D_LIC0.h" // initialization for lic
#include "vtkLineIntegralConvolution2D_LICI.h" // compute i'th lic step
#include "vtkLineIntegralConvolution2D_LICN.h" // finalize lic
#include "vtkLineIntegralConvolution2D_EE.h"   // Laplace edge-enhance
#include "vtkLineIntegralConvolution2D_CE.h"   // contrast enhance
#include "vtkLineIntegralConvolution2D_AAH.h"  // horizontal part of anti-alias filter
#include "vtkLineIntegralConvolution2D_AAV.h"  // 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(
113
      vtkOpenGLFramebufferObject *fbo,
Ken Martin's avatar
Ken Martin committed
114 115 116 117 118 119
      unsigned int *bufSize,
      vtkTextureObject *vectorTexture,
      vtkTextureObject *maskVectorTexture,
      vtkTextureObject *noiseTexture,
      int doEEPass,
      int doVTPass)
120
  {
Ken Martin's avatar
Ken Martin committed
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
    this->VectorTexture = vectorTexture;
    this->MaskVectorTexture = maskVectorTexture;
    this->NoiseTexture = noiseTexture;

    this->ReadIndex = 0;

    // allocate buffers
    vtkOpenGLRenderWindow *context =
      vtkOpenGLRenderWindow::SafeDownCast(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;

    // setup pairs for buffer ping-pong
    this->PingTextures[0] = this->LICTexture0;
    this->PingTextures[1] = this->SeedTexture0;

    this->PongTextures[0] = this->LICTexture1;
    this->PongTextures[1] = this->SeedTexture1;

    this->Textures[0] = this->PingTextures;
    this->Textures[1] = this->PongTextures;

    this->DettachBuffers(fbo);

    #if vtkLineIntegralConvolution2DDEBUG >= 3
    this->Print(cerr);
    #endif
152
  }
Ken Martin's avatar
Ken Martin committed
153 154

  ~vtkLICPingPongBufferManager()
155
  {
Ken Martin's avatar
Ken Martin committed
156 157 158 159 160 161
    // free buffers
    this->LICTexture0->Delete();
    this->SeedTexture0->Delete();
    this->LICTexture1->Delete();
    this->SeedTexture1->Delete();
    if (this->EETexture)
162
    {
Ken Martin's avatar
Ken Martin committed
163
      this->EETexture->Delete();
164
    }
Ken Martin's avatar
Ken Martin committed
165
    if (this->ImageVectorTexture)
166
    {
Ken Martin's avatar
Ken Martin committed
167 168
      this->ImageVectorTexture->Delete();
    }
169
  }
Ken Martin's avatar
Ken Martin committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

  // 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 this->VectorTexture->GetTextureUnit();
  }
  int GetImageVectorTextureUnit()
  {
    if (this->ImageVectorTexture)
188
    {
Ken Martin's avatar
Ken Martin committed
189 190
      this->ImageVectorTexture->Activate();
      return this->ImageVectorTexture->GetTextureUnit();
191
    }
Ken Martin's avatar
Ken Martin committed
192 193 194 195 196 197
    this->VectorTexture->Activate();
    return this->VectorTexture->GetTextureUnit();
  }
  int GetMaskVectorTextureUnit()
  {
    if (this->MaskVectorTexture)
198
    {
Ken Martin's avatar
Ken Martin committed
199 200
      this->MaskVectorTexture->Activate();
      return this->MaskVectorTexture->GetTextureUnit();
201
    }
Ken Martin's avatar
Ken Martin committed
202 203 204 205 206
    return this->GetImageVectorTextureUnit();
  }
  int GetNoiseTextureUnit(int LICPassNum)
  {
    if (LICPassNum == 0)
207
    {
Ken Martin's avatar
Ken Martin committed
208 209
      this->NoiseTexture->Activate();
      return this->NoiseTexture->GetTextureUnit();
210
    }
Ken Martin's avatar
Ken Martin committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    this->EETexture->Activate();
    return this->EETexture->GetTextureUnit();
  }
  int GetLICTextureUnit()
  {
    this->Textures[this->ReadIndex][0]->Activate();
    return this->Textures[this->ReadIndex][0]->GetTextureUnit();
  }
  int GetSeedTextureUnit()
  {
    this->Textures[this->ReadIndex][1]->Activate();
    return this->Textures[this->ReadIndex][1]->GetTextureUnit();
  }

  // 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()
232
  {
Ken Martin's avatar
Ken Martin committed
233
    return this->ReadIndex == 0 ? this->LICTexture0 : this->LICTexture1;
234
  }
Ken Martin's avatar
Ken Martin committed
235 236 237 238

  // Description:
  // Get the last output (assumes a swap has been done).
  vtkTextureObject *GetLastSeedBuffer()
239
  {
Ken Martin's avatar
Ken Martin committed
240
    return this->ReadIndex == 0 ? this->SeedTexture0 : this->SeedTexture1;
241
  }
Ken Martin's avatar
Ken Martin committed
242 243 244 245

  // Description:
  // Get the last output (assumes a swap has been done).
  vtkTextureObject *GetLICBuffer()
246
  {
Ken Martin's avatar
Ken Martin committed
247
    return 1-this->ReadIndex == 0 ? this->LICTexture0 : this->LICTexture1;
248
  }
Ken Martin's avatar
Ken Martin committed
249 250 251 252

  // Description:
  // Get the last output (assumes a swap has been done).
  vtkTextureObject *GetSeedBuffer()
253
  {
Ken Martin's avatar
Ken Martin committed
254
    return 1-this->ReadIndex == 0 ? this->SeedTexture0 : this->SeedTexture1;
255
  }
Ken Martin's avatar
Ken Martin committed
256 257 258 259

  // Description:
  // Clear all the buffers used for writing.
  void ClearBuffers(
260
        vtkOpenGLFramebufferObject *fbo,
Ken Martin's avatar
Ken Martin committed
261 262 263
        const vtkPixelExtent &viewExt,
        const deque<vtkPixelExtent> &extents,
        int clearEETex = 0)
264
  {
Ken Martin's avatar
Ken Martin committed
265 266 267 268 269 270 271
    //attach
    fbo->AddColorAttachment(GL_DRAW_FRAMEBUFFER, 0U, this->LICTexture0);
    fbo->AddColorAttachment(GL_DRAW_FRAMEBUFFER, 1U, this->SeedTexture0);
    fbo->AddColorAttachment(GL_DRAW_FRAMEBUFFER, 2U, this->LICTexture1);
    fbo->AddColorAttachment(GL_DRAW_FRAMEBUFFER, 3U, this->SeedTexture1);
    unsigned int num = 4U;
    if (clearEETex)
272
    {
Ken Martin's avatar
Ken Martin committed
273 274
      fbo->AddColorAttachment(GL_DRAW_FRAMEBUFFER, 4U, this->EETexture);
      num = 5U;
275
    }
Ken Martin's avatar
Ken Martin committed
276 277 278 279 280 281 282 283 284 285 286 287
    fbo->ActivateDrawBuffers(num);
    DEBUG3CheckFrameBufferStatusMacro(GL_DRAW_FRAMEBUFFER);

    // 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)
288
    {
Ken Martin's avatar
Ken Martin committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302
      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);
303
    }
Ken Martin's avatar
Ken Martin committed
304 305 306 307 308 309
    glDisable(GL_SCISSOR_TEST);
    #endif
    // detach
    // detach
    fbo->RemoveTexColorAttachments(GL_DRAW_FRAMEBUFFER, num);
    fbo->DeactivateDrawBuffers();
310
  }
Ken Martin's avatar
Ken Martin committed
311 312 313 314

  // Description:
  // Clear the given buffer
  void ClearBuffer(
315
        vtkOpenGLFramebufferObject *fbo,
Ken Martin's avatar
Ken Martin committed
316 317 318
        vtkTextureObject *tex,
        const vtkPixelExtent &viewExt,
        const deque<vtkPixelExtent> &extents)
319
  {
Ken Martin's avatar
Ken Martin committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333
    //attach
    fbo->AddColorAttachment(GL_DRAW_FRAMEBUFFER, 0U, tex);
    fbo->ActivateDrawBuffers(1);
    DEBUG3CheckFrameBufferStatusMacro(GL_DRAW_FRAMEBUFFER);

    // 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)
334
    {
Ken Martin's avatar
Ken Martin committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348
      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);
349
    }
Ken Martin's avatar
Ken Martin committed
350 351 352 353 354
    glDisable(GL_SCISSOR_TEST);
    #endif
    // detach
    fbo->RemoveTexColorAttachments(GL_DRAW_FRAMEBUFFER, 1);
    fbo->DeactivateDrawBuffers();
355
  }
Ken Martin's avatar
Ken Martin committed
356 357 358 359

  // Description:
  // Activates the input textures. These are read only.
  void AttachVectorTextures()
360
  {
Ken Martin's avatar
Ken Martin committed
361 362
    // vector
    if (this->ImageVectorTexture)
363
    {
Ken Martin's avatar
Ken Martin committed
364
      this->ImageVectorTexture->Activate();
365
    }
Ken Martin's avatar
Ken Martin committed
366
    else
367
    {
Ken Martin's avatar
Ken Martin committed
368
      this->VectorTexture->Activate();
369
    }
Ken Martin's avatar
Ken Martin committed
370 371 372

    // mask vectors (optional)
    if (this->MaskVectorTexture)
373
    {
Ken Martin's avatar
Ken Martin committed
374 375
      this->MaskVectorTexture->Activate();
    }
376
  }
Ken Martin's avatar
Ken Martin committed
377 378 379 380

  // Description:
  // Deactivates the input noise textures.
  void DettachVectorTextures()
381
  {
Ken Martin's avatar
Ken Martin committed
382
    if (this->ImageVectorTexture)
383
    {
Ken Martin's avatar
Ken Martin committed
384
      this->ImageVectorTexture->Deactivate();
385
    }
Ken Martin's avatar
Ken Martin committed
386
    else
387
    {
Ken Martin's avatar
Ken Martin committed
388
      this->VectorTexture->Deactivate();
389
    }
Ken Martin's avatar
Ken Martin committed
390 391 392

    // mask vectors (optional)
    if (this->MaskVectorTexture)
393
    {
Ken Martin's avatar
Ken Martin committed
394 395
      this->MaskVectorTexture->Deactivate();
    }
396
  }
Ken Martin's avatar
Ken Martin committed
397 398 399 400 401

  // Description:
  // Activate the read only noise texture. It's active for
  // the entirety of each LIC pass.
  void AttachNoiseTexture(int LICPassNum = 0)
402
  {
Ken Martin's avatar
Ken Martin committed
403
    switch (LICPassNum)
404
    {
Ken Martin's avatar
Ken Martin committed
405 406 407 408 409 410 411
      case 0:
        this->NoiseTexture->Activate();
        break;
      case 1:
        this->EETexture->Activate();
        break;
    }
412
  }
Ken Martin's avatar
Ken Martin committed
413 414 415 416

  // Dsecription:
  // Deactivate the inpyt noise tetxure.
  void DettachNoiseTexture(int LICPassNum = 0)
417
  {
Ken Martin's avatar
Ken Martin committed
418
    switch (LICPassNum)
419
    {
Ken Martin's avatar
Ken Martin committed
420 421 422 423 424 425 426
      case 0:
        this->NoiseTexture->Deactivate();
        break;
      case 1:
        this->EETexture->Deactivate();
        break;
    }
427
  }
Ken Martin's avatar
Ken Martin committed
428 429 430 431

  // Description:
  // Setup read/write from/to the active lic/seed buffer texture pair
  // for LIC pass.
432
  void AttachLICBuffers(vtkOpenGLFramebufferObject *vtkNotUsed(fbo))
433
  {
Ken Martin's avatar
Ken Martin committed
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
    // activate read textures
    vtkTextureObject **readTex = this->Textures[this->ReadIndex];
    readTex[0]->Activate();
    readTex[1]->Activate();

    // attach write textures
    vtkTextureObject **writeTex = this->Textures[1-this->ReadIndex];

    glFramebufferTexture2D(
          GL_DRAW_FRAMEBUFFER,
          GL_COLOR_ATTACHMENT0,
          GL_TEXTURE_2D,
          writeTex[0]->GetHandle(),
          0);
    vtkOpenGLStaticCheckErrorMacro("failed at glFramebuffereadTexture2D");

    glFramebufferTexture2D(
          GL_DRAW_FRAMEBUFFER,
          GL_COLOR_ATTACHMENT1,
          GL_TEXTURE_2D,
          writeTex[1]->GetHandle(),
          0);
    vtkOpenGLStaticCheckErrorMacro("failed at glFramebuffereadTexture2D");

    GLenum atts[2] = {
          GL_COLOR_ATTACHMENT0,
          GL_COLOR_ATTACHMENT1
          };
    glDrawBuffers(2, atts);
    vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
464
  }
Ken Martin's avatar
Ken Martin committed
465 466 467

  // Description:
  // Remove input/output bufers used for computing the LIC.
468
  void DettachLICBuffers(vtkOpenGLFramebufferObject *vtkNotUsed(fbo))
469
  {
Ken Martin's avatar
Ken Martin committed
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
    glFramebufferTexture2D(
          GL_DRAW_FRAMEBUFFER,
          GL_COLOR_ATTACHMENT0,
          GL_TEXTURE_2D,
          0U,
          0);

    vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
    glFramebufferTexture2D(
          GL_DRAW_FRAMEBUFFER,
          GL_COLOR_ATTACHMENT1,
          GL_TEXTURE_2D,
          0U,
          0);

    GLenum atts[1] = {GL_NONE};
    glDrawBuffers(1, atts);
    vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");

    vtkTextureObject **readTex = this->Textures[this->ReadIndex];
    readTex[0]->Deactivate();
    readTex[1]->Deactivate();
493
  }
Ken Martin's avatar
Ken Martin committed
494 495 496

  // Description:
  // Attach read/write buffers for transform pass.
497
  void AttachImageVectorBuffer(vtkOpenGLFramebufferObject *vtkNotUsed(fbo))
498
  {
Ken Martin's avatar
Ken Martin committed
499 500 501 502 503 504 505 506 507 508 509 510 511
    this->VectorTexture->Activate();

    glFramebufferTexture2D(
          GL_DRAW_FRAMEBUFFER,
          GL_COLOR_ATTACHMENT0,
          GL_TEXTURE_2D,
          this->ImageVectorTexture->GetHandle(),
          0);
    vtkOpenGLStaticCheckErrorMacro("failed at glFramebufferTexture2D");

    GLenum atts[1] = {GL_COLOR_ATTACHMENT0};
    glDrawBuffers(1, atts);
    vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
512
  }
Ken Martin's avatar
Ken Martin committed
513 514 515

  // Description:
  // Attach read/write buffers for transform pass.
516
  void DettachImageVectorBuffer(vtkOpenGLFramebufferObject *vtkNotUsed(fbo))
517
  {
Ken Martin's avatar
Ken Martin committed
518 519 520 521 522 523 524 525 526 527 528 529
    this->VectorTexture->Deactivate();

    glFramebufferTexture2D(
      GL_DRAW_FRAMEBUFFER,
      GL_COLOR_ATTACHMENT0,
      GL_TEXTURE_2D,
      0U,
      0);

    GLenum atts[1] = {GL_NONE};
    glDrawBuffers(1, atts);
    vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
530
  }
Ken Martin's avatar
Ken Martin committed
531 532 533

  // Description:
  // Attach read/write buffers for EE pass.
534
  void AttachEEBuffer(vtkOpenGLFramebufferObject *vtkNotUsed(fbo))
535
  {
Ken Martin's avatar
Ken Martin committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    vtkTextureObject **readTex = this->Textures[this->ReadIndex];
    readTex[0]->Activate();

    glFramebufferTexture2D(
          GL_DRAW_FRAMEBUFFER,
          GL_COLOR_ATTACHMENT0,
          GL_TEXTURE_2D,
          this->EETexture->GetHandle(),
          0);
    vtkOpenGLStaticCheckErrorMacro("failed at glFramebufferTexture2D");

    GLenum atts[1] = {GL_COLOR_ATTACHMENT0};
    glDrawBuffers(1, atts);
    vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");

    DEBUG3CheckFrameBufferStatusMacro(GL_DRAW_FRAMEBUFFER);
552
  }
Ken Martin's avatar
Ken Martin committed
553 554 555

  // Description:
  // Attach read/write buffers for EE pass.
556
  void DettachEEBuffer(vtkOpenGLFramebufferObject *vtkNotUsed(fbo))
557
  {
Ken Martin's avatar
Ken Martin committed
558 559 560 561 562 563 564 565 566 567 568 569 570
    vtkTextureObject **readTex = this->Textures[this->ReadIndex];
    readTex[0]->Deactivate();

    glFramebufferTexture2D(
          GL_DRAW_FRAMEBUFFER,
          GL_COLOR_ATTACHMENT0,
          GL_TEXTURE_2D,
          0U,
          0);

    GLenum atts[1] = {GL_NONE};
    glDrawBuffers(1, atts);
    vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");
571
  }
Ken Martin's avatar
Ken Martin committed
572 573 574 575

  // Description:
  // Deactivates and removes all read/write buffers that were in
  // use during the run, restoring a pristine FBO/texture unit state.
576
  void DettachBuffers(vtkOpenGLFramebufferObject *vtkNotUsed(fbo))
577
  {
Ken Martin's avatar
Ken Martin committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
    glFramebufferTexture2D(
          GL_DRAW_FRAMEBUFFER,
          GL_COLOR_ATTACHMENT0,
          GL_TEXTURE_2D,
          0U,
          0);
    vtkOpenGLStaticCheckErrorMacro("failed at glFramebufferTexture2D");

    glFramebufferTexture2D(
          GL_DRAW_FRAMEBUFFER,
          GL_COLOR_ATTACHMENT1,
          GL_TEXTURE_2D,
          0U,
          0);
    vtkOpenGLStaticCheckErrorMacro("failed at glFramebufferTexture2D");

    GLenum none = GL_NONE;
    glDrawBuffers(1, &none);
    vtkOpenGLStaticCheckErrorMacro("failed at glDrawBuffers");

    // deactivate all textures?
    vtkTextureObject **readTex = this->Textures[this->ReadIndex];
Ken Martin's avatar
Ken Martin committed
600 601
    if (readTex[0]) { readTex[0]->Deactivate(); }
    if (readTex[1]) { readTex[1]->Deactivate(); }
Ken Martin's avatar
Ken Martin committed
602
    vtkTextureObject **writeTex = this->Textures[1-this->ReadIndex];
Ken Martin's avatar
Ken Martin committed
603 604
    if (writeTex[0]) { writeTex[0]->Deactivate(); }
    if (writeTex[1]) { writeTex[1]->Deactivate(); }
605
  }
Ken Martin's avatar
Ken Martin committed
606 607 608 609 610 611 612 613 614 615 616 617

  // 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(
        vtkOpenGLRenderWindow *context,
        unsigned int texSize[2])
618
  {
Ken Martin's avatar
Ken Martin committed
619 620 621 622 623 624 625
    float border[4] = {0.0f, 1.0f, 0.0f, 0.0f};
    return this->AllocateBuffer(
          context,
          texSize,
          vtkTextureObject::Nearest,
          vtkTextureObject::ClampToBorder,
          border);
626
  }
Ken Martin's avatar
Ken Martin committed
627 628 629 630 631 632 633

  // Description:
  // Allocate a texture of the given size.
  // with parameters for noise lookups
  vtkTextureObject *AllocateNoiseBuffer(
        vtkOpenGLRenderWindow *context,
        unsigned int texSize[2])
634
  {
Ken Martin's avatar
Ken Martin committed
635 636 637 638 639 640 641
    float border[4] = {0.0f, 0.0f, 0.0f, 0.0f};
    return this->AllocateBuffer(
          context,
          texSize,
          vtkTextureObject::Nearest,
          vtkTextureObject::ClampToEdge,
          border);
642
  }
Ken Martin's avatar
Ken Martin committed
643 644 645 646 647 648 649

  // Description:
  // Allocate a texture of the given size.
  // with parameters for LIC
  vtkTextureObject *AllocateVectorBuffer(
        vtkOpenGLRenderWindow *context,
        unsigned int texSize[2])
650
  {
Ken Martin's avatar
Ken Martin committed
651 652 653 654 655 656 657
    float border[4] = {0.0f, 0.0f, 0.0f, 0.0f};
    return this->AllocateBuffer(
          context,
          texSize,
          vtkTextureObject::Linear,
          vtkTextureObject::ClampToBorder,
          border);
658
  }
Ken Martin's avatar
Ken Martin committed
659 660 661 662 663 664 665 666 667

  // Description:
  // Allocate a texture of the given size.
  vtkTextureObject *AllocateBuffer(
        vtkOpenGLRenderWindow *context,
        unsigned int texSize[2],
        int filter,
        int wrapping,
        float *borderColor)
668
  {
Ken Martin's avatar
Ken Martin committed
669 670 671 672 673 674 675 676 677 678 679 680
    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;
681
  }
Ken Martin's avatar
Ken Martin committed
682 683 684 685 686 687 688

  // Description:
  // Render screen aligned quad.
  void RenderQuad(
          float computeBoundsPt0[2],
          float computeBoundsPt1[2],
          vtkPixelExtent computeExtent,
Ken Martin's avatar
Ken Martin committed
689
          vtkOpenGLHelper *cbo)
690
  {
Ken Martin's avatar
Ken Martin committed
691 692 693 694 695
    float computeBounds[4] = {
          computeBoundsPt0[0], computeBoundsPt1[0],
          computeBoundsPt0[1], computeBoundsPt1[1]
          };
     this->RenderQuad(computeBounds, computeExtent, cbo);
696
  }
Ken Martin's avatar
Ken Martin committed
697 698 699 700 701 702

  // Description:
  // Render screen aligned quad.
  void RenderQuad(
          float computeBounds[4],
          vtkPixelExtent computeExtent,
Ken Martin's avatar
Ken Martin committed
703
          vtkOpenGLHelper *cbo)
704
  {
Ken Martin's avatar
Ken Martin committed
705 706 707 708 709 710 711 712 713 714 715
    float quadBounds[4];
    computeExtent.CellToNode();
    computeExtent.GetData(quadBounds);

    float tcoords[] = {
      computeBounds[0], computeBounds[2],
      computeBounds[1], computeBounds[2],
      computeBounds[1], computeBounds[3],
      computeBounds[0], computeBounds[3]};

    float verts[] = {
Ken Martin's avatar
Ken Martin committed
716 717 718 719
      computeBounds[0]*2.0f-1.0f, computeBounds[2]*2.0f-1.0f, 0.0f,
      computeBounds[1]*2.0f-1.0f, computeBounds[2]*2.0f-1.0f, 0.0f,
      computeBounds[1]*2.0f-1.0f, computeBounds[3]*2.0f-1.0f, 0.0f,
      computeBounds[0]*2.0f-1.0f, computeBounds[3]*2.0f-1.0f, 0.0f};
Ken Martin's avatar
Ken Martin committed
720

721
    vtkOpenGLRenderUtilities::RenderQuad(verts, tcoords,
Ken Martin's avatar
Ken Martin committed
722
      cbo->Program, cbo->VAO);
Ken Martin's avatar
Ken Martin committed
723
    vtkOpenGLStaticCheckErrorMacro("failed at RenderQuad");
724
  }
Ken Martin's avatar
Ken Martin committed
725 726 727 728 729 730 731 732 733 734

  #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)
735
  {
Ken Martin's avatar
Ken Martin committed
736
    if (licFileName)
737
    {
Ken Martin's avatar
Ken Martin committed
738 739 740 741
      vtkTextureIO::Write(
              mpifn(rank, licFileName),
              this->GetLastLICBuffer(),
              exts);
742
    }
Ken Martin's avatar
Ken Martin committed
743
    if (seedFileName)
744
    {
Ken Martin's avatar
Ken Martin committed
745 746 747 748 749
      vtkTextureIO::Write(
              mpifn(rank, seedFileName),
              this->GetLastSeedBuffer(),
              exts);
    }
750
  }
Ken Martin's avatar
Ken Martin committed
751
  void WriteEEBuffer(int rank, const deque<vtkPixelExtent> &exts)
752
  {
Ken Martin's avatar
Ken Martin committed
753 754 755 756
    vtkTextureIO::Write(
          mpifn(rank,"lic2d_ee.vtm"),
          this->EETexture,
          exts);
757
  }
Ken Martin's avatar
Ken Martin committed
758
  void WriteImageVectorBuffer(int rank, const deque<vtkPixelExtent> &exts)
759
  {
Ken Martin's avatar
Ken Martin committed
760 761 762 763
    vtkTextureIO::Write(
          mpifn(rank,"lic2d_ivec.vtm"),
          this->ImageVectorTexture,
          exts);
764
  }
Ken Martin's avatar
Ken Martin committed
765
  void WriteInputs(int rank, const deque<vtkPixelExtent>& exts)
766
  {
Ken Martin's avatar
Ken Martin committed
767 768 769 770 771
    vtkTextureIO::Write(
              mpifn(rank,"lic2d_vec.vtm"),
              this->VectorTexture,
              exts);
    if (this->MaskVectorTexture)
772
    {
Ken Martin's avatar
Ken Martin committed
773 774 775 776
      vtkTextureIO::Write(
                mpifn(rank,"lic2d_mask.vtm"),
                this->MaskVectorTexture,
                exts);
777
    }
Ken Martin's avatar
Ken Martin committed
778 779 780
    vtkTextureIO::Write(
              mpifn(rank,"lic2d_noise.vtk"),
              this->NoiseTexture);
781
  }
Ken Martin's avatar
Ken Martin committed
782 783 784 785

  // Description:
  // Print current state to the given stream
  void Print(ostream &os)
786
  {
Ken Martin's avatar
Ken Martin committed
787 788 789 790 791 792 793 794 795 796 797 798 799
    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;
800
  }
Ken Martin's avatar
Ken Martin committed
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
  #endif

private:
  vtkTextureObject *VectorTexture;
  vtkTextureObject *ImageVectorTexture;
  vtkTextureObject *MaskVectorTexture;
  vtkTextureObject *NoiseTexture;
  vtkTextureObject *EETexture;
  vtkTextureObject *LICTexture0;
  vtkTextureObject *SeedTexture0;
  vtkTextureObject *LICTexture1;
  vtkTextureObject *SeedTexture1;

  int  ReadIndex;
  vtkTextureObject *PingTextures[2];
  vtkTextureObject *PongTextures[2];
  vtkTextureObject **Textures[2];
};

namespace vtkLineIntegralConvolution2DUtil
{
/**
glsl shader code for selecting vector comps.
*/
string GetComponentSelectionProgram(int *compIds)
{
  // swizles at 45,46
  string srcCode(".$$");
  const char *compNames = "xyzw";
  srcCode[1] = compNames[compIds[0]];
  srcCode[2] = compNames[compIds[1]];
  return srcCode;
}

/*
Shader code for looking up vectors
*/
const char *GetVectorLookupProgram(int normalize)
{
  // lookup the vector and normalize
  const char *getNormVecSrc = " \
    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 = " \
    vec2 getVector( vec2 vectc )\n \
      {\n \
      return texture2D( texVectors, vectc ).xy;\n \
      }\n \
    ";

  if ( normalize )
867
  {
Ken Martin's avatar
Ken Martin committed
868
    return getNormVecSrc;
869
  }
Ken Martin's avatar
Ken Martin committed
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
 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)
889
  {
Ken Martin's avatar
Ken Martin committed
890 891
    const vtkPixelExtent &extent = extents[q];
    for (int j=extent[2]; j<=extent[3]; ++j)
892
    {
Ken Martin's avatar
Ken Martin committed
893
      for (int i=extent[0]; i<=extent[1]; ++i)
894
      {
Ken Martin's avatar
Ken Martin committed
895 896 897 898
        int id = 4*(j*size0+i);
        bool masked = pColors[id+1] != 0.0f;
        bool ceskip = pColors[id+2] != 0.0f;
        if ( !masked && !ceskip )
899
        {
Ken Martin's avatar
Ken Martin committed
900 901 902 903 904 905
          float color = pColors[id];
          min = min > color ? color : min;
          max = max < color ? color : max;
        }
      }
    }
906
  }
Ken Martin's avatar
Ken Martin committed
907 908 909 910 911 912 913 914 915 916 917
  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(
918
      vtkOpenGLFramebufferObject *fbo,
Ken Martin's avatar
Ken Martin committed
919 920 921 922 923 924 925 926 927 928 929 930 931 932
      vtkTextureObject *tex,
      const deque<vtkPixelExtent> &extents,
      float &min,
      float &max)
{
  size_t nExtents = extents.size();
  // initiate download of each region
  fbo->AddColorAttachment(GL_DRAW_FRAMEBUFFER, 0U, tex);
  fbo->AddColorAttachment(GL_READ_FRAMEBUFFER, 0U, tex);
  fbo->ActivateDrawBuffer(0U);
  fbo->ActivateReadBuffer(0U);
  fbo->CheckFrameBufferStatus(GL_FRAMEBUFFER);
  vector<vtkPixelBufferObject*> pbos(nExtents, NULL);
  for (size_t q=0; q<nExtents; ++q)
933
  {
Ken Martin's avatar
Ken Martin committed
934 935 936 937 938 939
    pbos[q] = fbo->Download(
          const_cast<int*>(extents[q].GetData()),
          VTK_FLOAT,
          4,
          GL_FLOAT,
          GL_RGBA);
940
  }
Ken Martin's avatar
Ken Martin committed
941 942 943 944 945 946
  fbo->DeactivateDrawBuffers();
  fbo->DeactivateReadBuffer();
  fbo->RemoveTexColorAttachment(GL_DRAW_FRAMEBUFFER, 0U);
  fbo->RemoveTexColorAttachment(GL_READ_FRAMEBUFFER, 0U);
  // search each region
  for (size_t q=0; q<nExtents; ++q)
947
  {
Ken Martin's avatar
Ken Martin committed
948 949 950 951 952
    vtkPixelBufferObject *&pbo = pbos[q];
    float *pColors = (float*)pbo->MapPackedBuffer();

    size_t n = extents[q].Size();
    for (size_t i = 0; i<n; ++i)
953
    {
Ken Martin's avatar
Ken Martin committed
954 955 956
      bool masked = pColors[4*i+1] != 0.0f;
      bool ceskip = pColors[4*i+2] != 0.0f;
      if ( !masked && !ceskip )
957
      {
Ken Martin's avatar
Ken Martin committed
958 959 960 961
        float color = pColors[4*i];
        min = min > color ? color : min;
        max = max < color ? color : max;
      }
962
    }
Ken Martin's avatar
Ken Martin committed
963 964 965 966

    pbo->UnmapPackedBuffer();
    pbo->Delete();
    pbo = NULL;
967
  }
Ken Martin's avatar
Ken Martin committed
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
  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;
986
  this->FBO = vtkOpenGLFramebufferObject::New();
Ken Martin's avatar
Ken Martin committed
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017

  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()
{
1018
  delete this->Comm;
Ken Martin's avatar
Ken Martin committed
1019 1020 1021 1022 1023 1024 1025 1026 1027
  this->SetContext(NULL);
  this->SetVTShader(NULL);
  this->SetLIC0Shader(NULL);
  this->SetLICIShader(NULL);
  this->SetLICNShader(NULL);
  this->SetEEShader(NULL);
  this->SetCEShader(NULL);
  this->SetAAHShader(NULL);
  this->SetAAVShader(NULL);
1028 1029

  if (this->VTShader)
1030
  {
1031
    delete this->VTShader;
1032
  }
1033
  if (this->LIC0Shader)
1034
  {
1035
    delete this->LIC0Shader;
1036
  }
1037
  if (this->LICIShader)
1038
  {
1039
    delete this->LICIShader;
1040
  }
1041
  if (this->LICNShader)
1042
  {
1043
    delete this->LICNShader;
1044
  }
1045
  if (this->EEShader)
1046
  {
1047
    delete this->EEShader;
1048
  }
1049
  if (this->CEShader)
1050
  {
1051
    delete this->CEShader;
1052
  }
1053
  if (this->AAHShader)
1054
  {
1055
    delete this->AAHShader;
1056
  }
1057
  if (this->AAVShader)
1058
  {
1059
    delete this->AAVShader;
1060
  }
1061

Ken Martin's avatar
Ken Martin committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
  this->FBO->Delete();
}

// ----------------------------------------------------------------------------
vtkOpenGLRenderWindow *vtkLineIntegralConvolution2D::GetContext()
{
  return this->Context;
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetContext(vtkOpenGLRenderWindow *renWin)
{
  if (this->Context == renWin)
1075
  {
Ken Martin's avatar
Ken Martin committed
1076
    return;
1077
  }
Ken Martin's avatar
Ken Martin committed
1078 1079 1080 1081 1082 1083 1084

  this->Context = renWin;
  this->ShadersNeedBuild = 1;
  this->FBO->SetContext(renWin);
  this->Modified();

  if (renWin && !this->IsSupported(renWin))
1085
  {
Ken Martin's avatar
Ken Martin committed
1086
    vtkErrorMacro("The required OpenGL extensions are not present");
1087
  }
Ken Martin's avatar
Ken Martin committed
1088 1089 1090 1091 1092 1093 1094
}

// ----------------------------------------------------------------------------
bool vtkLineIntegralConvolution2D::IsSupported(vtkRenderWindow *renWin)
{
  vtkOpenGLRenderWindow *context = vtkOpenGLRenderWindow::SafeDownCast(renWin);
  if (!context)
1095
  {
Ken Martin's avatar
Ken Martin committed
1096
    return false;
1097
  }
Ken Martin's avatar
Ken Martin committed
1098 1099

  return vtkTextureObject::IsSupported(context, true, false, false)
1100
     && vtkOpenGLFramebufferObject::IsSupported(context)
Ken Martin's avatar
Ken Martin committed
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
     && vtkPixelBufferObject::IsSupported(context);
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetNoiseTexParameters(vtkTextureObject * tex)
{
  tex->SetBaseLevel(0);
  tex->SetMaxLevel(0);
  tex->SetWrapS(vtkTextureObject::Repeat);
  tex->SetWrapT(vtkTextureObject::Repeat);
  tex->SetMinificationFilter(vtkTextureObject::Nearest);
  tex->SetMagnificationFilter(vtkTextureObject::Nearest);
  // note : as a side affect it sets the parameters
  // that is needed here.
  tex->Bind();
  tex->UnBind();
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetVectorTexParameters(vtkTextureObject *tex)
{
  tex->SetBaseLevel(0);
  tex->SetMaxLevel(0);
  tex->SetWrapS(vtkTextureObject::ClampToBorder);
  tex->SetWrapT(vtkTextureObject::ClampToBorder);
  tex->SetBorderColor(0.0, 0.0, 0.0, 0.0);
  tex->SetMinificationFilter(vtkTextureObject::Linear);
  tex->SetMagnificationFilter(vtkTextureObject::Linear);
  // note : as a side affect it sets the parameters
  // that is needed here.
  tex->Bind();
  tex->UnBind();
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetComponentIds(int c0, int c1)
{
  if ((this->ComponentIds[0] == c0) && (this->ComponentIds[1] == c1))
1139
  {
Ken Martin's avatar
Ken Martin committed
1140
    return;
1141
  }
Ken Martin's avatar
Ken Martin committed
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
  this->ComponentIds[0] = c0;
  this->ComponentIds[1] = c1;
  this->ShadersNeedBuild = 1;
  this->Modified();
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetTransformVectors(int val)
{
  val = val < 0 ? 0 : val;
  val = val > 1 ? 1 : val;
  if (this->TransformVectors == val)
1154
  {
Ken Martin's avatar
Ken Martin committed
1155
    return;
1156
  }
Ken Martin's avatar
Ken Martin committed
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
  this->TransformVectors = val;
  this->ShadersNeedBuild = 1;
  this->Modified();
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetNormalizeVectors(int val)
{
  val = val < 0 ? 0 : val;
  val = val > 1 ? 1 : val;
  if (this->NormalizeVectors == val)
1168
  {
Ken Martin's avatar
Ken Martin committed
1169
    return;
1170
  }
Ken Martin's avatar
Ken Martin committed
1171 1172 1173 1174 1175 1176 1177 1178
  this->NormalizeVectors = val;
  this->ShadersNeedBuild = 1;
  this->Modified();
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetVTShader(vtkShaderProgram * prog)
{
1179
  if (this->VTShader)
1180
  {
1181
    this->VTShader->Program = prog;
1182
  }
Ken Martin's avatar
Ken Martin committed
1183 1184 1185 1186 1187
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetLIC0Shader(vtkShaderProgram * prog)
{
1188
  if (this->LIC0Shader)
1189
  {
1190
    this->LIC0Shader->Program = prog;
1191
  }
Ken Martin's avatar
Ken Martin committed
1192 1193 1194 1195 1196
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetLICIShader(vtkShaderProgram * prog)
{
1197
  if (this->LICIShader)
1198
  {
1199
    this->LICIShader->Program = prog;
1200
  }
Ken Martin's avatar
Ken Martin committed
1201 1202 1203 1204 1205
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetLICNShader(vtkShaderProgram * prog)
{
1206
  if (this->LICNShader)
1207
  {
1208
    this->LICNShader->Program = prog;
1209
  }
Ken Martin's avatar
Ken Martin committed
1210 1211 1212 1213 1214
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetEEShader(vtkShaderProgram * prog)
{
1215
  if (this->EEShader)
1216
  {
1217
    this->EEShader->Program = prog;
1218
  }
Ken Martin's avatar
Ken Martin committed
1219
}
1220

Ken Martin's avatar
Ken Martin committed
1221 1222 1223
// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetCEShader(vtkShaderProgram * prog)
{
1224
  if (this->CEShader)
1225
  {
1226
    this->CEShader->Program = prog;
1227
  }
Ken Martin's avatar
Ken Martin committed
1228 1229 1230 1231 1232
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetAAHShader(vtkShaderProgram * prog)
{
1233
  if (this->AAHShader)
1234
  {
1235
    this->AAHShader->Program = prog;
1236
  }
Ken Martin's avatar
Ken Martin committed
1237 1238 1239 1240 1241
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::SetAAVShader(vtkShaderProgram * prog)
{
1242
  if (this->AAVShader)
1243
  {
1244
    this->AAVShader->Program = prog;
1245
  }
Ken Martin's avatar
Ken Martin committed
1246 1247 1248 1249
}

namespace {
  void BuildAShader(vtkOpenGLRenderWindow *renWin,
Ken Martin's avatar
Ken Martin committed
1250
    vtkOpenGLHelper **cbor, const char *frag)
Ken Martin's avatar
Ken Martin committed
1251 1252
  {
  if (*cbor == NULL)
1253
  {
Ken Martin's avatar
Ken Martin committed
1254
    *cbor = new vtkOpenGLHelper;
Ken Martin's avatar
Ken Martin committed
1255 1256 1257
    std::string VSSource = vtkTextureObjectVS;
    std::string GSSource;
    (*cbor)->Program =
1258
        renWin->GetShaderCache()->ReadyShaderProgram(VSSource.c_str(),
Ken Martin's avatar
Ken Martin committed
1259 1260
                                              frag,
                                              GSSource.c_str());
1261
  }
Ken Martin's avatar
Ken Martin committed
1262
  else
1263
  {
1264
    renWin->GetShaderCache()->ReadyShaderProgram((*cbor)->Program);
1265
  }
Ken Martin's avatar
Ken Martin committed
1266 1267 1268 1269 1270 1271 1272 1273 1274
  }
}

// ----------------------------------------------------------------------------
void vtkLineIntegralConvolution2D::BuildShaders()
{
  vtkOpenGLRenderWindow *renWin = this->Context;

  std::string FSSource = vtkLineIntegralConvolution2D_VT;
Ken Martin's avatar
Ken Martin committed
1275
  vtkShaderProgram::Substitute(FSSource,"//VTK::LICComponentSelection::Impl",
Ken Martin's avatar
Ken Martin committed
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
    "vec2 V = texture2D(texVectors, tcoordVC.st)" +
    GetComponentSelectionProgram(this->ComponentIds) + ";"
    );
  BuildAShader(renWin, &this->VTShader,
    FSSource.c_str());

  BuildAShader(renWin, &this->LIC0Shader,
    vtkLineIntegralConvolution2D_LIC0);

  FSSource = vtkLineIntegralConvolution2D_LICI;
Ken Martin's avatar
Ken Martin committed
1286
  vtkShaderProgram::Substitute(FSSource,"//VTK::LICVectorLookup::Impl",
Ken Martin's avatar
Ken Martin committed
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
    GetVectorLookupProgram(this->NormalizeVectors)
    );
  BuildAShader(renWin, &this->LICIShader,
    FSSource.c_str());

  BuildAShader(renWin, &this->LICNShader,
    vtkLineIntegralConvolution2D_LICN);
  BuildAShader(renWin, &this->EEShader,
    vtkLineIntegralConvolution2D_EE);
  BuildAShader(renWin, &this->CEShader,
    vtkLineIntegralConvolution2D_CE);
  BuildAShader(renWin, &this->AAHShader,
    vtkLineIntegralConvolution2D_AAH);
  BuildAShader(renWin, &this->AAVShader,
    vtkLineIntegralConvolution2D_AAV);
}

// ----------------------------------------------------------------------------
vtkPainterCommunicator *vtkLineIntegralConvolution2D::GetCommunicator()
{
  if (this->Comm == NULL)
1308
  {
Ken Martin's avatar
Ken Martin committed
1309
    this->Comm = new vtkPainterCommunicator;
1310
  }
Ken Martin's avatar
Ken Martin committed
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378
  return this->Comm;
}

// ----------------------------------------------------------------------------
vtkTextureObject *vtkLineIntegralConvolution2D::Execute(
        vtkTextureObject *vectorTex,
        vtkTextureObject *noiseTex)
{
  // execute over the entire vector field, no guard pixels are present
  // parallel results will be incorrect.

  vtkPixelExtent vectorTexExtent(
        vectorTex->GetWidth(),
        vectorTex->GetHeight());

  return this->Execute(
        vectorTexExtent.GetData(),
        vectorTex,
        noiseTex);
}

// ----------------------------------------------------------------------------
vtkTextureObject *vtkLineIntegralConvolution2D::Execute(
      const int ext[4],
      vtkTextureObject *vectorTex,
      vtkTextureObject *noiseTex)
{
  // execute over a subset of the input texture, no guard pixels are present
  // composite data and parallel results will be incorrect.

  this->SetVectorTexParameters(vectorTex);
  this->SetNoiseTexParameters(noiseTex);

  vtkPixelExtent vectorTexExtent(vectorTex->GetWidth(), vectorTex->GetHeight());
  vtkPixelExtent vectorExtent(ext);
  vtkPixelExtent licExtent(ext);
  vtkPixelExtent outputTexExtent(ext);
  vtkPixelExtent outputExtent(ext);

  deque<vtkPixelExtent> vectorExtents;
  vectorExtents.push_back(vectorExtent);

  deque<vtkPixelExtent> licExtents;
  licExtents.push_back(licExtent);

  unsigned int licSize[2];
  licExtent.Size(licSize);

  return this->Execute(
        vectorTexExtent,
        vectorExtents,
        licExtents,
        vectorTex,
        NULL,
        noiseTex);
}

// ----------------------------------------------------------------------------
vtkTextureObject *vtkLineIntegralConvolution2D::Execute(
      const vtkPixelExtent &inputTexExtent,              // screen space extent of the input texture
      const deque<vtkPixelExtent> &vectorExtents,        // disjoint set describing vector extents
      const deque<vtkPixelExtent> &licExtents,           // disjoint set describing desired lic extents
      vtkTextureObject *vectorTex,
      vtkTextureObject *maskVectorTex,
      vtkTextureObject *noiseTex)
{
  // validate inputs, internal state, etc...
  if (!this->Context)
1379
  {
Ken Martin's avatar
Ken Martin committed
1380 1381
    vtkErrorMacro("invalid this->Context");
    return NULL;
1382
  }
Ken Martin's avatar
Ken Martin committed
1383
  if (this->NumberOfSteps < 0)
1384
  {
Ken Martin's avatar
Ken Martin committed
1385 1386
    vtkErrorMacro("Number of integration steps should be positive.");
    return NULL;
1387
  }
Ken Martin's avatar
Ken Martin committed
1388
  if (this->StepSize < 0.0)
1389
  {
Ken Martin's avatar
Ken Martin committed
1390 1391
    vtkErrorMacro("Streamline integration step size should be positive.");
    return NULL;
1392
  }
Ken Martin's avatar
Ken Martin committed
1393
  if (vectorTex->GetComponents() < 2)
1394
  {
Ken Martin's avatar
Ken Martin committed
1395 1396
    vtkErrorMacro("VectorField must have at least 2 components.");
    return NULL;
1397
  }
Ken Martin's avatar
Ken Martin committed
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408

  #if defined(vtkLineIntegralConvolution2DTIME) && !defined(vtkSurfaceLICPainterTIME)
  this->StartTimerEvent("vtkLineIntegralConvolution::Execute");
  #elif defined(USE_VTK_TIMER)
  vtkSmartPointer<vtkTimerLog> timer = vtkSmartPointer<vtkTimerLog>::New();
  timer->StartTimer();
  #endif

  vtkOpenGLRenderWindow *renWin = this->Context;
  // initialize shaders
  if (this->ShadersNeedBuild)
1409
  {
Ken Martin's avatar
Ken Martin committed
1410 1411
    this->BuildShaders();
    this->ShadersNeedBuild = 0;
1412
  }
Ken Martin's avatar
Ken Martin committed
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435

  // input
  unsigned int inputTexSize[2];
  inputTexExtent.Size(inputTexSize);

  float dx = 1.0f/((float)inputTexSize[0]);
  float dy = 1.0f/((float)inputTexSize[1]);

  // texture coordinates and bounds for compute regions
  unsigned int computeTexSize[2];
  inputTexExtent.Size(computeTexSize);

  // at slight expense to the serial case
  // compute LIC,EE, and AA over the entire vector
  // extents (this is why it's critical that they
  // are disjoint)this allows us to forgo expensive
  // halo exchanges when running in parallel.
  size_t nComputeExtents1 = vectorExtents.size();
  const deque<vtkPixelExtent> &computeExtents1 = vectorExtents;

  size_t nbds = 4*nComputeExtents1;
  vector<float> computeBounds1(nbds, 0.0f);
  for (size_t i=0; i<nComputeExtents1; ++i)
1436
  {
Ken Martin's avatar
Ken Martin committed
1437 1438 1439 1440 1441 1442
    const vtkPixelExtent &computeExtent1 = computeExtents1[i];
    float *bound = &computeBounds1[4*i];
    bound[0] = ((float)computeExtent1[0])/((float)inputTexSize[0]);
    bound[1] = ((float)computeExtent1[1]+1.0f)/((float)inputTexSize[0]);
    bound[2] = ((float)computeExtent1[2])/((float)inputTexSize[1]);
    bound[3] = ((float)computeExtent1[3]+1.0f)/((float)inputTexSize[1]);
1443
  }
Ken Martin's avatar
Ken Martin committed
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455

  // for CE only compute on valid extents
  // because there is bleeding at the extent
  // edges that will result in correct scaling
  // if it's used.
  size_t nComputeExtents2 = licExtents.size();
  const deque<vtkPixelExtent> &computeExtents2 = licExtents;

  nbds = 4*nComputeExtents2;
  vector<float> computeBounds2(nbds, 0.0f);

  for (size_t i=0; i<nComputeExtents2; ++i)
1456
  {
Ken Martin's avatar
Ken Martin committed
1457 1458 1459 1460 1461 1462
    const vtkPixelExtent &computeExtent2 = computeExtents2[i];
    float *bound = &computeBounds2[4*i];
    bound[0] = ((float)computeExtent2[0])/((float)inputTexSize[0]);
    bound[1] = ((float)computeExtent2[1]+1.0f)/((float)inputTexSize[0]);
    bound[2] = ((float)computeExtent2[2])/((float)inputTexSize[1]);
    bound[3] = ((float)computeExtent2[3]+1.0f)/((float)inputTexSize[1]);
1463
  }
Ken Martin's avatar
Ken Martin committed
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504

  // during integration texture coordinates for
  // noise lookup is computed using the vector
  // texture coordinate this ensures that on any
  // rank we get the same noise value
  unsigned int noiseTexSize[2] = {
        noiseTex->GetWidth(),
        noiseTex->GetHeight()
        };

  vtkPixelExtent noiseExtent(noiseTexSize[0], noiseTexSize[1]);

  float noiseBoundsPt1[2];
  noiseBoundsPt1[0] = ((float)noiseTexSize[0]+1.0f)/((float)inputTexSize[0]);
  noiseBoundsPt1[1] = ((float)noiseTexSize[1]+1.0f)/((float)inputTexSize[1]);

  // bind our fbo
  this->FBO->SaveCurrentBindings();
  this->FBO->Bind(GL_FRAMEBUFFER);
  this->FBO->InitializeViewport(computeTexSize[0], computeTexSize[1]);

  // initialize the buffer mananger. Textures are assigned
  // and bound to individual units. These textures and units
  // are active and bound for the remainder of this execution.
  vtkLICPingPongBufferManager bufs(
        this->FBO,
        computeTexSize,
        vectorTex,
        maskVectorTex,
        noiseTex,
        this->EnhancedLIC,
        this->TransformVectors);

  #if  vtkLineIntegralConvolution2DDEBUG >= 1
  int rank = this->GetCommunicator()->GetRank();
  #endif
  #if vtkLineIntegralConvolution2DDEBUG >= 3
  bufs.WriteInputs(rank, vectorExtents);
  #endif

  if (this->TransformVectors)
1505
  {
Ken Martin's avatar
Ken Martin committed
1506 1507 1508 1509 1510
    // ------------------------------------------- begin normalized image space transform
    #if defined(vtkLineIntegralConvolution2DTIME)
    this->StartTimerEvent("vtkLineIntegralConvolution::TransformVectors");
    #endif

1511
    renWin->GetShaderCache()->ReadyShaderProgram(this->VTShader->Program);
Ken Martin's avatar
Ken Martin committed
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
    bufs.AttachImageVectorBuffer(this->FBO);

    float fTexSize[2];
    fTexSize[0] = inputTexSize[0];
    fTexSize[1] = inputTexSize[1];
    this->VTShader->Program->SetUniform2f("uTexSize", fTexSize);
    this->VTShader->Program->SetUniformi("texVectors", bufs.GetVectorTextureUnit());
    vtkOpenGLCheckErrorMacro("failed");
    // essential to initialize the entire buffer
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    vtkOpenGLCheckErrorMacro("failed");
    glClear(GL_COLOR_BUFFER_BIT);
    vtkOpenGLCheckErrorMacro("failed");
    size_t nVectorExtents = vectorExtents.size();
    for (size_t q=0; q<nVectorExtents; ++q)
1527
    {
Ken Martin's avatar
Ken Martin committed
1528 1529 1530
      bufs.RenderQuad(&computeBounds1[4*q], computeExtents1[q],
        this->VTShader);
    vtkOpenGLCheckErrorMacro("failed");
1531
    }
Ken Martin's avatar
Ken Martin committed
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
    bufs.DettachImageVectorBuffer(this->FBO);
    vtkOpenGLCheckErrorMacro("failed");

    #if (vtkLineIntegralConvolution2DDEBUG >= 2)
    bufs.WriteImageVectorBuffer(rank, vectorExtents);
    #endif

    #if defined(vtkLineIntegralConvolution2DTIME)
    this->EndTimerEvent("vtkLineIntegralConvolution::TransformVectors");
    #endif
    // ------------------------------------------- end normalized image space transform
1543
  }
Ken Martin's avatar
Ken Martin committed
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557

  // --------------------------------------------- begin first-pass LIC
  #if defined(vtkLineIntegralConvolution2DTIME)
  this->StartTimerEvent("vtkLineIntegralConvolution::Integrate1");
  #endif

  //
  // initialize convolution and seeds
  //
  bufs.ClearBuffers(this->FBO, inputTexExtent, vectorExtents, this->EnhancedLIC);
  bufs.AttachVectorTextures();
  bufs.AttachNoiseTexture(0);
  bufs.AttachLICBuffers(this->FBO);

1558
  renWin->GetShaderCache()->ReadyShaderProgram(this->LIC0Shader->Program);
Ken Martin's avatar
Ken Martin committed
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
  this->LIC0Shader->Program->SetUniformi("uStepNo", 0);
  this->LIC0Shader->Program->SetUniformi("uPassNo", 0);
  this->LIC0Shader->Program->SetUniformf("uMaskThreshold", this->MaskThreshold);
  this->LIC0Shader->Program->SetUniform2f("uNoiseBoundsPt1", noiseBoundsPt1);
    vtkOpenGLStaticCheckErrorMacro("failed at RenderQuad");
  this->LIC0Shader->Program->SetUniformi("texMaskVectors", bufs.GetMaskVectorTextureUnit());
    vtkOpenGLStaticCheckErrorMacro("failed at RenderQuad");
  this->LIC0Shader->Program->SetUniformi("texNoise", bufs.GetNoiseTextureUnit(0));
    vtkOpenGLStaticCheckErrorMacro("failed at RenderQuad");
  this->LIC0Shader->Program->SetUniformi("texLIC", bufs.GetLICTextureUnit());
    vtkOpenGLStaticCheckErrorMacro("failed at RenderQuad");

  for (size_t q=0; q<nComputeExtents1; ++q)
1572
  {
Ken Martin's avatar
Ken Martin committed
1573 1574
    bufs.RenderQuad(&computeBounds1[4*q], computeExtents1[q],
      this->LIC0Shader);
1575
  }
Ken Martin's avatar
Ken Martin committed
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
  bufs.DettachLICBuffers(this->FBO);
  bufs.Swap();

  #if (vtkLineIntegralConvolution2DDEBUG >= 2)
  bufs.WriteBuffers(rank,"lic2d_lic0b_a.vtm","lic2d_lic0b_s.vtm",computeExtents1);
  #endif

  //
  // backward LIC
  //
1586
  renWin->GetShaderCache()->ReadyShaderProgram(this->LICIShader->Program);
Ken Martin's avatar
Ken Martin committed
1587 1588 1589 1590 1591 1592 1593 1594 1595
  this->LICIShader->Program->SetUniformi("uPassNo", 0);
  this->LICIShader->Program->SetUniformf("uStepSize", -this->StepSize);
  this->LICIShader->Program->SetUniform2f("uNoiseBoundsPt1", noiseBoundsPt1);
  this->LICIShader->Program->SetUniformi("texVectors",
    bufs.GetImageVectorTextureUnit());
  this->LICIShader->Program->SetUniformi("texNoise", bufs.GetNoiseTextureUnit(0));

  int stepNum = 0;
  for (int stepIdx=0; stepIdx<this->NumberOfSteps; ++stepIdx, ++stepNum)
1596
  {
Ken Martin's avatar
Ken Martin committed
1597 1598 1599 1600
    bufs.AttachLICBuffers(this->FBO);
    this->LICIShader->Program->SetUniformi("texLIC", bufs.GetLICTextureUnit());
    this->LICIShader->Program->SetUniformi("texSeedPts", bufs.GetSeedTextureUnit());
    for (size_t q=0; q<nComputeExtents1; ++q)
1601
    {
Ken Martin's avatar
Ken Martin committed
1602 1603
      bufs.RenderQuad(&computeBounds1[4*q], computeExtents1[q],
        this->LICIShader);
1604
    }
Ken Martin's avatar
Ken Martin committed
1605 1606
    bufs.DettachLICBuffers(this->FBO);
    bufs.Swap();
1607
  }
Ken Martin's avatar
Ken Martin committed
1608 1609

  #if (vtkLineIntegralConvolution2DDEBUG >= 2)
1610
  bufs.WriteBuffers(rank,"lic2d_licib_a.vtm", "lic2d_licib_s.vtm", computeExtents1);
Ken Martin's avatar
Ken Martin committed
1611 1612 1613 1614
  #endif

  // initialize seeds
  //
1615
  renWin->GetShaderCache()->ReadyShaderProgram(this->LIC0Shader->Program);
Ken Martin's avatar
Ken Martin committed
1616 1617 1618 1619
  this->LIC0Shader->Program->SetUniformi("uStepNo", 1);

  bufs.AttachLICBuffers(this->FBO);
  for (size_t q=0; q<nComputeExtents1; ++q)
1620
  {
Ken Martin's avatar
Ken Martin committed
1621 1622
    bufs.RenderQuad(&computeBounds1[4*q], computeExtents1[q],
      this->LIC0Shader);
1623
  }
Ken Martin's avatar
Ken Martin committed
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
  bufs.DettachLICBuffers(this->FBO);
  bufs.Swap();

  #if (vtkLineIntegralConvolution2DDEBUG >= 2)
  bufs.WriteBuffers(rank,"lic2d_lic0f_a.vtm", "lic2d_lic0f_s.vtm", computeExtents1);
  #endif

  //
  // forward LIC
  //
1634
  renWin->GetShaderCache()->ReadyShaderProgram(this->LICIShader->Program);
Ken Martin's avatar
Ken Martin committed
1635 1636 1637
  this->LICIShader->Program->SetUniformf("uStepSize", this->StepSize);

  for (int stepIdx=0; stepIdx<this->NumberOfSteps; ++stepIdx, ++stepNum)
1638
  {
Ken Martin's avatar
Ken Martin committed
1639 1640 1641 1642
    bufs.AttachLICBuffers(this->FBO);
    this->LICIShader->Program->SetUniformi("texLIC", bufs.GetLICTextureUnit());
    this->LICIShader->Program->SetUniformi("texSeedPts", bufs.GetSeedTextureUnit());
    for (size_t q=0; q<nComputeExtents1; ++q)
1643
    {
Ken Martin's avatar
Ken Martin committed
1644 1645
      bufs.RenderQuad(&computeBounds1[4*q], computeExtents1[q],
        this->LICIShader);