blob: d95c4957395208e1b49b6e96f65db19eecf10c68 [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"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040018#include "include/gpu/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"
26#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040027#include "src/gpu/GrOpsRenderPass.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include "src/gpu/GrPipeline.h"
29#include "src/gpu/GrPrimitiveProcessor.h"
30#include "src/gpu/GrProcessor.h"
31#include "src/gpu/GrProcessorSet.h"
Robert Phillips901aff02019-10-08 12:32:56 -040032#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrRecordingContextPriv.h"
34#include "src/gpu/GrRenderTargetContext.h"
35#include "src/gpu/GrRenderTargetContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040036#include "src/gpu/GrResourceProvider.h"
37#include "src/gpu/GrShaderCaps.h"
38#include "src/gpu/GrShaderVar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050039#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
40#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040041#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
42#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
43#include "src/gpu/glsl/GrGLSLUniformHandler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/glsl/GrGLSLVarying.h"
45#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040046#include "src/gpu/ops/GrDrawOp.h"
47#include "src/gpu/ops/GrOp.h"
Robert Phillips34cea002019-11-21 16:02:34 -050048#include "tools/gpu/ProxyUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040049
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:
Robert Phillips5d07c522019-11-18 11:19:51 -050068 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const SkMatrix& viewMatrix) {
69 return arena->make<FwidthSquircleTestProcessor>(viewMatrix);
70 }
71
72 const char* name() const override { return "FwidthSquircleTestProcessor"; }
73
74 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {}
75
76 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
77
78private:
79 friend class ::SkArenaAlloc; // for access to ctor
80
Chris Dalton09212192018-11-13 15:07:24 -050081 FwidthSquircleTestProcessor(const SkMatrix& viewMatrix)
82 : GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID)
83 , fViewMatrix(viewMatrix) {
84 this->setVertexAttributes(&gVertex, 1);
85 }
Chris Dalton09212192018-11-13 15:07:24 -050086
Chris Dalton09212192018-11-13 15:07:24 -050087 const SkMatrix fViewMatrix;
88
89 class Impl;
Robert Phillips5d07c522019-11-18 11:19:51 -050090
91 typedef GrGeometryProcessor INHERITED;
Chris Dalton09212192018-11-13 15:07:24 -050092};
93
94class FwidthSquircleTestProcessor::Impl : public GrGLSLGeometryProcessor {
95 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
96 const auto& proc = args.fGP.cast<FwidthSquircleTestProcessor>();
97
98 auto* uniforms = args.fUniformHandler;
Ethan Nicholas16464c32020-04-06 13:53:05 -040099 fViewMatrixHandle = uniforms->addUniform(nullptr, kVertex_GrShaderFlag, kFloat3x3_GrSLType,
100 "viewmatrix");
Chris Dalton09212192018-11-13 15:07:24 -0500101
102 auto* varyings = args.fVaryingHandler;
103 varyings->emitAttributes(proc);
104
105 GrGLSLVarying squircleCoord(kFloat2_GrSLType);
106 varyings->addVarying("bboxcoord", &squircleCoord);
107
108 auto* v = args.fVertBuilder;
109 v->codeAppendf("float2x2 R = float2x2(cos(.05), sin(.05), -sin(.05), cos(.05));");
110
111 v->codeAppendf("%s = bboxcoord * 1.25;", squircleCoord.vsOut());
112 v->codeAppendf("float3 vertexpos = float3(bboxcoord * 100 * R + 100, 1);");
113 v->codeAppendf("vertexpos = %s * vertexpos;", uniforms->getUniformCStr(fViewMatrixHandle));
114 gpArgs->fPositionVar.set(kFloat3_GrSLType, "vertexpos");
115
116 auto* f = args.fFragBuilder;
117 f->codeAppendf("float golden_ratio = 1.61803398875;");
118 f->codeAppendf("float pi = 3.141592653589793;");
119 f->codeAppendf("float x = abs(%s.x), y = abs(%s.y);",
120 squircleCoord.fsIn(), squircleCoord.fsIn());
121
122 // Squircle function!
Ethan Nicholase1f55022019-02-05 17:17:40 -0500123 f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + pow(y, golden_ratio*pi) - 1);");
Chris Dalton09212192018-11-13 15:07:24 -0500124 f->codeAppendf("float fnwidth = fwidth(fn);");
125 f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500126 f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);");
Chris Dalton09212192018-11-13 15:07:24 -0500127
128 f->codeAppendf("%s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor);
129 f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
130 }
131
Brian Osman609f1592020-07-01 15:14:39 -0400132 void setData(const GrGLSLProgramDataManager& pdman,
133 const GrPrimitiveProcessor& primProc) override {
Chris Dalton09212192018-11-13 15:07:24 -0500134 const auto& proc = primProc.cast<FwidthSquircleTestProcessor>();
135 pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix);
136 }
137
138 UniformHandle fViewMatrixHandle;
139};
140
141GrGLSLPrimitiveProcessor* FwidthSquircleTestProcessor::createGLSLInstance(
142 const GrShaderCaps&) const {
143 return new Impl();
144}
145
146////////////////////////////////////////////////////////////////////////////////////////////////////
147// Draw Op.
148
149class FwidthSquircleTestOp : public GrDrawOp {
150public:
151 DEFINE_OP_CLASS_ID
152
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500153 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500154 GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
Chris Dalton09212192018-11-13 15:07:24 -0500155 return pool->allocate<FwidthSquircleTestOp>(viewMatrix);
156 }
157
158private:
159 FwidthSquircleTestOp(const SkMatrix& viewMatrix)
160 : GrDrawOp(ClassID())
161 , fViewMatrix(viewMatrix) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400162 this->setBounds(SkRect::MakeIWH(kWidth, kHeight), HasAABloat::kNo, IsHairline::kNo);
Chris Dalton09212192018-11-13 15:07:24 -0500163 }
164
Robert Phillips901aff02019-10-08 12:32:56 -0400165 const char* name() const override { return "FwidthSquircleTestOp"; }
Chris Dalton09212192018-11-13 15:07:24 -0500166 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Robert Phillips5d07c522019-11-18 11:19:51 -0500167 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
168 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700169 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton09212192018-11-13 15:07:24 -0500170 }
Robert Phillips5d07c522019-11-18 11:19:51 -0500171
Robert Phillipsac6156c2020-02-28 16:02:40 -0500172 GrProgramInfo* createProgramInfo(const GrCaps* caps,
173 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400174 const GrSurfaceProxyView* writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500175 GrAppliedClip&& appliedClip,
176 const GrXferProcessor::DstProxyView& dstProxyView) const {
177 GrGeometryProcessor* geomProc = FwidthSquircleTestProcessor::Make(arena, fViewMatrix);
178
Brian Salomon8afde5f2020-04-01 16:22:00 -0400179 return sk_gpu_test::CreateProgramInfo(caps, arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500180 std::move(appliedClip), dstProxyView,
181 geomProc, SkBlendMode::kSrcOver,
182 GrPrimitiveType::kTriangleStrip);
183 }
184
185 GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const {
186 return this->createProgramInfo(&flushState->caps(),
187 flushState->allocator(),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400188 flushState->writeView(),
Robert Phillipsac6156c2020-02-28 16:02:40 -0500189 flushState->detachAppliedClip(),
190 flushState->dstProxyView());
191 }
192
Robert Phillips5d07c522019-11-18 11:19:51 -0500193 void onPrePrepare(GrRecordingContext* context,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400194 const GrSurfaceProxyView* writeView,
Robert Phillips34cea002019-11-21 16:02:34 -0500195 GrAppliedClip* clip,
Robert Phillips8053c972019-11-21 10:44:53 -0500196 const GrXferProcessor::DstProxyView& dstProxyView) final {
Robert Phillips5d07c522019-11-18 11:19:51 -0500197 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
198
Robert Phillips34cea002019-11-21 16:02:34 -0500199 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400200 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillips5d07c522019-11-18 11:19:51 -0500201
Brian Salomon8afde5f2020-04-01 16:22:00 -0400202 fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500203 std::move(appliedClip), dstProxyView);
Robert Phillips5d07c522019-11-18 11:19:51 -0500204
Robert Phillipsac6156c2020-02-28 16:02:40 -0500205 context->priv().recordProgramInfo(fProgramInfo);
Robert Phillips5d07c522019-11-18 11:19:51 -0500206 }
207
208 void onPrepare(GrOpFlushState* flushState) final {
Chris Dalton09212192018-11-13 15:07:24 -0500209 SkPoint vertices[4] = {
210 {-1, -1},
211 {+1, -1},
212 {-1, +1},
213 {+1, +1},
214 };
Greg Danielf793de12019-09-05 13:23:23 -0400215 fVertexBuffer = flushState->resourceProvider()->createBuffer(
216 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
217 }
Robert Phillips5d07c522019-11-18 11:19:51 -0500218
219 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final {
Greg Danielf793de12019-09-05 13:23:23 -0400220 if (!fVertexBuffer) {
Chris Dalton09212192018-11-13 15:07:24 -0500221 return;
222 }
Robert Phillips901aff02019-10-08 12:32:56 -0400223
Robert Phillips34cea002019-11-21 16:02:34 -0500224 if (!fProgramInfo) {
Robert Phillipsac6156c2020-02-28 16:02:40 -0500225 fProgramInfo = this->createProgramInfo(flushState);
Robert Phillips34cea002019-11-21 16:02:34 -0500226 }
227
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600228 flushState->bindPipeline(*fProgramInfo, SkRect::MakeIWH(kWidth, kHeight));
229 flushState->bindBuffers(nullptr, nullptr, fVertexBuffer.get());
230 flushState->draw(4, 0);
Robert Phillips5d07c522019-11-18 11:19:51 -0500231
Chris Dalton09212192018-11-13 15:07:24 -0500232 }
233
Greg Daniel55e5be32019-09-30 12:23:26 -0400234 static const int kWidth = 200;
235 static const int kHeight = 200;
Chris Dalton09212192018-11-13 15:07:24 -0500236
Robert Phillips5d07c522019-11-18 11:19:51 -0500237 sk_sp<GrBuffer> fVertexBuffer;
238 const SkMatrix fViewMatrix;
239
240 // The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when
Robert Phillips34cea002019-11-21 16:02:34 -0500241 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
242 // arena's job to free up their memory so we just have a bare programInfo pointer here. We
243 // don't even store the GrPipeline and GrPrimitiveProcessor pointers here bc they are
244 // guaranteed to have the same lifetime as the program info.
Robert Phillips5d07c522019-11-18 11:19:51 -0500245 GrProgramInfo* fProgramInfo = nullptr;
246
Chris Dalton09212192018-11-13 15:07:24 -0500247 friend class ::GrOpMemoryPool; // for ctor
Robert Phillips5d07c522019-11-18 11:19:51 -0500248
249 typedef GrDrawOp INHERITED;
Chris Dalton09212192018-11-13 15:07:24 -0500250};
251
252////////////////////////////////////////////////////////////////////////////////////////////////////
253// Test.
254
Chris Dalton50e24d72019-02-07 16:20:09 -0700255DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, ctx, rtc, canvas, errorMsg, 200, 200) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500256 if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700257 *errorMsg = "Shader derivatives not supported.";
258 return DrawResult::kSkip;
Chris Dalton09212192018-11-13 15:07:24 -0500259 }
260
261 // Draw the test directly to the frame buffer.
Chris Dalton3a778372019-02-07 15:23:36 -0700262 canvas->clear(SK_ColorWHITE);
Chris Dalton09212192018-11-13 15:07:24 -0500263 rtc->priv().testingOnly_addDrawOp(FwidthSquircleTestOp::Make(ctx, canvas->getTotalMatrix()));
Chris Dalton50e24d72019-02-07 16:20:09 -0700264 return skiagm::DrawResult::kOk;
Chris Dalton09212192018-11-13 15:07:24 -0500265}
266
Chris Dalton09212192018-11-13 15:07:24 -0500267}