blob: 3840dc67245d35a6f1bb37609b20e78bdcc0cf4e [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
Brian Osmand4c29702018-09-14 16:16:55 -040074 static constexpr Attribute kVertex =
75 {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType};
76 static constexpr Attribute kColor =
77 {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
Chris Dalton46983b72017-06-06 12:27:16 -060078
79 friend class GLSLPipelineDynamicStateTestProcessor;
80 typedef GrGeometryProcessor INHERITED;
81};
Brian Salomon92be2f72018-06-19 14:33:47 -040082constexpr GrPrimitiveProcessor::Attribute GrPipelineDynamicStateTestProcessor::kVertex;
83constexpr GrPrimitiveProcessor::Attribute GrPipelineDynamicStateTestProcessor::kColor;
Chris Dalton46983b72017-06-06 12:27:16 -060084
85class GLSLPipelineDynamicStateTestProcessor : public GrGLSLGeometryProcessor {
86 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
87 FPCoordTransformIter&& transformIter) final {}
88
89 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
90 const GrPipelineDynamicStateTestProcessor& mp =
91 args.fGP.cast<GrPipelineDynamicStateTestProcessor>();
92
93 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
94 varyingHandler->emitAttributes(mp);
Brian Salomon92be2f72018-06-19 14:33:47 -040095 varyingHandler->addPassThroughAttribute(mp.kColor, args.fOutputColor);
Chris Dalton46983b72017-06-06 12:27:16 -060096
97 GrGLSLVertexBuilder* v = args.fVertBuilder;
Brian Salomon92be2f72018-06-19 14:33:47 -040098 v->codeAppendf("float2 vertex = %s;", mp.kVertex.name());
Ethan Nicholas8aa45692017-09-20 11:24:15 -040099 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
Chris Dalton46983b72017-06-06 12:27:16 -0600100
Chris Dalton60283612018-02-14 13:38:14 -0700101 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400102 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton46983b72017-06-06 12:27:16 -0600103 }
104};
105
106GrGLSLPrimitiveProcessor*
107GrPipelineDynamicStateTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
108 return new GLSLPipelineDynamicStateTestProcessor;
109}
110
111class GrPipelineDynamicStateTestOp : public GrDrawOp {
112public:
113 DEFINE_OP_CLASS_ID
114
Robert Phillips88a32ef2018-06-07 11:05:56 -0400115 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
Chris Dalton916c4982018-08-15 00:53:25 -0600116 GrScissorTest scissorTest,
Robert Phillips88a32ef2018-06-07 11:05:56 -0400117 sk_sp<const GrBuffer> vbuff) {
Robert Phillipsc994a932018-06-19 13:09:54 -0400118 GrOpMemoryPool* pool = context->contextPriv().opMemoryPool();
119
Chris Dalton916c4982018-08-15 00:53:25 -0600120 return pool->allocate<GrPipelineDynamicStateTestOp>(scissorTest, std::move(vbuff));
Robert Phillips88a32ef2018-06-07 11:05:56 -0400121 }
122
123private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400124 friend class GrOpMemoryPool;
125
Chris Dalton916c4982018-08-15 00:53:25 -0600126 GrPipelineDynamicStateTestOp(GrScissorTest scissorTest, sk_sp<const GrBuffer> vbuff)
Brian Salomond818ebf2018-07-02 14:08:49 +0000127 : INHERITED(ClassID())
Chris Dalton916c4982018-08-15 00:53:25 -0600128 , fScissorTest(scissorTest)
Brian Salomond818ebf2018-07-02 14:08:49 +0000129 , fVertexBuffer(std::move(vbuff)) {
Chris Dalton46983b72017-06-06 12:27:16 -0600130 this->setBounds(SkRect::MakeIWH(kScreenSize, kScreenSize),
131 HasAABloat::kNo, IsZeroArea::kNo);
132 }
133
Chris Dalton46983b72017-06-06 12:27:16 -0600134 const char* name() const override { return "GrPipelineDynamicStateTestOp"; }
135 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman532b3f92018-07-11 10:02:07 -0400136 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*) override {
Brian Salomonf86d37b2017-06-16 10:04:34 -0400137 return RequiresDstTexture::kNo;
138 }
Chris Dalton46983b72017-06-06 12:27:16 -0600139 void onPrepare(GrOpFlushState*) override {}
140 void onExecute(GrOpFlushState* state) override {
Robert Phillips2890fbf2017-07-26 15:48:41 -0400141 GrRenderTargetProxy* proxy = state->drawOpArgs().fProxy;
Chris Dalton916c4982018-08-15 00:53:25 -0600142 GrPipeline pipeline(proxy, fScissorTest, SkBlendMode::kSrc);
Chris Dalton46983b72017-06-06 12:27:16 -0600143 SkSTArray<kNumMeshes, GrMesh> meshes;
144 for (int i = 0; i < kNumMeshes; ++i) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600145 GrMesh& mesh = meshes.emplace_back(GrPrimitiveType::kTriangleStrip);
Chris Dalton46983b72017-06-06 12:27:16 -0600146 mesh.setNonIndexedNonInstanced(4);
147 mesh.setVertexData(fVertexBuffer.get(), 4 * i);
148 }
Brian Salomon49348902018-06-26 09:12:38 -0400149 GrPipeline::DynamicStateArrays dynamicState;
150 dynamicState.fScissorRects = kDynamicScissors;
151 state->rtCommandBuffer()->draw(GrPipelineDynamicStateTestProcessor(), pipeline, nullptr,
152 &dynamicState, meshes.begin(), 4,
Greg Daniel500d58b2017-08-24 15:59:33 -0400153 SkRect::MakeIWH(kScreenSize, kScreenSize));
Chris Dalton46983b72017-06-06 12:27:16 -0600154 }
155
Chris Dalton916c4982018-08-15 00:53:25 -0600156 GrScissorTest fScissorTest;
Chris Dalton46983b72017-06-06 12:27:16 -0600157 const sk_sp<const GrBuffer> fVertexBuffer;
158
159 typedef GrDrawOp INHERITED;
160};
161
162DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrPipelineDynamicStateTest, reporter, ctxInfo) {
Robert Phillips88a32ef2018-06-07 11:05:56 -0400163 GrContext* context = ctxInfo.grContext();
Robert Phillips6be756b2018-01-16 15:07:54 -0500164 GrResourceProvider* rp = context->contextPriv().resourceProvider();
Chris Dalton46983b72017-06-06 12:27:16 -0600165
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500166 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
167 SkBackingFit::kExact, kScreenSize, kScreenSize,
Chris Dalton46983b72017-06-06 12:27:16 -0600168 kRGBA_8888_GrPixelConfig, nullptr));
169 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
197 sk_sp<const GrBuffer> vbuff(rp->createBuffer(sizeof(vdata), kVertex_GrBufferType,
198 kDynamic_GrAccessPattern,
Chris Daltond004e0b2018-09-27 09:28:03 -0600199 GrResourceProvider::Flags::kNoPendingIO |
200 GrResourceProvider::Flags::kRequireGpuMemory,
Chris Dalton46983b72017-06-06 12:27:16 -0600201 vdata));
202 if (!vbuff) {
203 ERRORF(reporter, "vbuff is null.");
204 return;
205 }
206
207 uint32_t resultPx[kScreenSize * kScreenSize];
208
Chris Dalton916c4982018-08-15 00:53:25 -0600209 for (GrScissorTest scissorTest : {GrScissorTest::kEnabled, GrScissorTest::kDisabled}) {
Chris Dalton344e9032017-12-11 15:42:09 -0700210 rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton46983b72017-06-06 12:27:16 -0600211 rtc->priv().testingOnly_addDrawOp(
Chris Dalton916c4982018-08-15 00:53:25 -0600212 GrPipelineDynamicStateTestOp::Make(context, scissorTest, vbuff));
Chris Dalton46983b72017-06-06 12:27:16 -0600213 rtc->readPixels(SkImageInfo::Make(kScreenSize, kScreenSize,
214 kRGBA_8888_SkColorType, kPremul_SkAlphaType),
215 resultPx, 4 * kScreenSize, 0, 0, 0);
216 for (int y = 0; y < kScreenSize; ++y) {
217 for (int x = 0; x < kScreenSize; ++x) {
218 int expectedColorIdx;
Chris Dalton916c4982018-08-15 00:53:25 -0600219 if (GrScissorTest::kEnabled == scissorTest) {
Chris Dalton46983b72017-06-06 12:27:16 -0600220 expectedColorIdx = (x < kScreenSplitX ? 0 : 2) + (y < kScreenSplitY ? 0 : 1);
221 } else {
222 expectedColorIdx = kNumMeshes - 1;
223 }
224 uint32_t expected = kMeshColors[expectedColorIdx];
225 uint32_t actual = resultPx[y * kScreenSize + x];
226 if (expected != actual) {
227 ERRORF(reporter, "[scissor=%s] pixel (%i,%i): got 0x%x expected 0x%x",
Chris Dalton916c4982018-08-15 00:53:25 -0600228 GrScissorTest::kEnabled == scissorTest ? "enabled" : "disabled", x, y,
Chris Dalton46983b72017-06-06 12:27:16 -0600229 actual, expected);
230 return;
231 }
232 }
233 }
234 }
235}