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, |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 29 | kDefault_Filter = kNearest_Filter |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 32 | /** |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | * Describes how a texture is sampled when coordinates are outside the |
| 34 | * texture border |
| 35 | */ |
| 36 | enum WrapMode { |
| 37 | kClamp_WrapMode, |
| 38 | kRepeat_WrapMode, |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 39 | kMirror_WrapMode, |
| 40 | |
| 41 | kDefault_WrapMode = kClamp_WrapMode |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 45 | * Default sampler state is set to clamp, use normal sampling mode, be |
| 46 | * unfiltered, and use identity matrix. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | */ |
bsalomon@google.com | 6aab8e3 | 2011-06-21 20:32:12 +0000 | [diff] [blame] | 48 | GrSamplerState() |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 49 | : fCustomStage (NULL) { |
tomhudson@google.com | 194de08 | 2012-05-31 20:35:27 +0000 | [diff] [blame] | 50 | memset(this, 0, sizeof(GrSamplerState)); |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 51 | this->reset(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 52 | } |
| 53 | |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 54 | ~GrSamplerState() { |
| 55 | GrSafeUnref(fCustomStage); |
| 56 | } |
| 57 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 58 | bool operator ==(const GrSamplerState& s) const { |
tomhudson@google.com | b88bbd2 | 2012-05-01 12:48:07 +0000 | [diff] [blame] | 59 | /* We must be bit-identical as far as the CustomStage; |
| 60 | there may be multiple CustomStages that will produce |
| 61 | the same shader code and so are equivalent. |
| 62 | Can't take the address of fWrapX because it's :8 */ |
| 63 | int bitwiseRegion = (intptr_t) &fCustomStage - (intptr_t) this; |
| 64 | GrAssert(sizeof(GrSamplerState) == |
| 65 | bitwiseRegion + sizeof(fCustomStage)); |
| 66 | return !memcmp(this, &s, bitwiseRegion) && |
| 67 | ((fCustomStage == s.fCustomStage) || |
| 68 | (fCustomStage && s.fCustomStage && |
tomhudson@google.com | d8f856c | 2012-05-10 12:13:36 +0000 | [diff] [blame] | 69 | (fCustomStage->getFactory() == |
| 70 | s.fCustomStage->getFactory()) && |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 71 | fCustomStage->isEqual(*s.fCustomStage))); |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 72 | } |
| 73 | bool operator !=(const GrSamplerState& s) const { return !(*this == s); } |
| 74 | |
tomhudson@google.com | 0bdbed3 | 2012-06-01 19:50:02 +0000 | [diff] [blame] | 75 | GrSamplerState& operator =(const GrSamplerState& s) { |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 76 | // memcpy() breaks refcounting |
| 77 | fWrapX = s.fWrapX; |
| 78 | fWrapY = s.fWrapY; |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 79 | fFilter = s.fFilter; |
| 80 | fMatrix = s.fMatrix; |
| 81 | fSwapRAndB = s.fSwapRAndB; |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 82 | |
tomhudson@google.com | 50e4ce0 | 2012-06-19 15:27:50 +0000 | [diff] [blame] | 83 | GrSafeAssign(fCustomStage, s.fCustomStage); |
tomhudson@google.com | 1117559 | 2012-05-31 14:23:28 +0000 | [diff] [blame] | 84 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 85 | return *this; |
| 86 | } |
| 87 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 88 | WrapMode getWrapX() const { return fWrapX; } |
| 89 | WrapMode getWrapY() const { return fWrapY; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 90 | const GrMatrix& getMatrix() const { return fMatrix; } |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 91 | Filter getFilter() const { return fFilter; } |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 92 | bool swapsRAndB() const { return fSwapRAndB; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | |
| 94 | void setWrapX(WrapMode mode) { fWrapX = mode; } |
| 95 | void setWrapY(WrapMode mode) { fWrapY = mode; } |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 96 | |
| 97 | /** |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 98 | * Access the sampler's matrix. See SampleMode for explanation of |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 99 | * relationship between the matrix and sample mode. |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 100 | */ |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 101 | GrMatrix* matrix() { return &fMatrix; } |
| 102 | |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 103 | /** |
bsalomon@google.com | 0a97be2 | 2011-11-08 19:20:57 +0000 | [diff] [blame] | 104 | * Swaps the R and B components when reading from the texture. Has no effect |
| 105 | * if the texture is alpha only. |
| 106 | */ |
| 107 | void setRAndBSwap(bool swap) { fSwapRAndB = swap; } |
| 108 | |
| 109 | /** |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 110 | * Multiplies the current sampler matrix a matrix |
| 111 | * |
| 112 | * After this call M' = M*m where M is the old matrix, m is the parameter |
| 113 | * to this function, and M' is the new matrix. (We consider points to |
| 114 | * be column vectors so tex cood vector t is transformed by matrix X as |
| 115 | * t' = X*t.) |
| 116 | * |
| 117 | * @param matrix the matrix used to modify the matrix. |
| 118 | */ |
| 119 | void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); } |
| 120 | |
| 121 | /** |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 122 | * Sets filtering type. |
| 123 | * @param filter type of filtering to apply |
bsalomon@google.com | c6cf723 | 2011-02-17 16:43:10 +0000 | [diff] [blame] | 124 | */ |
bsalomon@google.com | 6aef1fb | 2011-05-05 12:33:22 +0000 | [diff] [blame] | 125 | void setFilter(Filter filter) { fFilter = filter; } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 126 | |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 127 | void reset(WrapMode wrapXAndY, |
| 128 | Filter filter, |
| 129 | const GrMatrix& matrix) { |
| 130 | fWrapX = wrapXAndY; |
| 131 | fWrapY = wrapXAndY; |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 132 | fFilter = filter; |
| 133 | fMatrix = matrix; |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 134 | fSwapRAndB = false; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 135 | GrSafeSetNull(fCustomStage); |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 136 | } |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 137 | void reset(WrapMode wrapXAndY, Filter filter) { |
| 138 | this->reset(wrapXAndY, filter, GrMatrix::I()); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 139 | } |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 140 | void reset(const GrMatrix& matrix) { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 141 | this->reset(kDefault_WrapMode, kDefault_Filter, matrix); |
bsalomon@google.com | aa814fe | 2011-12-12 18:45:07 +0000 | [diff] [blame] | 142 | } |
| 143 | void reset() { |
bsalomon@google.com | b505a12 | 2012-05-31 18:40:36 +0000 | [diff] [blame] | 144 | this->reset(kDefault_WrapMode, kDefault_Filter, GrMatrix::I()); |
bsalomon@google.com | 1e266f8 | 2011-12-12 16:11:33 +0000 | [diff] [blame] | 145 | } |
bsalomon@google.com | 9791291 | 2011-12-06 16:30:36 +0000 | [diff] [blame] | 146 | |
tomhudson@google.com | 83e5eb8 | 2012-06-04 19:58:30 +0000 | [diff] [blame] | 147 | GrCustomStage* setCustomStage(GrCustomStage* stage) { |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 148 | GrSafeAssign(fCustomStage, stage); |
tomhudson@google.com | 83e5eb8 | 2012-06-04 19:58:30 +0000 | [diff] [blame] | 149 | return stage; |
tomhudson@google.com | 07eecdc | 2012-04-20 18:35:38 +0000 | [diff] [blame] | 150 | } |
| 151 | GrCustomStage* getCustomStage() const { return fCustomStage; } |
| 152 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 153 | private: |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 154 | WrapMode fWrapX : 8; |
| 155 | WrapMode fWrapY : 8; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 156 | Filter fFilter : 8; |
senorblanco@chromium.org | 05054f1 | 2012-03-02 21:05:45 +0000 | [diff] [blame] | 157 | bool fSwapRAndB; |
tomhudson@google.com | 898e7b5 | 2012-06-01 20:42:15 +0000 | [diff] [blame] | 158 | GrMatrix fMatrix; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | |
tomhudson@google.com | 02b1ea2 | 2012-04-30 20:19:07 +0000 | [diff] [blame] | 160 | GrCustomStage* fCustomStage; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | #endif |
| 164 | |