blob: 6d343d04e21fef95d67722448bf5cff1bd5d8834 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
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.comc6cf7232011-02-17 16:43:10 +000022#include "GrMatrix.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
24class GrSamplerState {
25public:
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000026 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.comc6cf7232011-02-17 16:43:10 +000044 /**
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.comac10a2d2010-12-22 21:39:39 +000066 enum SampleMode {
67 kNormal_SampleMode, //!< sample color directly
reed@google.comac10a2d2010-12-22 21:39:39 +000068 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.comc6cf7232011-02-17 16:43:10 +000084 * Default sampler state is set to clamp, use normal sampling mode, be
85 * unfiltered, and use identity matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +000086 */
bsalomon@google.com6aab8e32011-06-21 20:32:12 +000087 GrSamplerState()
88 : fRadial2CenterX1()
89 , fRadial2Radius0()
90 , fRadial2PosRoot() {
reed@google.comac10a2d2010-12-22 21:39:39 +000091 this->setClampNoFilter();
92 }
93
bsalomon@google.com6aab8e32011-06-21 20:32:12 +000094 explicit GrSamplerState(Filter filter)
95 : fRadial2CenterX1()
96 , fRadial2Radius0()
97 , fRadial2PosRoot() {
reed@google.comac10a2d2010-12-22 21:39:39 +000098 fWrapX = kClamp_WrapMode;
99 fWrapY = kClamp_WrapMode;
100 fSampleMode = kNormal_SampleMode;
101 fFilter = filter;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000102 fMatrix.setIdentity();
junov@google.com6acc9b32011-05-16 18:32:07 +0000103 fTextureDomain.setEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000105
bsalomon@google.com6aab8e32011-06-21 20:32:12 +0000106 GrSamplerState(WrapMode wx, WrapMode wy, Filter filter)
107 : fRadial2CenterX1()
108 , fRadial2Radius0()
109 , fRadial2PosRoot() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 fWrapX = wx;
111 fWrapY = wy;
112 fSampleMode = kNormal_SampleMode;
113 fFilter = filter;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000114 fMatrix.setIdentity();
junov@google.com6acc9b32011-05-16 18:32:07 +0000115 fTextureDomain.setEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000116 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000117
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000118 GrSamplerState(WrapMode wx, WrapMode wy,
bsalomon@google.com6aab8e32011-06-21 20:32:12 +0000119 const GrMatrix& matrix, Filter filter)
120 : fRadial2CenterX1()
121 , fRadial2Radius0()
122 , fRadial2PosRoot() {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000123 fWrapX = wx;
124 fWrapY = wy;
125 fSampleMode = kNormal_SampleMode;
126 fFilter = filter;
127 fMatrix = matrix;
junov@google.com6acc9b32011-05-16 18:32:07 +0000128 fTextureDomain.setEmpty();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000129 }
130
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000131 GrSamplerState(WrapMode wx, WrapMode wy, SampleMode sample,
bsalomon@google.com6aab8e32011-06-21 20:32:12 +0000132 const GrMatrix& matrix, Filter filter)
133 : fRadial2CenterX1()
134 , fRadial2Radius0()
135 , fRadial2PosRoot() {
reed@google.comac10a2d2010-12-22 21:39:39 +0000136 fWrapX = wx;
137 fWrapY = wy;
138 fSampleMode = sample;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000139 fMatrix = matrix;
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 fFilter = filter;
junov@google.com6acc9b32011-05-16 18:32:07 +0000141 fTextureDomain.setEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000142 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000143
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 WrapMode getWrapX() const { return fWrapX; }
145 WrapMode getWrapY() const { return fWrapY; }
146 SampleMode getSampleMode() const { return fSampleMode; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000147 const GrMatrix& getMatrix() const { return fMatrix; }
junov@google.com6acc9b32011-05-16 18:32:07 +0000148 const GrRect& getTextureDomain() const { return fTextureDomain; }
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +0000149 bool hasTextureDomain() const {return SkIntToScalar(0) != fTextureDomain.right();}
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000150 Filter getFilter() const { return fFilter; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000151
152 bool isGradient() const {
153 return kRadial_SampleMode == fSampleMode ||
154 kRadial2_SampleMode == fSampleMode ||
155 kSweep_SampleMode == fSampleMode;
156 }
157
158 void setWrapX(WrapMode mode) { fWrapX = mode; }
159 void setWrapY(WrapMode mode) { fWrapY = mode; }
160 void setSampleMode(SampleMode mode) { fSampleMode = mode; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000161
162 /**
163 * Sets the sampler's matrix. See SampleMode for explanation of
164 * relationship between the matrix and sample mode.
165 * @param matrix the matrix to set
166 */
167 void setMatrix(const GrMatrix& matrix) { fMatrix = matrix; }
168
169 /**
junov@google.com6acc9b32011-05-16 18:32:07 +0000170 * Sets the sampler's texture coordinate domain to a
171 * custom rectangle, rather than the default (0,1).
172 * This option is currently only supported with kClamp_WrapMode
173 */
174 void setTextureDomain(const GrRect& textureDomain) { fTextureDomain = textureDomain; }
175
176 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000177 * Multiplies the current sampler matrix a matrix
178 *
179 * After this call M' = M*m where M is the old matrix, m is the parameter
180 * to this function, and M' is the new matrix. (We consider points to
181 * be column vectors so tex cood vector t is transformed by matrix X as
182 * t' = X*t.)
183 *
184 * @param matrix the matrix used to modify the matrix.
185 */
186 void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); }
187
188 /**
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000189 * Sets filtering type.
190 * @param filter type of filtering to apply
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000191 */
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000192 void setFilter(Filter filter) { fFilter = filter; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000193
194 void setClampNoFilter() {
195 fWrapX = kClamp_WrapMode;
196 fWrapY = kClamp_WrapMode;
197 fSampleMode = kNormal_SampleMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000198 fFilter = kNearest_Filter;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000199 fMatrix.setIdentity();
junov@google.com6acc9b32011-05-16 18:32:07 +0000200 fTextureDomain.setEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 }
202
203 GrScalar getRadial2CenterX1() const { return fRadial2CenterX1; }
204 GrScalar getRadial2Radius0() const { return fRadial2Radius0; }
205 bool isRadial2PosRoot() const { return fRadial2PosRoot; }
206
207 /**
bsalomon@google.com5782d712011-01-21 21:03:59 +0000208 * Sets the parameters for kRadial2_SampleMode. The texture
209 * matrix must be set so that the first point is at (0,0) and the second
reed@google.comac10a2d2010-12-22 21:39:39 +0000210 * point lies on the x-axis. The second radius minus the first is 1 unit.
211 * The additional parameters to define the gradient are specified by this
212 * function.
213 */
214 void setRadial2Params(GrScalar centerX1, GrScalar radius0, bool posRoot) {
215 fRadial2CenterX1 = centerX1;
216 fRadial2Radius0 = radius0;
217 fRadial2PosRoot = posRoot;
218 }
219
220 static const GrSamplerState& ClampNoFilter() {
221 return gClampNoFilter;
222 }
223
224private:
225 WrapMode fWrapX;
226 WrapMode fWrapY;
227 SampleMode fSampleMode;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000228 Filter fFilter;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000229 GrMatrix fMatrix;
junov@google.com6acc9b32011-05-16 18:32:07 +0000230 GrRect fTextureDomain;
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
232 // these are undefined unless fSampleMode == kRadial2_SampleMode
233 GrScalar fRadial2CenterX1;
234 GrScalar fRadial2Radius0;
235 bool fRadial2PosRoot;
236
237 static const GrSamplerState gClampNoFilter;
238};
239
240#endif
241