]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/texture2d_backend.cpp
Support multiple PipelineStates in Renderer
[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 uint64_t VulkanTexture2D::get_data_size() const
44 {
45         return 0;
46 }
47
48 void VulkanTexture2D::unload()
49 {
50 }
51
52
53 void *VulkanTexture2D::AsyncTransfer::allocate()
54 {
55         const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
56
57         Texture2D &tex = *self.texture;
58         unsigned level = self.level;
59         int x = self.x;
60         int y = self.y;
61         unsigned wd = self.width;
62         unsigned ht = self.height;
63
64         auto level_size = tex.get_level_size(level);
65         bool discard = (x==0 && y==0 && wd==level_size.x && ht==level_size.y);
66
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);
71                 },
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();
74
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, &region);
84                 });
85 }
86
87 void VulkanTexture2D::AsyncTransfer::finalize()
88 {
89         const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
90         self.texture->device.get_transfer_queue().finalize_transfer(self.dest_addr);
91 }
92
93 } // namespace GL
94 } // namespace Msp