aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/gl/GrGLEffectMatrix.h
blob: 1a116565342958c8a33652f4f7937c5e4678d3eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrGLEffectMatrix_DEFINED
#define GrGLEffectMatrix_DEFINED

#include "GrGLEffect.h"
#include "SkMatrix.h"

class GrTexture;

/**
 * This is a helper to implement a texture matrix in a GrGLEffect.
 */
class GrGLEffectMatrix {
public:
    typedef GrGLEffect::EffectKey EffectKey;
    /**
     * The matrix uses kKeyBits of the effect's EffectKey. A GrGLEffect may place these bits at an
     * arbitrary shift in its final key. However, when GrGLEffectMatrix::emitCode*() code is called
     * the relevant bits must be in the lower kKeyBits of the key parameter.
     */
    enum {
        kKeyBits = 2,
        kKeyMask = (1 << kKeyBits) - 1,
    };

    GrGLEffectMatrix() : fUni(GrGLUniformManager::kInvalidUniformHandle) {
        fPrevMatrix = SkMatrix::InvalidMatrix();
    }

    /**
     * Generates the key for the portion of the code emitted by this class's emitCode() function.
     * Pass a texture to make GrGLEffectMatrix automatically adjust for the texture's origin. Pass
     * NULL when not using the EffectMatrix for a texture lookups, or if the GrGLEffect subclass
     * wants to handle origin adjustments in some other manner. coordChangeMatrix is the matrix
     * from GrEffectStage.
     */
    static EffectKey GenKey(const SkMatrix& effectMatrix,
                            const SkMatrix& coordChangeMatrix,
                            const GrTexture*);

    /**
     * Emits code to implement the matrix in the VS. A varying is added as an output of the VS and
     * input to the FS. The varying may be either a vec2f or vec3f depending upon whether
     * perspective interpolation is required or not. The names of the varying in the VS and FS are
     * are returned as output parameters and the type of the varying is the return value. The suffix
     * is an optional parameter that can be used to make all variables emitted by the object
     * unique within a stage. It is only necessary if multiple GrGLEffectMatrix objects are used by
     * a GrGLEffect.
     */
    GrSLType emitCode(GrGLShaderBuilder*,
                      EffectKey,
                      const char* vertexCoords,
                      const char** fsCoordName, /* optional */
                      const char** vsCoordName = NULL,
                      const char* suffix = NULL);

    /**
     * This is similar to emitCode except that it performs perspective division in the FS if the
     * texture coordinates have a w coordinate. The fsCoordName always refers to a vec2f.
     */
    void emitCodeMakeFSCoords2D(GrGLShaderBuilder*,
                                EffectKey,
                                const char* vertexCoords,
                                const char** fsCoordName, /* optional */
                                const char** vsVaryingName = NULL,
                                GrSLType* vsVaryingType = NULL,
                                const char* suffix = NULL);
    /**
     * Call from a GrGLEffect's subclass to update the texture matrix. The matrix,
     * coordChangeMatrix, and texture params should match those used with GenKey.
     */
    void setData(const GrGLUniformManager& uniformManager,
                 const SkMatrix& effectMatrix,
                 const SkMatrix& coordChangeMatrix,
                 const GrTexture*);

private:
    enum {
        kIdentity_Key   = 0,
        kTrans_Key      = 1,
        kNoPersp_Key    = 2,
        kGeneral_Key    = 3,
    };

    GrGLUniformManager::UniformHandle fUni;
    GrSLType                          fUniType;
    SkMatrix                          fPrevMatrix;
};

#endif