2 * Copyright (C) 2011 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
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #if USE(ACCELERATED_COMPOSITING)
30 #include "ProgramBinding.h"
32 #include "GeometryBinding.h"
33 #include "GraphicsContext.h"
34 #include "GraphicsContext3D.h"
35 #include "LayerRendererChromium.h"
36 #include "TraceEvent.h"
40 ProgramBindingBase::ProgramBindingBase()
42 , m_initialized(false)
46 ProgramBindingBase::~ProgramBindingBase()
48 // If you hit these asserts, you initialized but forgot to call cleanup().
50 ASSERT(!m_initialized);
53 void ProgramBindingBase::init(GraphicsContext3D* context, const String& vertexShader, const String& fragmentShader)
55 m_program = createShaderProgram(context, vertexShader, fragmentShader);
59 void ProgramBindingBase::cleanup(GraphicsContext3D* context)
61 m_initialized = false;
66 GLC(context, context->deleteProgram(m_program));
70 unsigned ProgramBindingBase::loadShader(GraphicsContext3D* context, unsigned type, const String& shaderSource)
72 unsigned shader = context->createShader(type);
75 String sourceString(shaderSource);
76 GLC(context, context->shaderSource(shader, sourceString));
77 GLC(context, context->compileShader(shader));
80 GLC(context, context->getShaderiv(shader, GraphicsContext3D::COMPILE_STATUS, &compiled));
82 GLC(context, context->deleteShader(shader));
89 unsigned ProgramBindingBase::createShaderProgram(GraphicsContext3D* context, const String& vertexShaderSource, const String& fragmentShaderSource)
91 TRACE_EVENT("ProgramBindingBase::createShaderProgram", this, 0);
92 unsigned vertexShader = loadShader(context, GraphicsContext3D::VERTEX_SHADER, vertexShaderSource);
94 LOG_ERROR("Failed to create vertex shader");
98 unsigned fragmentShader = loadShader(context, GraphicsContext3D::FRAGMENT_SHADER, fragmentShaderSource);
99 if (!fragmentShader) {
100 GLC(context, context->deleteShader(vertexShader));
101 LOG_ERROR("Failed to create fragment shader");
105 unsigned programObject = context->createProgram();
106 if (!programObject) {
107 LOG_ERROR("Failed to create shader program");
111 GLC(context, context->attachShader(programObject, vertexShader));
112 GLC(context, context->attachShader(programObject, fragmentShader));
114 // Bind the common attrib locations.
115 GLC(context, context->bindAttribLocation(programObject, GeometryBinding::positionAttribLocation(), "a_position"));
116 GLC(context, context->bindAttribLocation(programObject, GeometryBinding::texCoordAttribLocation(), "a_texCoord"));
118 GLC(context, context->linkProgram(programObject));
121 GLC(context, context->getProgramiv(programObject, GraphicsContext3D::LINK_STATUS, &linked));
123 LOG_ERROR("Failed to link shader program");
124 GLC(context, context->deleteProgram(programObject));
129 GLC(context, context->deleteShader(vertexShader));
130 GLC(context, context->deleteShader(fragmentShader));
131 return programObject;
134 } // namespace WebCore
136 #endif // USE(ACCELERATED_COMPOSITING)