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" |
11 #include "SkEmptyShader.h" | 11 #include "SkEmptyShader.h" |
12 #include "SkErrorInternals.h" | 12 #include "SkErrorInternals.h" |
13 #include "SkLightingShader.h" | 13 #include "SkLightingShader.h" |
14 #include "SkMathPriv.h" | 14 #include "SkMathPriv.h" |
| 15 #include "SkPoint3.h" |
15 #include "SkReadBuffer.h" | 16 #include "SkReadBuffer.h" |
16 #include "SkWriteBuffer.h" | 17 #include "SkWriteBuffer.h" |
17 | 18 |
18 //////////////////////////////////////////////////////////////////////////// | 19 //////////////////////////////////////////////////////////////////////////// |
19 | 20 |
20 /* | 21 /* |
21 SkLightingShader TODOs: | 22 SkLightingShader TODOs: |
22 support other than clamp mode | 23 support other than clamp mode |
23 allow 'diffuse' & 'normal' to be of different dimensions? | 24 allow 'diffuse' & 'normal' to be of different dimensions? |
24 support different light types | 25 support different light types |
25 support multiple lights | 26 support multiple lights |
26 enforce normal map is 4 channel | 27 enforce normal map is 4 channel |
27 use SkImages instead if SkBitmaps | 28 use SkImages instead if SkBitmaps |
28 | 29 |
29 To Test: | 30 To Test: |
30 non-opaque diffuse textures | 31 non-opaque diffuse textures |
31 A8 diffuse textures | 32 A8 diffuse textures |
32 down & upsampled draws | 33 down & upsampled draws |
33 */ | 34 */ |
34 | 35 |
35 | 36 |
36 | 37 |
37 /** \class SkLightingShaderImpl | 38 /** \class SkLightingShaderImpl |
38 This subclass of shader applies lighting. | 39 This subclass of shader applies lighting. |
39 */ | 40 */ |
40 class SK_API SkLightingShaderImpl : public SkShader { | 41 class SK_API SkLightingShaderImpl : public SkShader { |
41 public: | 42 public: |
42 | 43 |
43 /** Create a new lighting shader that use the provided normal map, light | 44 /** Create a new lighting shader that uses the provided normal map and |
44 and ambient color to light the diffuse bitmap. | 45 lights to light the diffuse bitmap. |
45 @param diffuse the diffuse bitmap | 46 @param diffuse the diffuse bitmap |
46 @param normal the normal map | 47 @param normal the normal map |
47 @param light the light applied to the normal map | 48 @param lights the lights applied to the normal map |
48 @param ambient the linear (unpremul) ambient light color | 49 @param invNormRotation rotation applied to the normal map's normals |
| 50 @param diffLocalM the local matrix for the diffuse coordinates |
| 51 @param normLocalM the local matrix for the normal coordinates |
49 */ | 52 */ |
50 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, | 53 SkLightingShaderImpl(const SkBitmap& diffuse, const SkBitmap& normal, |
51 const SkLightingShader::Light& light, | 54 const SkLightingShader::Lights* lights, |
52 const SkColor3f& ambient, const SkMatrix* localMatrix) | 55 const SkVector& invNormRotation, |
53 : INHERITED(localMatrix) | 56 const SkMatrix* diffLocalM, const SkMatrix* normLocalM)
|
| 57 : INHERITED(diffLocalM) |
54 , fDiffuseMap(diffuse) | 58 , fDiffuseMap(diffuse) |
55 , fNormalMap(normal) | 59 , fNormalMap(normal) |
56 , fLight(light) | 60 , fLights(SkRef(lights)) |
57 , fAmbientColor(ambient) { | 61 , fInvNormRotation(invNormRotation) { |
58 if (!fLight.fDirection.normalize()) { | 62 |
59 fLight.fDirection = SkPoint3::Make(0.0f, 0.0f, 1.0f); | 63 if (normLocalM) { |
| 64 fNormLocalMatrix = *normLocalM; |
| 65 } else { |
| 66 fNormLocalMatrix.reset(); |
60 } | 67 } |
| 68 // Pre-cache so future calls to fNormLocalMatrix.getType() are threadsaf
e. |
| 69 (void)fNormLocalMatrix.getType(); |
| 70 |
61 } | 71 } |
62 | 72 |
63 bool isOpaque() const override; | 73 bool isOpaque() const override; |
64 | 74 |
65 bool asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix& v
iewM, | 75 bool asFragmentProcessor(GrContext*, const SkPaint& paint, const SkMatrix& v
iewM, |
66 const SkMatrix* localMatrix, GrColor* color, | 76 const SkMatrix* localMatrix, GrColor* color, |
67 GrProcessorDataManager*, GrFragmentProcessor** fp)
const override; | 77 GrProcessorDataManager*, GrFragmentProcessor** fp)
const override; |
68 | 78 |
69 size_t contextSize() const override; | 79 size_t contextSize() const override; |
70 | 80 |
(...skipping 16 matching lines...) Expand all Loading... |
87 | 97 |
88 typedef SkShader::Context INHERITED; | 98 typedef SkShader::Context INHERITED; |
89 }; | 99 }; |
90 | 100 |
91 SK_TO_STRING_OVERRIDE() | 101 SK_TO_STRING_OVERRIDE() |
92 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl) | 102 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkLightingShaderImpl) |
93 | 103 |
94 protected: | 104 protected: |
95 void flatten(SkWriteBuffer&) const override; | 105 void flatten(SkWriteBuffer&) const override; |
96 Context* onCreateContext(const ContextRec&, void*) const override; | 106 Context* onCreateContext(const ContextRec&, void*) const override; |
| 107 bool computeNormTotalInverse(const ContextRec& rec, SkMatrix* normTotalInver
se) const; |
97 | 108 |
98 private: | 109 private: |
99 SkBitmap fDiffuseMap; | 110 SkBitmap fDiffuseMap; |
100 SkBitmap fNormalMap; | 111 SkBitmap fNormalMap; |
101 SkLightingShader::Light fLight; | 112 |
102 SkColor3f fAmbientColor; // linear (unpremul) color. Range is
0..1/channel. | 113 SkAutoTUnref<const SkLightingShader::Lights> fLights; |
| 114 |
| 115 SkMatrix fNormLocalMatrix; |
| 116 SkVector fInvNormRotation; |
103 | 117 |
104 friend class SkLightingShader; | 118 friend class SkLightingShader; |
105 | 119 |
106 typedef SkShader INHERITED; | 120 typedef SkShader INHERITED; |
107 }; | 121 }; |
108 | 122 |
109 //////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////// |
110 | 124 |
111 #if SK_SUPPORT_GPU | 125 #if SK_SUPPORT_GPU |
112 | 126 |
113 #include "GrCoordTransform.h" | 127 #include "GrCoordTransform.h" |
114 #include "GrFragmentProcessor.h" | 128 #include "GrFragmentProcessor.h" |
115 #include "GrTextureAccess.h" | 129 #include "GrTextureAccess.h" |
116 #include "gl/GrGLProcessor.h" | 130 #include "gl/GrGLProcessor.h" |
117 #include "gl/builders/GrGLProgramBuilder.h" | 131 #include "gl/builders/GrGLProgramBuilder.h" |
118 #include "SkGr.h" | 132 #include "SkGr.h" |
119 | 133 |
120 class LightingFP : public GrFragmentProcessor { | 134 class LightingFP : public GrFragmentProcessor { |
121 public: | 135 public: |
122 LightingFP(GrTexture* diffuse, GrTexture* normal, const SkMatrix& matrix, | 136 LightingFP(GrProcessorDataManager* pdm, GrTexture* diffuse, GrTexture* norma
l, |
123 const SkVector3& lightDir, const SkColor3f& lightColor, | 137 const SkMatrix& diffMatrix, const SkMatrix& normMatrix, |
124 const SkColor3f& ambientColor) | 138 const GrTextureParams& diffParams, const GrTextureParams& normPar
ams, |
125 : fDeviceTransform(kDevice_GrCoordSet, matrix) | 139 const SkLightingShader::Lights* lights, const SkVector& invNormRo
tation) |
126 , fDiffuseTextureAccess(diffuse) | 140 : fDiffDeviceTransform(kLocal_GrCoordSet, diffMatrix, diffuse, diffParam
s.filterMode()) |
127 , fNormalTextureAccess(normal) | 141 , fNormDeviceTransform(kLocal_GrCoordSet, normMatrix, normal, normParams
.filterMode()) |
128 , fLightDir(lightDir) | 142 , fDiffuseTextureAccess(diffuse, diffParams) |
129 , fLightColor(lightColor) | 143 , fNormalTextureAccess(normal, normParams) |
130 , fAmbientColor(ambientColor) { | 144 , fInvNormRotation(invNormRotation) { |
131 this->addCoordTransform(&fDeviceTransform); | 145 this->addCoordTransform(&fDiffDeviceTransform); |
| 146 this->addCoordTransform(&fNormDeviceTransform); |
132 this->addTextureAccess(&fDiffuseTextureAccess); | 147 this->addTextureAccess(&fDiffuseTextureAccess); |
133 this->addTextureAccess(&fNormalTextureAccess); | 148 this->addTextureAccess(&fNormalTextureAccess); |
134 | 149 |
| 150 // fuse all ambient lights into a single one |
| 151 fAmbientColor.set(0.0f, 0.0f, 0.0f); |
| 152 for (int i = 0; i < lights->numLights(); ++i) { |
| 153 if (SkLight::kAmbient_LightType == lights->light(i).type()) { |
| 154 fAmbientColor += lights->light(i).color(); |
| 155 } else { |
| 156 // TODO: handle more than one of these |
| 157 fLightColor = lights->light(i).color(); |
| 158 fLightDir = lights->light(i).dir(); |
| 159 } |
| 160 } |
| 161 |
135 this->initClassID<LightingFP>(); | 162 this->initClassID<LightingFP>(); |
136 } | 163 } |
137 | 164 |
138 class LightingGLFP : public GrGLFragmentProcessor { | 165 class LightingGLFP : public GrGLFragmentProcessor { |
139 public: | 166 public: |
140 LightingGLFP() { | 167 LightingGLFP() { |
141 fLightDir.fX = 10000.0f; | 168 fLightDir.fX = 10000.0f; |
142 fLightColor.fX = 0.0f; | 169 fLightColor.fX = 0.0f; |
143 fAmbientColor.fX = 0.0f; | 170 fAmbientColor.fX = 0.0f; |
| 171 fInvNormRotation.fX = 0.0f; |
144 } | 172 } |
145 | 173 |
146 void emitCode(EmitArgs& args) override { | 174 void emitCode(EmitArgs& args) override { |
147 | 175 |
148 GrGLFragmentBuilder* fpb = args.fBuilder->getFragmentShaderBuilder()
; | 176 GrGLFragmentBuilder* fpb = args.fBuilder->getFragmentShaderBuilder()
; |
149 | 177 |
150 // add uniforms | 178 // add uniforms |
151 const char* lightDirUniName = NULL; | 179 const char* lightDirUniName = NULL; |
152 fLightDirUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragme
nt_Visibility, | 180 fLightDirUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragme
nt_Visibility, |
153 kVec3f_GrSLType, kDefault_G
rSLPrecision, | 181 kVec3f_GrSLType, kDefault_G
rSLPrecision, |
154 "LightDir", &lightDirUniNam
e); | 182 "LightDir", &lightDirUniNam
e); |
155 | 183 |
156 const char* lightColorUniName = NULL; | 184 const char* lightColorUniName = NULL; |
157 fLightColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFrag
ment_Visibility, | 185 fLightColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFrag
ment_Visibility, |
158 kVec3f_GrSLType, kDefault
_GrSLPrecision, | 186 kVec3f_GrSLType, kDefault
_GrSLPrecision, |
159 "LightColor", &lightColor
UniName); | 187 "LightColor", &lightColor
UniName); |
160 | 188 |
161 const char* ambientColorUniName = NULL; | 189 const char* ambientColorUniName = NULL; |
162 fAmbientColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFr
agment_Visibility, | 190 fAmbientColorUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFr
agment_Visibility, |
163 kVec3f_GrSLType, kDefau
lt_GrSLPrecision, | 191 kVec3f_GrSLType, kDefau
lt_GrSLPrecision, |
164 "AmbientColor", &ambien
tColorUniName); | 192 "AmbientColor", &ambien
tColorUniName); |
165 | 193 |
| 194 const char* xformUniName = NULL; |
| 195 fXformUni = args.fBuilder->addUniform(GrGLProgramBuilder::kFragment_
Visibility, |
| 196 kVec2f_GrSLType, kDefault_GrSL
Precision, |
| 197 "Xform", &xformUniName); |
| 198 |
166 fpb->codeAppend("vec4 diffuseColor = "); | 199 fpb->codeAppend("vec4 diffuseColor = "); |
167 fpb->appendTextureLookupAndModulate(args.fInputColor, args.fSamplers
[0], | 200 fpb->appendTextureLookupAndModulate(args.fInputColor, args.fSamplers
[0], |
168 args.fCoords[0].c_str(), | 201 args.fCoords[0].c_str(), |
169 args.fCoords[0].getType()); | 202 args.fCoords[0].getType()); |
170 fpb->codeAppend(";"); | 203 fpb->codeAppend(";"); |
171 | 204 |
172 fpb->codeAppend("vec4 normalColor = "); | 205 fpb->codeAppend("vec4 normalColor = "); |
173 fpb->appendTextureLookup(args.fSamplers[1], | 206 fpb->appendTextureLookup(args.fSamplers[1], |
174 args.fCoords[0].c_str(), | 207 args.fCoords[1].c_str(), |
175 args.fCoords[0].getType()); | 208 args.fCoords[1].getType()); |
176 fpb->codeAppend(";"); | 209 fpb->codeAppend(";"); |
177 | 210 |
178 fpb->codeAppend("vec3 normal = normalize(normalColor.rgb - vec3(0.5)
);"); | 211 fpb->codeAppend("vec3 normal = normalColor.rgb - vec3(0.5);"); |
179 fpb->codeAppendf("vec3 lightDir = normalize(%s);", lightDirUniName); | 212 |
180 fpb->codeAppend("float NdotL = dot(normal, lightDir);"); | 213 fpb->codeAppendf("mat3 m = mat3(%s.x, -%s.y, 0.0, %s.y, %s.x, 0.0, 0
.0, 0.0, 1.0);", |
| 214 xformUniName, xformUniName, xformUniName, xformUniN
ame); |
| 215 |
| 216 // TODO: inverse map the light direction vectors in the vertex shade
r rather than |
| 217 // transforming all the normals here! |
| 218 fpb->codeAppend("normal = normalize(m*normal);"); |
| 219 |
| 220 fpb->codeAppendf("float NdotL = clamp(dot(normal, %s), 0.0, 1.0);",
lightDirUniName); |
181 // diffuse light | 221 // diffuse light |
182 fpb->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;", lightCo
lorUniName); | 222 fpb->codeAppendf("vec3 result = %s*diffuseColor.rgb*NdotL;", lightCo
lorUniName); |
183 // ambient light | 223 // ambient light |
184 fpb->codeAppendf("result += %s;", ambientColorUniName); | 224 fpb->codeAppendf("result += %s;", ambientColorUniName); |
185 fpb->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", args.fOut
putColor); | 225 fpb->codeAppendf("%s = vec4(result.rgb, diffuseColor.a);", args.fOut
putColor); |
186 } | 226 } |
187 | 227 |
188 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | 228 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, |
189 GrProcessorKeyBuilder* b) { | 229 GrProcessorKeyBuilder* b) { |
190 // const LightingFP& lightingFP = proc.cast<LightingFP>(); | 230 // const LightingFP& lightingFP = proc.cast<LightingFP>(); |
(...skipping 15 matching lines...) Expand all Loading... |
206 if (lightColor != fLightColor) { | 246 if (lightColor != fLightColor) { |
207 pdman.set3fv(fLightColorUni, 1, &lightColor.fX); | 247 pdman.set3fv(fLightColorUni, 1, &lightColor.fX); |
208 fLightColor = lightColor; | 248 fLightColor = lightColor; |
209 } | 249 } |
210 | 250 |
211 const SkColor3f& ambientColor = lightingFP.ambientColor(); | 251 const SkColor3f& ambientColor = lightingFP.ambientColor(); |
212 if (ambientColor != fAmbientColor) { | 252 if (ambientColor != fAmbientColor) { |
213 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); | 253 pdman.set3fv(fAmbientColorUni, 1, &ambientColor.fX); |
214 fAmbientColor = ambientColor; | 254 fAmbientColor = ambientColor; |
215 } | 255 } |
| 256 |
| 257 const SkVector& invNormRotation = lightingFP.invNormRotation(); |
| 258 if (invNormRotation != fInvNormRotation) { |
| 259 pdman.set2fv(fXformUni, 1, &invNormRotation.fX); |
| 260 fInvNormRotation = invNormRotation; |
| 261 } |
216 } | 262 } |
217 | 263 |
218 private: | 264 private: |
219 SkVector3 fLightDir; | 265 SkVector3 fLightDir; |
220 GrGLProgramDataManager::UniformHandle fLightDirUni; | 266 GrGLProgramDataManager::UniformHandle fLightDirUni; |
221 | 267 |
222 SkColor3f fLightColor; | 268 SkColor3f fLightColor; |
223 GrGLProgramDataManager::UniformHandle fLightColorUni; | 269 GrGLProgramDataManager::UniformHandle fLightColorUni; |
224 | 270 |
225 SkColor3f fAmbientColor; | 271 SkColor3f fAmbientColor; |
226 GrGLProgramDataManager::UniformHandle fAmbientColorUni; | 272 GrGLProgramDataManager::UniformHandle fAmbientColorUni; |
| 273 |
| 274 SkVector fInvNormRotation; |
| 275 GrGLProgramDataManager::UniformHandle fXformUni; |
227 }; | 276 }; |
228 | 277 |
229 void onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) c
onst override { | 278 void onGetGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) c
onst override { |
230 LightingGLFP::GenKey(*this, caps, b); | 279 LightingGLFP::GenKey(*this, caps, b); |
231 } | 280 } |
232 | 281 |
233 const char* name() const override { return "LightingFP"; } | 282 const char* name() const override { return "LightingFP"; } |
234 | 283 |
235 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | 284 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { |
236 inout->mulByUnknownFourComponents(); | 285 inout->mulByUnknownFourComponents(); |
237 } | 286 } |
238 | 287 |
239 const SkVector3& lightDir() const { return fLightDir; } | 288 const SkVector3& lightDir() const { return fLightDir; } |
240 const SkColor3f& lightColor() const { return fLightColor; } | 289 const SkColor3f& lightColor() const { return fLightColor; } |
241 const SkColor3f& ambientColor() const { return fAmbientColor; } | 290 const SkColor3f& ambientColor() const { return fAmbientColor; } |
| 291 const SkVector& invNormRotation() const { return fInvNormRotation; } |
242 | 292 |
243 private: | 293 private: |
244 GrGLFragmentProcessor* onCreateGLInstance() const override { return SkNEW(Li
ghtingGLFP); } | 294 GrGLFragmentProcessor* onCreateGLInstance() const override { return SkNEW(Li
ghtingGLFP); } |
245 | 295 |
246 bool onIsEqual(const GrFragmentProcessor& proc) const override { | 296 bool onIsEqual(const GrFragmentProcessor& proc) const override { |
247 const LightingFP& lightingFP = proc.cast<LightingFP>(); | 297 const LightingFP& lightingFP = proc.cast<LightingFP>(); |
248 return fDeviceTransform == lightingFP.fDeviceTransform && | 298 return fDiffDeviceTransform == lightingFP.fDiffDeviceTransform && |
| 299 fNormDeviceTransform == lightingFP.fNormDeviceTransform && |
249 fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess && | 300 fDiffuseTextureAccess == lightingFP.fDiffuseTextureAccess && |
250 fNormalTextureAccess == lightingFP.fNormalTextureAccess && | 301 fNormalTextureAccess == lightingFP.fNormalTextureAccess && |
251 fLightDir == lightingFP.fLightDir && | 302 fLightDir == lightingFP.fLightDir && |
252 fLightColor == lightingFP.fLightColor && | 303 fLightColor == lightingFP.fLightColor && |
253 fAmbientColor == lightingFP.fAmbientColor; | 304 fAmbientColor == lightingFP.fAmbientColor && |
| 305 fInvNormRotation == lightingFP.fInvNormRotation; |
254 } | 306 } |
255 | 307 |
256 GrCoordTransform fDeviceTransform; | 308 GrCoordTransform fDiffDeviceTransform; |
| 309 GrCoordTransform fNormDeviceTransform; |
257 GrTextureAccess fDiffuseTextureAccess; | 310 GrTextureAccess fDiffuseTextureAccess; |
258 GrTextureAccess fNormalTextureAccess; | 311 GrTextureAccess fNormalTextureAccess; |
259 SkVector3 fLightDir; | 312 SkVector3 fLightDir; |
260 SkColor3f fLightColor; | 313 SkColor3f fLightColor; |
261 SkColor3f fAmbientColor; | 314 SkColor3f fAmbientColor; |
| 315 |
| 316 SkVector fInvNormRotation; |
262 }; | 317 }; |
263 | 318 |
264 //////////////////////////////////////////////////////////////////////////// | 319 //////////////////////////////////////////////////////////////////////////// |
265 | 320 |
| 321 static bool make_mat(const SkBitmap& bm, |
| 322 const SkMatrix& localMatrix1, |
| 323 const SkMatrix* localMatrix2, |
| 324 SkMatrix* result) { |
| 325 |
| 326 result->setIDiv(bm.width(), bm.height()); |
| 327 |
| 328 SkMatrix lmInverse; |
| 329 if (!localMatrix1.invert(&lmInverse)) { |
| 330 return false; |
| 331 } |
| 332 if (localMatrix2) { |
| 333 SkMatrix inv; |
| 334 if (!localMatrix2->invert(&inv)) { |
| 335 return false; |
| 336 } |
| 337 lmInverse.postConcat(inv); |
| 338 } |
| 339 result->preConcat(lmInverse); |
| 340 |
| 341 return true; |
| 342 } |
| 343 |
266 bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint
& paint, | 344 bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint
& paint, |
267 const SkMatrix& viewM, const SkMa
trix* localMatrix, | 345 const SkMatrix& viewM, const SkMa
trix* localMatrix, |
268 GrColor* color, GrProcessorDataMa
nager*, | 346 GrColor* color, GrProcessorDataMa
nager* pdm, |
269 GrFragmentProcessor** fp) const { | 347 GrFragmentProcessor** fp) const { |
270 // we assume diffuse and normal maps have same width and height | 348 // we assume diffuse and normal maps have same width and height |
271 // TODO: support different sizes | 349 // TODO: support different sizes |
272 SkASSERT(fDiffuseMap.width() == fNormalMap.width() && | 350 SkASSERT(fDiffuseMap.width() == fNormalMap.width() && |
273 fDiffuseMap.height() == fNormalMap.height()); | 351 fDiffuseMap.height() == fNormalMap.height()); |
274 SkMatrix matrix; | 352 SkMatrix diffM, normM; |
275 matrix.setIDiv(fDiffuseMap.width(), fDiffuseMap.height()); | 353 |
| 354 if (!make_mat(fDiffuseMap, this->getLocalMatrix(), localMatrix, &diffM)) { |
| 355 return false; |
| 356 } |
276 | 357 |
277 SkMatrix lmInverse; | 358 if (!make_mat(fNormalMap, fNormLocalMatrix, localMatrix, &normM)) { |
278 if (!this->getLocalMatrix().invert(&lmInverse)) { | |
279 return false; | 359 return false; |
280 } | 360 } |
281 if (localMatrix) { | |
282 SkMatrix inv; | |
283 if (!localMatrix->invert(&inv)) { | |
284 return false; | |
285 } | |
286 lmInverse.postConcat(inv); | |
287 } | |
288 matrix.preConcat(lmInverse); | |
289 | 361 |
290 // Must set wrap and filter on the sampler before requesting a texture. In t
wo places below | 362 bool doBicubic; |
291 // we check the matrix scale factors to determine how to interpret the filte
r quality setting. | 363 GrTextureParams::FilterMode diffFilterMode = GrSkFilterQualityToGrFilterMode
( |
292 // This completely ignores the complexity of the drawVertices case where exp
licit local coords | 364 SkTMin(paint.getFilterQuality(), kMedium
_SkFilterQuality), |
293 // are provided by the caller. | 365 viewM, |
294 GrTextureParams::FilterMode textureFilterMode = GrTextureParams::kBilerp_Fil
terMode; | 366 this->getLocalMatrix(), |
295 switch (paint.getFilterQuality()) { | 367 &doBicubic); |
296 case kNone_SkFilterQuality: | 368 SkASSERT(!doBicubic); |
297 textureFilterMode = GrTextureParams::kNone_FilterMode; | |
298 break; | |
299 case kLow_SkFilterQuality: | |
300 textureFilterMode = GrTextureParams::kBilerp_FilterMode; | |
301 break; | |
302 case kMedium_SkFilterQuality:{ | |
303 SkMatrix matrix; | |
304 matrix.setConcat(viewM, this->getLocalMatrix()); | |
305 if (matrix.getMinScale() < SK_Scalar1) { | |
306 textureFilterMode = GrTextureParams::kMipMap_FilterMode; | |
307 } else { | |
308 // Don't trigger MIP level generation unnecessarily. | |
309 textureFilterMode = GrTextureParams::kBilerp_FilterMode; | |
310 } | |
311 break; | |
312 } | |
313 case kHigh_SkFilterQuality: | |
314 default: | |
315 SkErrorInternals::SetError(kInvalidPaint_SkError, | |
316 "Sorry, I don't understand the filtering " | |
317 "mode you asked for. Falling back to " | |
318 "MIPMaps."); | |
319 textureFilterMode = GrTextureParams::kMipMap_FilterMode; | |
320 break; | |
321 | 369 |
322 } | 370 GrTextureParams::FilterMode normFilterMode = GrSkFilterQualityToGrFilterMode
( |
| 371 SkTMin(paint.getFilterQuality(), kMedium
_SkFilterQuality), |
| 372 viewM, |
| 373 fNormLocalMatrix, |
| 374 &doBicubic); |
| 375 SkASSERT(!doBicubic); |
323 | 376 |
324 // TODO: support other tile modes | 377 // TODO: support other tile modes |
325 GrTextureParams params(kClamp_TileMode, textureFilterMode); | 378 GrTextureParams diffParams(kClamp_TileMode, diffFilterMode); |
326 SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, fDi
ffuseMap, ¶ms)); | 379 SkAutoTUnref<GrTexture> diffuseTexture(GrRefCachedBitmapTexture(context, |
| 380 fDiffuseMap,
&diffParams)); |
327 if (!diffuseTexture) { | 381 if (!diffuseTexture) { |
328 SkErrorInternals::SetError(kInternalError_SkError, | 382 SkErrorInternals::SetError(kInternalError_SkError, |
329 "Couldn't convert bitmap to texture."); | 383 "Couldn't convert bitmap to texture."); |
330 return false; | 384 return false; |
331 } | 385 } |
332 | 386 |
333 SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context, fNor
malMap, ¶ms)); | 387 GrTextureParams normParams(kClamp_TileMode, normFilterMode); |
| 388 SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context, |
| 389 fNormalMap, &
normParams)); |
334 if (!normalTexture) { | 390 if (!normalTexture) { |
335 SkErrorInternals::SetError(kInternalError_SkError, | 391 SkErrorInternals::SetError(kInternalError_SkError, |
336 "Couldn't convert bitmap to texture."); | 392 "Couldn't convert bitmap to texture."); |
337 return false; | 393 return false; |
338 } | 394 } |
339 | 395 |
340 *fp = SkNEW_ARGS(LightingFP, (diffuseTexture, normalTexture, matrix, | 396 |
341 fLight.fDirection, fLight.fColor, fAmbientColo
r)); | 397 *fp = SkNEW_ARGS(LightingFP, (pdm, diffuseTexture, normalTexture, |
| 398 diffM, normM, diffParams, normParams, fLights, |
| 399 fInvNormRotation)); |
| 400 |
342 *color = GrColorPackA4(paint.getAlpha()); | 401 *color = GrColorPackA4(paint.getAlpha()); |
343 return true; | 402 return true; |
344 } | 403 } |
345 #else | 404 #else |
346 | 405 |
347 bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint
& paint, | 406 bool SkLightingShaderImpl::asFragmentProcessor(GrContext* context, const SkPaint
& paint, |
348 const SkMatrix& viewM, const SkMa
trix* localMatrix, | 407 const SkMatrix& viewM, const SkMa
trix* localMatrix, |
349 GrColor* color, GrProcessorDataMa
nager*, | 408 GrColor* color, GrProcessorDataMa
nager*, |
350 GrFragmentProcessor** fp) const { | 409 GrFragmentProcessor** fp) const { |
351 SkDEBUGFAIL("Should not call in GPU-less build"); | 410 SkDEBUGFAIL("Should not call in GPU-less build"); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 fFlags = flags; | 443 fFlags = flags; |
385 } | 444 } |
386 | 445 |
387 SkLightingShaderImpl::LightingShaderContext::~LightingShaderContext() { | 446 SkLightingShaderImpl::LightingShaderContext::~LightingShaderContext() { |
388 // The bitmap proc states have been created outside of the context on memory
that will be freed | 447 // The bitmap proc states have been created outside of the context on memory
that will be freed |
389 // elsewhere. Call the destructors but leave the freeing of the memory to th
e caller. | 448 // elsewhere. Call the destructors but leave the freeing of the memory to th
e caller. |
390 fDiffuseState->~SkBitmapProcState(); | 449 fDiffuseState->~SkBitmapProcState(); |
391 fNormalState->~SkBitmapProcState(); | 450 fNormalState->~SkBitmapProcState(); |
392 } | 451 } |
393 | 452 |
394 static inline int light(SkScalar light, int diff, SkScalar NdotL, SkScalar ambie
nt) { | 453 static inline SkPMColor convert(SkColor3f color, U8CPU a) { |
395 SkScalar color = light * diff * NdotL + 255 * ambient; | 454 if (color.fX <= 0.0f) { |
396 if (color <= 0.0f) { | 455 color.fX = 0.0f; |
397 return 0; | 456 } else if (color.fX >= 255.0f) { |
398 } else if (color >= 255.0f) { | 457 color.fX = 255.0f; |
399 return 255; | 458 } |
400 } else { | 459 |
401 return (int) color; | 460 if (color.fY <= 0.0f) { |
402 } | 461 color.fY = 0.0f; |
| 462 } else if (color.fY >= 255.0f) { |
| 463 color.fY = 255.0f; |
| 464 } |
| 465 |
| 466 if (color.fZ <= 0.0f) { |
| 467 color.fZ = 0.0f; |
| 468 } else if (color.fZ >= 255.0f) { |
| 469 color.fZ = 255.0f; |
| 470 } |
| 471 |
| 472 return SkPreMultiplyARGB(a, (int) color.fX, (int) color.fY, (int) color.fZ)
; |
403 } | 473 } |
404 | 474 |
405 // larger is better (fewer times we have to loop), but we shouldn't | 475 // larger is better (fewer times we have to loop), but we shouldn't |
406 // take up too much stack-space (each could here costs 16 bytes) | 476 // take up too much stack-space (each one here costs 16 bytes) |
407 #define TMP_COUNT 16 | 477 #define TMP_COUNT 16 |
408 | 478 |
409 void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y, | 479 void SkLightingShaderImpl::LightingShaderContext::shadeSpan(int x, int y, |
410 SkPMColor result[],
int count) { | 480 SkPMColor result[],
int count) { |
411 const SkLightingShaderImpl& lightShader = static_cast<const SkLightingShader
Impl&>(fShader); | 481 const SkLightingShaderImpl& lightShader = static_cast<const SkLightingShader
Impl&>(fShader); |
412 | 482 |
413 SkPMColor tmpColor[TMP_COUNT], tmpColor2[TMP_COUNT]; | 483 uint32_t tmpColor[TMP_COUNT], tmpNormal[TMP_COUNT]; |
414 SkPMColor tmpNormal[TMP_COUNT], tmpNormal2[TMP_COUNT]; | 484 SkPMColor tmpColor2[2*TMP_COUNT], tmpNormal2[2*TMP_COUNT]; |
415 | 485 |
416 SkBitmapProcState::MatrixProc diffMProc = fDiffuseState->getMatrixProc(); | 486 SkBitmapProcState::MatrixProc diffMProc = fDiffuseState->getMatrixProc(); |
417 SkBitmapProcState::SampleProc32 diffSProc = fDiffuseState->getSampleProc32()
; | 487 SkBitmapProcState::SampleProc32 diffSProc = fDiffuseState->getSampleProc32()
; |
418 | 488 |
419 SkBitmapProcState::MatrixProc normalMProc = fNormalState->getMatrixProc(); | 489 SkBitmapProcState::MatrixProc normalMProc = fNormalState->getMatrixProc(); |
420 SkBitmapProcState::SampleProc32 normalSProc = fNormalState->getSampleProc32(
); | 490 SkBitmapProcState::SampleProc32 normalSProc = fNormalState->getSampleProc32(
); |
421 | 491 |
| 492 int diffMax = fDiffuseState->maxCountForBufferSize(sizeof(tmpColor[0]) * TMP
_COUNT); |
| 493 int normMax = fNormalState->maxCountForBufferSize(sizeof(tmpNormal[0]) * TMP
_COUNT); |
| 494 int max = SkTMin(diffMax, normMax); |
| 495 |
422 SkASSERT(fDiffuseState->fPixmap.addr()); | 496 SkASSERT(fDiffuseState->fPixmap.addr()); |
423 SkASSERT(fNormalState->fPixmap.addr()); | 497 SkASSERT(fNormalState->fPixmap.addr()); |
424 | 498 |
425 SkPoint3 norm; | 499 SkPoint3 norm, xformedNorm; |
426 SkScalar NdotL; | |
427 int r, g, b; | |
428 | 500 |
429 do { | 501 do { |
430 int n = count; | 502 int n = count; |
431 if (n > TMP_COUNT) { | 503 if (n > max) { |
432 n = TMP_COUNT; | 504 n = max; |
433 } | 505 } |
434 | 506 |
435 diffMProc(*fDiffuseState, tmpColor, n, x, y); | 507 diffMProc(*fDiffuseState, tmpColor, n, x, y); |
436 diffSProc(*fDiffuseState, tmpColor, n, tmpColor2); | 508 diffSProc(*fDiffuseState, tmpColor, n, tmpColor2); |
437 | 509 |
438 normalMProc(*fNormalState, tmpNormal, n, x, y); | 510 normalMProc(*fNormalState, tmpNormal, n, x, y); |
439 normalSProc(*fNormalState, tmpNormal, n, tmpNormal2); | 511 normalSProc(*fNormalState, tmpNormal, n, tmpNormal2); |
440 | 512 |
441 for (int i = 0; i < n; ++i) { | 513 for (int i = 0; i < n; ++i) { |
442 SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul | 514 SkASSERT(0xFF == SkColorGetA(tmpNormal2[i])); // opaque -> unpremul |
443 norm.set(SkIntToScalar(SkGetPackedR32(tmpNormal2[i]))-127.0f, | 515 norm.set(SkIntToScalar(SkGetPackedR32(tmpNormal2[i]))-127.0f, |
444 SkIntToScalar(SkGetPackedG32(tmpNormal2[i]))-127.0f, | 516 SkIntToScalar(SkGetPackedG32(tmpNormal2[i]))-127.0f, |
445 SkIntToScalar(SkGetPackedB32(tmpNormal2[i]))-127.0f); | 517 SkIntToScalar(SkGetPackedB32(tmpNormal2[i]))-127.0f); |
446 norm.normalize(); | 518 norm.normalize(); |
447 | 519 |
| 520 xformedNorm.fX = lightShader.fInvNormRotation.fX * norm.fX + |
| 521 lightShader.fInvNormRotation.fY * norm.fY; |
| 522 xformedNorm.fY = lightShader.fInvNormRotation.fX * norm.fX - |
| 523 lightShader.fInvNormRotation.fY * norm.fY; |
| 524 xformedNorm.fZ = norm.fZ; |
| 525 |
448 SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]); | 526 SkColor diffColor = SkUnPreMultiply::PMColorToColor(tmpColor2[i]); |
449 NdotL = norm.dot(lightShader.fLight.fDirection); | |
450 | 527 |
451 // This is all done in linear unpremul color space | 528 SkColor3f accum = SkColor3f::Make(0.0f, 0.0f, 0.0f); |
452 r = light(lightShader.fLight.fColor.fX, SkColorGetR(diffColor), Ndot
L, | 529 // This is all done in linear unpremul color space (each component 0
..255.0f though) |
453 lightShader.fAmbientColor.fX); | 530 for (int l = 0; l < lightShader.fLights->numLights(); ++l) { |
454 g = light(lightShader.fLight.fColor.fY, SkColorGetG(diffColor), Ndot
L, | 531 const SkLight& light = lightShader.fLights->light(l); |
455 lightShader.fAmbientColor.fY); | |
456 b = light(lightShader.fLight.fColor.fZ, SkColorGetB(diffColor), Ndot
L, | |
457 lightShader.fAmbientColor.fZ); | |
458 | 532 |
459 result[i] = SkPreMultiplyARGB(SkColorGetA(diffColor), r, g, b); | 533 if (SkLight::kAmbient_LightType == light.type()) { |
| 534 accum += light.color().makeScale(255.0f); |
| 535 } else { |
| 536 SkScalar NdotL = xformedNorm.dot(light.dir()); |
| 537 if (NdotL < 0.0f) { |
| 538 NdotL = 0.0f; |
| 539 } |
| 540 |
| 541 accum.fX += light.color().fX * SkColorGetR(diffColor) * Ndot
L; |
| 542 accum.fY += light.color().fY * SkColorGetG(diffColor) * Ndot
L; |
| 543 accum.fZ += light.color().fZ * SkColorGetB(diffColor) * Ndot
L; |
| 544 } |
| 545 } |
| 546 |
| 547 result[i] = convert(accum, SkColorGetA(diffColor)); |
460 } | 548 } |
461 | 549 |
462 result += n; | 550 result += n; |
463 x += n; | 551 x += n; |
464 count -= n; | 552 count -= n; |
465 } while (count > 0); | 553 } while (count > 0); |
466 } | 554 } |
467 | 555 |
468 //////////////////////////////////////////////////////////////////////////// | 556 //////////////////////////////////////////////////////////////////////////// |
469 | 557 |
470 #ifndef SK_IGNORE_TO_STRING | 558 #ifndef SK_IGNORE_TO_STRING |
471 void SkLightingShaderImpl::toString(SkString* str) const { | 559 void SkLightingShaderImpl::toString(SkString* str) const { |
472 str->appendf("LightingShader: ()"); | 560 str->appendf("LightingShader: ()"); |
473 } | 561 } |
474 #endif | 562 #endif |
475 | 563 |
476 SkFlattenable* SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) { | 564 SkFlattenable* SkLightingShaderImpl::CreateProc(SkReadBuffer& buf) { |
477 SkMatrix localMatrix; | 565 SkMatrix diffLocalM; |
478 buf.readMatrix(&localMatrix); | 566 bool hasDiffLocalM = buf.readBool(); |
| 567 if (hasDiffLocalM) { |
| 568 buf.readMatrix(&diffLocalM); |
| 569 } else { |
| 570 diffLocalM.reset(); |
| 571 } |
| 572 |
| 573 SkMatrix normLocalM; |
| 574 bool hasNormLocalM = buf.readBool(); |
| 575 if (hasNormLocalM) { |
| 576 buf.readMatrix(&normLocalM); |
| 577 } else { |
| 578 normLocalM.reset(); |
| 579 } |
479 | 580 |
480 SkBitmap diffuse; | 581 SkBitmap diffuse; |
481 if (!buf.readBitmap(&diffuse)) { | 582 if (!buf.readBitmap(&diffuse)) { |
482 return NULL; | 583 return NULL; |
483 } | 584 } |
484 diffuse.setImmutable(); | 585 diffuse.setImmutable(); |
485 | 586 |
486 SkBitmap normal; | 587 SkBitmap normal; |
487 if (!buf.readBitmap(&normal)) { | 588 if (!buf.readBitmap(&normal)) { |
488 return NULL; | 589 return NULL; |
489 } | 590 } |
490 normal.setImmutable(); | 591 normal.setImmutable(); |
491 | 592 |
492 SkLightingShader::Light light; | 593 int numLights = buf.readInt(); |
493 if (!buf.readScalarArray(&light.fDirection.fX, 3)) { | 594 |
494 return NULL; | 595 SkLightingShader::Lights::Builder builder; |
495 } | 596 |
496 if (!buf.readScalarArray(&light.fColor.fX, 3)) { | 597 for (int l = 0; l < numLights; ++l) { |
497 return NULL; | 598 bool isAmbient = buf.readBool(); |
| 599 |
| 600 SkColor3f color; |
| 601 if (!buf.readScalarArray(&color.fX, 3)) { |
| 602 return NULL; |
| 603 } |
| 604 |
| 605 if (isAmbient) { |
| 606 builder.add(SkLight(color)); |
| 607 } else { |
| 608 SkVector3 dir; |
| 609 if (!buf.readScalarArray(&dir.fX, 3)) { |
| 610 return NULL; |
| 611 } |
| 612 builder.add(SkLight(color, dir)); |
| 613 } |
498 } | 614 } |
499 | 615 |
500 SkColor3f ambient; | 616 SkAutoTUnref<const SkLightingShader::Lights> lights(builder.finish()); |
501 if (!buf.readScalarArray(&ambient.fX, 3)) { | |
502 return NULL; | |
503 } | |
504 | 617 |
505 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, light, ambient, &l
ocalMatrix)); | 618 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, lights, |
| 619 SkVector::Make(1.0f, 0.0f), |
| 620 &diffLocalM, &normLocalM)); |
506 } | 621 } |
507 | 622 |
508 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { | 623 void SkLightingShaderImpl::flatten(SkWriteBuffer& buf) const { |
509 buf.writeMatrix(this->getLocalMatrix()); | 624 this->INHERITED::flatten(buf); |
| 625 |
| 626 bool hasNormLocalM = !fNormLocalMatrix.isIdentity(); |
| 627 buf.writeBool(hasNormLocalM); |
| 628 if (hasNormLocalM) { |
| 629 buf.writeMatrix(fNormLocalMatrix); |
| 630 } |
510 | 631 |
511 buf.writeBitmap(fDiffuseMap); | 632 buf.writeBitmap(fDiffuseMap); |
512 buf.writeBitmap(fNormalMap); | 633 buf.writeBitmap(fNormalMap); |
513 buf.writeScalarArray(&fLight.fDirection.fX, 3); | 634 |
514 buf.writeScalarArray(&fLight.fColor.fX, 3); | 635 buf.writeInt(fLights->numLights()); |
515 buf.writeScalarArray(&fAmbientColor.fX, 3); | 636 for (int l = 0; l < fLights->numLights(); ++l) { |
| 637 const SkLight& light = fLights->light(l); |
| 638 |
| 639 bool isAmbient = SkLight::kAmbient_LightType == light.type(); |
| 640 |
| 641 buf.writeBool(isAmbient); |
| 642 buf.writeScalarArray(&light.color().fX, 3); |
| 643 if (!isAmbient) { |
| 644 buf.writeScalarArray(&light.dir().fX, 3); |
| 645 } |
| 646 } |
| 647 } |
| 648 |
| 649 bool SkLightingShaderImpl::computeNormTotalInverse(const ContextRec& rec, |
| 650 SkMatrix* normTotalInverse) c
onst { |
| 651 SkMatrix total; |
| 652 total.setConcat(*rec.fMatrix, fNormLocalMatrix); |
| 653 |
| 654 const SkMatrix* m = &total; |
| 655 if (rec.fLocalMatrix) { |
| 656 total.setConcat(*m, *rec.fLocalMatrix); |
| 657 m = &total; |
| 658 } |
| 659 return m->invert(normTotalInverse); |
516 } | 660 } |
517 | 661 |
518 SkShader::Context* SkLightingShaderImpl::onCreateContext(const ContextRec& rec, | 662 SkShader::Context* SkLightingShaderImpl::onCreateContext(const ContextRec& rec, |
519 void* storage) const { | 663 void* storage) const { |
520 | 664 |
521 SkMatrix totalInverse; | 665 SkMatrix diffTotalInv; |
522 // Do this first, so we know the matrix can be inverted. | 666 // computeTotalInverse was called in SkShader::createContext so we know it w
ill succeed |
523 if (!this->computeTotalInverse(rec, &totalInverse)) { | 667 SkAssertResult(this->computeTotalInverse(rec, &diffTotalInv)); |
| 668 |
| 669 SkMatrix normTotalInv; |
| 670 if (!this->computeNormTotalInverse(rec, &normTotalInv)) { |
524 return NULL; | 671 return NULL; |
525 } | 672 } |
526 | 673 |
527 void* diffuseStateStorage = (char*)storage + sizeof(LightingShaderContext); | 674 void* diffuseStateStorage = (char*)storage + sizeof(LightingShaderContext); |
528 SkBitmapProcState* diffuseState = SkNEW_PLACEMENT(diffuseStateStorage, SkBit
mapProcState); | 675 SkBitmapProcState* diffuseState = SkNEW_PLACEMENT(diffuseStateStorage, SkBit
mapProcState); |
529 SkASSERT(diffuseState); | 676 SkASSERT(diffuseState); |
530 | 677 |
531 diffuseState->fTileModeX = SkShader::kClamp_TileMode; | 678 diffuseState->fTileModeX = SkShader::kClamp_TileMode; |
532 diffuseState->fTileModeY = SkShader::kClamp_TileMode; | 679 diffuseState->fTileModeY = SkShader::kClamp_TileMode; |
533 diffuseState->fOrigBitmap = fDiffuseMap; | 680 diffuseState->fOrigBitmap = fDiffuseMap; |
534 if (!diffuseState->chooseProcs(totalInverse, *rec.fPaint)) { | 681 if (!diffuseState->chooseProcs(diffTotalInv, *rec.fPaint)) { |
535 diffuseState->~SkBitmapProcState(); | 682 diffuseState->~SkBitmapProcState(); |
536 return NULL; | 683 return NULL; |
537 } | 684 } |
538 | 685 |
539 void* normalStateStorage = (char*)storage + sizeof(LightingShaderContext) +
sizeof(SkBitmapProcState); | 686 void* normalStateStorage = (char*)storage + sizeof(LightingShaderContext) +
sizeof(SkBitmapProcState); |
540 SkBitmapProcState* normalState = SkNEW_PLACEMENT(normalStateStorage, SkBitma
pProcState); | 687 SkBitmapProcState* normalState = SkNEW_PLACEMENT(normalStateStorage, SkBitma
pProcState); |
541 SkASSERT(normalState); | 688 SkASSERT(normalState); |
542 | 689 |
543 normalState->fTileModeX = SkShader::kClamp_TileMode; | 690 normalState->fTileModeX = SkShader::kClamp_TileMode; |
544 normalState->fTileModeY = SkShader::kClamp_TileMode; | 691 normalState->fTileModeY = SkShader::kClamp_TileMode; |
545 normalState->fOrigBitmap = fNormalMap; | 692 normalState->fOrigBitmap = fNormalMap; |
546 if (!normalState->chooseProcs(totalInverse, *rec.fPaint)) { | 693 if (!normalState->chooseProcs(normTotalInv, *rec.fPaint)) { |
547 diffuseState->~SkBitmapProcState(); | 694 diffuseState->~SkBitmapProcState(); |
548 normalState->~SkBitmapProcState(); | 695 normalState->~SkBitmapProcState(); |
549 return NULL; | 696 return NULL; |
550 } | 697 } |
551 | 698 |
552 return SkNEW_PLACEMENT_ARGS(storage, LightingShaderContext, (*this, rec, | 699 return SkNEW_PLACEMENT_ARGS(storage, LightingShaderContext, (*this, rec, |
553 diffuseState, n
ormalState)); | 700 diffuseState, n
ormalState)); |
554 } | 701 } |
555 | 702 |
556 /////////////////////////////////////////////////////////////////////////////// | 703 /////////////////////////////////////////////////////////////////////////////// |
557 | 704 |
558 static bool bitmap_is_too_big(const SkBitmap& bm) { | 705 static bool bitmap_is_too_big(const SkBitmap& bm) { |
559 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, as it | 706 // SkBitmapProcShader stores bitmap coordinates in a 16bit buffer, as it |
560 // communicates between its matrix-proc and its sampler-proc. Until we can | 707 // communicates between its matrix-proc and its sampler-proc. Until we can |
561 // widen that, we have to reject bitmaps that are larger. | 708 // widen that, we have to reject bitmaps that are larger. |
562 // | 709 // |
563 static const int kMaxSize = 65535; | 710 static const int kMaxSize = 65535; |
564 | 711 |
565 return bm.width() > kMaxSize || bm.height() > kMaxSize; | 712 return bm.width() > kMaxSize || bm.height() > kMaxSize; |
566 } | 713 } |
567 | 714 |
568 SkShader* SkLightingShader::Create(const SkBitmap& diffuse, const SkBitmap& norm
al, | 715 SkShader* SkLightingShader::Create(const SkBitmap& diffuse, const SkBitmap& norm
al, |
569 const SkLightingShader::Light& light, | 716 const Lights* lights, |
570 const SkColor3f& ambient, | 717 const SkVector& invNormRotation, |
571 const SkMatrix* localMatrix) { | 718 const SkMatrix* diffLocalM, const SkMatrix* n
ormLocalM) { |
572 if (diffuse.isNull() || bitmap_is_too_big(diffuse) || | 719 if (diffuse.isNull() || bitmap_is_too_big(diffuse) || |
573 normal.isNull() || bitmap_is_too_big(normal) || | 720 normal.isNull() || bitmap_is_too_big(normal) || |
574 diffuse.width() != normal.width() || | 721 diffuse.width() != normal.width() || |
575 diffuse.height() != normal.height()) { | 722 diffuse.height() != normal.height()) { |
576 return nullptr; | 723 return nullptr; |
577 } | 724 } |
578 | 725 |
579 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, light, ambient, lo
calMatrix)); | 726 SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1)); |
| 727 |
| 728 return SkNEW_ARGS(SkLightingShaderImpl, (diffuse, normal, lights, |
| 729 invNormRotation, diffLocalM, normLo
calM)); |
580 } | 730 } |
581 | 731 |
582 /////////////////////////////////////////////////////////////////////////////// | 732 /////////////////////////////////////////////////////////////////////////////// |
583 | 733 |
584 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) | 734 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader) |
585 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) | 735 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkLightingShaderImpl) |
586 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | 736 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END |
587 | 737 |
588 /////////////////////////////////////////////////////////////////////////////// | 738 /////////////////////////////////////////////////////////////////////////////// |
OLD | NEW |