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( |
22 | nullptr, kVertex_GrShaderFlag, kFloat3x3_GrSLType, "view_matrix" , &viewMatrix); |
23 | |
24 | args.fVaryingHandler->emitAttributes(shader); |
25 | |
26 | args.fVertBuilder->codeAppend("float2 localcoord, vertexpos;" ); |
27 | shader.emitVertexCode(this, args.fVertBuilder, viewMatrix, args.fUniformHandler); |
28 | |
29 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "vertexpos" ); |
30 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord" ); |
31 | |
32 | const char* color; |
33 | fColorUniform = args.fUniformHandler->addUniform( |
34 | nullptr, kFragment_GrShaderFlag, kHalf4_GrSLType, "color" , &color); |
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, |
41 | const GrPrimitiveProcessor& primProc) 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 | } |
53 | |
54 | GrGLSLUniformHandler::UniformHandle fViewMatrixUniform; |
55 | GrGLSLUniformHandler::UniformHandle fColorUniform; |
56 | GrGLSLUniformHandler::UniformHandle fPathBoundsUniform; |
57 | }; |
58 | |
59 | GrGLSLPrimitiveProcessor* GrFillPathShader::createGLSLInstance(const GrShaderCaps&) const { |
60 | return new Impl; |
61 | } |
62 | |
63 | void GrFillTriangleShader::emitVertexCode(Impl*, GrGLSLVertexBuilder* v, const char* viewMatrix, |
64 | GrGLSLUniformHandler* uniformHandler) const { |
65 | v->codeAppendf(R"( |
66 | localcoord = input_point; |
67 | vertexpos = (%s * float3(localcoord, 1)).xy;)" , viewMatrix); |
68 | } |
69 | |
70 | void GrFillCubicHullShader::emitVertexCode(Impl*, GrGLSLVertexBuilder* v, const char* viewMatrix, |
71 | GrGLSLUniformHandler* uniformHandler) const { |
72 | v->codeAppend(R"( |
73 | float4x2 P = float4x2(input_points_0_1, input_points_2_3); |
74 | |
75 | // Translate the points to v0..3 where v0=0. |
76 | float2 v1 = P[1] - P[0], v2 = P[2] - P[0], v3 = P[3] - P[0]; |
77 | |
78 | // Reorder the points so v2 bisects v1 and v3. |
79 | if (sign(determinant(float2x2(v2,v1))) == sign(determinant(float2x2(v2,v3)))) { |
80 | float2 tmp = P[2]; |
81 | if (sign(determinant(float2x2(v1,v2))) != sign(determinant(float2x2(v1,v3)))) { |
82 | P[2] = P[1]; // swap(P2, P1) |
83 | P[1] = tmp; |
84 | } else { |
85 | P[2] = P[3]; // swap(P2, P3) |
86 | P[3] = tmp; |
87 | } |
88 | } |
89 | |
90 | // Find the "turn direction" of each corner and net turn direction. |
91 | float4 dir; |
92 | float netdir = 0.0; |
93 | for (int i = 0; i < 4; ++i) { |
94 | float2 prev = P[i] - P[(i + 3) & 3], next = P[(i + 1) & 3] - P[i]; |
95 | dir[i] = sign(determinant(float2x2(prev, next))); |
96 | netdir += dir[i]; |
97 | } |
98 | |
99 | // sk_VertexID comes in fan order. Convert to strip order. |
100 | int vertexidx = sk_VertexID; |
101 | vertexidx ^= vertexidx >> 1; |
102 | |
103 | // Remove the non-convex vertex, if any. |
104 | if (dir[vertexidx] != sign(netdir)) { |
105 | vertexidx = (vertexidx + 1) & 3; |
106 | } |
107 | |
108 | localcoord = P[vertexidx];)" ); |
109 | |
110 | v->codeAppendf("vertexpos = (%s * float3(localcoord, 1)).xy;" , viewMatrix); |
111 | } |
112 | |
113 | void GrFillBoundingBoxShader::emitVertexCode(Impl* impl, GrGLSLVertexBuilder* v, |
114 | const char* viewMatrix, |
115 | GrGLSLUniformHandler* uniformHandler) const { |
116 | const char* pathBounds; |
117 | impl->fPathBoundsUniform = uniformHandler->addUniform( |
118 | nullptr, kVertex_GrShaderFlag, kFloat4_GrSLType, "path_bounds" , &pathBounds); |
119 | |
120 | v->codeAppendf(R"( |
121 | // Use sk_VertexID and uniforms (instead of vertex data) to find vertex positions. |
122 | float2 T = float2(sk_VertexID & 1, sk_VertexID >> 1); |
123 | localcoord = mix(%s.xy, %s.zw, T); |
124 | vertexpos = (%s * float3(localcoord, 1)).xy; |
125 | |
126 | // Outset to avoid possible T-junctions with extreme edges of the path. |
127 | float2x2 M2 = float2x2(%s); |
128 | float2 devoutset = .25 * sign(M2 * (T - .5)); |
129 | localcoord += inverse(M2) * devoutset; |
130 | vertexpos += devoutset;)" , pathBounds, pathBounds, viewMatrix, viewMatrix); |
131 | } |
132 | |