This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/gpu/opengl/gl_uniform_buffer.cc
Campbell Barton c434782e3a File headers: SPDX License migration
Use a shorter/simpler license convention, stops the header taking so
much space.

Follow the SPDX license specification: https://spdx.org/licenses

- C/C++/objc/objc++
- Python
- Shell Scripts
- CMake, GNUmakefile

While most of the source tree has been included

- `./extern/` was left out.
- `./intern/cycles` & `./intern/atomic` are also excluded because they
  use different header conventions.

doc/license/SPDX-license-identifiers.txt has been added to list SPDX all
used identifiers.

See P2788 for the script that automated these edits.

Reviewed By: brecht, mont29, sergey

Ref D14069
2022-02-11 09:14:36 +11:00

112 lines
2.3 KiB
C++

/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright 2020 Blender Foundation. All rights reserved. */
/** \file
* \ingroup gpu
*/
#include "BKE_global.h"
#include "BLI_string.h"
#include "gpu_backend.hh"
#include "gpu_context_private.hh"
#include "gl_backend.hh"
#include "gl_debug.hh"
#include "gl_uniform_buffer.hh"
namespace blender::gpu {
/* -------------------------------------------------------------------- */
/** \name Creation & Deletion
* \{ */
GLUniformBuf::GLUniformBuf(size_t size, const char *name) : UniformBuf(size, name)
{
/* Do not create ubo GL buffer here to allow allocation from any thread. */
BLI_assert(size <= GLContext::max_ubo_size);
}
GLUniformBuf::~GLUniformBuf()
{
GLContext::buf_free(ubo_id_);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Data upload / update
* \{ */
void GLUniformBuf::init()
{
BLI_assert(GLContext::get());
glGenBuffers(1, &ubo_id_);
glBindBuffer(GL_UNIFORM_BUFFER, ubo_id_);
glBufferData(GL_UNIFORM_BUFFER, size_in_bytes_, nullptr, GL_DYNAMIC_DRAW);
debug::object_label(GL_UNIFORM_BUFFER, ubo_id_, name_);
}
void GLUniformBuf::update(const void *data)
{
if (ubo_id_ == 0) {
this->init();
}
glBindBuffer(GL_UNIFORM_BUFFER, ubo_id_);
glBufferSubData(GL_UNIFORM_BUFFER, 0, size_in_bytes_, data);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Usage
* \{ */
void GLUniformBuf::bind(int slot)
{
if (slot >= GLContext::max_ubo_binds) {
fprintf(stderr,
"Error: Trying to bind \"%s\" ubo to slot %d which is above the reported limit of %d.",
name_,
slot,
GLContext::max_ubo_binds);
return;
}
if (ubo_id_ == 0) {
this->init();
}
if (data_ != nullptr) {
this->update(data_);
MEM_SAFE_FREE(data_);
}
slot_ = slot;
glBindBufferBase(GL_UNIFORM_BUFFER, slot_, ubo_id_);
#ifdef DEBUG
BLI_assert(slot < 16);
GLContext::get()->bound_ubo_slots |= 1 << slot;
#endif
}
void GLUniformBuf::unbind()
{
#ifdef DEBUG
/* NOTE: This only unbinds the last bound slot. */
glBindBufferBase(GL_UNIFORM_BUFFER, slot_, 0);
/* Hope that the context did not change. */
GLContext::get()->bound_ubo_slots &= ~(1 << slot_);
#endif
slot_ = 0;
}
/** \} */
} // namespace blender::gpu