Avoid manual memory management of certain arrays in Vulkan RD

This commit is contained in:
Pedro J. Estébanez 2022-05-16 18:52:39 +02:00
parent 3d58b79792
commit a82352c7e3
3 changed files with 21 additions and 21 deletions

View file

@ -38,7 +38,9 @@
#include <initializer_list> #include <initializer_list>
template <class T, class U = uint32_t, bool force_trivial = false> // If tight, it grows strictly as much as needed.
// Otherwise, it grows exponentially (the default and what you want in most cases).
template <class T, class U = uint32_t, bool force_trivial = false, bool tight = false>
class LocalVector { class LocalVector {
private: private:
U count = 0; U count = 0;
@ -121,7 +123,7 @@ public:
_FORCE_INLINE_ bool is_empty() const { return count == 0; } _FORCE_INLINE_ bool is_empty() const { return count == 0; }
_FORCE_INLINE_ U get_capacity() const { return capacity; } _FORCE_INLINE_ U get_capacity() const { return capacity; }
_FORCE_INLINE_ void reserve(U p_size) { _FORCE_INLINE_ void reserve(U p_size) {
p_size = nearest_power_of_2_templated(p_size); p_size = tight ? p_size : nearest_power_of_2_templated(p_size);
if (p_size > capacity) { if (p_size > capacity) {
capacity = p_size; capacity = p_size;
data = (T *)memrealloc(data, capacity * sizeof(T)); data = (T *)memrealloc(data, capacity * sizeof(T));
@ -262,4 +264,7 @@ public:
} }
}; };
template <class T, class U = uint32_t, bool force_trivial = false>
using TightLocalVector = LocalVector<T, U, force_trivial, true>;
#endif // LOCAL_VECTOR_H #endif // LOCAL_VECTOR_H

View file

@ -8753,7 +8753,7 @@ void RenderingDeviceVulkan::_begin_frame() {
} }
if (frames[frame].timestamp_count) { if (frames[frame].timestamp_count) {
vkGetQueryPoolResults(device, frames[frame].timestamp_pool, 0, frames[frame].timestamp_count, sizeof(uint64_t) * max_timestamp_query_elements, frames[frame].timestamp_result_values, sizeof(uint64_t), VK_QUERY_RESULT_64_BIT); vkGetQueryPoolResults(device, frames[frame].timestamp_pool, 0, frames[frame].timestamp_count, sizeof(uint64_t) * max_timestamp_query_elements, frames[frame].timestamp_result_values.ptr(), sizeof(uint64_t), VK_QUERY_RESULT_64_BIT);
vkCmdResetQueryPool(frames[frame].setup_command_buffer, frames[frame].timestamp_pool, 0, frames[frame].timestamp_count); vkCmdResetQueryPool(frames[frame].setup_command_buffer, frames[frame].timestamp_pool, 0, frames[frame].timestamp_count);
SWAP(frames[frame].timestamp_names, frames[frame].timestamp_result_names); SWAP(frames[frame].timestamp_names, frames[frame].timestamp_result_names);
SWAP(frames[frame].timestamp_cpu_values, frames[frame].timestamp_cpu_result_values); SWAP(frames[frame].timestamp_cpu_values, frames[frame].timestamp_cpu_result_values);
@ -9044,7 +9044,7 @@ void RenderingDeviceVulkan::initialize(VulkanContext *p_context, bool p_local_de
vmaCreateAllocator(&allocatorInfo, &allocator); vmaCreateAllocator(&allocatorInfo, &allocator);
} }
frames = memnew_arr(Frame, frame_count); frames.resize(frame_count);
frame = 0; frame = 0;
//create setup and frame buffers //create setup and frame buffers
for (int i = 0; i < frame_count; i++) { for (int i = 0; i < frame_count; i++) {
@ -9090,12 +9090,12 @@ void RenderingDeviceVulkan::initialize(VulkanContext *p_context, bool p_local_de
vkCreateQueryPool(device, &query_pool_create_info, nullptr, &frames[i].timestamp_pool); vkCreateQueryPool(device, &query_pool_create_info, nullptr, &frames[i].timestamp_pool);
frames[i].timestamp_names = memnew_arr(String, max_timestamp_query_elements); frames[i].timestamp_names.resize(max_timestamp_query_elements);
frames[i].timestamp_cpu_values = memnew_arr(uint64_t, max_timestamp_query_elements); frames[i].timestamp_cpu_values.resize(max_timestamp_query_elements);
frames[i].timestamp_count = 0; frames[i].timestamp_count = 0;
frames[i].timestamp_result_names = memnew_arr(String, max_timestamp_query_elements); frames[i].timestamp_result_names.resize(max_timestamp_query_elements);
frames[i].timestamp_cpu_result_values = memnew_arr(uint64_t, max_timestamp_query_elements); frames[i].timestamp_cpu_result_values.resize(max_timestamp_query_elements);
frames[i].timestamp_result_values = memnew_arr(uint64_t, max_timestamp_query_elements); frames[i].timestamp_result_values.resize(max_timestamp_query_elements);
frames[i].timestamp_result_count = 0; frames[i].timestamp_result_count = 0;
} }
} }
@ -9496,18 +9496,13 @@ void RenderingDeviceVulkan::finalize() {
_free_pending_resources(f); _free_pending_resources(f);
vkDestroyCommandPool(device, frames[i].command_pool, nullptr); vkDestroyCommandPool(device, frames[i].command_pool, nullptr);
vkDestroyQueryPool(device, frames[i].timestamp_pool, nullptr); vkDestroyQueryPool(device, frames[i].timestamp_pool, nullptr);
memdelete_arr(frames[i].timestamp_names);
memdelete_arr(frames[i].timestamp_cpu_values);
memdelete_arr(frames[i].timestamp_result_names);
memdelete_arr(frames[i].timestamp_result_values);
memdelete_arr(frames[i].timestamp_cpu_result_values);
} }
for (int i = 0; i < split_draw_list_allocators.size(); i++) { for (int i = 0; i < split_draw_list_allocators.size(); i++) {
vkDestroyCommandPool(device, split_draw_list_allocators[i].command_pool, nullptr); vkDestroyCommandPool(device, split_draw_list_allocators[i].command_pool, nullptr);
} }
memdelete_arr(frames); frames.clear();
for (int i = 0; i < staging_buffer_blocks.size(); i++) { for (int i = 0; i < staging_buffer_blocks.size(); i++) {
vmaDestroyBuffer(allocator, staging_buffer_blocks[i].buffer, staging_buffer_blocks[i].allocation); vmaDestroyBuffer(allocator, staging_buffer_blocks[i].buffer, staging_buffer_blocks[i].allocation);

View file

@ -994,19 +994,19 @@ class RenderingDeviceVulkan : public RenderingDevice {
VkQueryPool timestamp_pool; VkQueryPool timestamp_pool;
String *timestamp_names = nullptr; TightLocalVector<String> timestamp_names;
uint64_t *timestamp_cpu_values = nullptr; TightLocalVector<uint64_t> timestamp_cpu_values;
uint32_t timestamp_count = 0; uint32_t timestamp_count = 0;
String *timestamp_result_names = nullptr; TightLocalVector<String> timestamp_result_names;
uint64_t *timestamp_cpu_result_values = nullptr; TightLocalVector<uint64_t> timestamp_cpu_result_values;
uint64_t *timestamp_result_values = nullptr; TightLocalVector<uint64_t> timestamp_result_values;
uint32_t timestamp_result_count = 0; uint32_t timestamp_result_count = 0;
uint64_t index = 0; uint64_t index = 0;
}; };
uint32_t max_timestamp_query_elements = 0; uint32_t max_timestamp_query_elements = 0;
Frame *frames = nullptr; //frames available, for main device they are cycled (usually 3), for local devices only 1 TightLocalVector<Frame> frames; //frames available, for main device they are cycled (usually 3), for local devices only 1
int frame = 0; //current frame int frame = 0; //current frame
int frame_count = 0; //total amount of frames int frame_count = 0; //total amount of frames
uint64_t frames_drawn = 0; uint64_t frames_drawn = 0;