I’m building a video game engine using C++ as the coding language and Vulkan for graphics. I am trying to set up a generic renderer using Vulkan that is flexible and will render objects based on a vector that is supplied to it. The renderer will also handle the creation of the window using GLFW and use GLM for all relevant math calls. I am using the ASSIMP library to load 3d models and animations.
I am in the process of converting the codebase from using vulkan.h to vulkan.hpp, as well as utilising RAII. Here is the new header for the BufferUtils class:
#include "Texture.h"
#include "BufferUtils.h"
#include <iostream>
Texture::Texture()
: device(nullptr), image(), imageMemory(), imageView(), sampler()
{
}
Texture::~Texture()
{
Cleanup();
}
void Texture::LoadFromFile(const std::string& filename, vk::Device device, vk::PhysicalDevice physicalDevice, vk::CommandPool commandPool, vk::Queue graphicsQueue)
{
this->device = device;
this->physicalDevice = physicalDevice;
this->commandPool = commandPool;
this->graphicsQueue = graphicsQueue;
this->initialized = true;
// Load image from file using stb_image
int width, height, channels;
stbi_uc* pixels = stbi_load(filename.c_str(), &width, &height, &channels, STBI_rgb_alpha);
if (!pixels)
{
throw std::runtime_error("Failed to load texture image!");
}
// Calculate the number of mip levels
uint32_t mipLevels = static_cast<uint32_t>(std::floor(std::log2(std::max(width, height)))) + 1;
vk::DeviceSize imageSize = width * height * 4;
vk::Buffer stagingBuffer;
vk::DeviceMemory stagingBufferMemory;
// Create the staging buffer for transferring image data
BufferUtils::CreateBuffer(device, physicalDevice, imageSize, vk::BufferUsageFlagBits::eTransferSrc, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, stagingBuffer, stagingBufferMemory);
// Copy image data to the buffer
void* bufferData;
device.mapMemory(stagingBufferMemory, 0, imageSize,{}, &bufferData);
memcpy(bufferData, pixels, static_cast<size_t>(imageSize));
device.unmapMemory(stagingBufferMemory);
stbi_image_free(pixels); // Free the stb_image buffer
// Create vkImage, copy buffer to image, and create imageView and sampler
CreateImage(width, height, mipLevels, vk::SampleCountFlagBits::e1, vk::Format::eR8G8B8A8Srgb, vk::ImageTiling::eOptimal,
vk::ImageUsageFlagBits::eTransferSrc | vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled, vk::MemoryPropertyFlagBits::eDeviceLocal);
CreateImageView(vk::Format::eR8G8B8A8Srgb, vk::ImageAspectFlagBits::eColor, mipLevels);
CreateSampler(mipLevels);
TransitionImageLayout(vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, mipLevels, vk::SampleCountFlagBits::e1);
CopyBufferToImage(stagingBuffer, width, height);
GenerateMipmaps(width, height, mipLevels);
// Cleanup the staging buffer and staging buffer memory
device.destroyBuffer(stagingBuffer, nullptr);
device.freeMemory(stagingBufferMemory, nullptr);
}
Note that the BeginSingleTimeCommands and EndSingleTimeCommands functions are not included in this updated Texture class since they are already provided by the BufferUtils class. Make sure to use those functions directly from the BufferUtils class in the appropriate places in your Texture class methods.