• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
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.
13  *
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.
24  */
25 
26 #include "config.h"
27 
28 #if USE(ACCELERATED_COMPOSITING)
29 
30 #include "ProgramBinding.h"
31 
32 #include "GeometryBinding.h"
33 #include "GraphicsContext.h"
34 #include "GraphicsContext3D.h"
35 #include "LayerRendererChromium.h"
36 
37 namespace WebCore {
38 
ProgramBindingBase(GraphicsContext3D * context)39 ProgramBindingBase::ProgramBindingBase(GraphicsContext3D* context)
40     : m_context(context)
41     , m_program(0)
42     , m_initialized(false)
43 {
44 }
45 
~ProgramBindingBase()46 ProgramBindingBase::~ProgramBindingBase()
47 {
48     if (m_program)
49         GLC(m_context, m_context->deleteProgram(m_program));
50 }
51 
init(const String & vertexShader,const String & fragmentShader)52 bool ProgramBindingBase::init(const String& vertexShader, const String& fragmentShader)
53 {
54     m_program = createShaderProgram(vertexShader, fragmentShader);
55     if (!m_program) {
56         LOG_ERROR("Failed to create shader program");
57         return false;
58     }
59     return true;
60 }
61 
loadShader(unsigned type,const String & shaderSource)62 unsigned ProgramBindingBase::loadShader(unsigned type, const String& shaderSource)
63 {
64     unsigned shader = m_context->createShader(type);
65     if (!shader)
66         return 0;
67     String sourceString(shaderSource);
68     GLC(m_context, m_context->shaderSource(shader, sourceString));
69     GLC(m_context, m_context->compileShader(shader));
70     int compiled = 0;
71     GLC(m_context, m_context->getShaderiv(shader, GraphicsContext3D::COMPILE_STATUS, &compiled));
72     if (!compiled) {
73         GLC(m_context, m_context->deleteShader(shader));
74         return 0;
75     }
76     return shader;
77 }
78 
createShaderProgram(const String & vertexShaderSource,const String & fragmentShaderSource)79 unsigned ProgramBindingBase::createShaderProgram(const String& vertexShaderSource, const String& fragmentShaderSource)
80 {
81     unsigned vertexShader = loadShader(GraphicsContext3D::VERTEX_SHADER, vertexShaderSource);
82     if (!vertexShader) {
83         LOG_ERROR("Failed to create vertex shader");
84         return 0;
85     }
86 
87     unsigned fragmentShader = loadShader(GraphicsContext3D::FRAGMENT_SHADER, fragmentShaderSource);
88     if (!fragmentShader) {
89         GLC(m_context, m_context->deleteShader(vertexShader));
90         LOG_ERROR("Failed to create fragment shader");
91         return 0;
92     }
93 
94     unsigned programObject = m_context->createProgram();
95     if (!programObject) {
96         LOG_ERROR("Failed to create shader program");
97         return 0;
98     }
99 
100     GLC(m_context, m_context->attachShader(programObject, vertexShader));
101     GLC(m_context, m_context->attachShader(programObject, fragmentShader));
102 
103     // Bind the common attrib locations.
104     GLC(m_context, m_context->bindAttribLocation(programObject, GeometryBinding::positionAttribLocation(), "a_position"));
105     GLC(m_context, m_context->bindAttribLocation(programObject, GeometryBinding::texCoordAttribLocation(), "a_texCoord"));
106 
107     GLC(m_context, m_context->linkProgram(programObject));
108     int linked = 0;
109     GLC(m_context, m_context->getProgramiv(programObject, GraphicsContext3D::LINK_STATUS, &linked));
110     if (!linked) {
111         LOG_ERROR("Failed to link shader program");
112         GLC(m_context, m_context->deleteProgram(programObject));
113         return 0;
114     }
115 
116     GLC(m_context, m_context->deleteShader(vertexShader));
117     GLC(m_context, m_context->deleteShader(fragmentShader));
118     return programObject;
119 }
120 
121 } // namespace WebCore
122 
123 #endif // USE(ACCELERATED_COMPOSITING)
124