blob: a6f134815cb89a4bd93eb6319218c4692ac0da66 [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"
13#include "src/core/SkMakeUnique.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrColor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrContextPriv.h"
16#include "src/gpu/GrGeometryProcessor.h"
Brian Salomonf2ebdd92019-09-30 12:15:30 -040017#include "src/gpu/GrImageInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrMemoryPool.h"
19#include "src/gpu/GrOpFlushState.h"
Greg Daniel2d41d0d2019-08-26 11:08:51 -040020#include "src/gpu/GrOpsRenderPass.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:
62 GrPipelineDynamicStateTestProcessor()
Brian Salomon92be2f72018-06-19 14:33:47 -040063 : INHERITED(kGrPipelineDynamicStateTestProcessor_ClassID) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -050064 this->setVertexAttributes(kAttributes, SK_ARRAY_COUNT(kAttributes));
Brian Salomon92be2f72018-06-19 14:33:47 -040065 }
Chris Dalton46983b72017-06-06 12:27:16 -060066
67 const char* name() const override { return "GrPipelineDynamicStateTest Processor"; }
68
69 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {}
70
71 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
72
Brian Osmanf04fb3c2018-11-12 15:34:00 -050073 const Attribute& inVertex() const { return kAttributes[0]; }
74 const Attribute& inColor() const { return kAttributes[1]; }
Brian Salomon92be2f72018-06-19 14:33:47 -040075
Brian Osmanf04fb3c2018-11-12 15:34:00 -050076private:
77 static constexpr Attribute kAttributes[] = {
78 {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType},
79 {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType},
80 };
Chris Dalton46983b72017-06-06 12:27:16 -060081
82 friend class GLSLPipelineDynamicStateTestProcessor;
83 typedef GrGeometryProcessor INHERITED;
84};
Brian Osmanf04fb3c2018-11-12 15:34:00 -050085constexpr GrPrimitiveProcessor::Attribute GrPipelineDynamicStateTestProcessor::kAttributes[];
Chris Dalton46983b72017-06-06 12:27:16 -060086
87class GLSLPipelineDynamicStateTestProcessor : public GrGLSLGeometryProcessor {
88 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
89 FPCoordTransformIter&& transformIter) final {}
90
91 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
92 const GrPipelineDynamicStateTestProcessor& mp =
93 args.fGP.cast<GrPipelineDynamicStateTestProcessor>();
94
95 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
96 varyingHandler->emitAttributes(mp);
Brian Osmanf04fb3c2018-11-12 15:34:00 -050097 varyingHandler->addPassThroughAttribute(mp.inColor(), args.fOutputColor);
Chris Dalton46983b72017-06-06 12:27:16 -060098
99 GrGLSLVertexBuilder* v = args.fVertBuilder;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500100 v->codeAppendf("float2 vertex = %s;", mp.inVertex().name());
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400101 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
Chris Dalton46983b72017-06-06 12:27:16 -0600102
Chris Dalton60283612018-02-14 13:38:14 -0700103 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400104 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton46983b72017-06-06 12:27:16 -0600105 }
106};
107
108GrGLSLPrimitiveProcessor*
109GrPipelineDynamicStateTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
110 return new GLSLPipelineDynamicStateTestProcessor;
111}
112
113class GrPipelineDynamicStateTestOp : public GrDrawOp {
114public:
115 DEFINE_OP_CLASS_ID
116
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500117 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Chris Dalton916c4982018-08-15 00:53:25 -0600118 GrScissorTest scissorTest,
Robert Phillips88a32ef2018-06-07 11:05:56 -0400119 sk_sp<const GrBuffer> vbuff) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500120 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400121
Chris Dalton916c4982018-08-15 00:53:25 -0600122 return pool->allocate<GrPipelineDynamicStateTestOp>(scissorTest, std::move(vbuff));
Robert Phillips88a32ef2018-06-07 11:05:56 -0400123 }
124
125private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400126 friend class GrOpMemoryPool;
127
Chris Dalton916c4982018-08-15 00:53:25 -0600128 GrPipelineDynamicStateTestOp(GrScissorTest scissorTest, sk_sp<const GrBuffer> vbuff)
Brian Salomond818ebf2018-07-02 14:08:49 +0000129 : INHERITED(ClassID())
Chris Dalton916c4982018-08-15 00:53:25 -0600130 , fScissorTest(scissorTest)
Brian Salomond818ebf2018-07-02 14:08:49 +0000131 , fVertexBuffer(std::move(vbuff)) {
Chris Dalton46983b72017-06-06 12:27:16 -0600132 this->setBounds(SkRect::MakeIWH(kScreenSize, kScreenSize),
Greg Daniel5faf4742019-10-01 15:14:44 -0400133 HasAABloat::kNo, IsHairline::kNo);
Chris Dalton46983b72017-06-06 12:27:16 -0600134 }
135
Chris Dalton46983b72017-06-06 12:27:16 -0600136 const char* name() const override { return "GrPipelineDynamicStateTestOp"; }
137 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600138 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
139 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700140 return GrProcessorSet::EmptySetAnalysis();
Brian Salomonf86d37b2017-06-16 10:04:34 -0400141 }
Chris Dalton46983b72017-06-06 12:27:16 -0600142 void onPrepare(GrOpFlushState*) override {}
Brian Salomon588cec72018-11-14 13:56:37 -0500143 void onExecute(GrOpFlushState* state, const SkRect& chainBounds) override {
Greg Daniel2c3398d2019-06-19 11:58:01 -0400144 GrPipeline pipeline(fScissorTest, SkBlendMode::kSrc, state->drawOpArgs().fOutputSwizzle);
Chris Dalton46983b72017-06-06 12:27:16 -0600145 SkSTArray<kNumMeshes, GrMesh> meshes;
146 for (int i = 0; i < kNumMeshes; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600147 GrMesh& mesh = meshes.emplace_back(GrPrimitiveType::kTriangleStrip);
Chris Dalton46983b72017-06-06 12:27:16 -0600148 mesh.setNonIndexedNonInstanced(4);
Brian Salomon12d22642019-01-29 14:38:50 -0500149 mesh.setVertexData(fVertexBuffer, 4 * i);
Chris Dalton46983b72017-06-06 12:27:16 -0600150 }
Brian Salomon49348902018-06-26 09:12:38 -0400151 GrPipeline::DynamicStateArrays dynamicState;
152 dynamicState.fScissorRects = kDynamicScissors;
Greg Daniel2d41d0d2019-08-26 11:08:51 -0400153 state->opsRenderPass()->draw(GrPipelineDynamicStateTestProcessor(), pipeline, nullptr,
154 &dynamicState, meshes.begin(), 4,
155 SkRect::MakeIWH(kScreenSize, kScreenSize));
Chris Dalton46983b72017-06-06 12:27:16 -0600156 }
157
Chris Dalton916c4982018-08-15 00:53:25 -0600158 GrScissorTest fScissorTest;
Chris Dalton46983b72017-06-06 12:27:16 -0600159 const sk_sp<const GrBuffer> fVertexBuffer;
160
161 typedef GrDrawOp INHERITED;
162};
163
164DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrPipelineDynamicStateTest, reporter, ctxInfo) {
Robert Phillips88a32ef2018-06-07 11:05:56 -0400165 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500166 GrResourceProvider* rp = context->priv().resourceProvider();
Chris Dalton46983b72017-06-06 12:27:16 -0600167
Brian Salomonbf6b9792019-08-21 09:38:10 -0400168 auto rtc = context->priv().makeDeferredRenderTargetContext(
169 SkBackingFit::kExact, kScreenSize, kScreenSize, GrColorType::kRGBA_8888, nullptr);
Chris Dalton46983b72017-06-06 12:27:16 -0600170 if (!rtc) {
171 ERRORF(reporter, "could not create render target context.");
172 return;
173 }
174
175 constexpr float d = (float) kScreenSize;
176 Vertex vdata[kNumMeshes * 4] = {
177 {0, 0, kMeshColors[0]},
178 {0, d, kMeshColors[0]},
179 {d, 0, kMeshColors[0]},
180 {d, d, kMeshColors[0]},
181
182 {0, 0, kMeshColors[1]},
183 {0, d, kMeshColors[1]},
184 {d, 0, kMeshColors[1]},
185 {d, d, kMeshColors[1]},
186
187 {0, 0, kMeshColors[2]},
188 {0, d, kMeshColors[2]},
189 {d, 0, kMeshColors[2]},
190 {d, d, kMeshColors[2]},
191
192 {0, 0, kMeshColors[3]},
193 {0, d, kMeshColors[3]},
194 {d, 0, kMeshColors[3]},
195 {d, d, kMeshColors[3]}
196 };
197
Brian Salomondbf70722019-02-07 11:31:24 -0500198 sk_sp<const GrBuffer> vbuff(rp->createBuffer(sizeof(vdata), GrGpuBufferType::kVertex,
199 kDynamic_GrAccessPattern, vdata));
Chris Dalton46983b72017-06-06 12:27:16 -0600200 if (!vbuff) {
201 ERRORF(reporter, "vbuff is null.");
202 return;
203 }
204
205 uint32_t resultPx[kScreenSize * kScreenSize];
206
Chris Dalton916c4982018-08-15 00:53:25 -0600207 for (GrScissorTest scissorTest : {GrScissorTest::kEnabled, GrScissorTest::kDisabled}) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500208 rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xbaaaaaad),
209 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton46983b72017-06-06 12:27:16 -0600210 rtc->priv().testingOnly_addDrawOp(
Chris Dalton916c4982018-08-15 00:53:25 -0600211 GrPipelineDynamicStateTestOp::Make(context, scissorTest, vbuff));
Chris Dalton46983b72017-06-06 12:27:16 -0600212 rtc->readPixels(SkImageInfo::Make(kScreenSize, kScreenSize,
213 kRGBA_8888_SkColorType, kPremul_SkAlphaType),
Brian Salomon1d435302019-07-01 13:05:28 -0400214 resultPx, 4 * kScreenSize, {0, 0});
Chris Dalton46983b72017-06-06 12:27:16 -0600215 for (int y = 0; y < kScreenSize; ++y) {
216 for (int x = 0; x < kScreenSize; ++x) {
217 int expectedColorIdx;
Chris Dalton916c4982018-08-15 00:53:25 -0600218 if (GrScissorTest::kEnabled == scissorTest) {
Chris Dalton46983b72017-06-06 12:27:16 -0600219 expectedColorIdx = (x < kScreenSplitX ? 0 : 2) + (y < kScreenSplitY ? 0 : 1);
220 } else {
221 expectedColorIdx = kNumMeshes - 1;
222 }
223 uint32_t expected = kMeshColors[expectedColorIdx];
224 uint32_t actual = resultPx[y * kScreenSize + x];
225 if (expected != actual) {
226 ERRORF(reporter, "[scissor=%s] pixel (%i,%i): got 0x%x expected 0x%x",
Chris Dalton916c4982018-08-15 00:53:25 -0600227 GrScissorTest::kEnabled == scissorTest ? "enabled" : "disabled", x, y,
Chris Dalton46983b72017-06-06 12:27:16 -0600228 actual, expected);
229 return;
230 }
231 }
232 }
233 }
234}