using VulkanAllocator and public interface of instance

This commit is contained in:
matthias@arch 2022-10-21 22:00:34 +02:00
parent 5d254bc513
commit e8f736d27f
12 changed files with 279 additions and 261 deletions

View File

@ -6,10 +6,7 @@ namespace gz::vk {
void Renderer::cleanup_(){
vk.destroyCommandBuffers(commandBuffers);
vkDestroyBuffer(vk.device, indexBuffer, nullptr);
vkFreeMemory(vk.device, indexBufferMemory, nullptr);
vkDestroyBuffer(vk.device, vertexBuffer, nullptr);
vkFreeMemory(vk.device, vertexBufferMemory, nullptr);
vk.destroyBuffer(indexBuffer, indexBufferMemory);
vk.destroyBuffer(vertexBuffer, vertexBufferMemory);
}
}

View File

@ -3,6 +3,7 @@
// includes for child classes
#include "texture_manager.hpp"
#include "vertex.hpp"
#include "vulkan_allocator.hpp"
#include <gz-util/log.hpp>
@ -19,6 +20,13 @@ namespace gz::vk {
public:
Renderer(VulkanInstance& instance, TextureManager& textureManager) : vk(instance), textureManager(textureManager) {};
protected:
/**
* @brief Cleanup vulkan resources held by this class
* @details
* - destroy commandBuffers
* - destroy(free) indexBuffer(Memory)
* - destroy(free) vertexBuffer(Memory)
*/
void cleanup_();
VulkanInstance& vk;
TextureManager& textureManager;
@ -26,10 +34,10 @@ namespace gz::vk {
std::vector<VkCommandBuffer> commandBuffers;
/// On device local memory
VkBuffer vertexBuffer;
VkDeviceMemory vertexBufferMemory;
MemoryInfo vertexBufferMemory;
VkDeviceSize vertexBufferSize;
VkBuffer indexBuffer;
VkDeviceMemory indexBufferMemory;
MemoryInfo indexBufferMemory;
VkDeviceSize indexBufferSize;
}; // class RendererBase
} // namespace gz::vk

View File

