minecraftvulkan/engine/xe_renderer.cpp

157 lines
5.2 KiB
C++
Raw Normal View History

2022-09-19 01:20:51 +00:00
#include "xe_renderer.hpp"
#include "xe_device.hpp"
#include "xe_game_object.hpp"
#include "xe_swap_chain.hpp"
2022-09-19 11:08:42 +00:00
#include "xe_descriptors.hpp"
2022-09-19 01:20:51 +00:00
#include "xe_window.hpp"
#include <memory>
#include <vulkan/vulkan_core.h>
#include <array>
#include <cassert>
#include <stdexcept>
namespace xe {
2022-09-25 01:16:13 +00:00
Renderer::Renderer(Window& window, Device& device) : xeWindow{window}, xeDevice{device} {
2022-09-19 01:20:51 +00:00
recreateSwapChain();
createCommandBuffers();
}
2022-09-25 01:16:13 +00:00
Renderer::~Renderer() { freeCommandBuffers(); }
2022-09-19 01:20:51 +00:00
2022-09-25 01:16:13 +00:00
void Renderer::recreateSwapChain() {
2022-09-19 01:20:51 +00:00
auto extent = xeWindow.getExtent();
while (extent.width == 0 || extent.height == 0) {
extent = xeWindow.getExtent();
glfwWaitEvents();
}
vkDeviceWaitIdle(xeDevice.device());
if(xeSwapChain == nullptr) {
2022-09-25 01:16:13 +00:00
xeSwapChain = std::make_unique<SwapChain>(xeDevice, extent);
2022-09-19 01:20:51 +00:00
} else {
2022-09-25 01:16:13 +00:00
std::shared_ptr<SwapChain> oldSwapChain = std::move(xeSwapChain);
xeSwapChain = std::make_unique<SwapChain>(xeDevice, extent, oldSwapChain);
2022-09-19 01:20:51 +00:00
if(!oldSwapChain->compareSwapFormats(*xeSwapChain.get())) {
throw std::runtime_error("Swap chain image (or depth) format has changed");
}
}
// we'll come back to this in just a moment
}
2022-09-25 01:16:13 +00:00
void Renderer::createCommandBuffers() {
2022-09-19 01:20:51 +00:00
2022-09-25 01:16:13 +00:00
commandBuffers.resize(SwapChain::MAX_FRAMES_IN_FLIGHT);
2022-09-19 01:20:51 +00:00
VkCommandBufferAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandPool = xeDevice.getCommandPool();
allocInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers.size());
if(vkAllocateCommandBuffers(xeDevice.device(), &allocInfo, commandBuffers.data()) != VK_SUCCESS) {
throw std::runtime_error("failed to allocate command buffers!");
}
}
2022-09-25 01:16:13 +00:00
void Renderer::freeCommandBuffers() {
2022-09-19 01:20:51 +00:00
vkFreeCommandBuffers(
xeDevice.device(),
xeDevice.getCommandPool(),
static_cast<uint32_t>(commandBuffers.size()),
commandBuffers.data());
commandBuffers.clear();
}
2022-09-25 01:16:13 +00:00
VkCommandBuffer Renderer::beginFrame() {
2022-09-19 01:20:51 +00:00
assert(!isFrameStarted && "Can't acll beingFrame while already in progress");
auto result = xeSwapChain->acquireNextImage(&currentImageIndex);
if(result == VK_ERROR_OUT_OF_DATE_KHR) {
recreateSwapChain();
return nullptr;
}
if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) {
throw std::runtime_error("failed to acquire swap chain image");
}
isFrameStarted = true;
auto commandBuffer = getCurrentCommandBuffer();
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
if(vkBeginCommandBuffer(commandBuffer, &beginInfo) != VK_SUCCESS) {
throw std::runtime_error("failed to begin recording command buffers");
}
return commandBuffer;
}
2022-09-25 01:16:13 +00:00
void Renderer::endFrame() {
2022-09-19 01:20:51 +00:00
assert(isFrameStarted && "Can't call endFrame while frame is not in progress");
auto commandBuffer = getCurrentCommandBuffer();
if(vkEndCommandBuffer(commandBuffer) != VK_SUCCESS) {
throw std::runtime_error("failed to record command buffer");
}
auto result = xeSwapChain->submitCommandBuffers(&commandBuffer, &currentImageIndex);
if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || xeWindow.wasWindowResized()) {
xeWindow.resetWindowResizedFlag();
recreateSwapChain();
}
isFrameStarted = false;
2022-09-25 01:16:13 +00:00
currentFrameIndex = (currentFrameIndex + 1) % SwapChain::MAX_FRAMES_IN_FLIGHT;
2022-09-19 01:20:51 +00:00
}
2022-09-25 01:16:13 +00:00
void Renderer::beginSwapChainRenderPass(VkCommandBuffer commandBuffer){
2022-09-19 01:20:51 +00:00
assert(isFrameStarted && "Can't call beginSwapChainRenderPass while frame is not in progress");
assert(commandBuffer == getCurrentCommandBuffer() && "Can't begin render pass on command buffer from a different frame");
VkRenderPassBeginInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = xeSwapChain->getRenderPass();
renderPassInfo.framebuffer = xeSwapChain->getFrameBuffer(currentImageIndex);
renderPassInfo.renderArea.offset = {0, 0};
renderPassInfo.renderArea.extent = xeSwapChain->getSwapChainExtent();
std::array<VkClearValue, 2> clearValues{};
clearValues[0].color = {0.1f, 0.1f, 0.1f, 1.0f};
clearValues[1].depthStencil = {1.0f, 0};
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassInfo.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffer, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport{};
viewport.x = 0.0f;
viewport.y = static_cast<float>(xeSwapChain->getSwapChainExtent().height);
viewport.width = static_cast<float>(xeSwapChain->getSwapChainExtent().width);
viewport.height = -static_cast<float>(xeSwapChain->getSwapChainExtent().height);
viewport.minDepth = 0.0f;
viewport.maxDepth = 1.0f;
VkRect2D scissor{{0, 0}, xeSwapChain->getSwapChainExtent()};
vkCmdSetViewport(commandBuffer, 0, 1, &viewport);
vkCmdSetScissor(commandBuffer, 0, 1, &scissor);
}
2022-09-25 01:16:13 +00:00
void Renderer::endSwapChainRenderPass(VkCommandBuffer commandBuffer){
2022-09-19 01:20:51 +00:00
assert(isFrameStarted && "Can't call endSwapChainRenderPass while frame is not in progress");
assert(commandBuffer == getCurrentCommandBuffer() && "Can't end render pass on command buffer from a different frame");
vkCmdEndRenderPass(commandBuffer);
}
}