vtkTextureObject.h 18.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkTextureObject.h

  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.

=========================================================================*/
// .NAME vtkTextureObject - abstracts an OpenGL texture object.
// .SECTION Description
// vtkTextureObject represents an OpenGL texture object. It provides API to
// create textures using data already loaded into pixel buffer objects. It can
// also be used to create textures without uploading any data.
#ifndef __vtkTextureObject_h
#define __vtkTextureObject_h

#include "vtkRenderingOpenGL2Module.h" // For export macro
#include "vtkObject.h"
#include "vtkWeakPointer.h" // for render context

Ken Martin's avatar
Ken Martin committed
27
class vtkWindow;
28
class vtkShaderProgram;
29
class vtkOpenGLRenderWindow;
30
31
32
33
34
namespace vtkgl
{
class VertexArrayObject;
class CellBO;
}
35
36

#if GL_ES_VERSION_2_0 != 1
37
class vtkPixelBufferObject;
38
#endif
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

class VTKRENDERINGOPENGL2_EXPORT vtkTextureObject : public vtkObject
{
public:

  // DepthTextureCompareFunction values.
  enum
  {
    Lequal=0, // r=R<=Dt ? 1.0 : 0.0
    Gequal, // r=R>=Dt ? 1.0 : 0.0
    Less, // r=R<D_t ? 1.0 : 0.0
    Greater, // r=R>Dt ? 1.0 : 0.0
    Equal, // r=R==Dt ? 1.0 : 0.0
    NotEqual, // r=R!=Dt ? 1.0 : 0.0
    AlwaysTrue, //  r=1.0 // WARNING "Always" is macro defined in X11/X.h...
    Never, // r=0.0
    NumberOfDepthTextureCompareFunctions
  };

58
59
60
61
62
63
64
65
66
67
68
69
// ClampToBorder is not supported in ES 2.0
// Wrap values.
#if GL_ES_VERSION_2_0 != 1
  enum
  {
    ClampToEdge=0,
    Repeat,
    MirroredRepeat,
    ClampToBorder,
    NumberOfWrapModes
  };
#else
70
71
  enum
  {
72
    ClampToEdge=0,
73
74
75
76
    Repeat,
    MirroredRepeat,
    NumberOfWrapModes
  };
77
#endif
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

  // MinificationFilter values.
  enum
  {
    Nearest=0,
    Linear,
    NearestMipmapNearest,
    NearestMipmapLinear,
    LinearMipmapNearest,
    LinearMipmapLinear,
    NumberOfMinificationModes
  };

  // Internal depth format
  enum
  {
    Native=0, // will try to match with the depth buffer format.
    Fixed16,
    Fixed24,
    Fixed32,
    Float32,
    NumberOfDepthFormats
  };

  static vtkTextureObject* New();
  vtkTypeMacro(vtkTextureObject, vtkObject);
  void PrintSelf(ostream& os, vtkIndent indent);

  // Description:
  // Get/Set the context. This does not increase the reference count of the
  // context to avoid reference loops.
  // SetContext() may raise an error is the OpenGL context does not support the
  // required OpenGL extensions.
111
112
  void SetContext(vtkOpenGLRenderWindow*);
  vtkOpenGLRenderWindow* GetContext();
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

  // Description:
  // Get the texture dimensions.
  // These are the properties of the OpenGL texture this instance represents.
  vtkGetMacro(Width, unsigned int);
  vtkGetMacro(Height, unsigned int);
  vtkGetMacro(Depth, unsigned int);
  vtkGetMacro(Components, int);
  unsigned int GetTuples()
  { return this->Width*this->Height*this->Depth; }

  vtkGetMacro(NumberOfDimensions, int);

  // Description:
  // Returns OpenGL texture target to which the texture is/can be bound.
  vtkGetMacro(Target, unsigned int);

  // Description:
  // Returns the OpenGL handle.
  vtkGetMacro(Handle, unsigned int);

  // Description:
135
136
137
138
139
  // Return the texture unit used for this texture
  int GetTextureUnit();

