blob: 1f81afad5ed0085e6570b4ab9b15940660d2aa74 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrSamplerState_DEFINED
12#define GrSamplerState_DEFINED
13
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000014#include "GrCustomStage.h"
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000015#include "GrMatrix.h"
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000016#include "GrTypes.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
18class GrSamplerState {
19public:
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000020 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.comaa814fe2011-12-12 18:45:07 +000029 kDefault_Filter = kNearest_Filter
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000030 };
31
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000032 /**
reed@google.comac10a2d2010-12-22 21:39:39 +000033 * 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.comaa814fe2011-12-12 18:45:07 +000039 kMirror_WrapMode,
40
41 kDefault_WrapMode = kClamp_WrapMode
reed@google.comac10a2d2010-12-22 21:39:39 +000042 };
43
44 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000045 * Default sampler state is set to clamp, use normal sampling mode, be
46 * unfiltered, and use identity matrix.
reed@google.comac10a2d2010-12-22 21:39:39 +000047 */
bsalomon@google.com6aab8e32011-06-21 20:32:12 +000048 GrSamplerState()
tomhudson@google.com898e7b52012-06-01 20:42:15 +000049 : fCustomStage (NULL) {
tomhudson@google.com194de082012-05-31 20:35:27 +000050 memset(this, 0, sizeof(GrSamplerState));
bsalomon@google.com97912912011-12-06 16:30:36 +000051 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000052 }
53
tomhudson@google.com07eecdc2012-04-20 18:35:38 +000054 ~GrSamplerState() {
55 GrSafeUnref(fCustomStage);
56 }
57
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000058 bool operator ==(const GrSamplerState& s) const {
tomhudson@google.comb88bbd22012-05-01 12:48:07 +000059 /* 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.comd8f856c2012-05-10 12:13:36 +000069 (fCustomStage->getFactory() ==
70 s.fCustomStage->getFactory()) &&
bsalomon@google.comb505a122012-05-31 18:40:36 +000071 fCustomStage->isEqual(*s.fCustomStage)));
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000072 }
73 bool operator !=(const GrSamplerState& s) const { return !(*this == s); }
74
tomhudson@google.com0bdbed32012-06-01 19:50:02 +000075 GrSamplerState& operator =(const GrSamplerState& s) {
tomhudson@google.com11175592012-05-31 14:23:28 +000076 // memcpy() breaks refcounting
77 fWrapX = s.fWrapX;
78 fWrapY = s.fWrapY;
tomhudson@google.com11175592012-05-31 14:23:28 +000079 fFilter = s.fFilter;
80 fMatrix = s.fMatrix;
81 fSwapRAndB = s.fSwapRAndB;
82 fTextureDomain = s.fTextureDomain;
83
tomhudson@google.com50e4ce02012-06-19 15:27:50 +000084 GrSafeAssign(fCustomStage, s.fCustomStage);
tomhudson@google.com11175592012-05-31 14:23:28 +000085
tomhudson@google.com02b1ea22012-04-30 20:19:07 +000086 return *this;
87 }
88
reed@google.comac10a2d2010-12-22 21:39:39 +000089 WrapMode getWrapX() const { return fWrapX; }
90 WrapMode getWrapY() const { return fWrapY; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000091 const GrMatrix& getMatrix() const { return fMatrix; }
junov@google.com6acc9b32011-05-16 18:32:07 +000092 const GrRect& getTextureDomain() const { return fTextureDomain; }
senorblanco@chromium.org64cc5792011-05-19 19:58:58 +000093 bool hasTextureDomain() const {return SkIntToScalar(0) != fTextureDomain.right();}
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000094 Filter getFilter() const { return fFilter; }
bsalomon@google.com0a97be22011-11-08 19:20:57 +000095 bool swapsRAndB() const { return fSwapRAndB; }
reed@google.comac10a2d2010-12-22 21:39:39 +000096
97 void setWrapX(WrapMode mode) { fWrapX = mode; }
98 void setWrapY(WrapMode mode) { fWrapY = mode; }
bsalomon@google.comc6cf7232011-02-17 16:43:10 +000099
100 /**
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000101 * Access the sampler's matrix. See SampleMode for explanation of
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000102 * relationship between the matrix and sample mode.
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000103 */
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000104 GrMatrix* matrix() { return &fMatrix; }
105
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000106 /**
junov@google.com6acc9b32011-05-16 18:32:07 +0000107 * Sets the sampler's texture coordinate domain to a
108 * custom rectangle, rather than the default (0,1).
109 * This option is currently only supported with kClamp_WrapMode
110 */
111 void setTextureDomain(const GrRect& textureDomain) { fTextureDomain = textureDomain; }
112
113 /**
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000114 * Swaps the R and B components when reading from the texture. Has no effect
115 * if the texture is alpha only.
116 */
117 void setRAndBSwap(bool swap) { fSwapRAndB = swap; }
118
119 /**
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000120 * Multiplies the current sampler matrix a matrix
121 *
122 * After this call M' = M*m where M is the old matrix, m is the parameter
123 * to this function, and M' is the new matrix. (We consider points to
124 * be column vectors so tex cood vector t is transformed by matrix X as
125 * t' = X*t.)
126 *
127 * @param matrix the matrix used to modify the matrix.
128 */
129 void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); }
130
131 /**
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000132 * Sets filtering type.
133 * @param filter type of filtering to apply
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000134 */
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000135 void setFilter(Filter filter) { fFilter = filter; }
reed@google.comac10a2d2010-12-22 21:39:39 +0000136
bsalomon@google.com97912912011-12-06 16:30:36 +0000137 void reset(WrapMode wrapXAndY,
138 Filter filter,
139 const GrMatrix& matrix) {
140 fWrapX = wrapXAndY;
141 fWrapY = wrapXAndY;
bsalomon@google.com97912912011-12-06 16:30:36 +0000142 fFilter = filter;
143 fMatrix = matrix;
144 fTextureDomain.setEmpty();
145 fSwapRAndB = false;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000146 GrSafeSetNull(fCustomStage);
bsalomon@google.com97912912011-12-06 16:30:36 +0000147 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000148 void reset(WrapMode wrapXAndY, Filter filter) {
149 this->reset(wrapXAndY, filter, GrMatrix::I());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000150 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000151 void reset(const GrMatrix& matrix) {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000152 this->reset(kDefault_WrapMode, kDefault_Filter, matrix);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000153 }
154 void reset() {
bsalomon@google.comb505a122012-05-31 18:40:36 +0000155 this->reset(kDefault_WrapMode, kDefault_Filter, GrMatrix::I());
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000156 }
bsalomon@google.com97912912011-12-06 16:30:36 +0000157
tomhudson@google.com83e5eb82012-06-04 19:58:30 +0000158 GrCustomStage* setCustomStage(GrCustomStage* stage) {
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000159 GrSafeAssign(fCustomStage, stage);
tomhudson@google.com83e5eb82012-06-04 19:58:30 +0000160 return stage;
tomhudson@google.com07eecdc2012-04-20 18:35:38 +0000161 }
162 GrCustomStage* getCustomStage() const { return fCustomStage; }
163
reed@google.comac10a2d2010-12-22 21:39:39 +0000164private:
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000165 WrapMode fWrapX : 8;
166 WrapMode fWrapY : 8;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000167 Filter fFilter : 8;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000168 bool fSwapRAndB;
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000169 GrMatrix fMatrix;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000170 GrRect fTextureDomain;
reed@google.comac10a2d2010-12-22 21:39:39 +0000171
tomhudson@google.com02b1ea22012-04-30 20:19:07 +0000172 GrCustomStage* fCustomStage;
reed@google.comac10a2d2010-12-22 21:39:39 +0000173};
174
175#endif
176