]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/texture2d_backend.cpp
7c6b55f1ee94dfb0652ef76ea8adb7e75675fbbf
[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         Texture2D::AsyncTransfer transfer(*static_cast<Texture2D *>(this), level, x, y, wd, ht);
28         stage_pixels(transfer.get_address(), data, wd*ht);
29 }
30
31 void VulkanTexture2D::fill_mipmap_blit(unsigned level, void *b)
32 {
33         const Texture2D &self = *static_cast<const Texture2D *>(this);
34         VkImageBlit &blit = *static_cast<VkImageBlit *>(b);
35
36         auto src_size = self.get_level_size(level);
37         auto dst_size = self.get_level_size(level+1);
38
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 };
41 }
42
43 Resource::AsyncLoader *VulkanTexture2D::load(IO::Seekable &, const Resources *)
44 {
45         throw logic_error("Texture2D::load is unimplemented");
46 }
47
48 uint64_t VulkanTexture2D::get_data_size() const
49 {
50         return 0;
51 }
52
53 void VulkanTexture2D::unload()
54 {
55 }
56
57
58 void *VulkanTexture2D::AsyncTransfer::allocate()
59 {
60         const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
61
62         Texture2D &tex = *self.texture;
63         unsigned level = self.level;
64         int x = self.x;
65         int y = self.y;
66         unsigned wd = self.width;
67         unsigned ht = self.height;
68
69         auto level_size = tex.get_level_size(level);
70         bool discard = (x==0 && y==0 && wd==level_size.x && ht==level_size.y);
71
72         TransferQueue &tq = tex.device.get_transfer_queue();
73         return tq.prepare_transfer(&tex, false, self.data_size,
74                 [&tex, level, discard](){
75                         tex.change_layout(level, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, discard);
76                 },
77                 [&tex, level, x, y, wd, ht](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){
78                         const VulkanFunctions &vk = tex.device.get_functions();
79
80                         VkBufferImageCopy region = { };
81                         region.bufferOffset = src_off;
82                         region.imageSubresource.aspectMask = get_vulkan_aspect(get_components(tex.storage_fmt));
83                         region.imageSubresource.mipLevel = level;
84                         region.imageSubresource.baseArrayLayer = 0;
85                         region.imageSubresource.layerCount = 1;
86                         region.imageOffset = { x, y, 0 };
87                         region.imageExtent = { wd, ht, 1 };
88                         vk.CmdCopyBufferToImage(cmd_buf, staging_buf, tex.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
89                 });
90 }
91
92 void VulkanTexture2D::AsyncTransfer::finalize()
93 {
94         const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
95         self.texture->device.get_transfer_queue().finalize_transfer(self.dest_addr);
96 }
97
98 } // namespace GL
99 } // namespace Msp