Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: src/core/SkRadialShadowMapShader.cpp

Issue 2311223004: added in radial shadows (Closed)
Patch Set: made req changes Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "SkRadialShadowMapShader.h"
9 #include "SkLights.h"
10 #include "SkPoint3.h"
11 #include "SkReadBuffer.h"
12 #include "SkShadowShader.h"
13
14 ////////////////////////////////////////////////////////////////////////////
15 #ifdef SK_EXPERIMENTAL_SHADOWING
16
17
18 /** \class SkRadialShadowMapShaderImpl
19 This subclass of shader applies shadowing radially around a light
20 */
21 class SkRadialShadowMapShaderImpl : public SkShader {
22 public:
23 /** Create a new shadowing shader that shadows radially around a light
24 */
25 SkRadialShadowMapShaderImpl(sk_sp<SkShader> occluderShader,
26 sk_sp<SkLights> lights,
27 int diffuseWidth, int diffuseHeight)
28 : fOccluderShader(std::move(occluderShader))
29 , fLight(std::move(lights))
30 , fWidth(diffuseWidth)
31 , fHeight(diffuseHeight) { }
32
33 bool isOpaque() const override;
34
35 #if SK_SUPPORT_GPU
36 sk_sp<GrFragmentProcessor> asFragmentProcessor(const AsFPArgs&) const overri de;
37 #endif
38
39 class ShadowMap2DShaderContext : public SkShader::Context {
40 public:
41 // The context takes ownership of the states. It will call their destruc tors
42 // but will NOT free the memory.
43 ShadowMap2DShaderContext(const SkRadialShadowMapShaderImpl&, const Conte xtRec&,
44 SkShader::Context* occluderContext,
45 void* heapAllocated);
46
47 ~ShadowMap2DShaderContext() override;
48
49 void shadeSpan(int x, int y, SkPMColor[], int count) override;
50
51 uint32_t getFlags() const override { return fFlags; }
52
53 private:
54 SkShader::Context* fOccluderContext;
55 uint32_t fFlags;
56
57 void* fHeapAllocated;
58
59 typedef SkShader::Context INHERITED;
60 };
61
62 SK_TO_STRING_OVERRIDE()
63 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkRadialShadowMapShaderI mpl)
64
65 protected:
66 void flatten(SkWriteBuffer&) const override;
67 size_t onContextSize(const ContextRec&) const override;
68 Context* onCreateContext(const ContextRec&, void*) const override;
69
70 private:
71 sk_sp<SkShader> fOccluderShader;
72 sk_sp<SkLights> fLight;
73
74 int fWidth;
75 int fHeight;
76
77 friend class SkRadialShadowMapShader;
78
79 typedef SkShader INHERITED;
80 };
81
82 ////////////////////////////////////////////////////////////////////////////
83
84 #if SK_SUPPORT_GPU
85
86 #include "GrContext.h"
87 #include "GrCoordTransform.h"
88 #include "GrFragmentProcessor.h"
89 #include "glsl/GrGLSLFragmentProcessor.h"
90 #include "glsl/GrGLSLFragmentShaderBuilder.h"
91 #include "SkGr.h"
92 #include "SkGrPriv.h"
93 #include "SkImage_Base.h"
94 #include "GrInvariantOutput.h"
95 #include "SkSpecialImage.h"
96
97 class RadialShadowMapFP : public GrFragmentProcessor {
98 public:
99 RadialShadowMapFP(sk_sp<GrFragmentProcessor> occluder,
100 sk_sp<SkLights> light,
101 int diffuseWidth, int diffuseHeight,
102 GrContext* context) {
robertphillips 2016/09/07 17:11:55 fLightPos
103 flightPos = light->light(0).pos();
104
105 fWidth = diffuseWidth;
106 fHeight = diffuseHeight;
107
108 this->registerChildProcessor(std::move(occluder));
109 this->initClassID<RadialShadowMapFP>();
110 }
111
112 class GLSLRadialShadowMapFP : public GrGLSLFragmentProcessor {
113 public:
114 GLSLRadialShadowMapFP() { }
115
116 void emitCode(EmitArgs& args) override {
117
118 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
119 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
120
121 const char* lightPosUniName = nullptr;
122
123 flightPosUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
124 kVec3f_GrSLType,
125 kDefault_GrSLPrecision,
126 "lightPos",
127 &lightPosUniName);
128
129 const char* widthUniName = nullptr;
130 const char* heightUniName = nullptr;
131
132 fWidthUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
133 kInt_GrSLType,
134 kDefault_GrSLPrecision,
135 "width", &widthUniName);
136 fHeightUni = uniformHandler->addUniform(kFragment_GrShaderFlag,
137 kInt_GrSLType,
138 kDefault_GrSLPrecision,
139 "height", &heightUniName);
140
141
142 SkString occluder("occluder");
143 this->emitChild(0, nullptr, &occluder, args);
144
145 // the above isn't good enough. so we need to modify the indexing co ordinate as below
146 fragBuilder->codeAppendf("float distHere;");
147 fragBuilder->codeAppendf("float closestDistHere = 0;");
148 fragBuilder->codeAppendf("vec2 coords = vMatrixCoord_0_0_Stage0;");
149 fragBuilder->codeAppendf("coords.y = 0;");
150 fragBuilder->codeAppendf("vec2 destCoords = vec2(0,0);");
151 fragBuilder->codeAppendf("float step = 1 / %s;", heightUniName);
152
153 // assume that we are at 0, 0 light pos
154 // TODO use correct light positions
155
156 // this goes through each depth value in the final output buffer,
157 // basically raycasting outwards, and finding the first collision.
158 fragBuilder->codeAppendf("for (coords.y = 0; coords.y <= 1; coords.y += step) {");
159
160 fragBuilder->codeAppendf("vec2 norm = coords * 2.0 - 1.0;");
161 fragBuilder->codeAppendf("float theta = 3.1415 * 1.5 + norm.x * 3.1415;");
162 fragBuilder->codeAppendf("float r = coords.y;");
163 fragBuilder->codeAppendf("destCoords = "
164 "vec2(-r * sin(theta), -r * cos(theta)) /2.0 + 0.5;");
165 fragBuilder->codeAppendf("distHere = texture(uTextureSampler0_St age1, "
166 "destCoords).b;");
167 fragBuilder->codeAppendf("if (distHere > 0.0) {"
168 "closestDistHere = 1-coords.y;"
169 "break;}");
170 fragBuilder->codeAppendf("}");
171
172 fragBuilder->codeAppendf("%s = vec4(vec3(closestDistHere),1);", args .fOutputColor);
173 }
174
175 static void GenKey(const GrProcessor& proc, const GrGLSLCaps&,
176 GrProcessorKeyBuilder* b) {
177 b->add32(0); // nothing to add here
178 }
179
180 protected:
181 void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {
182 const RadialShadowMapFP &RadialShadowMapFP = proc.cast<RadialShadowM apFP>();
183
184 const SkVector3& lightPos = RadialShadowMapFP.lightPos();
185 if (lightPos != flightPos) {
186 pdman.set3fv(flightPosUni, 1, &lightPos.fX);
187 flightPos = lightPos;
188 }
189
190 int width = RadialShadowMapFP.width();
191 if (width != fWidth) {
192 pdman.set1i(fWidthUni, width);
193 fWidth = width;
194 }
195 int height = RadialShadowMapFP.height();
196 if (height != fHeight) {
197 pdman.set1i(fHeightUni, height);
198 fHeight = height;
199 }
200 }
201
202 private:
robertphillips 2016/09/07 17:11:55 fLightPos & fLightPosUni
vjiaoblack 2016/09/08 16:40:13 Done.
203 SkVector3 flightPos;
204 GrGLSLProgramDataManager::UniformHandle flightPosUni;
205
206 int fWidth;
207 GrGLSLProgramDataManager::UniformHandle fWidthUni;
208 int fHeight;
209 GrGLSLProgramDataManager::UniformHandle fHeightUni;
210 };
211
212 void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override {
213 GLSLRadialShadowMapFP::GenKey(*this, caps, b);
214 }
215
216 const char* name() const override { return "RadialShadowMapFP"; }
217
218 void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
219 inout->mulByUnknownFourComponents();
220 }
221 const SkVector3& lightPos() const {
222 return flightPos;
223 }
224
robertphillips 2016/09/07 17:11:55 add space between '{' & 'r' in these two
vjiaoblack 2016/09/08 16:40:13 Done.
225 int width() const {return fWidth; }
226 int height() const {return fHeight; }
227
228 private:
229 GrGLSLFragmentProcessor* onCreateGLSLInstance() const override {
230 return new GLSLRadialShadowMapFP;
231 }
232
233 bool onIsEqual(const GrFragmentProcessor& proc) const override {
234 const RadialShadowMapFP& RadialShadowMapFP = proc.cast<RadialShadowMapFP >();
235
236 if (fWidth != RadialShadowMapFP.fWidth || fHeight != RadialShadowMapFP.f Height) {
237 return false;
238 }
239
240 if (flightPos != RadialShadowMapFP.flightPos) {
241 return false;
242 }
243
244 return true;
245 }
246
247 SkVector3 flightPos;
248
249 int fHeight;
250 int fWidth;
251 };
252
253 ////////////////////////////////////////////////////////////////////////////
254
255 sk_sp<GrFragmentProcessor>
robertphillips 2016/09/07 17:11:55 can you put the parameters on their own line rathe
vjiaoblack 2016/09/08 16:40:13 Done.
256 SkRadialShadowMapShaderImpl::asFragmentProcessor(const AsFPArgs& fpargs) const {
257
258 sk_sp<GrFragmentProcessor> occluderFP = fOccluderShader->asFragmentProcessor (fpargs);
259
robertphillips 2016/09/07 17:11:55 shadowFP
vjiaoblack 2016/09/08 16:40:13 Done.
260 sk_sp<GrFragmentProcessor> shadowfp = sk_make_sp<RadialShadowMapFP>(std::mov e(occluderFP),
robertphillips 2016/09/07 17:11:55 line these 3 up with the first one You're moving
vjiaoblack 2016/09/08 16:40:13 My bad.
261 std::move(fLight) ,
262 fWidth, fHeight,
263 fpargs.fContext);
264 return shadowfp;
265 }
266
267 #endif
268
269 ////////////////////////////////////////////////////////////////////////////
270
271 bool SkRadialShadowMapShaderImpl::isOpaque() const {
272 return fOccluderShader->isOpaque();
273 }
274
275 SkRadialShadowMapShaderImpl::ShadowMap2DShaderContext::ShadowMap2DShaderContext(
276 const SkRadialShadowMapShaderImpl& shader, const ContextRec& rec,
277 SkShader::Context* occluderContext,
278 void* heapAllocated)
279 : INHERITED(shader, rec)
280 , fOccluderContext(occluderContext)
281 , fHeapAllocated(heapAllocated) {
282 bool isOpaque = shader.isOpaque();
283
284 // update fFlags
285 uint32_t flags = 0;
286 if (isOpaque && (255 == this->getPaintAlpha())) {
287 flags |= kOpaqueAlpha_Flag;
288 }
289
290 fFlags = flags;
291 }
292
293 SkRadialShadowMapShaderImpl::ShadowMap2DShaderContext::~ShadowMap2DShaderContext () {
294 // The dependencies have been created outside of the context on memory that was allocated by
295 // the onCreateContext() method. Call the destructors and free the memory.
296 fOccluderContext->~Context();
297
298 sk_free(fHeapAllocated);
299 }
300
301 static inline SkPMColor convert(SkColor3f color, U8CPU a) {
302 if (color.fX <= 0.0f) {
303 color.fX = 0.0f;
304 } else if (color.fX >= 255.0f) {
305 color.fX = 255.0f;
306 }
307
308 if (color.fY <= 0.0f) {
309 color.fY = 0.0f;
310 } else if (color.fY >= 255.0f) {
311 color.fY = 255.0f;
312 }
313
314 if (color.fZ <= 0.0f) {
315 color.fZ = 0.0f;
316 } else if (color.fZ >= 255.0f) {
317 color.fZ = 255.0f;
318 }
319
320 return SkPreMultiplyARGB(a, (int) color.fX, (int) color.fY, (int) color.fZ) ;
321 }
322
323 // larger is better (fewer times we have to loop), but we shouldn't
robertphillips 2016/09/07 17:11:55 Does each one really cost 16 bytes?
vjiaoblack 2016/09/08 16:40:13 I think so - I also copied it from Diego's code, w
324 // take up too much stack-space (each one here costs 16 bytes)
325 #define BUFFER_MAX 16
326 void SkRadialShadowMapShaderImpl::ShadowMap2DShaderContext::shadeSpan(int x, int y,
robertphillips 2016/09/07 17:11:55 line these two params up with the above '('
vjiaoblack 2016/09/08 16:40:13 Done.
327 SkPMColor result[], int count) {
328 do {
329 int n = SkTMin(count, BUFFER_MAX);
330
331 // just fill with white for now
332 SkPMColor accum = convert(SkColor3f::Make(1.0f, 1.0f, 1.0f), 0xFF);
333
334 for (int i = 0; i < n; ++i) {
335 result[i] = accum;
336 }
337
338 result += n;
339 x += n;
340 count -= n;
341 } while (count > 0);
342 }
343
344 ////////////////////////////////////////////////////////////////////////////
345
346 #ifndef SK_IGNORE_TO_STRING
347 void SkRadialShadowMapShaderImpl::toString(SkString* str) const {
robertphillips 2016/09/07 17:11:55 Wrong string
348 str->appendf("2DShadowMapShader: ()");
349 }
350 #endif
351
352 sk_sp<SkFlattenable> SkRadialShadowMapShaderImpl::CreateProc(SkReadBuffer& buf) {
353
354 // Discarding SkShader flattenable params
355 bool hasLocalMatrix = buf.readBool();
356 SkAssertResult(!hasLocalMatrix);
357
358 sk_sp<SkLights> light = SkLights::MakeFromBuffer(buf);
359
360 int diffuseWidth = buf.readInt();
361 int diffuseHeight = buf.readInt();
362
363 sk_sp<SkShader> occluderShader(buf.readFlattenable<SkShader>());
364
365 return sk_make_sp<SkRadialShadowMapShaderImpl>(std::move(occluderShader),
robertphillips 2016/09/07 17:11:55 line these two up with above '('
366 std::move(light),
367 diffuseWidth, diffuseHeight);
368 }
369
370 void SkRadialShadowMapShaderImpl::flatten(SkWriteBuffer& buf) const {
371 this->INHERITED::flatten(buf);
372
373 fLight->flatten(buf);
374
375 buf.writeInt(fWidth);
376 buf.writeInt(fHeight);
377
378 buf.writeFlattenable(fOccluderShader.get());
379 }
380
381 size_t SkRadialShadowMapShaderImpl::onContextSize(const ContextRec& rec) const {
382 return sizeof(ShadowMap2DShaderContext);
383 }
384
385 SkShader::Context* SkRadialShadowMapShaderImpl::onCreateContext(const ContextRec & rec,
robertphillips 2016/09/07 17:11:55 line this guy up with above
386 void* storage) const {
387 size_t heapRequired = fOccluderShader->contextSize(rec);
388
389 void* heapAllocated = sk_malloc_throw(heapRequired);
390
391 void* occluderContextStorage = heapAllocated;
392
393 SkShader::Context* occluderContext =
394 fOccluderShader->createContext(rec, occluderContextStorage);
395
396 if (!occluderContext) {
397 sk_free(heapAllocated);
398 return nullptr;
399 }
400
401 return new (storage) ShadowMap2DShaderContext(*this, rec, occluderContext, h eapAllocated);
402 }
403
404 ///////////////////////////////////////////////////////////////////////////////
405
406 sk_sp<SkShader> SkRadialShadowMapShader::Make(sk_sp<SkShader> occluderShader,
robertphillips 2016/09/07 17:11:55 line up
407 sk_sp<SkLights> light,
408 int diffuseWidth, int diffuseHeight) {
409 if (!occluderShader) {
410 // TODO: Use paint's color in absence of a diffuseShader
robertphillips 2016/09/07 17:11:55 normalSource ? diffuseShader ?
411 // TODO: Use a default implementation of normalSource instead
412 return nullptr;
413 }
414
415 return sk_make_sp<SkRadialShadowMapShaderImpl>(std::move(occluderShader),
robertphillips 2016/09/07 17:11:55 line up
416 std::move(light),
417 diffuseWidth, diffuseHeight);
418 }
419
420 ///////////////////////////////////////////////////////////////////////////////
421
robertphillips 2016/09/07 17:11:55 tabbed over too far
422 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_START(SkRadialShadowMapShader)
423 SK_DEFINE_FLATTENABLE_REGISTRAR_ENTRY(SkRadialShadowMapShaderImpl)
424 SK_DEFINE_FLATTENABLE_REGISTRAR_GROUP_END
425
426 ///////////////////////////////////////////////////////////////////////////////
427
428 #endif
OLDNEW
« src/core/SkRadialShadowMapShader.h ('K') | « src/core/SkRadialShadowMapShader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698