improved docs

This commit is contained in:
Jorijn van der Graaf 2025-06-14 14:58:02 +02:00
commit dfe9b1abe9
16 changed files with 315 additions and 40 deletions

View file

@ -31,15 +31,37 @@ import :VulkanBuffer;
namespace Crafter {
/**
* @brief Represents a Vulkan texture with pixel data of a specified type.
*
* This class manages a Vulkan image resource along with its associated memory,
* buffer, and image view. It provides functionality to create textures either
* by specifying dimensions or loading from an asset, and to update the texture
* data on the GPU using command buffers.
*
* @tparam PixelType The type of the pixel data stored in the texture.
*/
export template <typename PixelType>
class VulkanTexture {
public:
uint32_t width;
uint32_t height;
private:
VkImage image;
VkDeviceMemory imageMemory;
Buffer<PixelType> buffer;
VkImageView imageView;
public:
/**
* @brief Constructs a VulkanTexture with the given dimensions.
*
* Creates a Vulkan texture image with the specified width and height,
* initializing necessary resources.
*
* @param width The width of the texture.
* @param height The height of the texture.
* @param cmd Vulkan command buffer used for resource initialization.
*/
VulkanTexture(std::uint32_t width, std::uint32_t height, VkCommandBuffer cmd) : width(width), height(height), buffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, width*height) {
VkImageCreateInfo imageInfo{};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
@ -84,11 +106,27 @@ namespace Crafter {
TransitionImageLayout(cmd, buffer, image, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
}
/**
* @brief Constructs a VulkanTexture by loading from an in memory asset.
*
* Loads texture pixel data from the specified asset and initializes
* the Vulkan image resource accordingly.
*
* @param asset Pointer to the in memory asset.
* @param cmd Vulkan command buffer used for resource initialization.
*/
VulkanTexture(const char* asset, VkCommandBuffer cmd) : VulkanTexture(reinterpret_cast<const std::uint32_t*>(asset)[0], reinterpret_cast<const std::uint32_t*>(asset)[1], cmd) {
Update(reinterpret_cast<const PixelType*>(reinterpret_cast<const std::uint32_t*>(asset)+2), cmd);
}
/**
* @brief Updates the texture with new pixel data.
*
* Copies the given pixel data into the texture's buffer and issues Vulkan
* commands to upload the data to the GPU image.
*
* @param bufferdata Pointer to the new pixel data.
* @param cmd Vulkan command buffer used for the update operation.
*/
void Update(const PixelType* bufferdata, VkCommandBuffer cmd) {
memcpy(buffer.value, bufferdata, height*width*sizeof(PixelType));
TransitionImageLayout(cmd, buffer, image, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
@ -173,4 +211,4 @@ namespace Crafter {
);
}
};
}
}