| 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 #include "SampleCode.h" | 8 #include "SampleCode.h" |
| 9 #include "Resources.h" | 9 #include "Resources.h" |
| 10 | 10 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 122 |
| 123 this->initClassID<LightingFP>(); | 123 this->initClassID<LightingFP>(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 class LightingGLFP : public GrGLFragmentProcessor { | 126 class LightingGLFP : public GrGLFragmentProcessor { |
| 127 public: | 127 public: |
| 128 LightingGLFP() : fLightColor(GrColor_ILLEGAL) { | 128 LightingGLFP() : fLightColor(GrColor_ILLEGAL) { |
| 129 fLightDir.fX = 10000.0f; | 129 fLightDir.fX = 10000.0f; |
| 130 } | 130 } |
| 131 | 131 |
| 132 void emitCode(GrGLFPBuilder* builder, | 132 void emitCode(EmitArgs& args) override { |
| 133 const GrFragmentProcessor& fp, | |
| 134 const char* outputColor, | |
| 135 const char* inputColor, | |
| 136 const TransformedCoordsArray& coords, | |
| 137 const TextureSamplerArray& samplers) override { | |
| 138 | 133 |
| 139 GrGLFragmentBuilder* fpb = builder->getFragmentShaderBuilder(); | 134 GrGLFragmentBuilder* fpb = args.fBuilder->getFragmentShaderBuilder()
; |
| 140 | 135 |
| 141 // add uniforms | 136 // add uniforms |
| 142 const char* lightDirUniName = NULL; | 137 const char* lightDirUniName = NULL; |
| 143 fLightDirUni = builder->addUniform(GrGLProgramBuilder::kFragment_Vis
ibility, | 138 fLightDirUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragme
nt_Visibility, |
| 144 kVec3f_GrSLType, kDefault_GrSLPre
cision, | 139 kVec3f_GrSLType, kDefault_GrSLPre
cision, |
| 145 "LightDir", &lightDirUniName); | 140 "LightDir", &lightDirUniName); |
| 146 | 141 |
| 147 const char* lightColorUniName = NULL; | 142 const char* lightColorUniName = NULL; |
| 148 fLightColorUni = builder->addUniform(GrGLProgramBuilder::kFragment_V
isibility, | 143 fLightColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFrag
ment_Visibility, |
| 149 kVec4f_GrSLType, kDefault_GrSLP
recision, | 144 kVec4f_GrSLType, kDefault_GrSLP
recision, |
| 150 "LightColor", &lightColorUniNam
e); | 145 "LightColor", &lightColorUniNam
e); |
| 151 | 146 |
| 152 const char* ambientColorUniName = NULL; | 147 const char* ambientColorUniName = NULL; |
| 153 fAmbientColorUni = builder->addUniform(GrGLProgramBuilder::kFragment
_Visibility, | 148 fAmbientColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFr
agment_Visibility, |
| 154 kVec4f_GrSLType, kDefault_GrS
LPrecision, | 149 kVec4f_GrSLType, kDefault_GrS
LPrecision, |
| 155 "AmbientColor", &ambientColor
UniName); | 150 "AmbientColor", &ambientColor
UniName); |
| 156 | 151 |
| 157 fpb->codeAppend("vec4 diffuseColor = "); | 152 fpb->codeAppend("vec4 diffuseColor = "); |
| 158 fpb->appendTextureLookupAndModulate(inputColor, samplers[0], | 153 fpb->appendTextureLookupAndModulate(args.fInputColor, args.fSamplers
[0], |
| 159 coords[0].c_str(), coords[0].get
Type()); | 154 args.fCoords[0].c_str(), args.fC
oords[0].getType()); |
| 160 fpb->codeAppend(";"); | 155 fpb->codeAppend(";"); |
| 161 | 156 |
| 162 fpb->codeAppend("vec4 normalColor = "); | 157 fpb->codeAppend("vec4 normalColor = "); |
| 163 fpb->appendTextureLookup(samplers[1], coords[0].c_str(), coords[0].g
etType()); | 158 fpb->appendTextureLookup(args.fSamplers[1], args.fCoords[0].c_str(),
|
| 159 args.fCoords[0].getType()); |
| 164 fpb->codeAppend(";"); | 160 fpb->codeAppend(";"); |
| 165 | 161 |
| 166 fpb->codeAppend("vec3 normal = normalize(2.0*(normalColor.rgb - vec3
(0.5)));"); | 162 fpb->codeAppend("vec3 normal = normalize(2.0*(normalColor.rgb - vec3
(0.5)));"); |
| 167 fpb->codeAppendf("vec3 lightDir = normalize(%s);", lightDirUniName); | 163 fpb->codeAppendf("vec3 lightDir = normalize(%s);", lightDirUniName); |
| 168 fpb->codeAppend("float NdotL = dot(normal, lightDir);"); | 164 fpb->codeAppend("float NdotL = dot(normal, lightDir);"); |
| 169 // diffuse light | 165 // diffuse light |
| 170 fpb->codeAppendf("vec3 result = %s.rgb*diffuseColor.rgb*NdotL;", lig
htColorUniName); | 166 fpb->codeAppendf("vec3 result = %s.rgb*diffuseColor.rgb*NdotL;", lig
htColorUniName); |
| 171 // ambient light | 167 // ambient light |
| 172 fpb->codeAppendf("result += %s.rgb;", ambientColorUniName); | 168 fpb->codeAppendf("result += %s.rgb;", ambientColorUniName); |
| 173 fpb->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", outputCol
or); | 169 fpb->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", args.fOut
putColor); |
| 174 } | 170 } |
| 175 | 171 |
| 176 void setData(const GrGLProgramDataManager& pdman, const GrProcessor& pro
c) override { | 172 void setData(const GrGLProgramDataManager& pdman, const GrProcessor& pro
c) override { |
| 177 const LightingFP& lightingFP = proc.cast<LightingFP>(); | 173 const LightingFP& lightingFP = proc.cast<LightingFP>(); |
| 178 | 174 |
| 179 SkVector3 lightDir = lightingFP.lightDir(); | 175 SkVector3 lightDir = lightingFP.lightDir(); |
| 180 if (lightDir != fLightDir) { | 176 if (lightDir != fLightDir) { |
| 181 pdman.set3fv(fLightDirUni, 1, &lightDir.fX); | 177 pdman.set3fv(fLightDirUni, 1, &lightDir.fX); |
| 182 fLightDir = lightDir; | 178 fLightDir = lightDir; |
| 183 } | 179 } |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 } | 405 } |
| 410 | 406 |
| 411 private: | 407 private: |
| 412 typedef SampleView INHERITED; | 408 typedef SampleView INHERITED; |
| 413 }; | 409 }; |
| 414 | 410 |
| 415 ////////////////////////////////////////////////////////////////////////////// | 411 ////////////////////////////////////////////////////////////////////////////// |
| 416 | 412 |
| 417 static SkView* MyFactory() { return new LightingView; } | 413 static SkView* MyFactory() { return new LightingView; } |
| 418 static SkViewRegister reg(MyFactory); | 414 static SkViewRegister reg(MyFactory); |
| OLD | NEW |