Blender V5.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
8
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. */
44 MEM_SAFE_DELETE_VOID(data_);
45 }
46}
47
49{
50 if (is_subrange_) {
51 src_->bind_as_ssbo(binding);
52 return;
53 }
54
55 if (ibo_id_ == 0 || data_ != nullptr) {
56 /* Calling `glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id_)` changes the index buffer
57 * of the currently bound VAO.
58 *
59 * In the OpenGL backend, the VAO state persists even after `GLVertArray::update_bindings`
60 * is called.
61 *
62 * NOTE: For safety, we could call `glBindVertexArray(0)` right after drawing a `gpu::Batch`.
63 * However, for performance reasons, we have chosen not to do so. */
64 glBindVertexArray(0);
65 bind();
66 }
67
68 BLI_assert(ibo_id_ != 0);
69 glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, ibo_id_);
70
71#ifndef NDEBUG
72 BLI_assert(binding < 16);
73 GLContext::get()->bound_ssbo_slots |= 1 << binding;
74#endif
75}
76
78{
79 BLI_assert(is_active());
80 const void *buffer = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
81 memcpy(data, buffer, size_get());
82 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
83}
84
85bool GLIndexBuf::is_active() const
86{
87 if (!ibo_id_) {
88 return false;
89 }
90 int active_ibo_id = 0;
91 glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &active_ibo_id);
92 return ibo_id_ == active_ibo_id;
93}
94
96{
97 bind();
98}
99
100void GLIndexBuf::update_sub(uint start, uint len, const void *data)
101{
102 glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, start, len, data);
103}
104
106 uint source_first_index,
107 uint dest_first_index,
108 uint index_len)
109{
110 GLIndexBuf &src = static_cast<GLIndexBuf &>(source_buf);
114 "Index type mismatch between source and destination");
115 BLI_assert_msg(size_t(source_first_index) + index_len <= src.index_len_,
116 "Copy source range exceeds the source index buffer bounds");
117 BLI_assert_msg(size_t(dest_first_index) + index_len <= index_len_,
118 "Copy destination range exceeds the destination index buffer bounds");
119 BLI_assert_msg(ibo_id_, "GPU_indexbuf_use() not called on this buffer");
120 BLI_assert_msg(src.ibo_id_, "GPU_indexbuf_use() not called on the source buffer");
121
122 const size_t src_offset = size_t(source_first_index) * to_bytesize(src.index_type_);
123 const size_t dst_offset = size_t(dest_first_index) * to_bytesize(index_type_);
124 const size_t copy_size = size_t(index_len) * to_bytesize(index_type_);
125
127 glCopyNamedBufferSubData(src.ibo_id_, ibo_id_, src_offset, dst_offset, copy_size);
128 }
129 else {
130 glBindBuffer(GL_COPY_READ_BUFFER, src.ibo_id_);
131 glBindBuffer(GL_COPY_WRITE_BUFFER, ibo_id_);
132 glCopyBufferSubData(
133 GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, src_offset, dst_offset, copy_size);
134 glBindBuffer(GL_COPY_READ_BUFFER, 0);
135 glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
136 }
137}
138
139} // namespace blender::gpu
#define BLI_assert_msg(a, msg)
static void buffer_free(GLuint buf_id)
static bool direct_state_access_support
Definition gl_context.hh:60
static GLContext * get()
void copy_sub(IndexBuf &source_buf, uint source_first_index, uint dest_first_index, uint index_len) override
Copy part of source index buffer into this index buffer.
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
GPUIndexBufType index_type_
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
int to_bytesize(const DataFormat format)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
unsigned int uint32_t
Definition BKE_text.h:81
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
BMesh const char void * data