blob: 5afe56291d1c177b3a13dfaeda2c0d7f5488ea3b [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
11#if SK_SUPPORT_GPU
12
Brian Salomonc7fe0f72018-05-11 10:14:21 -040013#include <array>
14#include <vector>
15#include "GrCaps.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060016#include "GrContext.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040017#include "GrContextPriv.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060018#include "GrGeometryProcessor.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040019#include "GrGpuCommandBuffer.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060020#include "GrOpFlushState.h"
21#include "GrRenderTargetContext.h"
22#include "GrRenderTargetContextPriv.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060023#include "GrResourceKey.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040024#include "GrResourceProvider.h"
Mike Reed75ae4212018-01-23 11:24:08 -050025#include "SkBitmap.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060026#include "SkMakeUnique.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060027#include "glsl/GrGLSLFragmentShaderBuilder.h"
28#include "glsl/GrGLSLGeometryProcessor.h"
29#include "glsl/GrGLSLVarying.h"
Brian Salomonc7fe0f72018-05-11 10:14:21 -040030#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060031
32GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey);
33
34static constexpr int kBoxSize = 2;
35static constexpr int kBoxCountY = 8;
36static constexpr int kBoxCountX = 8;
37static constexpr int kBoxCount = kBoxCountY * kBoxCountX;
38
39static constexpr int kImageWidth = kBoxCountY * kBoxSize;
40static constexpr int kImageHeight = kBoxCountX * kBoxSize;
41
42static constexpr int kIndexPatternRepeatCount = 3;
43constexpr uint16_t kIndexPattern[6] = {0, 1, 2, 1, 2, 3};
44
45
46class DrawMeshHelper {
47public:
48 DrawMeshHelper(GrOpFlushState* state) : fState(state) {}
49
50 sk_sp<const GrBuffer> getIndexBuffer();
51
52 template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const SkTArray<T>& data) {
53 return this->makeVertexBuffer(data.begin(), data.count());
54 }
Chris Dalton1d616352017-05-31 12:51:23 -060055 template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const std::vector<T>& data) {
56 return this->makeVertexBuffer(data.data(), data.size());
57 }
Chris Dalton114a3c02017-05-26 15:17:19 -060058 template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const T* data, int count);
59
60 void drawMesh(const GrMesh& mesh);
61
62private:
63 GrOpFlushState* fState;
64};
65
66struct Box {
67 float fX, fY;
68 GrColor fColor;
69};
70
71////////////////////////////////////////////////////////////////////////////////////////////////////
72
73/**
74 * This is a GPU-backend specific test. It tries to test all possible usecases of GrMesh. The test
75 * works by drawing checkerboards of colored boxes, reading back the pixels, and comparing with
76 * expected results. The boxes are drawn on integer boundaries and the (opaque) colors are chosen
77 * from the set (r,g,b) = (0,255)^3, so the GPU renderings ought to produce exact matches.
78 */
79
80static void run_test(const char* testName, skiatest::Reporter*, const sk_sp<GrRenderTargetContext>&,
81 const SkBitmap& gold, std::function<void(DrawMeshHelper*)> testFn);
82
83DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrMeshTest, reporter, ctxInfo) {
84 GrContext* const context = ctxInfo.grContext();
85
Robert Phillips0c4b7b12018-03-06 08:20:37 -050086 sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext(
87 SkBackingFit::kExact, kImageWidth, kImageHeight,
Chris Dalton114a3c02017-05-26 15:17:19 -060088 kRGBA_8888_GrPixelConfig, nullptr));
89 if (!rtc) {
90 ERRORF(reporter, "could not create render target context.");
91 return;
92 }
93
94 SkTArray<Box> boxes;
95 SkTArray<std::array<Box, 4>> vertexData;
96 SkBitmap gold;
97
98 // ---- setup ----------
99
100 SkPaint paint;
101 paint.setBlendMode(SkBlendMode::kSrc);
102 gold.allocN32Pixels(kImageWidth, kImageHeight);
103
104 SkCanvas goldCanvas(gold);
105
106 for (int y = 0; y < kBoxCountY; ++y) {
107 for (int x = 0; x < kBoxCountX; ++x) {
108 int c = y + x;
109 int rgb[3] = {-(c & 1) & 0xff, -((c >> 1) & 1) & 0xff, -((c >> 2) & 1) & 0xff};
110
111 const Box box = boxes.push_back() = {
112 float(x * kBoxSize),
113 float(y * kBoxSize),
114 GrColorPackRGBA(rgb[0], rgb[1], rgb[2], 255)
115 };
116
117 std::array<Box, 4>& boxVertices = vertexData.push_back();
118 for (int i = 0; i < 4; ++i) {
119 boxVertices[i] = {
120 box.fX + (i/2) * kBoxSize,
121 box.fY + (i%2) * kBoxSize,
122 box.fColor
123 };
124 }
125
126 paint.setARGB(255, rgb[0], rgb[1], rgb[2]);
127 goldCanvas.drawRect(SkRect::MakeXYWH(box.fX, box.fY, kBoxSize, kBoxSize), paint);
128 }
129 }
130
131 goldCanvas.flush();
132
133 // ---- tests ----------
134
135#define VALIDATE(buff) \
136 if (!buff) { \
137 ERRORF(reporter, #buff " is null."); \
138 return; \
139 }
140
141 run_test("setNonIndexedNonInstanced", reporter, rtc, gold, [&](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 }
148
149 // 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) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600153 GrMesh mesh(GrPrimitiveType::kTriangles);
Chris Dalton1d616352017-05-31 12:51:23 -0600154 mesh.setNonIndexedNonInstanced(kBoxCountX * 6);
Chris Dalton114a3c02017-05-26 15:17:19 -0600155 mesh.setVertexData(vbuff.get(), y * kBoxCountX * 6);
156 helper->drawMesh(mesh);
157 }
158 });
159
160 run_test("setIndexed", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
161 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
184 run_test("setIndexedPatterned", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
185 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
205 run_test(indexed ? "setIndexedInstanced" : "setInstanced",
206 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
257 GrMeshTestOp(std::function<void(DrawMeshHelper*)> testFn)
258 : INHERITED(ClassID())
259 , fTestFn(testFn) {
260 this->setBounds(SkRect::MakeIWH(kImageWidth, kImageHeight),
261 HasAABloat::kNo, IsZeroArea::kNo);
262 }
263
264private:
265 const char* name() const override { return "GrMeshTestOp"; }
266 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman9a725dd2017-09-20 09:53:22 -0400267 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,
268 GrPixelConfigIsClamped) override {
Brian Salomonf86d37b2017-06-16 10:04:34 -0400269 return RequiresDstTexture::kNo;
270 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600271 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
272 void onPrepare(GrOpFlushState*) override {}
273 void onExecute(GrOpFlushState* state) override {
274 DrawMeshHelper helper(state);
275 fTestFn(&helper);
276 }
277
278 std::function<void(DrawMeshHelper*)> fTestFn;
279
280 typedef GrDrawOp INHERITED;
281};
282
283class GrMeshTestProcessor : public GrGeometryProcessor {
284public:
Chris Dalton1d616352017-05-31 12:51:23 -0600285 GrMeshTestProcessor(bool instanced, bool hasVertexBuffer)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400286 : INHERITED(kGrMeshTestProcessor_ClassID)
287 , fInstanceLocation(nullptr)
Chris Dalton1d616352017-05-31 12:51:23 -0600288 , fVertex(nullptr)
289 , fColor(nullptr) {
290 if (instanced) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400291 fInstanceLocation = &this->addInstanceAttrib("location", kHalf2_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600292 if (hasVertexBuffer) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400293 fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600294 }
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400295 fColor = &this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600296 } else {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400297 fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType);
298 fColor = &this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600299 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600300 }
301
302 const char* name() const override { return "GrMeshTest Processor"; }
303
Chris Dalton1d616352017-05-31 12:51:23 -0600304 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
305 b->add32(SkToBool(fInstanceLocation));
306 b->add32(SkToBool(fVertex));
307 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600308
309 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
310
311protected:
Chris Dalton1d616352017-05-31 12:51:23 -0600312 const Attribute* fInstanceLocation;
313 const Attribute* fVertex;
314 const Attribute* fColor;
Chris Dalton114a3c02017-05-26 15:17:19 -0600315
316 friend class GLSLMeshTestProcessor;
317 typedef GrGeometryProcessor INHERITED;
318};
319
320class GLSLMeshTestProcessor : public GrGLSLGeometryProcessor {
321 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
322 FPCoordTransformIter&& transformIter) final {}
323
324 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
325 const GrMeshTestProcessor& mp = args.fGP.cast<GrMeshTestProcessor>();
326
327 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
328 varyingHandler->emitAttributes(mp);
Chris Dalton1d616352017-05-31 12:51:23 -0600329 varyingHandler->addPassThroughAttribute(mp.fColor, args.fOutputColor);
Chris Dalton114a3c02017-05-26 15:17:19 -0600330
331 GrGLSLVertexBuilder* v = args.fVertBuilder;
Chris Dalton1d616352017-05-31 12:51:23 -0600332 if (!mp.fInstanceLocation) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400333 v->codeAppendf("float2 vertex = %s;", mp.fVertex->fName);
Chris Dalton1d616352017-05-31 12:51:23 -0600334 } else {
335 if (mp.fVertex) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400336 v->codeAppendf("float2 offset = %s;", mp.fVertex->fName);
Chris Dalton1d616352017-05-31 12:51:23 -0600337 } else {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400338 v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);");
Chris Dalton1d616352017-05-31 12:51:23 -0600339 }
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400340 v->codeAppendf("float2 vertex = %s + offset * %i;",
Chris Dalton1d616352017-05-31 12:51:23 -0600341 mp.fInstanceLocation->fName, kBoxSize);
342 }
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400343 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
Chris Dalton114a3c02017-05-26 15:17:19 -0600344
Chris Dalton60283612018-02-14 13:38:14 -0700345 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400346 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton114a3c02017-05-26 15:17:19 -0600347 }
348};
349
350GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
351 return new GLSLMeshTestProcessor;
352}
353
354////////////////////////////////////////////////////////////////////////////////////////////////////
355
356template<typename T>
357sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count) {
358 return sk_sp<const GrBuffer>(
359 fState->resourceProvider()->createBuffer(
360 count * sizeof(T), kVertex_GrBufferType, kDynamic_GrAccessPattern,
361 GrResourceProvider::kNoPendingIO_Flag |
362 GrResourceProvider::kRequireGpuMemory_Flag, data));
363}
364
365sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() {
366 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400367 return fState->resourceProvider()->findOrCreatePatternedIndexBuffer(
368 kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey);
Chris Dalton114a3c02017-05-26 15:17:19 -0600369}
370
371void DrawMeshHelper::drawMesh(const GrMesh& mesh) {
Robert Phillips2890fbf2017-07-26 15:48:41 -0400372 GrRenderTargetProxy* proxy = fState->drawOpArgs().fProxy;
373 GrPipeline pipeline(proxy, GrPipeline::ScissorState::kDisabled, SkBlendMode::kSrc);
Chris Dalton1d616352017-05-31 12:51:23 -0600374 GrMeshTestProcessor mtp(mesh.isInstanced(), mesh.hasVertexData());
Greg Daniel500d58b2017-08-24 15:59:33 -0400375 fState->rtCommandBuffer()->draw(pipeline, mtp, &mesh, nullptr, 1,
376 SkRect::MakeIWH(kImageWidth, kImageHeight));
Chris Dalton114a3c02017-05-26 15:17:19 -0600377}
378
379static void run_test(const char* testName, skiatest::Reporter* reporter,
380 const sk_sp<GrRenderTargetContext>& rtc, const SkBitmap& gold,
381 std::function<void(DrawMeshHelper*)> testFn) {
382 const int w = gold.width(), h = gold.height(), rowBytes = gold.rowBytes();
383 const uint32_t* goldPx = reinterpret_cast<const uint32_t*>(gold.getPixels());
384 if (h != rtc->height() || w != rtc->width()) {
385 ERRORF(reporter, "[%s] expectation and rtc not compatible (?).", testName);
386 return;
387 }
388 if (sizeof(uint32_t) * kImageWidth != gold.rowBytes()) {
389 ERRORF(reporter, "unexpected row bytes in gold image.", testName);
390 return;
391 }
392
393 SkAutoSTMalloc<kImageHeight * kImageWidth, uint32_t> resultPx(h * rowBytes);
Chris Dalton344e9032017-12-11 15:42:09 -0700394 rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton114a3c02017-05-26 15:17:19 -0600395 rtc->priv().testingOnly_addDrawOp(skstd::make_unique<GrMeshTestOp>(testFn));
396 rtc->readPixels(gold.info(), resultPx, rowBytes, 0, 0, 0);
397 for (int y = 0; y < h; ++y) {
398 for (int x = 0; x < w; ++x) {
399 uint32_t expected = goldPx[y * kImageWidth + x];
400 uint32_t actual = resultPx[y * kImageWidth + x];
401 if (expected != actual) {
402 ERRORF(reporter, "[%s] pixel (%i,%i): got 0x%x expected 0x%x",
403 testName, x, y, actual, expected);
404 return;
405 }
406 }
407 }
408}
409
410#endif