]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/texture2d_backend.cpp
Refactor TransferQueue to require explicit finalization of transfers
[libs/gl.git] / source / backends / vulkan / texture2d_backend.cpp
1 #include "device.h"
2 #include "texture2d.h"
3 #include "texture2d_backend.h"
4 #include "vulkan.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 VulkanTexture2D::VulkanTexture2D():
12         Texture(VK_IMAGE_VIEW_TYPE_2D)
13 { }
14
15 void VulkanTexture2D::fill_image_info(void *ii) const
16 {
17         const Texture2D &self = *static_cast<const Texture2D *>(this);
18
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 }
24
25 void VulkanTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
26 {
27         const Texture2D &self = *static_cast<const Texture2D *>(this);
28
29         auto level_size = self.get_level_size(level);
30         bool discard = (x==0 && y==0 && wd==level_size.x && ht==level_size.y);
31
32         TransferQueue &tq = device.get_transfer_queue();
33         size_t data_size = wd*ht*get_pixel_size(storage_fmt);
34         void *staging = tq.prepare_transfer(this, false, data_size,
35                 [this, level, discard](){
36                         change_layout(level, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, discard);
37                 },
38                 [this, level, x, y, wd, ht](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){
39                         const VulkanFunctions &vk = device.get_functions();
40
41                         VkBufferImageCopy region = { };
42                         region.bufferOffset = src_off;
43                         region.imageSubresource.aspectMask = get_vulkan_aspect(get_components(storage_fmt));
44                         region.imageSubresource.mipLevel = level;
45                         region.imageSubresource.baseArrayLayer = 0;
46                         region.imageSubresource.layerCount = 1;
47                         region.imageOffset = { x, y, 0 };
48                         region.imageExtent = { wd, ht, 1 };
49                         vk.CmdCopyBufferToImage(cmd_buf, staging_buf, handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
50                 });
51
52         stage_pixels(staging, data, wd*ht);
53         tq.finalize_transfer(staging);
54 }
55
56 void VulkanTexture2D::fill_mipmap_blit(unsigned level, void *b)
57 {
58         const Texture2D &self = *static_cast<const Texture2D *>(this);
59         VkImageBlit &blit = *static_cast<VkImageBlit *>(b);
60
61         auto src_size = self.get_level_size(level);
62         auto dst_size = self.get_level_size(level+1);
63
64         blit.srcOffsets[1] = { static_cast<int>(src_size.x), static_cast<int>(src_size.y), 1 };
65         blit.dstOffsets[1] = { static_cast<int>(dst_size.x), static_cast<int>(dst_size.y), 1 };
66 }
67
68 Resource::AsyncLoader *VulkanTexture2D::load(IO::Seekable &, const Resources *)
69 {
70         throw logic_error("Texture2D::load is unimplemented");
71 }
72
73 uint64_t VulkanTexture2D::get_data_size() const
74 {
75         return 0;
76 }
77
78 void VulkanTexture2D::unload()
79 {
80 }
81
82 } // namespace GL
83 } // namespace Msp