Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 1 | /* |
| 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 Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 15 | #include "GrGpuCommandBuffer.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 16 | #include "GrOpFlushState.h" |
| 17 | #include "GrRenderTargetContext.h" |
| 18 | #include "GrRenderTargetContextPriv.h" |
| 19 | #include "GrResourceProvider.h" |
| 20 | #include "GrResourceKey.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 21 | #include "SkBitmap.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 22 | #include "SkMakeUnique.h" |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 23 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 24 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 25 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 26 | #include "glsl/GrGLSLVarying.h" |
| 27 | #include <array> |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 28 | #include <vector> |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 29 | |
| 30 | |
| 31 | GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 32 | |
| 33 | static constexpr int kBoxSize = 2; |
| 34 | static constexpr int kBoxCountY = 8; |
| 35 | static constexpr int kBoxCountX = 8; |
| 36 | static constexpr int kBoxCount = kBoxCountY * kBoxCountX; |
| 37 | |
| 38 | static constexpr int kImageWidth = kBoxCountY * kBoxSize; |
| 39 | static constexpr int kImageHeight = kBoxCountX * kBoxSize; |
| 40 | |
| 41 | static constexpr int kIndexPatternRepeatCount = 3; |
| 42 | constexpr uint16_t kIndexPattern[6] = {0, 1, 2, 1, 2, 3}; |
| 43 | |
| 44 | |
| 45 | class DrawMeshHelper { |
| 46 | public: |
| 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 Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 54 | template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const std::vector<T>& data) { |
| 55 | return this->makeVertexBuffer(data.data(), data.size()); |
| 56 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 57 | template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const T* data, int count); |
| 58 | |
| 59 | void drawMesh(const GrMesh& mesh); |
| 60 | |
| 61 | private: |
| 62 | GrOpFlushState* fState; |
| 63 | }; |
| 64 | |
| 65 | struct 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 | |
| 79 | static void run_test(const char* testName, skiatest::Reporter*, const sk_sp<GrRenderTargetContext>&, |
| 80 | const SkBitmap& gold, std::function<void(DrawMeshHelper*)> testFn); |
| 81 | |
| 82 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrMeshTest, reporter, ctxInfo) { |
| 83 | GrContext* const context = ctxInfo.grContext(); |
| 84 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 85 | sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext( |
| 86 | SkBackingFit::kExact, kImageWidth, kImageHeight, |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 87 | 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 Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 152 | GrMesh mesh(GrPrimitiveType::kTriangles); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 153 | mesh.setNonIndexedNonInstanced(kBoxCountX * 6); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 154 | 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 Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 172 | GrMesh mesh(GrPrimitiveType::kTriangles); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 173 | 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 Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 192 | GrMesh mesh(GrPrimitiveType::kTriangles); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 193 | mesh.setIndexedPatterned(ibuff.get(), 6, 4, kBoxCountX, kIndexPatternRepeatCount); |
| 194 | mesh.setVertexData(vbuff.get(), y * kBoxCountX * 4); |
| 195 | helper->drawMesh(mesh); |
| 196 | } |
| 197 | }); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 198 | |
| 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 Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 218 | GrMesh mesh(indexed ? GrPrimitiveType::kTriangles |
| 219 | : GrPrimitiveType::kTriangleStrip); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 220 | 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 Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 251 | |
| 252 | class GrMeshTestOp : public GrDrawOp { |
| 253 | public: |
| 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 | |
| 263 | private: |
| 264 | const char* name() const override { return "GrMeshTestOp"; } |
| 265 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 266 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*, |
| 267 | GrPixelConfigIsClamped) override { |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 268 | return RequiresDstTexture::kNo; |
| 269 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 270 | 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 | |
| 282 | class GrMeshTestProcessor : public GrGeometryProcessor { |
| 283 | public: |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 284 | GrMeshTestProcessor(bool instanced, bool hasVertexBuffer) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 285 | : INHERITED(kGrMeshTestProcessor_ClassID) |
| 286 | , fInstanceLocation(nullptr) |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 287 | , fVertex(nullptr) |
| 288 | , fColor(nullptr) { |
| 289 | if (instanced) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 290 | fInstanceLocation = &this->addInstanceAttrib("location", kHalf2_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 291 | if (hasVertexBuffer) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 292 | fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 293 | } |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 294 | fColor = &this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 295 | } else { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 296 | fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType); |
| 297 | fColor = &this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 298 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | const char* name() const override { return "GrMeshTest Processor"; } |
| 302 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 303 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final { |
| 304 | b->add32(SkToBool(fInstanceLocation)); |
| 305 | b->add32(SkToBool(fVertex)); |
| 306 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 307 | |
| 308 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 309 | |
| 310 | protected: |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 311 | const Attribute* fInstanceLocation; |
| 312 | const Attribute* fVertex; |
| 313 | const Attribute* fColor; |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 314 | |
| 315 | friend class GLSLMeshTestProcessor; |
| 316 | typedef GrGeometryProcessor INHERITED; |
| 317 | }; |
| 318 | |
| 319 | class 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 Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 328 | varyingHandler->addPassThroughAttribute(mp.fColor, args.fOutputColor); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 329 | |
| 330 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 331 | if (!mp.fInstanceLocation) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 332 | v->codeAppendf("float2 vertex = %s;", mp.fVertex->fName); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 333 | } else { |
| 334 | if (mp.fVertex) { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 335 | v->codeAppendf("float2 offset = %s;", mp.fVertex->fName); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 336 | } else { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 337 | v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);"); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 338 | } |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 339 | v->codeAppendf("float2 vertex = %s + offset * %i;", |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 340 | mp.fInstanceLocation->fName, kBoxSize); |
| 341 | } |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 342 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex"); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 343 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 344 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 345 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 346 | } |
| 347 | }; |
| 348 | |
| 349 | GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 350 | return new GLSLMeshTestProcessor; |
| 351 | } |
| 352 | |
| 353 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 354 | |
| 355 | template<typename T> |
| 356 | sk_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 | |
| 364 | sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() { |
| 365 | GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 366 | return fState->resourceProvider()->findOrCreatePatternedIndexBuffer( |
| 367 | kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | void DrawMeshHelper::drawMesh(const GrMesh& mesh) { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 371 | GrRenderTargetProxy* proxy = fState->drawOpArgs().fProxy; |
| 372 | GrPipeline pipeline(proxy, GrPipeline::ScissorState::kDisabled, SkBlendMode::kSrc); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 373 | GrMeshTestProcessor mtp(mesh.isInstanced(), mesh.hasVertexData()); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 374 | fState->rtCommandBuffer()->draw(pipeline, mtp, &mesh, nullptr, 1, |
| 375 | SkRect::MakeIWH(kImageWidth, kImageHeight)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static 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 Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 393 | rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 394 | 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 |