Skip to content

Commit

Permalink
[TextureMapper] Do texture transformation in vertex shader when copyi…
Browse files Browse the repository at this point in the history
…ng texture

https://bugs.webkit.org/show_bug.cgi?id=261187

Reviewed by Fujii Hironori.

With the current implementation, transformation of texture coordinate
is performed in a fragment shader when copying texture or applying blur.

This change will move the transformation to vertex shader for better
performance.

* Source/WebCore/platform/graphics/texmap/TextureMapperGL.cpp:
(WebCore::TextureMapperGL::drawTextureCopy):
(WebCore::TextureMapperGL::drawBlurred):
* Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.cpp:
* Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.h:

Canonical link: https://commits.webkit.org/267687@main
  • Loading branch information
Akihiro Kiuchi authored and fujii committed Sep 6, 2023
1 parent 09fd886 commit 14f7f33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
31 changes: 21 additions & 10 deletions Source/WebCore/platform/graphics/texmap/TextureMapperGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ void TextureMapperGL::drawTexturedQuadWithProgram(TextureMapperShaderProgram& pr
void TextureMapperGL::drawTextureCopy(const BitmapTexture& sourceTexture, const FloatRect& sourceRect, const FloatRect& targetRect)
{
Ref<TextureMapperShaderProgram> program = data().getShaderProgram({ TextureMapperShaderProgram::TextureCopy });
IntSize textureSize = sourceTexture.contentSize();

glUseProgram(program->programID());

Expand All @@ -809,11 +810,15 @@ void TextureMapperGL::drawTextureCopy(const BitmapTexture& sourceTexture, const
0
);

program->setMatrix(program->textureCopyMatrixLocation(), textureCopyMatrix);
program->setMatrix(program->textureSpaceMatrixLocation(), textureCopyMatrix);

glUniform2f(program->texelSizeLocation(), 1.f / sourceRect.width(), 1.f / sourceRect.height());
glUniform2f(program->texelSizeLocation(), 1.f / textureSize.width(), 1.f / textureSize.height());

drawTexturedQuadWithProgram(program.get(), static_cast<const BitmapTextureGL&>(sourceTexture).id(), 0, targetRect, TransformationMatrix(), 1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, static_cast<const BitmapTextureGL&>(sourceTexture).id());
glUniform1i(program->samplerLocation(), 0);

draw(targetRect, TransformationMatrix(), program.get(), GL_TRIANGLE_FAN, 0);
}

void TextureMapperGL::drawBlurred(const BitmapTexture& sourceTexture, const FloatRect& rect, float radius, Direction direction, bool alphaBlur)
Expand All @@ -822,9 +827,11 @@ void TextureMapperGL::drawBlurred(const BitmapTexture& sourceTexture, const Floa
alphaBlur ? TextureMapperShaderProgram::AlphaBlur : TextureMapperShaderProgram::BlurFilter,
});

IntSize textureSize = sourceTexture.contentSize();

glUseProgram(program->programID());

glUniform2f(program->texelSizeLocation(), 1.f / rect.width(), 1.f / rect.height());
glUniform2f(program->texelSizeLocation(), 1.f / textureSize.width(), 1.f / textureSize.height());

auto directionVector = direction == Direction::X ? FloatPoint(1, 0) : FloatPoint(0, 1);
glUniform2f(program->blurDirectionLocation(), directionVector.x(), directionVector.y());
Expand All @@ -837,18 +844,22 @@ void TextureMapperGL::drawBlurred(const BitmapTexture& sourceTexture, const Floa
auto textureBlurMatrix = TransformationMatrix::identity;

textureBlurMatrix.scale3d(
double(rect.width()) / sourceTexture.contentSize().width(),
double(rect.height()) / sourceTexture.contentSize().height(),
double(rect.width()) / textureSize.width(),
double(rect.height()) / textureSize.height(),
1
).translate3d(
double(rect.x()) / sourceTexture.contentSize().width(),
double(rect.y()) / sourceTexture.contentSize().height(),
double(rect.x()) / textureSize.width(),
double(rect.y()) / textureSize.height(),
0
);

program->setMatrix(program->textureBlurMatrixLocation(), textureBlurMatrix);
program->setMatrix(program->textureSpaceMatrixLocation(), textureBlurMatrix);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, static_cast<const BitmapTextureGL&>(sourceTexture).id());
glUniform1i(program->samplerLocation(), 0);

drawTexturedQuadWithProgram(program.get(), static_cast<const BitmapTextureGL&>(sourceTexture).id(), 0, rect, TransformationMatrix(), 1);
draw(rect, TransformationMatrix(), program.get(), GL_TRIANGLE_FAN, 0);
}

