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 "src/gpu/effects/GrShadowGeoProc.h"
9
10#include "src/gpu/GrSurfaceProxyView.h"
11#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
12#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
13#include "src/gpu/glsl/GrGLSLUniformHandler.h"
14#include "src/gpu/glsl/GrGLSLVarying.h"
15#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
16
17class GrGLSLRRectShadowGeoProc : public GrGLSLGeometryProcessor {
18public:
19 GrGLSLRRectShadowGeoProc() {}
20
21 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
22 const GrRRectShadowGeoProc& rsgp = args.fGP.cast<GrRRectShadowGeoProc>();
23 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
24 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
25 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
26 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
27
28 // emit attributes
29 varyingHandler->emitAttributes(rsgp);
30 fragBuilder->codeAppend("half3 shadowParams;");
31 varyingHandler->addPassThroughAttribute(rsgp.inShadowParams(), "shadowParams");
32
33 // setup pass through color
34 varyingHandler->addPassThroughAttribute(rsgp.inColor(), args.fOutputColor);
35
36 // Setup position
37 this->writeOutputPosition(vertBuilder, gpArgs, rsgp.inPosition().name());
38
39 // emit transforms
40 this->emitTransforms(vertBuilder,
41 varyingHandler,
42 uniformHandler,
43 rsgp.inPosition().asShaderVar(),
44 args.fFPCoordTransformHandler);
45
46 fragBuilder->codeAppend("half d = length(shadowParams.xy);");
47 fragBuilder->codeAppend("float2 uv = float2(shadowParams.z * (1.0 - d), 0.5);");
48 fragBuilder->codeAppend("half factor = ");
49 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv");
50 fragBuilder->codeAppend(".a;");
51 fragBuilder->codeAppendf("%s = half4(factor);", args.fOutputCoverage);
52 }
53
54 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc,
55 const CoordTransformRange& transformRange) override {
56 this->setTransformDataHelper(SkMatrix::I(), pdman, transformRange);
57 }
58
59private:
60 typedef GrGLSLGeometryProcessor INHERITED;
61};
62
63///////////////////////////////////////////////////////////////////////////////
64
65GrRRectShadowGeoProc::GrRRectShadowGeoProc(const GrSurfaceProxyView& lutView)
66 : INHERITED(kGrRRectShadowGeoProc_ClassID) {
67 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
68 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
69 fInShadowParams = {"inShadowParams", kFloat3_GrVertexAttribType, kHalf3_GrSLType};
70 this->setVertexAttributes(&fInPosition, 3);
71
72 SkASSERT(lutView.proxy());
73 fLUTTextureSampler.reset(GrSamplerState::Filter::kBilerp, lutView.proxy()->backendFormat(),
74 lutView.swizzle());
75 this->setTextureSamplerCnt(1);
76}
77
78GrGLSLPrimitiveProcessor* GrRRectShadowGeoProc::createGLSLInstance(const GrShaderCaps&) const {
79 return new GrGLSLRRectShadowGeoProc();
80}
81
82///////////////////////////////////////////////////////////////////////////////
83
84GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrRRectShadowGeoProc);
85
86#if GR_TEST_UTILS
87GrGeometryProcessor* GrRRectShadowGeoProc::TestCreate(GrProcessorTestData* d) {
88 auto [view, ct, at] = d->randomAlphaOnlyView();
89
90 return GrRRectShadowGeoProc::Make(d->allocator(), view);
91}
92#endif
93