2010-12-22 21:39:39 +00:00
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
#ifndef GrSamplerState_DEFINED
|
|
|
|
#define GrSamplerState_DEFINED
|
|
|
|
|
|
|
|
#include "GrTypes.h"
|
2011-02-17 16:43:10 +00:00
|
|
|
#include "GrMatrix.h"
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-07-08 18:03:33 +00:00
|
|
|
#define MAX_KERNEL_WIDTH 25
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
class GrSamplerState {
|
|
|
|
public:
|
2011-05-05 12:33:22 +00:00
|
|
|
enum Filter {
|
|
|
|
/**
|
|
|
|
* Read the closest src texel to the sample position
|
|
|
|
*/
|
|
|
|
kNearest_Filter,
|
|
|
|
/**
|
|
|
|
* Blend between closest 4 src texels to sample position (tent filter)
|
|
|
|
*/
|
|
|
|
kBilinear_Filter,
|
|
|
|
/**
|
|
|
|
* Average of 4 bilinear filterings spaced +/- 1 texel from sample
|
|
|
|
* position in x and y. Intended for averaging 16 texels in a downsample
|
|
|
|
* pass. (rasterizing such that texture samples fall exactly halfway
|
2011-08-23 20:54:07 +00:00
|
|
|
* between texels in x and y spaced 4 texels apart.) Only supported
|
|
|
|
* on shader backends.
|
2011-05-05 12:33:22 +00:00
|
|
|
*/
|
|
|
|
k4x4Downsample_Filter,
|
2011-07-08 18:03:33 +00:00
|
|
|
/**
|
|
|
|
* Apply a separable convolution kernel.
|
|
|
|
*/
|
2011-12-12 18:45:07 +00:00
|
|
|
kConvolution_Filter,
|
|
|
|
|
|
|
|
kDefault_Filter = kNearest_Filter
|
2011-05-05 12:33:22 +00:00
|
|
|
};
|
|
|
|
|
2011-02-17 16:43:10 +00:00
|
|
|
/**
|
|
|
|
* The intepretation of the texture matrix depends on the sample mode. The
|
|
|
|
* texture matrix is applied both when the texture coordinates are explicit
|
|
|
|
* and when vertex positions are used as texture coordinates. In the latter
|
|
|
|
* case the texture matrix is applied to the pre-view-matrix position
|
|
|
|
* values.
|
|
|
|
*
|
|
|
|
* kNormal_SampleMode
|
|
|
|
* The post-matrix texture coordinates are in normalize space with (0,0) at
|
|
|
|
* the top-left and (1,1) at the bottom right.
|
|
|
|
* kRadial_SampleMode
|
|
|
|
* The matrix specifies the radial gradient parameters.
|
|
|
|
* (0,0) in the post-matrix space is center of the radial gradient.
|
|
|
|
* kRadial2_SampleMode
|
|
|
|
* Matrix transforms to space where first circle is centered at the
|
|
|
|
* origin. The second circle will be centered (x, 0) where x may be
|
|
|
|
* 0 and is provided by setRadial2Params. The post-matrix space is
|
|
|
|
* normalized such that 1 is the second radius - first radius.
|
|
|
|
* kSweepSampleMode
|
|
|
|
* The angle from the origin of texture coordinates in post-matrix space
|
|
|
|
* determines the gradient value.
|
|
|
|
*/
|
2010-12-22 21:39:39 +00:00
|
|
|
enum SampleMode {
|
|
|
|
kNormal_SampleMode, //!< sample color directly
|
|
|
|
kRadial_SampleMode, //!< treat as radial gradient
|
|
|
|
kRadial2_SampleMode, //!< treat as 2-point radial gradient
|
|
|
|
kSweep_SampleMode, //!< treat as sweep gradient
|
2011-12-12 18:45:07 +00:00
|
|
|
|
|
|
|
kDefault_SampleMode = kNormal_SampleMode
|
2010-12-22 21:39:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Describes how a texture is sampled when coordinates are outside the
|
|
|
|
* texture border
|
|
|
|
*/
|
|
|
|
enum WrapMode {
|
|
|
|
kClamp_WrapMode,
|
|
|
|
kRepeat_WrapMode,
|
2011-12-12 18:45:07 +00:00
|
|
|
kMirror_WrapMode,
|
|
|
|
|
|
|
|
kDefault_WrapMode = kClamp_WrapMode
|
2010-12-22 21:39:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-02-17 16:43:10 +00:00
|
|
|
* Default sampler state is set to clamp, use normal sampling mode, be
|
|
|
|
* unfiltered, and use identity matrix.
|
2010-12-22 21:39:39 +00:00
|
|
|
*/
|
2011-06-21 20:32:12 +00:00
|
|
|
GrSamplerState()
|
|
|
|
: fRadial2CenterX1()
|
|
|
|
, fRadial2Radius0()
|
|
|
|
, fRadial2PosRoot() {
|
2011-12-06 16:30:36 +00:00
|
|
|
this->reset();
|
2010-12-22 21:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WrapMode getWrapX() const { return fWrapX; }
|
|
|
|
WrapMode getWrapY() const { return fWrapY; }
|
|
|
|
SampleMode getSampleMode() const { return fSampleMode; }
|
2011-02-17 16:43:10 +00:00
|
|
|
const GrMatrix& getMatrix() const { return fMatrix; }
|
2011-05-16 18:32:07 +00:00
|
|
|
const GrRect& getTextureDomain() const { return fTextureDomain; }
|
2011-05-19 19:58:58 +00:00
|
|
|
bool hasTextureDomain() const {return SkIntToScalar(0) != fTextureDomain.right();}
|
2011-05-05 12:33:22 +00:00
|
|
|
Filter getFilter() const { return fFilter; }
|
2011-07-08 18:03:33 +00:00
|
|
|
int getKernelWidth() const { return fKernelWidth; }
|
|
|
|
const float* getKernel() const { return fKernel; }
|
|
|
|
const float* getImageIncrement() const { return fImageIncrement; }
|
2011-11-08 19:20:57 +00:00
|
|
|
bool swapsRAndB() const { return fSwapRAndB; }
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
bool isGradient() const {
|
|
|
|
return kRadial_SampleMode == fSampleMode ||
|
|
|
|
kRadial2_SampleMode == fSampleMode ||
|
|
|
|
kSweep_SampleMode == fSampleMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setWrapX(WrapMode mode) { fWrapX = mode; }
|
|
|
|
void setWrapY(WrapMode mode) { fWrapY = mode; }
|
|
|
|
void setSampleMode(SampleMode mode) { fSampleMode = mode; }
|
2011-02-17 16:43:10 +00:00
|
|
|
|
|
|
|
/**
|
2011-12-12 18:45:07 +00:00
|
|
|
* Access the sampler's matrix. See SampleMode for explanation of
|
2011-02-17 16:43:10 +00:00
|
|
|
* relationship between the matrix and sample mode.
|
|
|
|
*/
|
2011-12-12 18:45:07 +00:00
|
|
|
GrMatrix* matrix() { return &fMatrix; }
|
|
|
|
|
2011-05-16 18:32:07 +00:00
|
|
|
/**
|
|
|
|
* Sets the sampler's texture coordinate domain to a
|
|
|
|
* custom rectangle, rather than the default (0,1).
|
|
|
|
* This option is currently only supported with kClamp_WrapMode
|
|
|
|
*/
|
|
|
|
void setTextureDomain(const GrRect& textureDomain) { fTextureDomain = textureDomain; }
|
|
|
|
|
2011-11-08 19:20:57 +00:00
|
|
|
/**
|
|
|
|
* Swaps the R and B components when reading from the texture. Has no effect
|
|
|
|
* if the texture is alpha only.
|
|
|
|
*/
|
|
|
|
void setRAndBSwap(bool swap) { fSwapRAndB = swap; }
|
|
|
|
|
2011-02-17 16:43:10 +00:00
|
|
|
/**
|
|
|
|
* Multiplies the current sampler matrix a matrix
|
|
|
|
*
|
|
|
|
* After this call M' = M*m where M is the old matrix, m is the parameter
|
|
|
|
* to this function, and M' is the new matrix. (We consider points to
|
|
|
|
* be column vectors so tex cood vector t is transformed by matrix X as
|
|
|
|
* t' = X*t.)
|
|
|
|
*
|
|
|
|
* @param matrix the matrix used to modify the matrix.
|
|
|
|
*/
|
|
|
|
void preConcatMatrix(const GrMatrix& matrix) { fMatrix.preConcat(matrix); }
|
|
|
|
|
|
|
|
/**
|
2011-05-05 12:33:22 +00:00
|
|
|
* Sets filtering type.
|
|
|
|
* @param filter type of filtering to apply
|
2011-02-17 16:43:10 +00:00
|
|
|
*/
|
2011-05-05 12:33:22 +00:00
|
|
|
void setFilter(Filter filter) { fFilter = filter; }
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-12-06 16:30:36 +00:00
|
|
|
void reset(WrapMode wrapXAndY,
|
|
|
|
Filter filter,
|
|
|
|
const GrMatrix& matrix) {
|
|
|
|
fWrapX = wrapXAndY;
|
|
|
|
fWrapY = wrapXAndY;
|
2011-12-12 18:45:07 +00:00
|
|
|
fSampleMode = kDefault_SampleMode;
|
2011-12-06 16:30:36 +00:00
|
|
|
fFilter = filter;
|
|
|
|
fMatrix = matrix;
|
|
|
|
fTextureDomain.setEmpty();
|
2011-12-12 16:11:33 +00:00
|
|
|
fSwapRAndB = false;
|
|
|
|
}
|
2011-12-12 18:45:07 +00:00
|
|
|
void reset(WrapMode wrapXAndY,
|
|
|
|
Filter filter) {
|
|
|
|
this->reset(wrapXAndY, filter, GrMatrix::I());
|
|
|
|
}
|
2011-12-12 16:11:33 +00:00
|
|
|
void reset(const GrMatrix& matrix) {
|
2011-12-12 18:45:07 +00:00
|
|
|
this->reset(kDefault_WrapMode, kDefault_Filter, matrix);
|
|
|
|
}
|
|
|
|
void reset() {
|
|
|
|
this->reset(kDefault_WrapMode, kDefault_Filter, GrMatrix::I());
|
2011-12-06 16:30:36 +00:00
|
|
|
}
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
GrScalar getRadial2CenterX1() const { return fRadial2CenterX1; }
|
|
|
|
GrScalar getRadial2Radius0() const { return fRadial2Radius0; }
|
2011-11-10 15:28:16 +00:00
|
|
|
bool isRadial2PosRoot() const { return SkToBool(fRadial2PosRoot); }
|
2011-07-07 14:38:03 +00:00
|
|
|
// do the radial gradient params lead to a linear (rather than quadratic)
|
|
|
|
// equation.
|
|
|
|
bool radial2IsDegenerate() const { return GR_Scalar1 == fRadial2CenterX1; }
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
/**
|
2011-01-21 21:03:59 +00:00
|
|
|
* Sets the parameters for kRadial2_SampleMode. The texture
|
|
|
|
* matrix must be set so that the first point is at (0,0) and the second
|
2010-12-22 21:39:39 +00:00
|
|
|
* point lies on the x-axis. The second radius minus the first is 1 unit.
|
|
|
|
* The additional parameters to define the gradient are specified by this
|
|
|
|
* function.
|
|
|
|
*/
|
|
|
|
void setRadial2Params(GrScalar centerX1, GrScalar radius0, bool posRoot) {
|
|
|
|
fRadial2CenterX1 = centerX1;
|
|
|
|
fRadial2Radius0 = radius0;
|
|
|
|
fRadial2PosRoot = posRoot;
|
|
|
|
}
|
|
|
|
|
2011-07-08 18:03:33 +00:00
|
|
|
void setConvolutionParams(int kernelWidth, const float* kernel, float imageIncrement[2]) {
|
|
|
|
GrAssert(kernelWidth >= 0 && kernelWidth <= MAX_KERNEL_WIDTH);
|
|
|
|
fKernelWidth = kernelWidth;
|
|
|
|
if (NULL != kernel) {
|
|
|
|
memcpy(fKernel, kernel, kernelWidth * sizeof(float));
|
|
|
|
}
|
|
|
|
if (NULL != imageIncrement) {
|
|
|
|
memcpy(fImageIncrement, imageIncrement, sizeof(fImageIncrement));
|
|
|
|
} else {
|
|
|
|
memset(fImageIncrement, 0, sizeof(fImageIncrement));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-22 21:39:39 +00:00
|
|
|
private:
|
2011-11-09 16:39:17 +00:00
|
|
|
WrapMode fWrapX : 8;
|
|
|
|
WrapMode fWrapY : 8;
|
|
|
|
SampleMode fSampleMode : 8;
|
|
|
|
Filter fFilter : 8;
|
2011-02-17 16:43:10 +00:00
|
|
|
GrMatrix fMatrix;
|
2011-11-08 19:20:57 +00:00
|
|
|
bool fSwapRAndB;
|
2011-05-16 18:32:07 +00:00
|
|
|
GrRect fTextureDomain;
|
2010-12-22 21:39:39 +00:00
|
|
|
|
|
|
|
// these are undefined unless fSampleMode == kRadial2_SampleMode
|
|
|
|
GrScalar fRadial2CenterX1;
|
|
|
|
GrScalar fRadial2Radius0;
|
2011-11-09 16:39:17 +00:00
|
|
|
SkBool8 fRadial2PosRoot;
|
2010-12-22 21:39:39 +00:00
|
|
|
|
2011-07-08 18:03:33 +00:00
|
|
|
// These are undefined unless fFilter == kConvolution_Filter
|
2011-11-09 16:39:17 +00:00
|
|
|
uint8_t fKernelWidth;
|
2011-07-08 18:03:33 +00:00
|
|
|
float fImageIncrement[2];
|
2011-11-09 16:39:17 +00:00
|
|
|
float fKernel[MAX_KERNEL_WIDTH];
|
2010-12-22 21:39:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|