OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkBitmapProcShader.h" | |
9 #include "SkError.h" | |
10 #include "SkErrorInternals.h" | |
11 #include "SkLightingShader.h" | |
12 #include "SkReadBuffer.h" | |
13 #include "SkWriteBuffer.h" | |
14 | |
15 // Genretating vtable | |
16 SkLightingShader::NormalSource::~NormalSource() {} | |
17 | |
18 /////////////////////////////////////////////////////////////////////////////// | |
19 | |
20 class SK_API NormalMapSourceImpl : public SkLightingShader::NormalSource { | |
21 public: | |
22 NormalMapSourceImpl(const SkBitmap &normal, const SkVector &invNormRotation, | |
23 const SkMatrix *normLocalM) | |
24 : fNormalMap(normal) | |
25 , fInvNormRotation(invNormRotation) { | |
26 | |
27 if (normLocalM) { | |
28 fNormLocalMatrix = *normLocalM; | |
29 } else { | |
30 fNormLocalMatrix.reset(); | |
31 } | |
32 // Pre-cache so future calls to fNormLocalMatrix.getType() are threadsaf
e. | |
33 (void)fNormLocalMatrix.getType(); | |
34 } | |
35 | |
36 #if SK_SUPPORT_GPU | |
37 sk_sp<GrFragmentProcessor> asFragmentProcessor(GrContext*, | |
38 const SkMatrix& viewM, | |
39 const SkMatrix* localMatrix, | |
40 SkFilterQuality, | |
41 SkSourceGammaTreatment) const
override; | |
42 #endif | |
43 | |
44 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(NormalMapSourceImpl) | |
45 | |
46 protected: | |
47 void flatten(SkWriteBuffer& buf) const override; | |
48 | |
49 private: | |
50 SkBitmap fNormalMap; | |
51 SkMatrix fNormLocalMatrix; | |
52 SkVector fInvNormRotation; | |
53 | |
54 friend class SkLightingShader::NormalMapSource; | |
55 | |
56 typedef SkLightingShader::NormalSource INHERITED; | |
57 }; | |
58 | |
59 //////////////////////////////////////////////////////////////////////////// | |
60 | |
61 #if SK_SUPPORT_GPU | |
62 | |
63 #include "GrCoordTransform.h" | |
64 #include "GrInvariantOutput.h" | |
65 #include "GrTextureParams.h" | |
66 #include "glsl/GrGLSLFragmentProcessor.h" | |
67 #include "glsl/GrGLSLFragmentShaderBuilder.h" | |
68 #include "SkGr.h" | |
69 | |
70 class NormalMapFP : public GrFragmentProcessor { | |
71 public: | |
72 NormalMapFP(GrTexture* normal, const SkMatrix& normMatrix, const GrTexturePa
rams& normParams, | |
73 const SkVector& invNormRotation) | |
74 : fNormDeviceTransform(kLocal_GrCoordSet, normMatrix, normal, normParams
.filterMode()) | |
75 , fNormalTextureAccess(normal, normParams) | |
76 , fInvNormRotation(invNormRotation) { | |
77 this->addCoordTransform(&fNormDeviceTransform); | |
78 this->addTextureAccess(&fNormalTextureAccess); | |
79 | |
80 this->initClassID<NormalMapFP>(); | |
81 } | |
82 | |
83 class GLSLNormalMapFP : public GrGLSLFragmentProcessor { | |
84 public: | |
85 GLSLNormalMapFP() { | |
86 fInvNormRotation.set(0.0f, 0.0f); | |
87 } | |
88 | |
89 void emitCode(EmitArgs& args) override { | |
90 | |
91 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder; | |
92 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; | |
93 | |
94 // add uniform | |
95 const char* xformUniName = nullptr; | |
96 fXformUni = uniformHandler->addUniform(kFragment_GrShaderFlag, | |
97 kVec2f_GrSLType, kDefault_GrS
LPrecision, | |
98 "Xform", &xformUniName); | |
99 | |
100 fragBuilder->codeAppend("vec4 normalColor = "); | |
101 fragBuilder->appendTextureLookup(args.fTexSamplers[0], | |
102 args.fCoords[0].c_str(), | |
103 args.fCoords[0].getType()); | |
104 fragBuilder->codeAppend(";"); | |
105 | |
106 fragBuilder->codeAppend("vec3 normal = normalColor.rgb - vec3(0.5);"
); | |
107 | |
108 // TODO: inverse map the light direction vectors in the vertex shade
r rather than | |
109 // transforming all the normals here! | |
110 fragBuilder->codeAppendf( | |
111 "mat3 m = mat3(%s.x, -%s.y, 0.0, %s.y, %s.x, 0.0, 0.0, 0.0,
1.0);", | |
112 xformUniName, xformUniName, xformUniName, xformUniName); | |
113 | |
114 fragBuilder->codeAppend("normal = normalize(m*normal);"); | |
115 fragBuilder->codeAppendf("%s = vec4(normal, 0);", args.fOutputColor)
; | |
116 } | |
117 | |
118 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, | |
119 GrProcessorKeyBuilder* b) { | |
120 b->add32(0x0); | |
121 } | |
122 | |
123 protected: | |
124 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor&
proc) override { | |
125 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>(); | |
126 | |
127 const SkVector& invNormRotation = normalMapFP.invNormRotation(); | |
128 if (invNormRotation != fInvNormRotation) { | |
129 pdman.set2fv(fXformUni, 1, &invNormRotation.fX); | |
130 fInvNormRotation = invNormRotation; | |
131 } | |
132 } | |
133 | |
134 private: | |
135 SkVector fInvNormRotation; | |
136 GrGLSLProgramDataManager::UniformHandle fXformUni; | |
137 }; | |
138 | |
139 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b)
const override { | |
140 GLSLNormalMapFP::GenKey(*this, caps, b); | |
141 } | |
142 | |
143 const char* name() const override { return "NormalMapFP"; } | |
144 | |
145 void onComputeInvariantOutput(GrInvariantOutput* inout) const override { | |
146 inout->setToUnknown(GrInvariantOutput::ReadInput::kWillNot_ReadInput); | |
147 } | |
148 | |
149 const SkVector& invNormRotation() const { return fInvNormRotation; } | |
150 | |
151 private: | |
152 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override { return new
GLSLNormalMapFP; } | |
153 | |
154 bool onIsEqual(const GrFragmentProcessor& proc) const override { | |
155 const NormalMapFP& normalMapFP = proc.cast<NormalMapFP>(); | |
156 return fNormDeviceTransform == normalMapFP.fNormDeviceTransform && | |
157 fNormalTextureAccess == normalMapFP.fNormalTextureAccess && | |
158 fInvNormRotation == normalMapFP.fInvNormRotation; | |
159 } | |
160 | |
161 GrCoordTransform fNormDeviceTransform; | |
162 GrTextureAccess fNormalTextureAccess; | |
163 SkVector fInvNormRotation; | |
164 }; | |
165 | |
166 // TODO same code at SkLightingShader.cpp. Refactor to common source! | |
167 static bool make_mat(const SkBitmap& bm, | |
168 const SkMatrix& localMatrix1, | |
169 const SkMatrix* localMatrix2, | |
170 SkMatrix* result) { | |
171 | |
172 result->setIDiv(bm.width(), bm.height()); | |
173 | |
174 SkMatrix lmInverse; | |
175 if (!localMatrix1.invert(&lmInverse)) { | |
176 return false; | |
177 } | |
178 if (localMatrix2) { | |
179 SkMatrix inv; | |
180 if (!localMatrix2->invert(&inv)) { | |
181 return false; | |
182 } | |
183 lmInverse.postConcat(inv); | |
184 } | |
185 result->preConcat(lmInverse); | |
186 | |
187 return true; | |
188 } | |
189 | |
190 sk_sp<GrFragmentProcessor> NormalMapSourceImpl::asFragmentProcessor( | |
191 GrContext *context, | |
192 const SkMatrix &viewM, | |
193 const SkMatrix *localMatrix
, | |
194 SkFilterQuality filterQuali
ty, | |
195 SkSourceGammaTreatment gamm
aTreatment) const { | |
196 | |
197 // TODO Here, the old code was checking that diffuse map and normal map are
same size, that | |
198 // will be addressed when diffuse maps are factored out of SkLightingSh
ader in a future CL | |
199 | |
200 SkMatrix normM; | |
201 if (!make_mat(fNormalMap, fNormLocalMatrix, localMatrix, &normM)) { | |
202 return nullptr; | |
203 } | |
204 | |
205 bool doBicubic; | |
206 GrTextureParams::FilterMode normFilterMode = GrSkFilterQualityToGrFilterMode
( | |
207 SkTMin(filterQuality, kMedium_SkFilterQuality), | |
208 viewM, | |
209 fNormLocalMatrix, | |
210 &doBicubic); | |
211 SkASSERT(!doBicubic); | |
212 | |
213 // TODO: support other tile modes | |
214 GrTextureParams normParams(SkShader::kClamp_TileMode, normFilterMode); | |
215 SkAutoTUnref<GrTexture> normalTexture(GrRefCachedBitmapTexture(context, | |
216 fNormalMap, | |
217 normParams, | |
218 gammaTreatmen
t)); | |
219 if (!normalTexture) { | |
220 SkErrorInternals::SetError(kInternalError_SkError, "Couldn't convert bit
map to texture."); | |
221 return nullptr; | |
222 } | |
223 | |
224 return sk_make_sp<NormalMapFP>(normalTexture, normM, normParams, fInvNormRot
ation); | |
225 } | |
226 | |
227 #endif // SK_SUPPORT_GPU | |
228 | |
229 //////////////////////////////////////////////////////////////////////////// | |
230 | |
231 sk_sp<SkFlattenable> NormalMapSourceImpl::CreateProc(SkReadBuffer& buf) { | |
232 | |
233 SkMatrix normLocalM; | |
234 bool hasNormLocalM = buf.readBool(); | |
235 if (hasNormLocalM) { | |
236 buf.readMatrix(&normLocalM); | |
237 } else { | |
238 normLocalM.reset(); | |
239 } | |
240 | |
241 SkBitmap normal; | |
242 if (!buf.readBitmap(&normal)) { | |
243 return nullptr; | |
244 } | |
245 normal.setImmutable(); | |
246 | |
247 SkVector invNormRotation = {1,0}; | |
248 if (!buf.isVersionLT(SkReadBuffer::kLightingShaderWritesInvNormRotation)) { | |
249 invNormRotation = buf.readPoint(); | |
250 } | |
251 | |
252 return sk_make_sp<NormalMapSourceImpl>(normal, invNormRotation, &normLocalM)
; | |
253 } | |
254 | |
255 void NormalMapSourceImpl::flatten(SkWriteBuffer& buf) const { | |
256 this->INHERITED::flatten(buf); | |
257 | |
258 bool hasNormLocalM = !fNormLocalMatrix.isIdentity(); | |
259 buf.writeBool(hasNormLocalM); | |
260 if (hasNormLocalM) { | |
261 buf.writeMatrix(fNormLocalMatrix); | |
262 } | |
263 | |
264 buf.writeBitmap(fNormalMap); | |
265 buf.writePoint(fInvNormRotation); | |
266 } | |
267 | |
268 //////////////////////////////////////////////////////////////////////////// | |
269 | |
270 sk_sp<SkLightingShader::NormalSource> SkLightingShader::NormalMapSource::Make( | |
271 const SkBitmap &normal, const SkVector &invNormRotation, const SkMatrix
*normLocalM) { | |
272 | |
273 // TODO not checking normal and diffuse maps to be same size, will be addres
sed when diffuse | |
274 // maps are factored out of SkLightingShader in a future CL | |
275 if (normal.isNull() || SkBitmapProcShader::BitmapIsTooBig(normal)) { | |
276 return nullptr; | |
277 } | |
278 | |
279 SkASSERT(SkScalarNearlyEqual(invNormRotation.lengthSqd(), SK_Scalar1)); | |
280 | |
281 return sk_make_sp<NormalMapSourceImpl>(normal, invNormRotation, normLocalM); | |
282 } | |
283 | |
284 //////////////////////////////////////////////////////////////////////////// | |
285 | |
286 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkLightingShader::NormalMapSource) | |
287 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(NormalMapSourceImpl) | |
288 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END | |
289 | |
290 //////////////////////////////////////////////////////////////////////////// | |
OLD | NEW |