Blender V4.5
gpu_vertex_buffer_wrapper.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Foundation
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include "GPU_context.hh"
9
10namespace blender::opensubdiv {
11
19 gpu::VertBuf &gpu_vertex_buffer_;
20
22 int element_count_;
23
25 bool use_update_sub_;
26
27 public:
28 GPUVertexBuffer(gpu::VertBuf &gpu_vertex_buffer, int element_count, bool use_update_sub)
29 : gpu_vertex_buffer_(gpu_vertex_buffer),
30 element_count_(element_count),
31 use_update_sub_(use_update_sub)
32 {
33 }
34
42
43 static GPUVertexBuffer *Create(int element_count, int vertex_len, void *device_context = nullptr)
44 {
45 (void)device_context;
48 GPU_vertformat_attr_add(&format, "elements", GPU_COMP_F32, element_count, GPU_FETCH_FLOAT);
49 const bool use_update_sub = GPU_backend_get_type() != GPU_BACKEND_VULKAN;
50 gpu::VertBuf *vertex_buffer = nullptr;
51 if (use_update_sub) {
52 vertex_buffer = GPU_vertbuf_calloc();
53 GPU_vertbuf_init_build_on_device(*vertex_buffer, format, vertex_len);
54 }
55 else {
57 GPU_vertbuf_data_alloc(*vertex_buffer, vertex_len);
58 }
59 return new GPUVertexBuffer(*vertex_buffer, element_count, use_update_sub);
60 }
61
64 {
65 GPU_vertbuf_discard(&gpu_vertex_buffer_);
66 }
67
70 void UpdateData(const float *src,
71 int start_vertex,
72 int num_vertices,
73 void *device_context = NULL)
74 {
75 (void)device_context;
76 if (use_update_sub_) {
77 GPU_vertbuf_use(&gpu_vertex_buffer_);
78 size_t offset = start_vertex * element_count_ * sizeof(float);
79 size_t data_len = num_vertices * element_count_ * sizeof(float);
80 GPU_vertbuf_update_sub(&gpu_vertex_buffer_, offset, data_len, src);
81 }
82 else {
83 MutableSpan<float> buffer_nodes = gpu_vertex_buffer_.data<float>();
84 buffer_nodes = buffer_nodes.drop_front(start_vertex * element_count_);
85 memcpy(buffer_nodes.data(), src, sizeof(float) * element_count_ * num_vertices);
86 GPU_vertbuf_tag_dirty(&gpu_vertex_buffer_);
87 }
88 }
89
91 int GetNumVertices() const
92 {
93 return GPU_vertbuf_get_vertex_len(&gpu_vertex_buffer_);
94 }
95
97 {
98 return &gpu_vertex_buffer_;
99 }
100};
101
102} // namespace blender::opensubdiv
eGPUBackendType GPU_backend_get_type()
void GPU_vertbuf_init_build_on_device(blender::gpu::VertBuf &verts, const GPUVertFormat &format, uint v_len)
void GPU_vertbuf_use(blender::gpu::VertBuf *)
blender::gpu::VertBuf * GPU_vertbuf_create_with_format_ex(const GPUVertFormat &format, GPUUsageType usage)
void GPU_vertbuf_tag_dirty(blender::gpu::VertBuf *verts)
blender::gpu::VertBuf * GPU_vertbuf_calloc()
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
void GPU_vertbuf_update_sub(blender::gpu::VertBuf *verts, uint start, uint len, const void *data)
uint GPU_vertbuf_get_vertex_len(const blender::gpu::VertBuf *verts)
void GPU_vertbuf_discard(blender::gpu::VertBuf *)
@ GPU_USAGE_DYNAMIC
@ GPU_FETCH_FLOAT
void GPU_vertformat_clear(GPUVertFormat *)
uint GPU_vertformat_attr_add(GPUVertFormat *, blender::StringRef name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
constexpr T * data() const
Definition BLI_span.hh:539
constexpr MutableSpan drop_front(const int64_t n) const
Definition BLI_span.hh:607
GPUVertexBuffer(gpu::VertBuf &gpu_vertex_buffer, int element_count, bool use_update_sub)
void UpdateData(const float *src, int start_vertex, int num_vertices, void *device_context=NULL)
static GPUVertexBuffer * Create(int element_count, int vertex_len, void *device_context=nullptr)
int GetNumVertices() const
Returns how many vertices allocated in this vertex buffer.
format