Blender V5.3
vk_render_graph.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
37
38#pragma once
39
40#include <mutex>
41#include <optional>
42#include <pthread.h>
43
44#include "BKE_global.hh"
45
46#include "BLI_bit_vector.hh"
47#include "BLI_color_types.hh"
48#include "BLI_map.hh"
49#include "BLI_utility_mixins.hh"
50#include "BLI_vector.hh"
51#include "BLI_vector_set.hh"
52
53#include "BKE_global.hh"
54
55#include "vk_common.hh"
56
58#include "vk_command_builder.hh"
61
63class VKScheduler;
64class VKRenderGraph;
65
72template<typename NodeInfo> struct VKNodeData {
74 typename NodeInfo::Data &data;
75
76 void finalize(VKRenderGraph &render_graph, const typename NodeInfo::CreateInfo &create_info);
77};
78
79class VKRenderGraph : public NonCopyable {
80 friend class VKCommandBuilder;
81 friend class VKScheduler;
82 using DebugGroupNameID = int64_t;
83 using DebugGroupID = int64_t;
84
90 VKRenderGraphLinks links_;
92 VKRenderGraphStorage storage_;
93
102 VKResourceStateTracker &resources_;
106 BitVector<> resource_usages_;
107
108 struct DebugGroup {
109 std::string name;
111
112 friend bool operator==(const DebugGroup &a, const DebugGroup &b) = default;
113
114 uint64_t hash() const
115 {
117 }
118 };
119
120 struct {
122
124 Vector<DebugGroupNameID> group_stack;
125
130 bool group_used = false;
131
134
143 Vector<DebugGroupID> node_group_map;
144 } debug_;
145
146 public:
153 VKRenderGraph(VKResourceStateTracker &resources);
154
161 template<typename NodeInfo> VKNodeData<NodeInfo> alloc_node()
162 {
163 static VKRenderGraphNode node_template = {};
164 NodeHandle node_handle = nodes_.append_and_get_index(node_template);
165 VKRenderGraphNode &node = nodes_[node_handle];
166 typename NodeInfo::Data &data = node.alloc_node_data<NodeInfo>(storage_);
167 return {node_handle, data};
168 }
169
175 template<typename NodeInfo>
176 void finalize_node(NodeHandle node_handle, const typename NodeInfo::CreateInfo &create_info)
177 {
178 std::scoped_lock lock(resources_.mutex);
179 VKRenderGraphNode &node = nodes_[node_handle];
180 BLI_assert(node.type == NodeInfo::node_type);
181
182 if (G.debug & G_DEBUG_GPU) {
183 if (!debug_.group_used) {
184 debug_.group_used = true;
185 debug_.used_groups.append(debug_.group_stack);
186 }
187 if (nodes_.size() > debug_.node_group_map.size()) {
188 debug_.node_group_map.resize(nodes_.size());
189 }
190 debug_.node_group_map[node_handle] = debug_.used_groups.size() - 1;
191 }
192
193 node.template finalize_node<NodeInfo>(storage_, resources_, links_, create_info);
194 ref_resources(node);
195 }
196
197 private:
201 template<typename NodeInfo> NodeHandle add_node(const typename NodeInfo::CreateInfo &create_info)
202 {
203 std::scoped_lock lock(resources_.mutex);
204 static VKRenderGraphNode node_template = {};
205 NodeHandle node_handle = nodes_.append_and_get_index(node_template);
206#if 0
207 /* Useful during debugging. When a validation error occurs during submission we know the node
208 * type and node handle, but we don't know when and by who that specific node was added to the
209 * render graph. By enabling this part of the code and set the correct node_handle and node
210 * type a debugger can break at the moment the node has been added to the render graph. */
211 if (node_handle == 267 && NodeInfo::node_type == VKNodeType::DRAW) {
212 std::cout << "break\n";
213 }
214#endif
215 VKRenderGraphNode &node = nodes_[node_handle];
216 node.set_node_data<NodeInfo>(storage_, create_info);
217 node.build_links<NodeInfo>(resources_, links_, create_info);
218 ref_resources(node);
219
220 if (G.debug & G_DEBUG_GPU) {
221 if (!debug_.group_used) {
222 debug_.group_used = true;
223 debug_.used_groups.append(debug_.group_stack);
224 }
225 if (nodes_.size() > debug_.node_group_map.size()) {
226 debug_.node_group_map.resize(nodes_.size());
227 }
228 debug_.node_group_map[node_handle] = debug_.used_groups.size() - 1;
229 }
230 return node_handle;
231 }
232
237 void ref_resources(const VKRenderGraphNode &node)
238 {
239 BLI_assert_msg(resource_usages_.size() <= resources_.resources_.size(),
240 "State tracker resources can only grow.");
241 resource_usages_.resize(resources_.resources_.size());
242 for (const VKRenderGraphBuffer &buffer : links_.buffers.as_span().slice(node.links.buffers)) {
243 ResourceHandle handle = buffer.resource.handle;
244 if (resource_usages_[handle]) {
245 continue;
246 }
247 resource_usages_[handle].set();
248 resources_.ref_resource_impl(handle);
249 }
250 for (const VKRenderGraphImage &image : links_.images.as_span().slice(node.links.images)) {
251 ResourceHandle handle = image.resource.handle;
252 if (resource_usages_[handle]) {
253 continue;
254 }
255 resource_usages_[handle].set();
256 resources_.ref_resource_impl(handle);
257 }
258 }
259
260 public:
261#define ADD_NODE(NODE_CLASS) \
262 NodeHandle add_node(const NODE_CLASS::CreateInfo &create_info) \
263 { \
264 return add_node<NODE_CLASS>(create_info); \
265 }
266 ADD_NODE(VKBeginQueryNode)
267 ADD_NODE(VKBeginRenderingNode)
268 ADD_NODE(VKBuildAccelerationStructureNode)
269 ADD_NODE(VKEndQueryNode)
270 ADD_NODE(VKEndRenderingNode)
271 ADD_NODE(VKClearAttachmentsNode)
272 ADD_NODE(VKClearColorImageNode)
273 ADD_NODE(VKClearDepthStencilImageNode)
274 ADD_NODE(VKFillBufferNode)
275 ADD_NODE(VKCopyBufferNode)
276 ADD_NODE(VKCopyBufferToImageNode)
277 ADD_NODE(VKCopyImageNode)
278 ADD_NODE(VKCopyImageToBufferNode)
279 ADD_NODE(VKBlitImageNode)
280 ADD_NODE(VKDispatchNode)
281 ADD_NODE(VKDispatchIndirectNode)
282 ADD_NODE(VKResetQueryPoolNode)
283 ADD_NODE(VKUpdateBufferNode)
284 ADD_NODE(VKUpdateMipmapsNode)
285 ADD_NODE(VKSynchronizationNode)
286#undef ADD_NODE
287
295 {
296 VKRenderGraphNode &node = nodes_[node_handle];
298 return node.copy_buffer;
299 }
300
307 {
308 int64_t start = storage_.push_constants.size();
309 storage_.push_constants.extend(push_constants);
310 return IndexRange::from_begin_size(start, push_constants.size());
311 }
312
318 void debug_group_begin(const char *name, const ColorTheme4f &color);
319
326 void debug_group_end();
327
332 std::string full_debug_group(NodeHandle node_handle) const;
333
342 {
343 return nodes_.size();
344 }
345
346 bool is_empty()
347 {
348 return nodes_.is_empty();
349 }
350
351 void debug_print(NodeHandle node_handle) const;
352
358 void reset();
359
366 {
367 resources_.deref_resources(resource_usages_);
368 resource_usages_.fill(false);
369 }
370
371 void memstats() const;
372
375 {
376 return links_.images.as_span().slice(node.links.images);
377 }
378
379 inline Span<VKRenderGraphImage> linked_images(const NodeHandle node_handle) const
380 {
381 return linked_images(nodes_[node_handle]);
382 }
383
385 {
386 return links_.buffers.as_span().slice(node.links.buffers);
387 }
388
390 {
391 return linked_buffers(nodes_[node_handle]);
392 }
393
394 private:
395};
396
397template<typename NodeInfo>
399 const typename NodeInfo::CreateInfo &create_info)
400{
401 render_graph.finalize_node<NodeInfo>(node_handle, create_info);
402}
403
404} // namespace blender::gpu::render_graph
volatile int lock
#define BLI_assert_msg(a, msg)
void reset()
static constexpr IndexRange from_begin_size(const int64_t begin, const int64_t size)
constexpr int64_t size() const
Definition BLI_span.hh:256
NonCopyable(const NonCopyable &other)=delete
Span< VKRenderGraphImage > linked_images(const NodeHandle node_handle) const
VKRenderGraph(VKResourceStateTracker &resources)
Span< VKRenderGraphBuffer > linked_buffers(const VKRenderGraphNode &node) const
Span< VKRenderGraphImage > linked_images(const VKRenderGraphNode &node) const
void debug_group_begin(const char *name, const ColorTheme4f &color)
VKCopyBufferNode::Data & get_node_data(NodeHandle node_handle)
Span< VKRenderGraphBuffer > linked_buffers(const NodeHandle node_handle) const
void finalize_node(NodeHandle node_handle, const typename NodeInfo::CreateInfo &create_info)
void debug_print(NodeHandle node_handle) const
std::string full_debug_group(NodeHandle node_handle) const
IndexRange copy_push_constants(Span< uint8_t > push_constants)
#define image(slot, qualifiers, format)
bool operator==(const GPUState &a, const GPUState &b)
uint64_t ResourceHandle
Definition vk_common.hh:68
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
Global G
Definition blender.cc:57
BMesh const char void * data
ColorTheme4< float > ColorTheme4f
constexpr uint64_t get_default_hash(const T &v, const Args &...args)
Definition BLI_hash.hh:252
#define hash
Definition noise_c.cc:156
void finalize(VKRenderGraph &render_graph, const typename NodeInfo::CreateInfo &create_info)
int size() const
Definition vector.hh:130
#define ADD_NODE(NODE_CLASS)