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