444 lines
22 KiB
C++
444 lines
22 KiB
C++
#include "renderer3D.hpp"
|
|
|
|
#include "vulkan_instance.hpp"
|
|
#include "texture_manager.hpp"
|
|
#include "exceptions.hpp"
|
|
#include "vk_enum_string.h"
|
|
|
|
#include <cstring>
|
|
|
|
namespace gz::vk {
|
|
//
|
|
// INIT & CLEANUP
|
|
//
|
|
Renderer3D::Renderer3D(VulkanInstance& instance, TextureManager& textureManager) :
|
|
Renderer(instance, textureManager) {
|
|
LogCreateInfo logCI{};
|
|
logCI.logfile = "renderer3D.log";
|
|
logCI.storeLog = false;
|
|
logCI.prefix = "3D-Renderer";
|
|
logCI.prefixColor = Color::LI_CYAN;
|
|
logCI.timeColor = VULKAN_MESSAGE_TIME_COLOR;
|
|
rLog = Log(std::move(logCI));
|
|
|
|
vk.registerCleanupCallback(std::bind(&Renderer3D::cleanup, this));
|
|
vk.registerSwapChainRecreateCallback(std::bind(&Renderer3D::swapChainRecreateCallback, this));
|
|
|
|
vk.createCommandBuffers(commandBuffers);
|
|
const size_t vertexCount = 500;
|
|
const size_t indexCount = 1000;
|
|
vk.createVertexBuffer<Vertex3D>(vertexCount, vertexBuffer, vertexBufferMemory, vertexBufferSize);
|
|
vk.createIndexBuffer<uint32_t>(indexCount, indexBuffer, indexBufferMemory, indexBufferSize);
|
|
rLog("Created Renderer3D");
|
|
|
|
initSwapChainDependantResources();
|
|
VulkanInstance::registerObjectUsingVulkan(ObjectUsingVulkan("Renderer3D",
|
|
{ &pipelines[PL_3D].pipeline, &renderPass, &vertexBuffer, &vertexBufferMemory, &indexBuffer, &indexBufferMemory },
|
|
{ &framebuffers, &images, &imageMemory, &imageViews, &commandBuffers }));
|
|
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);
|
|
}
|
|
cleanupSwapChainDependantResources();
|
|
cleanup_();
|
|
}
|
|
|
|
|
|
//
|
|
// SWAPCHAIN DEPENDANT
|
|
//
|
|
void Renderer3D::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 Renderer3D::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 Renderer3D::swapChainRecreateCallback() {
|
|
cleanupSwapChainDependantResources();
|
|
initSwapChainDependantResources();
|
|
}
|
|
|
|
|
|
//
|
|
// IMAGES
|
|
//
|
|
void Renderer3D::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 Renderer3D::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", "Renderer3D::createRenderPass");
|
|
}
|
|
rLog("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
|
|
//
|
|
void Renderer3D::createDescriptorResources() {
|
|
// LAYOUT
|
|
// 1) uniform buffer object
|
|
VkDescriptorSetLayoutBinding uboLayoutBinding{};
|
|
uboLayoutBinding.binding = 0;
|
|
uboLayoutBinding.descriptorCount = 1;
|
|
uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
|
uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
|
/* uboLayoutBinding.pImmutableSamplers = nullptr; */
|
|
|
|
// 2) combined image sampler
|
|
VkDescriptorSetLayoutBinding samplerLayoutBinding{};
|
|
samplerLayoutBinding.binding = 1;
|
|
samplerLayoutBinding.descriptorCount = 1;
|
|
samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
|
|
/* samplerLayoutBinding.pImmutableSamplers = nullptr; */
|
|
|
|
std::vector<VkDescriptorSetLayoutBinding> bindings = { uboLayoutBinding, samplerLayoutBinding };
|
|
vk.createDescriptorSetLayout(bindings, descriptorSetLayout);
|
|
|
|
// 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[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
poolSizes[1].descriptorCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT);
|
|
|
|
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);
|
|
if (result != VK_SUCCESS) {
|
|
throw getVkException(result, "Failed to create descriptor pool", "Renderer3D::createDescriptorResources");
|
|
}
|
|
|
|
// SETS
|
|
std::vector<VkDescriptorSetLayout> layouts(MAX_FRAMES_IN_FLIGHT, 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.pSetLayouts = layouts.data();
|
|
|
|
descriptorSets.resize(MAX_FRAMES_IN_FLIGHT);
|
|
result = vkAllocateDescriptorSets(vk.device, &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++) {
|
|
VkDescriptorBufferInfo bufferI{};
|
|
bufferI.buffer = uniformBuffers[i];
|
|
bufferI.offset = 0;
|
|
bufferI.range = VK_WHOLE_SIZE; // sizeof(UniformBufferObject);
|
|
|
|
VkDescriptorImageInfo imageI{};
|
|
imageI.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
// TODO
|
|
/* imageI.imageView = textureImageView; */
|
|
/* imageI.sampler = textureSampler; */
|
|
|
|
std::array<VkWriteDescriptorSet, 2> descriptorW{};
|
|
descriptorW[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
descriptorW[0].dstSet = descriptorSets[i];
|
|
descriptorW[0].dstBinding = bindingUniformBuffer;
|
|
descriptorW[0].dstArrayElement = 0;
|
|
descriptorW[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
|
descriptorW[0].descriptorCount = 1;
|
|
descriptorW[0].pBufferInfo = &bufferI;
|
|
/* descriptorW[0].pImageInfo = nullptr; */
|
|
/* descriptorW[0].pTexelBufferView = nullptr; */
|
|
|
|
descriptorW[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
descriptorW[1].dstSet = descriptorSets[i];
|
|
descriptorW[1].dstBinding = bindingCombinedImageSampler;
|
|
descriptorW[1].dstArrayElement = 0;
|
|
descriptorW[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
descriptorW[1].descriptorCount = 1;
|
|
/* descriptorW[1].pBufferInfo = &bufferI; */
|
|
descriptorW[1].pImageInfo = &imageI;
|
|
/* descriptorW[1].pTexelBufferView = nullptr; */
|
|
|
|
uint32_t descriptorWriteCount = static_cast<uint32_t>(descriptorW.size());
|
|
uint32_t descriptorCopyCount = 0;
|
|
vkUpdateDescriptorSets(vk.device, descriptorWriteCount, descriptorW.data(), descriptorCopyCount, nullptr);
|
|
}
|
|
rLog("createDescriptorResources: Created descriptor layouts, pool and sets.");
|
|
}
|
|
|
|
//
|
|
// 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"); */
|
|
/* } */
|
|
|
|
/* 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(); */
|
|
|
|
/* 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", "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); */
|
|
|
|
/* } */
|
|
|
|
|
|
//
|
|
// RENDERING
|
|
//
|
|
void Renderer3D::drawFrame(uint32_t imageIndex) {
|
|
vkResetCommandBuffer(commandBuffers[vk.currentFrame], NO_FLAGS);
|
|
/* recordCommandBuffer(imageIndex, vk.currentFrame); */
|
|
recordCommandBuffer(imageIndex, vk.currentFrame);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace gz::vk
|