Blender V5.3
vk_vertex_attribute_object.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "vk_batch.hh"
12#include "vk_context.hh"
13#include "vk_immediate.hh"
14#include "vk_shader.hh"
16#include "vk_vertex_buffer.hh"
17
18#include "BLI_bit_vector.hh"
20
21namespace blender::gpu {
22
24{
25 bindings.clear();
26 attributes.clear();
27}
28
33
41
46
48{
49 if (this == &other) {
50 return *this;
51 }
52
55
56 vbos.clear();
57 vbos.extend(other.vbos);
58 buffers.clear();
59 buffers.extend(other.buffers);
60 return *this;
61}
62
63/* -------------------------------------------------------------------- */
66
68 render_graph::VKVertexBufferBindings &r_vertex_buffer_bindings) const
69{
70 BitVector visited_bindings(vertex_input.bindings.size());
71
73 for (VkVertexInputAttributeDescription2EXT attribute : vertex_input.attributes) {
74 if (visited_bindings[attribute.binding]) {
75 continue;
76 }
77 visited_bindings[attribute.binding].set(true);
78
80 VkDeviceSize offset = 0;
81
82 if (attribute.binding < buffers.size()) {
83 resource = buffers[attribute.binding].buffer;
84 offset = buffers[attribute.binding].offset;
85 }
86
87 if (resource.vk_handle == VK_NULL_HANDLE) {
88 resource = dummy.resource();
89 offset = 0;
90 }
91
92 r_vertex_buffer_bindings.buffer[attribute.binding] = resource;
93 r_vertex_buffer_bindings.resource_handles[attribute.binding] = resource;
94 r_vertex_buffer_bindings.offset[attribute.binding] = offset;
95 r_vertex_buffer_bindings.buffer_count = max_ii(r_vertex_buffer_bindings.buffer_count,
96 attribute.binding + 1);
97 }
98}
99
101
102/* -------------------------------------------------------------------- */
105
107{
108 clear();
109 const VKShaderInterface &interface = unwrap(context.shader)->interface_get();
110 AttributeMask occupied_attributes = 0;
111
112 fill_unused_bindings(interface, 0);
114
115 for (int v = 0; v < GPU_BATCH_VBO_MAX_LEN; v++) {
116 VKVertexBuffer *vbo = batch.vertex_buffer_get(v);
117 if (vbo) {
118 update_bindings(vbo->format, vbo, nullptr, vbo->vertex_len, interface, occupied_attributes);
119 }
120 }
121}
122
123void VKVertexAttributeObject::fill_unused_bindings(const VKShaderInterface &interface,
124 const AttributeMask occupied_attributes)
125{
126 for (int location : IndexRange(16)) {
127 AttributeMask location_mask = 1 << location;
128 /* Skip occupied slots */
129 if (occupied_attributes & location_mask) {
130 continue;
131 }
132 /* Skip slots that are not used by the vertex shader. */
133 if ((interface.enabled_attr_mask_ & location_mask) == 0) {
134 continue;
135 }
136
137 /* Use dummy binding. */
138 shader::Type attribute_type = interface.get_attribute_type(location);
139 const uint32_t binding = vertex_input.bindings.size();
140 VkVertexInputAttributeDescription2EXT attribute_description = {
141 VK_STRUCTURE_TYPE_VERTEX_INPUT_ATTRIBUTE_DESCRIPTION_2_EXT, nullptr};
142 attribute_description.binding = binding;
143 attribute_description.location = location;
144 attribute_description.offset = 0;
145 attribute_description.format = to_vk_format(attribute_type);
146 vertex_input.attributes.append(attribute_description);
147
148 VkVertexInputBindingDescription2EXT vk_binding_descriptor = {
149 VK_STRUCTURE_TYPE_VERTEX_INPUT_BINDING_DESCRIPTION_2_EXT, nullptr};
150 vk_binding_descriptor.binding = binding;
151 vk_binding_descriptor.stride = 0;
152 vk_binding_descriptor.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
153 vk_binding_descriptor.divisor = 1;
154 vertex_input.bindings.append(vk_binding_descriptor);
155 }
156}
157
159{
160 clear();
161 const VKShaderInterface &interface = unwrap(unwrap(immediate.shader))->interface_get();
162 AttributeMask occupied_attributes = 0;
163
164 fill_unused_bindings(interface, 0);
166
167 VKBufferWithOffset immediate_buffer = immediate.active_buffer();
169 nullptr,
170 &immediate_buffer,
171 immediate.vertex_len,
172 interface,
173 occupied_attributes);
174}
176 VKVertexBuffer *vertex_buffer,
177 VKBufferWithOffset *immediate_vertex_buffer,
178 const int64_t vertex_len,
179 const VKShaderInterface &interface,
180 AttributeMask &r_occupied_attributes)
181{
182 BLI_assert(vertex_buffer || immediate_vertex_buffer);
183 BLI_assert(!(vertex_buffer && immediate_vertex_buffer));
184
185 if (vertex_format.attr_len <= 0) {
186 return;
187 }
188
189 /* Interleaved offset is added to the buffer binding. Attribute offsets are hardware
190 * restricted (ref: VUID-VkVertexInputAttributeDescription-offset-00622). */
191 uint32_t buffer_offset = 0;
192 uint32_t attribute_offset = 0;
193 uint32_t stride = vertex_format.stride;
194
195 bool add_vbo = false;
196
197 for (uint32_t attribute_index = 0; attribute_index < vertex_format.attr_len; attribute_index++) {
198 const GPUVertAttr &attribute = vertex_format.attrs[attribute_index];
199 if (vertex_format.deinterleaved) {
200 buffer_offset += ((attribute_index == 0) ?
201 0 :
202 vertex_format.attrs[attribute_index - 1].type.size()) *
203 vertex_len;
204 stride = attribute.type.size();
205 }
206 else {
207 attribute_offset = attribute.offset;
208 }
209
210 for (uint32_t name_index = 0; name_index < attribute.name_len; name_index++) {
211 const char *name = GPU_vertformat_attr_name_get(&vertex_format, &attribute, name_index);
212 const ShaderInput *shader_input = interface.attr_get(name);
213 if (shader_input == nullptr || shader_input->location == -1) {
214 continue;
215 }
216
217 /* Don't overwrite attributes that are already occupied. */
218 AttributeMask attribute_mask = 1 << shader_input->location;
219 if (r_occupied_attributes & attribute_mask) {
220 continue;
221 }
222
223 r_occupied_attributes |= attribute_mask;
224 uint32_t target_binding = 0;
225
226 for (int i = 0; i < vertex_input.attributes.size(); i++) {
227 if (vertex_input.attributes[i].location == shader_input->location) {
228
229 vertex_input.attributes[i].offset = attribute_offset;
231 attribute.type.comp_type(), attribute.type.size(), attribute.type.fetch_mode());
232 vertex_input.bindings[i].stride = stride;
233
234 target_binding = vertex_input.attributes[i].binding;
235
236 break;
237 }
238 }
239
240 if (vertex_buffer) {
241 add_vbo = true;
242 vertex_buffer->upload();
243 buffers[target_binding] = {vertex_buffer->resource(), buffer_offset};
244 }
245 if (immediate_vertex_buffer) {
246 buffers[target_binding] = *immediate_vertex_buffer;
247 }
248 }
249 }
250
251 if (add_vbo) {
252 BLI_assert(vertex_buffer != nullptr);
253 vbos.append(vertex_buffer);
254 }
255}
256
258
259/* -------------------------------------------------------------------- */
262
264{
265 std::cout << __FILE__ << "::" << __func__ << "\n";
266 BitVector visited_bindings(vertex_input.bindings.size());
267
268 for (VkVertexInputAttributeDescription2EXT attribute : vertex_input.attributes) {
269 std::cout << " - attribute(binding=" << attribute.binding
270 << ", location=" << attribute.location << ")";
271
272 if (visited_bindings[attribute.binding]) {
273 std::cout << " WARNING: Already bound\n";
274 continue;
275 }
276 visited_bindings[attribute.binding].set(true);
277
278 if (attribute.binding < buffers.size()) {
279 std::cout << " Attach to Buffer\n";
280 }
281 else {
282 std::cout << " WARNING: Attach to dummy\n";
283 }
284 }
285}
286
288
289} // namespace blender::gpu
#define BLI_assert(a)
constexpr IndexRange()=default
const ShaderInput * attr_get(const StringRefNull name) const
static VKBackend & get()
VKVertexBuffer * vertex_buffer_get(int index) const
Definition vk_batch.hh:55
const VKResourceWithHandle< VkBuffer > & resource() const
Definition vk_buffer.hh:104
shader::Type get_attribute_type(int location) const
VKVertexAttributeObject & operator=(const VKVertexAttributeObject &other)
void update_bindings(const VKContext &context, VKBatch &batch)
void bind(render_graph::VKVertexBufferBindings &r_vertex_buffer_bindings) const
VKVertexInputDescriptionPool::Key vertex_input_key
void update_vertex_input_key(VKVertexInputDescriptionPool &pool)
const VKResourceWithHandle< VkBuffer > & resource() const
Pool with all used vertex input descriptions.
Key get_or_insert(VKVertexInputDescription &description)
Get the key of the given description. Will insert the description when it wasn't known.
#define interface_get(create_info, _res)
#define attribute(slot)
VkFormat to_vk_format(const TextureFormat format)
Definition vk_common.cc:157
BLI_INLINE MTLTexture * unwrap(Texture *tex)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
BLI_INLINE const char * GPU_vertformat_attr_name_get(const GPUVertFormat *format, const GPUVertAttr *attr, uint n_idx)
MINLINE int max_ii(int a, int b)
ATTR_WARN_UNUSED_RESULT const BMVert * v
unsigned int uint32_t
Definition BKE_text.h:81
struct blender::GPUVertAttr::Type type
GPUVertAttr attrs[GPU_VERT_ATTR_MAX_LEN]
Vector< VkVertexInputAttributeDescription2EXT > attributes
Vector< VkVertexInputBindingDescription2EXT > bindings
int size() const
Definition vector.hh:130