#include "pipelinestate.h"
#include "rect.h"
#include "semaphore.h"
+#include "swapchaintexture.h"
#include "vulkan.h"
using namespace std;
current_pool->in_use = true;
}
- VkCommandBufferAllocateInfo alloc_info = { };
- alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
- alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool->pool);
- alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
- alloc_info.commandBufferCount = 1;
+ if(current_pool->next_buffer>=current_pool->buffers.size())
+ {
+ VkCommandBufferAllocateInfo alloc_info = { };
+ alloc_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
+ alloc_info.commandPool = handle_cast<::VkCommandPool>(current_pool->pool);
+ alloc_info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
+ alloc_info.commandBufferCount = 1;
+
+ VkCommandBuffer buffer;
+ vk.AllocateCommandBuffers(alloc_info, &buffer);
+ current_pool->buffers.push_back(buffer);
+ }
- vk.AllocateCommandBuffers(alloc_info, ¤t_buffer);
+ current_buffer = current_pool->buffers[current_pool->next_buffer++];
VkCommandBufferBeginInfo begin_info = { };
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
vk.BeginCommandBuffer(current_buffer, begin_info);
}
-void VulkanCommands::begin_render_pass(const ClearValue *clear_values)
+void VulkanCommands::begin_render_pass(bool clear, const ClearValue *clear_values)
{
- const Framebuffer *target = pipeline_state->get_framebuffer();
- if(!target)
+ framebuffer = pipeline_state->get_framebuffer();
+ if(!framebuffer)
throw invalid_operation("VulkanCommands::begin_render_pass");
const VulkanFunctions &vk = device.get_functions();
device.get_transfer_queue().dispatch_transfers(current_buffer);
- // TODO Use proper value for to_present
- render_pass = device.get_pipeline_cache().get_render_pass(target->get_format(), clear_values, true);
+ Synchronizer &sync = device.get_synchronizer();
+ sync.reset();
+
+ bool to_present = false;
+ unsigned n_attachments = framebuffer->get_format().size();
+ for(unsigned i=0; i<n_attachments; ++i)
+ if(dynamic_cast<const SwapChainTexture *>(framebuffer->get_attachment(i)))
+ to_present = true;
+ if(!to_present)
+ framebuffer->prepare_image_layouts(clear);
+ VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(framebuffer->get_format(), clear, !clear_values, to_present);
+
+ framebuffer->refresh();
- target->refresh();
+ sync.barrier(current_buffer);
VkRenderPassBeginInfo begin_info = { };
begin_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
begin_info.renderPass = handle_cast<::VkRenderPass>(render_pass);
- begin_info.framebuffer = handle_cast<::VkFramebuffer>(target->handle);
+ begin_info.framebuffer = handle_cast<::VkFramebuffer>(framebuffer->handle);
- const Rect *viewport = pipeline_state->get_viewport();
+ viewport = pipeline_state->get_viewport();
if(viewport)
{
begin_info.renderArea.offset.x = viewport->left;
}
else
{
- begin_info.renderArea.extent.width = target->get_width();
- begin_info.renderArea.extent.height = target->get_height();
+ begin_info.renderArea.extent.width = framebuffer->get_width();
+ begin_info.renderArea.extent.height = framebuffer->get_height();
}
VkClearValue vk_clear_values[7];
if(clear_values)
{
unsigned i = 0;
- for(FrameAttachment a: target->get_format())
+ for(FrameAttachment a: framebuffer->get_format())
{
if(get_attach_point(a)==get_attach_point(DEPTH_ATTACHMENT))
vk_clear_values[i].depthStencil.depth = clear_values[i].depth_stencil.depth;
++i;
}
- begin_info.clearValueCount = target->get_format().size();
+ begin_info.clearValueCount = framebuffer->get_format().size();
begin_info.pClearValues = vk_clear_values;
}
const VulkanFunctions &vk = device.get_functions();
vk.CmdEndRenderPass(current_buffer);
- render_pass = 0;
+ framebuffer = 0;
+ viewport = 0;
}
void VulkanCommands::begin_frame(unsigned index)
current_pool->fence.wait();
vk.ResetCommandPool(current_pool->pool, 0);
current_pool->in_use = false;
+ current_pool->next_buffer = 0;
}
}
::VkSemaphore vk_signal_sem = (signal_sem ? handle_cast<::VkSemaphore>(signal_sem->handle) : 0);
VkPipelineStageFlags wait_stages = VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT;
- if(render_pass)
+ if(framebuffer)
end_render_pass();
vk.EndCommandBuffer(current_buffer);
void VulkanCommands::use_pipeline(const PipelineState *ps)
{
- if(!pipeline_state || !ps || ps->get_framebuffer()!=pipeline_state->get_framebuffer() || ps->get_viewport()!=pipeline_state->get_viewport())
- if(render_pass)
+ if(!pipeline_state || !ps || ps->get_framebuffer()!=framebuffer || ps->get_viewport()!=viewport)
+ if(framebuffer)
end_render_pass();
pipeline_state = ps;
- if(pipeline_state)
- pipeline_state->refresh();
}
void VulkanCommands::clear(const ClearValue *values)
{
- if(render_pass)
+ if(framebuffer)
throw invalid_operation("VulkanCommands::clear");
- begin_render_pass(values);
+ begin_render_pass(true, values);
}
void VulkanCommands::draw(const Batch &batch)
const VulkanFunctions &vk = device.get_functions();
- if(!render_pass)
- begin_render_pass(0);
+ if(!framebuffer)
+ begin_render_pass(false, 0);
+ pipeline_state->refresh();
pipeline_state->apply(current_buffer);
unsigned first_index = batch.get_offset()/batch.get_index_size();
vk.CmdDrawIndexed(current_buffer, batch.size(), count, first_index, 0, 0);