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