1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Quick module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qsgdepthstencilbuffer_p.h"
41 
42 QT_BEGIN_NAMESPACE
43 
QSGDepthStencilBuffer(QOpenGLContext * context,const Format & format)44 QSGDepthStencilBuffer::QSGDepthStencilBuffer(QOpenGLContext *context, const Format &format)
45     : m_functions(context)
46     , m_manager(nullptr)
47     , m_format(format)
48     , m_depthBuffer(0)
49     , m_stencilBuffer(0)
50 {
51     // 'm_manager' is set by QSGDepthStencilBufferManager::insertBuffer().
52 }
53 
~QSGDepthStencilBuffer()54 QSGDepthStencilBuffer::~QSGDepthStencilBuffer()
55 {
56     if (m_manager)
57         m_manager->m_buffers.remove(m_format);
58 }
59 
60 #ifndef GL_DEPTH_STENCIL_ATTACHMENT
61 #define GL_DEPTH_STENCIL_ATTACHMENT 0x821A
62 #endif
63 
attach()64 void QSGDepthStencilBuffer::attach()
65 {
66 #ifndef Q_OS_WASM
67     m_functions.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
68                                           GL_RENDERBUFFER, m_depthBuffer);
69     m_functions.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
70                                           GL_RENDERBUFFER, m_stencilBuffer);
71 #else
72     m_functions.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
73                                           GL_RENDERBUFFER, m_stencilBuffer);
74 #endif
75 }
76 
detach()77 void QSGDepthStencilBuffer::detach()
78 {
79 #ifndef Q_OS_WASM
80     m_functions.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
81                                           GL_RENDERBUFFER, 0);
82     m_functions.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
83                                           GL_RENDERBUFFER, 0);
84 #else
85     m_functions.glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT,
86                                           GL_RENDERBUFFER, 0);
87 #endif
88 }
89 
90 #ifndef GL_DEPTH24_STENCIL8_OES
91 #define GL_DEPTH24_STENCIL8_OES 0x88F0
92 #endif
93 
94 #ifndef GL_DEPTH_COMPONENT24_OES
95 #define GL_DEPTH_COMPONENT24_OES 0x81A6
96 #endif
97 
98 #ifndef GL_DEPTH_STENCIL
99 #define GL_DEPTH_STENCIL 0x84F9
100 #endif
101 
QSGDefaultDepthStencilBuffer(QOpenGLContext * context,const Format & format)102 QSGDefaultDepthStencilBuffer::QSGDefaultDepthStencilBuffer(QOpenGLContext *context, const Format &format)
103     : QSGDepthStencilBuffer(context, format)
104 {
105     const GLsizei width = format.size.width();
106     const GLsizei height = format.size.height();
107 
108 #ifndef Q_OS_WASM
109     if (format.attachments == (DepthAttachment | StencilAttachment)
110             && m_functions.hasOpenGLExtension(QOpenGLExtensions::PackedDepthStencil))
111     {
112         m_functions.glGenRenderbuffers(1, &m_depthBuffer);
113         m_functions.glBindRenderbuffer(GL_RENDERBUFFER, m_depthBuffer);
114         if (format.samples && m_functions.hasOpenGLExtension(QOpenGLExtensions::FramebufferMultisample)) {
115 #if defined(QT_OPENGL_ES_2)
116             m_functions.glRenderbufferStorageMultisample(GL_RENDERBUFFER, format.samples,
117                 GL_DEPTH24_STENCIL8_OES, width, height);
118 #else
119             m_functions.glRenderbufferStorageMultisample(GL_RENDERBUFFER, format.samples,
120                 GL_DEPTH24_STENCIL8, width, height);
121 #endif
122         } else {
123 #if defined(QT_OPENGL_ES_2)
124             m_functions.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, width, height);
125 #else
126             m_functions.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
127 #endif
128         }
129         m_stencilBuffer = m_depthBuffer;
130     }
131     if (!m_depthBuffer && (format.attachments & DepthAttachment)) {
132         m_functions.glGenRenderbuffers(1, &m_depthBuffer);
133         m_functions.glBindRenderbuffer(GL_RENDERBUFFER, m_depthBuffer);
134         GLenum internalFormat = GL_DEPTH_COMPONENT;
135         if (context->isOpenGLES())
136             internalFormat = m_functions.hasOpenGLExtension(QOpenGLExtensions::Depth24)
137                 ? GL_DEPTH_COMPONENT24_OES : GL_DEPTH_COMPONENT16;
138         if (format.samples && m_functions.hasOpenGLExtension(QOpenGLExtensions::FramebufferMultisample)) {
139             m_functions.glRenderbufferStorageMultisample(GL_RENDERBUFFER, format.samples,
140                 internalFormat, width, height);
141         } else {
142             m_functions.glRenderbufferStorage(GL_RENDERBUFFER, internalFormat, width, height);
143         }
144     }
145     if (!m_stencilBuffer && (format.attachments & StencilAttachment)) {
146         m_functions.glGenRenderbuffers(1, &m_stencilBuffer);
147         m_functions.glBindRenderbuffer(GL_RENDERBUFFER, m_stencilBuffer);
148 #ifdef QT_OPENGL_ES
149         const GLenum internalFormat = GL_STENCIL_INDEX8;
150 #else
151         const GLenum internalFormat = context->isOpenGLES() ? GL_STENCIL_INDEX8 : GL_STENCIL_INDEX;
152 #endif
153         if (format.samples && m_functions.hasOpenGLExtension(QOpenGLExtensions::FramebufferMultisample)) {
154             m_functions.glRenderbufferStorageMultisample(GL_RENDERBUFFER, format.samples,
155                 internalFormat, width, height);
156         } else {
157             m_functions.glRenderbufferStorage(GL_RENDERBUFFER, internalFormat, width, height);
158         }
159     }
160 #else
161     m_functions.glGenRenderbuffers(1, &m_depthBuffer);
162     m_functions.glBindRenderbuffer(GL_RENDERBUFFER, m_depthBuffer);
163     m_functions.glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_STENCIL, width, height);
164     m_stencilBuffer = m_depthBuffer;
165 #endif
166 }
167 
~QSGDefaultDepthStencilBuffer()168 QSGDefaultDepthStencilBuffer::~QSGDefaultDepthStencilBuffer()
169 {
170     free();
171 }
172 
free()173 void QSGDefaultDepthStencilBuffer::free()
174 {
175     if (m_depthBuffer)
176         m_functions.glDeleteRenderbuffers(1, &m_depthBuffer);
177     if (m_stencilBuffer && m_stencilBuffer != m_depthBuffer)
178         m_functions.glDeleteRenderbuffers(1, &m_stencilBuffer);
179     m_depthBuffer = m_stencilBuffer = 0;
180 }
181 
182 
~QSGDepthStencilBufferManager()183 QSGDepthStencilBufferManager::~QSGDepthStencilBufferManager()
184 {
185     for (Hash::const_iterator it = m_buffers.constBegin(), cend = m_buffers.constEnd(); it != cend; ++it) {
186         QSharedPointer<QSGDepthStencilBuffer> buffer = it.value().toStrongRef();
187         Q_ASSERT_X(buffer, "~QSGDepthStencilBufferManager",
188                    "~QSGDepthStencilBuffer is supposed to unregister from the manager");
189         buffer->free();
190         buffer->m_manager = nullptr;
191     }
192 }
193 
bufferForFormat(const QSGDepthStencilBuffer::Format & fmt)194 QSharedPointer<QSGDepthStencilBuffer> QSGDepthStencilBufferManager::bufferForFormat(const QSGDepthStencilBuffer::Format &fmt)
195 {
196     Hash::const_iterator it = m_buffers.constFind(fmt);
197     if (it != m_buffers.constEnd())
198         return it.value().toStrongRef();
199     return QSharedPointer<QSGDepthStencilBuffer>();
200 }
201 
insertBuffer(const QSharedPointer<QSGDepthStencilBuffer> & buffer)202 void QSGDepthStencilBufferManager::insertBuffer(const QSharedPointer<QSGDepthStencilBuffer> &buffer)
203 {
204     Q_ASSERT(buffer->m_manager == nullptr);
205     Q_ASSERT(!m_buffers.contains(buffer->m_format));
206     buffer->m_manager = this;
207     m_buffers.insert(buffer->m_format, buffer.toWeakRef());
208 }
209 
qHash(const QSGDepthStencilBuffer::Format & format)210 uint qHash(const QSGDepthStencilBuffer::Format &format)
211 {
212     return qHash(qMakePair(format.size.width(), format.size.height()))
213             ^ (uint(format.samples) << 12) ^ (uint(format.attachments) << 28);
214 }
215 
216 QT_END_NAMESPACE
217