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/GrGLSLUniformHandler.h"
13 #include "src/gpu/glsl/GrGLSLVarying.h"
14 #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
15
16 class GrRRectShadowGeoProc::Impl : public ProgramImpl {
17 public:
setData(const GrGLSLProgramDataManager &,const GrShaderCaps &,const GrGeometryProcessor &)18 void setData(const GrGLSLProgramDataManager&,
19 const GrShaderCaps&,
20 const GrGeometryProcessor&) override {}
21
22 private:
onEmitCode(EmitArgs & args,GrGPArgs * gpArgs)23 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
24 const GrRRectShadowGeoProc& rsgp = args.fGeomProc.cast<GrRRectShadowGeoProc>();
25 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
26 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
27 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
28
29 // emit attributes
30 varyingHandler->emitAttributes(rsgp);
31 fragBuilder->codeAppend("half3 shadowParams;");
32 varyingHandler->addPassThroughAttribute(rsgp.inShadowParams().asShaderVar(),
33 "shadowParams");
34
35 // setup pass through color
36 fragBuilder->codeAppendf("half4 %s;", args.fOutputColor);
37 varyingHandler->addPassThroughAttribute(rsgp.inColor().asShaderVar(), args.fOutputColor);
38
39 // Setup position
40 WriteOutputPosition(vertBuilder, gpArgs, rsgp.inPosition().name());
41 // No need for local coordinates, this GP does not combine with fragment processors
42
43 fragBuilder->codeAppend("half d = length(shadowParams.xy);");
44 fragBuilder->codeAppend("float2 uv = float2(shadowParams.z * (1.0 - d), 0.5);");
45 fragBuilder->codeAppend("half factor = ");
46 fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv");
47 fragBuilder->codeAppend(".a;");
48 fragBuilder->codeAppendf("half4 %s = half4(factor);", args.fOutputCoverage);
49 }
50 };
51
52 ///////////////////////////////////////////////////////////////////////////////
53
GrRRectShadowGeoProc(const GrSurfaceProxyView & lutView)54 GrRRectShadowGeoProc::GrRRectShadowGeoProc(const GrSurfaceProxyView& lutView)
55 : INHERITED(kGrRRectShadowGeoProc_ClassID) {
56 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
57 fInColor = {"inColor", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
58 fInShadowParams = {"inShadowParams", kFloat3_GrVertexAttribType, kHalf3_GrSLType};
59 this->setVertexAttributes(&fInPosition, 3);
60
61 SkASSERT(lutView.proxy());
62 fLUTTextureSampler.reset(GrSamplerState::Filter::kLinear, lutView.proxy()->backendFormat(),
63 lutView.swizzle());
64 this->setTextureSamplerCnt(1);
65 }
66
makeProgramImpl(const GrShaderCaps &) const67 std::unique_ptr<GrGeometryProcessor::ProgramImpl> GrRRectShadowGeoProc::makeProgramImpl(
68 const GrShaderCaps&) const {
69 return std::make_unique<Impl>();
70 }
71
72 ///////////////////////////////////////////////////////////////////////////////
73
74 GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrRRectShadowGeoProc);
75
76 #if GR_TEST_UTILS
TestCreate(GrProcessorTestData * d)77 GrGeometryProcessor* GrRRectShadowGeoProc::TestCreate(GrProcessorTestData* d) {
78 auto [view, ct, at] = d->randomAlphaOnlyView();
79
80 return GrRRectShadowGeoProc::Make(d->allocator(), view);
81 }
82 #endif
83