File: vtkValuePassHelper.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 (344 lines) | stat: -rw-r--r-- 10,678 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
#include "vtkDataArray.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkMapper.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLBufferObject.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLHelper.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLVertexArrayObject.h"
#include "vtkPolyData.h"
#include "vtkRenderer.h"
#include "vtkShaderProgram.h"
#include "vtkTextureObject.h"
#include "vtkValuePass.h"
#include "vtkValuePassHelper.h"
#include "vtkNew.h"


vtkStandardNewMacro(vtkValuePassHelper)

//------------------------------------------------------------------------------
class vtkValuePassHelper::vtkInternals
{
public:

  vtkInternals()
  : PointBuffer(NULL)
  , ValuePassArray(NULL)
  , CurrentDataArrayMode(VTK_SCALAR_MODE_USE_POINT_FIELD_DATA)
  , LastDataArrayMode(VTK_SCALAR_MODE_USE_POINT_FIELD_DATA)
  , ResourcesAllocated(false)
  , CellFloatTexture(NULL)
  , CellFloatBuffer(NULL)
  {
    this->CurrentValues->SetNumberOfComponents(1);
  }

  ~vtkInternals()
  {
    // Graphics resources released previously by the parent mapper or after switching
    // to INVERTIBLE_LUT mode
    if (this->PointBuffer)
    {
      this->PointBuffer->Delete();
      this->PointBuffer = NULL;
    }

    if (this->CellFloatTexture)
    {
      this->CellFloatTexture->Delete();
      this->CellFloatTexture = NULL;
    }

    if (this->CellFloatBuffer)
    {
      this->CellFloatBuffer->Delete();
      this->CellFloatBuffer = NULL;
    }
  }

  vtkOpenGLBufferObject* PointBuffer;
  vtkDataArray* ValuePassArray;
  vtkNew<vtkFloatArray> CurrentValues;
  int CurrentDataArrayMode;
  int LastDataArrayMode;
  bool ResourcesAllocated;
  vtkTextureObject* CellFloatTexture;
  vtkOpenGLBufferObject* CellFloatBuffer;
};

////////////////////////////////////////////////////////////////////////////////
vtkValuePassHelper::vtkValuePassHelper()
: Impl(new vtkInternals)
, RenderingMode(-1)
{
}

//-----------------------------------------------------------------------------
vtkValuePassHelper::~vtkValuePassHelper()
{
  delete Impl;
}

//-----------------------------------------------------------------------------
void vtkValuePassHelper::ReleaseGraphicsResources(vtkWindow *win)
{
  if (this->Impl->CellFloatTexture)
  {
    this->Impl->CellFloatTexture->ReleaseGraphicsResources(win);
    this->Impl->CellFloatTexture->Delete();
    this->Impl->CellFloatTexture = NULL;
  }

  if (this->Impl->CellFloatBuffer)
  {
    this->Impl->CellFloatBuffer->ReleaseGraphicsResources();
    this->Impl->CellFloatBuffer->Delete();
    this->Impl->CellFloatBuffer = NULL;
  }

  if (this->Impl->PointBuffer)
  {
    this->Impl->PointBuffer->ReleaseGraphicsResources();
    this->Impl->PointBuffer->Delete();
    this->Impl->PointBuffer = NULL;
  }

  this->Impl->ValuePassArray = NULL;
  this->Impl->ResourcesAllocated = false;
}

//-----------------------------------------------------------------------------
void vtkValuePassHelper::RenderPieceFinish()
{
  if (this->Impl->CurrentDataArrayMode == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA)
  {
    if (this->Impl->CellFloatTexture)
    {
      this->Impl->CellFloatTexture->Deactivate();
    }
  }
}

