diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.cpp | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.cpp')
-rw-r--r-- | Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.cpp | 193 |
1 files changed, 98 insertions, 95 deletions
diff --git a/Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.cpp b/Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.cpp index 5c0a76db6..b0c371b99 100644 --- a/Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.cpp +++ b/Source/WebCore/platform/graphics/texmap/TextureMapperShaderProgram.cpp @@ -22,19 +22,12 @@ #include "config.h" #include "TextureMapperShaderProgram.h" -#if USE(ACCELERATED_COMPOSITING) && USE(TEXTURE_MAPPER) -#include "LengthFunctions.h" +#if USE(TEXTURE_MAPPER_GL) + #include "Logging.h" #include "TextureMapperGL.h" - #include <wtf/text/StringBuilder.h> -#if PLATFORM(WIN) || (PLATFORM(GTK) && OS(WINDOWS)) -#undef NO_ERROR -#endif - -#define STRINGIFY(...) #__VA_ARGS__ - namespace WebCore { static inline bool compositingLogEnabled() @@ -46,85 +39,21 @@ static inline bool compositingLogEnabled() #endif } -TextureMapperShaderProgram::TextureMapperShaderProgram(PassRefPtr<GraphicsContext3D> context, const String& vertex, const String& fragment) - : m_context(context) -{ - m_vertexShader = m_context->createShader(GraphicsContext3D::VERTEX_SHADER); - m_fragmentShader = m_context->createShader(GraphicsContext3D::FRAGMENT_SHADER); - m_context->shaderSource(m_vertexShader, vertex); - m_context->shaderSource(m_fragmentShader, fragment); - m_id = m_context->createProgram(); - m_context->compileShader(m_vertexShader); - m_context->compileShader(m_fragmentShader); - m_context->attachShader(m_id, m_vertexShader); - m_context->attachShader(m_id, m_fragmentShader); - m_context->linkProgram(m_id); - - if (!compositingLogEnabled()) - return; - - if (m_context->getError() == GraphicsContext3D::NO_ERROR) - return; - - String log = m_context->getShaderInfoLog(m_vertexShader); - LOG(Compositing, "Vertex shader log: %s\n", log.utf8().data()); - log = m_context->getShaderInfoLog(m_fragmentShader); - LOG(Compositing, "Fragment shader log: %s\n", log.utf8().data()); - log = m_context->getProgramInfoLog(m_id); - LOG(Compositing, "Program log: %s\n", log.utf8().data()); -} - -void TextureMapperShaderProgram::setMatrix(GC3Duint location, const TransformationMatrix& matrix) -{ - GC3Dfloat matrixAsFloats[] = { - GC3Dfloat(matrix.m11()), GC3Dfloat(matrix.m12()), GC3Dfloat(matrix.m13()), GC3Dfloat(matrix.m14()), - GC3Dfloat(matrix.m21()), GC3Dfloat(matrix.m22()), GC3Dfloat(matrix.m23()), GC3Dfloat(matrix.m24()), - GC3Dfloat(matrix.m31()), GC3Dfloat(matrix.m32()), GC3Dfloat(matrix.m33()), GC3Dfloat(matrix.m34()), - GC3Dfloat(matrix.m41()), GC3Dfloat(matrix.m42()), GC3Dfloat(matrix.m43()), GC3Dfloat(matrix.m44()) - }; - - m_context->uniformMatrix4fv(location, 1, false, matrixAsFloats); -} - -GC3Duint TextureMapperShaderProgram::getLocation(const AtomicString& name, VariableType type) -{ - HashMap<AtomicString, GC3Duint>::iterator it = m_variables.find(name); - if (it != m_variables.end()) - return it->value; - - GC3Duint location = 0; - switch (type) { - case UniformVariable: - location = m_context->getUniformLocation(m_id, name); - break; - case AttribVariable: - location = m_context->getAttribLocation(m_id, name); - break; - default: - ASSERT_NOT_REACHED(); - break; - } - - m_variables.add(name, location); - return location; -} +#define STRINGIFY(...) #__VA_ARGS__ -TextureMapperShaderProgram::~TextureMapperShaderProgram() -{ - Platform3DObject programID = m_id; - if (!programID) - return; +#define GLSL_DIRECTIVE(...) "#"#__VA_ARGS__"\n" - m_context->detachShader(programID, m_vertexShader); - m_context->deleteShader(m_vertexShader); - m_context->detachShader(programID, m_fragmentShader); - m_context->deleteShader(m_fragmentShader); - m_context->deleteProgram(programID); -} +#define TEXTURE_SPACE_MATRIX_PRECISION_DIRECTIVE \ + GLSL_DIRECTIVE(ifdef GL_FRAGMENT_PRECISION_HIGH) \ + GLSL_DIRECTIVE(define TextureSpaceMatrixPrecision highp) \ + GLSL_DIRECTIVE(else) \ + GLSL_DIRECTIVE(define TextureSpaceMatrixPrecision mediump) \ + GLSL_DIRECTIVE(endif) -#define GLSL_DIRECTIVE(...) "#"#__VA_ARGS__"\n" static const char* vertexTemplate = + TEXTURE_SPACE_MATRIX_PRECISION_DIRECTIVE STRINGIFY( + precision TextureSpaceMatrixPrecision float; attribute vec4 a_vertex; uniform mat4 u_modelViewMatrix; uniform mat4 u_projectionMatrix; @@ -211,7 +140,10 @@ static const char* fragmentTemplate = RECT_TEXTURE_DIRECTIVE ANTIALIASING_TEX_COORD_DIRECTIVE BLUR_CONSTANTS + TEXTURE_SPACE_MATRIX_PRECISION_DIRECTIVE STRINGIFY( + precision TextureSpaceMatrixPrecision float; + uniform mat4 u_textureSpaceMatrix; precision mediump float; uniform SamplerType s_sampler; uniform sampler2D s_contentTexture; @@ -224,10 +156,10 @@ static const char* fragmentTemplate = uniform vec2 u_shadowOffset; uniform vec4 u_color; uniform float u_gaussianKernel[GAUSSIAN_KERNEL_HALF_WIDTH]; - uniform mat4 u_textureSpaceMatrix; void noop(inout vec4 dummyParameter) { } void noop(inout vec4 dummyParameter, vec2 texCoord) { } + void noop(inout vec2 dummyParameter) { } float antialias() { return smoothstep(0., 1., v_antialias); } @@ -239,6 +171,8 @@ static const char* fragmentTemplate = vec2 vertexTransformTexCoord() { return v_transformedTexCoord; } + void applyManualRepeat(inout vec2 pos) { pos = fract(pos); } + void applyTexture(inout vec4 color, vec2 texCoord) { color = SamplerFunction(s_sampler, texCoord); } void applyOpacity(inout vec4 color) { color *= u_opacity; } void applyAntialiasing(inout vec4 color) { color *= antialias(); } @@ -350,6 +284,7 @@ static const char* fragmentTemplate = { vec4 color = vec4(1., 1., 1., 1.); vec2 texCoord = transformTexCoord(); + applyManualRepeatIfNeeded(texCoord); applyTextureIfNeeded(color, texCoord); applySolidColorIfNeeded(color); applyAntialiasingIfNeeded(color); @@ -369,13 +304,13 @@ static const char* fragmentTemplate = } ); -PassRefPtr<TextureMapperShaderProgram> TextureMapperShaderProgram::create(PassRefPtr<GraphicsContext3D> context, TextureMapperShaderProgram::Options options) +Ref<TextureMapperShaderProgram> TextureMapperShaderProgram::create(Ref<GraphicsContext3D>&& context, TextureMapperShaderProgram::Options options) { - StringBuilder shaderBuilder; #define SET_APPLIER_FROM_OPTIONS(Applier) \ - shaderBuilder.append(\ + optionsApplierBuilder.append(\ (options & TextureMapperShaderProgram::Applier) ? ENABLE_APPLIER(Applier) : DISABLE_APPLIER(Applier)) + StringBuilder optionsApplierBuilder; SET_APPLIER_FROM_OPTIONS(Texture); SET_APPLIER_FROM_OPTIONS(Rect); SET_APPLIER_FROM_OPTIONS(SolidColor); @@ -392,16 +327,84 @@ PassRefPtr<TextureMapperShaderProgram> TextureMapperShaderProgram::create(PassRe SET_APPLIER_FROM_OPTIONS(BlurFilter); SET_APPLIER_FROM_OPTIONS(AlphaBlur); SET_APPLIER_FROM_OPTIONS(ContentTexture); - StringBuilder vertexBuilder; - vertexBuilder.append(shaderBuilder.toString()); - vertexBuilder.append(vertexTemplate); - shaderBuilder.append(fragmentTemplate); + SET_APPLIER_FROM_OPTIONS(ManualRepeat); + + StringBuilder vertexShaderBuilder; + vertexShaderBuilder.append(optionsApplierBuilder.toString()); + vertexShaderBuilder.append(vertexTemplate); + + StringBuilder fragmentShaderBuilder; + fragmentShaderBuilder.append(optionsApplierBuilder.toString()); + fragmentShaderBuilder.append(fragmentTemplate); + + return adoptRef(*new TextureMapperShaderProgram(WTFMove(context), vertexShaderBuilder.toString(), fragmentShaderBuilder.toString())); +} + +TextureMapperShaderProgram::TextureMapperShaderProgram(Ref<GraphicsContext3D>&& context, const String& vertex, const String& fragment) + : m_context(WTFMove(context)) +{ + m_vertexShader = m_context->createShader(GraphicsContext3D::VERTEX_SHADER); + m_context->shaderSource(m_vertexShader, vertex); + + m_fragmentShader = m_context->createShader(GraphicsContext3D::FRAGMENT_SHADER); + m_context->shaderSource(m_fragmentShader, fragment); + + m_id = m_context->createProgram(); + m_context->compileShader(m_vertexShader); + m_context->compileShader(m_fragmentShader); + m_context->attachShader(m_id, m_vertexShader); + m_context->attachShader(m_id, m_fragmentShader); + m_context->linkProgram(m_id); + + if (!compositingLogEnabled()) + return; + + if (m_context->getError() == GraphicsContext3D::NO_ERROR) + return; - String vertexSource = vertexBuilder.toString(); - String fragmentSource = shaderBuilder.toString(); + String log = m_context->getShaderInfoLog(m_vertexShader); + LOG(Compositing, "Vertex shader log: %s\n", log.utf8().data()); + log = m_context->getShaderInfoLog(m_fragmentShader); + LOG(Compositing, "Fragment shader log: %s\n", log.utf8().data()); + log = m_context->getProgramInfoLog(m_id); + LOG(Compositing, "Program log: %s\n", log.utf8().data()); +} + +TextureMapperShaderProgram::~TextureMapperShaderProgram() +{ + if (!m_id) + return; - return adoptRef(new TextureMapperShaderProgram(context, vertexSource, fragmentSource)); + m_context->detachShader(m_id, m_vertexShader); + m_context->deleteShader(m_vertexShader); + m_context->detachShader(m_id, m_fragmentShader); + m_context->deleteShader(m_fragmentShader); + m_context->deleteProgram(m_id); } +void TextureMapperShaderProgram::setMatrix(GC3Duint location, const TransformationMatrix& matrix) +{ + TransformationMatrix::FloatMatrix4 floatMatrix; + matrix.toColumnMajorFloatArray(floatMatrix); + m_context->uniformMatrix4fv(location, 1, false, floatMatrix); } -#endif + +GC3Duint TextureMapperShaderProgram::getLocation(const AtomicString& name, VariableType type) +{ + auto addResult = m_variables.ensure(name, + [this, &name, type] { + switch (type) { + case UniformVariable: + return m_context->getUniformLocation(m_id, name); + case AttribVariable: + return m_context->getAttribLocation(m_id, name); + } + ASSERT_NOT_REACHED(); + return 0; + }); + return addResult.iterator->value; +} + +} // namespace WebCore + +#endif // USE(TEXTURE_MAPPER_GL) |