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 |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "SkErrorInternals.h" | 12 #include "SkImageDecoder.h" |
13 #include "SkGr.h" | 13 #include "SkLightingShader.h" |
14 #include "SkPoint3.h" | |
15 #include "SkReadBuffer.h" | |
16 #include "SkShader.h" | |
17 #include "SkWriteBuffer.h" | |
18 #include "GrFragmentProcessor.h" | |
19 #include "GrCoordTransform.h" | |
20 #include "gl/GrGLProcessor.h" | |
21 #include "gl/builders/GrGLProgramBuilder.h" | |
22 | |
23 /////////////////////////////////////////////////////////////////////////////// | |
24 | |
25 class LightingShader : public SkShader { | |
26 public: | |
27 struct Light { | |
28 SkVector3 fDirection; | |
29 SkColor fColor; // assumed to be linear color | |
30 }; | |
31 | |
32 LightingShader(const SkBitmap& diffuse, const SkBitmap& normal, const Light&
light, | |
33 const SkColor ambient) | |
34 : fDiffuseMap(diffuse) | |
35 , fNormalMap(normal) | |
36 , fLight(light) | |
37 , fAmbientColor(ambient) {} | |
38 | |
39 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(LightingShader); | |
40 | |
41 void flatten(SkWriteBuffer& buf) const override { | |
42 buf.writeBitmap(fDiffuseMap); | |
43 buf.writeBitmap(fNormalMap); | |
44 buf.writeScalarArray(&fLight.fDirection.fX, 3); | |
45 buf.writeColor(fLight.fColor); | |
46 buf.writeColor(fAmbientColor); | |
47 } | |
48 | |
49 bool asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix& v
iewM, | |
50 const SkMatrix* localMatrix, GrColor* color, | |
51 GrProcessorDataManager*, GrFragmentProcessor** fp)
const override; | |
52 | |
53 SkShader::BitmapType asABitmap(SkBitmap* bitmap, SkMatrix* matrix, | |
54 SkShader::TileMode* xy) const override { | |
55 if (bitmap) { | |
56 *bitmap = fDiffuseMap; | |
57 } | |
58 if (matrix) { | |
59 matrix->reset(); | |
60 } | |
61 if (xy) { | |
62 xy[0] = kClamp_TileMode; | |
63 xy[1] = kClamp_TileMode; | |
64 } | |
65 return kDefault_BitmapType; | |
66 } | |
67 | |
68 #ifndef SK_IGNORE_TO_STRING | |
69 void toString(SkString* str) const override { | |
70 str->appendf("LightingShader: ()"); | |
71 } | |
72 #endif | |
73 | |
74 void setLight(const Light& light) { fLight = light; } | |
75 | |
76 private: | |
77 SkBitmap fDiffuseMap; | |
78 SkBitmap fNormalMap; | |
79 Light fLight; | |
80 SkColor fAmbientColor; | |
81 }; | |
82 | |
83 SkFlattenable* LightingShader::CreateProc(SkReadBuffer& buf) { | |
84 SkBitmap diffuse; | |
85 if (!buf.readBitmap(&diffuse)) { | |
86 return NULL; | |
87 } | |
88 diffuse.setImmutable(); | |
89 | |
90 SkBitmap normal; | |
91 if (!buf.readBitmap(&normal)) { | |
92 return NULL; | |
93 } | |
94 normal.setImmutable(); | |
95 | |
96 Light light; | |
97 if (!buf.readScalarArray(&light.fDirection.fX, 3)) { | |
98 return NULL; | |
99 } | |
100 light.fColor = buf.readColor(); | |
101 | |
102 SkColor ambient = buf.readColor(); | |
103 | |
104 return SkNEW_ARGS(LightingShader, (diffuse, normal, light, ambient)); | |
105 } | |
106 | |
107 //////////////////////////////////////////////////////////////////////////// | |
108 | |
109 class LightingFP : public GrFragmentProcessor { | |
110 public: | |
111 LightingFP(GrTexture* diffuse, GrTexture* normal, const SkMatrix& matrix, | |
112 SkVector3 lightDir, GrColor lightColor, GrColor ambientColor) | |
113 : fDeviceTransform(kDevice_GrCoordSet, matrix) | |
114 , fDiffuseTextureAccess(diffuse) | |
115 , fNormalTextureAccess(normal) | |
116 , fLightDir(lightDir) | |
117 , fLightColor(lightColor) | |
118 , fAmbientColor(ambientColor) { | |
119 this->addCoordTransform(&fDeviceTransform); | |
120 this->addTextureAccess(&fDiffuseTextureAccess); | |
121 this->addTextureAccess(&fNormalTextureAccess); | |
122 | |
123 this->initClassID<LightingFP>(); | |
124 } | |
125 | |
126 class LightingGLFP : public GrGLFragmentProcessor { | |
127 public: | |
128 LightingGLFP() : fLightColor(GrColor_ILLEGAL) { | |
129 fLightDir.fX = 10000.0f; | |
130 } | |
131 | |
132 void emitCode(GrGLFPBuilder* builder, | |
133 const GrFragmentProcessor& fp, | |
134 const char* outputColor, | |
135 const char* inputColor, | |
136 const TransformedCoordsArray& coords, | |
137 const TextureSamplerArray& samplers) override { | |
138 | |
139 GrGLFragmentBuilder* fpb = builder->getFragmentShaderBuilder(); | |
140 | |
141 // add uniforms | |
142 const char* lightDirUniName = NULL; | |
143 fLightDirUni = builder->addUniform(GrGLProgramBuilder::kFragment_Vis
ibility, | |
144 kVec3f_GrSLType, kDefault_GrSLPre
cision, | |
145 "LightDir", &lightDirUniName); | |
146 | |
147 const char* lightColorUniName = NULL; | |
148 fLightColorUni = builder->addUniform(GrGLProgramBuilder::kFragment_V
isibility, | |
149 kVec4f_GrSLType, kDefault_GrSLP
recision, | |
150 "LightColor", &lightColorUniNam
e); | |
151 | |
152 const char* ambientColorUniName = NULL; | |
153 fAmbientColorUni = builder->addUniform(GrGLProgramBuilder::kFragment
_Visibility, | |
154 kVec4f_GrSLType, kDefault_GrS
LPrecision, | |
155 "AmbientColor", &ambientColor
UniName); | |
156 | |
157 fpb->codeAppend("vec4 diffuseColor = "); | |
158 fpb->appendTextureLookupAndModulate(inputColor, samplers[0], | |
159 coords[0].c_str(), coords[0].get
Type()); | |
160 fpb->codeAppend(";"); | |
161 | |
162 fpb->codeAppend("vec4 normalColor = "); | |
163 fpb->appendTextureLookup(samplers[1], coords[0].c_str(), coords[0].g
etType()); | |
164 fpb->codeAppend(";"); | |
165 | |
166 fpb->codeAppend("vec3 normal = normalize(2.0*(normalColor.rgb - vec3
(0.5)));"); | |
167 fpb->codeAppendf("vec3 lightDir = normalize(%s);", lightDirUniName); | |
168 fpb->codeAppend("float NdotL = dot(normal, lightDir);"); | |
169 // diffuse light | |
170 fpb->codeAppendf("vec3 result = %s.rgb*diffuseColor.rgb*NdotL;", lig
htColorUniName); | |
171 // ambient light | |
172 fpb->codeAppendf("result += %s.rgb;", ambientColorUniName); | |
173 fpb->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", outputCol
or); | |
174 } | |
175 | |
176 void setData(const GrGLProgramDataManager& pdman, const GrProcessor& pro
c) override { | |
177 const LightingFP& lightingFP = proc.cast<LightingFP>(); | |
178 | |
179 SkVector3 lightDir = lightingFP.lightDir(); | |
180 if (lightDir != fLightDir) { | |
181 pdman.set3fv(fLightDirUni, 1, &lightDir.fX); | |
182 fLightDir = lightDir; | |
183 } | |
184 | |
185 GrColor lightColor = lightingFP.lightColor(); | |
186 if (lightColor != fLightColor) { | |
187 GrGLfloat c[4]; | |
188 GrColorToRGBAFloat(lightColor, c); | |
189 pdman.set4fv(fLightColorUni, 1, c); | |
190 fLightColor = lightColor; | |
191 } | |
192 | |
193 GrColor ambientColor = lightingFP.ambientColor(); | |
194 if (ambientColor != fAmbientColor) { | |
195 GrGLfloat c[4]; | |
196 GrColorToRGBAFloat(ambientColor, c); | |
197 pdman.set4fv(fAmbientColorUni, 1, c); | |
198 fAmbientColor = ambientColor; | |
199 } | |
200 } | |
201 | |
202 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | |
203 GrProcessorKeyBuilder* b) { | |
204 // const LightingFP& lightingFP = proc.cast<LightingFP>(); | |
205 // only one shader generated currently | |
206 b->add32(0x0); | |
207 } | |
208 | |
209 private: | |
210 SkVector3 fLightDir; | |
211 GrGLProgramDataManager::UniformHandle fLightDirUni; | |
212 | |
213 GrColor fLightColor; | |
214 GrGLProgramDataManager::UniformHandle fLightColorUni; | |
215 | |
216 GrColor fAmbientColor; | |
217 GrGLProgramDataManager::UniformHandle fAmbientColorUni; | |
218 }; | |
219 | |
220 GrGLFragmentProcessor* createGLInstance() const override { return SkNEW(Ligh
tingGLFP); } | |
221 | |
222 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) con
st override { | |
223 LightingGLFP::GenKey(*this, caps, b); | |
224 } | |
225 | |
226 const char* name() const override { return "LightingFP"; } | |
227 | |
228 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | |
229 inout->mulByUnknownFourComponents(); | |
230 } | |
231 | |
232 SkVector3 lightDir() const { return fLightDir; } | |
233 GrColor lightColor() const { return fLightColor; } | |
234 GrColor ambientColor() const { return fAmbientColor; } | |
235 | |
236 private: | |
237 bool onIsEqual(const GrFragmentProcessor& proc) const override { | |
238 const LightingFP& lightingFP = proc.cast<LightingFP>(); | |
239 return fDeviceTransform == lightingFP.fDeviceTransform && | |
240 fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess && | |
241 fNormalTextureAccess == lightingFP.fNormalTextureAccess && | |
242 fLightDir == lightingFP.fLightDir && | |
243 fLightColor == lightingFP.fLightColor && | |
244 fAmbientColor == lightingFP.fAmbientColor; | |
245 } | |
246 | |
247 GrCoordTransform fDeviceTransform; | |
248 GrTextureAccess fDiffuseTextureAccess; | |
249 GrTextureAccess fNormalTextureAccess; | |
250 SkVector3 fLightDir; | |
251 GrColor fLightColor; | |
252 GrColor fAmbientColor; | |
253 }; | |
254 | |
255 bool LightingShader::asFragmentProcessor(GrContext* context, const SkPaint& pain
t, | |
256 const SkMatrix& viewM, const SkMatrix*
localMatrix, | |
257 GrColor* color, GrProcessorDataManager*
, | |
258 GrFragmentProcessor** fp) const { | |
259 // we assume diffuse and normal maps have same width and height | |
260 // TODO: support different sizes | |
261 SkASSERT(fDiffuseMap.width() == fNormalMap.width() && | |
262 fDiffuseMap.height() == fNormalMap.height()); | |
263 SkMatrix matrix; | |
264 matrix.setIDiv(fDiffuseMap.width(), fDiffuseMap.height()); | |
265 | |
266 SkMatrix lmInverse; | |
267 if (!this->getLocalMatrix().invert(&lmInverse)) { | |
268 return false; | |
269 } | |
270 if (localMatrix) { | |
271 SkMatrix inv; | |
272 if (!localMatrix->invert(&inv)) { | |
273 return false; | |
274 } | |
275 lmInverse.postConcat(inv); | |
276 } | |
277 matrix.preConcat(lmInverse); | |
278 | |
279 // Must set wrap and filter on the sampler before requesting a texture. In t
wo places below | |
280 // we check the matrix scale factors to determine how to interpret the filte
r quality setting. | |
281 // This completely ignores the complexity of the drawVertices case where exp
licit local coords | |
282 // are provided by the caller. | |
283 GrTextureParams::FilterMode textureFilterMode = GrTextureParams::kBilerp_Fil
terMode; | |
284 switch (paint.getFilterQuality()) { | |
285 case kNone_SkFilterQuality: | |
286 textureFilterMode = GrTextureParams::kNone_FilterMode; | |
287 break; | |
288 case kLow_SkFilterQuality: | |
289 textureFilterMode = GrTextureParams::kBilerp_FilterMode; | |
290 break; | |
291 case kMedium_SkFilterQuality:{ | |
292 SkMatrix matrix; | |
293 matrix.setConcat(viewM, this->getLocalMatrix()); | |
294 if (matrix.getMinScale() < SK_Scalar1) { | |
295 textureFilterMode = GrTextureParams::kMipMap_FilterMode; | |
296 } else { | |
297 // Don't trigger MIP level generation unnecessarily. | |
298 textureFilterMode = GrTextureParams::kBilerp_FilterMode; | |
299 } | |
300 break; | |
301 } | |
302 case kHigh_SkFilterQuality: | |
303 default: | |
304 SkErrorInternals::SetError(kInvalidPaint_SkError, | |
305 "Sorry, I don't understand the filtering " | |
306 "mode you asked for. Falling back to " | |
307 "MIPMaps."); | |
308 textureFilterMode = GrTextureParams::kMipMap_FilterMode; | |
309 break; | |
310 | |
311 } | |
312 | |
313 // TODO: support other tile modes | |
314 GrTextureParams params(kClamp_TileMode, textureFilterMode); | |
315 SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, fDi
ffuseMap, ¶ms)); | |
316 if (!diffuseTexture) { | |
317 SkErrorInternals::SetError(kInternalError_SkError, | |
318 "Couldn't convert bitmap to texture."); | |
319 return false; | |
320 } | |
321 | |
322 SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context, fNor
malMap, ¶ms)); | |
323 if (!normalTexture) { | |
324 SkErrorInternals::SetError(kInternalError_SkError, | |
325 "Couldn't convert bitmap to texture."); | |
326 return false; | |
327 } | |
328 | |
329 GrColor lightColor = GrColorPackRGBA(SkColorGetR(fLight.fColor), SkColorGetG
(fLight.fColor), | |
330 SkColorGetB(fLight.fColor), SkColorGetA
(fLight.fColor)); | |
331 GrColor ambientColor = GrColorPackRGBA(SkColorGetR(fAmbientColor), SkColorGe
tG(fAmbientColor), | |
332 SkColorGetB(fAmbientColor), SkColorGe
tA(fAmbientColor)); | |
333 | |
334 *fp = SkNEW_ARGS(LightingFP, (diffuseTexture, normalTexture, matrix, | |
335 fLight.fDirection, lightColor, ambientColor)); | |
336 *color = GrColorPackA4(paint.getAlpha()); | |
337 return true; | |
338 } | |
339 | 14 |
340 //////////////////////////////////////////////////////////////////////////// | 15 //////////////////////////////////////////////////////////////////////////// |
341 | 16 |
342 class LightingView : public SampleView { | 17 class LightingView : public SampleView { |
343 public: | 18 public: |
344 SkAutoTUnref<LightingShader> fShader; | 19 SkAutoTUnref<SkLightingShader> fShader; |
345 SkBitmap fDiffuseBitmap; | 20 SkBitmap fDiffuseBitmap; |
346 SkBitmap fNormalBitmap; | 21 SkBitmap fNormalBitmap; |
347 SkScalar fLightAngle; | 22 SkScalar fLightAngle; |
348 int fColorFactor; | 23 int fColorFactor; |
349 | 24 |
350 LightingView() { | 25 LightingView() { |
351 SkString diffusePath = GetResourcePath("brickwork-texture.jpg"); | 26 SkString diffusePath = GetResourcePath("brickwork-texture.jpg"); |
352 SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap); | 27 SkImageDecoder::DecodeFile(diffusePath.c_str(), &fDiffuseBitmap); |
353 SkString normalPath = GetResourcePath("brickwork_normal-map.jpg"); | 28 SkString normalPath = GetResourcePath("brickwork_normal-map.jpg"); |
354 SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap); | 29 SkImageDecoder::DecodeFile(normalPath.c_str(), &fNormalBitmap); |
355 | 30 |
356 fLightAngle = 0.0f; | 31 fLightAngle = 0.0f; |
357 fColorFactor = 0; | 32 fColorFactor = 0; |
358 | 33 |
359 LightingShader::Light light; | 34 SkLightingShader::Light light; |
360 light.fColor = SkColorSetRGB(0xff, 0xff, 0xff); | 35 light.fColor = SkColorSetRGB(0xff, 0xff, 0xff); |
361 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); | 36 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); |
362 light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); | 37 light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); |
363 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f); | 38 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f); |
364 | 39 |
365 SkColor ambient = SkColorSetRGB(0x1f, 0x1f, 0x1f); | 40 SkColor ambient = SkColorSetRGB(0x1f, 0x1f, 0x1f); |
366 | 41 |
367 fShader.reset(SkNEW_ARGS(LightingShader, (fDiffuseBitmap, fNormalBitmap,
light, ambient))); | 42 fShader.reset(SkNEW_ARGS(SkLightingShader, (fDiffuseBitmap, fNormalBitma
p, light, ambient))); |
368 } | 43 } |
369 | 44 |
370 virtual ~LightingView() {} | 45 virtual ~LightingView() {} |
371 | 46 |
372 protected: | 47 protected: |
373 // overrides from SkEventSink | 48 // overrides from SkEventSink |
374 bool onQuery(SkEvent* evt) override { | 49 bool onQuery(SkEvent* evt) override { |
375 if (SampleCode::TitleQ(*evt)) { | 50 if (SampleCode::TitleQ(*evt)) { |
376 SampleCode::TitleR(evt, "Lighting"); | 51 SampleCode::TitleR(evt, "Lighting"); |
377 return true; | 52 return true; |
378 } | 53 } |
379 return this->INHERITED::onQuery(evt); | 54 return this->INHERITED::onQuery(evt); |
380 } | 55 } |
381 | 56 |
382 void onDrawContent(SkCanvas* canvas) override { | 57 void onDrawContent(SkCanvas* canvas) override { |
383 fLightAngle += 0.015f; | 58 fLightAngle += 0.015f; |
384 fColorFactor++; | 59 fColorFactor++; |
385 | 60 |
386 LightingShader::Light light; | 61 SkLightingShader::Light light; |
387 light.fColor = SkColorSetRGB(0xff, 0xff, (fColorFactor >> 1) & 0xff); | 62 light.fColor = SkColorSetRGB(0xff, 0xff, (fColorFactor >> 1) & 0xff); |
388 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); | 63 light.fDirection.fX = SkScalarSin(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); |
389 light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); | 64 light.fDirection.fY = SkScalarCos(fLightAngle)*SkScalarSin(SK_ScalarPI*0
.25f); |
390 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f); | 65 light.fDirection.fZ = SkScalarCos(SK_ScalarPI*0.25f); |
391 | 66 |
392 fShader.get()->setLight(light); | 67 fShader.get()->setLight(light); |
393 | 68 |
394 SkPaint paint; | 69 SkPaint paint; |
395 paint.setShader(fShader); | 70 paint.setShader(fShader); |
396 paint.setColor(SK_ColorBLACK); | 71 paint.setColor(SK_ColorBLACK); |
(...skipping 12 matching lines...) Expand all Loading... |
409 } | 84 } |
410 | 85 |
411 private: | 86 private: |
412 typedef SampleView INHERITED; | 87 typedef SampleView INHERITED; |
413 }; | 88 }; |
414 | 89 |
415 ////////////////////////////////////////////////////////////////////////////// | 90 ////////////////////////////////////////////////////////////////////////////// |
416 | 91 |
417 static SkView* MyFactory() { return new LightingView; } | 92 static SkView* MyFactory() { return new LightingView; } |
418 static SkViewRegister reg(MyFactory); | 93 static SkViewRegister reg(MyFactory); |
OLD | NEW |