blob: 138cd888df0c916103521c15f9f0b6d49a30ac0e [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
8#include "gm.h"
9#include "sk_tool_utils.h"
10#include "SkTextUtils.h"
11
12#if SK_SUPPORT_GPU
13
14#include "GrContext.h"
15#include "GrGpuCommandBuffer.h"
16#include "GrMemoryPool.h"
17#include "GrRenderTargetContext.h"
18#include "GrRenderTargetContextPriv.h"
19#include "glsl/GrGLSLFragmentShaderBuilder.h"
20#include "glsl/GrGLSLGeometryProcessor.h"
21#include "glsl/GrGLSLVarying.h"
22#include "glsl/GrGLSLVertexGeoBuilder.h"
23
Chris Dalton3a778372019-02-07 15:23:36 -070024/**
25 * This test ensures that fwidth() works properly on GPU configs by drawing a squircle.
26 */
Chris Dalton09212192018-11-13 15:07:24 -050027namespace skiagm {
28
29static constexpr GrGeometryProcessor::Attribute gVertex =
30 {"bboxcoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
31
Chris Dalton09212192018-11-13 15:07:24 -050032////////////////////////////////////////////////////////////////////////////////////////////////////
33// SkSL code.
34
35class FwidthSquircleTestProcessor : public GrGeometryProcessor {
36public:
37 FwidthSquircleTestProcessor(const SkMatrix& viewMatrix)
38 : GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID)
39 , fViewMatrix(viewMatrix) {
40 this->setVertexAttributes(&gVertex, 1);
41 }
42 const char* name() const override { return "FwidthSquircleTestProcessor"; }
43 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {}
44 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
45
46private:
47 const SkMatrix fViewMatrix;
48
49 class Impl;
50};
51
52class FwidthSquircleTestProcessor::Impl : public GrGLSLGeometryProcessor {
53 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
54 const auto& proc = args.fGP.cast<FwidthSquircleTestProcessor>();
55
56 auto* uniforms = args.fUniformHandler;
57 fViewMatrixHandle =
58 uniforms->addUniform(kVertex_GrShaderFlag, kFloat3x3_GrSLType, "viewmatrix");
59
60 auto* varyings = args.fVaryingHandler;
61 varyings->emitAttributes(proc);
62
63 GrGLSLVarying squircleCoord(kFloat2_GrSLType);
64 varyings->addVarying("bboxcoord", &squircleCoord);
65
66 auto* v = args.fVertBuilder;
67 v->codeAppendf("float2x2 R = float2x2(cos(.05), sin(.05), -sin(.05), cos(.05));");
68
69 v->codeAppendf("%s = bboxcoord * 1.25;", squircleCoord.vsOut());
70 v->codeAppendf("float3 vertexpos = float3(bboxcoord * 100 * R + 100, 1);");
71 v->codeAppendf("vertexpos = %s * vertexpos;", uniforms->getUniformCStr(fViewMatrixHandle));
72 gpArgs->fPositionVar.set(kFloat3_GrSLType, "vertexpos");
73
74 auto* f = args.fFragBuilder;
75 f->codeAppendf("float golden_ratio = 1.61803398875;");
76 f->codeAppendf("float pi = 3.141592653589793;");
77 f->codeAppendf("float x = abs(%s.x), y = abs(%s.y);",
78 squircleCoord.fsIn(), squircleCoord.fsIn());
79
80 // Squircle function!
Ethan Nicholase1f55022019-02-05 17:17:40 -050081 f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + pow(y, golden_ratio*pi) - 1);");
Chris Dalton09212192018-11-13 15:07:24 -050082 f->codeAppendf("float fnwidth = fwidth(fn);");
83 f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero.
Ethan Nicholase1f55022019-02-05 17:17:40 -050084 f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);");
Chris Dalton09212192018-11-13 15:07:24 -050085
86 f->codeAppendf("%s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor);
87 f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage);
88 }
89
90 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc,
91 FPCoordTransformIter&& transformIter) override {
92 const auto& proc = primProc.cast<FwidthSquircleTestProcessor>();
93 pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix);
94 }
95
96 UniformHandle fViewMatrixHandle;
97};
98
99GrGLSLPrimitiveProcessor* FwidthSquircleTestProcessor::createGLSLInstance(
100 const GrShaderCaps&) const {
101 return new Impl();
102}
103
104////////////////////////////////////////////////////////////////////////////////////////////////////
105// Draw Op.
106
107class FwidthSquircleTestOp : public GrDrawOp {
108public:
109 DEFINE_OP_CLASS_ID
110
111 static std::unique_ptr<GrDrawOp> Make(GrContext* ctx, const SkMatrix& viewMatrix) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500112 GrOpMemoryPool* pool = ctx->priv().opMemoryPool();
Chris Dalton09212192018-11-13 15:07:24 -0500113 return pool->allocate<FwidthSquircleTestOp>(viewMatrix);
114 }
115
116private:
117 FwidthSquircleTestOp(const SkMatrix& viewMatrix)
118 : GrDrawOp(ClassID())
119 , fViewMatrix(viewMatrix) {
120 this->setBounds(SkRect::MakeIWH(200, 200), HasAABloat::kNo, IsZeroArea::kNo);
121 }
122
123 const char* name() const override { return "ClockwiseTestOp"; }
124 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton4b62aed2019-01-15 11:53:00 -0700125 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*) override {
126 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton09212192018-11-13 15:07:24 -0500127 }
128 void onPrepare(GrOpFlushState*) override {}
Brian Salomon588cec72018-11-14 13:56:37 -0500129 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Dalton09212192018-11-13 15:07:24 -0500130 SkPoint vertices[4] = {
131 {-1, -1},
132 {+1, -1},
133 {-1, +1},
134 {+1, +1},
135 };
Brian Salomon12d22642019-01-29 14:38:50 -0500136 sk_sp<const GrBuffer> vertexBuffer(flushState->resourceProvider()->createBuffer(
Brian Salomondbf70722019-02-07 11:31:24 -0500137 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices));
Chris Dalton09212192018-11-13 15:07:24 -0500138 if (!vertexBuffer) {
139 return;
140 }
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500141 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrcOver);
Chris Dalton09212192018-11-13 15:07:24 -0500142 GrMesh mesh(GrPrimitiveType::kTriangleStrip);
143 mesh.setNonIndexedNonInstanced(4);
Brian Salomon12d22642019-01-29 14:38:50 -0500144 mesh.setVertexData(std::move(vertexBuffer));
Chris Dalton09212192018-11-13 15:07:24 -0500145 flushState->rtCommandBuffer()->draw(FwidthSquircleTestProcessor(fViewMatrix), pipeline,
146 nullptr, nullptr, &mesh, 1, SkRect::MakeIWH(100, 100));
147 }
148
149 const SkMatrix fViewMatrix;
150
151 friend class ::GrOpMemoryPool; // for ctor
152};
153
154////////////////////////////////////////////////////////////////////////////////////////////////////
155// Test.
156
Chris Dalton50e24d72019-02-07 16:20:09 -0700157DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, ctx, rtc, canvas, errorMsg, 200, 200) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500158 if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton50e24d72019-02-07 16:20:09 -0700159 *errorMsg = "Shader derivatives not supported.";
160 return DrawResult::kSkip;
Chris Dalton09212192018-11-13 15:07:24 -0500161 }
162
163 // Draw the test directly to the frame buffer.
Chris Dalton3a778372019-02-07 15:23:36 -0700164 canvas->clear(SK_ColorWHITE);
Chris Dalton09212192018-11-13 15:07:24 -0500165 rtc->priv().testingOnly_addDrawOp(FwidthSquircleTestOp::Make(ctx, canvas->getTotalMatrix()));
Chris Dalton50e24d72019-02-07 16:20:09 -0700166 return skiagm::DrawResult::kOk;
Chris Dalton09212192018-11-13 15:07:24 -0500167}
168
Chris Dalton09212192018-11-13 15:07:24 -0500169}
170
171#endif // SK_SUPPORT_GPU