From 87b743e92259ca74f82cb082bace3c447bb47a84 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Sat, 9 Jan 2016 22:14:19 +0100 Subject: [PATCH] Fix issues with texture replacement --- GPU/Vulkan/DrawEngineVulkan.cpp | 9 +++-- ext/native/thin3d/VulkanContext.cpp | 61 ++++++++++++++++++++--------- ext/native/thin3d/VulkanContext.h | 20 ++++++---- 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/GPU/Vulkan/DrawEngineVulkan.cpp b/GPU/Vulkan/DrawEngineVulkan.cpp index 84cbed6e6c..ee56b0a9bf 100644 --- a/GPU/Vulkan/DrawEngineVulkan.cpp +++ b/GPU/Vulkan/DrawEngineVulkan.cpp @@ -178,6 +178,7 @@ DrawEngineVulkan::DrawEngineVulkan(VulkanContext *vulkan) void DrawEngineVulkan::BeginFrame() { FrameData *frame = &frame_[curFrame_ & 1]; vkResetDescriptorPool(vulkan_->GetDevice(), frame->descPool, 0); + frame->descSets.clear(); frame->pushData->Begin(vulkan_->GetDevice()); frame->pushData->Reset(); } @@ -376,7 +377,7 @@ inline u32 ComputeMiniHashRange(const void *ptr, size_t sz) { VkDescriptorSet DrawEngineVulkan::GetDescriptorSet(VkImageView imageView, VkSampler sampler, VkBuffer dynamicUbo) { DescriptorSetKey key; - key.imageView_= imageView; + key.imageView_ = imageView; key.sampler_ = sampler; key.secondaryImageView_ = nullptr; key.buffer_ = dynamicUbo; @@ -387,7 +388,8 @@ VkDescriptorSet DrawEngineVulkan::GetDescriptorSet(VkImageView imageView, VkSamp return iter->second; } - // Didn't find one in the frame cache, let's make a new one. + // Didn't find one in the frame descriptor set cache, let's make a new one. + // We wipe the cache on every frame. VkDescriptorSet desc; VkDescriptorSetAllocateInfo descAlloc; @@ -396,7 +398,8 @@ VkDescriptorSet DrawEngineVulkan::GetDescriptorSet(VkImageView imageView, VkSamp descAlloc.pSetLayouts = &descriptorSetLayout_; descAlloc.descriptorPool = frame->descPool; descAlloc.descriptorSetCount = 1; - vkAllocateDescriptorSets(vulkan_->GetDevice(), &descAlloc, &desc); + VkResult result = vkAllocateDescriptorSets(vulkan_->GetDevice(), &descAlloc, &desc); + assert(result == VK_SUCCESS); // We just don't write to the slots we don't care about. VkWriteDescriptorSet writes[4]; diff --git a/ext/native/thin3d/VulkanContext.cpp b/ext/native/thin3d/VulkanContext.cpp index a1f3272547..c254cd930a 100644 --- a/ext/native/thin3d/VulkanContext.cpp +++ b/ext/native/thin3d/VulkanContext.cpp @@ -1131,25 +1131,41 @@ void VulkanContext::InitCommandPool() { assert(res == VK_SUCCESS); } -void VulkanTexture::Create(VulkanContext *vulkan, int w, int h, VkFormat format) { +VkResult VulkanTexture::Create(VulkanContext *vulkan, int w, int h, VkFormat format) { tex_width = w; tex_height = h; format_ = format; - bool U_ASSERT_ONLY pass; - VkFormatProperties formatProps; vkGetPhysicalDeviceFormatProperties(vulkan->GetPhysicalDevice(), format, &formatProps); // See if we can use a linear tiled image for a texture, if not, we will need a staging image for the texture data. // Linear tiling is usually only supported for 2D non-array textures. - needStaging = (!(formatProps.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) ? true : false; + // needStaging = (!(formatProps.linearTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT)) ? true : false; + // Always stage. + needStaging = true; + + return VK_SUCCESS; +} + +void VulkanTexture::CreateMappableImage(VulkanContext *vulkan) { + // If we already have a mappableImage, forget it. + if (mappableImage) { + vulkan->QueueDelete(mappableImage); + mappableImage = nullptr; + } + if (mappableMemory) { + vulkan->QueueDelete(mappableMemory); + mappableMemory = nullptr; + } + + bool U_ASSERT_ONLY pass; VkImageCreateInfo image_create_info = {}; image_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; image_create_info.pNext = NULL; image_create_info.imageType = VK_IMAGE_TYPE_2D; - image_create_info.format = format; + image_create_info.format = format_; image_create_info.extent.width = tex_width; image_create_info.extent.height = tex_height; image_create_info.extent.depth = 1; @@ -1169,8 +1185,8 @@ void VulkanTexture::Create(VulkanContext *vulkan, int w, int h, VkFormat format) mem_alloc.allocationSize = 0; mem_alloc.memoryTypeIndex = 0; - /* Create a mappable image. It will be the texture if linear images are ok to be textures */ - /* or it will be the staging image if they are not. */ + // Create a mappable image. It will be the texture if linear images are ok to be textures + // or it will be the staging image if they are not. VkResult res = vkCreateImage(vulkan->GetDevice(), &image_create_info, NULL, &mappableImage); assert(res == VK_SUCCESS); @@ -1179,7 +1195,7 @@ void VulkanTexture::Create(VulkanContext *vulkan, int w, int h, VkFormat format) mem_alloc.allocationSize = mem_reqs.size; - /* Find the memory type that is host mappable */ + // Find the memory type that is host mappable. pass = vulkan->MemoryTypeFromProperties(mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &mem_alloc.memoryTypeIndex); assert(pass); @@ -1191,6 +1207,8 @@ void VulkanTexture::Create(VulkanContext *vulkan, int w, int h, VkFormat format) } uint8_t *VulkanTexture::Lock(VulkanContext *vulkan, int level, int *rowPitch) { + CreateMappableImage(vulkan); + VkImageSubresource subres = {}; subres.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; subres.mipLevel = 0; @@ -1199,10 +1217,9 @@ uint8_t *VulkanTexture::Lock(VulkanContext *vulkan, int level, int *rowPitch) { VkSubresourceLayout layout; void *data; - /* Get the subresource layout so we know what the row pitch is */ + // Get the subresource layout so we know what the row pitch is vkGetImageSubresourceLayout(vulkan->GetDevice(), mappableImage, &subres, &layout); - - VkResult res = vkMapMemory(vulkan->GetDevice(), mappableMemory, 0, mem_reqs.size, 0, &data); + VkResult res = vkMapMemory(vulkan->GetDevice(), mappableMemory, layout.offset, layout.size, 0, &data); assert(res == VK_SUCCESS); *rowPitch = (int)layout.rowPitch; @@ -1214,6 +1231,15 @@ void VulkanTexture::Unlock(VulkanContext *vulkan) { VkCommandBuffer cmd = vulkan->GetInitCommandBuffer(); + // if we already have an image, queue it for destruction and forget it. + if (image) { + vulkan->QueueDelete(image); + image = nullptr; + } + if (view) { + vulkan->QueueDelete(view); + view = nullptr; + } if (!needStaging) { /* If we can use the linear tiled image as a texture, just do it */ image = mappableImage; @@ -1253,26 +1279,23 @@ void VulkanTexture::Unlock(VulkanContext *vulkan) { mem_alloc.memoryTypeIndex = 0; mem_alloc.allocationSize = mem_reqs.size; - /* Find memory type - dont specify any mapping requirements */ + // Find memory type - don't specify any mapping requirements bool pass = vulkan->MemoryTypeFromProperties(mem_reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &mem_alloc.memoryTypeIndex); assert(pass); - /* allocate memory */ res = vkAllocateMemory(vulkan->GetDevice(), &mem_alloc, NULL, &mem); assert(res == VK_SUCCESS); - /* bind memory */ res = vkBindImageMemory(vulkan->GetDevice(), image, mem, 0); assert(res == VK_SUCCESS); - /* Since we're going to blit from the mappable image, set its layout to SOURCE_OPTIMAL */ - /* Side effect is that this will create info.cmd */ + // Since we're going to blit from the mappable image, set its layout to SOURCE_OPTIMAL TransitionImageLayout(cmd, mappableImage, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL); - /* Since we're going to blit to the texture image, set its layout to DESTINATION_OPTIMAL */ + // Since we're going to blit to the texture image, set its layout to DESTINATION_OPTIMAL TransitionImageLayout(cmd, image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, @@ -1305,7 +1328,7 @@ void VulkanTexture::Unlock(VulkanContext *vulkan) { assert(res == VK_SUCCESS); - /* Set the layout for the texture image from DESTINATION_OPTIMAL to SHADER_READ_ONLY */ + // Set the layout for the texture image from DESTINATION_OPTIMAL to SHADER_READ_ONLY imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; TransitionImageLayout(cmd, image, VK_IMAGE_ASPECT_COLOR_BIT, @@ -1314,6 +1337,8 @@ void VulkanTexture::Unlock(VulkanContext *vulkan) { vulkan->QueueDelete(mappableMemory); vulkan->QueueDelete(mappableImage); + mappableImage = nullptr; + mappableMemory = nullptr; } VkImageViewCreateInfo view_info = {}; diff --git a/ext/native/thin3d/VulkanContext.h b/ext/native/thin3d/VulkanContext.h index b9e754f7ce..51093503e2 100644 --- a/ext/native/thin3d/VulkanContext.h +++ b/ext/native/thin3d/VulkanContext.h @@ -342,15 +342,15 @@ private: // Only supports simple 2D textures for now. Mipmap support will be added later. class VulkanTexture { public: - VkImage image; - VkImageLayout imageLayout; - - VkDeviceMemory mem; - VkImageView view; - + VulkanTexture() + : image(nullptr), mem(nullptr), view(nullptr), tex_width(0), tex_height(0), format_(VK_FORMAT_UNDEFINED), + mappableImage(nullptr), mappableMemory(nullptr) { + } // Always call Create, Lock, Unlock. Unlock performs the upload if necessary. + // Can later Lock and Unlock again. This cannot change the format. Create cannot + // be called a second time without recreating the texture object. - void Create(VulkanContext *vulkan, int w, int h, VkFormat format); + VkResult Create(VulkanContext *vulkan, int w, int h, VkFormat format); uint8_t *Lock(VulkanContext *vulkan, int level, int *rowPitch); void Unlock(VulkanContext *vulkan); @@ -359,6 +359,12 @@ public: VkImageView GetImageView() const { return view; } private: + void CreateMappableImage(VulkanContext *vulkan); + + VkImage image; + VkImageLayout imageLayout; + VkDeviceMemory mem; + VkImageView view; int32_t tex_width, tex_height; VkFormat format_; VkImage mappableImage;