vtkTextureObject.cxx 57.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkTextureObject.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 "vtkTextureObject.h"

Ken Martin's avatar
Ken Martin committed
17
#include "vtk_glew.h"
18 19

#include "vtkObjectFactory.h"
20 21


Ken Martin's avatar
Ken Martin committed
22
#if GL_ES_VERSION_2_0 != 1 || GL_ES_VERSION_3_0 == 1
23
#include "vtkPixelBufferObject.h"
24 25 26
#endif

#include "vtkNew.h"
Ken Martin's avatar
Ken Martin committed
27
#include "vtkOpenGLBufferObject.h"
28
#include "vtkOpenGLError.h"
29
#include "vtkOpenGLRenderUtilities.h"
30 31
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLShaderCache.h"
32
#include "vtkOpenGLTexture.h"
Ken Martin's avatar
Ken Martin committed
33
#include "vtkOpenGLVertexArrayObject.h"
34 35
#include "vtkRenderer.h"
#include "vtkShaderProgram.h"
36

Ken Martin's avatar
Ken Martin committed
37
#include "vtkOpenGLHelper.h"
38 39 40 41 42 43 44 45 46 47

#include <cassert>

//#define VTK_TO_DEBUG
//#define VTK_TO_TIMING

#ifdef VTK_TO_TIMING
#include "vtkTimerLog.h"
#endif

48 49 50
#include "vtkTextureObjectFS.h"
#include "vtkTextureObjectVS.h"  // a pass through shader

51 52 53
#define BUFFER_OFFSET(i) (static_cast<char *>(NULL) + (i))

// Mapping from DepthTextureCompareFunction values to OpenGL values.
54
//----------------------------------------------------------------------------
55 56 57 58 59 60 61 62 63 64 65 66
static GLint OpenGLDepthTextureCompareFunction[8]=
{
  GL_LEQUAL,
  GL_GEQUAL,
  GL_LESS,
  GL_GREATER,
  GL_EQUAL,
  GL_NOTEQUAL,
  GL_ALWAYS,
  GL_NEVER
};

67
//----------------------------------------------------------------------------
68 69 70 71 72 73 74 75 76 77 78 79
static const char *DepthTextureCompareFunctionAsString[8]=
{
  "Lequal",
  "Gequal",
  "Less",
  "Greater",
  "Equal",
  "NotEqual",
  "AlwaysTrue",
  "Never"
};

80 81
// Mapping from Wrap values to OpenGL values
#if GL_ES_VERSION_2_0 != 1
Aashish Chaudhary's avatar
Aashish Chaudhary committed
82
  //--------------------------------------------------------------------------
83 84 85 86 87 88 89 90
  static GLint OpenGLWrap[4]=
  {
    GL_CLAMP_TO_EDGE,
    GL_REPEAT,
    GL_MIRRORED_REPEAT,
    GL_CLAMP_TO_BORDER
  };

Aashish Chaudhary's avatar
Aashish Chaudhary committed
91
  //--------------------------------------------------------------------------
92 93 94 95 96 97 98
  static const char *WrapAsString[4]=
  {
    "ClampToEdge",
    "Repeat",
    "MirroredRepeat",
    "ClampToBorder"
  };
Ken Martin's avatar
Ken Martin committed
99 100 101 102

  //----------------------------------------------------------------------------
  static GLenum OpenGLAlphaInternalFormat[5]=
  {
103
    GL_R8,
104 105 106 107
    GL_R8,
    GL_R16,
    GL_R16F,
    GL_R32F
Ken Martin's avatar
Ken Martin committed
108 109
  };

110
#else
Aashish Chaudhary's avatar
Aashish Chaudhary committed
111
  //--------------------------------------------------------------------------
112 113 114 115 116 117 118
  static GLint OpenGLWrap[3]=
  {
    GL_CLAMP_TO_EDGE,
    GL_REPEAT,
    GL_MIRRORED_REPEAT
  };

Aashish Chaudhary's avatar
Aashish Chaudhary committed
119
  //--------------------------------------------------------------------------
120 121 122 123 124 125
  static const char *WrapAsString[3]=
  {
    "ClampToEdge",
    "Repeat",
    "MirroredRepeat"
  };
Ken Martin's avatar
Ken Martin committed
126 127

  //----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
128
  // Should be GL_RED but that requires an extension for ES 2.0
Ken Martin's avatar
Ken Martin committed
129 130
  static GLenum OpenGLAlphaInternalFormat[5]=
  {
Ken Martin's avatar
Ken Martin committed
131 132 133 134 135
    GL_LUMINANCE,
    GL_LUMINANCE,
    GL_LUMINANCE,
    GL_LUMINANCE,
    GL_LUMINANCE
Ken Martin's avatar
Ken Martin committed
136 137
  };

138
#endif
139 140

// Mapping MinificationFilter values to OpenGL values.
141
//----------------------------------------------------------------------------
142 143 144 145 146 147 148 149 150 151 152
static GLint OpenGLMinFilter[6]=
{
  GL_NEAREST,
  GL_LINEAR,
  GL_NEAREST_MIPMAP_NEAREST,
  GL_NEAREST_MIPMAP_LINEAR,
  GL_LINEAR_MIPMAP_NEAREST,
  GL_LINEAR_MIPMAP_LINEAR
};

// Mapping MagnificationFilter values to OpenGL values.
153
//----------------------------------------------------------------------------
154 155 156 157 158 159
static GLint OpenGLMagFilter[6]=
{
  GL_NEAREST,
  GL_LINEAR
};

160
//----------------------------------------------------------------------------
161 162 163 164 165 166 167 168 169 170
static const char *MinMagFilterAsString[6]=
{
  "Nearest",
  "Linear",
  "NearestMipmapNearest",
  "NearestMipmapLinear",
  "LinearMipmapNearest",
  "LinearMipmapLinear"
};

171
//----------------------------------------------------------------------------
172 173 174 175
static GLenum OpenGLDepthInternalFormat[5]=
{
  GL_DEPTH_COMPONENT,
  GL_DEPTH_COMPONENT16,
176
#ifdef GL_DEPTH_COMPONENT24
177
  GL_DEPTH_COMPONENT24,
178 179 180 181
#else
  GL_DEPTH_COMPONENT16,
#endif
#ifdef GL_DEPTH_COMPONENT32
182
  GL_DEPTH_COMPONENT32,
183 184 185 186
#else
  GL_DEPTH_COMPONENT16,
#endif
#ifdef GL_DEPTH_COMPONENT32F
187
  GL_DEPTH_COMPONENT32F
188 189 190
#else
  GL_DEPTH_COMPONENT16
#endif
191 192
};

