]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/texture3d_backend.cpp
44a25904161306afbb5ab52a9f24a420bb57edef
[libs/gl.git] / source / backends / vulkan / texture3d_backend.cpp
1 #include "device.h"
2 #include "texture3d.h"
3 #include "texture3d_backend.h"
4 #include "vulkan.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 VulkanTexture3D::VulkanTexture3D():
12         Texture(VK_IMAGE_VIEW_TYPE_3D)
13 { }
14
15 VulkanTexture3D::VulkanTexture3D(unsigned t):
16         Texture(t)
17 { }
18
19 void VulkanTexture3D::fill_image_info(void *ii) const
20 {
21         const Texture3D &self = *static_cast<const Texture3D *>(this);
22
23         VkImageCreateInfo *image_info = static_cast<VkImageCreateInfo *>(ii);
24         image_info->imageType = VK_IMAGE_TYPE_3D;
25         image_info->extent.width = self.width;
26         image_info->extent.height = self.height;
27         image_info->extent.depth = self.depth;
28         image_info->mipLevels = self.levels;
29 }
30
31 void VulkanTexture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
32 {
33         const Texture3D &self = *static_cast<const Texture3D *>(this);
34
35         auto level_size = self.get_level_size(level);
36         bool discard = (x==0 && y==0 && z==0 && wd==level_size.x && ht==level_size.y && dp==level_size.z);
37
38         size_t data_size = wd*ht*dp*get_pixel_size(storage_fmt);
39         void *staging = device.get_transfer_queue().prepare_transfer(this, false, data_size,
40                 [this, level, discard](){
41                         unsigned n_levels = static_cast<const Texture3D *>(this)->levels;
42                         change_layout(n_levels, level, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, discard);
43                 },
44                 [this, level, x, y, z, wd, ht, dp](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){
45                         const VulkanFunctions &vk = device.get_functions();
46
47                         VkBufferImageCopy region = { };
48                         region.bufferOffset = src_off;
49                         region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
50                         region.imageSubresource.mipLevel = level;
51                         region.imageSubresource.baseArrayLayer = (is_array() ? z : 0);
52                         region.imageSubresource.layerCount = (is_array() ? dp : 1);
53                         region.imageOffset = { x, y, (is_array() ? 0 : z) };
54                         region.imageExtent = { wd, ht, (is_array() ? 1 : dp) };
55                         vk.CmdCopyBufferToImage(cmd_buf, staging_buf, handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
56                 });
57
58         const char *src = static_cast<const char *>(data);
59         copy(src, src+data_size, static_cast<char *>(staging));
60 }
61
62 void VulkanTexture3D::generate_mipmap()
63 {
64         generate_mipmap_levels(static_cast<const Texture3D *>(this)->levels);
65 }
66
67 void VulkanTexture3D::fill_mipmap_blit(unsigned level, void *b)
68 {
69         const Texture3D &self = *static_cast<const Texture3D *>(this);
70         VkImageBlit &blit = *static_cast<VkImageBlit *>(b);
71
72         LinAl::Vector<int, 3> src_size = self.get_level_size(level);
73         LinAl::Vector<int, 3> dst_size = self.get_level_size(level+1);
74
75         blit.srcSubresource.layerCount = (is_array() ? self.depth : 1);
76         blit.dstSubresource.layerCount = blit.srcSubresource.layerCount;
77         blit.srcOffsets[1] = { src_size.x, src_size.y, (is_array() ? 1 : src_size.z) };
78         blit.dstOffsets[1] = { dst_size.x, dst_size.y, (is_array() ? 1 : dst_size.z) };
79 }
80
81 bool VulkanTexture3D::is_array() const
82 {
83         return view_type==VK_IMAGE_VIEW_TYPE_2D_ARRAY;
84 }
85
86 size_t VulkanTexture3D::get_data_size() const
87 {
88         return 0;
89 }
90
91 } // namespace GL
92 } // namespace Msp