]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/vulkan/texturecube_backend.cpp
Implement textures and samplers for Vulkan
[libs/gl.git] / source / backends / vulkan / texturecube_backend.cpp
index 77a8b3d3522e9d5e4145ab0329299dfc7fe705e9..fe528921f8c99b135537e4ae397f2978cabb62cf 100644 (file)
@@ -1,3 +1,5 @@
+#include "device.h"
+#include "texturecube.h"
 #include "texturecube_backend.h"
 #include "vulkan.h"
 
@@ -8,12 +10,46 @@ namespace GL {
 
 VulkanTextureCube::VulkanTextureCube():
        Texture(VK_IMAGE_VIEW_TYPE_CUBE)
+{ }
+
+void VulkanTextureCube::fill_image_info(void *ii) const
 {
-       throw std::logic_error("VulkanTextureCube is unimplemented");
+       const TextureCube &self = *static_cast<const TextureCube *>(this);
+
+       VkImageCreateInfo *image_info = static_cast<VkImageCreateInfo *>(ii);
+       image_info->flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
+       image_info->imageType = VK_IMAGE_TYPE_2D;
+       image_info->extent.width = self.size;
+       image_info->extent.height = self.size;
+       image_info->mipLevels = self.levels;
+       image_info->arrayLayers = 6;
 }
 
-void VulkanTextureCube::sub_image(unsigned, unsigned, int, int, unsigned, unsigned, const void *)
+void VulkanTextureCube::sub_image(unsigned face, unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
 {
+       const TextureCube &self = *static_cast<const TextureCube *>(this);
+
+       unsigned level_size = self.get_level_size(level);
+       synchronize(face, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, (x==0 && y==0 && wd==level_size && ht==level_size));
+
+       size_t data_size = wd*ht*get_pixel_size(storage_fmt);
+       void *staging = device.get_transfer_queue().prepare_transfer(data_size,
+               [this, face, level, x, y, wd, ht](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 = face;
+                       region.imageSubresource.layerCount = 1;
+                       region.imageOffset = { x, y, 0 };
+                       region.imageExtent = { wd, ht, 1 };
+                       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));
 }
 
 size_t VulkanTextureCube::get_data_size() const