2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2011 Igalia S.L.
5 * Redistribution and use in source and binary forms, with or without
6 * 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 COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "GraphicsContext3D.h"
31 #include <wtf/PassOwnPtr.h>
32 #include "Extensions3DOpenGL.h"
33 #include "GraphicsContext3DPrivate.h"
34 #include "OpenGLShims.h"
35 #include "ShaderLang.h"
36 #include <wtf/NotFound.h>
40 PassRefPtr<GraphicsContext3D> GraphicsContext3D::create(GraphicsContext3D::Attributes attributes, HostWindow* hostWindow, GraphicsContext3D::RenderStyle renderStyle)
42 // This implementation doesn't currently support rendering directly to the HostWindow.
43 if (renderStyle == RenderDirectlyToHostWindow)
46 OwnPtr<GraphicsContext3DPrivate> priv = GraphicsContext3DPrivate::create();
50 RefPtr<GraphicsContext3D> context = adoptRef(new GraphicsContext3D(attributes, hostWindow, false));
51 context->m_private = priv.release();
52 return context.release();
55 GraphicsContext3D::GraphicsContext3D(GraphicsContext3D::Attributes attributes, HostWindow*, bool)
61 , m_depthStencilBuffer(0)
64 , m_multisampleDepthStencilBuffer(0)
65 , m_multisampleColorBuffer(0)
67 GraphicsContext3DPrivate::addActiveGraphicsContext(this);
71 // Create a texture to render into.
72 ::glGenTextures(1, &m_texture);
73 ::glBindTexture(GL_TEXTURE_2D, m_texture);
74 ::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
75 ::glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
76 ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
77 ::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
78 ::glBindTexture(GL_TEXTURE_2D, 0);
81 ::glGenFramebuffersEXT(1, &m_fbo);
82 ::glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
85 if (!m_attrs.antialias && (m_attrs.stencil || m_attrs.depth))
86 ::glGenRenderbuffersEXT(1, &m_depthStencilBuffer);
88 // Create a multisample FBO.
89 if (m_attrs.antialias) {
90 ::glGenFramebuffersEXT(1, &m_multisampleFBO);
91 ::glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_multisampleFBO);
92 m_boundFBO = m_multisampleFBO;
93 ::glGenRenderbuffersEXT(1, &m_multisampleColorBuffer);
94 if (m_attrs.stencil || m_attrs.depth)
95 ::glGenRenderbuffersEXT(1, &m_multisampleDepthStencilBuffer);
98 // ANGLE initialization.
99 ShBuiltInResources ANGLEResources;
100 ShInitBuiltInResources(&ANGLEResources);
102 getIntegerv(GraphicsContext3D::MAX_VERTEX_ATTRIBS, &ANGLEResources.MaxVertexAttribs);
103 getIntegerv(GraphicsContext3D::MAX_VERTEX_UNIFORM_VECTORS, &ANGLEResources.MaxVertexUniformVectors);
104 getIntegerv(GraphicsContext3D::MAX_VARYING_VECTORS, &ANGLEResources.MaxVaryingVectors);
105 getIntegerv(GraphicsContext3D::MAX_VERTEX_TEXTURE_IMAGE_UNITS, &ANGLEResources.MaxVertexTextureImageUnits);
106 getIntegerv(GraphicsContext3D::MAX_COMBINED_TEXTURE_IMAGE_UNITS, &ANGLEResources.MaxCombinedTextureImageUnits);
107 getIntegerv(GraphicsContext3D::MAX_TEXTURE_IMAGE_UNITS, &ANGLEResources.MaxTextureImageUnits);
108 getIntegerv(GraphicsContext3D::MAX_FRAGMENT_UNIFORM_VECTORS, &ANGLEResources.MaxFragmentUniformVectors);
110 // Always set to 1 for OpenGL ES.
111 ANGLEResources.MaxDrawBuffers = 1;
112 m_compiler.setResources(ANGLEResources);
114 ::glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
115 ::glEnable(GL_POINT_SPRITE);
116 ::glClearColor(0, 0, 0, 0);
119 GraphicsContext3D::~GraphicsContext3D()
121 GraphicsContext3DPrivate::removeActiveGraphicsContext(this);
122 if (!m_private->m_context)
125 makeContextCurrent();
126 ::glDeleteTextures(1, &m_texture);
127 if (m_attrs.antialias) {
128 ::glDeleteRenderbuffersEXT(1, &m_multisampleColorBuffer);
129 if (m_attrs.stencil || m_attrs.depth)
130 ::glDeleteRenderbuffersEXT(1, &m_multisampleDepthStencilBuffer);
131 ::glDeleteFramebuffersEXT(1, &m_multisampleFBO);
133 if (m_attrs.stencil || m_attrs.depth)
134 ::glDeleteRenderbuffersEXT(1, &m_depthStencilBuffer);
136 ::glDeleteFramebuffersEXT(1, &m_fbo);
139 void GraphicsContext3D::makeContextCurrent()
143 m_private->makeContextCurrent();
146 PlatformGraphicsContext3D GraphicsContext3D::platformGraphicsContext3D()
148 return m_private->m_context;
151 bool GraphicsContext3D::isGLES2Compliant() const
158 #endif // ENABLE(WEBGL)