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