RefPtr<BitmapTexture> TextureMapperGL::applyBlurFilter(RefPtr<BitmapTexture> sourceTexture, const BlurFilterOperation& blurFilter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ static const char* fragmentTemplateCommon =
uniform int u_roundedRectNumber;
uniform vec4 u_roundedRect[ROUNDED_RECT_ARRAY_SIZE];
uniform mat4 u_roundedRectInverseTransformMatrix[ROUNDED_RECT_INVERSE_TRANSFORM_ARRAY_SIZE];
uniform mat4 u_textureCopyMatrix;
uniform mat4 u_textureBlurMatrix;

void noop(inout vec4 dummyParameter) { }
void noop(inout vec4 dummyParameter, vec2 texCoord) { }
Expand Down Expand Up @@ -348,40 +346,43 @@ static const char* fragmentTemplateCommon =

void applyTextureCopy(inout vec4 color, vec2 texCoord)
{
vec2 coord = clamp(texCoord, u_texelSize / 2., vec2(1., 1.) - u_texelSize / 2.);
coord = (u_textureCopyMatrix * vec4(coord, 0., 1.)).xy;
color = texture2D(s_sampler, coord);
}
vec2 min = (u_textureSpaceMatrix * vec4(0., 0., 0., 1.)).xy + u_texelSize / 2.;
vec2 max = (u_textureSpaceMatrix * vec4(1., 1., 0., 1.)).xy - u_texelSize / 2.;

vec4 sampleTextureClamp(sampler2D sampler, vec2 texCoord, vec2 offset, mat4 matrix)
{
vec2 coord = texCoord + offset;
coord = clamp(coord, u_texelSize / 2., vec2(1., 1.) - u_texelSize / 2.);
coord = (matrix * vec4(coord, 0., 1.)).xy;
return texture2D(sampler, coord);
vec2 coord = clamp(texCoord, min, max);

color = texture2D(s_sampler, coord);
}

void applyBlurFilter(inout vec4 color, vec2 texCoord)
{
vec4 total = sampleTextureClamp(s_sampler, texCoord, vec2(0., 0.), u_textureBlurMatrix) * u_gaussianKernel[0];
vec2 step = u_blurDirection * u_texelSize;
vec2 min = (u_textureSpaceMatrix * vec4(0., 0., 0., 1.)).xy + u_texelSize / 2.;
vec2 max = (u_textureSpaceMatrix * vec4(1., 1., 0., 1.)).xy - u_texelSize / 2.;

vec4 total = texture2D(s_sampler, texCoord) * u_gaussianKernel[0];

for (int i = 1; i < u_gaussianKernelHalfSize; i++) {
vec2 offset = u_blurDirection * u_texelSize * float(i);
total += sampleTextureClamp(s_sampler, texCoord, +offset, u_textureBlurMatrix) * u_gaussianKernel[i];
total += sampleTextureClamp(s_sampler, texCoord, -offset, u_textureBlurMatrix) * u_gaussianKernel[i];
vec2 offset = step * float(i);
total += texture2D(s_sampler, clamp(texCoord + offset, min, max)) * u_gaussianKernel[i];
total += texture2D(s_sampler, clamp(texCoord - offset, min, max)) * u_gaussianKernel[i];
}

color = total;
}

void applyAlphaBlur(inout vec4 color, vec2 texCoord)
{
float total = sampleTextureClamp(s_sampler, texCoord, vec2(0., 0.), u_textureBlurMatrix).a * u_gaussianKernel[0];
vec2 step = u_blurDirection * u_texelSize;
vec2 min = (u_textureSpaceMatrix * vec4(0., 0., 0., 1.)).xy + u_texelSize / 2.;
vec2 max = (u_textureSpaceMatrix * vec4(1., 1., 0., 1.)).xy - u_texelSize / 2.;

float total = texture2D(s_sampler, texCoord).a * u_gaussianKernel[0];

for (int i = 1; i < u_gaussianKernelHalfSize; i++) {
vec2 offset = u_blurDirection * u_texelSize * float(i);
total += sampleTextureClamp(s_sampler, texCoord, +offset, u_textureBlurMatrix).a * u_gaussianKernel[i];
total += sampleTextureClamp(s_sampler, texCoord, -offset, u_textureBlurMatrix).a * u_gaussianKernel[i];
vec2 offset = step * float(i);
total += texture2D(s_sampler, clamp(texCoord + offset, min, max)).a * u_gaussianKernel[i];
total += texture2D(s_sampler, clamp(texCoord - offset, min, max)).a * u_gaussianKernel[i];
}

color = vec4(0., 0., 0., total);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ namespace WebCore {
macro(blurDirection) \
macro(roundedRectNumber) \
macro(roundedRect) \
macro(roundedRectInverseTransformMatrix) \
macro(textureCopyMatrix) \
macro(textureBlurMatrix)
macro(roundedRectInverseTransformMatrix)

#define TEXMAP_SAMPLER_VARIABLES(macro) \
macro(sampler) \
Expand Down

0 comments on commit 14f7f33

Please sign in to comment.