  // Description:
  // Bind UnBind The texture must have been created using Create().
140
141
142
143
144
145
  // A side affect is that tex paramteres are sent.
  // RenderWindow must be set before calling this.
  void Bind();
  void UnBind();

  // Description:
146
147
  // Activate and Bind the texture
  void Activate();
148
149
150

  // Description:
  // Deactivate and UnBind the texture
151
  void Deactivate();
152

Ken Martin's avatar
Ken Martin committed
153
154
155
  // Description:
  // Deactivate and UnBind the texture
  void ReleaseGraphicsResources(vtkWindow *win);
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

  // Description:
  // Tells if the texture object is bound to the active texture image unit.
  // (a texture object can be bound to multiple texture image unit).
  bool IsBound();

  // Description:
  // Send all the texture object parameters to the hardware if not done yet.
  // Parameters are automatically sent as a side affect of Bind. Disable
  // this by setting AutoParameters 0.
  // \pre is_bound: IsBound()
  void SendParameters();
  vtkSetMacro(AutoParameters, int);
  vtkGetMacro(AutoParameters, int);

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  // Description:
  // Create a 2D texture from client memory
  // numComps must be in [1-4].
  bool Create2DFromRaw(unsigned int width, unsigned int height,
                       int numComps,  int dataType, void *data);

  // Description:
  // Create a 2D depth texture using a raw pointer.
  // This is a blocking call. If you can, use PBO instead.
  bool CreateDepthFromRaw(unsigned int width, unsigned int height,
                          int internalFormat, int rawType,
                          void *raw);

// PBO's are not supported in ES 2.0
#if GL_ES_VERSION_2_0 != 1

187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
  // Description:
  // Create a 1D texture using the PBO.
  // Eventually we may start supporting creating a texture from subset of data
  // in the PBO, but for simplicity we'll begin with entire PBO data.
  // numComps must be in [1-4].
  // shaderSupportsTextureInt is true if the shader has an alternate
  // implementation supporting sampler with integer values.
  // Even if the card supports texture int, it does not mean that
  // the implementor of the shader made a version that supports texture int.
  bool Create1D(int numComps,
                vtkPixelBufferObject *pbo,
                bool shaderSupportsTextureInt);

  // Description:
  // Create a 2D texture using the PBO.
  // Eventually we may start supporting creating a texture from subset of data
  // in the PBO, but for simplicity we'll begin with entire PBO data.
  // numComps must be in [1-4].
  bool Create2D(unsigned int width, unsigned int height, int numComps,
                vtkPixelBufferObject *pbo,
                bool shaderSupportsTextureInt);

209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  // Description:
  // Create a 3D texture using the PBO.
  // Eventually we may start supporting creating a texture from subset of data
  // in the PBO, but for simplicity we'll begin with entire PBO data.
  // numComps must be in [1-4].
  bool Create3D(unsigned int width, unsigned int height, unsigned int depth,
                int numComps, vtkPixelBufferObject *pbo,
                bool shaderSupportsTextureInt);
  // Description:
  // This is used to download raw data from the texture into a pixel bufer. The
  // pixel buffer API can then be used to download the pixel buffer data to CPU
  // arrays. The caller takes on the responsibility of deleting the returns
  // vtkPixelBufferObject once it done with it.
  vtkPixelBufferObject* Download();

224
225
226
227
228
229
230
231
  // Description:
  // Create a 2D depth texture using a PBO.
  // \pre: valid_internalFormat: internalFormat>=0 && internalFormat<NumberOfDepthFormats
  bool CreateDepth(unsigned int width,
                   unsigned int height,
                   int internalFormat,
                   vtkPixelBufferObject *pbo);

232
233
#endif

234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  // Description:
  // Create a 2D depth texture but does not initialize its values.
  bool AllocateDepth(unsigned int width,unsigned int height,
                     int internalFormat);

  // Description:
  // Create a 1D color texture but does not initialize its values.
  // Internal format is deduced from numComps and vtkType.
  bool Allocate1D(unsigned int width, int numComps,int vtkType);

  // Description:
  // Create a 2D color texture but does not initialize its values.
  // Internal format is deduced from numComps and vtkType.
  bool Allocate2D(unsigned int width,unsigned int height, int numComps,
                  int vtkType);

  // Description:
  // Create a 3D color texture but does not initialize its values.
  // Internal format is deduced from numComps and vtkType.
  bool Allocate3D(unsigned int width,unsigned int height,
                  unsigned int depth, int numComps,
                  int vtkType);


  // Description:
  // Create texture without uploading any data.
  bool Create2D(unsigned int width, unsigned int height, int numComps,
                int vtktype,
                bool shaderSupportsTextureInt);
  bool Create3D(unsigned int width, unsigned int height, unsigned int depth,
                int numComps, int vtktype,
                bool shaderSupportsTextureInt);

  // Description:
  // Get the data type for the texture as a vtk type int i.e. VTK_INT etc.
  int GetDataType();

  unsigned int GetInternalFormat(int vtktype, int numComps,
                                 bool shaderSupportsTextureInt);
  unsigned int GetFormat(int vtktype, int numComps,
                         bool shaderSupportsTextureInt);

  // Description:
  // Optional, require support for floating point depth buffer
  // formats. If supported extensions will be loaded, however
  // loading will fail if the extension is required but not
  // available.
  vtkSetMacro(RequireDepthBufferFloat, bool);
  vtkGetMacro(RequireDepthBufferFloat, bool);
  vtkGetMacro(SupportsDepthBufferFloat, bool);

  // Description:
  // Optional, require support for floating point texture
  // formats. If supported extensions will be loaded, however
  // loading will fail if the extension is required but not
  // available.
  vtkSetMacro(RequireTextureFloat,bool);
  vtkGetMacro(RequireTextureFloat,bool);
  vtkGetMacro(SupportsTextureFloat,bool);

  // Description:
  // Optional, require support for integer texture
  // formats. If supported extensions will be loaded, however
  // loading will fail if the extension is required but not
  // available.
  vtkSetMacro(RequireTextureInteger,bool);
  vtkGetMacro(RequireTextureInteger,bool);
  vtkGetMacro(SupportsTextureInteger,bool);

  // Description:
  // Wrap mode for the first texture coordinate "s"
  // Valid values are:
  // - Clamp
  // - ClampToEdge
  // - Repeat
  // - ClampToBorder
  // - MirroredRepeat
  // Initial value is Repeat (as in OpenGL spec)
  vtkGetMacro(WrapS,int);
  vtkSetMacro(WrapS,int);

  // Description:
  // Wrap mode for the first texture coordinate "t"
  // Valid values are:
  // - Clamp
  // - ClampToEdge
  // - Repeat
  // - ClampToBorder
  // - MirroredRepeat
  // Initial value is Repeat (as in OpenGL spec)
  vtkGetMacro(WrapT,int);
  vtkSetMacro(WrapT,int);

  // Description:
  // Wrap mode for the first texture coordinate "r"
  // Valid values are:
  // - Clamp
  // - ClampToEdge
  // - Repeat
  // - ClampToBorder
  // - MirroredRepeat
  // Initial value is Repeat (as in OpenGL spec)
  vtkGetMacro(WrapR,int);
  vtkSetMacro(WrapR,int);

  // Description:
  // Minification filter mode.
  // Valid values are:
  // - Nearest
  // - Linear
  // - NearestMipmapNearest
  // - NearestMipmapLinear
  // - LinearMipmapNearest
  // - LinearMipmapLinear
  // Initial value is Nearest (note initial value in OpenGL spec
  // is NearestMipMapLinear but this is error-prone because it makes the
  // texture object incomplete. ).
  vtkGetMacro(MinificationFilter,int);
  vtkSetMacro(MinificationFilter,int);

  // Description:
  // Magnification filter mode.
  // Valid values are:
  // - Nearest
  // - Linear
  // Initial value is Nearest
  vtkGetMacro(MagnificationFilter,int);
  vtkSetMacro(MagnificationFilter,int);

  // Description:
  // Tells if the magnification mode is linear (true) or nearest (false).
  // Initial value is false (initial value in OpenGL spec is true).
  void SetLinearMagnification(bool val)
  { this->SetMagnificationFilter(val?Linear:Nearest); }

  bool GetLinearMagnification()
  { return this->MagnificationFilter==Linear; }

372
373
374
375
376
377
378
  // Description:
  // Border Color (RGBA). The values can be any valid float value,
  // if the gpu supports it. Initial value is (0.0f,0.0f,0.0f,0.0f)
  // , as in OpenGL spec.
  vtkSetVector4Macro(BorderColor,float);
  vtkGetVector4Macro(BorderColor,float);

379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
  // Description:
  // Lower-clamp the computed LOD against this value. Any float value is valid.
  // Initial value is -1000.0f, as in OpenGL spec.
  vtkSetMacro(MinLOD,float);
  vtkGetMacro(MinLOD,float);

  // Description:
  // Upper-clamp the computed LOD against this value. Any float value is valid.
  // Initial value is 1000.0f, as in OpenGL spec.
  vtkSetMacro(MaxLOD,float);
  vtkGetMacro(MaxLOD,float);

  // Description:
  // Level of detail of the first texture image. A texture object is a list of
  // texture images. It is a non-negative integer value.
  // Initial value is 0, as in OpenGL spec.
  vtkSetMacro(BaseLevel,int);
  vtkGetMacro(BaseLevel,int);

  // Description:
  // Level of detail of the first texture image. A texture object is a list of
  // texture images. It is a non-negative integer value.
  // Initial value is 1000, as in OpenGL spec.
  vtkSetMacro(MaxLevel,int);
  vtkGetMacro(MaxLevel,int);

  // Description:
  // Tells if the output of a texture unit with a depth texture uses
  // comparison or not.
  // Comparison happens between D_t the depth texture value in the range [0,1]
  // and with R the interpolated third texture coordinate clamped to range
  // [0,1]. The result of the comparison is noted `r'. If this flag is false,
  // r=D_t.
  // Initial value is false, as in OpenGL spec.
  // Ignored if the texture object is not a depth texture.
  vtkGetMacro(DepthTextureCompare,bool);
  vtkSetMacro(DepthTextureCompare,bool);

  // Description:
  // In case DepthTextureCompare is true, specify the comparison function in
  // use. The result of the comparison is noted `r'.
  // Valid values are:
  // - Value
  // - Lequal: r=R<=Dt ? 1.0 : 0.0
  // - Gequal: r=R>=Dt ? 1.0 : 0.0
  // - Less: r=R<D_t ? 1.0 : 0.0
  // - Greater: r=R>Dt ? 1.0 : 0.0
  // - Equal: r=R==Dt ? 1.0 : 0.0
  // - NotEqual: r=R!=Dt ? 1.0 : 0.0
  // - AlwaysTrue: r=1.0
  // - Never: r=0.0
  // If the magnification of minification factor are not nearest, percentage
  // closer filtering (PCF) is used: R is compared to several D_t and r is
  // the average of the comparisons (it is NOT the average of D_t compared
  // once to R).
  // Initial value is Lequal, as in OpenGL spec.
  // Ignored if the texture object is not a depth texture.
  vtkGetMacro(DepthTextureCompareFunction,int);
  vtkSetMacro(DepthTextureCompareFunction,int);

  // Description:
  // Tells the hardware to generate mipmap textures from the first texture
  // image at BaseLevel.
  // Initial value is false, as in OpenGL spec.
  vtkGetMacro(GenerateMipmap,bool);
  vtkSetMacro(GenerateMipmap,bool);

  // Description:
  // Returns if the context supports the required extensions. If flags
  // for optional extenisons are set then the test fails when support
  // for them is not found.
  static bool IsSupported(
451
        vtkOpenGLRenderWindow* renWin,
452
453
454
455
456
457
        bool requireTexFloat,
        bool requireDepthFloat,
        bool requireTexInt);

