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