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 | |
17 | class GrGLSLRRectShadowGeoProc : public GrGLSLGeometryProcessor { |
18 | public: |
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 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
26 | |
27 | // emit attributes |
28 | varyingHandler->emitAttributes(rsgp); |
29 | fragBuilder->codeAppend("half3 shadowParams;" ); |
30 | varyingHandler->addPassThroughAttribute(rsgp.inShadowParams(), "shadowParams" ); |
31 | |
32 | // setup pass through color |
33 | varyingHandler->addPassThroughAttribute(rsgp.inColor(), args.fOutputColor); |
34 | |
35 | // Setup position |
36 | this->writeOutputPosition(vertBuilder, gpArgs, rsgp.inPosition().name()); |
37 | // No need for local coordinates, this GP does not combine with fragment processors |
38 | |
39 | fragBuilder->codeAppend("half d = length(shadowParams.xy);" ); |
40 | fragBuilder->codeAppend("float2 uv = float2(shadowParams.z * (1.0 - d), 0.5);" ); |
41 | fragBuilder->codeAppend("half factor = " ); |
42 | fragBuilder->appendTextureLookup(args.fTexSamplers[0], "uv" ); |
43 | fragBuilder->codeAppend(".a;" ); |
44 | fragBuilder->codeAppendf("%s = half4(factor);" , args.fOutputCoverage); |
45 | } |
46 | |
47 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& proc) override { |
48 | } |
49 | |
50 | private: |
51 | typedef GrGLSLGeometryProcessor INHERITED; |
52 | }; |
53 | |
54 | /////////////////////////////////////////////////////////////////////////////// |
55 | |
56 | GrRRectShadowGeoProc::GrRRectShadowGeoProc(const GrSurfaceProxyView& lutView) |
57 | : INHERITED(kGrRRectShadowGeoProc_ClassID) { |
58 | fInPosition = {"inPosition" , kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
59 | fInColor = {"inColor" , kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType}; |
60 | fInShadowParams = {"inShadowParams" , kFloat3_GrVertexAttribType, kHalf3_GrSLType}; |
61 | this->setVertexAttributes(&fInPosition, 3); |
62 | |
63 | SkASSERT(lutView.proxy()); |
64 | fLUTTextureSampler.reset(GrSamplerState::Filter::kLinear, lutView.proxy()->backendFormat(), |
65 | lutView.swizzle()); |
66 | this->setTextureSamplerCnt(1); |
67 | } |
68 | |
69 | GrGLSLPrimitiveProcessor* GrRRectShadowGeoProc::createGLSLInstance(const GrShaderCaps&) const { |
70 | return new GrGLSLRRectShadowGeoProc(); |
71 | } |
72 | |
73 | /////////////////////////////////////////////////////////////////////////////// |
74 | |
75 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(GrRRectShadowGeoProc); |
76 | |
77 | #if GR_TEST_UTILS |
78 | GrGeometryProcessor* GrRRectShadowGeoProc::TestCreate(GrProcessorTestData* d) { |
79 | auto [view, ct, at] = d->randomAlphaOnlyView(); |
80 | |
81 | return GrRRectShadowGeoProc::Make(d->allocator(), view); |
82 | } |
83 | #endif |
84 | |