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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
| 9 | #include "tests/Test.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 10 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 11 | #include <array> |
| 12 | #include <vector> |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkBitmap.h" |
| 14 | #include "include/gpu/GrContext.h" |
| 15 | #include "include/private/GrResourceKey.h" |
| 16 | #include "src/core/SkMakeUnique.h" |
| 17 | #include "src/gpu/GrCaps.h" |
| 18 | #include "src/gpu/GrContextPriv.h" |
| 19 | #include "src/gpu/GrGeometryProcessor.h" |
| 20 | #include "src/gpu/GrGpuCommandBuffer.h" |
| 21 | #include "src/gpu/GrMemoryPool.h" |
| 22 | #include "src/gpu/GrOpFlushState.h" |
| 23 | #include "src/gpu/GrRenderTargetContext.h" |
| 24 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 25 | #include "src/gpu/GrResourceProvider.h" |
| 26 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 27 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 28 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 29 | #include "src/gpu/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 | |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 86 | const GrBackendFormat format = |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 87 | context->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType); |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 88 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 89 | sk_sp<GrRenderTargetContext> rtc(context->priv().makeDeferredRenderTargetContext( |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 90 | format, SkBackingFit::kExact, kImageWidth, kImageHeight, kRGBA_8888_GrPixelConfig, |
| 91 | nullptr)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 92 | if (!rtc) { |
| 93 | ERRORF(reporter, "could not create render target context."); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | SkTArray<Box> boxes; |
| 98 | SkTArray<std::array<Box, 4>> vertexData; |
| 99 | SkBitmap gold; |
| 100 | |
| 101 | // ---- setup ---------- |
| 102 | |
| 103 | SkPaint paint; |
| 104 | paint.setBlendMode(SkBlendMode::kSrc); |
| 105 | gold.allocN32Pixels(kImageWidth, kImageHeight); |
| 106 | |
| 107 | SkCanvas goldCanvas(gold); |
| 108 | |
| 109 | for (int y = 0; y < kBoxCountY; ++y) { |
| 110 | for (int x = 0; x < kBoxCountX; ++x) { |
| 111 | int c = y + x; |
| 112 | int rgb[3] = {-(c & 1) & 0xff, -((c >> 1) & 1) & 0xff, -((c >> 2) & 1) & 0xff}; |
| 113 | |
| 114 | const Box box = boxes.push_back() = { |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 115 | float(x * kBoxSize), |
| 116 | float(y * kBoxSize), |
| 117 | GrColorPackRGBA(rgb[0], rgb[1], rgb[2], 255) |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | std::array<Box, 4>& boxVertices = vertexData.push_back(); |
| 121 | for (int i = 0; i < 4; ++i) { |
| 122 | boxVertices[i] = { |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 123 | box.fX + (i / 2) * kBoxSize, |
| 124 | box.fY + (i % 2) * kBoxSize, |
| 125 | box.fColor |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 126 | }; |
| 127 | } |
| 128 | |
| 129 | paint.setARGB(255, rgb[0], rgb[1], rgb[2]); |
| 130 | goldCanvas.drawRect(SkRect::MakeXYWH(box.fX, box.fY, kBoxSize, kBoxSize), paint); |
| 131 | } |
| 132 | } |
| 133 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 134 | // ---- tests ---------- |
| 135 | |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 136 | #define VALIDATE(buff) \ |
| 137 | do { \ |
| 138 | if (!buff) { \ |
| 139 | ERRORF(reporter, #buff " is null."); \ |
| 140 | return; \ |
| 141 | } \ |
| 142 | } while (0) |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 143 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 144 | run_test(context, "setNonIndexedNonInstanced", reporter, rtc, gold, |
| 145 | [&](DrawMeshHelper* helper) { |
| 146 | SkTArray<Box> expandedVertexData; |
| 147 | for (int i = 0; i < kBoxCount; ++i) { |
| 148 | for (int j = 0; j < 6; ++j) { |
| 149 | expandedVertexData.push_back(vertexData[i][kIndexPattern[j]]); |
| 150 | } |
| 151 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 152 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 153 | // Draw boxes one line at a time to exercise base vertex. |
| 154 | auto vbuff = helper->makeVertexBuffer(expandedVertexData); |
| 155 | VALIDATE(vbuff); |
| 156 | for (int y = 0; y < kBoxCountY; ++y) { |
| 157 | GrMesh mesh(GrPrimitiveType::kTriangles); |
| 158 | mesh.setNonIndexedNonInstanced(kBoxCountX * 6); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 159 | mesh.setVertexData(vbuff, y * kBoxCountX * 6); |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 160 | helper->drawMesh(mesh); |
| 161 | } |
| 162 | }); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 163 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 164 | run_test(context, "setIndexed", reporter, rtc, gold, [&](DrawMeshHelper* helper) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 165 | auto ibuff = helper->getIndexBuffer(); |
| 166 | VALIDATE(ibuff); |
| 167 | auto vbuff = helper->makeVertexBuffer(vertexData); |
| 168 | VALIDATE(vbuff); |
| 169 | int baseRepetition = 0; |
| 170 | int i = 0; |
| 171 | |
| 172 | // Start at various repetitions within the patterned index buffer to exercise base index. |
| 173 | while (i < kBoxCount) { |
| 174 | GR_STATIC_ASSERT(kIndexPatternRepeatCount >= 3); |
| 175 | int repetitionCount = SkTMin(3 - baseRepetition, kBoxCount - i); |
| 176 | |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 177 | GrMesh mesh(GrPrimitiveType::kTriangles); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 178 | mesh.setIndexed(ibuff, repetitionCount * 6, baseRepetition * 6, baseRepetition * 4, |
| 179 | (baseRepetition + repetitionCount) * 4 - 1, GrPrimitiveRestart::kNo); |
| 180 | mesh.setVertexData(vbuff, (i - baseRepetition) * 4); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 181 | helper->drawMesh(mesh); |
| 182 | |
| 183 | baseRepetition = (baseRepetition + 1) % 3; |
| 184 | i += repetitionCount; |
| 185 | } |
| 186 | }); |
| 187 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 188 | run_test(context, "setIndexedPatterned", reporter, rtc, gold, [&](DrawMeshHelper* helper) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 189 | auto ibuff = helper->getIndexBuffer(); |
| 190 | VALIDATE(ibuff); |
| 191 | auto vbuff = helper->makeVertexBuffer(vertexData); |
| 192 | VALIDATE(vbuff); |
| 193 | |
| 194 | // Draw boxes one line at a time to exercise base vertex. setIndexedPatterned does not |
| 195 | // support a base index. |
| 196 | for (int y = 0; y < kBoxCountY; ++y) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 197 | GrMesh mesh(GrPrimitiveType::kTriangles); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 198 | mesh.setIndexedPatterned(ibuff, 6, 4, kBoxCountX, kIndexPatternRepeatCount); |
| 199 | mesh.setVertexData(vbuff, y * kBoxCountX * 4); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 200 | helper->drawMesh(mesh); |
| 201 | } |
| 202 | }); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 203 | |
| 204 | for (bool indexed : {false, true}) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 205 | if (!context->priv().caps()->instanceAttribSupport()) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 206 | break; |
| 207 | } |
| 208 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 209 | run_test(context, indexed ? "setIndexedInstanced" : "setInstanced", |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 210 | reporter, rtc, gold, [&](DrawMeshHelper* helper) { |
| 211 | auto idxbuff = indexed ? helper->getIndexBuffer() : nullptr; |
| 212 | auto instbuff = helper->makeVertexBuffer(boxes); |
| 213 | VALIDATE(instbuff); |
| 214 | auto vbuff = helper->makeVertexBuffer(std::vector<float>{0,0, 0,1, 1,0, 1,1}); |
| 215 | VALIDATE(vbuff); |
| 216 | auto vbuff2 = helper->makeVertexBuffer( // for testing base vertex. |
| 217 | std::vector<float>{-1,-1, -1,-1, 0,0, 0,1, 1,0, 1,1}); |
| 218 | VALIDATE(vbuff2); |
| 219 | |
| 220 | // Draw boxes one line at a time to exercise base instance, base vertex, and null vertex |
| 221 | // buffer. setIndexedInstanced intentionally does not support a base index. |
| 222 | for (int y = 0; y < kBoxCountY; ++y) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 223 | GrMesh mesh(indexed ? GrPrimitiveType::kTriangles |
| 224 | : GrPrimitiveType::kTriangleStrip); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 225 | if (indexed) { |
| 226 | VALIDATE(idxbuff); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 227 | mesh.setIndexedInstanced(idxbuff, 6, instbuff, kBoxCountX, y * kBoxCountX, |
| 228 | GrPrimitiveRestart::kNo); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 229 | } else { |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 230 | mesh.setInstanced(instbuff, kBoxCountX, y * kBoxCountX, 4); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 231 | } |
| 232 | switch (y % 3) { |
| 233 | case 0: |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 234 | if (context->priv().caps()->shaderCaps()->vertexIDSupport()) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 235 | if (y % 2) { |
| 236 | // We don't need this call because it's the initial state of GrMesh. |
| 237 | mesh.setVertexData(nullptr); |
| 238 | } |
| 239 | break; |
| 240 | } |
| 241 | // Fallthru. |
| 242 | case 1: |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 243 | mesh.setVertexData(vbuff); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 244 | break; |
| 245 | case 2: |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 246 | mesh.setVertexData(vbuff2, 2); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 247 | break; |
| 248 | } |
| 249 | helper->drawMesh(mesh); |
| 250 | } |
| 251 | }); |
| 252 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 256 | |
| 257 | class GrMeshTestOp : public GrDrawOp { |
| 258 | public: |
| 259 | DEFINE_OP_CLASS_ID |
| 260 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 261 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 262 | std::function<void(DrawMeshHelper*)> testFn) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 263 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 264 | |
| 265 | return pool->allocate<GrMeshTestOp>(testFn); |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 269 | friend class GrOpMemoryPool; // for ctor |
| 270 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 271 | GrMeshTestOp(std::function<void(DrawMeshHelper*)> testFn) |
| 272 | : INHERITED(ClassID()) |
| 273 | , fTestFn(testFn) { |
| 274 | this->setBounds(SkRect::MakeIWH(kImageWidth, kImageHeight), |
| 275 | HasAABloat::kNo, IsZeroArea::kNo); |
| 276 | } |
| 277 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 278 | const char* name() const override { return "GrMeshTestOp"; } |
| 279 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 280 | GrProcessorSet::Analysis finalize( |
| 281 | const GrCaps&, const GrAppliedClip*, GrFSAAType, GrClampType) override { |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 282 | return GrProcessorSet::EmptySetAnalysis(); |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 283 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 284 | void onPrepare(GrOpFlushState*) override {} |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 285 | void onExecute(GrOpFlushState* state, const SkRect& chainBounds) override { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 286 | DrawMeshHelper helper(state); |
| 287 | fTestFn(&helper); |
| 288 | } |
| 289 | |
| 290 | std::function<void(DrawMeshHelper*)> fTestFn; |
| 291 | |
| 292 | typedef GrDrawOp INHERITED; |
| 293 | }; |
| 294 | |
| 295 | class GrMeshTestProcessor : public GrGeometryProcessor { |
| 296 | public: |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 297 | GrMeshTestProcessor(bool instanced, bool hasVertexBuffer) |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 298 | : INHERITED(kGrMeshTestProcessor_ClassID) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 299 | if (instanced) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 300 | fInstanceLocation = {"location", kFloat2_GrVertexAttribType, kHalf2_GrSLType}; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 301 | fInstanceColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType}; |
| 302 | this->setInstanceAttributes(&fInstanceLocation, 2); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 303 | if (hasVertexBuffer) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 304 | fVertexPosition = {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType}; |
| 305 | this->setVertexAttributes(&fVertexPosition, 1); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 306 | } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 307 | } else { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 308 | fVertexPosition = {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType}; |
| 309 | fVertexColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType}; |
| 310 | this->setVertexAttributes(&fVertexPosition, 2); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 311 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | const char* name() const override { return "GrMeshTest Processor"; } |
| 315 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 316 | const Attribute& inColor() const { |
| 317 | return fVertexColor.isInitialized() ? fVertexColor : fInstanceColor; |
| 318 | } |
| 319 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 320 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final { |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 321 | b->add32(fInstanceLocation.isInitialized()); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 322 | b->add32(fVertexPosition.isInitialized()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 323 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 324 | |
| 325 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 326 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 327 | private: |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 328 | Attribute fVertexPosition; |
| 329 | Attribute fVertexColor; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 330 | |
| 331 | Attribute fInstanceLocation; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 332 | Attribute fInstanceColor; |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 333 | |
| 334 | friend class GLSLMeshTestProcessor; |
| 335 | typedef GrGeometryProcessor INHERITED; |
| 336 | }; |
| 337 | |
| 338 | class GLSLMeshTestProcessor : public GrGLSLGeometryProcessor { |
| 339 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&, |
| 340 | FPCoordTransformIter&& transformIter) final {} |
| 341 | |
| 342 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final { |
| 343 | const GrMeshTestProcessor& mp = args.fGP.cast<GrMeshTestProcessor>(); |
| 344 | |
| 345 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 346 | varyingHandler->emitAttributes(mp); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 347 | varyingHandler->addPassThroughAttribute(mp.inColor(), args.fOutputColor); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 348 | |
| 349 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 350 | if (!mp.fInstanceLocation.isInitialized()) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 351 | v->codeAppendf("float2 vertex = %s;", mp.fVertexPosition.name()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 352 | } else { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 353 | if (mp.fVertexPosition.isInitialized()) { |
| 354 | v->codeAppendf("float2 offset = %s;", mp.fVertexPosition.name()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 355 | } else { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 356 | v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);"); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 357 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 358 | v->codeAppendf("float2 vertex = %s + offset * %i;", mp.fInstanceLocation.name(), |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 359 | kBoxSize); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 360 | } |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 361 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex"); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 362 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 363 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 364 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 365 | } |
| 366 | }; |
| 367 | |
| 368 | GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 369 | return new GLSLMeshTestProcessor; |
| 370 | } |
| 371 | |
| 372 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 373 | |
| 374 | template<typename T> |
| 375 | sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count) { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 376 | return sk_sp<const GrBuffer>(fState->resourceProvider()->createBuffer( |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 377 | count * sizeof(T), GrGpuBufferType::kVertex, kDynamic_GrAccessPattern, data)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() { |
| 381 | GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 382 | return fState->resourceProvider()->findOrCreatePatternedIndexBuffer( |
| 383 | kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void DrawMeshHelper::drawMesh(const GrMesh& mesh) { |
Robert Phillips | d0fe875 | 2019-01-31 14:13:59 -0500 | [diff] [blame] | 387 | GrPipeline pipeline(GrScissorTest::kDisabled, SkBlendMode::kSrc); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 388 | GrMeshTestProcessor mtp(mesh.isInstanced(), mesh.hasVertexData()); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 389 | fState->rtCommandBuffer()->draw(mtp, pipeline, nullptr, nullptr, &mesh, 1, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 390 | SkRect::MakeIWH(kImageWidth, kImageHeight)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 391 | } |
| 392 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 393 | static void run_test(GrContext* context, const char* testName, skiatest::Reporter* reporter, |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 394 | const sk_sp<GrRenderTargetContext>& rtc, const SkBitmap& gold, |
| 395 | std::function<void(DrawMeshHelper*)> testFn) { |
| 396 | const int w = gold.width(), h = gold.height(), rowBytes = gold.rowBytes(); |
| 397 | const uint32_t* goldPx = reinterpret_cast<const uint32_t*>(gold.getPixels()); |
| 398 | if (h != rtc->height() || w != rtc->width()) { |
| 399 | ERRORF(reporter, "[%s] expectation and rtc not compatible (?).", testName); |
| 400 | return; |
| 401 | } |
| 402 | if (sizeof(uint32_t) * kImageWidth != gold.rowBytes()) { |
| 403 | ERRORF(reporter, "unexpected row bytes in gold image.", testName); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | SkAutoSTMalloc<kImageHeight * kImageWidth, uint32_t> resultPx(h * rowBytes); |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 408 | rtc->clear(nullptr, SkPMColor4f::FromBytes_RGBA(0xbaaaaaad), |
| 409 | GrRenderTargetContext::CanClearFullscreen::kYes); |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 410 | rtc->priv().testingOnly_addDrawOp(GrMeshTestOp::Make(context, testFn)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 411 | rtc->readPixels(gold.info(), resultPx, rowBytes, 0, 0, 0); |
| 412 | for (int y = 0; y < h; ++y) { |
| 413 | for (int x = 0; x < w; ++x) { |
| 414 | uint32_t expected = goldPx[y * kImageWidth + x]; |
| 415 | uint32_t actual = resultPx[y * kImageWidth + x]; |
| 416 | if (expected != actual) { |
| 417 | ERRORF(reporter, "[%s] pixel (%i,%i): got 0x%x expected 0x%x", |
| 418 | testName, x, y, actual, expected); |
| 419 | return; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | } |