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" |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 49 | #include "tools/gpu/ProxyUtils.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 50 | |
| 51 | #include <memory> |
| 52 | #include <utility> |
| 53 | |
| 54 | class GrAppliedClip; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 55 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 56 | /** |
| 57 | * This test ensures that fwidth() works properly on GPU configs by drawing a squircle. |
| 58 | */ |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 59 | namespace skiagm { |
| 60 | |
| 61 | static constexpr GrGeometryProcessor::Attribute gVertex = |
| 62 | {"bboxcoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 63 | |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 64 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 65 | // SkSL code. |
| 66 | |
| 67 | class FwidthSquircleTestProcessor : public GrGeometryProcessor { |
| 68 | public: |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 69 | 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 | |
| 79 | private: |
| 80 | friend class ::SkArenaAlloc; // for access to ctor |
| 81 | |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 82 | FwidthSquircleTestProcessor(const SkMatrix& viewMatrix) |
| 83 | : GrGeometryProcessor(kFwidthSquircleTestProcessor_ClassID) |
| 84 | , fViewMatrix(viewMatrix) { |
| 85 | this->setVertexAttributes(&gVertex, 1); |
| 86 | } |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 87 | |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 88 | const SkMatrix fViewMatrix; |
| 89 | |
| 90 | class Impl; |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 91 | |
| 92 | typedef GrGeometryProcessor INHERITED; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | class 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 Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 124 | 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] | 125 | f->codeAppendf("float fnwidth = fwidth(fn);"); |
| 126 | f->codeAppendf("fnwidth += 1e-10;"); // Guard against divide-by-zero. |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 127 | f->codeAppendf("half coverage = clamp(half(.5 - fn/fnwidth), 0, 1);"); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 128 | |
| 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 Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 134 | const CoordTransformRange&) override { |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 135 | const auto& proc = primProc.cast<FwidthSquircleTestProcessor>(); |
| 136 | pdman.setSkMatrix(fViewMatrixHandle, proc.fViewMatrix); |
| 137 | } |
| 138 | |
| 139 | UniformHandle fViewMatrixHandle; |
| 140 | }; |
| 141 | |
| 142 | GrGLSLPrimitiveProcessor* FwidthSquircleTestProcessor::createGLSLInstance( |
| 143 | const GrShaderCaps&) const { |
| 144 | return new Impl(); |
| 145 | } |
| 146 | |
| 147 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 148 | // Draw Op. |
| 149 | |
| 150 | class FwidthSquircleTestOp : public GrDrawOp { |
| 151 | public: |
| 152 | DEFINE_OP_CLASS_ID |
| 153 | |
Robert Phillips | be9aff2 | 2019-02-15 11:33:22 -0500 | [diff] [blame] | 154 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* ctx, const SkMatrix& viewMatrix) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 155 | GrOpMemoryPool* pool = ctx->priv().opMemoryPool(); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 156 | return pool->allocate<FwidthSquircleTestOp>(viewMatrix); |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | FwidthSquircleTestOp(const SkMatrix& viewMatrix) |
| 161 | : GrDrawOp(ClassID()) |
| 162 | , fViewMatrix(viewMatrix) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 163 | this->setBounds(SkRect::MakeIWH(kWidth, kHeight), HasAABloat::kNo, IsHairline::kNo); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 164 | } |
| 165 | |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 166 | const char* name() const override { return "FwidthSquircleTestOp"; } |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 167 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 168 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 169 | bool hasMixedSampledCoverage, GrClampType) override { |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 170 | return GrProcessorSet::EmptySetAnalysis(); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 171 | } |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 172 | |
| 173 | void onPrePrepare(GrRecordingContext* context, |
| 174 | const GrSurfaceProxyView* dstView, |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 175 | GrAppliedClip* clip, |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 176 | const GrXferProcessor::DstProxyView& dstProxyView) final { |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 177 | SkArenaAlloc* arena = context->priv().recordTimeAllocator(); |
| 178 | |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 179 | // This is equivalent to a GrOpFlushState::detachAppliedClip |
| 180 | GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip(); |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 181 | |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 182 | GrGeometryProcessor* geomProc = FwidthSquircleTestProcessor::Make(arena, fViewMatrix); |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 183 | |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 184 | // 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 Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void onPrepare(GrOpFlushState* flushState) final { |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 192 | SkPoint vertices[4] = { |
| 193 | {-1, -1}, |
| 194 | {+1, -1}, |
| 195 | {-1, +1}, |
| 196 | {+1, +1}, |
| 197 | }; |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 198 | fVertexBuffer = flushState->resourceProvider()->createBuffer( |
| 199 | sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices); |
| 200 | } |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 201 | |
| 202 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) final { |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 203 | if (!fVertexBuffer) { |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 204 | return; |
| 205 | } |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 206 | |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 207 | 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 Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 220 | GrMesh mesh(GrPrimitiveType::kTriangleStrip); |
| 221 | mesh.setNonIndexedNonInstanced(4); |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 222 | mesh.setVertexData(std::move(fVertexBuffer)); |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 223 | |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 224 | flushState->opsRenderPass()->draw(*fProgramInfo, &mesh, 1, |
| 225 | SkRect::MakeIWH(kWidth, kHeight)); |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 226 | |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Greg Daniel | 55e5be3 | 2019-09-30 12:23:26 -0400 | [diff] [blame] | 229 | static const int kWidth = 200; |
| 230 | static const int kHeight = 200; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 231 | |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 232 | sk_sp<GrBuffer> fVertexBuffer; |
| 233 | const SkMatrix fViewMatrix; |
| 234 | |
| 235 | // The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when |
Robert Phillips | 34cea00 | 2019-11-21 16:02:34 -0500 | [diff] [blame] | 236 | // 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 Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 240 | GrProgramInfo* fProgramInfo = nullptr; |
| 241 | |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 242 | friend class ::GrOpMemoryPool; // for ctor |
Robert Phillips | 5d07c52 | 2019-11-18 11:19:51 -0500 | [diff] [blame] | 243 | |
| 244 | typedef GrDrawOp INHERITED; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 248 | // Test. |
| 249 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 250 | 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] | 251 | if (!ctx->priv().caps()->shaderCaps()->shaderDerivativeSupport()) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 252 | *errorMsg = "Shader derivatives not supported."; |
| 253 | return DrawResult::kSkip; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | // Draw the test directly to the frame buffer. |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 257 | canvas->clear(SK_ColorWHITE); |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 258 | rtc->priv().testingOnly_addDrawOp(FwidthSquircleTestOp::Make(ctx, canvas->getTotalMatrix())); |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 259 | return skiagm::DrawResult::kOk; |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 260 | } |
| 261 | |
Chris Dalton | 0921219 | 2018-11-13 15:07:24 -0500 | [diff] [blame] | 262 | } |