| 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 |
| 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" | 12 #include "SkPoint3.h" |
| 13 | 13 |
| 14 //////////////////////////////////////////////////////////////////////////// | 14 //////////////////////////////////////////////////////////////////////////// |
| 15 #ifdef SK_EXPERIMENTAL_SHADOWING | 15 #ifdef SK_EXPERIMENTAL_SHADOWING |
| 16 | 16 |
| 17 #define SK_MAX_NON_AMBIENT_LIGHTS 4 | |
| 18 | 17 |
| 19 /** \class SkShadowShaderImpl | 18 /** \class SkShadowShaderImpl |
| 20 This subclass of shader applies shadowing | 19 This subclass of shader applies shadowing |
| 21 */ | 20 */ |
| 22 class SkShadowShaderImpl : public SkShader { | 21 class SkShadowShaderImpl : public SkShader { |
| 23 public: | 22 public: |
| 24 /** Create a new shadowing shader that shadows | 23 /** Create a new shadowing shader that shadows |
| 25 @param to do to do | 24 @param to do to do |
| 26 */ | 25 */ |
| 27 SkShadowShaderImpl(sk_sp<SkShader> povDepthShader, | 26 SkShadowShaderImpl(sk_sp<SkShader> povDepthShader, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 int diffuseWidth, int diffuseHeight, | 108 int diffuseWidth, int diffuseHeight, |
| 110 GrContext* context) { | 109 GrContext* context) { |
| 111 | 110 |
| 112 // fuse all ambient lights into a single one | 111 // fuse all ambient lights into a single one |
| 113 fAmbientColor.set(0.0f, 0.0f, 0.0f); | 112 fAmbientColor.set(0.0f, 0.0f, 0.0f); |
| 114 | 113 |
| 115 fNumDirLights = 0; // refers to directional lights. | 114 fNumDirLights = 0; // refers to directional lights. |
| 116 for (int i = 0; i < lights->numLights(); ++i) { | 115 for (int i = 0; i < lights->numLights(); ++i) { |
| 117 if (SkLights::Light::kAmbient_LightType == lights->light(i).type())
{ | 116 if (SkLights::Light::kAmbient_LightType == lights->light(i).type())
{ |
| 118 fAmbientColor += lights->light(i).color(); | 117 fAmbientColor += lights->light(i).color(); |
| 119 } else if (fNumDirLights < SK_MAX_NON_AMBIENT_LIGHTS){ | 118 } else if (fNumDirLights < SkShadowShader::kMaxNonAmbientLights) { |
| 120 fLightColor[fNumDirLights] = lights->light(i).color(); | 119 fLightColor[fNumDirLights] = lights->light(i).color(); |
| 121 fLightDir[fNumDirLights] = lights->light(i).dir(); | 120 fLightDir[fNumDirLights] = lights->light(i).dir(); |
| 122 SkImage_Base* shadowMap = ((SkImage_Base*)lights->light(i).getSh
adowMap().get()); | 121 SkImage_Base* shadowMap = ((SkImage_Base*)lights->light(i).getSh
adowMap()); |
| 123 | 122 |
| 124 // this sk_sp gets deleted when the ShadowFP is destroyed, and f
rees the GrTexture* | 123 // gets deleted when the ShadowFP is destroyed, and frees the Gr
Texture* |
| 125 fTexture[fNumDirLights] = sk_sp<GrTexture>(shadowMap->asTextureR
ef(context, | 124 fTexture[fNumDirLights] = sk_sp<GrTexture>(shadowMap->asTextureR
ef(context, |
| 126 GrTextureParams::Clam
pNoFilter(), | 125 GrTextureParams::Clam
pNoFilter(), |
| 127 SkSourceGammaTreatmen
t::kIgnore)); | 126 SkSourceGammaTreatmen
t::kIgnore)); |
| 128 fDepthMapAccess[fNumDirLights].reset(fTexture[fNumDirLights].get
()); | 127 fDepthMapAccess[fNumDirLights].reset(fTexture[fNumDirLights].get
()); |
| 129 this->addTextureAccess(&fDepthMapAccess[fNumDirLights]); | 128 this->addTextureAccess(&fDepthMapAccess[fNumDirLights]); |
| 130 | 129 |
| 131 fDepthMapHeight[fNumDirLights] = shadowMap->height(); | 130 fDepthMapHeight[fNumDirLights] = shadowMap->height(); |
| 132 fDepthMapWidth[fNumDirLights] = shadowMap->width(); | 131 fDepthMapWidth[fNumDirLights] = shadowMap->width(); |
| 133 | 132 |
| 134 fNumDirLights++; | 133 fNumDirLights++; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 147 public: | 146 public: |
| 148 GLSLShadowFP() { } | 147 GLSLShadowFP() { } |
| 149 | 148 |
| 150 void emitCode(EmitArgs& args) override { | 149 void emitCode(EmitArgs& args) override { |
| 151 | 150 |
| 152 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | 151 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 153 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | 152 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
| 154 | 153 |
| 155 // add uniforms | 154 // add uniforms |
| 156 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; | 155 int32_t numLights = args.fFp.cast<ShadowFP>().fNumDirLights; |
| 157 SkASSERT(numLights <= SK_MAX_NON_AMBIENT_LIGHTS); | 156 SkASSERT(numLights <= SkShadowShader::kMaxNonAmbientLights); |
| 158 | 157 |
| 159 const char* lightDirUniName[SK_MAX_NON_AMBIENT_LIGHTS] = {nullptr}; | 158 const char* lightDirUniName[SkShadowShader::kMaxNonAmbientLights] =
{nullptr}; |
| 160 const char* lightColorUniName[SK_MAX_NON_AMBIENT_LIGHTS] = {nullptr}
; | 159 const char* lightColorUniName[SkShadowShader::kMaxNonAmbientLights]
= {nullptr}; |
| 161 | 160 |
| 162 const char* depthMapWidthUniName[SK_MAX_NON_AMBIENT_LIGHTS] = {nullp
tr}; | 161 const char* depthMapWidthUniName[SkShadowShader::kMaxNonAmbientLight
s] |
| 163 const char* depthMapHeightUniName[SK_MAX_NON_AMBIENT_LIGHTS] = {null
ptr}; | 162 = {nullptr}; |
| 163 const char* depthMapHeightUniName[SkShadowShader::kMaxNonAmbientLigh
ts] |
| 164 = {nullptr}; |
| 164 | 165 |
| 165 SkString lightDirUniNameBase("lightDir"); | 166 SkString lightDirUniNameBase("lightDir"); |
| 166 SkString lightColorUniNameBase("lightColor"); | 167 SkString lightColorUniNameBase("lightColor"); |
| 167 | 168 |
| 168 SkString depthMapWidthUniNameBase("dmapWidth"); | 169 SkString depthMapWidthUniNameBase("dmapWidth"); |
| 169 SkString depthMapHeightUniNameBase("dmapHeight"); | 170 SkString depthMapHeightUniNameBase("dmapHeight"); |
| 170 | 171 |
| 171 for (int i = 0; i < numLights; i++) { | 172 for (int i = 0; i < numLights; i++) { |
| 172 SkString lightDirUniNameStr(lightDirUniNameBase); | 173 SkString lightDirUniNameStr(lightDirUniNameBase); |
| 173 lightDirUniNameStr.appendf("%d", i); | 174 lightDirUniNameStr.appendf("%d", i); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 kDefault_GrSLPrecision, | 216 kDefault_GrSLPrecision, |
| 216 "height", &heightUniName); | 217 "height", &heightUniName); |
| 217 | 218 |
| 218 | 219 |
| 219 SkString povDepth("povDepth"); | 220 SkString povDepth("povDepth"); |
| 220 this->emitChild(0, nullptr, &povDepth, args); | 221 this->emitChild(0, nullptr, &povDepth, args); |
| 221 | 222 |
| 222 SkString diffuseColor("inDiffuseColor"); | 223 SkString diffuseColor("inDiffuseColor"); |
| 223 this->emitChild(1, nullptr, &diffuseColor, args); | 224 this->emitChild(1, nullptr, &diffuseColor, args); |
| 224 | 225 |
| 225 SkString depthMaps[SK_MAX_NON_AMBIENT_LIGHTS]; | 226 SkString depthMaps[SkShadowShader::kMaxNonAmbientLights]; |
| 226 | 227 |
| 227 for (int i = 0; i < numLights; i++) { | 228 for (int i = 0; i < numLights; i++) { |
| 228 SkString povCoord("povCoord"); | 229 SkString povCoord("povCoord"); |
| 229 povCoord.appendf("%d", i); | 230 povCoord.appendf("%d", i); |
| 230 | 231 |
| 231 // vMatrixCoord_0_1_Stage0 is the texture sampler coordinates. | 232 // vMatrixCoord_0_1_Stage0 is the texture sampler coordinates. |
| 232 // povDepth.b * 255 scales it to 0 - 255, bringing it to world s
pace, | 233 // povDepth.b * 255 scales it to 0 - 255, bringing it to world s
pace, |
| 233 // and the / 400 brings it back to a sampler coordinate, 0 - 1 | 234 // and the / 400 brings it back to a sampler coordinate, 0 - 1 |
| 234 // The 400 comes from the shadowmaps GM. | 235 // The 400 comes from the shadowmaps GM. |
| 235 // TODO use real shadowmaps size | 236 // TODO use real shadowmaps size |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 } | 348 } |
| 348 | 349 |
| 349 const SkColor3f& ambientColor = shadowFP.ambientColor(); | 350 const SkColor3f& ambientColor = shadowFP.ambientColor(); |
| 350 if (ambientColor != fAmbientColor) { | 351 if (ambientColor != fAmbientColor) { |
| 351 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); | 352 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); |
| 352 fAmbientColor = ambientColor; | 353 fAmbientColor = ambientColor; |
| 353 } | 354 } |
| 354 } | 355 } |
| 355 | 356 |
| 356 private: | 357 private: |
| 357 SkVector3 fLightDir[SK_MAX_NON_AMBIENT_LIGHTS]; | 358 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; |
| 358 GrGLSLProgramDataManager::UniformHandle fLightDirUni[SK_MAX_NON_AMBIENT_
LIGHTS]; | 359 GrGLSLProgramDataManager::UniformHandle |
| 359 SkColor3f fLightColor[SK_MAX_NON_AMBIENT_LIGHTS]; | 360 fLightDirUni[SkShadowShader::kMaxNonAmbientLights]; |
| 360 GrGLSLProgramDataManager::UniformHandle fLightColorUni[SK_MAX_NON_AMBIEN
T_LIGHTS]; | |
| 361 | 361 |
| 362 int fDepthMapWidth[SK_MAX_NON_AMBIENT_LIGHTS]; | 362 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; |
| 363 GrGLSLProgramDataManager::UniformHandle fDepthMapWidthUni[SK_MAX_NON_AMB
IENT_LIGHTS]; | 363 GrGLSLProgramDataManager::UniformHandle |
| 364 int fDepthMapHeight[SK_MAX_NON_AMBIENT_LIGHTS]; | 364 fLightColorUni[SkShadowShader::kMaxNonAmbientLights]; |
| 365 GrGLSLProgramDataManager::UniformHandle fDepthMapHeightUni[SK_MAX_NON_AM
BIENT_LIGHTS]; | 365 |
| 366 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; |
| 367 GrGLSLProgramDataManager::UniformHandle |
| 368 fDepthMapWidthUni[SkShadowShader::kMaxNonAmbientLights]; |
| 369 |
| 370 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
| 371 GrGLSLProgramDataManager::UniformHandle |
| 372 fDepthMapHeightUni[SkShadowShader::kMaxNonAmbientLights]; |
| 366 | 373 |
| 367 int fWidth; | 374 int fWidth; |
| 368 GrGLSLProgramDataManager::UniformHandle fWidthUni; | 375 GrGLSLProgramDataManager::UniformHandle fWidthUni; |
| 369 int fHeight; | 376 int fHeight; |
| 370 GrGLSLProgramDataManager::UniformHandle fHeightUni; | 377 GrGLSLProgramDataManager::UniformHandle fHeightUni; |
| 371 | 378 |
| 372 SkColor3f fAmbientColor; | 379 SkColor3f fAmbientColor; |
| 373 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; | 380 GrGLSLProgramDataManager::UniformHandle fAmbientColorUni; |
| 374 | 381 |
| 375 int fNumDirLights; | 382 int fNumDirLights; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 fDepthMapHeight[i] != shadowFP.fDepthMapHeight[i]) { | 436 fDepthMapHeight[i] != shadowFP.fDepthMapHeight[i]) { |
| 430 return false; | 437 return false; |
| 431 } | 438 } |
| 432 } | 439 } |
| 433 | 440 |
| 434 return true; | 441 return true; |
| 435 } | 442 } |
| 436 | 443 |
| 437 int fNumDirLights; | 444 int fNumDirLights; |
| 438 | 445 |
| 439 SkVector3 fLightDir[SK_MAX_NON_AMBIENT_LIGHTS]; | 446 SkVector3 fLightDir[SkShadowShader::kMaxNonAmbientLights]; |
| 440 SkColor3f fLightColor[SK_MAX_NON_AMBIENT_LIGHTS]; | 447 SkColor3f fLightColor[SkShadowShader::kMaxNonAmbientLights]; |
| 441 GrTextureAccess fDepthMapAccess[SK_MAX_NON_AMBIENT_LIGHTS]; | 448 GrTextureAccess fDepthMapAccess[SkShadowShader::kMaxNonAmbientLights]; |
| 442 sk_sp<GrTexture> fTexture[SK_MAX_NON_AMBIENT_LIGHTS]; | 449 sk_sp<GrTexture> fTexture[SkShadowShader::kMaxNonAmbientLights]; |
| 443 | 450 |
| 444 int fDepthMapWidth[SK_MAX_NON_AMBIENT_LIGHTS]; | 451 int fDepthMapWidth[SkShadowShader::kMaxNonAmbientLights]; |
| 445 int fDepthMapHeight[SK_MAX_NON_AMBIENT_LIGHTS]; | 452 int fDepthMapHeight[SkShadowShader::kMaxNonAmbientLights]; |
| 446 | 453 |
| 447 int fHeight; | 454 int fHeight; |
| 448 int fWidth; | 455 int fWidth; |
| 449 | 456 |
| 450 SkColor3f fAmbientColor; | 457 SkColor3f fAmbientColor; |
| 451 }; | 458 }; |
| 452 | 459 |
| 453 //////////////////////////////////////////////////////////////////////////// | 460 //////////////////////////////////////////////////////////////////////////// |
| 454 | 461 |
| 455 sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor(const AsFPArg
s& fpargs) const { | 462 sk_sp<GrFragmentProcessor> SkShadowShaderImpl::asFragmentProcessor(const AsFPArg
s& fpargs) const { |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 const SkLights::Light& light = fLights->light(l); | 647 const SkLights::Light& light = fLights->light(l); |
| 641 | 648 |
| 642 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); | 649 bool isAmbient = SkLights::Light::kAmbient_LightType == light.type(); |
| 643 | 650 |
| 644 buf.writeBool(isAmbient); | 651 buf.writeBool(isAmbient); |
| 645 buf.writeScalarArray(&light.color().fX, 3); | 652 buf.writeScalarArray(&light.color().fX, 3); |
| 646 if (!isAmbient) { | 653 if (!isAmbient) { |
| 647 buf.writeScalarArray(&light.dir().fX, 3); | 654 buf.writeScalarArray(&light.dir().fX, 3); |
| 648 } | 655 } |
| 649 | 656 |
| 650 buf.writeImage(light.getShadowMap().get()); | 657 buf.writeImage(light.getShadowMap()); |
| 651 } | 658 } |
| 652 | 659 |
| 653 buf.writeInt(fDiffuseWidth); | 660 buf.writeInt(fDiffuseWidth); |
| 654 buf.writeInt(fDiffuseHeight); | 661 buf.writeInt(fDiffuseHeight); |
| 655 | 662 |
| 656 buf.writeFlattenable(fPovDepthShader.get()); | 663 buf.writeFlattenable(fPovDepthShader.get()); |
| 657 buf.writeFlattenable(fDiffuseShader.get()); | 664 buf.writeFlattenable(fDiffuseShader.get()); |
| 658 } | 665 } |
| 659 | 666 |
| 660 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { | 667 size_t SkShadowShaderImpl::onContextSize(const ContextRec& rec) const { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 710 | 717 |
| 711 /////////////////////////////////////////////////////////////////////////////// | 718 /////////////////////////////////////////////////////////////////////////////// |
| 712 | 719 |
| 713 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) | 720 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkShadowShader) |
| 714 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) | 721 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkShadowShaderImpl) |
| 715 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 722 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 716 | 723 |
| 717 /////////////////////////////////////////////////////////////////////////////// | 724 /////////////////////////////////////////////////////////////////////////////// |
| 718 | 725 |
| 719 #endif | 726 #endif |
| OLD | NEW |