blob: 3eae01f943a58d72d9ad8dce2510ce6f7400e720 [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"
Robert Phillips34cea002019-11-21 16:02:34 -050049#include "tools/gpu/ProxyUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040050
51#include <memory>
52#include <utility>
53
54class GrAppliedClip;
Chris Dalton09212192018-11-13 15:07:24 -050055
Chris Dalton3a778372019-02-07 15:23:36 -070056/**
57 * This test ensures that fwidth() works properly on GPU configs by drawing a squircle.
58 */
Chris Dalton09212192018-11-13 15:07:24 -050059namespace skiagm {
60
61static constexpr GrGeometryProcessor::Attribute gVertex =
62 {"bboxcoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
63
Chris Dalton09212192018-11-13 15:07:24 -050064////////////////////////////////////////////////////////////////////////////////////////////////////
65// SkSL code.
66
67class FwidthSquircleTestProcessor : public GrGeometryProcessor {
68public:
Robert Phillips5d07c522019-11-18 11:19:51 -050069 static GrGeometryProcessor* Make(SkArenaAlloc* arena, const SkMatrix& viewMatrix) {
70 return arena->make<FwidthSquircleTestProcessor>(viewMatrix);
71 }
72
73 const char* name() const override { return "FwidthSquircleTestProcessor"; }
74
75 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {}
76
77 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
78
79private:
80 friend class ::SkArenaAlloc; // for access to ctor
81
Chris Dalton09212192018-11-13 15:07:24 -050082 FwidthSquircleTestProcessor(const SkMatrix& viewMatrix)
83 : GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID)
84 , fViewMatrix(viewMatrix) {
85 this->setVertexAttributes(&gVertex, 1);
86 }
Chris Dalton09212192018-11-13 15:07:24 -050087
Chris Dalton09212192018-11-13 15:07:24 -050088 const SkMatrix fViewMatrix;
89
90 class Impl;
Robert Phillips5d07c522019-11-18 11:19:51 -050091
92 typedef GrGeometryProcessor INHERITED;
Chris Dalton09212192018-11-13 15:07:24 -050093};
94
95class FwidthSquircleTestProcessor::Impl : public GrGLSLGeometryProcessor {
96 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
97 const auto& proc = args.fGP.cast<FwidthSquircleTestProcessor>();
98
99 auto* uniforms = args.fUniformHandler;
100 fViewMatrixHandle =
101 uniforms->addUniform(kVertex_GrShaderFlag, kFloat3x3_GrSLType, "viewmatrix");
102
103 auto* varyings = args.fVaryingHandler;
104 varyings->emitAttributes(proc);
105
106 GrGLSLVarying squircleCoord(kFloat2_GrSLType);
107 varyings->addVarying("bboxcoord", &squircleCoord);
108
109 auto* v = args.fVertBuilder;
110 v->codeAppendf("float2x2 R = float2x2(cos(.05), sin(.05), -sin(.05), cos(.05));");
111
112 v->codeAppendf("%s = bboxcoord * 1.25;", squircleCoord.vsOut());
113 v->codeAppendf("float3 vertexpos = float3(bboxcoord * 100 * R + 100, 1);");
114 v->codeAppendf("vertexpos = %s * vertexpos;", uniforms->getUniformCStr(fViewMatrixHandle));
115 gpArgs->fPositionVar.set(kFloat3_GrSLType, "vertexpos");
116
117 auto* f = args.fFragBuilder;
118 f->codeAppendf("float golden_ratio = 1.61803398875;");
119 f->codeAppendf("float pi = 3.141592653589793;");
120 f->codeAppendf("float x = abs(%s.x), y = abs(%s.y);",
121 squircleCoord.fsIn(), squircleCoord.fsIn());
122
123 // Squircle function!
Ethan Nicholase1f55022019-02-05 17:17:40 -0500124 f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + pow(y, golden_ratio*pi) - 1);");
Chris Dalton09212192018-11-13 15:07:24 -0500125 f->codeAppendf("float fnwidth = fwidth(fn);");
126 f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500127 f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);");
Chris Dalton09212192018-11-13 15:07:24 -0500128
129 f->codeAppendf("%s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor);
130 f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
131 }
132
133 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
Brian Salomonc241b582019-11-27 08:57:17 -0500134 const CoordTransformRange&) override {
Chris Dalton09212192018-11-13 15:07:24 -0500135 const auto& proc = primProc.cast<FwidthSquircleTestProcessor>();
136 pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix);
137 }
138
139 UniformHandle fViewMatrixHandle;
140};
141
142GrGLSLPrimitiveProcessor* FwidthSquircleTestProcessor::createGLSLInstance(
143 const GrShaderCaps&) const {
144 return new Impl();
145}
146
147////////////////////////////////////////////////////////////////////////////////////////////////////
148// Draw Op.
149
150class FwidthSquircleTestOp : public GrDrawOp {
151public:
152 DEFINE_OP_CLASS_ID
153
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500154 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500155 GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
Chris Dalton09212192018-11-13 15:07:24 -0500156 return pool->allocate<FwidthSquircleTestOp>(viewMatrix);
157 }
158
159private:
160 FwidthSquircleTestOp(const SkMatrix& viewMatrix)
161 : GrDrawOp(ClassID())
162 , fViewMatrix(viewMatrix) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400163 this->setBounds(SkRect::MakeIWH(kWidth, kHeight), HasAABloat::kNo, IsHairline::kNo);
Chris Dalton09212192018-11-13 15:07:24 -0500164 }
165
Robert Phillips901aff02019-10-08 12:32:56 -0400166 const char* name() const override { return "FwidthSquircleTestOp"; }
Chris Dalton09212192018-11-13 15:07:24 -0500167 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Robert Phillips5d07c522019-11-18 11:19:51 -0500168 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
169 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700170 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton09212192018-11-13 15:07:24 -0500171 }
Robert Phillips5d07c522019-11-18 11:19:51 -0500172
173 void onPrePrepare(GrRecordingContext* context,
174 const GrSurfaceProxyView* dstView,
Robert Phillips34cea002019-11-21 16:02:34 -0500175 GrAppliedClip* clip,
Robert Phillips8053c972019-11-21 10:44:53 -0500176 const GrXferProcessor::DstProxyView& dstProxyView) final {
Robert Phillips5d07c522019-11-18 11:19:51 -0500177 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
178
Robert Phillips34cea002019-11-21 16:02:34 -0500179 // This is equivalent to a GrOpFlushState::detachAppliedClip
180 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
Robert Phillips5d07c522019-11-18 11:19:51 -0500181
Robert Phillips34cea002019-11-21 16:02:34 -0500182 GrGeometryProcessor* geomProc = FwidthSquircleTestProcessor::Make(arena, fViewMatrix);
Robert Phillips5d07c522019-11-18 11:19:51 -0500183
Robert Phillips34cea002019-11-21 16:02:34 -0500184 // TODO: need to also give this to the recording context
185 fProgramInfo = sk_gpu_test::CreateProgramInfo(context->priv().caps(), arena, dstView,
186 std::move(appliedClip), dstProxyView,
187 geomProc, SkBlendMode::kSrcOver,
188 GrPrimitiveType::kTriangleStrip);
Robert Phillips5d07c522019-11-18 11:19:51 -0500189 }
190
191 void onPrepare(GrOpFlushState* flushState) final {
Chris Dalton09212192018-11-13 15:07:24 -0500192 SkPoint vertices[4] = {
193 {-1, -1},
194 {+1, -1},
195 {-1, +1},
196 {+1, +1},
197 };
Greg Danielf793de12019-09-05 13:23:23 -0400198 fVertexBuffer = flushState->resourceProvider()->createBuffer(
199 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
200 }
Robert Phillips5d07c522019-11-18 11:19:51 -0500201
202 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final {
Greg Danielf793de12019-09-05 13:23:23 -0400203 if (!fVertexBuffer) {
Chris Dalton09212192018-11-13 15:07:24 -0500204 return;
205 }
Robert Phillips901aff02019-10-08 12:32:56 -0400206
Robert Phillips34cea002019-11-21 16:02:34 -0500207 if (!fProgramInfo) {
208 auto geomProc = FwidthSquircleTestProcessor::Make(flushState->allocator(),
209 fViewMatrix);
210
211 fProgramInfo = sk_gpu_test::CreateProgramInfo(&flushState->caps(),
212 flushState->allocator(),
213 flushState->view(),
214 flushState->detachAppliedClip(),
215 flushState->dstProxyView(),
216 geomProc, SkBlendMode::kSrcOver,
217 GrPrimitiveType::kTriangleStrip);
218 }
219
Chris Dalton09212192018-11-13 15:07:24 -0500220 GrMesh mesh(GrPrimitiveType::kTriangleStrip);
221 mesh.setNonIndexedNonInstanced(4);
Greg Danielf793de12019-09-05 13:23:23 -0400222 mesh.setVertexData(std::move(fVertexBuffer));
Robert Phillips5d07c522019-11-18 11:19:51 -0500223
Robert Phillips34cea002019-11-21 16:02:34 -0500224 flushState->opsRenderPass()->draw(*fProgramInfo, &mesh, 1,
225 SkRect::MakeIWH(kWidth, kHeight));
Robert Phillips5d07c522019-11-18 11:19:51 -0500226
Chris Dalton09212192018-11-13 15:07:24 -0500227 }
228
Greg Daniel55e5be32019-09-30 12:23:26 -0400229 static const int kWidth = 200;
230 static const int kHeight = 200;
Chris Dalton09212192018-11-13 15:07:24 -0500231
Robert Phillips5d07c522019-11-18 11:19:51 -0500232 sk_sp<GrBuffer> fVertexBuffer;
233 const SkMatrix fViewMatrix;
234
235 // The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when
Robert Phillips34cea002019-11-21 16:02:34 -0500236 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
237 // arena's job to free up their memory so we just have a bare programInfo pointer here. We
238 // don't even store the GrPipeline and GrPrimitiveProcessor pointers here bc they are
239 // guaranteed to have the same lifetime as the program info.
Robert Phillips5d07c522019-11-18 11:19:51 -0500240 GrProgramInfo* fProgramInfo = nullptr;
241
Chris Dalton09212192018-11-13 15:07:24 -0500242 friend class ::GrOpMemoryPool; // for ctor
Robert Phillips5d07c522019-11-18 11:19:51 -0500243
244 typedef GrDrawOp INHERITED;
Chris Dalton09212192018-11-13 15:07:24 -0500245};
246
247////////////////////////////////////////////////////////////////////////////////////////////////////
248// Test.
249
Chris Dalton50e24d72019-02-07 16:20:09 -0700250DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, ctx, rtc, canvas, errorMsg, 200, 200) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500251 if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700252 *errorMsg = "Shader derivatives not supported.";
253 return DrawResult::kSkip;
Chris Dalton09212192018-11-13 15:07:24 -0500254 }
255
256 // Draw the test directly to the frame buffer.
Chris Dalton3a778372019-02-07 15:23:36 -0700257 canvas->clear(SK_ColorWHITE);
Chris Dalton09212192018-11-13 15:07:24 -0500258 rtc->priv().testingOnly_addDrawOp(FwidthSquircleTestOp::Make(ctx, canvas->getTotalMatrix()));
Chris Dalton50e24d72019-02-07 16:20:09 -0700259 return skiagm::DrawResult::kOk;
Chris Dalton09212192018-11-13 15:07:24 -0500260}
261
Chris Dalton09212192018-11-13 15:07:24 -0500262}