blob: 20acef5cff970c229f98fba0e39fb513d3fd23c6 [file] [log] [blame]
Chris Dalton46983b72017-06-06 12:27:16 -06001/*
2 * Copyright 2017 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 "include/core/SkTypes.h"
9#include "tests/Test.h"
Chris Dalton46983b72017-06-06 12:27:16 -060010
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrRecordingContext.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrContextPriv.h"
15#include "src/gpu/GrGeometryProcessor.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040016#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrMemoryPool.h"
18#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040019#include "src/gpu/GrOpsRenderPass.h"
Robert Phillips901aff02019-10-08 12:32:56 -040020#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrRecordingContextPriv.h"
22#include "src/gpu/GrRenderTargetContext.h"
23#include "src/gpu/GrRenderTargetContextPriv.h"
24#include "src/gpu/GrResourceProvider.h"
25#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
26#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
27#include "src/gpu/glsl/GrGLSLVarying.h"
28#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton46983b72017-06-06 12:27:16 -060029
30/**
31 * This is a GPU-backend specific test for dynamic pipeline state. It draws boxes using dynamic
32 * scissor rectangles then reads back the result to verify a successful test.
33 */
34
Chris Dalton46983b72017-06-06 12:27:16 -060035static constexpr int kScreenSize = 6;
36static constexpr int kNumMeshes = 4;
37static constexpr int kScreenSplitX = kScreenSize/2;
38static constexpr int kScreenSplitY = kScreenSize/2;
39
Brian Salomon49348902018-06-26 09:12:38 -040040static const SkIRect kDynamicScissors[kNumMeshes] = {
41 SkIRect::MakeLTRB(0, 0, kScreenSplitX, kScreenSplitY),
42 SkIRect::MakeLTRB(0, kScreenSplitY, kScreenSplitX, kScreenSize),
43 SkIRect::MakeLTRB(kScreenSplitX, 0, kScreenSize, kScreenSplitY),
44 SkIRect::MakeLTRB(kScreenSplitX, kScreenSplitY, kScreenSize, kScreenSize),
Chris Dalton46983b72017-06-06 12:27:16 -060045};
46
47static const GrColor kMeshColors[kNumMeshes] {
48 GrColorPackRGBA(255, 0, 0, 255),
49 GrColorPackRGBA(0, 255, 0, 255),
50 GrColorPackRGBA(0, 0, 255, 255),
51 GrColorPackRGBA(0, 0, 0, 255)
52};
53
54struct Vertex {
55 float fX;
56 float fY;
57 GrColor fColor;
58};
59
60class GrPipelineDynamicStateTestProcessor : public GrGeometryProcessor {
61public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050062 static GrGeometryProcessor* Make(SkArenaAlloc* arena) {
63 return arena->make<GrPipelineDynamicStateTestProcessor>();
Brian Salomon92be2f72018-06-19 14:33:47 -040064 }
Chris Dalton46983b72017-06-06 12:27:16 -060065
66 const char* name() const override { return "GrPipelineDynamicStateTest Processor"; }
67
68 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {}
69
70 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
71
Brian Osmanf04fb3c2018-11-12 15:34:00 -050072 const Attribute& inVertex() const { return kAttributes[0]; }
73 const Attribute& inColor() const { return kAttributes[1]; }
Brian Salomon92be2f72018-06-19 14:33:47 -040074
Brian Osmanf04fb3c2018-11-12 15:34:00 -050075private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -050076 friend class ::SkArenaAlloc; // for access to ctor
77
78 GrPipelineDynamicStateTestProcessor()
79 : INHERITED(kGrPipelineDynamicStateTestProcessor_ClassID) {
80 this->setVertexAttributes(kAttributes, SK_ARRAY_COUNT(kAttributes));
81 }
82
Brian Osmanf04fb3c2018-11-12 15:34:00 -050083 static constexpr Attribute kAttributes[] = {
84 {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType},
85 {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType},
86 };
Chris Dalton46983b72017-06-06 12:27:16 -060087
88 friend class GLSLPipelineDynamicStateTestProcessor;
89 typedef GrGeometryProcessor INHERITED;
90};
Brian Osmanf04fb3c2018-11-12 15:34:00 -050091constexpr GrPrimitiveProcessor::Attribute GrPipelineDynamicStateTestProcessor::kAttributes[];
Chris Dalton46983b72017-06-06 12:27:16 -060092
93class GLSLPipelineDynamicStateTestProcessor : public GrGLSLGeometryProcessor {
94 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
Brian Salomonc241b582019-11-27 08:57:17 -050095 const CoordTransformRange&) final {}
Chris Dalton46983b72017-06-06 12:27:16 -060096
97 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
98 const GrPipelineDynamicStateTestProcessor& mp =
99 args.fGP.cast<GrPipelineDynamicStateTestProcessor>();
100
101 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
102 varyingHandler->emitAttributes(mp);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500103 varyingHandler->addPassThroughAttribute(mp.inColor(), args.fOutputColor);
Chris Dalton46983b72017-06-06 12:27:16 -0600104
105 GrGLSLVertexBuilder* v = args.fVertBuilder;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500106 v->codeAppendf("float2 vertex = %s;", mp.inVertex().name());
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400107 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
Chris Dalton46983b72017-06-06 12:27:16 -0600108
Chris Dalton60283612018-02-14 13:38:14 -0700109 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400110 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton46983b72017-06-06 12:27:16 -0600111 }
112};
113
114GrGLSLPrimitiveProcessor*
115GrPipelineDynamicStateTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
116 return new GLSLPipelineDynamicStateTestProcessor;
117}
118
119class GrPipelineDynamicStateTestOp : public GrDrawOp {
120public:
121 DEFINE_OP_CLASS_ID
122
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500123 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Chris Dalton916c4982018-08-15 00:53:25 -0600124 GrScissorTest scissorTest,
Robert Phillips88a32ef2018-06-07 11:05:56 -0400125 sk_sp<const GrBuffer> vbuff) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500126 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400127
Chris Dalton916c4982018-08-15 00:53:25 -0600128 return pool->allocate<GrPipelineDynamicStateTestOp>(scissorTest, std::move(vbuff));
Robert Phillips88a32ef2018-06-07 11:05:56 -0400129 }
130
131private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400132 friend class GrOpMemoryPool;
133
Chris Dalton916c4982018-08-15 00:53:25 -0600134 GrPipelineDynamicStateTestOp(GrScissorTest scissorTest, sk_sp<const GrBuffer> vbuff)
Robert Phillips709e2402020-03-23 18:29:16 +0000135 : INHERITED(ClassID())
136 , fScissorTest(scissorTest)
137 , fVertexBuffer(std::move(vbuff)) {
Chris Dalton46983b72017-06-06 12:27:16 -0600138 this->setBounds(SkRect::MakeIWH(kScreenSize, kScreenSize),
Greg Daniel5faf4742019-10-01 15:14:44 -0400139 HasAABloat::kNo, IsHairline::kNo);
Chris Dalton46983b72017-06-06 12:27:16 -0600140 }
141
Chris Dalton46983b72017-06-06 12:27:16 -0600142 const char* name() const override { return "GrPipelineDynamicStateTestOp"; }
143 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Robert Phillips709e2402020-03-23 18:29:16 +0000144 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
145 bool hasMixedSampledCoverage, GrClampType) override {
146 return GrProcessorSet::EmptySetAnalysis();
Brian Salomonf86d37b2017-06-16 10:04:34 -0400147 }
Robert Phillips709e2402020-03-23 18:29:16 +0000148 void onPrePrepare(GrRecordingContext*,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400149 const GrSurfaceProxyView* writeView,
Robert Phillips709e2402020-03-23 18:29:16 +0000150 GrAppliedClip*,
151 const GrXferProcessor::DstProxyView&) override {}
Chris Dalton46983b72017-06-06 12:27:16 -0600152 void onPrepare(GrOpFlushState*) override {}
Robert Phillips901aff02019-10-08 12:32:56 -0400153 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips709e2402020-03-23 18:29:16 +0000154 GrPipeline pipeline(fScissorTest, SkBlendMode::kSrc,
Brian Salomon982f5462020-03-30 12:52:33 -0400155 flushState->drawOpArgs().writeSwizzle());
Chris Daltoneb694b72020-03-16 09:25:50 -0600156 SkSTArray<kNumMeshes, GrSimpleMesh> meshes;
Chris Dalton46983b72017-06-06 12:27:16 -0600157 for (int i = 0; i < kNumMeshes; ++i) {
Chris Daltoneb694b72020-03-16 09:25:50 -0600158 GrSimpleMesh& mesh = meshes.push_back();
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600159 mesh.set(fVertexBuffer, 4, 4 * i);
Chris Dalton46983b72017-06-06 12:27:16 -0600160 }
Robert Phillips901aff02019-10-08 12:32:56 -0400161
Robert Phillips709e2402020-03-23 18:29:16 +0000162 auto geomProc = GrPipelineDynamicStateTestProcessor::Make(flushState->allocator());
163
164 GrProgramInfo programInfo(flushState->proxy()->numSamples(),
165 flushState->proxy()->numStencilSamples(),
166 flushState->proxy()->backendFormat(),
Brian Salomon8afde5f2020-04-01 16:22:00 -0400167 flushState->writeView()->origin(),
Robert Phillips709e2402020-03-23 18:29:16 +0000168 &pipeline,
169 geomProc,
170 GrPrimitiveType::kTriangleStrip);
171
172 flushState->bindPipeline(programInfo, SkRect::MakeIWH(kScreenSize, kScreenSize));
173 for (int i = 0; i < 4; ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600174 if (fScissorTest == GrScissorTest::kEnabled) {
175 flushState->setScissorRect(kDynamicScissors[i]);
176 }
177 flushState->drawMesh(meshes[i]);
178 }
Chris Dalton46983b72017-06-06 12:27:16 -0600179 }
180
Chris Dalton916c4982018-08-15 00:53:25 -0600181 GrScissorTest fScissorTest;
Chris Dalton46983b72017-06-06 12:27:16 -0600182 const sk_sp<const GrBuffer> fVertexBuffer;
183
184 typedef GrDrawOp INHERITED;
185};
186
187DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrPipelineDynamicStateTest, reporter, ctxInfo) {
Robert Phillips88a32ef2018-06-07 11:05:56 -0400188 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500189 GrResourceProvider* rp = context->priv().resourceProvider();
Chris Dalton46983b72017-06-06 12:27:16 -0600190
Greg Daniele20fcad2020-01-08 11:52:34 -0500191 auto rtc = GrRenderTargetContext::Make(
192 context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact,
193 {kScreenSize, kScreenSize});
Chris Dalton46983b72017-06-06 12:27:16 -0600194 if (!rtc) {
195 ERRORF(reporter, "could not create render target context.");
196 return;
197 }
198
199 constexpr float d = (float) kScreenSize;
200 Vertex vdata[kNumMeshes * 4] = {
201 {0, 0, kMeshColors[0]},
202 {0, d, kMeshColors[0]},
203 {d, 0, kMeshColors[0]},
204 {d, d, kMeshColors[0]},
205
206 {0, 0, kMeshColors[1]},
207 {0, d, kMeshColors[1]},
208 {d, 0, kMeshColors[1]},
209 {d, d, kMeshColors[1]},
210
211 {0, 0, kMeshColors[2]},
212 {0, d, kMeshColors[2]},
213 {d, 0, kMeshColors[2]},
214 {d, d, kMeshColors[2]},
215
216 {0, 0, kMeshColors[3]},
217 {0, d, kMeshColors[3]},
218 {d, 0, kMeshColors[3]},
219 {d, d, kMeshColors[3]}
220 };
221
Brian Salomondbf70722019-02-07 11:31:24 -0500222 sk_sp<const GrBuffer> vbuff(rp->createBuffer(sizeof(vdata), GrGpuBufferType::kVertex,
223 kDynamic_GrAccessPattern, vdata));
Chris Dalton46983b72017-06-06 12:27:16 -0600224 if (!vbuff) {
225 ERRORF(reporter, "vbuff is null.");
226 return;
227 }
228
229 uint32_t resultPx[kScreenSize * kScreenSize];
230
Chris Dalton916c4982018-08-15 00:53:25 -0600231 for (GrScissorTest scissorTest : {GrScissorTest::kEnabled, GrScissorTest::kDisabled}) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500232 rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xbaaaaaad),
233 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton46983b72017-06-06 12:27:16 -0600234 rtc->priv().testingOnly_addDrawOp(
Chris Dalton916c4982018-08-15 00:53:25 -0600235 GrPipelineDynamicStateTestOp::Make(context, scissorTest, vbuff));
Chris Dalton46983b72017-06-06 12:27:16 -0600236 rtc->readPixels(SkImageInfo::Make(kScreenSize, kScreenSize,
237 kRGBA_8888_SkColorType, kPremul_SkAlphaType),
Brian Salomon1d435302019-07-01 13:05:28 -0400238 resultPx, 4 * kScreenSize, {0, 0});
Chris Dalton46983b72017-06-06 12:27:16 -0600239 for (int y = 0; y < kScreenSize; ++y) {
240 for (int x = 0; x < kScreenSize; ++x) {
241 int expectedColorIdx;
Chris Dalton916c4982018-08-15 00:53:25 -0600242 if (GrScissorTest::kEnabled == scissorTest) {
Chris Dalton46983b72017-06-06 12:27:16 -0600243 expectedColorIdx = (x < kScreenSplitX ? 0 : 2) + (y < kScreenSplitY ? 0 : 1);
244 } else {
245 expectedColorIdx = kNumMeshes - 1;
246 }
247 uint32_t expected = kMeshColors[expectedColorIdx];
248 uint32_t actual = resultPx[y * kScreenSize + x];
249 if (expected != actual) {
250 ERRORF(reporter, "[scissor=%s] pixel (%i,%i): got 0x%x expected 0x%x",
Chris Dalton916c4982018-08-15 00:53:25 -0600251 GrScissorTest::kEnabled == scissorTest ? "enabled" : "disabled", x, y,
Chris Dalton46983b72017-06-06 12:27:16 -0600252 actual, expected);
253 return;
254 }
255 }
256 }
257 }
258}