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.
28 #include "core/html/canvas/WebGLProgram.h"
30 #include "core/html/canvas/WebGLContextGroup.h"
31 #include "core/html/canvas/WebGLRenderingContextBase.h"
35 PassRefPtrWillBeRawPtr<WebGLProgram> WebGLProgram::create(WebGLRenderingContextBase* ctx)
37 return adoptRefWillBeNoop(new WebGLProgram(ctx));
40 WebGLProgram::WebGLProgram(WebGLRenderingContextBase* ctx)
41 : WebGLSharedObject(ctx)
46 ScriptWrappable::init(this);
47 setObject(ctx->webContext()->createProgram());
50 WebGLProgram::~WebGLProgram()
53 // These heap objects handle detachment on their own. Clear out
54 // the references to prevent deleteObjectImpl() from trying to do
55 // same, as we cannot safely access other heap objects from this
57 m_vertexShader = nullptr;
58 m_fragmentShader = nullptr;
60 // Always call detach here to ensure that platform object deletion
61 // happens with Oilpan enabled. It keeps the code regular to do it
62 // with or without Oilpan enabled.
64 // See comment in WebGLBuffer's destructor for additional
65 // information on why this is done for WebGLSharedObject-derived
67 detachAndDeleteObject();
70 void WebGLProgram::deleteObjectImpl(blink::WebGraphicsContext3D* context3d, Platform3DObject obj)
72 context3d->deleteProgram(obj);
74 m_vertexShader->onDetached(context3d);
75 m_vertexShader = nullptr;
77 if (m_fragmentShader) {
78 m_fragmentShader->onDetached(context3d);
79 m_fragmentShader = nullptr;
83 unsigned WebGLProgram::numActiveAttribLocations()
86 return m_activeAttribLocations.size();
89 GLint WebGLProgram::getActiveAttribLocation(GLuint index)
92 if (index >= numActiveAttribLocations())
94 return m_activeAttribLocations[index];
97 bool WebGLProgram::isUsingVertexAttrib0()
100 for (unsigned ii = 0; ii < numActiveAttribLocations(); ++ii) {
101 if (!getActiveAttribLocation(ii))
107 bool WebGLProgram::linkStatus()
113 void WebGLProgram::increaseLinkCount()
119 WebGLShader* WebGLProgram::getAttachedShader(GLenum type)
122 case GL_VERTEX_SHADER:
123 return m_vertexShader.get();
124 case GL_FRAGMENT_SHADER:
125 return m_fragmentShader.get();
131 bool WebGLProgram::attachShader(WebGLShader* shader)
133 if (!shader || !shader->object())
135 switch (shader->type()) {
136 case GL_VERTEX_SHADER:
139 m_vertexShader = shader;
141 case GL_FRAGMENT_SHADER:
142 if (m_fragmentShader)
144 m_fragmentShader = shader;
151 bool WebGLProgram::detachShader(WebGLShader* shader)
153 if (!shader || !shader->object())
155 switch (shader->type()) {
156 case GL_VERTEX_SHADER:
157 if (m_vertexShader != shader)
159 m_vertexShader = nullptr;
161 case GL_FRAGMENT_SHADER:
162 if (m_fragmentShader != shader)
164 m_fragmentShader = nullptr;
171 void WebGLProgram::cacheActiveAttribLocations(blink::WebGraphicsContext3D* context3d)
173 m_activeAttribLocations.clear();
175 GLint numAttribs = 0;
176 context3d->getProgramiv(object(), GL_ACTIVE_ATTRIBUTES, &numAttribs);
177 m_activeAttribLocations.resize(static_cast<size_t>(numAttribs));
178 for (int i = 0; i < numAttribs; ++i) {
179 blink::WebGraphicsContext3D::ActiveInfo info;
180 context3d->getActiveAttrib(object(), i, info);
181 m_activeAttribLocations[i] = context3d->getAttribLocation(object(), info.name.utf8().data());
185 void WebGLProgram::cacheInfoIfNeeded()
195 blink::WebGraphicsContext3D* context = contextGroup()->getAWebGraphicsContext3D();
198 GLint linkStatus = 0;
199 context->getProgramiv(object(), GL_LINK_STATUS, &linkStatus);
200 m_linkStatus = linkStatus;
202 cacheActiveAttribLocations(context);
206 void WebGLProgram::trace(Visitor* visitor)
208 visitor->trace(m_vertexShader);
209 visitor->trace(m_fragmentShader);
210 WebGLSharedObject::trace(visitor);