Overte C++ Documentation
BlurTask.h
1 //
2 // BlurTask.h
3 // render/src/render
4 //
5 // Created by Sam Gateau on 6/7/16.
6 // Copyright 2016 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_render_BlurTask_h
13 #define hifi_render_BlurTask_h
14 
15 #include "Engine.h"
16 
17 #include "BlurTask_shared.slh"
18 
19 namespace render {
20 
21 
22 class BlurParams {
23 public:
24 
25  void setWidthHeight(int width, int height, bool isStereo);
26 
27  void setTexcoordTransform(const glm::vec4 texcoordTransformViewport);
28 
29  void setFilterRadiusScale(float scale);
30  void setFilterNumTaps(int count);
31  // Tap 0 is considered the center of the kernel
32  void setFilterTap(int index, float offset, float value);
33  void setFilterGaussianTaps(int numHalfTaps, float sigma = 1.47f);
34  void setOutputAlpha(float value);
35 
36  void setDepthPerspective(float oneOverTan2FOV);
37  void setDepthThreshold(float threshold);
38 
39  void setLinearDepthPosFar(float farPosDepth);
40 
41  // Class describing the uniform buffer with all the parameters common to the blur shaders
42  class Params {
43  public:
44  // Resolution info (width, height, inverse of width, inverse of height)
45  glm::vec4 resolutionInfo{ 0.0f, 0.0f, 0.0f, 0.0f };
46 
47  // Viewport to Texcoord info, if the region of the blur (viewport) is smaller than the full frame
48  glm::vec4 texcoordTransform{ 0.0f, 0.0f, 1.0f, 1.0f };
49 
50  // Filter info (radius scale, number of taps, output alpha)
51  glm::vec4 filterInfo{ 1.0f, 0.0f, 0.0f, 0.0f };
52 
53  // Depth info (radius scale
54  glm::vec4 depthInfo{ 1.0f, 0.0f, 0.0f, 0.0f };
55 
56  // stereo info if blurring a stereo render
57  glm::vec4 stereoInfo{ 0.0f };
58 
59  // LinearDepth info is { f }
60  glm::vec4 linearDepthInfo{ 0.0f };
61 
62  // Taps (offset, weight)
63  glm::vec2 filterTaps[BLUR_MAX_NUM_TAPS];
64 
65  Params() {}
66  };
67  gpu::BufferView _parametersBuffer;
68 
69  BlurParams();
70 };
71 using BlurParamsPointer = std::shared_ptr<BlurParams>;
72 
73 class BlurInOutResource {
74 public:
75  BlurInOutResource() {}
76  BlurInOutResource(bool generateOutputFramebuffer, unsigned int downsampleFactor);
77 
78  struct Resources {
79  gpu::TexturePointer sourceTexture;
80  gpu::FramebufferPointer blurringFramebuffer;
81  gpu::TexturePointer blurringTexture;
82  gpu::FramebufferPointer finalFramebuffer;
83  };
84 
85  bool updateResources(const gpu::FramebufferPointer& sourceFramebuffer, Resources& resources);
86 
87  gpu::FramebufferPointer _blurredFramebuffer;
88 
89  // the output framebuffer defined if the job needs to output the result in a new framebuffer and not in place in the input buffer
90  gpu::FramebufferPointer _outputFramebuffer;
91  unsigned int _downsampleFactor{ 1U };
92  bool _generateOutputFramebuffer{ false };
93 };
94 
95 
96 class BlurGaussianConfig : public Job::Config {
97  Q_OBJECT
98  Q_PROPERTY(bool enabled WRITE setEnabled READ isEnabled NOTIFY dirty) // expose enabled flag
99  Q_PROPERTY(float filterScale MEMBER filterScale NOTIFY dirty)
100  Q_PROPERTY(float mix MEMBER mix NOTIFY dirty)
101 public:
102 
103  BlurGaussianConfig() : Job::Config(true) {}
104 
105  float filterScale{ 0.2f };
106  float mix{ 1.0f };
107 
108 signals :
109  void dirty();
110 
111 protected:
112 };
113 
114 
115 class BlurGaussian {
116 public:
117  using Inputs = VaryingSet5<gpu::FramebufferPointer, bool, unsigned int, int, float>;
118  using Config = BlurGaussianConfig;
119  using JobModel = Job::ModelIO<BlurGaussian, Inputs, gpu::FramebufferPointer, Config>;
120 
121  BlurGaussian();
122 
123  void configure(const Config& config);
124  void run(const RenderContextPointer& renderContext, const Inputs& inputs, gpu::FramebufferPointer& blurredFramebuffer);
125 
126  BlurParamsPointer getParameters() const { return _parameters; }
127 
128 protected:
129 
130  BlurParamsPointer _parameters;
131 
132  gpu::PipelinePointer _blurVPipeline;
133  gpu::PipelinePointer _blurHPipeline;
134 
135  gpu::PipelinePointer getBlurVPipeline();
136  gpu::PipelinePointer getBlurHPipeline();
137 
138  BlurInOutResource _inOutResources;
139 };
140 
141 class BlurGaussianDepthAwareConfig : public BlurGaussianConfig {
142  Q_OBJECT
143  Q_PROPERTY(float depthThreshold MEMBER depthThreshold NOTIFY dirty) // expose enabled flag
144 public:
145  BlurGaussianDepthAwareConfig() : BlurGaussianConfig() {}
146 
147  float depthThreshold{ 1.0f };
148 signals:
149  void dirty();
150 protected:
151 };
152 
153 class BlurGaussianDepthAware {
154 public:
155  using Inputs = VaryingSet2<gpu::FramebufferPointer, gpu::TexturePointer>;
156  using Config = BlurGaussianDepthAwareConfig;
157  using JobModel = Job::ModelIO<BlurGaussianDepthAware, Inputs, gpu::FramebufferPointer, Config>;
158 
159  BlurGaussianDepthAware(bool generateNewOutput = false, const BlurParamsPointer& params = BlurParamsPointer());
160 
161  void configure(const Config& config);
162  void run(const RenderContextPointer& renderContext, const Inputs& SourceAndDepth, gpu::FramebufferPointer& blurredFramebuffer);
163 
164  const BlurParamsPointer& getParameters() const { return _parameters; }
165 
166  gpu::PipelinePointer getBlurVPipeline();
167  gpu::PipelinePointer getBlurHPipeline();
168 
169 protected:
170  gpu::PipelinePointer _blurVPipeline;
171  gpu::PipelinePointer _blurHPipeline;
172 
173  BlurInOutResource _inOutResources;
174  BlurParamsPointer _parameters;
175 };
176 
177 }
178 
179 #endif // hifi_render_BlurTask_h