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 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 11 | #include <array> |
| 12 | #include <vector> |
| 13 | #include "GrCaps.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 14 | #include "GrContext.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 15 | #include "GrContextPriv.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 16 | #include "GrGeometryProcessor.h" |
Robert Phillips | 009e9af | 2017-06-15 14:01:04 -0400 | [diff] [blame] | 17 | #include "GrGpuCommandBuffer.h" |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 18 | #include "GrMemoryPool.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 19 | #include "GrOpFlushState.h" |
| 20 | #include "GrRenderTargetContext.h" |
| 21 | #include "GrRenderTargetContextPriv.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 22 | #include "GrResourceKey.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 23 | #include "GrResourceProvider.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 24 | #include "SkBitmap.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 25 | #include "SkMakeUnique.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 26 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 27 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 28 | #include "glsl/GrGLSLVarying.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 29 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 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 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 79 | static 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 Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 82 | |
| 83 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrMeshTest, reporter, ctxInfo) { |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 84 | GrContext* context = ctxInfo.grContext(); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 85 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 86 | sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext( |
| 87 | SkBackingFit::kExact, kImageWidth, kImageHeight, |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 88 | 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 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 141 | run_test(context, "setNonIndexedNonInstanced", reporter, rtc, gold, |
| 142 | [&](DrawMeshHelper* helper) { |
| 143 | SkTArray<Box> expandedVertexData; |
| 144 | for (int i = 0; i < kBoxCount; ++i) { |
| 145 | for (int j = 0; j < 6; ++j) { |
| 146 | expandedVertexData.push_back(vertexData[i][kIndexPattern[j]]); |
| 147 | } |
| 148 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 149 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 150 | // Draw boxes one line at a time to exercise base vertex. |
| 151 | auto vbuff = helper->makeVertexBuffer(expandedVertexData); |
| 152 | VALIDATE(vbuff); |
| 153 | for (int y = 0; y < kBoxCountY; ++y) { |
| 154 | GrMesh mesh(GrPrimitiveType::kTriangles); |
| 155 | mesh.setNonIndexedNonInstanced(kBoxCountX * 6); |
| 156 | mesh.setVertexData(vbuff.get(), y * kBoxCountX * 6); |
| 157 | helper->drawMesh(mesh); |
| 158 | } |
| 159 | }); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 160 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 161 | run_test(context, "setIndexed", reporter, rtc, gold, [&](DrawMeshHelper* helper) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 162 | auto ibuff = helper->getIndexBuffer(); |
| 163 | VALIDATE(ibuff); |
| 164 | auto vbuff = helper->makeVertexBuffer(vertexData); |
| 165 | VALIDATE(vbuff); |
| 166 | int baseRepetition = 0; |
| 167 | int i = 0; |
| 168 | |
| 169 | // Start at various repetitions within the patterned index buffer to exercise base index. |
| 170 | while (i < kBoxCount) { |
| 171 | GR_STATIC_ASSERT(kIndexPatternRepeatCount >= 3); |
| 172 | int repetitionCount = SkTMin(3 - baseRepetition, kBoxCount - i); |
| 173 | |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 174 | GrMesh mesh(GrPrimitiveType::kTriangles); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 175 | mesh.setIndexed(ibuff.get(), repetitionCount * 6, baseRepetition * 6, |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 176 | baseRepetition * 4, (baseRepetition + repetitionCount) * 4 - 1, |
| 177 | GrPrimitiveRestart::kNo); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 178 | mesh.setVertexData(vbuff.get(), (i - baseRepetition) * 4); |
| 179 | helper->drawMesh(mesh); |
| 180 | |
| 181 | baseRepetition = (baseRepetition + 1) % 3; |
| 182 | i += repetitionCount; |
| 183 | } |
| 184 | }); |
| 185 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 186 | run_test(context, "setIndexedPatterned", reporter, rtc, gold, [&](DrawMeshHelper* helper) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 187 | auto ibuff = helper->getIndexBuffer(); |
| 188 | VALIDATE(ibuff); |
| 189 | auto vbuff = helper->makeVertexBuffer(vertexData); |
| 190 | VALIDATE(vbuff); |
| 191 | |
| 192 | // Draw boxes one line at a time to exercise base vertex. setIndexedPatterned does not |
| 193 | // support a base index. |
| 194 | for (int y = 0; y < kBoxCountY; ++y) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 195 | GrMesh mesh(GrPrimitiveType::kTriangles); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 196 | mesh.setIndexedPatterned(ibuff.get(), 6, 4, kBoxCountX, kIndexPatternRepeatCount); |
| 197 | mesh.setVertexData(vbuff.get(), y * kBoxCountX * 4); |
| 198 | helper->drawMesh(mesh); |
| 199 | } |
| 200 | }); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 201 | |
| 202 | for (bool indexed : {false, true}) { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 203 | if (!context->contextPriv().caps()->instanceAttribSupport()) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 207 | run_test(context, indexed ? "setIndexedInstanced" : "setInstanced", |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 208 | reporter, rtc, gold, [&](DrawMeshHelper* helper) { |
| 209 | auto idxbuff = indexed ? helper->getIndexBuffer() : nullptr; |
| 210 | auto instbuff = helper->makeVertexBuffer(boxes); |
| 211 | VALIDATE(instbuff); |
| 212 | auto vbuff = helper->makeVertexBuffer(std::vector<float>{0,0, 0,1, 1,0, 1,1}); |
| 213 | VALIDATE(vbuff); |
| 214 | auto vbuff2 = helper->makeVertexBuffer( // for testing base vertex. |
| 215 | std::vector<float>{-1,-1, -1,-1, 0,0, 0,1, 1,0, 1,1}); |
| 216 | VALIDATE(vbuff2); |
| 217 | |
| 218 | // Draw boxes one line at a time to exercise base instance, base vertex, and null vertex |
| 219 | // buffer. setIndexedInstanced intentionally does not support a base index. |
| 220 | for (int y = 0; y < kBoxCountY; ++y) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 221 | GrMesh mesh(indexed ? GrPrimitiveType::kTriangles |
| 222 | : GrPrimitiveType::kTriangleStrip); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 223 | if (indexed) { |
| 224 | VALIDATE(idxbuff); |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 225 | mesh.setIndexedInstanced(idxbuff.get(), 6, instbuff.get(), kBoxCountX, |
| 226 | y * kBoxCountX, GrPrimitiveRestart::kNo); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 227 | } else { |
| 228 | mesh.setInstanced(instbuff.get(), kBoxCountX, y * kBoxCountX, 4); |
| 229 | } |
| 230 | switch (y % 3) { |
| 231 | case 0: |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 232 | if (context->contextPriv().caps()->shaderCaps()->vertexIDSupport()) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 233 | if (y % 2) { |
| 234 | // We don't need this call because it's the initial state of GrMesh. |
| 235 | mesh.setVertexData(nullptr); |
| 236 | } |
| 237 | break; |
| 238 | } |
| 239 | // Fallthru. |
| 240 | case 1: |
| 241 | mesh.setVertexData(vbuff.get()); |
| 242 | break; |
| 243 | case 2: |
| 244 | mesh.setVertexData(vbuff2.get(), 2); |
| 245 | break; |
| 246 | } |
| 247 | helper->drawMesh(mesh); |
| 248 | } |
| 249 | }); |
| 250 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 254 | |
| 255 | class GrMeshTestOp : public GrDrawOp { |
| 256 | public: |
| 257 | DEFINE_OP_CLASS_ID |
| 258 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 259 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 260 | std::function<void(DrawMeshHelper*)> testFn) { |
| 261 | return std::unique_ptr<GrDrawOp>(new GrMeshTestOp(testFn)); |
| 262 | } |
| 263 | |
| 264 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 265 | friend class GrOpMemoryPool; // for ctor |
| 266 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 267 | GrMeshTestOp(std::function<void(DrawMeshHelper*)> testFn) |
| 268 | : INHERITED(ClassID()) |
| 269 | , fTestFn(testFn) { |
| 270 | this->setBounds(SkRect::MakeIWH(kImageWidth, kImageHeight), |
| 271 | HasAABloat::kNo, IsZeroArea::kNo); |
| 272 | } |
| 273 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 274 | const char* name() const override { return "GrMeshTestOp"; } |
| 275 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 276 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*, |
| 277 | GrPixelConfigIsClamped) override { |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 278 | return RequiresDstTexture::kNo; |
| 279 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 280 | bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; } |
| 281 | void onPrepare(GrOpFlushState*) override {} |
| 282 | void onExecute(GrOpFlushState* state) override { |
| 283 | DrawMeshHelper helper(state); |
| 284 | fTestFn(&helper); |
| 285 | } |
| 286 | |
| 287 | std::function<void(DrawMeshHelper*)> fTestFn; |
| 288 | |
| 289 | typedef GrDrawOp INHERITED; |
| 290 | }; |
| 291 | |
| 292 | class GrMeshTestProcessor : public GrGeometryProcessor { |
| 293 | public: |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 294 | GrMeshTestProcessor(bool instanced, bool hasVertexBuffer) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 295 | : INHERITED(kGrMeshTestProcessor_ClassID) |
| 296 | , fInstanceLocation(nullptr) |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 297 | , fVertex(nullptr) |
| 298 | , fColor(nullptr) { |
| 299 | if (instanced) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 300 | fInstanceLocation = &this->addInstanceAttrib("location", kHalf2_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 301 | if (hasVertexBuffer) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 302 | fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 303 | } |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 304 | fColor = &this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 305 | } else { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 306 | fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType); |
| 307 | fColor = &this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 308 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | const char* name() const override { return "GrMeshTest Processor"; } |
| 312 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 313 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final { |
| 314 | b->add32(SkToBool(fInstanceLocation)); |
| 315 | b->add32(SkToBool(fVertex)); |
| 316 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 317 | |
| 318 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 319 | |
| 320 | protected: |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 321 | const Attribute* fInstanceLocation; |
| 322 | const Attribute* fVertex; |
| 323 | const Attribute* fColor; |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 324 | |
| 325 | friend class GLSLMeshTestProcessor; |
| 326 | typedef GrGeometryProcessor INHERITED; |
| 327 | }; |
| 328 | |
| 329 | class GLSLMeshTestProcessor : public GrGLSLGeometryProcessor { |
| 330 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&, |
| 331 | FPCoordTransformIter&& transformIter) final {} |
| 332 | |
| 333 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final { |
| 334 | const GrMeshTestProcessor& mp = args.fGP.cast<GrMeshTestProcessor>(); |
| 335 | |
| 336 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 337 | varyingHandler->emitAttributes(mp); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 338 | varyingHandler->addPassThroughAttribute(mp.fColor, args.fOutputColor); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 339 | |
| 340 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 341 | if (!mp.fInstanceLocation) { |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 342 | v->codeAppendf("float2 vertex = %s;", mp.fVertex->name()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 343 | } else { |
| 344 | if (mp.fVertex) { |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 345 | v->codeAppendf("float2 offset = %s;", mp.fVertex->name()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 346 | } else { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 347 | v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);"); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 348 | } |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 349 | v->codeAppendf("float2 vertex = %s + offset * %i;", mp.fInstanceLocation->name(), |
| 350 | kBoxSize); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 351 | } |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 352 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex"); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 353 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 354 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 355 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 356 | } |
| 357 | }; |
| 358 | |
| 359 | GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 360 | return new GLSLMeshTestProcessor; |
| 361 | } |
| 362 | |
| 363 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 364 | |
| 365 | template<typename T> |
| 366 | sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count) { |
| 367 | return sk_sp<const GrBuffer>( |
| 368 | fState->resourceProvider()->createBuffer( |
| 369 | count * sizeof(T), kVertex_GrBufferType, kDynamic_GrAccessPattern, |
| 370 | GrResourceProvider::kNoPendingIO_Flag | |
| 371 | GrResourceProvider::kRequireGpuMemory_Flag, data)); |
| 372 | } |
| 373 | |
| 374 | sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() { |
| 375 | GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 376 | return fState->resourceProvider()->findOrCreatePatternedIndexBuffer( |
| 377 | kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | void DrawMeshHelper::drawMesh(const GrMesh& mesh) { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 381 | GrRenderTargetProxy* proxy = fState->drawOpArgs().fProxy; |
| 382 | GrPipeline pipeline(proxy, GrPipeline::ScissorState::kDisabled, SkBlendMode::kSrc); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 383 | GrMeshTestProcessor mtp(mesh.isInstanced(), mesh.hasVertexData()); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 384 | fState->rtCommandBuffer()->draw(pipeline, mtp, &mesh, nullptr, 1, |
| 385 | SkRect::MakeIWH(kImageWidth, kImageHeight)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 386 | } |
| 387 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 388 | static void run_test(GrContext* context, const char* testName, skiatest::Reporter* reporter, |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 389 | const sk_sp<GrRenderTargetContext>& rtc, const SkBitmap& gold, |
| 390 | std::function<void(DrawMeshHelper*)> testFn) { |
| 391 | const int w = gold.width(), h = gold.height(), rowBytes = gold.rowBytes(); |
| 392 | const uint32_t* goldPx = reinterpret_cast<const uint32_t*>(gold.getPixels()); |
| 393 | if (h != rtc->height() || w != rtc->width()) { |
| 394 | ERRORF(reporter, "[%s] expectation and rtc not compatible (?).", testName); |
| 395 | return; |
| 396 | } |
| 397 | if (sizeof(uint32_t) * kImageWidth != gold.rowBytes()) { |
| 398 | ERRORF(reporter, "unexpected row bytes in gold image.", testName); |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | SkAutoSTMalloc<kImageHeight * kImageWidth, uint32_t> resultPx(h * rowBytes); |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 403 | rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes); |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 404 | rtc->priv().testingOnly_addDrawOp(GrMeshTestOp::Make(context, testFn)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 405 | rtc->readPixels(gold.info(), resultPx, rowBytes, 0, 0, 0); |
| 406 | for (int y = 0; y < h; ++y) { |
| 407 | for (int x = 0; x < w; ++x) { |
| 408 | uint32_t expected = goldPx[y * kImageWidth + x]; |
| 409 | uint32_t actual = resultPx[y * kImageWidth + x]; |
| 410 | if (expected != actual) { |
| 411 | ERRORF(reporter, "[%s] pixel (%i,%i): got 0x%x expected 0x%x", |
| 412 | testName, x, y, actual, expected); |
| 413 | return; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |