Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC. |
| 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 | |
| 8 | #include "src/gpu/tessellate/GrFillPathShader.h" |
| 9 | |
| 10 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 11 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 12 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 13 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
| 14 | |
| 15 | class GrFillPathShader::Impl : public GrGLSLGeometryProcessor { |
| 16 | public: |
| 17 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
| 18 | auto& shader = args.fGP.cast<GrFillPathShader>(); |
| 19 | |
| 20 | const char* viewMatrix; |
| 21 | fViewMatrixUniform = args.fUniformHandler->addUniform( |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 22 | nullptr, kVertex_GrShaderFlag, kFloat3x3_GrSLType, "view_matrix", &viewMatrix); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 23 | |
| 24 | args.fVaryingHandler->emitAttributes(shader); |
| 25 | |
| 26 | args.fVertBuilder->codeAppend("float2 localcoord, vertexpos;"); |
| 27 | shader.emitVertexCode(this, args.fVertBuilder, viewMatrix, args.fUniformHandler); |
| 28 | |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 29 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos"); |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame^] | 30 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord"); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 31 | |
| 32 | const char* color; |
| 33 | fColorUniform = args.fUniformHandler->addUniform( |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 34 | nullptr, kFragment_GrShaderFlag, kHalf4_GrSLType, "color", &color); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 35 | |
| 36 | args.fFragBuilder->codeAppendf("%s = %s;", args.fOutputColor, color); |
| 37 | args.fFragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
| 38 | } |
| 39 | |
| 40 | void setData(const GrGLSLProgramDataManager& pdman, const GrPrimitiveProcessor& primProc, |
| 41 | const CoordTransformRange& transformRange) override { |
| 42 | const GrFillPathShader& shader = primProc.cast<GrFillPathShader>(); |
| 43 | pdman.setSkMatrix(fViewMatrixUniform, shader.viewMatrix()); |
| 44 | |
| 45 | const SkPMColor4f& color = shader.fColor; |
| 46 | pdman.set4f(fColorUniform, color.fR, color.fG, color.fB, color.fA); |
| 47 | |
| 48 | if (fPathBoundsUniform.isValid()) { |
| 49 | const SkRect& b = primProc.cast<GrFillBoundingBoxShader>().pathBounds(); |
| 50 | pdman.set4f(fPathBoundsUniform, b.left(), b.top(), b.right(), b.bottom()); |
| 51 | } |
| 52 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame^] | 53 | this->setTransformDataHelper(pdman, transformRange); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | GrGLSLUniformHandler::UniformHandle fViewMatrixUniform; |
| 57 | GrGLSLUniformHandler::UniformHandle fColorUniform; |
| 58 | GrGLSLUniformHandler::UniformHandle fPathBoundsUniform; |
| 59 | }; |
| 60 | |
| 61 | GrGLSLPrimitiveProcessor* GrFillPathShader::createGLSLInstance(const GrShaderCaps&) const { |
| 62 | return new Impl; |
| 63 | } |
| 64 | |
| 65 | void GrFillTriangleShader::emitVertexCode(Impl*, GrGLSLVertexBuilder* v, const char* viewMatrix, |
| 66 | GrGLSLUniformHandler* uniformHandler) const { |
| 67 | v->codeAppendf(R"( |
| 68 | localcoord = input_point; |
| 69 | vertexpos = (%s * float3(localcoord, 1)).xy;)", viewMatrix); |
| 70 | } |
| 71 | |
| 72 | void GrFillCubicHullShader::emitVertexCode(Impl*, GrGLSLVertexBuilder* v, const char* viewMatrix, |
| 73 | GrGLSLUniformHandler* uniformHandler) const { |
| 74 | v->codeAppend(R"( |
| 75 | float4x2 P = float4x2(input_points_0_1, input_points_2_3); |
| 76 | |
| 77 | // Translate the points to v0..3 where v0=0. |
| 78 | float2 v1 = P[1] - P[0], v2 = P[2] - P[0], v3 = P[3] - P[0]; |
| 79 | |
| 80 | // Reorder the points so v2 bisects v1 and v3. |
| 81 | if (sign(determinant(float2x2(v2,v1))) == sign(determinant(float2x2(v2,v3)))) { |
| 82 | float2 tmp = P[2]; |
| 83 | if (sign(determinant(float2x2(v1,v2))) != sign(determinant(float2x2(v1,v3)))) { |
| 84 | P[2] = P[1]; // swap(P2, P1) |
| 85 | P[1] = tmp; |
| 86 | } else { |
| 87 | P[2] = P[3]; // swap(P2, P3) |
| 88 | P[3] = tmp; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Find the "turn direction" of each corner and net turn direction. |
| 93 | float4 dir; |
| 94 | float netdir = 0.0; |
| 95 | for (int i = 0; i < 4; ++i) { |
| 96 | float2 prev = P[i] - P[(i + 3) & 3], next = P[(i + 1) & 3] - P[i]; |
| 97 | dir[i] = sign(determinant(float2x2(prev, next))); |
| 98 | netdir += dir[i]; |
| 99 | } |
| 100 | |
| 101 | // sk_VertexID comes in fan order. Convert to strip order. |
| 102 | int vertexidx = sk_VertexID; |
| 103 | vertexidx ^= vertexidx >> 1; |
| 104 | |
| 105 | // Remove the non-convex vertex, if any. |
| 106 | if (dir[vertexidx] != sign(netdir)) { |
| 107 | vertexidx = (vertexidx + 1) & 3; |
| 108 | } |
| 109 | |
| 110 | localcoord = P[vertexidx];)"); |
| 111 | |
| 112 | v->codeAppendf("vertexpos = (%s * float3(localcoord, 1)).xy;", viewMatrix); |
| 113 | } |
| 114 | |
| 115 | void GrFillBoundingBoxShader::emitVertexCode(Impl* impl, GrGLSLVertexBuilder* v, |
| 116 | const char* viewMatrix, |
| 117 | GrGLSLUniformHandler* uniformHandler) const { |
| 118 | const char* pathBounds; |
| 119 | impl->fPathBoundsUniform = uniformHandler->addUniform( |
Ethan Nicholas | 16464c3 | 2020-04-06 13:53:05 -0400 | [diff] [blame] | 120 | nullptr, kVertex_GrShaderFlag, kFloat4_GrSLType, "path_bounds", &pathBounds); |
Chris Dalton | 4328e92 | 2020-01-29 13:16:14 -0700 | [diff] [blame] | 121 | |
| 122 | v->codeAppendf(R"( |
| 123 | // Use sk_VertexID and uniforms (instead of vertex data) to find vertex positions. |
| 124 | float2 T = float2(sk_VertexID & 1, sk_VertexID >> 1); |
| 125 | localcoord = mix(%s.xy, %s.zw, T); |
| 126 | vertexpos = (%s * float3(localcoord, 1)).xy; |
| 127 | |
| 128 | // Outset to avoid possible T-junctions with extreme edges of the path. |
| 129 | float2x2 M2 = float2x2(%s); |
| 130 | float2 devoutset = .25 * sign(M2 * (T - .5)); |
| 131 | localcoord += inverse(M2) * devoutset; |
| 132 | vertexpos += devoutset;)", pathBounds, pathBounds, viewMatrix, viewMatrix); |
| 133 | } |