The goal is to solve confusion of the "All rights reserved" for licensing
code under an open-source license.
The phrase "All rights reserved" comes from a historical convention that
required this phrase for the copyright protection to apply. This convention
is no longer relevant.
However, even though the phrase has no meaning in establishing the copyright
it has not lost meaning in terms of licensing.
This change makes it so code under the Blender Foundation copyright does
not use "all rights reserved". This is also how the GPL license itself
states how to apply it to the source code:
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software ...
This change does not change copyright notice in cases when the copyright
is dual (BF and an author), or just an author of the code. It also does
mot change copyright which is inherited from NaN Holding BV as it needs
some further investigation about what is the proper way to handle it.
152 lines
3.4 KiB
C++
152 lines
3.4 KiB
C++
/* SPDX-License-Identifier: GPL-2.0-or-later
|
|
* Copyright 2020 Blender Foundation */
|
|
|
|
/** \file
|
|
* \ingroup gpu
|
|
*/
|
|
|
|
#include "BLI_string.h"
|
|
|
|
#include "gpu_context_private.hh"
|
|
|
|
#include "gl_debug.hh"
|
|
#include "gl_texture.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);
|
|
}
|
|
|
|
void GLUniformBuf::clear_to_zero()
|
|
{
|
|
if (ubo_id_ == 0) {
|
|
this->init();
|
|
}
|
|
|
|
uint32_t data = 0;
|
|
eGPUTextureFormat internal_format = GPU_R32UI;
|
|
eGPUDataFormat data_format = GPU_DATA_UINT;
|
|
|
|
if (GLContext::direct_state_access_support) {
|
|
glClearNamedBufferData(ubo_id_,
|
|
to_gl_internal_format(internal_format),
|
|
to_gl_data_format(internal_format),
|
|
to_gl(data_format),
|
|
&data);
|
|
}
|
|
else {
|
|
/* WATCH(@fclem): This should be ok since we only use clear outside of drawing functions. */
|
|
glBindBuffer(GL_UNIFORM_BUFFER, ubo_id_);
|
|
glClearBufferData(GL_UNIFORM_BUFFER,
|
|
to_gl_internal_format(internal_format),
|
|
to_gl_data_format(internal_format),
|
|
to_gl(data_format),
|
|
&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.\n",
|
|
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::bind_as_ssbo(int slot)
|
|
{
|
|
if (ubo_id_ == 0) {
|
|
this->init();
|
|
}
|
|
if (data_ != nullptr) {
|
|
this->update(data_);
|
|
MEM_SAFE_FREE(data_);
|
|
}
|
|
|
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, slot, ubo_id_);
|
|
}
|
|
|
|
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
|