| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 The Android Open Source Project | 2 * Copyright 2012 The Android Open Source Project |
| 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 #include "SkLightingImageFilter.h" | 8 #include "SkLightingImageFilter.h" |
| 9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
| 10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
| 11 #include "SkReadBuffer.h" | 11 #include "SkReadBuffer.h" |
| 12 #include "SkWriteBuffer.h" | 12 #include "SkWriteBuffer.h" |
| 13 #include "SkReadBuffer.h" | 13 #include "SkReadBuffer.h" |
| 14 #include "SkWriteBuffer.h" | 14 #include "SkWriteBuffer.h" |
| 15 #include "SkTypes.h" | 15 #include "SkTypes.h" |
| 16 | 16 |
| 17 #if SK_SUPPORT_GPU | 17 #if SK_SUPPORT_GPU |
| 18 #include "effects/GrSingleTextureEffect.h" | 18 #include "effects/GrSingleTextureEffect.h" |
| 19 #include "gl/GrGLEffect.h" | 19 #include "gl/GrGLEffect.h" |
| 20 #include "gl/GrGLShaderBuilder.h" | 20 #include "gl/builders/GrGLProgramBuilder.h" |
| 21 #include "GrEffect.h" | 21 #include "GrEffect.h" |
| 22 #include "GrTBackendEffectFactory.h" | 22 #include "GrTBackendEffectFactory.h" |
| 23 | 23 |
| 24 class GrGLDiffuseLightingEffect; | 24 class GrGLDiffuseLightingEffect; |
| 25 class GrGLSpecularLightingEffect; | 25 class GrGLSpecularLightingEffect; |
| 26 | 26 |
| 27 // For brevity | 27 // For brevity |
| 28 typedef GrGLProgramDataManager::UniformHandle UniformHandle; | 28 typedef GrGLProgramDataManager::UniformHandle UniformHandle; |
| 29 #endif | 29 #endif |
| 30 | 30 |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 412 /////////////////////////////////////////////////////////////////////////////// | 412 /////////////////////////////////////////////////////////////////////////////// |
| 413 | 413 |
| 414 class GrGLLight { | 414 class GrGLLight { |
| 415 public: | 415 public: |
| 416 virtual ~GrGLLight() {} | 416 virtual ~GrGLLight() {} |
| 417 | 417 |
| 418 /** | 418 /** |
| 419 * This is called by GrGLLightingEffect::emitCode() before either of the two
virtual functions | 419 * This is called by GrGLLightingEffect::emitCode() before either of the two
virtual functions |
| 420 * below. It adds a vec3f uniform visible in the FS that represents the cons
tant light color. | 420 * below. It adds a vec3f uniform visible in the FS that represents the cons
tant light color. |
| 421 */ | 421 */ |
| 422 void emitLightColorUniform(GrGLShaderBuilder*); | 422 void emitLightColorUniform(GrGLProgramBuilder*); |
| 423 | 423 |
| 424 /** | 424 /** |
| 425 * These two functions are called from GrGLLightingEffect's emitCode() funct
ion. | 425 * These two functions are called from GrGLLightingEffect's emitCode() funct
ion. |
| 426 * emitSurfaceToLight places an expression in param out that is the vector f
rom the surface to | 426 * emitSurfaceToLight places an expression in param out that is the vector f
rom the surface to |
| 427 * the light. The expression will be used in the FS. emitLightColor writes a
n expression into | 427 * the light. The expression will be used in the FS. emitLightColor writes a
n expression into |
| 428 * the FS that is the color of the light. Either function may add functions
and/or uniforms to | 428 * the FS that is the color of the light. Either function may add functions
and/or uniforms to |
| 429 * the FS. The default of emitLightColor appends the name of the constant li
ght color uniform | 429 * the FS. The default of emitLightColor appends the name of the constant li
ght color uniform |
| 430 * and so this function only needs to be overridden if the light color varie
s spatially. | 430 * and so this function only needs to be overridden if the light color varie
s spatially. |
| 431 */ | 431 */ |
| 432 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) = 0; | 432 virtual void emitSurfaceToLight(GrGLProgramBuilder*, const char* z) = 0; |
| 433 virtual void emitLightColor(GrGLShaderBuilder*, const char *surfaceToLight); | 433 virtual void emitLightColor(GrGLProgramBuilder*, const char *surfaceToLight)
; |
| 434 | 434 |
| 435 // This is called from GrGLLightingEffect's setData(). Subclasses of GrGLLig
ht must call | 435 // This is called from GrGLLightingEffect's setData(). Subclasses of GrGLLig
ht must call |
| 436 // INHERITED::setData(). | 436 // INHERITED::setData(). |
| 437 virtual void setData(const GrGLProgramDataManager&, | 437 virtual void setData(const GrGLProgramDataManager&, |
| 438 const SkLight* light) const; | 438 const SkLight* light) const; |
| 439 | 439 |
| 440 protected: | 440 protected: |
| 441 /** | 441 /** |
| 442 * Gets the constant light color uniform. Subclasses can use this in their e
mitLightColor | 442 * Gets the constant light color uniform. Subclasses can use this in their e
mitLightColor |
| 443 * function. | 443 * function. |
| 444 */ | 444 */ |
| 445 UniformHandle lightColorUni() const { return fColorUni; } | 445 UniformHandle lightColorUni() const { return fColorUni; } |
| 446 | 446 |
| 447 private: | 447 private: |
| 448 UniformHandle fColorUni; | 448 UniformHandle fColorUni; |
| 449 | 449 |
| 450 typedef SkRefCnt INHERITED; | 450 typedef SkRefCnt INHERITED; |
| 451 }; | 451 }; |
| 452 | 452 |
| 453 /////////////////////////////////////////////////////////////////////////////// | 453 /////////////////////////////////////////////////////////////////////////////// |
| 454 | 454 |
| 455 class GrGLDistantLight : public GrGLLight { | 455 class GrGLDistantLight : public GrGLLight { |
| 456 public: | 456 public: |
| 457 virtual ~GrGLDistantLight() {} | 457 virtual ~GrGLDistantLight() {} |
| 458 virtual void setData(const GrGLProgramDataManager&, | 458 virtual void setData(const GrGLProgramDataManager&, |
| 459 const SkLight* light) const SK_OVERRIDE; | 459 const SkLight* light) const SK_OVERRIDE; |
| 460 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) SK_OVERRI
DE; | 460 virtual void emitSurfaceToLight(GrGLProgramBuilder*, const char* z) SK_OVERR
IDE; |
| 461 | 461 |
| 462 private: | 462 private: |
| 463 typedef GrGLLight INHERITED; | 463 typedef GrGLLight INHERITED; |
| 464 UniformHandle fDirectionUni; | 464 UniformHandle fDirectionUni; |
| 465 }; | 465 }; |
| 466 | 466 |
| 467 /////////////////////////////////////////////////////////////////////////////// | 467 /////////////////////////////////////////////////////////////////////////////// |
| 468 | 468 |
| 469 class GrGLPointLight : public GrGLLight { | 469 class GrGLPointLight : public GrGLLight { |
| 470 public: | 470 public: |
| 471 virtual ~GrGLPointLight() {} | 471 virtual ~GrGLPointLight() {} |
| 472 virtual void setData(const GrGLProgramDataManager&, | 472 virtual void setData(const GrGLProgramDataManager&, |
| 473 const SkLight* light) const SK_OVERRIDE; | 473 const SkLight* light) const SK_OVERRIDE; |
| 474 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) SK_OVERRI
DE; | 474 virtual void emitSurfaceToLight(GrGLProgramBuilder*, const char* z) SK_OVERR
IDE; |
| 475 | 475 |
| 476 private: | 476 private: |
| 477 typedef GrGLLight INHERITED; | 477 typedef GrGLLight INHERITED; |
| 478 UniformHandle fLocationUni; | 478 UniformHandle fLocationUni; |
| 479 }; | 479 }; |
| 480 | 480 |
| 481 /////////////////////////////////////////////////////////////////////////////// | 481 /////////////////////////////////////////////////////////////////////////////// |
| 482 | 482 |
| 483 class GrGLSpotLight : public GrGLLight { | 483 class GrGLSpotLight : public GrGLLight { |
| 484 public: | 484 public: |
| 485 virtual ~GrGLSpotLight() {} | 485 virtual ~GrGLSpotLight() {} |
| 486 virtual void setData(const GrGLProgramDataManager&, | 486 virtual void setData(const GrGLProgramDataManager&, |
| 487 const SkLight* light) const SK_OVERRIDE; | 487 const SkLight* light) const SK_OVERRIDE; |
| 488 virtual void emitSurfaceToLight(GrGLShaderBuilder*, const char* z) SK_OVERRI
DE; | 488 virtual void emitSurfaceToLight(GrGLProgramBuilder*, const char* z) SK_OVERR
IDE; |
| 489 virtual void emitLightColor(GrGLShaderBuilder*, const char *surfaceToLight)
SK_OVERRIDE; | 489 virtual void emitLightColor(GrGLProgramBuilder*, const char *surfaceToLight)
SK_OVERRIDE; |
| 490 | 490 |
| 491 private: | 491 private: |
| 492 typedef GrGLLight INHERITED; | 492 typedef GrGLLight INHERITED; |
| 493 | 493 |
| 494 SkString fLightColorFunc; | 494 SkString fLightColorFunc; |
| 495 UniformHandle fLocationUni; | 495 UniformHandle fLocationUni; |
| 496 UniformHandle fExponentUni; | 496 UniformHandle fExponentUni; |
| 497 UniformHandle fCosOuterConeAngleUni; | 497 UniformHandle fCosOuterConeAngleUni; |
| 498 UniformHandle fCosInnerConeAngleUni; | 498 UniformHandle fCosInnerConeAngleUni; |
| 499 UniformHandle fConeScaleUni; | 499 UniformHandle fConeScaleUni; |
| (...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1122 } | 1122 } |
| 1123 | 1123 |
| 1124 } | 1124 } |
| 1125 | 1125 |
| 1126 class GrGLLightingEffect : public GrGLEffect { | 1126 class GrGLLightingEffect : public GrGLEffect { |
| 1127 public: | 1127 public: |
| 1128 GrGLLightingEffect(const GrBackendEffectFactory& factory, | 1128 GrGLLightingEffect(const GrBackendEffectFactory& factory, |
| 1129 const GrDrawEffect& effect); | 1129 const GrDrawEffect& effect); |
| 1130 virtual ~GrGLLightingEffect(); | 1130 virtual ~GrGLLightingEffect(); |
| 1131 | 1131 |
| 1132 virtual void emitCode(GrGLShaderBuilder*, | 1132 virtual void emitCode(GrGLProgramBuilder*, |
| 1133 const GrDrawEffect&, | 1133 const GrDrawEffect&, |
| 1134 const GrEffectKey&, | 1134 const GrEffectKey&, |
| 1135 const char* outputColor, | 1135 const char* outputColor, |
| 1136 const char* inputColor, | 1136 const char* inputColor, |
| 1137 const TransformedCoordsArray&, | 1137 const TransformedCoordsArray&, |
| 1138 const TextureSamplerArray&) SK_OVERRIDE; | 1138 const TextureSamplerArray&) SK_OVERRIDE; |
| 1139 | 1139 |
| 1140 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyB
uilder* b); | 1140 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyB
uilder* b); |
| 1141 | 1141 |
| 1142 /** | 1142 /** |
| 1143 * Subclasses of GrGLLightingEffect must call INHERITED::setData(); | 1143 * Subclasses of GrGLLightingEffect must call INHERITED::setData(); |
| 1144 */ | 1144 */ |
| 1145 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; | 1145 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; |
| 1146 | 1146 |
| 1147 protected: | 1147 protected: |
| 1148 virtual void emitLightFunc(GrGLShaderBuilder*, SkString* funcName) = 0; | 1148 virtual void emitLightFunc(GrGLProgramBuilder*, SkString* funcName) = 0; |
| 1149 | 1149 |
| 1150 private: | 1150 private: |
| 1151 typedef GrGLEffect INHERITED; | 1151 typedef GrGLEffect INHERITED; |
| 1152 | 1152 |
| 1153 UniformHandle fImageIncrementUni; | 1153 UniformHandle fImageIncrementUni; |
| 1154 UniformHandle fSurfaceScaleUni; | 1154 UniformHandle fSurfaceScaleUni; |
| 1155 GrGLLight* fLight; | 1155 GrGLLight* fLight; |
| 1156 }; | 1156 }; |
| 1157 | 1157 |
| 1158 /////////////////////////////////////////////////////////////////////////////// | 1158 /////////////////////////////////////////////////////////////////////////////// |
| 1159 | 1159 |
| 1160 class GrGLDiffuseLightingEffect : public GrGLLightingEffect { | 1160 class GrGLDiffuseLightingEffect : public GrGLLightingEffect { |
| 1161 public: | 1161 public: |
| 1162 GrGLDiffuseLightingEffect(const GrBackendEffectFactory& factory, | 1162 GrGLDiffuseLightingEffect(const GrBackendEffectFactory& factory, |
| 1163 const GrDrawEffect& drawEffect); | 1163 const GrDrawEffect& drawEffect); |
| 1164 virtual void emitLightFunc(GrGLShaderBuilder*, SkString* funcName) SK_OVERRI
DE; | 1164 virtual void emitLightFunc(GrGLProgramBuilder*, SkString* funcName) SK_OVERR
IDE; |
| 1165 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; | 1165 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; |
| 1166 | 1166 |
| 1167 private: | 1167 private: |
| 1168 typedef GrGLLightingEffect INHERITED; | 1168 typedef GrGLLightingEffect INHERITED; |
| 1169 | 1169 |
| 1170 UniformHandle fKDUni; | 1170 UniformHandle fKDUni; |
| 1171 }; | 1171 }; |
| 1172 | 1172 |
| 1173 /////////////////////////////////////////////////////////////////////////////// | 1173 /////////////////////////////////////////////////////////////////////////////// |
| 1174 | 1174 |
| 1175 class GrGLSpecularLightingEffect : public GrGLLightingEffect { | 1175 class GrGLSpecularLightingEffect : public GrGLLightingEffect { |
| 1176 public: | 1176 public: |
| 1177 GrGLSpecularLightingEffect(const GrBackendEffectFactory& factory, | 1177 GrGLSpecularLightingEffect(const GrBackendEffectFactory& factory, |
| 1178 const GrDrawEffect& effect); | 1178 const GrDrawEffect& effect); |
| 1179 virtual void emitLightFunc(GrGLShaderBuilder*, SkString* funcName) SK_OVERRI
DE; | 1179 virtual void emitLightFunc(GrGLProgramBuilder*, SkString* funcName) SK_OVERR
IDE; |
| 1180 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; | 1180 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_
OVERRIDE; |
| 1181 | 1181 |
| 1182 private: | 1182 private: |
| 1183 typedef GrGLLightingEffect INHERITED; | 1183 typedef GrGLLightingEffect INHERITED; |
| 1184 | 1184 |
| 1185 UniformHandle fKSUni; | 1185 UniformHandle fKSUni; |
| 1186 UniformHandle fShininessUni; | 1186 UniformHandle fShininessUni; |
| 1187 }; | 1187 }; |
| 1188 | 1188 |
| 1189 /////////////////////////////////////////////////////////////////////////////// | 1189 /////////////////////////////////////////////////////////////////////////////// |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1257 const GrDrawEffect& drawEffect) | 1257 const GrDrawEffect& drawEffect) |
| 1258 : INHERITED(factory) { | 1258 : INHERITED(factory) { |
| 1259 const GrLightingEffect& m = drawEffect.castEffect<GrLightingEffect>(); | 1259 const GrLightingEffect& m = drawEffect.castEffect<GrLightingEffect>(); |
| 1260 fLight = m.light()->createGLLight(); | 1260 fLight = m.light()->createGLLight(); |
| 1261 } | 1261 } |
| 1262 | 1262 |
| 1263 GrGLLightingEffect::~GrGLLightingEffect() { | 1263 GrGLLightingEffect::~GrGLLightingEffect() { |
| 1264 delete fLight; | 1264 delete fLight; |
| 1265 } | 1265 } |
| 1266 | 1266 |
| 1267 void GrGLLightingEffect::emitCode(GrGLShaderBuilder* builder, | 1267 void GrGLLightingEffect::emitCode(GrGLProgramBuilder* builder, |
| 1268 const GrDrawEffect&, | 1268 const GrDrawEffect&, |
| 1269 const GrEffectKey& key, | 1269 const GrEffectKey& key, |
| 1270 const char* outputColor, | 1270 const char* outputColor, |
| 1271 const char* inputColor, | 1271 const char* inputColor, |
| 1272 const TransformedCoordsArray& coords, | 1272 const TransformedCoordsArray& coords, |
| 1273 const TextureSamplerArray& samplers) { | 1273 const TextureSamplerArray& samplers) { |
| 1274 SkString coords2D = builder->ensureFSCoords2D(coords, 0); | 1274 fImageIncrementUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visib
ility, |
| 1275 | |
| 1276 fImageIncrementUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibi
lity, | |
| 1277 kVec2f_GrSLType, | 1275 kVec2f_GrSLType, |
| 1278 "ImageIncrement"); | 1276 "ImageIncrement"); |
| 1279 fSurfaceScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibili
ty, | 1277 fSurfaceScaleUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibil
ity, |
| 1280 kFloat_GrSLType, | 1278 kFloat_GrSLType, |
| 1281 "SurfaceScale"); | 1279 "SurfaceScale"); |
| 1282 fLight->emitLightColorUniform(builder); | 1280 fLight->emitLightColorUniform(builder); |
| 1283 SkString lightFunc; | 1281 SkString lightFunc; |
| 1284 this->emitLightFunc(builder, &lightFunc); | 1282 this->emitLightFunc(builder, &lightFunc); |
| 1285 static const GrGLShaderVar gSobelArgs[] = { | 1283 static const GrGLShaderVar gSobelArgs[] = { |
| 1286 GrGLShaderVar("a", kFloat_GrSLType), | 1284 GrGLShaderVar("a", kFloat_GrSLType), |
| 1287 GrGLShaderVar("b", kFloat_GrSLType), | 1285 GrGLShaderVar("b", kFloat_GrSLType), |
| 1288 GrGLShaderVar("c", kFloat_GrSLType), | 1286 GrGLShaderVar("c", kFloat_GrSLType), |
| 1289 GrGLShaderVar("d", kFloat_GrSLType), | 1287 GrGLShaderVar("d", kFloat_GrSLType), |
| 1290 GrGLShaderVar("e", kFloat_GrSLType), | 1288 GrGLShaderVar("e", kFloat_GrSLType), |
| 1291 GrGLShaderVar("f", kFloat_GrSLType), | 1289 GrGLShaderVar("f", kFloat_GrSLType), |
| 1292 GrGLShaderVar("scale", kFloat_GrSLType), | 1290 GrGLShaderVar("scale", kFloat_GrSLType), |
| 1293 }; | 1291 }; |
| 1294 SkString sobelFuncName; | 1292 SkString sobelFuncName; |
| 1295 builder->fsEmitFunction(kFloat_GrSLType, | 1293 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 1294 SkString coords2D = fsBuilder->ensureFSCoords2D(coords, 0); |
| 1295 |
| 1296 fsBuilder->emitFunction(kFloat_GrSLType, |
| 1296 "sobel", | 1297 "sobel", |
| 1297 SK_ARRAY_COUNT(gSobelArgs), | 1298 SK_ARRAY_COUNT(gSobelArgs), |
| 1298 gSobelArgs, | 1299 gSobelArgs, |
| 1299 "\treturn (-a + b - 2.0 * c + 2.0 * d -e + f) * scal
e;\n", | 1300 "\treturn (-a + b - 2.0 * c + 2.0 * d -e + f) * scal
e;\n", |
| 1300 &sobelFuncName); | 1301 &sobelFuncName); |
| 1301 static const GrGLShaderVar gPointToNormalArgs[] = { | 1302 static const GrGLShaderVar gPointToNormalArgs[] = { |
| 1302 GrGLShaderVar("x", kFloat_GrSLType), | 1303 GrGLShaderVar("x", kFloat_GrSLType), |
| 1303 GrGLShaderVar("y", kFloat_GrSLType), | 1304 GrGLShaderVar("y", kFloat_GrSLType), |
| 1304 GrGLShaderVar("scale", kFloat_GrSLType), | 1305 GrGLShaderVar("scale", kFloat_GrSLType), |
| 1305 }; | 1306 }; |
| 1306 SkString pointToNormalName; | 1307 SkString pointToNormalName; |
| 1307 builder->fsEmitFunction(kVec3f_GrSLType, | 1308 fsBuilder->emitFunction(kVec3f_GrSLType, |
| 1308 "pointToNormal", | 1309 "pointToNormal", |
| 1309 SK_ARRAY_COUNT(gPointToNormalArgs), | 1310 SK_ARRAY_COUNT(gPointToNormalArgs), |
| 1310 gPointToNormalArgs, | 1311 gPointToNormalArgs, |
| 1311 "\treturn normalize(vec3(-x * scale, y * scale, 1));
\n", | 1312 "\treturn normalize(vec3(-x * scale, y * scale, 1));
\n", |
| 1312 &pointToNormalName); | 1313 &pointToNormalName); |
| 1313 | 1314 |
| 1314 static const GrGLShaderVar gInteriorNormalArgs[] = { | 1315 static const GrGLShaderVar gInteriorNormalArgs[] = { |
| 1315 GrGLShaderVar("m", kFloat_GrSLType, 9), | 1316 GrGLShaderVar("m", kFloat_GrSLType, 9), |
| 1316 GrGLShaderVar("surfaceScale", kFloat_GrSLType), | 1317 GrGLShaderVar("surfaceScale", kFloat_GrSLType), |
| 1317 }; | 1318 }; |
| 1318 SkString interiorNormalBody; | 1319 SkString interiorNormalBody; |
| 1319 interiorNormalBody.appendf("\treturn %s(%s(m[0], m[2], m[3], m[5], m[6], m[8
], 0.25),\n" | 1320 interiorNormalBody.appendf("\treturn %s(%s(m[0], m[2], m[3], m[5], m[6], m[8
], 0.25),\n" |
| 1320 "\t %s(m[0], m[6], m[1], m[7], m[2], m[8],
0.25),\n" | 1321 "\t %s(m[0], m[6], m[1], m[7], m[2], m[8],
0.25),\n" |
| 1321 "\t surfaceScale);\n", | 1322 "\t surfaceScale);\n", |
| 1322 pointToNormalName.c_str(), | 1323 pointToNormalName.c_str(), |
| 1323 sobelFuncName.c_str(), | 1324 sobelFuncName.c_str(), |
| 1324 sobelFuncName.c_str()); | 1325 sobelFuncName.c_str()); |
| 1325 SkString interiorNormalName; | 1326 SkString interiorNormalName; |
| 1326 builder->fsEmitFunction(kVec3f_GrSLType, | 1327 fsBuilder->emitFunction(kVec3f_GrSLType, |
| 1327 "interiorNormal", | 1328 "interiorNormal", |
| 1328 SK_ARRAY_COUNT(gInteriorNormalArgs), | 1329 SK_ARRAY_COUNT(gInteriorNormalArgs), |
| 1329 gInteriorNormalArgs, | 1330 gInteriorNormalArgs, |
| 1330 interiorNormalBody.c_str(), | 1331 interiorNormalBody.c_str(), |
| 1331 &interiorNormalName); | 1332 &interiorNormalName); |
| 1332 | 1333 |
| 1333 builder->fsCodeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); | 1334 fsBuilder->codeAppendf("\t\tvec2 coord = %s;\n", coords2D.c_str()); |
| 1334 builder->fsCodeAppend("\t\tfloat m[9];\n"); | 1335 fsBuilder->codeAppend("\t\tfloat m[9];\n"); |
| 1335 | 1336 |
| 1336 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); | 1337 const char* imgInc = builder->getUniformCStr(fImageIncrementUni); |
| 1337 const char* surfScale = builder->getUniformCStr(fSurfaceScaleUni); | 1338 const char* surfScale = builder->getUniformCStr(fSurfaceScaleUni); |
| 1338 | 1339 |
| 1339 int index = 0; | 1340 int index = 0; |
| 1340 for (int dy = -1; dy <= 1; dy++) { | 1341 for (int dy = -1; dy <= 1; dy++) { |
| 1341 for (int dx = -1; dx <= 1; dx++) { | 1342 for (int dx = -1; dx <= 1; dx++) { |
| 1342 SkString texCoords; | 1343 SkString texCoords; |
| 1343 texCoords.appendf("coord + vec2(%d, %d) * %s", dx, dy, imgInc); | 1344 texCoords.appendf("coord + vec2(%d, %d) * %s", dx, dy, imgInc); |
| 1344 builder->fsCodeAppendf("\t\tm[%d] = ", index++); | 1345 fsBuilder->codeAppendf("\t\tm[%d] = ", index++); |
| 1345 builder->fsAppendTextureLookup(samplers[0], texCoords.c_str()); | 1346 fsBuilder->appendTextureLookup(samplers[0], texCoords.c_str()); |
| 1346 builder->fsCodeAppend(".a;\n"); | 1347 fsBuilder->codeAppend(".a;\n"); |
| 1347 } | 1348 } |
| 1348 } | 1349 } |
| 1349 builder->fsCodeAppend("\t\tvec3 surfaceToLight = "); | 1350 fsBuilder->codeAppend("\t\tvec3 surfaceToLight = "); |
| 1350 SkString arg; | 1351 SkString arg; |
| 1351 arg.appendf("%s * m[4]", surfScale); | 1352 arg.appendf("%s * m[4]", surfScale); |
| 1352 fLight->emitSurfaceToLight(builder, arg.c_str()); | 1353 fLight->emitSurfaceToLight(builder, arg.c_str()); |
| 1353 builder->fsCodeAppend(";\n"); | 1354 fsBuilder->codeAppend(";\n"); |
| 1354 builder->fsCodeAppendf("\t\t%s = %s(%s(m, %s), surfaceToLight, ", | 1355 fsBuilder->codeAppendf("\t\t%s = %s(%s(m, %s), surfaceToLight, ", |
| 1355 outputColor, lightFunc.c_str(), interiorNormalName.c_
str(), surfScale); | 1356 outputColor, lightFunc.c_str(), interiorNormalName.c_
str(), surfScale); |
| 1356 fLight->emitLightColor(builder, "surfaceToLight"); | 1357 fLight->emitLightColor(builder, "surfaceToLight"); |
| 1357 builder->fsCodeAppend(");\n"); | 1358 fsBuilder->codeAppend(");\n"); |
| 1358 SkString modulate; | 1359 SkString modulate; |
| 1359 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); | 1360 GrGLSLMulVarBy4f(&modulate, 2, outputColor, inputColor); |
| 1360 builder->fsCodeAppend(modulate.c_str()); | 1361 fsBuilder->codeAppend(modulate.c_str()); |
| 1361 } | 1362 } |
| 1362 | 1363 |
| 1363 void GrGLLightingEffect::GenKey(const GrDrawEffect& drawEffect, | 1364 void GrGLLightingEffect::GenKey(const GrDrawEffect& drawEffect, |
| 1364 const GrGLCaps& caps, GrEffectKeyBuilder* b) { | 1365 const GrGLCaps& caps, GrEffectKeyBuilder* b) { |
| 1365 b->add32(drawEffect.castEffect<GrLightingEffect>().light()->type()); | 1366 b->add32(drawEffect.castEffect<GrLightingEffect>().light()->type()); |
| 1366 } | 1367 } |
| 1367 | 1368 |
| 1368 void GrGLLightingEffect::setData(const GrGLProgramDataManager& pdman, | 1369 void GrGLLightingEffect::setData(const GrGLProgramDataManager& pdman, |
| 1369 const GrDrawEffect& drawEffect) { | 1370 const GrDrawEffect& drawEffect) { |
| 1370 const GrLightingEffect& lighting = drawEffect.castEffect<GrLightingEffect>()
; | 1371 const GrLightingEffect& lighting = drawEffect.castEffect<GrLightingEffect>()
; |
| 1371 GrTexture* texture = lighting.texture(0); | 1372 GrTexture* texture = lighting.texture(0); |
| 1372 float ySign = texture->origin() == kTopLeft_GrSurfaceOrigin ? -1.0f : 1.0f; | 1373 float ySign = texture->origin() == kTopLeft_GrSurfaceOrigin ? -1.0f : 1.0f; |
| 1373 pdman.set2f(fImageIncrementUni, 1.0f / texture->width(), ySign / texture->he
ight()); | 1374 pdman.set2f(fImageIncrementUni, 1.0f / texture->width(), ySign / texture->he
ight()); |
| 1374 pdman.set1f(fSurfaceScaleUni, lighting.surfaceScale()); | 1375 pdman.set1f(fSurfaceScaleUni, lighting.surfaceScale()); |
| 1375 SkAutoTUnref<SkLight> transformedLight(lighting.light()->transform(lighting.
filterMatrix())); | 1376 SkAutoTUnref<SkLight> transformedLight(lighting.light()->transform(lighting.
filterMatrix())); |
| 1376 fLight->setData(pdman, transformedLight); | 1377 fLight->setData(pdman, transformedLight); |
| 1377 } | 1378 } |
| 1378 | 1379 |
| 1379 /////////////////////////////////////////////////////////////////////////////// | 1380 /////////////////////////////////////////////////////////////////////////////// |
| 1380 | 1381 |
| 1381 /////////////////////////////////////////////////////////////////////////////// | 1382 /////////////////////////////////////////////////////////////////////////////// |
| 1382 | 1383 |
| 1383 GrGLDiffuseLightingEffect::GrGLDiffuseLightingEffect(const GrBackendEffectFactor
y& factory, | 1384 GrGLDiffuseLightingEffect::GrGLDiffuseLightingEffect(const GrBackendEffectFactor
y& factory, |
| 1384 const GrDrawEffect& drawEff
ect) | 1385 const GrDrawEffect& drawEff
ect) |
| 1385 : INHERITED(factory, drawEffect) { | 1386 : INHERITED(factory, drawEffect) { |
| 1386 } | 1387 } |
| 1387 | 1388 |
| 1388 void GrGLDiffuseLightingEffect::emitLightFunc(GrGLShaderBuilder* builder, SkStri
ng* funcName) { | 1389 void GrGLDiffuseLightingEffect::emitLightFunc(GrGLProgramBuilder* builder, SkStr
ing* funcName) { |
| 1389 const char* kd; | 1390 const char* kd; |
| 1390 fKDUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 1391 fKDUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 1391 kFloat_GrSLType, | 1392 kFloat_GrSLType, |
| 1392 "KD", | 1393 "KD", |
| 1393 &kd); | 1394 &kd); |
| 1394 | 1395 |
| 1395 static const GrGLShaderVar gLightArgs[] = { | 1396 static const GrGLShaderVar gLightArgs[] = { |
| 1396 GrGLShaderVar("normal", kVec3f_GrSLType), | 1397 GrGLShaderVar("normal", kVec3f_GrSLType), |
| 1397 GrGLShaderVar("surfaceToLight", kVec3f_GrSLType), | 1398 GrGLShaderVar("surfaceToLight", kVec3f_GrSLType), |
| 1398 GrGLShaderVar("lightColor", kVec3f_GrSLType) | 1399 GrGLShaderVar("lightColor", kVec3f_GrSLType) |
| 1399 }; | 1400 }; |
| 1400 SkString lightBody; | 1401 SkString lightBody; |
| 1401 lightBody.appendf("\tfloat colorScale = %s * dot(normal, surfaceToLight);\n"
, kd); | 1402 lightBody.appendf("\tfloat colorScale = %s * dot(normal, surfaceToLight);\n"
, kd); |
| 1402 lightBody.appendf("\treturn vec4(lightColor * clamp(colorScale, 0.0, 1.0), 1
.0);\n"); | 1403 lightBody.appendf("\treturn vec4(lightColor * clamp(colorScale, 0.0, 1.0), 1
.0);\n"); |
| 1403 builder->fsEmitFunction(kVec4f_GrSLType, | 1404 builder->getFragmentShaderBuilder()->emitFunction(kVec4f_GrSLType, |
| 1404 "light", | 1405 "light", |
| 1405 SK_ARRAY_COUNT(gLightArgs), | 1406 SK_ARRAY_COUNT(gLightArgs)
, |
| 1406 gLightArgs, | 1407 gLightArgs, |
| 1407 lightBody.c_str(), | 1408 lightBody.c_str(), |
| 1408 funcName); | 1409 funcName); |
| 1409 } | 1410 } |
| 1410 | 1411 |
| 1411 void GrGLDiffuseLightingEffect::setData(const GrGLProgramDataManager& pdman, | 1412 void GrGLDiffuseLightingEffect::setData(const GrGLProgramDataManager& pdman, |
| 1412 const GrDrawEffect& drawEffect) { | 1413 const GrDrawEffect& drawEffect) { |
| 1413 INHERITED::setData(pdman, drawEffect); | 1414 INHERITED::setData(pdman, drawEffect); |
| 1414 const GrDiffuseLightingEffect& diffuse = drawEffect.castEffect<GrDiffuseLigh
tingEffect>(); | 1415 const GrDiffuseLightingEffect& diffuse = drawEffect.castEffect<GrDiffuseLigh
tingEffect>(); |
| 1415 pdman.set1f(fKDUni, diffuse.kd()); | 1416 pdman.set1f(fKDUni, diffuse.kd()); |
| 1416 } | 1417 } |
| 1417 | 1418 |
| 1418 /////////////////////////////////////////////////////////////////////////////// | 1419 /////////////////////////////////////////////////////////////////////////////// |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1457 light, surfaceScale, matrix, ks, shi
niness); | 1458 light, surfaceScale, matrix, ks, shi
niness); |
| 1458 } | 1459 } |
| 1459 | 1460 |
| 1460 /////////////////////////////////////////////////////////////////////////////// | 1461 /////////////////////////////////////////////////////////////////////////////// |
| 1461 | 1462 |
| 1462 GrGLSpecularLightingEffect::GrGLSpecularLightingEffect(const GrBackendEffectFact
ory& factory, | 1463 GrGLSpecularLightingEffect::GrGLSpecularLightingEffect(const GrBackendEffectFact
ory& factory, |
| 1463 const GrDrawEffect& drawE
ffect) | 1464 const GrDrawEffect& drawE
ffect) |
| 1464 : INHERITED(factory, drawEffect) { | 1465 : INHERITED(factory, drawEffect) { |
| 1465 } | 1466 } |
| 1466 | 1467 |
| 1467 void GrGLSpecularLightingEffect::emitLightFunc(GrGLShaderBuilder* builder, SkStr
ing* funcName) { | 1468 void GrGLSpecularLightingEffect::emitLightFunc(GrGLProgramBuilder* builder, SkSt
ring* funcName) { |
| 1468 const char* ks; | 1469 const char* ks; |
| 1469 const char* shininess; | 1470 const char* shininess; |
| 1470 | 1471 |
| 1471 fKSUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 1472 fKSUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 1472 kFloat_GrSLType, "KS", &ks); | 1473 kFloat_GrSLType, "KS", &ks); |
| 1473 fShininessUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 1474 fShininessUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility
, |
| 1474 kFloat_GrSLType, "Shininess", &shininess
); | 1475 kFloat_GrSLType, "Shininess", &shininess
); |
| 1475 | 1476 |
| 1476 static const GrGLShaderVar gLightArgs[] = { | 1477 static const GrGLShaderVar gLightArgs[] = { |
| 1477 GrGLShaderVar("normal", kVec3f_GrSLType), | 1478 GrGLShaderVar("normal", kVec3f_GrSLType), |
| 1478 GrGLShaderVar("surfaceToLight", kVec3f_GrSLType), | 1479 GrGLShaderVar("surfaceToLight", kVec3f_GrSLType), |
| 1479 GrGLShaderVar("lightColor", kVec3f_GrSLType) | 1480 GrGLShaderVar("lightColor", kVec3f_GrSLType) |
| 1480 }; | 1481 }; |
| 1481 SkString lightBody; | 1482 SkString lightBody; |
| 1482 lightBody.appendf("\tvec3 halfDir = vec3(normalize(surfaceToLight + vec3(0,
0, 1)));\n"); | 1483 lightBody.appendf("\tvec3 halfDir = vec3(normalize(surfaceToLight + vec3(0,
0, 1)));\n"); |
| 1483 lightBody.appendf("\tfloat colorScale = %s * pow(dot(normal, halfDir), %s);\
n", ks, shininess); | 1484 lightBody.appendf("\tfloat colorScale = %s * pow(dot(normal, halfDir), %s);\
n", ks, shininess); |
| 1484 lightBody.appendf("\tvec3 color = lightColor * clamp(colorScale, 0.0, 1.0);\
n"); | 1485 lightBody.appendf("\tvec3 color = lightColor * clamp(colorScale, 0.0, 1.0);\
n"); |
| 1485 lightBody.appendf("\treturn vec4(color, max(max(color.r, color.g), color.b))
;\n"); | 1486 lightBody.appendf("\treturn vec4(color, max(max(color.r, color.g), color.b))
;\n"); |
| 1486 builder->fsEmitFunction(kVec4f_GrSLType, | 1487 builder->getFragmentShaderBuilder()->emitFunction(kVec4f_GrSLType, |
| 1487 "light", | 1488 "light", |
| 1488 SK_ARRAY_COUNT(gLightArgs), | 1489 SK_ARRAY_COUNT(gLightArgs)
, |
| 1489 gLightArgs, | 1490 gLightArgs, |
| 1490 lightBody.c_str(), | 1491 lightBody.c_str(), |
| 1491 funcName); | 1492 funcName); |
| 1492 } | 1493 } |
| 1493 | 1494 |
| 1494 void GrGLSpecularLightingEffect::setData(const GrGLProgramDataManager& pdman, | 1495 void GrGLSpecularLightingEffect::setData(const GrGLProgramDataManager& pdman, |
| 1495 const GrDrawEffect& drawEffect) { | 1496 const GrDrawEffect& drawEffect) { |
| 1496 INHERITED::setData(pdman, drawEffect); | 1497 INHERITED::setData(pdman, drawEffect); |
| 1497 const GrSpecularLightingEffect& spec = drawEffect.castEffect<GrSpecularLight
ingEffect>(); | 1498 const GrSpecularLightingEffect& spec = drawEffect.castEffect<GrSpecularLight
ingEffect>(); |
| 1498 pdman.set1f(fKSUni, spec.ks()); | 1499 pdman.set1f(fKSUni, spec.ks()); |
| 1499 pdman.set1f(fShininessUni, spec.shininess()); | 1500 pdman.set1f(fShininessUni, spec.shininess()); |
| 1500 } | 1501 } |
| 1501 | 1502 |
| 1502 /////////////////////////////////////////////////////////////////////////////// | 1503 /////////////////////////////////////////////////////////////////////////////// |
| 1503 void GrGLLight::emitLightColorUniform(GrGLShaderBuilder* builder) { | 1504 void GrGLLight::emitLightColorUniform(GrGLProgramBuilder* builder) { |
| 1504 fColorUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 1505 fColorUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 1505 kVec3f_GrSLType, "LightColor"); | 1506 kVec3f_GrSLType, "LightColor"); |
| 1506 } | 1507 } |
| 1507 | 1508 |
| 1508 void GrGLLight::emitLightColor(GrGLShaderBuilder* builder, | 1509 void GrGLLight::emitLightColor(GrGLProgramBuilder* builder, |
| 1509 const char *surfaceToLight) { | 1510 const char *surfaceToLight) { |
| 1510 builder->fsCodeAppend(builder->getUniformCStr(this->lightColorUni())); | 1511 builder->getFragmentShaderBuilder()-> |
| 1512 codeAppend(builder->getUniformCStr(this->lightColorUni())); |
| 1511 } | 1513 } |
| 1512 | 1514 |
| 1513 void GrGLLight::setData(const GrGLProgramDataManager& pdman, | 1515 void GrGLLight::setData(const GrGLProgramDataManager& pdman, |
| 1514 const SkLight* light) const { | 1516 const SkLight* light) const { |
| 1515 setUniformPoint3(pdman, fColorUni, light->color() * SkScalarInvert(SkIntToSc
alar(255))); | 1517 setUniformPoint3(pdman, fColorUni, light->color() * SkScalarInvert(SkIntToSc
alar(255))); |
| 1516 } | 1518 } |
| 1517 | 1519 |
| 1518 /////////////////////////////////////////////////////////////////////////////// | 1520 /////////////////////////////////////////////////////////////////////////////// |
| 1519 | 1521 |
| 1520 void GrGLDistantLight::setData(const GrGLProgramDataManager& pdman, | 1522 void GrGLDistantLight::setData(const GrGLProgramDataManager& pdman, |
| 1521 const SkLight* light) const { | 1523 const SkLight* light) const { |
| 1522 INHERITED::setData(pdman, light); | 1524 INHERITED::setData(pdman, light); |
| 1523 SkASSERT(light->type() == SkLight::kDistant_LightType); | 1525 SkASSERT(light->type() == SkLight::kDistant_LightType); |
| 1524 const SkDistantLight* distantLight = static_cast<const SkDistantLight*>(ligh
t); | 1526 const SkDistantLight* distantLight = static_cast<const SkDistantLight*>(ligh
t); |
| 1525 setUniformNormal3(pdman, fDirectionUni, distantLight->direction()); | 1527 setUniformNormal3(pdman, fDirectionUni, distantLight->direction()); |
| 1526 } | 1528 } |
| 1527 | 1529 |
| 1528 void GrGLDistantLight::emitSurfaceToLight(GrGLShaderBuilder* builder, const char
* z) { | 1530 void GrGLDistantLight::emitSurfaceToLight(GrGLProgramBuilder* builder, const cha
r* z) { |
| 1529 const char* dir; | 1531 const char* dir; |
| 1530 fDirectionUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
kVec3f_GrSLType, | 1532 fDirectionUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility
, kVec3f_GrSLType, |
| 1531 "LightDirection", &dir); | 1533 "LightDirection", &dir); |
| 1532 builder->fsCodeAppend(dir); | 1534 builder->getFragmentShaderBuilder()->codeAppend(dir); |
| 1533 } | 1535 } |
| 1534 | 1536 |
| 1535 /////////////////////////////////////////////////////////////////////////////// | 1537 /////////////////////////////////////////////////////////////////////////////// |
| 1536 | 1538 |
| 1537 void GrGLPointLight::setData(const GrGLProgramDataManager& pdman, | 1539 void GrGLPointLight::setData(const GrGLProgramDataManager& pdman, |
| 1538 const SkLight* light) const { | 1540 const SkLight* light) const { |
| 1539 INHERITED::setData(pdman, light); | 1541 INHERITED::setData(pdman, light); |
| 1540 SkASSERT(light->type() == SkLight::kPoint_LightType); | 1542 SkASSERT(light->type() == SkLight::kPoint_LightType); |
| 1541 const SkPointLight* pointLight = static_cast<const SkPointLight*>(light); | 1543 const SkPointLight* pointLight = static_cast<const SkPointLight*>(light); |
| 1542 setUniformPoint3(pdman, fLocationUni, pointLight->location()); | 1544 setUniformPoint3(pdman, fLocationUni, pointLight->location()); |
| 1543 } | 1545 } |
| 1544 | 1546 |
| 1545 void GrGLPointLight::emitSurfaceToLight(GrGLShaderBuilder* builder, const char*
z) { | 1547 void GrGLPointLight::emitSurfaceToLight(GrGLProgramBuilder* builder, const char*
z) { |
| 1546 const char* loc; | 1548 const char* loc; |
| 1547 fLocationUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility,
kVec3f_GrSLType, | 1549 fLocationUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
kVec3f_GrSLType, |
| 1548 "LightLocation", &loc); | 1550 "LightLocation", &loc); |
| 1549 builder->fsCodeAppendf("normalize(%s - vec3(%s.xy, %s))", loc, builder->frag
mentPosition(), z); | 1551 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 1552 fsBuilder->codeAppendf("normalize(%s - vec3(%s.xy, %s))", |
| 1553 loc, fsBuilder->fragmentPosition(), z); |
| 1550 } | 1554 } |
| 1551 | 1555 |
| 1552 /////////////////////////////////////////////////////////////////////////////// | 1556 /////////////////////////////////////////////////////////////////////////////// |
| 1553 | 1557 |
| 1554 void GrGLSpotLight::setData(const GrGLProgramDataManager& pdman, | 1558 void GrGLSpotLight::setData(const GrGLProgramDataManager& pdman, |
| 1555 const SkLight* light) const { | 1559 const SkLight* light) const { |
| 1556 INHERITED::setData(pdman, light); | 1560 INHERITED::setData(pdman, light); |
| 1557 SkASSERT(light->type() == SkLight::kSpot_LightType); | 1561 SkASSERT(light->type() == SkLight::kSpot_LightType); |
| 1558 const SkSpotLight* spotLight = static_cast<const SkSpotLight *>(light); | 1562 const SkSpotLight* spotLight = static_cast<const SkSpotLight *>(light); |
| 1559 setUniformPoint3(pdman, fLocationUni, spotLight->location()); | 1563 setUniformPoint3(pdman, fLocationUni, spotLight->location()); |
| 1560 pdman.set1f(fExponentUni, spotLight->specularExponent()); | 1564 pdman.set1f(fExponentUni, spotLight->specularExponent()); |
| 1561 pdman.set1f(fCosInnerConeAngleUni, spotLight->cosInnerConeAngle()); | 1565 pdman.set1f(fCosInnerConeAngleUni, spotLight->cosInnerConeAngle()); |
| 1562 pdman.set1f(fCosOuterConeAngleUni, spotLight->cosOuterConeAngle()); | 1566 pdman.set1f(fCosOuterConeAngleUni, spotLight->cosOuterConeAngle()); |
| 1563 pdman.set1f(fConeScaleUni, spotLight->coneScale()); | 1567 pdman.set1f(fConeScaleUni, spotLight->coneScale()); |
| 1564 setUniformNormal3(pdman, fSUni, spotLight->s()); | 1568 setUniformNormal3(pdman, fSUni, spotLight->s()); |
| 1565 } | 1569 } |
| 1566 | 1570 |
| 1567 void GrGLSpotLight::emitSurfaceToLight(GrGLShaderBuilder* builder, const char* z
) { | 1571 void GrGLSpotLight::emitSurfaceToLight(GrGLProgramBuilder* builder, const char*
z) { |
| 1568 const char* location; | 1572 const char* location; |
| 1569 fLocationUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 1573 fLocationUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 1570 kVec3f_GrSLType, "LightLocation", &locati
on); | 1574 kVec3f_GrSLType, "LightLocation", &locati
on); |
| 1571 builder->fsCodeAppendf("normalize(%s - vec3(%s.xy, %s))", | 1575 |
| 1572 location, builder->fragmentPosition(), z); | 1576 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 1577 fsBuilder->codeAppendf("normalize(%s - vec3(%s.xy, %s))", |
| 1578 location, fsBuilder->fragmentPosition(), z); |
| 1573 } | 1579 } |
| 1574 | 1580 |
| 1575 void GrGLSpotLight::emitLightColor(GrGLShaderBuilder* builder, | 1581 void GrGLSpotLight::emitLightColor(GrGLProgramBuilder* builder, |
| 1576 const char *surfaceToLight) { | 1582 const char *surfaceToLight) { |
| 1577 | 1583 |
| 1578 const char* color = builder->getUniformCStr(this->lightColorUni()); // creat
ed by parent class. | 1584 const char* color = builder->getUniformCStr(this->lightColorUni()); // creat
ed by parent class. |
| 1579 | 1585 |
| 1580 const char* exponent; | 1586 const char* exponent; |
| 1581 const char* cosInner; | 1587 const char* cosInner; |
| 1582 const char* cosOuter; | 1588 const char* cosOuter; |
| 1583 const char* coneScale; | 1589 const char* coneScale; |
| 1584 const char* s; | 1590 const char* s; |
| 1585 fExponentUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 1591 fExponentUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 1586 kFloat_GrSLType, "Exponent", &exponent); | 1592 kFloat_GrSLType, "Exponent", &exponent); |
| 1587 fCosInnerConeAngleUni = builder->addUniform(GrGLShaderBuilder::kFragment_Vis
ibility, | 1593 fCosInnerConeAngleUni = builder->addUniform(GrGLProgramBuilder::kFragment_Vi
sibility, |
| 1588 kFloat_GrSLType, "CosInnerConeAn
gle", &cosInner); | 1594 kFloat_GrSLType, "CosInnerConeAn
gle", &cosInner); |
| 1589 fCosOuterConeAngleUni = builder->addUniform(GrGLShaderBuilder::kFragment_Vis
ibility, | 1595 fCosOuterConeAngleUni = builder->addUniform(GrGLProgramBuilder::kFragment_Vi
sibility, |
| 1590 kFloat_GrSLType, "CosOuterConeAn
gle", &cosOuter); | 1596 kFloat_GrSLType, "CosOuterConeAn
gle", &cosOuter); |
| 1591 fConeScaleUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 1597 fConeScaleUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility
, |
| 1592 kFloat_GrSLType, "ConeScale", &coneScale
); | 1598 kFloat_GrSLType, "ConeScale", &coneScale
); |
| 1593 fSUni = builder->addUniform(GrGLShaderBuilder::kFragment_Visibility, | 1599 fSUni = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
| 1594 kVec3f_GrSLType, "S", &s); | 1600 kVec3f_GrSLType, "S", &s); |
| 1595 | 1601 |
| 1596 static const GrGLShaderVar gLightColorArgs[] = { | 1602 static const GrGLShaderVar gLightColorArgs[] = { |
| 1597 GrGLShaderVar("surfaceToLight", kVec3f_GrSLType) | 1603 GrGLShaderVar("surfaceToLight", kVec3f_GrSLType) |
| 1598 }; | 1604 }; |
| 1599 SkString lightColorBody; | 1605 SkString lightColorBody; |
| 1600 lightColorBody.appendf("\tfloat cosAngle = -dot(surfaceToLight, %s);\n", s); | 1606 lightColorBody.appendf("\tfloat cosAngle = -dot(surfaceToLight, %s);\n", s); |
| 1601 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosOuter); | 1607 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosOuter); |
| 1602 lightColorBody.appendf("\t\treturn vec3(0);\n"); | 1608 lightColorBody.appendf("\t\treturn vec3(0);\n"); |
| 1603 lightColorBody.appendf("\t}\n"); | 1609 lightColorBody.appendf("\t}\n"); |
| 1604 lightColorBody.appendf("\tfloat scale = pow(cosAngle, %s);\n", exponent); | 1610 lightColorBody.appendf("\tfloat scale = pow(cosAngle, %s);\n", exponent); |
| 1605 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosInner); | 1611 lightColorBody.appendf("\tif (cosAngle < %s) {\n", cosInner); |
| 1606 lightColorBody.appendf("\t\treturn %s * scale * (cosAngle - %s) * %s;\n", | 1612 lightColorBody.appendf("\t\treturn %s * scale * (cosAngle - %s) * %s;\n", |
| 1607 color, cosOuter, coneScale); | 1613 color, cosOuter, coneScale); |
| 1608 lightColorBody.appendf("\t}\n"); | 1614 lightColorBody.appendf("\t}\n"); |
| 1609 lightColorBody.appendf("\treturn %s;\n", color); | 1615 lightColorBody.appendf("\treturn %s;\n", color); |
| 1610 builder->fsEmitFunction(kVec3f_GrSLType, | 1616 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 1617 fsBuilder->emitFunction(kVec3f_GrSLType, |
| 1611 "lightColor", | 1618 "lightColor", |
| 1612 SK_ARRAY_COUNT(gLightColorArgs), | 1619 SK_ARRAY_COUNT(gLightColorArgs), |
| 1613 gLightColorArgs, | 1620 gLightColorArgs, |
| 1614 lightColorBody.c_str(), | 1621 lightColorBody.c_str(), |
| 1615 &fLightColorFunc); | 1622 &fLightColorFunc); |
| 1616 | 1623 |
| 1617 builder->fsCodeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); | 1624 fsBuilder->codeAppendf("%s(%s)", fLightColorFunc.c_str(), surfaceToLight); |
| 1618 } | 1625 } |
| 1619 | 1626 |
| 1620 #endif | 1627 #endif |
| 1621 | 1628 |
| 1622 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) | 1629 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingImageFilter) |
| 1623 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) | 1630 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkDiffuseLightingImageFilter) |
| 1624 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) | 1631 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkSpecularLightingImageFilter) |
| 1625 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 1632 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| OLD | NEW |