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;
25 void VulkanTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
27 Texture2D::AsyncTransfer transfer(*static_cast<Texture2D *>(this), level, x, y, wd, ht);
28 stage_pixels(transfer.get_address(), data, wd*ht);
31 void VulkanTexture2D::fill_mipmap_blit(unsigned level, void *b)
33 const Texture2D &self = *static_cast<const Texture2D *>(this);
34 VkImageBlit &blit = *static_cast<VkImageBlit *>(b);
36 auto src_size = self.get_level_size(level);
37 auto dst_size = self.get_level_size(level+1);
39 blit.srcOffsets[1] = { static_cast<int>(src_size.x), static_cast<int>(src_size.y), 1 };
40 blit.dstOffsets[1] = { static_cast<int>(dst_size.x), static_cast<int>(dst_size.y), 1 };
43 uint64_t VulkanTexture2D::get_data_size() const
48 void VulkanTexture2D::unload()
53 void *VulkanTexture2D::AsyncTransfer::allocate()
55 const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
57 Texture2D &tex = *self.texture;
58 unsigned level = self.level;
61 unsigned wd = self.width;
62 unsigned ht = self.height;
64 auto level_size = tex.get_level_size(level);
65 bool discard = (x==0 && y==0 && wd==level_size.x && ht==level_size.y);
67 TransferQueue &tq = tex.device.get_transfer_queue();
68 return tq.prepare_transfer(&tex, false, self.data_size,
69 [&tex, level, discard](){
70 tex.change_layout(level, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, discard);
72 [&tex, level, x, y, wd, ht](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){
73 const VulkanFunctions &vk = tex.device.get_functions();
75 VkBufferImageCopy region = { };
76 region.bufferOffset = src_off;
77 region.imageSubresource.aspectMask = get_vulkan_aspect(get_components(tex.storage_fmt));
78 region.imageSubresource.mipLevel = level;
79 region.imageSubresource.baseArrayLayer = 0;
80 region.imageSubresource.layerCount = 1;
81 region.imageOffset = { x, y, 0 };
82 region.imageExtent = { wd, ht, 1 };
83 vk.CmdCopyBufferToImage(cmd_buf, staging_buf, tex.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
87 void VulkanTexture2D::AsyncTransfer::finalize()
89 const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
90 self.texture->device.get_transfer_queue().finalize_transfer(self.dest_addr);