1/*
2 * Copyright 2013 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
8#ifndef GrGLSLGeometryProcessor_DEFINED
9#define GrGLSLGeometryProcessor_DEFINED
10
11#include "src/gpu/glsl/GrGLSLPrimitiveProcessor.h"
12
13class GrGLSLGPBuilder;
14
15/**
16 * If a GL effect needs a GrGLFullShaderBuilder* object to emit vertex code, then it must inherit
17 * from this class. Since paths don't have vertices, this class is only meant to be used internally
18 * by skia, for special cases.
19 */
20class GrGLSLGeometryProcessor : public GrGLSLPrimitiveProcessor {
21public:
22 /* Any general emit code goes in the base class emitCode. Subclasses override onEmitCode */
23 void emitCode(EmitArgs&) final;
24
25 // Generate the final code for assigning transformed coordinates to the varyings recorded in
26 // the call to collectTransforms(). This must happen after FP code emission so that it has
27 // access to any uniforms the FPs registered for uniform sample matrix invocations.
28 void emitTransformCode(GrGLSLVertexBuilder* vb,
29 GrGLSLUniformHandler* uniformHandler) override;
30
31protected:
32 // A helper for setting the matrix on a uniform handle initialized through
33 // writeOutputPosition or writeLocalCoord. Automatically handles elided uniforms,
34 // scale+translate matrices, and state tracking (if provided state pointer is non-null).
35 void setTransform(const GrGLSLProgramDataManager& pdman, const UniformHandle& uniform,
36 const SkMatrix& matrix, SkMatrix* state=nullptr) const;
37
38 struct GrGPArgs {
39 // Used to specify the output variable used by the GP to store its device position. It can
40 // either be a float2 or a float3 (in order to handle perspective). The subclass sets this
41 // in its onEmitCode().
42 GrShaderVar fPositionVar;
43 // Used to specify the variable storing the draw's local coordinates. It can be either a
44 // float2, float3, or void. It can only be void when no FP needs local coordinates. This
45 // variable can be an attribute or local variable, but should not itself be a varying.
46 // GrGLSLGeometryProcessor automatically determines if this must be passed to a FS.
47 GrShaderVar fLocalCoordVar;
48 };
49
50 // Helpers for adding code to write the transformed vertex position. The first simple version
51 // just writes a variable named by 'posName' into the position output variable with the
52 // assumption that the position is 2D. The second version transforms the input position by a
53 // view matrix and the output variable is 2D or 3D depending on whether the view matrix is
54 // perspective. Both versions declare the output position variable and will set
55 // GrGPArgs::fPositionVar.
56 void writeOutputPosition(GrGLSLVertexBuilder*, GrGPArgs*, const char* posName);
57 void writeOutputPosition(GrGLSLVertexBuilder*,
58 GrGLSLUniformHandler* uniformHandler,
59 GrGPArgs*,
60 const char* posName,
61 const SkMatrix& mat,
62 UniformHandle* viewMatrixUniform);
63
64 // Helper to transform an existing variable by a given local matrix (e.g. the inverse view
65 // matrix). It will declare the transformed local coord variable and will set
66 // GrGPArgs::fLocalCoordVar.
67 void writeLocalCoord(GrGLSLVertexBuilder*, GrGLSLUniformHandler*, GrGPArgs*,
68 GrShaderVar localVar, const SkMatrix& localMatrix,
69 UniformHandle* localMatrixUniform);
70
71 // GPs that use writeOutputPosition and/or writeLocalCoord must incorporate the matrix type
72 // into their key, and should use this function or one of the other related helpers.
73 static uint32_t ComputeMatrixKey(const SkMatrix& mat) {
74 if (mat.isIdentity()) {
75 return 0b00;
76 } else if (mat.isScaleTranslate()) {
77 return 0b01;
78 } else if (!mat.hasPerspective()) {
79 return 0b10;
80 } else {
81 return 0b11;
82 }
83 }
84 static uint32_t ComputeMatrixKeys(const SkMatrix& viewMatrix, const SkMatrix& localMatrix) {
85 return (ComputeMatrixKey(viewMatrix) << kMatrixKeyBits) | ComputeMatrixKey(localMatrix);
86 }
87 static uint32_t AddMatrixKeys(uint32_t flags, const SkMatrix& viewMatrix,
88 const SkMatrix& localMatrix) {
89 // Shifting to make room for the matrix keys shouldn't lose bits
90 SkASSERT(((flags << (2 * kMatrixKeyBits)) >> (2 * kMatrixKeyBits)) == flags);
91 return (flags << (2 * kMatrixKeyBits)) | ComputeMatrixKeys(viewMatrix, localMatrix);
92 }
93 static constexpr int kMatrixKeyBits = 2;
94
95private:
96 virtual void onEmitCode(EmitArgs&, GrGPArgs*) = 0;
97
98 // Iterates over the FPs in 'handler' to register additional varyings and uniforms to support
99 // VS-promoted local coord evaluation for the FPs. Subclasses must call this with
100 // 'localCoordsVar' set to an SkSL variable expression of type 'float2' or 'float3' representing
101 // the original local coordinates of the draw.
102 //
103 // This must happen before FP code emission so that the FPs can find the appropriate varying
104 // handles they use in place of explicit coord sampling; it is automatically called after
105 // onEmitCode() returns using the value stored in GpArgs::fLocalCoordVar.
106 void collectTransforms(GrGLSLVertexBuilder* vb,
107 GrGLSLVaryingHandler* varyingHandler,
108 GrGLSLUniformHandler* uniformHandler,
109 const GrShaderVar& localCoordsVar,
110 FPCoordTransformHandler* handler);
111
112 struct TransformInfo {
113 // The vertex-shader output variable to assign the transformed coordinates to
114 GrShaderVar fOutputCoords;
115 // The coordinate to be transformed
116 GrShaderVar fLocalCoords;
117 // The leaf FP of a transform hierarchy to be evaluated in the vertex shader;
118 // this FP will be const-uniform sampled, and all of its parents will have a sample matrix
119 // type of none or const-uniform.
120 const GrFragmentProcessor* fFP;
121 };
122 SkTArray<TransformInfo> fTransformInfos;
123
124 typedef GrGLSLPrimitiveProcessor INHERITED;
125};
126
127#endif
128