193
//----------------------------------------------------------------------------
194 195 196 197 198 199
static GLenum OpenGLDepthInternalFormatType[5]=
{
  GL_UNSIGNED_INT,
  GL_UNSIGNED_INT,
  GL_UNSIGNED_INT,
  GL_UNSIGNED_INT,
200
#ifdef GL_DEPTH_COMPONENT32F
201
  GL_FLOAT
202 203 204
#else
  GL_UNSIGNED_INT
#endif
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
};

/*
static const char *DepthInternalFormatFilterAsString[6]=
{
  "Native",
  "Fixed16",
  "Fixed24",
  "Fixed32",
  "Float32"
};
*/

//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkTextureObject);

//----------------------------------------------------------------------------
vtkTextureObject::vtkTextureObject()
{
  this->Context = NULL;
  this->Handle = 0;
  this->NumberOfDimensions = 0;
  this->Target = 0;
  this->Components = 0;
  this->Width = 0;
  this->Height = 0;
  this->Depth = 0;
  this->RequireTextureInteger = false;
  this->SupportsTextureInteger = false;
  this->RequireTextureFloat = false;
  this->SupportsTextureFloat = false;
  this->RequireDepthBufferFloat = false;
  this->SupportsDepthBufferFloat = false;
  this->AutoParameters = 1;
  this->WrapS = Repeat;
  this->WrapT = Repeat;
  this->WrapR = Repeat;
  this->MinificationFilter = Nearest;
  this->MagnificationFilter = Nearest;
  this->MinLOD = -1000.0f;
  this->MaxLOD = 1000.0f;
  this->BaseLevel = 0;
  this->MaxLevel = 0;
  this->DepthTextureCompare = false;
  this->DepthTextureCompareFunction = Lequal;
  this->GenerateMipmap = false;
251
  this->ShaderProgram = NULL;
252 253 254 255
  this->BorderColor[0] = 0.0f;
  this->BorderColor[1] = 0.0f;
  this->BorderColor[2] = 0.0f;
  this->BorderColor[3] = 0.0f;
256
  this->BufferObject = 0;
257 258

  this->ResetFormatAndType();
259 260 261 262 263 264
}

//----------------------------------------------------------------------------
vtkTextureObject::~vtkTextureObject()
{
  this->DestroyTexture();
Ken Martin's avatar
Ken Martin committed
265 266 267 268 269
  if (this->ShaderProgram)
    {
    delete this->ShaderProgram;
    this->ShaderProgram = NULL;
    }
270 271 272
}

//----------------------------------------------------------------------------
273
bool vtkTextureObject::IsSupported(vtkOpenGLRenderWindow* vtkNotUsed(win),
274 275 276 277
      bool requireTexFloat,
      bool requireDepthFloat,
      bool requireTexInt)
{
278
#if GL_ES_VERSION_2_0 != 1
279 280 281 282
  if (vtkOpenGLRenderWindow::GetContextSupportsOpenGL32())
    {
    return true;
    }
283 284 285
  bool texFloat = true;
  if (requireTexFloat)
    {
286 287
    texFloat = (glewIsSupported("GL_ARB_texture_float") != 0
     && glewIsSupported("GL_ARB_texture_rg") != 0);
288 289 290 291 292
    }

  bool depthFloat = true;
  if (requireDepthFloat)
    {
293
    depthFloat = (glewIsSupported("GL_ARB_depth_buffer_float") != 0);
294 295 296 297 298
    }

  bool texInt = true;
  if (requireTexInt)
    {
299
    texInt = (glewIsSupported("GL_EXT_texture_integer") != 0);
300 301
    }

302
#else
303
  bool texFloat = true;
304 305
  bool depthFloat = !requireDepthFloat;
  bool texInt = !requireTexInt;
306 307 308 309 310
#if GL_ES_VERSION_3_0 == 1
  texFloat = true;
  depthFloat = true;  // I think this is the case
  texInt = true;
#endif
311 312
#endif

313 314 315 316
  return texFloat && depthFloat && texInt;
}

//----------------------------------------------------------------------------
317
bool vtkTextureObject::LoadRequiredExtensions(vtkOpenGLRenderWindow *renWin)
318
{
319
#if GL_ES_VERSION_2_0 != 1
320 321 322 323 324 325 326 327 328 329
  if (vtkOpenGLRenderWindow::GetContextSupportsOpenGL32())
    {
    this->SupportsTextureInteger = true;
    this->SupportsTextureFloat = true;
    this->SupportsDepthBufferFloat = true;
    }
  else
    {
    this->SupportsTextureInteger =
      (glewIsSupported("GL_EXT_texture_integer") != 0);
330

331
    this->SupportsTextureFloat =
332 333
      (glewIsSupported("GL_ARB_texture_float") != 0 &&
       glewIsSupported("GL_ARB_texture_rg") != 0);
334

335 336 337
    this->SupportsDepthBufferFloat =
      (glewIsSupported("GL_ARB_depth_buffer_float") != 0);
    }
338 339 340 341
#else
  // some of these may have extensions etc for ES 2.0
  // setting to false right now as I do not know
  this->SupportsTextureInteger = false;
342
  this->SupportsTextureFloat = true;
343
  this->SupportsDepthBufferFloat = false;
344 345 346 347 348
#if GL_ES_VERSION_3_0 == 1
  this->SupportsTextureInteger = true;
  this->SupportsTextureFloat = true;
  this->SupportsDepthBufferFloat = true;
#endif
349
#endif
350

351 352 353 354
  return this->IsSupported(renWin,
    this->RequireTextureFloat,
    this->RequireDepthBufferFloat,
    this->RequireTextureInteger);
355 356 357
}

//----------------------------------------------------------------------------
358
void vtkTextureObject::SetContext(vtkOpenGLRenderWindow* renWin)
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
{
  // avoid pointless reassignment
  if (this->Context == renWin)
    {
    return;
    }
  // free previous resources
  this->DestroyTexture();
  this->Context = NULL;
  this->Modified();
  // all done if assigned null
  if (!renWin)
    {
    return;
    }

375
  if (!this->LoadRequiredExtensions(renWin) )
376 377 378 379 380 381 382 383 384 385
    {
    vtkErrorMacro("Required OpenGL extensions not supported by the context.");
    return;
    }
  // initialize
  this->Context = renWin;
  this->Context->MakeCurrent();
}

