File: vtkOpenGLVertexArrayObject.cxx

package info (click to toggle)
vtk7 7.1.1%2Bdfsg1-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 125,776 kB
  • sloc: cpp: 1,539,582; ansic: 106,521; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 122; objc: 83
file content (466 lines) | stat: -rw-r--r-- 12,598 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*=========================================================================

  Program:   Visualization Toolkit

  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 "vtkOpenGLVertexArrayObject.h"
#include "vtkObjectFactory.h"

#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLBufferObject.h"
#include "vtkShaderProgram.h"

#include <map>
#include <vector>

#include "vtk_glew.h"

vtkStandardNewMacro(vtkOpenGLVertexArrayObject)

namespace
{
// Copied from vtkglShaderProgram, time to move into a common header?
inline GLenum convertTypeToGL(int type)
{
  switch (type)
  {
    case VTK_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:
      return GL_FLOAT;
    case VTK_DOUBLE:
#ifdef GL_DOUBLE
      return GL_DOUBLE;
#else
      vtkGenericWarningMacro(<< "Attempt to use GL_DOUBLE when not supported");
      return 0;
#endif
    default:
      return 0;
  }
}

struct VertexAttributes
{
  GLint Index;
  GLint  Size;
  GLenum Type;
  GLboolean Normalize;
  GLsizei Stride;
  GLuint Offset;
  int Divisor;
  bool IsMatrix;
};

} // end anonymous


class vtkOpenGLVertexArrayObject::Private
{
public:
  Private()
  {
    this->HandleVAO = 0;
    this->HandleProgram = 0;
    this->Supported = true;
    this->ForceEmulation = false;
  }
  ~Private()
  {
    if (this->HandleVAO)
    {
      glDeleteVertexArrays(1, &this->HandleVAO);
    }
  }

  void Initialize()
  {
    if (!this->ForceEmulation &&
        (GLEW_ARB_vertex_array_object ||
            vtkOpenGLRenderWindow::GetContextSupportsOpenGL32()))
    {
      this->Supported = true;
      glGenVertexArrays(1, &this->HandleVAO);
    }
    else
    {
      this->Supported = false;
    }
  }

  bool IsReady() const
  {
    // We either probed and allocated a VAO, or are falling back as the current
    // hardware does not support VAOs.
    return (this->HandleVAO != 0 || this->Supported == false);
  }

  void ReleaseGraphicsResources()
  {
    if (this->HandleVAO)
    {
      glDeleteVertexArrays(1, &this->HandleVAO);
    }
    this->HandleVAO = 0;
    this->Supported = true;
    this->HandleProgram = 0;
  }

  GLuint HandleVAO;
  GLuint HandleProgram;
  bool Supported;
  bool ForceEmulation;

  typedef std::map< GLuint, std::vector<VertexAttributes> > AttributeMap;
  AttributeMap Attributes;
};

#define BUFFER_OFFSET(i) ((char *)NULL + (i))

vtkOpenGLVertexArrayObject::vtkOpenGLVertexArrayObject()
{
  this->Internal = new vtkOpenGLVertexArrayObject::Private;
}

vtkOpenGLVertexArrayObject::~vtkOpenGLVertexArrayObject()
{
  delete this->Internal;
}

void vtkOpenGLVertexArrayObject::SetForceEmulation(bool val)
{
  this->Internal->ForceEmulation = val;
}

