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