blob: 6921b9b53e41f14e6d27bc3ee311e24cedc6ea7c [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"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/gpu/GrTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "include/private/GrRecordingContext.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrClip.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040027#include "src/gpu/GrColorSpaceXform.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/GrContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040029#include "src/gpu/GrGeometryProcessor.h"
30#include "src/gpu/GrGpuBuffer.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/GrMemoryPool.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040032#include "src/gpu/GrMesh.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050033#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040034#include "src/gpu/GrOpsRenderPass.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040035#include "src/gpu/GrPipeline.h"
36#include "src/gpu/GrPrimitiveProcessor.h"
37#include "src/gpu/GrProcessor.h"
38#include "src/gpu/GrProcessorSet.h"
Robert Phillips901aff02019-10-08 12:32:56 -040039#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050040#include "src/gpu/GrRecordingContextPriv.h"
41#include "src/gpu/GrRenderTargetContext.h"
42#include "src/gpu/GrRenderTargetContextPriv.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040043#include "src/gpu/GrResourceProvider.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040044#include "src/gpu/GrSamplerState.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040045#include "src/gpu/GrShaderCaps.h"
46#include "src/gpu/GrShaderVar.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040047#include "src/gpu/GrSurfaceProxy.h"
48#include "src/gpu/GrTextureProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050049#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
50#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040051#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050052#include "src/gpu/glsl/GrGLSLVarying.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040053#include "src/gpu/ops/GrDrawOp.h"
54#include "src/gpu/ops/GrOp.h"
55
56#include <memory>
57#include <utility>
58
59class GrAppliedClip;
60class GrGLSLProgramDataManager;
Chris Dalton49d14e92018-07-27 12:38:35 -060061
Hal Canaryfa3305a2019-07-18 12:36:54 -040062namespace {
Chris Dalton49d14e92018-07-27 12:38:35 -060063
Brian Osmand4c29702018-09-14 16:16:55 -040064static constexpr GrGeometryProcessor::Attribute gVertex =
Stephen White1e1ad8d2019-06-19 14:19:47 -040065 {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Chris Dalton49d14e92018-07-27 12:38:35 -060066
67/**
68 * This is a GPU-backend specific test. It ensures that SkSL properly identifies clockwise-winding
69 * triangles (sk_Clockwise), in terms of to Skia device space, in all backends and with all render
70 * target origins. We draw clockwise triangles green and counter-clockwise red.
71 */
Hal Canaryfa3305a2019-07-18 12:36:54 -040072class ClockwiseGM : public skiagm::GpuGM {
73 SkString onShortName() override { return SkString("clockwise"); }
74 SkISize onISize() override { return {300, 200}; }
Chris Dalton3a778372019-02-07 15:23:36 -070075 void onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*) override;
Chris Dalton49d14e92018-07-27 12:38:35 -060076};
77
78////////////////////////////////////////////////////////////////////////////////////////////////////
79// SkSL code.
80
81class ClockwiseTestProcessor : public GrGeometryProcessor {
82public:
Robert Phillips5d07c522019-11-18 11:19:51 -050083 static GrGeometryProcessor* Make(SkArenaAlloc* arena, bool readSkFragCoord) {
84 return arena->make<ClockwiseTestProcessor>(readSkFragCoord);
Robert Phillipsdf70f152019-11-15 14:57:05 -050085 }
86
87 const char* name() const final { return "ClockwiseTestProcessor"; }
88
89 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
90 b->add32(fReadSkFragCoord);
91 }
92
93 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
94
Robert Phillips5d07c522019-11-18 11:19:51 -050095 bool readSkFragCoord() const { return fReadSkFragCoord; }
96
Robert Phillipsdf70f152019-11-15 14:57:05 -050097private:
Robert Phillips5d07c522019-11-18 11:19:51 -050098 friend class ::SkArenaAlloc; // for access to ctor
99
Chris Dalton49d14e92018-07-27 12:38:35 -0600100 ClockwiseTestProcessor(bool readSkFragCoord)
101 : GrGeometryProcessor(kClockwiseTestProcessor_ClassID)
102 , fReadSkFragCoord(readSkFragCoord) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500103 this->setVertexAttributes(&gVertex, 1);
Chris Dalton49d14e92018-07-27 12:38:35 -0600104 }
Chris Dalton49d14e92018-07-27 12:38:35 -0600105
Chris Dalton49d14e92018-07-27 12:38:35 -0600106 const bool fReadSkFragCoord;
107
Robert Phillips5d07c522019-11-18 11:19:51 -0500108 typedef GrGeometryProcessor INHERITED;
Chris Dalton49d14e92018-07-27 12:38:35 -0600109};
110
111class GLSLClockwiseTestProcessor : public GrGLSLGeometryProcessor {
112 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
113 FPCoordTransformIter&& transformIter) override {}
114
115 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
116 const ClockwiseTestProcessor& proc = args.fGP.cast<ClockwiseTestProcessor>();
117 args.fVaryingHandler->emitAttributes(proc);
Stephen White1e1ad8d2019-06-19 14:19:47 -0400118 gpArgs->fPositionVar.set(kFloat2_GrSLType, "position");
Chris Dalton49d14e92018-07-27 12:38:35 -0600119 args.fFragBuilder->codeAppendf(
120 "%s = sk_Clockwise ? half4(0,1,0,1) : half4(1,0,0,1);", args.fOutputColor);
Robert Phillips5d07c522019-11-18 11:19:51 -0500121 if (!proc.readSkFragCoord()) {
Chris Dalton49d14e92018-07-27 12:38:35 -0600122 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
123 } else {
124 // Verify layout(origin_upper_left) on gl_FragCoord does not affect gl_FrontFacing.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500125 args.fFragBuilder->codeAppendf("%s = half4(min(half(sk_FragCoord.y), 1));",
Chris Dalton49d14e92018-07-27 12:38:35 -0600126 args.fOutputCoverage);
127 }
128 }
129};
130
131GrGLSLPrimitiveProcessor* ClockwiseTestProcessor::createGLSLInstance(
132 const GrShaderCaps&) const {
133 return new GLSLClockwiseTestProcessor;
134}
135
136////////////////////////////////////////////////////////////////////////////////////////////////////
137// Draw Op.
138
139class ClockwiseTestOp : public GrDrawOp {
140public:
141 DEFINE_OP_CLASS_ID
142
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500143 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
144 bool readSkFragCoord, int y = 0) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500145 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Chris Dalton49d14e92018-07-27 12:38:35 -0600146 return pool->allocate<ClockwiseTestOp>(readSkFragCoord, y);
147 }
148
149private:
150 ClockwiseTestOp(bool readSkFragCoord, float y)
Robert Phillips5d07c522019-11-18 11:19:51 -0500151 : GrDrawOp(ClassID())
152 , fReadSkFragCoord(readSkFragCoord)
153 , fY(y) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400154 this->setBounds(SkRect::MakeXYWH(0, fY, 100, 100), HasAABloat::kNo, IsHairline::kNo);
Chris Dalton49d14e92018-07-27 12:38:35 -0600155 }
156
157 const char* name() const override { return "ClockwiseTestOp"; }
158 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600159 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
160 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700161 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton49d14e92018-07-27 12:38:35 -0600162 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500163
164 void onPrePrepare(GrRecordingContext* context,
165 const GrSurfaceProxyView* dstView,
Robert Phillips8053c972019-11-21 10:44:53 -0500166 GrAppliedClip* appliedClip,
167 const GrXferProcessor::DstProxyView& dstProxyView) final {
Robert Phillips5d07c522019-11-18 11:19:51 -0500168 // We're going to create the GrProgramInfo (and the GrPipeline and geometry processor
169 // it relies on) in the DDL-record-time arena.
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500170 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500171
172 // Not POD! It has some sk_sp's buried inside it!
173 GrPipeline* pipeline = arena->make<GrPipeline>(GrScissorTest::kDisabled, SkBlendMode::kPlus,
174 dstView->swizzle());
175
Robert Phillips5d07c522019-11-18 11:19:51 -0500176 GrPrimitiveProcessor* geomProc = ClockwiseTestProcessor::Make(arena, fReadSkFragCoord);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500177
178 // The programInfo is POD
179 GrRenderTargetProxy* dstProxy = dstView->asRenderTargetProxy();
180 fProgramInfo = arena->make<GrProgramInfo>(dstProxy->numSamples(),
181 dstProxy->numStencilSamples(),
182 dstView->origin(),
Robert Phillips67a625e2019-11-15 15:37:07 -0500183 pipeline,
Robert Phillips5d07c522019-11-18 11:19:51 -0500184 geomProc,
Robert Phillipsdf70f152019-11-15 14:57:05 -0500185 nullptr, nullptr, 0,
186 GrPrimitiveType::kTriangleStrip);
187 }
188
Greg Danielf793de12019-09-05 13:23:23 -0400189 void onPrepare(GrOpFlushState* flushState) override {
Chris Dalton49d14e92018-07-27 12:38:35 -0600190 SkPoint vertices[4] = {
191 {100, fY},
192 {0, fY+100},
193 {0, fY},
194 {100, fY+100},
195 };
Greg Danielf793de12019-09-05 13:23:23 -0400196 fVertexBuffer = flushState->resourceProvider()->createBuffer(
197 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
198 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500199
Greg Danielf793de12019-09-05 13:23:23 -0400200 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
201 if (!fVertexBuffer) {
Chris Dalton79f99f72018-07-27 14:15:10 -0600202 return;
203 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500204
Chris Dalton49d14e92018-07-27 12:38:35 -0600205 GrMesh mesh(GrPrimitiveType::kTriangleStrip);
206 mesh.setNonIndexedNonInstanced(4);
Greg Danielf793de12019-09-05 13:23:23 -0400207 mesh.setVertexData(std::move(fVertexBuffer));
Robert Phillips901aff02019-10-08 12:32:56 -0400208
Robert Phillipsdf70f152019-11-15 14:57:05 -0500209 if (fProgramInfo) {
210 flushState->opsRenderPass()->draw(*fProgramInfo, &mesh, 1,
211 SkRect::MakeXYWH(0, fY, 100, 100));
212 } else {
213 const GrSurfaceProxyView* dstView = flushState->view();
Robert Phillips901aff02019-10-08 12:32:56 -0400214
Robert Phillipsdf70f152019-11-15 14:57:05 -0500215 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kPlus,
216 dstView->swizzle());
Robert Phillips901aff02019-10-08 12:32:56 -0400217
Robert Phillips5d07c522019-11-18 11:19:51 -0500218 GrGeometryProcessor* gp = ClockwiseTestProcessor::Make(flushState->allocator(),
219 fReadSkFragCoord);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500220
221 GrProgramInfo programInfo(dstView->asRenderTargetProxy()->numSamples(),
222 dstView->asRenderTargetProxy()->numStencilSamples(),
223 dstView->origin(),
Robert Phillips67a625e2019-11-15 15:37:07 -0500224 &pipeline,
Robert Phillips5d07c522019-11-18 11:19:51 -0500225 gp,
Robert Phillipsdf70f152019-11-15 14:57:05 -0500226 nullptr, nullptr, 0,
227 GrPrimitiveType::kTriangleStrip);
228
229 flushState->opsRenderPass()->draw(programInfo, &mesh, 1,
230 SkRect::MakeXYWH(0, fY, 100, 100));
231
232 }
Chris Dalton49d14e92018-07-27 12:38:35 -0600233 }
234
Robert Phillips5d07c522019-11-18 11:19:51 -0500235 sk_sp<GrBuffer> fVertexBuffer;
236 const bool fReadSkFragCoord;
237 const float fY;
Robert Phillipsdf70f152019-11-15 14:57:05 -0500238
Robert Phillips5d07c522019-11-18 11:19:51 -0500239 // The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when
240 // allocated, are allocated in the ddl-record-time arena. It is the arena's job to free up
241 // their memory so we just have a bare programInfo pointer here. We don't even store the
242 // GrPipeline and GrPrimitiveProcessor pointers here bc they are guaranteed to have the
243 // same lifetime as the program info.
244 GrProgramInfo* fProgramInfo = nullptr;
Chris Dalton49d14e92018-07-27 12:38:35 -0600245
246 friend class ::GrOpMemoryPool; // for ctor
Robert Phillips5d07c522019-11-18 11:19:51 -0500247
248 typedef GrDrawOp INHERITED;
Chris Dalton49d14e92018-07-27 12:38:35 -0600249};
250
251////////////////////////////////////////////////////////////////////////////////////////////////////
252// Test.
253
Chris Dalton3a778372019-02-07 15:23:36 -0700254void ClockwiseGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500255 rtc->clear(nullptr, { 0, 0, 0, 1 }, GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton49d14e92018-07-27 12:38:35 -0600256
257 // Draw the test directly to the frame buffer.
258 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
259 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
260
261 // Draw the test to an off-screen, top-down render target.
Greg Danielc594e622019-10-15 14:01:49 -0400262 GrColorType rtcColorType = rtc->colorInfo().colorType();
Robert Phillips9da87e02019-02-04 13:26:26 -0500263 if (auto topLeftRTC = ctx->priv().makeDeferredRenderTargetContext(
Greg Danielc594e622019-10-15 14:01:49 -0400264 SkBackingFit::kExact, 100, 200, rtcColorType, nullptr, 1,
Brian Salomond6287472019-06-24 15:50:07 -0400265 GrMipMapped::kNo, kTopLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes)) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500266 topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
267 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton49d14e92018-07-27 12:38:35 -0600268 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
269 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Greg Danielc594e622019-10-15 14:01:49 -0400270 rtc->drawTexture(GrNoClip(), sk_ref_sp(topLeftRTC->asTextureProxy()), rtcColorType,
Brian Salomon078e8fa2019-11-22 04:10:18 +0000271 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
272 SK_PMColor4fWHITE, {0, 0, 100, 200},
Michael Ludwig136f45a2019-02-19 11:44:41 -0500273 {100, 0, 200, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
Chris Dalton49d14e92018-07-27 12:38:35 -0600274 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, 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.
Robert Phillips9da87e02019-02-04 13:26:26 -0500279 if (auto topLeftRTC = ctx->priv().makeDeferredRenderTargetContext(
Greg Danielc594e622019-10-15 14:01:49 -0400280 SkBackingFit::kExact, 100, 200, rtcColorType, nullptr, 1,
Brian Salomond6287472019-06-24 15:50:07 -0400281 GrMipMapped::kNo, kBottomLeft_GrSurfaceOrigin, nullptr, SkBudgeted::kYes)) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500282 topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
283 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton49d14e92018-07-27 12:38:35 -0600284 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
285 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Greg Danielc594e622019-10-15 14:01:49 -0400286 rtc->drawTexture(GrNoClip(), sk_ref_sp(topLeftRTC->asTextureProxy()), rtcColorType,
Brian Salomon078e8fa2019-11-22 04:10:18 +0000287 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver,
288 SK_PMColor4fWHITE, {0, 0, 100, 200},
Michael Ludwig136f45a2019-02-19 11:44:41 -0500289 {200, 0, 300, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
Chris Dalton49d14e92018-07-27 12:38:35 -0600290 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500291 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600292 }
293}
294
295////////////////////////////////////////////////////////////////////////////////////////////////////
296
297DEF_GM( return new ClockwiseGM(); )
298
299}