Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [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/SkColorSpace.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/SkSize.h" |
| 17 | #include "include/core/SkString.h" |
| 18 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "include/gpu/GrContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 20 | #include "include/gpu/GrSamplerState.h" |
| 21 | #include "include/gpu/GrTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/private/GrRecordingContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 23 | #include "include/private/GrSurfaceProxy.h" |
| 24 | #include "include/private/GrTextureProxy.h" |
| 25 | #include "include/private/GrTypesPriv.h" |
| 26 | #include "include/private/SkColorData.h" |
| 27 | #include "src/gpu/GrBuffer.h" |
| 28 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/gpu/GrClip.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 30 | #include "src/gpu/GrColorSpaceXform.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 31 | #include "src/gpu/GrContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 32 | #include "src/gpu/GrGeometryProcessor.h" |
| 33 | #include "src/gpu/GrGpuBuffer.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 34 | #include "src/gpu/GrGpuCommandBuffer.h" |
| 35 | #include "src/gpu/GrMemoryPool.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 36 | #include "src/gpu/GrMesh.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 37 | #include "src/gpu/GrOpFlushState.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 38 | #include "src/gpu/GrPipeline.h" |
| 39 | #include "src/gpu/GrPrimitiveProcessor.h" |
| 40 | #include "src/gpu/GrProcessor.h" |
| 41 | #include "src/gpu/GrProcessorSet.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 42 | #include "src/gpu/GrRecordingContextPriv.h" |
| 43 | #include "src/gpu/GrRenderTargetContext.h" |
| 44 | #include "src/gpu/GrRenderTargetContextPriv.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 45 | #include "src/gpu/GrResourceProvider.h" |
| 46 | #include "src/gpu/GrShaderCaps.h" |
| 47 | #include "src/gpu/GrShaderVar.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 48 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 49 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 50 | #include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 51 | #include "src/gpu/glsl/GrGLSLVarying.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 52 | #include "src/gpu/ops/GrDrawOp.h" |
| 53 | #include "src/gpu/ops/GrOp.h" |
| 54 | |
| 55 | #include <memory> |
| 56 | #include <utility> |
| 57 | |
| 58 | class GrAppliedClip; |
| 59 | class GrGLSLProgramDataManager; |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 60 | |
| 61 | namespace skiagm { |
| 62 | |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 63 | static constexpr GrGeometryProcessor::Attribute gVertex = |
| 64 | {"vertex", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * This is a GPU-backend specific test. It ensures that SkSL properly identifies clockwise-winding |
| 68 | * triangles (sk_Clockwise), in terms of to Skia device space, in all backends and with all render |
| 69 | * target origins. We draw clockwise triangles green and counter-clockwise red. |
| 70 | */ |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 71 | class ClockwiseGM : public GpuGM { |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 72 | private: |
| 73 | SkString onShortName() final { return SkString("clockwise"); } |
| 74 | SkISize onISize() override { return SkISize::Make(300, 200); } |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 75 | void onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*) override; |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 79 | // SkSL code. |
| 80 | |
| 81 | class ClockwiseTestProcessor : public GrGeometryProcessor { |
| 82 | public: |
| 83 | ClockwiseTestProcessor(bool readSkFragCoord) |
| 84 | : GrGeometryProcessor(kClockwiseTestProcessor_ClassID) |
| 85 | , fReadSkFragCoord(readSkFragCoord) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 86 | this->setVertexAttributes(&gVertex, 1); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 87 | } |
| 88 | const char* name() const override { return "ClockwiseTestProcessor"; } |
| 89 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final { |
| 90 | b->add32(fReadSkFragCoord); |
| 91 | } |
| 92 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 93 | |
| 94 | private: |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 95 | const bool fReadSkFragCoord; |
| 96 | |
| 97 | friend class GLSLClockwiseTestProcessor; |
| 98 | }; |
| 99 | |
| 100 | class GLSLClockwiseTestProcessor : public GrGLSLGeometryProcessor { |
| 101 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&, |
| 102 | FPCoordTransformIter&& transformIter) override {} |
| 103 | |
| 104 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 105 | const ClockwiseTestProcessor& proc = args.fGP.cast<ClockwiseTestProcessor>(); |
| 106 | args.fVaryingHandler->emitAttributes(proc); |
| 107 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex"); |
| 108 | args.fFragBuilder->codeAppendf( |
| 109 | "%s = sk_Clockwise ? half4(0,1,0,1) : half4(1,0,0,1);", args.fOutputColor); |
| 110 | if (!proc.fReadSkFragCoord) { |
| 111 | args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
| 112 | } else { |
| 113 | // Verify layout(origin_upper_left) on gl_FragCoord does not affect gl_FrontFacing. |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 114 | args.fFragBuilder->codeAppendf("%s = half4(min(half(sk_FragCoord.y), 1));", |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 115 | args.fOutputCoverage); |
| 116 | } |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | GrGLSLPrimitiveProcessor* ClockwiseTestProcessor::createGLSLInstance( |
| 121 | const GrShaderCaps&) const { |
| 122 | return new GLSLClockwiseTestProcessor; |
| 123 | } |
| 124 | |
| 125 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 126 | // Draw Op. |
| 127 | |
| 128 | class ClockwiseTestOp : public GrDrawOp { |
| 129 | public: |
| 130 | DEFINE_OP_CLASS_ID |
| 131 | |
Robert Phillips | be9aff2 | 2019-02-15 11:33:22 -0500 | [diff] [blame] | 132 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
| 133 | bool readSkFragCoord, int y = 0) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 134 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 135 | return pool->allocate<ClockwiseTestOp>(readSkFragCoord, y); |
| 136 | } |
| 137 | |
| 138 | private: |
| 139 | ClockwiseTestOp(bool readSkFragCoord, float y) |
| 140 | : GrDrawOp(ClassID()), fReadSkFragCoord(readSkFragCoord), fY(y) { |
| 141 | this->setBounds(SkRect::MakeIWH(300, 100), HasAABloat::kNo, IsZeroArea::kNo); |
| 142 | } |
| 143 | |
| 144 | const char* name() const override { return "ClockwiseTestOp"; } |
| 145 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 146 | GrProcessorSet::Analysis finalize( |
| 147 | const GrCaps&, const GrAppliedClip*, GrFSAAType, GrClampType) override { |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 148 | return GrProcessorSet::EmptySetAnalysis(); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 149 | } |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 150 | void onPrepare(GrOpFlushState*) override {} |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 151 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 152 | SkPoint vertices[4] = { |
| 153 | {100, fY}, |
| 154 | {0, fY+100}, |
| 155 | {0, fY}, |
| 156 | {100, fY+100}, |
| 157 | }; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 158 | sk_sp<const GrBuffer> vertexBuffer(flushState->resourceProvider()->createBuffer( |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 159 | sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices)); |
Chris Dalton | 79f99f7 | 2018-07-27 14:15:10 -0600 | [diff] [blame] | 160 | if (!vertexBuffer) { |
| 161 | return; |
| 162 | } |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 163 | GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kPlus); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 164 | GrMesh mesh(GrPrimitiveType::kTriangleStrip); |
| 165 | mesh.setNonIndexedNonInstanced(4); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 166 | mesh.setVertexData(std::move(vertexBuffer)); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 167 | flushState->rtCommandBuffer()->draw(ClockwiseTestProcessor(fReadSkFragCoord), pipeline, |
| 168 | nullptr, nullptr, &mesh, 1, SkRect::MakeIWH(100, 100)); |
| 169 | } |
| 170 | |
| 171 | const bool fReadSkFragCoord; |
| 172 | const float fY; |
| 173 | |
| 174 | friend class ::GrOpMemoryPool; // for ctor |
| 175 | }; |
| 176 | |
| 177 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 178 | // Test. |
| 179 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 180 | void ClockwiseGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas) { |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 181 | rtc->clear(nullptr, { 0, 0, 0, 1 }, GrRenderTargetContext::CanClearFullscreen::kYes); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 182 | |
| 183 | // Draw the test directly to the frame buffer. |
| 184 | rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0)); |
| 185 | rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100)); |
| 186 | |
| 187 | // Draw the test to an off-screen, top-down render target. |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 188 | if (auto topLeftRTC = ctx->priv().makeDeferredRenderTargetContext( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 189 | rtc->asSurfaceProxy()->backendFormat(), SkBackingFit::kExact, 100, 200, |
| 190 | rtc->asSurfaceProxy()->config(), nullptr, 1, GrMipMapped::kNo, |
| 191 | kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes)) { |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 192 | topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT, |
| 193 | GrRenderTargetContext::CanClearFullscreen::kYes); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 194 | topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0)); |
| 195 | topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100)); |
| 196 | rtc->drawTexture(GrNoClip(), sk_ref_sp(topLeftRTC->asTextureProxy()), |
Michael Ludwig | d54ca8f | 2019-02-13 13:25:21 -0500 | [diff] [blame] | 197 | GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver, |
| 198 | SK_PMColor4fWHITE, {0, 0, 100, 200}, |
Michael Ludwig | 136f45a | 2019-02-19 11:44:41 -0500 | [diff] [blame] | 199 | {100, 0, 200, 200}, GrAA::kNo, GrQuadAAFlags::kNone, |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 200 | SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(), |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 201 | nullptr); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | // Draw the test to an off-screen, bottom-up render target. |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 205 | if (auto topLeftRTC = ctx->priv().makeDeferredRenderTargetContext( |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 206 | rtc->asSurfaceProxy()->backendFormat(), SkBackingFit::kExact, 100, 200, |
| 207 | rtc->asSurfaceProxy()->config(), nullptr, 1, GrMipMapped::kNo, |
| 208 | kBottomLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes)) { |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 209 | topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT, |
| 210 | GrRenderTargetContext::CanClearFullscreen::kYes); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 211 | topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0)); |
| 212 | topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100)); |
| 213 | rtc->drawTexture(GrNoClip(), sk_ref_sp(topLeftRTC->asTextureProxy()), |
Michael Ludwig | d54ca8f | 2019-02-13 13:25:21 -0500 | [diff] [blame] | 214 | GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver, |
| 215 | SK_PMColor4fWHITE, {0, 0, 100, 200}, |
Michael Ludwig | 136f45a | 2019-02-19 11:44:41 -0500 | [diff] [blame] | 216 | {200, 0, 300, 200}, GrAA::kNo, GrQuadAAFlags::kNone, |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 217 | SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(), |
Brian Osman | 3d139a4 | 2018-11-19 10:42:10 -0500 | [diff] [blame] | 218 | nullptr); |
Chris Dalton | 49d14e9 | 2018-07-27 12:38:35 -0600 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 223 | |
| 224 | DEF_GM( return new ClockwiseGM(); ) |
| 225 | |
| 226 | } |