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" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 18 | #include "GrOpFlushState.h" |
| 19 | #include "GrRenderTargetContext.h" |
| 20 | #include "GrRenderTargetContextPriv.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 21 | #include "GrResourceKey.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 22 | #include "GrResourceProvider.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 23 | #include "SkBitmap.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 24 | #include "SkMakeUnique.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 25 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
| 26 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 27 | #include "glsl/GrGLSLVarying.h" |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 28 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 29 | |
| 30 | GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 31 | |
| 32 | static constexpr int kBoxSize = 2; |
| 33 | static constexpr int kBoxCountY = 8; |
| 34 | static constexpr int kBoxCountX = 8; |
| 35 | static constexpr int kBoxCount = kBoxCountY * kBoxCountX; |
| 36 | |
| 37 | static constexpr int kImageWidth = kBoxCountY * kBoxSize; |
| 38 | static constexpr int kImageHeight = kBoxCountX * kBoxSize; |
| 39 | |
| 40 | static constexpr int kIndexPatternRepeatCount = 3; |
| 41 | constexpr uint16_t kIndexPattern[6] = {0, 1, 2, 1, 2, 3}; |
| 42 | |
| 43 | |
| 44 | class DrawMeshHelper { |
| 45 | public: |
| 46 | DrawMeshHelper(GrOpFlushState* state) : fState(state) {} |
| 47 | |
| 48 | sk_sp<const GrBuffer> getIndexBuffer(); |
| 49 | |
| 50 | template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const SkTArray<T>& data) { |
| 51 | return this->makeVertexBuffer(data.begin(), data.count()); |
| 52 | } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 53 | template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const std::vector<T>& data) { |
| 54 | return this->makeVertexBuffer(data.data(), data.size()); |
| 55 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 56 | template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const T* data, int count); |
| 57 | |
| 58 | void drawMesh(const GrMesh& mesh); |
| 59 | |
| 60 | private: |
| 61 | GrOpFlushState* fState; |
| 62 | }; |
| 63 | |
| 64 | struct Box { |
| 65 | float fX, fY; |
| 66 | GrColor fColor; |
| 67 | }; |
| 68 | |
| 69 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 70 | |
| 71 | /** |
| 72 | * This is a GPU-backend specific test. It tries to test all possible usecases of GrMesh. The test |
| 73 | * works by drawing checkerboards of colored boxes, reading back the pixels, and comparing with |
| 74 | * expected results. The boxes are drawn on integer boundaries and the (opaque) colors are chosen |
| 75 | * from the set (r,g,b) = (0,255)^3, so the GPU renderings ought to produce exact matches. |
| 76 | */ |
| 77 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame^] | 78 | static void run_test(GrContext* context, const char* testName, skiatest::Reporter*, |
| 79 | const sk_sp<GrRenderTargetContext>&, const SkBitmap& gold, |
| 80 | std::function<void(DrawMeshHelper*)> testFn); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 81 | |
| 82 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrMeshTest, reporter, ctxInfo) { |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame^] | 83 | GrContext* context = ctxInfo.grContext(); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 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 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame^] | 140 | run_test(context, "setNonIndexedNonInstanced", reporter, rtc, gold, |
| 141 | [&](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 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 148 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame^] | 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) { |
| 153 | GrMesh mesh(GrPrimitiveType::kTriangles); |
| 154 | mesh.setNonIndexedNonInstanced(kBoxCountX * 6); |
| 155 | mesh.setVertexData(vbuff.get(), y * kBoxCountX * 6); |
| 156 | helper->drawMesh(mesh); |
| 157 | } |
| 158 | }); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 159 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame^] | 160 | run_test(context, "setIndexed", reporter, rtc, gold, [&](DrawMeshHelper* helper) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 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 Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 173 | GrMesh mesh(GrPrimitiveType::kTriangles); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 174 | 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 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame^] | 184 | run_test(context, "setIndexedPatterned", reporter, rtc, gold, [&](DrawMeshHelper* helper) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 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 Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 193 | GrMesh mesh(GrPrimitiveType::kTriangles); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 194 | mesh.setIndexedPatterned(ibuff.get(), 6, 4, kBoxCountX, kIndexPatternRepeatCount); |
| 195 | mesh.setVertexData(vbuff.get(), y * kBoxCountX * 4); |
| 196 | helper->drawMesh(mesh); |
| 197 | } |
| 198 | }); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 199 | |
| 200 | for (bool indexed : {false, true}) { |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 201 | if (!context->contextPriv().caps()->instanceAttribSupport()) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 202 | break; |
| 203 | } |
| 204 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame^] | 205 | run_test(context, indexed ? "setIndexedInstanced" : "setInstanced", |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 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 Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 219 | GrMesh mesh(indexed ? GrPrimitiveType::kTriangles |
| 220 | : GrPrimitiveType::kTriangleStrip); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 221 | 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 Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 230 | if (context->contextPriv().caps()->shaderCaps()->vertexIDSupport()) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 231 | 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 Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 252 | |
| 253 | class GrMeshTestOp : public GrDrawOp { |
| 254 | public: |
| 255 | DEFINE_OP_CLASS_ID |
| 256 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame^] | 257 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 258 | std::function<void(DrawMeshHelper*)> testFn) { |
| 259 | return std::unique_ptr<GrDrawOp>(new GrMeshTestOp(testFn)); |
| 260 | } |
| 261 | |
| 262 | private: |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 263 | GrMeshTestOp(std::function<void(DrawMeshHelper*)> testFn) |
| 264 | : INHERITED(ClassID()) |
| 265 | , fTestFn(testFn) { |
| 266 | this->setBounds(SkRect::MakeIWH(kImageWidth, kImageHeight), |
| 267 | HasAABloat::kNo, IsZeroArea::kNo); |
| 268 | } |
| 269 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 270 | const char* name() const override { return "GrMeshTestOp"; } |
| 271 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 272 | RequiresDstTexture finalize(const GrCaps&, const GrAppliedClip*, |
| 273 | GrPixelConfigIsClamped) override { |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 274 | return RequiresDstTexture::kNo; |
| 275 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 276 | bool onCombineIfPossible(GrOp* other, const GrCaps& caps) override { return false; } |
| 277 | void onPrepare(GrOpFlushState*) override {} |
| 278 | void onExecute(GrOpFlushState* state) override { |
| 279 | DrawMeshHelper helper(state); |
| 280 | fTestFn(&helper); |
| 281 | } |
| 282 | |
| 283 | std::function<void(DrawMeshHelper*)> fTestFn; |
| 284 | |
| 285 | typedef GrDrawOp INHERITED; |
| 286 | }; |
| 287 | |
| 288 | class GrMeshTestProcessor : public GrGeometryProcessor { |
| 289 | public: |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 290 | GrMeshTestProcessor(bool instanced, bool hasVertexBuffer) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 291 | : INHERITED(kGrMeshTestProcessor_ClassID) |
| 292 | , fInstanceLocation(nullptr) |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 293 | , fVertex(nullptr) |
| 294 | , fColor(nullptr) { |
| 295 | if (instanced) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 296 | fInstanceLocation = &this->addInstanceAttrib("location", kHalf2_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 297 | if (hasVertexBuffer) { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 298 | fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 299 | } |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 300 | fColor = &this->addInstanceAttrib("color", kUByte4_norm_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 301 | } else { |
Ethan Nicholas | fa7ee24 | 2017-09-25 09:52:04 -0400 | [diff] [blame] | 302 | fVertex = &this->addVertexAttrib("vertex", kHalf2_GrVertexAttribType); |
| 303 | fColor = &this->addVertexAttrib("color", kUByte4_norm_GrVertexAttribType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 304 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | const char* name() const override { return "GrMeshTest Processor"; } |
| 308 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 309 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final { |
| 310 | b->add32(SkToBool(fInstanceLocation)); |
| 311 | b->add32(SkToBool(fVertex)); |
| 312 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 313 | |
| 314 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 315 | |
| 316 | protected: |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 317 | const Attribute* fInstanceLocation; |
| 318 | const Attribute* fVertex; |
| 319 | const Attribute* fColor; |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 320 | |
| 321 | friend class GLSLMeshTestProcessor; |
| 322 | typedef GrGeometryProcessor INHERITED; |
| 323 | }; |
| 324 | |
| 325 | class GLSLMeshTestProcessor : public GrGLSLGeometryProcessor { |
| 326 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&, |
| 327 | FPCoordTransformIter&& transformIter) final {} |
| 328 | |
| 329 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final { |
| 330 | const GrMeshTestProcessor& mp = args.fGP.cast<GrMeshTestProcessor>(); |
| 331 | |
| 332 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 333 | varyingHandler->emitAttributes(mp); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 334 | varyingHandler->addPassThroughAttribute(mp.fColor, args.fOutputColor); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 335 | |
| 336 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 337 | if (!mp.fInstanceLocation) { |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 338 | v->codeAppendf("float2 vertex = %s;", mp.fVertex->name()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 339 | } else { |
| 340 | if (mp.fVertex) { |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 341 | v->codeAppendf("float2 offset = %s;", mp.fVertex->name()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 342 | } else { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 343 | v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);"); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 344 | } |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 345 | v->codeAppendf("float2 vertex = %s + offset * %i;", mp.fInstanceLocation->name(), |
| 346 | kBoxSize); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 347 | } |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 348 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex"); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 349 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 350 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 351 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 352 | } |
| 353 | }; |
| 354 | |
| 355 | GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 356 | return new GLSLMeshTestProcessor; |
| 357 | } |
| 358 | |
| 359 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 360 | |
| 361 | template<typename T> |
| 362 | sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count) { |
| 363 | return sk_sp<const GrBuffer>( |
| 364 | fState->resourceProvider()->createBuffer( |
| 365 | count * sizeof(T), kVertex_GrBufferType, kDynamic_GrAccessPattern, |
| 366 | GrResourceProvider::kNoPendingIO_Flag | |
| 367 | GrResourceProvider::kRequireGpuMemory_Flag, data)); |
| 368 | } |
| 369 | |
| 370 | sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() { |
| 371 | GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 372 | return fState->resourceProvider()->findOrCreatePatternedIndexBuffer( |
| 373 | kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | void DrawMeshHelper::drawMesh(const GrMesh& mesh) { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 377 | GrRenderTargetProxy* proxy = fState->drawOpArgs().fProxy; |
| 378 | GrPipeline pipeline(proxy, GrPipeline::ScissorState::kDisabled, SkBlendMode::kSrc); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 379 | GrMeshTestProcessor mtp(mesh.isInstanced(), mesh.hasVertexData()); |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 380 | fState->rtCommandBuffer()->draw(pipeline, mtp, &mesh, nullptr, 1, |
| 381 | SkRect::MakeIWH(kImageWidth, kImageHeight)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 382 | } |
| 383 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame^] | 384 | static void run_test(GrContext* context, const char* testName, skiatest::Reporter* reporter, |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 385 | const sk_sp<GrRenderTargetContext>& rtc, const SkBitmap& gold, |
| 386 | std::function<void(DrawMeshHelper*)> testFn) { |
| 387 | const int w = gold.width(), h = gold.height(), rowBytes = gold.rowBytes(); |
| 388 | const uint32_t* goldPx = reinterpret_cast<const uint32_t*>(gold.getPixels()); |
| 389 | if (h != rtc->height() || w != rtc->width()) { |
| 390 | ERRORF(reporter, "[%s] expectation and rtc not compatible (?).", testName); |
| 391 | return; |
| 392 | } |
| 393 | if (sizeof(uint32_t) * kImageWidth != gold.rowBytes()) { |
| 394 | ERRORF(reporter, "unexpected row bytes in gold image.", testName); |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | SkAutoSTMalloc<kImageHeight * kImageWidth, uint32_t> resultPx(h * rowBytes); |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 399 | rtc->clear(nullptr, 0xbaaaaaad, GrRenderTargetContext::CanClearFullscreen::kYes); |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame^] | 400 | rtc->priv().testingOnly_addDrawOp(GrMeshTestOp::Make(context, testFn)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 401 | rtc->readPixels(gold.info(), resultPx, rowBytes, 0, 0, 0); |
| 402 | for (int y = 0; y < h; ++y) { |
| 403 | for (int x = 0; x < w; ++x) { |
| 404 | uint32_t expected = goldPx[y * kImageWidth + x]; |
| 405 | uint32_t actual = resultPx[y * kImageWidth + x]; |
| 406 | if (expected != actual) { |
| 407 | ERRORF(reporter, "[%s] pixel (%i,%i): got 0x%x expected 0x%x", |
| 408 | testName, x, y, actual, expected); |
| 409 | return; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |