]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/vulkan/texture3d_backend.cpp
Implement textures and samplers for Vulkan
[libs/gl.git] / source / backends / vulkan / texture3d_backend.cpp
index 26e55e5ac9e846c696744279011584782e605a15..7a11b35e2eec04d1e84378f4fa892fe4619cd836 100644 (file)
@@ -1,21 +1,59 @@
+#include "device.h"
+#include "texture3d.h"
 #include "texture3d_backend.h"
 #include "vulkan.h"
 
+using namespace std;
+
 namespace Msp {
 namespace GL {
 
 VulkanTexture3D::VulkanTexture3D():
        Texture(VK_IMAGE_VIEW_TYPE_3D)
-{
-       throw std::logic_error("VulkanTexture3D is unimplemented");
-}
+{ }
 
 VulkanTexture3D::VulkanTexture3D(unsigned t):
        Texture(t)
 { }
 
-void VulkanTexture3D::sub_image(unsigned, int, int, int, unsigned, unsigned, unsigned, const void *)
+void VulkanTexture3D::fill_image_info(void *ii) const
+{
+       const Texture3D &self = *static_cast<const Texture3D *>(this);
+
+       VkImageCreateInfo *image_info = static_cast<VkImageCreateInfo *>(ii);
+       image_info->imageType = VK_IMAGE_TYPE_3D;
+       image_info->extent.width = self.width;
+       image_info->extent.height = self.height;
+       image_info->extent.depth = self.depth;
+       image_info->mipLevels = self.levels;
+}
+
+void VulkanTexture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
 {
+       const Texture3D &self = *static_cast<const Texture3D *>(this);
+
+       auto level_size = self.get_level_size(level);
+       int layer = (is_array() && dp==1 ? z : -1);
+       synchronize(layer, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, (x==0 && y==0 && z==0 && wd==level_size.x && ht==level_size.y && dp==level_size.z));
+
+       size_t data_size = wd*ht*dp*get_pixel_size(storage_fmt);
+       void *staging = device.get_transfer_queue().prepare_transfer(data_size,
+               [this, level, x, y, z, wd, ht, dp](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){
+                       const VulkanFunctions &vk = device.get_functions();
+
+                       VkBufferImageCopy region = { };
+                       region.bufferOffset = src_off;
+                       region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
+                       region.imageSubresource.mipLevel = level;
+                       region.imageSubresource.baseArrayLayer = (is_array() ? z : 0);
+                       region.imageSubresource.layerCount = (is_array() ? dp : 1);
+                       region.imageOffset = { x, y, (is_array() ? 0 : z) };
+                       region.imageExtent = { wd, ht, (is_array() ? 1 : dp) };
+                       vk.CmdCopyBufferToImage(cmd_buf, staging_buf, handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
+               });
+
+       const char *src = static_cast<const char *>(data);
+       copy(src, src+data_size, static_cast<char *>(staging));
 }
 
 bool VulkanTexture3D::is_array() const