reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef GrSamplerState_DEFINED |
| 19 | #define GrSamplerState_DEFINED |
| 20 | |
| 21 | #include "GrTypes.h" |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 22 | #include "GrMatrix.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | |
| 24 | class GrSamplerState { |
| 25 | public: |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 26 | /** |
| 27 | * The intepretation of the texture matrix depends on the sample mode. The |
| 28 | * texture matrix is applied both when the texture coordinates are explicit |
| 29 | * and when vertex positions are used as texture coordinates. In the latter |
| 30 | * case the texture matrix is applied to the pre-view-matrix position |
| 31 | * values. |
| 32 | * |
| 33 | * kNormal_SampleMode |
| 34 | * The post-matrix texture coordinates are in normalize space with (0,0) at |
| 35 | * the top-left and (1,1) at the bottom right. |
| 36 | * kRadial_SampleMode |
| 37 | * The matrix specifies the radial gradient parameters. |
| 38 | * (0,0) in the post-matrix space is center of the radial gradient. |
| 39 | * kRadial2_SampleMode |
| 40 | * Matrix transforms to space where first circle is centered at the |
| 41 | * origin. The second circle will be centered (x, 0) where x may be |
| 42 | * 0 and is provided by setRadial2Params. The post-matrix space is |
| 43 | * normalized such that 1 is the second radius - first radius. |
| 44 | * kSweepSampleMode |
| 45 | * The angle from the origin of texture coordinates in post-matrix space |
| 46 | * determines the gradient value. |
| 47 | */ |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | enum SampleMode { |
| 49 | kNormal_SampleMode, //!< sample color directly |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | kRadial_SampleMode, //!< treat as radial gradient |
| 51 | kRadial2_SampleMode, //!< treat as 2-point radial gradient |
| 52 | kSweep_SampleMode, //!< treat as sweep gradient |
| 53 | }; |
| 54 | |
| 55 | /** |
| 56 | * Describes how a texture is sampled when coordinates are outside the |
| 57 | * texture border |
| 58 | */ |
| 59 | enum WrapMode { |
| 60 | kClamp_WrapMode, |
| 61 | kRepeat_WrapMode, |
| 62 | kMirror_WrapMode |
| 63 | }; |
| 64 | |
| 65 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 66 | * Default sampler state is set to clamp, use normal sampling mode, be |
| 67 | * unfiltered, and use identity matrix. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 68 | */ |
| 69 | GrSamplerState() { |
| 70 | this->setClampNoFilter(); |
| 71 | } |
| 72 | |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 73 | explicit GrSamplerState(bool filter) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 74 | fWrapX = kClamp_WrapMode; |
| 75 | fWrapY = kClamp_WrapMode; |
| 76 | fSampleMode = kNormal_SampleMode; |
| 77 | fFilter = filter; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 78 | fMatrix.setIdentity(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 79 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 80 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 81 | GrSamplerState(WrapMode wx, WrapMode wy, bool filter) { |
| 82 | fWrapX = wx; |
| 83 | fWrapY = wy; |
| 84 | fSampleMode = kNormal_SampleMode; |
| 85 | fFilter = filter; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 86 | fMatrix.setIdentity(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 87 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 88 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 89 | GrSamplerState(WrapMode wx, WrapMode wy, const GrMatrix& matrix, bool filter) { |
| 90 | fWrapX = wx; |
| 91 | fWrapY = wy; |
| 92 | fSampleMode = kNormal_SampleMode; |
| 93 | fFilter = filter; |
| 94 | fMatrix = matrix; |
| 95 | } |
| 96 | |
| 97 | GrSamplerState(WrapMode wx, WrapMode wy, SampleMode sample, const GrMatrix& matrix, bool filter) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 98 | fWrapX = wx; |
| 99 | fWrapY = wy; |
| 100 | fSampleMode = sample; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 101 | fMatrix = matrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 102 | fFilter = filter; |
| 103 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 104 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | WrapMode getWrapX() const { return fWrapX; } |
| 106 | WrapMode getWrapY() const { return fWrapY; } |
| 107 | SampleMode getSampleMode() const { return fSampleMode; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 108 | const GrMatrix& getMatrix() const { return fMatrix; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 109 | bool isFilter() const { return fFilter; } |
| 110 | |
| 111 | bool isGradient() const { |
| 112 | return kRadial_SampleMode == fSampleMode || |
| 113 | kRadial2_SampleMode == fSampleMode || |
| 114 | kSweep_SampleMode == fSampleMode; |
| 115 | } |
| 116 | |
| 117 | void setWrapX(WrapMode mode) { fWrapX = mode; } |
| 118 | void setWrapY(WrapMode mode) { fWrapY = mode; } |
| 119 | void setSampleMode(SampleMode mode) { fSampleMode = mode; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 120 | |
| 121 | /** |
| 122 | * Sets the sampler's matrix. See SampleMode for explanation of |
| 123 | * relationship between the matrix and sample mode. |
| 124 | * @param matrix the matrix to set |
| 125 | */ |
| 126 | void setMatrix(const GrMatrix& matrix) { fMatrix = matrix; } |
| 127 | |
| 128 | /** |
| 129 | * Multiplies the current sampler matrix a matrix |
| 130 | * |
| 131 | * After this call M' = M*m where M is the old matrix, m is the parameter |
| 132 | * to this function, and M' is the new matrix. (We consider points to |
| 133 | * be column vectors so tex cood vector t is transformed by matrix X as |
| 134 | * t' = X*t.) |
| 135 | * |
| 136 | * @param matrix the matrix used to modify the matrix. |
| 137 | */ |
| 138 | void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); } |
| 139 | |
| 140 | /** |
| 141 | * Enables or disables filtering. |
| 142 | * @param filter indicates whether filtering is applied. |
| 143 | */ |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | void setFilter(bool filter) { fFilter = filter; } |
| 145 | |
| 146 | void setClampNoFilter() { |
| 147 | fWrapX = kClamp_WrapMode; |
| 148 | fWrapY = kClamp_WrapMode; |
| 149 | fSampleMode = kNormal_SampleMode; |
| 150 | fFilter = false; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 151 | fMatrix.setIdentity(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | GrScalar getRadial2CenterX1() const { return fRadial2CenterX1; } |
| 155 | GrScalar getRadial2Radius0() const { return fRadial2Radius0; } |
| 156 | bool isRadial2PosRoot() const { return fRadial2PosRoot; } |
| 157 | |
| 158 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 159 | * Sets the parameters for kRadial2_SampleMode. The texture |
| 160 | * matrix must be set so that the first point is at (0,0) and the second |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 161 | * point lies on the x-axis. The second radius minus the first is 1 unit. |
| 162 | * The additional parameters to define the gradient are specified by this |
| 163 | * function. |
| 164 | */ |
| 165 | void setRadial2Params(GrScalar centerX1, GrScalar radius0, bool posRoot) { |
| 166 | fRadial2CenterX1 = centerX1; |
| 167 | fRadial2Radius0 = radius0; |
| 168 | fRadial2PosRoot = posRoot; |
| 169 | } |
| 170 | |
| 171 | static const GrSamplerState& ClampNoFilter() { |
| 172 | return gClampNoFilter; |
| 173 | } |
| 174 | |
| 175 | private: |
| 176 | WrapMode fWrapX; |
| 177 | WrapMode fWrapY; |
| 178 | SampleMode fSampleMode; |
| 179 | bool fFilter; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame^] | 180 | GrMatrix fMatrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 181 | |
| 182 | // these are undefined unless fSampleMode == kRadial2_SampleMode |
| 183 | GrScalar fRadial2CenterX1; |
| 184 | GrScalar fRadial2Radius0; |
| 185 | bool fRadial2PosRoot; |
| 186 | |
| 187 | static const GrSamplerState gClampNoFilter; |
| 188 | }; |
| 189 | |
| 190 | #endif |
| 191 | |