1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkTypes.h"
#include "Test.h"
#if SK_SUPPORT_GPU
#include "GrContext.h"
#include "GrGeometryProcessor.h"
#include "GrGpuCommandBuffer.h"
#include "GrOpFlushState.h"
#include "GrRenderTargetContext.h"
#include "GrRenderTargetContextPriv.h"
#include "GrResourceProvider.h"
#include "GrResourceKey.h"
#include "SkBitmap.h"
#include "SkMakeUnique.h"
#include "glsl/GrGLSLVertexGeoBuilder.h"
#include "glsl/GrGLSLFragmentShaderBuilder.h"
#include "glsl/GrGLSLGeometryProcessor.h"
#include "glsl/GrGLSLVarying.h"
#include <array>
#include <vector>
GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
static constexpr int kBoxSize = 2;
static constexpr int kBoxCountY = 8;
static constexpr int kBoxCountX = 8;
static constexpr int kBoxCount = kBoxCountY * kBoxCountX;
static constexpr int kImageWidth = kBoxCountY * kBoxSize;
static constexpr int kImageHeight = kBoxCountX * kBoxSize;
static constexpr int kIndexPatternRepeatCount = 3;
constexpr uint16_t kIndexPattern[6] = {0, 1, 2, 1, 2, 3};
class DrawMeshHelper {
public:
DrawMeshHelper(GrOpFlushState* state) : fState(state) {}
sk_sp<const GrBuffer> getIndexBuffer();
template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const SkTArray<T>& data) {
return this->makeVertexBuffer(data.begin(), data.count());
}
template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const std::vector<T>& data) {
return this->makeVertexBuffer(data.data(), data.size());
}
template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const T* data, int count);
void drawMesh(const GrMesh& mesh);
private:
GrOpFlushState* fState;
};
struct Box {
float fX, fY;
GrColor fColor;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* This is a GPU-backend specific test. It tries to test all possible usecases of GrMesh. The test
* works by drawing checkerboards of colored boxes, reading back the pixels, and comparing with
* expected results. The boxes are drawn on integer boundaries and the (opaque) colors are chosen
* from the set (r,g,b) = (0,255)^3, so the GPU renderings ought to produce exact matches.
*/
static void run_test(const char* testName, skiatest::Reporter*, const sk_sp<GrRenderTargetContext>&,
const SkBitmap& gold, std::function<void(DrawMeshHelper*)> testFn);
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrMeshTest, reporter, ctxInfo) {
GrContext* const context = ctxInfo.grContext();
sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
SkBackingFit::kExact, kImageWidth, kImageHeight,
kRGBA_8888_GrPixelConfig, nullptr));
if (!rtc) {
ERRORF(reporter, "could not create render target context.");
return;
}
SkTArray<Box> boxes;
SkTArray<std::array<Box, 4>> vertexData;
SkBitmap gold;
// ---- setup ----------
SkPaint paint;
paint.setBlendMode(SkBlendMode::kSrc);
gold.allocN32Pixels(kImageWidth, kImageHeight);
SkCanvas goldCanvas(gold);
for (int y = 0; y < kBoxCountY; ++y) {
for (int x = 0; x < kBoxCountX; ++x) {
int c = y + x;
int rgb[3] = {-(c & 1) & 0xff, -((c >> 1) & 1) & 0xff, -((c >> 2) & 1) & 0xff};
const Box box = boxes.push_back() = {
float(x * kBoxSize),
float(y * kBoxSize),
GrColorPackRGBA(rgb[0], rgb[1], rgb[2], 255)
};
std::array<Box, 4>& boxVertices = vertexData.push_back();
for (int i = 0; i < 4; ++i) {
boxVertices[i] = {
box.fX + (i/2) * kBoxSize,
box.fY + (i%2) * kBoxSize,
box.fColor
};
}
paint.setARGB(255, rgb[0], rgb[1], rgb[2]);
goldCanvas.drawRect(SkRect::MakeXYWH(box.fX, box.fY, kBoxSize, kBoxSize), paint);
}
}
goldCanvas.flush();
// ---- tests ----------
#define VALIDATE(buff) \
if (!buff) { \
ERRORF(reporter, #buff " is null."); \
return; \
}
run_test("setNonIndexedNonInstanced", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
SkTArray<Box> expandedVertexData;
for (int i = 0; i < kBoxCount; ++i) {
for (int j = 0; j < 6; ++j) {
expandedVertexData.push_back(vertexData[i][kIndexPattern[j]]);
}
}
// Draw boxes one line at a time to exercise base vertex.
auto vbuff = helper->makeVertexBuffer(expandedVertexData);
VALIDATE(vbuff);
for (int y = 0; y < kBoxCountY; ++y) {
GrMesh mesh(GrPrimitiveType::kTriangles);
mesh.setNonIndexedNonInstanced(kBoxCountX * 6);
mesh.setVertexData(vbuff.get(), y * kBoxCountX * 6);
helper->drawMesh(mesh);
}
});
run_test("setIndexed", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
auto ibuff = helper->getIndexBuffer();
VALIDATE(ibuff);
auto vbuff = helper->makeVertexBuffer(vertexData);
VALIDATE(vbuff);
int baseRepetition = 0;
int i = 0;
// Start at various repetitions within the patterned index buffer to exercise base index.
while (i < kBoxCount) {
GR_STATIC_ASSERT(kIndexPatternRepeatCount >= 3);
int repetitionCount = SkTMin(3 - baseRepetition, kBoxCount - i);
GrMesh mesh(GrPrimitiveType::kTriangles);
mesh.setIndexed(ibuff.get(), repetitionCount * 6, baseRepetition * 6,
baseRepetition * 4, (baseRepetition + repetitionCount) * 4 - 1);
mesh.setVertexData(vbuff.get(), (i - baseRepetition) * 4);
helper->drawMesh(mesh);
baseRepetition = (baseRepetition + 1) % 3;
i += repetitionCount;
}
});
run_test("setIndexedPatterned", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
auto ibuff = helper->getIndexBuffer();
VALIDATE(ibuff);
auto vbuff = helper->makeVertexBuffer(vertexData);
VALIDATE(vbuff);
// Draw boxes one line at a time to exercise base vertex. setIndexedPatterned does not
// support a base index.
for (int y = 0; y < kBoxCountY; ++y) {
GrMesh mesh(GrPrimitiveType::kTriangles);
mesh.setIndexedPatterned(ibuff.get(), 6, 4, kBoxCountX, kIndexPatternRepeatCount);
mesh.setVertexData(vbuff.get(), y * kBoxCountX * 4);
helper->drawMesh(mesh);
}
});
for (bool indexed : {false, true}) {
if (!context->caps()->instanceAttribSupport()) {
break;
}
run_test(indexed ? "setIndexedInstanced" : "setInstanced",
reporter, rtc, gold, [&](DrawMeshHelper* helper) {
auto idxbuff = indexed ? helper->getIndexBuffer() : nullptr;
auto instbuff = helper->makeVertexBuffer(boxes);
VALIDATE(instbuff);
auto vbuff = helper->makeVertexBuffer(std::vector<float>{0,0, 0,1, 1,0, 1,1});
VALIDATE(vbuff);
auto vbuff2 = helper->makeVertexBuffer( // for testing base vertex.
std::vector<float>{-1,-1, -1,-1, 0,0, 0,1, 1,0, 1,1});
VALIDATE(vbuff2);
// Draw boxes one line at a time to exercise base instance, base vertex, and null vertex
// buffer. setIndexedInstanced intentionally does not support a base index.
for (int y = 0; y < kBoxCountY; ++y) {
GrMesh mesh(indexed ? GrPrimitiveType::kTriangles
: GrPrimitiveType::kTriangleStrip);
if (indexed) {
VALIDATE(idxbuff);
mesh.setIndexedInstanced(idxbuff.get(), 6,
instbuff.get(), kBoxCountX, y * kBoxCountX);
} else {
mesh.setInstanced(instbuff.get(), kBoxCountX, y * kBoxCountX, 4);
}
switch (y % 3) {
case 0:
if (context->caps()->shaderCaps()->vertexIDSupport()) {
if (y % 2) {
// We don't need this call because it's the initial state of GrMesh.
mesh.setVertexData(nullptr);
}
break;
}
// Fallthru.
case 1:
mesh.setVertexData(vbuff.get());
break;
case 2:
mesh.setVertexData(vbuff2.get(), 2);
break;
}
helper->drawMesh(mesh);
}
});
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
class GrMeshTestOp : public GrDrawOp {
public:
DEFINE_OP_CLASS_ID
GrMeshTestOp(std::function<void(DrawMeshHelper*)> testFn)
: INHERITED(ClassID())
, fTestFn(testFn) {
this->setBounds(SkRect::MakeIWH(kImageWidth, kImageHeight),
HasAABloat::kNo, IsZeroArea::kNo);
}
private:
const char* name() const override { return "GrMeshTestOp"; }
FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,
GrPixelConfigIsClamped) override {
return RequiresDstTexture::kNo;
}
bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
void onPrepare(GrOpFlushState*) override {}
void onExecute(GrOpFlushState* state) override {
DrawMeshHelper helper(state);
fTestFn(&helper);
}
std::function<void(DrawMeshHelper*)> fTestFn;
typedef GrDrawOp INHERITED;
};
class GrMeshTestProcessor : public GrGeometryProcessor {
public:
GrMeshTestProcessor(bool instanced, bool hasVertexBuffer)
: INHERITED(kGrMeshTestProcessor_ClassID)
, fInstanceLocation(nullptr)
, fVertex(nullptr)
, fColor(nullptr) {
if (instanced) {
fInstanceLocation = &this->addInstanceAttrib("location", kHalf2_GrVertexAttribType);
if (hasVertexBuffer) {
fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType);
}
fColor = &this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType);
} else {
fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType);
fColor = &this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
}
}
const char* name() const override { return "GrMeshTest Processor"; }
void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
b->add32(SkToBool(fInstanceLocation));
b->add32(SkToBool(fVertex));
}
GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
protected:
const Attribute* fInstanceLocation;
const Attribute* fVertex;
const Attribute* fColor;
friend class GLSLMeshTestProcessor;
typedef GrGeometryProcessor INHERITED;
};
class GLSLMeshTestProcessor : public GrGLSLGeometryProcessor {
void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
FPCoordTransformIter&& transformIter) final {}
void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
const GrMeshTestProcessor& mp = args.fGP.cast<GrMeshTestProcessor>();
GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
varyingHandler->emitAttributes(mp);
varyingHandler->addPassThroughAttribute(mp.fColor, args.fOutputColor);
GrGLSLVertexBuilder* v = args.fVertBuilder;
if (!mp.fInstanceLocation) {
v->codeAppendf("float2 vertex = %s;", mp.fVertex->fName);
} else {
if (mp.fVertex) {
v->codeAppendf("float2 offset = %s;", mp.fVertex->fName);
} else {
v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);");
}
v->codeAppendf("float2 vertex = %s + offset * %i;",
mp.fInstanceLocation->fName, kBoxSize);
}
gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
}
};
GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
return new GLSLMeshTestProcessor;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
template<typename T>
sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count) {
return sk_sp<const GrBuffer>(
fState->resourceProvider()->createBuffer(
count * sizeof(T), kVertex_GrBufferType, kDynamic_GrAccessPattern,
GrResourceProvider::kNoPendingIO_Flag |
GrResourceProvider::kRequireGpuMemory_Flag, data));
}
sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() {
GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
return fState->resourceProvider()->findOrCreatePatternedIndexBuffer(
kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey);
}
void DrawMeshHelper::drawMesh(const GrMesh& mesh) {
GrRenderTargetProxy* proxy = fState->drawOpArgs().fProxy;
GrPipeline pipeline(proxy, GrPipeline::ScissorState::kDisabled, SkBlendMode::kSrc);
GrMeshTestProcessor mtp(mesh.isInstanced(), mesh.hasVertexData());
fState->rtCommandBuffer()->draw(pipeline, mtp, &mesh, nullptr, 1,
SkRect::MakeIWH(kImageWidth, kImageHeight));
}
static void run_test(const char* testName, skiatest::Reporter* reporter,
const sk_sp<GrRenderTargetContext>& rtc, const SkBitmap& gold,
std::function<void(DrawMeshHelper*)> testFn) {
const int w = gold.width(), h = gold.height(), rowBytes = gold.rowBytes();
const uint32_t* goldPx = reinterpret_cast<const uint32_t*>(gold.getPixels());
if (h != rtc->height() || w != rtc->width()) {
ERRORF(reporter, "[%s] expectation and rtc not compatible (?).", testName);
return;
}
if (sizeof(uint32_t) * kImageWidth != gold.rowBytes()) {
ERRORF(reporter, "unexpected row bytes in gold image.", testName);
return;
}
SkAutoSTMalloc<kImageHeight * kImageWidth, uint32_t> resultPx(h * rowBytes);
rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes);
rtc->priv().testingOnly_addDrawOp(skstd::make_unique<GrMeshTestOp>(testFn));
rtc->readPixels(gold.info(), resultPx, rowBytes, 0, 0, 0);
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
uint32_t expected = goldPx[y * kImageWidth + x];
uint32_t actual = resultPx[y * kImageWidth + x];
if (expected != actual) {
ERRORF(reporter, "[%s] pixel (%i,%i): got 0x%x expected 0x%x",
testName, x, y, actual, expected);
return;
}
}
}
}
#endif
|