Blender  V3.3
gl_vertex_buffer.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2016 by Mike Erwin. All rights reserved. */
3 
8 #include "GPU_texture.h"
9 
10 #include "gl_context.hh"
11 
12 #include "gl_vertex_buffer.hh"
13 
14 namespace blender::gpu {
15 
17 {
19  return;
20  }
21 
22  /* Discard previous data if any. */
24  data = (uchar *)MEM_mallocN(sizeof(uchar) * this->size_alloc_get(), __func__);
25 }
26 
28 {
30  return;
31  }
32 
33  data = (uchar *)MEM_reallocN(data, sizeof(uchar) * this->size_alloc_get());
34 }
35 
37 {
38  if (is_wrapper_) {
39  return;
40  }
41 
42  if (vbo_id_ != 0) {
43  GPU_TEXTURE_FREE_SAFE(buffer_texture_);
44  GLContext::buf_free(vbo_id_);
45  vbo_id_ = 0;
46  memory_usage -= vbo_size_;
47  }
48 
50 }
51 
53 {
54  BLI_assert(GLContext::get() != nullptr);
55  GLVertBuf *src = this;
56  GLVertBuf *dst = static_cast<GLVertBuf *>(dst_);
57  dst->buffer_texture_ = nullptr;
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  /* Do not transfer data from host to device when buffer is device only. */
99  glBufferSubData(GL_ARRAY_BUFFER, 0, vbo_size_, data);
100  }
101  memory_usage += vbo_size_;
102 
103  if (usage_ == GPU_USAGE_STATIC) {
105  }
108  }
109 }
110 
112 {
113  bind();
114  BLI_assert(vbo_id_ != 0);
115  glBindBufferBase(GL_SHADER_STORAGE_BUFFER, binding, vbo_id_);
116 }
117 
119 {
120  bind();
121  BLI_assert(vbo_id_ != 0);
122  if (buffer_texture_ == nullptr) {
123  buffer_texture_ = GPU_texture_create_from_vertbuf("vertbuf_as_texture", wrap(this));
124  }
125  GPU_texture_bind(buffer_texture_, binding);
126 }
127 
128 const void *GLVertBuf::read() const
129 {
130  BLI_assert(is_active());
131  void *result = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
132  return result;
133 }
134 
135 void *GLVertBuf::unmap(const void *mapped_data) const
136 {
137  void *result = MEM_mallocN(vbo_size_, __func__);
138  memcpy(result, mapped_data, vbo_size_);
139  return result;
140 }
141 
143 {
144  BLI_assert(vbo_id_ == 0);
145  BLI_assert(glIsBuffer(static_cast<uint>(handle)));
146  is_wrapper_ = true;
147  vbo_id_ = static_cast<uint>(handle);
148  /* We assume the data is already on the device, so no need to allocate or send it. */
150 }
151 
152 bool GLVertBuf::is_active() const
153 {
154  if (!vbo_id_) {
155  return false;
156  }
157  int active_vbo_id = 0;
158  glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &active_vbo_id);
159  return vbo_id_ == active_vbo_id;
160 }
161 
162 void GLVertBuf::update_sub(uint start, uint len, const void *data)
163 {
164  glBufferSubData(GL_ARRAY_BUFFER, start, len, data);
165 }
166 
167 } // namespace blender::gpu
#define BLI_assert(a)
Definition: BLI_assert.h:46
unsigned char uchar
Definition: BLI_sys_types.h:70
unsigned int uint
Definition: BLI_sys_types.h:67
GPUTexture * GPU_texture_create_from_vertbuf(const char *name, struct GPUVertBuf *vert)
Definition: gpu_texture.cc:361
#define GPU_TEXTURE_FREE_SAFE(texture)
Definition: GPU_texture.h:40
void GPU_texture_bind(GPUTexture *tex, int unit)
Definition: gpu_texture.cc:466
@ GPU_VERTBUF_DATA_DIRTY
@ GPU_VERTBUF_DATA_UPLOADED
@ GPU_USAGE_STATIC
@ GPU_USAGE_DEVICE_ONLY
#define MEM_SAFE_FREE(v)
#define MEM_reallocN(vmemh, len)
static void buf_free(GLuint buf_id)
Definition: gl_context.cc:250
static GLContext * get()
Definition: gl_context.hh:117
void bind_as_texture(uint binding) override
void upload_data() override
void * unmap(const void *mapped_data) const override
void bind_as_ssbo(uint binding) override
void wrap_handle(uint64_t handle) override
void duplicate_data(VertBuf *dst) override
void resize_data() override
void update_sub(uint start, uint len, const void *data) override
void acquire_data() override
const void * read() const override
void release_data() override
SyclQueue void void * src
int len
Definition: draw_manager.c:108
void *(* MEM_dupallocN)(const void *vmemh)
Definition: mallocn.c:28
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
static GPUContext * wrap(Context *ctx)
static GLenum to_gl(const GPUAttachmentType type)
unsigned __int64 uint64_t
Definition: stdint.h:90