blob: 8a563ad9b12fc9614480af8c435350622f54095d [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/GrGpuCommandBuffer.h"
26#include "src/gpu/GrMemoryPool.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include "src/gpu/GrMesh.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrOpFlushState.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"
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"
48
49#include <memory>
50#include <utility>
51
52class GrAppliedClip;
Chris Dalton09212192018-11-13 15:07:24 -050053
Chris Dalton3a778372019-02-07 15:23:36 -070054/**
55 * This test ensures that fwidth() works properly on GPU configs by drawing a squircle.
56 */
Chris Dalton09212192018-11-13 15:07:24 -050057namespace skiagm {
58
59static constexpr GrGeometryProcessor::Attribute gVertex =
60 {"bboxcoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
61
Chris Dalton09212192018-11-13 15:07:24 -050062////////////////////////////////////////////////////////////////////////////////////////////////////
63// SkSL code.
64
65class FwidthSquircleTestProcessor : public GrGeometryProcessor {
66public:
67 FwidthSquircleTestProcessor(const SkMatrix& viewMatrix)
68 : GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID)
69 , fViewMatrix(viewMatrix) {
70 this->setVertexAttributes(&gVertex, 1);
71 }
72 const char* name() const override { return "FwidthSquircleTestProcessor"; }
73 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {}
74 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
75
76private:
77 const SkMatrix fViewMatrix;
78
79 class Impl;
80};
81
82class FwidthSquircleTestProcessor::Impl : public GrGLSLGeometryProcessor {
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!
Ethan Nicholase1f55022019-02-05 17:17:40 -0500111 f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + pow(y, golden_ratio*pi) - 1);");
Chris Dalton09212192018-11-13 15:07:24 -0500112 f->codeAppendf("float fnwidth = fwidth(fn);");
113 f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500114 f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);");
Chris Dalton09212192018-11-13 15:07:24 -0500115
116 f->codeAppendf("%s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor);
117 f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
118 }
119
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
129GrGLSLPrimitiveProcessor* FwidthSquircleTestProcessor::createGLSLInstance(
130 const GrShaderCaps&) const {
131 return new Impl();
132}
133
134////////////////////////////////////////////////////////////////////////////////////////////////////
135// Draw Op.
136
137class FwidthSquircleTestOp : public GrDrawOp {
138public:
139 DEFINE_OP_CLASS_ID
140
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500141 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500142 GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
Chris Dalton09212192018-11-13 15:07:24 -0500143 return pool->allocate<FwidthSquircleTestOp>(viewMatrix);
144 }
145
146private:
147 FwidthSquircleTestOp(const SkMatrix& viewMatrix)
148 : GrDrawOp(ClassID())
149 , fViewMatrix(viewMatrix) {
150 this->setBounds(SkRect::MakeIWH(200, 200), HasAABloat::kNo, IsZeroArea::kNo);
151 }
152
153 const char* name() const override { return "ClockwiseTestOp"; }
154 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman5ced0bf2019-03-15 10:15:29 -0400155 GrProcessorSet::Analysis finalize(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600156 const GrCaps&, const GrAppliedClip*, bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700157 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton09212192018-11-13 15:07:24 -0500158 }
159 void onPrepare(GrOpFlushState*) override {}
Brian Salomon588cec72018-11-14 13:56:37 -0500160 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) 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 };
Brian Salomon12d22642019-01-29 14:38:50 -0500167 sk_sp<const GrBuffer> vertexBuffer(flushState->resourceProvider()->createBuffer(
Brian Salomondbf70722019-02-07 11:31:24 -0500168 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices));
Chris Dalton09212192018-11-13 15:07:24 -0500169 if (!vertexBuffer) {
170 return;
171 }
Greg Daniel2c3398d2019-06-19 11:58:01 -0400172 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrcOver,
173 flushState->drawOpArgs().fOutputSwizzle);
Chris Dalton09212192018-11-13 15:07:24 -0500174 GrMesh mesh(GrPrimitiveType::kTriangleStrip);
175 mesh.setNonIndexedNonInstanced(4);
Brian Salomon12d22642019-01-29 14:38:50 -0500176 mesh.setVertexData(std::move(vertexBuffer));
Chris Dalton09212192018-11-13 15:07:24 -0500177 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
Chris Dalton50e24d72019-02-07 16:20:09 -0700189DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, ctx, rtc, canvas, errorMsg, 200, 200) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500190 if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700191 *errorMsg = "Shader derivatives not supported.";
192 return DrawResult::kSkip;
Chris Dalton09212192018-11-13 15:07:24 -0500193 }
194
195 // Draw the test directly to the frame buffer.
Chris Dalton3a778372019-02-07 15:23:36 -0700196 canvas->clear(SK_ColorWHITE);
Chris Dalton09212192018-11-13 15:07:24 -0500197 rtc->priv().testingOnly_addDrawOp(FwidthSquircleTestOp::Make(ctx, canvas->getTotalMatrix()));
Chris Dalton50e24d72019-02-07 16:20:09 -0700198 return skiagm::DrawResult::kOk;
Chris Dalton09212192018-11-13 15:07:24 -0500199}
200
Chris Dalton09212192018-11-13 15:07:24 -0500201}