Remove default texture coords / texture matrix
Review URL: https://codereview.appspot.com/6775100
git-svn-id: http://skia.googlecode.com/svn/trunk@6293 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/gl/GrGLEffect.cpp b/src/gpu/gl/GrGLEffect.cpp
index 5e0875b..0bbf1f7 100644
--- a/src/gpu/gl/GrGLEffect.cpp
+++ b/src/gpu/gl/GrGLEffect.cpp
@@ -10,8 +10,6 @@
GrGLEffect::GrGLEffect(const GrBackendEffectFactory& factory)
: fFactory(factory) {
-
- fRequiresTextureMatrix = true;
}
GrGLEffect::~GrGLEffect() {
diff --git a/src/gpu/gl/GrGLEffect.h b/src/gpu/gl/GrGLEffect.h
index 0fd5722..f2142c5 100644
--- a/src/gpu/gl/GrGLEffect.h
+++ b/src/gpu/gl/GrGLEffect.h
@@ -9,7 +9,6 @@
#define GrGLEffect_DEFINED
#include "GrBackendEffectFactory.h"
-#include "GrGLProgram.h"
#include "GrGLShaderBuilder.h"
#include "GrGLShaderVar.h"
#include "GrGLSL.h"
@@ -35,6 +34,7 @@
typedef GrBackendEffectFactory::EffectKey EffectKey;
enum {
+ kNoEffectKey = GrBackendEffectFactory::kNoEffectKey,
// the number of bits in EffectKey available to GenKey
kEffectKeyBits = GrBackendEffectFactory::kEffectKeyBits,
};
@@ -54,9 +54,10 @@
@param key The key that was computed by GenKey() from the generating GrEffect.
Only the bits indicated by GrBackendEffectFactory::kEffectKeyBits are
guaranteed to match the value produced by GenKey();
- @param vertexCoords A vec2 of texture coordinates in the VS, which may be altered. This will
- be removed soon and stages will be responsible for computing their own
- coords.
+ @param vertexCoords A vec2 in the VS that holds the position in local coords. This is either
+ the pre-view-matrix vertex position or if explicit per-vertex texture
+ coords are used with a stage then it is those coordinates. See
+ GrVertexLayout.
@param outputColor A predefined vec4 in the FS in which the stage should place its output
color (or coverage).
@param inputColor A vec4 that holds the input color to the stage in the FS. This may be
@@ -87,15 +88,7 @@
static EffectKey GenTextureKey(const GrEffect&, const GrGLCaps&);
- bool requiresTextureMatrix() const { return fRequiresTextureMatrix; }
-
-
protected:
- // HACK: This is a temporary field that allows GrGLEffect subclasses to opt into the new
- // shader gen where a texture matrix is not automatically inserted. It defaults to true and is
- // set to false in a subclass to opt into the new behavior.
- bool fRequiresTextureMatrix;
-
const GrBackendEffectFactory& fFactory;
};
diff --git a/src/gpu/gl/GrGLProgram.cpp b/src/gpu/gl/GrGLProgram.cpp
index 911b5e4..0283a5a 100644
--- a/src/gpu/gl/GrGLProgram.cpp
+++ b/src/gpu/gl/GrGLProgram.cpp
@@ -10,7 +10,6 @@
#include "GrAllocator.h"
#include "GrEffect.h"
#include "GrGLEffect.h"
-#include "gl/GrGLShaderBuilder.h"
#include "GrGLShaderVar.h"
#include "GrBackendEffectFactory.h"
#include "SkTrace.h"
@@ -23,8 +22,6 @@
#define PRINT_SHADERS 0
-typedef GrGLProgram::Desc::StageDesc StageDesc;
-
#define COL_ATTR_NAME "aColor"
#define COV_ATTR_NAME "aCoverage"
#define EDGE_ATTR_NAME "aEdge"
@@ -80,9 +77,6 @@
for (int s = 0; s < GrDrawState::kNumStages; ++s) {
fEffects[s] = NULL;
- fTextureMatrices[s] = SkMatrix::InvalidMatrix();
- // this is arbitrary, just initialize to something
- fTextureOrigin[s] = GrSurface::kBottomLeft_Origin;
}
this->genProgram(stages);
@@ -602,7 +596,7 @@
if (needComputedColor) {
SkString outColor;
for (int s = 0; s < fDesc.fFirstCoverageStage; ++s) {
- if (fDesc.fStages[s].isEnabled()) {
+ if (GrGLEffect::kNoEffectKey != fDesc.fEffectKeys[s]) {
// create var to hold stage result
outColor = "color";
outColor.appendS32(s);
@@ -621,7 +615,7 @@
builder.setCurrentStage(s);
fEffects[s] = GenStageCode(*stages[s],
- fDesc.fStages[s],
+ fDesc.fEffectKeys[s],
&fUniforms.fStages[s],
inColor.size() ? inColor.c_str() : NULL,
outColor.c_str(),
@@ -698,7 +692,7 @@
SkString outCoverage;
const int& startStage = fDesc.fFirstCoverageStage;
for (int s = startStage; s < GrDrawState::kNumStages; ++s) {
- if (fDesc.fStages[s].isEnabled()) {
+ if (fDesc.fEffectKeys[s]) {
// create var to hold stage output
outCoverage = "coverage";
outCoverage.appendS32(s);
@@ -726,7 +720,7 @@
}
builder.setCurrentStage(s);
fEffects[s] = GenStageCode(*stages[s],
- fDesc.fStages[s],
+ fDesc.fEffectKeys[s],
&fUniforms.fStages[s],
inCoverage.size() ? inCoverage.c_str() : NULL,
outCoverage.c_str(),
@@ -897,7 +891,7 @@
// TODO: Move this function to GrGLShaderBuilder
GrGLEffect* GrGLProgram::GenStageCode(const GrEffectStage& stage,
- const StageDesc& desc,
+ GrGLEffect::EffectKey key,
StageUniforms* uniforms,
const char* fsInColor, // NULL means no incoming color
const char* fsOutColor,
@@ -907,51 +901,7 @@
const GrEffect* effect = stage.getEffect();
GrGLEffect* glEffect = effect->getFactory().createGLInstance(*effect);
- /// Vertex Shader Stuff
-
- const char* vertexCoords;
-
- // Has the effect not yet been updated to insert its own texture matrix if necessary.
- if (glEffect->requiresTextureMatrix()) {
- // Decide whether we need a matrix to transform texture coords and whether the varying needs
- // a perspective coord.
- const char* matName = NULL;
- GrSLType texCoordVaryingType;
- if (desc.fOptFlags & StageDesc::kIdentityMatrix_OptFlagBit) {
- texCoordVaryingType = kVec2f_GrSLType;
- } else {
- uniforms->fTextureMatrixUni = builder->addUniform(GrGLShaderBuilder::kVertex_ShaderType,
- kMat33f_GrSLType, "TexM", &matName);
- builder->getUniformVariable(uniforms->fTextureMatrixUni);
-
- if (desc.fOptFlags & StageDesc::kNoPerspective_OptFlagBit) {
- texCoordVaryingType = kVec2f_GrSLType;
- } else {
- texCoordVaryingType = kVec3f_GrSLType;
- }
- }
- const char *varyingVSName, *varyingFSName;
- builder->addVarying(texCoordVaryingType,
- "Stage",
- &varyingVSName,
- &varyingFSName);
- builder->setupTextureAccess(varyingFSName, texCoordVaryingType);
-
- if (!matName) {
- GrAssert(kVec2f_GrSLType == texCoordVaryingType);
- builder->fVSCode.appendf("\t%s = %s;\n", varyingVSName, vsInCoord);
- } else {
- // varying = texMatrix * texCoord
- builder->fVSCode.appendf("\t%s = (%s * vec3(%s, 1))%s;\n",
- varyingVSName, matName, vsInCoord,
- vector_all_coords(GrSLTypeToVecLength(texCoordVaryingType)));
- }
- vertexCoords = varyingVSName;
- } else {
- vertexCoords = vsInCoord;
- }
-
- // setup texture samplers for gl effect
+ // setup texture samplers for GL effect
int numTextures = effect->numTextures();
SkSTArray<8, GrGLShaderBuilder::TextureSampler> textureSamplers;
textureSamplers.push_back_n(numTextures);
@@ -965,8 +915,8 @@
builder->fFSCode.appendf("\t{ // %s \n", glEffect->name());
glEffect->emitCode(builder,
stage,
- desc.fEffectKey,
- vertexCoords,
+ key,
+ vsInCoord,
fsOutColor,
fsInColor,
textureSamplers);
diff --git a/src/gpu/gl/GrGLProgram.h b/src/gpu/gl/GrGLProgram.h
index bc7b3bc..d385e60 100644
--- a/src/gpu/gl/GrGLProgram.h
+++ b/src/gpu/gl/GrGLProgram.h
@@ -10,6 +10,7 @@
#define GrGLProgram_DEFINED
#include "GrDrawState.h"
+#include "GrGLEffect.h"
#include "GrGLContextInfo.h"
#include "GrGLSL.h"
#include "GrGLTexture.h"
@@ -73,8 +74,7 @@
void setData(const GrDrawState& drawState);
// Parameters that affect code generation
- // These structs should be kept compact; they are the input to an
- // expensive hash key generator.
+ // This structs should be kept compact; it is input to an expensive hash key generator.
struct Desc {
Desc() {
// since we use this as part of a key we can't have any uninitialized
@@ -87,30 +87,6 @@
return reinterpret_cast<const uint32_t*>(this);
}
- struct StageDesc {
- enum OptFlagBits {
- kNoPerspective_OptFlagBit = 1 << 0,
- kIdentityMatrix_OptFlagBit = 1 << 1,
- kIsEnabled_OptFlagBit = 1 << 7
- };
-
- uint8_t fOptFlags;
-
- /** Non-zero if this stage has an effect */
- GrBackendEffectFactory::EffectKey fEffectKey;
-
- inline bool isEnabled() const {
- return SkToBool(fOptFlags & kIsEnabled_OptFlagBit);
- }
- inline void setEnabled(bool newValue) {
- if (newValue) {
- fOptFlags |= kIsEnabled_OptFlagBit;
- } else {
- fOptFlags &= ~kIsEnabled_OptFlagBit;
- }
- }
- };
-
// Specifies where the initial color comes from before the stages are applied.
enum ColorInput {
kSolidWhite_ColorInput,
@@ -137,7 +113,8 @@
// stripped of bits that don't affect program generation
GrVertexLayout fVertexLayout;
- StageDesc fStages[GrDrawState::kNumStages];
+ /** Non-zero if this stage has an effect */
+ GrGLEffect::EffectKey fEffectKeys[GrDrawState::kNumStages];
// To enable experimental geometry shader code (not for use in
// production)
@@ -155,9 +132,6 @@
};
GR_STATIC_ASSERT(!(sizeof(Desc) % 4));
- // for code readability
- typedef Desc::StageDesc StageDesc;
-
private:
struct StageUniforms;
@@ -175,7 +149,7 @@
void genInputColor(GrGLShaderBuilder* builder, SkString* inColor);
static GrGLEffect* GenStageCode(const GrEffectStage& stage,
- const StageDesc& desc, // TODO: Eliminate this
+ GrGLEffect::EffectKey key,
StageUniforms* stageUniforms, // TODO: Eliminate this
const char* fsInColor, // NULL means no incoming color
const char* fsOutColor,
@@ -207,11 +181,7 @@
const char* adjustInColor(const SkString& inColor) const;
struct StageUniforms {
- UniformHandle fTextureMatrixUni;
SkTArray<UniformHandle, true> fSamplerUniforms;
- StageUniforms() {
- fTextureMatrixUni = GrGLUniformManager::kInvalidUniformHandle;
- }
};
struct Uniforms {
@@ -249,9 +219,6 @@
GrColor fCoverage;
GrColor fColorFilterColor;
int fRTHeight;
- /// When it is sent to GL, the texture matrix will be flipped if the texture origin requires.
- SkMatrix fTextureMatrices[GrDrawState::kNumStages];
- GrSurface::Origin fTextureOrigin[GrDrawState::kNumStages];
GrGLEffect* fEffects[GrDrawState::kNumStages];
diff --git a/src/gpu/gl/GrGLShaderBuilder.cpp b/src/gpu/gl/GrGLShaderBuilder.cpp
index 07cdbff..8239740 100644
--- a/src/gpu/gl/GrGLShaderBuilder.cpp
+++ b/src/gpu/gl/GrGLShaderBuilder.cpp
@@ -94,49 +94,19 @@
, fUniformManager(uniformManager)
, fCurrentStageIdx(kNonStageIdx)
, fSetupFragPosition(false)
- , fRTHeightUniform(GrGLUniformManager::kInvalidUniformHandle)
- , fTexCoordVaryingType(kVoid_GrSLType) {
+ , fRTHeightUniform(GrGLUniformManager::kInvalidUniformHandle) {
fPositionVar = &fVSAttrs.push_back();
fPositionVar->set(kVec2f_GrSLType, GrGLShaderVar::kAttribute_TypeModifier, "aPosition");
}
-void GrGLShaderBuilder::setupTextureAccess(const char* varyingFSName, GrSLType varyingType) {
- // FIXME: We don't know how the effect will manipulate the coords. So we give up on using
- // projective texturing and always give the stage 2D coords. This will be fixed when effects
- // are responsible for setting up their own tex coords / tex matrices.
- switch (varyingType) {
- case kVec2f_GrSLType:
- fDefaultTexCoordsName = varyingFSName;
- fTexCoordVaryingType = kVec2f_GrSLType;
- break;
- case kVec3f_GrSLType: {
- fDefaultTexCoordsName = "inCoord";
- GrAssert(kNonStageIdx != fCurrentStageIdx);
- fDefaultTexCoordsName.appendS32(fCurrentStageIdx);
- fTexCoordVaryingType = kVec3f_GrSLType;
- fFSCode.appendf("\t%s %s = %s.xy / %s.z;\n",
- GrGLShaderVar::TypeString(kVec2f_GrSLType),
- fDefaultTexCoordsName.c_str(),
- varyingFSName,
- varyingFSName);
- break;
- }
- default:
- GrCrash("Tex coords must either be Vec2f or Vec3f");
- }
-}
-
void GrGLShaderBuilder::appendTextureLookup(SkString* out,
const GrGLShaderBuilder::TextureSampler& sampler,
const char* coordName,
GrSLType varyingType) const {
GrAssert(NULL != sampler.textureAccess());
+ GrAssert(NULL != coordName);
- if (NULL == coordName) {
- coordName = fDefaultTexCoordsName.c_str();
- varyingType = kVec2f_GrSLType;
- }
out->appendf("%s(%s, %s)",
sample_function_name(varyingType),
this->getUniformCStr(sampler.fSamplerUniform),
diff --git a/src/gpu/gl/GrGLShaderBuilder.h b/src/gpu/gl/GrGLShaderBuilder.h
index 6fdec63..852079a 100644
--- a/src/gpu/gl/GrGLShaderBuilder.h
+++ b/src/gpu/gl/GrGLShaderBuilder.h
@@ -77,18 +77,12 @@
GrGLShaderBuilder(const GrGLContextInfo&, GrGLUniformManager&);
- /** Determines whether we should use texture2D() or texture2Dproj(), and if an explicit divide
- is required for the sample coordinates, creates the new variable and emits the code to
- initialize it. This should only be called by GrGLProgram.*/
- void setupTextureAccess(const char* varyingFSName, GrSLType varyingType);
-
- /** Appends a texture sample with projection if necessary; if coordName is not
- specified, uses fSampleCoords. coordType must either be Vec2f or Vec3f. The latter is
- interpreted as projective texture coords. The vec length and swizzle order of the result
- depends on the GrTextureAccess associated with the TextureSampler. */
+ /** Appends a 2D texture sample with projection if necessary. coordType must either be Vec2f or
+ Vec3f. The latter is interpreted as projective texture coords. The vec length and swizzle
+ order of the result depends on the GrTextureAccess associated with the TextureSampler. */
void appendTextureLookup(SkString* out,
const TextureSampler&,
- const char* coordName = NULL,
+ const char* coordName,
GrSLType coordType = kVec2f_GrSLType) const;
/** Does the work of appendTextureLookup and modulates the result by modulation. The result is
@@ -98,18 +92,9 @@
void appendTextureLookupAndModulate(SkString* out,
const char* modulation,
const TextureSampler&,
- const char* coordName = NULL,
+ const char* coordName,
GrSLType coordType = kVec2f_GrSLType) const;
- /** Gets the name of the default texture coords which are always kVec2f */
- const char* defaultTexCoordsName() const { return fDefaultTexCoordsName.c_str(); }
-
- /* Returns true if the texture matrix from which the default texture coords are computed has
- perspective. */
- bool defaultTextureMatrixIsPerspective() const {
- return fTexCoordVaryingType == kVec3f_GrSLType;
- }
-
/** Emits a helper function outside of main(). Currently ShaderType must be
kFragment_ShaderType. */
void emitFunction(ShaderType shader,
@@ -233,14 +218,6 @@
GrGLUniformManager::UniformHandle fRTHeightUniform;
GrGLShaderVar* fPositionVar;
-
- /// Per-stage settings - only valid while we're inside GrGLProgram::genStageCode().
- //@{
- GrSLType fTexCoordVaryingType; // the type, either Vec2f or Vec3f, of the coords passed
- // as a varying from the VS to the FS.
- SkString fDefaultTexCoordsName; // the name of the default 2D coords value.
- //@}
-
};
#endif
diff --git a/src/gpu/gl/GrGpuGL.h b/src/gpu/gl/GrGpuGL.h
index 34af3fd..7f74d8e 100644
--- a/src/gpu/gl/GrGpuGL.h
+++ b/src/gpu/gl/GrGpuGL.h
@@ -145,18 +145,10 @@
const GrGLContextInfo& glContextInfo() const { return fGLContextInfo; }
- // adjusts texture matrix to account for orientation
- static void AdjustTextureMatrix(const GrTexture* texture, SkMatrix* matrix);
-
- // This helper determines if what optimizations can be applied to the matrix after any coord
- // adjustments are applied. The return is a bitfield of GrGLProgram::StageDesc::OptFlags.
- static int TextureMatrixOptFlags(const GrGLTexture* texture, const GrEffectStage& sampler);
-
static bool BlendCoeffReferencesConstant(GrBlendCoeff coeff);
// for readability of function impls
typedef GrGLProgram::Desc ProgramDesc;
- typedef ProgramDesc::StageDesc StageDesc;
class ProgramCache : public ::GrNoncopyable {
public:
@@ -214,9 +206,6 @@
const GrTextureParams& params,
GrGLTexture* nextTexture);
- // sets the texture matrix for the currently bound program
- void flushTextureMatrix(int stageIdx);
-
// sets the color specified by GrDrawState::setColor()
void flushColor(GrColor color);
diff --git a/src/gpu/gl/GrGpuGL_program.cpp b/src/gpu/gl/GrGpuGL_program.cpp
index 17a3036..73f60ca 100644
--- a/src/gpu/gl/GrGpuGL_program.cpp
+++ b/src/gpu/gl/GrGpuGL_program.cpp
@@ -159,85 +159,6 @@
///////////////////////////////////////////////////////////////////////////////
-// helpers for texture matrices
-
-void GrGpuGL::AdjustTextureMatrix(const GrTexture* texture, SkMatrix* matrix) {
- GrAssert(NULL != texture);
- GrAssert(NULL != matrix);
- if (GrSurface::kBottomLeft_Origin == texture->origin()) {
- SkMatrix invY;
- invY.setAll(SK_Scalar1, 0, 0,
- 0, -SK_Scalar1, SK_Scalar1,
- 0, 0, SkMatrix::I()[8]);
- matrix->postConcat(invY);
- }
-}
-
-int GrGpuGL::TextureMatrixOptFlags(const GrGLTexture* texture,
- const GrEffectStage& stage) {
- GrAssert(NULL != texture);
- SkMatrix matrix;
- stage.getTotalMatrix(&matrix);
-
- bool canBeIndentity = GrSurface::kTopLeft_Origin == texture->origin();
-
- if (canBeIndentity && matrix.isIdentity()) {
- return GrGLProgram::StageDesc::kIdentityMatrix_OptFlagBit;
- } else if (!matrix.hasPerspective()) {
- return GrGLProgram::StageDesc::kNoPerspective_OptFlagBit;
- }
- return 0;
-}
-
-///////////////////////////////////////////////////////////////////////////////
-
-void GrGpuGL::flushTextureMatrix(int s) {
- const GrDrawState& drawState = this->getDrawState();
-
- // FIXME: Still assuming only a single texture per effect
- const GrEffect* effect = drawState.getStage(s).getEffect();
- if (0 == effect->numTextures()) {
- return;
- }
- const GrGLTexture* texture = static_cast<const GrGLTexture*>(effect->texture(0));
- if (NULL != texture) {
-
- bool originChange = fCurrentProgram->fTextureOrigin[s] != texture->origin();
-
- UniformHandle matrixUni = fCurrentProgram->fUniforms.fStages[s].fTextureMatrixUni;
-
- const SkMatrix& hwMatrix = fCurrentProgram->fTextureMatrices[s];
- SkMatrix samplerMatrix;
- drawState.getStage(s).getTotalMatrix(&samplerMatrix);
-
- if (kInvalidUniformHandle != matrixUni &&
- (originChange || !hwMatrix.cheapEqualTo(samplerMatrix))) {
-
- SkMatrix m = samplerMatrix;
- AdjustTextureMatrix(texture, &m);
-
- // ES doesn't allow you to pass true to the transpose param,
- // so do our own transpose
- GrGLfloat mt[] = {
- SkScalarToFloat(m[SkMatrix::kMScaleX]),
- SkScalarToFloat(m[SkMatrix::kMSkewY]),
- SkScalarToFloat(m[SkMatrix::kMPersp0]),
- SkScalarToFloat(m[SkMatrix::kMSkewX]),
- SkScalarToFloat(m[SkMatrix::kMScaleY]),
- SkScalarToFloat(m[SkMatrix::kMPersp1]),
- SkScalarToFloat(m[SkMatrix::kMTransX]),
- SkScalarToFloat(m[SkMatrix::kMTransY]),
- SkScalarToFloat(m[SkMatrix::kMPersp2])
- };
-
- fCurrentProgram->fUniformManager.setMatrix3f(matrixUni, mt);
- fCurrentProgram->fTextureMatrices[s] = samplerMatrix;
- }
-
- fCurrentProgram->fTextureOrigin[s] = texture->origin();
- }
-}
-
void GrGpuGL::flushColor(GrColor color) {
const ProgramDesc& desc = fCurrentProgram->getDesc();
const GrDrawState& drawState = this->getDrawState();
@@ -386,8 +307,6 @@
for (int s = 0; s < GrDrawState::kNumStages; ++s) {
if (this->isStageEnabled(s)) {
this->flushBoundTextureAndParams(s);
-
- this->flushTextureMatrix(s);
}
}
}
@@ -640,40 +559,15 @@
}
for (int s = 0; s < GrDrawState::kNumStages; ++s) {
- StageDesc& stageDesc = desc->fStages[s];
- stageDesc.fOptFlags = 0;
- stageDesc.setEnabled(this->isStageEnabled(s));
-
- bool skip = s < drawState.getFirstCoverageStage() ? skipColor :
- skipCoverage;
-
- if (!skip && stageDesc.isEnabled()) {
+ bool skip = s < drawState.getFirstCoverageStage() ? skipColor : skipCoverage;
+ if (!skip && drawState.isStageEnabled(s)) {
lastEnabledStage = s;
- const GrEffectStage& stage = drawState.getStage(s);
- // FIXME: Still assuming one texture per effect
const GrEffect* effect = drawState.getStage(s).getEffect();
-
- if (effect->numTextures() > 0) {
- const GrGLTexture* texture = static_cast<const GrGLTexture*>(effect->texture(0));
- SkMatrix samplerMatrix;
- stage.getTotalMatrix(&samplerMatrix);
- if (NULL != texture) {
- // We call this helper function rather then simply checking the client-specified
- // texture matrix. This is because we may have to concat a y-inversion to account
- // for texture orientation.
- stageDesc.fOptFlags |= TextureMatrixOptFlags(texture, stage);
- }
- } else {
- // Set identity to do the minimal amount of extra work for the no texture case.
- // This will go away when effects manage their own texture matrix.
- stageDesc.fOptFlags |= StageDesc::kIdentityMatrix_OptFlagBit;
- }
const GrBackendEffectFactory& factory = effect->getFactory();
- stageDesc.fEffectKey = factory.glEffectKey(stage, this->glCaps());
+ desc->fEffectKeys[s] = factory.glEffectKey(drawState.getStage(s), this->glCaps());
} else {
- stageDesc.fOptFlags = 0;
- stageDesc.fEffectKey = 0;
+ desc->fEffectKeys[s] = 0;
}
}