|
|
@ -6,41 +6,58 @@ |
|
|
|
#include "vulkan/command_pool.hpp" |
|
|
|
#include "vulkan/command_pool.hpp" |
|
|
|
#include "vulkan/image.hpp" |
|
|
|
#include "vulkan/image.hpp" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <glm/gtc/matrix_transform.hpp> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Application::Application() { |
|
|
|
Application::Application() { |
|
|
|
new Instance; |
|
|
|
new Instance; |
|
|
|
swapchain = new Swapchain(); |
|
|
|
swapchain = new Swapchain(); |
|
|
|
graphicsPipeline = new Pipeline(swapchain->renderPass); |
|
|
|
graphicsPipeline = new Pipeline(swapchain->renderPass); |
|
|
|
auto stagedVertexBuffer = Buffer::createStagedVertexBuffer(); |
|
|
|
std::vector<Vertex> vertices = { |
|
|
|
vertexBuffer = new Buffer(stagedVertexBuffer->size, |
|
|
|
{{0.0, -0.5, 0}, {1, 0, 0}}, |
|
|
|
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, |
|
|
|
{{0.5, 0.5, 0}, {0, 1, 0}}, |
|
|
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); |
|
|
|
{{-0.5, 0.5, 0}, {0, 0, 1}}, |
|
|
|
|
|
|
|
{{0.8, -0.5, 0}, {1, 1, 1}} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vertices.insert(vertices.end(), vertices.begin(), vertices.end()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (size_t i = 4; i < vertices.size(); i++){ |
|
|
|
|
|
|
|
vertices[i].pos.z -= 0.5f; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
VkDeviceSize size = vertices.size() * sizeof(Vertex); |
|
|
|
|
|
|
|
vertexBuffer = new Buffer(size, vertices.data(), size, |
|
|
|
|
|
|
|
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, |
|
|
|
|
|
|
|
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); |
|
|
|
|
|
|
|
|
|
|
|
auto stagedIndexBuffer = Buffer::createStagedIndexBuffer(); |
|
|
|
std::vector<uint32_t> indices = { |
|
|
|
indexBuffer = new Buffer(stagedIndexBuffer->size, |
|
|
|
0, 1, 2, 0, 3, 1, |
|
|
|
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, |
|
|
|
4, 5, 6, 4, 7, 5 |
|
|
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
size = indices.size() * sizeof(uint32_t); |
|
|
|
|
|
|
|
indexBuffer = new Buffer(size, indices.data(), size, |
|
|
|
|
|
|
|
VK_BUFFER_USAGE_INDEX_BUFFER_BIT, |
|
|
|
|
|
|
|
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, 0); |
|
|
|
|
|
|
|
|
|
|
|
VkDeviceSize bufferSize = sizeof(UniformBufferObject); |
|
|
|
VkDeviceSize bufferSize = sizeof(UniformBufferObject); |
|
|
|
|
|
|
|
|
|
|
|
uniformBuffer = new Buffer(bufferSize, |
|
|
|
uniformBuffer = new Buffer(bufferSize, |
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, |
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, |
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); |
|
|
|
VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE, |
|
|
|
vkMapMemory(Instance::instance->device, uniformBuffer->memory, 0, bufferSize, 0, &uniformBufferMapped); |
|
|
|
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT); |
|
|
|
|
|
|
|
|
|
|
|
graphicsPipeline->createDescriptorSet(uniformBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
|
|
|
graphicsPipeline->createDescriptorSet(uniformBuffer, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER); |
|
|
|
computePipeline = new ComputePipeline(vertexBuffer); |
|
|
|
computePipeline = new ComputePipeline(vertexBuffer); |
|
|
|
commandPool = new CommandPool(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stagedVertexBuffer->copyTo(vertexBuffer, commandPool); |
|
|
|
|
|
|
|
stagedIndexBuffer->copyTo(indexBuffer, commandPool); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete stagedVertexBuffer; |
|
|
|
|
|
|
|
delete stagedIndexBuffer; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
createSyncObjects(); |
|
|
|
createSyncObjects(); |
|
|
|
|
|
|
|
|
|
|
|
recordComputeCommandBuffer(); |
|
|
|
recordComputeCommandBuffer(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char* stats; |
|
|
|
|
|
|
|
vmaBuildStatsString(Instance::instance->allocator, &stats, VK_TRUE); |
|
|
|
|
|
|
|
// printf("%s", stats);
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Application::updateUniformBuffer() { |
|
|
|
void Application::updateUniformBuffer() { |
|
|
@ -56,7 +73,7 @@ void Application::updateUniformBuffer() { |
|
|
|
0.1f, 10.f); |
|
|
|
0.1f, 10.f); |
|
|
|
ubo.projection[1][1] *= -1; |
|
|
|
ubo.projection[1][1] *= -1; |
|
|
|
|
|
|
|
|
|
|
|
memcpy(uniformBufferMapped, &ubo, sizeof(UniformBufferObject)); |
|
|
|
memcpy(uniformBuffer->allocationInfo.pMappedData, &ubo, sizeof(UniformBufferObject)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { |
|
|
|
void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { |
|
|
@ -102,7 +119,7 @@ void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t im |
|
|
|
vkCmdBindIndexBuffer(commandBuffer, indexBuffer->handle, 0, VK_INDEX_TYPE_UINT32); |
|
|
|
vkCmdBindIndexBuffer(commandBuffer, indexBuffer->handle, 0, VK_INDEX_TYPE_UINT32); |
|
|
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline->layout, 0, 1, &graphicsPipeline->descriptorSet, 0, nullptr); |
|
|
|
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline->layout, 0, 1, &graphicsPipeline->descriptorSet, 0, nullptr); |
|
|
|
|
|
|
|
|
|
|
|
vkCmdDrawIndexed(commandBuffer, indices.size(), 1, 0, 0, 0); |
|
|
|
vkCmdDrawIndexed(commandBuffer, 12, 1, 0, 0, 0); |
|
|
|
|
|
|
|
|
|
|
|
vkCmdEndRenderPass(commandBuffer); |
|
|
|
vkCmdEndRenderPass(commandBuffer); |
|
|
|
vkEndCommandBuffer(commandBuffer); |
|
|
|
vkEndCommandBuffer(commandBuffer); |
|
|
@ -144,8 +161,8 @@ void Application::drawFrame() { |
|
|
|
|
|
|
|
|
|
|
|
vkResetFences(Instance::instance->device, 1, &renderInFlightFence); |
|
|
|
vkResetFences(Instance::instance->device, 1, &renderInFlightFence); |
|
|
|
|
|
|
|
|
|
|
|
vkResetCommandBuffer(commandPool->graphicsBuffer, 0); |
|
|
|
vkResetCommandBuffer(Instance::instance->commandPool->graphicsBuffer, 0); |
|
|
|
recordCommandBuffer(commandPool->graphicsBuffer, imageIndex); |
|
|
|
recordCommandBuffer(Instance::instance->commandPool->graphicsBuffer, imageIndex); |
|
|
|
|
|
|
|
|
|
|
|
updateUniformBuffer(); |
|
|
|
updateUniformBuffer(); |
|
|
|
|
|
|
|
|
|
|
@ -158,7 +175,7 @@ void Application::drawFrame() { |
|
|
|
submitInfo.pWaitSemaphores = waitSemaphores; |
|
|
|
submitInfo.pWaitSemaphores = waitSemaphores; |
|
|
|
submitInfo.pWaitDstStageMask = waitStages; |
|
|
|
submitInfo.pWaitDstStageMask = waitStages; |
|
|
|
submitInfo.commandBufferCount = 1; |
|
|
|
submitInfo.commandBufferCount = 1; |
|
|
|
submitInfo.pCommandBuffers = &commandPool->graphicsBuffer; |
|
|
|
submitInfo.pCommandBuffers = &Instance::instance->commandPool->graphicsBuffer; |
|
|
|
|
|
|
|
|
|
|
|
VkSemaphore signalSemaphores[] = {renderFinishedSemaphore}; |
|
|
|
VkSemaphore signalSemaphores[] = {renderFinishedSemaphore}; |
|
|
|
submitInfo.signalSemaphoreCount = 1; |
|
|
|
submitInfo.signalSemaphoreCount = 1; |
|
|
@ -190,7 +207,7 @@ void Application::update() { |
|
|
|
VkSubmitInfo submit {}; |
|
|
|
VkSubmitInfo submit {}; |
|
|
|
submit.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
|
|
|
submit.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
|
|
|
submit.commandBufferCount = 1; |
|
|
|
submit.commandBufferCount = 1; |
|
|
|
submit.pCommandBuffers = &commandPool->computeBuffer; |
|
|
|
submit.pCommandBuffers = &Instance::instance->commandPool->computeBuffer; |
|
|
|
submit.signalSemaphoreCount = 1; |
|
|
|
submit.signalSemaphoreCount = 1; |
|
|
|
submit.pSignalSemaphores = &computeFinishedSemaphore; |
|
|
|
submit.pSignalSemaphores = &computeFinishedSemaphore; |
|
|
|
|
|
|
|
|
|
|
@ -198,7 +215,7 @@ void Application::update() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Application::recordComputeCommandBuffer() { |
|
|
|
void Application::recordComputeCommandBuffer() { |
|
|
|
VkCommandBuffer buffer = commandPool->computeBuffer; |
|
|
|
VkCommandBuffer buffer = Instance::instance->commandPool->computeBuffer; |
|
|
|
|
|
|
|
|
|
|
|
VkCommandBufferBeginInfo beginInfo {}; |
|
|
|
VkCommandBufferBeginInfo beginInfo {}; |
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
|
|
@ -219,7 +236,6 @@ Application::~Application() { |
|
|
|
vkDestroySemaphore(Instance::instance->device, computeFinishedSemaphore, nullptr); |
|
|
|
vkDestroySemaphore(Instance::instance->device, computeFinishedSemaphore, nullptr); |
|
|
|
vkDestroyFence(Instance::instance->device, renderInFlightFence, nullptr); |
|
|
|
vkDestroyFence(Instance::instance->device, renderInFlightFence, nullptr); |
|
|
|
vkDestroyFence(Instance::instance->device, computeInFlightFence, nullptr); |
|
|
|
vkDestroyFence(Instance::instance->device, computeInFlightFence, nullptr); |
|
|
|
delete commandPool; |
|
|
|
|
|
|
|
delete vertexBuffer; |
|
|
|
delete vertexBuffer; |
|
|
|
delete indexBuffer; |
|
|
|
delete indexBuffer; |
|
|
|
delete uniformBuffer; |
|
|
|
delete uniformBuffer; |
|
|
|