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