//-----------------------------------------------------------------------------
void vtkValuePassHelper::RenderPieceStart(vtkActor* actor, vtkDataSet* input)
{
  vtkInformation *info = actor->GetPropertyKeys();

  // TODO It should only be necessary to upload the data if something has changed.
  // In the parallel case however (ParaView with IceT), the solution below causes
  // data not to be uploaded at all (leading to empty images). Because of this, data
  // is uploaded on every render pass.
  //
  //if (info && info->Has(vtkValuePass::RELOAD_DATA()))
  if (info)
  {
    int cellFlag = 0;
    typedef vtkValuePass vp;
    this->Impl->ValuePassArray = vtkAbstractMapper::GetScalars(input,
      info->Get(vp::SCALAR_MODE()), info->Get(vp::ARRAY_MODE()),
      info->Get(vp::ARRAY_ID()), info->Get(vp::ARRAY_NAME()), cellFlag);

    if (!this->Impl->ValuePassArray)
    {
      vtkErrorMacro("Invalid data array from GetScalars()!");
      return;
    }

    vtkIdType const numTuples = this->Impl->ValuePassArray->GetNumberOfTuples();
    int const comp = info->Get(vp::ARRAY_COMPONENT());
    this->Impl->CurrentValues->SetNumberOfTuples(numTuples);
    this->Impl->CurrentValues->CopyComponent(0, this->Impl->ValuePassArray, comp);
    float const* data = static_cast<float*>(this->Impl->CurrentValues->GetVoidPointer(0));

    // Upload array data
    if (this->Impl->CurrentDataArrayMode == VTK_SCALAR_MODE_USE_POINT_FIELD_DATA)
    {
      // Point data
      this->Impl->PointBuffer->Upload(data, static_cast<size_t>(numTuples),
        vtkOpenGLBufferObject::ArrayBuffer);
    }
    else
    {
      // Cell data
      this->Impl->CellFloatBuffer->Upload(data, static_cast<size_t>(numTuples),
        vtkOpenGLBufferObject::TextureBuffer);

      this->Impl->CellFloatTexture->CreateTextureBuffer(static_cast<unsigned int>(numTuples),
        1, VTK_FLOAT, this->Impl->CellFloatBuffer);
    }
  }

  // Bind textures
  if (this->Impl->CurrentDataArrayMode == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA)
  {
    this->Impl->CellFloatTexture->Activate();
  }
}

//-----------------------------------------------------------------------------
void vtkValuePassHelper::UpdateConfiguration(vtkRenderer* ren, vtkActor* act,
  vtkMapper* mapper, vtkPolyData* input)
{
  this->RenderingMode = -1;
  vtkInformation *info = act->GetPropertyKeys();
  if (info && info->Has(vtkValuePass::RENDER_VALUES()))
  {
    this->RenderingMode = info->Get(vtkValuePass::RENDER_VALUES());
  }

  // Configure the mapper's behavior if the ValuePass is active.
  if (this->RenderingMode > 0)
  {
    // Since it has RENDER_VALUES it is assumed it has all the tags from ValuePass
    this->Impl->CurrentDataArrayMode = info->Get(vtkValuePass::SCALAR_MODE());

    switch (this->RenderingMode)
    {
      case vtkValuePass::FLOATING_POINT:
        this->AllocateGraphicsResources(ren);
        break;

      case vtkValuePass::INVERTIBLE_LUT:
      default:
      {
        mapper->UseInvertibleColorFor(input,
                                    info->Get(vtkValuePass::SCALAR_MODE()),
                                    info->Get(vtkValuePass::ARRAY_MODE()),
                                    info->Get(vtkValuePass::ARRAY_ID()),
                                    info->Get(vtkValuePass::ARRAY_NAME()),
                                    info->Get(vtkValuePass::ARRAY_COMPONENT()),
                                    info->Get(vtkValuePass::SCALAR_RANGE()));
      }
        break;
    }
  }
  else
  {
    this->ReleaseGraphicsResources(ren->GetRenderWindow());
    mapper->ClearInvertibleColor();
  }
}

