Blender V4.5
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"
22
23namespace blender::gpu {
24class VKFrameBuffer;
26class VKBatch;
27class VKStateManager;
28class VKShader;
29class VKThreadData;
30class VKDevice;
31
39
40class VKContext : public Context, NonCopyable {
41 friend class VKDevice;
42
43 private:
44 VkExtent2D vk_extent_ = {};
45 VkSurfaceFormatKHR swap_chain_format_ = {};
46 GPUTexture *surface_texture_ = nullptr;
47 void *ghost_context_;
48
49 /* Reusable data. Stored inside context to limit reallocations. */
50 render_graph::VKResourceAccessInfo access_info_ = {};
51
52 std::optional<std::reference_wrapper<VKThreadData>> thread_data_;
53 std::optional<std::reference_wrapper<render_graph::VKRenderGraph>> render_graph_;
54
55 /* Active shader specialization constants state. */
56 shader::SpecializationConstants constants_state_;
57
58 /* Debug scope timings. Adapted form GLContext::TimeQuery.
59 * Only supports CPU timings for now. */
60 struct ScopeTimings {
61 using Clock = std::chrono::steady_clock;
62 using TimePoint = Clock::time_point;
63 using Nanoseconds = std::chrono::nanoseconds;
64
65 std::string name;
66 bool finished;
67 TimePoint cpu_start, cpu_end;
68 };
69 Vector<ScopeTimings> scope_timings;
70
71 void process_frame_timings();
72
73 public:
75
77 {
78 return render_graph_.value().get();
79 }
81 {
82 return render_graph_.value().get();
83 }
84
85 VKContext(void *ghost_window, void *ghost_context);
86 virtual ~VKContext();
87
88 void activate() override;
89 void deactivate() override;
90 void begin_frame() override;
91 void end_frame() override;
92
93 void flush() override;
94
97 VkPipelineStageFlags wait_dst_stage_mask = VK_PIPELINE_STAGE_NONE,
98 VkSemaphore wait_semaphore = VK_NULL_HANDLE,
99 VkSemaphore signal_semaphore = VK_NULL_HANDLE,
100 VkFence signal_fence = VK_NULL_HANDLE);
101 void finish() override;
102
103 void memory_statistics_get(int *r_total_mem_kb, int *r_free_mem_kb) override;
104
105 void debug_group_begin(const char *, int) override;
106 void debug_group_end() override;
107 bool debug_capture_begin(const char *title) override;
108 void debug_capture_end() override;
109 void *debug_capture_scope_create(const char *name) override;
110 bool debug_capture_scope_begin(void *scope) override;
111 void debug_capture_scope_end(void *scope) override;
112
113 void debug_unbind_all_ubo() override;
114 void debug_unbind_all_ssbo() override;
115
116 bool has_active_framebuffer() const;
117 void activate_framebuffer(VKFrameBuffer &framebuffer);
120
128 void rendering_end();
129
131
136 void update_pipeline_data(GPUPrimType primitive,
138 render_graph::VKPipelineData &r_pipeline_data);
139
140 void sync_backbuffer(bool cycle_resource_pool);
141
142 static VKContext *get()
143 {
144 return static_cast<VKContext *>(Context::get());
145 }
146
150
151 static void swap_buffers_pre_callback(const GHOST_VulkanSwapChainData *data);
152 static void swap_buffers_post_callback();
153 static void openxr_acquire_framebuffer_image_callback(GHOST_VulkanOpenXRData *data);
154 static void openxr_release_framebuffer_image_callback(GHOST_VulkanOpenXRData *data);
155
157
158 private:
159 void swap_buffers_pre_handler(const GHOST_VulkanSwapChainData &data);
160 void swap_buffers_post_handler();
161
162 void openxr_acquire_framebuffer_image_handler(GHOST_VulkanOpenXRData &data);
163 void openxr_release_framebuffer_image_handler(GHOST_VulkanOpenXRData &data);
164
166 VkPipeline vk_pipeline,
167 render_graph::VKPipelineData &r_pipeline_data);
168};
169
171{
172 return static_cast<const void *>(&a) == static_cast<const void *>(&b);
173}
174
175} // 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:80
void debug_unbind_all_ssbo() override
void sync_backbuffer(bool cycle_resource_pool)
Definition vk_context.cc:55
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:25
VKDiscardPool discard_pool
Definition vk_context.hh:74
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
void flush() override
static void swap_buffers_post_callback()
VKFrameBuffer * active_framebuffer_get() const
void activate() override
const render_graph::VKRenderGraph & render_graph() const
Definition vk_context.hh:76
bool has_active_framebuffer() const
void debug_unbind_all_ubo() override
static void swap_buffers_pre_callback(const GHOST_VulkanSwapChainData *data)
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()
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)