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

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