blob: fb3579cabe0f90a2766e5b74392960f489d52643 [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,
175 baseRepetition * 4, (baseRepetition + repetitionCount) * 4 - 1);
176 mesh.setVertexData(vbuff.get(), (i - baseRepetition) * 4);
177 helper->drawMesh(mesh);
178
179 baseRepetition = (baseRepetition + 1) % 3;
180 i += repetitionCount;
181 }
182 });
183
Robert Phillips88a32ef2018-06-07 11:05:56 -0400184 run_test(context, "setIndexedPatterned", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600185 auto ibuff = helper->getIndexBuffer();
186 VALIDATE(ibuff);
187 auto vbuff = helper->makeVertexBuffer(vertexData);
188 VALIDATE(vbuff);
189
190 // Draw boxes one line at a time to exercise base vertex. setIndexedPatterned does not
191 // support a base index.
192 for (int y = 0; y < kBoxCountY; ++y) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600193 GrMesh mesh(GrPrimitiveType::kTriangles);
Chris Dalton114a3c02017-05-26 15:17:19 -0600194 mesh.setIndexedPatterned(ibuff.get(), 6, 4, kBoxCountX, kIndexPatternRepeatCount);
195 mesh.setVertexData(vbuff.get(), y * kBoxCountX * 4);
196 helper->drawMesh(mesh);
197 }
198 });
Chris Dalton1d616352017-05-31 12:51:23 -0600199
200 for (bool indexed : {false, true}) {
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400201 if (!context->contextPriv().caps()->instanceAttribSupport()) {
Chris Dalton1d616352017-05-31 12:51:23 -0600202 break;
203 }
204
Robert Phillips88a32ef2018-06-07 11:05:56 -0400205 run_test(context, indexed ? "setIndexedInstanced" : "setInstanced",
Chris Dalton1d616352017-05-31 12:51:23 -0600206 reporter, rtc, gold, [&](DrawMeshHelper* helper) {
207 auto idxbuff = indexed ? helper->getIndexBuffer() : nullptr;
208 auto instbuff = helper->makeVertexBuffer(boxes);
209 VALIDATE(instbuff);
210 auto vbuff = helper->makeVertexBuffer(std::vector<float>{0,0, 0,1, 1,0, 1,1});
211 VALIDATE(vbuff);
212 auto vbuff2 = helper->makeVertexBuffer( // for testing base vertex.
213 std::vector<float>{-1,-1, -1,-1, 0,0, 0,1, 1,0, 1,1});
214 VALIDATE(vbuff2);
215
216 // Draw boxes one line at a time to exercise base instance, base vertex, and null vertex
217 // buffer. setIndexedInstanced intentionally does not support a base index.
218 for (int y = 0; y < kBoxCountY; ++y) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600219 GrMesh mesh(indexed ? GrPrimitiveType::kTriangles
220 : GrPrimitiveType::kTriangleStrip);
Chris Dalton1d616352017-05-31 12:51:23 -0600221 if (indexed) {
222 VALIDATE(idxbuff);
223 mesh.setIndexedInstanced(idxbuff.get(), 6,
224 instbuff.get(), kBoxCountX, y * kBoxCountX);
225 } else {
226 mesh.setInstanced(instbuff.get(), kBoxCountX, y * kBoxCountX, 4);
227 }
228 switch (y % 3) {
229 case 0:
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400230 if (context->contextPriv().caps()->shaderCaps()->vertexIDSupport()) {
Chris Dalton1d616352017-05-31 12:51:23 -0600231 if (y % 2) {
232 // We don't need this call because it's the initial state of GrMesh.
233 mesh.setVertexData(nullptr);
234 }
235 break;
236 }
237 // Fallthru.
238 case 1:
239 mesh.setVertexData(vbuff.get());
240 break;
241 case 2:
242 mesh.setVertexData(vbuff2.get(), 2);
243 break;
244 }
245 helper->drawMesh(mesh);
246 }
247 });
248 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600249}
250
251////////////////////////////////////////////////////////////////////////////////////////////////////
252
253class GrMeshTestOp : public GrDrawOp {
254public:
255 DEFINE_OP_CLASS_ID
256
Robert Phillips88a32ef2018-06-07 11:05:56 -0400257 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
258 std::function<void(DrawMeshHelper*)> testFn) {
259 return std::unique_ptr<GrDrawOp>(new GrMeshTestOp(testFn));
260 }
261
262private:
Chris Dalton114a3c02017-05-26 15:17:19 -0600263 GrMeshTestOp(std::function<void(DrawMeshHelper*)> testFn)
264 : INHERITED(ClassID())
265 , fTestFn(testFn) {
266 this->setBounds(SkRect::MakeIWH(kImageWidth, kImageHeight),
267 HasAABloat::kNo, IsZeroArea::kNo);
268 }
269
Chris Dalton114a3c02017-05-26 15:17:19 -0600270 const char* name() const override { return "GrMeshTestOp"; }
271 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman9a725dd2017-09-20 09:53:22 -0400272 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,
273 GrPixelConfigIsClamped) override {
Brian Salomonf86d37b2017-06-16 10:04:34 -0400274 return RequiresDstTexture::kNo;
275 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600276 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
277 void onPrepare(GrOpFlushState*) override {}
278 void onExecute(GrOpFlushState* state) override {
279 DrawMeshHelper helper(state);
280 fTestFn(&helper);
281 }
282
283 std::function<void(DrawMeshHelper*)> fTestFn;
284
285 typedef GrDrawOp INHERITED;
286};
287
288class GrMeshTestProcessor : public GrGeometryProcessor {
289public:
Chris Dalton1d616352017-05-31 12:51:23 -0600290 GrMeshTestProcessor(bool instanced, bool hasVertexBuffer)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400291 : INHERITED(kGrMeshTestProcessor_ClassID)
292 , fInstanceLocation(nullptr)
Chris Dalton1d616352017-05-31 12:51:23 -0600293 , fVertex(nullptr)
294 , fColor(nullptr) {
295 if (instanced) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400296 fInstanceLocation = &this->addInstanceAttrib("location", kHalf2_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600297 if (hasVertexBuffer) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400298 fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600299 }
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400300 fColor = &this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600301 } else {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400302 fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType);
303 fColor = &this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600304 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600305 }
306
307 const char* name() const override { return "GrMeshTest Processor"; }
308
Chris Dalton1d616352017-05-31 12:51:23 -0600309 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
310 b->add32(SkToBool(fInstanceLocation));
311 b->add32(SkToBool(fVertex));
312 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600313
314 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
315
316protected:
Chris Dalton1d616352017-05-31 12:51:23 -0600317 const Attribute* fInstanceLocation;
318 const Attribute* fVertex;
319 const Attribute* fColor;
Chris Dalton114a3c02017-05-26 15:17:19 -0600320
321 friend class GLSLMeshTestProcessor;
322 typedef GrGeometryProcessor INHERITED;
323};
324
325class GLSLMeshTestProcessor : public GrGLSLGeometryProcessor {
326 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
327 FPCoordTransformIter&& transformIter) final {}
328
329 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
330 const GrMeshTestProcessor& mp = args.fGP.cast<GrMeshTestProcessor>();
331
332 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
333 varyingHandler->emitAttributes(mp);
Chris Dalton1d616352017-05-31 12:51:23 -0600334 varyingHandler->addPassThroughAttribute(mp.fColor, args.fOutputColor);
Chris Dalton114a3c02017-05-26 15:17:19 -0600335
336 GrGLSLVertexBuilder* v = args.fVertBuilder;
Chris Dalton1d616352017-05-31 12:51:23 -0600337 if (!mp.fInstanceLocation) {
Brian Salomon70132d02018-05-29 15:33:06 -0400338 v->codeAppendf("float2 vertex = %s;", mp.fVertex->name());
Chris Dalton1d616352017-05-31 12:51:23 -0600339 } else {
340 if (mp.fVertex) {
Brian Salomon70132d02018-05-29 15:33:06 -0400341 v->codeAppendf("float2 offset = %s;", mp.fVertex->name());
Chris Dalton1d616352017-05-31 12:51:23 -0600342 } else {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400343 v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);");
Chris Dalton1d616352017-05-31 12:51:23 -0600344 }
Brian Salomon70132d02018-05-29 15:33:06 -0400345 v->codeAppendf("float2 vertex = %s + offset * %i;", mp.fInstanceLocation->name(),
346 kBoxSize);
Chris Dalton1d616352017-05-31 12:51:23 -0600347 }
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400348 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
Chris Dalton114a3c02017-05-26 15:17:19 -0600349
Chris Dalton60283612018-02-14 13:38:14 -0700350 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400351 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton114a3c02017-05-26 15:17:19 -0600352 }
353};
354
355GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
356 return new GLSLMeshTestProcessor;
357}
358
359////////////////////////////////////////////////////////////////////////////////////////////////////
360
361template<typename T>
362sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count) {
363 return sk_sp<const GrBuffer>(
364 fState->resourceProvider()->createBuffer(
365 count * sizeof(T), kVertex_GrBufferType, kDynamic_GrAccessPattern,
366 GrResourceProvider::kNoPendingIO_Flag |
367 GrResourceProvider::kRequireGpuMemory_Flag, data));
368}
369
370sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() {
371 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400372 return fState->resourceProvider()->findOrCreatePatternedIndexBuffer(
373 kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey);
Chris Dalton114a3c02017-05-26 15:17:19 -0600374}
375
376void DrawMeshHelper::drawMesh(const GrMesh& mesh) {
Robert Phillips2890fbf2017-07-26 15:48:41 -0400377 GrRenderTargetProxy* proxy = fState->drawOpArgs().fProxy;
378 GrPipeline pipeline(proxy, GrPipeline::ScissorState::kDisabled, SkBlendMode::kSrc);
Chris Dalton1d616352017-05-31 12:51:23 -0600379 GrMeshTestProcessor mtp(mesh.isInstanced(), mesh.hasVertexData());
Greg Daniel500d58b2017-08-24 15:59:33 -0400380 fState->rtCommandBuffer()->draw(pipeline, mtp, &mesh, nullptr, 1,
381 SkRect::MakeIWH(kImageWidth, kImageHeight));
Chris Dalton114a3c02017-05-26 15:17:19 -0600382}
383
Robert Phillips88a32ef2018-06-07 11:05:56 -0400384static void run_test(GrContext* context, const char* testName, skiatest::Reporter* reporter,
Chris Dalton114a3c02017-05-26 15:17:19 -0600385 const sk_sp<GrRenderTargetContext>& rtc, const SkBitmap& gold,
386 std::function<void(DrawMeshHelper*)> testFn) {
387 const int w = gold.width(), h = gold.height(), rowBytes = gold.rowBytes();
388 const uint32_t* goldPx = reinterpret_cast<const uint32_t*>(gold.getPixels());
389 if (h != rtc->height() || w != rtc->width()) {
390 ERRORF(reporter, "[%s] expectation and rtc not compatible (?).", testName);
391 return;
392 }
393 if (sizeof(uint32_t) * kImageWidth != gold.rowBytes()) {
394 ERRORF(reporter, "unexpected row bytes in gold image.", testName);
395 return;
396 }
397
398 SkAutoSTMalloc<kImageHeight * kImageWidth, uint32_t> resultPx(h * rowBytes);
Chris Dalton344e9032017-12-11 15:42:09 -0700399 rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillips88a32ef2018-06-07 11:05:56 -0400400 rtc->priv().testingOnly_addDrawOp(GrMeshTestOp::Make(context, testFn));
Chris Dalton114a3c02017-05-26 15:17:19 -0600401 rtc->readPixels(gold.info(), resultPx, rowBytes, 0, 0, 0);
402 for (int y = 0; y < h; ++y) {
403 for (int x = 0; x < w; ++x) {
404 uint32_t expected = goldPx[y * kImageWidth + x];
405 uint32_t actual = resultPx[y * kImageWidth + x];
406 if (expected != actual) {
407 ERRORF(reporter, "[%s] pixel (%i,%i): got 0x%x expected 0x%x",
408 testName, x, y, actual, expected);
409 return;
410 }
411 }
412 }
413}