Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/gpu/GrContext.h" |
| 18 | #include "include/private/GrRecordingContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 19 | #include "include/private/GrTypesPriv.h" |
| 20 | #include "src/gpu/GrBuffer.h" |
| 21 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/GrContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrGeometryProcessor.h" |
| 24 | #include "src/gpu/GrGpuBuffer.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/GrMemoryPool.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include "src/gpu/GrMesh.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "src/gpu/GrOpFlushState.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 28 | #include "src/gpu/GrOpsRenderPass.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 29 | #include "src/gpu/GrPipeline.h" |
| 30 | #include "src/gpu/GrPrimitiveProcessor.h" |
| 31 | #include "src/gpu/GrProcessor.h" |
| 32 | #include "src/gpu/GrProcessorSet.h" |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 33 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 34 | #include "src/gpu/GrRecordingContextPriv.h" |
| 35 | #include "src/gpu/GrRenderTargetContext.h" |
| 36 | #include "src/gpu/GrRenderTargetContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 37 | #include "src/gpu/GrResourceProvider.h" |
| 38 | #include "src/gpu/GrShaderCaps.h" |
| 39 | #include "src/gpu/GrShaderVar.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 40 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 41 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 42 | #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h" |
| 43 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 44 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 45 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 46 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 47 | #include "src/gpu/ops/GrDrawOp.h" |
| 48 | #include "src/gpu/ops/GrOp.h" |
| 49 | |
| 50 | #include <memory> |
| 51 | #include <utility> |
| 52 | |
| 53 | class GrAppliedClip; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 54 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 55 | /** |
| 56 | * This test ensures that fwidth() works properly on GPU configs by drawing a squircle. |
| 57 | */ |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 58 | namespace skiagm { |
| 59 | |
| 60 | static constexpr GrGeometryProcessor::Attribute gVertex = |
| 61 | {"bboxcoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 62 | |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 63 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 64 | // SkSL code. |
| 65 | |
| 66 | class FwidthSquircleTestProcessor : public GrGeometryProcessor { |
| 67 | public: |
| 68 | FwidthSquircleTestProcessor(const SkMatrix& viewMatrix) |
| 69 | : GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID) |
| 70 | , fViewMatrix(viewMatrix) { |
| 71 | this->setVertexAttributes(&gVertex, 1); |
| 72 | } |
| 73 | const char* name() const override { return "FwidthSquircleTestProcessor"; } |
| 74 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {} |
| 75 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 76 | |
| 77 | private: |
| 78 | const SkMatrix fViewMatrix; |
| 79 | |
| 80 | class Impl; |
| 81 | }; |
| 82 | |
| 83 | class FwidthSquircleTestProcessor::Impl : public GrGLSLGeometryProcessor { |
| 84 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 85 | const auto& proc = args.fGP.cast<FwidthSquircleTestProcessor>(); |
| 86 | |
| 87 | auto* uniforms = args.fUniformHandler; |
| 88 | fViewMatrixHandle = |
| 89 | uniforms->addUniform(kVertex_GrShaderFlag, kFloat3x3_GrSLType, "viewmatrix"); |
| 90 | |
| 91 | auto* varyings = args.fVaryingHandler; |
| 92 | varyings->emitAttributes(proc); |
| 93 | |
| 94 | GrGLSLVarying squircleCoord(kFloat2_GrSLType); |
| 95 | varyings->addVarying("bboxcoord", &squircleCoord); |
| 96 | |
| 97 | auto* v = args.fVertBuilder; |
| 98 | v->codeAppendf("float2x2 R = float2x2(cos(.05), sin(.05), -sin(.05), cos(.05));"); |
| 99 | |
| 100 | v->codeAppendf("%s = bboxcoord * 1.25;", squircleCoord.vsOut()); |
| 101 | v->codeAppendf("float3 vertexpos = float3(bboxcoord * 100 * R + 100, 1);"); |
| 102 | v->codeAppendf("vertexpos = %s * vertexpos;", uniforms->getUniformCStr(fViewMatrixHandle)); |
| 103 | gpArgs->fPositionVar.set(kFloat3_GrSLType, "vertexpos"); |
| 104 | |
| 105 | auto* f = args.fFragBuilder; |
| 106 | f->codeAppendf("float golden_ratio = 1.61803398875;"); |
| 107 | f->codeAppendf("float pi = 3.141592653589793;"); |
| 108 | f->codeAppendf("float x = abs(%s.x), y = abs(%s.y);", |
| 109 | squircleCoord.fsIn(), squircleCoord.fsIn()); |
| 110 | |
| 111 | // Squircle function! |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 112 | f->codeAppendf("float fn = half(pow(x, golden_ratio*pi) + pow(y, golden_ratio*pi) - 1);"); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 113 | f->codeAppendf("float fnwidth = fwidth(fn);"); |
| 114 | f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero. |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 115 | f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);"); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 116 | |
| 117 | f->codeAppendf("%s = half4(.51, .42, .71, 1) * .89;", args.fOutputColor); |
| 118 | f->codeAppendf("%s = half4(coverage);", args.fOutputCoverage); |
| 119 | } |
| 120 | |
| 121 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 122 | FPCoordTransformIter&& transformIter) override { |
| 123 | const auto& proc = primProc.cast<FwidthSquircleTestProcessor>(); |
| 124 | pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix); |
| 125 | } |
| 126 | |
| 127 | UniformHandle fViewMatrixHandle; |
| 128 | }; |
| 129 | |
| 130 | GrGLSLPrimitiveProcessor* FwidthSquircleTestProcessor::createGLSLInstance( |
| 131 | const GrShaderCaps&) const { |
| 132 | return new Impl(); |
| 133 | } |
| 134 | |
| 135 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 136 | // Draw Op. |
| 137 | |
| 138 | class FwidthSquircleTestOp : public GrDrawOp { |
| 139 | public: |
| 140 | DEFINE_OP_CLASS_ID |
| 141 | |
Robert Phillips | be9aff2 | 2019-02-15 11:33:22 -0500 | [diff] [blame] | 142 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 143 | GrOpMemoryPool* pool = ctx->priv().opMemoryPool(); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 144 | return pool->allocate<FwidthSquircleTestOp>(viewMatrix); |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | FwidthSquircleTestOp(const SkMatrix& viewMatrix) |
| 149 | : GrDrawOp(ClassID()) |
| 150 | , fViewMatrix(viewMatrix) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 151 | this->setBounds(SkRect::MakeIWH(kWidth, kHeight), HasAABloat::kNo, IsHairline::kNo); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 154 | const char* name() const override { return "FwidthSquircleTestOp"; } |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 155 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 156 | GrProcessorSet::Analysis finalize( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 157 | const GrCaps&, const GrAppliedClip*, bool hasMixedSampledCoverage, GrClampType) override { |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 158 | return GrProcessorSet::EmptySetAnalysis(); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 159 | } |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 160 | void onPrepare(GrOpFlushState* flushState) override { |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 161 | SkPoint vertices[4] = { |
| 162 | {-1, -1}, |
| 163 | {+1, -1}, |
| 164 | {-1, +1}, |
| 165 | {+1, +1}, |
| 166 | }; |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 167 | fVertexBuffer = flushState->resourceProvider()->createBuffer( |
| 168 | sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices); |
| 169 | } |
| 170 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 171 | if (!fVertexBuffer) { |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 172 | return; |
| 173 | } |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 174 | GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrcOver, |
Robert Phillips | 405413f | 2019-10-04 10:39:28 -0400 | [diff] [blame] | 175 | flushState->drawOpArgs().outputSwizzle()); |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 176 | |
| 177 | FwidthSquircleTestProcessor primProc(fViewMatrix); |
| 178 | |
Chris Dalton | 5e8cdfd | 2019-11-11 15:23:30 -0700 | [diff] [blame] | 179 | GrProgramInfo programInfo(flushState->proxy()->numSamples(), |
| 180 | flushState->proxy()->numStencilSamples(), |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 181 | flushState->drawOpArgs().origin(), |
Robert Phillips | 67a625e | 2019-11-15 15:37:07 -0500 | [diff] [blame] | 182 | &pipeline, |
| 183 | &primProc, |
Robert Phillips | cea290f | 2019-11-06 11:21:03 -0500 | [diff] [blame] | 184 | nullptr, nullptr, 0, |
| 185 | GrPrimitiveType::kTriangleStrip); |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 186 | |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 187 | GrMesh mesh(GrPrimitiveType::kTriangleStrip); |
| 188 | mesh.setNonIndexedNonInstanced(4); |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 189 | mesh.setVertexData(std::move(fVertexBuffer)); |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 190 | flushState->opsRenderPass()->draw(programInfo, &mesh, 1, SkRect::MakeIWH(kWidth, kHeight)); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 191 | } |
| 192 | |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 193 | sk_sp<GrBuffer> fVertexBuffer; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 194 | const SkMatrix fViewMatrix; |
Greg Daniel | 55e5be3 | 2019-09-30 12:23:26 -0400 | [diff] [blame] | 195 | static const int kWidth = 200; |
| 196 | static const int kHeight = 200; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 197 | |
| 198 | friend class ::GrOpMemoryPool; // for ctor |
| 199 | }; |
| 200 | |
| 201 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 202 | // Test. |
| 203 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 204 | DEF_SIMPLE_GPU_GM_CAN_FAIL(fwidth_squircle, ctx, rtc, canvas, errorMsg, 200, 200) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 205 | if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport()) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 206 | *errorMsg = "Shader derivatives not supported."; |
| 207 | return DrawResult::kSkip; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Draw the test directly to the frame buffer. |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 211 | canvas->clear(SK_ColorWHITE); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 212 | rtc->priv().testingOnly_addDrawOp(FwidthSquircleTestOp::Make(ctx, canvas->getTotalMatrix())); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 213 | return skiagm::DrawResult::kOk; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 214 | } |
| 215 | |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 216 | } |