blob: d18995e743356f43deba55d9dffbc9bb639df276 [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"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040017#include "include/gpu/GrRecordingContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/private/GrTypesPriv.h"
Robert Phillips7a0d3c32021-07-21 15:39:51 -040019#include "src/core/SkCanvasPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "src/gpu/GrBuffer.h"
21#include "src/gpu/GrCaps.h"
Adlai Hollera0693042020-10-14 11:23:11 -040022#include "src/gpu/GrDirectContextPriv.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"
Ben Wagner7fde8e12019-05-01 17:28:53 -040029#include "src/gpu/GrProcessor.h"
30#include "src/gpu/GrProcessorSet.h"
Robert Phillips901aff02019-10-08 12:32:56 -040031#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050032#include "src/gpu/GrRecordingContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include "src/gpu/GrResourceProvider.h"
34#include "src/gpu/GrShaderCaps.h"
35#include "src/gpu/GrShaderVar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
37#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040038#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
39#include "src/gpu/glsl/GrGLSLUniformHandler.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/glsl/GrGLSLVarying.h"
41#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040042#include "src/gpu/ops/GrDrawOp.h"
43#include "src/gpu/ops/GrOp.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040044#include "src/gpu/v1/SurfaceDrawContext_v1.h"
Robert Phillips34cea002019-11-21 16:02:34 -050045#include "tools/gpu/ProxyUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040046
47#include <memory>
48#include <utility>
49
50class GrAppliedClip;
Chris Dalton09212192018-11-13 15:07:24 -050051
Chris Dalton3a778372019-02-07 15:23:36 -070052/**
53 * This test ensures that fwidth() works properly on GPU configs by drawing a squircle.
54 */
Robert Phillips7a0d3c32021-07-21 15:39:51 -040055namespace {
Chris Dalton09212192018-11-13 15:07:24 -050056
57static constexpr GrGeometryProcessor::Attribute gVertex =
58 {"bboxcoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
59
Chris Dalton09212192018-11-13 15:07:24 -050060////////////////////////////////////////////////////////////////////////////////////////////////////
61// SkSL code.
62
63class FwidthSquircleTestProcessor : public GrGeometryProcessor {
64public:
Robert Phillips5d07c522019-11-18 11:19:51 -050065 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const SkMatrix& viewMatrix) {
Mike Kleinf1241082020-12-14 15:59:09 -060066 return arena->make([&](void* ptr) {
67 return new (ptr) FwidthSquircleTestProcessor(viewMatrix);
68 });
Robert Phillips5d07c522019-11-18 11:19:51 -050069 }
70
71 const char* name() const override { return "FwidthSquircleTestProcessor"; }
72
73 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {}
74
Robert Phillipsf10535f2021-03-23 09:30:45 -040075 GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const final;
Robert Phillips5d07c522019-11-18 11:19:51 -050076
77private:
Chris Dalton09212192018-11-13 15:07:24 -050078 FwidthSquircleTestProcessor(const SkMatrix& viewMatrix)
79 : GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID)
80 , fViewMatrix(viewMatrix) {
81 this->setVertexAttributes(&gVertex, 1);
82 }
Chris Dalton09212192018-11-13 15:07:24 -050083
Chris Dalton09212192018-11-13 15:07:24 -050084 const SkMatrix fViewMatrix;
85
86 class Impl;
Robert Phillips5d07c522019-11-18 11:19:51 -050087
John Stiles7571f9e2020-09-02 22:42:33 -040088 using INHERITED = GrGeometryProcessor;
Chris Dalton09212192018-11-13 15:07:24 -050089};
90
91class FwidthSquircleTestProcessor::Impl : public GrGLSLGeometryProcessor {
92 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Robert Phillips787fd9d2021-03-22 14:48:09 -040093 const auto& proc = args.fGeomProc.cast<FwidthSquircleTestProcessor>();
Chris Dalton09212192018-11-13 15:07:24 -050094
95 auto* uniforms = args.fUniformHandler;
Ethan Nicholas16464c32020-04-06 13:53:05 -040096 fViewMatrixHandle = uniforms->addUniform(nullptr, kVertex_GrShaderFlag, kFloat3x3_GrSLType,
97 "viewmatrix");
Chris Dalton09212192018-11-13 15:07:24 -050098
99 auto* varyings = args.fVaryingHandler;
100 varyings->emitAttributes(proc);
101
102 GrGLSLVarying squircleCoord(kFloat2_GrSLType);
103 varyings->addVarying("bboxcoord", &squircleCoord);
104
105 auto* v = args.fVertBuilder;
106 v->codeAppendf("float2x2 R = float2x2(cos(.05), sin(.05), -sin(.05), cos(.05));");
107
108 v->codeAppendf("%s = bboxcoord * 1.25;", squircleCoord.vsOut());
109 v->codeAppendf("float3 vertexpos = float3(bboxcoord * 100 * R + 100, 1);");
110 v->codeAppendf("vertexpos = %s * vertexpos;", uniforms->getUniformCStr(fViewMatrixHandle));
111 gpArgs->fPositionVar.set(kFloat3_GrSLType, "vertexpos");
112
113 auto* f = args.fFragBuilder;
114 f->codeAppendf("float golden_ratio = 1.61803398875;");
115 f->codeAppendf("float pi = 3.141592653589793;");
116 f->codeAppendf("float x = abs(%s.x), y = abs(%s.y);",
117 squircleCoord.fsIn(), squircleCoord.fsIn());
118
119 // Squircle function!
Ethan Nicholase1f55022019-02-05 17:17:40 -0500120 f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + pow(y, golden_ratio*pi) - 1);");
Chris Dalton09212192018-11-13 15:07:24 -0500121 f->codeAppendf("float fnwidth = fwidth(fn);");
122 f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500123 f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);");
Chris Dalton09212192018-11-13 15:07:24 -0500124
John Stiles4d7ac492021-03-09 20:16:43 -0500125 f->codeAppendf("half4 %s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor);
126 f->codeAppendf("half4 %s = half4(coverage);", args.fOutputCoverage);
Chris Dalton09212192018-11-13 15:07:24 -0500127 }
128
Brian Osman609f1592020-07-01 15:14:39 -0400129 void setData(const GrGLSLProgramDataManager& pdman,
Brian Salomon5a328282021-04-14 10:32:25 -0400130 const GrShaderCaps&,
Robert Phillips787fd9d2021-03-22 14:48:09 -0400131 const GrGeometryProcessor& geomProc) override {
132 const auto& proc = geomProc.cast<FwidthSquircleTestProcessor>();
Chris Dalton09212192018-11-13 15:07:24 -0500133 pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix);
134 }
135
136 UniformHandle fViewMatrixHandle;
137};
138
Robert Phillipsf10535f2021-03-23 09:30:45 -0400139GrGLSLGeometryProcessor* FwidthSquircleTestProcessor::createGLSLInstance(
Chris Dalton09212192018-11-13 15:07:24 -0500140 const GrShaderCaps&) const {
141 return new Impl();
142}
143
144////////////////////////////////////////////////////////////////////////////////////////////////////
145// Draw Op.
146
147class FwidthSquircleTestOp : public GrDrawOp {
148public:
149 DEFINE_OP_CLASS_ID
150
Herb Derbyc76d4092020-10-07 16:46:15 -0400151 static GrOp::Owner Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) {
152 return GrOp::Make<FwidthSquircleTestOp>(ctx, viewMatrix);
Chris Dalton09212192018-11-13 15:07:24 -0500153 }
154
155private:
156 FwidthSquircleTestOp(const SkMatrix& viewMatrix)
157 : GrDrawOp(ClassID())
158 , fViewMatrix(viewMatrix) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400159 this->setBounds(SkRect::MakeIWH(kWidth, kHeight), HasAABloat::kNo, IsHairline::kNo);
Chris Dalton09212192018-11-13 15:07:24 -0500160 }
161
Robert Phillips901aff02019-10-08 12:32:56 -0400162 const char* name() const override { return "FwidthSquircleTestOp"; }
Chris Dalton09212192018-11-13 15:07:24 -0500163 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton57ab06c2021-04-22 12:57:28 -0600164 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700165 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton09212192018-11-13 15:07:24 -0500166 }
Robert Phillips5d07c522019-11-18 11:19:51 -0500167
Robert Phillipsac6156c2020-02-28 16:02:40 -0500168 GrProgramInfo* createProgramInfo(const GrCaps* caps,
169 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500170 const GrSurfaceProxyView& writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500171 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400172 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500173 GrXferBarrierFlags renderPassXferBarriers,
174 GrLoadOp colorLoadOp) const {
Robert Phillipsac6156c2020-02-28 16:02:40 -0500175 GrGeometryProcessor* geomProc = FwidthSquircleTestProcessor::Make(arena, fViewMatrix);
176
Brian Salomon8afde5f2020-04-01 16:22:00 -0400177 return sk_gpu_test::CreateProgramInfo(caps, arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500178 std::move(appliedClip), dstProxyView,
179 geomProc, SkBlendMode::kSrcOver,
Greg Danield358cbe2020-09-11 09:33:54 -0400180 GrPrimitiveType::kTriangleStrip,
Greg Daniel42dbca52020-11-20 10:22:43 -0500181 renderPassXferBarriers, colorLoadOp);
Robert Phillipsac6156c2020-02-28 16:02:40 -0500182 }
183
184 GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const {
185 return this->createProgramInfo(&flushState->caps(),
186 flushState->allocator(),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400187 flushState->writeView(),
Robert Phillipsac6156c2020-02-28 16:02:40 -0500188 flushState->detachAppliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400189 flushState->dstProxyView(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500190 flushState->renderPassBarriers(),
191 flushState->colorLoadOp());
Robert Phillipsac6156c2020-02-28 16:02:40 -0500192 }
193
Robert Phillips5d07c522019-11-18 11:19:51 -0500194 void onPrePrepare(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -0500195 const GrSurfaceProxyView& writeView,
Robert Phillips34cea002019-11-21 16:02:34 -0500196 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -0400197 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500198 GrXferBarrierFlags renderPassXferBarriers,
199 GrLoadOp colorLoadOp) final {
Robert Phillips5d07c522019-11-18 11:19:51 -0500200 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
201
Robert Phillips34cea002019-11-21 16:02:34 -0500202 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400203 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillips5d07c522019-11-18 11:19:51 -0500204
Brian Salomon8afde5f2020-04-01 16:22:00 -0400205 fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -0400206 std::move(appliedClip), dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500207 renderPassXferBarriers, colorLoadOp);
Robert Phillips5d07c522019-11-18 11:19:51 -0500208
Robert Phillipsac6156c2020-02-28 16:02:40 -0500209 context->priv().recordProgramInfo(fProgramInfo);
Robert Phillips5d07c522019-11-18 11:19:51 -0500210 }
211
212 void onPrepare(GrOpFlushState* flushState) final {
Chris Dalton09212192018-11-13 15:07:24 -0500213 SkPoint vertices[4] = {
214 {-1, -1},
215 {+1, -1},
216 {-1, +1},
217 {+1, +1},
218 };
Greg Danielf793de12019-09-05 13:23:23 -0400219 fVertexBuffer = flushState->resourceProvider()->createBuffer(
220 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
221 }
Robert Phillips5d07c522019-11-18 11:19:51 -0500222
223 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final {
Greg Danielf793de12019-09-05 13:23:23 -0400224 if (!fVertexBuffer) {
Chris Dalton09212192018-11-13 15:07:24 -0500225 return;
226 }
Robert Phillips901aff02019-10-08 12:32:56 -0400227
Robert Phillips34cea002019-11-21 16:02:34 -0500228 if (!fProgramInfo) {
Robert Phillipsac6156c2020-02-28 16:02:40 -0500229 fProgramInfo = this->createProgramInfo(flushState);
Robert Phillips34cea002019-11-21 16:02:34 -0500230 }
231
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600232 flushState->bindPipeline(*fProgramInfo, SkRect::MakeIWH(kWidth, kHeight));
Greg Daniel426274b2020-07-20 11:37:38 -0400233 flushState->bindBuffers(nullptr, nullptr, std::move(fVertexBuffer));
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600234 flushState->draw(4, 0);
Robert Phillips5d07c522019-11-18 11:19:51 -0500235
Chris Dalton09212192018-11-13 15:07:24 -0500236 }
237
Greg Daniel55e5be32019-09-30 12:23:26 -0400238 static const int kWidth = 200;
239 static const int kHeight = 200;
Chris Dalton09212192018-11-13 15:07:24 -0500240
Robert Phillips5d07c522019-11-18 11:19:51 -0500241 sk_sp<GrBuffer> fVertexBuffer;
242 const SkMatrix fViewMatrix;
243
Robert Phillips787fd9d2021-03-22 14:48:09 -0400244 // The program info (and both the GrPipeline and GrGeometryProcessor it relies on), when
Robert Phillips34cea002019-11-21 16:02:34 -0500245 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
246 // arena's job to free up their memory so we just have a bare programInfo pointer here. We
Robert Phillips787fd9d2021-03-22 14:48:09 -0400247 // don't even store the GrPipeline and GrGeometryProcessor pointers here bc they are
Robert Phillips34cea002019-11-21 16:02:34 -0500248 // guaranteed to have the same lifetime as the program info.
Robert Phillips5d07c522019-11-18 11:19:51 -0500249 GrProgramInfo* fProgramInfo = nullptr;
250
Herb Derbyc76d4092020-10-07 16:46:15 -0400251 friend class ::GrOp; // for ctor
Robert Phillips5d07c522019-11-18 11:19:51 -0500252
John Stiles7571f9e2020-09-02 22:42:33 -0400253 using INHERITED = GrDrawOp;
Chris Dalton09212192018-11-13 15:07:24 -0500254};
255
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400256} // namespace
257
Chris Dalton09212192018-11-13 15:07:24 -0500258////////////////////////////////////////////////////////////////////////////////////////////////////
259// Test.
260
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400261namespace skiagm {
262
263DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, rContext, canvas, errorMsg, 200, 200) {
264 if (!rContext->priv().caps()->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700265 *errorMsg = "Shader derivatives not supported.";
266 return DrawResult::kSkip;
Chris Dalton09212192018-11-13 15:07:24 -0500267 }
268
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400269 auto sdc = SkCanvasPriv::TopDeviceSurfaceDrawContext(canvas);
270 if (!sdc) {
271 *errorMsg = GM::kErrorMsg_DrawSkippedGpuOnly;
272 return DrawResult::kSkip;
273 }
274
Chris Dalton09212192018-11-13 15:07:24 -0500275 // Draw the test directly to the frame buffer.
Chris Dalton3a778372019-02-07 15:23:36 -0700276 canvas->clear(SK_ColorWHITE);
Robert Phillips7a0d3c32021-07-21 15:39:51 -0400277 sdc->addDrawOp(FwidthSquircleTestOp::Make(rContext, canvas->getTotalMatrix()));
Chris Dalton50e24d72019-02-07 16:20:09 -0700278 return skiagm::DrawResult::kOk;
Chris Dalton09212192018-11-13 15:07:24 -0500279}
280
John Stilesa6841be2020-08-06 14:11:56 -0400281} // namespace skiagm