1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "renderbuffer_p.h"
5#include <QOpenGLContext>
6#include <QOpenGLFunctions>
7
8QT_BEGIN_NAMESPACE
9
10namespace Qt3DRender {
11namespace Render {
12namespace OpenGL {
13
14RenderBuffer::RenderBuffer(int width, int height, QAbstractTexture::TextureFormat format)
15 : m_size(width, height),
16 m_format(format),
17 m_renderBuffer(0),
18 m_context(nullptr)
19{
20 QOpenGLContext *ctx = QOpenGLContext::currentContext();
21 if (!ctx) {
22 qWarning(msg: "Renderbuffer requires an OpenGL context");
23 return;
24 }
25
26 m_context = ctx;
27 QOpenGLFunctions *f = ctx->functions();
28 f->glGenRenderbuffers(n: 1, renderbuffers: &m_renderBuffer);
29 if (!m_renderBuffer)
30 return;
31
32 f->glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer: m_renderBuffer);
33 while (f->glGetError() != GL_NO_ERROR) { }
34 f->glRenderbufferStorage(GL_RENDERBUFFER, internalformat: format, width, height);
35 GLint err = f->glGetError();
36 if (err != GL_NO_ERROR)
37 qWarning(msg: "Failed to set renderbuffer storage: error 0x%x", err);
38 f->glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer: 0);
39}
40
41RenderBuffer::~RenderBuffer()
42{
43 if (m_renderBuffer) {
44 QOpenGLContext *ctx = QOpenGLContext::currentContext();
45
46 // Ignore the fact that renderbuffers are sharable resources and let's
47 // just expect that the context is the same as when the resource was
48 // created. QOpenGLTexture suffers from the same limitation anyway, and
49 // this is unlikely to become an issue within Qt 3D.
50 if (ctx == m_context) {
51 ctx->functions()->glDeleteRenderbuffers(n: 1, renderbuffers: &m_renderBuffer);
52 } else {
53 qWarning(msg: "Wrong current context; renderbuffer not destroyed");
54 }
55 }
56}
57
58void RenderBuffer::bind()
59{
60 if (!m_renderBuffer)
61 return;
62
63 m_context->functions()->glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer: m_renderBuffer);
64}
65
66void RenderBuffer::release()
67{
68 if (!m_context)
69 return;
70
71 m_context->functions()->glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer: 0);
72}
73
74} // namespace OpenGL
75} // namespace Render
76} // namespace Qt3DRender
77
78QT_END_NAMESPACE
79

source code of qt3d/src/plugins/renderers/opengl/textures/renderbuffer.cpp