@ -2,6 +2,7 @@
#include "exceptions.hpp"
#include "vk_enum_string.h"
#include "vulkan_allocator.hpp"
#include "vulkan_instance.hpp"
#include "texture_manager.hpp"
@ -62,16 +63,16 @@ namespace gz::vk {
void Renderer2D::cleanupSwapChainDependantResources() {
// destroy pipelines
pipelines.destroy(vk.device);
pipelines.destroy(vk.getDevice());
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);
vkDestroyImageView(vk.getDevice(), imageViews[i], nullptr);
vkDestroyImage(vk.getDevice(), images[i], nullptr);
vkFreeMemory(vk.getDevice(), imageMemory[i], nullptr);
}
vkDestroyRenderPass(vk.device, renderPass, nullptr);
vkDestroyRenderPass(vk.getDevice(), renderPass, nullptr);
}
@ -87,13 +88,13 @@ namespace gz::vk {
// IMAGES
//
void Renderer2D::createImages() {
images.resize(vk.scImages.size());
imageMemory.resize(vk.scImages.size());
imageViews.resize(vk.scImages.size());
images.resize(vk.getScImages().size());
imageMemory.resize(vk.getScImages().size());
imageViews.resize(vk.getScImages().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);
vk.createImage(vk.getScExtent().width, vk.getScExtent().height, vk.getScImageFormat(), VK_IMAGE_TILING_OPTIMAL, usage, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, images[i], imageMemory[i]);
vk.createImageView(vk.getScImageFormat(), images[i], imageViews[i], VK_IMAGE_ASPECT_COLOR_BIT);
}
}
@ -103,7 +104,7 @@ namespace gz::vk {
void Renderer2D::createRenderPass() {
VkAttachmentDescription2 colorBlendAttachment{};
colorBlendAttachment.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2;
colorBlendAttachment.format = vk.scImageFormat;
colorBlendAttachment.format = vk.getScImageFormat();
colorBlendAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorBlendAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorBlendAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
@ -149,14 +150,14 @@ namespace gz::vk {
// 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;
layoutTransitionSD.sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2;
layoutTransitionSD.srcSubpass = 0;
layoutTransitionSD.dstSubpass = VK_SUBPASS_EXTERNAL;
layoutTransitionSD.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT;
layoutTransitionSD.srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT;
layoutTransitionSD.dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT;
layoutTransitionSD.dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT;
layoutTransitionSD.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
/* VkSubpassDependency dependency{}; */
/* dependency.srcSubpass = VK_SUBPASS_EXTERNAL; */
/* dependency.dstSubpass = 0; */
@ -180,7 +181,7 @@ namespace gz::vk {
/* renderPassCI.pDependencies = nullptr; */
/* renderPassCI.correlatedViewMaskCount = 0; */
/* renderPassCI.pCorrelatedViewMasks = nullptr; */
VkResult result = vkCreateRenderPass2(vk.device, &renderPassCI, nullptr, &renderPass);
VkResult result = vkCreateRenderPass2(vk.getDevice(), &renderPassCI, nullptr, &renderPass);
if (result != VK_SUCCESS) {
throw getVkException(result, "Could not create render pass", "Renderer2D::createRenderPass");
}
@ -207,7 +208,7 @@ namespace gz::vk {
renderPassBI.renderPass = renderPass;
renderPassBI.framebuffer = framebuffers[imageIndex];
renderPassBI.renderArea.offset = { 0, 0 };
renderPassBI.renderArea.extent = vk.scExtent;
renderPassBI.renderArea.extent = vk.getScExtent();
// clear
std::array<VkClearValue, 1> clearValues{};
clearValues[0].color = {{1.0f, 0.0f, 0.0f, 1.0f}};
@ -237,7 +238,7 @@ namespace gz::vk {
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);
vk.copyImageToImage(commandBuffers[currentFrame], images[imageIndex], vk.getScImages()[imageIndex], vk.getScExtent());
result = vkEndCommandBuffer(commandBuffers[currentFrame]);
if (result != VK_SUCCESS) {
rLog.error("Failed to record 2D - command buffer", "VkResult:", STR_VK_RESULT(result));
@ -255,12 +256,12 @@ namespace gz::vk {
// create staging buffer
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
MemoryInfo 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);
vkMapMemory(vk.getDevice(), stagingBufferMemory.memory, stagingBufferMemory.offset, vertexBufferSize, NO_FLAGS, &data);
Vertex2D* vdata = reinterpret_cast<Vertex2D*>(data);
size_t offset = 0;
for (auto it = shapes.begin(); it != shapes.end(); it++) {
@ -268,11 +269,10 @@ namespace gz::vk {
memcpy(vdata+offset, it->getVertices().data(), it->getVertices().size() * sizeof(Vertex2D));
offset += it->getVertices().size();
}
vkUnmapMemory(vk.device, stagingBufferMemory);
vkUnmapMemory(vk.getDevice(), stagingBufferMemory.memory);
// fill vertex buffer
vk.copyBuffer(stagingBuffer, vertexBuffer, vertexBufferSize);
vkDestroyBuffer(vk.device, stagingBuffer, nullptr);
vkFreeMemory(vk.device, stagingBufferMemory, nullptr);
vk.destroyBuffer(stagingBuffer, stagingBufferMemory);
}
void Renderer2D::fillIndexBufferWithShapes() {
rLog("fillIndexBufferWithShapes");
@ -282,12 +282,12 @@ namespace gz::vk {
// create staging buffer
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
MemoryInfo 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);
vkMapMemory(vk.getDevice(), stagingBufferMemory.memory, stagingBufferMemory.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++) {
@ -296,12 +296,11 @@ namespace gz::vk {
offset += it->getIndices().size();
}
rLog("fillIndexBufferWithShapes: indices count:", shapesIndicesCount);
vkUnmapMemory(vk.device, stagingBufferMemory);
vkUnmapMemory(vk.getDevice(), stagingBufferMemory.memory);
// fill index buffer
vk.copyBuffer(stagingBuffer, indexBuffer, indexBufferSize);
vkDestroyBuffer(vk.device, stagingBuffer, nullptr);
vkFreeMemory(vk.device, stagingBufferMemory, nullptr);
vk.destroyBuffer(stagingBuffer, stagingBufferMemory);
}
@ -309,7 +308,7 @@ namespace gz::vk {
void Renderer2D::drawShape(Shape* shape) {
// make indices valid
shape->setIndexOffset(shapesVerticesCount);
shape->normalizeVertices(vk.scExtent.width, vk.scExtent.height);
shape->normalizeVertices(vk.getScExtent().width, vk.getScExtent().height);
shape->setTextureCoordinates(textureManager);
// object slicing here, need to call virtual setTextureCoordinates before this!
shapes.push_back(*shape);
@ -319,9 +318,9 @@ namespace gz::vk {
void Renderer2D::drawFrame(uint32_t imageIndex) {
vkResetCommandBuffer(commandBuffers[vk.currentFrame], NO_FLAGS);
/* recordCommandBuffer(imageIndex, vk.currentFrame); */
recordCommandBuffer(imageIndex, vk.currentFrame);
vkResetCommandBuffer(commandBuffers[vk.getCurrentFrame()], NO_FLAGS);
/* recordCommandBuffer(imageIndex, vk.getCurrentFrame()); */
recordCommandBuffer(imageIndex, vk.getCurrentFrame());
}

View File

@ -80,7 +80,7 @@ namespace gz::vk {
* @details
* Attachments:
* - color blend:
* - loadOp = VK_ATTACHMENT_LOAD_OP_LOAD (not clear!)
* - loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR
* - storeOp = VK_ATTACHMENT_STORE_OP_STORE
* - initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
* - finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
@ -104,7 +104,7 @@ namespace gz::vk {
*/
/// @{
/**
* @brief Cleans up resources that were initialized by initSwapChainDependantResources
* @brief Cleans up resources that were initialized by initSwapChainDependantResources()
*/
void cleanupSwapChainDependantResources();
/**
@ -121,8 +121,8 @@ namespace gz::vk {
* @brief Recreates swap chain dependant resources
* @details
* Calls:
* -# cleanupSwapChainDependantResources
* -# initSwapChainDependantResources
* -# cleanupSwapChainDependantResources()
* -# initSwapChainDependantResources()
*/
void swapChainRecreateCallback();
/// @}

View File

@ -1,11 +1,14 @@
#include "renderer3D.hpp"
#include "vertex.hpp"
#include "vulkan_instance.hpp"
#include "texture_manager.hpp"
#include "exceptions.hpp"
#include "vk_enum_string.h"
#include <cstring>
#include <vulkan/vulkan_core.h>
#include <chrono>
namespace gz::vk {
//
@ -25,23 +28,30 @@ namespace gz::vk {
vk.registerSwapChainRecreateCallback(std::bind(&Renderer3D::swapChainRecreateCallback, this));
vk.createCommandBuffers(commandBuffers);
const size_t vertexCount = 500;
const size_t indexCount = 1000;
const size_t vertexCount = 20000;
const size_t indexCount = 10000;
vk.createVertexBuffer<Vertex3D>(vertexCount, vertexBuffer, vertexBufferMemory, vertexBufferSize);
vk.createIndexBuffer<uint32_t>(indexCount, indexBuffer, indexBufferMemory, indexBufferSize);
rLog("Created Renderer3D");
// TODO
loadModel();
createUniformBuffers();
createDescriptorResources();
initSwapChainDependantResources();
VulkanInstance::registerObjectUsingVulkan(ObjectUsingVulkan("Renderer3D",
{ &pipelines[PL_3D].pipeline, &renderPass, &vertexBuffer, &vertexBufferMemory, &indexBuffer, &indexBufferMemory },
{ &framebuffers, &images, &imageMemory, &imageViews, &commandBuffers }));
{ &pipelines[PL_3D].pipeline, &renderPass, &vertexBuffer, &vertexBufferMemory, &indexBuffer, &indexBufferMemory,
&descriptorSetLayout, &descriptorPool, },
{ &framebuffers, &images, &imageMemory, &imageViews, &commandBuffers,
&descriptorSets }));
rLog("Created Renderer3D");
}
void Renderer3D::cleanup() {
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
vkDestroyBuffer(vk.device, uniformBuffers[i], nullptr);
vkFreeMemory(vk.device, uniformBuffersMemory[i], nullptr);
for (size_t i = 0; i < vk.getMaxFramesInFlight(); i++) {
vkDestroyBuffer(vk.getDevice(), uniformBuffers[i], nullptr);
vkFreeMemory(vk.getDevice(), uniformBuffersMemory[i], nullptr);
}
cleanupSwapChainDependantResources();
cleanup_();
@ -55,23 +65,23 @@ namespace gz::vk {
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]);
std::vector<VkDescriptorSetLayout> descriptorSetLayouts = { descriptorSetLayout };
vk.createGraphicsPipeline<Vertex3D>("shaders/vert.spv", "shaders/frag.spv", descriptorSetLayouts, false, renderPass, pipelines[PL_3D]);
}
void Renderer3D::cleanupSwapChainDependantResources() {
// destroy pipelines
pipelines.destroy(vk.device);
pipelines.destroy(vk.getDevice());
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);
vkDestroyImageView(vk.getDevice(), imageViews[i], nullptr);
vkDestroyImage(vk.getDevice(), images[i], nullptr);
vkFreeMemory(vk.getDevice(), imageMemory[i], nullptr);
}
vkDestroyRenderPass(vk.device, renderPass, nullptr);
vkDestroyRenderPass(vk.getDevice(), renderPass, nullptr);
}
@ -87,13 +97,13 @@ namespace gz::vk {
// IMAGES
//
void Renderer3D::createImages() {
images.resize(vk.scImages.size());
imageMemory.resize(vk.scImages.size());
imageViews.resize(vk.scImages.size());
images.resize(vk.getScImages().size());
imageMemory.resize(vk.getScImages().size());
imageViews.resize(vk.getScImages().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);
vk.createImage(vk.getScExtent().width, vk.getScExtent().height, vk.getScImageFormat(), VK_IMAGE_TILING_OPTIMAL, usage, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, images[i], imageMemory[i]);
vk.createImageView(vk.getScImageFormat(), images[i], imageViews[i], VK_IMAGE_ASPECT_COLOR_BIT);
}
}
@ -104,7 +114,7 @@ namespace gz::vk {
void Renderer3D::createRenderPass() {
VkAttachmentDescription2 colorBlendAttachment{};
colorBlendAttachment.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2;
colorBlendAttachment.format = vk.scImageFormat;
colorBlendAttachment.format = vk.getScImageFormat();
colorBlendAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorBlendAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorBlendAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
@ -150,21 +160,21 @@ namespace gz::vk {
// 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; */
layoutTransitionSD.sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2;
layoutTransitionSD.srcSubpass = 0;
layoutTransitionSD.dstSubpass = VK_SUBPASS_EXTERNAL;
layoutTransitionSD.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT;
layoutTransitionSD.srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT;
layoutTransitionSD.dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT;
layoutTransitionSD.dstAccessMask = VK_ACCESS_2_TRANSFER_READ_BIT;
layoutTransitionSD.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 };
@ -181,38 +191,15 @@ namespace gz::vk {
/* renderPassCI.pDependencies = nullptr; */
/* renderPassCI.correlatedViewMaskCount = 0; */
/* renderPassCI.pCorrelatedViewMasks = nullptr; */
VkResult result = vkCreateRenderPass2(vk.device, &renderPassCI, nullptr, &renderPass);
VkResult result = vkCreateRenderPass2(vk.getDevice(), &renderPassCI, nullptr, &renderPass);
if (result != VK_SUCCESS) {
throw getVkException(result, "Could not create render pass", "Renderer3D::createRenderPass");
}
rLog("createRenderPass: Created render pass.");
rLog.log0("createRenderPass: Created render pass.");
}
void Renderer3D::updateUniformBuffer() {
static auto startTime = std::chrono::high_resolution_clock::now();
auto currentTime = std::chrono::high_resolution_clock::now();
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
// TODO use push constant instead of ubo
UniformBufferObject ubo{};
ubo.model = glm::rotate(glm::mat4(1.0f), time * std::numbers::pi_v<float> / 2, glm::vec3(0.0f, 0.0f, 1.0f));
ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
ubo.projection = glm::perspective(glm::radians(45.0f), static_cast<float>(vk.scExtent.width) / vk.scExtent.height, 1.0f, 10.0f);
/* ubo.model = glm::mat4(1); */
/* ubo.view = glm::mat4(1); */
/* ubo.projection = glm::mat4(1); */
/* ubo.projection[1][1] *= -1; // y coordinate inverted in opengl */
void* data;
vkMapMemory(vk.device, uniformBuffersMemory[vk.currentFrame], NO_OFFSET, sizeof(ubo), NO_FLAGS, &data);
memcpy(data, &ubo, sizeof(ubo));
vkUnmapMemory(vk.device, uniformBuffersMemory[vk.currentFrame]);
}
//
// DESCRIPTORS
//
@ -240,36 +227,36 @@ namespace gz::vk {
// POOL
std::array<VkDescriptorPoolSize, 2> poolSizes;
poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
poolSizes[0].descriptorCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT);
poolSizes[0].descriptorCount = static_cast<uint32_t>(vk.getMaxFramesInFlight());
poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
poolSizes[1].descriptorCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT);
poolSizes[1].descriptorCount = static_cast<uint32_t>(vk.getMaxFramesInFlight());
VkDescriptorPoolCreateInfo poolCI{};
poolCI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
poolCI.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
poolCI.pPoolSizes = poolSizes.data();
poolCI.maxSets = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT);
VkResult result = vkCreateDescriptorPool(vk.device, &poolCI, nullptr, &descriptorPool);
poolCI.maxSets = static_cast<uint32_t>(vk.getMaxFramesInFlight());
VkResult result = vkCreateDescriptorPool(vk.getDevice(), &poolCI, nullptr, &descriptorPool);
if (result != VK_SUCCESS) {
throw getVkException(result, "Failed to create descriptor pool", "Renderer3D::createDescriptorResources");
}
// SETS
std::vector<VkDescriptorSetLayout> layouts(MAX_FRAMES_IN_FLIGHT, descriptorSetLayout);
std::vector<VkDescriptorSetLayout> layouts(vk.getMaxFramesInFlight(), descriptorSetLayout);
VkDescriptorSetAllocateInfo setAI{};
setAI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
setAI.descriptorPool = descriptorPool;
setAI.descriptorSetCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT);
setAI.descriptorSetCount = static_cast<uint32_t>(layouts.size());
setAI.pSetLayouts = layouts.data();
descriptorSets.resize(MAX_FRAMES_IN_FLIGHT);
result = vkAllocateDescriptorSets(vk.device, &setAI, descriptorSets.data());
descriptorSets.resize(vk.getMaxFramesInFlight());
result = vkAllocateDescriptorSets(vk.getDevice(), &setAI, descriptorSets.data());
if (result != VK_SUCCESS) {
throw getVkException(result, "Failed to create descriptor sets", "Renderer3D::createDescriptorResources");
}
// configure sets
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
for (size_t i = 0; i < vk.getMaxFramesInFlight(); i++) {
VkDescriptorBufferInfo bufferI{};
bufferI.buffer = uniformBuffers[i];
bufferI.offset = 0;
@ -277,9 +264,8 @@ namespace gz::vk {
VkDescriptorImageInfo imageI{};
imageI.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
// TODO
/* imageI.imageView = textureImageView; */
/* imageI.sampler = textureSampler; */
imageI.imageView = textureManager.getTextureAtlas().getTextureImageView();
imageI.sampler = textureManager.getTextureAtlas().getTextureSampler();
std::array<VkWriteDescriptorSet, 2> descriptorW{};
descriptorW[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
@ -304,139 +290,158 @@ namespace gz::vk {
uint32_t descriptorWriteCount = static_cast<uint32_t>(descriptorW.size());
uint32_t descriptorCopyCount = 0;
vkUpdateDescriptorSets(vk.device, descriptorWriteCount, descriptorW.data(), descriptorCopyCount, nullptr);
vkUpdateDescriptorSets(vk.getDevice(), descriptorWriteCount, descriptorW.data(), descriptorCopyCount, nullptr);
}
rLog("createDescriptorResources: Created descriptor layouts, pool and sets.");
rLog.log0("createDescriptorResources: Created descriptor layouts, pool and sets.");
}
//
// MODEL
//
void Renderer3D::loadModel() {
// load model into VerticesAndIndices struct
rLog.log1("Renderer3D: loadModel: loading model");
vk.loadModel(model);
// TODO use correct type
VkDeviceSize requiredVertexBufferSize = model.vertices.size() * sizeof(Vertex3D);
VkDeviceSize requiredIndexBufferSize = model.indices.size() * sizeof(uint32_t);
if (requiredVertexBufferSize > vertexBufferSize) { throw VkException("Renderer3D::loadModel: vertex buffer too small"); }
if (requiredIndexBufferSize > indexBufferSize) { throw VkException("Renderer3D::loadModel: index buffer too small"); }
rLog.log0("Renderer3D: loadModel: filling vertex buffer");
// copy to vertexBuffer
// create staging buffer
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
vk.createBuffer(requiredVertexBufferSize, 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.getDevice(), stagingBufferMemory, NO_OFFSET, requiredVertexBufferSize, NO_FLAGS, &data);
memcpy(data, model.vertices.data(), requiredVertexBufferSize);
vkUnmapMemory(vk.getDevice(), stagingBufferMemory);
// fill vertex buffer
vk.copyBuffer(stagingBuffer, vertexBuffer, requiredVertexBufferSize);
vkDestroyBuffer(vk.getDevice(), stagingBuffer, nullptr);
vkFreeMemory(vk.getDevice(), stagingBufferMemory, nullptr);
rLog.log0("Renderer3D: loadModel: filling index buffer");
data = nullptr;
stagingBuffer = VK_NULL_HANDLE;
stagingBufferMemory = VK_NULL_HANDLE;
// copy to index buffer
vk.createBuffer(requiredIndexBufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
// fill staging buffer
vkMapMemory(vk.getDevice(), stagingBufferMemory, NO_OFFSET, requiredIndexBufferSize, NO_FLAGS, &data);
memcpy(data, model.indices.data(), requiredIndexBufferSize);
vkUnmapMemory(vk.getDevice(), stagingBufferMemory);
// fill index buffer
vk.copyBuffer(stagingBuffer, indexBuffer, requiredIndexBufferSize);
vkDestroyBuffer(vk.getDevice(), stagingBuffer, nullptr);
vkFreeMemory(vk.getDevice(), stagingBufferMemory, nullptr);
}
//
// RENDERING
//
void Renderer3D::recordCommandBuffer(uint32_t imageIndex, uint32_t currentFrame) {}
/* VkCommandBufferBeginInfo commandBufferBI{}; */
/* commandBufferBI.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; */
/* /1* commandBufferBI.flags = 0; *1/ */
/* /1* commandBufferBI.pInheritanceInfo = nullptr; *1/ */
/* VkResult result = vkBeginCommandBuffer(commandBuffers[currentFrame], &commandBufferBI); */
/* if (result != VK_SUCCESS) { */
/* throw getVkException(result, "Failed to begin 2D command buffer", "Renderer3D::recordCommandBuffer"); */
/* } */
void Renderer3D::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 3D command buffer", "Renderer3D::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}}; */
/* /1* clearValues[1].depthStencil = {1.0f, 0}; *1/ */
/* renderPassBI.clearValueCount = static_cast<uint32_t>(clearValues.size()); */
/* renderPassBI.pClearValues = clearValues.data(); */
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.getScExtent();
// 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); */
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); */
vkCmdBindPipeline(commandBuffers[currentFrame], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines[PL_3D].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", "Renderer3D::recordCommandBufferWithTexture"); */
/* } */
/* vk.submitThisFrame(commandBuffers[currentFrame]); */
/* } */
/* void Renderer3D::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 Renderer3D::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); */
/* } */
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_3D].layout, firstSet, descriptorCount, &descriptorSets[currentFrame], dynamicOffsetCount, dynamicOffsets);
int instanceCount = 1;
int firstIndex = 0;
int firstInstance = 0;
vkCmdDrawIndexed(commandBuffers[currentFrame], static_cast<uint32_t>(model.indices.size()), instanceCount, firstIndex, NO_OFFSET, firstInstance);
vkCmdEndRenderPass(commandBuffers[currentFrame]);
vk.copyImageToImage(commandBuffers[currentFrame], images[imageIndex], vk.getScImages()[imageIndex], vk.getScExtent());
result = vkEndCommandBuffer(commandBuffers[currentFrame]);
if (result != VK_SUCCESS) {
rLog.error("Failed to record 3D - command buffer", "VkResult:", STR_VK_RESULT(result));
throw getVkException(result, "Failed to record 3D - command buffer", "Renderer3D::recordCommandBuffer");
}
vk.submitThisFrame(commandBuffers[currentFrame]);
}
//
// RENDERING
//
void Renderer3D::drawFrame(uint32_t imageIndex) {
vkResetCommandBuffer(commandBuffers[vk.currentFrame], NO_FLAGS);
/* recordCommandBuffer(imageIndex, vk.currentFrame); */
recordCommandBuffer(imageIndex, vk.currentFrame);
vkResetCommandBuffer(commandBuffers[vk.getCurrentFrame()], NO_FLAGS);
/* recordCommandBuffer(imageIndex, vk.getCurrentFrame()); */
updateUniformBuffer();
recordCommandBuffer(imageIndex, vk.getCurrentFrame());
}
void Renderer3D::createUniformBuffers() {
VkDeviceSize bufferSize = sizeof(UniformBufferObject);
uniformBuffers.resize(vk.getMaxFramesInFlight());
uniformBuffersMemory.resize(vk.getMaxFramesInFlight());
for (size_t i = 0; i < vk.getMaxFramesInFlight(); i++) {
vk.createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, uniformBuffers[i], uniformBuffersMemory[i]);
}
}
void Renderer3D::updateUniformBuffer() {
static auto startTime = std::chrono::high_resolution_clock::now();
auto currentTime = std::chrono::high_resolution_clock::now();
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
// TODO use push constant instead of ubo
UniformBufferObject ubo{};
ubo.model = glm::rotate(glm::mat4(1.0f), time * std::numbers::pi_v<float> / 2, glm::vec3(0.0f, 0.0f, 1.0f));
ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
ubo.projection = glm::perspective(glm::radians(45.0f), static_cast<float>(vk.getScExtent().width) / vk.getScExtent().height, 1.0f, 10.0f);
/* ubo.model = glm::mat4(1); */
/* ubo.view = glm::mat4(1); */
/* ubo.projection = glm::mat4(1); */
/* ubo.projection[1][1] *= -1; // y coordinate inverted in opengl */
void* data;
vkMapMemory(vk.getDevice(), uniformBuffersMemory[vk.getCurrentFrame()], NO_OFFSET, sizeof(ubo), NO_FLAGS, &data);
memcpy(data, &ubo, sizeof(ubo));
vkUnmapMemory(vk.getDevice(), uniformBuffersMemory[vk.getCurrentFrame()]);
}

View File

@ -17,10 +17,12 @@ namespace gz::vk {
* @brief Create a 3D renderer
* @details
* -# @ref VulkanInstance::registerCleanupCallback "register" @ref cleanup() "cleanup callback"
* -# @ref VulkanInstance::registerSwapChainRecreateCallback "register" @ref swapChainRecreateCallback "swapChain recreation callback"
* -# @ref VulkanInstance::registerSwapChainRecreateCallback "register" @ref swapChainRecreateCallback() "swapChain recreation callback"
* -# create command buffers
* -# create vertex & index buffers
* -# call initSwapChainDependantResources
* -# call createUniformBuffers()
* -# call createDescriptorResources()
* -# call initSwapChainDependantResources()
*/
Renderer3D(VulkanInstance& instance, TextureManager& textureManager);
/**
@ -33,6 +35,7 @@ namespace gz::vk {
void recordCommandBuffer(uint32_t imageIndex, uint32_t currentFrame);
std::vector<VkBuffer> uniformBuffers;
std::vector<VkDeviceMemory> uniformBuffersMemory;
void createUniformBuffers();
void updateUniformBuffer();
/**
@ -55,7 +58,7 @@ namespace gz::vk {
* @details
* Attachments:
* - color blend:
* - loadOp = VK_ATTACHMENT_LOAD_OP_LOAD (not clear!)
* - loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR
* - storeOp = VK_ATTACHMENT_STORE_OP_STORE
* - initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
* - finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
@ -123,8 +126,8 @@ namespace gz::vk {
* @brief Recreates swap chain dependant resources
* @details
* Calls:
* -# cleanupSwapChainDependantResources
* -# initSwapChainDependantResources
* -# cleanupSwapChainDependantResources()
* -# initSwapChainDependantResources()
*/
void swapChainRecreateCallback();
/// @}
@ -139,7 +142,9 @@ namespace gz::vk {
*/
void cleanup();
void loadModel();
VerticesAndIndices<uint32_t> model;
Log rLog;
};
}

View File

@ -33,4 +33,5 @@ namespace gz {
* @brief Return a VkException with a formatted string
*/
VkException getVkException(VkResult result, std::string&& what="", std::string&& functionName="");
}

View File

@ -39,10 +39,10 @@ TextureAtlas::TextureAtlas(VulkanInstance& instance, uint16_t slotWidth, uint16_
void TextureAtlas::cleanup() {
VulkanInstance::vLog("TextureAtlas::cleanup, textureSampler", reinterpret_cast<uint64_t>(textureSampler), "textureImageView", reinterpret_cast<uint64_t>(textureImageView));
vkDestroySampler(vk.device, textureSampler, nullptr);
vkDestroyImageView(vk.device, textureImageView, nullptr);
vkDestroyImage(vk.device, textureImage, nullptr);
vkFreeMemory(vk.device, textureImageMemory, nullptr);
vkDestroySampler(vk.getDevice(), textureSampler, nullptr);
vkDestroyImageView(vk.getDevice(), textureImageView, nullptr);
vkDestroyImage(vk.getDevice(), textureImage, nullptr);
vkFreeMemory(vk.getDevice(), textureImageMemory, nullptr);
}
@ -154,9 +154,9 @@ void TextureAtlas::blitTextureOnImage(uint8_t* pixels, uint16_t slotX, uint16_t
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
stagingBuffer, stagingBufferMemory);
void* data;
vkMapMemory(vk.device, stagingBufferMemory, NO_OFFSET, imageSize, NO_FLAGS, &data);
vkMapMemory(vk.getDevice(), stagingBufferMemory, NO_OFFSET, imageSize, NO_FLAGS, &data);
memcpy(data, pixels, static_cast<size_t>(imageSize));
vkUnmapMemory(vk.device, stagingBufferMemory);
vkUnmapMemory(vk.getDevice(), stagingBufferMemory);
vk.transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
vk.copyBufferToImage(stagingBuffer, textureImage,
@ -164,8 +164,8 @@ void TextureAtlas::blitTextureOnImage(uint8_t* pixels, uint16_t slotX, uint16_t
static_cast<uint32_t>(textureWidth), static_cast<uint32_t>(textureHeight)); // dimensions
vk.transitionImageLayout(textureImage, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
vkDestroyBuffer(vk.device, stagingBuffer, nullptr);
vkFreeMemory(vk.device, stagingBufferMemory, nullptr);
vkDestroyBuffer(vk.getDevice(), stagingBuffer, nullptr);
vkFreeMemory(vk.getDevice(), stagingBufferMemory, nullptr);
}

View File

@ -21,9 +21,9 @@ namespace gz::vk {
void TextureManager::cleanup() {
/* vkFreeDescriptorSets(vk.device, descriptorPool, 1, &descriptorSet); */
vkDestroyDescriptorSetLayout(vk.device, descriptorSetLayout, NO_ALLOC);
vkDestroyDescriptorPool(vk.device, descriptorPool, NO_ALLOC);
/* vkFreeDescriptorSets(vk.getDevice(), descriptorPool, 1, &descriptorSet); */
vkDestroyDescriptorSetLayout(vk.getDevice(), descriptorSetLayout, NO_ALLOC);
vkDestroyDescriptorPool(vk.getDevice(), descriptorPool, NO_ALLOC);
}
@ -73,7 +73,7 @@ void TextureManager::createDescriptorSetLayout() {
descriptorSetLayoutCI.bindingCount = 1;
descriptorSetLayoutCI.pBindings = &samplerLayoutBinding;
VkResult result = vkCreateDescriptorSetLayout(vk.device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayout);
VkResult result = vkCreateDescriptorSetLayout(vk.getDevice(), &descriptorSetLayoutCI, nullptr, &descriptorSetLayout);
if (result != VK_SUCCESS) {
throw getVkException(result, "Could not create descriptorSetLayout", "TextureManager::createDescriptorSetLayout");
}
@ -93,7 +93,7 @@ void TextureManager::createDescriptorPool() {
poolCI.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
poolCI.pPoolSizes = poolSizes.data();
poolCI.maxSets = 1;
VkResult result = vkCreateDescriptorPool(vk.device, &poolCI, nullptr, &descriptorPool);
VkResult result = vkCreateDescriptorPool(vk.getDevice(), &poolCI, nullptr, &descriptorPool);
if (result != VK_SUCCESS) {
throw getVkException(result, "Failed to create descriptor pool", "TextureManager::createDescriptorPool");
}
@ -112,8 +112,8 @@ void TextureManager::createDescriptorSet() {
setAI.pSetLayouts = &descriptorSetLayout;
/* descriptorSets.resize(MAX_FRAMES_IN_FLIGHT); */
/* VkResult result = vkAllocateDescriptorSets(vk.device, &setAI, descriptorSets.data()); */
VkResult result = vkAllocateDescriptorSets(vk.device, &setAI, &descriptorSet);
/* VkResult result = vkAllocateDescriptorSets(vk.getDevice(), &setAI, descriptorSets.data()); */
VkResult result = vkAllocateDescriptorSets(vk.getDevice(), &setAI, &descriptorSet);
if (result != VK_SUCCESS) {
throw getVkException(result, "Failed to create descriptor set", "TextureManager::createDescriptorPool");
}
@ -138,7 +138,7 @@ void TextureManager::createDescriptorSet() {
uint32_t descriptorWriteCount = static_cast<uint32_t>(descriptorW.size());
uint32_t descriptorCopyCount = 0;
vkUpdateDescriptorSets(vk.device, descriptorWriteCount, descriptorW.data(), descriptorCopyCount, nullptr);
vkUpdateDescriptorSets(vk.getDevice(), descriptorWriteCount, descriptorW.data(), descriptorCopyCount, nullptr);
/* } */
/* vLog("createDescriptorSets: Created descriptor sets."); */
}

View File

@ -27,6 +27,9 @@ namespace gz::vk {
void getTexCoords(const std::string& textureName, glm::vec2& texCoords);
const VkDescriptorSet& getDescriptorSet() const { return descriptorSet; }
const VkDescriptorSetLayout& getDescriptorSetLayout() const { return descriptorSetLayout; }
// TODO
/// @todo take texture as argument
const TextureAtlas& getTextureAtlas() const { return atlases.at(0); };
private:

View File

@ -68,18 +68,18 @@ namespace gz::vk {
return true;
}
void PipelineContainer::erase(const PipelineT& key, VkDevice& device, const VkAllocationCallbacks* pAllocator) {
void PipelineContainer::erase(const PipelineT& key, const VkDevice& device, const VkAllocationCallbacks* pAllocator) {
vkDestroyPipeline(device, pipelines[key].pipeline, pAllocator);
vkDestroyPipelineLayout(device, pipelines[key].layout, pAllocator);
pipelines.erase(pipelines.find(key));
}
PipelineContainer::iterator PipelineContainer::erase(const PipelineContainer::iterator& it, VkDevice& device, const VkAllocationCallbacks* pAllocator) {
PipelineContainer::iterator PipelineContainer::erase(const PipelineContainer::iterator& it, const VkDevice& device, const VkAllocationCallbacks* pAllocator) {
vkDestroyPipeline(device, it->second.pipeline, pAllocator);
vkDestroyPipelineLayout(device, it->second.layout, pAllocator);
return pipelines.erase(it);
}
void PipelineContainer::destroy(VkDevice& device, const VkAllocationCallbacks* pAllocator) {
void PipelineContainer::destroy(const VkDevice& device, const VkAllocationCallbacks* pAllocator) {
auto it = pipelines.begin(); while (it != pipelines.end()) {
it = erase(it, device);
}

View File

@ -53,15 +53,15 @@ namespace gz::vk {
/**
* @brief Destroy the pipeline+layout and then remove the handles from the underlying map
*/
void erase(const PipelineT& key, VkDevice& device, const VkAllocationCallbacks* pAllocator=nullptr);
void erase(const PipelineT& key, const VkDevice& device, const VkAllocationCallbacks* pAllocator=nullptr);
/**
* @brief Destroy the pipeline+layout and then remove the handles from the underlying map
*/
iterator erase(const iterator& it, VkDevice& device, const VkAllocationCallbacks* pAllocator=nullptr);
iterator erase(const iterator& it, const VkDevice& device, const VkAllocationCallbacks* pAllocator=nullptr);
/**
* @brief Destroy all pipelines#layouts and then remove the handles from the underlying map
*/
void destroy(VkDevice& device, const VkAllocationCallbacks* pAllocator=nullptr);
void destroy(const VkDevice& device, const VkAllocationCallbacks* pAllocator=nullptr);
iterator begin() { return pipelines.begin(); }
iterator end() { return pipelines.end(); }
size_t size() const { return pipelines.size(); }