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 #ifndef GrStrokeTessellationShader_DEFINED 9 #define GrStrokeTessellationShader_DEFINED 10 11 #include "src/gpu/tessellate/shaders/GrTessellationShader.h" 12 13 #include "include/core/SkStrokeRec.h" 14 #include "src/gpu/GrVx.h" 15 #include "src/gpu/glsl/GrGLSLVarying.h" 16 #include "src/gpu/tessellate/Tessellation.h" 17 18 // Tessellates a batch of stroke patches directly to the canvas. Tessellated stroking works by 19 // creating stroke-width, orthogonal edges at set locations along the curve and then connecting them 20 // with a quad strip. These orthogonal edges come from two different sets: "parametric edges" and 21 // "radial edges". Parametric edges are spaced evenly in the parametric sense, and radial edges 22 // divide the curve's _rotation_ into even steps. The tessellation shader evaluates both sets of 23 // edges and sorts them into a single quad strip. With this combined set of edges we can stroke any 24 // curve, regardless of curvature. 25 class GrStrokeTessellationShader : public GrTessellationShader { 26 using PatchAttribs = skgpu::PatchAttribs; 27 28 public: 29 // Are we using hardware tessellation or indirect draws? 30 enum class Mode : int8_t { 31 kHardwareTessellation, 32 kLog2Indirect, 33 kFixedCount 34 }; 35 36 // 'viewMatrix' is applied to the geometry post tessellation. It cannot have perspective. 37 GrStrokeTessellationShader(const GrShaderCaps&, Mode, PatchAttribs, const SkMatrix& viewMatrix, 38 const SkStrokeRec&, SkPMColor4f, int8_t maxParametricSegments_log2); 39 mode()40 Mode mode() const { return fMode; } attribs()41 PatchAttribs attribs() const { return fPatchAttribs; } hasDynamicStroke()42 bool hasDynamicStroke() const { return fPatchAttribs & PatchAttribs::kStrokeParams; } hasDynamicColor()43 bool hasDynamicColor() const { return fPatchAttribs & PatchAttribs::kColor; } hasExplicitCurveType()44 bool hasExplicitCurveType() const { return fPatchAttribs & PatchAttribs::kExplicitCurveType; } stroke()45 const SkStrokeRec& stroke() const { return fStroke;} maxParametricSegments_log2()46 int8_t maxParametricSegments_log2() const { return fMaxParametricSegments_log2; } fixedCountNumTotalEdges()47 float fixedCountNumTotalEdges() const { return fFixedCountNumTotalEdges;} 48 49 // Used by GrFixedCountTessellator to configure the uniform value that tells the shader how many 50 // total edges are in the triangle strip. setFixedCountNumTotalEdges(int value)51 void setFixedCountNumTotalEdges(int value) { 52 SkASSERT(fMode == Mode::kFixedCount); 53 fFixedCountNumTotalEdges = value; 54 } 55 56 private: name()57 const char* name() const override { 58 switch (fMode) { 59 case Mode::kHardwareTessellation: 60 return "GrStrokeTessellationShader_HardwareImpl"; 61 case Mode::kLog2Indirect: 62 case Mode::kFixedCount: 63 return "GrStrokeTessellationShader_InstancedImpl"; 64 } 65 SkUNREACHABLE; 66 } 67 void addToKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override; 68 std::unique_ptr<ProgramImpl> makeProgramImpl(const GrShaderCaps&) const final; 69 70 const Mode fMode; 71 const PatchAttribs fPatchAttribs; 72 const SkStrokeRec fStroke; 73 const int8_t fMaxParametricSegments_log2; 74 75 constexpr static int kMaxAttribCount = 6; 76 SkSTArray<kMaxAttribCount, Attribute> fAttribs; 77 78 // This is a uniform value used when fMode is kFixedCount that tells the shader how many total 79 // edges are in the triangle strip. 80 float fFixedCountNumTotalEdges = 0; 81 82 class Impl; 83 class HardwareImpl; 84 class InstancedImpl; 85 }; 86 87 // This common base class emits shader code for our parametric/radial stroke tessellation algorithm 88 // described above. The subclass emits its own specific setup code before calling into 89 // emitTessellationCode and emitFragment code. 90 class GrStrokeTessellationShader::Impl : public ProgramImpl { 91 protected: 92 // float cosine_between_vectors(float2 a, float2 b) { ... 93 // 94 // Returns dot(a, b) / (length(a) * length(b)). 95 static const char* kCosineBetweenVectorsFn; 96 97 // float miter_extent(float cosTheta, float miterLimit) { ... 98 // 99 // Extends the middle radius to either the miter point, or the bevel edge if we surpassed the 100 // miter limit and need to revert to a bevel join. 101 static const char* kMiterExtentFn; 102 103 // float num_radial_segments_per_radian(float parametricPrecision, float strokeRadius) { ... 104 // 105 // Returns the number of radial segments required for each radian of rotation, in order for the 106 // curve to appear "smooth" as defined by the parametricPrecision. 107 static const char* kNumRadialSegmentsPerRadianFn; 108 109 // float<N> unchecked_mix(float<N> a, float<N> b, float<N> T) { ... 110 // 111 // Unlike mix(), this does not return b when t==1. But it otherwise seems to get better 112 // precision than "a*(1 - t) + b*t" for things like chopping cubics on exact cusp points. 113 // We override this result anyway when t==1 so it shouldn't be a problem. 114 static const char* kUncheckedMixFn; 115 116 // Emits code that calculates the vertex position and any other inputs to the fragment shader. 117 // The subclass is responsible to define the following symbols before calling this method: 118 // 119 // // Functions. 120 // float2 unchecked_mix(float2, float2, float); 121 // float unchecked_mix(float, float, float); 122 // 123 // // Values provided by either uniforms or attribs. 124 // float2 p0, p1, p2, p3; 125 // float w; 126 // float STROKE_RADIUS; 127 // float 2x2 AFFINE_MATRIX; 128 // float2 TRANSLATE; 129 // 130 // // Values calculated by the specific subclass. 131 // float combinedEdgeID; 132 // bool isFinalEdge; 133 // float numParametricSegments; 134 // float radsPerSegment; 135 // float2 tan0; 136 // float2 tan1; 137 // float strokeOutset; 138 // 139 void emitTessellationCode(const GrStrokeTessellationShader& shader, SkString* code, 140 GrGPArgs* gpArgs, const GrShaderCaps& shaderCaps) const; 141 142 // Emits all necessary fragment code. If using dynamic color, the impl is responsible to set up 143 // a half4 varying for color and provide its name in 'fDynamicColorName'. 144 void emitFragmentCode(const GrStrokeTessellationShader&, const EmitArgs&); 145 146 void setData(const GrGLSLProgramDataManager& pdman, const GrShaderCaps&, 147 const GrGeometryProcessor&) final; 148 149 GrGLSLUniformHandler::UniformHandle fTessControlArgsUniform; 150 GrGLSLUniformHandler::UniformHandle fTranslateUniform; 151 GrGLSLUniformHandler::UniformHandle fAffineMatrixUniform; 152 GrGLSLUniformHandler::UniformHandle fEdgeCountUniform; 153 GrGLSLUniformHandler::UniformHandle fColorUniform; 154 SkString fDynamicColorName; 155 }; 156 157 class GrStrokeTessellationShader::InstancedImpl : public GrStrokeTessellationShader::Impl { 158 void onEmitCode(EmitArgs&, GrGPArgs*) override; 159 }; 160 161 class GrStrokeTessellationShader::HardwareImpl : public GrStrokeTessellationShader::Impl { 162 void onEmitCode(EmitArgs&, GrGPArgs*) override; 163 SkString getTessControlShaderGLSL(const GrGeometryProcessor&, 164 const char* versionAndExtensionDecls, 165 const GrGLSLUniformHandler&, 166 const GrShaderCaps&) const override; 167 SkString getTessEvaluationShaderGLSL(const GrGeometryProcessor&, 168 const char* versionAndExtensionDecls, 169 const GrGLSLUniformHandler&, 170 const GrShaderCaps&) const override; 171 }; 172 173 #endif 174