blob: 80788ddc07169394b5e76ec535050574051b8a21 [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
13#include "GrContext.h"
14#include "GrGeometryProcessor.h"
Robert Phillips009e9af2017-06-15 14:01:04 -040015#include "GrGpuCommandBuffer.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060016#include "GrOpFlushState.h"
17#include "GrRenderTargetContext.h"
18#include "GrRenderTargetContextPriv.h"
19#include "GrResourceProvider.h"
20#include "GrResourceKey.h"
Mike Reed75ae4212018-01-23 11:24:08 -050021#include "SkBitmap.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060022#include "SkMakeUnique.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060023#include "glsl/GrGLSLVertexGeoBuilder.h"
Chris Dalton114a3c02017-05-26 15:17:19 -060024#include "glsl/GrGLSLFragmentShaderBuilder.h"
25#include "glsl/GrGLSLGeometryProcessor.h"
26#include "glsl/GrGLSLVarying.h"
27#include <array>
Chris Dalton1d616352017-05-31 12:51:23 -060028#include <vector>
Chris Dalton114a3c02017-05-26 15:17:19 -060029
30
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
79static void run_test(const char* testName, skiatest::Reporter*, const sk_sp<GrRenderTargetContext>&,
80 const SkBitmap& gold, std::function<void(DrawMeshHelper*)> testFn);
81
82DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrMeshTest, reporter, ctxInfo) {
83 GrContext* const context = ctxInfo.grContext();
84
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
140 run_test("setNonIndexedNonInstanced", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
141 SkTArray<Box> expandedVertexData;
142 for (int i = 0; i < kBoxCount; ++i) {
143 for (int j = 0; j < 6; ++j) {
144 expandedVertexData.push_back(vertexData[i][kIndexPattern[j]]);
145 }
146 }
147
148 // Draw boxes one line at a time to exercise base vertex.
149 auto vbuff = helper->makeVertexBuffer(expandedVertexData);
150 VALIDATE(vbuff);
151 for (int y = 0; y < kBoxCountY; ++y) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600152 GrMesh mesh(GrPrimitiveType::kTriangles);
Chris Dalton1d616352017-05-31 12:51:23 -0600153 mesh.setNonIndexedNonInstanced(kBoxCountX * 6);
Chris Dalton114a3c02017-05-26 15:17:19 -0600154 mesh.setVertexData(vbuff.get(), y * kBoxCountX * 6);
155 helper->drawMesh(mesh);
156 }
157 });
158
159 run_test("setIndexed", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
160 auto ibuff = helper->getIndexBuffer();
161 VALIDATE(ibuff);
162 auto vbuff = helper->makeVertexBuffer(vertexData);
163 VALIDATE(vbuff);
164 int baseRepetition = 0;
165 int i = 0;
166
167 // Start at various repetitions within the patterned index buffer to exercise base index.
168 while (i < kBoxCount) {
169 GR_STATIC_ASSERT(kIndexPatternRepeatCount >= 3);
170 int repetitionCount = SkTMin(3 - baseRepetition, kBoxCount - i);
171
Chris Dalton3809bab2017-06-13 10:55:06 -0600172 GrMesh mesh(GrPrimitiveType::kTriangles);
Chris Dalton114a3c02017-05-26 15:17:19 -0600173 mesh.setIndexed(ibuff.get(), repetitionCount * 6, baseRepetition * 6,
174 baseRepetition * 4, (baseRepetition + repetitionCount) * 4 - 1);
175 mesh.setVertexData(vbuff.get(), (i - baseRepetition) * 4);
176 helper->drawMesh(mesh);
177
178 baseRepetition = (baseRepetition + 1) % 3;
179 i += repetitionCount;
180 }
181 });
182
183 run_test("setIndexedPatterned", reporter, rtc, gold, [&](DrawMeshHelper* helper) {
184 auto ibuff = helper->getIndexBuffer();
185 VALIDATE(ibuff);
186 auto vbuff = helper->makeVertexBuffer(vertexData);
187 VALIDATE(vbuff);
188
189 // Draw boxes one line at a time to exercise base vertex. setIndexedPatterned does not
190 // support a base index.
191 for (int y = 0; y < kBoxCountY; ++y) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600192 GrMesh mesh(GrPrimitiveType::kTriangles);
Chris Dalton114a3c02017-05-26 15:17:19 -0600193 mesh.setIndexedPatterned(ibuff.get(), 6, 4, kBoxCountX, kIndexPatternRepeatCount);
194 mesh.setVertexData(vbuff.get(), y * kBoxCountX * 4);
195 helper->drawMesh(mesh);
196 }
197 });
Chris Dalton1d616352017-05-31 12:51:23 -0600198
199 for (bool indexed : {false, true}) {
200 if (!context->caps()->instanceAttribSupport()) {
201 break;
202 }
203
204 run_test(indexed ? "setIndexedInstanced" : "setInstanced",
205 reporter, rtc, gold, [&](DrawMeshHelper* helper) {
206 auto idxbuff = indexed ? helper->getIndexBuffer() : nullptr;
207 auto instbuff = helper->makeVertexBuffer(boxes);
208 VALIDATE(instbuff);
209 auto vbuff = helper->makeVertexBuffer(std::vector<float>{0,0, 0,1, 1,0, 1,1});
210 VALIDATE(vbuff);
211 auto vbuff2 = helper->makeVertexBuffer( // for testing base vertex.
212 std::vector<float>{-1,-1, -1,-1, 0,0, 0,1, 1,0, 1,1});
213 VALIDATE(vbuff2);
214
215 // Draw boxes one line at a time to exercise base instance, base vertex, and null vertex
216 // buffer. setIndexedInstanced intentionally does not support a base index.
217 for (int y = 0; y < kBoxCountY; ++y) {
Chris Dalton3809bab2017-06-13 10:55:06 -0600218 GrMesh mesh(indexed ? GrPrimitiveType::kTriangles
219 : GrPrimitiveType::kTriangleStrip);
Chris Dalton1d616352017-05-31 12:51:23 -0600220 if (indexed) {
221 VALIDATE(idxbuff);
222 mesh.setIndexedInstanced(idxbuff.get(), 6,
223 instbuff.get(), kBoxCountX, y * kBoxCountX);
224 } else {
225 mesh.setInstanced(instbuff.get(), kBoxCountX, y * kBoxCountX, 4);
226 }
227 switch (y % 3) {
228 case 0:
229 if (context->caps()->shaderCaps()->vertexIDSupport()) {
230 if (y % 2) {
231 // We don't need this call because it's the initial state of GrMesh.
232 mesh.setVertexData(nullptr);
233 }
234 break;
235 }
236 // Fallthru.
237 case 1:
238 mesh.setVertexData(vbuff.get());
239 break;
240 case 2:
241 mesh.setVertexData(vbuff2.get(), 2);
242 break;
243 }
244 helper->drawMesh(mesh);
245 }
246 });
247 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600248}
249
250////////////////////////////////////////////////////////////////////////////////////////////////////
251
252class GrMeshTestOp : public GrDrawOp {
253public:
254 DEFINE_OP_CLASS_ID
255
256 GrMeshTestOp(std::function<void(DrawMeshHelper*)> testFn)
257 : INHERITED(ClassID())
258 , fTestFn(testFn) {
259 this->setBounds(SkRect::MakeIWH(kImageWidth, kImageHeight),
260 HasAABloat::kNo, IsZeroArea::kNo);
261 }
262
263private:
264 const char* name() const override { return "GrMeshTestOp"; }
265 FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; }
Brian Osman9a725dd2017-09-20 09:53:22 -0400266 RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*,
267 GrPixelConfigIsClamped) override {
Brian Salomonf86d37b2017-06-16 10:04:34 -0400268 return RequiresDstTexture::kNo;
269 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600270 bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; }
271 void onPrepare(GrOpFlushState*) override {}
272 void onExecute(GrOpFlushState* state) override {
273 DrawMeshHelper helper(state);
274 fTestFn(&helper);
275 }
276
277 std::function<void(DrawMeshHelper*)> fTestFn;
278
279 typedef GrDrawOp INHERITED;
280};
281
282class GrMeshTestProcessor : public GrGeometryProcessor {
283public:
Chris Dalton1d616352017-05-31 12:51:23 -0600284 GrMeshTestProcessor(bool instanced, bool hasVertexBuffer)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400285 : INHERITED(kGrMeshTestProcessor_ClassID)
286 , fInstanceLocation(nullptr)
Chris Dalton1d616352017-05-31 12:51:23 -0600287 , fVertex(nullptr)
288 , fColor(nullptr) {
289 if (instanced) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400290 fInstanceLocation = &this->addInstanceAttrib("location", kHalf2_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600291 if (hasVertexBuffer) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400292 fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600293 }
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400294 fColor = &this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600295 } else {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -0400296 fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType);
297 fColor = &this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType);
Chris Dalton1d616352017-05-31 12:51:23 -0600298 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600299 }
300
301 const char* name() const override { return "GrMeshTest Processor"; }
302
Chris Dalton1d616352017-05-31 12:51:23 -0600303 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final {
304 b->add32(SkToBool(fInstanceLocation));
305 b->add32(SkToBool(fVertex));
306 }
Chris Dalton114a3c02017-05-26 15:17:19 -0600307
308 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final;
309
310protected:
Chris Dalton1d616352017-05-31 12:51:23 -0600311 const Attribute* fInstanceLocation;
312 const Attribute* fVertex;
313 const Attribute* fColor;
Chris Dalton114a3c02017-05-26 15:17:19 -0600314
315 friend class GLSLMeshTestProcessor;
316 typedef GrGeometryProcessor INHERITED;
317};
318
319class GLSLMeshTestProcessor : public GrGLSLGeometryProcessor {
320 void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&,
321 FPCoordTransformIter&& transformIter) final {}
322
323 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final {
324 const GrMeshTestProcessor& mp = args.fGP.cast<GrMeshTestProcessor>();
325
326 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
327 varyingHandler->emitAttributes(mp);
Chris Dalton1d616352017-05-31 12:51:23 -0600328 varyingHandler->addPassThroughAttribute(mp.fColor, args.fOutputColor);
Chris Dalton114a3c02017-05-26 15:17:19 -0600329
330 GrGLSLVertexBuilder* v = args.fVertBuilder;
Chris Dalton1d616352017-05-31 12:51:23 -0600331 if (!mp.fInstanceLocation) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400332 v->codeAppendf("float2 vertex = %s;", mp.fVertex->fName);
Chris Dalton1d616352017-05-31 12:51:23 -0600333 } else {
334 if (mp.fVertex) {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400335 v->codeAppendf("float2 offset = %s;", mp.fVertex->fName);
Chris Dalton1d616352017-05-31 12:51:23 -0600336 } else {
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400337 v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);");
Chris Dalton1d616352017-05-31 12:51:23 -0600338 }
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400339 v->codeAppendf("float2 vertex = %s + offset * %i;",
Chris Dalton1d616352017-05-31 12:51:23 -0600340 mp.fInstanceLocation->fName, kBoxSize);
341 }
Ethan Nicholas8aa45692017-09-20 11:24:15 -0400342 gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex");
Chris Dalton114a3c02017-05-26 15:17:19 -0600343
Chris Dalton60283612018-02-14 13:38:14 -0700344 GrGLSLFPFragmentBuilder* f = args.fFragBuilder;
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400345 f->codeAppendf("%s = half4(1);", args.fOutputCoverage);
Chris Dalton114a3c02017-05-26 15:17:19 -0600346 }
347};
348
349GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const {
350 return new GLSLMeshTestProcessor;
351}
352
353////////////////////////////////////////////////////////////////////////////////////////////////////
354
355template<typename T>
356sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count) {
357 return sk_sp<const GrBuffer>(
358 fState->resourceProvider()->createBuffer(
359 count * sizeof(T), kVertex_GrBufferType, kDynamic_GrAccessPattern,
360 GrResourceProvider::kNoPendingIO_Flag |
361 GrResourceProvider::kRequireGpuMemory_Flag, data));
362}
363
364sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() {
365 GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey);
Brian Salomond28a79d2017-10-16 13:01:07 -0400366 return fState->resourceProvider()->findOrCreatePatternedIndexBuffer(
367 kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey);
Chris Dalton114a3c02017-05-26 15:17:19 -0600368}
369
370void DrawMeshHelper::drawMesh(const GrMesh& mesh) {
Robert Phillips2890fbf2017-07-26 15:48:41 -0400371 GrRenderTargetProxy* proxy = fState->drawOpArgs().fProxy;
372 GrPipeline pipeline(proxy, GrPipeline::ScissorState::kDisabled, SkBlendMode::kSrc);
Chris Dalton1d616352017-05-31 12:51:23 -0600373 GrMeshTestProcessor mtp(mesh.isInstanced(), mesh.hasVertexData());
Greg Daniel500d58b2017-08-24 15:59:33 -0400374 fState->rtCommandBuffer()->draw(pipeline, mtp, &mesh, nullptr, 1,
375 SkRect::MakeIWH(kImageWidth, kImageHeight));
Chris Dalton114a3c02017-05-26 15:17:19 -0600376}
377
378static void run_test(const char* testName, skiatest::Reporter* reporter,
379 const sk_sp<GrRenderTargetContext>& rtc, const SkBitmap& gold,
380 std::function<void(DrawMeshHelper*)> testFn) {
381 const int w = gold.width(), h = gold.height(), rowBytes = gold.rowBytes();
382 const uint32_t* goldPx = reinterpret_cast<const uint32_t*>(gold.getPixels());
383 if (h != rtc->height() || w != rtc->width()) {
384 ERRORF(reporter, "[%s] expectation and rtc not compatible (?).", testName);
385 return;
386 }
387 if (sizeof(uint32_t) * kImageWidth != gold.rowBytes()) {
388 ERRORF(reporter, "unexpected row bytes in gold image.", testName);
389 return;
390 }
391
392 SkAutoSTMalloc<kImageHeight * kImageWidth, uint32_t> resultPx(h * rowBytes);
Chris Dalton344e9032017-12-11 15:42:09 -0700393 rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes);
Chris Dalton114a3c02017-05-26 15:17:19 -0600394 rtc->priv().testingOnly_addDrawOp(skstd::make_unique<GrMeshTestOp>(testFn));
395 rtc->readPixels(gold.info(), resultPx, rowBytes, 0, 0, 0);
396 for (int y = 0; y < h; ++y) {
397 for (int x = 0; x < w; ++x) {
398 uint32_t expected = goldPx[y * kImageWidth + x];
399 uint32_t actual = resultPx[y * kImageWidth + x];
400 if (expected != actual) {
401 ERRORF(reporter, "[%s] pixel (%i,%i): got 0x%x expected 0x%x",
402 testName, x, y, actual, expected);
403 return;
404 }
405 }
406 }
407}
408
409#endif