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 | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame^] | 26 | enum Filter { |
| 27 | /** |
| 28 | * Read the closest src texel to the sample position |
| 29 | */ |
| 30 | kNearest_Filter, |
| 31 | /** |
| 32 | * Blend between closest 4 src texels to sample position (tent filter) |
| 33 | */ |
| 34 | kBilinear_Filter, |
| 35 | /** |
| 36 | * Average of 4 bilinear filterings spaced +/- 1 texel from sample |
| 37 | * position in x and y. Intended for averaging 16 texels in a downsample |
| 38 | * pass. (rasterizing such that texture samples fall exactly halfway |
| 39 | * between texels in x and y spaced 4 texels apart.) |
| 40 | */ |
| 41 | k4x4Downsample_Filter, |
| 42 | }; |
| 43 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 44 | /** |
| 45 | * The intepretation of the texture matrix depends on the sample mode. The |
| 46 | * texture matrix is applied both when the texture coordinates are explicit |
| 47 | * and when vertex positions are used as texture coordinates. In the latter |
| 48 | * case the texture matrix is applied to the pre-view-matrix position |
| 49 | * values. |
| 50 | * |
| 51 | * kNormal_SampleMode |
| 52 | * The post-matrix texture coordinates are in normalize space with (0,0) at |
| 53 | * the top-left and (1,1) at the bottom right. |
| 54 | * kRadial_SampleMode |
| 55 | * The matrix specifies the radial gradient parameters. |
| 56 | * (0,0) in the post-matrix space is center of the radial gradient. |
| 57 | * kRadial2_SampleMode |
| 58 | * Matrix transforms to space where first circle is centered at the |
| 59 | * origin. The second circle will be centered (x, 0) where x may be |
| 60 | * 0 and is provided by setRadial2Params. The post-matrix space is |
| 61 | * normalized such that 1 is the second radius - first radius. |
| 62 | * kSweepSampleMode |
| 63 | * The angle from the origin of texture coordinates in post-matrix space |
| 64 | * determines the gradient value. |
| 65 | */ |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 66 | enum SampleMode { |
| 67 | kNormal_SampleMode, //!< sample color directly |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 68 | kRadial_SampleMode, //!< treat as radial gradient |
| 69 | kRadial2_SampleMode, //!< treat as 2-point radial gradient |
| 70 | kSweep_SampleMode, //!< treat as sweep gradient |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * Describes how a texture is sampled when coordinates are outside the |
| 75 | * texture border |
| 76 | */ |
| 77 | enum WrapMode { |
| 78 | kClamp_WrapMode, |
| 79 | kRepeat_WrapMode, |
| 80 | kMirror_WrapMode |
| 81 | }; |
| 82 | |
| 83 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 84 | * Default sampler state is set to clamp, use normal sampling mode, be |
| 85 | * unfiltered, and use identity matrix. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | */ |
| 87 | GrSamplerState() { |
| 88 | this->setClampNoFilter(); |
| 89 | } |
| 90 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame^] | 91 | explicit GrSamplerState(Filter filter) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 92 | fWrapX = kClamp_WrapMode; |
| 93 | fWrapY = kClamp_WrapMode; |
| 94 | fSampleMode = kNormal_SampleMode; |
| 95 | fFilter = filter; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 96 | fMatrix.setIdentity(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 98 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame^] | 99 | GrSamplerState(WrapMode wx, WrapMode wy, Filter filter) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 100 | fWrapX = wx; |
| 101 | fWrapY = wy; |
| 102 | fSampleMode = kNormal_SampleMode; |
| 103 | fFilter = filter; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 104 | fMatrix.setIdentity(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 106 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame^] | 107 | GrSamplerState(WrapMode wx, WrapMode wy, |
| 108 | const GrMatrix& matrix, Filter filter) { |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 109 | fWrapX = wx; |
| 110 | fWrapY = wy; |
| 111 | fSampleMode = kNormal_SampleMode; |
| 112 | fFilter = filter; |
| 113 | fMatrix = matrix; |
| 114 | } |
| 115 | |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame^] | 116 | GrSamplerState(WrapMode wx, WrapMode wy, SampleMode sample, |
| 117 | const GrMatrix& matrix, Filter filter) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 118 | fWrapX = wx; |
| 119 | fWrapY = wy; |
| 120 | fSampleMode = sample; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 121 | fMatrix = matrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 122 | fFilter = filter; |
| 123 | } |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 124 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 125 | WrapMode getWrapX() const { return fWrapX; } |
| 126 | WrapMode getWrapY() const { return fWrapY; } |
| 127 | SampleMode getSampleMode() const { return fSampleMode; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 128 | const GrMatrix& getMatrix() const { return fMatrix; } |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame^] | 129 | Filter getFilter() const { return fFilter; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 130 | |
| 131 | bool isGradient() const { |
| 132 | return kRadial_SampleMode == fSampleMode || |
| 133 | kRadial2_SampleMode == fSampleMode || |
| 134 | kSweep_SampleMode == fSampleMode; |
| 135 | } |
| 136 | |
| 137 | void setWrapX(WrapMode mode) { fWrapX = mode; } |
| 138 | void setWrapY(WrapMode mode) { fWrapY = mode; } |
| 139 | void setSampleMode(SampleMode mode) { fSampleMode = mode; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * Sets the sampler's matrix. See SampleMode for explanation of |
| 143 | * relationship between the matrix and sample mode. |
| 144 | * @param matrix the matrix to set |
| 145 | */ |
| 146 | void setMatrix(const GrMatrix& matrix) { fMatrix = matrix; } |
| 147 | |
| 148 | /** |
| 149 | * Multiplies the current sampler matrix a matrix |
| 150 | * |
| 151 | * After this call M' = M*m where M is the old matrix, m is the parameter |
| 152 | * to this function, and M' is the new matrix. (We consider points to |
| 153 | * be column vectors so tex cood vector t is transformed by matrix X as |
| 154 | * t' = X*t.) |
| 155 | * |
| 156 | * @param matrix the matrix used to modify the matrix. |
| 157 | */ |
| 158 | void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); } |
| 159 | |
| 160 | /** |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame^] | 161 | * Sets filtering type. |
| 162 | * @param filter type of filtering to apply |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 163 | */ |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame^] | 164 | void setFilter(Filter filter) { fFilter = filter; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 165 | |
| 166 | void setClampNoFilter() { |
| 167 | fWrapX = kClamp_WrapMode; |
| 168 | fWrapY = kClamp_WrapMode; |
| 169 | fSampleMode = kNormal_SampleMode; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame^] | 170 | fFilter = kNearest_Filter; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 171 | fMatrix.setIdentity(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | GrScalar getRadial2CenterX1() const { return fRadial2CenterX1; } |
| 175 | GrScalar getRadial2Radius0() const { return fRadial2Radius0; } |
| 176 | bool isRadial2PosRoot() const { return fRadial2PosRoot; } |
| 177 | |
| 178 | /** |
bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +0000 | [diff] [blame] | 179 | * Sets the parameters for kRadial2_SampleMode. The texture |
| 180 | * 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] | 181 | * point lies on the x-axis. The second radius minus the first is 1 unit. |
| 182 | * The additional parameters to define the gradient are specified by this |
| 183 | * function. |
| 184 | */ |
| 185 | void setRadial2Params(GrScalar centerX1, GrScalar radius0, bool posRoot) { |
| 186 | fRadial2CenterX1 = centerX1; |
| 187 | fRadial2Radius0 = radius0; |
| 188 | fRadial2PosRoot = posRoot; |
| 189 | } |
| 190 | |
| 191 | static const GrSamplerState& ClampNoFilter() { |
| 192 | return gClampNoFilter; |
| 193 | } |
| 194 | |
| 195 | private: |
| 196 | WrapMode fWrapX; |
| 197 | WrapMode fWrapY; |
| 198 | SampleMode fSampleMode; |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame^] | 199 | Filter fFilter; |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 200 | GrMatrix fMatrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 201 | |
| 202 | // these are undefined unless fSampleMode == kRadial2_SampleMode |
| 203 | GrScalar fRadial2CenterX1; |
| 204 | GrScalar fRadial2Radius0; |
| 205 | bool fRadial2PosRoot; |
| 206 | |
| 207 | static const GrSamplerState gClampNoFilter; |
| 208 | }; |
| 209 | |
| 210 | #endif |
| 211 | |