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