blob: d39db4b8f3d7595720ed116aad74aa55a08fd6d3 [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"
Robert Phillips34cea002019-11-21 16:02:34 -050055#include "tools/gpu/ProxyUtils.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040056
57#include <memory>
58#include <utility>
59
60class GrAppliedClip;
61class GrGLSLProgramDataManager;
Chris Dalton49d14e92018-07-27 12:38:35 -060062
Hal Canaryfa3305a2019-07-18 12:36:54 -040063namespace {
Chris Dalton49d14e92018-07-27 12:38:35 -060064
Brian Osmand4c29702018-09-14 16:16:55 -040065static constexpr GrGeometryProcessor::Attribute gVertex =
Stephen White1e1ad8d2019-06-19 14:19:47 -040066 {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Chris Dalton49d14e92018-07-27 12:38:35 -060067
68/**
69 * This is a GPU-backend specific test. It ensures that SkSL properly identifies clockwise-winding
70 * triangles (sk_Clockwise), in terms of to Skia device space, in all backends and with all render
71 * target origins. We draw clockwise triangles green and counter-clockwise red.
72 */
Hal Canaryfa3305a2019-07-18 12:36:54 -040073class ClockwiseGM : public skiagm::GpuGM {
74 SkString onShortName() override { return SkString("clockwise"); }
75 SkISize onISize() override { return {300, 200}; }
Chris Dalton3a778372019-02-07 15:23:36 -070076 void onDraw(GrContext*, GrRenderTargetContext*, SkCanvas*) override;
Chris Dalton49d14e92018-07-27 12:38:35 -060077};
78
79////////////////////////////////////////////////////////////////////////////////////////////////////
80// SkSL code.
81
82class ClockwiseTestProcessor : public GrGeometryProcessor {
83public:
Robert Phillips5d07c522019-11-18 11:19:51 -050084 static GrGeometryProcessor* Make(SkArenaAlloc* arena, bool readSkFragCoord) {
85 return arena->make<ClockwiseTestProcessor>(readSkFragCoord);
Robert Phillipsdf70f152019-11-15 14:57:05 -050086 }
87
88 const char* name() const final { return "ClockwiseTestProcessor"; }
89
90 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
91 b->add32(fReadSkFragCoord);
92 }
93
94 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
95
Robert Phillips5d07c522019-11-18 11:19:51 -050096 bool readSkFragCoord() const { return fReadSkFragCoord; }
97
Robert Phillipsdf70f152019-11-15 14:57:05 -050098private:
Robert Phillips5d07c522019-11-18 11:19:51 -050099 friend class ::SkArenaAlloc; // for access to ctor
100
Chris Dalton49d14e92018-07-27 12:38:35 -0600101 ClockwiseTestProcessor(bool readSkFragCoord)
102 : GrGeometryProcessor(kClockwiseTestProcessor_ClassID)
103 , fReadSkFragCoord(readSkFragCoord) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500104 this->setVertexAttributes(&gVertex, 1);
Chris Dalton49d14e92018-07-27 12:38:35 -0600105 }
Chris Dalton49d14e92018-07-27 12:38:35 -0600106
Chris Dalton49d14e92018-07-27 12:38:35 -0600107 const bool fReadSkFragCoord;
108
Robert Phillips5d07c522019-11-18 11:19:51 -0500109 typedef GrGeometryProcessor INHERITED;
Chris Dalton49d14e92018-07-27 12:38:35 -0600110};
111
112class GLSLClockwiseTestProcessor : public GrGLSLGeometryProcessor {
113 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
Brian Salomonc241b582019-11-27 08:57:17 -0500114 const CoordTransformRange&) override {}
Chris Dalton49d14e92018-07-27 12:38:35 -0600115
116 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
117 const ClockwiseTestProcessor& proc = args.fGP.cast<ClockwiseTestProcessor>();
118 args.fVaryingHandler->emitAttributes(proc);
Stephen White1e1ad8d2019-06-19 14:19:47 -0400119 gpArgs->fPositionVar.set(kFloat2_GrSLType, "position");
Chris Dalton49d14e92018-07-27 12:38:35 -0600120 args.fFragBuilder->codeAppendf(
121 "%s = sk_Clockwise ? half4(0,1,0,1) : half4(1,0,0,1);", args.fOutputColor);
Robert Phillips5d07c522019-11-18 11:19:51 -0500122 if (!proc.readSkFragCoord()) {
Chris Dalton49d14e92018-07-27 12:38:35 -0600123 args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage);
124 } else {
125 // Verify layout(origin_upper_left) on gl_FragCoord does not affect gl_FrontFacing.
Ethan Nicholase1f55022019-02-05 17:17:40 -0500126 args.fFragBuilder->codeAppendf("%s = half4(min(half(sk_FragCoord.y), 1));",
Chris Dalton49d14e92018-07-27 12:38:35 -0600127 args.fOutputCoverage);
128 }
129 }
130};
131
132GrGLSLPrimitiveProcessor* ClockwiseTestProcessor::createGLSLInstance(
133 const GrShaderCaps&) const {
134 return new GLSLClockwiseTestProcessor;
135}
136
137////////////////////////////////////////////////////////////////////////////////////////////////////
138// Draw Op.
139
140class ClockwiseTestOp : public GrDrawOp {
141public:
142 DEFINE_OP_CLASS_ID
143
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500144 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
145 bool readSkFragCoord, int y = 0) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500146 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Chris Dalton49d14e92018-07-27 12:38:35 -0600147 return pool->allocate<ClockwiseTestOp>(readSkFragCoord, y);
148 }
149
150private:
151 ClockwiseTestOp(bool readSkFragCoord, float y)
Robert Phillips5d07c522019-11-18 11:19:51 -0500152 : GrDrawOp(ClassID())
153 , fReadSkFragCoord(readSkFragCoord)
154 , fY(y) {
Greg Daniel5faf4742019-10-01 15:14:44 -0400155 this->setBounds(SkRect::MakeXYWH(0, fY, 100, 100), HasAABloat::kNo, IsHairline::kNo);
Chris Dalton49d14e92018-07-27 12:38:35 -0600156 }
157
158 const char* name() const override { return "ClockwiseTestOp"; }
159 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600160 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
161 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700162 return GrProcessorSet::EmptySetAnalysis();
Chris Dalton49d14e92018-07-27 12:38:35 -0600163 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500164
165 void onPrePrepare(GrRecordingContext* context,
166 const GrSurfaceProxyView* dstView,
Robert Phillips34cea002019-11-21 16:02:34 -0500167 GrAppliedClip* clip,
Robert Phillips8053c972019-11-21 10:44:53 -0500168 const GrXferProcessor::DstProxyView& dstProxyView) final {
Robert Phillipsd4fb7c72019-11-15 17:28:37 -0500169 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500170
Robert Phillips34cea002019-11-21 16:02:34 -0500171 // This is equivalent to a GrOpFlushState::detachAppliedClip
172 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip();
Robert Phillipsdf70f152019-11-15 14:57:05 -0500173
Robert Phillips34cea002019-11-21 16:02:34 -0500174 GrGeometryProcessor* geomProc = ClockwiseTestProcessor::Make(arena, fReadSkFragCoord);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500175
Robert Phillips34cea002019-11-21 16:02:34 -0500176 // TODO: need to also give this to the recording context
177 fProgramInfo = sk_gpu_test::CreateProgramInfo(context->priv().caps(), arena, dstView,
178 std::move(appliedClip), dstProxyView,
179 geomProc, SkBlendMode::kPlus,
180 GrPrimitiveType::kTriangleStrip);
Robert Phillipsdf70f152019-11-15 14:57:05 -0500181 }
182
Greg Danielf793de12019-09-05 13:23:23 -0400183 void onPrepare(GrOpFlushState* flushState) override {
Chris Dalton49d14e92018-07-27 12:38:35 -0600184 SkPoint vertices[4] = {
185 {100, fY},
186 {0, fY+100},
187 {0, fY},
188 {100, fY+100},
189 };
Greg Danielf793de12019-09-05 13:23:23 -0400190 fVertexBuffer = flushState->resourceProvider()->createBuffer(
191 sizeof(vertices), GrGpuBufferType::kVertex, kStatic_GrAccessPattern, vertices);
192 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500193
Greg Danielf793de12019-09-05 13:23:23 -0400194 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
195 if (!fVertexBuffer) {
Chris Dalton79f99f72018-07-27 14:15:10 -0600196 return;
197 }
Robert Phillipsdf70f152019-11-15 14:57:05 -0500198
Robert Phillips34cea002019-11-21 16:02:34 -0500199 if (!fProgramInfo) {
200 GrGeometryProcessor* geomProc = ClockwiseTestProcessor::Make(flushState->allocator(),
201 fReadSkFragCoord);
202
203 fProgramInfo = sk_gpu_test::CreateProgramInfo(&flushState->caps(),
204 flushState->allocator(),
205 flushState->view(),
206 flushState->detachAppliedClip(),
207 flushState->dstProxyView(),
208 geomProc, SkBlendMode::kPlus,
209 GrPrimitiveType::kTriangleStrip);
210 }
211
Chris Dalton49d14e92018-07-27 12:38:35 -0600212 GrMesh mesh(GrPrimitiveType::kTriangleStrip);
213 mesh.setNonIndexedNonInstanced(4);
Greg Danielf793de12019-09-05 13:23:23 -0400214 mesh.setVertexData(std::move(fVertexBuffer));
Robert Phillips901aff02019-10-08 12:32:56 -0400215
Robert Phillips34cea002019-11-21 16:02:34 -0500216 flushState->opsRenderPass()->draw(*fProgramInfo, &mesh, 1,
217 SkRect::MakeXYWH(0, fY, 100, 100));
Chris Dalton49d14e92018-07-27 12:38:35 -0600218 }
219
Robert Phillips5d07c522019-11-18 11:19:51 -0500220 sk_sp<GrBuffer> fVertexBuffer;
221 const bool fReadSkFragCoord;
222 const float fY;
Robert Phillipsdf70f152019-11-15 14:57:05 -0500223
Robert Phillips5d07c522019-11-18 11:19:51 -0500224 // The program info (and both the GrPipeline and GrPrimitiveProcessor it relies on), when
Robert Phillips34cea002019-11-21 16:02:34 -0500225 // allocated, are allocated in either the ddl-record-time or flush-time arena. It is the
226 // arena's job to free up their memory so we just have a bare programInfo pointer here. We
227 // don't even store the GrPipeline and GrPrimitiveProcessor pointers here bc they are
228 // guaranteed to have the same lifetime as the program info.
Robert Phillips5d07c522019-11-18 11:19:51 -0500229 GrProgramInfo* fProgramInfo = nullptr;
Chris Dalton49d14e92018-07-27 12:38:35 -0600230
231 friend class ::GrOpMemoryPool; // for ctor
Robert Phillips5d07c522019-11-18 11:19:51 -0500232
233 typedef GrDrawOp INHERITED;
Chris Dalton49d14e92018-07-27 12:38:35 -0600234};
235
236////////////////////////////////////////////////////////////////////////////////////////////////////
237// Test.
238
Chris Dalton3a778372019-02-07 15:23:36 -0700239void ClockwiseGM::onDraw(GrContext* ctx, GrRenderTargetContext* rtc, SkCanvas* canvas) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500240 rtc->clear(nullptr, { 0, 0, 0, 1 }, GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton49d14e92018-07-27 12:38:35 -0600241
242 // Draw the test directly to the frame buffer.
243 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
244 rtc->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
245
246 // Draw the test to an off-screen, top-down render target.
Greg Danielc594e622019-10-15 14:01:49 -0400247 GrColorType rtcColorType = rtc->colorInfo().colorType();
Greg Daniele20fcad2020-01-08 11:52:34 -0500248 if (auto topLeftRTC = GrRenderTargetContext::Make(
249 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, 1,
250 GrMipMapped::kNo, GrProtected::kNo, kTopLeft_GrSurfaceOrigin, SkBudgeted::kYes,
251 nullptr)) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500252 topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
253 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton49d14e92018-07-27 12:38:35 -0600254 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
255 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Greg Daniel40903af2020-01-30 14:55:05 -0500256 rtc->drawTexture(GrNoClip(), topLeftRTC->readSurfaceView(), rtc->colorInfo().alphaType(),
257 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver, SK_PMColor4fWHITE,
258 {0, 0, 100, 200}, {100, 0, 200, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
Chris Dalton49d14e92018-07-27 12:38:35 -0600259 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500260 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600261 }
262
263 // Draw the test to an off-screen, bottom-up render target.
Greg Daniele20fcad2020-01-08 11:52:34 -0500264 if (auto topLeftRTC = GrRenderTargetContext::Make(
265 ctx, rtcColorType, nullptr, SkBackingFit::kExact, {100, 200}, 1,
266 GrMipMapped::kNo, GrProtected::kNo, kBottomLeft_GrSurfaceOrigin, SkBudgeted::kYes,
267 nullptr)) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500268 topLeftRTC->clear(nullptr, SK_PMColor4fTRANSPARENT,
269 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton49d14e92018-07-27 12:38:35 -0600270 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, false, 0));
271 topLeftRTC->priv().testingOnly_addDrawOp(ClockwiseTestOp::Make(ctx, true, 100));
Greg Daniel40903af2020-01-30 14:55:05 -0500272 rtc->drawTexture(GrNoClip(), topLeftRTC->readSurfaceView(), rtc->colorInfo().alphaType(),
273 GrSamplerState::Filter::kNearest, SkBlendMode::kSrcOver, SK_PMColor4fWHITE,
274 {0, 0, 100, 200}, {200, 0, 300, 200}, GrAA::kNo, GrQuadAAFlags::kNone,
Chris Dalton49d14e92018-07-27 12:38:35 -0600275 SkCanvas::SrcRectConstraint::kStrict_SrcRectConstraint, SkMatrix::I(),
Brian Osman3d139a42018-11-19 10:42:10 -0500276 nullptr);
Chris Dalton49d14e92018-07-27 12:38:35 -0600277 }
278}
279
280////////////////////////////////////////////////////////////////////////////////////////////////////
281
282DEF_GM( return new ClockwiseGM(); )
283
284}