Blender V4.3
gl_index_buffer.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9#include "gl_context.hh"
10
11#include "gl_index_buffer.hh"
12
13namespace blender::gpu {
14
19
21{
22 if (is_subrange_) {
23 static_cast<GLIndexBuf *>(src_)->bind();
24 return;
25 }
26
27 const bool allocate_on_device = ibo_id_ == 0;
28 if (allocate_on_device) {
29 glGenBuffers(1, &ibo_id_);
30 }
31
32 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id_);
33
34 if (data_ != nullptr || allocate_on_device) {
35 size_t size = this->size_get();
36 /* Pad the buffer to avoid out of bound reads when using vertex pulling mode. */
37 glBufferData(GL_ELEMENT_ARRAY_BUFFER, ceil_to_multiple_ul(size, 16), nullptr, GL_STATIC_DRAW);
38
39 if (data_ != nullptr) {
40 /* Sends data to GPU. */
41 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, size, data_);
42 }
43 /* No need to keep copy of data in system memory. */
45 }
46}
47
49{
50 if (ibo_id_ == 0 || data_ != nullptr) {
51 /* Calling `glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id_)` changes the index buffer
52 * of the currently bound VAO.
53 *
54 * In the OpenGL backend, the VAO state persists even after `GLVertArray::update_bindings`
55 * is called.
56 *
57 * NOTE: For safety, we could call `glBindVertexArray(0)` right after drawing a `gpu::Batch`.
58 * However, for performance reasons, we have chosen not to do so. */
59 glBindVertexArray(0);
60 bind();
61 }
62 BLI_assert(ibo_id_ != 0);
63 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, ibo_id_);
64
65#ifndef NDEBUG
66 BLI_assert(binding < 16);
67 GLContext::get()->bound_ssbo_slots |= 1 << binding;
68#endif
69}
70
71void GLIndexBuf::read(uint32_t *data) const
72{
73 BLI_assert(is_active());
74 const void *buffer = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
75 memcpy(data, buffer, size_get());
76 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
77}
78
79bool GLIndexBuf::is_active() const
80{
81 if (!ibo_id_) {
82 return false;
83 }
84 int active_ibo_id = 0;
85 glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &active_ibo_id);
86 return ibo_id_ == active_ibo_id;
87}
88
90{
91 bind();
92}
93
94void GLIndexBuf::update_sub(uint start, uint len, const void *data)
95{
96 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, start, len, data);
97}
98
99} // namespace blender::gpu
#define BLI_assert(a)
Definition BLI_assert.h:50
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
unsigned int uint
#define MEM_SAFE_FREE(v)
static void buf_free(GLuint buf_id)
static GLContext * get()
void bind_as_ssbo(uint binding) override
void update_sub(uint start, uint len, const void *data) override
void read(uint32_t *data) const override
int len
unsigned int uint32_t
Definition stdint.h:80