//-----------------------------------------------------------------------------
void vtkValuePassHelper::AllocateGraphicsResources(vtkRenderer* ren)
{
  if (this->Impl->ResourcesAllocated)
  {
    return;
  }

  // For point data
  this->Impl->PointBuffer = vtkOpenGLBufferObject::New();
  this->Impl->PointBuffer->SetType(vtkOpenGLBufferObject::ArrayBuffer);

  // For cell data
  this->Impl->CellFloatTexture = vtkTextureObject::New();
  this->Impl->CellFloatTexture->SetContext
    (static_cast<vtkOpenGLRenderWindow*>(ren->GetVTKWindow()));

  this->Impl->CellFloatBuffer = vtkOpenGLBufferObject::New();
  this->Impl->CellFloatBuffer->SetType(vtkOpenGLBufferObject::TextureBuffer);

  this->Impl->ResourcesAllocated = true;
}

//-----------------------------------------------------------------------------
void vtkValuePassHelper::UpdateShaders(std::string & VSSource, std::string & FSSource,
  std::string & required)
{
  // Pass the value pass attribute to the fragment shader.
  vtkShaderProgram::Substitute(VSSource, "//VTK::ValuePass::Dec",
    "attribute float dataAttribute;\n"
    "varying float dataValue;\n"
    "uniform samplerBuffer textureF;\n"
    );

  vtkShaderProgram::Substitute(VSSource, "//VTK::ValuePass::Impl",
    " dataValue = dataAttribute;\n");

  // Render floating point values (variables in 'required' are a requirement in
  // other sections of the fragment shader, so they are included for it to build
  // correctly).
  vtkShaderProgram::Substitute(FSSource, "//VTK::ValuePass::Dec",
    "varying float dataValue;\n"
    "uniform samplerBuffer textureF;\n");

  if (this->Impl->CurrentDataArrayMode == VTK_SCALAR_MODE_USE_POINT_FIELD_DATA)
  {
    required += std::string
      (
       "  vec4 texColor = vec4(vec3(dataValue), 1.0);\n"
       "  gl_FragData[0] = texColor;\n"
       "  // Return right away since vtkValuePass::FLOATING_POINT mode is enabled\n"
       "  return;"
       );
  }
  else
  {
    required += std::string
      (
       "  gl_FragData[0] = texelFetchBuffer(textureF, gl_PrimitiveID + PrimitiveIDOffset);\n"

       "  // Return right away since vtkValuePass::FLOATING_POINT mode is enabled\n"
       "  return;"
       );
  }

  vtkShaderProgram::Substitute(FSSource, "//VTK::Color::Impl", required);
}

//-----------------------------------------------------------------------------
void vtkValuePassHelper::BindAttributes(vtkOpenGLHelper& cellBO)
{
  if (this->Impl->CurrentDataArrayMode == VTK_SCALAR_MODE_USE_POINT_FIELD_DATA)
  {
    if (this->Impl->ValuePassArray)
    {
      if (cellBO.Program->IsAttributeUsed("dataAttribute"))
      {
        size_t const stride = sizeof(float);

        if (!cellBO.VAO->AddAttributeArray(cellBO.Program, this->Impl->PointBuffer,
          "dataAttribute", 0, stride, VTK_FLOAT, 1, false))
        {
          vtkErrorMacro(<< "Error setting 'dataAttribute' in shader VAO.");
        }
      }
    }
  }
}

//-----------------------------------------------------------------------------
void vtkValuePassHelper::BindUniforms(vtkOpenGLHelper& cellBO)
{
  if (this->Impl->CurrentDataArrayMode == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA)
  {
    if (cellBO.Program->IsAttributeUsed("textureF"))
    {
      int tunit = this->Impl->CellFloatTexture->GetTextureUnit();
      cellBO.Program->SetUniformi("textureF", tunit);
    }
  }
}

//-----------------------------------------------------------------------------
bool vtkValuePassHelper::RequiresShaderRebuild()
{
  if (this->RenderingMode == vtkValuePass::FLOATING_POINT &&
   this->Impl->CurrentDataArrayMode != this->Impl->LastDataArrayMode)
  {
    this->Impl->LastDataArrayMode = this->Impl->CurrentDataArrayMode;
    return true;
  }

  return false;
}

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