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 "WebGLFramebuffer.h"
32 #include "WebGLRenderingContext.h"
38 // This function is only for depth/stencil/depth_stencil attachment.
39 // Currently we assume these attachments are all renderbuffers.
40 GC3Denum getInternalFormat(WebGLObject* buffer)
42 ASSERT(buffer && buffer->isRenderbuffer());
43 return (reinterpret_cast<WebGLRenderbuffer*>(buffer))->getInternalFormat();
46 bool isUninitialized(WebGLObject* attachedObject)
48 if (attachedObject && attachedObject->object() && attachedObject->isRenderbuffer()
49 && !(reinterpret_cast<WebGLRenderbuffer*>(attachedObject))->isInitialized())
54 void setInitialized(WebGLObject* attachedObject)
56 if (attachedObject && attachedObject->object() && attachedObject->isRenderbuffer())
57 (reinterpret_cast<WebGLRenderbuffer*>(attachedObject))->setInitialized();
60 bool isValid(WebGLObject* attachedObject)
62 if (attachedObject && attachedObject->object() && attachedObject->isRenderbuffer()) {
63 if (!(reinterpret_cast<WebGLRenderbuffer*>(attachedObject))->isValid())
69 } // anonymous namespace
71 PassRefPtr<WebGLFramebuffer> WebGLFramebuffer::create(WebGLRenderingContext* ctx)
73 return adoptRef(new WebGLFramebuffer(ctx));
76 WebGLFramebuffer::WebGLFramebuffer(WebGLRenderingContext* ctx)
78 , m_hasEverBeenBound(false)
82 setObject(context()->graphicsContext3D()->createFramebuffer());
85 void WebGLFramebuffer::setAttachment(GC3Denum attachment, GC3Denum texTarget, WebGLTexture* texture, GC3Dint level)
89 if (texture && !texture->object())
92 case GraphicsContext3D::COLOR_ATTACHMENT0:
93 m_colorAttachment = texture;
95 m_texTarget = texTarget;
99 case GraphicsContext3D::DEPTH_ATTACHMENT:
100 m_depthAttachment = texture;
102 case GraphicsContext3D::STENCIL_ATTACHMENT:
103 m_stencilAttachment = texture;
105 case GraphicsContext3D::DEPTH_STENCIL_ATTACHMENT:
106 m_depthStencilAttachment = texture;
113 void WebGLFramebuffer::setAttachment(GC3Denum attachment, WebGLRenderbuffer* renderbuffer)
117 if (renderbuffer && !renderbuffer->object())
119 switch (attachment) {
120 case GraphicsContext3D::COLOR_ATTACHMENT0:
121 m_colorAttachment = renderbuffer;
123 case GraphicsContext3D::DEPTH_ATTACHMENT:
124 m_depthAttachment = renderbuffer;
126 case GraphicsContext3D::STENCIL_ATTACHMENT:
127 m_stencilAttachment = renderbuffer;
129 case GraphicsContext3D::DEPTH_STENCIL_ATTACHMENT:
130 m_depthStencilAttachment = renderbuffer;
137 WebGLObject* WebGLFramebuffer::getAttachment(GC3Denum attachment) const
141 switch (attachment) {
142 case GraphicsContext3D::COLOR_ATTACHMENT0:
143 return m_colorAttachment.get();
144 case GraphicsContext3D::DEPTH_ATTACHMENT:
145 return m_depthAttachment.get();
146 case GraphicsContext3D::STENCIL_ATTACHMENT:
147 return m_stencilAttachment.get();
148 case GraphicsContext3D::DEPTH_STENCIL_ATTACHMENT:
149 return m_depthStencilAttachment.get();
155 void WebGLFramebuffer::removeAttachment(WebGLObject* attachment)
159 if (attachment == m_colorAttachment.get())
160 m_colorAttachment = 0;
161 else if (attachment == m_depthAttachment.get())
162 m_depthAttachment = 0;
163 else if (attachment == m_stencilAttachment.get())
164 m_stencilAttachment = 0;
165 else if (attachment == m_depthStencilAttachment.get())
166 m_depthStencilAttachment = 0;
171 GC3Dsizei WebGLFramebuffer::getWidth() const
173 if (!object() || !isColorAttached())
175 if (m_colorAttachment->isRenderbuffer())
176 return (reinterpret_cast<WebGLRenderbuffer*>(m_colorAttachment.get()))->getWidth();
177 if (m_colorAttachment->isTexture())
178 return (reinterpret_cast<WebGLTexture*>(m_colorAttachment.get()))->getWidth(m_texTarget, m_texLevel);
179 ASSERT_NOT_REACHED();
183 GC3Dsizei WebGLFramebuffer::getHeight() const
185 if (!object() || !isColorAttached())
187 if (m_colorAttachment->isRenderbuffer())
188 return (reinterpret_cast<WebGLRenderbuffer*>(m_colorAttachment.get()))->getHeight();
189 if (m_colorAttachment->isTexture())
190 return (reinterpret_cast<WebGLTexture*>(m_colorAttachment.get()))->getHeight(m_texTarget, m_texLevel);
191 ASSERT_NOT_REACHED();
195 GC3Denum WebGLFramebuffer::getColorBufferFormat() const
197 if (!object() || !isColorAttached())
199 if (m_colorAttachment->isRenderbuffer()) {
200 unsigned long format = (reinterpret_cast<WebGLRenderbuffer*>(m_colorAttachment.get()))->getInternalFormat();
202 case GraphicsContext3D::RGBA4:
203 case GraphicsContext3D::RGB5_A1:
204 return GraphicsContext3D::RGBA;
205 case GraphicsContext3D::RGB565:
206 return GraphicsContext3D::RGB;
210 if (m_colorAttachment->isTexture())
211 return (reinterpret_cast<WebGLTexture*>(m_colorAttachment.get()))->getInternalFormat(m_texTarget, m_texLevel);
212 ASSERT_NOT_REACHED();
216 bool WebGLFramebuffer::isIncomplete(bool checkInternalFormat) const
218 unsigned int count = 0;
219 if (isDepthAttached()) {
220 if (checkInternalFormat && getInternalFormat(m_depthAttachment.get()) != GraphicsContext3D::DEPTH_COMPONENT16)
224 if (isStencilAttached()) {
225 if (checkInternalFormat && getInternalFormat(m_stencilAttachment.get()) != GraphicsContext3D::STENCIL_INDEX8)
229 if (isDepthStencilAttached()) {
230 if (checkInternalFormat && getInternalFormat(m_depthStencilAttachment.get()) != GraphicsContext3D::DEPTH_STENCIL)
232 if (!isValid(m_depthStencilAttachment.get()))
241 bool WebGLFramebuffer::onAccess(bool needToInitializeRenderbuffers)
243 if (isIncomplete(true))
245 if (needToInitializeRenderbuffers)
246 return initializeRenderbuffers();
250 void WebGLFramebuffer::deleteObjectImpl(Platform3DObject object)
252 context()->graphicsContext3D()->deleteFramebuffer(object);
253 m_colorAttachment = 0;
254 m_depthAttachment = 0;
255 m_stencilAttachment = 0;
256 m_depthStencilAttachment = 0;
259 bool WebGLFramebuffer::initializeRenderbuffers()
262 bool initColor = false, initDepth = false, initStencil = false;
263 GC3Dbitfield mask = 0;
264 if (isUninitialized(m_colorAttachment.get())) {
266 mask |= GraphicsContext3D::COLOR_BUFFER_BIT;
268 if (isUninitialized(m_depthAttachment.get())) {
270 mask |= GraphicsContext3D::DEPTH_BUFFER_BIT;
272 if (isUninitialized(m_stencilAttachment.get())) {
274 mask |= GraphicsContext3D::STENCIL_BUFFER_BIT;
276 if (isUninitialized(m_depthStencilAttachment.get())) {
279 mask |= (GraphicsContext3D::DEPTH_BUFFER_BIT | GraphicsContext3D::STENCIL_BUFFER_BIT);
281 if (!initColor && !initDepth && !initStencil)
284 // We only clear un-initialized renderbuffers when they are ready to be
285 // read, i.e., when the framebuffer is complete.
286 GraphicsContext3D* g3d = context()->graphicsContext3D();
287 if (g3d->checkFramebufferStatus(GraphicsContext3D::FRAMEBUFFER) != GraphicsContext3D::FRAMEBUFFER_COMPLETE)
290 GC3Dfloat colorClearValue[] = {0, 0, 0, 0}, depthClearValue = 0;
291 GC3Dint stencilClearValue = 0;
292 GC3Dboolean colorMask[] = {0, 0, 0, 0}, depthMask = 0;
293 GC3Duint stencilMask = 0xffffffff;
294 GC3Dboolean isScissorEnabled = 0;
295 GC3Dboolean isDitherEnabled = 0;
297 g3d->getFloatv(GraphicsContext3D::COLOR_CLEAR_VALUE, colorClearValue);
298 g3d->getBooleanv(GraphicsContext3D::COLOR_WRITEMASK, colorMask);
299 g3d->clearColor(0, 0, 0, 0);
300 g3d->colorMask(true, true, true, true);
303 g3d->getFloatv(GraphicsContext3D::DEPTH_CLEAR_VALUE, &depthClearValue);
304 g3d->getBooleanv(GraphicsContext3D::DEPTH_WRITEMASK, &depthMask);
306 g3d->depthMask(true);
309 g3d->getIntegerv(GraphicsContext3D::STENCIL_CLEAR_VALUE, &stencilClearValue);
310 g3d->getIntegerv(GraphicsContext3D::STENCIL_WRITEMASK, reinterpret_cast<GC3Dint*>(&stencilMask));
311 g3d->clearStencil(0);
312 g3d->stencilMask(0xffffffff);
314 isScissorEnabled = g3d->isEnabled(GraphicsContext3D::SCISSOR_TEST);
315 g3d->disable(GraphicsContext3D::SCISSOR_TEST);
316 isDitherEnabled = g3d->isEnabled(GraphicsContext3D::DITHER);
317 g3d->disable(GraphicsContext3D::DITHER);
322 g3d->clearColor(colorClearValue[0], colorClearValue[1], colorClearValue[2], colorClearValue[3]);
323 g3d->colorMask(colorMask[0], colorMask[1], colorMask[2], colorMask[3]);
326 g3d->clearDepth(depthClearValue);
327 g3d->depthMask(depthMask);
330 g3d->clearStencil(stencilClearValue);
331 g3d->stencilMask(stencilMask);
333 if (isScissorEnabled)
334 g3d->enable(GraphicsContext3D::SCISSOR_TEST);
336 g3d->disable(GraphicsContext3D::SCISSOR_TEST);
338 g3d->enable(GraphicsContext3D::DITHER);
340 g3d->disable(GraphicsContext3D::DITHER);
343 setInitialized(m_colorAttachment.get());
344 if (initDepth && initStencil && m_depthStencilAttachment)
345 setInitialized(m_depthStencilAttachment.get());
348 setInitialized(m_depthAttachment.get());
350 setInitialized(m_stencilAttachment.get());
357 #endif // ENABLE(WEBGL)