Blender V5.0
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_utildefines.h"
12
14
15#include "GHOST_Types.h"
16
18#include "vk_common.hh"
19#include "vk_debug.hh"
21#include "vk_resource_pool.hh"
23
24namespace blender::gpu {
25class VKFrameBuffer;
27class VKBatch;
28class VKStateManager;
29class VKShader;
30class VKThreadData;
31class VKDevice;
32
40
41class VKContext : public Context, NonCopyable {
42 friend class VKDevice;
43
44 private:
45 VkExtent2D vk_extent_ = {};
46 VkSurfaceFormatKHR swap_chain_format_ = {};
47 gpu::Texture *surface_texture_ = nullptr;
48 void *ghost_context_;
49
51
52 /* Reusable data. Stored inside context to limit reallocations. */
53 render_graph::VKResourceAccessInfo access_info_ = {};
54
55 std::optional<std::reference_wrapper<VKThreadData>> thread_data_;
56 std::optional<std::reference_wrapper<render_graph::VKRenderGraph>> render_graph_;
57
58 /* Active shader specialization constants state. */
59 shader::SpecializationConstants constants_state_;
60
61 /* Debug scope timings. Adapted form GLContext::TimeQuery.
62 * Only supports CPU timings for now. */
63 struct ScopeTimings {
64 using Clock = std::chrono::steady_clock;
65 using TimePoint = Clock::time_point;
66 using Nanoseconds = std::chrono::nanoseconds;
67
68 std::string name;
69 bool finished;
70 TimePoint cpu_start, cpu_end;
71 };
72 Vector<ScopeTimings> scope_timings;
73
74 void process_frame_timings();
75
76 public:
78
80 {
81 return render_graph_.value().get();
82 }
84 {
85 return render_graph_.value().get();
86 }
87
88 VKContext(void *ghost_window, void *ghost_context);
89 virtual ~VKContext();
90
91 void activate() override;
92 void deactivate() override;
93 void begin_frame() override;
94 void end_frame() override;
95
96 void flush() override;
97
100 VkPipelineStageFlags wait_dst_stage_mask = VK_PIPELINE_STAGE_NONE,
101 VkSemaphore wait_semaphore = VK_NULL_HANDLE,
102 VkSemaphore signal_semaphore = VK_NULL_HANDLE,
103 VkFence signal_fence = VK_NULL_HANDLE);
104 void finish() override;
105
106 void memory_statistics_get(int *r_total_mem_kb, int *r_free_mem_kb) override;
107
108 void debug_group_begin(const char *, int) override;
109 void debug_group_end() override;
110 bool debug_capture_begin(const char *title) override;
111 void debug_capture_end() override;
112 void *debug_capture_scope_create(const char *name) override;
113 bool debug_capture_scope_begin(void *scope) override;
114 void debug_capture_scope_end(void *scope) override;
115
116 void debug_unbind_all_ubo() override;
117 void debug_unbind_all_ssbo() override;
118
119 bool has_active_framebuffer() const;
120 void activate_framebuffer(VKFrameBuffer &framebuffer);
123
131 void rendering_end();
132
134
139 void update_pipeline_data(GPUPrimType primitive,
141 render_graph::VKPipelineDataGraphics &r_pipeline_data);
142
143 void sync_backbuffer();
144
145 static VKContext *get()
146 {
147 return static_cast<VKContext *>(Context::get());
148 }
149
153
154 static void swap_buffer_draw_callback(const GHOST_VulkanSwapChainData *data);
155 static void swap_buffer_acquired_callback();
156 static void openxr_acquire_framebuffer_image_callback(GHOST_VulkanOpenXRData *data);
157 static void openxr_release_framebuffer_image_callback(GHOST_VulkanOpenXRData *data);
158
160
161 std::unique_ptr<VKStreamingBuffer> &get_or_create_streaming_buffer(
162 VKBuffer &buffer, VkDeviceSize min_offset_alignment);
163
164 private:
165 void swap_buffer_draw_handler(const GHOST_VulkanSwapChainData &data);
166 void swap_buffer_acquired_handler();
167
168 void openxr_acquire_framebuffer_image_handler(GHOST_VulkanOpenXRData &data);
169 void openxr_release_framebuffer_image_handler(GHOST_VulkanOpenXRData &data);
170
172 VkPipeline vk_pipeline,
173 render_graph::VKPipelineData &r_pipeline_data);
174};
175
177{
178 return static_cast<const void *>(&a) == static_cast<const void *>(&b);
179}
180
181} // namespace blender::gpu
#define BLI_INLINE
#define ENUM_OPERATORS(_type, _max)
GPUPrimType
BMesh const char void * data
NonCopyable(const NonCopyable &other)=delete
static Context * get()
render_graph::VKRenderGraph & render_graph()
Definition vk_context.hh:83
void debug_unbind_all_ssbo() override
render_graph::VKResourceAccessInfo & reset_and_get_access_info()
void debug_group_begin(const char *, int) override
Definition vk_debug.cc:24
void debug_group_end() override
Definition vk_debug.cc:40
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
VKContext(void *ghost_window, void *ghost_context)
Definition vk_context.cc:29
VKDiscardPool discard_pool
Definition vk_context.hh:77
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:92
static void swap_buffer_draw_callback(const GHOST_VulkanSwapChainData *data)
void flush() override
VKFrameBuffer * active_framebuffer_get() const
void activate() override
const render_graph::VKRenderGraph & render_graph() const
Definition vk_context.hh:79
bool has_active_framebuffer() const
void debug_unbind_all_ubo() override
void debug_capture_end() override
Definition vk_debug.cc:113
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)
uint64_t TimelineValue
Definition vk_common.hh:36
bool operator==(const GPUState &a, const GPUState &b)
const char * name