• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrTessellationShader_DEFINED
9 #define GrTessellationShader_DEFINED
10 
11 #include "src/gpu/BufferWriter.h"
12 #include "src/gpu/GrGeometryProcessor.h"
13 #include "src/gpu/GrProgramInfo.h"
14 
15 class SkArenaAlloc;
16 
17  // This is a common base class for shaders in the GPU tessellator.
18 class GrTessellationShader : public GrGeometryProcessor {
19 public:
GrTessellationShader(ClassID classID,GrPrimitiveType primitiveType,int tessellationPatchVertexCount,const SkMatrix & viewMatrix,const SkPMColor4f & color)20     GrTessellationShader(ClassID classID, GrPrimitiveType primitiveType,
21                          int tessellationPatchVertexCount, const SkMatrix& viewMatrix,
22                          const SkPMColor4f& color)
23             : GrGeometryProcessor(classID)
24             , fPrimitiveType(primitiveType)
25             , fTessellationPatchVertexCount(tessellationPatchVertexCount)
26             , fViewMatrix(viewMatrix)
27             , fColor(color) {
28         if (fTessellationPatchVertexCount) {
29             this->setWillUseTessellationShaders();
30         }
31     }
32 
primitiveType()33     GrPrimitiveType primitiveType() const { return fPrimitiveType; }
tessellationPatchVertexCount()34     int tessellationPatchVertexCount() const { return fTessellationPatchVertexCount; }
viewMatrix()35     const SkMatrix& viewMatrix() const { return fViewMatrix; }
color()36     const SkPMColor4f& color() const { return fColor;}
37 
38     // A conic curve is written out with p3=[w,Infinity], but GPUs that don't support infinity can't
39     // detect this. On these platforms we also write out an extra float with each patch that
40     // explicitly tells the shader what type of curve it is.
41     inline constexpr static float kCubicCurveType = 0;
42     inline constexpr static float kConicCurveType = 1;
43     inline constexpr static float kTriangularConicCurveType = 2;  // Conic curve with w=Infinity.
44 
45     // Fills in a 4-point patch in such a way that the shader will recognize it as a conic.
WriteConicPatch(const SkPoint pts[3],float w,skgpu::VertexWriter * writer)46     static void WriteConicPatch(const SkPoint pts[3], float w, skgpu::VertexWriter* writer) {
47         // Write out the 3 conic points to patch[0..2], the weight to patch[3].x, and then set
48         // patch[3].y as NaN to flag this patch as a conic.
49         writer->writeArray(pts, 3);
50         *writer << w << skgpu::VertexWriter::kIEEE_32_infinity;
51     }
WriteConicPatch(const SkPoint pts[3],float w,SkPoint patch[4])52     static void WriteConicPatch(const SkPoint pts[3], float w, SkPoint patch[4]) {
53         skgpu::VertexWriter writer(patch);
54         WriteConicPatch(pts, w, &writer);
55     }
56 
57     struct ProgramArgs {
58         SkArenaAlloc* fArena;
59         const GrSurfaceProxyView& fWriteView;
60         bool fUsesMSAASurface;
61         const GrDstProxyView* fDstProxyView;
62         GrXferBarrierFlags fXferBarrierFlags;
63         GrLoadOp fColorLoadOp;
64         const GrCaps* fCaps;
65     };
66 
67     static const GrPipeline* MakePipeline(const ProgramArgs&, GrAAType,
68                                           GrAppliedClip&&, GrProcessorSet&&);
69 
MakeProgram(const ProgramArgs & args,const GrTessellationShader * shader,const GrPipeline * pipeline,const GrUserStencilSettings * stencil)70     static GrProgramInfo* MakeProgram(const ProgramArgs& args,
71                                       const GrTessellationShader* shader,
72                                       const GrPipeline* pipeline,
73                                       const GrUserStencilSettings* stencil) {
74         return args.fArena->make<GrProgramInfo>(*args.fCaps, args.fWriteView, args.fUsesMSAASurface,
75                                                 pipeline, stencil, shader, shader->fPrimitiveType,
76                                                 shader->fTessellationPatchVertexCount,
77                                                 args.fXferBarrierFlags, args.fColorLoadOp);
78     }
79 
80 private:
81     const GrPrimitiveType fPrimitiveType;
82     const int fTessellationPatchVertexCount;
83     const SkMatrix fViewMatrix;
84     const SkPMColor4f fColor;
85 };
86 
87 #endif
88