3 #include "texture2d_backend.h"
11 VulkanTexture2D::VulkanTexture2D():
12 Texture(VK_IMAGE_VIEW_TYPE_2D)
15 void VulkanTexture2D::fill_image_info(void *ii) const
17 const Texture2D &self = *static_cast<const Texture2D *>(this);
19 VkImageCreateInfo *image_info = static_cast<VkImageCreateInfo *>(ii);
20 image_info->imageType = VK_IMAGE_TYPE_2D;
21 image_info->extent.width = self.width;
22 image_info->extent.height = self.height;
23 image_info->mipLevels = self.levels;
26 void VulkanTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
28 const Texture2D &self = *static_cast<const Texture2D *>(this);
30 auto level_size = self.get_level_size(level);
31 bool discard = (x==0 && y==0 && wd==level_size.x && ht==level_size.y);
33 size_t data_size = wd*ht*get_pixel_size(storage_fmt);
34 void *staging = device.get_transfer_queue().prepare_transfer(this, false, data_size,
35 [this, level, discard](){
36 unsigned n_levels = static_cast<const Texture2D *>(this)->levels;
37 change_layout(n_levels, level, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, discard);
39 [this, level, x, y, wd, ht](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){
40 const VulkanFunctions &vk = device.get_functions();
42 VkBufferImageCopy region = { };
43 region.bufferOffset = src_off;
44 region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
45 region.imageSubresource.mipLevel = level;
46 region.imageSubresource.baseArrayLayer = 0;
47 region.imageSubresource.layerCount = 1;
48 region.imageOffset = { x, y, 0 };
49 region.imageExtent = { wd, ht, 1 };
50 vk.CmdCopyBufferToImage(cmd_buf, staging_buf, handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
53 const char *src = static_cast<const char *>(data);
54 copy(src, src+data_size, static_cast<char *>(staging));
57 void VulkanTexture2D::generate_mipmap()
59 generate_mipmap_levels(static_cast<const Texture2D *>(this)->levels);
62 void VulkanTexture2D::fill_mipmap_blit(unsigned level, void *b)
64 const Texture2D &self = *static_cast<const Texture2D *>(this);
65 VkImageBlit &blit = *static_cast<VkImageBlit *>(b);
67 auto src_size = self.get_level_size(level);
68 auto dst_size = self.get_level_size(level+1);
70 blit.srcOffsets[1] = { static_cast<int>(src_size.x), static_cast<int>(src_size.y), 1 };
71 blit.dstOffsets[1] = { static_cast<int>(dst_size.x), static_cast<int>(dst_size.y), 1 };
74 Resource::AsyncLoader *VulkanTexture2D::load(IO::Seekable &, const Resources *)
76 throw logic_error("Texture2D::load is unimplemented");
79 uint64_t VulkanTexture2D::get_data_size() const
84 void VulkanTexture2D::unload()