  // Description:
  // Check for feature support, without any optional features.
458
  static bool IsSupported(vtkOpenGLRenderWindow* renWin)
459
460
461
    { return vtkTextureObject::IsSupported(renWin, false, false, false); }

  // Description:
462
463
464
465
466
467
  // Copy the texture (src) in the current framebuffer.  A variety of
  // signatures based on what you want to do
  // Copy the entire texture to the entire current viewport
  void CopyToFrameBuffer(vtkShaderProgram *program,
                         vtkgl::VertexArrayObject *vao);
  // part of a texture to part of a viewport, scaling as needed
468
469
470
  void CopyToFrameBuffer(int srcXmin, int srcYmin,
                         int srcXmax, int srcYmax,
                         int dstXmin, int dstYmin,
471
                         int dstXmax, int dstYmax,
472
                         int dstSizeX, int dstSizeY,
473
474
475
                         vtkShaderProgram *program,
                         vtkgl::VertexArrayObject *vao
                         );
476
477
478
479
480
481
482
483
484
485
486
487
488
  // copy part of a texure to part of a viewport, no scalaing
  void CopyToFrameBuffer(int srcXmin, int srcYmin,
                         int srcXmax, int srcYmax,
                         int dstXmin, int dstYmin,
                         int dstSizeX, int dstSizeY,
                         vtkShaderProgram *program,
                         vtkgl::VertexArrayObject *vao
                         );
  // copy a texture to a quad using the provided tcoords and verts
  void CopyToFrameBuffer(float *tcoords, float *verts,
                         vtkShaderProgram *program,
                         vtkgl::VertexArrayObject *vao
                         );
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515


  // Description:
  // Copy a sub-part of a logical buffer of the framebuffer (color or depth)
  // to the texture object. src is the framebuffer, dst is the texture.
  // (srcXmin,srcYmin) is the location of the lower left corner of the
  // rectangle in the framebuffer. (dstXmin,dstYmin) is the location of the
  // lower left corner of the rectangle in the texture. width and height
  // specifies the size of the rectangle in pixels.
  // If the logical buffer is a color buffer, it has to be selected first with
  // glReadBuffer().
  // \pre is2D: GetNumberOfDimensions()==2
  void CopyFromFrameBuffer(int srcXmin,
                           int srcYmin,
                           int dstXmin,
                           int dstYmin,
                           int width,
                           int height);



protected:
  vtkTextureObject();
  ~vtkTextureObject();

  // Description:
  // Load all necessary extensions.
516
  bool LoadRequiredExtensions(vtkOpenGLRenderWindow *renWin);
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535

  // Description:
  // Creates a texture handle if not already created.
  void CreateTexture();

  // Description:
  // Destroy the texture.
  void DestroyTexture();

  int NumberOfDimensions;
  unsigned int Width;
  unsigned int Height;
  unsigned int Depth;

  unsigned int Target; // GLenum
  unsigned int Format; // GLenum
  unsigned int Type; // GLenum
  int Components;

536
  vtkWeakPointer<vtkOpenGLRenderWindow> Context;
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
  unsigned int Handle;
  bool RequireTextureInteger;
  bool SupportsTextureInteger;
  bool RequireTextureFloat;
  bool SupportsTextureFloat;
  bool RequireDepthBufferFloat;
  bool SupportsDepthBufferFloat;

  int WrapS;
  int WrapT;
  int WrapR;
  int MinificationFilter;
  int MagnificationFilter;
  bool LinearMagnification;

  float MinLOD;
  float MaxLOD;
  int BaseLevel;
  int MaxLevel;
556
  float BorderColor[4];
557
558
559
560
561
562
563
564
565

  bool DepthTextureCompare;
  int DepthTextureCompareFunction;

  bool GenerateMipmap;

  int AutoParameters;
  vtkTimeStamp SendParametersTime;

566
567
  // used for copying to framebuffer
  vtkgl::CellBO *ShaderProgram;
568

569
570
571
572
573
574
private:
  vtkTextureObject(const vtkTextureObject&); // Not implemented.
  void operator=(const vtkTextureObject&); // Not implemented.
};

#endif