diff options
author | Juan Linietsky <reduzio@gmail.com> | 2019-10-25 11:22:19 -0300 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2020-02-11 12:04:54 +0100 |
commit | 2af701fa03037aed2cd544cc4fa4fe6ec81510bf (patch) | |
tree | f82cc5708d7bbc27835b2b2ebf9bd5f82348ba70 /drivers | |
parent | a60edae5060664b32fd24a3970b027171f358045 (diff) |
Use a different strategy to get textures from GPU (via buffers), for more compatibility.
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/vulkan/rendering_device_vulkan.cpp | 171 | ||||
-rw-r--r-- | drivers/vulkan/rendering_device_vulkan.h | 2 |
2 files changed, 55 insertions, 118 deletions
diff --git a/drivers/vulkan/rendering_device_vulkan.cpp b/drivers/vulkan/rendering_device_vulkan.cpp index d74f77c328..cc538593b5 100644 --- a/drivers/vulkan/rendering_device_vulkan.cpp +++ b/drivers/vulkan/rendering_device_vulkan.cpp @@ -2250,10 +2250,10 @@ Error RenderingDeviceVulkan::texture_update(RID p_texture, uint32_t p_layer, con return OK; } -PoolVector<uint8_t> RenderingDeviceVulkan::_texture_get_data_from_image(Texture *tex, VkImage p_image, VmaAllocation p_allocation, uint32_t p_layer) { +PoolVector<uint8_t> RenderingDeviceVulkan::_texture_get_data_from_image(Texture *tex, VkImage p_image, VmaAllocation p_allocation, uint32_t p_layer, bool p_2d) { uint32_t width, height, depth; - uint32_t image_size = get_image_format_required_size(tex->format, tex->width, tex->height, tex->depth, tex->mipmaps, &width, &height, &depth); + uint32_t image_size = get_image_format_required_size(tex->format, tex->width, tex->height, p_2d ? 1 : tex->depth, tex->mipmaps, &width, &height, &depth); PoolVector<uint8_t> image_data; image_data.resize(image_size); @@ -2272,7 +2272,7 @@ PoolVector<uint8_t> RenderingDeviceVulkan::_texture_get_data_from_image(Texture uint32_t mipmap_offset = 0; for (uint32_t mm_i = 0; mm_i < tex->mipmaps; mm_i++) { - uint32_t image_total = get_image_format_required_size(tex->format, tex->width, tex->height, tex->depth, mm_i + 1, &width, &height, &depth); + uint32_t image_total = get_image_format_required_size(tex->format, tex->width, tex->height, p_2d ? 1 : tex->depth, mm_i + 1, &width, &height, &depth); uint8_t *write_ptr_mipmap = w.ptr() + mipmap_offset; image_size = image_total - mipmap_offset; @@ -2339,46 +2339,17 @@ PoolVector<uint8_t> RenderingDeviceVulkan::texture_get_data(RID p_texture, uint3 //does not need anything fancy, map and read. return _texture_get_data_from_image(tex, tex->image, tex->allocation, p_layer); } else { - VkImageCreateInfo image_create_info; - image_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; - image_create_info.pNext = NULL; - image_create_info.flags = 0; - image_create_info.imageType = vulkan_image_type[tex->type]; - image_create_info.format = vulkan_formats[tex->format]; - image_create_info.extent.width = tex->width; - image_create_info.extent.height = tex->height; - image_create_info.extent.depth = tex->depth; - image_create_info.mipLevels = tex->mipmaps; - image_create_info.arrayLayers = 1; //for retrieving, only one layer - image_create_info.samples = rasterization_sample_count[tex->samples]; - image_create_info.tiling = VK_IMAGE_TILING_LINEAR; // for retrieving, linear is recommended - image_create_info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT; - image_create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; - image_create_info.queueFamilyIndexCount = 0; - image_create_info.pQueueFamilyIndices = NULL; - image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - - VmaAllocationCreateInfo allocInfo; - allocInfo.flags = 0; - allocInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY; - allocInfo.requiredFlags = 0; - allocInfo.preferredFlags = 0; - allocInfo.memoryTypeBits = 0; - allocInfo.pool = NULL; - allocInfo.pUserData = NULL; - - VkImage image; - VmaAllocation allocation; - VmaAllocationInfo allocation_info; - - //Allocate the image - VkResult err = vmaCreateImage(allocator, &image_create_info, &allocInfo, &image, &allocation, &allocation_info); - ERR_FAIL_COND_V(err, PoolVector<uint8_t>()); + //compute total image size + uint32_t width, height, depth; + uint32_t buffer_size = get_image_format_required_size(tex->format, tex->width, tex->height, tex->depth, tex->mipmaps, &width, &height, &depth); + + //allocate buffer VkCommandBuffer command_buffer = frames[frame].setup_command_buffer; - //PRE Copy the image + Buffer tmp_buffer; + _buffer_allocate(&tmp_buffer, buffer_size, VK_BUFFER_USAGE_TRANSFER_DST_BIT, VMA_MEMORY_USAGE_CPU_ONLY); - { //Source + { //Source image barrier VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; image_memory_barrier.pNext = NULL; @@ -2398,70 +2369,43 @@ PoolVector<uint8_t> RenderingDeviceVulkan::texture_get_data(RID p_texture, uint3 vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); } - { //Dest - VkImageMemoryBarrier image_memory_barrier; - image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; - image_memory_barrier.srcAccessMask = 0; - image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; - image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; - image_memory_barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; - - image_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; - image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; - image_memory_barrier.image = image; - image_memory_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - image_memory_barrier.subresourceRange.baseMipLevel = 0; - image_memory_barrier.subresourceRange.levelCount = tex->mipmaps; - image_memory_barrier.subresourceRange.baseArrayLayer = 0; - image_memory_barrier.subresourceRange.layerCount = 1; - - vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); - } - - //COPY - - { - - //despite textures being in block sizes, spec requiers they are in pixel sizes (?) - uint32_t computed_w = tex->width; - uint32_t computed_h = tex->height; - for (uint32_t i = 0; i < tex->mipmaps; i++) { + uint32_t computed_w = tex->width; + uint32_t computed_h = tex->height; + uint32_t computed_d = tex->depth; - uint32_t mm_width, mm_height, mm_depth; - get_image_format_required_size(tex->format, tex->width, tex->height, tex->depth, i + 1, &mm_width, &mm_height, &mm_depth); + uint32_t prev_size = 0; + uint32_t offset = 0; + for (uint32_t i = 0; i < tex->mipmaps; i++) { - VkImageCopy image_copy_region; - image_copy_region.srcSubresource.aspectMask = tex->read_aspect_mask; - image_copy_region.srcSubresource.baseArrayLayer = p_layer; - image_copy_region.srcSubresource.layerCount = 1; - image_copy_region.srcSubresource.mipLevel = i; - image_copy_region.srcOffset.x = 0; - image_copy_region.srcOffset.y = 0; - image_copy_region.srcOffset.z = 0; + VkBufferImageCopy buffer_image_copy; - image_copy_region.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - image_copy_region.dstSubresource.baseArrayLayer = p_layer; - image_copy_region.dstSubresource.layerCount = 1; - image_copy_region.dstSubresource.mipLevel = i; - image_copy_region.dstOffset.x = 0; - image_copy_region.dstOffset.y = 0; - image_copy_region.dstOffset.z = 0; + uint32_t image_size = get_image_format_required_size(tex->format, tex->width, tex->height, tex->depth, i + 1); + uint32_t size = image_size - prev_size; + prev_size = image_size; - image_copy_region.extent.width = computed_w; - image_copy_region.extent.height = computed_h; - image_copy_region.extent.depth = mm_depth; //block is only x,y so this is fine anyway + buffer_image_copy.bufferOffset = offset; + buffer_image_copy.bufferImageHeight = 0; + buffer_image_copy.bufferRowLength = 0; + buffer_image_copy.imageSubresource.aspectMask = tex->read_aspect_mask; + buffer_image_copy.imageSubresource.baseArrayLayer = p_layer; + buffer_image_copy.imageSubresource.layerCount = 1; + buffer_image_copy.imageSubresource.mipLevel = i; + buffer_image_copy.imageOffset.x = 0; + buffer_image_copy.imageOffset.y = 0; + buffer_image_copy.imageOffset.z = 0; + buffer_image_copy.imageExtent.width = computed_w; + buffer_image_copy.imageExtent.height = computed_h; + buffer_image_copy.imageExtent.depth = computed_d; - vkCmdCopyImage(command_buffer, tex->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy_region); + vkCmdCopyImageToBuffer(command_buffer, tex->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, tmp_buffer.buffer, 1, &buffer_image_copy); - computed_w = MAX(1, computed_w >> 1); - computed_h = MAX(1, computed_h >> 1); - } + computed_w = MAX(1, computed_w >> 1); + computed_h = MAX(1, computed_h >> 1); + computed_d = MAX(1, computed_d >> 1); + offset += size; } - // RESTORE LAYOUT for SRC and DST - { //restore src VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; @@ -2482,34 +2426,27 @@ PoolVector<uint8_t> RenderingDeviceVulkan::texture_get_data(RID p_texture, uint3 vkCmdPipelineBarrier(command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); } - { //make dst readable + _flush(true); - VkImageMemoryBarrier image_memory_barrier; - image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; - image_memory_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; - image_memory_barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; - image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; - image_memory_barrier.newLayout = VK_IMAGE_LAYOUT_GENERAL; + void *buffer_mem; + VkResult vkerr = vmaMapMemory(allocator, tmp_buffer.allocation, &buffer_mem); + if (vkerr) { + ERR_FAIL_V(PoolVector<uint8_t>()); + } - image_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; - image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; - image_memory_barrier.image = image; - image_memory_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - image_memory_barrier.subresourceRange.baseMipLevel = 0; - image_memory_barrier.subresourceRange.levelCount = tex->mipmaps; - image_memory_barrier.subresourceRange.baseArrayLayer = 0; - image_memory_barrier.subresourceRange.layerCount = 1; + PoolVector<uint8_t> buffer_data; + { - vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + buffer_data.resize(buffer_size); + PoolVector<uint8_t>::Write w = buffer_data.write(); + copymem(w.ptr(), buffer_mem, buffer_size); } - //flush everything so memory can be safely mapped - _flush(true); + vmaUnmapMemory(allocator, tmp_buffer.allocation); + + _buffer_free(&tmp_buffer); - PoolVector<uint8_t> ret = _texture_get_data_from_image(tex, image, allocation, p_layer); - vmaDestroyImage(allocator, image, allocation); - return ret; + return buffer_data; } } diff --git a/drivers/vulkan/rendering_device_vulkan.h b/drivers/vulkan/rendering_device_vulkan.h index 6b7d9e7c98..0dc3cc702b 100644 --- a/drivers/vulkan/rendering_device_vulkan.h +++ b/drivers/vulkan/rendering_device_vulkan.h @@ -150,7 +150,7 @@ class RenderingDeviceVulkan : public RenderingDevice { RID_Owner<Texture, true> texture_owner; uint32_t texture_upload_region_size_px; - PoolVector<uint8_t> _texture_get_data_from_image(Texture *tex, VkImage p_image, VmaAllocation p_allocation, uint32_t p_layer); + PoolVector<uint8_t> _texture_get_data_from_image(Texture *tex, VkImage p_image, VmaAllocation p_allocation, uint32_t p_layer, bool p_2d = false); /*****************/ /**** SAMPLER ****/ |