Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: src/core/SkShadowShader.cpp

Issue 2224163005: Made shadows blurry (thru implementing variance mapping) (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 8
9 #include "SkLights.h" 9 #include "SkLights.h"
10 #include "SkReadBuffer.h" 10 #include "SkReadBuffer.h"
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 242
243 SkString scaleOffsetVec("scaleOffsetVec"); 243 SkString scaleOffsetVec("scaleOffsetVec");
244 scaleOffsetVec.appendf("%d", i); 244 scaleOffsetVec.appendf("%d", i);
245 245
246 fragBuilder->codeAppendf("vec2 %s = vec2(%s) * povDepth.b * 255 / 400;\n", 246 fragBuilder->codeAppendf("vec2 %s = vec2(%s) * povDepth.b * 255 / 400;\n",
247 offset.c_str(), lightDirUniName[i]); 247 offset.c_str(), lightDirUniName[i]);
248 248
249 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n", 249 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s) );\n",
250 scaleVec.c_str(), 250 scaleVec.c_str(),
251 widthUniName, heightUniName, 251 widthUniName, heightUniName,
252 depthMapWidthUniName[i], depthMapHeight UniName[i]); 252 depthMapWidthUniName[i], depthMapHeight UniName[i]);
robertphillips 2016/08/10 14:23:18 ??
vjiaoblack 2016/08/11 18:28:51 Done.
253 253 //
254 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", 254 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n",
255 scaleOffsetVec.c_str(), scaleVec.c_str( )); 255 scaleOffsetVec.c_str(), scaleVec.c_str( ));
256 256
257
258 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " 257 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + "
259 "vec2(%s.x, 0 - %s.y)) " 258 "vec2(%s.x, 0 - %s.y)) "
260 " * %s + vec2(0,1) * %s;\n", 259 " * %s + vec2(0,1) * %s;\n",
261
262 povCoord.c_str(), offset.c_str(), offse t.c_str(), 260 povCoord.c_str(), offset.c_str(), offse t.c_str(),
263 scaleVec.c_str(), scaleOffsetVec.c_str( )); 261 scaleVec.c_str(), scaleOffsetVec.c_str( ));
264 262
265 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i], 263 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler s[i],
266 povCoord.c_str(), 264 povCoord.c_str(),
267 kVec2f_GrSLType); 265 kVec2f_GrSLType);
266
267
268 } 268 }
269 269
270 const char* ambientColorUniName = nullptr; 270 const char* ambientColorUniName = nullptr;
271 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag , 271 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag ,
272 kVec3f_GrSLType, kDefa ult_GrSLPrecision, 272 kVec3f_GrSLType, kDefa ult_GrSLPrecision,
273 "AmbientColor", &ambie ntColorUniName); 273 "AmbientColor", &ambie ntColorUniName);
274 274
275 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str()); 275 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol or.c_str());
276 276
robertphillips 2016/08/10 14:23:18 can we parameterize the shadow generation ?
vjiaoblack 2016/08/11 18:28:51 Done.
277 // Essentially, 277 // all the normal vectors point straight up
278 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N . L)))
279 SkString totalLightColor("totalLightColor"); 278 SkString totalLightColor("totalLightColor");
280 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str ()); 279 fragBuilder->codeAppendf("vec3 %s = vec3(0,0,0);", totalLightColor.c _str());
280
281 SkString lightProb("lightProbability");
282 fragBuilder->codeAppendf("float %s;", lightProb.c_str());
283 fragBuilder->codeAppendf("float variance;");
284 fragBuilder->codeAppendf("float d;");
281 285
282 for (int i = 0; i < numLights; i++) { 286 for (int i = 0; i < numLights; i++) {
283 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", 287 fragBuilder->codeAppendf("%s = 1;", lightProb.c_str());
288
289 // 1/512 is less than half a pixel; imperceptible
290 fragBuilder->codeAppendf("if (%s.b <= %s.b + 1/512) {",
284 povDepth.c_str(), depthMaps[i].c_str()) ; 291 povDepth.c_str(), depthMaps[i].c_str()) ;
285 // Note that dot(vec3(0,0,1), %s) == %s.z * %s 292
286 fragBuilder->codeAppendf("%s += %s.z * %s;", 293 fragBuilder->codeAppendf("vec2 moments = vec2(%s.b * 255, %s.g * 255 * 256 );",
294 depthMaps[i].c_str(), depthMaps[i].c_st r());
295
296 // variance biasing lessens light bleeding
297 fragBuilder->codeAppendf("variance = max(moments.y - (moments.x * moments.x),"
298 "1024);");
299
300 fragBuilder->codeAppendf("d = (%s.b * 255) - moments.x;", povDep th.c_str());
301 fragBuilder->codeAppendf("%s = (variance / (variance + d * d));" ,
302 lightProb.c_str());
303
304 // The curved shadows near plane edges are caused by using a non -optimal blurring
305 // function. Optimally, it'd be a linear blur.
306 fragBuilder->codeAppendf("}");
307
308 fragBuilder->codeAppendf("%s += dot(vec3(0,0,1), %s) * %s * %s;" ,
287 totalLightColor.c_str(), 309 totalLightColor.c_str(),
288 lightDirUniName[i], 310 lightDirUniName[i],
289 lightColorUniName[i]); 311 lightColorUniName[i],
290 fragBuilder->codeAppendf("}"); 312 lightProb.c_str());
291 } 313 }
292 314
293 fragBuilder->codeAppendf("%s += %s;", 315 fragBuilder->codeAppendf("%s += %s;", totalLightColor.c_str(), ambie ntColorUniName);
294 totalLightColor.c_str(),
295 ambientColorUniName);
296 316
297 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", 317 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);",
298 totalLightColor.c_str()); 318 totalLightColor.c_str());
299 319
300 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or); 320 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol or);
301 } 321 }
302 322
303 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, 323 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
304 GrProcessorKeyBuilder* b) { 324 GrProcessorKeyBuilder* b) {
305 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); 325 const ShadowFP& shadowFP = proc.cast<ShadowFP>();
(...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 737
718 /////////////////////////////////////////////////////////////////////////////// 738 ///////////////////////////////////////////////////////////////////////////////
719 739
720 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) 740 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader)
721 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) 741 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl)
722 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END 742 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
723 743
724 /////////////////////////////////////////////////////////////////////////////// 744 ///////////////////////////////////////////////////////////////////////////////
725 745
726 #endif 746 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698