Don't call new every frame on Vulkan
This commit is contained in:
parent
5d7e98c4be
commit
8361590294
2 changed files with 24 additions and 19 deletions
|
@ -8,6 +8,8 @@
|
|||
#include "gfx.hpp"
|
||||
#include "gfx_vulkan_constants.hpp"
|
||||
|
||||
class GFXVulkanCommandBuffer;
|
||||
|
||||
struct NativeSurface {
|
||||
platform::window_ptr identifier = nullptr;
|
||||
|
||||
|
@ -28,7 +30,7 @@ struct NativeSurface {
|
|||
VkRenderPass swapchainRenderPass = VK_NULL_HANDLE;
|
||||
|
||||
std::vector<VkFramebuffer> swapchainFramebuffers;
|
||||
std::vector<VkCommandBuffer> commandBuffers;
|
||||
std::vector<GFXVulkanCommandBuffer*> gfx_command_buffers;
|
||||
};
|
||||
|
||||
class GFXVulkanPipeline;
|
||||
|
|
|
@ -1240,9 +1240,9 @@ GFXSize GFXVulkan::get_alignment(GFXSize size) {
|
|||
}
|
||||
|
||||
GFXCommandBuffer* GFXVulkan::acquire_command_buffer(bool for_presentation_use) {
|
||||
auto cmdbuf = new GFXVulkanCommandBuffer();
|
||||
|
||||
if(!for_presentation_use) {
|
||||
auto cmdbuf = new GFXVulkanCommandBuffer();
|
||||
|
||||
VkCommandBufferAllocateInfo info = {};
|
||||
info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||
info.commandPool = commandPool;
|
||||
|
@ -1250,9 +1250,12 @@ GFXCommandBuffer* GFXVulkan::acquire_command_buffer(bool for_presentation_use) {
|
|||
info.commandBufferCount = 1;
|
||||
|
||||
vkAllocateCommandBuffers(device, &info, &cmdbuf->handle);
|
||||
|
||||
return cmdbuf;
|
||||
} else {
|
||||
native_surfaces[0]->gfx_command_buffers[native_surfaces[0]->currentFrame]->commands.clear();
|
||||
return native_surfaces[0]->gfx_command_buffers[native_surfaces[0]->currentFrame];
|
||||
}
|
||||
|
||||
return cmdbuf;
|
||||
}
|
||||
|
||||
void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const platform::window_ptr identifier) {
|
||||
|
@ -1274,11 +1277,8 @@ void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const platform::window_
|
|||
VkCommandBuffer cmd = VK_NULL_HANDLE;
|
||||
|
||||
auto cmdbuf = (GFXVulkanCommandBuffer*)command_buffer;
|
||||
if(cmdbuf->handle != VK_NULL_HANDLE)
|
||||
cmd = cmdbuf->handle;
|
||||
else if(current_surface != nullptr)
|
||||
cmd = current_surface->commandBuffers[current_surface-> currentFrame];
|
||||
|
||||
cmd = cmdbuf->handle;
|
||||
|
||||
if(cmd == nullptr)
|
||||
return;
|
||||
|
||||
|
@ -2124,18 +2124,21 @@ void GFXVulkan::createSwapchain(NativeSurface* native_surface, VkSwapchainKHR ol
|
|||
}
|
||||
|
||||
// allocate command buffers
|
||||
native_surface->commandBuffers.resize(MAX_FRAMES_IN_FLIGHT);
|
||||
native_surface->gfx_command_buffers.resize(MAX_FRAMES_IN_FLIGHT);
|
||||
for (auto [i, cmdbuf] : utility::enumerate(native_surface->gfx_command_buffers)) {
|
||||
cmdbuf = new GFXVulkanCommandBuffer();
|
||||
|
||||
VkCommandBufferAllocateInfo allocInfo = {};
|
||||
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||
allocInfo.commandPool = commandPool;
|
||||
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||
allocInfo.commandBufferCount = (uint32_t)native_surface->commandBuffers.size();
|
||||
VkCommandBufferAllocateInfo allocInfo = {};
|
||||
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||
allocInfo.commandPool = commandPool;
|
||||
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||
allocInfo.commandBufferCount = 1;
|
||||
|
||||
vkAllocateCommandBuffers(device, &allocInfo, native_surface->commandBuffers.data());
|
||||
vkAllocateCommandBuffers(device, &allocInfo, &cmdbuf->handle);
|
||||
|
||||
for (auto [i, cmdbuf] : utility::enumerate(native_surface->commandBuffers))
|
||||
name_object(device, VK_OBJECT_TYPE_COMMAND_BUFFER, (uint64_t)cmdbuf, ("main cmd buf " + std::to_string(i)).c_str());
|
||||
name_object(device, VK_OBJECT_TYPE_COMMAND_BUFFER, (uint64_t) cmdbuf,
|
||||
("main cmd buf " + std::to_string(i)).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void GFXVulkan::createDescriptorPool() {
|
||||
|
|
Reference in a new issue