Add textures

Textures supported
This commit is contained in:
Piotr Krygier committed 2023-10-03 09:00:02 +02:00
1 parent 4fcda01ba2
commit 548ed57969
11 files changed
+183 -76

No files matched your search

+31 -9
View File
@@ -90,7 +90,7 @@ static VkDeviceSize format_to_pixel_size(VkFormat format)
}
static uint8_t create_vulkan_image(uint32_t width, uint32_t height, VkFormat format, unsigned char* pixels)
static int8_t create_vulkan_image(uint32_t width, uint32_t height, VkFormat format, unsigned char* pixels)
{
struct rse_vulkan_buffer_t staging_buffer = {0};
VkDeviceSize staging_buffer_size = 0U;
@@ -116,7 +116,7 @@ static uint8_t create_vulkan_image(uint32_t width, uint32_t height, VkFormat for
/* Check if we found free image handle */
if (free_texture_image == NULL) {
LOGE("Failed to find free texture image handle");
return 1;
return -1;
}
staging_buffer_size = width * height * format_to_pixel_size(format);
@@ -157,7 +157,7 @@ static uint8_t create_vulkan_image(uint32_t width, uint32_t height, VkFormat for
&free_texture_image->allocation,
&free_texture_image->allocation_info)) {
LOGF("Failed to create image");
return 1;
return -1;
}
/* Put image into correct layout to copy pixels from buffer to image memory */
@@ -199,7 +199,7 @@ static uint8_t create_vulkan_image(uint32_t width, uint32_t height, VkFormat for
| VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
&staging_buffer)) {
LOGF(vulkan_error_messages[VULKAN_BUFFER_CREATION_FAILED]);
return VULKAN_ERROR_BUFFER_CREATION_FAILED;
return -1;
}
memcpy(staging_buffer.allocation_info.pMappedData, pixels, staging_buffer_size);
@@ -264,11 +264,11 @@ static uint8_t create_vulkan_image(uint32_t width, uint32_t height, VkFormat for
NULL,
&free_texture_image->image_view)) {
LOGF("Failed to create image view");
return 1;
return -1;
}
free_texture_image->id_taken = IMAGE_TAKEN;
return VULKAN_ERROR_NO_ERROR;
return i;
}
uint8_t init_vulkan_images()
@@ -282,29 +282,33 @@ uint8_t init_vulkan_images()
return status;
}
void rse_load_texture(const char* file_path)
int8_t rse_load_texture(const char* file_path)
{
int width = 0;
int height = 0;
int channels = 0;
stbi_uc* pixel_buffer = NULL;
int8_t ret;
pixel_buffer = stbi_load(file_path, &width, &height, &channels, STBI_rgb_alpha);
if (pixel_buffer == NULL) {
LOGE("Failed to load texture from file: %s", file_path);
return;
return -1;
}
/* Create actual Vulkan Image */
create_vulkan_image(width,
ret = create_vulkan_image(width,
height,
VK_FORMAT_R8G8B8A8_SRGB,
pixel_buffer);
stbi_image_free(pixel_buffer);
return ret;
}
void rse_destroy_textures()
@@ -321,6 +325,10 @@ void rse_destroy_textures()
}
}
int8_t image_exists(uint8_t image_id) {
return g_texture_images[image_id].id_taken == IMAGE_TAKEN;
}
VkImageView rse_get_image_view(size_t image_id)
{
return g_texture_images[image_id].image_view;
@@ -329,4 +337,18 @@ VkImageView rse_get_image_view(size_t image_id)
VkSampler rse_get_texture_sampler()
{
return g_sampler;
}
size_t get_textures_count()
{
size_t count = 0U;
size_t i = 0U;
for (i = 0U; i < RSE_MAX_IMAGE_COUNT; ++i) {
if(g_texture_images[i].id_taken == IMAGE_TAKEN) {
count++;
}
}
return count;
}