diff options
Diffstat (limited to 'gpu/src')
-rw-r--r-- | gpu/src/GrContext.cpp | 39 | ||||
-rw-r--r-- | gpu/src/GrDrawTarget.cpp | 17 | ||||
-rw-r--r-- | gpu/src/GrGLTexture.cpp | 35 | ||||
-rw-r--r-- | gpu/src/GrGpu.cpp | 1 | ||||
-rw-r--r-- | gpu/src/GrGpuFactory.cpp | 11 | ||||
-rw-r--r-- | gpu/src/GrGpuGL.cpp | 84 | ||||
-rw-r--r-- | gpu/src/GrGpuGL.h | 40 | ||||
-rw-r--r-- | gpu/src/GrGpuGLFixed.cpp | 57 | ||||
-rw-r--r-- | gpu/src/GrGpuGLFixed.h | 10 | ||||
-rw-r--r-- | gpu/src/GrGpuGLShaders.cpp | 933 | ||||
-rw-r--r-- | gpu/src/GrGpuGLShaders.h | 154 | ||||
-rw-r--r-- | gpu/src/GrGpuGLShaders2.cpp | 104 | ||||
-rw-r--r-- | gpu/src/GrGpuGLShaders2.h | 6 | ||||
-rw-r--r-- | gpu/src/GrMatrix.cpp | 39 | ||||
-rw-r--r-- | gpu/src/GrTextContext.cpp | 3 | ||||
-rw-r--r-- | gpu/src/GrTextureCache.cpp | 4 | ||||
-rw-r--r-- | gpu/src/gr_files.mk | 3 |
17 files changed, 234 insertions, 1306 deletions
diff --git a/gpu/src/GrContext.cpp b/gpu/src/GrContext.cpp index ad7f776a13..0423483c30 100644 --- a/gpu/src/GrContext.cpp +++ b/gpu/src/GrContext.cpp @@ -129,7 +129,6 @@ GrTextureEntry* GrContext::createAndLockTexture(GrTextureKey* key, return NULL;
}
}
- GrTexture* clampTexture = clampEntry->texture();
GrGpu::TextureDesc rtDesc = desc;
rtDesc.fFlags |= GrGpu::kRenderTarget_TextureFlag |
GrGpu::kNoPathRendering_TextureFlag;
@@ -145,7 +144,6 @@ GrTextureEntry* GrContext::createAndLockTexture(GrTextureKey* key, fGpu->setRenderTarget(texture->asRenderTarget());
fGpu->setTexture(0, clampEntry->texture());
fGpu->setStencilPass(GrDrawTarget::kNone_StencilPass);
- fGpu->setTextureMatrix(0, GrMatrix::I());
fGpu->setViewMatrix(GrMatrix::I());
fGpu->setAlpha(0xff);
fGpu->setBlendFunc(GrDrawTarget::kOne_BlendCoeff, GrDrawTarget::kZero_BlendCoeff);
@@ -164,16 +162,10 @@ GrTextureEntry* GrContext::createAndLockTexture(GrTextureKey* key, if (arg.succeeded()) {
GrPoint* verts = (GrPoint*) arg.vertices();
verts[0].setIRectFan(0, 0,
- texture->contentWidth(),
- texture->contentHeight(),
+ texture->width(),
+ texture->height(),
2*sizeof(GrPoint));
- GrScalar tw = GrFixedToScalar(GR_Fixed1 *
- clampTexture->contentWidth() /
- clampTexture->allocWidth());
- GrScalar th = GrFixedToScalar(GR_Fixed1 *
- clampTexture->contentHeight() /
- clampTexture->allocHeight());
- verts[1].setRectFan(0, 0, tw, th, 2*sizeof(GrPoint));
+ verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
fGpu->drawNonIndexed(GrDrawTarget::kTriangleFan_PrimitiveType,
0, 4);
entry = fTextureCache->createAndLock(*key, texture);
@@ -377,8 +369,8 @@ void GrContext::drawRect(const GrPaint& paint, GrDrawTarget::AutoViewMatrixRestore avmr;
if (NULL != matrix) {
avmr.set(target);
- target->concatViewMatrix(*matrix);
- target->concatTextureMatrix(0, *matrix);
+ target->preConcatViewMatrix(*matrix);
+ target->preConcatSamplerMatrix(0, *matrix);
}
target->drawNonIndexed(primType, 0, vertCount);
@@ -399,10 +391,10 @@ void GrContext::drawRect(const GrPaint& paint, m.postConcat(*matrix);
}
- target->concatViewMatrix(m);
+ target->preConcatViewMatrix(m);
if (textured) {
- target->concatTextureMatrix(0, m);
+ target->preConcatSamplerMatrix(0, m);
}
target->drawNonIndexed(GrDrawTarget::kTriangleFan_PrimitiveType, 0, 4);
#else
@@ -438,7 +430,7 @@ void GrContext::drawRectToRect(const GrPaint& paint, if (NULL != dstMatrix) {
m.postConcat(*dstMatrix);
}
- target->concatViewMatrix(m);
+ target->preConcatViewMatrix(m);
m.setAll(srcRect.width(), 0, srcRect.fLeft,
0, srcRect.height(), srcRect.fTop,
@@ -446,7 +438,7 @@ void GrContext::drawRectToRect(const GrPaint& paint, if (NULL != srcMatrix) {
m.postConcat(*srcMatrix);
}
- target->concatTextureMatrix(0, m);
+ target->preConcatSamplerMatrix(0, m);
target->setVertexSourceToBuffer(layout, fGpu->getUnitSquareVertexBuffer());
target->drawNonIndexed(GrDrawTarget::kTriangleFan_PrimitiveType, 0, 4);
@@ -921,16 +913,18 @@ void GrContext::writePixels(int left, int top, int width, int height, GrMatrix matrix;
matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
fGpu->setViewMatrix(matrix);
- matrix.setScale(GR_Scalar1 / texture->allocWidth(),
- GR_Scalar1 / texture->allocHeight());
- fGpu->setTextureMatrix(0, matrix);
fGpu->disableState(GrDrawTarget::kClip_StateBit);
fGpu->setAlpha(0xFF);
fGpu->setBlendFunc(GrDrawTarget::kOne_BlendCoeff,
GrDrawTarget::kZero_BlendCoeff);
fGpu->setTexture(0, texture);
- fGpu->setSamplerState(0, GrSamplerState::ClampNoFilter());
+
+ GrSamplerState sampler;
+ sampler.setClampNoFilter();
+ matrix.setScale(GR_Scalar1 / width, GR_Scalar1 / height);
+ sampler.setMatrix(matrix);
+ fGpu->setSamplerState(0, sampler);
GrVertexLayout layout = GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
static const int VCOUNT = 4;
@@ -946,7 +940,6 @@ void GrContext::writePixels(int left, int top, int width, int height, void GrContext::SetPaint(const GrPaint& paint, GrDrawTarget* target) {
target->setTexture(0, paint.getTexture());
- target->setTextureMatrix(0, paint.fTextureMatrix);
target->setSamplerState(0, paint.fSampler);
target->setColor(paint.fColor);
@@ -1019,7 +1012,7 @@ void GrContext::setMatrix(const GrMatrix& m) { }
void GrContext::concatMatrix(const GrMatrix& m) const {
- fGpu->concatViewMatrix(m);
+ fGpu->preConcatViewMatrix(m);
}
static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
diff --git a/gpu/src/GrDrawTarget.cpp b/gpu/src/GrDrawTarget.cpp index 06e5ab8b3f..63e59c4831 100644 --- a/gpu/src/GrDrawTarget.cpp +++ b/gpu/src/GrDrawTarget.cpp @@ -331,7 +331,7 @@ void GrDrawTarget::setViewMatrix(const GrMatrix& m) { fCurrDrawState.fViewMatrix = m; } -void GrDrawTarget::concatViewMatrix(const GrMatrix& matrix) { +void GrDrawTarget::preConcatViewMatrix(const GrMatrix& matrix) { fCurrDrawState.fViewMatrix.preConcat(matrix); } @@ -358,21 +358,6 @@ void GrDrawTarget::setSamplerState(int stage, const GrSamplerState& state) { fCurrDrawState.fSamplerStates[stage] = state; } -void GrDrawTarget::setTextureMatrix(int stage, const GrMatrix& m) { - GrAssert(stage >= 0 && stage < kNumStages); - fCurrDrawState.fTextureMatrices[stage] = m; -} - -void GrDrawTarget::concatTextureMatrix(int stage, const GrMatrix& m) { - GrAssert(stage >= 0 && stage < kNumStages); - fCurrDrawState.fTextureMatrices[stage].preConcat(m); -} - -const GrMatrix& GrDrawTarget::getTextureMatrix(int stage) const { - GrAssert(stage >= 0 && stage < kNumStages); - return fCurrDrawState.fTextureMatrices[stage]; -} - void GrDrawTarget::setStencilPass(StencilPass pass) { fCurrDrawState.fStencilPass = pass; } diff --git a/gpu/src/GrGLTexture.cpp b/gpu/src/GrGLTexture.cpp index 9460086fe2..110535f3ea 100644 --- a/gpu/src/GrGLTexture.cpp +++ b/gpu/src/GrGLTexture.cpp @@ -76,20 +76,25 @@ const GLenum GrGLTexture::gWrapMode2GLWrap[] = { GrGLTexture::GrGLTexture(const GLTextureDesc& textureDesc, const GLRenderTargetIDs& rtIDs, const TexParams& initialTexParams, - GrGpuGL* gl) : - INHERITED(textureDesc.fContentWidth, - textureDesc.fContentHeight, - textureDesc.fAllocWidth, - textureDesc.fAllocHeight, - textureDesc.fFormat), - fTexParams(initialTexParams), - fTextureID(textureDesc.fTextureID), - fUploadFormat(textureDesc.fUploadFormat), - fUploadByteCount(textureDesc.fUploadByteCount), - fUploadType(textureDesc.fUploadType), - fOrientation(textureDesc.fOrientation), - fRenderTarget(NULL), - fGpuGL(gl) { + GrGpuGL* gl) + : INHERITED(textureDesc.fContentWidth, + textureDesc.fContentHeight, + textureDesc.fFormat) { + + fTexParams = initialTexParams; + fTextureID = textureDesc.fTextureID; + fUploadFormat = textureDesc.fUploadFormat; + fUploadByteCount = textureDesc.fUploadByteCount; + fUploadType = textureDesc.fUploadType; + fOrientation = textureDesc.fOrientation; + fAllocWidth = textureDesc.fAllocWidth; + fAllocHeight = textureDesc.fAllocHeight; + fScaleX = GrIntToScalar(textureDesc.fContentWidth) / + textureDesc.fAllocWidth; + fScaleY = GrIntToScalar(textureDesc.fContentHeight) / + textureDesc.fAllocHeight; + fRenderTarget = NULL; + fGpuGL = gl; GrAssert(0 != textureDesc.fTextureID); @@ -135,7 +140,7 @@ void GrGLTexture::removeRenderTarget() { // must do this notify before the delete fGpuGL->notifyTextureRemoveRenderTarget(this); delete fRenderTarget; - fRenderTarget = NULL; + fRenderTarget = NULL; } } diff --git a/gpu/src/GrGpu.cpp b/gpu/src/GrGpu.cpp index 5425b58954..57d313ce15 100644 --- a/gpu/src/GrGpu.cpp +++ b/gpu/src/GrGpu.cpp @@ -427,6 +427,7 @@ const GrSamplerState GrSamplerState::gClampNoFilter( GrSamplerState::kClamp_WrapMode, GrSamplerState::kClamp_WrapMode, GrSamplerState::kNormal_SampleMode, + GrMatrix::I(), false); diff --git a/gpu/src/GrGpuFactory.cpp b/gpu/src/GrGpuFactory.cpp index 5e61609948..2298776f48 100644 --- a/gpu/src/GrGpuFactory.cpp +++ b/gpu/src/GrGpuFactory.cpp @@ -24,18 +24,13 @@ #include "GrGLConfig.h" -#define GR_USE_GLSHADERS2 1 #if GR_SUPPORT_GLES1 || GR_SUPPORT_GLDESKTOP #include "GrGpuGLFixed.h" #endif #if GR_SUPPORT_GLES2 || GR_SUPPORT_GLDESKTOP - #if GR_USE_GLSHADERS2 - #include "GrGpuGLShaders2.h" - #else - #include "GrGpuGLShaders.h" - #endif + #include "GrGpuGLShaders2.h" #endif #include "GrGpu.h" @@ -47,11 +42,7 @@ GrGpu* GrGpu::Create(Engine engine, Platform3DContext context3D) { case kOpenGL_Shaders_Engine: GrAssert(NULL == context3D); #if GR_SUPPORT_GLES2 || GR_SUPPORT_GLDESKTOP - #if GR_USE_GLSHADERS2 gpu = new GrGpuGLShaders2; - #else - gpu = new GrGpuGLShaders; - #endif #endif break; case kOpenGL_Fixed_Engine: diff --git a/gpu/src/GrGpuGL.cpp b/gpu/src/GrGpuGL.cpp index 8d00af9ce2..ba133721ba 100644 --- a/gpu/src/GrGpuGL.cpp +++ b/gpu/src/GrGpuGL.cpp @@ -49,6 +49,58 @@ static const GLenum gXfermodeCoeff2Blend[] = { /////////////////////////////////////////////////////////////////////////////// +void GrGpuGL::AdjustTextureMatrix(const GrGLTexture* texture, + GrSamplerState::SampleMode mode, + GrMatrix* matrix) { + GrAssert(NULL != texture); + GrAssert(NULL != matrix); + if (GR_Scalar1 != texture->contentScaleX() || + GR_Scalar1 != texture->contentScaleY()) { + if (GrSamplerState::kRadial_SampleMode == mode) { + GrMatrix scale; + scale.setScale(texture->contentScaleX(), texture->contentScaleX()); + matrix->postConcat(scale); + } else if (GrSamplerState::kNormal_SampleMode == mode) { + GrMatrix scale; + scale.setScale(texture->contentScaleX(), texture->contentScaleY()); + matrix->postConcat(scale); + } else { + GrPrintf("We haven't handled NPOT adjustment for other sample modes!"); + } + } + GrGLTexture::Orientation orientation = texture->orientation(); + if (GrGLTexture::kBottomUp_Orientation == orientation) { + GrMatrix invY; + invY.setAll(GR_Scalar1, 0, 0, + 0, -GR_Scalar1, GR_Scalar1, + 0, 0, GrMatrix::I()[8]); + matrix->postConcat(invY); + } else { + GrAssert(GrGLTexture::kTopDown_Orientation == orientation); + } +} + +bool GrGpuGL::TextureMatrixIsIdentity(const GrGLTexture* texture, + const GrSamplerState& sampler) { + GrAssert(NULL != texture); + if (!sampler.getMatrix().isIdentity()) { + return false; + } + if (GR_Scalar1 != texture->contentScaleX() || + GR_Scalar1 != texture->contentScaleY()) { + return false; + } + GrGLTexture::Orientation orientation = texture->orientation(); + if (GrGLTexture::kBottomUp_Orientation == orientation) { + return false; + } else { + GrAssert(GrGLTexture::kTopDown_Orientation == orientation); + } + return true; +} + +/////////////////////////////////////////////////////////////////////////////// + static bool gPrintStartupSpew; @@ -102,8 +154,7 @@ GrGpuGL::GrGpuGL() { resetContextHelper(); - fHWDrawState.fRenderTarget = NULL; - fRenderTargetChanged = true; + resetDirtyFlags(); GLint maxTextureUnits; // check FS and fixed-function texture unit limits @@ -265,6 +316,7 @@ GrGpuGL::GrGpuGL() { fNPOTTextureTileSupport = false; } #endif + //////////////////////////////////////////////////////////////////////////// // Experiments to determine limitations that can't be queried. TODO: Make // these a preprocess that generate some compile time constants. @@ -395,14 +447,15 @@ void GrGpuGL::resetContextHelper() { fHWDrawState.fDstBlend = (BlendCoeff)-1; fHWDrawState.fColor = GrColor_ILLEGAL; - fHWDrawState.fViewMatrix.setScale(GR_ScalarMax, GR_ScalarMax); // illegal + fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix(); for (int s = 0; s < kNumStages; ++s) { fHWDrawState.fTextures[s] = NULL; fHWDrawState.fSamplerStates[s].setRadial2Params(-GR_ScalarMax, -GR_ScalarMax, true); - fHWDrawState.fTextureMatrices[s].setScale(GR_ScalarMax, GR_ScalarMax); + + fHWDrawState.fSamplerStates[s].setMatrix(GrMatrix::InvalidMatrix()); } GR_GL(Scissor(0,0,0,0)); @@ -1017,7 +1070,7 @@ void GrGpuGL::eraseColor(GrColor color) { GrColorUnpackB(color)/255.f, GrColorUnpackA(color)/255.f)); GR_GL(Clear(GL_COLOR_BUFFER_BIT)); - fWriteMaskChanged = true; + fDirtyFlags.fWriteMaskChanged = true; } void GrGpuGL::eraseStencil(uint32_t value, uint32_t mask) { @@ -1032,7 +1085,7 @@ void GrGpuGL::eraseStencil(uint32_t value, uint32_t mask) { GR_GL(StencilMask(mask)); GR_GL(ClearStencil(value)); GR_GL(Clear(GL_STENCIL_BUFFER_BIT)); - fWriteMaskChanged = true; + fDirtyFlags.fWriteMaskChanged = true; } void GrGpuGL::eraseStencilClip() { @@ -1107,7 +1160,7 @@ void GrGpuGL::flushRenderTarget() { #endif fHWDrawState.fRenderTarget = fCurrDrawState.fRenderTarget; const GrIRect& vp = rt->viewport(); - fRenderTargetChanged = true; + fDirtyFlags.fRenderTargetChanged = true; if (fHWBounds.fViewportRect != vp) { GR_GL(Viewport(vp.fLeft, vp.fBottom, @@ -1181,10 +1234,10 @@ void GrGpuGL::resolveTextureRenderTarget(GrGLTexture* texture) { fHWDrawState.fRenderTarget = NULL; GLint left = 0; - GLint right = texture->contentWidth(); + GLint right = texture->width(); // we will have rendered to the top of the FBO. GLint top = texture->allocHeight(); - GLint bottom = texture->allocHeight() - texture->contentHeight(); + GLint bottom = texture->allocHeight() - texture->height(); if (kApple_MSFBO == fMSFBOType) { GR_GL(Enable(GL_SCISSOR_TEST)); GR_GL(Scissor(left, bottom, right-left, top-bottom)); @@ -1208,7 +1261,7 @@ void GrGpuGL::flushStencil() { bool stencilClip = fClipState.fClipInStencil && (kClip_StateBit & fCurrDrawState.fFlagBits); bool stencilChange = - fWriteMaskChanged || + fDirtyFlags.fWriteMaskChanged || fHWStencilClip != stencilClip || fHWDrawState.fStencilPass != fCurrDrawState.fStencilPass || (kNone_StencilPass != fCurrDrawState.fStencilPass && @@ -1399,7 +1452,6 @@ void GrGpuGL::flushStencil() { } fHWDrawState.fStencilPass = fCurrDrawState.fStencilPass; fHWDrawState.fReverseFill = fCurrDrawState.fReverseFill; - fWriteMaskChanged = false; fHWStencilClip = stencilClip; } } @@ -1468,6 +1520,10 @@ bool GrGpuGL::flushGLStateCommon(PrimitiveType type) { newTexParams.fWrapT)); } nextTexture->setTexParams(newTexParams); + + // The texture matrix has to compensate for texture width/height + // and NPOT-embedded-in-POT + fDirtyFlags.fTextureChangedMask |= (1 << s); } else { GrAssert(!"Rendering with texture vert flag set but no texture"); return false; @@ -1489,7 +1545,7 @@ bool GrGpuGL::flushGLStateCommon(PrimitiveType type) { #if GR_SUPPORT_GLDESKTOP // ES doesn't support toggling GL_MULTISAMPLE and doesn't have // smooth lines. - if (fRenderTargetChanged || + if (fDirtyFlags.fRenderTargetChanged || (fCurrDrawState.fFlagBits & kAntialias_StateBit) != (fHWDrawState.fFlagBits & kAntialias_StateBit)) { GLint msaa = 0; @@ -1712,6 +1768,10 @@ bool GrGpuGL::fboInternalFormat(GrTexture::PixelConfig config, GLenum* format) { } } +void GrGpuGL::resetDirtyFlags() { + Gr_bzero(&fDirtyFlags, sizeof(fDirtyFlags)); +} + void GrGpuGL::setBuffers(bool indexed, int* extraVertexOffset, int* extraIndexOffset) { diff --git a/gpu/src/GrGpuGL.h b/gpu/src/GrGpuGL.h index d89b8d54d7..a2ccc12c56 100644 --- a/gpu/src/GrGpuGL.h +++ b/gpu/src/GrGpuGL.h @@ -71,6 +71,26 @@ protected: DrState fHWDrawState; bool fHWStencilClip; + // As flush of GL state proceeds it updates fHDrawState + // to reflect the new state. Later parts of the state flush + // may perform cascaded changes but cannot refer to fHWDrawState. + // These code paths can refer to the dirty flags. Subclass should + // call resetDirtyFlags after its flush is complete + struct { + bool fRenderTargetChanged : 1; + bool fWriteMaskChanged : 1; + int fTextureChangedMask; + } fDirtyFlags; + GR_STATIC_ASSERT(8 * sizeof(int) >= kNumStages); + + // clears the dirty flags + void resetDirtyFlags(); + + // last scissor / viewport scissor state seen by the GL. + BoundsState fHWBounds; + + GrGLExts fExts; + // GrGpu overrides virtual void drawIndexedHelper(PrimitiveType type, uint32_t startVertex, @@ -103,18 +123,16 @@ protected: // line width bool flushGLStateCommon(PrimitiveType type); - // set when this class changes the rendertarget. - // Subclass should notice at flush time, take appropriate action, - // and set false. - bool fRenderTargetChanged; - - // set by eraseColor or eraseStencil. Picked up in in flushStencil. - bool fWriteMaskChanged; - - // last scissor / viewport scissor state seen by the GL. - BoundsState fHWBounds; + // adjusts texture matrix to account for orientation, size, and npotness + static void AdjustTextureMatrix(const GrGLTexture* texture, + GrSamplerState::SampleMode mode, + GrMatrix* matrix); - GrGLExts fExts; + // subclass may try to take advantage of identity tex matrices. + // This helper determines if matrix will be identity after all + // adjustments are applied. + static bool TextureMatrixIsIdentity(const GrGLTexture* texture, + const GrSamplerState& sampler); private: void resetContextHelper(); diff --git a/gpu/src/GrGpuGLFixed.cpp b/gpu/src/GrGpuGLFixed.cpp index 76494de34c..efb09908a0 100644 --- a/gpu/src/GrGpuGLFixed.cpp +++ b/gpu/src/GrGpuGLFixed.cpp @@ -25,7 +25,7 @@ #define SKIP_CACHE_CHECK true struct GrGpuMatrix { - GrScalar fMat[16]; + GLfloat fMat[16]; void reset() { Gr_bzero(fMat, sizeof(fMat)); @@ -34,19 +34,19 @@ struct GrGpuMatrix { void set(const GrMatrix& m) { Gr_bzero(fMat, sizeof(fMat)); - fMat[0] = m[GrMatrix::kScaleX]; - fMat[4] = m[GrMatrix::kSkewX]; - fMat[12] = m[GrMatrix::kTransX]; + fMat[0] = GrScalarToFloat(m[GrMatrix::kScaleX]); + fMat[4] = GrScalarToFloat(m[GrMatrix::kSkewX]); + fMat[12] = GrScalarToFloat(m[GrMatrix::kTransX]); - fMat[1] = m[GrMatrix::kSkewY]; - fMat[5] = m[GrMatrix::kScaleY]; - fMat[13] = m[GrMatrix::kTransY]; + fMat[1] = GrScalarToFloat(m[GrMatrix::kSkewY]); + fMat[5] = GrScalarToFloat(m[GrMatrix::kScaleY]); + fMat[13] = GrScalarToFloat(m[GrMatrix::kTransY]); - fMat[3] = m[GrMatrix::kPersp0]; - fMat[7] = m[GrMatrix::kPersp1]; - fMat[15] = m[GrMatrix::kPersp2]; + fMat[3] = GrScalarToFloat(m[GrMatrix::kPersp0]); + fMat[7] = GrScalarToFloat(m[GrMatrix::kPersp1]); + fMat[15] = GrScalarToFloat(m[GrMatrix::kPersp2]); - fMat[10] = GR_Scalar1; // z-scale + fMat[10] = 1.f; // z-scale } }; @@ -106,7 +106,6 @@ void GrGpuGLFixed::resetContextHelper() { GrGLClearErr(); fTextVerts = false; - fHWTextureOrientation = (GrGLTexture::Orientation)-1; // illegal fBaseVertex = 0xffffffff; } @@ -146,9 +145,8 @@ bool GrGpuGLFixed::flushGraphicsState(PrimitiveType type) { return false; } - if (fRenderTargetChanged) { + if (fDirtyFlags.fRenderTargetChanged) { flushProjectionMatrix(); - fRenderTargetChanged = false; } for (int s = 0; s < kNumStages; ++s) { @@ -205,28 +203,20 @@ bool GrGpuGLFixed::flushGraphicsState(PrimitiveType type) { fHWRGBOperand0[s] = nextRGBOperand0; } - if (fHWTextureOrientation != texture->orientation() || - fHWDrawState.fTextureMatrices[s] != - fCurrDrawState.fTextureMatrices[s]) { + if (((1 << s) & fDirtyFlags.fTextureChangedMask) || + (fHWDrawState.fSamplerStates[s].getMatrix() != + getSamplerMatrix(s))) { + + GrMatrix texMat = getSamplerMatrix(s); + AdjustTextureMatrix(texture, + GrSamplerState::kNormal_SampleMode, + &texMat); GrGpuMatrix glm; - if (GrGLTexture::kBottomUp_Orientation == - texture->orientation()) { - GrMatrix m( - GR_Scalar1, 0, 0, - 0, -GR_Scalar1, GR_Scalar1, - 0, 0, GrMatrix::I()[8] - ); - m.preConcat(fCurrDrawState.fTextureMatrices[s]); - glm.set(m); - } else { - glm.set(fCurrDrawState.fTextureMatrices[s]); - } + glm.set(texMat); setTextureUnit(s); GR_GL(MatrixMode(GL_TEXTURE)); GR_GL(LoadMatrixf(glm.fMat)); - fHWDrawState.fTextureMatrices[s] = - fCurrDrawState.fTextureMatrices[s]; - fHWTextureOrientation = texture->orientation(); + recordHWSamplerMatrix(s, getSamplerMatrix(s)); } } else { GrAssert(!"Rendering with texture vert flag set but no bound texture"); @@ -243,6 +233,7 @@ bool GrGpuGLFixed::flushGraphicsState(PrimitiveType type) { fHWDrawState.fViewMatrix = fCurrDrawState.fViewMatrix; } + resetDirtyFlags(); return true; } @@ -263,7 +254,7 @@ void GrGpuGLFixed::setupGeometry(int* startVertex, oldTexCoordOffsets, &oldColorOffset); - bool indexed = NULL == startIndex; + bool indexed = NULL != startIndex; int extraVertexOffset; int extraIndexOffset; diff --git a/gpu/src/GrGpuGLFixed.h b/gpu/src/GrGpuGLFixed.h index abb11aa4b0..e5d472dd2e 100644 --- a/gpu/src/GrGpuGLFixed.h +++ b/gpu/src/GrGpuGLFixed.h @@ -39,6 +39,14 @@ protected: private: void resetContextHelper(); + // Helpers to make code more readable + const GrMatrix& getHWSamplerMatrix(int stage) const { + return fHWDrawState.fSamplerStates[stage].getMatrix(); + } + const void recordHWSamplerMatrix(int stage, const GrMatrix& matrix) { + fHWDrawState.fSamplerStates[stage].setMatrix(matrix); + } + // when the texture is GL_RGBA we set the GL_COMBINE texture // environment rgb operand 0 to be GL_COLOR to modulate each incoming // R,G, & B by the texture's R, G, & B. When the texture is alpha-only we @@ -60,8 +68,6 @@ private: // glVertexPointer/glTexCoordPointer/etc int fBaseVertex; - GrGLTexture::Orientation fHWTextureOrientation; - typedef GrGpuGL INHERITED; }; diff --git a/gpu/src/GrGpuGLShaders.cpp b/gpu/src/GrGpuGLShaders.cpp deleted file mode 100644 index cf41b1d26a..0000000000 --- a/gpu/src/GrGpuGLShaders.cpp +++ /dev/null @@ -1,933 +0,0 @@ -/* - Copyright 2010 Google Inc. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - */ - - -#include "GrGLConfig.h" - -#if GR_SUPPORT_GLES2 || GR_SUPPORT_GLDESKTOP - -#include "GrGpuGLShaders.h" -#include "GrGpuVertex.h" -#include "GrMemory.h" - -#define ATTRIBUTE_MATRIX 0 - -#define ATTRIBUTE_TEXT_COLOR 1 - -#if ATTRIBUTE_MATRIX - #define DECL_MATRIX(name) "attribute mat3 " #name ";\n" -#else - #define DECL_MATRIX(name) "uniform mat3 " #name ";\n" -#endif - -#define SKIP_CACHE_CHECK true - -#if GR_SUPPORT_GLES2 - #define GR_PRECISION "mediump" - #define GR_SHADER_PRECISION "precision mediump float;\n" -#else - #define GR_PRECISION "" - #define GR_SHADER_PRECISION "" -#endif - -static const char* gvshad[] = { - // 0: kTextureVertCoords_Program, kTextureVertCoordsProj_Program, - // kRadialTextureVertCoords_Program, kSweepTextureVertCoords_Program - "attribute vec2 aPosition;\n" - "attribute vec4 aColor;\n" - "varying vec3 vTexture;\n" - "varying vec4 vColor;\n" - DECL_MATRIX(viewM) - DECL_MATRIX(texM) - "void main() {\n" - " vec3 pos3 = viewM*vec3(aPosition,1);\n" - " gl_Position = vec4(pos3.xy,0,pos3.z);\n" - " gl_PointSize = 1.0;\n" - " vTexture = texM * vec3(aPosition,1);\n" - " vColor = aColor;\n" - "}\n", - - // 1: kTextureTexCoords_Program, kTextureTexCoordsProj_Program, - // kRadialTextureTexCoords_Program, kSweepTextureTexCoords_Program - "attribute vec2 aPosition;\n" - "attribute vec2 aTexture;\n" - "attribute vec4 aColor;\n" - "varying vec3 vTexture;\n" - "varying vec4 vColor;\n" - DECL_MATRIX(viewM) - DECL_MATRIX(texM) - "void main() {\n" - " vec3 pos3 = viewM*vec3(aPosition,1);\n" - " gl_Position = vec4(pos3.xy,0,pos3.z);\n" - " gl_PointSize = 1.0;\n" - " vTexture = texM * vec3(aTexture,1);\n" - " vColor = aColor;\n" - "}\n", - - // 2: kText_Program - "attribute vec2 aPosition;\n" - "attribute vec2 aTexture;\n" - "varying vec2 vTexture;\n" - DECL_MATRIX(viewM) -#if ATTRIBUTE_TEXT_COLOR - "varying vec4 vColor;\n" - "attribute vec4 aColor;\n" -#endif - "void main() {\n" - " vec3 pos3 = viewM*vec3(aPosition,1);\n" - " gl_Position = vec4(pos3.xy,0,pos3.z);\n" - " vTexture = aTexture;\n" -#if ATTRIBUTE_TEXT_COLOR - " vColor = aColor;\n" -#endif - "}\n", - - // 3: kNoTexture_Program - "attribute vec2 aPosition;\n" - "attribute vec4 aColor;\n" - "varying vec4 vColor;\n" - DECL_MATRIX(viewM) - "void main() {\n" - " vec3 pos3 = viewM*vec3(aPosition,1);\n" - " gl_Position = vec4(pos3.xy,0,pos3.z);\n" - " gl_PointSize = 1.0;\n" - " vColor = aColor;\n" - "}\n", - - // 4: kTextureVertCoordsNoColor_Program - "attribute vec2 aPosition;\n" - "attribute vec4 aColor;\n" - "varying vec3 vTexture;\n" - DECL_MATRIX(viewM) - DECL_MATRIX(texM) - "void main() {\n" - " vec3 pos3 = viewM*vec3(aPosition,1);\n" - " gl_Position = vec4(pos3.xy,0,pos3.z);\n" - " vTexture = texM * vec3(aPosition,1);\n" - "}\n", - - // 5: kTextureTexCoordsNoColor_Program - "attribute vec2 aPosition;\n" - "attribute vec2 aTexture;\n" - "varying vec3 vTexture;\n" - DECL_MATRIX(viewM) - DECL_MATRIX(texM) - "void main() {\n" - " vec3 pos3 = viewM*vec3(aPosition,1);\n" - " gl_Position = vec4(pos3.xy,0,pos3.z);\n" - " gl_PointSize = 1.0;\n" - " vTexture = texM * vec3(aTexture,1);\n" - "}\n", - - // 6: kTwoPointRadialTextureVertCoords_Program - "uniform " GR_PRECISION " float uParams[6];\n" - // 0 is t^2 term of quadratic - // 1 is one-half the inverse of above - // 2 is x offset of the second circle (post tex-matrix) - // 3 is the radius of the first circle (post tex-matrix) - // 4 is the first circle radius squared - // 5 is 1 to use + in the quadratic eq or -1 to use - - DECL_MATRIX(viewM) - DECL_MATRIX(texM) - "attribute vec2 aPosition;\n" - "attribute vec4 aColor;\n" - "varying vec4 vColor;\n" - "varying float vB;\n" // t coeffecient of quadratic. - "varying vec2 t;\n" // coordinates in canonical space - "void main() {\n" - " vec3 pos3 = viewM*vec3(aPosition,1);\n" - " gl_Position = vec4(pos3.xy,0,pos3.z);\n" - " t = vec2(texM * vec3(aPosition,1));\n" - " vColor = aColor;\n" - " vB = 2.0 * (uParams[2] * t.x - uParams[3]);\n" - "}\n", - - // 6: kTwoPointRadialTextureVertCoords_Program - "uniform " GR_PRECISION " float uParams[6];\n" - DECL_MATRIX(viewM) - DECL_MATRIX(texM) - "attribute vec2 aPosition;\n" - "attribute vec2 aTexture;\n" - "attribute vec4 aColor;\n" - "varying vec4 vColor;\n" - "varying float vB;\n" // t coeffecient of quadratic. - "varying vec2 t;\n" // coordinates in canonical space - "void main() {\n" - " vec3 pos3 = viewM*vec3(aPosition,1);\n" - " gl_Position = vec4(pos3.xy,0,pos3.z);\n" - " t = vec2(texM * vec3(aTexture,1));\n" - " vColor = aColor;\n" - " vB = 2.0 * (uParams[2] * t.x - uParams[3]);\n" - "}\n", -}; - -static const char* gfshad[] = { - // 0: kTextureVertCoords_Program, kTextureTexCoords_Program - GR_SHADER_PRECISION - "varying vec3 vTexture;\n" - "varying vec4 vColor;\n" - "uniform sampler2D sTexture;\n" - "void main() {\n" - " gl_FragColor = vColor * texture2D(sTexture, vTexture.xy);\n" - "}\n", - - // 1: kTextureVertCoordsProj_Program, kTextureTexCoordsProj_Program - GR_SHADER_PRECISION - "varying vec3 vTexture;\n" - "varying vec4 vColor;\n" - "uniform sampler2D sTexture;\n" - "void main() {\n" - // On Brian's PC laptop with Intel Gfx texture2DProj seems to be broken - // but it works everywhere else tested. - " gl_FragColor = vColor * texture2DProj(sTexture, vTexture);\n" - - "}\n", - - // 2: kText_Program - GR_SHADER_PRECISION - "varying vec2 vTexture;\n" -#if ATTRIBUTE_TEXT_COLOR - "varying vec4 vColor;\n" -#else - "uniform vec4 uColor;\n" -#endif - "uniform sampler2D sTexture;\n" - "void main() {\n" -#if ATTRIBUTE_TEXT_COLOR - " gl_FragColor = vColor * texture2D(sTexture, vTexture).a;\n" -#else - " gl_FragColor = uColor * texture2D(sTexture, vTexture).a;\n" -#endif - "}\n", - - // 3: kNoTexture_Program - GR_SHADER_PRECISION - "varying vec4 vColor;\n" - "void main() {\n" - " gl_FragColor = vColor;\n" - "}\n", - - // 4: kTextureVertCoordsNoColor_Program - GR_SHADER_PRECISION - "varying vec3 vTexture;\n" - "uniform sampler2D sTexture;\n" - "void main() {\n" - " gl_FragColor = texture2D(sTexture, vTexture.xy);\n" - "}\n", - - // 5: kRadialTextureVertCoords_Program, kRadialTextureTexCoords_Program - GR_SHADER_PRECISION - "varying vec3 vTexture;\n" - "varying vec4 vColor;\n" - "uniform sampler2D sTexture;\n" - "void main() {\n" - " gl_FragColor = vColor * texture2D(sTexture, vec2(length(vTexture.xy), 0.5));\n" - "}\n", - - // 6: kSweepTextureVertCoords_Program, kSweepTextureTexCoords_Program - GR_SHADER_PRECISION - "varying vec3 vTexture;\n" - "varying vec4 vColor;\n" - "uniform sampler2D sTexture;\n" - "void main() {\n" - " vec2 t = vec2(atan(-vTexture.y, -vTexture.x)*0.1591549430918 + 0.5,\n" - " 0.5);\n" - " gl_FragColor = vColor * texture2D(sTexture, t);\n" - "}\n", - - // 7: kTwoPointRadialTextureVertCoords_Program, kTwoPointRadialTextureTexCoords_Program - GR_SHADER_PRECISION - "varying vec4 vColor;\n" - "varying float vB;\n" // t coeffecient of quadratic. - "varying vec2 t;\n" // coordinates in canonical radial gradient space - "uniform sampler2D sTexture;\n" - "uniform float uParams[6];\n" - "void main() {\n" - "float c = t.x*t.x + t.y*t.y - uParams[4];\n" - "float ac4 = uParams[0] * c * 4.0;\n" - "float root = sqrt(abs(vB * vB - ac4));\n" - "float t = (-vB + uParams[5] * root) * uParams[1];\n" - "gl_FragColor = vColor * texture2D(sTexture, vec2(t,0.5))\n;" - "}\n", -}; - -// determines which frag/vert shaders are used for each program in Programs enum - -static const struct { - int fVShaderIdx; - int fFShaderIdx; - bool fHasTexMatrix; - bool fHasTexCoords; - bool fTwoPointRadial; - GrGpuGLShaders::ColorType fColorType; -} gProgramLoadData[] = { - // kTextureVertCoords_Program - {0, 0, true, false, false, GrGpuGLShaders::kAttrib_ColorType }, - // kTextureVertCoordsProj_Program - {0, 1, true, false, false, GrGpuGLShaders::kAttrib_ColorType }, - // kTextureTexCoords_Program - {1, 0, true, true, false, GrGpuGLShaders::kAttrib_ColorType }, - // kTextureTexCoordsProj_Program - {1, 1, true, true, false, GrGpuGLShaders::kAttrib_ColorType }, - // kTextureVertCoordsNoColor_Program - {4, 4, true, false, false, GrGpuGLShaders::kNone_ColorType }, - // kTextureTexCoordsNoColor_Program - {5, 4, true, false, false, GrGpuGLShaders::kNone_ColorType }, - // kText_Program -#if ATTRIBUTE_TEXT_COLOR - {2, 2, false, true, false, GrGpuGLShaders::kAttrib_ColorType }, -#else - {2, 2, false, true, false, GrGpuGLShaders::kUniform_ColorType }, -#endif - // kRadialTextureVertCoords_Program - {0, 5, true, false, false, GrGpuGLShaders::kAttrib_ColorType }, - // kRadialTextureTexCoords_Program - {1, 5, true, true, false, GrGpuGLShaders::kAttrib_ColorType }, - // kSweepTextureVertCoords_Program - {0, 6, true, false, false, GrGpuGLShaders::kAttrib_ColorType }, - // kSweepTextureTexCoords_Program - {1, 6, true, true, false, GrGpuGLShaders::kAttrib_ColorType }, - // kTwoPointRadialTextureVertCoords_Program - {6, 7, true, false, true, GrGpuGLShaders::kAttrib_ColorType }, - // kTwoPointRadialTextureTexCoords_Program - {7, 7, true, true, true, GrGpuGLShaders::kAttrib_ColorType }, - // kNoTexture_Program - {3, 3, false, false, false, GrGpuGLShaders::kAttrib_ColorType }, -}; - -#define GR_GL_POS_ATTR_LOCATION 0 -#define GR_GL_TEX_ATTR_LOCATION 1 -#define GR_GL_COL_ATTR_LOCATION 2 -#if ATTRIBUTE_MATRIX - #define GR_GL_MAT_ATTR_LOCATION 3 - #define GR_GL_TEXMAT_ATTR_LOCATION 6 -#endif - -GLuint GrGpuGLShaders::loadShader(GLenum type, const char* src) { - GLuint shader = GR_GL(CreateShader(type)); - if (0 == shader) { - return 0; - } - - GR_GL(ShaderSource(shader, 1, &src, NULL)); - GR_GL(CompileShader(shader)); - - GLint compiled = GR_GL_INIT_ZERO; - GR_GL(GetShaderiv(shader, GL_COMPILE_STATUS, &compiled)); - - if (!compiled) { - GLint infoLen = GR_GL_INIT_ZERO; - GR_GL(GetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen)); - GrAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger - if (infoLen > 0) { - GR_GL(GetShaderInfoLog(shader, infoLen+1, NULL, (char*)log.get())); - GrPrintf((char*)log.get()); - } - GrAssert(!"Shader compilation failed!"); - GR_GL(DeleteShader(shader)); - return 0; - } - return shader; -} - -bool GrGpuGLShaders::createProgram(GLuint vshader, GLuint fshader, - bool hasTexMatrix, - bool hasTexCoords, - GrGpuGLShaders::ColorType colorType, - bool twoPointRadial, - ProgramData* program) { - program->fProgramID = GR_GL(CreateProgram()); - program->fVShaderID = vshader; - program->fFShaderID = fshader; - - GrAssert(0 != program->fProgramID); - - GR_GL(AttachShader(program->fProgramID, vshader)); - GR_GL(AttachShader(program->fProgramID, fshader)); - - GR_GL(BindAttribLocation(program->fProgramID, - GR_GL_POS_ATTR_LOCATION, - "aPosition")); - if (hasTexCoords) { - GR_GL(BindAttribLocation(program->fProgramID, - GR_GL_TEX_ATTR_LOCATION, - "aTexture")); - } -#if ATTRIBUTE_MATRIX - if (hasTexMatrix) { - GR_GL(BindAttribLocation(program->fProgramID, - GR_GL_TEXMAT_ATTR_LOCATION, - "texM")); - // set to something arbitrary to signal to flush that program - // uses the texture matrix. - program->fTexMatrixLocation = 1000; - } -#endif - if (colorType == kAttrib_ColorType) { - GR_GL(BindAttribLocation(program->fProgramID, - GR_GL_COL_ATTR_LOCATION, - "aColor")); - } -#if ATTRIBUTE_MATRIX - GR_GL(BindAttribLocation(program->fProgramID, - GR_GL_MAT_ATTR_LOCATION, - "viewM")); -#endif - - GR_GL(LinkProgram(program->fProgramID)); - - GLint linked = GR_GL_INIT_ZERO; - GR_GL(GetProgramiv(program->fProgramID, GL_LINK_STATUS, &linked)); - if (!linked) { - GLint infoLen = GR_GL_INIT_ZERO; - GR_GL(GetProgramiv(program->fProgramID, GL_INFO_LOG_LENGTH, &infoLen)); - GrAutoMalloc log(sizeof(char)*(infoLen+1)); // outside if for debugger - if (infoLen > 0) { - GR_GL(GetProgramInfoLog(program->fProgramID, - infoLen+1, - NULL, - (char*)log.get())); - GrPrintf((char*)log.get()); - } - GrAssert(!"Error linking program"); - GR_GL(DeleteProgram(program->fProgramID)); - program->fProgramID = 0; - return false; - } - program->fColorType = colorType; - -#if !ATTRIBUTE_MATRIX - program->fMatrixLocation = - GR_GL(GetUniformLocation(program->fProgramID, "viewM")); - program->fTexMatrixLocation = - GR_GL(GetUniformLocation(program->fProgramID, "texM")); -#endif - program->fColorLocation = - GR_GL(GetUniformLocation(program->fProgramID, "uColor")); - program->fTwoPointParamsLocation = - GR_GL(GetUniformLocation(program->fProgramID, "uParams")); - - GLint samplerLocation = - GR_GL(GetUniformLocation(program->fProgramID, "sTexture")); - -#if !ATTRIBUTE_MATRIX - if (-1 == program->fMatrixLocation) { - GrAssert(!"Cannot find matrix uniform in program"); - GR_GL(DeleteProgram(program->fProgramID)); - program->fProgramID = 0; - return false; - } -#endif - - bool hasTexture = hasTexCoords || hasTexMatrix; - - if (-1 == samplerLocation && hasTexture) { - GrAssert(!"Expected to find texture sampler"); - GR_GL(DeleteProgram(program->fProgramID)); - program->fProgramID = 0; - return false; - } else if (-1 != samplerLocation && !hasTexture) { - GrAssert(!"unexpectedly found texture sampler"); - } -#if !ATTRIBUTE_MATRIX && !defined(GR_SKIP_2POINTRADIAL_PROGRAMS) - if (-1 == program->fTexMatrixLocation && hasTexMatrix) { - GrAssert(!"Expected to find texture matrix"); - GR_GL(DeleteProgram(program->fProgramID)); - program->fProgramID = 0; - return false; - } else if (-1 != program->fTexMatrixLocation && !hasTexMatrix) { - GrAssert(!"unexpectedly found texture matrix"); - } -#endif - - if (-1 == program->fColorLocation && - (kUniform_ColorType == colorType)) { - GR_GL(DeleteProgram(program->fProgramID)); - program->fProgramID = 0; - return false; - } else if (-1 != program->fColorLocation && - (kUniform_ColorType != colorType)) { - GrAssert(!"Unexpectedly found color uniform"); - } - - if (twoPointRadial) { - if (-1 == program->fTwoPointParamsLocation) { - GrAssert(!"Didn't find expected uniform for 2pt radial gradient"); - GR_GL(DeleteProgram(program->fProgramID)); - program->fProgramID = 0; - return false; - } - } else { - GrAssert(-1 == program->fTwoPointParamsLocation); - } - - GR_GL(UseProgram(program->fProgramID)); - if (-1 != samplerLocation) { - GR_GL(Uniform1i(samplerLocation, 0)); - } - - return true; -} - -GrGpuGLShaders::GrGpuGLShaders() { - - resetContextHelper(); - - GLuint vshadIDs[GR_ARRAY_COUNT(gvshad)]; - for (size_t s = 0; s < GR_ARRAY_COUNT(gvshad); ++s) { - vshadIDs[s] = loadShader(GL_VERTEX_SHADER, gvshad[s]); - } - - GLuint fshadIDs[GR_ARRAY_COUNT(gfshad)]; - for (size_t s = 0; s < GR_ARRAY_COUNT(gfshad); ++s) { - fshadIDs[s] = loadShader(GL_FRAGMENT_SHADER, gfshad[s]); - } - - GR_STATIC_ASSERT(kProgramCount == GR_ARRAY_COUNT(gProgramLoadData)); - for (int p = 0; p < kProgramCount; ++p) { -#ifdef GR_SKIP_2POINTRADIAL_PROGRAMS - if (11 == p || 12 == p) continue; -#endif - GR_DEBUGCODE(bool result = ) - createProgram(vshadIDs[gProgramLoadData[p].fVShaderIdx], - fshadIDs[gProgramLoadData[p].fFShaderIdx], - gProgramLoadData[p].fHasTexMatrix, - gProgramLoadData[p].fHasTexCoords, - gProgramLoadData[p].fColorType, - gProgramLoadData[p].fTwoPointRadial, - &fPrograms[p]); - GR_DEBUGASSERT(result); - - fPrograms[p].fViewMatrix.setScale(GR_ScalarMax, GR_ScalarMax); - for (int s = 0; s < kNumStages; ++s) { - fPrograms[p].fTextureMatrices[s].setScale(GR_ScalarMax, - GR_ScalarMax); // illegal - }; - fPrograms[p].fColor = GrColor_ILLEGAL; - fPrograms[p].fTextureOrientation = (GrGLTexture::Orientation)-1; // illegal - - // these aren't strictly invalid, just really unlikely. - fPrograms[p].fRadial2CenterX1 = GR_ScalarMin; - fPrograms[p].fRadial2Radius0 = GR_ScalarMin; - fPrograms[p].fRadial2PosRoot = true; // arbitrary - } -} - -GrGpuGLShaders::~GrGpuGLShaders() { - // shaders get deleted once for each program that uses them, do we care? - // probably not - for (int i = 0; i < kProgramCount; ++i) { - GR_GL(DeleteProgram(fPrograms[i].fProgramID)); - GR_GL(DeleteShader(fPrograms[i].fVShaderID)); - GR_GL(DeleteShader(fPrograms[i].fFShaderID)); - } -} - -void GrGpuGLShaders::resetContext() { - INHERITED::resetContext(); - resetContextHelper(); -} - -void GrGpuGLShaders::resetContextHelper() { - fHWProgram = (Programs)-1; - fTextureOrientation = (GrGLTexture::Orientation)-1; // illegal - - fHWGeometryState.fVertexLayout = 0; - fHWGeometryState.fVertexOffset = ~0; - GR_GL(DisableVertexAttribArray(GR_GL_COL_ATTR_LOCATION)); - GR_GL(DisableVertexAttribArray(GR_GL_TEX_ATTR_LOCATION)); - GR_GL(EnableVertexAttribArray(GR_GL_POS_ATTR_LOCATION)); -} - - -void GrGpuGLShaders::flushMatrix(GLint location) { - GrAssert(NULL != fCurrDrawState.fRenderTarget); - GrMatrix m ( - GrIntToScalar(2) / fCurrDrawState.fRenderTarget->width(), 0, -GR_Scalar1, - 0,-GrIntToScalar(2) / fCurrDrawState.fRenderTarget->height(), GR_Scalar1, - 0, 0, GrMatrix::I()[8]); - m.setConcat(m, fCurrDrawState.fViewMatrix); - - // ES doesn't allow you to pass true to the transpose param, - // so do our own transpose - GrScalar mt[] = { - m[GrMatrix::kScaleX], - m[GrMatrix::kSkewY], - m[GrMatrix::kPersp0], - m[GrMatrix::kSkewX], - m[GrMatrix::kScaleY], - m[GrMatrix::kPersp1], - m[GrMatrix::kTransX], - m[GrMatrix::kTransY], - m[GrMatrix::kPersp2] - }; -#if ATTRIBUTE_MATRIX - GR_GL(VertexAttrib4fv(GR_GL_MAT_ATTR_LOCATION+0, mt+0)); - GR_GL(VertexAttrib4fv(GR_GL_MAT_ATTR_LOCATION+1, mt+3)); - GR_GL(VertexAttrib4fv(GR_GL_MAT_ATTR_LOCATION+2, mt+6)); -#else - GR_GL(UniformMatrix3fv(location,1,false,mt)); -#endif -} - -void GrGpuGLShaders::flushTexMatrix(GLint location, - GrGLTexture::Orientation orientation) { - GrMatrix* m; - GrMatrix temp; - if (GrGLTexture::kBottomUp_Orientation == orientation) { - temp.setAll( - GR_Scalar1, 0, 0, - 0, -GR_Scalar1, GR_Scalar1, - 0, 0, GrMatrix::I()[8] - ); - temp.preConcat(fCurrDrawState.fTextureMatrices[0]); - m = &temp; - } else { - GrAssert(GrGLTexture::kTopDown_Orientation == orientation); - m = &fCurrDrawState.fTextureMatrices[0]; - } - - // ES doesn't allow you to pass true to the transpose param, - // so do our own transpose - GrScalar mt[] = { - (*m)[GrMatrix::kScaleX], - (*m)[GrMatrix::kSkewY], - (*m)[GrMatrix::kPersp0], - (*m)[GrMatrix::kSkewX], - (*m)[GrMatrix::kScaleY], - (*m)[GrMatrix::kPersp1], - (*m)[GrMatrix::kTransX], - (*m)[GrMatrix::kTransY], - (*m)[GrMatrix::kPersp2] - }; -#if ATTRIBUTE_MATRIX - GR_GL(VertexAttrib4fv(GR_GL_TEXMAT_ATTR_LOCATION+0, mt+0)); - GR_GL(VertexAttrib4fv(GR_GL_TEXMAT_ATTR_LOCATION+1, mt+3)); - GR_GL(VertexAttrib4fv(GR_GL_TEXMAT_ATTR_LOCATION+2, mt+6)); -#else - GR_GL(UniformMatrix3fv(location,1,false,mt)); -#endif -} - -void GrGpuGLShaders::flushTwoPointRadial(GLint paramsLocation, - const GrSamplerState& state) { - GrScalar centerX1 = state.getRadial2CenterX1(); - GrScalar radius0 = state.getRadial2Radius0(); - - GrScalar a = GrMul(centerX1, centerX1) - GR_Scalar1; - - float unis[6] = { - GrScalarToFloat(a), - 1 / (2.f * unis[0]), - GrScalarToFloat(centerX1), - GrScalarToFloat(radius0), - GrScalarToFloat(GrMul(radius0, radius0)), - state.isRadial2PosRoot() ? 1.f : -1.f - }; - GR_GL(Uniform1fv(paramsLocation, 6, unis)); -} - -void GrGpuGLShaders::flushProgram(PrimitiveType type) { - - Programs nextProgram = kNoTexture_Program; - - GrTexture* texture = fCurrDrawState.fTextures[0]; - bool posAsTex = - !!(StagePosAsTexCoordVertexLayoutBit(0) & fGeometrySrc.fVertexLayout); - - if (!VertexUsesStage(0, fGeometrySrc.fVertexLayout)) { - goto HAVE_NEXT_PROGRAM; - } - - GrAssert(NULL != texture); - - switch (fCurrDrawState.fSamplerStates[0].getSampleMode()) { - case GrSamplerState::kRadial_SampleMode: - GrAssert(!fCurrDrawState.fTextureMatrices[0].hasPerspective()); - GrAssert(GrTexture::kAlpha_8_PixelConfig != texture->config()); - if (posAsTex) { - nextProgram = kRadialTextureVertCoords_Program; - } else { - nextProgram = kRadialTextureTexCoords_Program; - } - break; - case GrSamplerState::kSweep_SampleMode: - GrAssert(!fCurrDrawState.fTextureMatrices[0].hasPerspective()); - GrAssert(GrTexture::kAlpha_8_PixelConfig != texture->config()); - if (posAsTex) { - nextProgram = kSweepTextureVertCoords_Program; - } else { - nextProgram = kSweepTextureTexCoords_Program; - } - break; - case GrSamplerState::kRadial2_SampleMode: - GrAssert(!fCurrDrawState.fTextureMatrices[0].hasPerspective()); - GrAssert(GrTexture::kAlpha_8_PixelConfig != texture->config()); - if (posAsTex) { - nextProgram = kTwoPointRadialTextureVertCoords_Program; - } else { - nextProgram = kTwoPointRadialTextureTexCoords_Program; - } - break; - case GrSamplerState::kNormal_SampleMode: - if (GrTexture::kAlpha_8_PixelConfig == texture->config()) { - GrAssert(((GrGLTexture*)texture)->orientation() == - GrGLTexture::kTopDown_Orientation); - GrAssert(!posAsTex); - nextProgram = kText_Program; - } else { - bool persp = fCurrDrawState.fTextureMatrices[0].hasPerspective(); - if (posAsTex) { - nextProgram = persp ? kTextureVertCoordsProj_Program : - kTextureVertCoords_Program; - } else { - nextProgram = persp ? kTextureTexCoordsProj_Program : - kTextureTexCoords_Program; - } - // check for case when frag shader can skip the color modulation - if (!persp && !(fGeometrySrc.fVertexLayout - & kColor_VertexLayoutBit) && - 0xffffffff == fCurrDrawState.fColor) { - switch (nextProgram) { - case kTextureVertCoords_Program: - nextProgram = kTextureVertCoordsNoColor_Program; - break; - case kTextureTexCoords_Program: - nextProgram = kTextureTexCoordsNoColor_Program; - break; - default: - GrAssert("Unexpected"); - break; - } - } - } - break; - default: - GrAssert(!"Unknown samplemode"); - break; - } - -HAVE_NEXT_PROGRAM: - if (fHWProgram != nextProgram) { - GR_GL(UseProgram(fPrograms[nextProgram].fProgramID)); - fHWProgram = nextProgram; -#if GR_COLLECT_STATS - ++fStats.fProgChngCnt; -#endif - } -} - -bool GrGpuGLShaders::flushGraphicsState(PrimitiveType type) { - - for (int s = 1; s < kNumStages; ++s) { - if (VertexUsesStage(s, fGeometrySrc.fVertexLayout)) { - unimpl("the hard-coded shaders used by this " - "class only support 1 stage"); - return false; - } - } - - if (!flushGLStateCommon(type)) { - return false; - } - - if (fRenderTargetChanged) { - // our coords are in pixel space and the GL matrices map to NDC - // so if the viewport changed, our matrix is now wrong. -#if ATTRIBUTE_MATRIX - fHWDrawState.fViewMatrix.setScale(GR_ScalarMax, GR_ScalarMax); -#else - // we assume all shader matrices may be wrong after viewport changes - for (int p = 0; p < kProgramCount; ++p) { - // set to illegal matrix - fPrograms[p].fViewMatrix.setScale(GR_ScalarMax, GR_ScalarMax); - } -#endif - fRenderTargetChanged = false; - } - - flushProgram(type); - - if (fGeometrySrc.fVertexLayout & kColor_VertexLayoutBit) { - // invalidate the immediate mode color - fHWDrawState.fColor = GrColor_ILLEGAL; - } else { - // if we don't have per-vert colors either set the color attr - // or color uniform (depending on which program). - if (-1 != fPrograms[fHWProgram].fColorLocation) { - GrAssert(kUniform_ColorType == fPrograms[fHWProgram].fColorType); - if (fPrograms[fHWProgram].fColor != fCurrDrawState.fColor) { - float c[] = { - GrColorUnpackR(fCurrDrawState.fColor) / 255.f, - GrColorUnpackG(fCurrDrawState.fColor) / 255.f, - GrColorUnpackB(fCurrDrawState.fColor) / 255.f, - GrColorUnpackA(fCurrDrawState.fColor) / 255.f - }; - GR_GL(Uniform4fv(fPrograms[fHWProgram].fColorLocation, 1, c)); - fPrograms[fHWProgram].fColor = fCurrDrawState.fColor; - } - } else if (kAttrib_ColorType == fPrograms[fHWProgram].fColorType && - fHWDrawState.fColor != fCurrDrawState.fColor) { - // OpenGL ES only supports the float varities of glVertexAttrib - float c[] = { - GrColorUnpackR(fCurrDrawState.fColor) / 255.f, - GrColorUnpackG(fCurrDrawState.fColor) / 255.f, - GrColorUnpackB(fCurrDrawState.fColor) / 255.f, - GrColorUnpackA(fCurrDrawState.fColor) / 255.f - }; - GR_GL(VertexAttrib4fv(GR_GL_COL_ATTR_LOCATION, c)); - fHWDrawState.fColor = fCurrDrawState.fColor; - } - } - -#if ATTRIBUTE_MATRIX - GrMatrix& currentViewMatrix = fHWDrawState.fViewMatrix; - GrMatrix& currentTexMatrix = fHWDrawState.fTextureMatrices[0]; - GrGLTexture::Orientation& orientation = fTextureOrientation; -#else - GrMatrix& currentViewMatrix = fPrograms[fHWProgram].fViewMatrix; - GrMatrix& currentTexMatrix = fPrograms[fHWProgram].fTextureMatrices[0]; - GrGLTexture::Orientation& orientation = - fPrograms[fHWProgram].fTextureOrientation; -#endif - - if (currentViewMatrix != - fCurrDrawState.fViewMatrix) { - flushMatrix(fPrograms[fHWProgram].fMatrixLocation); - currentViewMatrix = fCurrDrawState.fViewMatrix; - } - - GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[0]; - if (NULL != texture) { - if (-1 != fPrograms[fHWProgram].fTexMatrixLocation && - (currentTexMatrix != fCurrDrawState.fTextureMatrices[0] || - orientation != texture->orientation())) { - flushTexMatrix(fPrograms[fHWProgram].fTexMatrixLocation, - texture->orientation()); - currentTexMatrix = fCurrDrawState.fTextureMatrices[0]; - orientation = texture->orientation(); - } - } - - const GrSamplerState& sampler = fCurrDrawState.fSamplerStates[0]; - if (-1 != fPrograms[fHWProgram].fTwoPointParamsLocation && - (fPrograms[fHWProgram].fRadial2CenterX1 != sampler.getRadial2CenterX1() || - fPrograms[fHWProgram].fRadial2Radius0 != sampler.getRadial2Radius0() || - fPrograms[fHWProgram].fRadial2PosRoot != sampler.isRadial2PosRoot())) { - - flushTwoPointRadial(fPrograms[fHWProgram].fTwoPointParamsLocation, - sampler); - fPrograms[fHWProgram].fRadial2CenterX1 = sampler.getRadial2CenterX1(); - fPrograms[fHWProgram].fRadial2Radius0 = sampler.getRadial2Radius0(); - fPrograms[fHWProgram].fRadial2PosRoot = sampler.isRadial2PosRoot(); - } - - return true; -} - -void GrGpuGLShaders::setupGeometry(int* startVertex, - int* startIndex, - int vertexCount, - int indexCount) { - - int newColorOffset; - int newTexCoordOffsets[kNumStages]; - - GLsizei newStride = VertexSizeAndOffsetsByStage(fGeometrySrc.fVertexLayout, - newTexCoordOffsets, - &newColorOffset); - int oldColorOffset; - int oldTexCoordOffsets[kNumStages]; - GLsizei oldStride = VertexSizeAndOffsetsByStage(fHWGeometryState.fVertexLayout, - oldTexCoordOffsets, - &oldColorOffset); - bool indexed = NULL == startIndex; - - int extraVertexOffset; - int extraIndexOffset; - setBuffers(indexed, &extraVertexOffset, &extraIndexOffset); - - GLenum scalarType; - bool texCoordNorm; - if (fGeometrySrc.fVertexLayout & kTextFormat_VertexLayoutBit) { - scalarType = GrGLTextType; - texCoordNorm = GR_GL_TEXT_TEXTURE_NORMALIZED; - } else { - scalarType = GrGLType; - texCoordNorm = false; - } - - size_t vertexOffset = (*startVertex + extraVertexOffset) * newStride; - *startVertex = 0; - if (indexed) { - *startIndex += extraIndexOffset; - } - - // all the Pointers must be set if any of these are true - bool allOffsetsChange = fHWGeometryState.fArrayPtrsDirty || - vertexOffset != fHWGeometryState.fVertexOffset || - newStride != oldStride; - - // position and tex coord offsets change if above conditions are true - // or the type/normalization changed based on text vs nontext type coords. - bool posAndTexChange = allOffsetsChange || - (((GrGLTextType != GrGLType) || GR_GL_TEXT_TEXTURE_NORMALIZED) && - (kTextFormat_VertexLayoutBit & - (fHWGeometryState.fVertexLayout ^ - fGeometrySrc.fVertexLayout))); - - if (posAndTexChange) { - GR_GL(VertexAttribPointer(GR_GL_POS_ATTR_LOCATION, 2, scalarType, - false, newStride, (GLvoid*)vertexOffset)); - fHWGeometryState.fVertexOffset = vertexOffset; - } - - // this class only supports one stage. - if (newTexCoordOffsets[0] > 0) { - GLvoid* texCoordOffset = (GLvoid*)(vertexOffset + newTexCoordOffsets[0]); - if (oldTexCoordOffsets[0] <= 0) { - GR_GL(EnableVertexAttribArray(GR_GL_TEX_ATTR_LOCATION)); - GR_GL(VertexAttribPointer(GR_GL_TEX_ATTR_LOCATION, 2, scalarType, - texCoordNorm, newStride, texCoordOffset)); - } else if (posAndTexChange || - newTexCoordOffsets[0] != oldTexCoordOffsets[0]) { - GR_GL(VertexAttribPointer(GR_GL_TEX_ATTR_LOCATION, 2, scalarType, - texCoordNorm, newStride, texCoordOffset)); - } - } else if (oldTexCoordOffsets[0] > 0) { - GR_GL(DisableVertexAttribArray(GR_GL_TEX_ATTR_LOCATION)); - } - - if (newColorOffset > 0) { - GLvoid* colorOffset = (GLvoid*)(vertexOffset + newColorOffset); - if (oldColorOffset <= 0) { - GR_GL(EnableVertexAttribArray(GR_GL_COL_ATTR_LOCATION)); - GR_GL(VertexAttribPointer(GR_GL_COL_ATTR_LOCATION, 4, - GL_UNSIGNED_BYTE, - true, newStride, colorOffset)); - } else if (allOffsetsChange || newColorOffset != oldColorOffset) { - GR_GL(VertexAttribPointer(GR_GL_COL_ATTR_LOCATION, 4, - GL_UNSIGNED_BYTE, - true, newStride, colorOffset)); - } - } else if (oldColorOffset > 0) { - GR_GL(DisableVertexAttribArray(GR_GL_COL_ATTR_LOCATION)); - } - - fHWGeometryState.fVertexLayout = fGeometrySrc.fVertexLayout; - fHWGeometryState.fArrayPtrsDirty = false; -} -#endif diff --git a/gpu/src/GrGpuGLShaders.h b/gpu/src/GrGpuGLShaders.h deleted file mode 100644 index a048e93b6a..0000000000 --- a/gpu/src/GrGpuGLShaders.h +++ /dev/null @@ -1,154 +0,0 @@ -/* - Copyright 2010 Google Inc. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - */ - - -#ifndef GrGpuGLShaders_DEFINED -#define GrGpuGLShaders_DEFINED - -#include "GrGpuGL.h" - -// Programmable OpenGL or OpenGL ES 2.0 -class GrGpuGLShaders : public GrGpuGL { -public: - GrGpuGLShaders(); - virtual ~GrGpuGLShaders(); - - virtual void resetContext(); - - // type of colors used by a program - enum ColorType { - kNone_ColorType, - kAttrib_ColorType, - kUniform_ColorType, - }; -protected: - // overrides from GrGpu - virtual bool flushGraphicsState(PrimitiveType type); - virtual void setupGeometry(int* startVertex, - int* startIndex, - int vertexCount, - int indexCount); - -private: - void resetContextHelper(); - - // sets the texture matrix uniform for currently bound program - void flushTexMatrix(GLint location, - GrGLTexture::Orientation orientation); - // sets the MVP matrix uniform for currently bound program - void flushMatrix(GLint location); - - void flushTwoPointRadial(GLint paramsLocation, const GrSamplerState&); - - // reads shader from array and compiles it with GL, returns shader ID or 0 if failed - GLuint loadShader(GLenum type, const char* src); - - struct ProgramData; - // creates a GL program with two shaders attached. - // Gets the relevant uniform locations. - // Sets the texture sampler if present to texture 0 - // Binds the program - // returns true if succeeded. - bool createProgram(GLuint vshader, - GLuint fshader, - bool hasTexMatrix, - bool hasTexCoords, - ColorType colorType, - bool twoPointRadial, - ProgramData* program); - - // called at flush time to setup the appropriate program - void flushProgram(PrimitiveType type); - - enum Programs { - // use vertex coordinates - kTextureVertCoords_Program = 0, - kTextureVertCoordsProj_Program, - - // use separate tex coords - kTextureTexCoords_Program, - kTextureTexCoordsProj_Program, - - // constant color texture, no proj - // verts as a tex coords - kTextureVertCoordsNoColor_Program, - - // constant color texture, no proj - // separate tex coords - kTextureTexCoordsNoColor_Program, - - // special program for text glyphs - kText_Program, - - // programs for radial texture lookup - kRadialTextureVertCoords_Program, - kRadialTextureTexCoords_Program, - - // programs for sweep texture lookup - kSweepTextureVertCoords_Program, - kSweepTextureTexCoords_Program, - - // programs for two-point radial lookup - kTwoPointRadialTextureVertCoords_Program, - kTwoPointRadialTextureTexCoords_Program, - - // color only drawing - kNoTexture_Program, - - kProgramCount - }; - - // Records per-program information - // we can specify the attribute locations so that they are constant - // across our shaders. But the driver determines the uniform locations - // at link time. We don't need to remember the sampler uniform location - // because we will bind a texture slot to it and never change it - // Uniforms are program-local so we can't rely on fHWState to hold the - // previous uniform state after a program change. - struct ProgramData { - // IDs - GLuint fVShaderID; - GLuint fFShaderID; - GLuint fProgramID; - - // shader uniform locations (-1 if shader doesn't use them) - GLint fMatrixLocation; - GLint fTexMatrixLocation; - GLint fColorLocation; - GLint fTwoPointParamsLocation; - - ColorType fColorType; - - // these reflect the current values of uniforms - // (GL uniform values travel with program) - GrMatrix fViewMatrix; - GrMatrix fTextureMatrices[kNumStages]; - GrColor fColor; - GrGLTexture::Orientation fTextureOrientation; - GrScalar fRadial2CenterX1; - GrScalar fRadial2Radius0; - bool fRadial2PosRoot; - }; - - ProgramData fPrograms[kProgramCount]; - Programs fHWProgram; - - GrGLTexture::Orientation fTextureOrientation; - - typedef GrGpuGL INHERITED; -}; - -#endif diff --git a/gpu/src/GrGpuGLShaders2.cpp b/gpu/src/GrGpuGLShaders2.cpp index d423ee2740..34e7c87e50 100644 --- a/gpu/src/GrGpuGLShaders2.cpp +++ b/gpu/src/GrGpuGLShaders2.cpp @@ -81,8 +81,7 @@ struct GrGpuGLShaders2::Program { // these reflect the current values of uniforms // (GL uniform values travel with program) GrMatrix fViewMatrix; - GrMatrix fTextureMatrix[kNumStages]; - GrGLTexture::Orientation fTextureOrientation[kNumStages]; + GrMatrix fTextureMatrices[kNumStages]; GrScalar fRadial2CenterX1[kNumStages]; GrScalar fRadial2Radius0[kNumStages]; bool fRadial2PosRoot[kNumStages]; @@ -189,8 +188,7 @@ public: void invalidateViewMatrices() { for (int i = 0; i < fCount; ++i) { // set to illegal matrix - fEntries[i].fProgram.fViewMatrix.setScale(GR_ScalarMax, - GR_ScalarMax); + fEntries[i].fProgram.fViewMatrix = GrMatrix::InvalidMatrix(); } } @@ -684,7 +682,7 @@ void GrGpuGLShaders2::GenProgram(const ProgramDesc& desc, desc.fOptFlags); #if ATTRIBUTE_MATRIX - segments.fVSAttrs = "attribute mat3 " VIEW_MATRIX_NAME ";\n" + segments.fVSAttrs = "attribute mat3 " VIEW_MATRIX_NAME ";\n"; #else segments.fVSUnis = "uniform mat3 " VIEW_MATRIX_NAME ";\n"; segments.fVSAttrs = ""; @@ -883,9 +881,11 @@ void GrGpuGLShaders2::GenProgram(const ProgramDesc& desc, for (int s = 0; s < kNumStages; ++s) { if (desc.fStages[s].fEnabled) { + GrStringBuilder matName; + tex_matrix_name(s, &matName); GR_GL(BindAttribLocation(progID, TEXMAT_ATTR_LOCATION(s), - tex_matrix_name(i).cstr())); + matName.cstr())); program->fUniLocations.fStages[s].fTextureMatrixUni = BOGUS_MATRIX_UNI_LOCATION; } @@ -972,11 +972,11 @@ void GrGpuGLShaders2::GenProgram(const ProgramDesc& desc, if (-1 != program->fUniLocations.fStages[s].fSamplerUni) { GR_GL(Uniform1i(program->fUniLocations.fStages[s].fSamplerUni, s)); } - program->fTextureMatrix[s].setScale(GR_ScalarMax, GR_ScalarMax); + program->fTextureMatrices[s] = GrMatrix::InvalidMatrix(); program->fRadial2CenterX1[s] = GR_ScalarMax; program->fRadial2Radius0[s] = -GR_ScalarMax; } - program->fViewMatrix.setScale(GR_ScalarMax, GR_ScalarMax); + program->fViewMatrix = GrMatrix::InvalidMatrix(); } void GrGpuGLShaders2::getProgramDesc(PrimitiveType primType, ProgramDesc* desc) { @@ -1005,10 +1005,9 @@ void GrGpuGLShaders2::getProgramDesc(PrimitiveType primType, ProgramDesc* desc) GrAssert(NULL != texture); // we matrix to invert when orientation is TopDown, so make sure // we aren't in that case before flagging as identity. - if (fCurrDrawState.fTextureMatrices[s].isIdentity() && - GrGLTexture::kTopDown_Orientation == texture->orientation()) { + if (TextureMatrixIsIdentity(texture, fCurrDrawState.fSamplerStates[s])) { stage.fOptFlags = StageDesc::kIdentityMatrix_OptFlagBit; - } else if (!fCurrDrawState.fTextureMatrices[s].hasPerspective()) { + } else if (!getSamplerMatrix(s).hasPerspective()) { stage.fOptFlags = StageDesc::kNoPerspective_OptFlagBit; } else { stage.fOptFlags = 0; @@ -1103,14 +1102,28 @@ GrGpuGLShaders2::~GrGpuGLShaders2() { delete fProgramCache; } +const GrMatrix& GrGpuGLShaders2::getHWSamplerMatrix(int stage) { +#if ATTRIBUTE_MATRIX + return fHWDrawState.fSamplerStates[stage].getMatrix(); +#else + return fProgram->fTextureMatrices[stage]; +#endif +} + +void GrGpuGLShaders2::recordHWSamplerMatrix(int stage, const GrMatrix& matrix){ +#if ATTRIBUTE_MATRIX + fHWDrawState.fSamplerStates[stage].setMatrix(matrix); +#else + fProgram->fTextureMatrices[stage] = matrix; +#endif +} + void GrGpuGLShaders2::resetContext() { INHERITED::resetContext(); resetContextHelper(); } void GrGpuGLShaders2::resetContextHelper() { - fTextureOrientation = (GrGLTexture::Orientation)-1; // illegal - fHWGeometryState.fVertexLayout = 0; fHWGeometryState.fVertexOffset = ~0; GR_GL(DisableVertexAttribArray(COL_ATTR_LOCATION)); @@ -1153,38 +1166,27 @@ void GrGpuGLShaders2::flushViewMatrix() { } void GrGpuGLShaders2::flushTextureMatrix(int stage) { - GrAssert(NULL != fCurrDrawState.fTextures[stage]); - GrGLTexture::Orientation orientation = - ((GrGLTexture*)fCurrDrawState.fTextures[stage])->orientation(); - - GrMatrix* m; - GrMatrix temp; - if (GrGLTexture::kBottomUp_Orientation == orientation) { - temp.setAll( - GR_Scalar1, 0, 0, - 0, -GR_Scalar1, GR_Scalar1, - 0, 0, GrMatrix::I()[8] - ); - temp.preConcat(fCurrDrawState.fTextureMatrices[stage]); - m = &temp; - } else { - GrAssert(GrGLTexture::kTopDown_Orientation == orientation); - m = &fCurrDrawState.fTextureMatrices[stage]; - } + + GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[stage]; + + GrMatrix m = getSamplerMatrix(stage); + GrSamplerState::SampleMode mode = + fCurrDrawState.fSamplerStates[0].getSampleMode(); + AdjustTextureMatrix(texture, mode, &m); // ES doesn't allow you to pass true to the transpose param, // so do our own transpose GrScalar mt[] = { - (*m)[GrMatrix::kScaleX], - (*m)[GrMatrix::kSkewY], - (*m)[GrMatrix::kPersp0], - (*m)[GrMatrix::kSkewX], - (*m)[GrMatrix::kScaleY], - (*m)[GrMatrix::kPersp1], - (*m)[GrMatrix::kTransX], - (*m)[GrMatrix::kTransY], - (*m)[GrMatrix::kPersp2] + m[GrMatrix::kScaleX], + m[GrMatrix::kSkewY], + m[GrMatrix::kPersp0], + m[GrMatrix::kSkewX], + m[GrMatrix::kScaleY], + m[GrMatrix::kPersp1], + m[GrMatrix::kTransX], + m[GrMatrix::kTransY], + m[GrMatrix::kPersp2] }; #if ATTRIBUTE_MATRIX GR_GL(VertexAttrib4fv(TEXMAT_ATTR_LOCATION(0)+0, mt+0)); @@ -1192,9 +1194,7 @@ void GrGpuGLShaders2::flushTextureMatrix(int stage) { GR_GL(VertexAttrib4fv(TEXMAT_ATTR_LOCATION(0)+2, mt+6)); #else GR_GL(UniformMatrix3fv(fProgram->fUniLocations.fStages[stage].fTextureMatrixUni, - 1, - false, - mt)); + 1, false, mt)); #endif } @@ -1240,16 +1240,15 @@ bool GrGpuGLShaders2::flushGraphicsState(PrimitiveType type) { return false; } - if (fRenderTargetChanged) { + if (fDirtyFlags.fRenderTargetChanged) { // our coords are in pixel space and the GL matrices map to NDC // so if the viewport changed, our matrix is now wrong. #if ATTRIBUTE_MATRIX - fHWDrawState.fViewMatrix.setScale(GR_ScalarMax, GR_ScalarMax); + fHWDrawState.fViewMatrix = GrMatrix::InvalidMatrix(); #else // we assume all shader matrices may be wrong after viewport changes fProgramCache->invalidateViewMatrices(); #endif - fRenderTargetChanged = false; } flushProgram(type); @@ -1273,12 +1272,8 @@ bool GrGpuGLShaders2::flushGraphicsState(PrimitiveType type) { #if ATTRIBUTE_MATRIX GrMatrix& currViewMatrix = fHWDrawState.fViewMatrix; - GrMatrix& currTextureMatrix = fHWDrawState.fMatrixModeCache[kTexture_MatrixMode]; - GrGLTexture::Orientation& orientation = fTextureOrientation; #else GrMatrix& currViewMatrix = fProgram->fViewMatrix; - GrMatrix& currTextureMatrix = fProgram->fTextureMatrix[0]; - GrGLTexture::Orientation& orientation = fProgram->fTextureOrientation[0]; #endif if (currViewMatrix != fCurrDrawState.fViewMatrix) { @@ -1290,11 +1285,10 @@ bool GrGpuGLShaders2::flushGraphicsState(PrimitiveType type) { GrGLTexture* texture = (GrGLTexture*) fCurrDrawState.fTextures[s]; if (NULL != texture) { if (-1 != fProgram->fUniLocations.fStages[s].fTextureMatrixUni && - (currTextureMatrix != fCurrDrawState.fTextureMatrices[s] || - orientation != texture->orientation())) { + (((1 << s) & fDirtyFlags.fTextureChangedMask) || + getHWSamplerMatrix(s) != getSamplerMatrix(s))) { flushTextureMatrix(s); - currTextureMatrix = fCurrDrawState.fTextureMatrices[s]; - orientation = texture->orientation(); + recordHWSamplerMatrix(s, getSamplerMatrix(s)); } } @@ -1311,7 +1305,7 @@ bool GrGpuGLShaders2::flushGraphicsState(PrimitiveType type) { fProgram->fRadial2PosRoot[s] = sampler.isRadial2PosRoot(); } } - + resetDirtyFlags(); return true; } diff --git a/gpu/src/GrGpuGLShaders2.h b/gpu/src/GrGpuGLShaders2.h index bb710e33ba..2647e23a15 100644 --- a/gpu/src/GrGpuGLShaders2.h +++ b/gpu/src/GrGpuGLShaders2.h @@ -40,6 +40,10 @@ private: void resetContextHelper(); + // Helpers to make code more readable + const GrMatrix& getHWSamplerMatrix(int stage); + void recordHWSamplerMatrix(int stage, const GrMatrix& matrix); + // sets the texture matrix uniform for currently bound program void flushTextureMatrix(int stage); @@ -89,8 +93,6 @@ private: void ProgramUnitTest(); - GrGLTexture::Orientation fTextureOrientation; - ProgramCache* fProgramCache; Program* fProgram; GLuint fHWProgramID; diff --git a/gpu/src/GrMatrix.cpp b/gpu/src/GrMatrix.cpp index 023968cd58..ee2067900d 100644 --- a/gpu/src/GrMatrix.cpp +++ b/gpu/src/GrMatrix.cpp @@ -70,15 +70,6 @@ const GrMatrix::MapProc GrMatrix::gMapProcs[] = { &GrMatrix::mapPerspective,
};
-const GrMatrix& GrMatrix::I() {
- static GrMatrix* gIdent;
- if (NULL == gIdent) {
- gIdent = new GrMatrix;
- gIdent->setIdentity();
- }
- return *gIdent;
-}
-
void GrMatrix::setIdentity() {
fM[0] = GR_Scalar1; fM[1] = 0; fM[2] = 0;
fM[3] = 0; fM[4] = GR_Scalar1; fM[5] = 0;
@@ -161,7 +152,7 @@ void GrMatrix::setConcat(const GrMatrix& a, const GrMatrix& b) { tmp.fM[8] = a.fM[6] * b.fM[2] + a.fM[7] * b.fM[5] + a.fM[8] * b.fM[8];
}
*this = tmp;
- setTypeMask();
+ this->computeTypeMask();
}
void GrMatrix::preConcat(const GrMatrix& m) {
@@ -239,7 +230,7 @@ bool GrMatrix::invert(GrMatrix* inverted) const { inverted->fM[7] = 0;
inverted->fM[8] = (GrScalar)(t[8] * det);
}
- inverted->setTypeMask();
+ inverted->computeTypeMask();
return true;
}
@@ -347,26 +338,6 @@ bool GrMatrix::operator != (const GrMatrix& m) const { return !(*this == m);
}
-void GrMatrix::setTypeMask()
-{
- fTypeMask = 0;
- if (0 != fM[kPersp0] || 0 != fM[kPersp1] || gRESCALE != fM[kPersp2]) {
- fTypeMask |= kPerspective_TypeBit;
- }
- if (GR_Scalar1 != fM[kScaleX] || GR_Scalar1 != fM[kScaleY]) {
- fTypeMask |= kScale_TypeBit;
- if (0 == fM[kScaleX] && 0 == fM[kScaleY]) {
- fTypeMask |= kZeroScale_TypeBit;
- }
- }
- if (0 != fM[kSkewX] || 0 != fM[kSkewY]) {
- fTypeMask |= kSkew_TypeBit;
- }
- if (0 != fM[kTransX] || 0 != fM[kTransY]) {
- fTypeMask |= kTranslate_TypeBit;
- }
-}
-
////////////////////////////////////////////////////////////////////////////////
// Matrix transformation procs
//////
@@ -648,17 +619,17 @@ void GrMatrix::UnitTest() { a.set(j, GR_Scalar1);
mask = a.fTypeMask;
- a.setTypeMask();
+ a.computeTypeMask();
GrAssert(mask == a.fTypeMask);
a.set(j, 0);
mask = a.fTypeMask;
- a.setTypeMask();
+ a.computeTypeMask();
GrAssert(mask == a.fTypeMask);
a.set(j, 10 * GR_Scalar1);
mask = a.fTypeMask;
- a.setTypeMask();
+ a.computeTypeMask();
GrAssert(mask == a.fTypeMask);
a.set(j, old);
diff --git a/gpu/src/GrTextContext.cpp b/gpu/src/GrTextContext.cpp index 7341005b86..0f244a2e81 100644 --- a/gpu/src/GrTextContext.cpp +++ b/gpu/src/GrTextContext.cpp @@ -45,7 +45,6 @@ void GrTextContext::flushGlyphs() { int nIndices = fCurrVertex + (fCurrVertex >> 1); GrAssert(fCurrTexture); fDrawTarget->setTexture(TEXT_STAGE, fCurrTexture); - fDrawTarget->setTextureMatrix(TEXT_STAGE, GrMatrix::I()); fDrawTarget->setIndexSourceToBuffer(fContext->getQuadIndexBuffer()); fDrawTarget->drawIndexed(GrDrawTarget::kTriangles_PrimitiveType, @@ -94,7 +93,7 @@ GrTextContext::GrTextContext(GrContext* context, fVertexLayout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0); GrMatrix inverseViewMatrix; if (fOrigViewMatrix.invert(&inverseViewMatrix)) { - fPaint.fTextureMatrix.preConcat(inverseViewMatrix); + fPaint.fSampler.preConcatMatrix(inverseViewMatrix); } } diff --git a/gpu/src/GrTextureCache.cpp b/gpu/src/GrTextureCache.cpp index e27272ec25..fe3ff689a4 100644 --- a/gpu/src/GrTextureCache.cpp +++ b/gpu/src/GrTextureCache.cpp @@ -228,8 +228,8 @@ void GrTextureCache::purgeAsNeeded() { #if GR_DUMP_TEXTURE_UPLOAD GrPrintf("--- ~texture from cache %p [%d %d]\n", entry->texture(), - entry->texture()->contentWidth(), - entry->texture()->contentHeight()); + entry->texture()->width(), + entry->texture()->height()); #endif delete entry; } diff --git a/gpu/src/gr_files.mk b/gpu/src/gr_files.mk index 4c06345fe7..e6d69972cb 100644 --- a/gpu/src/gr_files.mk +++ b/gpu/src/gr_files.mk @@ -8,11 +8,10 @@ SOURCE := \ GrGLTexture.cpp \ GrGLVertexBuffer.cpp \ GrGpu.cpp \ - GrGpuGLShaders.cpp \ GrGpuGLShaders2.cpp \ GrGpuGLFixed.cpp \ GrGpuFactory.cpp \ - GrGLUtil.cpp \ + GrGLUtil.cpp \ GrGpuGL.cpp \ GrInOrderDrawBuffer.cpp \ GrMatrix.cpp \ |