blob: 8f1f8f85f7324348bbadfd310d36612bddddef9b [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"
Robert Phillips7c525e62018-06-12 10:11:12 -040018#include "GrMemoryPool.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060019#include "GrOpFlushState.h"
20#include "GrRenderTargetContext.h"
21#include "GrRenderTargetContextPriv.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060022#include "GrResourceKey.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040023#include "GrResourceProvider.h"
Mike Reed75ae4212018-01-23 11:24:08 -050024#include "SkBitmap.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060025#include "SkMakeUnique.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060026#include "glsl/GrGLSLFragmentShaderBuilder.h"
27#include "glsl/GrGLSLGeometryProcessor.h"
28#include "glsl/GrGLSLVarying.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040029#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060030
31GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
32
33static constexpr int kBoxSize = 2;
34static constexpr int kBoxCountY = 8;
35static constexpr int kBoxCountX = 8;
36static constexpr int kBoxCount = kBoxCountY * kBoxCountX;
37
38static constexpr int kImageWidth = kBoxCountY * kBoxSize;
39static constexpr int kImageHeight = kBoxCountX * kBoxSize;
40
41static constexpr int kIndexPatternRepeatCount = 3;
42constexpr uint16_t kIndexPattern[6] = {0, 1, 2, 1, 2, 3};
43
44
45class DrawMeshHelper {
46public:
47 DrawMeshHelper(GrOpFlushState* state) : fState(state) {}
48
49 sk_sp<const GrBuffer> getIndexBuffer();
50
51 template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const SkTArray<T>& data) {
52 return this->makeVertexBuffer(data.begin(), data.count());
53 }
Chris Dalton1d616352017-05-31 12:51:23 -060054 template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const std::vector<T>& data) {
55 return this->makeVertexBuffer(data.data(), data.size());
56 }
Chris Dalton114a3c02017-05-26 15:17:19 -060057 template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const T* data, int count);
58
59 void drawMesh(const GrMesh& mesh);
60
61private:
62 GrOpFlushState* fState;
63};
64
65struct Box {
66 float fX, fY;
67 GrColor fColor;
68};
69
70////////////////////////////////////////////////////////////////////////////////////////////////////
71
72/**
73 * This is a GPU-backend specific test. It tries to test all possible usecases of GrMesh. The test
74 * works by drawing checkerboards of colored boxes, reading back the pixels, and comparing with
75 * expected results. The boxes are drawn on integer boundaries and the (opaque) colors are chosen
76 * from the set (r,g,b) = (0,255)^3, so the GPU renderings ought to produce exact matches.
77 */
78
Robert Phillips88a32ef2018-06-07 11:05:56 -040079static void run_test(GrContext* context, const char* testName, skiatest::Reporter*,
80 const sk_sp<GrRenderTargetContext>&, const SkBitmap& gold,
81 std::function<void(DrawMeshHelper*)> testFn);
Chris Dalton114a3c02017-05-26 15:17:19 -060082
83DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrMeshTest, reporter, ctxInfo) {
Robert Phillips88a32ef2018-06-07 11:05:56 -040084 GrContext* context = ctxInfo.grContext();
Chris Dalton114a3c02017-05-26 15:17:19 -060085
Greg Daniel4065d452018-11-16 15:43:41 -050086 const GrBackendFormat format =
Robert Phillips9da87e02019-02-04 13:26:26 -050087 context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -050088
Robert Phillips9da87e02019-02-04 13:26:26 -050089 sk_sp<GrRenderTargetContext> rtc(context->priv().makeDeferredRenderTargetContext(
Brian Salomon23356442018-11-30 15:33:19 -050090 format, SkBackingFit::kExact, kImageWidth, kImageHeight, kRGBA_8888_GrPixelConfig,
91 nullptr));
Chris Dalton114a3c02017-05-26 15:17:19 -060092 if (!rtc) {
93 ERRORF(reporter, "could not create render target context.");
94 return;
95 }
96
97 SkTArray<Box> boxes;
98 SkTArray<std::array<Box, 4>> vertexData;
99 SkBitmap gold;
100
101 // ---- setup ----------
102
103 SkPaint paint;
104 paint.setBlendMode(SkBlendMode::kSrc);
105 gold.allocN32Pixels(kImageWidth, kImageHeight);
106
107 SkCanvas goldCanvas(gold);
108
109 for (int y = 0; y < kBoxCountY; ++y) {
110 for (int x = 0; x < kBoxCountX; ++x) {
111 int c = y + x;
112 int rgb[3] = {-(c & 1) & 0xff, -((c >> 1) & 1) & 0xff, -((c >> 2) & 1) & 0xff};
113
114 const Box box = boxes.push_back() = {
Brian Salomon23356442018-11-30 15:33:19 -0500115 float(x * kBoxSize),
116 float(y * kBoxSize),
117 GrColorPackRGBA(rgb[0], rgb[1], rgb[2], 255)
Chris Dalton114a3c02017-05-26 15:17:19 -0600118 };
119
120 std::array<Box, 4>& boxVertices = vertexData.push_back();
121 for (int i = 0; i < 4; ++i) {
122 boxVertices[i] = {
Brian Salomon23356442018-11-30 15:33:19 -0500123 box.fX + (i / 2) * kBoxSize,
124 box.fY + (i % 2) * kBoxSize,
125 box.fColor
Chris Dalton114a3c02017-05-26 15:17:19 -0600126 };
127 }
128
129 paint.setARGB(255, rgb[0], rgb[1], rgb[2]);
130 goldCanvas.drawRect(SkRect::MakeXYWH(box.fX, box.fY, kBoxSize, kBoxSize), paint);
131 }
132 }
133
Chris Dalton114a3c02017-05-26 15:17:19 -0600134 // ---- tests ----------
135
Brian Salomon23356442018-11-30 15:33:19 -0500136#define VALIDATE(buff) \
137 do { \
138 if (!buff) { \
139 ERRORF(reporter, #buff " is null."); \
140 return; \
141 } \
142 } while (0)
Chris Dalton114a3c02017-05-26 15:17:19 -0600143
Robert Phillips88a32ef2018-06-07 11:05:56 -0400144 run_test(context, "setNonIndexedNonInstanced", reporter, rtc, gold,
145 [&](DrawMeshHelper* helper) {
146 SkTArray<Box> expandedVertexData;
147 for (int i = 0; i < kBoxCount; ++i) {
148 for (int j = 0; j < 6; ++j) {
149 expandedVertexData.push_back(vertexData[i][kIndexPattern[j]]);
150 }
151 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600152
Robert Phillips88a32ef2018-06-07 11:05:56 -0400153 // Draw boxes one line at a time to exercise base vertex.
154 auto vbuff = helper->makeVertexBuffer(expandedVertexData);
155 VALIDATE(vbuff);
156 for (int y = 0; y < kBoxCountY; ++y) {
157 GrMesh mesh(GrPrimitiveType::kTriangles);
158 mesh.setNonIndexedNonInstanced(kBoxCountX * 6);
Brian Salomon12d22642019-01-29 14:38:50 -0500159 mesh.setVertexData(vbuff, y * kBoxCountX * 6);
Robert Phillips88a32ef2018-06-07 11:05:56 -0400160 helper->drawMesh(mesh);
161 }
162 });
Chris Dalton114a3c02017-05-26 15:17:19 -0600163
Robert Phillips88a32ef2018-06-07 11:05:56 -0400164 run_test(context, "setIndexed", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600165 auto ibuff = helper->getIndexBuffer();
166 VALIDATE(ibuff);
167 auto vbuff = helper->makeVertexBuffer(vertexData);
168 VALIDATE(vbuff);
169 int baseRepetition = 0;
170 int i = 0;
171
172 // Start at various repetitions within the patterned index buffer to exercise base index.
173 while (i < kBoxCount) {
174 GR_STATIC_ASSERT(kIndexPatternRepeatCount >= 3);
175 int repetitionCount = SkTMin(3 - baseRepetition, kBoxCount - i);
176
Chris Dalton3809bab2017-06-13 10:55:06 -0600177 GrMesh mesh(GrPrimitiveType::kTriangles);
Brian Salomon12d22642019-01-29 14:38:50 -0500178 mesh.setIndexed(ibuff, repetitionCount * 6, baseRepetition * 6, baseRepetition * 4,
179 (baseRepetition + repetitionCount) * 4 - 1, GrPrimitiveRestart::kNo);
180 mesh.setVertexData(vbuff, (i - baseRepetition) * 4);
Chris Dalton114a3c02017-05-26 15:17:19 -0600181 helper->drawMesh(mesh);
182
183 baseRepetition = (baseRepetition + 1) % 3;
184 i += repetitionCount;
185 }
186 });
187
Robert Phillips88a32ef2018-06-07 11:05:56 -0400188 run_test(context, "setIndexedPatterned", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
Chris Dalton114a3c02017-05-26 15:17:19 -0600189 auto ibuff = helper->getIndexBuffer();
190 VALIDATE(ibuff);
191 auto vbuff = helper->makeVertexBuffer(vertexData);
192 VALIDATE(vbuff);
193
194 // Draw boxes one line at a time to exercise base vertex. setIndexedPatterned does not
195 // support a base index.
196 for (int y = 0; y < kBoxCountY; ++y) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600197 GrMesh mesh(GrPrimitiveType::kTriangles);
Brian Salomon12d22642019-01-29 14:38:50 -0500198 mesh.setIndexedPatterned(ibuff, 6, 4, kBoxCountX, kIndexPatternRepeatCount);
199 mesh.setVertexData(vbuff, y * kBoxCountX * 4);
Chris Dalton114a3c02017-05-26 15:17:19 -0600200 helper->drawMesh(mesh);
201 }
202 });
Chris Dalton1d616352017-05-31 12:51:23 -0600203
204 for (bool indexed : {false, true}) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500205 if (!context->priv().caps()->instanceAttribSupport()) {
Chris Dalton1d616352017-05-31 12:51:23 -0600206 break;
207 }
208
Robert Phillips88a32ef2018-06-07 11:05:56 -0400209 run_test(context, indexed ? "setIndexedInstanced" : "setInstanced",
Chris Dalton1d616352017-05-31 12:51:23 -0600210 reporter, rtc, gold, [&](DrawMeshHelper* helper) {
211 auto idxbuff = indexed ? helper->getIndexBuffer() : nullptr;
212 auto instbuff = helper->makeVertexBuffer(boxes);
213 VALIDATE(instbuff);
214 auto vbuff = helper->makeVertexBuffer(std::vector<float>{0,0, 0,1, 1,0, 1,1});
215 VALIDATE(vbuff);
216 auto vbuff2 = helper->makeVertexBuffer( // for testing base vertex.
217 std::vector<float>{-1,-1, -1,-1, 0,0, 0,1, 1,0, 1,1});
218 VALIDATE(vbuff2);
219
220 // Draw boxes one line at a time to exercise base instance, base vertex, and null vertex
221 // buffer. setIndexedInstanced intentionally does not support a base index.
222 for (int y = 0; y < kBoxCountY; ++y) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600223 GrMesh mesh(indexed ? GrPrimitiveType::kTriangles
224 : GrPrimitiveType::kTriangleStrip);
Chris Dalton1d616352017-05-31 12:51:23 -0600225 if (indexed) {
226 VALIDATE(idxbuff);
Brian Salomon12d22642019-01-29 14:38:50 -0500227 mesh.setIndexedInstanced(idxbuff, 6, instbuff, kBoxCountX, y * kBoxCountX,
228 GrPrimitiveRestart::kNo);
Chris Dalton1d616352017-05-31 12:51:23 -0600229 } else {
Brian Salomon12d22642019-01-29 14:38:50 -0500230 mesh.setInstanced(instbuff, kBoxCountX, y * kBoxCountX, 4);
Chris Dalton1d616352017-05-31 12:51:23 -0600231 }
232 switch (y % 3) {
233 case 0:
Robert Phillips9da87e02019-02-04 13:26:26 -0500234 if (context->priv().caps()->shaderCaps()->vertexIDSupport()) {
Chris Dalton1d616352017-05-31 12:51:23 -0600235 if (y % 2) {
236 // We don't need this call because it's the initial state of GrMesh.
237 mesh.setVertexData(nullptr);
238 }
239 break;
240 }
241 // Fallthru.
242 case 1:
Brian Salomon12d22642019-01-29 14:38:50 -0500243 mesh.setVertexData(vbuff);
Chris Dalton1d616352017-05-31 12:51:23 -0600244 break;
245 case 2:
Brian Salomon12d22642019-01-29 14:38:50 -0500246 mesh.setVertexData(vbuff2, 2);
Chris Dalton1d616352017-05-31 12:51:23 -0600247 break;
248 }
249 helper->drawMesh(mesh);
250 }
251 });
252 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600253}
254
255////////////////////////////////////////////////////////////////////////////////////////////////////
256
257class GrMeshTestOp : public GrDrawOp {
258public:
259 DEFINE_OP_CLASS_ID
260
Robert Phillips88a32ef2018-06-07 11:05:56 -0400261 static std::unique_ptr<GrDrawOp> Make(GrContext* context,
262 std::function<void(DrawMeshHelper*)> testFn) {
Robert Phillips9da87e02019-02-04 13:26:26 -0500263 GrOpMemoryPool* pool = context->priv().opMemoryPool();
Robert Phillipsc994a932018-06-19 13:09:54 -0400264
265 return pool->allocate<GrMeshTestOp>(testFn);
Robert Phillips88a32ef2018-06-07 11:05:56 -0400266 }
267
268private:
Robert Phillips7c525e62018-06-12 10:11:12 -0400269 friend class GrOpMemoryPool; // for ctor
270
Chris Dalton114a3c02017-05-26 15:17:19 -0600271 GrMeshTestOp(std::function<void(DrawMeshHelper*)> testFn)
272 : INHERITED(ClassID())
273 , fTestFn(testFn) {
274 this->setBounds(SkRect::MakeIWH(kImageWidth, kImageHeight),
275 HasAABloat::kNo, IsZeroArea::kNo);
276 }
277
Chris Dalton114a3c02017-05-26 15:17:19 -0600278 const char* name() const override { return "GrMeshTestOp"; }
279 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700280 GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrFSAAType) override {
Chris Dalton4b62aed2019-01-15 11:53:00 -0700281 return GrProcessorSet::EmptySetAnalysis();
Brian Salomonf86d37b2017-06-16 10:04:34 -0400282 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600283 void onPrepare(GrOpFlushState*) override {}
Brian Salomon588cec72018-11-14 13:56:37 -0500284 void onExecute(GrOpFlushState* state, const SkRect& chainBounds) override {
Chris Dalton114a3c02017-05-26 15:17:19 -0600285 DrawMeshHelper helper(state);
286 fTestFn(&helper);
287 }
288
289 std::function<void(DrawMeshHelper*)> fTestFn;
290
291 typedef GrDrawOp INHERITED;
292};
293
294class GrMeshTestProcessor : public GrGeometryProcessor {
295public:
Chris Dalton1d616352017-05-31 12:51:23 -0600296 GrMeshTestProcessor(bool instanced, bool hasVertexBuffer)
Brian Salomon92be2f72018-06-19 14:33:47 -0400297 : INHERITED(kGrMeshTestProcessor_ClassID) {
Chris Dalton1d616352017-05-31 12:51:23 -0600298 if (instanced) {
Brian Osmand4c29702018-09-14 16:16:55 -0400299 fInstanceLocation = {"location", kFloat2_GrVertexAttribType, kHalf2_GrSLType};
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500300 fInstanceColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
301 this->setInstanceAttributes(&fInstanceLocation, 2);
Chris Dalton1d616352017-05-31 12:51:23 -0600302 if (hasVertexBuffer) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500303 fVertexPosition = {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType};
304 this->setVertexAttributes(&fVertexPosition, 1);
Chris Dalton1d616352017-05-31 12:51:23 -0600305 }
Chris Dalton1d616352017-05-31 12:51:23 -0600306 } else {
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500307 fVertexPosition = {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType};
308 fVertexColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType};
309 this->setVertexAttributes(&fVertexPosition, 2);
Chris Dalton1d616352017-05-31 12:51:23 -0600310 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600311 }
312
313 const char* name() const override { return "GrMeshTest Processor"; }
314
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500315 const Attribute& inColor() const {
316 return fVertexColor.isInitialized() ? fVertexColor : fInstanceColor;
317 }
318
Chris Dalton1d616352017-05-31 12:51:23 -0600319 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
Brian Salomon92be2f72018-06-19 14:33:47 -0400320 b->add32(fInstanceLocation.isInitialized());
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500321 b->add32(fVertexPosition.isInitialized());
Chris Dalton1d616352017-05-31 12:51:23 -0600322 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600323
324 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
325
Brian Salomon92be2f72018-06-19 14:33:47 -0400326private:
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500327 Attribute fVertexPosition;
328 Attribute fVertexColor;
Brian Salomon92be2f72018-06-19 14:33:47 -0400329
330 Attribute fInstanceLocation;
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500331 Attribute fInstanceColor;
Chris Dalton114a3c02017-05-26 15:17:19 -0600332
333 friend class GLSLMeshTestProcessor;
334 typedef GrGeometryProcessor INHERITED;
335};
336
337class GLSLMeshTestProcessor : public GrGLSLGeometryProcessor {
338 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
339 FPCoordTransformIter&& transformIter) final {}
340
341 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
342 const GrMeshTestProcessor& mp = args.fGP.cast<GrMeshTestProcessor>();
343
344 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
345 varyingHandler->emitAttributes(mp);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500346 varyingHandler->addPassThroughAttribute(mp.inColor(), args.fOutputColor);
Chris Dalton114a3c02017-05-26 15:17:19 -0600347
348 GrGLSLVertexBuilder* v = args.fVertBuilder;
Brian Salomon92be2f72018-06-19 14:33:47 -0400349 if (!mp.fInstanceLocation.isInitialized()) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500350 v->codeAppendf("float2 vertex = %s;", mp.fVertexPosition.name());
Chris Dalton1d616352017-05-31 12:51:23 -0600351 } else {
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500352 if (mp.fVertexPosition.isInitialized()) {
353 v->codeAppendf("float2 offset = %s;", mp.fVertexPosition.name());
Chris Dalton1d616352017-05-31 12:51:23 -0600354 } else {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400355 v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);");
Chris Dalton1d616352017-05-31 12:51:23 -0600356 }
Brian Salomon92be2f72018-06-19 14:33:47 -0400357 v->codeAppendf("float2 vertex = %s + offset * %i;", mp.fInstanceLocation.name(),
Brian Salomon70132d02018-05-29 15:33:06 -0400358 kBoxSize);
Chris Dalton1d616352017-05-31 12:51:23 -0600359 }
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400360 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
Chris Dalton114a3c02017-05-26 15:17:19 -0600361
Chris Dalton60283612018-02-14 13:38:14 -0700362 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400363 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton114a3c02017-05-26 15:17:19 -0600364 }
365};
366
367GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
368 return new GLSLMeshTestProcessor;
369}
370
371////////////////////////////////////////////////////////////////////////////////////////////////////
372
373template<typename T>
374sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count) {
Brian Salomonae64c192019-02-05 09:41:37 -0500375 return sk_sp<const GrBuffer>(fState->resourceProvider()->createBuffer(
Brian Salomondbf70722019-02-07 11:31:24 -0500376 count * sizeof(T), GrGpuBufferType::kVertex, kDynamic_GrAccessPattern, data));
Chris Dalton114a3c02017-05-26 15:17:19 -0600377}
378
379sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() {
380 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400381 return fState->resourceProvider()->findOrCreatePatternedIndexBuffer(
382 kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey);
Chris Dalton114a3c02017-05-26 15:17:19 -0600383}
384
385void DrawMeshHelper::drawMesh(const GrMesh& mesh) {
Robert Phillipsd0fe8752019-01-31 14:13:59 -0500386 GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrc);
Chris Dalton1d616352017-05-31 12:51:23 -0600387 GrMeshTestProcessor mtp(mesh.isInstanced(), mesh.hasVertexData());
Brian Salomon49348902018-06-26 09:12:38 -0400388 fState->rtCommandBuffer()->draw(mtp, pipeline, nullptr, nullptr, &mesh, 1,
Greg Daniel500d58b2017-08-24 15:59:33 -0400389 SkRect::MakeIWH(kImageWidth, kImageHeight));
Chris Dalton114a3c02017-05-26 15:17:19 -0600390}
391
Robert Phillips88a32ef2018-06-07 11:05:56 -0400392static void run_test(GrContext* context, const char* testName, skiatest::Reporter* reporter,
Chris Dalton114a3c02017-05-26 15:17:19 -0600393 const sk_sp<GrRenderTargetContext>& rtc, const SkBitmap& gold,
394 std::function<void(DrawMeshHelper*)> testFn) {
395 const int w = gold.width(), h = gold.height(), rowBytes = gold.rowBytes();
396 const uint32_t* goldPx = reinterpret_cast<const uint32_t*>(gold.getPixels());
397 if (h != rtc->height() || w != rtc->width()) {
398 ERRORF(reporter, "[%s] expectation and rtc not compatible (?).", testName);
399 return;
400 }
401 if (sizeof(uint32_t) * kImageWidth != gold.rowBytes()) {
402 ERRORF(reporter, "unexpected row bytes in gold image.", testName);
403 return;
404 }
405
406 SkAutoSTMalloc<kImageHeight * kImageWidth, uint32_t> resultPx(h * rowBytes);
Brian Osman9a9baae2018-11-05 15:06:26 -0500407 rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xbaaaaaad),
408 GrRenderTargetContext::CanClearFullscreen::kYes);
Robert Phillips88a32ef2018-06-07 11:05:56 -0400409 rtc->priv().testingOnly_addDrawOp(GrMeshTestOp::Make(context, testFn));
Chris Dalton114a3c02017-05-26 15:17:19 -0600410 rtc->readPixels(gold.info(), resultPx, rowBytes, 0, 0, 0);
411 for (int y = 0; y < h; ++y) {
412 for (int x = 0; x < w; ++x) {
413 uint32_t expected = goldPx[y * kImageWidth + x];
414 uint32_t actual = resultPx[y * kImageWidth + x];
415 if (expected != actual) {
416 ERRORF(reporter, "[%s] pixel (%i,%i): got 0x%x expected 0x%x",
417 testName, x, y, actual, expected);
418 return;
419 }
420 }
421 }
422}