Blender V5.3
vk_push_constants.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
19
20#pragma once
21
22#include "BLI_vector.hh"
23
25
26#include "vk_buffer.hh"
27#include "vk_descriptor_set.hh"
28
29namespace blender::gpu {
31class VKUniformBuffer;
32class VKContext;
33class VKDevice;
34
48 friend class VKContext;
49
50 public:
52 enum class StorageType {
55
57 PUSH_CONSTANTS,
58
62 BUFFER,
63 };
64
68 struct Layout {
71
72 struct PushConstant {
73 /* Used as lookup based on ShaderInput. */
75
81 };
82
83 private:
84 Vector<PushConstant> push_constants;
85 uint32_t size_in_bytes_ = 0;
86 StorageType storage_type_ = StorageType::NONE;
90 VKDescriptorSet::Location descriptor_set_location_;
91
92 public:
104 const VKDevice &device);
105
116 void init(const shader::ShaderCreateInfo &info,
117 const VKShaderInterface &interface,
118 StorageType storage_type,
120
125 {
126 return storage_type_;
127 }
128
135 {
136 return descriptor_set_location_;
137 }
138
143 {
144 return size_in_bytes_;
145 }
146
151 const PushConstant *find(int32_t location) const;
152
153 void debug_print() const;
154 };
155
156 private:
157 const Layout *layout_ = nullptr;
158 void *data_ = nullptr;
159
160 public:
162 VKPushConstants(const Layout *layout);
164 virtual ~VKPushConstants();
165
167
168 size_t offset() const
169 {
170 return 0;
171 }
172
173 const Layout &layout_get() const
174 {
175 return *layout_;
176 }
177
185 const void *data() const
186 {
187 return data_;
188 }
189
198 template<typename T>
200 int32_t comp_len,
201 int32_t array_size,
202 const T *input_data)
203 {
204 const Layout::PushConstant *push_constant_layout = layout_->find(location);
205 if (push_constant_layout == nullptr) {
206 /* Legacy code can still try to update push constants when they don't exist. For example
207 * `immDrawPixelsTexSetup` will bind an image slot manually. This works in OpenGL, but in
208 * vulkan images aren't stored as push constants. */
209 return;
210 }
211
212 uint8_t *bytes = static_cast<uint8_t *>(data_);
213 T *dst = static_cast<T *>(static_cast<void *>(&bytes[push_constant_layout->offset]));
214 const int inner_row_padding = push_constant_layout->inner_row_padding;
215 const bool is_tightly_std140_packed = (comp_len % 4) == 0;
216 /* Vec3[] are not tightly packed in std430. */
217 const bool is_tightly_std430_packed = comp_len != 3 || array_size == 0;
218 if (inner_row_padding == 0 &&
219 ((layout_->storage_type_get() == StorageType::PUSH_CONSTANTS &&
220 is_tightly_std430_packed) ||
221 array_size == 0 || push_constant_layout->array_size == 0 || is_tightly_std140_packed))
222 {
223 const size_t copy_size_in_bytes = comp_len * max_ii(array_size, 1) * sizeof(T);
224 BLI_assert_msg(push_constant_layout->offset + copy_size_in_bytes <= layout_->size_in_bytes(),
225 "Tried to write outside the push constant allocated memory.");
226 memcpy(dst, input_data, copy_size_in_bytes);
227 return;
228 }
229
230 /* Store elements in buffer as array. In Std140 arrays have an element stride of 16
231 * bytes. */
232 BLI_assert(sizeof(T) == 4);
233 const T *src = input_data;
234 if (inner_row_padding == 0) {
235 for (const int i : IndexRange(array_size)) {
236 UNUSED_VARS(i);
237 memcpy(dst, src, comp_len * sizeof(T));
238 src += comp_len;
239 dst += 4;
240 }
241 }
242 else {
243 BLI_assert_msg(array_size == 1, "No support for MAT3 arrays, but can be added when needed");
244 for (const int component_index : IndexRange(comp_len)) {
245 *dst = *src;
246 dst += 1;
247 src += 1;
248 if ((component_index % inner_row_padding) == (inner_row_padding - 1)) {
249 dst += 1;
250 }
251 }
252 }
253 }
254
261};
262
263} // namespace blender::gpu
#define UNUSED_VARS(...)
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
VKBufferWithOffset update_uniform_buffer(VKContext &context)
VKPushConstants & operator=(VKPushConstants &&other)
const Layout & layout_get() const
void push_constant_set(int32_t location, int32_t comp_len, int32_t array_size, const T *input_data)
#define T
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned int uint
Definition BKE_text.h:55
MINLINE int max_ii(int a, int b)
unsigned int uint32_t
Definition BKE_text.h:81
unsigned char uint8_t
Definition BKE_text.h:79
signed int int32_t
Definition BKE_text.h:78
static constexpr StorageType STORAGE_TYPE_DEFAULT
static constexpr StorageType STORAGE_TYPE_FALLBACK
static StorageType determine_storage_type(const shader::ShaderCreateInfo &info, const VKDevice &device)
VKDescriptorSet::Location descriptor_set_location_get() const
const PushConstant * find(int32_t location) const
void init(const shader::ShaderCreateInfo &info, const VKShaderInterface &interface, StorageType storage_type, VKDescriptorSet::Location location)
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...