blob: 9e45f3ef82dd6f193311eaf46ddd887df43ea984 [file] [log] [blame]
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001/*
2 * Copyright 2012 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 GrGLEffectMatrix_DEFINED
9#define GrGLEffectMatrix_DEFINED
10
11#include "GrGLEffect.h"
12#include "SkMatrix.h"
13
14class GrTexture;
15class SkRandom;
16
17/**
18 * This is a helper to implement a texture matrix in a GrGLEffect.
19 */
20class GrGLEffectMatrix {
21public:
22 typedef GrGLEffect::EffectKey EffectKey;
23 /**
24 * The matrix uses kKeyBits of the effect's EffectKey. A GrGLEffect may place these bits at an
25 * arbitrary shift in its final key. However, when GrGLEffectMatrix::emitCode*() code is called
26 * the relevant bits must be in the lower kKeyBits of the key parameter.
27 */
28 enum {
29 kKeyBits = 2,
30 kKeyMask = (1 << kKeyBits) - 1,
31 };
32
33 GrGLEffectMatrix() : fUni(GrGLUniformManager::kInvalidUniformHandle) {
34 fPrevMatrix = SkMatrix::InvalidMatrix();
35 }
36
37 /**
38 * Generates the key for the portion of the code emitted by this class's emitCode() function.
39 * Pass a texture to make GrGLEffectMatrix automatically adjust for the texture's origin. Pass
40 * NULL when not using the EffectMatrix for a texture lookups, or if the GrGLEffect subclass
41 * wants to handle origin adjustments in some other manner. coordChangeMatrix is the matrix
42 * from GrEffectStage.
43 */
44 static EffectKey GenKey(const SkMatrix& effectMatrix,
45 const SkMatrix& coordChangeMatrix,
46 const GrTexture*);
47
48 /**
49 * Emits code to implement the matrix in the VS. A varying is added as an output of the VS and
50 * input to the FS. The varying may be either a vec2f or vec3f depending upon whether
51 * perspective interpolation is required or not. The names of the varying in the VS and FS are
52 * are returned as output parameters and the type of the varying is the return value. The suffix
53 * is an optional parameter that can be used to make all variables emitted by the object
54 * unique within a stage. It is only necessary if multiple GrGLEffectMatrix objects are used by
55 * a GrGLEffect.
56 */
57 GrSLType emitCode(GrGLShaderBuilder*,
58 EffectKey,
59 const char* vertexCoords,
60 const char** fsCoordName, /* optional */
61 const char** vsCoordName = NULL,
62 const char* suffix = NULL);
63
64 /**
65 * This is similar to emitCode except that it performs perspective division in the FS if the
66 * texture coordinates have a w coordinate. The fsCoordName always refers to a vec2f.
67 */
68 void emitCodeMakeFSCoords2D(GrGLShaderBuilder*,
69 EffectKey,
70 const char* vertexCoords,
71 const char** fsCoordName, /* optional */
72 const char** vsVaryingName = NULL,
73 GrSLType* vsVaryingType = NULL,
74 const char* suffix = NULL);
75 /**
76 * Call from a GrGLEffect's subclass to update the texture matrix. The matrix,
77 * coordChangeMatrix, and texture params should match those used with GenKey.
78 */
79 void setData(const GrGLUniformManager& uniformManager,
80 const SkMatrix& effectMatrix,
81 const SkMatrix& coordChangeMatrix,
82 const GrTexture*);
83
84private:
85 enum {
86 kIdentity_Key = 0,
87 kTrans_Key = 1,
88 kNoPersp_Key = 2,
89 kGeneral_Key = 3,
90 };
91
92 GrGLUniformManager::UniformHandle fUni;
93 GrSLType fUniType;
94 SkMatrix fPrevMatrix;
95};
96
97#endif