Blender V5.3
vk_immediate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "GPU_capabilities.hh"
12#include "GPU_matrix.hh"
13
14#include "vk_backend.hh"
15#include "vk_context.hh"
16#include "vk_framebuffer.hh"
17#include "vk_immediate.hh"
18#include "vk_memory_layout.hh"
19#include "vk_state_manager.hh"
20
21#include "CLG_log.h"
22
23namespace blender::gpu {
24
25static CLG_LogRef LOG = {"gpu.vulkan"};
26
28{
29 uint add_vertex = prim_type == GPU_PRIM_LINE_LOOP ? 1 : 0;
30 const size_t bytes_needed = vertex_buffer_size(&vertex_format, vertex_len + add_vertex);
31 size_t offset_alignment = GPU_storage_buffer_alignment();
32 VKBuffer &buffer = ensure_space(bytes_needed, offset_alignment);
33 buffer_offset_ = ceil_to_multiple_ul(buffer_offset_, offset_alignment);
34
35 current_subbuffer_len_ = bytes_needed;
36 uchar *data = static_cast<uchar *>(buffer.mapped_memory_get());
37 return data + buffer_offset_;
38}
39
41{
42 BLI_assert_msg(prim_type != GPU_PRIM_NONE, "Illegal state: not between an immBegin/End pair.");
43 if (vertex_idx == 0) {
44 return;
45 }
46
48 uchar *first_vertex_ptr = static_cast<uchar *>(active_buffer_->mapped_memory_get()) +
49 buffer_offset_;
50 size_t vertex_stride = current_subbuffer_len_ / (vertex_len + 1);
51 uchar *last_vertex_ptr = first_vertex_ptr + vertex_stride * vertex_len;
52 memcpy(last_vertex_ptr, first_vertex_ptr, vertex_stride);
53
55 vertex_idx += 1;
56 }
57
58 VKContext &context = *VKContext::get();
59 BLI_assert(context.shader == shader);
60 Shader &shader = *this->shader;
61 if (shader.is_polyline) {
62 VKBuffer &buffer = active_buffer_.value();
63 VKStateManager &state_manager = context.state_manager_get();
65 &buffer,
67 buffer_offset_);
69 &buffer,
71 buffer_offset_);
72 /* Not used. Satisfy the binding. */
73 state_manager.storage_buffer_bind(
76 }
77 else {
78 GPU_matrix_bind(context.shader);
79 render_graph::VKRenderGraph &graph = context.render_graph();
80 render_graph::VKResourceAccessInfo &resource_access_info = context.reset_and_get_access_info();
81 VKDevice &device = VKBackend::get().device;
82 vertex_attributes_.update_bindings(*this);
83 vertex_attributes_.update_vertex_input_key(device.vertex_input_descriptions);
84 VKFrameBuffer &framebuffer = *context.active_framebuffer_get();
85 framebuffer.rendering_ensure(context);
86
89 node.data.vertex_count = vertex_idx;
90 node.data.instance_count = 1;
91 node.data.first_vertex = 0;
92 node.data.first_instance = 0;
93
94 vertex_attributes_.bind(node.data.vertex_buffers);
95 context.update_pipeline_data(
96 framebuffer, prim_type, vertex_attributes_.vertex_input_key, node.data.graphics);
97
98 render_graph::VKDrawNode::CreateInfo draw(resource_access_info);
99 node.finalize(graph, draw);
100 }
101
102 buffer_offset_ += current_subbuffer_len_;
103 current_subbuffer_len_ = 0;
104}
105
106VKBufferWithOffset VKImmediate::active_buffer() const
107{
108 VKBufferWithOffset result = {active_buffer_->resource(), buffer_offset_};
109 return result;
110}
111
112VkDeviceSize VKImmediate::buffer_bytes_free()
113{
114 return active_buffer_->size_in_bytes() - buffer_offset_;
115}
116
117static VkDeviceSize new_buffer_size(VkDeviceSize sub_buffer_size)
118{
119 return max_ulul(sub_buffer_size, DEFAULT_INTERNAL_BUFFER_SIZE);
120}
121
122VKBuffer &VKImmediate::ensure_space(VkDeviceSize bytes_needed, VkDeviceSize offset_alignment)
123{
124 VkDeviceSize bytes_required = ceil_to_multiple_ul(bytes_needed, offset_alignment);
125
126 /* Last used buffer still has space. */
127 if (active_buffer_.has_value() && buffer_bytes_free() >= bytes_required) {
128 return active_buffer_.value();
129 }
130
131 /* Offset alignment isn't needed when creating buffers as it is managed by VMA. */
132 VkDeviceSize alloc_size = new_buffer_size(bytes_needed);
134 "Immediate buffer cannot hold another %d bytes, it contains %d bytes. A new "
135 "buffer will be allocated (size=%d)",
136 int(bytes_required),
137 int(buffer_offset_),
138 int(alloc_size));
139 buffer_offset_ = 0;
140 VKBuffer &result = active_buffer_.emplace();
141 result.create(alloc_size,
142 VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT |
143 VK_BUFFER_USAGE_TRANSFER_DST_BIT,
144 VMA_MEMORY_USAGE_AUTO_PREFER_HOST,
145 VMA_ALLOCATION_CREATE_MAPPED_BIT |
146 VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
147 0.8,
148 false,
149 "Immediate");
150
151 return result;
152}
153
154} // namespace blender::gpu
#define CLOG_TRACE(clg_ref,...)
Definition CLG_log.h:192
#define BLI_assert_msg(a, msg)
void polyline_draw_workaround(uint64_t offset)
static VKBackend & get()
void * mapped_memory_get() const
Definition vk_buffer.hh:158
static VKContext * get()
VKVertexInputDescriptionPool vertex_input_descriptions
Definition vk_device.hh:275
void rendering_ensure(VKContext &context)
uchar * begin() override
void storage_buffer_bind(BindSpaceStorageBuffers::Type resource_type, void *resource, int binding)
#define GPU_SSBO_POLYLINE_POS_BUF_SLOT
#define GPU_SSBO_POLYLINE_COL_BUF_SLOT
#define GPU_SSBO_INDEX_BUF_SLOT
#define LOG(level)
Definition log.h:97
constexpr size_t DEFAULT_INTERNAL_BUFFER_SIZE
static CLG_LogRef LOG
static VkDeviceSize new_buffer_size(VkDeviceSize sub_buffer_size)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE unsigned long long max_ulul(unsigned long long a, unsigned long long b)
unsigned int uint
Definition BKE_text.h:55
@ GPU_PRIM_LINE_STRIP
@ GPU_PRIM_LINE_LOOP
void GPU_matrix_bind(gpu::Shader *shader)
size_t GPU_storage_buffer_alignment()
MINLINE uint64_t ceil_to_multiple_ul(uint64_t a, uint64_t b)
unsigned char uchar
Definition BKE_text.h:58
BMesh const char void * data
uint vertex_buffer_size(const GPUVertFormat *format, uint vertex_len)