|
|
|
@ -19,6 +19,14 @@ Application::Application() { |
|
|
|
|
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT, |
|
|
|
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); |
|
|
|
|
|
|
|
|
|
VkDeviceSize bufferSize = sizeof(UniformBufferObject); |
|
|
|
|
|
|
|
|
|
uniformBuffer = new Buffer(instance, bufferSize, |
|
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, |
|
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); |
|
|
|
|
vkMapMemory(instance->device, uniformBuffer->memory, 0, bufferSize, 0, &uniformBufferMapped); |
|
|
|
|
|
|
|
|
|
pipeline->createDescriptorSet(uniformBuffer); |
|
|
|
|
commandPool = new CommandPool(instance); |
|
|
|
|
|
|
|
|
|
stagedVertexBuffer->copyTo(vertexBuffer, commandPool); |
|
|
|
@ -28,16 +36,68 @@ Application::Application() { |
|
|
|
|
delete stagedIndexBuffer; |
|
|
|
|
|
|
|
|
|
createSyncObjects(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Application::updateUniformBuffer() { |
|
|
|
|
static float elapsed = 0; |
|
|
|
|
|
|
|
|
|
mainLoop(); |
|
|
|
|
elapsed += 0.007; |
|
|
|
|
|
|
|
|
|
UniformBufferObject ubo {}; |
|
|
|
|
ubo.model = glm::rotate(glm::mat4(1), elapsed * glm::radians(90.f), glm::vec3(0, 0, 1)); |
|
|
|
|
ubo.view = glm::lookAt(glm::vec3(2), glm::vec3(0), glm::vec3(0, 0, 1)); |
|
|
|
|
ubo.projection = glm::perspective(glm::radians(45.f), |
|
|
|
|
static_cast<float>(swapchain->extent.width) / static_cast<float>(swapchain->extent.height), |
|
|
|
|
0.1f, 10.f); |
|
|
|
|
ubo.projection[1][1] *= -1; |
|
|
|
|
|
|
|
|
|
memcpy(uniformBufferMapped, &ubo, sizeof(UniformBufferObject)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Application::mainLoop() { |
|
|
|
|
while (!glfwWindowShouldClose(instance->window)){ |
|
|
|
|
glfwPollEvents(); |
|
|
|
|
drawFrame(); |
|
|
|
|
} |
|
|
|
|
vkDeviceWaitIdle(instance->device); |
|
|
|
|
void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { |
|
|
|
|
VkCommandBufferBeginInfo beginInfo {}; |
|
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
|
|
|
|
|
|
|
|
|
vkBeginCommandBuffer(commandBuffer, &beginInfo); |
|
|
|
|
|
|
|
|
|
VkRenderPassBeginInfo renderPassInfo {}; |
|
|
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; |
|
|
|
|
renderPassInfo.renderPass = swapchain->renderPass; |
|
|
|
|
renderPassInfo.framebuffer = swapchain->frameBuffers[imageIndex]; |
|
|
|
|
renderPassInfo.renderArea.offset = {0, 0}; |
|
|
|
|
renderPassInfo.renderArea.extent = swapchain->extent; |
|
|
|
|
|
|
|
|
|
VkClearValue clearColor = {{{0, 0, 0, 1}}}; |
|
|
|
|
renderPassInfo.clearValueCount = 1; |
|
|
|
|
renderPassInfo.pClearValues = &clearColor; |
|
|
|
|
|
|
|
|
|
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); |
|
|
|
|
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->handle); |
|
|
|
|
|
|
|
|
|
VkViewport viewport {}; |
|
|
|
|
viewport.x = 0; |
|
|
|
|
viewport.y = 0; |
|
|
|
|
viewport.width = static_cast<float>(swapchain->extent.width); |
|
|
|
|
viewport.height = static_cast<float>(swapchain->extent.height); |
|
|
|
|
viewport.minDepth = 0; |
|
|
|
|
viewport.maxDepth = 1; |
|
|
|
|
vkCmdSetViewport(commandBuffer, 0, 1, &viewport); |
|
|
|
|
|
|
|
|
|
VkRect2D scissor {}; |
|
|
|
|
scissor.offset = {0, 0}; |
|
|
|
|
scissor.extent = swapchain->extent; |
|
|
|
|
vkCmdSetScissor(commandBuffer, 0, 1, &scissor); |
|
|
|
|
|
|
|
|
|
VkBuffer buffers[] = {vertexBuffer->handle}; |
|
|
|
|
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, pipeline->layout, 0, 1, &pipeline->descriptorSet, 0, nullptr); |
|
|
|
|
|
|
|
|
|
vkCmdDrawIndexed(commandBuffer, indices.size(), 1, 0, 0, 0); |
|
|
|
|
|
|
|
|
|
vkCmdEndRenderPass(commandBuffer); |
|
|
|
|
vkEndCommandBuffer(commandBuffer); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Application::drawFrame() { |
|
|
|
@ -55,6 +115,8 @@ void Application::drawFrame() { |
|
|
|
|
vkResetCommandBuffer(commandPool->buffers[currentFrame], 0); |
|
|
|
|
recordCommandBuffer(commandPool->buffers[currentFrame], imageIndex); |
|
|
|
|
|
|
|
|
|
updateUniformBuffer(); |
|
|
|
|
|
|
|
|
|
VkSubmitInfo submitInfo {}; |
|
|
|
|
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
|
|
|
|
|
|
|
|
@ -110,50 +172,12 @@ void Application::createSyncObjects() { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex) { |
|
|
|
|
VkCommandBufferBeginInfo beginInfo {}; |
|
|
|
|
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
|
|
|
|
|
|
|
|
|
vkBeginCommandBuffer(commandBuffer, &beginInfo); |
|
|
|
|
|
|
|
|
|
VkRenderPassBeginInfo renderPassInfo {}; |
|
|
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; |
|
|
|
|
renderPassInfo.renderPass = swapchain->renderPass; |
|
|
|
|
renderPassInfo.framebuffer = swapchain->frameBuffers[imageIndex]; |
|
|
|
|
renderPassInfo.renderArea.offset = {0, 0}; |
|
|
|
|
renderPassInfo.renderArea.extent = swapchain->extent; |
|
|
|
|
|
|
|
|
|
VkClearValue clearColor = {{{0, 0, 0, 1}}}; |
|
|
|
|
renderPassInfo.clearValueCount = 1; |
|
|
|
|
renderPassInfo.pClearValues = &clearColor; |
|
|
|
|
|
|
|
|
|
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); |
|
|
|
|
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->graphicsPipeline); |
|
|
|
|
|
|
|
|
|
VkViewport viewport {}; |
|
|
|
|
viewport.x = 0; |
|
|
|
|
viewport.y = 0; |
|
|
|
|
viewport.width = static_cast<float>(swapchain->extent.width); |
|
|
|
|
viewport.height = static_cast<float>(swapchain->extent.height); |
|
|
|
|
viewport.minDepth = 0; |
|
|
|
|
viewport.maxDepth = 1; |
|
|
|
|
vkCmdSetViewport(commandBuffer, 0, 1, &viewport); |
|
|
|
|
|
|
|
|
|
VkRect2D scissor {}; |
|
|
|
|
scissor.offset = {0, 0}; |
|
|
|
|
scissor.extent = swapchain->extent; |
|
|
|
|
vkCmdSetScissor(commandBuffer, 0, 1, &scissor); |
|
|
|
|
|
|
|
|
|
VkBuffer buffers[] = {vertexBuffer->handle}; |
|
|
|
|
VkDeviceSize offsets[] = {0}; |
|
|
|
|
vkCmdBindVertexBuffers(commandBuffer, 0, 1, buffers, offsets); |
|
|
|
|
|
|
|
|
|
vkCmdBindIndexBuffer(commandBuffer, indexBuffer->handle, 0, VK_INDEX_TYPE_UINT32); |
|
|
|
|
|
|
|
|
|
vkCmdDrawIndexed(commandBuffer, indices.size(), 1, 0, 0, 0); |
|
|
|
|
|
|
|
|
|
vkCmdEndRenderPass(commandBuffer); |
|
|
|
|
vkEndCommandBuffer(commandBuffer); |
|
|
|
|
void Application::mainLoop() { |
|
|
|
|
while (!glfwWindowShouldClose(instance->window)){ |
|
|
|
|
glfwPollEvents(); |
|
|
|
|
drawFrame(); |
|
|
|
|
} |
|
|
|
|
vkDeviceWaitIdle(instance->device); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Application::~Application() { |
|
|
|
@ -166,6 +190,7 @@ Application::~Application() { |
|
|
|
|
delete commandPool; |
|
|
|
|
delete vertexBuffer; |
|
|
|
|
delete indexBuffer; |
|
|
|
|
delete uniformBuffer; |
|
|
|
|
delete pipeline; |
|
|
|
|
delete instance; |
|
|
|
|
} |
|
|
|
|