Blender  V2.93
gl_vertex_buffer.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2016 by Mike Erwin.
17  * All rights reserved.
18  */
19 
24 #include "gl_context.hh"
25 
26 #include "gl_vertex_buffer.hh"
27 
28 namespace blender::gpu {
29 
31 {
32  /* Discard previous data if any. */
34  data = (uchar *)MEM_mallocN(sizeof(uchar) * this->size_alloc_get(), __func__);
35 }
36 
38 {
39  data = (uchar *)MEM_reallocN(data, sizeof(uchar) * this->size_alloc_get());
40 }
41 
43 {
44  if (vbo_id_ != 0) {
45  GLContext::buf_free(vbo_id_);
46  vbo_id_ = 0;
47  memory_usage -= vbo_size_;
48  }
49 
51 }
52 
54 {
55  BLI_assert(GLContext::get() != nullptr);
56  GLVertBuf *src = this;
57  GLVertBuf *dst = static_cast<GLVertBuf *>(dst_);
58 
59  if (src->vbo_id_ != 0) {
60  dst->vbo_size_ = src->size_used_get();
61 
62  glGenBuffers(1, &dst->vbo_id_);
63  glBindBuffer(GL_COPY_WRITE_BUFFER, dst->vbo_id_);
64  glBufferData(GL_COPY_WRITE_BUFFER, dst->vbo_size_, nullptr, to_gl(dst->usage_));
65 
66  glBindBuffer(GL_COPY_READ_BUFFER, src->vbo_id_);
67 
68  glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, dst->vbo_size_);
69 
70  memory_usage += dst->vbo_size_;
71  }
72 
73  if (data != nullptr) {
74  dst->data = (uchar *)MEM_dupallocN(src->data);
75  }
76 }
77 
79 {
80  this->bind();
81 }
82 
84 {
85  BLI_assert(GLContext::get() != nullptr);
86 
87  if (vbo_id_ == 0) {
88  glGenBuffers(1, &vbo_id_);
89  }
90 
91  glBindBuffer(GL_ARRAY_BUFFER, vbo_id_);
92 
94  vbo_size_ = this->size_used_get();
95  /* Orphan the vbo to avoid sync then upload data. */
96  glBufferData(GL_ARRAY_BUFFER, vbo_size_, nullptr, to_gl(usage_));
97  glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_size_, data);
98 
99  memory_usage += vbo_size_;
100 
101  if (usage_ == GPU_USAGE_STATIC) {
103  }
106  }
107 }
108 
110 {
111  glBufferSubData(GL_ARRAY_BUFFER, start, len, data);
112 }
113 
114 } // namespace blender::gpu
#define BLI_assert(a)
Definition: BLI_assert.h:58
unsigned char uchar
Definition: BLI_sys_types.h:86
unsigned int uint
Definition: BLI_sys_types.h:83
@ GPU_VERTBUF_DATA_DIRTY
@ GPU_VERTBUF_DATA_UPLOADED
@ GPU_USAGE_STATIC
#define MEM_SAFE_FREE(v)
#define MEM_reallocN(vmemh, len)
static void buf_free(GLuint buf_id)
Definition: gl_context.cc:257
static GLContext * get()
Definition: gl_context.hh:118
void resize_data(void) override
void duplicate_data(VertBuf *dst) override
void acquire_data(void) override
void release_data(void) override
void upload_data(void) override
void update_sub(uint start, uint len, void *data) override
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:42
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static GLenum to_gl(const GPUAttachmentType type)
uint len