//----------------------------------------------------------------------------
386
vtkOpenGLRenderWindow* vtkTextureObject::GetContext()
387
{
388
  return this->Context;
389 390 391 392 393
}

//----------------------------------------------------------------------------
void vtkTextureObject::DestroyTexture()
{
394 395 396
  // deactivate it first
  this->Deactivate();

397 398 399 400 401 402 403 404 405 406 407 408 409
  // because we don't hold a reference to the render
  // context we don't have any control on when it is
  // destroyed. In fact it may be destroyed before
  // we are(eg smart pointers), in which case we should
  // do nothing.
  if (this->Context && this->Handle)
    {
    GLuint tex = this->Handle;
    glDeleteTextures(1, &tex);
    vtkOpenGLCheckErrorMacro("failed at glDeleteTexture");
    }
  this->Handle = 0;
  this->NumberOfDimensions = 0;
410
  this->Target = 0;
411 412
  this->Components = 0;
  this->Width = this->Height = this->Depth = 0;
413
  this->ResetFormatAndType();
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
}

//----------------------------------------------------------------------------
void vtkTextureObject::CreateTexture()
{
  assert(this->Context);

  // reuse the existing handle if we have one
  if (!this->Handle)
    {
    GLuint tex=0;
    glGenTextures(1, &tex);
    vtkOpenGLCheckErrorMacro("failed at glGenTextures");
    this->Handle=tex;

Ken Martin's avatar
Ken Martin committed
429
#if defined(GL_TEXTURE_BUFFER)
430
    if (this->Target && this->Target != GL_TEXTURE_BUFFER)
Ken Martin's avatar
Ken Martin committed
431 432 433
#else
    if (this->Target)
#endif
434 435 436 437 438 439 440
      {
      glBindTexture(this->Target, this->Handle);
      vtkOpenGLCheckErrorMacro("failed at glBindTexture");

      // See: http://www.opengl.org/wiki/Common_Mistakes#Creating_a_complete_texture
      // turn off mip map filter or set the base and max level correctly. here
      // both are done.
441 442 443 444
      glTexParameteri(this->Target, GL_TEXTURE_MIN_FILTER,
                      this->GetMinificationFilterMode(this->MinificationFilter));
      glTexParameteri(this->Target, GL_TEXTURE_MAG_FILTER,
                      this->GetMagnificationFilterMode(this->MagnificationFilter));
445

446 447 448 449
      glTexParameteri(this->Target, GL_TEXTURE_WRAP_S,
                      this->GetWrapSMode(this->WrapS));
      glTexParameteri(this->Target, GL_TEXTURE_WRAP_T,
                      this->GetWrapTMode(this->WrapT));
450

Ken Martin's avatar
Ken Martin committed
451
#if defined(GL_TEXTURE_3D)
452 453 454 455 456
      if (this->Target == GL_TEXTURE_3D)
        {
        glTexParameteri(this->Target, GL_TEXTURE_WRAP_R,
                        this->GetWrapRMode(this->WrapR));
        }
Ken Martin's avatar
Ken Martin committed
457
#endif
458

459
#ifdef GL_TEXTURE_BASE_LEVEL
460
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
461 462 463
#endif

#ifdef GL_TEXTURE_MAX_LEVEL
464
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
465
#endif
466 467 468 469 470 471

      glBindTexture(this->Target, 0);
      }
    }
}

Aashish Chaudhary's avatar
Aashish Chaudhary committed
472
//---------------------------------------------------------------------------
473 474
int vtkTextureObject::GetTextureUnit()
{
475 476 477 478 479
  if (this->Context)
    {
    return this->Context->GetTextureUnitForTexture(this);
    }
  return -1;
480 481
}

482
//---------------------------------------------------------------------------
483
void vtkTextureObject::Activate()
484
{
485 486
  // activate a free texture unit for this texture
  this->Context->ActivateTexture(this);
487 488 489 490
  this->Bind();
}

//---------------------------------------------------------------------------
491
void vtkTextureObject::Deactivate()
492
{
493 494
  if (this->Context)
    {
495
    this->Context->ActivateTexture(this);
496 497 498
    this->UnBind();
    this->Context->DeactivateTexture(this);
    }
499 500
}

Ken Martin's avatar
Ken Martin committed
501 502 503 504 505
//---------------------------------------------------------------------------
void vtkTextureObject::ReleaseGraphicsResources(vtkWindow *win)
{
  vtkOpenGLRenderWindow *rwin =
   vtkOpenGLRenderWindow::SafeDownCast(win);
Aashish Chaudhary's avatar
Aashish Chaudhary committed
506

507
  if (rwin && this->Handle)
508
    {
509 510
    rwin->MakeCurrent();

511 512 513 514 515 516 517 518 519 520 521 522 523
    rwin->ActivateTexture(this);
    this->UnBind();
    rwin->DeactivateTexture(this);
    GLuint tex = this->Handle;
    glDeleteTextures(1, &tex);
    this->Handle = 0;
    this->NumberOfDimensions = 0;
    this->Target =0;
    this->InternalFormat = 0;
    this->Format = 0;
    this->Type = 0;
    this->Components = 0;
    this->Width = this->Height = this->Depth = 0;
524
    }
525 526 527 528 529 530
  if (this->ShaderProgram)
    {
    this->ShaderProgram->ReleaseGraphicsResources(win);
    delete this->ShaderProgram;
    this->ShaderProgram = NULL;
    }
Ken Martin's avatar
Ken Martin committed
531 532
}

533 534 535 536 537 538 539 540 541
//----------------------------------------------------------------------------
void vtkTextureObject::Bind()
{
  assert(this->Context);
  assert(this->Handle);

  glBindTexture(this->Target, this->Handle);
  vtkOpenGLCheckErrorMacro("failed at glBindTexture");

542
  if (this->AutoParameters && (this->GetMTime() > this->SendParametersTime))
543 544 545 546 547 548 549 550
    {
    this->SendParameters();
    }
}

//----------------------------------------------------------------------------
void vtkTextureObject::UnBind()
{
551 552 553 554 555
  if (this->Target)
    {
    glBindTexture(this->Target, 0);
    vtkOpenGLCheckErrorMacro("failed at glBindTexture(0)");
    }
556 557 558 559 560 561 562 563 564 565 566
}

