1 /*
2 * Copyright 2018 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 "gm/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkString.h"
17 #include "include/gpu/GrContext.h"
18 #include "include/private/GrRecordingContext.h"
19 #include "include/private/GrTypesPriv.h"
20 #include "src/gpu/GrBuffer.h"
21 #include "src/gpu/GrCaps.h"
22 #include "src/gpu/GrContextPriv.h"
23 #include "src/gpu/GrGeometryProcessor.h"
24 #include "src/gpu/GrGpuBuffer.h"
25 #include "src/gpu/GrGpuCommandBuffer.h"
26 #include "src/gpu/GrMemoryPool.h"
27 #include "src/gpu/GrMesh.h"
28 #include "src/gpu/GrOpFlushState.h"
29 #include "src/gpu/GrPipeline.h"
30 #include "src/gpu/GrPrimitiveProcessor.h"
31 #include "src/gpu/GrProcessor.h"
32 #include "src/gpu/GrProcessorSet.h"
33 #include "src/gpu/GrRecordingContextPriv.h"
34 #include "src/gpu/GrRenderTargetContext.h"
35 #include "src/gpu/GrRenderTargetContextPriv.h"
36 #include "src/gpu/GrResourceProvider.h"
37 #include "src/gpu/GrShaderCaps.h"
38 #include "src/gpu/GrShaderVar.h"
39 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
40 #include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
41 #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
42 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
43 #include "src/gpu/glsl/GrGLSLUniformHandler.h"
44 #include "src/gpu/glsl/GrGLSLVarying.h"
45 #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
46 #include "src/gpu/ops/GrDrawOp.h"
47 #include "src/gpu/ops/GrOp.h"
48
49 #include <memory>
50 #include <utility>
51
52 class GrAppliedClip;
53
54 /**
55 * This test ensures that fwidth() works properly on GPU configs by drawing a squircle.
56 */
57 namespace skiagm {
58
59 static constexpr GrGeometryProcessor::Attribute gVertex =
60 {"bboxcoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
61
62 ////////////////////////////////////////////////////////////////////////////////////////////////////
63 // SkSL code.
64
65 class FwidthSquircleTestProcessor : public GrGeometryProcessor {
66 public:
FwidthSquircleTestProcessor(const SkMatrix & viewMatrix)67 FwidthSquircleTestProcessor(const SkMatrix& viewMatrix)
68 : GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID)
69 , fViewMatrix(viewMatrix) {
70 this->setVertexAttributes(&gVertex, 1);
71 }
name() const72 const char* name() const override { return "FwidthSquircleTestProcessor"; }
getGLSLProcessorKey(const GrShaderCaps &,GrProcessorKeyBuilder * b) const73 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {}
74 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
75
76 private:
77 const SkMatrix fViewMatrix;
78
79 class Impl;
80 };
81
82 class FwidthSquircleTestProcessor::Impl : public GrGLSLGeometryProcessor {
onEmitCode(EmitArgs & args,GrGPArgs * gpArgs)83 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
84 const auto& proc = args.fGP.cast<FwidthSquircleTestProcessor>();
85
86 auto* uniforms = args.fUniformHandler;
87 fViewMatrixHandle =
88 uniforms->addUniform(kVertex_GrShaderFlag, kFloat3x3_GrSLType, "viewmatrix");
89
90 auto* varyings = args.fVaryingHandler;
91 varyings->emitAttributes(proc);
92
93 GrGLSLVarying squircleCoord(kFloat2_GrSLType);
94 varyings->addVarying("bboxcoord", &squircleCoord);
95
96 auto* v = args.fVertBuilder;
97 v->codeAppendf("float2x2 R = float2x2(cos(.05), sin(.05), -sin(.05), cos(.05));");
98
99 v->codeAppendf("%s = bboxcoord * 1.25;", squircleCoord.vsOut());
100 v->codeAppendf("float3 vertexpos = float3(bboxcoord * 100 * R + 100, 1);");
101 v->codeAppendf("vertexpos = %s * vertexpos;", uniforms->getUniformCStr(fViewMatrixHandle));
102 gpArgs->fPositionVar.set(kFloat3_GrSLType, "vertexpos");
103
104 auto* f = args.fFragBuilder;
105 f->codeAppendf("float golden_ratio = 1.61803398875;");
106 f->codeAppendf("float pi = 3.141592653589793;");
107 f->codeAppendf("float x = abs(%s.x), y = abs(%s.y);",
108 squircleCoord.fsIn(), squircleCoord.fsIn());
109
110 // Squircle function!
111 f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + pow(y, golden_ratio*pi) - 1);");
112 f->codeAppendf("float fnwidth = fwidth(fn);");
113 f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero.
114 f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);");
115
116 f->codeAppendf("%s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor);
117 f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
118 }
119
setData(const GrGLSLProgramDataManager & pdman,const GrPrimitiveProcessor & primProc,FPCoordTransformIter && transformIter)120 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
121 FPCoordTransformIter&& transformIter) override {
122 const auto& proc = primProc.cast<FwidthSquircleTestProcessor>();
123 pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix);
124 }
125
126 UniformHandle fViewMatrixHandle;
127 };
128
createGLSLInstance(const GrShaderCaps &) const129 GrGLSLPrimitiveProcessor* FwidthSquircleTestProcessor::createGLSLInstance(
130 const GrShaderCaps&) const {
131 return new Impl();
132 }
133
134 ////////////////////////////////////////////////////////////////////////////////////////////////////
135 // Draw Op.
136
137 class FwidthSquircleTestOp : public GrDrawOp {
138 public:
139 DEFINE_OP_CLASS_ID
140
Make(GrRecordingContext * ctx,const SkMatrix & viewMatrix)141 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) {
142 GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
143 return pool->allocate<FwidthSquircleTestOp>(viewMatrix);
144 }
145
146 private:
FwidthSquircleTestOp(const SkMatrix & viewMatrix)147 FwidthSquircleTestOp(const SkMatrix& viewMatrix)
148 : GrDrawOp(ClassID())
149 , fViewMatrix(viewMatrix) {
150 this->setBounds(SkRect::MakeIWH(200, 200), HasAABloat::kNo, IsZeroArea::kNo);
151 }
152
name() const153 const char* name() const override { return "ClockwiseTestOp"; }
fixedFunctionFlags() const154 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
finalize(const GrCaps &,const GrAppliedClip *,bool hasMixedSampledCoverage,GrClampType)155 GrProcessorSet::Analysis finalize(
156 const GrCaps&, const GrAppliedClip*, bool hasMixedSampledCoverage, GrClampType) override {
157 return GrProcessorSet::EmptySetAnalysis();
158 }
onPrepare(GrOpFlushState *)159 void onPrepare(GrOpFlushState*) override {}
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)160 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
161 SkPoint vertices[4] = {
162 {-1, -1},
163 {+1, -1},
164 {-1, +1},
165 {+1, +1},
166 };
167 sk_sp<const GrBuffer> vertexBuffer(flushState->resourceProvider()->createBuffer(
168 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices));
169 if (!vertexBuffer) {
170 return;
171 }
172 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrcOver,
173 flushState->drawOpArgs().fOutputSwizzle);
174 GrMesh mesh(GrPrimitiveType::kTriangleStrip);
175 mesh.setNonIndexedNonInstanced(4);
176 mesh.setVertexData(std::move(vertexBuffer));
177 flushState->rtCommandBuffer()->draw(FwidthSquircleTestProcessor(fViewMatrix), pipeline,
178 nullptr, nullptr, &mesh, 1, SkRect::MakeIWH(100, 100));
179 }
180
181 const SkMatrix fViewMatrix;
182
183 friend class ::GrOpMemoryPool; // for ctor
184 };
185
186 ////////////////////////////////////////////////////////////////////////////////////////////////////
187 // Test.
188
189 DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, ctx, rtc, canvas, errorMsg, 200, 200) {
190 if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport()) {
191 *errorMsg = "Shader derivatives not supported.";
192 return DrawResult::kSkip;
193 }
194
195 // Draw the test directly to the frame buffer.
196 canvas->clear(SK_ColorWHITE);
197 rtc->priv().testingOnly_addDrawOp(FwidthSquircleTestOp::Make(ctx, canvas->getTotalMatrix()));
198 return skiagm::DrawResult::kOk;
199 }
200
201 }
202