Overte C++ Documentation
AntialiasingEffect.h
1 //
2 // AntialiasingEffect.h
3 // libraries/render-utils/src/
4 //
5 // Created by Raffi Bedikian on 8/30/15
6 // Copyright 2015 High Fidelity, Inc.
7 // Copyright 2022-2023 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 // SPDX-License-Identifier: Apache-2.0
12 //
13 
14 #ifndef hifi_AntialiasingEffect_h
15 #define hifi_AntialiasingEffect_h
16 
17 #include <DependencyManager.h>
18 
19 #include "render/DrawTask.h"
20 #include "DeferredFrameTransform.h"
21 #include "DeferredFramebuffer.h"
22 #include "SurfaceGeometryPass.h"
23 
24 class AntialiasingSetupConfig : public render::Job::Config {
25  Q_OBJECT
26  Q_PROPERTY(float scale MEMBER scale NOTIFY dirty)
27  Q_PROPERTY(bool freeze MEMBER freeze NOTIFY dirty)
28  Q_PROPERTY(bool stop MEMBER stop NOTIFY dirty)
29  Q_PROPERTY(int index READ getIndex NOTIFY dirty)
30  Q_PROPERTY(State state READ getState WRITE setState NOTIFY dirty)
31  Q_PROPERTY(Mode mode READ getAAMode WRITE setAAMode NOTIFY dirty)
32 
33 public:
34  AntialiasingSetupConfig() : render::Job::Config(true) {}
35 
36  /*@jsdoc
37  *Antialiasing modes. <table>
38  * <thead>
39  * <tr><th>Value</th><th>Name</th><th>Description</th>
40  * </thead>
41  * <tbody>
42  * <tr><td><code>0</code></td><td>NONE</td><td>Antialiasing is disabled.</td></tr>
43  * <tr><td><code>1</code></td><td>TAA</td><td>Temporal Antialiasing.</td></tr>
44  * <tr><td><code>2</code></td><td>FXAA</td><td>FXAA.</td></tr>
45  * <tr><td><code>3</code></td><td>MODE_COUNT</td><td>Indicates number of antialiasing modes</td></tr>
46  * </tbody>
47  * </table>
48  * @typedef {number} AntialiasingMode
49  */
50  enum class Mode {
51  NONE = 0,
52  TAA,
53  FXAA,
54  MODE_COUNT
55  };
56  Q_ENUM(Mode) // Stored as signed int.
57 
58  /*@jsdoc
59  *TAA Antialiasing state. <table>
60  * <thead>
61  * <tr><th>Value</th><th>Name</th><th>Description</th>
62  * </thead>
63  * <tbody>
64  * <tr><td><code>0</code></td><td>NONE</td><td>TAA is disabled.</td></tr>
65  * <tr><td><code>1</code></td><td>PAUSE</td><td>TAA jitter is paused.</td></tr>
66  * <tr><td><code>2</code></td><td>PLAY</td><td>TAA jitter is playing.</td></tr>
67  * <tr><td><code>3</code></td><td>STATE_COUNT</td><td>Indicates number of antialiasing states</td></tr>
68  * </tbody>
69  * </table>
70  * @typedef {number} AntialiasingState
71  */
72  enum class State
73  {
74  NONE = 0,
75  PAUSE,
76  PLAY,
77  STATE_COUNT
78  };
79  Q_ENUM(State)
80 
81  float scale { 0.75f };
82  bool stop { false };
83  bool freeze { false };
84  Mode mode { Mode::TAA };
85 
86 public slots:
87  int prev();
88  int next();
89  State none();
90  State pause();
91  State play();
92 
93  int getIndex() const { return _index; }
94  void setIndex(int current);
95 
96  State getState() const { return _state; }
97  void setState(State state);
98 
99  Mode getAAMode() const { return mode; }
100  void setAAMode(Mode mode);
101 
102 signals:
103  void dirty();
104 
105 private:
106  State _state { State::PLAY };
107  int _index { 0 };
108 
109 };
110 
111 class AntialiasingSetup {
112 public:
113 
114  using Config = AntialiasingSetupConfig;
115  using Output = AntialiasingSetupConfig::Mode;
116  using JobModel = render::Job::ModelO<AntialiasingSetup, Output, Config>;
117 
118  AntialiasingSetup();
119 
120  void configure(const Config& config);
121  void run(const render::RenderContextPointer& renderContext, Output& output);
122 
123 private:
124 
125  std::vector<glm::vec2> _sampleSequence;
126  float _scale { 1.0f };
127  int _freezedSampleIndex { 0 };
128  bool _isStopped { false };
129  bool _isFrozen { false };
130  AntialiasingSetupConfig::Mode _mode{ AntialiasingSetupConfig::Mode::TAA };
131 };
132 
133 
134 class AntialiasingConfig : public render::Job::Config {
135  Q_OBJECT
136  Q_PROPERTY(float blend MEMBER blend NOTIFY dirty)
137  Q_PROPERTY(float sharpen MEMBER sharpen NOTIFY dirty)
138  Q_PROPERTY(float covarianceGamma MEMBER covarianceGamma NOTIFY dirty)
139 
140  Q_PROPERTY(bool constrainColor MEMBER constrainColor NOTIFY dirty)
141  Q_PROPERTY(bool feedbackColor MEMBER feedbackColor NOTIFY dirty)
142  Q_PROPERTY(bool bicubicHistoryFetch MEMBER bicubicHistoryFetch NOTIFY dirty)
143 
144  Q_PROPERTY(bool debug MEMBER debug NOTIFY dirty)
145  Q_PROPERTY(float debugX MEMBER debugX NOTIFY dirty)
146  Q_PROPERTY(bool fxaaOnOff READ debugFXAA WRITE setDebugFXAA NOTIFY dirty)
147  Q_PROPERTY(float debugShowVelocityThreshold MEMBER debugShowVelocityThreshold NOTIFY dirty)
148  Q_PROPERTY(bool showCursorPixel MEMBER showCursorPixel NOTIFY dirty)
149  Q_PROPERTY(glm::vec2 debugCursorTexcoord MEMBER debugCursorTexcoord NOTIFY dirty)
150  Q_PROPERTY(float debugOrbZoom MEMBER debugOrbZoom NOTIFY dirty)
151 
152  Q_PROPERTY(bool showClosestFragment MEMBER showClosestFragment NOTIFY dirty)
153 
154 public:
155  AntialiasingConfig() : render::Job::Config(true) {}
156 
157  void setDebugFXAA(bool debug) { debugFXAAX = (debug ? 0.0f : 1.0f); emit dirty();}
158  bool debugFXAA() const { return (debugFXAAX == 0.0f ? true : false); }
159 
160  float blend { 0.2f };
161  float sharpen { 0.05f };
162 
163  bool constrainColor { true };
164  float covarianceGamma { 1.15f };
165  bool feedbackColor { false };
166  bool bicubicHistoryFetch { true };
167 
168  float debugX { 0.0f };
169  float debugFXAAX { 1.0f };
170  float debugShowVelocityThreshold { 1.0f };
171  glm::vec2 debugCursorTexcoord { 0.5f, 0.5f };
172  float debugOrbZoom { 2.0f };
173 
174  bool debug { false };
175  bool showCursorPixel { false };
176  bool showClosestFragment { false };
177 
178 signals:
179  void dirty();
180 };
181 
182 #define SET_BIT(bitfield, bitIndex, value) bitfield = ((bitfield) & ~(1 << (bitIndex))) | ((value) << (bitIndex))
183 #define GET_BIT(bitfield, bitIndex) ((bitfield) & (1 << (bitIndex)))
184 
185 struct TAAParams {
186  float nope { 0.0f };
187  float blend { 0.15f };
188  float covarianceGamma { 0.9f };
189  float debugShowVelocityThreshold { 1.0f };
190 
191  glm::ivec4 flags { 0 };
192  glm::vec4 pixelInfo { 0.5f, 0.5f, 2.0f, 0.0f };
193  glm::vec4 regionInfo { 0.0f, 0.0f, 1.0f, 0.0f };
194 
195  void setConstrainColor(bool enabled) { SET_BIT(flags.y, 1, enabled); }
196  bool isConstrainColor() const { return (bool)GET_BIT(flags.y, 1); }
197 
198  void setFeedbackColor(bool enabled) { SET_BIT(flags.y, 4, enabled); }
199  bool isFeedbackColor() const { return (bool)GET_BIT(flags.y, 4); }
200 
201  void setBicubicHistoryFetch(bool enabled) { SET_BIT(flags.y, 0, enabled); }
202  bool isBicubicHistoryFetch() const { return (bool)GET_BIT(flags.y, 0); }
203 
204  void setSharpenedOutput(bool enabled) { SET_BIT(flags.y, 2, enabled); }
205  bool isSharpenedOutput() const { return (bool)GET_BIT(flags.y, 2); }
206 
207  void setDebug(bool enabled) { SET_BIT(flags.x, 0, enabled); }
208  bool isDebug() const { return (bool) GET_BIT(flags.x, 0); }
209 
210  void setShowDebugCursor(bool enabled) { SET_BIT(flags.x, 1, enabled); }
211  bool showDebugCursor() const { return (bool)GET_BIT(flags.x, 1); }
212 
213  void setDebugCursor(glm::vec2 debugCursor) { pixelInfo.x = debugCursor.x; pixelInfo.y = debugCursor.y; }
214  glm::vec2 getDebugCursor() const { return glm::vec2(pixelInfo.x, pixelInfo.y); }
215 
216  void setDebugOrbZoom(float orbZoom) { pixelInfo.z = orbZoom; }
217  float getDebugOrbZoom() const { return pixelInfo.z; }
218 
219  void setShowClosestFragment(bool enabled) { SET_BIT(flags.x, 3, enabled); }
220 
221  bool isFXAAEnabled() const { return regionInfo.z == 0.0f; }
222 };
223 using TAAParamsBuffer = gpu::StructBuffer<TAAParams>;
224 
225 class Antialiasing {
226 public:
227  using Inputs = render::VaryingSet4<DeferredFrameTransformPointer, DeferredFramebufferPointer, LinearDepthFramebufferPointer, AntialiasingSetupConfig::Mode>;
228  using Outputs = gpu::TexturePointer;
229  using Config = AntialiasingConfig;
230  using JobModel = render::Job::ModelIO<Antialiasing, Inputs, Outputs, Config>;
231 
232  Antialiasing(bool isSharpenEnabled = true);
233  ~Antialiasing();
234  void configure(const Config& config);
235  void run(const render::RenderContextPointer& renderContext, const Inputs& inputs, Outputs& outputs);
236 
237  static const gpu::PipelinePointer& getAntialiasingPipeline();
238  static const gpu::PipelinePointer& getIntensityPipeline();
239  static const gpu::PipelinePointer& getBlendPipeline();
240  static const gpu::PipelinePointer& getDebugBlendPipeline();
241 
242 private:
243  struct AntialiasingBuffer {
244  gpu::FramebufferSwapChainPointer _swapChain;
245  gpu::TexturePointer _textures[2];
246 
247  void clear() {
248  _swapChain.reset();
249  _textures[0].reset();
250  _textures[1].reset();
251  }
252  };
253  AntialiasingBuffer _antialiasingBuffers;
254  gpu::FramebufferPointer _intensityFramebuffer;
255  gpu::TexturePointer _intensityTexture;
256  gpu::BufferPointer _blendParamsBuffer;
257 
258  static gpu::PipelinePointer _antialiasingPipeline;
259  static gpu::PipelinePointer _intensityPipeline;
260  static gpu::PipelinePointer _blendPipeline;
261  static gpu::PipelinePointer _debugBlendPipeline;
262 
263  TAAParamsBuffer _params;
264  float _sharpen { 0.15f };
265  bool _isSharpenEnabled { true };
266  float _debugFXAAX { 0.0f };
267 };
268 
269 #endif // hifi_AntialiasingEffect_h