| 1034 | } |
| 1035 | |
| 1036 | Texture ApiVulkanSample::load_texture(const std::string &file, vkb::sg::Image::ContentType content_type) |
| 1037 | { |
| 1038 | Texture texture{}; |
| 1039 | |
| 1040 | texture.image = vkb::sg::Image::load(file, file, content_type); |
| 1041 | texture.image->create_vk_image(get_device()); |
| 1042 | |
| 1043 | const auto &queue = get_device().get_queue_by_flags(VK_QUEUE_GRAPHICS_BIT, 0); |
| 1044 | |
| 1045 | VkCommandBuffer command_buffer = get_device().create_command_buffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); |
| 1046 | |
| 1047 | vkb::core::BufferC stage_buffer = vkb::core::BufferC::create_staging_buffer(get_device(), texture.image->get_data()); |
| 1048 | |
| 1049 | // Setup buffer copy regions for each mip level |
| 1050 | std::vector<VkBufferImageCopy> bufferCopyRegions; |
| 1051 | |
| 1052 | auto &mipmaps = texture.image->get_mipmaps(); |
| 1053 | |
| 1054 | for (size_t i = 0; i < mipmaps.size(); i++) |
| 1055 | { |
| 1056 | VkBufferImageCopy buffer_copy_region = {}; |
| 1057 | buffer_copy_region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1058 | buffer_copy_region.imageSubresource.mipLevel = vkb::to_u32(i); |
| 1059 | buffer_copy_region.imageSubresource.baseArrayLayer = 0; |
| 1060 | buffer_copy_region.imageSubresource.layerCount = 1; |
| 1061 | buffer_copy_region.imageExtent.width = texture.image->get_extent().width >> i; |
| 1062 | buffer_copy_region.imageExtent.height = texture.image->get_extent().height >> i; |
| 1063 | buffer_copy_region.imageExtent.depth = 1; |
| 1064 | buffer_copy_region.bufferOffset = mipmaps[i].offset; |
| 1065 | |
| 1066 | bufferCopyRegions.push_back(buffer_copy_region); |
| 1067 | } |
| 1068 | |
| 1069 | VkImageSubresourceRange subresource_range = {}; |
| 1070 | subresource_range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1071 | subresource_range.baseMipLevel = 0; |
| 1072 | subresource_range.levelCount = vkb::to_u32(mipmaps.size()); |
| 1073 | subresource_range.layerCount = 1; |
| 1074 | |
| 1075 | // Image barrier for optimal image (target) |
| 1076 | // Optimal image will be used as destination for the copy |
| 1077 | vkb::image_layout_transition(command_buffer, |
| 1078 | texture.image->get_vk_image().get_handle(), |
| 1079 | VK_IMAGE_LAYOUT_UNDEFINED, |
| 1080 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1081 | subresource_range); |
| 1082 | |
| 1083 | // Copy mip levels from staging buffer |
| 1084 | vkCmdCopyBufferToImage( |
| 1085 | command_buffer, |
| 1086 | stage_buffer.get_handle(), |
| 1087 | texture.image->get_vk_image().get_handle(), |
| 1088 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1089 | static_cast<uint32_t>(bufferCopyRegions.size()), |
| 1090 | bufferCopyRegions.data()); |
| 1091 | |
| 1092 | // Change texture image layout to shader read after all mip levels have been copied |
| 1093 | vkb::image_layout_transition(command_buffer, |
nothing calls this directly
no test coverage detected