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.hpp"
|
||||||
#include "gfx_vulkan_constants.hpp"
|
#include "gfx_vulkan_constants.hpp"
|
||||||
|
|
||||||
|
class GFXVulkanCommandBuffer;
|
||||||
|
|
||||||
struct NativeSurface {
|
struct NativeSurface {
|
||||||
platform::window_ptr identifier = nullptr;
|
platform::window_ptr identifier = nullptr;
|
||||||
|
|
||||||
|
@ -28,7 +30,7 @@ struct NativeSurface {
|
||||||
VkRenderPass swapchainRenderPass = VK_NULL_HANDLE;
|
VkRenderPass swapchainRenderPass = VK_NULL_HANDLE;
|
||||||
|
|
||||||
std::vector<VkFramebuffer> swapchainFramebuffers;
|
std::vector<VkFramebuffer> swapchainFramebuffers;
|
||||||
std::vector<VkCommandBuffer> commandBuffers;
|
std::vector<GFXVulkanCommandBuffer*> gfx_command_buffers;
|
||||||
};
|
};
|
||||||
|
|
||||||
class GFXVulkanPipeline;
|
class GFXVulkanPipeline;
|
||||||
|
|
|
@ -1240,9 +1240,9 @@ GFXSize GFXVulkan::get_alignment(GFXSize size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
GFXCommandBuffer* GFXVulkan::acquire_command_buffer(bool for_presentation_use) {
|
GFXCommandBuffer* GFXVulkan::acquire_command_buffer(bool for_presentation_use) {
|
||||||
auto cmdbuf = new GFXVulkanCommandBuffer();
|
|
||||||
|
|
||||||
if(!for_presentation_use) {
|
if(!for_presentation_use) {
|
||||||
|
auto cmdbuf = new GFXVulkanCommandBuffer();
|
||||||
|
|
||||||
VkCommandBufferAllocateInfo info = {};
|
VkCommandBufferAllocateInfo info = {};
|
||||||
info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||||
info.commandPool = commandPool;
|
info.commandPool = commandPool;
|
||||||
|
@ -1250,9 +1250,12 @@ GFXCommandBuffer* GFXVulkan::acquire_command_buffer(bool for_presentation_use) {
|
||||||
info.commandBufferCount = 1;
|
info.commandBufferCount = 1;
|
||||||
|
|
||||||
vkAllocateCommandBuffers(device, &info, &cmdbuf->handle);
|
vkAllocateCommandBuffers(device, &info, &cmdbuf->handle);
|
||||||
}
|
|
||||||
|
|
||||||
return cmdbuf;
|
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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const platform::window_ptr identifier) {
|
void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const platform::window_ptr identifier) {
|
||||||
|
@ -1274,10 +1277,7 @@ void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const platform::window_
|
||||||
VkCommandBuffer cmd = VK_NULL_HANDLE;
|
VkCommandBuffer cmd = VK_NULL_HANDLE;
|
||||||
|
|
||||||
auto cmdbuf = (GFXVulkanCommandBuffer*)command_buffer;
|
auto cmdbuf = (GFXVulkanCommandBuffer*)command_buffer;
|
||||||
if(cmdbuf->handle != VK_NULL_HANDLE)
|
cmd = cmdbuf->handle;
|
||||||
cmd = cmdbuf->handle;
|
|
||||||
else if(current_surface != nullptr)
|
|
||||||
cmd = current_surface->commandBuffers[current_surface-> currentFrame];
|
|
||||||
|
|
||||||
if(cmd == nullptr)
|
if(cmd == nullptr)
|
||||||
return;
|
return;
|
||||||
|
@ -2124,18 +2124,21 @@ void GFXVulkan::createSwapchain(NativeSurface* native_surface, VkSwapchainKHR ol
|
||||||
}
|
}
|
||||||
|
|
||||||
// allocate command buffers
|
// 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 = {};
|
VkCommandBufferAllocateInfo allocInfo = {};
|
||||||
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||||
allocInfo.commandPool = commandPool;
|
allocInfo.commandPool = commandPool;
|
||||||
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
||||||
allocInfo.commandBufferCount = (uint32_t)native_surface->commandBuffers.size();
|
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,
|
||||||
name_object(device, VK_OBJECT_TYPE_COMMAND_BUFFER, (uint64_t)cmdbuf, ("main cmd buf " + std::to_string(i)).c_str());
|
("main cmd buf " + std::to_string(i)).c_str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GFXVulkan::createDescriptorPool() {
|
void GFXVulkan::createDescriptorPool() {
|
||||||
|
|
Reference in a new issue