I’m building a video game engine using C++ as the coding language and Vulkan for…
- User
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.
Here is a portion of the code:
BufferUtils.h:
#pragma once
#include <vulkan/vulkan.h> #include <stdint.h>
namespace BufferUtils { void CreateBuffer( VkDevice device, VkPhysicalDevice physicalDevice, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory);
uint32_t FindMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties); void CopyBuffer( VkDevice device, VkCommandPool commandPool, VkQueue graphicsQueue, VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size);}
Camera.h:
#pragma once
#include <glm/glm.hpp>
class Camera { public: Camera(); ~Camera();
void Initialize(float aspectRatio); void Shutdown(); void SetPosition(const glm::vec3& position); void SetRotation(const glm::vec3& rotation); const glm::mat4& GetViewMatrix() const; const glm::mat4& GetProjectionMatrix() const;private: glm::vec3 position; glm::vec3 rotation; glm::mat4 viewMatrix; glm::mat4 projectionMatrix;
void UpdateViewMatrix();};
Engine.h:
#pragma once
#include "Window.h" #include "Renderer.h" #include "Scene.h" #include #include
class Engine { public: Engine(); ~Engine();
void Run(); void Shutdown(); int MaxFPS = 60;private:
void Initialize(); void MainLoop(); void Update(float deltaTime); void Render(); Window window; Renderer renderer; Scene scene;};
GameObject.h:
#pragma once
#include <glm/glm.hpp> #include "Mesh.h" #include "Material.h" #include "Camera.h" #include "Renderer.h"
class GameObject { public: GameObject(); ~GameObject();
void Initialize(); void Initialize2(Renderer& renderer); void Update(float deltaTime); void Render(Renderer& renderer, const Camera& camera); void Shutdown(); void SetPosition(const glm::vec3& position); void SetRotation(const glm::vec3& rotation); void SetScale(const glm::vec3& scale); Mesh* GetMesh(); Material* GetMaterial();private: glm::mat4 modelMatrix; glm::vec3 position; glm::vec3 rotation; glm::vec3 scale;
VkDeviceMemory mvpBufferMemory; VkBuffer mvpBuffer; Mesh* mesh; Material* material; bool initialized = false; void UpdateModelMatrix();};
Material.h:
#pragma once
#include <vulkan/vulkan.h> #include "Texture.h" #include "Shader.h" #include #include // Don’t forget to include #include
// Add this struct outside the Material class, possibly at the top of Material.cpp struct ShaderDeleter { void operator()(Shader* shaderPtr) { if (shaderPtr != nullptr) { Shader::Cleanup(shaderPtr); } } };
class Material { public: Material(); ~Material();
void Initialize(const std::string& vertShaderPath, const std::string& fragShaderPath, const std::string& texturePath, VkDevice device, VkDescriptorSetLayout descriptorSetLayout, VkDescriptorPool descriptorPool, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue); void Cleanup(); void LoadTexture(const std::string& filename, VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue); void LoadShaders(const std::string& vertFilename, const std::string& fragFilename, VkDevice device); void UpdateBufferBinding(VkBuffer newBuffer, VkDevice device, VkDeviceSize devicesize); VkDescriptorSet GetDescriptorSet() const; VkPipelineLayout GetPipelineLayout() const; std::shared_ptr <Shader> GetvertexShader(); std::shared_ptr <Shader> GetfragmentShader(); void CreateDescriptorSet(VkBuffer uniformBuffer, VkDeviceSize bufferSize);private: VkDevice device; std::shared_ptr vertexShader; std::shared_ptr fragmentShader; std::shared_ptr texture;
void CreatePipelineLayout(VkDescriptorSetLayout descriptorSetLayout); VkDescriptorSet descriptorSet; VkPipelineLayout pipelineLayout; VkDescriptorSetLayout descriptorSetLayout;// = VK_NULL_HANDLE; VkDescriptorPool descriptorPool; void CleanupDescriptorSetLayout();};
Mesh.h:
#pragma once
#include #include <vulkan/vulkan.h> #include <glm/glm.hpp> #include "BufferUtils.h"
struct Vertex { glm::vec3 position; glm::vec3 color; };
class Mesh { public: Mesh(); ~Mesh();
void Initialize(std::vector<Vertex> vertices, std::vector<uint32_t> indices, VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue); void Initialize(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue); void Cleanup(); const std::vector<Vertex>& GetVertices() const; const std::vector<uint32_t>& GetIndices() const; VkBuffer GetVertexBuffer() const; VkBuffer GetIndexBuffer() const; void SetVertices(const std::vector<Vertex>& vertices); void SetIndices(const std::vector<uint32_t>& indices); std::vector<VkVertexInputBindingDescription> GetVertexInputBindingDescriptions() const; std::vector<VkVertexInputAttributeDescription> GetVertexInputAttributeDescriptions() const;private: VkDevice device; std::vector vertices; std::vector<uint32_t> indices;
VkBuffer vertexBuffer; VkDeviceMemory vertexBufferMemory; VkBuffer indexBuffer; VkDeviceMemory indexBufferMemory;};
Model.h:
#pragma once
#include <assimp/Importer.hpp> #include <assimp/scene.h> #include <assimp/postprocess.h> #include "Mesh.h" #include #include #include <vulkan/vulkan.h> #include
class Model { public: Model(); ~Model();
void LoadModel(const std::string& filepath, VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue); void Cleanup(); const std::vector<Mesh>& GetMeshes() const;private: std::vector meshes;
void LoadNode(aiNode* node, const aiScene* scene, VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue); Mesh LoadMesh(aiMesh* mesh, const aiScene* scene);};
Pipeline.h:
#pragma once
#include <vulkan/vulkan.h> #include #include #include #include "Shader.h"
class Pipeline { public: Pipeline(); ~Pipeline();
void CreateGraphicsPipeline(const std::vector<VkVertexInputBindingDescription>& vertexBindingDescriptions, const std::vector<VkVertexInputAttributeDescription>& vertexAttributeDescriptions, VkExtent2D swapchainExtent, const std::vector<Shader*>& shaders, VkRenderPass renderPass, VkPipelineLayout pipelineLayout, VkDevice device); void Cleanup(); VkPipeline GetPipeline() const; bool IsInitialized() const;private: VkDevice device; VkPipeline pipeline; bool initialized;
void CreateShaderStages(const std::vector<Shader*>& shaders, std::vector<VkPipelineShaderStageCreateInfo>& shaderStages);};
Renderer.h:
#pragma once
#include <vulkan/vulkan.h> #include "Window.h" #include #include #include #include #include #include "Pipeline.h" #include "Material.h" #include "Mesh.h"
struct QueueFamilyIndices { std::optional<uint32_t> graphicsFamily; std::optional<uint32_t> presentFamily;
bool IsComplete() { return graphicsFamily.has_value() && presentFamily.has_value(); }};
struct SwapChainSupportDetails { VkSurfaceCapabilitiesKHR capabilities; std::vector formats; std::vector presentModes; };
struct MVP { glm::mat4 model; glm::mat4 view; glm::mat4 projection; };
class Renderer { public: Renderer(); ~Renderer();
void Initialize(GLFWwindow* window); void Shutdown(); void BeginFrame(); void EndFrame(); VkDescriptorSetLayout CreateDescriptorSetLayout(); VkDescriptorPool CreateDescriptorPool(uint32_t maxSets); VkDevice* GetDevice(); VkPhysicalDevice* GetPhysicalDevice(); VkCommandPool* GetCommandPool(); VkQueue* GetGraphicsQueue(); VkCommandBuffer* GetCurrentCommandBuffer(); std::shared_ptr<Pipeline> GetPipeline(); void CreateGraphicsPipeline(Mesh* mesh, Material* material); std::pair<VkBuffer, VkDeviceMemory> RequestMvpBuffer();private:
bool isShutDown = false; static const uint32_t kMvpBufferCount = 3; std::vector<VkBuffer> mvpBuffers; std::vector<VkDeviceMemory> mvpBufferMemory; uint32_t currentMvpBufferIndex = 0; bool shutdownInProgress; uint32_t currentCmdBufferIndex = 0; std::vector<size_t> currentFramePerImage; std::vector<VkImage> swapChainImages; std::vector<VkImageView> swapChainImageViews; VkExtent2D swapChainExtent; VkRenderPass renderPass; uint32_t imageIndex; std::shared_ptr<Pipeline> pipeline; VkFormat swapChainImageFormat; std::vector<VkCommandBuffer> commandBuffers; void CreateImageViews(); void CleanupImageViews(); void CreateRenderPass(); void CleanupRenderPass(); void CreateSurface(); void DestroySurface(); void CreateInstance(); void CleanupInstance(); void ChoosePhysicalDevice(); void CreateDevice(); void CleanupDevice(); void CreateSwapchain(); void CleanupSwapchain(); void CreateCommandPool(); void CleanupCommandPool(); void CreateFramebuffers(); void CleanupFramebuffers(); void CreateCommandBuffers(); void CleanupCommandBuffers(); void Present(); GLFWwindow* window; VkInstance instance = VK_NULL_HANDLE; VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; VkDevice device = VK_NULL_HANDLE; VkSurfaceKHR surface; VkSwapchainKHR swapchain; VkCommandPool commandPool; VkCommandBuffer currentCommandBuffer; std::vector<VkFramebuffer> framebuffers; // Additional Vulkan objects needed for rendering… const uint32_t kMaxFramesInFlight = 2; std::vector<VkSemaphore> imageAvailableSemaphores; std::vector<VkSemaphore> renderFinishedSemaphores; std::vector<VkFence> inFlightFences; size_t currentFrame; VkQueue graphicsQueue; VkQueue presentQueue; void CreateSyncObjects(); void CleanupSyncObjects(); SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device, VkSurfaceKHR surface); VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& availableFormats); VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR>& availablePresentModes); VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities, GLFWwindow* window); std::vector<const char*> deviceExtensions = { VK_KHR_SWAPCHAIN_EXTENSION_NAME }; std::vector<const char*> CheckPhysicalDeviceExtensionSupport(VkPhysicalDevice physicalDevice); QueueFamilyIndices GetQueueFamilyIndices(VkPhysicalDevice physicalDevice);};
Scene.h:
#pragma once
#include #include "GameObject.h" #include "Camera.h" #include "Renderer.h"
class Scene { public: Scene(); ~Scene();
void Initialize(); void Update(float deltaTime); void Render(Renderer& renderer); void Shutdown(); void AddGameObject(GameObject* gameObject); Camera& GetCamera(); float temp;private: std::vector<GameObject*> gameObjects; Camera camera; };
Shader.h:
#pragma once
#include <vulkan/vulkan.h> #include
class Shader { public: Shader(); ~Shader();
void LoadFromFile(const std::string& filename, VkDevice device, VkShaderStageFlagBits stage); VkPipelineShaderStageCreateInfo GetPipelineShaderStageCreateInfo() const; static void Cleanup(Shader* shader);private: VkDevice device; VkShaderModule shaderModule; VkShaderStageFlagBits stage; };
Terrain.h:
#pragma once
#include "Simplex.h" #include "Mesh.h" #include "Material.h" #include "GameObject.h"
class Terrain { public: Terrain(int seed, int worldSize, float scale, VkDevice* device, VkPhysicalDevice* physicalDevice, VkCommandPool* commandPool, VkQueue* graphicsQueue); ~Terrain();
void GenerateTerrain(VkDescriptorSetLayout descriptorSetLayout, VkDescriptorSetLayout samplerDescriptorSetLayout, VkDescriptorPool descriptorPool); GameObject* GetTerrainObject();private: void GenerateHeightMap(); void ConvertHeightMapToMeshData();
int seed; int worldSize; float scale; VkDevice* device; VkPhysicalDevice* physicalDevice; VkCommandPool* commandPool; VkQueue* graphicsQueue; SimplexNoise noise; std::vector<std::vector<float>> heightMap; std::vector<Vertex> vertices; std::vector<uint32_t> indices; Mesh* terrainMesh; Material* terrainMaterial; GameObject* terrainObject;};
Texture.h:
#pragma once
#include <vulkan/vulkan.h>
#include "stb_image.h" // Include the stb_image header #include "BufferUtils.h" #include
class Texture { public: Texture(); ~Texture();
void LoadFromFile(const std::string& filename, VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue graphicsQueue); VkImageView GetImageView() const; VkSampler GetSampler() const; void Cleanup();private: VkDevice device; VkImage image; VkDeviceMemory imageMemory; VkImageView imageView; VkSampler sampler; VkPhysicalDevice physicalDevice; VkCommandPool commandPool; VkQueue graphicsQueue; bool initialized = false;
void CreateImage(uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCountFlagBits numSamples, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties); void TransitionImageLayout(VkImageLayout oldLayout, VkImageLayout newLayout, uint32_t mipLevels, VkSampleCountFlagBits numSamples); void CreateImageView(VkFormat format, VkImageAspectFlags aspectFlags, uint32_t mipLevels); void CreateSampler(uint32_t mipLevels); void CopyBufferToImage(VkBuffer buffer, uint32_t width, uint32_t height); // Additional helper functions for texture loading…};
Window.h:
#pragma once
#define GLFW_INCLUDE_VULKAN #include <GLFW/glfw3.h>
class Window { public: Window(int width = 800, int height = 600, const char* title = "Game Engine"); ~Window();
void Initialize(); void PollEvents(); void Shutdown(); bool ShouldClose() const; GLFWwindow* GetWindow() const; float GetDeltaTime();private: static void FramebufferResizeCallback(GLFWwindow* window, int width, int height); static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
int width; int height; const char* title; GLFWwindow* window; double lastFrameTime;};
Texture.cpp:
#include "Texture.h" #include "BufferUtils.h" #include
Texture::Texture() : device(VK_NULL_HANDLE), image(VK_NULL_HANDLE), imageMemory(VK_NULL_HANDLE), imageView(VK_NULL_HANDLE), sampler(VK_NULL_HANDLE) { }
Texture::~Texture() { Cleanup(); }
void Texture::LoadFromFile(const std::string& filename, VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue 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; // Create a buffer to store the image data VkDeviceSize imageSize = width * height * 4; VkBuffer stagingBuffer; VkDeviceMemory stagingBufferMemory; // Create the staging buffer for transferring image data BufferUtils::CreateBuffer(device, physicalDevice, imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory); // Copy image data to the buffer void* bufferData; vkMapMemory(device, stagingBufferMemory, 0, imageSize, 0, &bufferData); memcpy(bufferData, pixels, static_cast<size_t>(imageSize)); vkUnmapMemory(device, stagingBufferMemory); // Free the stb_image buffer stbi_image_free(pixels); // Create vkImage, copy buffer to image, and create imageView and sampler // … CreateImage(width, height, mipLevels, VK_SAMPLE_COUNT_1_BIT, VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); CreateImageView(VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_ASPECT_COLOR_BIT, mipLevels); CreateSampler(mipLevels); TransitionImageLayout(VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, mipLevels, VK_SAMPLE_COUNT_1_BIT); // Add this call CopyBufferToImage(stagingBuffer, width, height); // Cleanup the staging buffer and staging buffer memory // … // Destroy the staging buffer and free the staging buffer memory vkDestroyBuffer(device, stagingBuffer, nullptr); vkFreeMemory(device, stagingBufferMemory, nullptr);}
void Texture::Cleanup() { if (!initialized) { return; }
if (sampler != VK_NULL_HANDLE) { vkDestroySampler(device, sampler, nullptr); sampler = VK_NULL_HANDLE; } if (imageView != VK_NULL_HANDLE) { vkDestroyImageView(device, imageView, nullptr); imageView = VK_NULL_HANDLE; } if (image != VK_NULL_HANDLE) { vkDestroyImage(device, image, nullptr); image = VK_NULL_HANDLE; } if (imageMemory != VK_NULL_HANDLE) { vkFreeMemory(device, imageMemory, nullptr); imageMemory = VK_NULL_HANDLE; } initialized = false;}
VkImageView Texture::GetImageView() const { return imageView; }
VkSampler Texture::GetSampler() const { return sampler; }
void Texture::CreateImage(uint32_t width, uint32_t height, uint32_t mipLevels, VkSampleCountFlagBits numSamples, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties) { VkImageCreateInfo imageInfo{}; imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; imageInfo.imageType = VK_IMAGE_TYPE_2D; imageInfo.extent.width = width; imageInfo.extent.height = height; imageInfo.extent.depth = 1; imageInfo.mipLevels = mipLevels; imageInfo.arrayLayers = 1; imageInfo.format = format; imageInfo.tiling = tiling; imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; imageInfo.usage = usage; imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; imageInfo.samples = numSamples; imageInfo.flags = 0;
if (vkCreateImage(device, &imageInfo, nullptr, &image) != VK_SUCCESS) { throw std::runtime_error("Failed to create image!"); } VkMemoryRequirements memRequirements; vkGetImageMemoryRequirements(device, image, &memRequirements); VkMemoryAllocateInfo allocInfo{}; allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; allocInfo.allocationSize = memRequirements.size; allocInfo.memoryTypeIndex = BufferUtils::FindMemoryType(physicalDevice, memRequirements.memoryTypeBits, properties); if (vkAllocateMemory(device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS) { throw std::runtime_error("Failed to allocate image memory!"); } vkBindImageMemory(device, image, imageMemory, 0);}
void Texture::TransitionImageLayout(VkImageLayout oldLayout, VkImageLayout newLayout, uint32_t mipLevels, VkSampleCountFlagBits numSamples) { // Create a one-time-use command buffer VkCommandBufferAllocateInfo allocInfo{}; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocInfo.commandPool = commandPool; allocInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer; vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer); VkCommandBufferBeginInfo beginInfo{}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; vkBeginCommandBuffer(commandBuffer, &beginInfo); VkImageMemoryBarrier barrier{}; barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; barrier.oldLayout = oldLayout; barrier.newLayout = newLayout; barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; barrier.image = image; barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; barrier.subresourceRange.baseMipLevel = 0; barrier.subresourceRange.levelCount = mipLevels; barrier.subresourceRange.baseArrayLayer = 0; barrier.subresourceRange.layerCount = 1; VkPipelineStageFlags sourceStage; VkPipelineStageFlags destinationStage; if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) { barrier.srcAccessMask = 0; barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT; } else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) { barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT; destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; } else { throw std::invalid_argument("Unsupported layout transition!"); } vkCmdPipelineBarrier( commandBuffer, sourceStage, destinationStage, 0, 0, nullptr, 0, nullptr, 1, &barrier ); // End command buffer recording vkEndCommandBuffer(commandBuffer); // Submit command buffer VkSubmitInfo submitInfo{}; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &commandBuffer; vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); vkQueueWaitIdle(graphicsQueue); // Free command buffer vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);}
void Texture::CreateImageView(VkFormat format, VkImageAspectFlags aspectFlags, uint32_t mipLevels) { VkImageViewCreateInfo viewInfo{}; viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; viewInfo.image = image; viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; viewInfo.format = format; viewInfo.subresourceRange.aspectMask = aspectFlags; viewInfo.subresourceRange.baseMipLevel = 0; viewInfo.subresourceRange.levelCount = mipLevels; viewInfo.subresourceRange.baseArrayLayer = 0; viewInfo.subresourceRange.layerCount = 1;
if (vkCreateImageView(device, &viewInfo, nullptr, &imageView) != VK_SUCCESS) { throw std::runtime_error("Failed to create texture image view!"); }}
void Texture::CreateSampler(uint32_t mipLevels) { VkSamplerCreateInfo samplerInfo{}; samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; samplerInfo.magFilter = VK_FILTER_LINEAR; samplerInfo.minFilter = VK_FILTER_LINEAR; samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerInfo.anisotropyEnable = VK_TRUE; samplerInfo.maxAnisotropy = 16; samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK; samplerInfo.unnormalizedCoordinates = VK_FALSE; samplerInfo.compareEnable = VK_FALSE; samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS; samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; samplerInfo.mipLodBias = 0.0f; samplerInfo.minLod = 0.0f; samplerInfo.maxLod = static_cast(mipLevels);
if (vkCreateSampler(device, &samplerInfo, nullptr, &sampler) != VK_SUCCESS) { throw std::runtime_error("Failed to create texture sampler!"); }}
void Texture::CopyBufferToImage(VkBuffer buffer, uint32_t width, uint32_t height) { // Create a one-time-use command buffer VkCommandBufferAllocateInfo allocInfo{}; allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; allocInfo.commandPool = commandPool; allocInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer; vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer); VkCommandBufferBeginInfo beginInfo{}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; // Record buffer to image copy command in the command buffer vkBeginCommandBuffer(commandBuffer, &beginInfo); VkBufferImageCopy region{}; region.bufferOffset = 0; region.bufferRowLength = 0; region.bufferImageHeight = 0; region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; region.imageSubresource.mipLevel = 0; region.imageSubresource.baseArrayLayer = 0; region.imageSubresource.layerCount = 1; region.imageOffset = { 0, 0, 0 }; region.imageExtent = { width, height, 1 }; vkCmdCopyBufferToImage( commandBuffer, buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion ); // End command buffer recording vkEndCommandBuffer(commandBuffer); // Submit command buffer VkSubmitInfo submitInfo{}; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &commandBuffer; vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); vkQueueWaitIdle(graphicsQueue); // Free command buffer vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);}
Can you identify or suggest any improvements to the Texture class?
- Assistant
-
Reusing existing command buffers: In the current implementation, you're creating a new command buffer every time you load a texture. This practice is not advisable as it can cause resource usage issues. Instead, consider reusing existing command buffers or creating a single command buffer for multiple transfer operations.
-
Create optimal mipmaps: It's better to create mipmaps using vkCmdGenerateMipmaps instead of generating them with stb_image_modified. vkCmdGenerateMipmaps would perform more optimal blits using the GPU.
-
Error handling: Make sure to handle error codes returned by Vulkan functions properly.
-
Constructor initializer list: Utilize the constructor initializer list to initialize member variables.
-
Use smart pointers: Texture class uses raw pointers to Vulkan handles. Consider using smart pointers like std::unique_ptr or custom deleters to manage the Vulkan resource clean-up automatically.
-
Consistent naming convention: Be consistent with naming conventions, like camelCase for member variables and PascalCase for class names.
-
Code comments: Include comments in your code explaining the functionality of each function and how it operates. This will help you and others understand the code better in the future.
-
Separate Vulkan utility functions: Consider separating Vulkan utility functions (like CreateImage, CreateSampler, etc.) into a separate file with a dedicated namespace for reusability and readability.
-
Use consistent indentation and coding style: Maintain a consistent indentation style throughout the codebase. This makes the code easier to read and maintain.
-
Modularize and reuse code: There are several similar code blocks in functions such as TransitionImageLayout() and LoadFromFile() that can be extracted into smaller functions for better code maintenance and reusability.
-