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.
I am in the process of converting the codebase from using vulkan.h to vulkan.hpp, as well as utilising RAII. I have updated the BufferUtils class and Texture class header to incorporate these elements. However I still need to update the Texture class source based on these updates. Here is some of the code:
BufferUtils.h:
#pragma once
#include <vulkan/vulkan.hpp>
namespace BufferUtils { void CreateBuffer( vk::Device device, vk::PhysicalDevice physicalDevice, vk::DeviceSize size, vk::BufferUsageFlags usage, vk::MemoryPropertyFlags properties, vk::Buffer& buffer, vk::DeviceMemory& bufferMemory);
uint32_t FindMemoryType(vk::PhysicalDevice physicalDevice, uint32_t typeFilter, vk::MemoryPropertyFlags properties); void CopyBuffer( vk::Device device, vk::CommandPool commandPool, vk::Queue graphicsQueue, vk::Buffer srcBuffer, vk::Buffer dstBuffer, vk::DeviceSize size);}
Old 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 CopyBufferToImage(VkBuffer buffer, uint32_t width, uint32_t height); void CreateImageView(VkFormat format, VkImageAspectFlags aspectFlags, uint32_t mipLevels); void CreateSampler(uint32_t mipLevels); void GenerateMipmaps(uint32_t width, uint32_t height, uint32_t mipLevels); VkCommandBuffer BeginSingleTimeCommands(VkCommandPool commandPool, VkDevice device); void EndSingleTimeCommands(VkCommandPool commandPool, VkQueue graphicsQueue, VkDevice device, VkCommandBuffer commandBuffer);};
New Texture.h:
- Assistant
- User
- Assistant
- User
I am getting some errors when trying to assign region.imageOffset and region.imageExtent. It says more than one operator "=" matches these operands.
- Assistant
Apologies for that issue, and thank you for pointing it out. The issue is caused by the implicit conversion of data types. You can fix the problem by explicitly constructing the
vk::Offset3Dandvk::Extent3Dobjects.Update the
CopyBufferToImagefunction like this:void Texture::CopyBufferToImage(vk::Buffer buffer, uint32_t width, uint32_t height) { // ... vk::BufferImageCopy region{}; region.bufferOffset = 0; region.bufferRowLength = 0; region.bufferImageHeight = 0; region.imageSubresource.aspectMask = vk::ImageAspectFlagBits::eColor; region.imageSubresource.mipLevel = 0; region.imageSubresource.baseArrayLayer = 0; region.imageSubresource.layerCount = 1; region.imageOffset = vk::Offset3D{ 0, 0, 0 }; region.imageExtent = vk::Extent3D{ width, height, 1 }; commandBuffer.copyBufferToImage( buffer, image, vk::ImageLayout::eTransferDstOptimal, 1, ®ion ); // ... }By using
vk::Offset3D{ 0, 0, 0 }andvk::Extent3D{ width, height, 1 }, we make sure to construct and assign their respective objects explicitly, thus resolving any type conversion issues.