blob: be1551756cd3cf69c26187f65797c1712094821b [file] [log] [blame]
Chris Dalton49d14e92018-07-27 12:38:35 -06001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#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 Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/gpu/GrContext.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040020#include "include/gpu/GrRecordingContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/gpu/GrTypes.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040022#include "include/private/GrTypesPriv.h"
23#include "include/private/SkColorData.h"
24#include "src/gpu/GrBuffer.h"
25#include "src/gpu/GrCaps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040026#include "src/gpu/GrColorSpaceXform.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/GrContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040028#include "src/gpu/GrGeometryProcessor.h"
29#include "src/gpu/GrGpuBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrMemoryPool.h"
31#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040032#include "src/gpu/GrOpsRenderPass.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include "src/gpu/GrPipeline.h"
34#include "src/gpu/GrPrimitiveProcessor.h"
35#include "src/gpu/GrProcessor.h"
36#include "src/gpu/GrProcessorSet.h"
Robert Phillips901aff02019-10-08 12:32:56 -040037#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050038#include "src/gpu/GrRecordingContextPriv.h"
39#include "src/gpu/GrRenderTargetContext.h"
40#include "src/gpu/GrRenderTargetContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040041#include "src/gpu/GrResourceProvider.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040042#include "src/gpu/GrSamplerState.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040043#include "src/gpu/GrShaderCaps.h"
44#include "src/gpu/GrShaderVar.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040045#include "src/gpu/GrSurfaceProxy.h"
46#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050047#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
48#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040049#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050050#include "src/gpu/glsl/GrGLSLVarying.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040051#include "src/gpu/ops/GrDrawOp.h"
52#include "src/gpu/ops/GrOp.h"
Robert Phillips34cea002019-11-21 16:02:34 -050053#include "tools/gpu/ProxyUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040054
55#include <memory>
56#include <utility>
57
58class GrAppliedClip;
59class GrGLSLProgramDataManager;
Chris Dalton49d14e92018-07-27 12:38:35 -060060
Hal Canaryfa3305a2019-07-18 12:36:54 -040061namespace {
Chris Dalton49d14e92018-07-27 12:38:35 -060062
Brian Osmand4c29702018-09-14 16:16:55 -040063static constexpr GrGeometryProcessor::Attribute gVertex =
Stephen White1e1ad8d2019-06-19 14:19:47 -040064 {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Chris Dalton49d14e92018-07-27 12:38:35 -060065
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 */
Hal Canaryfa3305a2019-07-18 12:36:54 -040071class ClockwiseGM : public skiagm::GpuGM {
72 SkString onShortName() override { return SkString("clockwise"); }
73 SkISize onISize() override { return {300, 200}; }
Robert Phillips95c250c2020-06-29 15:36:12 -040074 void onDraw(GrRecordingContext*, GrRenderTargetContext*, SkCanvas*) override;
Chris Dalton49d14e92018-07-27 12:38:35 -060075};
76
77////////////////////////////////////////////////////////////////////////////////////////////////////
78// SkSL code.
79
80class ClockwiseTestProcessor : public GrGeometryProcessor {
81public:
Robert Phillips5d07c522019-11-18 11:19:51 -050082 static GrGeometryProcessor* Make(SkArenaAlloc* arena, bool readSkFragCoord) {
83 return arena->make<ClockwiseTestProcessor>(readSkFragCoord);
Robert Phillipsdf70f152019-11-15 14:57:05 -050084 }
85
86 const char* name() const final { return "ClockwiseTestProcessor"; }
87
88 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
89 b->add32(fReadSkFragCoord);
90 }
91
92 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
93
Robert Phillips5d07c522019-11-18 11:19:51 -050094 bool readSkFragCoord() const { return fReadSkFragCoord; }
95
Robert Phillipsdf70f152019-11-15 14:57:05 -050096private:
Robert Phillips5d07c522019-11-18 11:19:51 -050097 friend class ::SkArenaAlloc; // for access to ctor
98
Chris Dalton49d14e92018-07-27 12:38:35 -060099 ClockwiseTestProcessor(bool readSkFragCoord)
100 : GrGeometryProcessor(kClockwiseTestProcessor_ClassID)
101 , fReadSkFragCoord(readSkFragCoord) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500102 this->setVertexAttributes(&gVertex, 1);
Chris Dalton49d14e92018-07-27 12:38:35 -0600103 }
Chris Dalton49d14e92018-07-27 12:38:35 -0600104
Chris Dalton49d14e92018-07-27 12:38:35 -0600105 const bool fReadSkFragCoord;
106
Robert Phillips5d07c522019-11-18 11:19:51 -0500107 typedef GrGeometryProcessor INHERITED;
Chris Dalton49d14e92018-07-27 12:38:35 -0600108};
109
110class GLSLClockwiseTestProcessor : public GrGLSLGeometryProcessor {
Brian Osman609f1592020-07-01 15:14:39 -0400111 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&) override {}
Chris Dalton49d14e92018-07-27 12:38:35 -0600112
113 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
114 const ClockwiseTestProcessor& proc = args.fGP.cast<ClockwiseTestProcessor>();
115 args.fVaryingHandler->emitAttributes(proc);
Stephen White1e1ad8d2019-06-19 14:19:47 -0400116 gpArgs->fPositionVar.set(kFloat2_GrSLType, "position");
Chris Dalton49d14e92018-07-27 12:38:35 -0600117 args.fFragBuilder->codeAppendf(
118 "%s = sk_Clockwise ? half4(0,1,0,1) : half4(1,0,0,1);", args.fOutputColor);
Robert Phillips5d07c522019-11-18 11:19:51 -0500119 if (!proc.readSkFragCoord()) {
Chris Dalton49d14e92018-07-27 12:38:35 -0600120 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
121 } else {
122 // Verify layout(origin_upper_left) on gl_FragCoord does not affect gl_FrontFacing.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500123 args.fFragBuilder->codeAppendf("%s = half4(min(half(sk_FragCoord.y), 1));",
Chris Dalton49d14e92018-07-27 12:38:35 -0600124 args.fOutputCoverage);
125 }
126 }
127};
128
129GrGLSLPrimitiveProcessor* ClockwiseTestProcessor::createGLSLInstance(
130 const GrShaderCaps&) const {
131 return new GLSLClockwiseTestProcessor;
132}
133
134////////////////////////////////////////////////////////////////////////////////////////////////////
135// Draw Op.
136
137class ClockwiseTestOp : public GrDrawOp {
138public:
139 DEFINE_OP_CLASS_ID
140
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500141 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
142 bool readSkFragCoord, int y = 0) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500143 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Chris Dalton49d14e92018-07-27 12:38:35 -0600144 return pool->allocate<ClockwiseTestOp>(readSkFragCoord, y);
145 }
146
147private:
148 ClockwiseTestOp(bool readSkFragCoord, float y)
Robert Phillips5d07c522019-11-18 11:19:51 -0500149 : GrDrawOp(ClassID())
150 , fReadSkFragCoord(readSkFragCoord)
151 , fY(y) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400152 this->setBounds(SkRect::MakeXYWH(0, fY, 100, 100), HasAABloat::kNo, IsHairline::kNo);
Chris Dalton49d14e92018-07-27 12:38:35 -0600153 }
154
155 const char* name() const override { return "ClockwiseTestOp"; }
156 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600157 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
158 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700159 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton49d14e92018-07-27 12:38:35 -0600160 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500161
Robert Phillipsac6156c2020-02-28 16:02:40 -0500162 GrProgramInfo* createProgramInfo(const GrCaps* caps,
163 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400164 const GrSurfaceProxyView* writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500165 GrAppliedClip&& appliedClip,
166 const GrXferProcessor::DstProxyView& dstProxyView) const {
167 GrGeometryProcessor* geomProc = ClockwiseTestProcessor::Make(arena, fReadSkFragCoord);
168
Brian Salomon8afde5f2020-04-01 16:22:00 -0400169 return sk_gpu_test::CreateProgramInfo(caps, arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500170 std::move(appliedClip), dstProxyView,
171 geomProc, SkBlendMode::kPlus,
172 GrPrimitiveType::kTriangleStrip);
173 }
174
175 GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const {
176 return this->createProgramInfo(&flushState->caps(),
177 flushState->allocator(),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400178 flushState->writeView(),
Robert Phillipsac6156c2020-02-28 16:02:40 -0500179 flushState->detachAppliedClip(),
180 flushState->dstProxyView());
181 }
182
Robert Phillipsdf70f152019-11-15 14:57:05 -0500183 void onPrePrepare(GrRecordingContext* context,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400184 const GrSurfaceProxyView* writeView,
Robert Phillips34cea002019-11-21 16:02:34 -0500185 GrAppliedClip* clip,
Robert Phillips8053c972019-11-21 10:44:53 -0500186 const GrXferProcessor::DstProxyView& dstProxyView) final {
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500187 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500188
Robert Phillips34cea002019-11-21 16:02:34 -0500189 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400190 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500191
Brian Salomon8afde5f2020-04-01 16:22:00 -0400192 fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500193 std::move(appliedClip), dstProxyView);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500194
Robert Phillipsac6156c2020-02-28 16:02:40 -0500195 context->priv().recordProgramInfo(fProgramInfo);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500196 }
197
Greg Danielf793de12019-09-05 13:23:23 -0400198 void onPrepare(GrOpFlushState* flushState) override {
Chris Dalton49d14e92018-07-27 12:38:35 -0600199 SkPoint vertices[4] = {
200 {100, fY},
201 {0, fY+100},
202 {0, fY},
203 {100, fY+100},
204 };
Greg Danielf793de12019-09-05 13:23:23 -0400205 fVertexBuffer = flushState->resourceProvider()->createBuffer(
206 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
207 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500208
Greg Danielf793de12019-09-05 13:23:23 -0400209 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
210 if (!fVertexBuffer) {
Chris Dalton79f99f72018-07-27 14:15:10 -0600211 return;
212 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500213
Robert Phillips34cea002019-11-21 16:02:34 -0500214 if (!fProgramInfo) {
Robert Phillipsac6156c2020-02-28 16:02:40 -0500215 fProgramInfo = this->createProgramInfo(flushState);
Robert Phillips34cea002019-11-21 16:02:34 -0500216 }
217
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600218 flushState->bindPipeline(*fProgramInfo, SkRect::MakeXYWH(0, fY, 100, 100));
219 flushState->bindBuffers(nullptr, nullptr, fVertexBuffer.get());
220 flushState->draw(4, 0);
Chris Dalton49d14e92018-07-27 12:38:35 -0600221 }
222
Robert Phillips5d07c522019-11-18 11:19:51 -0500223 sk_sp<GrBuffer> fVertexBuffer;
224 const bool fReadSkFragCoord;
225 const float fY;
Robert Phillipsdf70f152019-11-15 14:57:05 -0500226
Robert Phillips5d07c522019-11-18 11:19:51 -0500227 // The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when
Robert Phillips34cea002019-11-21 16:02:34 -0500228 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
229 // arena's job to free up their memory so we just have a bare programInfo pointer here. We
230 // don't even store the GrPipeline and GrPrimitiveProcessor pointers here bc they are
231 // guaranteed to have the same lifetime as the program info.
Robert Phillips5d07c522019-11-18 11:19:51 -0500232 GrProgramInfo* fProgramInfo = nullptr;
Chris Dalton49d14e92018-07-27 12:38:35 -0600233
234 friend class ::GrOpMemoryPool; // for ctor
Robert Phillips5d07c522019-11-18 11:19:51 -0500235
236 typedef GrDrawOp INHERITED;
Chris Dalton49d14e92018-07-27 12:38:35 -0600237};
238
239////////////////////////////////////////////////////////////////////////////////////////////////////
240// Test.
241
Robert Phillips95c250c2020-06-29 15:36:12 -0400242void ClockwiseGM::onDraw(GrRecordingContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400243 rtc->clear(SK_PMColor4fBLACK);
Chris Dalton49d14e92018-07-27 12:38:35 -0600244
245 // Draw the test directly to the frame buffer.
246 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
247 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
248
249 // Draw the test to an off-screen, top-down render target.
Greg Danielc594e622019-10-15 14:01:49 -0400250 GrColorType rtcColorType = rtc->colorInfo().colorType();
Greg Daniele20fcad2020-01-08 11:52:34 -0500251 if (auto topLeftRTC = GrRenderTargetContext::Make(
252 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, 1,
253 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin, SkBudgeted::kYes,
254 nullptr)) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400255 topLeftRTC->clear(SK_PMColor4fTRANSPARENT);
Chris Dalton49d14e92018-07-27 12:38:35 -0600256 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
257 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400258 rtc->drawTexture(nullptr, topLeftRTC->readSurfaceView(), rtc->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -0500259 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver, SK_PMColor4fWHITE,
260 {0, 0, 100, 200}, {100, 0, 200, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
Chris Dalton49d14e92018-07-27 12:38:35 -0600261 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500262 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600263 }
264
265 // Draw the test to an off-screen, bottom-up render target.
Greg Daniele20fcad2020-01-08 11:52:34 -0500266 if (auto topLeftRTC = GrRenderTargetContext::Make(
267 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, 1,
268 GrMipMapped::kNo, GrProtected::kNo, kBottomLeft_GrSurfaceOrigin, SkBudgeted::kYes,
269 nullptr)) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400270 topLeftRTC->clear(SK_PMColor4fTRANSPARENT);
Chris Dalton49d14e92018-07-27 12:38:35 -0600271 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
272 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Michael Ludwig7c12e282020-05-29 09:54:07 -0400273 rtc->drawTexture(nullptr, topLeftRTC->readSurfaceView(), rtc->colorInfo().alphaType(),
Greg Daniel40903af2020-01-30 14:55:05 -0500274 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver, SK_PMColor4fWHITE,
275 {0, 0, 100, 200}, {200, 0, 300, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
Chris Dalton49d14e92018-07-27 12:38:35 -0600276 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500277 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600278 }
279}
280
281////////////////////////////////////////////////////////////////////////////////////////////////////
282
283DEF_GM( return new ClockwiseGM(); )
284
285}