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 "tests/Test.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 9 | |
Brian Salomon | c7fe0f7 | 2018-05-11 10:14:21 -0400 | [diff] [blame] | 10 | #include <array> |
| 11 | #include <vector> |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkBitmap.h" |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 13 | #include "include/gpu/GrDirectContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "include/private/GrResourceKey.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrCaps.h" |
| 16 | #include "src/gpu/GrContextPriv.h" |
| 17 | #include "src/gpu/GrGeometryProcessor.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrMemoryPool.h" |
| 20 | #include "src/gpu/GrOpFlushState.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 21 | #include "src/gpu/GrOpsRenderPass.h" |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 22 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 30 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 31 | |
Chris Dalton | d4dec97 | 2020-04-03 11:01:30 -0600 | [diff] [blame] | 32 | #if 0 |
| 33 | #include "tools/ToolUtils.h" |
| 34 | #define WRITE_PNG_CONTEXT_TYPE kANGLE_D3D11_ES3_ContextType |
| 35 | #endif |
| 36 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 37 | GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
| 38 | |
| 39 | static constexpr int kBoxSize = 2; |
| 40 | static constexpr int kBoxCountY = 8; |
| 41 | static constexpr int kBoxCountX = 8; |
| 42 | static constexpr int kBoxCount = kBoxCountY * kBoxCountX; |
| 43 | |
| 44 | static constexpr int kImageWidth = kBoxCountY * kBoxSize; |
| 45 | static constexpr int kImageHeight = kBoxCountX * kBoxSize; |
| 46 | |
| 47 | static constexpr int kIndexPatternRepeatCount = 3; |
| 48 | constexpr uint16_t kIndexPattern[6] = {0, 1, 2, 1, 2, 3}; |
| 49 | |
| 50 | |
| 51 | class DrawMeshHelper { |
| 52 | public: |
| 53 | DrawMeshHelper(GrOpFlushState* state) : fState(state) {} |
| 54 | |
| 55 | sk_sp<const GrBuffer> getIndexBuffer(); |
| 56 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 57 | sk_sp<const GrBuffer> makeIndexBuffer(const uint16_t[], int count); |
| 58 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 59 | template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const SkTArray<T>& data) { |
| 60 | return this->makeVertexBuffer(data.begin(), data.count()); |
| 61 | } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 62 | template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const std::vector<T>& data) { |
| 63 | return this->makeVertexBuffer(data.data(), data.size()); |
| 64 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 65 | template<typename T> sk_sp<const GrBuffer> makeVertexBuffer(const T* data, int count); |
| 66 | |
Chris Dalton | b8d7e00 | 2020-05-13 11:02:10 -0600 | [diff] [blame] | 67 | GrMeshDrawOp::Target* target() { return fState; } |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 68 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 69 | sk_sp<const GrBuffer> fIndexBuffer; |
| 70 | sk_sp<const GrBuffer> fIndexBuffer2; |
| 71 | sk_sp<const GrBuffer> fInstBuffer; |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 72 | sk_sp<const GrBuffer> fVertBuffer; |
| 73 | sk_sp<const GrBuffer> fVertBuffer2; |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 74 | sk_sp<const GrBuffer> fDrawIndirectBuffer; |
| 75 | size_t fDrawIndirectBufferOffset; |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 76 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 77 | GrOpsRenderPass* bindPipeline(GrPrimitiveType, bool isInstanced, bool hasVertexBuffer); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | GrOpFlushState* fState; |
| 81 | }; |
| 82 | |
| 83 | struct Box { |
| 84 | float fX, fY; |
| 85 | GrColor fColor; |
| 86 | }; |
| 87 | |
| 88 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | /** |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 91 | * This is a GPU-backend specific test. It tries to test all possible usecases of |
| 92 | * GrOpsRenderPass::draw*. The test works by drawing checkerboards of colored boxes, reading back |
| 93 | * the pixels, and comparing with expected results. The boxes are drawn on integer boundaries and |
| 94 | * the (opaque) colors are chosen from the set (r,g,b) = (0,255)^3, so the GPU renderings ought to |
| 95 | * produce exact matches. |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 96 | */ |
| 97 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 98 | static void run_test(GrContext* context, const char* testName, skiatest::Reporter*, |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 99 | const std::unique_ptr<GrRenderTargetContext>&, const SkBitmap& gold, |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 100 | std::function<void(DrawMeshHelper*)> prepareFn, |
| 101 | std::function<void(DrawMeshHelper*)> executeFn); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 102 | |
Chris Dalton | d4dec97 | 2020-04-03 11:01:30 -0600 | [diff] [blame] | 103 | #ifdef WRITE_PNG_CONTEXT_TYPE |
| 104 | static bool IsContextTypeForOutputPNGs(skiatest::GrContextFactoryContextType type) { |
| 105 | return type == skiatest::GrContextFactoryContextType::WRITE_PNG_CONTEXT_TYPE; |
| 106 | } |
| 107 | DEF_GPUTEST_FOR_CONTEXTS(GrMeshTest, IsContextTypeForOutputPNGs, reporter, ctxInfo, nullptr) { |
| 108 | #else |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 109 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GrMeshTest, reporter, ctxInfo) { |
Chris Dalton | d4dec97 | 2020-04-03 11:01:30 -0600 | [diff] [blame] | 110 | #endif |
Robert Phillips | 6d344c3 | 2020-07-06 10:56:46 -0400 | [diff] [blame] | 111 | auto context = ctxInfo.directContext(); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 112 | |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 113 | auto rtc = GrRenderTargetContext::Make( |
| 114 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, |
| 115 | {kImageWidth, kImageHeight}); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 116 | if (!rtc) { |
| 117 | ERRORF(reporter, "could not create render target context."); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | SkTArray<Box> boxes; |
| 122 | SkTArray<std::array<Box, 4>> vertexData; |
| 123 | SkBitmap gold; |
| 124 | |
| 125 | // ---- setup ---------- |
| 126 | |
| 127 | SkPaint paint; |
| 128 | paint.setBlendMode(SkBlendMode::kSrc); |
| 129 | gold.allocN32Pixels(kImageWidth, kImageHeight); |
| 130 | |
| 131 | SkCanvas goldCanvas(gold); |
| 132 | |
| 133 | for (int y = 0; y < kBoxCountY; ++y) { |
| 134 | for (int x = 0; x < kBoxCountX; ++x) { |
| 135 | int c = y + x; |
| 136 | int rgb[3] = {-(c & 1) & 0xff, -((c >> 1) & 1) & 0xff, -((c >> 2) & 1) & 0xff}; |
| 137 | |
| 138 | const Box box = boxes.push_back() = { |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 139 | float(x * kBoxSize), |
| 140 | float(y * kBoxSize), |
| 141 | GrColorPackRGBA(rgb[0], rgb[1], rgb[2], 255) |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | std::array<Box, 4>& boxVertices = vertexData.push_back(); |
| 145 | for (int i = 0; i < 4; ++i) { |
| 146 | boxVertices[i] = { |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 147 | box.fX + (i / 2) * kBoxSize, |
| 148 | box.fY + (i % 2) * kBoxSize, |
| 149 | box.fColor |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 150 | }; |
| 151 | } |
| 152 | |
| 153 | paint.setARGB(255, rgb[0], rgb[1], rgb[2]); |
| 154 | goldCanvas.drawRect(SkRect::MakeXYWH(box.fX, box.fY, kBoxSize, kBoxSize), paint); |
| 155 | } |
| 156 | } |
| 157 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 158 | // ---- tests ---------- |
| 159 | |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 160 | #define VALIDATE(buff) \ |
| 161 | do { \ |
| 162 | if (!buff) { \ |
| 163 | ERRORF(reporter, #buff " is null."); \ |
| 164 | return; \ |
| 165 | } \ |
| 166 | } while (0) |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 167 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 168 | run_test(context, "draw", reporter, rtc, gold, |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 169 | [&](DrawMeshHelper* helper) { |
| 170 | SkTArray<Box> expandedVertexData; |
| 171 | for (int i = 0; i < kBoxCount; ++i) { |
| 172 | for (int j = 0; j < 6; ++j) { |
| 173 | expandedVertexData.push_back(vertexData[i][kIndexPattern[j]]); |
| 174 | } |
| 175 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 176 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 177 | // Draw boxes one line at a time to exercise base vertex. |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 178 | helper->fVertBuffer = helper->makeVertexBuffer(expandedVertexData); |
| 179 | VALIDATE(helper->fVertBuffer); |
| 180 | }, |
| 181 | [&](DrawMeshHelper* helper) { |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 182 | for (int y = 0; y < kBoxCountY; ++y) { |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 183 | auto pass = helper->bindPipeline(GrPrimitiveType::kTriangles, false, true); |
| 184 | pass->bindBuffers(nullptr, nullptr, helper->fVertBuffer.get()); |
| 185 | pass->draw(kBoxCountX * 6, y * kBoxCountX * 6); |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 186 | } |
| 187 | }); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 188 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 189 | run_test(context, "drawIndexed", reporter, rtc, gold, |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 190 | [&](DrawMeshHelper* helper) { |
| 191 | helper->fIndexBuffer = helper->getIndexBuffer(); |
| 192 | VALIDATE(helper->fIndexBuffer); |
| 193 | helper->fVertBuffer = helper->makeVertexBuffer(vertexData); |
| 194 | VALIDATE(helper->fVertBuffer); |
| 195 | }, |
| 196 | [&](DrawMeshHelper* helper) { |
| 197 | int baseRepetition = 0; |
| 198 | int i = 0; |
| 199 | // Start at various repetitions within the patterned index buffer to exercise base |
| 200 | // index. |
| 201 | while (i < kBoxCount) { |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 202 | static_assert(kIndexPatternRepeatCount >= 3); |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 203 | int repetitionCount = std::min(3 - baseRepetition, kBoxCount - i); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 204 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 205 | auto pass = helper->bindPipeline(GrPrimitiveType::kTriangles, false, true); |
| 206 | pass->bindBuffers(helper->fIndexBuffer.get(), nullptr, |
| 207 | helper->fVertBuffer.get()); |
| 208 | pass->drawIndexed(repetitionCount * 6, baseRepetition * 6, baseRepetition * 4, |
| 209 | (baseRepetition + repetitionCount) * 4 - 1, |
| 210 | (i - baseRepetition) * 4); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 211 | |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 212 | baseRepetition = (baseRepetition + 1) % 3; |
| 213 | i += repetitionCount; |
| 214 | } |
| 215 | }); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 216 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 217 | run_test(context, "drawIndexPattern", reporter, rtc, gold, |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 218 | [&](DrawMeshHelper* helper) { |
| 219 | helper->fIndexBuffer = helper->getIndexBuffer(); |
| 220 | VALIDATE(helper->fIndexBuffer); |
| 221 | helper->fVertBuffer = helper->makeVertexBuffer(vertexData); |
| 222 | VALIDATE(helper->fVertBuffer); |
| 223 | }, |
| 224 | [&](DrawMeshHelper* helper) { |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 225 | // Draw boxes one line at a time to exercise base vertex. drawIndexPattern does |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 226 | // not support a base index. |
| 227 | for (int y = 0; y < kBoxCountY; ++y) { |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 228 | auto pass = helper->bindPipeline(GrPrimitiveType::kTriangles, false, true); |
| 229 | pass->bindBuffers(helper->fIndexBuffer.get(), nullptr, |
| 230 | helper->fVertBuffer.get()); |
| 231 | pass->drawIndexPattern(6, kBoxCountX, kIndexPatternRepeatCount, 4, |
| 232 | y * kBoxCountX * 4); |
| 233 | |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 234 | } |
| 235 | }); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 236 | |
| 237 | for (bool indexed : {false, true}) { |
Chris Dalton | a77cdee | 2020-04-03 14:50:43 -0600 | [diff] [blame] | 238 | if (!context->priv().caps()->drawInstancedSupport()) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 239 | break; |
| 240 | } |
| 241 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 242 | run_test(context, indexed ? "drawIndexedInstanced" : "drawInstanced", |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 243 | reporter, rtc, gold, |
| 244 | [&](DrawMeshHelper* helper) { |
| 245 | helper->fIndexBuffer = indexed ? helper->getIndexBuffer() : nullptr; |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 246 | SkTArray<uint16_t> baseIndexData; |
| 247 | baseIndexData.push_back(kBoxCountX/2 * 6); // for testing base index. |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 248 | for (int i = 0; i < 6; ++i) { |
| 249 | baseIndexData.push_back(kIndexPattern[i]); |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 250 | } |
| 251 | helper->fIndexBuffer2 = helper->makeIndexBuffer(baseIndexData.begin(), |
| 252 | baseIndexData.count()); |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 253 | helper->fInstBuffer = helper->makeVertexBuffer(boxes); |
| 254 | VALIDATE(helper->fInstBuffer); |
| 255 | helper->fVertBuffer = |
| 256 | helper->makeVertexBuffer(std::vector<float>{0,0, 0,1, 1,0, 1,1}); |
| 257 | VALIDATE(helper->fVertBuffer); |
| 258 | helper->fVertBuffer2 = helper->makeVertexBuffer( // for testing base vertex. |
| 259 | std::vector<float>{-1,-1, -1,-1, 0,0, 0,1, 1,0, 1,1}); |
| 260 | VALIDATE(helper->fVertBuffer2); |
| 261 | }, |
| 262 | [&](DrawMeshHelper* helper) { |
| 263 | // Draw boxes one line at a time to exercise base instance, base vertex, and |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 264 | // null vertex buffer. |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 265 | for (int y = 0; y < kBoxCountY; ++y) { |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 266 | const GrBuffer* vertexBuffer = nullptr; |
| 267 | int baseVertex = 0; |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 268 | switch (y % 3) { |
| 269 | case 0: |
| 270 | if (context->priv().caps()->shaderCaps()->vertexIDSupport()) { |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 271 | break; |
| 272 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 273 | [[fallthrough]]; |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 274 | case 1: |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 275 | vertexBuffer = helper->fVertBuffer.get(); |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 276 | break; |
| 277 | case 2: |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 278 | vertexBuffer = helper->fVertBuffer2.get(); |
| 279 | baseVertex = 2; |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 280 | break; |
| 281 | } |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 282 | |
| 283 | GrPrimitiveType primitiveType = indexed ? GrPrimitiveType::kTriangles |
| 284 | : GrPrimitiveType::kTriangleStrip; |
| 285 | auto pass = helper->bindPipeline(primitiveType, true, |
| 286 | SkToBool(vertexBuffer)); |
| 287 | if (indexed) { |
| 288 | const GrBuffer* indexBuffer = (y % 2) ? |
| 289 | helper->fIndexBuffer2.get() : helper->fIndexBuffer.get(); |
| 290 | VALIDATE(indexBuffer); |
| 291 | int baseIndex = (y % 2); |
| 292 | pass->bindBuffers(indexBuffer, helper->fInstBuffer.get(), |
| 293 | vertexBuffer); |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 294 | pass->drawIndexedInstanced(6, baseIndex, kBoxCountX, y * kBoxCountX, |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 295 | baseVertex); |
| 296 | } else { |
| 297 | pass->bindBuffers(nullptr, helper->fInstBuffer.get(), vertexBuffer); |
| 298 | pass->drawInstanced(kBoxCountX, y * kBoxCountY, 4, baseVertex); |
| 299 | } |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 300 | } |
| 301 | }); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 302 | } |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 303 | |
| 304 | for (bool indexed : {false, true}) { |
| 305 | if (!context->priv().caps()->drawInstancedSupport()) { |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | run_test(context, (indexed) ? "drawIndexedIndirect" : "drawIndirect", |
| 310 | reporter, rtc, gold, |
| 311 | [&](DrawMeshHelper* helper) { |
| 312 | SkTArray<uint16_t> baseIndexData; |
| 313 | baseIndexData.push_back(kBoxCountX/2 * 6); // for testing base index. |
| 314 | for (int j = 0; j < kBoxCountY; ++j) { |
| 315 | for (int i = 0; i < 6; ++i) { |
| 316 | baseIndexData.push_back(kIndexPattern[i]); |
| 317 | } |
| 318 | } |
| 319 | helper->fIndexBuffer2 = helper->makeIndexBuffer(baseIndexData.begin(), |
| 320 | baseIndexData.count()); |
| 321 | VALIDATE(helper->fIndexBuffer2); |
| 322 | helper->fInstBuffer = helper->makeVertexBuffer(boxes); |
| 323 | VALIDATE(helper->fInstBuffer); |
| 324 | helper->fVertBuffer = helper->makeVertexBuffer(std::vector<float>{ |
| 325 | -1,-1, 0,0, 0,1, 1,0, 1,1, -1,-1, 0,0, 1,0, 0,1, 1,1}); |
| 326 | VALIDATE(helper->fVertBuffer); |
| 327 | |
| 328 | GrDrawIndirectCommand* drawIndirect = nullptr; |
| 329 | GrDrawIndexedIndirectCommand* drawIndexedIndirect = nullptr; |
| 330 | if (indexed) { |
| 331 | // Make helper->fDrawIndirectBufferOffset nonzero. |
| 332 | sk_sp<const GrBuffer> dummyBuff; |
| 333 | size_t dummyOffset; |
Chris Dalton | b8d7e00 | 2020-05-13 11:02:10 -0600 | [diff] [blame] | 334 | // Make a superfluous call to makeDrawIndirectSpace in order to test |
| 335 | // "offsetInBytes!=0" for the actual call to makeDrawIndexedIndirectSpace. |
| 336 | helper->target()->makeDrawIndirectSpace(29, &dummyBuff, &dummyOffset); |
| 337 | drawIndexedIndirect = helper->target()->makeDrawIndexedIndirectSpace( |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 338 | kBoxCountY, &helper->fDrawIndirectBuffer, |
| 339 | &helper->fDrawIndirectBufferOffset); |
| 340 | } else { |
| 341 | // Make helper->fDrawIndirectBufferOffset nonzero. |
| 342 | sk_sp<const GrBuffer> dummyBuff; |
| 343 | size_t dummyOffset; |
Chris Dalton | b8d7e00 | 2020-05-13 11:02:10 -0600 | [diff] [blame] | 344 | // Make a superfluous call to makeDrawIndexedIndirectSpace in order to test |
| 345 | // "offsetInBytes!=0" for the actual call to makeDrawIndirectSpace. |
| 346 | helper->target()->makeDrawIndexedIndirectSpace(7, &dummyBuff, &dummyOffset); |
| 347 | drawIndirect = helper->target()->makeDrawIndirectSpace( |
Chris Dalton | 03fdf6a | 2020-04-07 12:31:59 -0600 | [diff] [blame] | 348 | kBoxCountY, &helper->fDrawIndirectBuffer, |
| 349 | &helper->fDrawIndirectBufferOffset); |
| 350 | } |
| 351 | |
| 352 | // Draw boxes one line at a time to exercise multiple draws. |
| 353 | for (int y = 0; y < kBoxCountY; ++y) { |
| 354 | int baseVertex = (y % 2) ? 1 : 6; |
| 355 | if (indexed) { |
| 356 | int baseIndex = 1 + y * 6; |
| 357 | drawIndexedIndirect->fIndexCount = 6; |
| 358 | drawIndexedIndirect->fBaseIndex = baseIndex; |
| 359 | drawIndexedIndirect->fInstanceCount = kBoxCountX; |
| 360 | drawIndexedIndirect->fBaseInstance = y * kBoxCountX; |
| 361 | drawIndexedIndirect->fBaseVertex = baseVertex; |
| 362 | ++drawIndexedIndirect; |
| 363 | } else { |
| 364 | drawIndirect->fInstanceCount = kBoxCountX; |
| 365 | drawIndirect->fBaseInstance = y * kBoxCountX; |
| 366 | drawIndirect->fVertexCount = 4; |
| 367 | drawIndirect->fBaseVertex = baseVertex; |
| 368 | ++drawIndirect; |
| 369 | } |
| 370 | } |
| 371 | }, |
| 372 | [&](DrawMeshHelper* helper) { |
| 373 | GrOpsRenderPass* pass; |
| 374 | if (indexed) { |
| 375 | pass = helper->bindPipeline(GrPrimitiveType::kTriangles, true, true); |
| 376 | pass->bindBuffers(helper->fIndexBuffer2.get(), helper->fInstBuffer.get(), |
| 377 | helper->fVertBuffer.get()); |
| 378 | for (int i = 0; i < 3; ++i) { |
| 379 | int start = kBoxCountY * i / 3; |
| 380 | int end = kBoxCountY * (i + 1) / 3; |
| 381 | size_t offset = helper->fDrawIndirectBufferOffset + start * |
| 382 | sizeof(GrDrawIndexedIndirectCommand); |
| 383 | pass->drawIndexedIndirect(helper->fDrawIndirectBuffer.get(), offset, |
| 384 | end - start); |
| 385 | } |
| 386 | } else { |
| 387 | pass = helper->bindPipeline(GrPrimitiveType::kTriangleStrip, true, true); |
| 388 | pass->bindBuffers(nullptr, helper->fInstBuffer.get(), |
| 389 | helper->fVertBuffer.get()); |
| 390 | for (int i = 0; i < 2; ++i) { |
| 391 | int start = kBoxCountY * i / 2; |
| 392 | int end = kBoxCountY * (i + 1) / 2; |
| 393 | size_t offset = helper->fDrawIndirectBufferOffset + start * |
| 394 | sizeof(GrDrawIndirectCommand); |
| 395 | pass->drawIndirect(helper->fDrawIndirectBuffer.get(), offset, |
| 396 | end - start); |
| 397 | } |
| 398 | } |
| 399 | }); |
| 400 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 404 | |
| 405 | class GrMeshTestOp : public GrDrawOp { |
| 406 | public: |
| 407 | DEFINE_OP_CLASS_ID |
| 408 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 409 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 410 | std::function<void(DrawMeshHelper*)> prepareFn, |
| 411 | std::function<void(DrawMeshHelper*)> executeFn) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 412 | GrOpMemoryPool* pool = context->priv().opMemoryPool(); |
Robert Phillips | c994a93 | 2018-06-19 13:09:54 -0400 | [diff] [blame] | 413 | |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 414 | return pool->allocate<GrMeshTestOp>(prepareFn, executeFn); |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | private: |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 418 | friend class GrOpMemoryPool; // for ctor |
| 419 | |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 420 | GrMeshTestOp(std::function<void(DrawMeshHelper*)> prepareFn, |
| 421 | std::function<void(DrawMeshHelper*)> executeFn) |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 422 | : INHERITED(ClassID()) |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 423 | , fPrepareFn(prepareFn) |
| 424 | , fExecuteFn(executeFn){ |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 425 | this->setBounds(SkRect::MakeIWH(kImageWidth, kImageHeight), |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 426 | HasAABloat::kNo, IsHairline::kNo); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 427 | } |
| 428 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 429 | const char* name() const override { return "GrMeshTestOp"; } |
| 430 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 431 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, |
| 432 | bool hasMixedSampledCoverage, GrClampType) override { |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 433 | return GrProcessorSet::EmptySetAnalysis(); |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 434 | } |
Robert Phillips | c655c3a | 2020-03-18 13:23:45 -0400 | [diff] [blame] | 435 | |
| 436 | void onPrePrepare(GrRecordingContext*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 437 | const GrSurfaceProxyView* writeView, |
Robert Phillips | c655c3a | 2020-03-18 13:23:45 -0400 | [diff] [blame] | 438 | GrAppliedClip*, |
| 439 | const GrXferProcessor::DstProxyView&) override {} |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 440 | void onPrepare(GrOpFlushState* state) override { |
| 441 | fHelper.reset(new DrawMeshHelper(state)); |
| 442 | fPrepareFn(fHelper.get()); |
| 443 | } |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 444 | void onExecute(GrOpFlushState* state, const SkRect& chainBounds) override { |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 445 | fExecuteFn(fHelper.get()); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 446 | } |
| 447 | |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 448 | std::unique_ptr<DrawMeshHelper> fHelper; |
| 449 | std::function<void(DrawMeshHelper*)> fPrepareFn; |
| 450 | std::function<void(DrawMeshHelper*)> fExecuteFn; |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 451 | |
| 452 | typedef GrDrawOp INHERITED; |
| 453 | }; |
| 454 | |
| 455 | class GrMeshTestProcessor : public GrGeometryProcessor { |
| 456 | public: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 457 | static GrGeometryProcessor* Make(SkArenaAlloc* arena, bool instanced, bool hasVertexBuffer) { |
| 458 | return arena->make<GrMeshTestProcessor>(instanced, hasVertexBuffer); |
| 459 | } |
| 460 | |
| 461 | const char* name() const override { return "GrMeshTestProcessor"; } |
| 462 | |
| 463 | const Attribute& inColor() const { |
| 464 | return fVertexColor.isInitialized() ? fVertexColor : fInstanceColor; |
| 465 | } |
| 466 | |
| 467 | void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const final { |
| 468 | b->add32(fInstanceLocation.isInitialized()); |
| 469 | b->add32(fVertexPosition.isInitialized()); |
| 470 | } |
| 471 | |
| 472 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
| 473 | |
| 474 | private: |
| 475 | friend class GLSLMeshTestProcessor; |
| 476 | friend class ::SkArenaAlloc; // for access to ctor |
| 477 | |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 478 | GrMeshTestProcessor(bool instanced, bool hasVertexBuffer) |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 479 | : INHERITED(kGrMeshTestProcessor_ClassID) { |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 480 | if (instanced) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 481 | fInstanceLocation = {"location", kFloat2_GrVertexAttribType, kHalf2_GrSLType}; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 482 | fInstanceColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType}; |
| 483 | this->setInstanceAttributes(&fInstanceLocation, 2); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 484 | if (hasVertexBuffer) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 485 | fVertexPosition = {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType}; |
| 486 | this->setVertexAttributes(&fVertexPosition, 1); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 487 | } |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 488 | } else { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 489 | fVertexPosition = {"vertex", kFloat2_GrVertexAttribType, kHalf2_GrSLType}; |
| 490 | fVertexColor = {"color", kUByte4_norm_GrVertexAttribType, kHalf4_GrSLType}; |
| 491 | this->setVertexAttributes(&fVertexPosition, 2); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 492 | } |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 493 | } |
| 494 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 495 | Attribute fVertexPosition; |
| 496 | Attribute fVertexColor; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 497 | |
| 498 | Attribute fInstanceLocation; |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 499 | Attribute fInstanceColor; |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 500 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 501 | typedef GrGeometryProcessor INHERITED; |
| 502 | }; |
| 503 | |
| 504 | class GLSLMeshTestProcessor : public GrGLSLGeometryProcessor { |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 505 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor&) final {} |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 506 | |
| 507 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) final { |
| 508 | const GrMeshTestProcessor& mp = args.fGP.cast<GrMeshTestProcessor>(); |
| 509 | |
| 510 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
| 511 | varyingHandler->emitAttributes(mp); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 512 | varyingHandler->addPassThroughAttribute(mp.inColor(), args.fOutputColor); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 513 | |
| 514 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 515 | if (!mp.fInstanceLocation.isInitialized()) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 516 | v->codeAppendf("float2 vertex = %s;", mp.fVertexPosition.name()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 517 | } else { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 518 | if (mp.fVertexPosition.isInitialized()) { |
| 519 | v->codeAppendf("float2 offset = %s;", mp.fVertexPosition.name()); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 520 | } else { |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 521 | v->codeAppend ("float2 offset = float2(sk_VertexID / 2, sk_VertexID % 2);"); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 522 | } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 523 | v->codeAppendf("float2 vertex = %s + offset * %i;", mp.fInstanceLocation.name(), |
Brian Salomon | 70132d0 | 2018-05-29 15:33:06 -0400 | [diff] [blame] | 524 | kBoxSize); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 525 | } |
Ethan Nicholas | 8aa4569 | 2017-09-20 11:24:15 -0400 | [diff] [blame] | 526 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertex"); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 527 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 528 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 529 | f->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 530 | } |
| 531 | }; |
| 532 | |
| 533 | GrGLSLPrimitiveProcessor* GrMeshTestProcessor::createGLSLInstance(const GrShaderCaps&) const { |
| 534 | return new GLSLMeshTestProcessor; |
| 535 | } |
| 536 | |
| 537 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 538 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 539 | sk_sp<const GrBuffer> DrawMeshHelper::makeIndexBuffer(const uint16_t indices[], int count) { |
| 540 | return sk_sp<const GrBuffer>(fState->resourceProvider()->createBuffer( |
| 541 | count * sizeof(uint16_t), GrGpuBufferType::kIndex, kDynamic_GrAccessPattern, indices)); |
| 542 | } |
| 543 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 544 | template<typename T> |
| 545 | sk_sp<const GrBuffer> DrawMeshHelper::makeVertexBuffer(const T* data, int count) { |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 546 | return sk_sp<const GrBuffer>(fState->resourceProvider()->createBuffer( |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 547 | count * sizeof(T), GrGpuBufferType::kVertex, kDynamic_GrAccessPattern, data)); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | sk_sp<const GrBuffer> DrawMeshHelper::getIndexBuffer() { |
| 551 | GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 552 | return fState->resourceProvider()->findOrCreatePatternedIndexBuffer( |
| 553 | kIndexPattern, 6, kIndexPatternRepeatCount, 4, gIndexBufferKey); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 554 | } |
| 555 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 556 | GrOpsRenderPass* DrawMeshHelper::bindPipeline(GrPrimitiveType primitiveType, bool isInstanced, |
| 557 | bool hasVertexBuffer) { |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 558 | GrProcessorSet processorSet(SkBlendMode::kSrc); |
| 559 | |
| 560 | // TODO: add a GrProcessorSet testing helper to make this easier |
| 561 | SkPMColor4f overrideColor; |
| 562 | processorSet.finalize(GrProcessorAnalysisColor(), |
| 563 | GrProcessorAnalysisCoverage::kNone, |
| 564 | fState->appliedClip(), |
| 565 | nullptr, |
| 566 | false, |
| 567 | fState->caps(), |
| 568 | GrClampType::kAuto, |
| 569 | &overrideColor); |
| 570 | |
| 571 | auto pipeline = GrSimpleMeshDrawOpHelper::CreatePipeline(fState, |
| 572 | std::move(processorSet), |
| 573 | GrPipeline::InputFlags::kNone); |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 574 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 575 | GrGeometryProcessor* mtp = GrMeshTestProcessor::Make(fState->allocator(), isInstanced, |
| 576 | hasVertexBuffer); |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 577 | |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 578 | GrProgramInfo programInfo(fState->proxy()->numSamples(), fState->proxy()->numStencilSamples(), |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 579 | fState->proxy()->backendFormat(), fState->writeView()->origin(), |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 580 | pipeline, mtp, primitiveType); |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 581 | |
Chris Dalton | 4386ad1 | 2020-02-19 16:42:06 -0700 | [diff] [blame] | 582 | fState->opsRenderPass()->bindPipeline(programInfo, SkRect::MakeIWH(kImageWidth, kImageHeight)); |
Chris Dalton | bb76842 | 2020-03-12 12:13:29 -0600 | [diff] [blame] | 583 | return fState->opsRenderPass(); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 584 | } |
| 585 | |
Robert Phillips | 88a32ef | 2018-06-07 11:05:56 -0400 | [diff] [blame] | 586 | static void run_test(GrContext* context, const char* testName, skiatest::Reporter* reporter, |
Brian Salomon | bf6b979 | 2019-08-21 09:38:10 -0400 | [diff] [blame] | 587 | const std::unique_ptr<GrRenderTargetContext>& rtc, const SkBitmap& gold, |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 588 | std::function<void(DrawMeshHelper*)> prepareFn, |
| 589 | std::function<void(DrawMeshHelper*)> executeFn) { |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 590 | const int w = gold.width(), h = gold.height(), rowBytes = gold.rowBytes(); |
| 591 | const uint32_t* goldPx = reinterpret_cast<const uint32_t*>(gold.getPixels()); |
| 592 | if (h != rtc->height() || w != rtc->width()) { |
| 593 | ERRORF(reporter, "[%s] expectation and rtc not compatible (?).", testName); |
| 594 | return; |
| 595 | } |
| 596 | if (sizeof(uint32_t) * kImageWidth != gold.rowBytes()) { |
Adlai Holler | 684838f | 2020-05-12 10:41:04 -0400 | [diff] [blame] | 597 | ERRORF(reporter, "[%s] unexpected row bytes in gold image", testName); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 598 | return; |
| 599 | } |
| 600 | |
| 601 | SkAutoSTMalloc<kImageHeight * kImageWidth, uint32_t> resultPx(h * rowBytes); |
Michael Ludwig | 81d4172 | 2020-05-26 16:57:38 -0400 | [diff] [blame] | 602 | rtc->clear(SkPMColor4f::FromBytes_RGBA(0xbaaaaaad)); |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 603 | rtc->priv().testingOnly_addDrawOp(GrMeshTestOp::Make(context, prepareFn, executeFn)); |
Chris Dalton | d4dec97 | 2020-04-03 11:01:30 -0600 | [diff] [blame] | 604 | |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 605 | rtc->readPixels(gold.info(), resultPx, rowBytes, {0, 0}); |
Chris Dalton | d4dec97 | 2020-04-03 11:01:30 -0600 | [diff] [blame] | 606 | |
| 607 | #ifdef WRITE_PNG_CONTEXT_TYPE |
| 608 | #define STRINGIFY(X) #X |
| 609 | #define TOSTRING(X) STRINGIFY(X) |
| 610 | SkString filename; |
| 611 | filename.printf("GrMeshTest_%s_%s.png", TOSTRING(WRITE_PNG_CONTEXT_TYPE), testName); |
| 612 | SkDebugf("writing %s...\n", filename.c_str()); |
| 613 | ToolUtils::EncodeImageToFile(filename.c_str(), SkPixmap(gold.info(), resultPx, rowBytes), |
| 614 | SkEncodedImageFormat::kPNG, 100); |
| 615 | #endif |
| 616 | |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 617 | for (int y = 0; y < h; ++y) { |
| 618 | for (int x = 0; x < w; ++x) { |
| 619 | uint32_t expected = goldPx[y * kImageWidth + x]; |
| 620 | uint32_t actual = resultPx[y * kImageWidth + x]; |
| 621 | if (expected != actual) { |
| 622 | ERRORF(reporter, "[%s] pixel (%i,%i): got 0x%x expected 0x%x", |
| 623 | testName, x, y, actual, expected); |
| 624 | return; |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | } |