blob: a31b1f8b22eb68e8602187daa5c5cf9851d2bb8c [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
8#include "SkTypes.h"
9#include "Test.h"
10
Chris Dalton46983b72017-06-06 12:27:16 -060011#include "GrContext.h"
12#include "GrColor.h"
13#include "GrGeometryProcessor.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040014#include "GrGpuCommandBuffer.h"
Robert Phillips7c525e62018-06-12 10:11:12 -040015#include "GrMemoryPool.h"
Chris Dalton46983b72017-06-06 12:27:16 -060016#include "GrOpFlushState.h"
17#include "GrRenderTargetContext.h"
18#include "GrRenderTargetContextPriv.h"
19#include "GrResourceProvider.h"
20#include "SkMakeUnique.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060021#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton46983b72017-06-06 12:27:16 -060022#include "glsl/GrGLSLFragmentShaderBuilder.h"
23#include "glsl/GrGLSLGeometryProcessor.h"
24#include "glsl/GrGLSLVarying.h"
25
26/**
27 * This is a GPU-backend specific test for dynamic pipeline state. It draws boxes using dynamic
28 * scissor rectangles then reads back the result to verify a successful test.
29 */
30
Chris Dalton46983b72017-06-06 12:27:16 -060031static constexpr int kScreenSize = 6;
32static constexpr int kNumMeshes = 4;
33static constexpr int kScreenSplitX = kScreenSize/2;
34static constexpr int kScreenSplitY = kScreenSize/2;
35
Brian Salomon49348902018-06-26 09:12:38 -040036static const SkIRect kDynamicScissors[kNumMeshes] = {
37 SkIRect::MakeLTRB(0, 0, kScreenSplitX, kScreenSplitY),
38 SkIRect::MakeLTRB(0, kScreenSplitY, kScreenSplitX, kScreenSize),
39 SkIRect::MakeLTRB(kScreenSplitX, 0, kScreenSize, kScreenSplitY),
40 SkIRect::MakeLTRB(kScreenSplitX, kScreenSplitY, kScreenSize, kScreenSize),
Chris Dalton46983b72017-06-06 12:27:16 -060041};
42
43static const GrColor kMeshColors[kNumMeshes] {
44 GrColorPackRGBA(255, 0, 0, 255),
45 GrColorPackRGBA(0, 255, 0, 255),
46 GrColorPackRGBA(0, 0, 255, 255),
47 GrColorPackRGBA(0, 0, 0, 255)
48};
49
50struct Vertex {
51 float fX;
52 float fY;
53 GrColor fColor;
54};
55
56class GrPipelineDynamicStateTestProcessor : public GrGeometryProcessor {
57public:
58 GrPipelineDynamicStateTestProcessor()
Brian Salomon92be2f72018-06-19 14:33:47 -040059 : INHERITED(kGrPipelineDynamicStateTestProcessor_ClassID) {
60 this->setVertexAttributeCnt(2);
61 }
Chris Dalton46983b72017-06-06 12:27:16 -060062
63 const char* name() const override { return "GrPipelineDynamicStateTest Processor"; }
64
65 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const final {}
66
67 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
68
Brian Salomon92be2f72018-06-19 14:33:47 -040069private:
70 const Attribute& onVertexAttribute(int i) const override {
71 return IthAttribute(i, kVertex, kColor);
72 }
73
74 static constexpr Attribute kVertex = {"vertex", kHalf2_GrVertexAttribType};
75 static constexpr Attribute kColor = {"color", kUByte4_norm_GrVertexAttribType};
Chris Dalton46983b72017-06-06 12:27:16 -060076
77 friend class GLSLPipelineDynamicStateTestProcessor;
78 typedef GrGeometryProcessor INHERITED;
79};
Brian Salomon92be2f72018-06-19 14:33:47 -040080constexpr GrPrimitiveProcessor::Attribute GrPipelineDynamicStateTestProcessor::kVertex;
81constexpr GrPrimitiveProcessor::Attribute GrPipelineDynamicStateTestProcessor::kColor;
Chris Dalton46983b72017-06-06 12:27:16 -060082
83class GLSLPipelineDynamicStateTestProcessor : public GrGLSLGeometryProcessor {
84 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
85 FPCoordTransformIter&& transformIter) final {}
86
87 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
88 const GrPipelineDynamicStateTestProcessor& mp =
89 args.fGP.cast<GrPipelineDynamicStateTestProcessor>();
90
91 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
92 varyingHandler->emitAttributes(mp);
Brian Salomon92be2f72018-06-19 14:33:47 -040093 varyingHandler->addPassThroughAttribute(mp.kColor, args.fOutputColor);
Chris Dalton46983b72017-06-06 12:27:16 -060094
95 GrGLSLVertexBuilder* v = args.fVertBuilder;
Brian Salomon92be2f72018-06-19 14:33:47 -040096 v->codeAppendf("float2 vertex = %s;", mp.kVertex.name());
Ethan Nicholas8aa45692017-09-20 11:24:15 -040097 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
Chris Dalton46983b72017-06-06 12:27:16 -060098
Chris Dalton60283612018-02-14 13:38:14 -070099 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400100 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton46983b72017-06-06 12:27:16 -0600101 }
102};
103
104GrGLSLPrimitiveProcessor*
105GrPipelineDynamicStateTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
106 return new GLSLPipelineDynamicStateTestProcessor;
107}
108
109class GrPipelineDynamicStateTestOp : public GrDrawOp {
110public:
111 DEFINE_OP_CLASS_ID
112
Robert Phillips88a32ef2018-06-07 11:05:56 -0400113 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
Chris Dalton916c4982018-08-15 00:53:25 -0600114 GrScissorTest scissorTest,
Robert Phillips88a32ef2018-06-07 11:05:56 -0400115 sk_sp<const GrBuffer> vbuff) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400116 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
117
Chris Dalton916c4982018-08-15 00:53:25 -0600118 return pool->allocate<GrPipelineDynamicStateTestOp>(scissorTest, std::move(vbuff));
Robert Phillips88a32ef2018-06-07 11:05:56 -0400119 }
120
121private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400122 friend class GrOpMemoryPool;
123
Chris Dalton916c4982018-08-15 00:53:25 -0600124 GrPipelineDynamicStateTestOp(GrScissorTest scissorTest, sk_sp<const GrBuffer> vbuff)
Brian Salomond818ebf2018-07-02 14:08:49 +0000125 : INHERITED(ClassID())
Chris Dalton916c4982018-08-15 00:53:25 -0600126 , fScissorTest(scissorTest)
Brian Salomond818ebf2018-07-02 14:08:49 +0000127 , fVertexBuffer(std::move(vbuff)) {
Chris Dalton46983b72017-06-06 12:27:16 -0600128 this->setBounds(SkRect::MakeIWH(kScreenSize, kScreenSize),
129 HasAABloat::kNo, IsZeroArea::kNo);
130 }
131
Chris Dalton46983b72017-06-06 12:27:16 -0600132 const char* name() const override { return "GrPipelineDynamicStateTestOp"; }
133 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman532b3f92018-07-11 10:02:07 -0400134 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override {
Brian Salomonf86d37b2017-06-16 10:04:34 -0400135 return RequiresDstTexture::kNo;
136 }
Chris Dalton46983b72017-06-06 12:27:16 -0600137 void onPrepare(GrOpFlushState*) override {}
138 void onExecute(GrOpFlushState* state) override {
Robert Phillips2890fbf2017-07-26 15:48:41 -0400139 GrRenderTargetProxy* proxy = state->drawOpArgs().fProxy;
Chris Dalton916c4982018-08-15 00:53:25 -0600140 GrPipeline pipeline(proxy, fScissorTest, SkBlendMode::kSrc);
Chris Dalton46983b72017-06-06 12:27:16 -0600141 SkSTArray<kNumMeshes, GrMesh> meshes;
142 for (int i = 0; i < kNumMeshes; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600143 GrMesh& mesh = meshes.emplace_back(GrPrimitiveType::kTriangleStrip);
Chris Dalton46983b72017-06-06 12:27:16 -0600144 mesh.setNonIndexedNonInstanced(4);
145 mesh.setVertexData(fVertexBuffer.get(), 4 * i);
146 }
Brian Salomon49348902018-06-26 09:12:38 -0400147 GrPipeline::DynamicStateArrays dynamicState;
148 dynamicState.fScissorRects = kDynamicScissors;
149 state->rtCommandBuffer()->draw(GrPipelineDynamicStateTestProcessor(), pipeline, nullptr,
150 &dynamicState, meshes.begin(), 4,
Greg Daniel500d58b2017-08-24 15:59:33 -0400151 SkRect::MakeIWH(kScreenSize, kScreenSize));
Chris Dalton46983b72017-06-06 12:27:16 -0600152 }
153
Chris Dalton916c4982018-08-15 00:53:25 -0600154 GrScissorTest fScissorTest;
Chris Dalton46983b72017-06-06 12:27:16 -0600155 const sk_sp<const GrBuffer> fVertexBuffer;
156
157 typedef GrDrawOp INHERITED;
158};
159
160DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrPipelineDynamicStateTest, reporter, ctxInfo) {
Robert Phillips88a32ef2018-06-07 11:05:56 -0400161 GrContext* context = ctxInfo.grContext();
Robert Phillips6be756b2018-01-16 15:07:54 -0500162 GrResourceProvider* rp = context->contextPriv().resourceProvider();
Chris Dalton46983b72017-06-06 12:27:16 -0600163
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500164 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
165 SkBackingFit::kExact, kScreenSize, kScreenSize,
Chris Dalton46983b72017-06-06 12:27:16 -0600166 kRGBA_8888_GrPixelConfig, nullptr));
167 if (!rtc) {
168 ERRORF(reporter, "could not create render target context.");
169 return;
170 }
171
172 constexpr float d = (float) kScreenSize;
173 Vertex vdata[kNumMeshes * 4] = {
174 {0, 0, kMeshColors[0]},
175 {0, d, kMeshColors[0]},
176 {d, 0, kMeshColors[0]},
177 {d, d, kMeshColors[0]},
178
179 {0, 0, kMeshColors[1]},
180 {0, d, kMeshColors[1]},
181 {d, 0, kMeshColors[1]},
182 {d, d, kMeshColors[1]},
183
184 {0, 0, kMeshColors[2]},
185 {0, d, kMeshColors[2]},
186 {d, 0, kMeshColors[2]},
187 {d, d, kMeshColors[2]},
188
189 {0, 0, kMeshColors[3]},
190 {0, d, kMeshColors[3]},
191 {d, 0, kMeshColors[3]},
192 {d, d, kMeshColors[3]}
193 };
194
195 sk_sp<const GrBuffer> vbuff(rp->createBuffer(sizeof(vdata), kVertex_GrBufferType,
196 kDynamic_GrAccessPattern,
197 GrResourceProvider::kNoPendingIO_Flag |
198 GrResourceProvider::kRequireGpuMemory_Flag,
199 vdata));
200 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}) {
Chris Dalton344e9032017-12-11 15:42:09 -0700208 rtc->clear(nullptr, 0xbaaaaaad, 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),
213 resultPx, 4 * kScreenSize, 0, 0, 0);
214 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}