blob: 0958d202fe0c3e0cb71a96c78ceea8847861df1e [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 {
Brian Salomon5a328282021-04-14 10:32:25 -0400107 void setData(const GrGLSLProgramDataManager&,
108 const GrShaderCaps&,
109 const GrGeometryProcessor&) override {}
Chris Dalton49d14e92018-07-27 12:38:35 -0600110
111 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
Robert Phillips787fd9d2021-03-22 14:48:09 -0400112 const ClockwiseTestProcessor& proc = args.fGeomProc.cast<ClockwiseTestProcessor>();
Chris Dalton49d14e92018-07-27 12:38:35 -0600113 args.fVaryingHandler->emitAttributes(proc);
Stephen White1e1ad8d2019-06-19 14:19:47 -0400114 gpArgs->fPositionVar.set(kFloat2_GrSLType, "position");
Chris Dalton49d14e92018-07-27 12:38:35 -0600115 args.fFragBuilder->codeAppendf(
John Stiles4d7ac492021-03-09 20:16:43 -0500116 "half4 %s = sk_Clockwise ? half4(0,1,0,1) : half4(1,0,0,1);",
117 args.fOutputColor);
Robert Phillips5d07c522019-11-18 11:19:51 -0500118 if (!proc.readSkFragCoord()) {
John Stiles4d7ac492021-03-09 20:16:43 -0500119 args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", args.fOutputCoverage);
Chris Dalton49d14e92018-07-27 12:38:35 -0600120 } else {
121 // Verify layout(origin_upper_left) on gl_FragCoord does not affect gl_FrontFacing.
John Stiles4d7ac492021-03-09 20:16:43 -0500122 args.fFragBuilder->codeAppendf("half4 %s = half4(min(half(sk_FragCoord.y), 1));",
Chris Dalton49d14e92018-07-27 12:38:35 -0600123 args.fOutputCoverage);
124 }
125 }
126};
127
Robert Phillipsf10535f2021-03-23 09:30:45 -0400128GrGLSLGeometryProcessor* ClockwiseTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
Chris Dalton49d14e92018-07-27 12:38:35 -0600129 return new GLSLClockwiseTestProcessor;
130}
131
132////////////////////////////////////////////////////////////////////////////////////////////////////
133// Draw Op.
134
135class ClockwiseTestOp : public GrDrawOp {
136public:
137 DEFINE_OP_CLASS_ID
138
Herb Derbyc76d4092020-10-07 16:46:15 -0400139 static GrOp::Owner Make(GrRecordingContext* context,
140 bool readSkFragCoord, int y = 0) {
141 return GrOp::Make<ClockwiseTestOp>(context, readSkFragCoord, y);
Chris Dalton49d14e92018-07-27 12:38:35 -0600142 }
143
144private:
145 ClockwiseTestOp(bool readSkFragCoord, float y)
Robert Phillips5d07c522019-11-18 11:19:51 -0500146 : GrDrawOp(ClassID())
147 , fReadSkFragCoord(readSkFragCoord)
148 , fY(y) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400149 this->setBounds(SkRect::MakeXYWH(0, fY, 100, 100), HasAABloat::kNo, IsHairline::kNo);
Chris Dalton49d14e92018-07-27 12:38:35 -0600150 }
151
152 const char* name() const override { return "ClockwiseTestOp"; }
153 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton57ab06c2021-04-22 12:57:28 -0600154 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700155 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton49d14e92018-07-27 12:38:35 -0600156 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500157
Robert Phillipsac6156c2020-02-28 16:02:40 -0500158 GrProgramInfo* createProgramInfo(const GrCaps* caps,
159 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500160 const GrSurfaceProxyView& writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500161 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -0400162 const GrXferProcessor::DstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500163 GrXferBarrierFlags renderPassXferBarriers,
164 GrLoadOp colorLoadOp) const {
Robert Phillipsac6156c2020-02-28 16:02:40 -0500165 GrGeometryProcessor* geomProc = ClockwiseTestProcessor::Make(arena, fReadSkFragCoord);
166
Brian Salomon8afde5f2020-04-01 16:22:00 -0400167 return sk_gpu_test::CreateProgramInfo(caps, arena, writeView,
Robert Phillipsac6156c2020-02-28 16:02:40 -0500168 std::move(appliedClip), dstProxyView,
169 geomProc, SkBlendMode::kPlus,
Greg Danield358cbe2020-09-11 09:33:54 -0400170 GrPrimitiveType::kTriangleStrip,
Greg Daniel42dbca52020-11-20 10:22:43 -0500171 renderPassXferBarriers, colorLoadOp);
Robert Phillipsac6156c2020-02-28 16:02:40 -0500172 }
173
174 GrProgramInfo* createProgramInfo(GrOpFlushState* flushState) const {
175 return this->createProgramInfo(&flushState->caps(),
176 flushState->allocator(),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400177 flushState->writeView(),
Robert Phillipsac6156c2020-02-28 16:02:40 -0500178 flushState->detachAppliedClip(),
Greg Danield358cbe2020-09-11 09:33:54 -0400179 flushState->dstProxyView(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500180 flushState->renderPassBarriers(),
181 flushState->colorLoadOp());
Robert Phillipsac6156c2020-02-28 16:02:40 -0500182 }
183
Robert Phillipsdf70f152019-11-15 14:57:05 -0500184 void onPrePrepare(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -0500185 const GrSurfaceProxyView& writeView,
Robert Phillips34cea002019-11-21 16:02:34 -0500186 GrAppliedClip* clip,
Greg Danield358cbe2020-09-11 09:33:54 -0400187 const GrXferProcessor::DstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500188 GrXferBarrierFlags renderPassXferBarriers,
189 GrLoadOp colorLoadOp) final {
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500190 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500191
Robert Phillips34cea002019-11-21 16:02:34 -0500192 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -0400193 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500194
Brian Salomon8afde5f2020-04-01 16:22:00 -0400195 fProgramInfo = this->createProgramInfo(context->priv().caps(), arena, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -0400196 std::move(appliedClip), dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500197 renderPassXferBarriers, colorLoadOp);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500198
Robert Phillipsac6156c2020-02-28 16:02:40 -0500199 context->priv().recordProgramInfo(fProgramInfo);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500200 }
201
Greg Danielf793de12019-09-05 13:23:23 -0400202 void onPrepare(GrOpFlushState* flushState) override {
Chris Dalton49d14e92018-07-27 12:38:35 -0600203 SkPoint vertices[4] = {
204 {100, fY},
205 {0, fY+100},
206 {0, fY},
207 {100, fY+100},
208 };
Greg Danielf793de12019-09-05 13:23:23 -0400209 fVertexBuffer = flushState->resourceProvider()->createBuffer(
210 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
211 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500212
Greg Danielf793de12019-09-05 13:23:23 -0400213 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
214 if (!fVertexBuffer) {
Chris Dalton79f99f72018-07-27 14:15:10 -0600215 return;
216 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500217
Robert Phillips34cea002019-11-21 16:02:34 -0500218 if (!fProgramInfo) {
Robert Phillipsac6156c2020-02-28 16:02:40 -0500219 fProgramInfo = this->createProgramInfo(flushState);
Robert Phillips34cea002019-11-21 16:02:34 -0500220 }
221
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600222 flushState->bindPipeline(*fProgramInfo, SkRect::MakeXYWH(0, fY, 100, 100));
Greg Daniel426274b2020-07-20 11:37:38 -0400223 flushState->bindBuffers(nullptr, nullptr, std::move(fVertexBuffer));
Chris Daltonaa0e45c2020-03-16 10:05:11 -0600224 flushState->draw(4, 0);
Chris Dalton49d14e92018-07-27 12:38:35 -0600225 }
226
Robert Phillips5d07c522019-11-18 11:19:51 -0500227 sk_sp<GrBuffer> fVertexBuffer;
228 const bool fReadSkFragCoord;
229 const float fY;
Robert Phillipsdf70f152019-11-15 14:57:05 -0500230
Robert Phillips787fd9d2021-03-22 14:48:09 -0400231 // The program info (and both the GrPipeline and GrGeometryProcessor it relies on), when
Robert Phillips34cea002019-11-21 16:02:34 -0500232 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
233 // 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 -0400234 // don't even store the GrPipeline and GrGeometryProcessor pointers here bc they are
Robert Phillips34cea002019-11-21 16:02:34 -0500235 // guaranteed to have the same lifetime as the program info.
Robert Phillips5d07c522019-11-18 11:19:51 -0500236 GrProgramInfo* fProgramInfo = nullptr;
Chris Dalton49d14e92018-07-27 12:38:35 -0600237
Herb Derbyc76d4092020-10-07 16:46:15 -0400238 friend class ::GrOp; // for ctor
Robert Phillips5d07c522019-11-18 11:19:51 -0500239
John Stiles7571f9e2020-09-02 22:42:33 -0400240 using INHERITED = GrDrawOp;
Chris Dalton49d14e92018-07-27 12:38:35 -0600241};
242
243////////////////////////////////////////////////////////////////////////////////////////////////////
244// Test.
245
Brian Salomoneebe7352020-12-09 16:37:04 -0500246void ClockwiseGM::onDraw(GrRecordingContext* ctx, GrSurfaceDrawContext* rtc, SkCanvas* canvas) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400247 rtc->clear(SK_PMColor4fBLACK);
Chris Dalton49d14e92018-07-27 12:38:35 -0600248
249 // Draw the test directly to the frame buffer.
Brian Salomon70fe17e2020-11-30 14:33:58 -0500250 rtc->addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
251 rtc->addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Chris Dalton49d14e92018-07-27 12:38:35 -0600252
253 // Draw the test to an off-screen, top-down render target.
Greg Danielc594e622019-10-15 14:01:49 -0400254 GrColorType rtcColorType = rtc->colorInfo().colorType();
Brian Salomoneebe7352020-12-09 16:37:04 -0500255 if (auto topLeftRTC = GrSurfaceDrawContext::Make(
Chris Daltonf5b87f92021-04-19 17:27:09 -0600256 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, SkSurfaceProps(),
257 1, GrMipmapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin,
258 SkBudgeted::kYes)) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400259 topLeftRTC->clear(SK_PMColor4fTRANSPARENT);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500260 topLeftRTC->addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
261 topLeftRTC->addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Brian Salomone69b9ef2020-07-22 11:18:06 -0400262 rtc->drawTexture(nullptr,
263 topLeftRTC->readSurfaceView(),
264 rtc->colorInfo().alphaType(),
265 GrSamplerState::Filter::kNearest,
266 GrSamplerState::MipmapMode::kNone,
267 SkBlendMode::kSrcOver,
268 SK_PMColor4fWHITE,
269 {0, 0, 100, 200},
270 {100, 0, 200, 200},
271 GrAA::kNo,
272 GrQuadAAFlags::kNone,
273 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint,
274 SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500275 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600276 }
277
278 // Draw the test to an off-screen, bottom-up render target.
Brian Salomoneebe7352020-12-09 16:37:04 -0500279 if (auto topLeftRTC = GrSurfaceDrawContext::Make(
Chris Daltonf5b87f92021-04-19 17:27:09 -0600280 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, SkSurfaceProps(),
281 1, GrMipmapped::kNo, GrProtected::kNo, kBottomLeft_GrSurfaceOrigin,
282 SkBudgeted::kYes)) {
Michael Ludwig81d41722020-05-26 16:57:38 -0400283 topLeftRTC->clear(SK_PMColor4fTRANSPARENT);
Brian Salomon70fe17e2020-11-30 14:33:58 -0500284 topLeftRTC->addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
285 topLeftRTC->addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Brian Salomone69b9ef2020-07-22 11:18:06 -0400286 rtc->drawTexture(nullptr,
287 topLeftRTC->readSurfaceView(),
288 rtc->colorInfo().alphaType(),
289 GrSamplerState::Filter::kNearest,
290 GrSamplerState::MipmapMode::kNone,
291 SkBlendMode::kSrcOver,
292 SK_PMColor4fWHITE,
293 {0, 0, 100, 200},
294 {200, 0, 300, 200},
295 GrAA::kNo,
296 GrQuadAAFlags::kNone,
297 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint,
298 SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500299 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600300 }
301}
302
303////////////////////////////////////////////////////////////////////////////////////////////////////
304
305DEF_GM( return new ClockwiseGM(); )
306
John Stilesa6841be2020-08-06 14:11:56 -0400307} // namespace