| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2015 Google Inc. | 3 * Copyright 2015 Google Inc. |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 #include "SkBitmapProcState.h" | 9 #include "SkBitmapProcState.h" |
| 10 #include "SkColor.h" | 10 #include "SkColor.h" |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 public: | 172 public: |
| 173 LightingGLFP() { | 173 LightingGLFP() { |
| 174 fLightDir.fX = 10000.0f; | 174 fLightDir.fX = 10000.0f; |
| 175 fLightColor.fX = 0.0f; | 175 fLightColor.fX = 0.0f; |
| 176 fAmbientColor.fX = 0.0f; | 176 fAmbientColor.fX = 0.0f; |
| 177 fInvNormRotation.set(0.0f, 0.0f); | 177 fInvNormRotation.set(0.0f, 0.0f); |
| 178 } | 178 } |
| 179 | 179 |
| 180 void emitCode(EmitArgs& args) override { | 180 void emitCode(EmitArgs& args) override { |
| 181 | 181 |
| 182 GrGLSLFragmentBuilder* fpb = args.fBuilder->getFragmentShaderBuilder
(); | 182 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; |
| 183 | 183 |
| 184 // add uniforms | 184 // add uniforms |
| 185 const char* lightDirUniName = nullptr; | 185 const char* lightDirUniName = nullptr; |
| 186 fLightDirUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFrag
ment_Visibility, | 186 fLightDirUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFrag
ment_Visibility, |
| 187 kVec3f_GrSLType, kDefault_G
rSLPrecision, | 187 kVec3f_GrSLType, kDefault_G
rSLPrecision, |
| 188 "LightDir", &lightDirUniNam
e); | 188 "LightDir", &lightDirUniNam
e); |
| 189 | 189 |
| 190 const char* lightColorUniName = nullptr; | 190 const char* lightColorUniName = nullptr; |
| 191 fLightColorUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFr
agment_Visibility, | 191 fLightColorUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFr
agment_Visibility, |
| 192 kVec3f_GrSLType, kDefault
_GrSLPrecision, | 192 kVec3f_GrSLType, kDefault
_GrSLPrecision, |
| 193 "LightColor", &lightColor
UniName); | 193 "LightColor", &lightColor
UniName); |
| 194 | 194 |
| 195 const char* ambientColorUniName = nullptr; | 195 const char* ambientColorUniName = nullptr; |
| 196 fAmbientColorUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::k
Fragment_Visibility, | 196 fAmbientColorUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::k
Fragment_Visibility, |
| 197 kVec3f_GrSLType, kDefau
lt_GrSLPrecision, | 197 kVec3f_GrSLType, kDefau
lt_GrSLPrecision, |
| 198 "AmbientColor", &ambien
tColorUniName); | 198 "AmbientColor", &ambien
tColorUniName); |
| 199 | 199 |
| 200 const char* xformUniName = nullptr; | 200 const char* xformUniName = nullptr; |
| 201 fXformUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragmen
t_Visibility, | 201 fXformUni = args.fBuilder->addUniform(GrGLSLProgramBuilder::kFragmen
t_Visibility, |
| 202 kVec2f_GrSLType, kDefault_GrSL
Precision, | 202 kVec2f_GrSLType, kDefault_GrSL
Precision, |
| 203 "Xform", &xformUniName); | 203 "Xform", &xformUniName); |
| 204 | 204 |
| 205 fpb->codeAppend("vec4 diffuseColor = "); | 205 fragBuilder->codeAppend("vec4 diffuseColor = "); |
| 206 fpb->appendTextureLookupAndModulate(args.fInputColor, args.fSamplers
[0], | 206 fragBuilder->appendTextureLookupAndModulate(args.fInputColor, args.f
Samplers[0], |
| 207 args.fCoords[0].c_str(), | 207 args.fCoords[0].c_str(), |
| 208 args.fCoords[0].getType()); | 208 args.fCoords[0].getType()); |
| 209 fpb->codeAppend(";"); | 209 fragBuilder->codeAppend(";"); |
| 210 | 210 |
| 211 fpb->codeAppend("vec4 normalColor = "); | 211 fragBuilder->codeAppend("vec4 normalColor = "); |
| 212 fpb->appendTextureLookup(args.fSamplers[1], | 212 fragBuilder->appendTextureLookup(args.fSamplers[1], |
| 213 args.fCoords[1].c_str(), | 213 args.fCoords[1].c_str(), |
| 214 args.fCoords[1].getType()); | 214 args.fCoords[1].getType()); |
| 215 fpb->codeAppend(";"); | 215 fragBuilder->codeAppend(";"); |
| 216 | 216 |
| 217 fpb->codeAppend("vec3 normal = normalColor.rgb - vec3(0.5);"); | 217 fragBuilder->codeAppend("vec3 normal = normalColor.rgb - vec3(0.5);"
); |
| 218 | 218 |
| 219 fpb->codeAppendf("mat3 m = mat3(%s.x, -%s.y, 0.0, %s.y, %s.x, 0.0, 0
.0, 0.0, 1.0);", | 219 fragBuilder->codeAppendf( |
| 220 xformUniName, xformUniName, xformUniName, xformUniN
ame); | 220 "mat3 m = mat3(%s.x, -%s.y, 0.0, %s.y, %s.x, 0.
0, 0.0, 0.0, 1.0);", |
| 221 xformUniName, xformUniName, xformUniName, xform
UniName); |
| 221 | 222 |
| 222 // TODO: inverse map the light direction vectors in the vertex shade
r rather than | 223 // TODO: inverse map the light direction vectors in the vertex shade
r rather than |
| 223 // transforming all the normals here! | 224 // transforming all the normals here! |
| 224 fpb->codeAppend("normal = normalize(m*normal);"); | 225 fragBuilder->codeAppend("normal = normalize(m*normal);"); |
| 225 | 226 |
| 226 fpb->codeAppendf("float NdotL = clamp(dot(normal, %s), 0.0, 1.0);",
lightDirUniName); | 227 fragBuilder->codeAppendf("float NdotL = clamp(dot(normal, %s), 0.0,
1.0);", |
| 228 lightDirUniName); |
| 227 // diffuse light | 229 // diffuse light |
| 228 fpb->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;", lightCo
lorUniName); | 230 fragBuilder->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;",
lightColorUniName); |
| 229 // ambient light | 231 // ambient light |
| 230 fpb->codeAppendf("result += %s;", ambientColorUniName); | 232 fragBuilder->codeAppendf("result += %s;", ambientColorUniName); |
| 231 fpb->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", args.fOut
putColor); | 233 fragBuilder->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", a
rgs.fOutputColor); |
| 232 } | 234 } |
| 233 | 235 |
| 234 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 236 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
| 235 GrProcessorKeyBuilder* b) { | 237 GrProcessorKeyBuilder* b) { |
| 236 // const LightingFP& lightingFP = proc.cast<LightingFP>(); | 238 // const LightingFP& lightingFP = proc.cast<LightingFP>(); |
| 237 // only one shader generated currently | 239 // only one shader generated currently |
| 238 b->add32(0x0); | 240 b->add32(0x0); |
| 239 } | 241 } |
| 240 | 242 |
| 241 protected: | 243 protected: |
| (...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 714 normLocalM); | 716 normLocalM); |
| 715 } | 717 } |
| 716 | 718 |
| 717 /////////////////////////////////////////////////////////////////////////////// | 719 /////////////////////////////////////////////////////////////////////////////// |
| 718 | 720 |
| 719 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) | 721 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) |
| 720 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) | 722 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) |
| 721 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 723 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
| 722 | 724 |
| 723 /////////////////////////////////////////////////////////////////////////////// | 725 /////////////////////////////////////////////////////////////////////////////// |
| OLD | NEW |