KiCad PCB EDA Suite
cpostshader_ssao.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include "cpostshader_ssao.h"
31 #include "../3d_fastmath.h"
32 
33 
35  CPOSTSHADER( aCamera ),
36  m_shadedBuffer( nullptr ),
37  m_isUsingShadows( false )
38 {
39 }
40 
41 // There are differente sources for this shader on the web
42 //https://github.com/scanberg/hbao/blob/master/resources/shaders/ssao_frag.glsl
43 
44 //http://www.gamedev.net/topic/556187-the-best-ssao-ive-seen/
45 //http://www.gamedev.net/topic/556187-the-best-ssao-ive-seen/?view=findpost&p=4632208
46 
47 float CPOSTSHADER_SSAO::aoFF( const SFVEC2I &aShaderPos,
48  const SFVEC3F &ddiff,
49  const SFVEC3F &cnorm,
50  const float aShadowAtSamplePos,
51  const float aShadowAtCenterPos,
52  int c1,
53  int c2 ) const
54 {
55  const float shadowGain = 0.60f;
56  const float aoGain = 1.0f;
57 
58  const float shadow_factor_at_sample = ( 1.0f - aShadowAtSamplePos ) * shadowGain;
59  const float shadow_factor_at_center = ( 1.0f - aShadowAtCenterPos ) * shadowGain;
60 
61  float return_value = shadow_factor_at_center;
62 
63  const float rd = glm::length( ddiff );
64 
65  // This limits the zero of the function (see below)
66  if( rd < 2.0f )
67  {
68  if( rd > FLT_EPSILON )
69  {
70  const SFVEC3F vv = glm::normalize( ddiff );
71 
72  // Calculate an attenuation distance factor, this was get the best
73  // results by experimentation
74  // Changing this factor will change how much shadow in relation to the
75  // distance of the hit it will be in shadow
76 
77  // http://www.fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIwLjgteCowLjYiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjAsImVxIjoiMS8oeCp4KjAuNSsxKSIsImNvbG9yIjoiIzAwMDAwMCJ9LHsidHlwZSI6MTAwMCwid2luZG93IjpbIi0yLjU5Mjk0NTkyNTA5ODA0MSIsIjQuNTUzODc5NjU1NDQ1OTIzIiwiLTEuNzY3MDMwOTAzMjgxNjgxOCIsIjIuNjMxMDE1NjA3ODIyMjk3Il0sInNpemUiOls2NDksMzk5XX1d
78  const float attDistFactor = 1.0f / ( rd * rd * 8.0f + 1.0f );
79 
80  const SFVEC2I vr = aShaderPos + SFVEC2I( c1, c2 );
81 
82  float sampledNormalFactor = glm::max( glm::dot( GetNormalAt( vr ), cnorm ), 0.0f );
83 
84  sampledNormalFactor = glm::max( 1.0f - sampledNormalFactor * sampledNormalFactor, 0.0f );
85 
86  const float shadowAttDistFactor = glm::max( glm::min( rd * 5.0f - 0.25f, 1.0f ), 0.0f );
87 
88  float shadowAttFactor = glm::min( sampledNormalFactor + shadowAttDistFactor, 1.0f );
89 
90  const float shadowFactor = glm::mix( shadow_factor_at_sample,
91  shadow_factor_at_center,
92  shadowAttFactor );
93 
94  // This is a dot product threshold factor.
95  // it defines after wich angle we consider that the point starts to occlude.
96  // if the value is high, it will discard low angles point
97  const float aDotThreshold = 0.15f;
98 
99  // This is the dot product between the center pixel normal (the one that is being shaded)
100  // and the vector from the center to the sampled point
101  const float localNormalFactor = glm::dot( cnorm, vv );
102 
103  const float localNormalFactorWithThreshold = (glm::max( localNormalFactor, aDotThreshold ) - aDotThreshold) /
104  (1.0f - aDotThreshold);
105 
106  const float aoFactor = localNormalFactorWithThreshold * aoGain * attDistFactor;
107 
108  return_value = glm::min( aoFactor + shadowFactor, 1.0f );
109  }
110  }
111 
112  return return_value;
113 }
114 
115 
116 float CPOSTSHADER_SSAO::giFF( const SFVEC2I &aShaderPos,
117  const SFVEC3F &ddiff,
118  const SFVEC3F &cnorm,
119  const float aShadow,
120  int c1,
121  int c2 ) const
122 {
123  if( (ddiff.x > FLT_EPSILON) ||
124  (ddiff.y > FLT_EPSILON) ||
125  (ddiff.z > FLT_EPSILON) )
126  {
127  const SFVEC3F vv = glm::normalize( ddiff );
128  const float rd = glm::length( ddiff );
129  const SFVEC2I vr = aShaderPos + SFVEC2I( c1, c2 );
130 
131  const float attDistFactor = 1.0f / ( rd * rd + 1.0f );
132 
133  return ( glm::clamp( glm::dot( GetNormalAt( vr ), -vv), 0.0f, 1.0f ) *
134  glm::clamp( glm::dot( cnorm, vv ), 0.0f, 1.0f ) * attDistFactor ) * ( 0.03f + aShadow ) * 3.0f;
135  }
136 
137  return 0.0f;
138 }
139 
140 
141 SFVEC3F CPOSTSHADER_SSAO::Shade( const SFVEC2I &aShaderPos ) const
142 {
143  float cdepth = GetDepthAt( aShaderPos );
144 
145  if( cdepth > FLT_EPSILON )
146  {
147  cdepth = ( 30.0f / ( cdepth * 2.0f + 1.0f ) );
148 
149  // read current normal,position and color.
150  const SFVEC3F n = GetNormalAt( aShaderPos );
151  const SFVEC3F p = GetPositionAt( aShaderPos );
152 
153  const float shadowAt0 = GetShadowFactorAt( aShaderPos );
154 
155  // initialize variables:
156  float ao = 0.0f;
157  SFVEC3F gi = SFVEC3F(0.0f);
158 
159 #define ROUNDS 3
160  for( unsigned int i = 0; i < ROUNDS; ++i )
161  {
162  static const int limit[ROUNDS] = { 0x01, 0x03, 0x03 };
163 
164  const int pw = Fast_rand() & limit[i];
165  const int ph = Fast_rand() & limit[i];
166 
167  const int npw = (int) ( ( pw + i ) * cdepth ) + ( i + 1 );
168  const int nph = (int) ( ( ph + i ) * cdepth ) + ( i + 1 );
169 
170  const SFVEC3F ddiff = GetPositionAt( aShaderPos + SFVEC2I( npw, nph ) ) - p;
171  const SFVEC3F ddiff2 = GetPositionAt( aShaderPos + SFVEC2I( npw, -nph ) ) - p;
172  const SFVEC3F ddiff3 = GetPositionAt( aShaderPos + SFVEC2I( -npw, nph ) ) - p;
173  const SFVEC3F ddiff4 = GetPositionAt( aShaderPos + SFVEC2I( -npw, -nph ) ) - p;
174  const SFVEC3F ddiff5 = GetPositionAt( aShaderPos + SFVEC2I( pw, nph ) ) - p;
175  const SFVEC3F ddiff6 = GetPositionAt( aShaderPos + SFVEC2I( pw, -nph ) ) - p;
176  const SFVEC3F ddiff7 = GetPositionAt( aShaderPos + SFVEC2I( npw, ph ) ) - p;
177  const SFVEC3F ddiff8 = GetPositionAt( aShaderPos + SFVEC2I(-npw, ph ) ) - p;
178 
179  const float shadowAt1 = GetShadowFactorAt( aShaderPos + SFVEC2I( +npw, nph ) );
180  const float shadowAt2 = GetShadowFactorAt( aShaderPos + SFVEC2I( +npw, -nph ) );
181  const float shadowAt3 = GetShadowFactorAt( aShaderPos + SFVEC2I( -npw, nph ) );
182  const float shadowAt4 = GetShadowFactorAt( aShaderPos + SFVEC2I( -npw, -nph ) );
183  const float shadowAt5 = GetShadowFactorAt( aShaderPos + SFVEC2I( +pw, nph ) );
184  const float shadowAt6 = GetShadowFactorAt( aShaderPos + SFVEC2I( pw, -nph ) );
185  const float shadowAt7 = GetShadowFactorAt( aShaderPos + SFVEC2I( npw, ph ) );
186  const float shadowAt8 = GetShadowFactorAt( aShaderPos + SFVEC2I( -npw, ph ) );
187 
188  ao += aoFF( aShaderPos, ddiff , n, shadowAt1, shadowAt0, npw, nph );
189  ao += aoFF( aShaderPos, ddiff2, n, shadowAt2, shadowAt0, npw, -nph );
190  ao += aoFF( aShaderPos, ddiff3, n, shadowAt3, shadowAt0, -npw, nph );
191  ao += aoFF( aShaderPos, ddiff4, n, shadowAt4, shadowAt0, -npw, -nph );
192  ao += aoFF( aShaderPos, ddiff5, n, shadowAt5, shadowAt0, pw, nph );
193  ao += aoFF( aShaderPos, ddiff6, n, shadowAt6, shadowAt0, pw, -nph );
194  ao += aoFF( aShaderPos, ddiff7, n, shadowAt7, shadowAt0, npw, ph );
195  ao += aoFF( aShaderPos, ddiff8, n, shadowAt8, shadowAt0, -npw, ph );
196 
197  gi += giFF( aShaderPos, ddiff , n, shadowAt1, npw, nph) *
198  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( npw, nph ) ) );
199  gi += giFF( aShaderPos, ddiff2, n, shadowAt2, npw, -nph) *
200  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( npw,-nph ) ) );
201  gi += giFF( aShaderPos, ddiff3, n, shadowAt3, -npw, nph) *
202  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( -npw, nph ) ) );
203  gi += giFF( aShaderPos, ddiff4, n, shadowAt4, -npw, -nph) *
204  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( -npw,-nph ) ) );
205  gi += giFF( aShaderPos, ddiff5, n, shadowAt5 , pw, nph) *
206  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( pw, nph ) ) );
207  gi += giFF( aShaderPos, ddiff6, n, shadowAt6, pw,-nph) *
208  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( pw,-nph ) ) );
209  gi += giFF( aShaderPos, ddiff7, n, shadowAt7, npw, ph) *
210  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( npw, ph ) ) );
211  gi += giFF( aShaderPos, ddiff8, n, shadowAt8, -npw, ph) *
212  giColorCurve( GetColorAt( aShaderPos + SFVEC2I( -npw, ph ) ) );
213  }
214 
215  // If it received direct light, it shouldn't consider much AO
216  // shadowAt0 1.0 when no shadow
217  const float reduceAOwhenNoShadow = m_isUsingShadows ? ( 1.0f - shadowAt0 * 0.3f ) : 1.0f;
218 
219  ao = reduceAOwhenNoShadow * ( ao / ( ROUNDS * 8.0f ) );
220 
221  ao = ( 1.0f - 1.0f / ( ao * ao * 5.0f + 1.0f ) ) * 1.2f;
222 
223  gi = ( gi / ( ROUNDS * 8.0f ) );
224 
225  float giL = glm::min( glm::length( gi ) * 4.0f, 1.0f );
226 
227  giL = ( 1.0f - 1.0f / ( giL * 4.0f + 1.0f ) ) * 1.5f;
228 
229  return glm::mix( SFVEC3F( ao ), -gi, giL );
230  }
231  else
232  return SFVEC3F(0.0f);
233 }
234 
235 
236 SFVEC3F CPOSTSHADER_SSAO::ApplyShadeColor( const SFVEC2I &aShaderPos, const SFVEC3F &aInputColor, const SFVEC3F &aShadeColor ) const
237 {
238  SFVEC3F outColor;
239 
240  const SFVEC3F subtracted = aInputColor - aShadeColor;
241  const SFVEC3F mixed = glm::mix( aInputColor,
242  aInputColor * 0.50f - aShadeColor * 0.05f,
243  glm::min( aShadeColor, 1.0f ) );
244 
245  outColor.r = ( aShadeColor.r < 0.0f ) ? subtracted.r : mixed.r;
246  outColor.g = ( aShadeColor.g < 0.0f ) ? subtracted.g : mixed.g;
247  outColor.b = ( aShadeColor.b < 0.0f ) ? subtracted.b : mixed.b;
248 
249  return outColor;
250 }
251 
252 
254 {
255  const SFVEC3F vec1 = SFVEC3F(1.0f);
256 
257  // This option actually apply a gama since we are using linear color space
258  // and the result shader will be applied after convert back to sRGB
259 
260  // http://fooplot.com/#W3sidHlwZSI6MCwiZXEiOiIxLjAtKDEuMC8oeCo5LjArMS4wKSkreCowLjEiLCJjb2xvciI6IiMwMDAwMDAifSx7InR5cGUiOjEwMDAsIndpbmRvdyI6WyItMC4wNjIxODQ2MTUzODQ2MTU1MDUiLCIxLjE0Mjk4NDYxNTM4NDYxNDYiLCItMC4xMjcwOTk5OTk5OTk5OTk3NyIsIjEuMTMyNiJdfV0-
261  return vec1 - ( vec1 / (aColor * SFVEC3F(9.0f) + vec1) ) + aColor * SFVEC3F(0.10f);
262 }
263 
264 SFVEC3F CPOSTSHADER_SSAO::Blur( const SFVEC2I& aShaderPos ) const
265 {
266  const float dCenter = GetDepthAt( aShaderPos );
267 
268  SFVEC3F shadedOut = SFVEC3F( 0.0f );
269 
270  float totalWeight = 1.0f;
271 
272  for( int y = -3; y < 3; y++ )
273  {
274  for( int x = -3; x < 3; x++ )
275  {
276 
277  const unsigned int idx = GetIndex( SFVEC2I( aShaderPos.x + x, aShaderPos.y + y ) );
278 
279  const SFVEC3F s = m_shadedBuffer[idx];
280 
281  if( !( ( x == 0 ) && ( y == 0 ) ) )
282  {
283 
284  const float d = GetDepthAt( SFVEC2I( aShaderPos.x + x, aShaderPos.y + y ) );
285 
286  const float depthAtt = ( dCenter - d ) * dCenter
287  * 25.0f; // increse the value will get more sharpness effect
288 
289  const float depthAttSqr = depthAtt * depthAtt;
290 
291  float weight = ( 1.0f / ( depthAttSqr + 1.0f ) ) - 0.02f * depthAttSqr;
292 
293  weight = glm::max( weight, 0.0f );
294 
295  shadedOut += s * weight;
296  totalWeight += weight;
297  }
298  else
299  {
300  shadedOut += s;
301  }
302  }
303  }
304 
305  return shadedOut / totalWeight;
306 }
SFVEC3F ApplyShadeColor(const SFVEC2I &aShaderPos, const SFVEC3F &aInputColor, const SFVEC3F &aShadeColor) const override
ApplyShadeColor - apply the final color process using a previous stage color.
SFVEC3F Shade(const SFVEC2I &aShaderPos) const override
Class CCAMERA is a virtual class used to derive CCAMERA objects from.
Definition: ccamera.h:79
Implements a post shader screen space ambient occlusion on software.
int Fast_rand(void)
Definition: 3d_fastmath.cpp:58
glm::ivec2 SFVEC2I
Definition: xv3d_types.h:42
unsigned int GetIndex(const SFVEC2F &aPos) const
Definition: cpostshader.h:87
float GetDepthAt(const SFVEC2F &aPos) const
#define ROUNDS
const float & GetShadowFactorAt(const SFVEC2I &aPos) const
SFVEC3F * m_shadedBuffer
const SFVEC3F & GetColorAt(const SFVEC2F &aPos) const
SFVEC3F Blur(const SFVEC2I &aShaderPos) const
CPOSTSHADER_SSAO(const CCAMERA &aCamera)
SFVEC3F giColorCurve(const SFVEC3F &aColor) const
giColorCurve - Apply a curve transformation to the original color it will atenuate the bright colors ...
float giFF(const SFVEC2I &aShaderPos, const SFVEC3F &ddiff, const SFVEC3F &cnorm, const float aShadow, int c1, int c2) const
glm::vec3 SFVEC3F
Definition: xv3d_types.h:47
const SFVEC3F & GetPositionAt(const SFVEC2F &aPos) const
float aoFF(const SFVEC2I &aShaderPos, const SFVEC3F &ddiff, const SFVEC3F &cnorm, const float aShadowAtSamplePos, const float aShadowAtCenterPos, int c1, int c2) const
const SFVEC3F & GetNormalAt(const SFVEC2F &aPos) const