|
|
@ -5,6 +5,7 @@ |
|
|
|
#include "vulkan/buffer.hpp" |
|
|
|
#include "vulkan/buffer.hpp" |
|
|
|
#include "vulkan/command_pool.hpp" |
|
|
|
#include "vulkan/command_pool.hpp" |
|
|
|
#include "vulkan/image.hpp" |
|
|
|
#include "vulkan/image.hpp" |
|
|
|
|
|
|
|
#include "vulkan/synchronization.hpp" |
|
|
|
|
|
|
|
|
|
|
|
#include <glm/gtc/matrix_transform.hpp> |
|
|
|
#include <glm/gtc/matrix_transform.hpp> |
|
|
|
|
|
|
|
|
|
|
@ -53,17 +54,16 @@ Application::Application() { |
|
|
|
|
|
|
|
|
|
|
|
createSyncObjects(); |
|
|
|
createSyncObjects(); |
|
|
|
|
|
|
|
|
|
|
|
recordComputeCommandBuffer(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char* stats; |
|
|
|
char* stats; |
|
|
|
vmaBuildStatsString(Instance::instance->allocator, &stats, VK_TRUE); |
|
|
|
vmaBuildStatsString(Instance::instance->allocator, &stats, VK_TRUE); |
|
|
|
// printf("%s", stats);
|
|
|
|
// printf("%s", stats);
|
|
|
|
|
|
|
|
vmaFreeStatsString(Instance::instance->allocator, stats); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Application::updateUniformBuffer() { |
|
|
|
void Application::updateUniformBuffer() { |
|
|
|
static float elapsed = 0; |
|
|
|
static float elapsed = 0; |
|
|
|
|
|
|
|
|
|
|
|
elapsed += 0.007; |
|
|
|
// elapsed += 0.007;
|
|
|
|
|
|
|
|
|
|
|
|
UniformBufferObject ubo {}; |
|
|
|
UniformBufferObject ubo {}; |
|
|
|
ubo.model = glm::rotate(glm::mat4(1), elapsed * glm::radians(90.f), glm::vec3(0, 0, 1)); |
|
|
|
ubo.model = glm::rotate(glm::mat4(1), elapsed * glm::radians(90.f), glm::vec3(0, 0, 1)); |
|
|
@ -113,31 +113,18 @@ void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t im |
|
|
|
scissor.extent = swapchain->extent; |
|
|
|
scissor.extent = swapchain->extent; |
|
|
|
vkCmdSetScissor(commandBuffer, 0, 1, &scissor); |
|
|
|
vkCmdSetScissor(commandBuffer, 0, 1, &scissor); |
|
|
|
|
|
|
|
|
|
|
|
VkBuffer buffers[] = {vertexBuffer->handle}; |
|
|
|
recordDrawCommands(); |
|
|
|
VkDeviceSize offsets[] = {0}; |
|
|
|
|
|
|
|
vkCmdBindVertexBuffers(commandBuffer, 0, 1, buffers, offsets); |
|
|
|
|
|
|
|
vkCmdBindIndexBuffer(commandBuffer, indexBuffer->handle, 0, VK_INDEX_TYPE_UINT32); |
|
|
|
|
|
|
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline->layout, 0, 1, &graphicsPipeline->descriptorSet, 0, nullptr); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vkCmdDrawIndexed(commandBuffer, 12, 1, 0, 0, 0); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vkCmdEndRenderPass(commandBuffer); |
|
|
|
vkCmdEndRenderPass(commandBuffer); |
|
|
|
vkEndCommandBuffer(commandBuffer); |
|
|
|
vkEndCommandBuffer(commandBuffer); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Application::createSyncObjects() { |
|
|
|
void Application::createSyncObjects() { |
|
|
|
VkSemaphoreCreateInfo semaphoreInfo {}; |
|
|
|
imageAvailable = new Semaphore; |
|
|
|
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
|
|
|
renderFinished = new Semaphore; |
|
|
|
|
|
|
|
computeFinished = new Semaphore; |
|
|
|
VkFenceCreateInfo fenceInfo {}; |
|
|
|
renderInFlight = new Fence(true); |
|
|
|
fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
|
|
|
computeInFlight = new Fence(true); |
|
|
|
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vkCreateSemaphore(Instance::instance->device, &semaphoreInfo, nullptr, &imageAvailableSemaphore); |
|
|
|
|
|
|
|
vkCreateSemaphore(Instance::instance->device, &semaphoreInfo, nullptr, &renderFinishedSemaphore); |
|
|
|
|
|
|
|
vkCreateSemaphore(Instance::instance->device, &semaphoreInfo, nullptr, &computeFinishedSemaphore); |
|
|
|
|
|
|
|
vkCreateFence(Instance::instance->device, &fenceInfo, nullptr, &renderInFlightFence); |
|
|
|
|
|
|
|
vkCreateFence(Instance::instance->device, &fenceInfo, nullptr, &computeInFlightFence); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Application::mainLoop() { |
|
|
|
void Application::mainLoop() { |
|
|
@ -150,16 +137,16 @@ void Application::mainLoop() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Application::drawFrame() { |
|
|
|
void Application::drawFrame() { |
|
|
|
vkWaitForFences(Instance::instance->device, 1, &renderInFlightFence, VK_TRUE, UINT64_MAX); |
|
|
|
vkWaitForFences(Instance::instance->device, 1, &renderInFlight->handle, VK_TRUE, UINT64_MAX); |
|
|
|
|
|
|
|
|
|
|
|
uint32_t imageIndex; |
|
|
|
uint32_t imageIndex; |
|
|
|
VkResult result = vkAcquireNextImageKHR(Instance::instance->device, swapchain->handle, UINT64_MAX, imageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex); |
|
|
|
VkResult result = vkAcquireNextImageKHR(Instance::instance->device, swapchain->handle, UINT64_MAX, imageAvailable->handle, VK_NULL_HANDLE, &imageIndex); |
|
|
|
if (result == VK_ERROR_OUT_OF_DATE_KHR){ |
|
|
|
if (result == VK_ERROR_OUT_OF_DATE_KHR){ |
|
|
|
swapchain->recreateSwapchain(); |
|
|
|
swapchain->recreateSwapchain(); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
vkResetFences(Instance::instance->device, 1, &renderInFlightFence); |
|
|
|
vkResetFences(Instance::instance->device, 1, &renderInFlight->handle); |
|
|
|
|
|
|
|
|
|
|
|
vkResetCommandBuffer(Instance::instance->commandPool->graphicsBuffer, 0); |
|
|
|
vkResetCommandBuffer(Instance::instance->commandPool->graphicsBuffer, 0); |
|
|
|
recordCommandBuffer(Instance::instance->commandPool->graphicsBuffer, imageIndex); |
|
|
|
recordCommandBuffer(Instance::instance->commandPool->graphicsBuffer, imageIndex); |
|
|
@ -169,7 +156,7 @@ void Application::drawFrame() { |
|
|
|
VkSubmitInfo submitInfo {}; |
|
|
|
VkSubmitInfo submitInfo {}; |
|
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
|
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
|
|
|
|
|
|
|
|
|
|
|
VkSemaphore waitSemaphores[] = {imageAvailableSemaphore, computeFinishedSemaphore}; |
|
|
|
VkSemaphore waitSemaphores[] = {imageAvailable->handle, computeFinished->handle}; |
|
|
|
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT}; |
|
|
|
VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT}; |
|
|
|
submitInfo.waitSemaphoreCount = 2; |
|
|
|
submitInfo.waitSemaphoreCount = 2; |
|
|
|
submitInfo.pWaitSemaphores = waitSemaphores; |
|
|
|
submitInfo.pWaitSemaphores = waitSemaphores; |
|
|
@ -177,11 +164,11 @@ void Application::drawFrame() { |
|
|
|
submitInfo.commandBufferCount = 1; |
|
|
|
submitInfo.commandBufferCount = 1; |
|
|
|
submitInfo.pCommandBuffers = &Instance::instance->commandPool->graphicsBuffer; |
|
|
|
submitInfo.pCommandBuffers = &Instance::instance->commandPool->graphicsBuffer; |
|
|
|
|
|
|
|
|
|
|
|
VkSemaphore signalSemaphores[] = {renderFinishedSemaphore}; |
|
|
|
VkSemaphore signalSemaphores[] = {renderFinished->handle}; |
|
|
|
submitInfo.signalSemaphoreCount = 1; |
|
|
|
submitInfo.signalSemaphoreCount = 1; |
|
|
|
submitInfo.pSignalSemaphores = signalSemaphores; |
|
|
|
submitInfo.pSignalSemaphores = signalSemaphores; |
|
|
|
|
|
|
|
|
|
|
|
vkQueueSubmit(Instance::instance->graphicsQueue, 1, &submitInfo, renderInFlightFence); |
|
|
|
vkQueueSubmit(Instance::instance->graphicsQueue, 1, &submitInfo, renderInFlight->handle); |
|
|
|
|
|
|
|
|
|
|
|
VkPresentInfoKHR presentInfo {}; |
|
|
|
VkPresentInfoKHR presentInfo {}; |
|
|
|
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; |
|
|
|
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; |
|
|
@ -201,41 +188,40 @@ void Application::drawFrame() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Application::update() { |
|
|
|
void Application::update() { |
|
|
|
vkWaitForFences(Instance::instance->device, 1, &computeInFlightFence, VK_TRUE, UINT64_MAX); |
|
|
|
vkWaitForFences(Instance::instance->device, 1, &computeInFlight->handle, VK_TRUE, UINT64_MAX); |
|
|
|
vkResetFences(Instance::instance->device, 1, &computeInFlightFence); |
|
|
|
vkResetFences(Instance::instance->device, 1, &computeInFlight->handle); |
|
|
|
|
|
|
|
|
|
|
|
VkSubmitInfo submit {}; |
|
|
|
|
|
|
|
submit.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
|
|
|
|
|
|
|
submit.commandBufferCount = 1; |
|
|
|
|
|
|
|
submit.pCommandBuffers = &Instance::instance->commandPool->computeBuffer; |
|
|
|
|
|
|
|
submit.signalSemaphoreCount = 1; |
|
|
|
|
|
|
|
submit.pSignalSemaphores = &computeFinishedSemaphore; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vkQueueSubmit(Instance::instance->computeQueue, 1, &submit, computeInFlightFence); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Application::recordComputeCommandBuffer() { |
|
|
|
VkCommandBuffer commandBuffer = Instance::instance->commandPool->computeBuffer; |
|
|
|
VkCommandBuffer buffer = Instance::instance->commandPool->computeBuffer; |
|
|
|
vkResetCommandBuffer(commandBuffer, 0); |
|
|
|
|
|
|
|
|
|
|
|
VkCommandBufferBeginInfo beginInfo {}; |
|
|
|
VkCommandBufferBeginInfo beginInfo {}; |
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
|
|
|
|
|
|
|
beginInfo.flags = 0; |
|
|
|
|
|
|
|
|
|
|
|
vkBeginCommandBuffer(buffer, &beginInfo); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vkCmdBindPipeline(buffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipeline->handle); |
|
|
|
vkBeginCommandBuffer(commandBuffer, &beginInfo); |
|
|
|
vkCmdBindDescriptorSets(buffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipeline->layout,0, 1, &computePipeline->descriptorSet, 0, nullptr); |
|
|
|
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, computePipeline->handle); |
|
|
|
vkCmdDispatch(buffer, 1, 1, 1); |
|
|
|
recordComputeCommands(); |
|
|
|
|
|
|
|
vkEndCommandBuffer(commandBuffer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VkSubmitInfo submit {}; |
|
|
|
|
|
|
|
submit.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
|
|
|
|
|
|
|
submit.commandBufferCount = 1; |
|
|
|
|
|
|
|
submit.pCommandBuffers = &Instance::instance->commandPool->computeBuffer; |
|
|
|
|
|
|
|
submit.signalSemaphoreCount = 1; |
|
|
|
|
|
|
|
submit.pSignalSemaphores = &computeFinished->handle; |
|
|
|
|
|
|
|
|
|
|
|
vkEndCommandBuffer(buffer); |
|
|
|
vkQueueSubmit(Instance::instance->computeQueue, 1, &submit, computeInFlight->handle); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Application::~Application() { |
|
|
|
Application::~Application() { |
|
|
|
delete swapchain; |
|
|
|
delete swapchain; |
|
|
|
vkDestroySemaphore(Instance::instance->device, imageAvailableSemaphore, nullptr); |
|
|
|
delete imageAvailable; |
|
|
|
vkDestroySemaphore(Instance::instance->device, renderFinishedSemaphore, nullptr); |
|
|
|
delete renderFinished; |
|
|
|
vkDestroySemaphore(Instance::instance->device, computeFinishedSemaphore, nullptr); |
|
|
|
delete computeFinished; |
|
|
|
vkDestroyFence(Instance::instance->device, renderInFlightFence, nullptr); |
|
|
|
delete renderInFlight; |
|
|
|
vkDestroyFence(Instance::instance->device, computeInFlightFence, nullptr); |
|
|
|
delete computeInFlight; |
|
|
|
delete vertexBuffer; |
|
|
|
delete vertexBuffer; |
|
|
|
delete indexBuffer; |
|
|
|
delete indexBuffer; |
|
|
|
delete uniformBuffer; |
|
|
|
delete uniformBuffer; |
|
|
|