blob: e612fb4b5f78d7b88ca840e0dfc2c008ca1b9628 [file] [log] [blame]
Chris Dalton114a3c02017-05-26 15:17:19 -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
Brian Salomonc7fe0f72018-05-11 10:14:21 -040011#include <array>
12#include <vector>
13#include "GrCaps.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060014#include "GrContext.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040015#include "GrContextPriv.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060016#include "GrGeometryProcessor.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040017#include "GrGpuCommandBuffer.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060018#include "GrOpFlushState.h"
19#include "GrRenderTargetContext.h"
20#include "GrRenderTargetContextPriv.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060021#include "GrResourceKey.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040022#include "GrResourceProvider.h"
Mike Reed75ae4212018-01-23 11:24:08 -050023#include "SkBitmap.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060024#include "SkMakeUnique.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060025#include "glsl/GrGLSLFragmentShaderBuilder.h"
26#include "glsl/GrGLSLGeometryProcessor.h"
27#include "glsl/GrGLSLVarying.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040028#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060029
30GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
31
32static constexpr int kBoxSize = 2;
33static constexpr int kBoxCountY = 8;
34static constexpr int kBoxCountX = 8;
35static constexpr int kBoxCount = kBoxCountY * kBoxCountX;
36
37static constexpr int kImageWidth = kBoxCountY * kBoxSize;
38static constexpr int kImageHeight = kBoxCountX * kBoxSize;
39
40static constexpr int kIndexPatternRepeatCount = 3;
41constexpr uint16_t kIndexPattern[6] = {0, 1, 2, 1, 2, 3};
42
43
44class DrawMeshHelper {
45public:
46 DrawMeshHelper(GrOpFlushState* state) : fState(state) {}
47
48 sk_sp<const GrBuffer> getIndexBuffer();
49
50 template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const SkTArray<T>& data) {
51 return this->makeVertexBuffer(data.begin(), data.count());
52 }
Chris Dalton1d616352017-05-31 12:51:23 -060053 template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const std::vector<T>& data) {
54 return this->makeVertexBuffer(data.data(), data.size());
55 }
Chris Dalton114a3c02017-05-26 15:17:19 -060056 template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const T* data, int count);
57
58 void drawMesh(const GrMesh& mesh);
59
60private:
61 GrOpFlushState* fState;
62};
63
64struct Box {
65 float fX, fY;
66 GrColor fColor;
67};
68
69////////////////////////////////////////////////////////////////////////////////////////////////////
70
71/**
72 * This is a GPU-backend specific test. It tries to test all possible usecases of GrMesh. The test
73 * works by drawing checkerboards of colored boxes, reading back the pixels, and comparing with
74 * expected results. The boxes are drawn on integer boundaries and the (opaque) colors are chosen
75 * from the set (r,g,b) = (0,255)^3, so the GPU renderings ought to produce exact matches.
76 */
77
Robert Phillips88a32ef2018-06-07 11:05:56 -040078static void run_test(GrContext* context, const char* testName, skiatest::Reporter*,
79 const sk_sp<GrRenderTargetContext>&, const SkBitmap& gold,
80 std::function<void(DrawMeshHelper*)> testFn);
Chris Dalton114a3c02017-05-26 15:17:19 -060081
82DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrMeshTest, reporter, ctxInfo) {
Robert Phillips88a32ef2018-06-07 11:05:56 -040083 GrContext* context = ctxInfo.grContext();
Chris Dalton114a3c02017-05-26 15:17:19 -060084
Robert Phillips0c4b7b12018-03-06 08:20:37 -050085 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
86 SkBackingFit::kExact, kImageWidth, kImageHeight,
Chris Dalton114a3c02017-05-26 15:17:19 -060087 kRGBA_8888_GrPixelConfig, nullptr));
88 if (!rtc) {
89 ERRORF(reporter, "could not create render target context.");
90 return;
91 }
92
93 SkTArray<Box> boxes;
94 SkTArray<std::array<Box, 4>> vertexData;
95 SkBitmap gold;
96
97 // ---- setup ----------
98
99 SkPaint paint;
100 paint.setBlendMode(SkBlendMode::kSrc);
101 gold.allocN32Pixels(kImageWidth, kImageHeight);
102
103 SkCanvas goldCanvas(gold);
104
105 for (int y = 0; y < kBoxCountY; ++y) {
106 for (int x = 0; x < kBoxCountX; ++x) {
107 int c = y + x;
108 int rgb[3] = {-(c & 1) & 0xff, -((c >> 1) & 1) & 0xff, -((c >> 2) & 1) & 0xff};
109
110 const Box box = boxes.push_back() = {
111 float(x * kBoxSize),
112 float(y * kBoxSize),
113 GrColorPackRGBA(rgb[0], rgb[1], rgb[2], 255)
114 };
115
116 std::array<Box, 4>& boxVertices = vertexData.push_back();
117 for (int i = 0; i < 4; ++i) {
118 boxVertices[i] = {
119 box.fX + (i/2) * kBoxSize,
120 box.fY + (i%2) * kBoxSize,
121 box.fColor
122 };
123 }
124
125 paint.setARGB(255, rgb[0], rgb[1], rgb[2]);
126 goldCanvas.drawRect(SkRect::MakeXYWH(box.fX, box.fY, kBoxSize, kBoxSize), paint);
127 }
128 }
129
130 goldCanvas.flush();
131
132 // ---- tests ----------
133
134#define VALIDATE(buff) \
135 if (!buff) { \
136 ERRORF(reporter, #buff " is null."); \
137 return; \
138 }
139
Robert Phillips88a32ef2018-06-07 11:05:56 -0400140 run_test(context, "setNonIndexedNonInstanced", reporter, rtc, gold,
141 [&](DrawMeshHelper* helper) {
142 SkTArray<Box> expandedVertexData;
143 for (int i = 0; i < kBoxCount; ++i) {
144 for (int j = 0; j < 6; ++j) {
145 expandedVertexData.push_back(vertexData[i][kIndexPattern[j]]);
146 }
147 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600148
Robert Phillips88a32ef2018-06-07 11:05:56 -0400149 // Draw boxes one line at a time to exercise base vertex.
150 auto vbuff = helper->makeVertexBuffer(expandedVertexData);
151 VALIDATE(vbuff);
152 for (int y = 0; y < kBoxCountY; ++y) {
153 GrMesh mesh(GrPrimitiveType::kTriangles);
154 mesh.setNonIndexedNonInstanced(kBoxCountX * 6);
155 mesh.setVertexData(vbuff.get(), y * kBoxCountX * 6);
156 helper->drawMesh(mesh);
157 }
158 });
Chris Dalton114a3c02017-05-26 15:17:19 -0600159
Robert Phillips88a32ef2018-06-07 11:05:56 -0400160 run_test(context, "setIndexed", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600161 auto ibuff = helper->getIndexBuffer();
162 VALIDATE(ibuff);
163 auto vbuff = helper->makeVertexBuffer(vertexData);
164 VALIDATE(vbuff);
165 int baseRepetition = 0;
166 int i = 0;
167
168 // Start at various repetitions within the patterned index buffer to exercise base index.
169 while (i < kBoxCount) {
170 GR_STATIC_ASSERT(kIndexPatternRepeatCount >= 3);
171 int repetitionCount = SkTMin(3 - baseRepetition, kBoxCount - i);
172
Chris Dalton3809bab2017-06-13 10:55:06 -0600173 GrMesh mesh(GrPrimitiveType::kTriangles);
Chris Dalton114a3c02017-05-26 15:17:19 -0600174 mesh.setIndexed(ibuff.get(), repetitionCount * 6, baseRepetition * 6,
Brian Salomon802cb312018-06-08 18:05:20 -0400175 baseRepetition * 4, (baseRepetition + repetitionCount) * 4 - 1,
176 GrPrimitiveRestart::kNo);
Chris Dalton114a3c02017-05-26 15:17:19 -0600177 mesh.setVertexData(vbuff.get(), (i - baseRepetition) * 4);
178 helper->drawMesh(mesh);
179
180 baseRepetition = (baseRepetition + 1) % 3;
181 i += repetitionCount;
182 }
183 });
184
Robert Phillips88a32ef2018-06-07 11:05:56 -0400185 run_test(context, "setIndexedPatterned", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600186 auto ibuff = helper->getIndexBuffer();
187 VALIDATE(ibuff);
188 auto vbuff = helper->makeVertexBuffer(vertexData);
189 VALIDATE(vbuff);
190
191 // Draw boxes one line at a time to exercise base vertex. setIndexedPatterned does not
192 // support a base index.
193 for (int y = 0; y < kBoxCountY; ++y) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600194 GrMesh mesh(GrPrimitiveType::kTriangles);
Chris Dalton114a3c02017-05-26 15:17:19 -0600195 mesh.setIndexedPatterned(ibuff.get(), 6, 4, kBoxCountX, kIndexPatternRepeatCount);
196 mesh.setVertexData(vbuff.get(), y * kBoxCountX * 4);
197 helper->drawMesh(mesh);
198 }
199 });
Chris Dalton1d616352017-05-31 12:51:23 -0600200
201 for (bool indexed : {false, true}) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400202 if (!context->contextPriv().caps()->instanceAttribSupport()) {
Chris Dalton1d616352017-05-31 12:51:23 -0600203 break;
204 }
205
Robert Phillips88a32ef2018-06-07 11:05:56 -0400206 run_test(context, indexed ? "setIndexedInstanced" : "setInstanced",
Chris Dalton1d616352017-05-31 12:51:23 -0600207 reporter, rtc, gold, [&](DrawMeshHelper* helper) {
208 auto idxbuff = indexed ? helper->getIndexBuffer() : nullptr;
209 auto instbuff = helper->makeVertexBuffer(boxes);
210 VALIDATE(instbuff);
211 auto vbuff = helper->makeVertexBuffer(std::vector<float>{0,0, 0,1, 1,0, 1,1});
212 VALIDATE(vbuff);
213 auto vbuff2 = helper->makeVertexBuffer( // for testing base vertex.
214 std::vector<float>{-1,-1, -1,-1, 0,0, 0,1, 1,0, 1,1});
215 VALIDATE(vbuff2);
216
217 // Draw boxes one line at a time to exercise base instance, base vertex, and null vertex
218 // buffer. setIndexedInstanced intentionally does not support a base index.
219 for (int y = 0; y < kBoxCountY; ++y) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600220 GrMesh mesh(indexed ? GrPrimitiveType::kTriangles
221 : GrPrimitiveType::kTriangleStrip);
Chris Dalton1d616352017-05-31 12:51:23 -0600222 if (indexed) {
223 VALIDATE(idxbuff);
Brian Salomon802cb312018-06-08 18:05:20 -0400224 mesh.setIndexedInstanced(idxbuff.get(), 6, instbuff.get(), kBoxCountX,
225 y * kBoxCountX, GrPrimitiveRestart::kNo);
Chris Dalton1d616352017-05-31 12:51:23 -0600226 } else {
227 mesh.setInstanced(instbuff.get(), kBoxCountX, y * kBoxCountX, 4);
228 }
229 switch (y % 3) {
230 case 0:
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400231 if (context->contextPriv().caps()->shaderCaps()->vertexIDSupport()) {
Chris Dalton1d616352017-05-31 12:51:23 -0600232 if (y % 2) {
233 // We don't need this call because it's the initial state of GrMesh.
234 mesh.setVertexData(nullptr);
235 }
236 break;
237 }
238 // Fallthru.
239 case 1:
240 mesh.setVertexData(vbuff.get());
241 break;
242 case 2:
243 mesh.setVertexData(vbuff2.get(), 2);
244 break;
245 }
246 helper->drawMesh(mesh);
247 }
248 });
249 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600250}
251
252////////////////////////////////////////////////////////////////////////////////////////////////////
253
254class GrMeshTestOp : public GrDrawOp {
255public:
256 DEFINE_OP_CLASS_ID
257
Robert Phillips88a32ef2018-06-07 11:05:56 -0400258 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
259 std::function<void(DrawMeshHelper*)> testFn) {
260 return std::unique_ptr<GrDrawOp>(new GrMeshTestOp(testFn));
261 }
262
263private:
Chris Dalton114a3c02017-05-26 15:17:19 -0600264 GrMeshTestOp(std::function<void(DrawMeshHelper*)> testFn)
265 : INHERITED(ClassID())
266 , fTestFn(testFn) {
267 this->setBounds(SkRect::MakeIWH(kImageWidth, kImageHeight),
268 HasAABloat::kNo, IsZeroArea::kNo);
269 }
270
Chris Dalton114a3c02017-05-26 15:17:19 -0600271 const char* name() const override { return "GrMeshTestOp"; }
272 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman9a725dd2017-09-20 09:53:22 -0400273 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,
274 GrPixelConfigIsClamped) override {
Brian Salomonf86d37b2017-06-16 10:04:34 -0400275 return RequiresDstTexture::kNo;
276 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600277 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
278 void onPrepare(GrOpFlushState*) override {}
279 void onExecute(GrOpFlushState* state) override {
280 DrawMeshHelper helper(state);
281 fTestFn(&helper);
282 }
283
284 std::function<void(DrawMeshHelper*)> fTestFn;
285
286 typedef GrDrawOp INHERITED;
287};
288
289class GrMeshTestProcessor : public GrGeometryProcessor {
290public:
Chris Dalton1d616352017-05-31 12:51:23 -0600291 GrMeshTestProcessor(bool instanced, bool hasVertexBuffer)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400292 : INHERITED(kGrMeshTestProcessor_ClassID)
293 , fInstanceLocation(nullptr)
Chris Dalton1d616352017-05-31 12:51:23 -0600294 , fVertex(nullptr)
295 , fColor(nullptr) {
296 if (instanced) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400297 fInstanceLocation = &this->addInstanceAttrib("location", kHalf2_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600298 if (hasVertexBuffer) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400299 fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600300 }
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400301 fColor = &this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600302 } else {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400303 fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType);
304 fColor = &this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600305 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600306 }
307
308 const char* name() const override { return "GrMeshTest Processor"; }
309
Chris Dalton1d616352017-05-31 12:51:23 -0600310 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
311 b->add32(SkToBool(fInstanceLocation));
312 b->add32(SkToBool(fVertex));
313 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600314
315 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
316
317protected:
Chris Dalton1d616352017-05-31 12:51:23 -0600318 const Attribute* fInstanceLocation;
319 const Attribute* fVertex;
320 const Attribute* fColor;
Chris Dalton114a3c02017-05-26 15:17:19 -0600321
322 friend class GLSLMeshTestProcessor;
323 typedef GrGeometryProcessor INHERITED;
324};
325
326class GLSLMeshTestProcessor : public GrGLSLGeometryProcessor {
327 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
328 FPCoordTransformIter&& transformIter) final {}
329
330 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
331 const GrMeshTestProcessor& mp = args.fGP.cast<GrMeshTestProcessor>();
332
333 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
334 varyingHandler->emitAttributes(mp);
Chris Dalton1d616352017-05-31 12:51:23 -0600335 varyingHandler->addPassThroughAttribute(mp.fColor, args.fOutputColor);
Chris Dalton114a3c02017-05-26 15:17:19 -0600336
337 GrGLSLVertexBuilder* v = args.fVertBuilder;
Chris Dalton1d616352017-05-31 12:51:23 -0600338 if (!mp.fInstanceLocation) {
Brian Salomon70132d02018-05-29 15:33:06 -0400339 v->codeAppendf("float2 vertex = %s;", mp.fVertex->name());
Chris Dalton1d616352017-05-31 12:51:23 -0600340 } else {
341 if (mp.fVertex) {
Brian Salomon70132d02018-05-29 15:33:06 -0400342 v->codeAppendf("float2 offset = %s;", mp.fVertex->name());
Chris Dalton1d616352017-05-31 12:51:23 -0600343 } else {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400344 v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);");
Chris Dalton1d616352017-05-31 12:51:23 -0600345 }
Brian Salomon70132d02018-05-29 15:33:06 -0400346 v->codeAppendf("float2 vertex = %s + offset * %i;", mp.fInstanceLocation->name(),
347 kBoxSize);
Chris Dalton1d616352017-05-31 12:51:23 -0600348 }
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400349 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
Chris Dalton114a3c02017-05-26 15:17:19 -0600350
Chris Dalton60283612018-02-14 13:38:14 -0700351 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400352 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton114a3c02017-05-26 15:17:19 -0600353 }
354};
355
356GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
357 return new GLSLMeshTestProcessor;
358}
359
360////////////////////////////////////////////////////////////////////////////////////////////////////
361
362template<typename T>
363sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count) {
364 return sk_sp<const GrBuffer>(
365 fState->resourceProvider()->createBuffer(
366 count * sizeof(T), kVertex_GrBufferType, kDynamic_GrAccessPattern,
367 GrResourceProvider::kNoPendingIO_Flag |
368 GrResourceProvider::kRequireGpuMemory_Flag, data));
369}
370
371sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() {
372 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400373 return fState->resourceProvider()->findOrCreatePatternedIndexBuffer(
374 kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey);
Chris Dalton114a3c02017-05-26 15:17:19 -0600375}
376
377void DrawMeshHelper::drawMesh(const GrMesh& mesh) {
Robert Phillips2890fbf2017-07-26 15:48:41 -0400378 GrRenderTargetProxy* proxy = fState->drawOpArgs().fProxy;
379 GrPipeline pipeline(proxy, GrPipeline::ScissorState::kDisabled, SkBlendMode::kSrc);
Chris Dalton1d616352017-05-31 12:51:23 -0600380 GrMeshTestProcessor mtp(mesh.isInstanced(), mesh.hasVertexData());
Greg Daniel500d58b2017-08-24 15:59:33 -0400381 fState->rtCommandBuffer()->draw(pipeline, mtp, &mesh, nullptr, 1,
382 SkRect::MakeIWH(kImageWidth, kImageHeight));
Chris Dalton114a3c02017-05-26 15:17:19 -0600383}
384
Robert Phillips88a32ef2018-06-07 11:05:56 -0400385static void run_test(GrContext* context, const char* testName, skiatest::Reporter* reporter,
Chris Dalton114a3c02017-05-26 15:17:19 -0600386 const sk_sp<GrRenderTargetContext>& rtc, const SkBitmap& gold,
387 std::function<void(DrawMeshHelper*)> testFn) {
388 const int w = gold.width(), h = gold.height(), rowBytes = gold.rowBytes();
389 const uint32_t* goldPx = reinterpret_cast<const uint32_t*>(gold.getPixels());
390 if (h != rtc->height() || w != rtc->width()) {
391 ERRORF(reporter, "[%s] expectation and rtc not compatible (?).", testName);
392 return;
393 }
394 if (sizeof(uint32_t) * kImageWidth != gold.rowBytes()) {
395 ERRORF(reporter, "unexpected row bytes in gold image.", testName);
396 return;
397 }
398
399 SkAutoSTMalloc<kImageHeight * kImageWidth, uint32_t> resultPx(h * rowBytes);
Chris Dalton344e9032017-12-11 15:42:09 -0700400 rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillips88a32ef2018-06-07 11:05:56 -0400401 rtc->priv().testingOnly_addDrawOp(GrMeshTestOp::Make(context, testFn));
Chris Dalton114a3c02017-05-26 15:17:19 -0600402 rtc->readPixels(gold.info(), resultPx, rowBytes, 0, 0, 0);
403 for (int y = 0; y < h; ++y) {
404 for (int x = 0; x < w; ++x) {
405 uint32_t expected = goldPx[y * kImageWidth + x];
406 uint32_t actual = resultPx[y * kImageWidth + x];
407 if (expected != actual) {
408 ERRORF(reporter, "[%s] pixel (%i,%i): got 0x%x expected 0x%x",
409 testName, x, y, actual, expected);
410 return;
411 }
412 }
413 }
414}