Chronos 0.0
A advanced 2D rendering and animation system
Loading...
Searching...
No Matches
Chronos::Engine::Texture Class Reference

This class holds the Vulkan data and objects needed for a texture. More...

#include <texture.hpp>

Collaboration diagram for Chronos::Engine::Texture:

Public Member Functions

void create (Chronos::Engine::Device device, VkCommandPool commandPool, std::string texturePath, std::string textureName)
 Create the texture by loading the texture from the given path. Only supports jpg and png images.
 
void create (Chronos::Engine::Device device, VkCommandPool commandPool, void *data, size_t texWidth, size_t texHeight, VkDeviceSize imageSize, VkFormat format, std::string textureName)
 Create the texture by loading the texture from the given raw data.
 
void destroy ()
 Destroy the texture assets.
 

Public Attributes

VkDeviceMemory textureImageMemory
 The memory for the texture.
 
VkImage textureImage
 The image for the texture.
 
VkImageView textureImageView
 The image view for the texture.
 
std::string texturePath
 The path of the texture that it was loaded from.
 
std::string textureName
 The name of the texture.
 
int height
 
int width
 

Private Attributes

Chronos::Engine::Device device
 The device that has the texture.
 

Detailed Description

This class holds the Vulkan data and objects needed for a texture.

The items needed for creating a texture that are stored in this class are:

  • VkDeviceMemory for the texture memory
  • VkImage for the texture
  • VkImageView for the texture image view

Definition at line 134 of file texture.hpp.

Member Function Documentation

◆ create() [1/2]

void Chronos::Engine::Texture::create ( Chronos::Engine::Device  device,
VkCommandPool  commandPool,
std::string  texturePath,
std::string  textureName 
)

Create the texture by loading the texture from the given path. Only supports jpg and png images.

Parameters
deviceThe device that has the texture.
commandPoolThe command pool to create the temporary command buffer from.
texturePathThe path to the texture(only jpg or png).
textureNameThe name of the texture(must be unique).

Definition at line 83 of file texture.cpp.

85{
88 int texWidth = 0, texHeight = 0, texChannels = 0;
89 this->device = device;
90 VkDeviceSize imageSize;
91 uint8_t* pixels;
92
93 if (texturePath.ends_with(".png") || texturePath.ends_with(".jpg")) {
94 pixels = stbi_load(texturePath.c_str(), &texWidth, &texHeight,
95 &texChannels, STBI_rgb_alpha);
96 imageSize = texWidth * texHeight * 4;
97 this->width = texWidth;
98 this->height = texHeight;
99 if (!pixels) {
100 throw std::runtime_error(
101 "failed to load texture image " + texturePath);
102 }
103 } else {
104 throw std::runtime_error("unsupported texture format " + texturePath);
105 }
106
107 VkBuffer stagingBuffer;
108 VkDeviceMemory stagingBufferMemory;
110 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
111 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
112 | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
113 &stagingBuffer, &stagingBufferMemory);
114
115 void* data;
116 vkMapMemory(device.device, stagingBufferMemory, 0, imageSize, 0, &data);
117 memcpy(data, pixels, static_cast<size_t>(imageSize));
118 vkUnmapMemory(device.device, stagingBufferMemory);
119
120 stbi_image_free(pixels);
121
122 Chronos::Engine::createImage(device, texWidth, texHeight,
123 VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_TILING_OPTIMAL,
124 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
125 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &textureImage, &textureImageMemory,
126 VK_SAMPLE_COUNT_1_BIT);
127
129 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
130 commandPool, device);
132 static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight),
133 commandPool, device);
135 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
136 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, commandPool, device);
137 vkDestroyBuffer(device.device, stagingBuffer, nullptr);
138 vkFreeMemory(device.device, stagingBufferMemory, nullptr);
140 = createImageView(device, VK_FORMAT_R8G8B8A8_SRGB, textureImage);
141#ifdef ENABLE_EDITOR
143 descriptorSet = ImGui_ImplVulkan_AddTexture(textureSampler,
144 textureImageView, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
145
146#endif
147}
VkDevice device
This is the logical device that is used by Vulkan.
Definition device.hpp:52
std::string textureName
The name of the texture.
Definition texture.hpp:197
VkDeviceMemory textureImageMemory
The memory for the texture.
Definition texture.hpp:139
Chronos::Engine::Device device
The device that has the texture.
Definition texture.hpp:210
VkImageView textureImageView
The image view for the texture.
Definition texture.hpp:149
VkImage textureImage
The image for the texture.
Definition texture.hpp:144
std::string texturePath
The path of the texture that it was loaded from.
Definition texture.hpp:192
void createTextureSampler(Chronos::Engine::Device device, VkSampler *textureSampler)
Creates a VkSampler
Definition texture.cpp:278
VkImageView createImageView(Chronos::Engine::Device device, VkFormat format, VkImage image)
Create a VkImageView for a given VkImage.
Definition texture.cpp:255
void createImage(Chronos::Engine::Device device, uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage *image, VkDeviceMemory *imageMemory, VkSampleCountFlagBits numSamples)
For a given image dimensons, it creates a VkImage and.
Definition texture.cpp:42
void copyBufferToImage(VkBuffer buffer, VkImage image, uint32_t width, uint32_t height, VkCommandPool commandPool, Chronos::Engine::Device device)
Copy data from a buffer to an image.
Definition texture.cpp:234
void transitionImageLayout(VkImage image, VkImageLayout oldLayout, VkImageLayout newLayout, VkCommandPool commandPool, Chronos::Engine::Device device)
Transiton a VkImage from one layout to another.
Definition texture.cpp:190
void createBuffer(Chronos::Engine::Device device, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer *buffer, VkDeviceMemory *bufferMemory)
Creates a buffer of a given size, usage and properties.
Definition helper.cpp:78

