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 | |
| 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 Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 24 | /** |
| 25 | * This test ensures that fwidth() works properly on GPU configs by drawing a squircle. |
| 26 | */ |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 27 | namespace skiagm { |
| 28 | |
| 29 | static constexpr GrGeometryProcessor::Attribute gVertex = |
| 30 | {"bboxcoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 31 | |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 32 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 33 | // SkSL code. |
| 34 | |
| 35 | class FwidthSquircleTestProcessor : public GrGeometryProcessor { |
| 36 | public: |
| 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 | |
| 46 | private: |
| 47 | const SkMatrix fViewMatrix; |
| 48 | |
| 49 | class Impl; |
| 50 | }; |
| 51 | |
| 52 | class 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 Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 81 | 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] | 82 | f->codeAppendf("float fnwidth = fwidth(fn);"); |
| 83 | f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero. |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 84 | f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);"); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 85 | |
| 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 | |
| 99 | GrGLSLPrimitiveProcessor* FwidthSquircleTestProcessor::createGLSLInstance( |
| 100 | const GrShaderCaps&) const { |
| 101 | return new Impl(); |
| 102 | } |
| 103 | |
| 104 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 105 | // Draw Op. |
| 106 | |
| 107 | class FwidthSquircleTestOp : public GrDrawOp { |
| 108 | public: |
| 109 | DEFINE_OP_CLASS_ID |
| 110 | |
| 111 | static std::unique_ptr<GrDrawOp> Make(GrContext* ctx, const SkMatrix& viewMatrix) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 112 | GrOpMemoryPool* pool = ctx->priv().opMemoryPool(); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 113 | return pool->allocate<FwidthSquircleTestOp>(viewMatrix); |
| 114 | } |
| 115 | |
| 116 | private: |
| 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 Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 125 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*) override { |
| 126 | return GrProcessorSet::EmptySetAnalysis(); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 127 | } |
| 128 | void onPrepare(GrOpFlushState*) override {} |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 129 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 130 | SkPoint vertices[4] = { |
| 131 | {-1, -1}, |
| 132 | {+1, -1}, |
| 133 | {-1, +1}, |
| 134 | {+1, +1}, |
| 135 | }; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 136 | sk_sp<const GrBuffer> vertexBuffer(flushState->resourceProvider()->createBuffer( |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 137 | sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices)); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 138 | if (!vertexBuffer) { |
| 139 | return; |
| 140 | } |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 141 | GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrcOver); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 142 | GrMesh mesh(GrPrimitiveType::kTriangleStrip); |
| 143 | mesh.setNonIndexedNonInstanced(4); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 144 | mesh.setVertexData(std::move(vertexBuffer)); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 145 | 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 Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 157 | 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] | 158 | if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport()) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 159 | *errorMsg = "Shader derivatives not supported."; |
| 160 | return DrawResult::kSkip; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Draw the test directly to the frame buffer. |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 164 | canvas->clear(SK_ColorWHITE); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 165 | rtc->priv().testingOnly_addDrawOp(FwidthSquircleTestOp::Make(ctx, canvas->getTotalMatrix())); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 166 | return skiagm::DrawResult::kOk; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | #endif // SK_SUPPORT_GPU |