void vtkOpenGLVertexArrayObject::Bind()
{
  // Either simply bind the VAO, or emulate behavior by binding all attributes.
  if (!this->Internal->IsReady())
  {
    this->Internal->Initialize();
  }
  if (this->Internal->IsReady() && this->Internal->Supported)
  {
    glBindVertexArray(this->Internal->HandleVAO);
  }
  else if (this->Internal->IsReady())
  {
    Private::AttributeMap::const_iterator it;
    for (it = this->Internal->Attributes.begin(); it != this->Internal->Attributes.end();
         ++it)
    {
      std::vector<VertexAttributes>::const_iterator attrIt;
      glBindBuffer(GL_ARRAY_BUFFER, it->first);
      for (attrIt = it->second.begin(); attrIt != it->second.end(); ++attrIt)
      {
        int matrixCount = attrIt->IsMatrix ? attrIt->Size : 1;
        for (int i = 0; i < matrixCount; ++i)
        {
          glEnableVertexAttribArray(attrIt->Index+i);
          glVertexAttribPointer(attrIt->Index+i, attrIt->Size, attrIt->Type,
                                attrIt->Normalize, attrIt->Stride,
                                BUFFER_OFFSET(attrIt->Offset + attrIt->Stride*i/attrIt->Size));
          if (attrIt->Divisor > 0)
          {
#if GL_ES_VERSION_2_0 != 1 || GL_ES_VERSION_3_0 == 1
#if GL_ES_VERSION_3_0 == 1
            glVertexAttribDivisor(attrIt->Index+i, 1);
#else
            if (GLEW_ARB_instanced_arrays)
            {
              glVertexAttribDivisorARB(attrIt->Index+i, 1);
            }
#endif
#endif
          }
        }
      }
      glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
  }
}

void vtkOpenGLVertexArrayObject::Release()
{
  if (this->Internal->IsReady() && this->Internal->Supported)
  {
    glBindVertexArray(0);
  }
  else if (this->Internal->IsReady())
  {
    Private::AttributeMap::const_iterator it;
    for (it = this->Internal->Attributes.begin(); it != this->Internal->Attributes.end();
         ++it)
    {
      std::vector<VertexAttributes>::const_iterator attrIt;
      for (attrIt = it->second.begin(); attrIt != it->second.end(); ++attrIt)
      {
        int matrixCount = attrIt->IsMatrix ? attrIt->Size : 1;
        for (int i = 0; i < matrixCount; ++i)
        {
          if (attrIt->Divisor > 0)
          {
#if GL_ES_VERSION_2_0 != 1 || GL_ES_VERSION_3_0 == 1
#if GL_ES_VERSION_3_0 == 1
            glVertexAttribDivisor(attrIt->Index+i, 0);
#else
            if (GLEW_ARB_instanced_arrays)
            {
              glVertexAttribDivisorARB(attrIt->Index+i, 0);
            }
#endif
#endif
          }
          glDisableVertexAttribArray(attrIt->Index+i);
        }
      }
    }
  }
}

void vtkOpenGLVertexArrayObject::ShaderProgramChanged()
{
  this->Release();

  Private::AttributeMap::iterator it;
  for (it = this->Internal->Attributes.begin(); it != this->Internal->Attributes.end();
       ++it)
  {
    it->second.clear();
  }
  this->Internal->Attributes.clear();

  this->Internal->HandleProgram = 0;
}

void vtkOpenGLVertexArrayObject::ReleaseGraphicsResources()
{
  this->ShaderProgramChanged();
  this->Internal->ReleaseGraphicsResources();
}

bool vtkOpenGLVertexArrayObject::AddAttributeArrayWithDivisor(vtkShaderProgram *program,
                                          vtkOpenGLBufferObject *buffer,
                                          const std::string &name,
                                          int offset, size_t stride,
                                          int elementType, int elementTupleSize,
                                          bool normalize,
                                          int divisor, bool isMatrix)
{
  if(!program)
  {
    vtkErrorMacro("attempt to add attribute without a program for attribute " << name);
    return false;
  }

  // Check the program is bound, and the buffer is valid.
  if (!program->isBound())
  {
    vtkErrorMacro("attempt to add attribute without a bound program for attribute " << name);
    return false;
  }

  if (buffer->GetHandle() == 0)
  {
    vtkErrorMacro("attempt to add attribute without a handleless buffer for attribute " << name);
    return false;
  }

  if (buffer->GetType() != vtkOpenGLBufferObject::ArrayBuffer )
  {
    vtkErrorMacro("attempt to add attribute without an array buffer for attribute " << name);
    return false;
  }

  // Perform initalization if necessary, ensure program matches VAOs.
  if (this->Internal->HandleProgram == 0)
  {
    this->Internal->HandleProgram = static_cast<GLuint>(program->GetHandle());
  }
  if (!this->Internal->IsReady() ||
      this->Internal->HandleProgram != static_cast<GLuint>(program->GetHandle()))
  {
    vtkErrorMacro("attempt to add attribute when not ready for attribute " << name);
    return false;
  }

  const GLchar *namePtr = static_cast<const GLchar *>(name.c_str());
  VertexAttributes attribs;
  attribs.Index = glGetAttribLocation(this->Internal->HandleProgram, namePtr);
  attribs.Offset = offset;
  attribs.Stride = static_cast<GLsizei>(stride);
  attribs.Type = convertTypeToGL(elementType);
  attribs.Size = elementTupleSize;
  attribs.Normalize = normalize;
  attribs.IsMatrix = isMatrix;
  attribs.Divisor = divisor;

  if (attribs.Index == -1)
  {
    vtkErrorMacro("attempt to add attribute not found in program for attribute " << name);
    return false;
  }

  // Always make the call as even the first use wants the attrib pointer setting
  // up when we are emulating.
  buffer->Bind();
  glEnableVertexAttribArray(attribs.Index);
  glVertexAttribPointer(attribs.Index, attribs.Size, attribs.Type,
                        attribs.Normalize, attribs.Stride,
                        BUFFER_OFFSET(attribs.Offset));


  if (divisor > 0)
  {
#if GL_ES_VERSION_2_0 != 1 || GL_ES_VERSION_3_0 == 1
#if GL_ES_VERSION_3_0 == 1
    glVertexAttribDivisor(attribs.Index, 1);
#else
    if (GLEW_ARB_instanced_arrays)
    {
      glVertexAttribDivisorARB(attribs.Index, 1);
    }
#endif
#endif
  }

  // If vertex array objects are not supported then build up our list.
  if (!this->Internal->Supported)
  {
    GLuint handleBuffer = buffer->GetHandle();
    Private::AttributeMap::iterator it = this->Internal->Attributes.find(handleBuffer);
    if (it != this->Internal->Attributes.end())
    {
      std::vector<VertexAttributes> &attribsVector = it->second;
      std::vector<VertexAttributes>::iterator it2;
      for (it2 = attribsVector.begin(); it2 != attribsVector.end(); ++it2)
      {
        if (it2->Index == attribs.Index)
        {
          *it2 = attribs;
          return true;
        }
      }
      // Attribute not found, add it.
      attribsVector.push_back(attribs);
    }
    else
    {
      // a single handle can have multiple attribs
      std::vector<VertexAttributes> attribsVector;
      attribsVector.push_back(attribs);
      this->Internal->Attributes[handleBuffer] = attribsVector;
    }
  }

  return true;
}

bool vtkOpenGLVertexArrayObject::AddAttributeMatrixWithDivisor(
  vtkShaderProgram *program,
  vtkOpenGLBufferObject *buffer,
  const std::string &name,
  int offset, size_t stride,
  int elementType, int elementTupleSize,
  bool normalize,
  int divisor)
{
  // bind the first row of values
  bool result =
    this->AddAttributeArrayWithDivisor(program, buffer, name,
      offset, stride, elementType, elementTupleSize, normalize, divisor, true);

  if (!result)
  {
    return result;
  }

  const GLchar *namePtr = static_cast<const GLchar *>(name.c_str());
  VertexAttributes attribs;
  attribs.Index = glGetAttribLocation(this->Internal->HandleProgram, namePtr);

  for (int i = 1; i < elementTupleSize; i++)
  {
    glEnableVertexAttribArray(attribs.Index+i);
    glVertexAttribPointer(attribs.Index + i, elementTupleSize, convertTypeToGL(elementType),
                          normalize, static_cast<GLsizei>(stride),
                          BUFFER_OFFSET(offset + stride*i/elementTupleSize));
    if (divisor > 0)
    {
#if GL_ES_VERSION_2_0 != 1 || GL_ES_VERSION_3_0 == 1
#if GL_ES_VERSION_3_0 == 1
      glVertexAttribDivisor(attribs.Index+i, 1);
#else
      if (GLEW_ARB_instanced_arrays)
      {
        glVertexAttribDivisorARB(attribs.Index+i, 1);
      }
#endif
#endif
    }
  }

  return true;
}

bool vtkOpenGLVertexArrayObject::RemoveAttributeArray(const std::string &name)
{
  if (!this->Internal->IsReady() || this->Internal->HandleProgram == 0)
  {
    return false;
  }

  const GLchar *namePtr = static_cast<const GLchar *>(name.c_str());
  GLint location = glGetAttribLocation(this->Internal->HandleProgram, namePtr);
  if (location == -1)
  {
    return false;
  }

  glDisableVertexAttribArray(location);
  // If we don't have real VAOs find the entry and remove it too.
  if (!this->Internal->Supported)
  {
    Private::AttributeMap::iterator it;
    for (it = this->Internal->Attributes.begin(); it != this->Internal->Attributes.end();
         ++it)
    {
      std::vector<VertexAttributes>::iterator attrIt;
      for (attrIt = it->second.begin(); attrIt != it->second.end(); ++attrIt)
      {
        if (attrIt->Index == location)
        {
          it->second.erase(attrIt);
          return true;
        }
      }
    }
  }

  return true;
}

//-----------------------------------------------------------------------------
void vtkOpenGLVertexArrayObject::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
}