References Chronos::Engine::copyBufferToImage(), Chronos::Engine::createBuffer(), Chronos::Engine::createImage(), Chronos::Engine::createImageView(), Chronos::Engine::createTextureSampler(), Chronos::Engine::Device::device, device, height, textureImage, textureImageMemory, textureImageView, textureName, texturePath, Chronos::Engine::transitionImageLayout(), and width.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ create() [2/2]

void Chronos::Engine::Texture::create ( Chronos::Engine::Device  device,
VkCommandPool  commandPool,
void *  data,
size_t  texWidth,
size_t  texHeight,
VkDeviceSize  imageSize,
VkFormat  format,
std::string  textureName 
)

Create the texture by loading the texture from the given raw data.

Used in Text Rendering

Parameters
deviceThe device that has the texture.
commandPoolThe command pool to create the temporary command buffer from.
dataThe raw data of the texture.
texWidthThe width of the texture.
texHeightThe height of the texture.
imageSizeThe size of the image.
formatThe format of the image.
textureNameThe name of the texture(must be unique).

Definition at line 149 of file texture.cpp.

152{
153 this->textureName = textureName;
154 this->texturePath = "NA";
155 this->device = device;
156 VkBuffer stagingBuffer;
157 VkDeviceMemory stagingBufferMemory;
159 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
160 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
161 | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
162 &stagingBuffer, &stagingBufferMemory);
163
164 void* mappedData;
165 vkMapMemory(
166 device.device, stagingBufferMemory, 0, imageSize, 0, &mappedData);
167 memcpy(mappedData, data, static_cast<size_t>(imageSize));
168 vkUnmapMemory(device.device, stagingBufferMemory);
169
170 Chronos::Engine::createImage(device, texWidth, texHeight, format,
171 VK_IMAGE_TILING_OPTIMAL,
172 VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
173 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &textureImage, &textureImageMemory,
174 VK_SAMPLE_COUNT_1_BIT);
175
177 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
178 commandPool, device);
180 static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight),
181 commandPool, device);
183 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
184 VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, commandPool, device);
185 vkDestroyBuffer(device.device, stagingBuffer, nullptr);
186 vkFreeMemory(device.device, stagingBufferMemory, nullptr);
188}

References Chronos::Engine::copyBufferToImage(), Chronos::Engine::createBuffer(), Chronos::Engine::createImage(), Chronos::Engine::createImageView(), Chronos::Engine::Device::device, and Chronos::Engine::transitionImageLayout().

Here is the call graph for this function:

◆ destroy()

void Chronos::Engine::Texture::destroy ( )

Destroy the texture assets.

Definition at line 306 of file texture.cpp.

307{
308 vkDestroyImageView(device.device, textureImageView, nullptr);
309 vkDestroyImage(device.device, textureImage, nullptr);
310 vkFreeMemory(device.device, textureImageMemory, nullptr);
311}

Member Data Documentation

◆ device

Chronos::Engine::Device Chronos::Engine::Texture::device
private

The device that has the texture.

Definition at line 210 of file texture.hpp.

◆ height

int Chronos::Engine::Texture::height

Definition at line 199 of file texture.hpp.

◆ textureImage

VkImage Chronos::Engine::Texture::textureImage

The image for the texture.

Definition at line 144 of file texture.hpp.

◆ textureImageMemory

VkDeviceMemory Chronos::Engine::Texture::textureImageMemory

The memory for the texture.

Definition at line 139 of file texture.hpp.

◆ textureImageView

VkImageView Chronos::Engine::Texture::textureImageView

The image view for the texture.

Definition at line 149 of file texture.hpp.

◆ textureName

std::string Chronos::Engine::Texture::textureName

The name of the texture.

Definition at line 197 of file texture.hpp.

◆ texturePath

std::string Chronos::Engine::Texture::texturePath

The path of the texture that it was loaded from.

Definition at line 192 of file texture.hpp.

◆ width

int Chronos::Engine::Texture::width

Definition at line 200 of file texture.hpp.


The documentation for this class was generated from the following files: