| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <vk_textures.h> | ||
| 2 | #include <iostream> | ||
| 3 | |||
| 4 | #include <vk_initializers.h> | ||
| 5 | |||
| 6 | #define STB_IMAGE_IMPLEMENTATION | ||
| 7 | #include <stb_image.h> | ||
| 8 | |||
| 9 | |||
| 10 | |||
| 11 | bool vkutil::load_image_from_file(VulkanEngine& engine, const char* file, Texture & texture) | ||
| 12 | { | ||
| 13 | int texWidth, texHeight, texChannels; | ||
| 14 | |||
| 15 | ✗ | stbi_uc* pixels = stbi_load(file, &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); | |
| 16 | |||
| 17 | ✗ | if (!pixels) { | |
| 18 | ✗ | std::cout << "Failed to load texture file " << file << std::endl; | |
| 19 | ✗ | return false; | |
| 20 | } | ||
| 21 | |||
| 22 | ✗ | texture.height = texHeight; | |
| 23 | ✗ | texture.width = texWidth; | |
| 24 | |||
| 25 | ✗ | void* pixel_ptr = pixels; | |
| 26 | ✗ | VkDeviceSize imageSize = texWidth * texHeight * 4; | |
| 27 | |||
| 28 | ✗ | VkFormat image_format = VK_FORMAT_R8G8B8A8_SRGB; | |
| 29 | |||
| 30 | ✗ | AllocatedBuffer stagingBuffer = engine.create_buffer(imageSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY); | |
| 31 | |||
| 32 | void* data; | ||
| 33 | ✗ | vmaMapMemory(engine._allocator, stagingBuffer._allocation, &data); | |
| 34 | |||
| 35 | ✗ | memcpy(data, pixel_ptr, static_cast<size_t>(imageSize)); | |
| 36 | |||
| 37 | ✗ | vmaUnmapMemory(engine._allocator, stagingBuffer._allocation); | |
| 38 | |||
| 39 | ✗ | stbi_image_free(pixels); | |
| 40 | |||
| 41 | VkExtent3D imageExtent; | ||
| 42 | ✗ | imageExtent.width = static_cast<uint32_t>(texWidth); | |
| 43 | ✗ | imageExtent.height = static_cast<uint32_t>(texHeight); | |
| 44 | ✗ | imageExtent.depth = 1; | |
| 45 | |||
| 46 | ✗ | VkImageCreateInfo dimg_info = vkinit::image_create_info( | |
| 47 | image_format, | ||
| 48 | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, | ||
| 49 | imageExtent); | ||
| 50 | |||
| 51 | AllocatedImage newImage; | ||
| 52 | |||
| 53 | ✗ | VmaAllocationCreateInfo dimg_allocinfo = {}; | |
| 54 | ✗ | dimg_allocinfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; | |
| 55 | |||
| 56 | //allocate and create the image | ||
| 57 | ✗ | vmaCreateImage(engine._allocator, &dimg_info, &dimg_allocinfo, &newImage._image, &newImage._allocation, nullptr); | |
| 58 | |||
| 59 | //transition image to transfer-receiver | ||
| 60 | engine.immediate_submit([&](VkCommandBuffer cmd) { | ||
| 61 | VkImageSubresourceRange range; | ||
| 62 | ✗ | range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; | |
| 63 | ✗ | range.baseMipLevel = 0; | |
| 64 | ✗ | range.levelCount = 1; | |
| 65 | ✗ | range.baseArrayLayer = 0; | |
| 66 | ✗ | range.layerCount = 1; | |
| 67 | |||
| 68 | ✗ | VkImageMemoryBarrier imageBarrier_toTransfer = {}; | |
| 69 | ✗ | imageBarrier_toTransfer.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; | |
| 70 | |||
| 71 | ✗ | imageBarrier_toTransfer.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; | |
| 72 | ✗ | imageBarrier_toTransfer.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; | |
| 73 | ✗ | imageBarrier_toTransfer.image = newImage._image; | |
| 74 | ✗ | imageBarrier_toTransfer.subresourceRange = range; | |
| 75 | |||
| 76 | ✗ | imageBarrier_toTransfer.srcAccessMask = 0; | |
| 77 | ✗ | imageBarrier_toTransfer.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | |
| 78 | |||
| 79 | //barrier the image into the transfer-receive layout | ||
| 80 | ✗ | vkCmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &imageBarrier_toTransfer); | |
| 81 | |||
| 82 | ✗ | VkBufferImageCopy copyRegion = {}; | |
| 83 | ✗ | copyRegion.bufferOffset = 0; | |
| 84 | ✗ | copyRegion.bufferRowLength = 0; | |
| 85 | ✗ | copyRegion.bufferImageHeight = 0; | |
| 86 | |||
| 87 | ✗ | copyRegion.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; | |
| 88 | ✗ | copyRegion.imageSubresource.mipLevel = 0; | |
| 89 | ✗ | copyRegion.imageSubresource.baseArrayLayer = 0; | |
| 90 | ✗ | copyRegion.imageSubresource.layerCount = 1; | |
| 91 | ✗ | copyRegion.imageExtent = imageExtent; | |
| 92 | |||
| 93 | //copy the buffer into the image | ||
| 94 | ✗ | vkCmdCopyBufferToImage(cmd, stagingBuffer._buffer, newImage._image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©Region); | |
| 95 | |||
| 96 | ✗ | VkImageMemoryBarrier imageBarrier_toReadable = imageBarrier_toTransfer; | |
| 97 | |||
| 98 | ✗ | imageBarrier_toReadable.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; | |
| 99 | ✗ | imageBarrier_toReadable.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; | |
| 100 | |||
| 101 | ✗ | imageBarrier_toReadable.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; | |
| 102 | ✗ | imageBarrier_toReadable.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; | |
| 103 | |||
| 104 | //barrier the image into the shader readable layout | ||
| 105 | ✗ | vkCmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &imageBarrier_toReadable); | |
| 106 | ✗ | }); | |
| 107 | |||
| 108 | |||
| 109 | engine._mainDeletionQueue.push_function([=]() { | ||
| 110 | |||
| 111 | ✗ | vmaDestroyImage(engine._allocator, newImage._image, newImage._allocation); | |
| 112 | ✗ | }); | |
| 113 | |||
| 114 | ✗ | vmaDestroyBuffer(engine._allocator, stagingBuffer._buffer, stagingBuffer._allocation); | |
| 115 | |||
| 116 | ✗ | std::cout << "Texture loaded succesfully " << file << std::endl; | |
| 117 | |||
| 118 | ✗ | texture.image = newImage; | |
| 119 | ✗ | return true; | |
| 120 | ✗ | } | |
| 121 |