Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Michael Ludwig | fd4f4df | 2019-05-29 09:51:09 -0400 | [diff] [blame] | 8 | #include "src/gpu/ops/GrQuadPerEdgeAA.h" |
| 9 | |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 10 | #include "include/private/SkVx.h" |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 11 | #include "src/gpu/GrMeshDrawTarget.h" |
Robert Phillips | 1a82a4e | 2021-07-01 10:27:44 -0400 | [diff] [blame] | 12 | #include "src/gpu/GrResourceProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/SkGr.h" |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 14 | #include "src/gpu/geometry/GrQuadUtils.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h" |
| 16 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 18 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 19 | |
Brian Osman | 01e6d17 | 2020-03-30 15:57:14 -0400 | [diff] [blame] | 20 | static_assert((int)GrQuadAAFlags::kLeft == SkCanvas::kLeft_QuadAAFlag); |
| 21 | static_assert((int)GrQuadAAFlags::kTop == SkCanvas::kTop_QuadAAFlag); |
| 22 | static_assert((int)GrQuadAAFlags::kRight == SkCanvas::kRight_QuadAAFlag); |
| 23 | static_assert((int)GrQuadAAFlags::kBottom == SkCanvas::kBottom_QuadAAFlag); |
| 24 | static_assert((int)GrQuadAAFlags::kNone == SkCanvas::kNone_QuadAAFlags); |
| 25 | static_assert((int)GrQuadAAFlags::kAll == SkCanvas::kAll_QuadAAFlags); |
Michael Ludwig | f995c05 | 2018-11-26 15:24:29 -0500 | [diff] [blame] | 26 | |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 27 | namespace { |
| 28 | |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 29 | // Generic WriteQuadProc that can handle any VertexSpec. It writes the 4 vertices in triangle strip |
| 30 | // order, although the data per-vertex is dependent on the VertexSpec. |
| 31 | static void write_quad_generic(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec, |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 32 | const GrQuad* deviceQuad, const GrQuad* localQuad, |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 33 | const float coverage[4], const SkPMColor4f& color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 34 | const SkRect& geomSubset, const SkRect& texSubset) { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 35 | static constexpr auto If = GrVertexWriter::If<float>; |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 36 | |
| 37 | SkASSERT(!spec.hasLocalCoords() || localQuad); |
| 38 | |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 39 | GrQuadPerEdgeAA::CoverageMode mode = spec.coverageMode(); |
Michael Ludwig | 553e9a9 | 2018-11-29 12:38:35 -0500 | [diff] [blame] | 40 | for (int i = 0; i < 4; ++i) { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 41 | // save position, this is a float2 or float3 or float4 depending on the combination of |
| 42 | // perspective and coverage mode. |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 43 | vb->write(deviceQuad->x(i), deviceQuad->y(i), |
| 44 | If(spec.deviceQuadType() == GrQuad::Type::kPerspective, deviceQuad->w(i)), |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 45 | If(mode == GrQuadPerEdgeAA::CoverageMode::kWithPosition, coverage[i])); |
Michael Ludwig | 4921dc3 | 2018-12-03 14:57:29 +0000 | [diff] [blame] | 46 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 47 | // save color |
| 48 | if (spec.hasVertexColors()) { |
Brian Osman | 2715bf5 | 2019-12-06 14:38:47 -0500 | [diff] [blame] | 49 | bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat; |
Michael Ludwig | e6266a2 | 2019-03-07 11:24:32 -0500 | [diff] [blame] | 50 | vb->write(GrVertexColor( |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 51 | color * (mode == GrQuadPerEdgeAA::CoverageMode::kWithColor ? coverage[i] : 1.f), |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 52 | wide)); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | // save local position |
| 56 | if (spec.hasLocalCoords()) { |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 57 | vb->write(localQuad->x(i), localQuad->y(i), |
| 58 | If(spec.localQuadType() == GrQuad::Type::kPerspective, localQuad->w(i))); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 61 | // save the geometry subset |
| 62 | if (spec.requiresGeometrySubset()) { |
| 63 | vb->write(geomSubset); |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 66 | // save the texture subset |
| 67 | if (spec.hasSubset()) { |
| 68 | vb->write(texSubset); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 73 | // Specialized WriteQuadProcs for particular VertexSpecs that show up frequently (determined |
| 74 | // experimentally through recorded GMs, SKPs, and SVGs, as well as SkiaRenderer's usage patterns): |
| 75 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 76 | // 2D (XY), no explicit coverage, vertex color, no locals, no geometry subset, no texture subsetn |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 77 | // This represents simple, solid color or shader, non-AA (or AA with cov. as alpha) rects. |
| 78 | static void write_2d_color(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec, |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 79 | const GrQuad* deviceQuad, const GrQuad* localQuad, |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 80 | const float coverage[4], const SkPMColor4f& color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 81 | const SkRect& geomSubset, const SkRect& texSubset) { |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 82 | // Assert assumptions about VertexSpec |
| 83 | SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective); |
| 84 | SkASSERT(!spec.hasLocalCoords()); |
| 85 | SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone || |
| 86 | spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor); |
| 87 | SkASSERT(spec.hasVertexColors()); |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 88 | SkASSERT(!spec.requiresGeometrySubset()); |
| 89 | SkASSERT(!spec.hasSubset()); |
Robert Phillips | fbf0214 | 2021-09-01 16:31:34 -0400 | [diff] [blame^] | 90 | // We don't assert that localQuad == nullptr, since it is possible for FillRectOp to |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 91 | // accumulate local coords conservatively (paint not trivial), and then after analysis realize |
| 92 | // the processors don't need local coordinates. |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 93 | |
Brian Osman | 2715bf5 | 2019-12-06 14:38:47 -0500 | [diff] [blame] | 94 | bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat; |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 95 | for (int i = 0; i < 4; ++i) { |
| 96 | // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything |
| 97 | SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor || |
| 98 | coverage[i] == 1.f); |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 99 | vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide)); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 103 | // 2D (XY), no explicit coverage, UV locals, no color, no geometry subset, no texture subset |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 104 | // This represents opaque, non AA, textured rects |
| 105 | static void write_2d_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec, |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 106 | const GrQuad* deviceQuad, const GrQuad* localQuad, |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 107 | const float coverage[4], const SkPMColor4f& color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 108 | const SkRect& geomSubset, const SkRect& texSubset) { |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 109 | // Assert assumptions about VertexSpec |
| 110 | SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective); |
| 111 | SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective); |
| 112 | SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone); |
| 113 | SkASSERT(!spec.hasVertexColors()); |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 114 | SkASSERT(!spec.requiresGeometrySubset()); |
| 115 | SkASSERT(!spec.hasSubset()); |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 116 | SkASSERT(localQuad); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 117 | |
| 118 | for (int i = 0; i < 4; ++i) { |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 119 | vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i)); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 123 | // 2D (XY), no explicit coverage, UV locals, vertex color, no geometry or texture subsets |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 124 | // This represents transparent, non AA (or AA with cov. as alpha), textured rects |
| 125 | static void write_2d_color_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec, |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 126 | const GrQuad* deviceQuad, const GrQuad* localQuad, |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 127 | const float coverage[4], const SkPMColor4f& color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 128 | const SkRect& geomSubset, const SkRect& texSubset) { |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 129 | // Assert assumptions about VertexSpec |
| 130 | SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective); |
| 131 | SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective); |
| 132 | SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone || |
| 133 | spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor); |
| 134 | SkASSERT(spec.hasVertexColors()); |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 135 | SkASSERT(!spec.requiresGeometrySubset()); |
| 136 | SkASSERT(!spec.hasSubset()); |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 137 | SkASSERT(localQuad); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 138 | |
Brian Osman | 2715bf5 | 2019-12-06 14:38:47 -0500 | [diff] [blame] | 139 | bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat; |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 140 | for (int i = 0; i < 4; ++i) { |
| 141 | // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything |
| 142 | SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor || |
| 143 | coverage[i] == 1.f); |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 144 | vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide), |
| 145 | localQuad->x(i), localQuad->y(i)); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 149 | // 2D (XY), explicit coverage, UV locals, no color, no geometry subset, no texture subset |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 150 | // This represents opaque, AA, textured rects |
| 151 | static void write_2d_cov_uv(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec, |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 152 | const GrQuad* deviceQuad, const GrQuad* localQuad, |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 153 | const float coverage[4], const SkPMColor4f& color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 154 | const SkRect& geomSubset, const SkRect& texSubset) { |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 155 | // Assert assumptions about VertexSpec |
| 156 | SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective); |
| 157 | SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective); |
| 158 | SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition); |
| 159 | SkASSERT(!spec.hasVertexColors()); |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 160 | SkASSERT(!spec.requiresGeometrySubset()); |
| 161 | SkASSERT(!spec.hasSubset()); |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 162 | SkASSERT(localQuad); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 163 | |
| 164 | for (int i = 0; i < 4; ++i) { |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 165 | vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i], |
| 166 | localQuad->x(i), localQuad->y(i)); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | // NOTE: The three _strict specializations below match the non-strict uv functions above, except |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 171 | // that they also write the UV subset. These are included to benefit SkiaRenderer, which must make |
| 172 | // use of both fast and strict constrained subsets. When testing _strict was not that common across |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 173 | // GMS, SKPs, and SVGs but we have little visibility into actual SkiaRenderer statistics. If |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 174 | // SkiaRenderer can avoid subsets more, these 3 functions should probably be removed for simplicity. |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 175 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 176 | // 2D (XY), no explicit coverage, UV locals, no color, tex subset but no geometry subset |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 177 | // This represents opaque, non AA, textured rects with strict uv sampling |
| 178 | static void write_2d_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec, |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 179 | const GrQuad* deviceQuad, const GrQuad* localQuad, |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 180 | const float coverage[4], const SkPMColor4f& color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 181 | const SkRect& geomSubset, const SkRect& texSubset) { |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 182 | // Assert assumptions about VertexSpec |
| 183 | SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective); |
| 184 | SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective); |
| 185 | SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone); |
| 186 | SkASSERT(!spec.hasVertexColors()); |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 187 | SkASSERT(!spec.requiresGeometrySubset()); |
| 188 | SkASSERT(spec.hasSubset()); |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 189 | SkASSERT(localQuad); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 190 | |
| 191 | for (int i = 0; i < 4; ++i) { |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 192 | vb->write(deviceQuad->x(i), deviceQuad->y(i), localQuad->x(i), localQuad->y(i), texSubset); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 196 | // 2D (XY), no explicit coverage, UV locals, vertex color, tex subset but no geometry subset |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 197 | // This represents transparent, non AA (or AA with cov. as alpha), textured rects with strict sample |
| 198 | static void write_2d_color_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec, |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 199 | const GrQuad* deviceQuad, const GrQuad* localQuad, |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 200 | const float coverage[4], const SkPMColor4f& color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 201 | const SkRect& geomSubset, const SkRect& texSubset) { |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 202 | // Assert assumptions about VertexSpec |
| 203 | SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective); |
| 204 | SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective); |
| 205 | SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kNone || |
| 206 | spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor); |
| 207 | SkASSERT(spec.hasVertexColors()); |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 208 | SkASSERT(!spec.requiresGeometrySubset()); |
| 209 | SkASSERT(spec.hasSubset()); |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 210 | SkASSERT(localQuad); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 211 | |
Brian Osman | 2715bf5 | 2019-12-06 14:38:47 -0500 | [diff] [blame] | 212 | bool wide = spec.colorType() == GrQuadPerEdgeAA::ColorType::kFloat; |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 213 | for (int i = 0; i < 4; ++i) { |
| 214 | // If this is not coverage-with-alpha, make sure coverage == 1 so it doesn't do anything |
| 215 | SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithColor || |
| 216 | coverage[i] == 1.f); |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 217 | vb->write(deviceQuad->x(i), deviceQuad->y(i), GrVertexColor(color * coverage[i], wide), |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 218 | localQuad->x(i), localQuad->y(i), texSubset); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 222 | // 2D (XY), explicit coverage, UV locals, no color, tex subset but no geometry subset |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 223 | // This represents opaque, AA, textured rects with strict uv sampling |
| 224 | static void write_2d_cov_uv_strict(GrVertexWriter* vb, const GrQuadPerEdgeAA::VertexSpec& spec, |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 225 | const GrQuad* deviceQuad, const GrQuad* localQuad, |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 226 | const float coverage[4], const SkPMColor4f& color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 227 | const SkRect& geomSubset, const SkRect& texSubset) { |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 228 | // Assert assumptions about VertexSpec |
| 229 | SkASSERT(spec.deviceQuadType() != GrQuad::Type::kPerspective); |
| 230 | SkASSERT(spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective); |
| 231 | SkASSERT(spec.coverageMode() == GrQuadPerEdgeAA::CoverageMode::kWithPosition); |
| 232 | SkASSERT(!spec.hasVertexColors()); |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 233 | SkASSERT(!spec.requiresGeometrySubset()); |
| 234 | SkASSERT(spec.hasSubset()); |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 235 | SkASSERT(localQuad); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 236 | |
| 237 | for (int i = 0; i < 4; ++i) { |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 238 | vb->write(deviceQuad->x(i), deviceQuad->y(i), coverage[i], |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 239 | localQuad->x(i), localQuad->y(i), texSubset); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 243 | } // anonymous namespace |
| 244 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 245 | namespace GrQuadPerEdgeAA { |
| 246 | |
Brian Salomon | b8c4add | 2021-06-28 09:20:44 -0400 | [diff] [blame] | 247 | IndexBufferOption CalcIndexBufferOption(GrAAType aa, int numQuads) { |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 248 | if (aa == GrAAType::kCoverage) { |
| 249 | return IndexBufferOption::kPictureFramed; |
Brian Salomon | b8c4add | 2021-06-28 09:20:44 -0400 | [diff] [blame] | 250 | } else if (numQuads > 1) { |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 251 | return IndexBufferOption::kIndexedRects; |
| 252 | } else { |
| 253 | return IndexBufferOption::kTriStrips; |
| 254 | } |
| 255 | } |
| 256 | |
Brian Osman | 2715bf5 | 2019-12-06 14:38:47 -0500 | [diff] [blame] | 257 | // This is a more elaborate version of fitsInBytes() that allows "no color" for white |
| 258 | ColorType MinColorType(SkPMColor4f color) { |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 259 | if (color == SK_PMColor4fWHITE) { |
| 260 | return ColorType::kNone; |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 261 | } else { |
Brian Osman | 2715bf5 | 2019-12-06 14:38:47 -0500 | [diff] [blame] | 262 | return color.fitsInBytes() ? ColorType::kByte : ColorType::kFloat; |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 266 | ////////////////// Tessellator Implementation |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 267 | |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 268 | Tessellator::WriteQuadProc Tessellator::GetWriteQuadProc(const VertexSpec& spec) { |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 269 | // All specialized writing functions requires 2D geometry and no geometry subset. This is not |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 270 | // the same as just checking device type vs. kRectilinear since non-AA general 2D quads do not |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 271 | // require a geometry subset and could then go through a fast path. |
| 272 | if (spec.deviceQuadType() != GrQuad::Type::kPerspective && !spec.requiresGeometrySubset()) { |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 273 | CoverageMode mode = spec.coverageMode(); |
| 274 | if (spec.hasVertexColors()) { |
| 275 | if (mode != CoverageMode::kWithPosition) { |
| 276 | // Vertex colors, but no explicit coverage |
| 277 | if (!spec.hasLocalCoords()) { |
| 278 | // Non-UV with vertex colors (possibly with coverage folded into alpha) |
| 279 | return write_2d_color; |
| 280 | } else if (spec.localQuadType() != GrQuad::Type::kPerspective) { |
| 281 | // UV locals with vertex colors (possibly with coverage-as-alpha) |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 282 | return spec.hasSubset() ? write_2d_color_uv_strict : write_2d_color_uv; |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | // Else fall through; this is a spec that requires vertex colors and explicit coverage, |
| 286 | // which means it's anti-aliased and the FPs don't support coverage as alpha, or |
| 287 | // it uses 3D local coordinates. |
| 288 | } else if (spec.hasLocalCoords() && spec.localQuadType() != GrQuad::Type::kPerspective) { |
| 289 | if (mode == CoverageMode::kWithPosition) { |
| 290 | // UV locals with explicit coverage |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 291 | return spec.hasSubset() ? write_2d_cov_uv_strict : write_2d_cov_uv; |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 292 | } else { |
| 293 | SkASSERT(mode == CoverageMode::kNone); |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 294 | return spec.hasSubset() ? write_2d_uv_strict : write_2d_uv; |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | // Else fall through to generic vertex function; this is a spec that has no vertex colors |
| 298 | // and [no|uvr] local coords, which doesn't happen often enough to warrant specialization. |
| 299 | } |
Michael Ludwig | 41f395d | 2019-05-23 13:59:45 -0400 | [diff] [blame] | 300 | |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 301 | // Arbitrary spec hits the slow path |
| 302 | return write_quad_generic; |
| 303 | } |
| 304 | |
Michael Ludwig | 189c980 | 2019-11-21 11:21:12 -0500 | [diff] [blame] | 305 | Tessellator::Tessellator(const VertexSpec& spec, char* vertices) |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 306 | : fVertexSpec(spec) |
Michael Ludwig | 189c980 | 2019-11-21 11:21:12 -0500 | [diff] [blame] | 307 | , fVertexWriter{vertices} |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 308 | , fWriteProc(Tessellator::GetWriteQuadProc(spec)) {} |
| 309 | |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 310 | void Tessellator::append(GrQuad* deviceQuad, GrQuad* localQuad, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 311 | const SkPMColor4f& color, const SkRect& uvSubset, GrQuadAAFlags aaFlags) { |
Michael Ludwig | 189c980 | 2019-11-21 11:21:12 -0500 | [diff] [blame] | 312 | // We allow Tessellator to be created with a null vertices pointer for convenience, but it is |
| 313 | // assumed it will never actually be used in those cases. |
| 314 | SkASSERT(fVertexWriter.fPtr); |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 315 | SkASSERT(deviceQuad->quadType() <= fVertexSpec.deviceQuadType()); |
| 316 | SkASSERT(localQuad || !fVertexSpec.hasLocalCoords()); |
| 317 | SkASSERT(!fVertexSpec.hasLocalCoords() || localQuad->quadType() <= fVertexSpec.localQuadType()); |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 318 | |
| 319 | static const float kFullCoverage[4] = {1.f, 1.f, 1.f, 1.f}; |
| 320 | static const float kZeroCoverage[4] = {0.f, 0.f, 0.f, 0.f}; |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 321 | static const SkRect kIgnoredSubset = SkRect::MakeEmpty(); |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 322 | |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 323 | if (fVertexSpec.usesCoverageAA()) { |
| 324 | SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kWithColor || |
| 325 | fVertexSpec.coverageMode() == CoverageMode::kWithPosition); |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 326 | // Must calculate inner and outer quadrilaterals for the vertex coverage ramps, and possibly |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 327 | // a geometry subset if corners are not right angles |
| 328 | SkRect geomSubset; |
| 329 | if (fVertexSpec.requiresGeometrySubset()) { |
Brian Salomon | 659e71f | 2021-02-24 10:09:02 -0500 | [diff] [blame] | 330 | #ifdef SK_USE_LEGACY_AA_QUAD_SUBSET |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 331 | geomSubset = deviceQuad->bounds(); |
| 332 | geomSubset.outset(0.5f, 0.5f); // account for AA expansion |
Brian Salomon | 659e71f | 2021-02-24 10:09:02 -0500 | [diff] [blame] | 333 | #else |
| 334 | // Our GP code expects a 0.5 outset rect (coverage is computed as 0 at the values of |
| 335 | // the uniform). However, if we have quad edges that aren't supposed to be antialiased |
| 336 | // they may lie close to the bounds. So in that case we outset by an additional 0.5. |
| 337 | // This is a sort of backup clipping mechanism for cases where quad outsetting of nearly |
| 338 | // parallel edges produces long thin extrusions from the original geometry. |
| 339 | float outset = aaFlags == GrQuadAAFlags::kAll ? 0.5f : 1.f; |
| 340 | geomSubset = deviceQuad->bounds().makeOutset(outset, outset); |
| 341 | #endif |
Michael Ludwig | e6266a2 | 2019-03-07 11:24:32 -0500 | [diff] [blame] | 342 | } |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 343 | |
Michael Ludwig | d84dd4b | 2019-11-05 12:03:12 -0500 | [diff] [blame] | 344 | if (aaFlags == GrQuadAAFlags::kNone) { |
| 345 | // Have to write the coverage AA vertex structure, but there's no math to be done for a |
| 346 | // non-aa quad batched into a coverage AA op. |
Michael Ludwig | 189c980 | 2019-11-21 11:21:12 -0500 | [diff] [blame] | 347 | fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 348 | geomSubset, uvSubset); |
Michael Ludwig | d84dd4b | 2019-11-05 12:03:12 -0500 | [diff] [blame] | 349 | // Since we pass the same corners in, the outer vertex structure will have 0 area and |
| 350 | // the coverage interpolation from 1 to 0 will not be visible. |
Michael Ludwig | 189c980 | 2019-11-21 11:21:12 -0500 | [diff] [blame] | 351 | fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 352 | geomSubset, uvSubset); |
Michael Ludwig | d84dd4b | 2019-11-05 12:03:12 -0500 | [diff] [blame] | 353 | } else { |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 354 | // Reset the tessellation helper to match the current geometry |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 355 | fAAHelper.reset(*deviceQuad, localQuad); |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 356 | |
Michael Ludwig | d84dd4b | 2019-11-05 12:03:12 -0500 | [diff] [blame] | 357 | // Edge inset/outset distance ordered LBTR, set to 0.5 for a half pixel if the AA flag |
| 358 | // is turned on, or 0.0 if the edge is not anti-aliased. |
| 359 | skvx::Vec<4, float> edgeDistances; |
| 360 | if (aaFlags == GrQuadAAFlags::kAll) { |
| 361 | edgeDistances = 0.5f; |
| 362 | } else { |
| 363 | edgeDistances = { (aaFlags & GrQuadAAFlags::kLeft) ? 0.5f : 0.f, |
| 364 | (aaFlags & GrQuadAAFlags::kBottom) ? 0.5f : 0.f, |
| 365 | (aaFlags & GrQuadAAFlags::kTop) ? 0.5f : 0.f, |
| 366 | (aaFlags & GrQuadAAFlags::kRight) ? 0.5f : 0.f }; |
| 367 | } |
Michael Ludwig | fb7ba52 | 2019-10-29 15:33:34 -0400 | [diff] [blame] | 368 | |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 369 | // Write inner vertices first |
| 370 | float coverage[4]; |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 371 | fAAHelper.inset(edgeDistances, deviceQuad, localQuad).store(coverage); |
| 372 | fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, coverage, color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 373 | geomSubset, uvSubset); |
Michael Ludwig | d84dd4b | 2019-11-05 12:03:12 -0500 | [diff] [blame] | 374 | |
Michael Ludwig | 290d6df | 2020-10-27 09:28:59 -0400 | [diff] [blame] | 375 | // Then outer vertices, which use 0.f for their coverage. If the inset was degenerate |
| 376 | // to a line (had all coverages < 1), tweak the outset distance so the outer frame's |
| 377 | // narrow axis reaches out to 2px, which gives better animation under translation. |
Michael Ludwig | 575c921 | 2021-07-13 11:09:52 -0400 | [diff] [blame] | 378 | const bool hairline = aaFlags == GrQuadAAFlags::kAll && |
| 379 | coverage[0] < 1.f && |
| 380 | coverage[1] < 1.f && |
| 381 | coverage[2] < 1.f && |
| 382 | coverage[3] < 1.f; |
| 383 | if (hairline) { |
Michael Ludwig | 290d6df | 2020-10-27 09:28:59 -0400 | [diff] [blame] | 384 | skvx::Vec<4, float> len = fAAHelper.getEdgeLengths(); |
| 385 | // Using max guards us against trying to scale a degenerate triangle edge of 0 len |
| 386 | // up to 2px. The shuffles are so that edge 0's adjustment is based on the lengths |
| 387 | // of its connecting edges (1 and 2), and so forth. |
| 388 | skvx::Vec<4, float> maxWH = max(skvx::shuffle<1, 0, 3, 2>(len), |
| 389 | skvx::shuffle<2, 3, 0, 1>(len)); |
| 390 | // wh + 2e' = 2, so e' = (2 - wh) / 2 => e' = e * (2 - wh). But if w or h > 1, then |
| 391 | // 2 - wh < 1 and represents the non-narrow axis so clamp to 1. |
| 392 | edgeDistances *= max(1.f, 2.f - maxWH); |
| 393 | } |
Michael Ludwig | 704d540 | 2019-11-25 09:43:37 -0500 | [diff] [blame] | 394 | fAAHelper.outset(edgeDistances, deviceQuad, localQuad); |
| 395 | fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kZeroCoverage, color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 396 | geomSubset, uvSubset); |
Michael Ludwig | d84dd4b | 2019-11-05 12:03:12 -0500 | [diff] [blame] | 397 | } |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 398 | } else { |
| 399 | // No outsetting needed, just write a single quad with full coverage |
Michael Ludwig | 73dbea6 | 2019-11-19 14:55:36 -0500 | [diff] [blame] | 400 | SkASSERT(fVertexSpec.coverageMode() == CoverageMode::kNone && |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 401 | !fVertexSpec.requiresGeometrySubset()); |
Michael Ludwig | 189c980 | 2019-11-21 11:21:12 -0500 | [diff] [blame] | 402 | fWriteProc(&fVertexWriter, fVertexSpec, deviceQuad, localQuad, kFullCoverage, color, |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 403 | kIgnoredSubset, uvSubset); |
Michael Ludwig | 460eb5e | 2018-10-29 11:09:29 -0400 | [diff] [blame] | 404 | } |
| 405 | } |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 406 | |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 407 | sk_sp<const GrBuffer> GetIndexBuffer(GrMeshDrawTarget* target, |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 408 | IndexBufferOption indexBufferOption) { |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 409 | auto resourceProvider = target->resourceProvider(); |
| 410 | |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 411 | switch (indexBufferOption) { |
| 412 | case IndexBufferOption::kPictureFramed: return resourceProvider->refAAQuadIndexBuffer(); |
| 413 | case IndexBufferOption::kIndexedRects: return resourceProvider->refNonAAQuadIndexBuffer(); |
| 414 | case IndexBufferOption::kTriStrips: // fall through |
| 415 | default: return nullptr; |
| 416 | } |
| 417 | } |
Robert Phillips | c554dcf | 2019-10-28 11:43:55 -0400 | [diff] [blame] | 418 | |
Robert Phillips | 438d986 | 2019-11-14 12:46:05 -0500 | [diff] [blame] | 419 | int QuadLimit(IndexBufferOption option) { |
| 420 | switch (option) { |
| 421 | case IndexBufferOption::kPictureFramed: return GrResourceProvider::MaxNumAAQuads(); |
| 422 | case IndexBufferOption::kIndexedRects: return GrResourceProvider::MaxNumNonAAQuads(); |
| 423 | case IndexBufferOption::kTriStrips: return SK_MaxS32; // not limited by an indexBuffer |
| 424 | } |
| 425 | |
| 426 | SkUNREACHABLE; |
| 427 | } |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 428 | |
Chris Dalton | dbb833b | 2020-03-17 12:15:46 -0600 | [diff] [blame] | 429 | void IssueDraw(const GrCaps& caps, GrOpsRenderPass* renderPass, const VertexSpec& spec, |
| 430 | int runningQuadCount, int quadsInDraw, int maxVerts, int absVertBufferOffset) { |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 431 | if (spec.indexBufferOption() == IndexBufferOption::kTriStrips) { |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 432 | int offset = absVertBufferOffset + |
| 433 | runningQuadCount * GrResourceProvider::NumVertsPerNonAAQuad(); |
Chris Dalton | dbb833b | 2020-03-17 12:15:46 -0600 | [diff] [blame] | 434 | renderPass->draw(4, offset); |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 435 | return; |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 436 | } |
| 437 | |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 438 | SkASSERT(spec.indexBufferOption() == IndexBufferOption::kPictureFramed || |
| 439 | spec.indexBufferOption() == IndexBufferOption::kIndexedRects); |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 440 | |
Robert Phillips | 2f05a48 | 2019-11-25 09:54:43 -0500 | [diff] [blame] | 441 | int maxNumQuads, numIndicesPerQuad, numVertsPerQuad; |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 442 | |
| 443 | if (spec.indexBufferOption() == IndexBufferOption::kPictureFramed) { |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 444 | // AA uses 8 vertices and 30 indices per quad, basically nested rectangles |
Robert Phillips | 2f05a48 | 2019-11-25 09:54:43 -0500 | [diff] [blame] | 445 | maxNumQuads = GrResourceProvider::MaxNumAAQuads(); |
| 446 | numIndicesPerQuad = GrResourceProvider::NumIndicesPerAAQuad(); |
| 447 | numVertsPerQuad = GrResourceProvider::NumVertsPerAAQuad(); |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 448 | } else { |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 449 | // Non-AA uses 4 vertices and 6 indices per quad |
Robert Phillips | 2f05a48 | 2019-11-25 09:54:43 -0500 | [diff] [blame] | 450 | maxNumQuads = GrResourceProvider::MaxNumNonAAQuads(); |
| 451 | numIndicesPerQuad = GrResourceProvider::NumIndicesPerNonAAQuad(); |
| 452 | numVertsPerQuad = GrResourceProvider::NumVertsPerNonAAQuad(); |
Robert Phillips | fd0c3b5 | 2019-11-01 08:44:42 -0400 | [diff] [blame] | 453 | } |
| 454 | |
Robert Phillips | 2f05a48 | 2019-11-25 09:54:43 -0500 | [diff] [blame] | 455 | SkASSERT(runningQuadCount + quadsInDraw <= maxNumQuads); |
| 456 | |
| 457 | if (caps.avoidLargeIndexBufferDraws()) { |
| 458 | // When we need to avoid large index buffer draws we modify the base vertex of the draw |
| 459 | // which, in GL, requires rebinding all vertex attrib arrays, so a base index is generally |
| 460 | // preferred. |
| 461 | int offset = absVertBufferOffset + runningQuadCount * numVertsPerQuad; |
| 462 | |
Chris Dalton | dbb833b | 2020-03-17 12:15:46 -0600 | [diff] [blame] | 463 | renderPass->drawIndexPattern(numIndicesPerQuad, quadsInDraw, maxNumQuads, numVertsPerQuad, |
| 464 | offset); |
Robert Phillips | 2f05a48 | 2019-11-25 09:54:43 -0500 | [diff] [blame] | 465 | } else { |
| 466 | int baseIndex = runningQuadCount * numIndicesPerQuad; |
| 467 | int numIndicesToDraw = quadsInDraw * numIndicesPerQuad; |
| 468 | |
| 469 | int minVertex = runningQuadCount * numVertsPerQuad; |
Michael Ludwig | fa1cb40 | 2021-06-02 10:53:16 -0400 | [diff] [blame] | 470 | int maxVertex = (runningQuadCount + quadsInDraw) * numVertsPerQuad - 1; // inclusive |
Robert Phillips | 2f05a48 | 2019-11-25 09:54:43 -0500 | [diff] [blame] | 471 | |
Chris Dalton | dbb833b | 2020-03-17 12:15:46 -0600 | [diff] [blame] | 472 | renderPass->drawIndexed(numIndicesToDraw, baseIndex, minVertex, maxVertex, |
| 473 | absVertBufferOffset); |
Robert Phillips | 2f05a48 | 2019-11-25 09:54:43 -0500 | [diff] [blame] | 474 | } |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 475 | } |
| 476 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 477 | ////////////////// VertexSpec Implementation |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 478 | |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 479 | int VertexSpec::deviceDimensionality() const { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 480 | return this->deviceQuadType() == GrQuad::Type::kPerspective ? 3 : 2; |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | int VertexSpec::localDimensionality() const { |
Michael Ludwig | de4c58c | 2019-06-04 09:12:59 -0400 | [diff] [blame] | 484 | return fHasLocalCoords ? (this->localQuadType() == GrQuad::Type::kPerspective ? 3 : 2) : 0; |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 485 | } |
| 486 | |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 487 | CoverageMode VertexSpec::coverageMode() const { |
| 488 | if (this->usesCoverageAA()) { |
| 489 | if (this->compatibleWithCoverageAsAlpha() && this->hasVertexColors() && |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 490 | !this->requiresGeometrySubset()) { |
| 491 | // Using a geometric subset acts as a second source of coverage and folding |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 492 | // the original coverage into color makes it impossible to apply the color's |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 493 | // alpha to the geometric subset's coverage when the original shape is clipped. |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 494 | return CoverageMode::kWithColor; |
| 495 | } else { |
| 496 | return CoverageMode::kWithPosition; |
| 497 | } |
| 498 | } else { |
| 499 | return CoverageMode::kNone; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // This needs to stay in sync w/ QuadPerEdgeAAGeometryProcessor::initializeAttrs |
| 504 | size_t VertexSpec::vertexSize() const { |
| 505 | bool needsPerspective = (this->deviceDimensionality() == 3); |
| 506 | CoverageMode coverageMode = this->coverageMode(); |
| 507 | |
| 508 | size_t count = 0; |
| 509 | |
| 510 | if (coverageMode == CoverageMode::kWithPosition) { |
| 511 | if (needsPerspective) { |
| 512 | count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType); |
| 513 | } else { |
| 514 | count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType) + |
| 515 | GrVertexAttribTypeSize(kFloat_GrVertexAttribType); |
| 516 | } |
| 517 | } else { |
| 518 | if (needsPerspective) { |
| 519 | count += GrVertexAttribTypeSize(kFloat3_GrVertexAttribType); |
| 520 | } else { |
| 521 | count += GrVertexAttribTypeSize(kFloat2_GrVertexAttribType); |
| 522 | } |
| 523 | } |
| 524 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 525 | if (this->requiresGeometrySubset()) { |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 526 | count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType); |
| 527 | } |
| 528 | |
| 529 | count += this->localDimensionality() * GrVertexAttribTypeSize(kFloat_GrVertexAttribType); |
| 530 | |
| 531 | if (ColorType::kByte == this->colorType()) { |
| 532 | count += GrVertexAttribTypeSize(kUByte4_norm_GrVertexAttribType); |
Brian Osman | 2715bf5 | 2019-12-06 14:38:47 -0500 | [diff] [blame] | 533 | } else if (ColorType::kFloat == this->colorType()) { |
| 534 | count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType); |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 535 | } |
| 536 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 537 | if (this->hasSubset()) { |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 538 | count += GrVertexAttribTypeSize(kFloat4_GrVertexAttribType); |
| 539 | } |
| 540 | |
| 541 | return count; |
| 542 | } |
| 543 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 544 | ////////////////// Geometry Processor Implementation |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 545 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 546 | class QuadPerEdgeAAGeometryProcessor : public GrGeometryProcessor { |
| 547 | public: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 548 | static GrGeometryProcessor* Make(SkArenaAlloc* arena, const VertexSpec& spec) { |
Mike Klein | f124108 | 2020-12-14 15:59:09 -0600 | [diff] [blame] | 549 | return arena->make([&](void* ptr) { |
| 550 | return new (ptr) QuadPerEdgeAAGeometryProcessor(spec); |
| 551 | }); |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 552 | } |
| 553 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 554 | static GrGeometryProcessor* Make(SkArenaAlloc* arena, |
| 555 | const VertexSpec& vertexSpec, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 556 | const GrShaderCaps& caps, |
| 557 | const GrBackendFormat& backendFormat, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 558 | GrSamplerState samplerState, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 559 | const GrSwizzle& swizzle, |
| 560 | sk_sp<GrColorSpaceXform> textureColorSpaceXform, |
| 561 | Saturate saturate) { |
Mike Klein | f124108 | 2020-12-14 15:59:09 -0600 | [diff] [blame] | 562 | return arena->make([&](void* ptr) { |
| 563 | return new (ptr) QuadPerEdgeAAGeometryProcessor( |
| 564 | vertexSpec, caps, backendFormat, samplerState, swizzle, |
| 565 | std::move(textureColorSpaceXform), saturate); |
| 566 | }); |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 567 | } |
| 568 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 569 | const char* name() const override { return "QuadPerEdgeAAGeometryProcessor"; } |
Michael Ludwig | 024e262 | 2018-11-30 13:25:55 -0500 | [diff] [blame] | 570 | |
Brian Salomon | 13b2873 | 2021-08-06 15:33:58 -0400 | [diff] [blame] | 571 | void addToKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override { |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 572 | // texturing, device-dimensions are single bit flags |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 573 | b->addBool(fTexSubset.isInitialized(), "subset"); |
| 574 | b->addBool(fSampler.isInitialized(), "textured"); |
| 575 | b->addBool(fNeedsPerspective, "perspective"); |
| 576 | b->addBool((fSaturate == Saturate::kYes), "saturate"); |
| 577 | |
| 578 | b->addBool(fLocalCoord.isInitialized(), "hasLocalCoords"); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 579 | if (fLocalCoord.isInitialized()) { |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 580 | // 2D (0) or 3D (1) |
| 581 | b->addBits(1, (kFloat3_GrVertexAttribType == fLocalCoord.cpuType()), "localCoordsType"); |
Brian Osman | 78dc72c | 2018-12-03 13:20:43 +0000 | [diff] [blame] | 582 | } |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 583 | b->addBool(fColor.isInitialized(), "hasColor"); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 584 | if (fColor.isInitialized()) { |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 585 | // bytes (0) or floats (1) |
| 586 | b->addBits(1, (kFloat4_GrVertexAttribType == fColor.cpuType()), "colorType"); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 587 | } |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 588 | // and coverage mode, 00 for none, 01 for withposition, 10 for withcolor, 11 for |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 589 | // position+geomsubset |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 590 | uint32_t coverageKey = 0; |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 591 | SkASSERT(!fGeomSubset.isInitialized() || fCoverageMode == CoverageMode::kWithPosition); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 592 | if (fCoverageMode != CoverageMode::kNone) { |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 593 | coverageKey = fGeomSubset.isInitialized() |
| 594 | ? 0x3 |
| 595 | : (CoverageMode::kWithPosition == fCoverageMode ? 0x1 : 0x2); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 596 | } |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 597 | b->addBits(2, coverageKey, "coverageMode"); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 598 | |
Brian Osman | 48d7f7c | 2021-03-03 13:38:08 -0500 | [diff] [blame] | 599 | b->add32(GrColorSpaceXform::XformKey(fTextureColorSpaceXform.get()), "colorSpaceXform"); |
Brian Osman | 78dc72c | 2018-12-03 13:20:43 +0000 | [diff] [blame] | 600 | } |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 601 | |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame] | 602 | std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const override { |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 603 | class Impl : public ProgramImpl { |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 604 | public: |
Brian Osman | 609f159 | 2020-07-01 15:14:39 -0400 | [diff] [blame] | 605 | void setData(const GrGLSLProgramDataManager& pdman, |
Brian Salomon | 5a32828 | 2021-04-14 10:32:25 -0400 | [diff] [blame] | 606 | const GrShaderCaps&, |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 607 | const GrGeometryProcessor& geomProc) override { |
| 608 | const auto& gp = geomProc.cast<QuadPerEdgeAAGeometryProcessor>(); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 609 | fTextureColorSpaceXformHelper.setData(pdman, gp.fTextureColorSpaceXform.get()); |
| 610 | } |
| 611 | |
| 612 | private: |
| 613 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 614 | using Interpolation = GrGLSLVaryingHandler::Interpolation; |
| 615 | |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 616 | const auto& gp = args.fGeomProc.cast<QuadPerEdgeAAGeometryProcessor>(); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 617 | fTextureColorSpaceXformHelper.emitCode(args.fUniformHandler, |
| 618 | gp.fTextureColorSpaceXform.get()); |
| 619 | |
| 620 | args.fVaryingHandler->emitAttributes(gp); |
| 621 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 622 | if (gp.fCoverageMode == CoverageMode::kWithPosition) { |
| 623 | // Strip last channel from the vertex attribute to remove coverage and get the |
| 624 | // actual position |
| 625 | if (gp.fNeedsPerspective) { |
| 626 | args.fVertBuilder->codeAppendf("float3 position = %s.xyz;", |
| 627 | gp.fPosition.name()); |
| 628 | } else { |
| 629 | args.fVertBuilder->codeAppendf("float2 position = %s.xy;", |
| 630 | gp.fPosition.name()); |
| 631 | } |
| 632 | gpArgs->fPositionVar = {"position", |
| 633 | gp.fNeedsPerspective ? kFloat3_GrSLType |
| 634 | : kFloat2_GrSLType, |
Ben Wagner | dabd98c | 2020-03-25 15:17:18 -0400 | [diff] [blame] | 635 | GrShaderVar::TypeModifier::None}; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 636 | } else { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 637 | // No coverage to eliminate |
| 638 | gpArgs->fPositionVar = gp.fPosition.asShaderVar(); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 639 | } |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 640 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 641 | // This attribute will be uninitialized if earlier FP analysis determined no |
| 642 | // local coordinates are needed (and this will not include the inline texture |
| 643 | // fetch this GP does before invoking FPs). |
| 644 | gpArgs->fLocalCoordVar = gp.fLocalCoord.asShaderVar(); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 645 | |
| 646 | // Solid color before any texturing gets modulated in |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 647 | const char* blendDst; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 648 | if (gp.fColor.isInitialized()) { |
Michael Ludwig | 3d2753e | 2019-03-29 14:36:32 -0400 | [diff] [blame] | 649 | SkASSERT(gp.fCoverageMode != CoverageMode::kWithColor || !gp.fNeedsPerspective); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 650 | // The color cannot be flat if the varying coverage has been modulated into it |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 651 | args.fFragBuilder->codeAppendf("half4 %s;", args.fOutputColor); |
Brian Salomon | 4895946 | 2021-08-11 13:01:06 -0400 | [diff] [blame] | 652 | args.fVaryingHandler->addPassThroughAttribute( |
| 653 | gp.fColor.asShaderVar(), |
| 654 | args.fOutputColor, |
| 655 | gp.fCoverageMode == CoverageMode::kWithColor |
| 656 | ? Interpolation::kInterpolated |
| 657 | : Interpolation::kCanBeFlat); |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 658 | blendDst = args.fOutputColor; |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 659 | } else { |
| 660 | // Output color must be initialized to something |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 661 | args.fFragBuilder->codeAppendf("half4 %s = half4(1);", args.fOutputColor); |
| 662 | blendDst = nullptr; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | // If there is a texture, must also handle texture coordinates and reading from |
| 666 | // the texture in the fragment shader before continuing to fragment processors. |
| 667 | if (gp.fSampler.isInitialized()) { |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 668 | // Texture coordinates clamped by the subset on the fragment shader; if the GP |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 669 | // has a texture, it's guaranteed to have local coordinates |
| 670 | args.fFragBuilder->codeAppend("float2 texCoord;"); |
| 671 | if (gp.fLocalCoord.cpuType() == kFloat3_GrVertexAttribType) { |
| 672 | // Can't do a pass through since we need to perform perspective division |
| 673 | GrGLSLVarying v(gp.fLocalCoord.gpuType()); |
| 674 | args.fVaryingHandler->addVarying(gp.fLocalCoord.name(), &v); |
| 675 | args.fVertBuilder->codeAppendf("%s = %s;", |
| 676 | v.vsOut(), gp.fLocalCoord.name()); |
| 677 | args.fFragBuilder->codeAppendf("texCoord = %s.xy / %s.z;", |
| 678 | v.fsIn(), v.fsIn()); |
| 679 | } else { |
Brian Salomon | 4895946 | 2021-08-11 13:01:06 -0400 | [diff] [blame] | 680 | args.fVaryingHandler->addPassThroughAttribute(gp.fLocalCoord.asShaderVar(), |
| 681 | "texCoord"); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 684 | // Clamp the now 2D localCoordName variable by the subset if it is provided |
| 685 | if (gp.fTexSubset.isInitialized()) { |
| 686 | args.fFragBuilder->codeAppend("float4 subset;"); |
Brian Salomon | 4895946 | 2021-08-11 13:01:06 -0400 | [diff] [blame] | 687 | args.fVaryingHandler->addPassThroughAttribute(gp.fTexSubset.asShaderVar(), |
| 688 | "subset", |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 689 | Interpolation::kCanBeFlat); |
| 690 | args.fFragBuilder->codeAppend( |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 691 | "texCoord = clamp(texCoord, subset.LT, subset.RB);"); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | // Now modulate the starting output color by the texture lookup |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 695 | args.fFragBuilder->codeAppendf( |
| 696 | "%s = %s(", |
| 697 | args.fOutputColor, |
| 698 | (gp.fSaturate == Saturate::kYes) ? "saturate" : ""); |
Brian Salomon | 87e9ddb | 2019-12-19 14:50:22 -0500 | [diff] [blame] | 699 | args.fFragBuilder->appendTextureLookupAndBlend( |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 700 | blendDst, SkBlendMode::kModulate, args.fTexSamplers[0], |
Brian Salomon | d19cd76 | 2020-01-06 13:16:31 -0500 | [diff] [blame] | 701 | "texCoord", &fTextureColorSpaceXformHelper); |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 702 | args.fFragBuilder->codeAppend(");"); |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 703 | } else { |
| 704 | // Saturate is only intended for use with a proxy to account for the fact |
Robert Phillips | fbf0214 | 2021-09-01 16:31:34 -0400 | [diff] [blame^] | 705 | // that TextureOp skips SkPaint conversion, which normally handles this. |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 706 | SkASSERT(gp.fSaturate == Saturate::kNo); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | // And lastly, output the coverage calculation code |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 710 | if (gp.fCoverageMode == CoverageMode::kWithPosition) { |
| 711 | GrGLSLVarying coverage(kFloat_GrSLType); |
| 712 | args.fVaryingHandler->addVarying("coverage", &coverage); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 713 | if (gp.fNeedsPerspective) { |
Michael Ludwig | 3d2753e | 2019-03-29 14:36:32 -0400 | [diff] [blame] | 714 | // Multiply by "W" in the vertex shader, then by 1/w (sk_FragCoord.w) in |
| 715 | // the fragment shader to get screen-space linear coverage. |
| 716 | args.fVertBuilder->codeAppendf("%s = %s.w * %s.z;", |
| 717 | coverage.vsOut(), gp.fPosition.name(), |
| 718 | gp.fPosition.name()); |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 719 | args.fFragBuilder->codeAppendf("float coverage = %s * sk_FragCoord.w;", |
| 720 | coverage.fsIn()); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 721 | } else { |
Jim Van Verth | d5d9c21 | 2019-05-02 10:22:10 -0400 | [diff] [blame] | 722 | args.fVertBuilder->codeAppendf("%s = %s;", |
| 723 | coverage.vsOut(), gp.fCoverage.name()); |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 724 | args.fFragBuilder->codeAppendf("float coverage = %s;", coverage.fsIn()); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 725 | } |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 726 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 727 | if (gp.fGeomSubset.isInitialized()) { |
| 728 | // Calculate distance from sk_FragCoord to the 4 edges of the subset |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 729 | // and clamp them to (0, 1). Use the minimum of these and the original |
| 730 | // coverage. This only has to be done in the exterior triangles, the |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 731 | // interior of the quad geometry can never be clipped by the subset box. |
| 732 | args.fFragBuilder->codeAppend("float4 geoSubset;"); |
Brian Salomon | 4895946 | 2021-08-11 13:01:06 -0400 | [diff] [blame] | 733 | args.fVaryingHandler->addPassThroughAttribute(gp.fGeomSubset.asShaderVar(), |
| 734 | "geoSubset", |
| 735 | Interpolation::kCanBeFlat); |
Brian Salomon | 659e71f | 2021-02-24 10:09:02 -0500 | [diff] [blame] | 736 | #ifdef SK_USE_LEGACY_AA_QUAD_SUBSET |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 737 | args.fFragBuilder->codeAppend( |
| 738 | "if (coverage < 0.5) {" |
| 739 | " float4 dists4 = clamp(float4(1, 1, -1, -1) * " |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 740 | "(sk_FragCoord.xyxy - geoSubset), 0, 1);" |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 741 | " float2 dists2 = dists4.xy * dists4.zw;" |
| 742 | " coverage = min(coverage, dists2.x * dists2.y);" |
| 743 | "}"); |
Brian Salomon | 659e71f | 2021-02-24 10:09:02 -0500 | [diff] [blame] | 744 | #else |
| 745 | args.fFragBuilder->codeAppend( |
| 746 | // This is lifted from GrAARectEffect. It'd be nice if we could |
| 747 | // invoke a FP from a GP rather than duplicate this code. |
| 748 | "half4 dists4 = clamp(half4(1, 1, -1, -1) * " |
| 749 | "half4(sk_FragCoord.xyxy - geoSubset), 0, 1);\n" |
| 750 | "half2 dists2 = dists4.xy + dists4.zw - 1;\n" |
| 751 | "half subsetCoverage = dists2.x * dists2.y;\n" |
| 752 | "coverage = min(coverage, subsetCoverage);"); |
| 753 | #endif |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 754 | } |
| 755 | |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 756 | args.fFragBuilder->codeAppendf("half4 %s = half4(half(coverage));", |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 757 | args.fOutputCoverage); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 758 | } else { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 759 | // Set coverage to 1, since it's either non-AA or the coverage was already |
| 760 | // folded into the output color |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 761 | SkASSERT(!gp.fGeomSubset.isInitialized()); |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 762 | args.fFragBuilder->codeAppendf("const half4 %s = half4(1);", |
| 763 | args.fOutputCoverage); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 766 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 767 | GrGLSLColorSpaceXformHelper fTextureColorSpaceXformHelper; |
| 768 | }; |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 769 | |
| 770 | return std::make_unique<Impl>(); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | private: |
Robert Phillips | 2145390 | 2021-08-27 16:05:04 -0400 | [diff] [blame] | 774 | using Saturate = skgpu::v1::TextureOp::Saturate; |
Brian Salomon | bab2d11 | 2021-08-11 09:59:56 -0400 | [diff] [blame] | 775 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 776 | QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec) |
| 777 | : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID) |
| 778 | , fTextureColorSpaceXform(nullptr) { |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 779 | SkASSERT(!spec.hasSubset()); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 780 | this->initializeAttrs(spec); |
| 781 | this->setTextureSamplerCnt(0); |
| 782 | } |
| 783 | |
Brian Salomon | 67529b2 | 2019-08-13 15:31:04 -0400 | [diff] [blame] | 784 | QuadPerEdgeAAGeometryProcessor(const VertexSpec& spec, |
| 785 | const GrShaderCaps& caps, |
Robert Phillips | f272bea | 2019-10-17 08:56:16 -0400 | [diff] [blame] | 786 | const GrBackendFormat& backendFormat, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 787 | GrSamplerState samplerState, |
Greg Daniel | 2c3398d | 2019-06-19 11:58:01 -0400 | [diff] [blame] | 788 | const GrSwizzle& swizzle, |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 789 | sk_sp<GrColorSpaceXform> textureColorSpaceXform, |
| 790 | Saturate saturate) |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 791 | : INHERITED(kQuadPerEdgeAAGeometryProcessor_ClassID) |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 792 | , fSaturate(saturate) |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 793 | , fTextureColorSpaceXform(std::move(textureColorSpaceXform)) |
Robert Phillips | 323471e | 2019-11-11 11:33:37 -0500 | [diff] [blame] | 794 | , fSampler(samplerState, backendFormat, swizzle) { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 795 | SkASSERT(spec.hasLocalCoords()); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 796 | this->initializeAttrs(spec); |
| 797 | this->setTextureSamplerCnt(1); |
| 798 | } |
| 799 | |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 800 | // This needs to stay in sync w/ VertexSpec::vertexSize |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 801 | void initializeAttrs(const VertexSpec& spec) { |
| 802 | fNeedsPerspective = spec.deviceDimensionality() == 3; |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 803 | fCoverageMode = spec.coverageMode(); |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 804 | |
| 805 | if (fCoverageMode == CoverageMode::kWithPosition) { |
| 806 | if (fNeedsPerspective) { |
| 807 | fPosition = {"positionWithCoverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType}; |
| 808 | } else { |
Jim Van Verth | d5d9c21 | 2019-05-02 10:22:10 -0400 | [diff] [blame] | 809 | fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 810 | fCoverage = {"coverage", kFloat_GrVertexAttribType, kFloat_GrSLType}; |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 811 | } |
| 812 | } else { |
| 813 | if (fNeedsPerspective) { |
| 814 | fPosition = {"position", kFloat3_GrVertexAttribType, kFloat3_GrSLType}; |
| 815 | } else { |
| 816 | fPosition = {"position", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 817 | } |
| 818 | } |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 819 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 820 | // Need a geometry subset when the quads are AA and not rectilinear, since their AA |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 821 | // outsetting can go beyond a half pixel. |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 822 | if (spec.requiresGeometrySubset()) { |
| 823 | fGeomSubset = {"geomSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType}; |
Michael Ludwig | dcfbe32 | 2019-04-01 14:55:54 -0400 | [diff] [blame] | 824 | } |
| 825 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 826 | int localDim = spec.localDimensionality(); |
| 827 | if (localDim == 3) { |
| 828 | fLocalCoord = {"localCoord", kFloat3_GrVertexAttribType, kFloat3_GrSLType}; |
| 829 | } else if (localDim == 2) { |
| 830 | fLocalCoord = {"localCoord", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
| 831 | } // else localDim == 0 and attribute remains uninitialized |
| 832 | |
Brian Osman | 2715bf5 | 2019-12-06 14:38:47 -0500 | [diff] [blame] | 833 | if (spec.hasVertexColors()) { |
| 834 | fColor = MakeColorAttribute("color", ColorType::kFloat == spec.colorType()); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 837 | if (spec.hasSubset()) { |
| 838 | fTexSubset = {"texSubset", kFloat4_GrVertexAttribType, kFloat4_GrSLType}; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Jim Van Verth | d5d9c21 | 2019-05-02 10:22:10 -0400 | [diff] [blame] | 841 | this->setVertexAttributes(&fPosition, 6); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | const TextureSampler& onTextureSampler(int) const override { return fSampler; } |
| 845 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 846 | Attribute fPosition; // May contain coverage as last channel |
Jim Van Verth | d5d9c21 | 2019-05-02 10:22:10 -0400 | [diff] [blame] | 847 | Attribute fCoverage; // Used for non-perspective position to avoid Intel Metal issues |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 848 | Attribute fColor; // May have coverage modulated in if the FPs support it |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 849 | Attribute fLocalCoord; |
Brian Salomon | 2432d06 | 2020-04-16 20:48:09 -0400 | [diff] [blame] | 850 | Attribute fGeomSubset; // Screen-space bounding box on geometry+aa outset |
| 851 | Attribute fTexSubset; // Texture-space bounding box on local coords |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 852 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 853 | // The positions attribute may have coverage built into it, so float3 is an ambiguous type |
| 854 | // and may mean 2d with coverage, or 3d with no coverage |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 855 | bool fNeedsPerspective; |
Brian Salomon | f19f9ca | 2019-09-18 15:54:26 -0400 | [diff] [blame] | 856 | // Should saturate() be called on the color? Only relevant when created with a texture. |
| 857 | Saturate fSaturate = Saturate::kNo; |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 858 | CoverageMode fCoverageMode; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 859 | |
| 860 | // Color space will be null and fSampler.isInitialized() returns false when the GP is configured |
| 861 | // to skip texturing. |
| 862 | sk_sp<GrColorSpaceXform> fTextureColorSpaceXform; |
| 863 | TextureSampler fSampler; |
| 864 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 865 | using INHERITED = GrGeometryProcessor; |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 866 | }; |
| 867 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 868 | GrGeometryProcessor* MakeProcessor(SkArenaAlloc* arena, const VertexSpec& spec) { |
| 869 | return QuadPerEdgeAAGeometryProcessor::Make(arena, spec); |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 872 | GrGeometryProcessor* MakeTexturedProcessor(SkArenaAlloc* arena, |
| 873 | const VertexSpec& spec, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 874 | const GrShaderCaps& caps, |
| 875 | const GrBackendFormat& backendFormat, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 876 | GrSamplerState samplerState, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 877 | const GrSwizzle& swizzle, |
| 878 | sk_sp<GrColorSpaceXform> textureColorSpaceXform, |
Brian Salomon | ccb6142 | 2020-01-09 10:46:36 -0500 | [diff] [blame] | 879 | Saturate saturate) { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 880 | return QuadPerEdgeAAGeometryProcessor::Make(arena, spec, caps, backendFormat, samplerState, |
| 881 | swizzle, std::move(textureColorSpaceXform), |
| 882 | saturate); |
Michael Ludwig | 20e909e | 2018-10-30 10:43:57 -0400 | [diff] [blame] | 883 | } |
Michael Ludwig | c182b94 | 2018-11-16 10:27:51 -0500 | [diff] [blame] | 884 | |
| 885 | } // namespace GrQuadPerEdgeAA |