[Texmap] Consolidate redundant code for ARB_RECT texture
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 12 Dec 2012 08:10:44 +0000 (08:10 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 12 Dec 2012 08:10:44 +0000 (08:10 +0000)
https://bugs.webkit.org/show_bug.cgi?id=104705

Patch by No'am Rosenthal <noam@webkit.org> on 2012-12-12
Reviewed by Kenneth Rohde Christiansen.

Instead of having a whole different function for RECT textures,
keep the existing functions and pass a flag that tells
TextureMapperGL to use the non-normalized coordinates and the
RECT shaders.

No new tests, refactor only. Tested to not break WebGL on
Qt-Mac.

* platform/graphics/surfaces/mac/GraphicsSurfaceMac.cpp:
(WebCore::GraphicsSurface::platformPaintToTextureMapper):
* platform/graphics/texmap/TextureMapperGL.cpp:
(WebCore::TextureMapperGL::drawQuad):
    Compensate for rect textures in drawQuad.

(WebCore::TextureMapperGL::drawBorder):
(WebCore::TextureMapperGL::drawTexture):
(WebCore::TextureMapperGL::drawSolidColor):
(WebCore::TextureMapperGL::drawTextureWithAntialiasing):
(WebCore::TextureMapperGL::drawTexturedQuadWithProgram):
    Pass flags to TextureMapperGL::drawQuad instead of a bool.

* platform/graphics/texmap/TextureMapperGL.h:
(TextureMapperGL):
* platform/graphics/texmap/TextureMapperShaderManager.cpp:
(WebCore::getShaderSpec):
* platform/graphics/texmap/TextureMapperShaderManager.h:
(TextureMapperShaderProgram):
    Compensate for the texture-size in the vertex
    shader instead of in the fragment shader.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@137428 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/surfaces/mac/GraphicsSurfaceMac.cpp
Source/WebCore/platform/graphics/texmap/TextureMapperGL.cpp
Source/WebCore/platform/graphics/texmap/TextureMapperGL.h
Source/WebCore/platform/graphics/texmap/TextureMapperShaderManager.cpp
Source/WebCore/platform/graphics/texmap/TextureMapperShaderManager.h

index 35933c6..e88d6c8 100644 (file)
@@ -1,3 +1,40 @@
+2012-12-12  No'am Rosenthal  <noam@webkit.org>
+
+        [Texmap] Consolidate redundant code for ARB_RECT texture
+        https://bugs.webkit.org/show_bug.cgi?id=104705
+
+        Reviewed by Kenneth Rohde Christiansen.
+
+        Instead of having a whole different function for RECT textures,
+        keep the existing functions and pass a flag that tells
+        TextureMapperGL to use the non-normalized coordinates and the
+        RECT shaders.
+
+        No new tests, refactor only. Tested to not break WebGL on
+        Qt-Mac.
+
+        * platform/graphics/surfaces/mac/GraphicsSurfaceMac.cpp:
+        (WebCore::GraphicsSurface::platformPaintToTextureMapper):
+        * platform/graphics/texmap/TextureMapperGL.cpp:
+        (WebCore::TextureMapperGL::drawQuad):
+            Compensate for rect textures in drawQuad.
+
+        (WebCore::TextureMapperGL::drawBorder):
+        (WebCore::TextureMapperGL::drawTexture):
+        (WebCore::TextureMapperGL::drawSolidColor):
+        (WebCore::TextureMapperGL::drawTextureWithAntialiasing):
+        (WebCore::TextureMapperGL::drawTexturedQuadWithProgram):
+            Pass flags to TextureMapperGL::drawQuad instead of a bool.
+
+        * platform/graphics/texmap/TextureMapperGL.h:
+        (TextureMapperGL):
+        * platform/graphics/texmap/TextureMapperShaderManager.cpp:
+        (WebCore::getShaderSpec):
+        * platform/graphics/texmap/TextureMapperShaderManager.h:
+        (TextureMapperShaderProgram):
+            Compensate for the texture-size in the vertex
+            shader instead of in the fragment shader.
+
 2012-12-11  Hayato Ito  <hayato@chromium.org>
 
         REGRESSION(r137408): breaks chromium's browser tests which use WebKitShadowRoot (Requested by hayato on #webkit).
index 439bf9a..f4aa4c6 100644 (file)
@@ -345,7 +345,7 @@ void GraphicsSurface::platformPaintToTextureMapper(TextureMapper* textureMapper,
 {
     TransformationMatrix adjustedTransform = transform;
     adjustedTransform.multiply(TransformationMatrix::rectToRect(FloatRect(FloatPoint::zero(), m_private->size()), targetRect));
-    static_cast<TextureMapperGL*>(textureMapper)->drawTextureRectangleARB(m_private->frontBufferTextureID(), TextureMapperGL::SupportsBlending, m_private->size(), targetRect, adjustedTransform, opacity, mask);
+    static_cast<TextureMapperGL*>(textureMapper)->drawTexture(m_private->frontBufferTextureID(), TextureMapperGL::ShouldBlend | TextureMapperGL::ShouldUseARBTextureRect, m_private->size(), targetRect, adjustedTransform, opacity, mask);
 }
 
 uint32_t GraphicsSurface::platformFrontBuffer() const
index cbd7f1a..488e901 100644 (file)
@@ -22,6 +22,7 @@
 #include "config.h"
 #include "TextureMapperGL.h"
 
+#include "Extensions3D.h"
 #include "GraphicsContext.h"
 #include "Image.h"
 #include "LengthFunctions.h"
@@ -56,7 +57,6 @@
 
 #if !USE(TEXMAP_OPENGL_ES_2)
 // FIXME: Move to Extensions3D.h.
-#define GL_TEXTURE_RECTANGLE_ARB 0x84F5
 #define GL_UNSIGNED_INT_8_8_8_8_REV 0x8367
 #define GL_UNPACK_ROW_LENGTH 0x0CF2
 #define GL_UNPACK_SKIP_PIXELS 0x0CF4
@@ -272,7 +272,7 @@ void TextureMapperGL::endPainting()
 #endif
 }
 
-void TextureMapperGL::drawQuad(const DrawQuad& quadToDraw, const TransformationMatrix& modelViewMatrix, TextureMapperShaderProgram* shaderProgram, GC3Denum drawingMode, bool needsBlending)
+void TextureMapperGL::drawQuad(const DrawQuad& quadToDraw, const TransformationMatrix& modelViewMatrix, TextureMapperShaderProgram* shaderProgram, GC3Denum drawingMode, Flags flags)
 {
     m_context3D->enableVertexAttribArray(shaderProgram->vertexLocation());
     m_context3D->bindBuffer(GraphicsContext3D::ARRAY_BUFFER, 0);
@@ -298,7 +298,7 @@ void TextureMapperGL::drawQuad(const DrawQuad& quadToDraw, const TransformationM
     };
     m_context3D->uniformMatrix4fv(shaderProgram->matrixLocation(), 1, false, m4);
 
-    if (needsBlending) {
+    if (flags & ShouldBlend) {
         m_context3D->blendFunc(GraphicsContext3D::ONE, GraphicsContext3D::ONE_MINUS_SRC_ALPHA);
         m_context3D->enable(GraphicsContext3D::BLEND);
     } else
@@ -321,7 +321,7 @@ void TextureMapperGL::drawBorder(const Color& color, float width, const FloatRec
     m_context3D->uniform4f(program->colorLocation(), r, g, b, a);
     m_context3D->lineWidth(width);
 
-    drawQuad(targetRect, modelViewMatrix, program.get(), GraphicsContext3D::LINE_LOOP, color.hasAlpha());
+    drawQuad(targetRect, modelViewMatrix, program.get(), GraphicsContext3D::LINE_LOOP, color.hasAlpha() ? ShouldBlend : 0);
 }
 
 void TextureMapperGL::drawRepaintCounter(int value, int pointSize, const FloatPoint& targetPoint, const TransformationMatrix& modelViewMatrix)
@@ -402,55 +402,31 @@ void TextureMapperGL::drawTexture(const BitmapTexture& texture, const FloatRect&
         return;
 
     const BitmapTextureGL& textureGL = static_cast<const BitmapTextureGL&>(texture);
-    drawTexture(textureGL.id(), textureGL.isOpaque() ? 0 : SupportsBlending, textureGL.size(), targetRect, matrix, opacity, mask, exposedEdges);
+    drawTexture(textureGL.id(), textureGL.isOpaque() ? 0 : ShouldBlend, textureGL.size(), targetRect, matrix, opacity, mask, exposedEdges);
 }
 
-#if !USE(TEXMAP_OPENGL_ES_2)
-void TextureMapperGL::drawTextureRectangleARB(uint32_t texture, Flags flags, const IntSize& textureSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture)
-{
-    RefPtr<TextureMapperShaderProgram> program;
-    if (maskTexture)
-        program = data().sharedGLData().textureMapperShaderManager.getShaderProgram(TextureMapperShaderManager::MaskedRect);
-    else
-        program = data().sharedGLData().textureMapperShaderManager.getShaderProgram(TextureMapperShaderManager::Rect);
-    m_context3D->useProgram(program->programID());
-
-    m_context3D->enableVertexAttribArray(program->vertexLocation());
-    m_context3D->activeTexture(GraphicsContext3D::TEXTURE0);
-    m_context3D->bindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);
-    m_context3D->uniform1i(program->samplerLocation(), 0);
-
-    m_context3D->uniform1f(program->flipLocation(), !!(flags & ShouldFlipTexture));
-    m_context3D->uniform2f(program->samplerSizeLocation(), textureSize.width(), textureSize.height());
-    m_context3D->uniform1f(program->opacityLocation(), opacity);
-
-    if (maskTexture && maskTexture->isValid()) {
-        const BitmapTextureGL* maskTextureGL = static_cast<const BitmapTextureGL*>(maskTexture);
-        m_context3D->activeTexture(GraphicsContext3D::TEXTURE1);
-        m_context3D->bindTexture(GraphicsContext3D::TEXTURE_2D, maskTextureGL->id());
-        m_context3D->uniform1i(program->maskLocation(), 1);
-        m_context3D->activeTexture(GraphicsContext3D::TEXTURE0);
-    }
-
-    bool needsBlending = (flags & SupportsBlending) || opacity < 0.99 || maskTexture;
-    drawQuad(targetRect, modelViewMatrix, program.get(), GraphicsContext3D::TRIANGLE_FAN, needsBlending);
-}
-#endif // !USE(TEXMAP_OPENGL_ES_2)
-
-void TextureMapperGL::drawTexture(uint32_t texture, Flags flags, const IntSize& /* textureSize */, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture, unsigned exposedEdges)
+void TextureMapperGL::drawTexture(Platform3DObject texture, Flags flags, const IntSize& textureSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture, unsigned exposedEdges)
 {
     bool needsAntialiasing = m_enableEdgeDistanceAntialiasing && !modelViewMatrix.isIntegerTranslation();
-    if (needsAntialiasing && drawTextureWithAntialiasing(texture, flags, targetRect, modelViewMatrix, opacity, maskTexture, exposedEdges))
+    if (needsAntialiasing && drawTextureWithAntialiasing(texture, flags, textureSize, targetRect, modelViewMatrix, opacity, maskTexture, exposedEdges))
        return;
 
+    bool useRect = flags & ShouldUseARBTextureRect;
+    bool masked = !!maskTexture;
+
+    TextureMapperShaderManager::ShaderKey key = TextureMapperShaderManager::Default;
+    if (masked && useRect)
+        key = TextureMapperShaderManager::MaskedRect;
+    else if (masked)
+        key = TextureMapperShaderManager::Masked;
+    else if (useRect)
+        key = TextureMapperShaderManager::Rect;
+
     RefPtr<TextureMapperShaderProgram> program;
-    if (maskTexture)
-        program = data().sharedGLData().textureMapperShaderManager.getShaderProgram(TextureMapperShaderManager::Masked);
-    else
-        program = data().sharedGLData().textureMapperShaderManager.getShaderProgram(TextureMapperShaderManager::Default);
+    program = data().sharedGLData().textureMapperShaderManager.getShaderProgram(key);
     m_context3D->useProgram(program->programID());
 
-    drawTexturedQuadWithProgram(program.get(), texture, flags, targetRect, modelViewMatrix, opacity, maskTexture);
+    drawTexturedQuadWithProgram(program.get(), texture, flags, textureSize, targetRect, modelViewMatrix, opacity, maskTexture);
 }
 
 void TextureMapperGL::drawSolidColor(const FloatRect& rect, const TransformationMatrix& matrix, const Color& color)
@@ -462,7 +438,7 @@ void TextureMapperGL::drawSolidColor(const FloatRect& rect, const Transformation
     color.getRGBA(r, g, b, a);
     m_context3D->uniform4f(program->colorLocation(), r, g, b, a);
 
-    drawQuad(rect, matrix, program.get(), GraphicsContext3D::TRIANGLE_FAN, a < 1);
+    drawQuad(rect, matrix, program.get(), GraphicsContext3D::TRIANGLE_FAN, a < 1 ? ShouldBlend : 0);
 }
 
 static TransformationMatrix viewportMatrix(GraphicsContext3D* context3D)
@@ -549,7 +525,7 @@ static FloatQuad inflateQuad(const FloatQuad& quad, float distance)
     return expandedQuad;
 }
 
-bool TextureMapperGL::drawTextureWithAntialiasing(uint32_t texture, Flags flags, const FloatRect& originalTargetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture, unsigned exposedEdges)
+bool TextureMapperGL::drawTextureWithAntialiasing(uint32_t texture, Flags flags, const IntSize& size, const FloatRect& originalTargetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture, unsigned exposedEdges)
 {
     // The antialiasing path does not support mask textures at the moment.
     if (maskTexture)
@@ -592,17 +568,20 @@ bool TextureMapperGL::drawTextureWithAntialiasing(uint32_t texture, Flags flags,
     m_context3D->useProgram(program->programID());
     m_context3D->uniform3fv(program->expandedQuadEdgesInScreenSpaceLocation(), 8, targetQuadEdges);
 
-    drawTexturedQuadWithProgram(program.get(), texture, flags, DrawQuad(originalTargetRect, expandedQuadInTextureCoordinates), modelViewMatrix, opacity, 0 /* maskTexture */);
+    drawTexturedQuadWithProgram(program.get(), texture, flags, size, DrawQuad(originalTargetRect, expandedQuadInTextureCoordinates), modelViewMatrix, opacity, 0 /* maskTexture */);
     return true;
 }
 
-void TextureMapperGL::drawTexturedQuadWithProgram(TextureMapperShaderProgram* program, uint32_t texture, Flags flags, const DrawQuad& quadToDraw, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture)
+void TextureMapperGL::drawTexturedQuadWithProgram(TextureMapperShaderProgram* program, uint32_t texture, Flags flags, const IntSize& size, const DrawQuad& quadToDraw, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture)
 {
     m_context3D->enableVertexAttribArray(program->vertexLocation());
     m_context3D->activeTexture(GraphicsContext3D::TEXTURE0);
-    m_context3D->bindTexture(GraphicsContext3D::TEXTURE_2D, texture);
+    GC3Denum target = flags & ShouldUseARBTextureRect ? Extensions3D::TEXTURE_RECTANGLE_ARB : GraphicsContext3D::TEXTURE_2D;
+    m_context3D->bindTexture(target, texture);
     m_context3D->uniform1i(program->samplerLocation(), 0);
 
+    FloatSize sizeParameter = flags & ShouldUseARBTextureRect ? size : FloatSize(1, 1);
+    m_context3D->uniform2f(program->textureSizeLocation(), sizeParameter.width(), sizeParameter.height());
     m_context3D->uniform1f(program->flipLocation(), !!(flags & ShouldFlipTexture));
     m_context3D->uniform1f(program->opacityLocation(), opacity);
 
@@ -614,8 +593,10 @@ void TextureMapperGL::drawTexturedQuadWithProgram(TextureMapperShaderProgram* pr
         m_context3D->activeTexture(GraphicsContext3D::TEXTURE0);
     }
 
-    bool needsBlending = (flags & SupportsBlending) || opacity < 0.99 || maskTexture;
-    drawQuad(quadToDraw, modelViewMatrix, program, GraphicsContext3D::TRIANGLE_FAN, needsBlending);
+    if (opacity < 1 || maskTexture)
+        flags |= ShouldBlend;
+
+    drawQuad(quadToDraw, modelViewMatrix, program, GraphicsContext3D::TRIANGLE_FAN, flags);
 }
 
 BitmapTextureGL::BitmapTextureGL(TextureMapperGL* textureMapper)
index 6646dc7..899dcd6 100644 (file)
@@ -43,8 +43,9 @@ public:
     virtual ~TextureMapperGL();
 
     enum Flag {
-        SupportsBlending = 0x01,
-        ShouldFlipTexture = 0x02
+        ShouldBlend = 0x01,
+        ShouldFlipTexture = 0x02,
+        ShouldUseARBTextureRect = 0x04
     };
 
     typedef int Flags;
@@ -53,13 +54,9 @@ public:
     virtual void drawBorder(const Color&, float borderWidth, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix = TransformationMatrix()) OVERRIDE;
     virtual void drawRepaintCounter(int value, int pointSize, const FloatPoint&, const TransformationMatrix& modelViewMatrix = TransformationMatrix()) OVERRIDE;
     virtual void drawTexture(const BitmapTexture&, const FloatRect&, const TransformationMatrix&, float opacity, const BitmapTexture* maskTexture, unsigned exposedEdges) OVERRIDE;
-    virtual void drawTexture(uint32_t texture, Flags, const IntSize& textureSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture, unsigned exposedEdges = AllEdges);
+    virtual void drawTexture(Platform3DObject texture, Flags, const IntSize& textureSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture, unsigned exposedEdges = AllEdges);
     virtual void drawSolidColor(const FloatRect&, const TransformationMatrix&, const Color&) OVERRIDE;
 
-#if !USE(TEXMAP_OPENGL_ES_2)
-    virtual void drawTextureRectangleARB(uint32_t texture, Flags, const IntSize& textureSize, const FloatRect& targetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture);
-#endif
-
     virtual void bindSurface(BitmapTexture* surface) OVERRIDE;
     virtual void beginClip(const TransformationMatrix&, const FloatRect&) OVERRIDE;
     virtual void beginPainting(PaintFlags = 0) OVERRIDE;
@@ -117,9 +114,9 @@ private:
 
     TextureMapperGL();
 
-    bool drawTextureWithAntialiasing(uint32_t texture, Flags, const FloatRect& originalTargetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture, unsigned exposedEdges);
-    void drawTexturedQuadWithProgram(TextureMapperShaderProgram*, uint32_t texture, Flags, const DrawQuad&, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture);
-    void drawQuad(const DrawQuad&, const TransformationMatrix& modelViewMatrix, TextureMapperShaderProgram*, GC3Denum drawingMode, bool needsBlending);
+    bool drawTextureWithAntialiasing(uint32_t texture, Flags, const IntSize&, const FloatRect& originalTargetRect, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture, unsigned exposedEdges);
+    void drawTexturedQuadWithProgram(TextureMapperShaderProgram*, uint32_t texture, Flags, const IntSize&, const DrawQuad&, const TransformationMatrix& modelViewMatrix, float opacity, const BitmapTexture* maskTexture);
+    void drawQuad(const DrawQuad&, const TransformationMatrix& modelViewMatrix, TextureMapperShaderProgram*, GC3Denum drawingMode, Flags);
 
     bool beginScissorClip(const TransformationMatrix&, const FloatRect&);
     void bindDefaultSurface();
index d3aea26..e04f858 100644 (file)
@@ -157,12 +157,13 @@ static void getShaderSpec(TextureMapperShaderManager::ShaderKey key, String& ver
         STRINGIFY(
             uniform mat4 u_matrix;
             uniform lowp float u_flip;
+            uniform lowp vec2 u_textureSize;
             attribute vec4 a_vertex;
             varying highp vec2 v_sourceTexCoord;
             varying highp vec2 v_maskTexCoord;
             void main(void)
             {
-                v_sourceTexCoord = vec2(a_vertex.x, mix(a_vertex.y, 1. - a_vertex.y, u_flip));
+                v_sourceTexCoord = vec2(a_vertex.x, mix(a_vertex.y, 1. - a_vertex.y, u_flip)) * u_textureSize;
                 v_maskTexCoord = vec2(a_vertex);
                 gl_Position = u_matrix * a_vertex;
             }
@@ -242,12 +243,11 @@ static void getShaderSpec(TextureMapperShaderManager::ShaderKey key, String& ver
         STRINGIFY(
             precision mediump float;
             uniform sampler2DRect s_sampler;
-            uniform lowp vec2 u_samplerSize;
             uniform lowp float u_opacity;
             varying highp vec2 v_sourceTexCoord;
             void main(void)
             {
-                lowp vec4 color = texture2DRect(s_sampler, u_samplerSize * v_sourceTexCoord);
+                lowp vec4 color = texture2DRect(s_sampler, v_sourceTexCoord);
                 gl_FragColor = vec4(color.rgb * u_opacity, color.a * u_opacity);
             }
         );
@@ -256,11 +256,12 @@ static void getShaderSpec(TextureMapperShaderManager::ShaderKey key, String& ver
         STRINGIFY(
             uniform mat4 u_matrix;
             uniform lowp float u_flip;
+            uniform lowp vec2 u_textureSize;
             attribute vec4 a_vertex;
             varying highp vec2 v_sourceTexCoord;
             void main(void)
             {
-                v_sourceTexCoord = vec2(a_vertex.x, mix(a_vertex.y, 1. - a_vertex.y, u_flip));
+                v_sourceTexCoord = vec2(a_vertex.x, mix(a_vertex.y, 1. - a_vertex.y, u_flip)) * u_textureSize;
                 gl_Position = u_matrix * a_vertex;
             }
         );
index 496b7e8..60605f4 100644 (file)
@@ -51,7 +51,7 @@ public:
 
     TEXMAP_DECLARE_UNIFORM(matrix)
     TEXMAP_DECLARE_UNIFORM(flip)
-    TEXMAP_DECLARE_UNIFORM(samplerSize)
+    TEXMAP_DECLARE_UNIFORM(textureSize)
     TEXMAP_DECLARE_UNIFORM(opacity)
     TEXMAP_DECLARE_UNIFORM(color)
     TEXMAP_DECLARE_UNIFORM(expandedQuadEdgesInScreenSpace)