X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbackends%2Fvulkan%2Ftexture2d_backend.cpp;h=1ab49b888a63a5eeeebb7cc626ebc21d9a243050;hb=225dbd7ba2dde73bb28e54e03ae960e88e708f57;hp=a80e1f945e280ebcfbc422bd62eea127528aa2d3;hpb=99ca354f18119f82f1adeca100cd665a8f640317;p=libs%2Fgl.git diff --git a/source/backends/vulkan/texture2d_backend.cpp b/source/backends/vulkan/texture2d_backend.cpp index a80e1f94..1ab49b88 100644 --- a/source/backends/vulkan/texture2d_backend.cpp +++ b/source/backends/vulkan/texture2d_backend.cpp @@ -1,3 +1,5 @@ +#include "device.h" +#include "texture2d.h" #include "texture2d_backend.h" #include "vulkan.h" @@ -10,14 +12,32 @@ VulkanTexture2D::VulkanTexture2D(): Texture(VK_IMAGE_VIEW_TYPE_2D) { } -void VulkanTexture2D::sub_image(unsigned, int, int, unsigned, unsigned, const void *) +void VulkanTexture2D::fill_image_info(void *ii) const { - throw logic_error("Texture2D::sub_image is unimplemented"); + const Texture2D &self = *static_cast(this); + + VkImageCreateInfo *image_info = static_cast(ii); + image_info->imageType = VK_IMAGE_TYPE_2D; + image_info->extent.width = self.width; + image_info->extent.height = self.height; } -Resource::AsyncLoader *VulkanTexture2D::load(IO::Seekable &, const Resources *) +void VulkanTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data) { - throw logic_error("Texture2D::load is unimplemented"); + Texture2D::AsyncTransfer transfer(*static_cast(this), level, x, y, wd, ht); + stage_pixels(transfer.get_address(), data, wd*ht); +} + +void VulkanTexture2D::fill_mipmap_blit(unsigned level, void *b) +{ + const Texture2D &self = *static_cast(this); + VkImageBlit &blit = *static_cast(b); + + auto src_size = self.get_level_size(level); + auto dst_size = self.get_level_size(level+1); + + blit.srcOffsets[1] = { static_cast(src_size.x), static_cast(src_size.y), 1 }; + blit.dstOffsets[1] = { static_cast(dst_size.x), static_cast(dst_size.y), 1 }; } uint64_t VulkanTexture2D::get_data_size() const @@ -29,5 +49,46 @@ void VulkanTexture2D::unload() { } + +void *VulkanTexture2D::AsyncTransfer::allocate() +{ + const Texture2D::AsyncTransfer &self = *static_cast(this); + + Texture2D &tex = *self.texture; + unsigned level = self.level; + int x = self.x; + int y = self.y; + unsigned wd = self.width; + unsigned ht = self.height; + + auto level_size = tex.get_level_size(level); + bool discard = (x==0 && y==0 && wd==level_size.x && ht==level_size.y); + + TransferQueue &tq = tex.device.get_transfer_queue(); + return tq.prepare_transfer(&tex, false, self.data_size, + [&tex, level, discard](){ + tex.change_layout(level, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, discard); + }, + [&tex, level, x, y, wd, ht](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){ + const VulkanFunctions &vk = tex.device.get_functions(); + + VkBufferImageCopy region = { }; + region.bufferOffset = src_off; + region.imageSubresource.aspectMask = get_vulkan_aspect(get_components(tex.storage_fmt)); + region.imageSubresource.mipLevel = level; + region.imageSubresource.baseArrayLayer = 0; + region.imageSubresource.layerCount = 1; + region.imageOffset = { x, y, 0 }; + region.imageExtent = { wd, ht, 1 }; + vk.CmdCopyBufferToImage(cmd_buf, staging_buf, tex.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion); + }); +} + +void VulkanTexture2D::AsyncTransfer::finalize() +{ + const Texture2D::AsyncTransfer &self = *static_cast(this); + self.texture->device.get_transfer_queue().finalize_transfer(self.dest_addr); +} + } // namespace GL } // namespace Msp