//----------------------------------------------------------------------------
bool vtkTextureObject::IsBound()
{
  bool result=false;
  if(this->Context && this->Handle)
    {
    GLenum target=0; // to avoid warnings.
    switch(this->Target)
      {
567
#if defined(GL_TEXTURE_1D) && defined(GL_TEXTURE_BINDING_1D)
568 569 570
      case GL_TEXTURE_1D:
        target=GL_TEXTURE_BINDING_1D;
        break;
571
#endif
572 573 574
      case GL_TEXTURE_2D:
        target=GL_TEXTURE_BINDING_2D;
        break;
575
#if defined(GL_TEXTURE_3D) && defined(GL_TEXTURE_BINDING_3D)
576 577 578
      case GL_TEXTURE_3D:
        target=GL_TEXTURE_BINDING_3D;
        break;
579 580 581 582 583
#endif
#if defined(GL_TEXTURE_BUFFER) && defined(GL_TEXTURE_BINDING_BUFFER)
      case GL_TEXTURE_BUFFER:
        target=GL_TEXTURE_BINDING_BUFFER;
        break;
584
#endif
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
      default:
        assert("check: impossible case" && 0);
        break;
      }
    GLint objectId;
    glGetIntegerv(target,&objectId);
    result=static_cast<GLuint>(objectId)==this->Handle;
    }
  return result;
}

//----------------------------------------------------------------------------
void vtkTextureObject::SendParameters()
{
  assert("pre: is_bound" && this->IsBound());

Ken Martin's avatar
Ken Martin committed
601
#if defined(GL_TEXTURE_BUFFER)
602 603 604 605
  if (this->Target == GL_TEXTURE_BUFFER)
    {
    return;
    }
Ken Martin's avatar
Ken Martin committed
606
#endif
607

608
  glTexParameteri(this->Target,GL_TEXTURE_WRAP_S, OpenGLWrap[this->WrapS]);
609
  glTexParameteri(this->Target,GL_TEXTURE_WRAP_T, OpenGLWrap[this->WrapT]);
610

611
#ifdef GL_TEXTURE_WRAP_R
612 613 614 615
  glTexParameteri(
        this->Target,
        GL_TEXTURE_WRAP_R,
        OpenGLWrap[this->WrapR]);
616
#endif
617 618 619 620 621 622 623 624 625 626 627

  glTexParameteri(
        this->Target,
        GL_TEXTURE_MIN_FILTER,
        OpenGLMinFilter[this->MinificationFilter]);

  glTexParameteri(
        this->Target,
        GL_TEXTURE_MAG_FILTER,
        OpenGLMagFilter[this->MagnificationFilter]);

628 629
#if GL_ES_VERSION_2_0 != 1 || GL_ES_VERSION_3_0 == 1
#if GL_ES_VERSION_3_0 != 1
630
  glTexParameterfv(this->Target, GL_TEXTURE_BORDER_COLOR, this->BorderColor);
631

Ken Martin's avatar
Ken Martin committed
632
  if(this->DepthTextureCompare)
633 634 635 636
    {
    glTexParameteri(
          this->Target,
          GL_TEXTURE_COMPARE_MODE,
Ken Martin's avatar
Ken Martin committed
637
          GL_COMPARE_REF_TO_TEXTURE);
638 639 640 641 642 643 644 645
    }
  else
    {
    glTexParameteri(
          this->Target,
          GL_TEXTURE_COMPARE_MODE,
          GL_NONE);
    }
646 647
#endif

648 649 650 651
  glTexParameterf(this->Target, GL_TEXTURE_MIN_LOD, this->MinLOD);
  glTexParameterf(this->Target, GL_TEXTURE_MAX_LOD, this->MaxLOD);
  glTexParameteri(this->Target, GL_TEXTURE_BASE_LEVEL, this->BaseLevel);
  glTexParameteri(this->Target, GL_TEXTURE_MAX_LEVEL, this->MaxLevel);
652 653 654 655 656

  glTexParameteri(
        this->Target,
        GL_TEXTURE_COMPARE_FUNC,
        OpenGLDepthTextureCompareFunction[this->DepthTextureCompareFunction]);
657
#endif
658 659 660 661 662

  vtkOpenGLCheckErrorMacro("failed after SendParameters");
  this->SendParametersTime.Modified();
}

663

664 665 666 667
//----------------------------------------------------------------------------
unsigned int vtkTextureObject::GetInternalFormat(int vtktype, int numComps,
                                                 bool shaderSupportsTextureInt)
{
668 669 670 671
  if (this->InternalFormat)
    {
    return this->InternalFormat;
    }
672

673
  // pre-condition
674
  if(vtktype == VTK_VOID && numComps != 1)
675
    {
676 677
    vtkErrorMacro("Depth component texture must have 1 component only (" <<
                  numComps << " requested");
678 679
    this->InternalFormat = 0;
    return this->InternalFormat;
680 681
    }

682 683 684 685
  this->InternalFormat =
    this->GetDefaultInternalFormat(vtktype, numComps, shaderSupportsTextureInt);

  if (!this->InternalFormat)
686
    {
687 688 689
    vtkDebugMacro("Unable to find suitable internal format for T="
                  << vtktype << " NC=" << numComps << " SSTI="
                  << shaderSupportsTextureInt);
690
    }
691

692
  return this->InternalFormat;
693 694
}

695
//----------------------------------------------------------------------------
696 697 698
unsigned int vtkTextureObject::GetDefaultInternalFormat(
  int vtktype, int numComps,
  bool shaderSupportsTextureInt)
699
{
700 701 702 703
  GLenum result = 0;

  // if shader supports int textures try that first
  if (shaderSupportsTextureInt)
704
    {
705 706 707 708
    result = this->Context->GetDefaultTextureInternalFormat(
      vtktype,numComps,true,false);
    if (!result)
      {
709
      vtkDebugMacro("Unsupported internal texture type!");
710 711
      }
    return result;
712
    }
713 714 715 716 717

  // try default next
  result = this->Context->GetDefaultTextureInternalFormat(
    vtktype,numComps,false,false);
  if (result)
718
    {
719
    return result;
720 721
    }

722 723 724 725 726
  // try floating point
  result = this->Context->GetDefaultTextureInternalFormat(
    vtktype,numComps,false,true);

  if (!result)
727
    {
728 729
    vtkDebugMacro("Unsupported internal texture type!");
    vtkDebugMacro("Unable to find suitable internal format for T="
730
      << vtktype << " NC=" << numComps << " SSTI=" << shaderSupportsTextureInt);
731
    }
732 733

  return result;
734 735 736
}


