Overte C++ Documentation
SurfaceGeometryPass.h
1 //
2 // SurfaceGeometryPass.h
3 // libraries/render-utils/src/
4 //
5 // Created by Sam Gateau 6/3/2016.
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_SurfaceGeometryPass_h
13 #define hifi_SurfaceGeometryPass_h
14 
15 #include <DependencyManager.h>
16 
17 #include "render/DrawTask.h"
18 #include "render/BlurTask.h"
19 #include "DeferredFrameTransform.h"
20 #include "DeferredFramebuffer.h"
21 
22 
23 // SurfaceGeometryFramebuffer is a helper class gathering in one place theframebuffers and targets describing the surface geometry linear depth
24 // from a z buffer
25 class LinearDepthFramebuffer {
26 public:
27  LinearDepthFramebuffer();
28 
29  gpu::FramebufferPointer getLinearDepthFramebuffer();
30  gpu::TexturePointer getLinearDepthTexture();
31  gpu::TexturePointer getNormalTexture();
32 
33  gpu::FramebufferPointer getDownsampleFramebuffer();
34  gpu::TexturePointer getHalfLinearDepthTexture();
35  gpu::TexturePointer getHalfNormalTexture();
36 
37  // Update the depth buffer which will drive the allocation of all the other resources according to its size.
38  void update(const gpu::TexturePointer& depthBuffer, const gpu::TexturePointer& normalTexture, bool isStereo);
39  const glm::ivec2& getDepthFrameSize() const { return _frameSize; }
40 
41  void setResolutionLevel(int level) { _resolutionLevel = std::max(0, level); }
42  int getResolutionLevel() const { return _resolutionLevel; }
43 
44 protected:
45  void clear();
46  void allocate();
47 
48  gpu::TexturePointer _primaryDepthTexture;
49 
50  gpu::FramebufferPointer _linearDepthFramebuffer;
51  gpu::TexturePointer _linearDepthTexture;
52  gpu::TexturePointer _normalTexture;
53 
54  gpu::FramebufferPointer _downsampleFramebuffer;
55  gpu::TexturePointer _halfLinearDepthTexture;
56  gpu::TexturePointer _halfNormalTexture;
57 
58 
59  glm::ivec2 _frameSize;
60  glm::ivec2 _halfFrameSize;
61  int _resolutionLevel{ 0 };
62  bool _isStereo{ false };
63 };
64 
65 using LinearDepthFramebufferPointer = std::shared_ptr<LinearDepthFramebuffer>;
66 
67 using LinearDepthPassConfig = render::GPUJobConfig;
68 
69 class LinearDepthPass {
70 public:
71  using Inputs = render::VaryingSet2<DeferredFrameTransformPointer, DeferredFramebufferPointer>;
72  using Outputs = render::VaryingSet5<LinearDepthFramebufferPointer, gpu::FramebufferPointer, gpu::TexturePointer, gpu::TexturePointer, gpu::TexturePointer>;
73  using Config = LinearDepthPassConfig;
74  using JobModel = render::Job::ModelIO<LinearDepthPass, Inputs, Outputs, Config>;
75 
76  LinearDepthPass() {}
77 
78  void configure(const Config& config);
79  void run(const render::RenderContextPointer& renderContext, const Inputs& inputs, Outputs& outputs);
80 
81 private:
82  LinearDepthFramebufferPointer _linearDepthFramebuffer;
83 
84  static const gpu::PipelinePointer& getLinearDepthPipeline();
85  static gpu::PipelinePointer _linearDepthPipeline;
86 
87  static const gpu::PipelinePointer& getDownsamplePipeline();
88  static gpu::PipelinePointer _downsamplePipeline;
89 
90  gpu::RangeTimerPointer _gpuTimer;
91 };
92 
93 
94 // SurfaceGeometryFramebuffer is a helper class gathering in one place theframebuffers and targets describing the surface geometry linear depth and curvature generated
95 // from a z buffer and a normal buffer
96 class SurfaceGeometryFramebuffer {
97 public:
98  SurfaceGeometryFramebuffer();
99 
100  gpu::FramebufferPointer getCurvatureFramebuffer();
101  gpu::TexturePointer getCurvatureTexture();
102 
103  gpu::FramebufferPointer getLowCurvatureFramebuffer();
104  gpu::TexturePointer getLowCurvatureTexture();
105 
106  gpu::FramebufferPointer getBlurringFramebuffer();
107  gpu::TexturePointer getBlurringTexture();
108 
109  // Update the source framebuffer size which will drive the allocation of all the other resources.
110  void update(const gpu::TexturePointer& linearDepthBuffer);
111  gpu::TexturePointer getLinearDepthTexture();
112  const glm::ivec2& getSourceFrameSize() const { return _frameSize; }
113 
114  void setResolutionLevel(int level);
115  int getResolutionLevel() const { return _resolutionLevel; }
116 
117 protected:
118  void clear();
119  void allocate();
120 
121  gpu::TexturePointer _linearDepthTexture;
122 
123  gpu::FramebufferPointer _curvatureFramebuffer;
124  gpu::TexturePointer _curvatureTexture;
125 
126  gpu::FramebufferPointer _blurringFramebuffer;
127  gpu::TexturePointer _blurringTexture;
128 
129  gpu::FramebufferPointer _lowCurvatureFramebuffer;
130  gpu::TexturePointer _lowCurvatureTexture;
131 
132  glm::ivec2 _frameSize;
133  int _resolutionLevel{ 0 };
134 };
135 
136 using SurfaceGeometryFramebufferPointer = std::shared_ptr<SurfaceGeometryFramebuffer>;
137 
138 class SurfaceGeometryPassConfig : public render::GPUJobConfig {
139  Q_OBJECT
140  Q_PROPERTY(float depthThreshold MEMBER depthThreshold NOTIFY dirty)
141  Q_PROPERTY(float basisScale MEMBER basisScale NOTIFY dirty)
142  Q_PROPERTY(float curvatureScale MEMBER curvatureScale NOTIFY dirty)
143  Q_PROPERTY(int resolutionLevel MEMBER resolutionLevel NOTIFY dirty)
144 
145  Q_PROPERTY(float diffuseFilterScale MEMBER diffuseFilterScale NOTIFY dirty)
146  Q_PROPERTY(float diffuseDepthThreshold MEMBER diffuseDepthThreshold NOTIFY dirty)
147 
148 public:
149  SurfaceGeometryPassConfig() : render::GPUJobConfig(true) {}
150 
151  float depthThreshold{ 5.0f }; // centimeters
152  float basisScale{ 1.0f };
153  float curvatureScale{ 10.0f };
154  int resolutionLevel{ 1 };
155  float diffuseFilterScale{ 0.2f };
156  float diffuseDepthThreshold{ 1.0f };
157 
158 signals:
159  void dirty();
160 };
161 
162 class SurfaceGeometryPass {
163 public:
164  using Inputs = render::VaryingSet3<DeferredFrameTransformPointer, DeferredFramebufferPointer, LinearDepthFramebufferPointer>;
165  using Outputs = render::VaryingSet4<SurfaceGeometryFramebufferPointer, gpu::FramebufferPointer, gpu::FramebufferPointer, gpu::FramebufferPointer>;
166  using Config = SurfaceGeometryPassConfig;
167  using JobModel = render::Job::ModelIO<SurfaceGeometryPass, Inputs, Outputs, Config>;
168 
169  SurfaceGeometryPass();
170 
171  void configure(const Config& config);
172  void run(const render::RenderContextPointer& renderContext, const Inputs& inputs, Outputs& outputs);
173 
174 
175  float getCurvatureDepthThreshold() const { return _parametersBuffer.get<Parameters>().curvatureInfo.x; }
176  float getCurvatureBasisScale() const { return _parametersBuffer.get<Parameters>().curvatureInfo.y; }
177  float getCurvatureScale() const { return _parametersBuffer.get<Parameters>().curvatureInfo.w; }
178  int getResolutionLevel() const { return (int)_parametersBuffer.get<Parameters>().resolutionInfo.w; }
179 
180 private:
181  typedef gpu::BufferView UniformBufferView;
182 
183  // Class describing the uniform buffer with all the parameters common to the AO shaders
184  class Parameters {
185  public:
186  // Resolution info
187  glm::vec4 resolutionInfo { 0.0f, 0.0f, 0.0f, 1.0f }; // Default Curvature & Diffusion is running half res
188  // Curvature algorithm
189  glm::vec4 curvatureInfo{ 0.0f };
190 
191  Parameters() {}
192  };
193  gpu::BufferView _parametersBuffer;
194 
195  SurfaceGeometryFramebufferPointer _surfaceGeometryFramebuffer;
196 
197  static const gpu::PipelinePointer& getCurvaturePipeline();
198 
199  static gpu::PipelinePointer _curvaturePipeline;
200 
201  render::BlurGaussianDepthAware _diffusePass;
202 
203  gpu::RangeTimerPointer _gpuTimer;
204 };
205 
206 #endif // hifi_SurfaceGeometryPass_h