File: vtkShader2.cxx

package info (click to toggle)
vtk6 6.3.0%2Bdfsg2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,972 kB
  • sloc: cpp: 1,442,790; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,119; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 154; makefile: 68; objc: 17
file content (382 lines) | stat: -rw-r--r-- 11,243 bytes parent folder | download | duplicates (6)
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkShader2.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 "vtkShader2.h"
#include "vtkObjectFactory.h"
#include <cassert>
#include <vtkgl.h>
#include "vtkUniformVariables.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLExtensionManager.h"
#include "vtkOpenGLError.h"

static GLenum vtkShaderTypeVTKToGL[5] = {
  vtkgl::VERTEX_SHADER, // VTK_SHADER_TYPE_VERTEX=0
  vtkgl::GEOMETRY_SHADER, // VTK_SHADER_TYPE_GEOMETRY=1,
  vtkgl::FRAGMENT_SHADER, // VTK_SHADER_TYPE_FRAGMENT=2,
  0, // VTK_SHADER_TYPE_TESSELLATION_CONTROL=3, not yet
  0// VTK_SHADER_TYPE_TESSELLATION_EVALUATION=4, not yet
};

static const char *TypeAsStringArray[5] = {
  "vertex shader",
  "geometry shader",
  "fragment shader",
  "tessellation control shader",
  "tessellation evaluation shader"
};

//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkShader2);

//-----------------------------------------------------------------------------
vtkCxxSetObjectMacro(vtkShader2,UniformVariables,vtkUniformVariables);

// ----------------------------------------------------------------------------
// Description:
// Default constructor. SourceCode is NULL. Type is vertex.
vtkShader2::vtkShader2()
{
  // user API
  this->SourceCode = 0;
  this->Type = VTK_SHADER_TYPE_VERTEX;

  // OpenGL part
  this->Context = NULL;
  this->Id = 0;
  this->ExtensionsLoaded = false;
  this->SupportGeometryShader = false;
  this->LastCompileStatus = false;

  // 8 as an initial capcity is nice because the allocation is aligned on
  // 32-bit or 64-bit architecture.
  this->LastCompileLogCapacity = 8;
  this->LastCompileLog = new char[this->LastCompileLogCapacity];
  this->LastCompileLog[0] = '\0'; // empty string

  this->UniformVariables = vtkUniformVariables::New();
}

// ----------------------------------------------------------------------------
void vtkShader2::ReleaseGraphicsResources()
{
  // 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->Id != 0))
    {
    vtkgl::DeleteShader(this->Id);
    vtkOpenGLCheckErrorMacro("failed at glDeleteShader");
    this->Id = 0;
    }
}

// ----------------------------------------------------------------------------
// Description:
// Destructor. Delete SourceCode if any.
vtkShader2::~vtkShader2()
{
  // esplicitly release resources
  this->ReleaseGraphicsResources();

  delete[] this->SourceCode;
  delete[] this->LastCompileLog;

  if (this->UniformVariables)
    {
    this->UniformVariables->Delete();
    }
}

//----------------------------------------------------------------------------
bool vtkShader2::IsSupported(vtkRenderWindow *renWin)
{
  vtkOpenGLRenderWindow *context
    = dynamic_cast<vtkOpenGLRenderWindow*>(renWin);

  if (!context) return false;

  vtkOpenGLExtensionManager *e = context->GetExtensionManager();
  return e->ExtensionSupported("GL_VERSION_2_0") ||
    (e->ExtensionSupported("GL_ARB_shading_language_100") &&
     e->ExtensionSupported("GL_ARB_shader_objects") &&
     e->ExtensionSupported("GL_ARB_vertex_shader") &&
     e->ExtensionSupported("GL_ARB_fragment_shader"));
}

