forked from Kitware/VTK
/
vtkOpenGLFXAAFilter.cxx
469 lines (405 loc) · 14.9 KB
/
vtkOpenGLFXAAFilter.cxx
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
467
468
469
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenGLFXAAFilter.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 "vtkOpenGLFXAAFilter.h"
#include "vtk_glew.h"
#include "vtkFXAAOptions.h"
#include "vtkObjectFactory.h"
#include "vtkOpenGLBufferObject.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGLQuadHelper.h"
#include "vtkOpenGLRenderer.h"
#include "vtkOpenGLRenderTimer.h"
#include "vtkOpenGLRenderUtilities.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLShaderCache.h"
#include "vtkOpenGLState.h"
#include "vtkOpenGLVertexArrayObject.h"
#include "vtkShaderProgram.h"
#include "vtkTextureObject.h"
#include "vtkTimerLog.h"
#include "vtkTypeTraits.h"
#include <algorithm>
#include <cassert>
// Our fragment shader:
#include "vtkFXAAFilterFS.h"
// Define to perform/dump benchmarking info:
//#define FXAA_BENCHMARK
vtkStandardNewMacro(vtkOpenGLFXAAFilter)
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::PrintSelf(std::ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "RelativeContrastThreshold: "
<< this->RelativeContrastThreshold << "\n";
os << indent << "HardContrastThreshold: "
<< this->HardContrastThreshold << "\n";
os << indent << "SubpixelBlendLimit: " <<
this->SubpixelBlendLimit << "\n";
os << indent << "SubpixelContrastThreshold: "
<< this->SubpixelContrastThreshold << "\n";
os << indent << "EndpointSearchIterations: "
<< this->EndpointSearchIterations << "\n";
os << indent << "UseHighQualityEndpoints: "
<< this->UseHighQualityEndpoints << "\n";
os << indent << "DebugOptionValue: ";
switch (this->DebugOptionValue)
{
default:
case vtkFXAAOptions::FXAA_NO_DEBUG:
os << "FXAA_NO_DEBUG\n";
break;
case vtkFXAAOptions::FXAA_DEBUG_SUBPIXEL_ALIASING:
os << "FXAA_DEBUG_SUBPIXEL_ALIASING\n";
break;
case vtkFXAAOptions::FXAA_DEBUG_EDGE_DIRECTION:
os << "FXAA_DEBUG_EDGE_DIRECTION\n";
break;
case vtkFXAAOptions::FXAA_DEBUG_EDGE_NUM_STEPS:
os << "FXAA_DEBUG_EDGE_NUM_STEPS\n";
break;
case vtkFXAAOptions::FXAA_DEBUG_EDGE_DISTANCE:
os << "FXAA_DEBUG_EDGE_DISTANCE\n";
break;
case vtkFXAAOptions::FXAA_DEBUG_EDGE_SAMPLE_OFFSET:
os << "FXAA_DEBUG_EDGE_SAMPLE_OFFSET\n";
break;
case vtkFXAAOptions::FXAA_DEBUG_ONLY_SUBPIX_AA:
os << "FXAA_DEBUG_ONLY_SUBPIX_AA\n";
break;
case vtkFXAAOptions::FXAA_DEBUG_ONLY_EDGE_AA:
os << "FXAA_DEBUG_ONLY_EDGE_AA\n";
break;
}
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::Execute(vtkOpenGLRenderer *ren)
{
assert(ren);
this->Renderer = ren;
this->StartTimeQuery(this->PreparationTimer);
this->Prepare();
this->LoadInput();
this->EndTimeQuery(this->PreparationTimer);
this->StartTimeQuery(this->FXAATimer);
this->ApplyFilter();
this->EndTimeQuery(this->FXAATimer);
this->Finalize();
this->PrintBenchmark();
this->Renderer = nullptr;
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::ReleaseGraphicsResources()
{
this->FreeGLObjects();
this->PreparationTimer->ReleaseGraphicsResources();
this->FXAATimer->ReleaseGraphicsResources();
if (this->QHelper)
{
delete this->QHelper;
this->QHelper = nullptr;
}
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::UpdateConfiguration(vtkFXAAOptions *opts)
{
// Use the setters -- some of these options will trigger a shader rebuild
// when they change, and the setters hold the logic for determining this.
this->SetRelativeContrastThreshold(opts->GetRelativeContrastThreshold());
this->SetHardContrastThreshold(opts->GetHardContrastThreshold());
this->SetSubpixelBlendLimit(opts->GetSubpixelBlendLimit());
this->SetSubpixelContrastThreshold(opts->GetSubpixelContrastThreshold());
this->SetEndpointSearchIterations(opts->GetEndpointSearchIterations());
this->SetUseHighQualityEndpoints(opts->GetUseHighQualityEndpoints());
this->SetDebugOptionValue(opts->GetDebugOptionValue());
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::SetUseHighQualityEndpoints(bool val)
{
if (this->UseHighQualityEndpoints != val)
{
this->NeedToRebuildShader = true;
this->Modified();
this->UseHighQualityEndpoints = val;
}
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::SetDebugOptionValue(vtkFXAAOptions::DebugOption opt)
{
if (this->DebugOptionValue != opt)
{
this->NeedToRebuildShader = true;
this->Modified();
this->DebugOptionValue = opt;
}
}
//------------------------------------------------------------------------------
vtkOpenGLFXAAFilter::vtkOpenGLFXAAFilter()
: BlendState(false),
DepthTestState(false),
PreparationTimer(new vtkOpenGLRenderTimer),
FXAATimer(new vtkOpenGLRenderTimer),
RelativeContrastThreshold(1.f/8.f),
HardContrastThreshold(1.f/16.f),
SubpixelBlendLimit(3.f/4.f),
SubpixelContrastThreshold(1.f/4.f),
EndpointSearchIterations(12),
UseHighQualityEndpoints(true),
DebugOptionValue(vtkFXAAOptions::FXAA_NO_DEBUG),
NeedToRebuildShader(true),
Renderer(nullptr),
Input(nullptr),
QHelper(nullptr)
{
std::fill(this->Viewport, this->Viewport + 4, 0);
}
//------------------------------------------------------------------------------
vtkOpenGLFXAAFilter::~vtkOpenGLFXAAFilter()
{
if (this->QHelper)
{
delete this->QHelper;
this->QHelper = nullptr;
}
this->FreeGLObjects();
delete PreparationTimer;
delete FXAATimer;
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::Prepare()
{
this->Renderer->GetTiledSizeAndOrigin(&this->Viewport[2], &this->Viewport[3],
&this->Viewport[0], &this->Viewport[1]);
// Check if we need to create a new working texture:
if (this->Input)
{
unsigned int rendererWidth = static_cast<unsigned int>(this->Viewport[2]);
unsigned int rendererHeight = static_cast<unsigned int>(this->Viewport[3]);
if (this->Input->GetWidth() != rendererWidth ||
this->Input->GetHeight() != rendererHeight)
{
this->FreeGLObjects();
}
}
if (!this->Input)
{
this->CreateGLObjects();
}
vtkOpenGLState *ostate = this->Renderer->GetState();
this->BlendState = ostate->GetEnumState(GL_BLEND);
this->DepthTestState = ostate->GetEnumState(GL_DEPTH_TEST);
#ifdef __APPLE__
// Restore viewport to its original size. This is necessary only on
// MacOS when HiDPI is supported. Enabling HiDPI has the side effect that
// Cocoa will start overriding any glViewport calls in application code.
// For reference, see QCocoaWindow::initialize().
int origin[2];
int usize, vsize;
this->Renderer->GetTiledSizeAndOrigin(&usize, &vsize, origin, origin+1);
ostate->vtkglViewport(origin[0], origin[1], usize, vsize);
#endif
ostate->vtkglDisable(GL_BLEND);
ostate->vtkglDisable(GL_DEPTH_TEST);
vtkOpenGLCheckErrorMacro("Error after saving GL state.");
}
//------------------------------------------------------------------------------
// Delete the vtkObject subclass pointed at by ptr if it is set.
namespace {
template <typename T> void DeleteHelper(T *& ptr)
{
if (ptr)
{
ptr->Delete();
ptr = nullptr;
}
}
} // end anon namespace
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::FreeGLObjects()
{
DeleteHelper(this->Input);
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::CreateGLObjects()
{
assert(!this->Input);
this->Input = vtkTextureObject::New();
vtkOpenGLRenderWindow* renWin =
static_cast<vtkOpenGLRenderWindow*>(this->Renderer->GetRenderWindow());
this->Input->SetContext(renWin);
this->Input->SetFormat(GL_RGB);
// we need to get the format of current color buffer in order to allocate the right format
// for the texture used in FXAA
int internalFormat = renWin->GetColorBufferInternalFormat(0);
if (internalFormat != 0)
{
this->Input->SetInternalFormat(static_cast<unsigned int>(internalFormat));
}
else // the query failed, fallback to classic texture
{
// ES doesn't support GL_RGB8, and OpenGL 3 doesn't support GL_RGB.
// What a world.
#if defined(GL_ES_VERSION_3_0)
this->Input->SetInternalFormat(GL_RGB);
#else // OpenGL ES
this->Input->SetInternalFormat(GL_RGB8);
#endif // OpenGL ES
}
// Required for FXAA, since we interpolate texels for blending.
this->Input->SetMinificationFilter(vtkTextureObject::Linear);
this->Input->SetMagnificationFilter(vtkTextureObject::Linear);
// Clamp to edge, since we'll be sampling off-texture texels:
this->Input->SetWrapS(vtkTextureObject::ClampToEdge);
this->Input->SetWrapT(vtkTextureObject::ClampToEdge);
this->Input->SetWrapR(vtkTextureObject::ClampToEdge);
this->Input->Allocate2D(this->Viewport[2], this->Viewport[3], 4,
vtkTypeTraits<vtkTypeUInt8>::VTK_TYPE_ID);
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::LoadInput()
{
this->Input->CopyFromFrameBuffer(this->Viewport[0], this->Viewport[1], 0, 0,
this->Viewport[2], this->Viewport[3]);
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::ApplyFilter()
{
typedef vtkOpenGLRenderUtilities GLUtil;
vtkOpenGLRenderWindow *renWin = static_cast<vtkOpenGLRenderWindow*>(
this->Renderer->GetRenderWindow());
this->Input->Activate();
if (this->NeedToRebuildShader)
{
delete this->QHelper;
this->QHelper = nullptr;
this->NeedToRebuildShader = false;
}
if (!this->QHelper)
{
std::string fragShader = vtkFXAAFilterFS;
this->SubstituteFragmentShader(fragShader);
this->QHelper = new vtkOpenGLQuadHelper(renWin,
GLUtil::GetFullScreenQuadVertexShader().c_str(),
fragShader.c_str(),
GLUtil::GetFullScreenQuadGeometryShader().c_str());
}
else
{
renWin->GetShaderCache()->ReadyShaderProgram(this->QHelper->Program);
}
vtkShaderProgram *program = this->QHelper->Program;
program->SetUniformi("Input", this->Input->GetTextureUnit());
float invTexSize[2] = { 1.f / static_cast<float>(this->Viewport[2]),
1.f / static_cast<float>(this->Viewport[3]) };
program->SetUniform2f("InvTexSize", invTexSize);
program->SetUniformf("RelativeContrastThreshold",
this->RelativeContrastThreshold);
program->SetUniformf("HardContrastThreshold",
this->HardContrastThreshold);
program->SetUniformf("SubpixelBlendLimit",
this->SubpixelBlendLimit);
program->SetUniformf("SubpixelContrastThreshold",
this->SubpixelContrastThreshold);
program->SetUniformi("EndpointSearchIterations",
this->EndpointSearchIterations);
this->QHelper->Render();
this->Input->Deactivate();
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::SubstituteFragmentShader(std::string &fragShader)
{
if (this->UseHighQualityEndpoints)
{
vtkShaderProgram::Substitute(fragShader, "//VTK::EndpointAlgo::Def",
"#define FXAA_USE_HIGH_QUALITY_ENDPOINTS");
}
#define DEBUG_OPT_CASE(optName) \
case vtkFXAAOptions::optName: \
vtkShaderProgram::Substitute(fragShader, "//VTK::DebugOptions::Def", \
"#define " #optName); \
break
switch (this->DebugOptionValue)
{
default:
case vtkFXAAOptions::FXAA_NO_DEBUG:
break;
DEBUG_OPT_CASE(FXAA_DEBUG_SUBPIXEL_ALIASING);
DEBUG_OPT_CASE(FXAA_DEBUG_EDGE_DIRECTION);
DEBUG_OPT_CASE(FXAA_DEBUG_EDGE_NUM_STEPS);
DEBUG_OPT_CASE(FXAA_DEBUG_EDGE_DISTANCE);
DEBUG_OPT_CASE(FXAA_DEBUG_EDGE_SAMPLE_OFFSET);
DEBUG_OPT_CASE(FXAA_DEBUG_ONLY_SUBPIX_AA);
DEBUG_OPT_CASE(FXAA_DEBUG_ONLY_EDGE_AA);
}
#undef DEBUG_OPT_CASE
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::Finalize()
{
vtkOpenGLState *ostate = this->Renderer->GetState();
if (this->BlendState)
{
ostate->vtkglEnable(GL_BLEND);
}
if (this->DepthTestState)
{
ostate->vtkglEnable(GL_DEPTH_TEST);
}
vtkOpenGLCheckErrorMacro("Error after restoring GL state.");
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::StartTimeQuery(vtkOpenGLRenderTimer *timer)
{
// Since it may take a few frames for the results to become available,
// check if we've started the timer already.
if (!timer->Started())
{
timer->Start();
}
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::EndTimeQuery(vtkOpenGLRenderTimer *timer)
{
// Since it may take a few frames for the results to become available,
// check if we've stopped the timer already.
if (!timer->Stopped())
{
timer->Stop();
}
}
//------------------------------------------------------------------------------
void vtkOpenGLFXAAFilter::PrintBenchmark()
{
if (this->PreparationTimer->Ready() &&
this->FXAATimer->Ready())
{
#ifdef FXAA_BENCHMARK
int numPixels = this->Input->GetWidth() * this->Input->GetHeight();
float ptime = this->PreparationTimer->GetElapsedMilliseconds();
float ftime = this->FXAATimer->GetElapsedMilliseconds();
float ttime = ptime + ftime;
float ptimePerPixel = (this->PreparationTimer->GetElapsedNanoseconds() /
static_cast<float>(numPixels));
float ftimePerPixel = (this->FXAATimer->GetElapsedNanoseconds() /
static_cast<float>(numPixels));
float ttimePerPixel = ptimePerPixel + ftimePerPixel;
std::cerr << "FXAA Info:\n"
<< " - Number of pixels: " << numPixels << "\n"
<< " - Preparation time: " << ptime << "ms ("
<< ptimePerPixel << "ns per pixel)\n"
<< " - FXAA time: " << ftime << "ms ("
<< ftimePerPixel << "ns per pixel)\n"
<< " - Total time: " << ttime << "ms ("
<< ttimePerPixel << "ns per pixel)\n";
#endif // FXAA_BENCHMARK
this->PreparationTimer->Reset();
this->FXAATimer->Reset();
}
}