blob: 742966420b6bc4b2443747977d95186697e94973 [file] [log] [blame]
Chris Dalton09212192018-11-13 15:07:24 -05001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/gpu/GrContext.h"
18#include "include/private/GrRecordingContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040019#include "include/private/GrTypesPriv.h"
20#include "src/gpu/GrBuffer.h"
21#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "src/gpu/GrGeometryProcessor.h"
24#include "src/gpu/GrGpuBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrMemoryPool.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "src/gpu/GrMesh.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040028#include "src/gpu/GrOpsRenderPass.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040029#include "src/gpu/GrPipeline.h"
30#include "src/gpu/GrPrimitiveProcessor.h"
31#include "src/gpu/GrProcessor.h"
32#include "src/gpu/GrProcessorSet.h"
Robert Phillips901aff02019-10-08 12:32:56 -040033#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrRecordingContextPriv.h"
35#include "src/gpu/GrRenderTargetContext.h"
36#include "src/gpu/GrRenderTargetContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040037#include "src/gpu/GrResourceProvider.h"
38#include "src/gpu/GrShaderCaps.h"
39#include "src/gpu/GrShaderVar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
41#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040042#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
43#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
44#include "src/gpu/glsl/GrGLSLUniformHandler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050045#include "src/gpu/glsl/GrGLSLVarying.h"
46#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040047#include "src/gpu/ops/GrDrawOp.h"
48#include "src/gpu/ops/GrOp.h"
49
50#include <memory>
51#include <utility>
52
53class GrAppliedClip;
Chris Dalton09212192018-11-13 15:07:24 -050054
Chris Dalton3a778372019-02-07 15:23:36 -070055/**
56 * This test ensures that fwidth() works properly on GPU configs by drawing a squircle.
57 */
Chris Dalton09212192018-11-13 15:07:24 -050058namespace skiagm {
59
60static constexpr GrGeometryProcessor::Attribute gVertex =
61 {"bboxcoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
62
Chris Dalton09212192018-11-13 15:07:24 -050063////////////////////////////////////////////////////////////////////////////////////////////////////
64// SkSL code.
65
66class FwidthSquircleTestProcessor : public GrGeometryProcessor {
67public:
68 FwidthSquircleTestProcessor(const SkMatrix& viewMatrix)
69 : GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID)
70 , fViewMatrix(viewMatrix) {
71 this->setVertexAttributes(&gVertex, 1);
72 }
73 const char* name() const override { return "FwidthSquircleTestProcessor"; }
74 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {}
75 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
76
77private:
78 const SkMatrix fViewMatrix;
79
80 class Impl;
81};
82
83class FwidthSquircleTestProcessor::Impl : public GrGLSLGeometryProcessor {
84 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
85 const auto& proc = args.fGP.cast<FwidthSquircleTestProcessor>();
86
87 auto* uniforms = args.fUniformHandler;
88 fViewMatrixHandle =
89 uniforms->addUniform(kVertex_GrShaderFlag, kFloat3x3_GrSLType, "viewmatrix");
90
91 auto* varyings = args.fVaryingHandler;
92 varyings->emitAttributes(proc);
93
94 GrGLSLVarying squircleCoord(kFloat2_GrSLType);
95 varyings->addVarying("bboxcoord", &squircleCoord);
96
97 auto* v = args.fVertBuilder;
98 v->codeAppendf("float2x2 R = float2x2(cos(.05), sin(.05), -sin(.05), cos(.05));");
99
100 v->codeAppendf("%s = bboxcoord * 1.25;", squircleCoord.vsOut());
101 v->codeAppendf("float3 vertexpos = float3(bboxcoord * 100 * R + 100, 1);");
102 v->codeAppendf("vertexpos = %s * vertexpos;", uniforms->getUniformCStr(fViewMatrixHandle));
103 gpArgs->fPositionVar.set(kFloat3_GrSLType, "vertexpos");
104
105 auto* f = args.fFragBuilder;
106 f->codeAppendf("float golden_ratio = 1.61803398875;");
107 f->codeAppendf("float pi = 3.141592653589793;");
108 f->codeAppendf("float x = abs(%s.x), y = abs(%s.y);",
109 squircleCoord.fsIn(), squircleCoord.fsIn());
110
111 // Squircle function!
Ethan Nicholase1f55022019-02-05 17:17:40 -0500112 f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + pow(y, golden_ratio*pi) - 1);");
Chris Dalton09212192018-11-13 15:07:24 -0500113 f->codeAppendf("float fnwidth = fwidth(fn);");
114 f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500115 f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);");
Chris Dalton09212192018-11-13 15:07:24 -0500116
117 f->codeAppendf("%s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor);
118 f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
119 }
120
121 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
122 FPCoordTransformIter&& transformIter) override {
123 const auto& proc = primProc.cast<FwidthSquircleTestProcessor>();
124 pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix);
125 }
126
127 UniformHandle fViewMatrixHandle;
128};
129
130GrGLSLPrimitiveProcessor* FwidthSquircleTestProcessor::createGLSLInstance(
131 const GrShaderCaps&) const {
132 return new Impl();
133}
134
135////////////////////////////////////////////////////////////////////////////////////////////////////
136// Draw Op.
137
138class FwidthSquircleTestOp : public GrDrawOp {
139public:
140 DEFINE_OP_CLASS_ID
141
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500142 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500143 GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
Chris Dalton09212192018-11-13 15:07:24 -0500144 return pool->allocate<FwidthSquircleTestOp>(viewMatrix);
145 }
146
147private:
148 FwidthSquircleTestOp(const SkMatrix& viewMatrix)
149 : GrDrawOp(ClassID())
150 , fViewMatrix(viewMatrix) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400151 this->setBounds(SkRect::MakeIWH(kWidth, kHeight), HasAABloat::kNo, IsHairline::kNo);
Chris Dalton09212192018-11-13 15:07:24 -0500152 }
153
Robert Phillips901aff02019-10-08 12:32:56 -0400154 const char* name() const override { return "FwidthSquircleTestOp"; }
Chris Dalton09212192018-11-13 15:07:24 -0500155 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman5ced0bf2019-03-15 10:15:29 -0400156 GrProcessorSet::Analysis finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600157 const GrCaps&, const GrAppliedClip*, bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700158 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton09212192018-11-13 15:07:24 -0500159 }
Greg Danielf793de12019-09-05 13:23:23 -0400160 void onPrepare(GrOpFlushState* flushState) override {
Chris Dalton09212192018-11-13 15:07:24 -0500161 SkPoint vertices[4] = {
162 {-1, -1},
163 {+1, -1},
164 {-1, +1},
165 {+1, +1},
166 };
Greg Danielf793de12019-09-05 13:23:23 -0400167 fVertexBuffer = flushState->resourceProvider()->createBuffer(
168 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
169 }
170 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
171 if (!fVertexBuffer) {
Chris Dalton09212192018-11-13 15:07:24 -0500172 return;
173 }
Greg Daniel2c3398d2019-06-19 11:58:01 -0400174 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrcOver,
Robert Phillips405413f2019-10-04 10:39:28 -0400175 flushState->drawOpArgs().outputSwizzle());
Robert Phillips901aff02019-10-08 12:32:56 -0400176
177 FwidthSquircleTestProcessor primProc(fViewMatrix);
178
Chris Dalton5e8cdfd2019-11-11 15:23:30 -0700179 GrProgramInfo programInfo(flushState->proxy()->numSamples(),
180 flushState->proxy()->numStencilSamples(),
Robert Phillips901aff02019-10-08 12:32:56 -0400181 flushState->drawOpArgs().origin(),
Robert Phillips67a625e2019-11-15 15:37:07 -0500182 &pipeline,
183 &primProc,
Robert Phillipscea290f2019-11-06 11:21:03 -0500184 nullptr, nullptr, 0,
185 GrPrimitiveType::kTriangleStrip);
Robert Phillips901aff02019-10-08 12:32:56 -0400186
Chris Dalton09212192018-11-13 15:07:24 -0500187 GrMesh mesh(GrPrimitiveType::kTriangleStrip);
188 mesh.setNonIndexedNonInstanced(4);
Greg Danielf793de12019-09-05 13:23:23 -0400189 mesh.setVertexData(std::move(fVertexBuffer));
Robert Phillips901aff02019-10-08 12:32:56 -0400190 flushState->opsRenderPass()->draw(programInfo, &mesh, 1, SkRect::MakeIWH(kWidth, kHeight));
Chris Dalton09212192018-11-13 15:07:24 -0500191 }
192
Greg Danielf793de12019-09-05 13:23:23 -0400193 sk_sp<GrBuffer> fVertexBuffer;
Chris Dalton09212192018-11-13 15:07:24 -0500194 const SkMatrix fViewMatrix;
Greg Daniel55e5be32019-09-30 12:23:26 -0400195 static const int kWidth = 200;
196 static const int kHeight = 200;
Chris Dalton09212192018-11-13 15:07:24 -0500197
198 friend class ::GrOpMemoryPool; // for ctor
199};
200
201////////////////////////////////////////////////////////////////////////////////////////////////////
202// Test.
203
Chris Dalton50e24d72019-02-07 16:20:09 -0700204DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, ctx, rtc, canvas, errorMsg, 200, 200) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500205 if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700206 *errorMsg = "Shader derivatives not supported.";
207 return DrawResult::kSkip;
Chris Dalton09212192018-11-13 15:07:24 -0500208 }
209
210 // Draw the test directly to the frame buffer.
Chris Dalton3a778372019-02-07 15:23:36 -0700211 canvas->clear(SK_ColorWHITE);
Chris Dalton09212192018-11-13 15:07:24 -0500212 rtc->priv().testingOnly_addDrawOp(FwidthSquircleTestOp::Make(ctx, canvas->getTotalMatrix()));
Chris Dalton50e24d72019-02-07 16:20:09 -0700213 return skiagm::DrawResult::kOk;
Chris Dalton09212192018-11-13 15:07:24 -0500214}
215
Chris Dalton09212192018-11-13 15:07:24 -0500216}