vulkan-project/renderer2D.cpp
2022-10-14 20:58:20 +02:00

323 lines
15 KiB
C++

#include "renderer2D.hpp"
#include "exceptions.hpp"
#include "vk_enum_string.h"
#include "vulkan_instance.hpp"
#include "texture_manager.hpp"
#include <cstring>
#include <thread>
#include <vulkan/vulkan_core.h>
namespace gz::vk {
//
// INIT & CLEANUP
//
Renderer2D::Renderer2D(VulkanInstance& instance, TextureManager& textureManager) :
Renderer(instance, textureManager),
rLog("renderer2D.log", true, false, "2D-Renderer", Color::LI_MAGENTA, VULKAN_MESSAGE_TIME_COLOR, true, 100)
{
vk.registerCleanupCallback(std::bind(&Renderer2D::cleanup, this));
vk.registerSwapChainRecreateCallback(std::bind(&Renderer2D::swapChainRecreateCallback, this));
vk.createCommandBuffers(commandBuffers);
const size_t vertexCount = 500;
const size_t indexCount = 1000;
vk.createVertexBuffer<Vertex2D>(vertexCount, vertexBuffer, vertexBufferMemory, vertexBufferSize);
vk.createIndexBuffer<uint32_t>(indexCount, indexBuffer, indexBufferMemory, indexBufferSize);
initSwapChainDependantResources();
VulkanInstance::registerObjectUsingVulkan(ObjectUsingVulkan("Renderer2D",
{ &pipelines[PL_2D].pipeline, &renderPass, &vertexBuffer, &vertexBufferMemory, &indexBuffer, &indexBufferMemory },
{ &framebuffers, &images, &imageMemory, &imageViews, &commandBuffers }));
rLog("Created Renderer2D");
}
void Renderer2D::cleanup() {
/* vk.destroyCommandBuffers(commandBuffers); */
cleanupSwapChainDependantResources();
cleanup_();
}
//
// SWAPCHAIN DEPENDANT
//
void Renderer2D::initSwapChainDependantResources() {
createRenderPass();
createImages();
vk.createFramebuffers(framebuffers, imageViews, renderPass);
std::vector<VkDescriptorSetLayout> descriptorSetLayouts = { textureManager.getDescriptorSetLayout() };
vk.createGraphicsPipeline<Vertex2D>("shaders/vert2D.spv", "shaders/frag2D.spv", descriptorSetLayouts, false, renderPass, pipelines[PL_2D]);
}
void Renderer2D::cleanupSwapChainDependantResources() {
// destroy pipelines
pipelines.destroy(vk.device);
vk.destroyFramebuffers(framebuffers);
for (size_t i = 0; i < images.size(); i++) {
vkDestroyImageView(vk.device, imageViews[i], nullptr);
vkDestroyImage(vk.device, images[i], nullptr);
vkFreeMemory(vk.device, imageMemory[i], nullptr);
}
vkDestroyRenderPass(vk.device, renderPass, nullptr);
}
void Renderer2D::swapChainRecreateCallback() {
cleanupSwapChainDependantResources();
initSwapChainDependantResources();
}
//
// IMAGES
//
void Renderer2D::createImages() {
images.resize(vk.scImages.size());
imageMemory.resize(vk.scImages.size());
imageViews.resize(vk.scImages.size());
VkImageUsageFlags usage= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
for (size_t i = 0; i < images.size(); i++) {
vk.createImage(vk.scExtent.width, vk.scExtent.height, vk.scImageFormat, VK_IMAGE_TILING_OPTIMAL, usage, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, images[i], imageMemory[i]);
vk.createImageView(vk.scImageFormat, images[i], imageViews[i], VK_IMAGE_ASPECT_COLOR_BIT);
}
}
//
// RENDER PASS
//
void Renderer2D::createRenderPass() {
VkAttachmentDescription2 colorBlendAttachment{};
colorBlendAttachment.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2;
colorBlendAttachment.format = vk.scImageFormat;
colorBlendAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorBlendAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorBlendAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorBlendAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorBlendAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorBlendAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorBlendAttachment.finalLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
VkAttachmentReference2 colorAttachmentRef{};
colorAttachmentRef.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2;
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
/* VkAttachmentDescription depthAttachment{}; */
/* depthAttachment.format = findDepthFormat(); */
/* depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT; */
/* depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; */
/* depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; */
/* depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; */
/* depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; */
/* depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; */
/* depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; */
/* VkAttachmentReference depthAttachmentRef{}; */
/* depthAttachmentRef.attachment = 1; */
/* depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; */
VkSubpassDescription2 subpass{};
subpass.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2;
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorAttachmentRef;
/* subpass.pDepthStencilAttachment = &depthAttachmentRef; */
VkSubpassDependency2 colorAttachmentSD{};
colorAttachmentSD.sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2;
colorAttachmentSD.srcSubpass = VK_SUBPASS_EXTERNAL;
colorAttachmentSD.dstSubpass = 0;
colorAttachmentSD.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT;
colorAttachmentSD.srcAccessMask = 0;
colorAttachmentSD.dstStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT;
colorAttachmentSD.dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT;
// dependecy for the image layout transition to transfer dst
VkSubpassDependency2 layoutTransitionSD{};
colorAttachmentSD.sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2;
colorAttachmentSD.srcSubpass = 0;
colorAttachmentSD.dstSubpass = VK_SUBPASS_EXTERNAL;
colorAttachmentSD.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT;
colorAttachmentSD.srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT;
colorAttachmentSD.dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT;
colorAttachmentSD.dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT;
colorAttachmentSD.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
/* VkSubpassDependency dependency{}; */
/* dependency.srcSubpass = VK_SUBPASS_EXTERNAL; */
/* dependency.dstSubpass = 0; */
/* dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT; */
/* dependency.srcAccessMask = 0; */
/* dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT; */
/* dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; */
/* std::array<VkAttachmentDescription, 2> attachments = { colorBlendAttachment, depthAttachment }; */
std::vector<VkAttachmentDescription2> attachments = { colorBlendAttachment };
std::vector<VkSubpassDependency2> dependencies = { colorAttachmentSD, layoutTransitionSD };
VkRenderPassCreateInfo2 renderPassCI{};
renderPassCI.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2;
renderPassCI.attachmentCount = static_cast<uint32_t>(attachments.size());
renderPassCI.pAttachments = attachments.data();
renderPassCI.subpassCount = 1;
renderPassCI.pSubpasses = &subpass;
renderPassCI.dependencyCount = dependencies.size();
renderPassCI.pDependencies = dependencies.data();
/* renderPassCI.dependencyCount = 0; */
/* renderPassCI.pDependencies = nullptr; */
/* renderPassCI.correlatedViewMaskCount = 0; */
/* renderPassCI.pCorrelatedViewMasks = nullptr; */
VkResult result = vkCreateRenderPass2(vk.device, &renderPassCI, nullptr, &renderPass);
if (result != VK_SUCCESS) {
throw getVkException(result, "Could not create render pass", "Renderer2D::createRenderPass");
}
rLog("createRenderPass: Created render pass.");
}
//
// RENDERING
//
void Renderer2D::recordCommandBuffer(uint32_t imageIndex, uint32_t currentFrame) {
VkCommandBufferBeginInfo commandBufferBI{};
commandBufferBI.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
/* commandBufferBI.flags = 0; */
/* commandBufferBI.pInheritanceInfo = nullptr; */
VkResult result = vkBeginCommandBuffer(commandBuffers[currentFrame], &commandBufferBI);
if (result != VK_SUCCESS) {
throw getVkException(result, "Failed to begin 2D command buffer", "Renderer2D::recordCommandBuffer");
}
VkRenderPassBeginInfo renderPassBI{};
renderPassBI.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassBI.renderPass = renderPass;
renderPassBI.framebuffer = framebuffers[imageIndex];
renderPassBI.renderArea.offset = { 0, 0 };
renderPassBI.renderArea.extent = vk.scExtent;
// clear
std::array<VkClearValue, 1> clearValues{};
clearValues[0].color = {{1.0f, 0.0f, 0.0f, 1.0f}};
/* clearValues[1].depthStencil = {1.0f, 0}; */
renderPassBI.clearValueCount = static_cast<uint32_t>(clearValues.size());
renderPassBI.pClearValues = clearValues.data();
vkCmdBeginRenderPass(commandBuffers[currentFrame], &renderPassBI, VK_SUBPASS_CONTENTS_INLINE);
vkCmdBindPipeline(commandBuffers[currentFrame], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines[PL_2D].pipeline);
VkBuffer vertexBuffers[] = { vertexBuffer };
VkDeviceSize offsets[] = {0};
uint32_t bindingCount = 1;
vkCmdBindVertexBuffers(commandBuffers[currentFrame], BINDING, bindingCount, vertexBuffers, offsets);
// TODO use correct index type!
vkCmdBindIndexBuffer(commandBuffers[currentFrame], indexBuffer, NO_OFFSET, VK_INDEX_TYPE_UINT32);
uint32_t descriptorCount = 1;
uint32_t firstSet = 0;
uint32_t dynamicOffsetCount = 0;
uint32_t* dynamicOffsets = nullptr;
vkCmdBindDescriptorSets(commandBuffers[currentFrame], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines[PL_2D].layout, firstSet, descriptorCount, &textureManager.getDescriptorSet(), dynamicOffsetCount, dynamicOffsets);
int instanceCount = 1;
int firstIndex = 0;
int firstInstance = 0;
vkCmdDrawIndexed(commandBuffers[currentFrame], static_cast<uint32_t>(shapesIndicesCount), instanceCount, firstIndex, NO_OFFSET, firstInstance);
vkCmdEndRenderPass(commandBuffers[currentFrame]);
vk.copyImageToImage(commandBuffers[currentFrame], images[imageIndex], vk.scImages[imageIndex], vk.scExtent);
result = vkEndCommandBuffer(commandBuffers[currentFrame]);
if (result != VK_SUCCESS) {
rLog.error("Failed to record 2D - command buffer", "VkResult:", STR_VK_RESULT(result));
throw getVkException(result, "Failed to record 2D - command buffer", "Renderer2D::recordCommandBufferWithTexture");
}
vk.submitThisFrame(commandBuffers[currentFrame]);
}
void Renderer2D::fillVertexBufferWithShapes() {
rLog("fillVertexBufferWithShapes");
if (vertexBufferSize < shapesVerticesCount * sizeof(Vertex2D)) {
throw VkException("vertex buffer too small! vertexBufferSize: " + std::to_string(vertexBufferSize) + ", required size: " + std::to_string(shapesVerticesCount), "fillVertexBufferWithShapes");
}
// create staging buffer
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
vk.createBuffer(vertexBufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
// fill staging buffer
void* data;
vkMapMemory(vk.device, stagingBufferMemory, NO_OFFSET, vertexBufferSize, NO_FLAGS, &data);
Vertex2D* vdata = reinterpret_cast<Vertex2D*>(data);
size_t offset = 0;
for (auto it = shapes.begin(); it != shapes.end(); it++) {
rLog("fillVertexBufferWithShapes: copying vertex buffer nr", it - shapes.begin(), "-", it->getVertices(), "to address:", long(vdata + offset), " offset:", offset);
memcpy(vdata+offset, it->getVertices().data(), it->getVertices().size() * sizeof(Vertex2D));
offset += it->getVertices().size();
}
vkUnmapMemory(vk.device, stagingBufferMemory);
// fill vertex buffer
vk.copyBuffer(stagingBuffer, vertexBuffer, vertexBufferSize);
vkDestroyBuffer(vk.device, stagingBuffer, nullptr);
vkFreeMemory(vk.device, stagingBufferMemory, nullptr);
}
void Renderer2D::fillIndexBufferWithShapes() {
rLog("fillIndexBufferWithShapes");
if (indexBufferSize < shapesIndicesCount * sizeof(uint32_t)) {
throw VkException("index buffer too small! indexBufferSize: " + std::to_string(vertexBufferSize) + ", required size: " + std::to_string(shapesVerticesCount), "fillVertexBufferWithShapes");
}
// create staging buffer
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
vk.createBuffer(indexBufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
// fill staging buffer
void* data;
vkMapMemory(vk.device, stagingBufferMemory, NO_OFFSET, indexBufferSize, NO_FLAGS, &data);
uint32_t* idata = reinterpret_cast<uint32_t*>(data);
size_t offset = 0;
for (auto it = shapes.begin(); it != shapes.end(); it++) {
rLog("fillIndexBufferWithShapes: copying index buffer nr", it - shapes.begin(), "-", it->getIndices(), "to address:", long(idata + offset), " offset:", offset);
memcpy(idata+offset, it->getIndices().data(), it->getIndices().size() * sizeof(uint32_t));
offset += it->getIndices().size();
}
rLog("fillIndexBufferWithShapes: indices count:", shapesIndicesCount);
vkUnmapMemory(vk.device, stagingBufferMemory);
// fill index buffer
vk.copyBuffer(stagingBuffer, indexBuffer, indexBufferSize);
vkDestroyBuffer(vk.device, stagingBuffer, nullptr);
vkFreeMemory(vk.device, stagingBufferMemory, nullptr);
}
void Renderer2D::drawShape(Shape* shape) {
// make indices valid
shape->setIndexOffset(shapesVerticesCount);
shape->normalizeVertices(vk.scExtent.width, vk.scExtent.height);
shape->setTextureCoordinates(textureManager);
// object slicing here, need to call virtual setTextureCoordinates before this!
shapes.push_back(*shape);
shapesVerticesCount += shape->getVertices().size();
shapesIndicesCount += shape->getIndices().size();
}
void Renderer2D::drawFrame(uint32_t imageIndex) {
vkResetCommandBuffer(commandBuffers[vk.currentFrame], NO_FLAGS);
/* recordCommandBuffer(imageIndex, vk.currentFrame); */
recordCommandBuffer(imageIndex, vk.currentFrame);
}
} // namespace gz::vk