737 738 739 740 741 742 743 744 745 746
//----------------------------------------------------------------------------
void vtkTextureObject::SetInternalFormat(unsigned int glInternalFormat)
{
  if (this->InternalFormat != glInternalFormat)
    {
    this->InternalFormat = glInternalFormat;
    this->Modified();
    }
}

747
//----------------------------------------------------------------------------
748 749 750
unsigned int vtkTextureObject::GetFormat(int vtktype, int numComps,
                                         bool shaderSupportsTextureInt)
{
751
  if (!this->Format)
752
    {
753 754
    this->Format = this->GetDefaultFormat(vtktype,
      numComps, shaderSupportsTextureInt);
755
    }
756 757 758
  return this->Format;
}

759

760 761 762 763
//----------------------------------------------------------------------------
unsigned int vtkTextureObject::GetDefaultFormat(int vtktype, int numComps,
                                                bool shaderSupportsTextureInt)
{
764 765
  if (vtktype == VTK_VOID)
    {
766
    return GL_DEPTH_COMPONENT;
767 768
    }

769
#if GL_ES_VERSION_2_0 != 1
770 771 772 773 774 775 776 777
  if(this->SupportsTextureInteger && shaderSupportsTextureInt
     && (vtktype==VTK_SIGNED_CHAR||vtktype==VTK_UNSIGNED_CHAR||
         vtktype==VTK_SHORT||vtktype==VTK_UNSIGNED_SHORT||vtktype==VTK_INT||
       vtktype==VTK_UNSIGNED_INT))
    {
    switch (numComps)
      {
      case 1:
778
        return GL_RED_INTEGER;
779
      case 2:
780
        return GL_RG_INTEGER;
781
      case 3:
782
        return GL_RGB_INTEGER_EXT;
783
      case 4:
784
        return GL_RGBA_INTEGER_EXT;
785 786 787 788 789 790 791
      }
    }
  else
    {
    switch (numComps)
      {
      case 1:
792
        return GL_RED;
793
      case 2:
794
        return GL_RG;
795
      case 3:
796
        return GL_RGB;
797
      case 4:
798
        return GL_RGBA;
799
      }
Ken Martin's avatar
Ken Martin committed
800 801 802 803
#else
    {
    switch (numComps)
      {
804 805 806 807 808 809
#ifdef GL_RED
      case 1:
        return GL_RED;
      case 2:
        return GL_RG;
#else
Ken Martin's avatar
Ken Martin committed
810
      case 1:
811
        return GL_LUMINANCE;
Ken Martin's avatar
Ken Martin committed
812
      case 2:
813 814
        return GL_LUMINANCE_ALPHA;
#endif
Ken Martin's avatar
Ken Martin committed
815
      case 3:
816
        return GL_RGB;
Ken Martin's avatar
Ken Martin committed
817
      case 4:
818
        return GL_RGBA;
Ken Martin's avatar
Ken Martin committed
819 820
      }
#endif
821
    }
822
  return GL_RGB;
823 824
}

825 826 827 828 829 830 831 832
//----------------------------------------------------------------------------
void vtkTextureObject::SetFormat(unsigned int glFormat)
{
  if (this->Format != glFormat)
    {
    this->Format = glFormat;
    this->Modified();
    }
833 834
}

835 836 837 838 839 840 841 842
//----------------------------------------------------------------------------
void vtkTextureObject::ResetFormatAndType()
{
  this->Format = 0;
  this->InternalFormat = 0;
  this->Type = 0;
}

843
//----------------------------------------------------------------------------
844
int vtkTextureObject::GetDefaultDataType(int vtk_scalar_type)
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
{
  // DON'T DEAL with VTK_CHAR as this is platform dependent.
  switch (vtk_scalar_type)
    {
  case VTK_SIGNED_CHAR:
    return GL_BYTE;

  case VTK_UNSIGNED_CHAR:
    return GL_UNSIGNED_BYTE;

  case VTK_SHORT:
    return GL_SHORT;

  case VTK_UNSIGNED_SHORT:
    return GL_UNSIGNED_SHORT;

  case VTK_INT:
    return GL_INT;

  case VTK_UNSIGNED_INT:
    return GL_UNSIGNED_INT;

  case VTK_FLOAT:
  case VTK_VOID: // used for depth component textures.
    return GL_FLOAT;
    }
  return 0;
}

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
static int vtkGetVTKType(GLenum gltype)
{
   // DON'T DEAL with VTK_CHAR as this is platform dependent.
  switch (gltype)
    {
  case GL_BYTE:
    return VTK_SIGNED_CHAR;

  case GL_UNSIGNED_BYTE:
    return VTK_UNSIGNED_CHAR;

  case GL_SHORT:
    return VTK_SHORT;

  case GL_UNSIGNED_SHORT:
    return VTK_UNSIGNED_SHORT;

  case GL_INT:
    return VTK_INT;

  case GL_UNSIGNED_INT:
    return VTK_UNSIGNED_INT;

  case GL_FLOAT:
    return VTK_FLOAT;
    }

  return 0;
}

Ken Martin's avatar
Ken Martin committed
905 906 907 908 909 910
//----------------------------------------------------------------------------
int vtkTextureObject::GetVTKDataType()
{
  return ::vtkGetVTKType(this->Type);
}

911
//----------------------------------------------------------------------------
912
int vtkTextureObject::GetDataType(int vtk_scalar_type)
913
{
914 915
  if (!this->Type)
    {
916
    this->Type = this->GetDefaultDataType(vtk_scalar_type);
917 918 919
    }

  return this->Type;
920 921
}

922 923 924 925 926 927 928 929 930 931
//----------------------------------------------------------------------------
void vtkTextureObject::SetDataType(unsigned int glType)
{
  if (this->Type != glType)
    {
    this->Type = glType;
    this->Modified();
    }
}

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
//----------------------------------------------------------------------------
unsigned int vtkTextureObject::GetMinificationFilterMode(int vtktype)
{
  switch(vtktype)
    {
    case Nearest:
      return GL_NEAREST;
    case Linear:
      return GL_LINEAR;
    case NearestMipmapNearest:
      return GL_NEAREST_MIPMAP_NEAREST;
    case NearestMipmapLinear:
      return GL_NEAREST_MIPMAP_LINEAR;
    case LinearMipmapNearest:
      return GL_LINEAR_MIPMAP_NEAREST;
    case LinearMipmapLinear:
      return GL_LINEAR_MIPMAP_LINEAR;
    default:
      return GL_NEAREST;
    }
}

//----------------------------------------------------------------------------
unsigned int vtkTextureObject::GetMagnificationFilterMode(int vtktype)
{
  switch(vtktype)
    {
    case Nearest:
      return GL_NEAREST;
    case Linear:
      return GL_LINEAR;
    default:
      return GL_NEAREST;
    }
}

//----------------------------------------------------------------------------
unsigned int vtkTextureObject::GetWrapSMode(int vtktype)
{
  switch(vtktype)
    {
    case ClampToEdge:
      return GL_CLAMP_TO_EDGE;
    case Repeat:
      return GL_REPEAT;
Ken Martin's avatar
Ken Martin committed
977
#ifdef GL_CLAMP_TO_BORDER
978 979
    case ClampToBorder:
      return GL_CLAMP_TO_BORDER;
Ken Martin's avatar
Ken Martin committed
980
#endif
981 982 983 984 985 986 987 988 989 990 991 992 993
    case MirroredRepeat:
      return GL_MIRRORED_REPEAT;
    default:
      return GL_CLAMP_TO_EDGE;
    }
}

//----------------------------------------------------------------------------
unsigned int vtkTextureObject::GetWrapTMode(int vtktype)
{
  return this->GetWrapSMode(vtktype);
}

994 995 996 997 998
//----------------------------------------------------------------------------
unsigned int vtkTextureObject::GetWrapRMode(int vtktype)
{
  return this->GetWrapSMode(vtktype);
}
999

Ken Martin's avatar
Ken Martin committed
1000 1001
// 1D  textures are not supported in ES 2.0 or 3.0
#if GL_ES_VERSION_2_0 != 1
1002

1003 1004 1005 1006 1007 1008
//----------------------------------------------------------------------------
bool vtkTextureObject::Create1D(int numComps,
                                vtkPixelBufferObject* pbo,
                                bool shaderSupportsTextureInt)
{
  assert(this->Context);
1009
  assert(pbo->GetContext() == this->Context.GetPointer());
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023

  GLenum target = GL_TEXTURE_1D;

  // Now, detemine texture parameters using the information from the pbo.

  // * internalFormat depends on number of components and the data type.
  GLenum internalFormat = this->GetInternalFormat(pbo->GetType(), numComps,
                                                 shaderSupportsTextureInt);

  // * format depends on the number of components.
  GLenum format = this->GetFormat(pbo->GetType(), numComps,
                                 shaderSupportsTextureInt);

  // * type if the data type in the pbo
1024
  GLenum type = this->GetDefaultDataType(pbo->GetType());
1025 1026 1027 1028 1029 1030 1031 1032

  if (!internalFormat || !format || !type)
    {
    vtkErrorMacro("Failed to detemine texture parameters.");
    return false;
    }

  this->Target = target;
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1033
  this->Context->ActivateTexture(this);
1034
  this->CreateTexture();
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1035
  this->Bind();
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047

  pbo->Bind(vtkPixelBufferObject::UNPACKED_BUFFER);

  // Source texture data from the PBO.
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glTexImage1D(target, 0, static_cast<GLint>(internalFormat),
               static_cast<GLsizei>(pbo->GetSize()/
                                    static_cast<unsigned int>(numComps)),
               0, format,
               type, BUFFER_OFFSET(0));
  vtkOpenGLCheckErrorMacro("failed at glTexImage1D");
  pbo->UnBind();
1048
  this->Deactivate();
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

  this->Target = target;
  this->Format = format;
  this->Type = type;
  this->Components = numComps;
  this->Width = pbo->GetSize();
  this->Height = 1;
  this->Depth =1;
  this->NumberOfDimensions=1;
  return true;
}

Ken Martin's avatar
Ken Martin committed
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
//----------------------------------------------------------------------------
bool vtkTextureObject::Create1DFromRaw(unsigned int width, int numComps,
                                       int dataType, void *data)
{
  assert(this->Context);

  // Now determine the texture parameters using the arguments.
  this->GetDataType(dataType);
  this->GetInternalFormat(dataType, numComps, false);
  this->GetFormat(dataType, numComps, false);

  if (!this->InternalFormat || !this->Format || !this->Type)
    {
    vtkErrorMacro("Failed to determine texture parameters.");
    return false;
    }

  GLenum target = GL_TEXTURE_1D;
  this->Target = target;
  this->Components = numComps;
  this->Width = width;
  this->Height = 1;
  this->Depth = 1;
  this->NumberOfDimensions = 1;
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1085
  this->Context->ActivateTexture(this);
Ken Martin's avatar
Ken Martin committed
1086
  this->CreateTexture();
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1087
  this->Bind();
Ken Martin's avatar
Ken Martin committed
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099

  glTexImage1D(this->Target,
               0,
               this->InternalFormat,
               static_cast<GLsizei> (this->Width),
               0,
               this->Format,
               this->Type,
               static_cast<const GLvoid *> (data));

  vtkOpenGLCheckErrorMacro("failed at glTexImage1D");

1100
  this->Deactivate();
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
  return true;
}

// ----------------------------------------------------------------------------
// Description:
// Create a 1D alpha texture using a raw pointer.
// This is a blocking call. If you can, use PBO instead.
bool vtkTextureObject::CreateAlphaFromRaw(unsigned int width,
                                          int internalFormat,
                                          int rawType,
                                          void* raw)
{
  assert("pre: context_exists" && this->GetContext()!=0);
  assert("pre: raw_exists" && raw!=0);

  assert("pre: valid_internalFormat" && internalFormat>=0
         && internalFormat<NumberOfAlphaFormats);

  // Now, detemine texture parameters using the arguments.
  this->GetDataType(rawType);

  if (!this->InternalFormat)
    {
    this->InternalFormat
      = OpenGLAlphaInternalFormat[internalFormat];
    }

  if (!this->InternalFormat || !this->Type)
    {
    vtkErrorMacro("Failed to detemine texture parameters.");
    return false;
    }

  this->Target = GL_TEXTURE_1D;
1135
  this->Format = GL_RED;
Ken Martin's avatar
Ken Martin committed
1136 1137 1138 1139 1140 1141
  this->Width = width;
  this->Height = 1;
  this->Depth = 1;
  this->NumberOfDimensions = 1;
  this->Components = 1;

Marcin Adamczyk's avatar
Marcin Adamczyk committed
1142
  this->Context->ActivateTexture(this);
Ken Martin's avatar
Ken Martin committed
1143
  this->CreateTexture();
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1144
  this->Bind();
Ken Martin's avatar
Ken Martin committed
1145 1146 1147 1148 1149 1150

  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glTexImage1D(this->Target, 0, static_cast<GLint>(this->InternalFormat),
               static_cast<GLsizei>(this->Width), 0,
               this->Format, this->Type, raw);
  vtkOpenGLCheckErrorMacro("failed at glTexImage1D");
1151
  this->Deactivate();
Ken Martin's avatar
Ken Martin committed
1152 1153 1154
  return true;
}

Ken Martin's avatar
Ken Martin committed
1155 1156 1157 1158
// Description:
// Create a texture buffer basically a 1D texture that can be
// very large for passing data into the fragment shader
bool vtkTextureObject::CreateTextureBuffer(unsigned int numValues, int numComps,
Ken Martin's avatar
Ken Martin committed
1159
                         int dataType, vtkOpenGLBufferObject *bo)
Ken Martin's avatar
Ken Martin committed
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
{
  assert(this->Context);

  // Now, detemine texture parameters using the arguments.
  this->GetDataType(dataType);
  this->GetInternalFormat(dataType, numComps, false);
  this->GetFormat(dataType, numComps, false);

  if (!this->InternalFormat || !this->Format || !this->Type)
    {
    vtkErrorMacro("Failed to detemine texture parameters.");
    return false;
    }

  this->Target = GL_TEXTURE_BUFFER;
  this->Components = numComps;
  this->Width = numValues;
  this->Height = 1;
  this->Depth = 1;
  this->NumberOfDimensions = 1;
  this->BufferObject = bo;

Marcin Adamczyk's avatar
Marcin Adamczyk committed
1182
  this->Context->ActivateTexture(this);
Ken Martin's avatar
Ken Martin committed
1183
  this->CreateTexture();
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1184
  this->Bind();
Ken Martin's avatar
Ken Martin committed
1185 1186 1187 1188 1189 1190 1191 1192 1193

  // Source texture data from the PBO.
  glTexBuffer(
      this->Target,
      this->InternalFormat,
      this->BufferObject->GetHandle());

  vtkOpenGLCheckErrorMacro("failed at glTexBuffer");

1194
  this->Deactivate();
Ken Martin's avatar
Ken Martin committed
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

  return true;
}

#else

// Description:
// Create a texture buffer basically a 1D texture that can be
// very large for passing data into the fragment shader
bool vtkTextureObject::CreateTextureBuffer(unsigned int numValues, int numComps,
Ken Martin's avatar
Ken Martin committed
1205
                         int dataType, vtkOpenGLBufferObject *bo)
Ken Martin's avatar
Ken Martin committed
1206 1207 1208 1209 1210 1211 1212
{
  assert(this->Context);
  vtkErrorMacro("TextureBuffers not supported in OPenGL ES");
  // TODO: implement 1D and Texture buffers using 2D textures
  return false;
}

Ken Martin's avatar
Ken Martin committed
1213 1214 1215 1216
#endif // not ES 2.0 or 3.0

#if GL_ES_VERSION_2_0 != 1 || GL_ES_VERSION_3_0 == 1

1217 1218 1219 1220 1221 1222
//----------------------------------------------------------------------------
bool vtkTextureObject::Create2D(unsigned int width, unsigned int height,
                                int numComps, vtkPixelBufferObject* pbo,
                                bool shaderSupportsTextureInt)
{
  assert(this->Context);
1223
  assert(pbo->GetContext() == this->Context.GetPointer());
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236

  if (pbo->GetSize() < width*height*static_cast<unsigned int>(numComps))
    {
    vtkErrorMacro("PBO size must match texture size.");
    return false;
    }

  // Now, detemine texture parameters using the information from the pbo.
  // * internalFormat depends on number of components and the data type.
  // * format depends on the number of components.
  // * type if the data type in the pbo

  int vtktype = pbo->GetType();
1237
  GLenum type = this->GetDefaultDataType(vtktype);
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252

  GLenum internalFormat
    = this->GetInternalFormat(vtktype, numComps, shaderSupportsTextureInt);

  GLenum format
    = this->GetFormat(vtktype, numComps, shaderSupportsTextureInt);

  if (!internalFormat || !format || !type)
    {
    vtkErrorMacro("Failed to detemine texture parameters.");
    return false;
    }

  GLenum target = GL_TEXTURE_2D;
  this->Target = target;
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1253
  this->Context->ActivateTexture(this);
1254
  this->CreateTexture();
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1255
  this->Bind();
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

  // Source texture data from the PBO.
  pbo->Bind(vtkPixelBufferObject::UNPACKED_BUFFER);
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

  glTexImage2D(
        target,
        0,
        internalFormat,
        static_cast<GLsizei>(width),
        static_cast<GLsizei>(height),
        0,
        format,
        type,
        BUFFER_OFFSET(0));

  vtkOpenGLCheckErrorMacro("failed at glTexImage2D");

  pbo->UnBind();
1275
  this->Deactivate();
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304

  this->Target = target;
  this->Format = format;
  this->Type = type;
  this->Components = numComps;
  this->Width = width;
  this->Height = height;
  this->Depth = 1;
  this->NumberOfDimensions = 2;

  return true;
}

// ----------------------------------------------------------------------------
// Description:
// Create a 2D depth texture using a PBO.
bool vtkTextureObject::CreateDepth(unsigned int width,
                                   unsigned int height,
                                   int internalFormat,
                                   vtkPixelBufferObject *pbo)
{
  assert("pre: context_exists" && this->GetContext()!=0);
  assert("pre: pbo_context_exists" && pbo->GetContext()!=0);
  assert("pre: context_match" && this->GetContext()==pbo->GetContext());
  assert("pre: sizes_match" && pbo->GetSize()==width*height);
  assert("pre: valid_internalFormat" && internalFormat>=0
         && internalFormat<NumberOfDepthFormats);

  GLenum inFormat=OpenGLDepthInternalFormat[internalFormat];
1305
  GLenum type = this->GetDefaultDataType(pbo->GetType());
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315

  this->Target=GL_TEXTURE_2D;
  this->Format=GL_DEPTH_COMPONENT;
  this->Type=type;
  this->Width=width;
  this->Height=height;
  this->Depth=1;
  this->NumberOfDimensions=2;
  this->Components=1;

Marcin Adamczyk's avatar
Marcin Adamczyk committed
1316
  this->Context->ActivateTexture(this);
1317
  this->CreateTexture();
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1318
  this->Bind();
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328

  pbo->Bind(vtkPixelBufferObject::UNPACKED_BUFFER);

  // Source texture data from the PBO.
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  glTexImage2D(this->Target, 0, static_cast<GLint>(inFormat),
               static_cast<GLsizei>(width), static_cast<GLsizei>(height), 0,
               this->Format, this->Type, BUFFER_OFFSET(0));
  vtkOpenGLCheckErrorMacro("failed at glTexImage2D");
  pbo->UnBind();
1329
  this->Deactivate();
1330 1331 1332
  return true;
}

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
//----------------------------------------------------------------------------
bool vtkTextureObject::Create3D(unsigned int width, unsigned int height,
                                unsigned int depth, int numComps,
                                vtkPixelBufferObject* pbo,
                                bool shaderSupportsTextureInt)
{
#ifdef GL_TEXTURE_3D
  assert(this->Context);
  assert(this->Context.GetPointer() == pbo->GetContext());

  if (pbo->GetSize() != width*height*depth*static_cast<unsigned int>(numComps))
    {
    vtkErrorMacro("PBO size must match texture size.");
    return false;
    }

  GLenum target = GL_TEXTURE_3D;

  // Now, detemine texture parameters using the information from the pbo.

  // * internalFormat depends on number of components and the data type.
  GLenum internalFormat = this->GetInternalFormat(pbo->GetType(), numComps,
                                                 shaderSupportsTextureInt);

  // * format depends on the number of components.
  GLenum format = this->GetFormat(pbo->GetType(), numComps,
                                  shaderSupportsTextureInt);

  // * type if the data type in the pbo
1362
  GLenum type = this->GetDefaultDataType(pbo->GetType());
1363 1364 1365 1366 1367 1368 1369 1370

  if (!internalFormat || !format || !type)
    {
    vtkErrorMacro("Failed to detemine texture parameters.");
    return false;
    }

  this->Target = target;
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1371
  this->Context->ActivateTexture(this);
1372
  this->CreateTexture();
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1373
  this->Bind();
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385

  pbo->Bind(vtkPixelBufferObject::UNPACKED_BUFFER);

  // Source texture data from the PBO.
  glTexImage3D(target, 0, static_cast<GLint>(internalFormat),
                    static_cast<GLsizei>(width), static_cast<GLsizei>(height),
                    static_cast<GLsizei>(depth), 0, format, type,
                    BUFFER_OFFSET(0));

  vtkOpenGLCheckErrorMacro("failed at glTexImage3D");

  pbo->UnBind();
1386
  this->Deactivate();
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440

  this->Target = target;
  this->Format = format;
  this->Type = type;
  this->Components = numComps;
  this->Width = width;
  this->Height = height;
  this->Depth = depth;
  this->NumberOfDimensions = 3;
  return true;

  #else
    return false;
  #endif
}

//----------------------------------------------------------------------------
vtkPixelBufferObject* vtkTextureObject::Download()
{
  assert(this->Context);
  assert(this->Handle);

  vtkPixelBufferObject* pbo = vtkPixelBufferObject::New();
  pbo->SetContext(this->Context);

  int vtktype = ::vtkGetVTKType(this->Type);
  if (vtktype == 0)
    {
    vtkErrorMacro("Failed to determine type.");
    return 0;
    }

  unsigned int size = this->Width* this->Height* this->Depth;

  // doesn't matter which Upload*D method we use since we are not really
  // uploading any data, simply allocating GPU space.
  if (!pbo->Upload1D(vtktype, NULL, size, this->Components, 0))
    {
    vtkErrorMacro("Could not allocate memory for PBO.");
    pbo->Delete();
    return 0;
    }

  pbo->Bind(vtkPixelBufferObject::PACKED_BUFFER);
  this->Bind();

#if GL_ES_VERSION_2_0 != 1
  glGetTexImage(this->Target, 0, this->Format, this->Type, BUFFER_OFFSET(0));
#else
  // you can do something with glReadPixels and binding a texture as a FBO
  // I believe for ES 2.0
#endif

  vtkOpenGLCheckErrorMacro("failed at glGetTexImage");
1441
  this->Deactivate();
1442 1443 1444 1445 1446 1447 1448
  pbo->UnBind();

  pbo->SetComponents(this->Components);

  return pbo;
}

1449 1450 1451
//----------------------------------------------------------------------------
bool vtkTextureObject::Create3DFromRaw(unsigned int width, unsigned int height,
                                       unsigned int depth, int numComps,
1452
                                       int dataType, void *data)
1453 1454 1455 1456
{
  assert(this->Context);

  // Now, detemine texture parameters using the arguments.
1457 1458 1459
  this->GetDataType(dataType);
  this->GetInternalFormat(dataType, numComps, false);
  this->GetFormat(dataType, numComps, false);
1460 1461

  if (!this->InternalFormat || !this->Format || !this->Type)
1462 1463 1464 1465 1466 1467
    {
    vtkErrorMacro("Failed to detemine texture parameters.");
    return false;
    }

  this->Target = GL_TEXTURE_3D;
1468 1469 1470
  this->Components = numComps;
  this->Width = width;
  this->Height = height;
1471 1472 1473
  this->Depth = depth;
  this->NumberOfDimensions = 3;

Marcin Adamczyk's avatar
Marcin Adamczyk committed
1474
  this->Context->ActivateTexture(this);
1475
  this->CreateTexture();
Marcin Adamczyk's avatar
Marcin Adamczyk committed
1476
  this->Bind();
1477 1478 1479 1480 1481 1482 1483

  // Source texture data from the PBO.
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

  glTexImage3D(
        this->Target,
        0,
1484
        this->InternalFormat,
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
        static_cast<GLsizei>(this->Width),
        static_cast<GLsizei>(this->Height),
        static_cast<GLsizei>(this->Depth),
        0,
        this->Format,
        this->Type,
        static_cast<const GLvoid *>(data));

  vtkOpenGLCheckErrorMacro("failed at glTexImage2D");

1495
  this->