Blender V5.3
vk_context.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_enum_flags.hh"
12
14
15#include "GHOST_Types.hh"
16
18#include "vk_buffer_pool.hh"
19#include "vk_common.hh"
20#include "vk_debug.hh"
22#include "vk_resource_pool.hh"
24
25namespace blender::gpu {
26class VKFrameBuffer;
27class VKBatch;
28class VKStateManager;
29class VKShader;
30class VKThreadData;
31class VKDevice;
32
41
42class VKContext : public Context, NonCopyable {
43 friend class VKDevice;
44
45 private:
46 VkExtent2D vk_extent_ = {};
47 VkSurfaceFormatKHR swap_chain_format_ = {};
48 gpu::Texture *surface_texture_ = nullptr;
49 GHOST_IContext *ghost_context_;
50
52
53 /* Reusable data. Stored inside context to limit reallocations. */
54 render_graph::VKResourceAccessInfo access_info_ = {};
55
56 std::optional<std::reference_wrapper<VKThreadData>> thread_data_;
57 std::optional<std::reference_wrapper<render_graph::VKRenderGraph>> render_graph_;
58
59 /* Active shader specialization constants state. */
60 shader::SpecializationConstants constants_state_;
61
62 /* Debug scope timings. Adapted form GLContext::TimeQuery.
63 * Only supports CPU timings for now. */
64 struct ScopeTimings {
65 using Clock = std::chrono::steady_clock;
66 using TimePoint = Clock::time_point;
67 using Nanoseconds = std::chrono::nanoseconds;
68
69 std::string name;
70 bool finished;
71 TimePoint cpu_start, cpu_end;
72 };
73 Vector<ScopeTimings> scope_timings;
74
75 void process_frame_timings();
76
77 public:
80
82 {
83 return render_graph_.value().get();
84 }
86 {
87 return render_graph_.value().get();
88 }
89
90 VKContext(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context);
91 virtual ~VKContext();
92
93 void activate() override;
94 void deactivate() override;
95 void begin_frame() override;
96 void end_frame() override;
97
98 void flush() override;
99
102 VkPipelineStageFlags wait_dst_stage_mask = VK_PIPELINE_STAGE_NONE,
103 VkSemaphore wait_semaphore = VK_NULL_HANDLE,
104 VkSemaphore signal_semaphore = VK_NULL_HANDLE,
105 VkFence signal_fence = VK_NULL_HANDLE);
106 void finish() override;
107
108 void memory_statistics_get(int *r_total_mem_kb, int *r_free_mem_kb) override;
109
110 void debug_group_begin(const char *name, int index) override;
111 void debug_group_end() override;
112 bool debug_capture_begin(const char *title) override;
113 void debug_capture_end() override;
114 void *debug_capture_scope_create(const char *name) override;
115 bool debug_capture_scope_begin(void *scope) override;
116 void debug_capture_scope_end(void *scope) override;
117
118 void debug_unbind_all_ubo() override;
119 void debug_unbind_all_ssbo() override;
120
121 bool has_active_framebuffer() const;
122 void activate_framebuffer(VKFrameBuffer &framebuffer);
125
133 void rendering_end();
134
136
141 void update_pipeline_data(const VKFrameBuffer &framebuffer,
142 GPUPrimType primitive,
143 VKVertexInputDescriptionPool::Key vertex_input_key,
144 render_graph::VKPipelineDataGraphics &r_pipeline_data);
145
146 void sync_backbuffer();
147
148 static VKContext *get()
149 {
150 return static_cast<VKContext *>(Context::get());
151 }
152
156
157 static void swap_buffer_draw_callback(const GHOST_VulkanSwapChainData *data,
158 bool wait_for_submission);
159 static void swap_buffer_acquired_callback();
160 static void openxr_acquire_framebuffer_image_callback(GHOST_VulkanOpenXRData *data);
161 static void openxr_release_framebuffer_image_callback(GHOST_VulkanOpenXRData *data);
162
164
165 std::unique_ptr<VKStreamingBuffer> &get_or_create_streaming_buffer(
166 VKBuffer &buffer, VkDeviceSize min_offset_alignment);
167
168 private:
169 void swap_buffer_draw_handler(const GHOST_VulkanSwapChainData &data, bool wait_for_submission);
170 void swap_buffer_acquired_handler();
171
172 void openxr_acquire_framebuffer_image_handler(GHOST_VulkanOpenXRData &data);
173 void openxr_release_framebuffer_image_handler(GHOST_VulkanOpenXRData &data);
174
176 VkPipeline vk_pipeline,
177 render_graph::VKPipelineData &r_pipeline_data);
178};
179
181{
182 return static_cast<const void *>(&a) == static_cast<const void *>(&b);
183}
184
185} // namespace blender::gpu
#define BLI_INLINE
void activate(bool forceActivation=false) const
NonCopyable(const NonCopyable &other)=delete
static Context * get()
A pool of VkBuffers.
render_graph::VKRenderGraph & render_graph()
Definition vk_context.hh:85
static void swap_buffer_draw_callback(const GHOST_VulkanSwapChainData *data, bool wait_for_submission)
VKContext(GHOST_IWindow *ghost_window, GHOST_IContext *ghost_context)
Definition vk_context.cc:28
void debug_unbind_all_ssbo() override
void debug_group_begin(const char *name, int index) override
Definition vk_debug.cc:27
render_graph::VKResourceAccessInfo & reset_and_get_access_info()
void debug_group_end() override
Definition vk_debug.cc:44
VKBufferPool push_constants_pool
Definition vk_context.hh:79
void finish() override
static void openxr_acquire_framebuffer_image_callback(GHOST_VulkanOpenXRData *data)
bool debug_capture_scope_begin(void *scope) override
Definition vk_debug.cc:132
void specialization_constants_set(const shader::SpecializationConstants *constants_state)
void deactivate() override
void memory_statistics_get(int *r_total_mem_kb, int *r_free_mem_kb) override
VKDiscardPool discard_pool
Definition vk_context.hh:78
static void swap_buffer_acquired_callback()
void * debug_capture_scope_create(const char *name) override
Definition vk_debug.cc:127
static void openxr_release_framebuffer_image_callback(GHOST_VulkanOpenXRData *data)
bool debug_capture_begin(const char *title) override
Definition vk_debug.cc:96
void flush() override
VKFrameBuffer * active_framebuffer_get() const
const render_graph::VKRenderGraph & render_graph() const
Definition vk_context.hh:81
bool has_active_framebuffer() const
void debug_unbind_all_ubo() override
void debug_capture_end() override
Definition vk_debug.cc:115
void activate_framebuffer(VKFrameBuffer &framebuffer)
void debug_capture_scope_end(void *scope) override
Definition vk_debug.cc:146
VKStateManager & state_manager_get() const
VKDescriptorSetTracker & descriptor_set_get()
std::unique_ptr< VKStreamingBuffer > & get_or_create_streaming_buffer(VKBuffer &buffer, VkDeviceSize min_offset_alignment)
void update_pipeline_data(render_graph::VKPipelineData &r_pipeline_data)
static VKContext * get()
void begin_frame() override
VKDescriptorPools & descriptor_pools_get()
void end_frame() override
TimelineValue flush_render_graph(RenderGraphFlushFlags flags, VkPipelineStageFlags wait_dst_stage_mask=VK_PIPELINE_STAGE_NONE, VkSemaphore wait_semaphore=VK_NULL_HANDLE, VkSemaphore signal_semaphore=VK_NULL_HANDLE, VkFence signal_fence=VK_NULL_HANDLE)
std::chrono::high_resolution_clock Clock
uint64_t TimelineValue
Definition vk_common.hh:39
bool operator==(const GPUState &a, const GPUState &b)
ENUM_OPERATORS(ShaderStage)
std::chrono::nanoseconds Nanoseconds
Definition BLI_timeit.hh:21
Clock::time_point TimePoint
Definition BLI_timeit.hh:20
BMesh const char void * data