| OLD | NEW |
| 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 #include "SkCanvas.h" |
| 9 #include "SkLights.h" | 9 #include "SkLights.h" |
| 10 #include "SkReadBuffer.h" | 10 #include "SkReadBuffer.h" |
| 11 #include "SkShadowShader.h" | 11 #include "SkShadowShader.h" |
| 12 #include "SkPoint3.h" | |
| 13 | 12 |
| 14 //////////////////////////////////////////////////////////////////////////// | 13 //////////////////////////////////////////////////////////////////////////// |
| 15 #ifdef SK_EXPERIMENTAL_SHADOWING | 14 #ifdef SK_EXPERIMENTAL_SHADOWING |
| 16 | 15 |
| 17 | 16 |
| 18 /** \class SkShadowShaderImpl | 17 /** \class SkShadowShaderImpl |
| 19 This subclass of shader applies shadowing | 18 This subclass of shader applies shadowing |
| 20 */ | 19 */ |
| 21 class SkShadowShaderImpl : public SkShader { | 20 class SkShadowShaderImpl : public SkShader { |
| 22 public: | 21 public: |
| 23 /** Create a new shadowing shader that shadows | 22 /** Create a new shadowing shader that shadows |
| 24 @param to do to do | 23 @param to do to do |
| 25 */ | 24 */ |
| 26 SkShadowShaderImpl(sk_sp<SkShader> povDepthShader, | 25 SkShadowShaderImpl(sk_sp<SkShader> povDepthShader, |
| 27 sk_sp<SkShader> diffuseShader, | 26 sk_sp<SkShader> diffuseShader, |
| 28 sk_sp<SkLights> lights, | 27 sk_sp<SkLights> lights, |
| 29 int diffuseWidth, int diffuseHeight) | 28 int diffuseWidth, int diffuseHeight, |
| 29 const SkShadowParams& sParams) |
| 30 : fPovDepthShader(std::move(povDepthShader)) | 30 : fPovDepthShader(std::move(povDepthShader)) |
| 31 , fDiffuseShader(std::move(diffuseShader)) | 31 , fDiffuseShader(std::move(diffuseShader)) |
| 32 , fLights(std::move(lights)) | 32 , fLights(std::move(lights)) |
| 33 , fDiffuseWidth(diffuseWidth) | 33 , fDiffuseWidth(diffuseWidth) |
| 34 , fDiffuseHeight(diffuseHeight) { } | 34 , fDiffuseHeight(diffuseHeight) |
| 35 , fShadowParams(sParams) { } |
| 35 | 36 |
| 36 bool isOpaque() const override; | 37 bool isOpaque() const override; |
| 37 | 38 |
| 38 #if SK_SUPPORT_GPU | 39 #if SK_SUPPORT_GPU |
| 39 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri
de; | 40 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri
de; |
| 40 #endif | 41 #endif |
| 41 | 42 |
| 42 class ShadowShaderContext : public SkShader::Context { | 43 class ShadowShaderContext : public SkShader::Context { |
| 43 public: | 44 public: |
| 44 // The context takes ownership of the states. It will call their destruc
tors | 45 // The context takes ownership of the states. It will call their destruc
tors |
| (...skipping 28 matching lines...) Expand all Loading... |
| 73 Context* onCreateContext(const ContextRec&, void*) const override; | 74 Context* onCreateContext(const ContextRec&, void*) const override; |
| 74 | 75 |
| 75 private: | 76 private: |
| 76 sk_sp<SkShader> fPovDepthShader; | 77 sk_sp<SkShader> fPovDepthShader; |
| 77 sk_sp<SkShader> fDiffuseShader; | 78 sk_sp<SkShader> fDiffuseShader; |
| 78 sk_sp<SkLights> fLights; | 79 sk_sp<SkLights> fLights; |
| 79 | 80 |
| 80 int fDiffuseWidth; | 81 int fDiffuseWidth; |
| 81 int fDiffuseHeight; | 82 int fDiffuseHeight; |
| 82 | 83 |
| 84 SkShadowParams fShadowParams; |
| 85 |
| 83 friend class SkShadowShader; | 86 friend class SkShadowShader; |
| 84 | 87 |
| 85 typedef SkShader INHERITED; | 88 typedef SkShader INHERITED; |
| 86 }; | 89 }; |
| 87 | 90 |
| 88 //////////////////////////////////////////////////////////////////////////// | 91 //////////////////////////////////////////////////////////////////////////// |
| 89 | 92 |
| 90 #if SK_SUPPORT_GPU | 93 #if SK_SUPPORT_GPU |
| 91 | 94 |
| 92 #include "GrCoordTransform.h" | 95 #include "GrCoordTransform.h" |
| 93 #include "GrFragmentProcessor.h" | 96 #include "GrFragmentProcessor.h" |
| 94 #include "GrInvariantOutput.h" | 97 #include "GrInvariantOutput.h" |
| 95 #include "glsl/GrGLSLFragmentProcessor.h" | 98 #include "glsl/GrGLSLFragmentProcessor.h" |
| 96 #include "glsl/GrGLSLFragmentShaderBuilder.h" | 99 #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 97 #include "SkGr.h" | 100 #include "SkGr.h" |
| 98 #include "SkGrPriv.h" | 101 #include "SkGrPriv.h" |
| 99 #include "SkSpecialImage.h" | 102 #include "SkSpecialImage.h" |
| 100 #include "SkImage_Base.h" | 103 #include "SkImage_Base.h" |
| 101 #include "GrContext.h" | 104 #include "GrContext.h" |
| 102 | 105 |
| 103 class ShadowFP : public GrFragmentProcessor { | 106 class ShadowFP : public GrFragmentProcessor { |
| 104 public: | 107 public: |
| 105 ShadowFP(sk_sp<GrFragmentProcessor> povDepth, | 108 ShadowFP(sk_sp<GrFragmentProcessor> povDepth, |
| 106 sk_sp<GrFragmentProcessor> diffuse, | 109 sk_sp<GrFragmentProcessor> diffuse, |
| 107 sk_sp<SkLights> lights, | 110 sk_sp<SkLights> lights, |
| 108 int diffuseWidth, int diffuseHeight, | 111 int diffuseWidth, int diffuseHeight, |
| 112 const SkShadowParams& sParams, |
| 109 GrContext* context) { | 113 GrContext* context) { |
| 110 | 114 |
| 111 // fuse all ambient lights into a single one | 115 // fuse all ambient lights into a single one |
| 112 fAmbientColor.set(0.0f, 0.0f, 0.0f); | 116 fAmbientColor.set(0.0f, 0.0f, 0.0f); |
| 113 | 117 |
| 114 fNumDirLights = 0; // refers to directional lights. | 118 fNumDirLights = 0; // refers to directional lights. |
| 115 for (int i = 0; i < lights->numLights(); ++i) { | 119 for (int i = 0; i < lights->numLights(); ++i) { |
| 116 if (SkLights::Light::kAmbient_LightType == lights->light(i).type())
{ | 120 if (SkLights::Light::kAmbient_LightType == lights->light(i).type())
{ |
| 117 fAmbientColor += lights->light(i).color(); | 121 fAmbientColor += lights->light(i).color(); |
| 118 } else if (fNumDirLights < SkShadowShader::kMaxNonAmbientLights) { | 122 } else if (fNumDirLights < SkShadowShader::kMaxNonAmbientLights) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 130 fDepthMapHeight[fNumDirLights] = shadowMap->height(); | 134 fDepthMapHeight[fNumDirLights] = shadowMap->height(); |
| 131 fDepthMapWidth[fNumDirLights] = shadowMap->width(); | 135 fDepthMapWidth[fNumDirLights] = shadowMap->width(); |
| 132 | 136 |
| 133 fNumDirLights++; | 137 fNumDirLights++; |
| 134 } | 138 } |
| 135 } | 139 } |
| 136 | 140 |
| 137 fWidth = diffuseWidth; | 141 fWidth = diffuseWidth; |
| 138 fHeight = diffuseHeight; | 142 fHeight = diffuseHeight; |
| 139 | 143 |
| 140 this->registerChildProcessor(std::move(povDepth)); | 144 fShadowParams = sParams; |
| 145 |
| 146 this->registerChildProcessor(std::move(povDepth)); |
| 141 this->registerChildProcessor(std::move(diffuse)); | 147 this->registerChildProcessor(std::move(diffuse)); |
| 142 this->initClassID<ShadowFP>(); | 148 this->initClassID<ShadowFP>(); |
| 143 } | 149 } |
| 144 | 150 |
| 145 class GLSLShadowFP : public GrGLSLFragmentProcessor { | 151 class GLSLShadowFP : public GrGLSLFragmentProcessor { |
| 146 public: | 152 public: |
| 147 GLSLShadowFP() { } | 153 GLSLShadowFP() { } |
| 148 | 154 |
| 149 void emitCode(EmitArgs& args) override { | 155 void emitCode(EmitArgs& args) override { |
| 150 | 156 |
| 151 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 157 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 152 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 158 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 153 | 159 |
| 154 // add uniforms | 160 // add uniforms |
| 155 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; | 161 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; |
| 156 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights); | 162 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights); |
| 157 | 163 |
| 164 int blurAlgorithm = args.fFp.cast<ShadowFP>().fShadowParams.fShadowT
ype; |
| 165 |
| 158 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] =
{nullptr}; | 166 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] =
{nullptr}; |
| 159 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights]
= {nullptr}; | 167 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights]
= {nullptr}; |
| 160 | 168 |
| 161 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight
s] | 169 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight
s] |
| 162 = {nullptr}; | 170 = {nullptr}; |
| 163 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh
ts] | 171 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh
ts] |
| 164 = {nullptr}; | 172 = {nullptr}; |
| 165 | 173 |
| 166 SkString lightDirUniNameBase("lightDir"); | 174 SkString lightDirUniNameBase("lightDir"); |
| 167 SkString lightColorUniNameBase("lightColor"); | 175 SkString lightColorUniNameBase("lightColor"); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 196 kDefault_GrSLPrecision, | 204 kDefault_GrSLPrecision, |
| 197 depthMapWidthUniNameStr.c_str
(), | 205 depthMapWidthUniNameStr.c_str
(), |
| 198 &depthMapWidthUniName[i]); | 206 &depthMapWidthUniName[i]); |
| 199 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS
haderFlag, | 207 fDepthMapHeightUni[i] = uniformHandler->addUniform(kFragment_GrS
haderFlag, |
| 200 kInt_GrSLType, | 208 kInt_GrSLType, |
| 201 kDefault_GrSLPrecision, | 209 kDefault_GrSLPrecision, |
| 202 depthMapHeightUniNameStr.c_st
r(), | 210 depthMapHeightUniNameStr.c_st
r(), |
| 203 &depthMapHeightUniName[i]); | 211 &depthMapHeightUniName[i]); |
| 204 } | 212 } |
| 205 | 213 |
| 214 const char* shBiasUniName = nullptr; |
| 215 const char* minVarianceUniName = nullptr; |
| 216 |
| 217 fBiasingConstantUni = uniformHandler->addUniform(kFragment_GrShaderF
lag, |
| 218 kFloat_GrSLType, |
| 219 kDefault_GrSLPrecis
ion, |
| 220 "shadowBias", &shBi
asUniName); |
| 221 fMinVarianceUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 222 kFloat_GrSLType, |
| 223 kDefault_GrSLPrecision, |
| 224 "minVariance", &minVari
anceUniName); |
| 206 | 225 |
| 207 const char* widthUniName = nullptr; | 226 const char* widthUniName = nullptr; |
| 208 const char* heightUniName = nullptr; | 227 const char* heightUniName = nullptr; |
| 209 | 228 |
| 210 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 229 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 211 kInt_GrSLType, | 230 kInt_GrSLType, |
| 212 kDefault_GrSLPrecision, | 231 kDefault_GrSLPrecision, |
| 213 "width", &widthUniName); | 232 "width", &widthUniName); |
| 214 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | 233 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag, |
| 215 kInt_GrSLType, | 234 kInt_GrSLType, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 offset.c_str(), lightDirUniName[i]); | 266 offset.c_str(), lightDirUniName[i]); |
| 248 | 267 |
| 249 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s)
);\n", | 268 fragBuilder->codeAppendf("vec2 %s = (vec2(%s, %s) / vec2(%s, %s)
);\n", |
| 250 scaleVec.c_str(), | 269 scaleVec.c_str(), |
| 251 widthUniName, heightUniName, | 270 widthUniName, heightUniName, |
| 252 depthMapWidthUniName[i], depthMapHeight
UniName[i]); | 271 depthMapWidthUniName[i], depthMapHeight
UniName[i]); |
| 253 | 272 |
| 254 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", | 273 fragBuilder->codeAppendf("vec2 %s = 1 - %s;\n", |
| 255 scaleOffsetVec.c_str(), scaleVec.c_str(
)); | 274 scaleOffsetVec.c_str(), scaleVec.c_str(
)); |
| 256 | 275 |
| 257 | |
| 258 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " | 276 fragBuilder->codeAppendf("vec2 %s = (vMatrixCoord_0_1_Stage0 + " |
| 259 "vec2(%s.x, 0 - %s.y)) " | 277 "vec2(%s.x, 0 - %s.y)) " |
| 260 " * %s + vec2(0,1) * %s;\n", | 278 " * %s + vec2(0,1) * %s;\n", |
| 261 | |
| 262 povCoord.c_str(), offset.c_str(), offse
t.c_str(), | 279 povCoord.c_str(), offset.c_str(), offse
t.c_str(), |
| 263 scaleVec.c_str(), scaleOffsetVec.c_str(
)); | 280 scaleVec.c_str(), scaleOffsetVec.c_str(
)); |
| 264 | 281 |
| 265 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler
s[i], | 282 fragBuilder->appendTextureLookup(&depthMaps[i], args.fTexSampler
s[i], |
| 266 povCoord.c_str(), | 283 povCoord.c_str(), |
| 267 kVec2f_GrSLType); | 284 kVec2f_GrSLType); |
| 285 |
| 286 |
| 268 } | 287 } |
| 269 | 288 |
| 270 const char* ambientColorUniName = nullptr; | 289 const char* ambientColorUniName = nullptr; |
| 271 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag
, | 290 fAmbientColorUni = uniformHandler->addUniform(kFragment_GrShaderFlag
, |
| 272 kVec3f_GrSLType, kDefa
ult_GrSLPrecision, | 291 kVec3f_GrSLType, kDefa
ult_GrSLPrecision, |
| 273 "AmbientColor", &ambie
ntColorUniName); | 292 "AmbientColor", &ambie
ntColorUniName); |
| 274 | 293 |
| 275 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol
or.c_str()); | 294 fragBuilder->codeAppendf("vec4 resultDiffuseColor = %s;", diffuseCol
or.c_str()); |
| 276 | 295 |
| 277 // Essentially, | 296 // all the normal vectors point straight up |
| 278 // diffColor * (ambientLightTot + foreachDirLight(lightColor * (N .
L))) | |
| 279 SkString totalLightColor("totalLightColor"); | 297 SkString totalLightColor("totalLightColor"); |
| 280 fragBuilder->codeAppendf("vec3 %s = vec3(0);", totalLightColor.c_str
()); | 298 fragBuilder->codeAppendf("vec3 %s = vec3(0,0,0);", totalLightColor.c
_str()); |
| 299 |
| 300 SkString lightProb("lightProbability"); |
| 301 fragBuilder->codeAppendf("float %s;", lightProb.c_str()); |
| 302 fragBuilder->codeAppendf("float variance;"); |
| 303 fragBuilder->codeAppendf("float d;"); |
| 281 | 304 |
| 282 for (int i = 0; i < numLights; i++) { | 305 for (int i = 0; i < numLights; i++) { |
| 283 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", | 306 fragBuilder->codeAppendf("%s = 1;", lightProb.c_str()); |
| 307 |
| 308 // 1/512 is less than half a pixel; imperceptible |
| 309 fragBuilder->codeAppendf("if (%s.b <= %s.b + 1/512) {", |
| 284 povDepth.c_str(), depthMaps[i].c_str())
; | 310 povDepth.c_str(), depthMaps[i].c_str())
; |
| 285 // Note that dot(vec3(0,0,1), %s) == %s.z * %s | 311 if (blurAlgorithm == SkShadowParams::kVariance_BlurAlgorithm) { |
| 286 fragBuilder->codeAppendf("%s += %s.z * %s;", | 312 fragBuilder->codeAppendf("vec2 moments = vec2(%s.b * 255, %
s.g * 255 * 256 );", |
| 313 depthMaps[i].c_str(), depthMaps[i].
c_str()); |
| 314 |
| 315 // variance biasing lessens light bleeding |
| 316 fragBuilder->codeAppendf("variance = max(moments.y - (moment
s.x * moments.x)," |
| 317 "%s);", minVarianceUniName)
; |
| 318 |
| 319 fragBuilder->codeAppendf("d = (%s.b * 255) - moments.x;", po
vDepth.c_str()); |
| 320 fragBuilder->codeAppendf("%s = (variance / (variance + d * d
));", |
| 321 lightProb.c_str()); |
| 322 |
| 323 SkString clamp("clamp"); |
| 324 clamp.appendf("%d", i); |
| 325 |
| 326 // choosing between light artifacts or correct shape shadows |
| 327 // linstep |
| 328 fragBuilder->codeAppendf("float %s = clamp((%s - %s) / (1 -
%s), 0, 1);", |
| 329 clamp.c_str(), lightProb.c_str(), |
| 330 shBiasUniName, shBiasUniName); |
| 331 |
| 332 fragBuilder->codeAppendf("%s = %s;", |
| 333 lightProb.c_str(), clamp.c_str()); |
| 334 } else { |
| 335 fragBuilder->codeAppendf("if (%s.b >= %s.b) {", |
| 336 povDepth.c_str(), depthMaps[i].c_st
r()); |
| 337 fragBuilder->codeAppendf("%s = 1;", lightProb.c_str()); |
| 338 fragBuilder->codeAppendf("} else { %s = 0; }", lightProb.c_s
tr()); |
| 339 } |
| 340 |
| 341 // VSM: The curved shadows near plane edges are mostly light ble
eding. |
| 342 fragBuilder->codeAppendf("}"); |
| 343 |
| 344 fragBuilder->codeAppendf("%s += dot(vec3(0,0,1), %s) * %s * %s;"
, |
| 287 totalLightColor.c_str(), | 345 totalLightColor.c_str(), |
| 288 lightDirUniName[i], | 346 lightDirUniName[i], |
| 289 lightColorUniName[i]); | 347 lightColorUniName[i], |
| 290 fragBuilder->codeAppendf("}"); | 348 lightProb.c_str()); |
| 291 } | 349 } |
| 292 | 350 |
| 293 fragBuilder->codeAppendf("%s += %s;", | 351 fragBuilder->codeAppendf("%s += %s;", totalLightColor.c_str(), ambie
ntColorUniName); |
| 294 totalLightColor.c_str(), | |
| 295 ambientColorUniName); | |
| 296 | 352 |
| 297 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", | 353 fragBuilder->codeAppendf("resultDiffuseColor *= vec4(%s, 1);", |
| 298 totalLightColor.c_str()); | 354 totalLightColor.c_str()); |
| 299 | 355 |
| 300 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol
or); | 356 fragBuilder->codeAppendf("%s = resultDiffuseColor;", args.fOutputCol
or); |
| 301 } | 357 } |
| 302 | 358 |
| 303 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 359 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
| 304 GrProcessorKeyBuilder* b) { | 360 GrProcessorKeyBuilder* b) { |
| 305 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | 361 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
| 306 b->add32(shadowFP.fNumDirLights); | 362 b->add32(shadowFP.fNumDirLights); |
| 363 b->add32(shadowFP.fShadowParams.fShadowType); |
| 307 } | 364 } |
| 308 | 365 |
| 309 protected: | 366 protected: |
| 310 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&
proc) override { | 367 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&
proc) override { |
| 311 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); | 368 const ShadowFP &shadowFP = proc.cast<ShadowFP>(); |
| 312 | 369 |
| 313 fNumDirLights = shadowFP.numLights(); | 370 for (int i = 0; i < shadowFP.fNumDirLights; i++) { |
| 314 | |
| 315 for (int i = 0; i < fNumDirLights; i++) { | |
| 316 const SkVector3& lightDir = shadowFP.lightDir(i); | 371 const SkVector3& lightDir = shadowFP.lightDir(i); |
| 317 if (lightDir != fLightDir[i]) { | 372 if (lightDir != fLightDir[i]) { |
| 318 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); | 373 pdman.set3fv(fLightDirUni[i], 1, &lightDir.fX); |
| 319 fLightDir[i] = lightDir; | 374 fLightDir[i] = lightDir; |
| 320 } | 375 } |
| 321 const SkColor3f& lightColor = shadowFP.lightColor(i); | 376 const SkColor3f& lightColor = shadowFP.lightColor(i); |
| 322 if (lightColor != fLightColor[i]) { | 377 if (lightColor != fLightColor[i]) { |
| 323 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); | 378 pdman.set3fv(fLightColorUni[i], 1, &lightColor.fX); |
| 324 fLightColor[i] = lightColor; | 379 fLightColor[i] = lightColor; |
| 325 } | 380 } |
| 326 | 381 |
| 327 int depthMapWidth = shadowFP.depthMapWidth(i); | 382 int depthMapWidth = shadowFP.depthMapWidth(i); |
| 328 if (depthMapWidth != fDepthMapWidth[i]) { | 383 if (depthMapWidth != fDepthMapWidth[i]) { |
| 329 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth); | 384 pdman.set1i(fDepthMapWidthUni[i], depthMapWidth); |
| 330 fDepthMapWidth[i] = depthMapWidth; | 385 fDepthMapWidth[i] = depthMapWidth; |
| 331 } | 386 } |
| 332 int depthMapHeight = shadowFP.depthMapHeight(i); | 387 int depthMapHeight = shadowFP.depthMapHeight(i); |
| 333 if (depthMapHeight != fDepthMapHeight[i]) { | 388 if (depthMapHeight != fDepthMapHeight[i]) { |
| 334 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight); | 389 pdman.set1i(fDepthMapHeightUni[i], depthMapHeight); |
| 335 fDepthMapHeight[i] = depthMapHeight; | 390 fDepthMapHeight[i] = depthMapHeight; |
| 336 } | 391 } |
| 337 } | 392 } |
| 338 | 393 |
| 394 SkScalar biasingConstant = shadowFP.shadowType().fBiasingConstant; |
| 395 if (biasingConstant != fBiasingConstant) { |
| 396 pdman.set1f(fBiasingConstantUni, biasingConstant); |
| 397 fBiasingConstant = biasingConstant; |
| 398 } |
| 399 |
| 400 SkScalar minVariance = shadowFP.shadowType().fMinVariance; |
| 401 if (minVariance != fMinVariance) { |
| 402 pdman.set1f(fMinVarianceUni, minVariance); |
| 403 fMinVariance = minVariance; |
| 404 } |
| 405 |
| 339 int width = shadowFP.width(); | 406 int width = shadowFP.width(); |
| 340 if (width != fWidth) { | 407 if (width != fWidth) { |
| 341 pdman.set1i(fWidthUni, width); | 408 pdman.set1i(fWidthUni, width); |
| 342 fWidth = width; | 409 fWidth = width; |
| 343 } | 410 } |
| 344 int height = shadowFP.height(); | 411 int height = shadowFP.height(); |
| 345 if (height != fHeight) { | 412 if (height != fHeight) { |
| 346 pdman.set1i(fHeightUni, height); | 413 pdman.set1i(fHeightUni, height); |
| 347 fHeight = height; | 414 fHeight = height; |
| 348 } | 415 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 369 | 436 |
| 370 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; | 437 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
| 371 GrGLSLProgramDataManager::UniformHandle | 438 GrGLSLProgramDataManager::UniformHandle |
| 372 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; | 439 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; |
| 373 | 440 |
| 374 int fWidth; | 441 int fWidth; |
| 375 GrGLSLProgramDataManager::UniformHandle fWidthUni; | 442 GrGLSLProgramDataManager::UniformHandle fWidthUni; |
| 376 int fHeight; | 443 int fHeight; |
| 377 GrGLSLProgramDataManager::UniformHandle fHeightUni; | 444 GrGLSLProgramDataManager::UniformHandle fHeightUni; |
| 378 | 445 |
| 446 SkScalar fBiasingConstant; |
| 447 GrGLSLProgramDataManager::UniformHandle fBiasingConstantUni; |
| 448 SkScalar fMinVariance; |
| 449 GrGLSLProgramDataManager::UniformHandle fMinVarianceUni; |
| 450 |
| 379 SkColor3f fAmbientColor; | 451 SkColor3f fAmbientColor; |
| 380 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; | 452 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; |
| 381 | |
| 382 int fNumDirLights; | |
| 383 }; | 453 }; |
| 384 | 454 |
| 385 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b)
const override { | 455 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b)
const override { |
| 386 GLSLShadowFP::GenKey(*this, caps, b); | 456 GLSLShadowFP::GenKey(*this, caps, b); |
| 387 } | 457 } |
| 388 | 458 |
| 389 const char* name() const override { return "shadowFP"; } | 459 const char* name() const override { return "shadowFP"; } |
| 390 | 460 |
| 391 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 461 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
| 392 inout->mulByUnknownFourComponents(); | 462 inout->mulByUnknownFourComponents(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 406 SkASSERT(i < fNumDirLights); | 476 SkASSERT(i < fNumDirLights); |
| 407 return fDepthMapWidth[i]; | 477 return fDepthMapWidth[i]; |
| 408 } | 478 } |
| 409 int depthMapHeight(int i) const { | 479 int depthMapHeight(int i) const { |
| 410 SkASSERT(i < fNumDirLights); | 480 SkASSERT(i < fNumDirLights); |
| 411 return fDepthMapHeight[i]; | 481 return fDepthMapHeight[i]; |
| 412 } | 482 } |
| 413 int width() const {return fWidth; } | 483 int width() const {return fWidth; } |
| 414 int height() const {return fHeight; } | 484 int height() const {return fHeight; } |
| 415 | 485 |
| 486 const SkShadowParams& shadowType() const {return fShadowParams; } |
| 487 |
| 416 private: | 488 private: |
| 417 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
GLSLShadowFP; } | 489 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
GLSLShadowFP; } |
| 418 | 490 |
| 419 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 491 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
| 420 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); | 492 const ShadowFP& shadowFP = proc.cast<ShadowFP>(); |
| 421 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP
.fNumDirLights) { | 493 if (fAmbientColor != shadowFP.fAmbientColor || fNumDirLights != shadowFP
.fNumDirLights) { |
| 422 return false; | 494 return false; |
| 423 } | 495 } |
| 424 | 496 |
| 425 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { | 497 if (fWidth != shadowFP.fWidth || fHeight != shadowFP.fHeight) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 447 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; | 519 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; |
| 448 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; | 520 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; |
| 449 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; | 521 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; |
| 450 | 522 |
| 451 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; | 523 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; |
| 452 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; | 524 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
| 453 | 525 |
| 454 int fHeight; | 526 int fHeight; |
| 455 int fWidth; | 527 int fWidth; |
| 456 | 528 |
| 529 SkShadowParams fShadowParams; |
| 530 |
| 457 SkColor3f fAmbientColor; | 531 SkColor3f fAmbientColor; |
| 458 }; | 532 }; |
| 459 | 533 |
| 460 //////////////////////////////////////////////////////////////////////////// | 534 //////////////////////////////////////////////////////////////////////////// |
| 461 | 535 |
| 462 sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor(const AsFPArg
s& fpargs) const { | 536 sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor(const AsFPArg
s& fpargs) const { |
| 463 | 537 |
| 464 sk_sp<GrFragmentProcessor> povDepthFP = fPovDepthShader->asFragmentProcessor
(fpargs); | 538 sk_sp<GrFragmentProcessor> povDepthFP = fPovDepthShader->asFragmentProcessor
(fpargs); |
| 465 | 539 |
| 466 sk_sp<GrFragmentProcessor> diffuseFP = fDiffuseShader->asFragmentProcessor(f
pargs); | 540 sk_sp<GrFragmentProcessor> diffuseFP = fDiffuseShader->asFragmentProcessor(f
pargs); |
| 467 | 541 |
| 468 sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<ShadowFP>(std::move(povDept
hFP), | 542 sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<ShadowFP>(std::move(povDept
hFP), |
| 469 std::move(diffuse
FP), | 543 std::move(diffuse
FP), |
| 470 std::move(fLights
), | 544 std::move(fLights
), |
| 471 fDiffuseWidth, fD
iffuseHeight, | 545 fDiffuseWidth, fD
iffuseHeight, |
| 472 fpargs.fContext); | 546 fShadowParams, fp
args.fContext); |
| 473 return shadowfp; | 547 return shadowfp; |
| 474 } | 548 } |
| 475 | 549 |
| 476 | 550 |
| 477 #endif | 551 #endif |
| 478 | 552 |
| 479 //////////////////////////////////////////////////////////////////////////// | 553 //////////////////////////////////////////////////////////////////////////// |
| 480 | 554 |
| 481 bool SkShadowShaderImpl::isOpaque() const { | 555 bool SkShadowShaderImpl::isOpaque() const { |
| 482 return fDiffuseShader->isOpaque(); | 556 return fDiffuseShader->isOpaque(); |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 619 | 693 |
| 620 SkLights::Light light = SkLights::Light(color, dir); | 694 SkLights::Light light = SkLights::Light(color, dir); |
| 621 light.setShadowMap(depthMap); | 695 light.setShadowMap(depthMap); |
| 622 | 696 |
| 623 builder.add(light); | 697 builder.add(light); |
| 624 } | 698 } |
| 625 } | 699 } |
| 626 | 700 |
| 627 sk_sp<SkLights> lights(builder.finish()); | 701 sk_sp<SkLights> lights(builder.finish()); |
| 628 | 702 |
| 703 SkShadowParams sParams; |
| 704 sParams.fMinVariance = buf.readScalar(); |
| 705 sParams.fBiasingConstant = buf.readScalar(); |
| 706 sParams.fShadowType = (SkShadowParams::ShadowType) buf.readInt(); |
| 707 sParams.fShadowRadius = buf.readScalar(); |
| 708 |
| 629 int diffuseWidth = buf.readInt(); | 709 int diffuseWidth = buf.readInt(); |
| 630 int diffuseHeight = buf.readInt(); | 710 int diffuseHeight = buf.readInt(); |
| 631 | 711 |
| 632 sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); | 712 sk_sp<SkShader> povDepthShader(buf.readFlattenable<SkShader>()); |
| 633 sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>()); | 713 sk_sp<SkShader> diffuseShader(buf.readFlattenable<SkShader>()); |
| 634 | 714 |
| 635 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), | 715 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), |
| 636 std::move(diffuseShader), | 716 std::move(diffuseShader), |
| 637 std::move(lights), | 717 std::move(lights), |
| 638 diffuseWidth, diffuseHeight); | 718 diffuseWidth, diffuseHeight, |
| 719 sParams); |
| 639 } | 720 } |
| 640 | 721 |
| 641 void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const { | 722 void SkShadowShaderImpl::flatten(SkWriteBuffer& buf) const { |
| 642 this->INHERITED::flatten(buf); | 723 this->INHERITED::flatten(buf); |
| 643 | 724 |
| 644 buf.writeInt(fLights->numLights()); | 725 buf.writeInt(fLights->numLights()); |
| 645 | 726 |
| 646 for (int l = 0; l < fLights->numLights(); ++l) { | 727 for (int l = 0; l < fLights->numLights(); ++l) { |
| 647 const SkLights::Light& light = fLights->light(l); | 728 const SkLights::Light& light = fLights->light(l); |
| 648 | 729 |
| 649 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); | 730 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); |
| 650 | 731 |
| 651 buf.writeBool(isAmbient); | 732 buf.writeBool(isAmbient); |
| 652 buf.writeScalarArray(&light.color().fX, 3); | 733 buf.writeScalarArray(&light.color().fX, 3); |
| 653 if (!isAmbient) { | 734 if (!isAmbient) { |
| 654 buf.writeScalarArray(&light.dir().fX, 3); | 735 buf.writeScalarArray(&light.dir().fX, 3); |
| 655 } | 736 } |
| 656 | 737 |
| 657 buf.writeImage(light.getShadowMap()); | 738 buf.writeImage(light.getShadowMap()); |
| 658 } | 739 } |
| 659 | 740 |
| 741 buf.writeScalar(fShadowParams.fMinVariance); |
| 742 buf.writeScalar(fShadowParams.fBiasingConstant); |
| 743 buf.writeInt(fShadowParams.fShadowType); |
| 744 buf.writeScalar(fShadowParams.fShadowRadius); |
| 745 |
| 660 buf.writeInt(fDiffuseWidth); | 746 buf.writeInt(fDiffuseWidth); |
| 661 buf.writeInt(fDiffuseHeight); | 747 buf.writeInt(fDiffuseHeight); |
| 662 | 748 |
| 663 buf.writeFlattenable(fPovDepthShader.get()); | 749 buf.writeFlattenable(fPovDepthShader.get()); |
| 664 buf.writeFlattenable(fDiffuseShader.get()); | 750 buf.writeFlattenable(fDiffuseShader.get()); |
| 665 } | 751 } |
| 666 | 752 |
| 667 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { | 753 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { |
| 668 return sizeof(ShadowShaderContext); | 754 return sizeof(ShadowShaderContext); |
| 669 } | 755 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 695 | 781 |
| 696 return new (storage) ShadowShaderContext(*this, rec, povDepthContext, diffus
eContext, | 782 return new (storage) ShadowShaderContext(*this, rec, povDepthContext, diffus
eContext, |
| 697 heapAllocated); | 783 heapAllocated); |
| 698 } | 784 } |
| 699 | 785 |
| 700 /////////////////////////////////////////////////////////////////////////////// | 786 /////////////////////////////////////////////////////////////////////////////// |
| 701 | 787 |
| 702 sk_sp<SkShader> SkShadowShader::Make(sk_sp<SkShader> povDepthShader, | 788 sk_sp<SkShader> SkShadowShader::Make(sk_sp<SkShader> povDepthShader, |
| 703 sk_sp<SkShader> diffuseShader, | 789 sk_sp<SkShader> diffuseShader, |
| 704 sk_sp<SkLights> lights, | 790 sk_sp<SkLights> lights, |
| 705 int diffuseWidth, int diffuseHeight) { | 791 int diffuseWidth, int diffuseHeight, |
| 792 const SkShadowParams& sParams) { |
| 706 if (!povDepthShader || !diffuseShader) { | 793 if (!povDepthShader || !diffuseShader) { |
| 707 // TODO: Use paint's color in absence of a diffuseShader | 794 // TODO: Use paint's color in absence of a diffuseShader |
| 708 // TODO: Use a default implementation of normalSource instead | 795 // TODO: Use a default implementation of normalSource instead |
| 709 return nullptr; | 796 return nullptr; |
| 710 } | 797 } |
| 711 | 798 |
| 712 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), | 799 return sk_make_sp<SkShadowShaderImpl>(std::move(povDepthShader), |
| 713 std::move(diffuseShader), | 800 std::move(diffuseShader), |
| 714 std::move(lights), | 801 std::move(lights), |
| 715 diffuseWidth, diffuseHeight); | 802 diffuseWidth, diffuseHeight, |
| 803 sParams); |
| 716 } | 804 } |
| 717 | 805 |
| 718 /////////////////////////////////////////////////////////////////////////////// | 806 /////////////////////////////////////////////////////////////////////////////// |
| 719 | 807 |
| 720 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 808 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
| 721 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 809 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
| 722 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 810 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 723 | 811 |
| 724 /////////////////////////////////////////////////////////////////////////////// | 812 /////////////////////////////////////////////////////////////////////////////// |
| 725 | 813 |
| 726 #endif | 814 #endif |
| OLD | NEW |