Blender  V3.3
gl_index_buffer.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2020 Blender Foundation. All rights reserved. */
3 
8 #include "gl_context.hh"
9 
10 #include "gl_index_buffer.hh"
11 
12 namespace blender::gpu {
13 
15 {
16  GLContext::buf_free(ibo_id_);
17 }
18 
20 {
21  if (is_subrange_) {
22  static_cast<GLIndexBuf *>(src_)->bind();
23  return;
24  }
25 
26  const bool allocate_on_device = ibo_id_ == 0;
27  if (allocate_on_device) {
28  glGenBuffers(1, &ibo_id_);
29  }
30 
31  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id_);
32 
33  if (data_ != nullptr || allocate_on_device) {
34  size_t size = this->size_get();
35  /* Sends data to GPU. */
36  glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, data_, GL_STATIC_DRAW);
37  /* No need to keep copy of data in system memory. */
39  }
40 }
41 
43 {
44  if (ibo_id_ == 0 || data_ != nullptr) {
45  /* Calling `glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_id_)` changes the index buffer
46  * of the currently bound VAO.
47  *
48  * In the OpenGL backend, the VAO state persists even after `GLVertArray::update_bindings`
49  * is called.
50  *
51  * NOTE: For safety, we could call `glBindVertexArray(0)` right after drawing a `GPUBatch`.
52  * However, for performance reasons, we have chosen not to do so. */
53  glBindVertexArray(0);
54  bind();
55  }
56  BLI_assert(ibo_id_ != 0);
57  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, ibo_id_);
58 }
59 
60 const uint32_t *GLIndexBuf::read() const
61 {
62  BLI_assert(is_active());
63  void *data = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
64  uint32_t *result = static_cast<uint32_t *>(data);
65  return result;
66 }
67 
68 bool GLIndexBuf::is_active() const
69 {
70  if (!ibo_id_) {
71  return false;
72  }
73  int active_ibo_id = 0;
74  glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &active_ibo_id);
75  return ibo_id_ == active_ibo_id;
76 }
77 
79 {
80  bind();
81 }
82 
83 void GLIndexBuf::update_sub(uint start, uint len, const void *data)
84 {
85  glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, start, len, data);
86 }
87 
88 } // namespace blender::gpu
#define BLI_assert(a)
Definition: BLI_assert.h:46
unsigned int uint
Definition: BLI_sys_types.h:67
#define MEM_SAFE_FREE(v)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
static void buf_free(GLuint buf_id)
Definition: gl_context.cc:250
void bind_as_ssbo(uint binding) override
const uint32_t * read() const override
void update_sub(uint start, uint len, const void *data) override
void upload_data() override
int len
Definition: draw_manager.c:108
unsigned int uint32_t
Definition: stdint.h:80