51{
53 {
54 throw std::runtime_error("ImageData is not initialized!");
55 }
56
59
60 if (width > static_cast<unsigned int>(std::numeric_limits<int32_t>::max()) ||
61 height > static_cast<unsigned int>(std::numeric_limits<int32_t>::max()))
62 {
63 throw std::out_of_range("Texture dimensions exceed supported mipmap range.");
64 }
65
66 mMipmapLevels =
static_cast<uint32_t
>(std::floor(std::log2(std::max(width, height)))) + 1;
68
69 const VkDeviceSize imageSize =
70 static_cast<VkDeviceSize>(width) *
71 static_cast<VkDeviceSize>(height) *
72 4;
73
74 if (pixelVector.size() == 0)
75 {
76 throw std::runtime_error("failed to load texture image!");
77 }
78
79 VkBuffer stagingBuffer;
80 VkDeviceMemory stagingBufferMemory;
81
82 mRenderer->
CreateBuffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, stagingBuffer, stagingBufferMemory);
83
84 unsigned char* pixels = new unsigned char[static_cast<size_t>(imageSize)];
85 std::copy(pixelVector.begin(), pixelVector.end(), pixels);
86 void* data;
88 memcpy(data, pixels, static_cast<size_t>(imageSize));
90 delete[] pixels;
91
92 CreateVulkanImage(
static_cast<uint32_t
>(width),
static_cast<uint32_t
>(height),
mMipmapLevels, 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,
94
97
100
103 VK_FORMAT_R8G8B8A8_SRGB,
104 static_cast<int32_t>(width),
105 static_cast<int32_t>(height),
107}
unsigned int GetWidth() const
Returns the image width.
std::vector< uint8_t > GetImageDataRGBA() const
Gets raw image data in RGBA format.
unsigned int GetHeight() const
Returns the image height.
void GenerateMipmaps(VkImage image, VkFormat imageFormat, int32_t texWidth, int32_t texHeight, uint32_t mipLevels)
Generates mipmaps for a given image resource.
void CopyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height)
Copies buffer data into an image (used for uploading texture data).
void CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer &buffer, VkDeviceMemory &bufferMemory)
Creates a new Vulkan buffer with the specified usage and memory properties.
void TransitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout, std::uint32_t mipmapLevels)
Transitions the image layout for a given Vulkan image.
VkDeviceMemory mTextureImageMemory
void CreateVulkanImage(uint32_t width, uint32_t height, std::uint32_t mipmapLevels, VkSampleCountFlagBits numSamples, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage &image, VkDeviceMemory &imageMemory)
std::uint32_t mMipmapLevels