epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrSamplerState_DEFINED |
| 12 | #define GrSamplerState_DEFINED |
| 13 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 14 | #include "GrCustomStage.h" |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 15 | #include "GrMatrix.h" |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 16 | #include "GrTypes.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | |
| 18 | class GrSamplerState { |
| 19 | public: |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 20 | enum Filter { |
| 21 | /** |
| 22 | * Read the closest src texel to the sample position |
| 23 | */ |
| 24 | kNearest_Filter, |
| 25 | /** |
| 26 | * Blend between closest 4 src texels to sample position (tent filter) |
| 27 | */ |
| 28 | kBilinear_Filter, |
| 29 | /** |
| 30 | * Average of 4 bilinear filterings spaced +/- 1 texel from sample |
| 31 | * position in x and y. Intended for averaging 16 texels in a downsample |
| 32 | * pass. (rasterizing such that texture samples fall exactly halfway |
bsalomon@google.com | 1f221a7 | 2011-08-23 20:54:07 +0000 | [diff] [blame] | 33 | * between texels in x and y spaced 4 texels apart.) Only supported |
| 34 | * on shader backends. |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 35 | */ |
| 36 | k4x4Downsample_Filter, |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 37 | |
| 38 | kDefault_Filter = kNearest_Filter |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 41 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 42 | * Describes how a texture is sampled when coordinates are outside the |
| 43 | * texture border |
| 44 | */ |
| 45 | enum WrapMode { |
| 46 | kClamp_WrapMode, |
| 47 | kRepeat_WrapMode, |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 48 | kMirror_WrapMode, |
| 49 | |
| 50 | kDefault_WrapMode = kClamp_WrapMode |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 54 | * Default sampler state is set to clamp, use normal sampling mode, be |
| 55 | * unfiltered, and use identity matrix. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | */ |
bsalomon@google.com | 6aab8e3 | 2011-06-21 20:32:12 +0000 | [diff] [blame] | 57 | GrSamplerState() |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 58 | : fCustomStage (NULL) { |
tomhudson@google.com | 194de08 | 2012-05-31 20:35:27 +0000 | [diff] [blame] | 59 | memset(this, 0, sizeof(GrSamplerState)); |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 60 | this->reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 61 | } |
| 62 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 63 | ~GrSamplerState() { |
| 64 | GrSafeUnref(fCustomStage); |
| 65 | } |
| 66 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 67 | bool operator ==(const GrSamplerState& s) const { |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +0000 | [diff] [blame] | 68 | /* We must be bit-identical as far as the CustomStage; |
| 69 | there may be multiple CustomStages that will produce |
| 70 | the same shader code and so are equivalent. |
| 71 | Can't take the address of fWrapX because it's :8 */ |
| 72 | int bitwiseRegion = (intptr_t) &fCustomStage - (intptr_t) this; |
| 73 | GrAssert(sizeof(GrSamplerState) == |
| 74 | bitwiseRegion + sizeof(fCustomStage)); |
| 75 | return !memcmp(this, &s, bitwiseRegion) && |
| 76 | ((fCustomStage == s.fCustomStage) || |
| 77 | (fCustomStage && s.fCustomStage && |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 78 | (fCustomStage->getFactory() == |
| 79 | s.fCustomStage->getFactory()) && |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 80 | fCustomStage->isEqual(*s.fCustomStage))); |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 81 | } |
| 82 | bool operator !=(const GrSamplerState& s) const { return !(*this == s); } |
| 83 | |
tomhudson@google.com | 0bdbed3 | 2012-06-01 19:50:02 +0000 | [diff] [blame] | 84 | GrSamplerState& operator =(const GrSamplerState& s) { |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 85 | // memcpy() breaks refcounting |
| 86 | fWrapX = s.fWrapX; |
| 87 | fWrapY = s.fWrapY; |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 88 | fFilter = s.fFilter; |
| 89 | fMatrix = s.fMatrix; |
| 90 | fSwapRAndB = s.fSwapRAndB; |
| 91 | fTextureDomain = s.fTextureDomain; |
| 92 | |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 93 | fCustomStage = s.fCustomStage; |
| 94 | SkSafeRef(fCustomStage); |
| 95 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 96 | return *this; |
| 97 | } |
| 98 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | WrapMode getWrapX() const { return fWrapX; } |
| 100 | WrapMode getWrapY() const { return fWrapY; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 101 | const GrMatrix& getMatrix() const { return fMatrix; } |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 102 | const GrRect& getTextureDomain() const { return fTextureDomain; } |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 103 | bool hasTextureDomain() const {return SkIntToScalar(0) != fTextureDomain.right();} |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 104 | Filter getFilter() const { return fFilter; } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 105 | bool swapsRAndB() const { return fSwapRAndB; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 106 | |
| 107 | void setWrapX(WrapMode mode) { fWrapX = mode; } |
| 108 | void setWrapY(WrapMode mode) { fWrapY = mode; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 109 | |
| 110 | /** |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 111 | * Access the sampler's matrix. See SampleMode for explanation of |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 112 | * relationship between the matrix and sample mode. |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 113 | */ |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 114 | GrMatrix* matrix() { return &fMatrix; } |
| 115 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 116 | /** |
junov@google.com | 6acc9b3 | 2011-05-16 18:32:07 +0000 | [diff] [blame] | 117 | * Sets the sampler's texture coordinate domain to a |
| 118 | * custom rectangle, rather than the default (0,1). |
| 119 | * This option is currently only supported with kClamp_WrapMode |
| 120 | */ |
| 121 | void setTextureDomain(const GrRect& textureDomain) { fTextureDomain = textureDomain; } |
| 122 | |
| 123 | /** |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 124 | * Swaps the R and B components when reading from the texture. Has no effect |
| 125 | * if the texture is alpha only. |
| 126 | */ |
| 127 | void setRAndBSwap(bool swap) { fSwapRAndB = swap; } |
| 128 | |
| 129 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 130 | * Multiplies the current sampler matrix a matrix |
| 131 | * |
| 132 | * After this call M' = M*m where M is the old matrix, m is the parameter |
| 133 | * to this function, and M' is the new matrix. (We consider points to |
| 134 | * be column vectors so tex cood vector t is transformed by matrix X as |
| 135 | * t' = X*t.) |
| 136 | * |
| 137 | * @param matrix the matrix used to modify the matrix. |
| 138 | */ |
| 139 | void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); } |
| 140 | |
| 141 | /** |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 142 | * Sets filtering type. |
| 143 | * @param filter type of filtering to apply |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 144 | */ |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 145 | void setFilter(Filter filter) { fFilter = filter; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 146 | |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 147 | void reset(WrapMode wrapXAndY, |
| 148 | Filter filter, |
| 149 | const GrMatrix& matrix) { |
| 150 | fWrapX = wrapXAndY; |
| 151 | fWrapY = wrapXAndY; |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 152 | fFilter = filter; |
| 153 | fMatrix = matrix; |
| 154 | fTextureDomain.setEmpty(); |
| 155 | fSwapRAndB = false; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 156 | GrSafeSetNull(fCustomStage); |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 157 | } |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 158 | void reset(WrapMode wrapXAndY, Filter filter) { |
| 159 | this->reset(wrapXAndY, filter, GrMatrix::I()); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 160 | } |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 161 | void reset(const GrMatrix& matrix) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 162 | this->reset(kDefault_WrapMode, kDefault_Filter, matrix); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 163 | } |
| 164 | void reset() { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 165 | this->reset(kDefault_WrapMode, kDefault_Filter, GrMatrix::I()); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 166 | } |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 167 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 168 | void setCustomStage(GrCustomStage* stage) { |
| 169 | GrSafeAssign(fCustomStage, stage); |
| 170 | } |
| 171 | GrCustomStage* getCustomStage() const { return fCustomStage; } |
| 172 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 173 | private: |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 174 | WrapMode fWrapX : 8; |
| 175 | WrapMode fWrapY : 8; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 176 | Filter fFilter : 8; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 177 | bool fSwapRAndB; |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 178 | GrMatrix fMatrix; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 179 | GrRect fTextureDomain; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 180 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 181 | GrCustomStage* fCustomStage; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | #endif |
| 185 | |