Commit 950da9b6 authored by Lukas Tietze's avatar Lukas Tietze

Frame-Klasse

parent a3a5fb02
......@@ -12,6 +12,7 @@
#include "Core/AppCreateInfo.hpp"
#include "Core/Camera.hpp"
#include "Core/DeviceInfo.hpp"
#include "Core/Frame.hpp"
#include "Util/GenericVertex.hpp"
#include "Util/NameList.hpp"
#include "VkUtil/BottomLevelAccelerationStructure.hpp"
......@@ -118,15 +119,12 @@ namespace lib
vk::DebugUtilsMessengerEXT debugMessenger;
vk::SurfaceKHR surface;
vk::SwapchainKHR swapChain;
std::vector<vk::Image> swapChainImages;
std::vector<vk::ImageView> swapChainImageViews;
vk::Extent2D swapChainExtent = {};
vk::Format swapChainImageFormat = {};
vk::PipelineLayout pipelineLayout;
vk::Pipeline pipeline;
std::vector<vk::CommandPool> drawingCommandPools;
vk::CommandPool stagingCommandPool;
vk::CommandPool computeCommandPool;
......@@ -135,11 +133,10 @@ namespace lib
vk::Queue bufferStagingQueue;
vk::Queue computeQueue;
std::vector<vk::CommandBuffer> commandBuffers;
std::vector<vk::Semaphore> imageAvailableSemaphores;
std::vector<vk::Semaphore> renderFinishedSemaphores;
std::vector<vk::Fence> inFlightFences;
std::vector<vk::Fence> inFlightImages;
std::vector<lib::Frame> frames;
// memory
using vertex_t = GenericVertex<float, 4, vk::Format::eR32G32B32Sfloat,
......@@ -147,7 +144,6 @@ namespace lib
float, 4, vk::Format::eR32G32B32Sfloat,
float, 4, vk::Format::eR32G32B32Sfloat>;
using index_t = uint32_t;
std::vector<StagedBuffer> uniformBuffers;
std::vector<Buffer> vertexBuffers;
std::vector<Buffer> indexBuffers;
std::unique_ptr<Buffer> materialsBuffer;
......@@ -192,7 +188,6 @@ namespace lib
uint32_t count = 0;
} shaderOffsets;
std::vector<vk::DescriptorSet> descriptorSets;
std::unique_ptr<lib::Buffer> shaderBindingTableBuffer;
bool modelLoaded;
......
#pragma once
#include <memory>
#include "GlfwAndVulkan.hpp"
#include "VkUtil/StagedBuffer.hpp"
namespace lib
{
struct Frame
{
std::unique_ptr<lib::StagedBuffer> ubo;
vk::CommandPool commandPool;
vk::CommandBuffer commandBuffer;
vk::Image swapChainImage;
vk::ImageView swapChainImageView;
vk::Fence inFlightFence;
vk::DescriptorSet descriptorSet;
std::unique_ptr<lib::Texture> raytracingTexture;
std::unique_ptr<lib::Texture> raytracingTexture;
};
} // namespace lib
......@@ -24,15 +24,27 @@ namespace lib
vk::DeviceMemory memory;
vk::Sampler sampler;
void LoadData(uint32_t width, uint32_t height, const void *data, uint8_t pixelByteCount);
static constexpr vk::Format DefaultFormat = vk::Format::eR8G8B8A8Srgb;
protected:
virtual void LoadData(uint32_t width, uint32_t height, const void *data, uint8_t pixelByteCount);
public:
/**
* @brief Initialisiert eine neue Instanz der Texture Klasse als leere Textur.
*
* @param parent Der Kontext.
* @param width Die Breite in Pixeln.
* @param height Die H枚he in Pixeln.
* @param format Das Zielformat der Daten, Standard ist RGBA mit 8 bit pro Kanal.
*/
Texture(CgContext *parent, uint32_t width, uint32_t height, vk::Format format = DefaultFormat);
/**
* @brief Initialisiert eine neue Instanz der Texture Klasse und l盲dt die Daten aus
* einer Datei. Der Typ der Daten wird dabei automatisch anhand der Dateiendung bestimmt.
*
* @param parent Der Kontext.
* @param format Das Format der Daten, Standard ist RGBA mit 8 bit pro Kanal.
* @param path Der Pfad zur Quelldatei.
*/
......@@ -41,6 +53,7 @@ namespace lib
/**
* @brief Initialisiert eine neue Instanz der Texture Klasse und l盲dt die Daten aus einem Puffer.
*
* @param parent Der Kontext.
* @param width Die Breite in Pixeln.
* @param height Die H枚he in Pixeln.
* @param data Die Daten als byte-Array.
......@@ -53,6 +66,7 @@ namespace lib
* @brief Initialisiert eine neue Instanz der Texture Klasse und erzeugt die Daten mittels
* eines Generators.
*
* @param parent Der Kontext.
* @param width Die Breite in Pixeln.
* @param height Die H枚he in Pixeln.
* @tparam T Der Typ des Generators.
......
......@@ -63,9 +63,9 @@ namespace
std::vector<vk::Framebuffer> genImGuiFrameBuffers(vk::Device logicalDevice,
const vk::RenderPass &renderpass,
const vk::Extent2D &extent,
const std::vector<vk::ImageView> &imageViews)
const std::vector<lib::Frame> &frames)
{
const auto count = static_cast<uint32_t>(imageViews.size());
const auto count = static_cast<uint32_t>(frames.size());
std::vector<vk::Framebuffer> res;
res.reserve(count);
......@@ -75,7 +75,7 @@ namespace
vk::FramebufferCreateInfo createInfo;
createInfo.renderPass = renderpass;
createInfo.attachmentCount = 1;
createInfo.pAttachments = &imageViews[i];
createInfo.pAttachments = &frames[i].swapChainImageView;
createInfo.width = extent.width;
createInfo.height = extent.height;
createInfo.layers = 1;
......@@ -94,12 +94,12 @@ namespace
void lib::CgContext::CreateImGuiComponents()
{
this->imGui.descriptorPool = ::genImGuiDescriptorPool(this->logicalDevice,
static_cast<uint32_t>(this->swapChainImages.size()));
static_cast<uint32_t>(this->frames.size()));
this->imGui.renderPass = ::genImGuiRenderPass(this->logicalDevice, this->swapChainImageFormat);
this->imGui.frameBuffers = ::genImGuiFrameBuffers(this->logicalDevice,
this->imGui.renderPass,
this->swapChainExtent,
this->swapChainImageViews);
this->frames);
ImGui_ImplVulkan_InitInfo info = {};
info.Instance = this->instance;
......@@ -110,8 +110,8 @@ void lib::CgContext::CreateImGuiComponents()
info.PipelineCache = VK_NULL_HANDLE;
info.DescriptorPool = this->imGui.descriptorPool;
info.Allocator = nullptr;
info.MinImageCount = static_cast<uint32_t>(this->swapChainImages.size());
info.ImageCount = static_cast<uint32_t>(this->swapChainImages.size());
info.MinImageCount = static_cast<uint32_t>(this->frames.size());
info.ImageCount = static_cast<uint32_t>(this->frames.size());
info.CheckVkResultFn = [](VkResult result) {
const auto res = static_cast<vk::Result>(result);
......@@ -163,7 +163,7 @@ void lib::CgContext::DestroyImGui()
void lib::CgContext::ReloadImGui()
{
ImGui_ImplVulkan_SetMinImageCount(static_cast<uint32_t>(this->swapChainImages.size()));
ImGui_ImplVulkan_SetMinImageCount(static_cast<uint32_t>(this->frames.size()));
ImGui_ImplVulkan_Shutdown();
this->DestroyImGuiComponents();
......
......@@ -155,9 +155,10 @@ lib::CgContext::~CgContext()
this->logicalDevice.destroyDescriptorSetLayout(this->descriptorSetLayout);
}
for (auto &uniformBuffer : this->uniformBuffers)
for (auto &frame : this->frames)
{
uniformBuffer.Destroy();
frame.ubo.reset();
this->logicalDevice.destroyCommandPool(frame.commandPool);
}
for (auto &semaphore : this->imageAvailableSemaphores)
......@@ -184,13 +185,6 @@ lib::CgContext::~CgContext()
}
}
for (const auto &commandPool : this->drawingCommandPools)
{
this->logicalDevice.destroyCommandPool(commandPool);
}
this->drawingCommandPools.clear();
if (this->stagingCommandPool)
{
this->logicalDevice.destroyCommandPool(this->stagingCommandPool);
......@@ -226,9 +220,9 @@ lib::CgContext::~CgContext()
void lib::CgContext::CleanupSwapChain()
{
for (size_t i = 0; i < this->drawingCommandPools.size(); i++)
for (const auto &frame : this->frames)
{
this->logicalDevice.freeCommandBuffers(this->drawingCommandPools[i], this->commandBuffers[i]);
this->logicalDevice.freeCommandBuffers(frame.commandPool, frame.commandBuffer);
}
if (this->shaderBindingTableBuffer)
......@@ -255,16 +249,14 @@ void lib::CgContext::CleanupSwapChain()
this->descriptorPool = nullptr;
}
for (const auto &imageView : this->swapChainImageViews)
for (const auto &frame : this->frames)
{
if (imageView)
if (frame.swapChainImageView)
{
this->logicalDevice.destroyImageView(imageView);
this->logicalDevice.destroyImageView(frame.swapChainImageView);
}
}
this->swapChainImageViews.clear();
if (this->swapChain)
{
this->logicalDevice.destroySwapchainKHR(this->swapChain);
......@@ -641,19 +633,23 @@ void lib::CgContext::CreateSwapChain()
Log("Successfully created swap chain!");
this->swapChainImages = this->logicalDevice.getSwapchainImagesKHR(this->swapChain);
const auto swapChainImages = this->logicalDevice.getSwapchainImagesKHR(this->swapChain);
this->frames.resize(swapChainImages.size());
for (size_t i = 0; i < swapChainImages.size(); i++)
{
this->frames[i].swapChainImage = swapChainImages[i];
}
Log("Successfully acquired swap chain images!");
}
void lib::CgContext::CreateImageViews()
{
this->swapChainImageViews.reserve(this->swapChainImages.size());
this->swapChainImageViews.clear();
for (const auto &image : this->swapChainImages)
for (auto &frame : this->frames)
{
this->swapChainImageViews.push_back(lib::CreateImageView(this->logicalDevice, image, this->swapChainImageFormat));
frame.swapChainImageView = lib::CreateImageView(this->logicalDevice, frame.swapChainImage, this->swapChainImageFormat);
}
Log("Successfully created all image views!");
......@@ -661,13 +657,11 @@ void lib::CgContext::CreateImageViews()
void lib::CgContext::CreateCommandPool()
{
this->drawingCommandPools.reserve(this->swapChainImages.size());
for (size_t i = 0; i < this->swapChainImages.size(); i++)
for (auto &frame : this->frames)
{
this->drawingCommandPools.push_back(
this->logicalDevice.createCommandPool({vk::CommandPoolCreateFlagBits::eResetCommandBuffer | vk::CommandPoolCreateFlagBits::eTransient,
this->physicalDeviceInfo.GetGraphicsQueueFamily()}));
frame.commandPool = this->logicalDevice.createCommandPool({vk::CommandPoolCreateFlagBits::eResetCommandBuffer |
vk::CommandPoolCreateFlagBits::eTransient,
this->physicalDeviceInfo.GetGraphicsQueueFamily()});
}
this->computeCommandPool = this->logicalDevice.createCommandPool(vk::CommandPoolCreateInfo(
......@@ -683,44 +677,44 @@ void lib::CgContext::CreateCommandPool()
void lib::CgContext::UpdateCommandBuffer(uint32_t imageIndex)
{
auto &commandPool = this->drawingCommandPools[imageIndex];
auto &commandBuffer = this->commandBuffers[imageIndex];
this->logicalDevice.resetCommandPool(commandPool, vk::CommandPoolResetFlagBits::eReleaseResources);
auto &frame = this->frames[imageIndex];
this->logicalDevice.resetCommandPool(frame.commandPool, vk::CommandPoolResetFlagBits::eReleaseResources);
this->pushConstant.rngSeed = this->randomDistribution(this->randomEngine);
vk::CommandBufferBeginInfo beginInfo;
commandBuffer.begin(beginInfo);
frame.commandBuffer.begin(beginInfo);
auto sbtEntrySize = this->physicalDeviceInfo.GetRaytracingProperties().shaderGroupHandleSize;
if (this->IsOk())
{
commandBuffer.bindPipeline(vk::PipelineBindPoint::eRayTracingNV, this->pipeline);
commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eRayTracingNV, this->pipelineLayout,
0, this->descriptorSets[imageIndex], {});
frame.commandBuffer.bindPipeline(vk::PipelineBindPoint::eRayTracingNV, this->pipeline);
frame.commandBuffer.bindDescriptorSets(vk::PipelineBindPoint::eRayTracingNV, this->pipelineLayout,
0, frame.descriptorSet, {});
}
TransitionImageLayout(commandBuffer,
this->swapChainImages[imageIndex], this->swapChainImageFormat,
TransitionImageLayout(frame.commandBuffer,
frame.swapChainImage, this->swapChainImageFormat,
vk::ImageLayout::eUndefined, vk::ImageLayout::eGeneral,
vk::PipelineStageFlagBits::eTopOfPipe,
vk::PipelineStageFlagBits::eRayTracingShaderNV);
if (this->IsOk())
{
this->uniformBuffers[imageIndex].ScheduleTransfer(commandBuffer);
frame.ubo->ScheduleTransfer(frame.commandBuffer);
commandBuffer.pushConstants(this->pipelineLayout,
vk::ShaderStageFlagBits::eRaygenNV | vk::ShaderStageFlagBits::eClosestHitNV,
0, sizeof(this->pushConstant),
&this->pushConstant);
frame.commandBuffer.pushConstants(this->pipelineLayout,
vk::ShaderStageFlagBits::eRaygenNV | vk::ShaderStageFlagBits::eClosestHitNV,
0, sizeof(this->pushConstant),
&this->pushConstant);
commandBuffer.traceRaysNV(this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.rayGen * sbtEntrySize,
this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.miss * sbtEntrySize, sbtEntrySize,
this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.hitGroup * sbtEntrySize, sbtEntrySize,
this->shaderBindingTableBuffer->GetBufferHandle(), 0, 0,
this->swapChainExtent.width, this->swapChainExtent.height, 1);
frame.commandBuffer.traceRaysNV(this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.rayGen * sbtEntrySize,
this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.miss * sbtEntrySize, sbtEntrySize,
this->shaderBindingTableBuffer->GetBufferHandle(), this->shaderOffsets.hitGroup * sbtEntrySize, sbtEntrySize,
this->shaderBindingTableBuffer->GetBufferHandle(), 0, 0,
this->swapChainExtent.width, this->swapChainExtent.height, 1);
}
if (this->imGui.show)
......@@ -731,36 +725,34 @@ void lib::CgContext::UpdateCommandBuffer(uint32_t imageIndex)
uiRenderInfo.renderArea.offset = {0, 0};
uiRenderInfo.renderArea.extent = this->swapChainExtent;
commandBuffer.beginRenderPass(uiRenderInfo, vk::SubpassContents::eInline);
frame.commandBuffer.beginRenderPass(uiRenderInfo, vk::SubpassContents::eInline);
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), commandBuffer);
ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), frame.commandBuffer);
commandBuffer.endRenderPass();
frame.commandBuffer.endRenderPass();
}
else
{
TransitionImageLayout(commandBuffer,
this->swapChainImages[imageIndex], this->swapChainImageFormat,
TransitionImageLayout(frame.commandBuffer,
frame.swapChainImage, this->swapChainImageFormat,
vk::ImageLayout::eGeneral, vk::ImageLayout::ePresentSrcKHR,
vk::PipelineStageFlagBits::eRayTracingShaderNV,
vk::PipelineStageFlagBits::eBottomOfPipe);
}
commandBuffer.end();
frame.commandBuffer.end();
}
void lib::CgContext::CreateCommandBuffers()
{
this->commandBuffers.clear();
for (const auto &commandPool : this->drawingCommandPools)
for (auto &frame : this->frames)
{
vk::CommandBufferAllocateInfo allocateInfo;
allocateInfo.commandPool = commandPool;
allocateInfo.commandPool = frame.commandPool;
allocateInfo.level = vk::CommandBufferLevel::ePrimary;
allocateInfo.commandBufferCount = 1;
this->commandBuffers.push_back(this->logicalDevice.allocateCommandBuffers(allocateInfo)[0]);
frame.commandBuffer = this->logicalDevice.allocateCommandBuffers(allocateInfo)[0];
}
Log("Successfully created command buffers!");
......@@ -774,12 +766,10 @@ void lib::CgContext::CreateSyncObjects()
this->imageAvailableSemaphores.clear();
this->renderFinishedSemaphores.clear();
this->inFlightFences.clear();
this->inFlightImages.clear();
this->imageAvailableSemaphores.reserve(this->appInfo.maxImagesInFlight);
this->renderFinishedSemaphores.reserve(this->appInfo.maxImagesInFlight);
this->inFlightFences.reserve(this->appInfo.maxImagesInFlight);
this->inFlightImages.resize(static_cast<uint32_t>(this->swapChainImages.size()));
for (uint32_t i = 0; i < this->appInfo.maxImagesInFlight; i++)
{
......@@ -896,7 +886,7 @@ vk::ShaderModule lib::CgContext::LoadShader(const ShaderInfo &info)
void lib::CgContext::UpdateUniformBuffers(uint32_t imageIndex, lib::time_diff_t delta)
{
this->camera.UpdateMatrices();
this->uniformBuffers[imageIndex].BufferDataHostOnly(SceneDescription{
this->frames[imageIndex].ubo->BufferDataHostOnly(SceneDescription{
this->camera.GetModel(),
this->lightSources,
this->clearColor});
......@@ -938,14 +928,14 @@ void lib::CgContext::DrawFrame(lib::time_diff_t delta)
this->UpdateUniformBuffers(imageIndex, delta);
if (this->inFlightImages[imageIndex])
if (this->frames[imageIndex].inFlightFence)
{
this->logicalDevice.waitForFences(inFlightImages[imageIndex], VK_TRUE, std::numeric_limits<uint64_t>::max());
this->logicalDevice.waitForFences(this->frames[imageIndex].inFlightFence, VK_TRUE, std::numeric_limits<uint64_t>::max());
}
this->UpdateCommandBuffer(imageIndex);
this->inFlightImages[imageIndex] = this->inFlightFences[this->frameIndex];
this->frames[imageIndex].inFlightFence = this->inFlightFences[this->frameIndex];
vk::PipelineStageFlags waitStages[] = {vk::PipelineStageFlagBits::eColorAttachmentOutput};
vk::SubmitInfo submitInfo;
......@@ -953,7 +943,7 @@ void lib::CgContext::DrawFrame(lib::time_diff_t delta)
submitInfo.pWaitSemaphores = &this->imageAvailableSemaphores[this->frameIndex];
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &this->commandBuffers[imageIndex];
submitInfo.pCommandBuffers = &this->frames[imageIndex].commandBuffer;
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &this->renderFinishedSemaphores[this->frameIndex];
......@@ -1127,20 +1117,19 @@ void lib::CgContext::CreateUniformBuffers()
this->clearColor = this->appInfo.modelInfo.clearColor;
this->lightSources = this->appInfo.modelInfo.lightSources;
auto bufferCount = this->swapChainImages.size();
this->uniformBuffers.clear();
this->uniformBuffers.reserve(bufferCount);
int i = 0;
for (int i = 0; i < bufferCount; i++)
for (auto &frame : this->frames)
{
const auto name = "UBO" + std::to_string(i);
this->uniformBuffers.emplace_back(std::move(this->CreateStagedBuffer(vk::BufferUsageFlagBits::eUniformBuffer)));
this->uniformBuffers.back().AllocateFor<SceneDescription>();
this->SetObjectName(this->uniformBuffers.back().GetHostBufferHandle(), name + "[Host]");
this->SetObjectName(this->uniformBuffers.back().GetDeviceBufferHandle(), name + "[Device]");
frame.ubo = std::make_unique<lib::StagedBuffer>(std::move(this->CreateStagedBuffer(vk::BufferUsageFlagBits::eUniformBuffer)));
frame.ubo->AllocateFor<SceneDescription>();
this->SetObjectName(frame.ubo->GetHostBufferHandle(), name + "[Host]");
this->SetObjectName(frame.ubo->GetDeviceBufferHandle(), name + "[Device]");
this->UpdateUniformBuffers(i, time_diff_t());
i++;
}
}
......@@ -1249,7 +1238,7 @@ void lib::CgContext::CreateDescriptorSetLayout()
void lib::CgContext::CreateDescriptorPool()
{
auto count = static_cast<uint32_t>(this->swapChainImages.size());
auto count = static_cast<uint32_t>(this->frames.size());
std::vector<vk::DescriptorPoolSize> sizes{
//Acceleration structure
vk::DescriptorPoolSize(vk::DescriptorType::eAccelerationStructureNV, count),
......@@ -1279,7 +1268,7 @@ void lib::CgContext::CreateDescriptorPool()
void lib::CgContext::CreateDescriptorSets()
{
auto count = static_cast<uint32_t>(this->swapChainImages.size());
auto count = static_cast<uint32_t>(this->frames.size());
std::vector<vk::DescriptorSetLayout> layouts(count, this->descriptorSetLayout);
vk::DescriptorSetAllocateInfo allocateInfo;
......@@ -1287,12 +1276,15 @@ void lib::CgContext::CreateDescriptorSets()
allocateInfo.pSetLayouts = layouts.data();
allocateInfo.descriptorSetCount = count;
this->descriptorSets = this->logicalDevice.allocateDescriptorSets(allocateInfo);
const auto descriptorSets = this->logicalDevice.allocateDescriptorSets(allocateInfo);
for (size_t i = 0; i < count; i++)
{
auto &frame = this->frames[i];
frame.descriptorSet = descriptorSets[i];
vk::StructureChain<vk::WriteDescriptorSet, vk::WriteDescriptorSetAccelerationStructureNV> accelerationStructureWrite;
accelerationStructureWrite.get<vk::WriteDescriptorSet>().dstSet = this->descriptorSets[i];
accelerationStructureWrite.get<vk::WriteDescriptorSet>().dstSet = frame.descriptorSet;
accelerationStructureWrite.get<vk::WriteDescriptorSet>().dstBinding = this->descriptorBindings.accelerationStructure;
accelerationStructureWrite.get<vk::WriteDescriptorSet>().dstArrayElement = 0;
accelerationStructureWrite.get<vk::WriteDescriptorSet>().descriptorType = vk::DescriptorType::eAccelerationStructureNV;
......@@ -1302,10 +1294,10 @@ void lib::CgContext::CreateDescriptorSets()
vk::DescriptorImageInfo outputImageInfo;
outputImageInfo.imageLayout = vk::ImageLayout::eGeneral;
outputImageInfo.imageView = this->swapChainImageViews[i];
outputImageInfo.imageView = frame.swapChainImageView;
vk::WriteDescriptorSet outputImageWrite;
outputImageWrite.dstSet = this->descriptorSets[i];
outputImageWrite.dstSet = frame.descriptorSet;
outputImageWrite.dstBinding = this->descriptorBindings.outputImage;
outputImageWrite.dstArrayElement = 0;
outputImageWrite.descriptorType = vk::DescriptorType::eStorageImage;
......@@ -1313,11 +1305,11 @@ void lib::CgContext::CreateDescriptorSets()
outputImageWrite.pImageInfo = &outputImageInfo;
vk::DescriptorBufferInfo camInfo;
camInfo.buffer = this->uniformBuffers[i].GetDeviceBufferHandle();
camInfo.buffer = frame.ubo->GetDeviceBufferHandle();
camInfo.range = sizeof(SceneDescription);
vk::WriteDescriptorSet camWrite;
camWrite.dstSet = this->descriptorSets[i];
camWrite.dstSet = frame.descriptorSet;
camWrite.dstBinding = this->descriptorBindings.cameraAndLightUbo;
camWrite.dstArrayElement = 0;
camWrite.descriptorType = vk::DescriptorType::eUniformBuffer;
......@@ -1335,7 +1327,7 @@ void lib::CgContext::CreateDescriptorSets()
}
vk::WriteDescriptorSet vertexBufferWrite;
vertexBufferWrite.dstSet = this->descriptorSets[i];
vertexBufferWrite.dstSet = frame.descriptorSet;
vertexBufferWrite.dstBinding = this->descriptorBindings.vertices;
vertexBufferWrite.dstArrayElement = 0;
vertexBufferWrite.descriptorType = vk::DescriptorType::eStorageBuffer;
......@@ -1353,7 +1345,7 @@ void lib::CgContext::CreateDescriptorSets()
}
vk::WriteDescriptorSet indexBufferWrite;
indexBufferWrite.dstSet = this->descriptorSets[i];
indexBufferWrite.dstSet = frame.descriptorSet;
indexBufferWrite.dstBinding = this->descriptorBindings.indices;
indexBufferWrite.dstArrayElement = 0;
indexBufferWrite.descriptorType = vk::DescriptorType::eStorageBuffer;
......@@ -1365,7 +1357,7 @@ void lib::CgContext::CreateDescriptorSets()
materialBufferInfo.range = VK_WHOLE_SIZE;
vk::WriteDescriptorSet materialBufferWrite;
materialBufferWrite.dstSet = this->descriptorSets[i];
materialBufferWrite.dstSet = frame.descriptorSet;
materialBufferWrite.dstBinding = this->descriptorBindings.materials;
materialBufferWrite.dstArrayElement = 0;
materialBufferWrite.descriptorType = vk::DescriptorType::eStorageBuffer;
......@@ -1377,7 +1369,7 @@ void lib::CgContext::CreateDescriptorSets()
objectInstanceInfo.range = VK_WHOLE_SIZE;
vk::WriteDescriptorSet objectInstanceWrite;
objectInstanceWrite.dstSet = this->descriptorSets[i];
objectInstanceWrite.dstSet = frame.descriptorSet;
objectInstanceWrite.dstBinding = this->descriptorBindings.objectInstances;
objectInstanceWrite.dstArrayElement = 0;
objectInstanceWrite.descriptorType = vk::DescriptorType::eStorageBuffer;
......@@ -1390,7 +1382,7 @@ void lib::CgContext::CreateDescriptorSets()
dummyTextureInfo.sampler = this->dummyTexture->GetSampler();
vk::WriteDescriptorSet dummyTextureWrite;
dummyTextureWrite.dstSet = this->descriptorSets[i];
dummyTextureWrite.dstSet = frame.descriptorSet;
dummyTextureWrite.dstBinding = this->descriptorBindings.dummyTexture;
dummyTextureWrite.dstArrayElement = 0;
dummyTextureWrite.descriptorType = vk::DescriptorType::eCombinedImageSampler;
......
......@@ -3,6 +3,12 @@
#include "stb_image.h"
#include "VkUtil/VkUtil.hpp"
lib::Texture::Texture(CgContext *parent, uint32_t width, uint32_t height, vk::Format format = DefaultFormat) : parent(parent),
format(format)
{
this->LoadData(width, height, nullptr, 0);
}
lib::Texture::Texture(CgContext *parent, const std::string &path, vk::Format format) : parent(parent),
format(format)
{
......@@ -60,9 +66,14 @@ lib::Texture::~Texture()
void lib::Texture::LoadData(uint32_t width, uint32_t height, const void *data, uint8_t pixelByteCount)
{
auto stagingBuffer = this->parent->CreateStagingSrcBuffer();
std::unique_ptr<lib::Buffer> stagingBuffer;
if (data && pixelByteCount)
{
stagingBuffer = std::make_unique<lib::Buffer>(std::move(this->parent->CreateStagingSrcBuffer()));
stagingBuffer.BufferData(data, width * height * pixelByteCount);
stagingBuffer->BufferData(data, width * height * pixelByteCount);
}
vk::ImageCreateInfo createInfo;
createInfo.imageType = vk::ImageType::e2D;
......@@ -96,28 +107,31 @@ void lib::Texture::LoadData(uint32_t width, uint32_t height, const void *data, u
device.bindImageMemory(this->image, this->memory, 0);
auto commandBuffer = this->parent->AllocateSingleUseStagingCommandBuffer();
if (data && pixelByteCount)
{
auto commandBuffer = this->parent->AllocateSingleUseStagingCommandBuffer();
vk::BufferImageCopy region;
region.imageSubresource.aspectMask = vk::ImageAspectFlagBits::eColor;
region.imageSubresource.mipLevel = 0;
region.imageSubresource.baseArrayLayer = 0;
region.imageSubresource.layerCount = 1;
region.imageOffset = {0, 0, 0};
region.imageExtent = {width, height, 1};
vk::BufferImageCopy