blob: bfa7909060f871f3365898f066cbcb0908898b3e [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"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040019#include "include/gpu/GrRecordingContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/gpu/GrTypes.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/private/GrTypesPriv.h"
22#include "include/private/SkColorData.h"
23#include "src/gpu/GrBuffer.h"
24#include "src/gpu/GrCaps.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "src/gpu/GrColorSpaceXform.h"
Adlai Hollera0693042020-10-14 11:23:11 -040026#include "src/gpu/GrDirectContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include "src/gpu/GrGeometryProcessor.h"
28#include "src/gpu/GrGpuBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrMemoryPool.h"
30#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040031#include "src/gpu/GrOpsRenderPass.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040032#include "src/gpu/GrPipeline.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include "src/gpu/GrProcessor.h"
34#include "src/gpu/GrProcessorSet.h"
Robert Phillips901aff02019-10-08 12:32:56 -040035#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "src/gpu/GrRecordingContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040037#include "src/gpu/GrResourceProvider.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040038#include "src/gpu/GrSamplerState.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040039#include "src/gpu/GrShaderCaps.h"
40#include "src/gpu/GrShaderVar.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050041#include "src/gpu/GrSurfaceDrawContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040042#include "src/gpu/GrSurfaceProxy.h"
43#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050044#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
45#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
46#include "src/gpu/glsl/GrGLSLVarying.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040047#include "src/gpu/ops/GrDrawOp.h"
48#include "src/gpu/ops/GrOp.h"
Robert Phillips34cea002019-11-21 16:02:34 -050049#include "tools/gpu/ProxyUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040050
51#include <memory>
52#include <utility>
53
54class GrAppliedClip;
55class GrGLSLProgramDataManager;
Chris Dalton49d14e92018-07-27 12:38:35 -060056
Hal Canaryfa3305a2019-07-18 12:36:54 -040057namespace {
Chris Dalton49d14e92018-07-27 12:38:35 -060058
Brian Osmand4c29702018-09-14 16:16:55 -040059static constexpr GrGeometryProcessor::Attribute gVertex =
Stephen White1e1ad8d2019-06-19 14:19:47 -040060 {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Chris Dalton49d14e92018-07-27 12:38:35 -060061
62/**
63 * This is a GPU-backend specific test. It ensures that SkSL properly identifies clockwise-winding
64 * triangles (sk_Clockwise), in terms of to Skia device space, in all backends and with all render
65 * target origins. We draw clockwise triangles green and counter-clockwise red.
66 */
Hal Canaryfa3305a2019-07-18 12:36:54 -040067class ClockwiseGM : public skiagm::GpuGM {
68 SkString onShortName() override { return SkString("clockwise"); }
69 SkISize onISize() override { return {300, 200}; }
Brian Salomoneebe7352020-12-09 16:37:04 -050070 void onDraw(GrRecordingContext*, GrSurfaceDrawContext*, SkCanvas*) override;
Chris Dalton49d14e92018-07-27 12:38:35 -060071};
72
73////////////////////////////////////////////////////////////////////////////////////////////////////
74// SkSL code.
75
76class ClockwiseTestProcessor : public GrGeometryProcessor {
77public:
Robert Phillips5d07c522019-11-18 11:19:51 -050078 static GrGeometryProcessor* Make(SkArenaAlloc* arena, bool readSkFragCoord) {
Mike Kleinf1241082020-12-14 15:59:09 -060079 return arena->make([&](void* ptr) {
80 return new (ptr) ClockwiseTestProcessor(readSkFragCoord);
81 });
Robert Phillipsdf70f152019-11-15 14:57:05 -050082 }
83
84 const char* name() const final { return "ClockwiseTestProcessor"; }
85
86 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
87 b->add32(fReadSkFragCoord);
88 }
89
Robert Phillipsf10535f2021-03-23 09:30:45 -040090 GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const final;
Robert Phillipsdf70f152019-11-15 14:57:05 -050091
Robert Phillips5d07c522019-11-18 11:19:51 -050092 bool readSkFragCoord() const { return fReadSkFragCoord; }
93
Robert Phillipsdf70f152019-11-15 14:57:05 -050094private:
Chris Dalton49d14e92018-07-27 12:38:35 -060095 ClockwiseTestProcessor(bool readSkFragCoord)
96 : GrGeometryProcessor(kClockwiseTestProcessor_ClassID)
97 , fReadSkFragCoord(readSkFragCoord) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -050098 this->setVertexAttributes(&gVertex, 1);
Chris Dalton49d14e92018-07-27 12:38:35 -060099 }
Chris Dalton49d14e92018-07-27 12:38:35 -0600100
Chris Dalton49d14e92018-07-27 12:38:35 -0600101 const bool fReadSkFragCoord;
102
John Stiles7571f9e2020-09-02 22:42:33 -0400103 using INHERITED = GrGeometryProcessor;
Chris Dalton49d14e92018-07-27 12:38:35 -0600104};
105
106class GLSLClockwiseTestProcessor : public GrGLSLGeometryProcessor {
Robert Phillips787fd9d2021-03-22 14:48:09 -0400107 void setData(const GrGLSLProgramDataManager&, const GrGeometryProcessor&) override {}
Chris Dalton49d14e92018-07-27 12:38:35 -0600108
109 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Robert Phillips787fd9d2021-03-22 14:48:09 -0400110 const ClockwiseTestProcessor& proc = args.fGeomProc.cast<ClockwiseTestProcessor>();
Chris Dalton49d14e92018-07-27 12:38:35 -0600111 args.fVaryingHandler->emitAttributes(proc);
Stephen White1e1ad8d2019-06-19 14:19:47 -0400112 gpArgs->fPositionVar.set(kFloat2_GrSLType, "position");
Chris Dalton49d14e92018-07-27 12:38:35 -0600113 args.fFragBuilder->codeAppendf(
John Stiles4d7ac492021-03-09 20:16:43 -0500114 "half4 %s = sk_Clockwise ? half4(0,1,0,1) : half4(1,0,0,1);",
115 args.fOutputColor);
Robert Phillips5d07c522019-11-18 11:19:51 -0500116 if (!proc.readSkFragCoord()) {
John Stiles4d7ac492021-03-09 20:16:43 -0500117 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
Chris Dalton49d14e92018-07-27 12:38:35 -0600118 } else {
119 // Verify layout(origin_upper_left) on gl_FragCoord does not affect gl_FrontFacing.
John Stiles4d7ac492021-03-09 20:16:43 -0500120 args.fFragBuilder->codeAppendf("half4 %s = half4(min(half(sk_FragCoord.y), 1));",
Chris Dalton49d14e92018-07-27 12:38:35 -0600121 args.fOutputCoverage);
122 }
123 }
124};
125
Robert Phillipsf10535f2021-03-23 09:30:45 -0400126GrGLSLGeometryProcessor* ClockwiseTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
Chris Dalton49d14e92018-07-27 12:38:35 -0600127 return new GLSLClockwiseTestProcessor;
128}
129
130////////////////////////////////////////////////////////////////////////////////////////////////////
131// Draw Op.
132
133class ClockwiseTestOp : public GrDrawOp {
134public:
135 DEFINE_OP_CLASS_ID
136
Herb Derbyc76d4092020-10-07 16:46:15 -0400137 static GrOp::Owner Make(GrRecordingContext* context,
138 bool readSkFragCoord, int y = 0) {
139 return GrOp::Make<ClockwiseTestOp>(context, readSkFragCoord, y);
Chris Dalton49d14e92018-07-27 12:38:35 -0600140 }
141
142private:
143 ClockwiseTestOp(bool readSkFragCoord, float y)
Robert Phillips5d07c522019-11-18 11:19:51 -0500144 : GrDrawOp(ClassID())
145 , fReadSkFragCoord(readSkFragCoord)
146 , fY(y) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400147 this->setBounds(SkRect::MakeXYWH(0, fY, 100, 100), HasAABloat::kNo, IsHairline::kNo);
Chris Dalton49d14e92018-07-27 12:38:35 -0600148 }
149
150 const char* name() const override { return "ClockwiseTestOp"; }
151 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600152 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
153 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700154 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton49d14e92018-07-27 12:38:35 -0600155 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500156
Robert Phillipsac6156c2020-02-28 16:02:40 -0500157 GrProgramInfo* createProgramInfo(const GrCaps* caps,
158 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500159 const GrSurfaceProxyView& writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500160 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -0400161 const GrXferProcessor::DstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500162 GrXferBarrierFlags renderPassXferBarriers,
163 GrLoadOp colorLoadOp) const {
Robert Phillipsac6156c2020-02-28 16:02:40 -0500164 GrGeometryProcessor* geomProc = ClockwiseTestProcessor::Make(arena, fReadSkFragCoord);
165
Brian Salomon8afde5f2020-04-01 16:22:00 -0400166 return sk_gpu_test::CreateProgramInfo(caps, arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500167 std::move(appliedClip), dstProxyView,
168 geomProc, SkBlendMode::kPlus,
Greg Danield358cbe2020-09-11 09:33:54 -0400169 GrPrimitiveType::kTriangleStrip,
Greg Daniel42dbca52020-11-20 10:22:43 -0500170 renderPassXferBarriers, colorLoadOp);
Robert Phillipsac6156c2020-02-28 16:02:40 -0500171 }
172
173 GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const {
174 return this->createProgramInfo(&flushState->caps(),
175 flushState->allocator(),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400176 flushState->writeView(),
Robert Phillipsac6156c2020-02-28 16:02:40 -0500177 flushState->detachAppliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400178 flushState->dstProxyView(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500179 flushState->renderPassBarriers(),
180 flushState->colorLoadOp());
Robert Phillipsac6156c2020-02-28 16:02:40 -0500181 }
182
Robert Phillipsdf70f152019-11-15 14:57:05 -0500183 void onPrePrepare(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -0500184 const GrSurfaceProxyView& writeView,
Robert Phillips34cea002019-11-21 16:02:34 -0500185 GrAppliedClip* clip,
Greg Danield358cbe2020-09-11 09:33:54 -0400186 const GrXferProcessor::DstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500187 GrXferBarrierFlags renderPassXferBarriers,
188 GrLoadOp colorLoadOp) final {
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500189 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500190
Robert Phillips34cea002019-11-21 16:02:34 -0500191 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400192 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500193
Brian Salomon8afde5f2020-04-01 16:22:00 -0400194 fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -0400195 std::move(appliedClip), dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500196 renderPassXferBarriers, colorLoadOp);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500197
Robert Phillipsac6156c2020-02-28 16:02:40 -0500198 context->priv().recordProgramInfo(fProgramInfo);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500199 }
200
Greg Danielf793de12019-09-05 13:23:23 -0400201 void onPrepare(GrOpFlushState* flushState) override {
Chris Dalton49d14e92018-07-27 12:38:35 -0600202 SkPoint vertices[4] = {
203 {100, fY},
204 {0, fY+100},
205 {0, fY},
206 {100, fY+100},
207 };
Greg Danielf793de12019-09-05 13:23:23 -0400208 fVertexBuffer = flushState->resourceProvider()->createBuffer(
209 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
210 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500211
Greg Danielf793de12019-09-05 13:23:23 -0400212 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
213 if (!fVertexBuffer) {
Chris Dalton79f99f72018-07-27 14:15:10 -0600214 return;
215 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500216
Robert Phillips34cea002019-11-21 16:02:34 -0500217 if (!fProgramInfo) {
Robert Phillipsac6156c2020-02-28 16:02:40 -0500218 fProgramInfo = this->createProgramInfo(flushState);
Robert Phillips34cea002019-11-21 16:02:34 -0500219 }
220
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600221 flushState->bindPipeline(*fProgramInfo, SkRect::MakeXYWH(0, fY, 100, 100));
Greg Daniel426274b2020-07-20 11:37:38 -0400222 flushState->bindBuffers(nullptr, nullptr, std::move(fVertexBuffer));
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600223 flushState->draw(4, 0);
Chris Dalton49d14e92018-07-27 12:38:35 -0600224 }
225
Robert Phillips5d07c522019-11-18 11:19:51 -0500226 sk_sp<GrBuffer> fVertexBuffer;
227 const bool fReadSkFragCoord;
228 const float fY;
Robert Phillipsdf70f152019-11-15 14:57:05 -0500229
Robert Phillips787fd9d2021-03-22 14:48:09 -0400230 // The program info (and both the GrPipeline and GrGeometryProcessor it relies on), when
Robert Phillips34cea002019-11-21 16:02:34 -0500231 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
232 // arena's job to free up their memory so we just have a bare programInfo pointer here. We
Robert Phillips787fd9d2021-03-22 14:48:09 -0400233 // don't even store the GrPipeline and GrGeometryProcessor pointers here bc they are
Robert Phillips34cea002019-11-21 16:02:34 -0500234 // guaranteed to have the same lifetime as the program info.
Robert Phillips5d07c522019-11-18 11:19:51 -0500235 GrProgramInfo* fProgramInfo = nullptr;
Chris Dalton49d14e92018-07-27 12:38:35 -0600236
Herb Derbyc76d4092020-10-07 16:46:15 -0400237 friend class ::GrOp; // for ctor
Robert Phillips5d07c522019-11-18 11:19:51 -0500238
John Stiles7571f9e2020-09-02 22:42:33 -0400239 using INHERITED = GrDrawOp;
Chris Dalton49d14e92018-07-27 12:38:35 -0600240};
241
242////////////////////////////////////////////////////////////////////////////////////////////////////
243// Test.
244
Brian Salomoneebe7352020-12-09 16:37:04 -0500245void ClockwiseGM::onDraw(GrRecordingContext* ctx, GrSurfaceDrawContext* rtc, SkCanvas* canvas) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400246 rtc->clear(SK_PMColor4fBLACK);
Chris Dalton49d14e92018-07-27 12:38:35 -0600247
248 // Draw the test directly to the frame buffer.
Brian Salomon70fe17e2020-11-30 14:33:58 -0500249 rtc->addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
250 rtc->addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Chris Dalton49d14e92018-07-27 12:38:35 -0600251
252 // Draw the test to an off-screen, top-down render target.
Greg Danielc594e622019-10-15 14:01:49 -0400253 GrColorType rtcColorType = rtc->colorInfo().colorType();
Brian Salomoneebe7352020-12-09 16:37:04 -0500254 if (auto topLeftRTC = GrSurfaceDrawContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -0500255 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400256 GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin, SkBudgeted::kYes,
Greg Daniele20fcad2020-01-08 11:52:34 -0500257 nullptr)) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400258 topLeftRTC->clear(SK_PMColor4fTRANSPARENT);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500259 topLeftRTC->addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
260 topLeftRTC->addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Brian Salomone69b9ef2020-07-22 11:18:06 -0400261 rtc->drawTexture(nullptr,
262 topLeftRTC->readSurfaceView(),
263 rtc->colorInfo().alphaType(),
264 GrSamplerState::Filter::kNearest,
265 GrSamplerState::MipmapMode::kNone,
266 SkBlendMode::kSrcOver,
267 SK_PMColor4fWHITE,
268 {0, 0, 100, 200},
269 {100, 0, 200, 200},
270 GrAA::kNo,
271 GrQuadAAFlags::kNone,
272 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint,
273 SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500274 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600275 }
276
277 // Draw the test to an off-screen, bottom-up render target.
Brian Salomoneebe7352020-12-09 16:37:04 -0500278 if (auto topLeftRTC = GrSurfaceDrawContext::Make(
Greg Daniele20fcad2020-01-08 11:52:34 -0500279 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, 1,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400280 GrMipmapped::kNo, GrProtected::kNo, kBottomLeft_GrSurfaceOrigin, SkBudgeted::kYes,
Greg Daniele20fcad2020-01-08 11:52:34 -0500281 nullptr)) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400282 topLeftRTC->clear(SK_PMColor4fTRANSPARENT);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500283 topLeftRTC->addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
284 topLeftRTC->addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Brian Salomone69b9ef2020-07-22 11:18:06 -0400285 rtc->drawTexture(nullptr,
286 topLeftRTC->readSurfaceView(),
287 rtc->colorInfo().alphaType(),
288 GrSamplerState::Filter::kNearest,
289 GrSamplerState::MipmapMode::kNone,
290 SkBlendMode::kSrcOver,
291 SK_PMColor4fWHITE,
292 {0, 0, 100, 200},
293 {200, 0, 300, 200},
294 GrAA::kNo,
295 GrQuadAAFlags::kNone,
296 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint,
297 SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500298 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600299 }
300}
301
302////////////////////////////////////////////////////////////////////////////////////////////////////
303
304DEF_GM( return new ClockwiseGM(); )
305
John Stilesa6841be2020-08-06 14:11:56 -0400306} // namespace