//----------------------------------------------------------------------------
bool vtkShader2::LoadRequiredExtensions(vtkRenderWindow *renWin)
{
  vtkOpenGLRenderWindow *context
    = dynamic_cast<vtkOpenGLRenderWindow*>(renWin);

  this->ExtensionsLoaded = false;
  this->SupportGeometryShader = false;

  if (!context) return false;

  vtkOpenGLExtensionManager *e = context->GetExtensionManager();


  if (e->ExtensionSupported("GL_VERSION_2_0"))
    {
    e->LoadExtension("GL_VERSION_2_0");
    this->ExtensionsLoaded = true;
    }
  else
    {
    if (e->ExtensionSupported("GL_ARB_shading_language_100") &&
      e->ExtensionSupported("GL_ARB_shader_objects") &&
      e->ExtensionSupported("GL_ARB_vertex_shader") &&
      e->ExtensionSupported("GL_ARB_fragment_shader"))
      {
      e->LoadCorePromotedExtension("GL_ARB_shading_language_100");
      e->LoadCorePromotedExtension("GL_ARB_shader_objects");
      e->LoadCorePromotedExtension("GL_ARB_vertex_shader");
      e->LoadCorePromotedExtension("GL_ARB_fragment_shader");
      this->ExtensionsLoaded = true;
      }
    }

  if (this->ExtensionsLoaded)
    {
    bool supportGeometryShaderARB
      = e->ExtensionSupported("GL_ARB_geometry_shader4") == 1;

    this->SupportGeometryShader
      = supportGeometryShaderARB
      || e->ExtensionSupported("GL_EXT_geometry_shader4") == 1;

    if (this->SupportGeometryShader)
      {
      if (supportGeometryShaderARB)
        {
        e->LoadExtension("GL_ARB_geometry_shader4");
        }
      else
        {
        e->LoadAsARBExtension("GL_EXT_geometry_shader4");
        }
      }
    }

  return this->ExtensionsLoaded;
}

// ----------------------------------------------------------------------------
vtkRenderWindow *vtkShader2::GetContext()
{
  return this->Context;
}

// ----------------------------------------------------------------------------
void vtkShader2::SetContext(vtkRenderWindow *renWin)
{
  // avoid pointless reassignment
  if (this->Context == renWin)
    {
    return;
    }
  // free resources
  this->ReleaseGraphicsResources();
  this->Context = NULL;
  this->Modified();
  // all done if assigned null
  if (!renWin)
    {
    return;
    }
  // check for support
  vtkOpenGLRenderWindow *context
    = dynamic_cast<vtkOpenGLRenderWindow*>(renWin);

  if ( !context
    || !this->LoadRequiredExtensions(renWin) )
    {
    vtkErrorMacro("The context does not support the required extensions");
    return;
    }
  // initialize
  this->Context = renWin;
  this->Context->MakeCurrent();
}

//-----------------------------------------------------------------------------
// Description:
// Compile the shader code.
// The result of compilation can be query with GetLastCompileStatus()
// The log of compilation can be query with GetLastCompileLog()
// \pre SourceCode_exists: SourceCode!=0
void vtkShader2::Compile()
{
  assert("pre: SourceCode_exists" && this->SourceCode != 0);
  vtkOpenGLClearErrorMacro();

  if(this->Id == 0 || this->LastCompileTime < this->MTime)
    {
    if (this->Type == VTK_SHADER_TYPE_TESSELLATION_CONTROL)
      {
      vtkErrorMacro(<<"tessellation control shader is not supported.");
      this->LastCompileStatus = false;
      this->LastCompileLog = 0;
      return;
      }
     if (this->Type == VTK_SHADER_TYPE_TESSELLATION_EVALUATION)
      {
      vtkErrorMacro(<<"tessellation evaluation shader is not supported.");
      this->LastCompileStatus = false;
      this->LastCompileLog = 0;
      return;
      }
    if (this->Type == VTK_SHADER_TYPE_GEOMETRY && !this->SupportGeometryShader)
      {
      vtkErrorMacro(<<"geometry shader is not supported.");
      this->LastCompileStatus = false;
      this->LastCompileLog = 0;
      return;
      }
    GLuint shaderId = static_cast<GLuint>(this->Id);
    if (shaderId == 0)
      {
      shaderId = vtkgl::CreateShader(vtkShaderTypeVTKToGL[this->Type]);
      if (shaderId == 0)
        {
        vtkErrorMacro(<<"fatal error (bad current OpenGL context?, extension not supported?).");
        this->LastCompileStatus = false;
        this->LastCompileLog = 0;
        return;
        }
      this->Id = static_cast<unsigned int>(shaderId);
      }

    vtkgl::ShaderSource(shaderId,1,const_cast<const vtkgl::GLchar**>(&this->SourceCode), 0);
    vtkgl::CompileShader(shaderId);
    GLint value;
    vtkgl::GetShaderiv(shaderId, vtkgl::COMPILE_STATUS, &value);
    this->LastCompileStatus = (value== GL_TRUE);
    vtkgl::GetShaderiv(shaderId, vtkgl::INFO_LOG_LENGTH, &value);
    if (static_cast<size_t>(value) > this->LastCompileLogCapacity)
      {
      delete[] this->LastCompileLog;
      this->LastCompileLogCapacity = static_cast<size_t>(value);
      this->LastCompileLog = new char[this->LastCompileLogCapacity];
      }
    vtkgl::GetShaderInfoLog(shaderId,value, 0, this->LastCompileLog);
    this->LastCompileTime.Modified();
    }

  vtkOpenGLCheckErrorMacro("failed after Compile");
}

//-----------------------------------------------------------------------------
// Description:
// Return the shader type as a string.
const char *vtkShader2::GetTypeAsString()
{
  return TypeAsStringArray[this->Type];
}

//-----------------------------------------------------------------------------
// Description:
// Tells if the last call to compile succeeded (true) or not (false).
bool vtkShader2::GetLastCompileStatus()
{
  return this->LastCompileStatus;
}

//-----------------------------------------------------------------------------
// Description:
// Return the log of the last call to compile as a string.
const char *vtkShader2::GetLastCompileLog()
{
  assert("post: result_exists" && this->LastCompileLog != 0);
  return this->LastCompileLog;
}

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

  os << indent << "Type: ";
  switch (this->Type)
    {
    case VTK_SHADER_TYPE_VERTEX:
      os << "vertex" << endl;
      break;
    case VTK_SHADER_TYPE_TESSELLATION_CONTROL:
      os << "tessellation control" << endl;
      break;
    case VTK_SHADER_TYPE_TESSELLATION_EVALUATION:
      os << "tessellation evaluation" << endl;
      break;
    case VTK_SHADER_TYPE_GEOMETRY:
      os << "geometry" << endl;
      break;
    case VTK_SHADER_TYPE_FRAGMENT:
      os << "fragment" << endl;
      break;
    default:
      assert("check: impossible_case" && 0); // impossible case
    }

  os << indent << "OpenGL Id: " << this->Id << endl;
  os << indent << "Last Compile Status: ";
  os << ((this->LastCompileStatus) ? "true" : "false") << endl;

  os << indent << "Last Compile Log Capacity: " <<
    this->LastCompileLogCapacity<< endl;

  os << indent << "Last Compile Log: ";
  if (this->LastCompileLog == 0)
    {
    os << "(none)" << endl;
    }
  else
    {
    os << this->LastCompileLog << endl;
    }

  os << indent << "Context: ";
  if (this->Context)
    {
    os << static_cast<void *>(this->Context) <<endl;
    }
  else
    {
    os << "none" << endl;
    }

  os << indent << "UniformVariables: ";
  if (this->UniformVariables)
    {
    this->UniformVariables->PrintSelf(os, indent);
    }
  else
    {
    os << "none" << endl;
    }

  os << indent << "SourceCode: ";
  if (this->SourceCode == 0)
    {
    os << "(none)" << endl;
    }
  else
    {
    os << endl << this->SourceCode << endl;
    }
}