From e6f5d5623160a69e1585f5121a3695092327dfe0 Mon Sep 17 00:00:00 2001 From: vjiaoblack Date: Thu, 25 Aug 2016 06:30:23 -0700 Subject: Made shadows blurry (thru implementing variance mapping) BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2224163005 Review-Url: https://codereview.chromium.org/2224163005 --- include/core/SkCanvas.h | 31 +++++++++++++++++++++----- include/private/SkRecords.h | 3 ++- include/private/SkShadowParams.h | 48 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 include/private/SkShadowParams.h (limited to 'include') diff --git a/include/core/SkCanvas.h b/include/core/SkCanvas.h index 1e0301179a..823c30fd7a 100644 --- a/include/core/SkCanvas.h +++ b/include/core/SkCanvas.h @@ -18,6 +18,7 @@ #include "SkSurfaceProps.h" #include "SkXfermode.h" #include "SkLights.h" +#include "../private/SkShadowParams.h" class GrContext; class GrDrawContext; @@ -1073,7 +1074,7 @@ public: #ifdef SK_EXPERIMENTAL_SHADOWING /** - * Draw the picture into this canvas. + * Draw the picture into this canvas, with shadows! * * We will use the canvas's lights along with the picture information (draw depths of * objects, etc) to first create a set of shadowmaps for the light-picture pairs, and @@ -1088,14 +1089,33 @@ public: * This is logically equivalent to * saveLayer(paint)/drawPicture/restore * + * We also support using variance shadow maps for blurred shadows; the user can specify + * what shadow mapping algorithm to use with params. + * - Variance Shadow Mapping works by storing both the depth and depth^2 in the shadow map. + * - Then, the shadow map can be blurred, and when reading from it, the fragment shader + * can calculate the variance of the depth at a position by doing E(x^2) - E(x)^2. + * - We can then use the depth variance and depth at a fragment to arrive at an upper bound + * of the probability that the current surface is shadowed by using Chebyshev's + * inequality, and then use that to shade the fragment. + * + * - There are a few problems with VSM. + * * Light Bleeding | Areas with high variance, such as near the edges of high up rects, + * will cause their shadow penumbras to overwrite otherwise solid + * shadows. + * * Shape Distortion | We can combat Light Bleeding by biasing the shadow (setting + * mostly shaded fragments to completely shaded) and increasing + * the minimum allowed variance. However, this warps and rounds + * out the shape of the shadow. */ void drawShadowedPicture(const SkPicture*, const SkMatrix* matrix, - const SkPaint* paint); + const SkPaint* paint, + const SkShadowParams& params); void drawShadowedPicture(const sk_sp& picture, const SkMatrix* matrix, - const SkPaint* paint) { - this->drawShadowedPicture(picture.get(), matrix, paint); + const SkPaint* paint, + const SkShadowParams& params) { + this->drawShadowedPicture(picture.get(), matrix, paint, params); } #endif @@ -1434,7 +1454,8 @@ protected: #ifdef SK_EXPERIMENTAL_SHADOWING virtual void onDrawShadowedPicture(const SkPicture*, const SkMatrix*, - const SkPaint*); + const SkPaint*, + const SkShadowParams& params); #endif // Returns the canvas to be used by DrawIter. Default implementation diff --git a/include/private/SkRecords.h b/include/private/SkRecords.h index 14b653190a..637a2ef68c 100644 --- a/include/private/SkRecords.h +++ b/include/private/SkRecords.h @@ -268,7 +268,8 @@ RECORD(DrawPicture, kDraw_Tag|kHasPaint_Tag, RECORD(DrawShadowedPicture, kDraw_Tag|kHasPaint_Tag, Optional paint; sk_sp picture; - TypedMatrix matrix); + TypedMatrix matrix; + const SkShadowParams& params); RECORD(DrawPoints, kDraw_Tag|kHasPaint_Tag, SkPaint paint; SkCanvas::PointMode mode; diff --git a/include/private/SkShadowParams.h b/include/private/SkShadowParams.h new file mode 100644 index 0000000000..3df0a44279 --- /dev/null +++ b/include/private/SkShadowParams.h @@ -0,0 +1,48 @@ +/* + * Copyright 2016 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +#ifndef SkShadowParams_DEFINED +#define SkShadowParams_DEFINED + +/** \struct SkShadowParams + + This struct holds information needed for drawing shadows. + + fShadowRadius - radius of the shadow blur + + fBiasingConstant - A constant used in variance shadow mapping to directly + 0.0 - 1.0 reduce light bleeding. Essentially sets all shadows + ~.25 below a certain brightness equal to no light, and does + a linear step on the rest. Essentially makes shadows + darker and more rounded at higher values. + + fMinVariance - Too low of a variance (near the outer edges of blurry + ~512, 1024 shadows) will lead to ugly sharp shadow brightness + distortions. This enforces a minimum amount of variance + in the calculation to smooth out the outside edges of + blurry shadows. However, too high of a value for this will + cause all shadows to be lighter by visibly different + amounts varying on depth. + + fType - Decides which algorithm to use to draw shadows. +*/ +struct SkShadowParams { + SkScalar fShadowRadius; + SkScalar fBiasingConstant; + SkScalar fMinVariance; + + enum ShadowType { + kNoBlur_ShadowType, + kVariance_ShadowType, + + kLast_ShadowType = kVariance_ShadowType + }; + static const int kShadowTypeCount = kLast_ShadowType + 1; + + ShadowType fType; +}; + +#endif -- cgit v1.2.3