From 47c66ddaeb48faf963a2ef3f508a7d816e4168cc Mon Sep 17 00:00:00 2001 From: "commit-bot@chromium.org" Date: Thu, 29 May 2014 01:12:10 +0000 Subject: [PATCH] separate view matrix from rt adjustment R=robertphillips@google.com Author: bsalomon@google.com Review URL: https://codereview.chromium.org/299943002 git-svn-id: http://skia.googlecode.com/svn/trunk@14944 2bbb7eff-a529-9590-31e7-b0007b416f81 --- src/gpu/gl/GrGLProgram.cpp | 6 ++++++ src/gpu/gl/GrGLProgram.h | 35 ++++++++++++++++++++++++++++++-- src/gpu/gl/GrGLShaderBuilder.cpp | 12 +++++++++-- src/gpu/gl/GrGLShaderBuilder.h | 9 +++++++- src/gpu/gl/GrGpuGL.cpp | 2 +- 5 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/gpu/gl/GrGLProgram.cpp b/src/gpu/gl/GrGLProgram.cpp index aa46aedae0..efd6e4606b 100644 --- a/src/gpu/gl/GrGLProgram.cpp +++ b/src/gpu/gl/GrGLProgram.cpp @@ -52,6 +52,7 @@ GrGLProgram::GrGLProgram(GrGpuGL* gpu, GrGLFullShaderBuilder fullBuilder(fGpu, fUniformManager, fDesc); if (this->genProgram(&fullBuilder, colorStages, coverageStages)) { fUniformHandles.fViewMatrixUni = fullBuilder.getViewMatrixUniform(); + fUniformHandles.fRTAdjustmentUni = fullBuilder.getRTAdjustmentVecUniform(); fHasVertexShader = true; } } else { @@ -329,6 +330,7 @@ void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) { if (!fHasVertexShader) { SkASSERT(!fUniformHandles.fViewMatrixUni.isValid()); + SkASSERT(!fUniformHandles.fRTAdjustmentUni.isValid()); fGpu->setProjectionMatrix(drawState.getViewMatrix(), size, rt->origin()); } else if (fMatrixState.fRenderTargetOrigin != rt->origin() || fMatrixState.fRenderTargetSize != size || @@ -342,5 +344,9 @@ void GrGLProgram::setMatrixAndRenderTargetHeight(const GrDrawState& drawState) { GrGLfloat viewMatrix[3 * 3]; fMatrixState.getGLMatrix<3>(viewMatrix); fUniformManager.setMatrix3f(fUniformHandles.fViewMatrixUni, viewMatrix); + + GrGLfloat rtAdjustmentVec[4]; + fMatrixState.getRTAdjustmentVec(rtAdjustmentVec); + fUniformManager.set4fv(fUniformHandles.fRTAdjustmentUni, 1, rtAdjustmentVec); } } diff --git a/src/gpu/gl/GrGLProgram.h b/src/gpu/gl/GrGLProgram.h index 0a2a242fca..b6d9a8e085 100644 --- a/src/gpu/gl/GrGLProgram.h +++ b/src/gpu/gl/GrGLProgram.h @@ -103,20 +103,50 @@ public: fRenderTargetSize.fHeight = -1; fRenderTargetOrigin = (GrSurfaceOrigin) -1; } + + /** + * Gets a matrix that goes from local coords to Skia's device coordinates. + */ template void getGLMatrix(GrGLfloat* destMatrix) { + GrGLGetMatrix(destMatrix, fViewMatrix); + } + + /** + * Gets a matrix that goes from local coordinates to GL normalized device coords. + */ + template void getRTAdjustedGLMatrix(GrGLfloat* destMatrix) { SkMatrix combined; if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1, 0, -SkIntToScalar(2) / fRenderTargetSize.fHeight, SK_Scalar1, - 0, 0, SkMatrix::I()[8]); + 0, 0, 1); } else { combined.setAll(SkIntToScalar(2) / fRenderTargetSize.fWidth, 0, -SK_Scalar1, 0, SkIntToScalar(2) / fRenderTargetSize.fHeight, -SK_Scalar1, - 0, 0, SkMatrix::I()[8]); + 0, 0, 1); } combined.preConcat(fViewMatrix); GrGLGetMatrix(destMatrix, combined); } + + /** + * Gets a vec4 that adjusts the position from Skia device coords to GL's normalized device + * coords. Assuming the transformed position, pos, is a homogeneous vec3, the vec, v, is + * applied as such: + * pos.x = dot(v.xy, pos.xz) + * pos.y = dot(v.zq, pos.yz) + */ + void getRTAdjustmentVec(GrGLfloat* destVec) { + destVec[0] = 2.f / fRenderTargetSize.fWidth; + destVec[1] = -1.f; + if (kBottomLeft_GrSurfaceOrigin == fRenderTargetOrigin) { + destVec[2] = -2.f / fRenderTargetSize.fHeight; + destVec[3] = 1.f; + } else { + destVec[2] = 2.f / fRenderTargetSize.fHeight; + destVec[3] = -1.f; + } + } }; /** @@ -137,6 +167,7 @@ private: // handles for uniforms (aside from per-effect samplers) struct UniformHandles { UniformHandle fViewMatrixUni; + UniformHandle fRTAdjustmentUni; UniformHandle fColorUni; UniformHandle fCoverageUni; diff --git a/src/gpu/gl/GrGLShaderBuilder.cpp b/src/gpu/gl/GrGLShaderBuilder.cpp index c5df4c803c..1fd1967e21 100644 --- a/src/gpu/gl/GrGLShaderBuilder.cpp +++ b/src/gpu/gl/GrGLShaderBuilder.cpp @@ -757,11 +757,19 @@ GrGLFullShaderBuilder::GrGLFullShaderBuilder(GrGpuGL* gpu, const char* viewMName; fViewMatrixUniform = this->addUniform(GrGLShaderBuilder::kVertex_Visibility, kMat33f_GrSLType, "ViewM", &viewMName); + const char* rtAdjustName; + fRTAdustmentVecUniform = this->addUniform(GrGLShaderBuilder::kVertex_Visibility, + kVec4f_GrSLType, "rtAdjustment", &rtAdjustName); - this->vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n" - "\tgl_Position = vec4(pos3.xy, 0, pos3.z);\n", + // Transform the position into Skia's device coords. + this->vsCodeAppendf("\tvec3 pos3 = %s * vec3(%s, 1);\n", viewMName, fPositionVar->c_str()); + // Transform from Skia's device coords to GL's normalized device coords. + this->vsCodeAppendf( + "\tgl_Position = vec4(dot(pos3.xz, %s.xy), dot(pos3.yz, %s.zw), 0, pos3.z);\n", + rtAdjustName, rtAdjustName); + // we output point size in the GS if present if (header.fEmitsPointSize #if GR_GL_EXPERIMENTAL_GS diff --git a/src/gpu/gl/GrGLShaderBuilder.h b/src/gpu/gl/GrGLShaderBuilder.h index 233bb524f5..1be9a881d3 100644 --- a/src/gpu/gl/GrGLShaderBuilder.h +++ b/src/gpu/gl/GrGLShaderBuilder.h @@ -417,10 +417,17 @@ public: int effectCnt, GrGLSLExpr4* inOutFSColor) SK_OVERRIDE; + /** + * The view matrix uniform is only valid in the VS. It is always mat33. + */ GrGLUniformManager::UniformHandle getViewMatrixUniform() const { return fViewMatrixUniform; } + GrGLUniformManager::UniformHandle getRTAdjustmentVecUniform() const { + return fRTAdustmentVecUniform; + } + protected: virtual bool compileAndAttachShaders(GrGLuint programId, SkTDArray* shaderIds) const SK_OVERRIDE; virtual void bindProgramLocations(GrGLuint programId) const SK_OVERRIDE; @@ -444,7 +451,7 @@ private: SkSTArray<10, AttributePair, true> fEffectAttributes; GrGLUniformManager::UniformHandle fViewMatrixUniform; - + GrGLUniformManager::UniformHandle fRTAdustmentVecUniform; GrGLShaderVar* fPositionVar; GrGLShaderVar* fLocalCoordsVar; diff --git a/src/gpu/gl/GrGpuGL.cpp b/src/gpu/gl/GrGpuGL.cpp index c4d1c27c00..ff53b9c14c 100644 --- a/src/gpu/gl/GrGpuGL.cpp +++ b/src/gpu/gl/GrGpuGL.cpp @@ -2233,7 +2233,7 @@ void GrGpuGL::setProjectionMatrix(const SkMatrix& matrix, fHWProjectionMatrixState.fRenderTargetOrigin = renderTargetOrigin; GrGLfloat glMatrix[4 * 4]; - fHWProjectionMatrixState.getGLMatrix<4>(glMatrix); + fHWProjectionMatrixState.getRTAdjustedGLMatrix<4>(glMatrix); GL_CALL(MatrixLoadf(GR_GL_PROJECTION, glMatrix)); }