2 * Copyright (C) 2009 Apple 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "WebGLProgram.h"
32 #include "WebGLRenderingContext.h"
36 PassRefPtr<WebGLProgram> WebGLProgram::create(WebGLRenderingContext* ctx)
38 return adoptRef(new WebGLProgram(ctx));
41 WebGLProgram::WebGLProgram(WebGLRenderingContext* ctx)
46 setObject(context()->graphicsContext3D()->createProgram());
49 void WebGLProgram::deleteObjectImpl(Platform3DObject obj)
51 context()->graphicsContext3D()->deleteProgram(obj);
53 m_vertexShader->onDetached();
56 if (m_fragmentShader) {
57 m_fragmentShader->onDetached();
62 bool WebGLProgram::cacheActiveAttribLocations()
64 m_activeAttribLocations.clear();
67 GraphicsContext3D* context3d = context()->graphicsContext3D();
69 // Assume link status has already been cached.
73 GC3Dint numAttribs = 0;
74 context3d->getProgramiv(object(), GraphicsContext3D::ACTIVE_ATTRIBUTES, &numAttribs);
75 m_activeAttribLocations.resize(static_cast<size_t>(numAttribs));
76 for (int i = 0; i < numAttribs; ++i) {
78 context3d->getActiveAttrib(object(), i, info);
79 m_activeAttribLocations[i] = context3d->getAttribLocation(object(), info.name.charactersWithNullTermination());
85 unsigned WebGLProgram::numActiveAttribLocations() const
87 return m_activeAttribLocations.size();
90 GC3Dint WebGLProgram::getActiveAttribLocation(GC3Duint index) const
92 if (index >= numActiveAttribLocations())
94 return m_activeAttribLocations[index];
97 bool WebGLProgram::isUsingVertexAttrib0() const
99 for (unsigned ii = 0; ii < numActiveAttribLocations(); ++ii) {
100 if (!getActiveAttribLocation(ii))
106 WebGLShader* WebGLProgram::getAttachedShader(GC3Denum type)
109 case GraphicsContext3D::VERTEX_SHADER:
110 return m_vertexShader.get();
111 case GraphicsContext3D::FRAGMENT_SHADER:
112 return m_fragmentShader.get();
118 bool WebGLProgram::attachShader(WebGLShader* shader)
120 if (!shader || !shader->object())
122 switch (shader->getType()) {
123 case GraphicsContext3D::VERTEX_SHADER:
126 m_vertexShader = shader;
128 case GraphicsContext3D::FRAGMENT_SHADER:
129 if (m_fragmentShader)
131 m_fragmentShader = shader;
138 bool WebGLProgram::detachShader(WebGLShader* shader)
140 if (!shader || !shader->object())
142 switch (shader->getType()) {
143 case GraphicsContext3D::VERTEX_SHADER:
144 if (m_vertexShader != shader)
148 case GraphicsContext3D::FRAGMENT_SHADER:
149 if (m_fragmentShader != shader)
151 m_fragmentShader = 0;
160 #endif // ENABLE(WEBGL)