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

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

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

Powered by Google App Engine
This is Rietveld 408576698