2 * Copyright (C) 2013 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 #include "core/html/canvas/WebGLDrawBuffers.h"
32 WebGLDrawBuffers::WebGLDrawBuffers(WebGLRenderingContextBase* context)
33 : WebGLExtension(context)
35 ScriptWrappable::init(this);
36 context->extensionsUtil()->ensureExtensionEnabled("GL_EXT_draw_buffers");
39 WebGLDrawBuffers::~WebGLDrawBuffers()
43 WebGLExtensionName WebGLDrawBuffers::name() const
45 return WebGLDrawBuffersName;
48 PassRefPtrWillBeRawPtr<WebGLDrawBuffers> WebGLDrawBuffers::create(WebGLRenderingContextBase* context)
50 return adoptRefWillBeNoop(new WebGLDrawBuffers(context));
54 bool WebGLDrawBuffers::supported(WebGLRenderingContextBase* context)
56 return (context->extensionsUtil()->supportsExtension("GL_EXT_draw_buffers")
57 && satisfiesWebGLRequirements(context));
60 const char* WebGLDrawBuffers::extensionName()
62 return "WEBGL_draw_buffers";
65 void WebGLDrawBuffers::drawBuffersWEBGL(const Vector<GLenum>& buffers)
69 GLsizei n = buffers.size();
70 const GLenum* bufs = buffers.data();
71 if (!m_context->m_framebufferBinding) {
73 m_context->synthesizeGLError(GL_INVALID_VALUE, "drawBuffersWEBGL", "more than one buffer");
76 if (bufs[0] != GL_BACK && bufs[0] != GL_NONE) {
77 m_context->synthesizeGLError(GL_INVALID_OPERATION, "drawBuffersWEBGL", "BACK or NONE");
80 // Because the backbuffer is simulated on all current WebKit ports, we need to change BACK to COLOR_ATTACHMENT0.
81 GLenum value = (bufs[0] == GL_BACK) ? GL_COLOR_ATTACHMENT0 : GL_NONE;
82 m_context->webContext()->drawBuffersEXT(1, &value);
83 m_context->setBackDrawBuffer(bufs[0]);
85 if (n > m_context->maxDrawBuffers()) {
86 m_context->synthesizeGLError(GL_INVALID_VALUE, "drawBuffersWEBGL", "more than max draw buffers");
89 for (GLsizei i = 0; i < n; ++i) {
90 if (bufs[i] != GL_NONE && bufs[i] != static_cast<GLenum>(GL_COLOR_ATTACHMENT0_EXT + i)) {
91 m_context->synthesizeGLError(GL_INVALID_OPERATION, "drawBuffersWEBGL", "COLOR_ATTACHMENTi_EXT or NONE");
95 m_context->m_framebufferBinding->drawBuffers(buffers);
100 bool WebGLDrawBuffers::satisfiesWebGLRequirements(WebGLRenderingContextBase* webglContext)
102 blink::WebGraphicsContext3D* context = webglContext->webContext();
103 Extensions3DUtil* extensionsUtil = webglContext->extensionsUtil();
105 // This is called after we make sure GL_EXT_draw_buffers is supported.
106 GLint maxDrawBuffers = 0;
107 GLint maxColorAttachments = 0;
108 context->getIntegerv(GL_MAX_DRAW_BUFFERS_EXT, &maxDrawBuffers);
109 context->getIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &maxColorAttachments);
110 if (maxDrawBuffers < 4 || maxColorAttachments < 4)
113 Platform3DObject fbo = context->createFramebuffer();
114 context->bindFramebuffer(GL_FRAMEBUFFER, fbo);
116 const unsigned char* buffer = 0; // Chromium doesn't allow init data for depth/stencil tetxures.
117 bool supportsDepth = (extensionsUtil->supportsExtension("GL_CHROMIUM_depth_texture")
118 || extensionsUtil->supportsExtension("GL_OES_depth_texture")
119 || extensionsUtil->supportsExtension("GL_ARB_depth_texture"));
120 bool supportsDepthStencil = (extensionsUtil->supportsExtension("GL_EXT_packed_depth_stencil")
121 || extensionsUtil->supportsExtension("GL_OES_packed_depth_stencil"));
122 Platform3DObject depthStencil = 0;
123 if (supportsDepthStencil) {
124 depthStencil = context->createTexture();
125 context->bindTexture(GL_TEXTURE_2D, depthStencil);
126 context->texImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_STENCIL_OES, 1, 1, 0, GL_DEPTH_STENCIL_OES, GL_UNSIGNED_INT_24_8_OES, buffer);
128 Platform3DObject depth = 0;
130 depth = context->createTexture();
131 context->bindTexture(GL_TEXTURE_2D, depth);
132 context->texImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1, 1, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, buffer);
135 Vector<Platform3DObject> colors;
137 GLint maxAllowedBuffers = std::min(maxDrawBuffers, maxColorAttachments);
138 for (GLint i = 0; i < maxAllowedBuffers; ++i) {
139 Platform3DObject color = context->createTexture();
140 colors.append(color);
141 context->bindTexture(GL_TEXTURE_2D, color);
142 context->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
143 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, color, 0);
144 if (context->checkFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
149 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0);
150 if (context->checkFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
154 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
156 if (supportsDepthStencil) {
157 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthStencil, 0);
158 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, depthStencil, 0);
159 if (context->checkFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
163 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
164 context->framebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
168 webglContext->restoreCurrentFramebuffer();
169 context->deleteFramebuffer(fbo);
170 webglContext->restoreCurrentTexture2D();
172 context->deleteTexture(depth);
173 if (supportsDepthStencil)
174 context->deleteTexture(depthStencil);
175 for (size_t i = 0; i < colors.size(); ++i)
176 context->deleteTexture(colors[i]);