2 * Copyright (c) 2010, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #if ENABLE(ACCELERATED_2D_CANVAS)
37 #include "AffineTransform.h"
38 #include "GraphicsContext3D.h"
40 #include <wtf/text/CString.h>
41 #include <wtf/text/StringBuilder.h>
46 void Shader::affineTo3x3(const AffineTransform& transform, float mat[9])
48 mat[0] = transform.a();
49 mat[1] = transform.b();
51 mat[3] = transform.c();
52 mat[4] = transform.d();
54 mat[6] = transform.e();
55 mat[7] = transform.f();
60 void Shader::affineTo4x4(const AffineTransform& transform, float mat[16])
62 mat[0] = transform.a();
63 mat[1] = transform.b();
66 mat[4] = transform.c();
67 mat[5] = transform.d();
74 mat[12] = transform.e();
75 mat[13] = transform.f();
81 unsigned Shader::loadShader(GraphicsContext3D* context, unsigned type, const String& shaderSource)
83 unsigned shader = context->createShader(type);
87 context->shaderSource(shader, shaderSource);
88 context->compileShader(shader);
89 int compileStatus = 0;
90 context->getShaderiv(shader, GraphicsContext3D::COMPILE_STATUS, &compileStatus);
92 String infoLog = context->getShaderInfoLog(shader);
93 LOG_ERROR("%s", infoLog.utf8().data());
94 context->deleteShader(shader);
101 unsigned Shader::loadProgram(GraphicsContext3D* context, const String& vertexShaderSource, const String& fragmentShaderSource)
103 unsigned vertexShader = loadShader(context, GraphicsContext3D::VERTEX_SHADER, vertexShaderSource);
106 unsigned fragmentShader = loadShader(context, GraphicsContext3D::FRAGMENT_SHADER, fragmentShaderSource);
109 unsigned program = context->createProgram();
112 context->attachShader(program, vertexShader);
113 context->attachShader(program, fragmentShader);
114 context->linkProgram(program);
116 context->getProgramiv(program, GraphicsContext3D::LINK_STATUS, &linkStatus);
118 context->deleteProgram(program);
119 context->deleteShader(vertexShader);
120 context->deleteShader(fragmentShader);
124 Shader::Shader(GraphicsContext3D* context, unsigned program)
132 m_context->deleteProgram(m_program);
136 String Shader::generateVertex(Shader::VertexType vertexType, Shader::FillType fillType)
138 StringBuilder builder;
139 switch (vertexType) {
142 "uniform mat3 matrix;\n"
143 "attribute vec2 position;\n");
145 case LoopBlinnInterior:
147 "uniform mat4 worldViewProjection;\n"
148 "attribute vec2 position;\n");
150 case LoopBlinnExterior:
152 "uniform mat4 worldViewProjection;\n"
153 "attribute vec2 position;\n"
154 "attribute vec3 klm;\n"
155 "varying vec3 v_klm;\n");
159 if (fillType == TextureFill) {
161 "uniform mat3 texMatrix;\n"
162 "varying vec3 texCoord;\n");
168 if (vertexType == TwoDimensional) {
170 "gl_Position = vec4(matrix * vec3(position, 1.0), 1.0);\n");
173 "gl_Position = worldViewProjection * vec4(position, 0.0, 1.0);\n");
174 if (vertexType == LoopBlinnExterior) {
180 if (fillType == TextureFill) {
182 "texCoord = texMatrix * vec3(position, 1.0);\n");
188 return builder.toString();
192 String Shader::generateFragment(Shader::VertexType vertexType, Shader::FillType fillType, Shader::AntialiasType antialiasType)
194 StringBuilder builder;
197 "precision mediump float;\n"
200 if (vertexType == LoopBlinnExterior) {
201 if (antialiasType == Antialiased) {
203 "#extension GL_OES_standard_derivatives : enable\n");
206 "varying vec3 v_klm;\n");
212 "uniform vec4 color;\n");
216 "uniform sampler2D sampler;\n"
217 "uniform float globalAlpha;\n"
218 "varying vec3 texCoord;\n");
225 if (vertexType != LoopBlinnExterior) {
227 "float alpha = 1.0;\n");
229 if (antialiasType == Antialiased) {
232 " vec3 px = dFdx(v_klm);\n"
233 " vec3 py = dFdy(v_klm);\n"
236 " float k2 = v_klm.x * v_klm.x;\n"
237 " float c = k2 * v_klm.x - v_klm.y * v_klm.z;\n"
238 " float k23 = 3.0 * k2;\n"
239 " float cx = k23 * px.x - v_klm.z * px.y - v_klm.y * px.z;\n"
240 " float cy = k23 * py.x - v_klm.z * py.y - v_klm.y * py.z;\n"
242 " // Signed distance\n"
243 " float sd = c / sqrt(cx * cx + cy * cy);\n"
246 " // FIXME: figure out why this needs to be\n"
247 " // negated compared to the HLSL version, and also why\n"
248 " // we need an adjustment by +1.0 for it to look good.\n"
249 " // float alpha = clamp(0.5 - sd, 0.0, 1.0);\n"
250 " float alpha = clamp(sd + 0.5, 0.0, 1.0);\n");
253 " float t = v_klm.x * v_klm.x * v_klm.x - v_klm.y * v_klm.z;\n"
254 " float alpha = clamp(sign(t), 0.0, 1.0);\n");
261 "gl_FragColor = color * alpha;\n");
265 "gl_FragColor = texture2D(sampler, texCoord.xy) * alpha * globalAlpha;\n");
272 return builder.toString();
275 } // namespace WebCore