blob: 4e1a297f7852de5b9215f0fce6061e0c6ab1982a [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"
17#include "src/gpu/GrGpuCommandBuffer.h"
18#include "src/gpu/GrMemoryPool.h"
19#include "src/gpu/GrOpFlushState.h"
20#include "src/gpu/GrRecordingContextPriv.h"
21#include "src/gpu/GrRenderTargetContext.h"
22#include "src/gpu/GrRenderTargetContextPriv.h"
23#include "src/gpu/GrResourceProvider.h"
24#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
25#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
26#include "src/gpu/glsl/GrGLSLVarying.h"
27#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton46983b72017-06-06 12:27:16 -060028
29/**
30 * This is a GPU-backend specific test for dynamic pipeline state. It draws boxes using dynamic
31 * scissor rectangles then reads back the result to verify a successful test.
32 */
33
Chris Dalton46983b72017-06-06 12:27:16 -060034static constexpr int kScreenSize = 6;
35static constexpr int kNumMeshes = 4;
36static constexpr int kScreenSplitX = kScreenSize/2;
37static constexpr int kScreenSplitY = kScreenSize/2;
38
Brian Salomon49348902018-06-26 09:12:38 -040039static const SkIRect kDynamicScissors[kNumMeshes] = {
40 SkIRect::MakeLTRB(0, 0, kScreenSplitX, kScreenSplitY),
41 SkIRect::MakeLTRB(0, kScreenSplitY, kScreenSplitX, kScreenSize),
42 SkIRect::MakeLTRB(kScreenSplitX, 0, kScreenSize, kScreenSplitY),
43 SkIRect::MakeLTRB(kScreenSplitX, kScreenSplitY, kScreenSize, kScreenSize),
Chris Dalton46983b72017-06-06 12:27:16 -060044};
45
46static const GrColor kMeshColors[kNumMeshes] {
47 GrColorPackRGBA(255, 0, 0, 255),
48 GrColorPackRGBA(0, 255, 0, 255),
49 GrColorPackRGBA(0, 0, 255, 255),
50 GrColorPackRGBA(0, 0, 0, 255)
51};
52
53struct Vertex {
54 float fX;
55 float fY;
56 GrColor fColor;
57};
58
59class GrPipelineDynamicStateTestProcessor : public GrGeometryProcessor {
60public:
61 GrPipelineDynamicStateTestProcessor()
Brian Salomon92be2f72018-06-19 14:33:47 -040062 : INHERITED(kGrPipelineDynamicStateTestProcessor_ClassID) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -050063 this->setVertexAttributes(kAttributes, SK_ARRAY_COUNT(kAttributes));
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:
76 static constexpr Attribute kAttributes[] = {
77 {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType},
78 {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType},
79 };
Chris Dalton46983b72017-06-06 12:27:16 -060080
81 friend class GLSLPipelineDynamicStateTestProcessor;
82 typedef GrGeometryProcessor INHERITED;
83};
Brian Osmanf04fb3c2018-11-12 15:34:00 -050084constexpr GrPrimitiveProcessor::Attribute GrPipelineDynamicStateTestProcessor::kAttributes[];
Chris Dalton46983b72017-06-06 12:27:16 -060085
86class GLSLPipelineDynamicStateTestProcessor : public GrGLSLGeometryProcessor {
87 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
88 FPCoordTransformIter&& transformIter) final {}
89
90 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
91 const GrPipelineDynamicStateTestProcessor& mp =
92 args.fGP.cast<GrPipelineDynamicStateTestProcessor>();
93
94 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
95 varyingHandler->emitAttributes(mp);
Brian Osmanf04fb3c2018-11-12 15:34:00 -050096 varyingHandler->addPassThroughAttribute(mp.inColor(), args.fOutputColor);
Chris Dalton46983b72017-06-06 12:27:16 -060097
98 GrGLSLVertexBuilder* v = args.fVertBuilder;
Brian Osmanf04fb3c2018-11-12 15:34:00 -050099 v->codeAppendf("float2 vertex = %s;", mp.inVertex().name());
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400100 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
Chris Dalton46983b72017-06-06 12:27:16 -0600101
Chris Dalton60283612018-02-14 13:38:14 -0700102 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400103 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton46983b72017-06-06 12:27:16 -0600104 }
105};
106
107GrGLSLPrimitiveProcessor*
108GrPipelineDynamicStateTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
109 return new GLSLPipelineDynamicStateTestProcessor;
110}
111
112class GrPipelineDynamicStateTestOp : public GrDrawOp {
113public:
114 DEFINE_OP_CLASS_ID
115
Robert Phillipsbe9aff22019-02-15 11:33:22 -0500116 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Chris Dalton916c4982018-08-15 00:53:25 -0600117 GrScissorTest scissorTest,
Robert Phillips88a32ef2018-06-07 11:05:56 -0400118 sk_sp<const GrBuffer> vbuff) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500119 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400120
Chris Dalton916c4982018-08-15 00:53:25 -0600121 return pool->allocate<GrPipelineDynamicStateTestOp>(scissorTest, std::move(vbuff));
Robert Phillips88a32ef2018-06-07 11:05:56 -0400122 }
123
124private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400125 friend class GrOpMemoryPool;
126
Chris Dalton916c4982018-08-15 00:53:25 -0600127 GrPipelineDynamicStateTestOp(GrScissorTest scissorTest, sk_sp<const GrBuffer> vbuff)
Brian Salomond818ebf2018-07-02 14:08:49 +0000128 : INHERITED(ClassID())
Chris Dalton916c4982018-08-15 00:53:25 -0600129 , fScissorTest(scissorTest)
Brian Salomond818ebf2018-07-02 14:08:49 +0000130 , fVertexBuffer(std::move(vbuff)) {
Chris Dalton46983b72017-06-06 12:27:16 -0600131 this->setBounds(SkRect::MakeIWH(kScreenSize, kScreenSize),
132 HasAABloat::kNo, IsZeroArea::kNo);
133 }
134
Chris Dalton46983b72017-06-06 12:27:16 -0600135 const char* name() const override { return "GrPipelineDynamicStateTestOp"; }
136 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Dalton6ce447a2019-06-23 18:07:38 -0600137 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
138 bool hasMixedSampledCoverage, GrClampType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700139 return GrProcessorSet::EmptySetAnalysis();
Brian Salomonf86d37b2017-06-16 10:04:34 -0400140 }
Chris Dalton46983b72017-06-06 12:27:16 -0600141 void onPrepare(GrOpFlushState*) override {}
Brian Salomon588cec72018-11-14 13:56:37 -0500142 void onExecute(GrOpFlushState* state, const SkRect& chainBounds) override {
Greg Daniel2c3398d2019-06-19 11:58:01 -0400143 GrPipeline pipeline(fScissorTest, SkBlendMode::kSrc, state->drawOpArgs().fOutputSwizzle);
Chris Dalton46983b72017-06-06 12:27:16 -0600144 SkSTArray<kNumMeshes, GrMesh> meshes;
145 for (int i = 0; i < kNumMeshes; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600146 GrMesh& mesh = meshes.emplace_back(GrPrimitiveType::kTriangleStrip);
Chris Dalton46983b72017-06-06 12:27:16 -0600147 mesh.setNonIndexedNonInstanced(4);
Brian Salomon12d22642019-01-29 14:38:50 -0500148 mesh.setVertexData(fVertexBuffer, 4 * i);
Chris Dalton46983b72017-06-06 12:27:16 -0600149 }
Brian Salomon49348902018-06-26 09:12:38 -0400150 GrPipeline::DynamicStateArrays dynamicState;
151 dynamicState.fScissorRects = kDynamicScissors;
152 state->rtCommandBuffer()->draw(GrPipelineDynamicStateTestProcessor(), pipeline, nullptr,
153 &dynamicState, meshes.begin(), 4,
Greg Daniel500d58b2017-08-24 15:59:33 -0400154 SkRect::MakeIWH(kScreenSize, kScreenSize));
Chris Dalton46983b72017-06-06 12:27:16 -0600155 }
156
Chris Dalton916c4982018-08-15 00:53:25 -0600157 GrScissorTest fScissorTest;
Chris Dalton46983b72017-06-06 12:27:16 -0600158 const sk_sp<const GrBuffer> fVertexBuffer;
159
160 typedef GrDrawOp INHERITED;
161};
162
163DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrPipelineDynamicStateTest, reporter, ctxInfo) {
Robert Phillips88a32ef2018-06-07 11:05:56 -0400164 GrContext* context = ctxInfo.grContext();
Robert Phillips9da87e02019-02-04 13:26:26 -0500165 GrResourceProvider* rp = context->priv().resourceProvider();
Chris Dalton46983b72017-06-06 12:27:16 -0600166
Robert Phillips9da87e02019-02-04 13:26:26 -0500167 sk_sp<GrRenderTargetContext> rtc(context->priv().makeDeferredRenderTargetContext(
Brian Salomon27ae52c2019-07-03 11:27:44 -0400168 SkBackingFit::kExact, kScreenSize, kScreenSize, GrColorType::kRGBA_8888, nullptr));
Chris Dalton46983b72017-06-06 12:27:16 -0600169 if (!rtc) {
170 ERRORF(reporter, "could not create render target context.");
171 return;
172 }
173
174 constexpr float d = (float) kScreenSize;
175 Vertex vdata[kNumMeshes * 4] = {
176 {0, 0, kMeshColors[0]},
177 {0, d, kMeshColors[0]},
178 {d, 0, kMeshColors[0]},
179 {d, d, kMeshColors[0]},
180
181 {0, 0, kMeshColors[1]},
182 {0, d, kMeshColors[1]},
183 {d, 0, kMeshColors[1]},
184 {d, d, kMeshColors[1]},
185
186 {0, 0, kMeshColors[2]},
187 {0, d, kMeshColors[2]},
188 {d, 0, kMeshColors[2]},
189 {d, d, kMeshColors[2]},
190
191 {0, 0, kMeshColors[3]},
192 {0, d, kMeshColors[3]},
193 {d, 0, kMeshColors[3]},
194 {d, d, kMeshColors[3]}
195 };
196
Brian Salomondbf70722019-02-07 11:31:24 -0500197 sk_sp<const GrBuffer> vbuff(rp->createBuffer(sizeof(vdata), GrGpuBufferType::kVertex,
198 kDynamic_GrAccessPattern, vdata));
Chris Dalton46983b72017-06-06 12:27:16 -0600199 if (!vbuff) {
200 ERRORF(reporter, "vbuff is null.");
201 return;
202 }
203
204 uint32_t resultPx[kScreenSize * kScreenSize];
205
Chris Dalton916c4982018-08-15 00:53:25 -0600206 for (GrScissorTest scissorTest : {GrScissorTest::kEnabled, GrScissorTest::kDisabled}) {
Brian Osman9a9baae2018-11-05 15:06:26 -0500207 rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xbaaaaaad),
208 GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton46983b72017-06-06 12:27:16 -0600209 rtc->priv().testingOnly_addDrawOp(
Chris Dalton916c4982018-08-15 00:53:25 -0600210 GrPipelineDynamicStateTestOp::Make(context, scissorTest, vbuff));
Chris Dalton46983b72017-06-06 12:27:16 -0600211 rtc->readPixels(SkImageInfo::Make(kScreenSize, kScreenSize,
212 kRGBA_8888_SkColorType, kPremul_SkAlphaType),
Brian Salomon1d435302019-07-01 13:05:28 -0400213 resultPx, 4 * kScreenSize, {0, 0});
Chris Dalton46983b72017-06-06 12:27:16 -0600214 for (int y = 0; y < kScreenSize; ++y) {
215 for (int x = 0; x < kScreenSize; ++x) {
216 int expectedColorIdx;
Chris Dalton916c4982018-08-15 00:53:25 -0600217 if (GrScissorTest::kEnabled == scissorTest) {
Chris Dalton46983b72017-06-06 12:27:16 -0600218 expectedColorIdx = (x < kScreenSplitX ? 0 : 2) + (y < kScreenSplitY ? 0 : 1);
219 } else {
220 expectedColorIdx = kNumMeshes - 1;
221 }
222 uint32_t expected = kMeshColors[expectedColorIdx];
223 uint32_t actual = resultPx[y * kScreenSize + x];
224 if (expected != actual) {
225 ERRORF(reporter, "[scissor=%s] pixel (%i,%i): got 0x%x expected 0x%x",
Chris Dalton916c4982018-08-15 00:53:25 -0600226 GrScissorTest::kEnabled == scissorTest ? "enabled" : "disabled", x, y,
Chris Dalton46983b72017-06-06 12:27:16 -0600227 actual, expected);
228 return;
229 }
230 }
231 }
232 }
233}