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