From 1f33907f20439d62f757f9e0557aa5b533b76630 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 Nov 2017 17:59:34 -0800 Subject: [PATCH 1/5] TexCache: Warning fix. --- GPU/Common/TextureCacheCommon.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GPU/Common/TextureCacheCommon.cpp b/GPU/Common/TextureCacheCommon.cpp index ad65820ed7..d00dfd8b90 100644 --- a/GPU/Common/TextureCacheCommon.cpp +++ b/GPU/Common/TextureCacheCommon.cpp @@ -705,7 +705,7 @@ bool TextureCacheCommon::AttachFramebuffer(TexCacheEntry *entry, u32 address, Vi } // Check if it's in bufferWidth (which might be higher than width and may indicate the framebuffer includes the data.) - if (fbInfo.xOffset >= framebuffer->bufferWidth && fbInfo.xOffset + w <= framebuffer->fb_stride) { + if (fbInfo.xOffset >= framebuffer->bufferWidth && fbInfo.xOffset + w <= (u32)framebuffer->fb_stride) { // This happens in Brave Story, see #10045 - the texture is in the space between strides, with matching stride. DetachFramebuffer(entry, address, framebuffer); return false; From cd6249d0803f25f7e9db869355f9360a828a2745 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 Nov 2017 18:00:14 -0800 Subject: [PATCH 2/5] D3D11: Show FBO-as-texture in GE debugger. --- GPU/D3D11/TextureCacheD3D11.cpp | 14 ++++++++++++++ GPU/GPU.vcxproj.filters | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/GPU/D3D11/TextureCacheD3D11.cpp b/GPU/D3D11/TextureCacheD3D11.cpp index 26744c22b8..5f190a6eb1 100644 --- a/GPU/D3D11/TextureCacheD3D11.cpp +++ b/GPU/D3D11/TextureCacheD3D11.cpp @@ -819,6 +819,20 @@ bool TextureCacheD3D11::GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level if (!nextTexture_) return false; + // TODO: Centralize. + if (nextTexture_->framebuffer) { + VirtualFramebuffer *vfb = nextTexture_->framebuffer; + bool flipY = g_Config.iGPUBackend == GPU_BACKEND_OPENGL && g_Config.iRenderingMode != FB_NON_BUFFERED_MODE; + buffer.Allocate(vfb->bufferWidth, vfb->bufferHeight, GPU_DBG_FORMAT_8888, flipY); + bool retval = draw_->CopyFramebufferToMemorySync(vfb->fbo, Draw::FB_COLOR_BIT, 0, 0, vfb->bufferWidth, vfb->bufferHeight, Draw::DataFormat::R8G8B8A8_UNORM, buffer.GetData(), vfb->bufferWidth); + // Vulkan requires us to re-apply all dynamic state for each command buffer, and the above will cause us to start a new cmdbuf. + // So let's dirty the things that are involved in Vulkan dynamic state. Readbacks are not frequent so this won't hurt other backends. + gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE); + // We may have blitted to a temp FBO. + framebufferManager_->RebindFramebuffer(); + return retval; + } + ID3D11Texture2D *texture = (ID3D11Texture2D *)nextTexture_->texturePtr; if (!texture) return false; diff --git a/GPU/GPU.vcxproj.filters b/GPU/GPU.vcxproj.filters index 8aa5382057..ad9211a28c 100644 --- a/GPU/GPU.vcxproj.filters +++ b/GPU/GPU.vcxproj.filters @@ -255,7 +255,6 @@ D3D11 - Common @@ -268,6 +267,9 @@ Common + + D3D11 + From 138837ac5405b4df60f302190fc455c78104a8bb Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 Nov 2017 18:03:53 -0800 Subject: [PATCH 3/5] Vulkan: Add texture image readback. This way we can see how it was decoded, scaled, etc. This also helps seeing texture cache invalidation issues. --- ext/native/thin3d/DataFormat.h | 6 +++ ext/native/thin3d/VulkanQueueRunner.cpp | 65 +++++++++++++++++++++-- ext/native/thin3d/VulkanQueueRunner.h | 10 +++- ext/native/thin3d/VulkanRenderManager.cpp | 41 +++++++++++++- ext/native/thin3d/VulkanRenderManager.h | 1 + ext/native/thin3d/thin3d.h | 6 --- 6 files changed, 115 insertions(+), 14 deletions(-) diff --git a/ext/native/thin3d/DataFormat.h b/ext/native/thin3d/DataFormat.h index fa9a8325be..bc89107c56 100644 --- a/ext/native/thin3d/DataFormat.h +++ b/ext/native/thin3d/DataFormat.h @@ -66,6 +66,12 @@ enum class DataFormat : uint8_t { D32F_S8, }; +size_t DataFormatSizeInBytes(DataFormat fmt); +bool DataFormatIsDepthStencil(DataFormat fmt); +inline bool DataFormatIsColor(DataFormat fmt) { + return !DataFormatIsDepthStencil(fmt); +} + void ConvertFromRGBA8888(uint8_t *dst, const uint8_t *src, uint32_t dstStride, uint32_t srcStride, uint32_t width, uint32_t height, DataFormat format); } // namespace diff --git a/ext/native/thin3d/VulkanQueueRunner.cpp b/ext/native/thin3d/VulkanQueueRunner.cpp index 50fbf123bd..d06596488c 100644 --- a/ext/native/thin3d/VulkanQueueRunner.cpp +++ b/ext/native/thin3d/VulkanQueueRunner.cpp @@ -2,6 +2,8 @@ #include "VulkanQueueRunner.h" #include "VulkanRenderManager.h" +// TODO: This is only enough for 4x render resolution / 4x texture upscale for debugger. +// Maybe we should use a dynamically allocated one for larger? const uint32_t readbackBufferSize = 2048 * 2048 * 4; void VulkanQueueRunner::CreateDeviceObjects() { @@ -205,6 +207,9 @@ void VulkanQueueRunner::RunSteps(VkCommandBuffer cmd, const std::vector &steps) { case VKRStepType::READBACK: LogReadback(step); break; + case VKRStepType::READBACK_IMAGE: + LogReadbackImage(step); + break; } } } @@ -280,6 +288,10 @@ void VulkanQueueRunner::LogReadback(const VKRStep &pass) { ILOG("Readback"); } +void VulkanQueueRunner::LogReadbackImage(const VKRStep &pass) { + ILOG("ReadbackImage"); +} + void VulkanQueueRunner::PerformRenderPass(const VKRStep &step, VkCommandBuffer cmd) { // TODO: If there are multiple, we can transition them together. for (const auto &iter : step.preTransitions) { @@ -822,8 +834,6 @@ void VulkanQueueRunner::PerformReadback(const VKRStep &step, VkCommandBuffer cmd assert(false); } - assert(srcImage->format == VK_FORMAT_R8G8B8A8_UNORM); - VkImageMemoryBarrier barrier{ VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER }; VkPipelineStageFlags stage = 0; if (srcImage->layout != VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL) { @@ -846,13 +856,58 @@ void VulkanQueueRunner::PerformReadback(const VKRStep &step, VkCommandBuffer cmd step.readback.src->Release(); } -void VulkanQueueRunner::CopyReadbackBuffer(int width, int height, Draw::DataFormat destFormat, int pixelStride, uint8_t *pixels) { +void VulkanQueueRunner::PerformReadbackImage(const VKRStep &step, VkCommandBuffer cmd) { + // TODO: Clean this up - just reusing `SetupTransitionToTransferSrc`. + VKRImage srcImage; + srcImage.image = step.readback_image.image; + srcImage.layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + + VkImageMemoryBarrier barrier{ VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER }; + VkPipelineStageFlags stage = 0; + SetupTransitionToTransferSrc(srcImage, barrier, stage, VK_IMAGE_ASPECT_COLOR_BIT); + vkCmdPipelineBarrier(cmd, stage, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &barrier); + + VkBufferImageCopy region{}; + region.imageOffset = { step.readback_image.srcRect.offset.x, step.readback_image.srcRect.offset.y, 0 }; + region.imageExtent = { step.readback_image.srcRect.extent.width, step.readback_image.srcRect.extent.height, 1 }; + region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + region.imageSubresource.layerCount = 1; + region.imageSubresource.mipLevel = step.readback_image.mipLevel; + region.bufferOffset = 0; + region.bufferRowLength = step.readback_image.srcRect.extent.width; + region.bufferImageHeight = step.readback_image.srcRect.extent.height; + vkCmdCopyImageToBuffer(cmd, step.readback_image.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, readbackBuffer_, 1, ®ion); + + // Now transfer it back to a texture. + TransitionImageLayout2(cmd, step.readback_image.image, + VK_IMAGE_ASPECT_COLOR_BIT, + VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, + VK_ACCESS_TRANSFER_READ_BIT, VK_ACCESS_SHADER_READ_BIT); + + // NOTE: Can't read the buffer using the CPU here - need to sync first. +} + +void VulkanQueueRunner::CopyReadbackBuffer(int width, int height, Draw::DataFormat srcFormat, Draw::DataFormat destFormat, int pixelStride, uint8_t *pixels) { // Read back to the requested address in ram from buffer. void *mappedData; - const int srcPixelSize = 4; // TODO: Fix. + const size_t srcPixelSize = DataFormatSizeInBytes(destFormat); VkResult res = vkMapMemory(vulkan_->GetDevice(), readbackMemory_, 0, width * height * srcPixelSize, 0, &mappedData); assert(res == VK_SUCCESS); - ConvertFromRGBA8888(pixels, (const uint8_t *)mappedData, pixelStride, width, width, height, destFormat); + if (srcFormat == Draw::DataFormat::R8G8B8A8_UNORM) { + ConvertFromRGBA8888(pixels, (const uint8_t *)mappedData, pixelStride, width, width, height, destFormat); + } else if (srcFormat == destFormat) { + uint8_t *dst = pixels; + const uint8_t *src = (const uint8_t *)mappedData; + for (int y = 0; y < height; ++y) { + memcpy(dst, src, width * srcPixelSize); + src += width * srcPixelSize; + dst += pixelStride * srcPixelSize; + } + } else { + // TODO: Maybe depth conversion or something? + assert(false); + } vkUnmapMemory(vulkan_->GetDevice(), readbackMemory_); } diff --git a/ext/native/thin3d/VulkanQueueRunner.h b/ext/native/thin3d/VulkanQueueRunner.h index 06a85a0721..7b70a14f23 100644 --- a/ext/native/thin3d/VulkanQueueRunner.h +++ b/ext/native/thin3d/VulkanQueueRunner.h @@ -84,6 +84,7 @@ enum class VKRStepType : uint8_t { COPY, BLIT, READBACK, + READBACK_IMAGE, }; enum class VKRRenderPassAction { @@ -133,6 +134,11 @@ struct VKRStep { VKRFramebuffer *src; VkRect2D srcRect; } readback; + struct { + VkImage image; + VkRect2D srcRect; + int mipLevel; + } readback_image; }; }; @@ -159,7 +165,7 @@ public: return (int)depth * 3 + (int)color; } - void CopyReadbackBuffer(int width, int height, Draw::DataFormat destFormat, int pixelStride, uint8_t *pixels); + void CopyReadbackBuffer(int width, int height, Draw::DataFormat srcFormat, Draw::DataFormat destFormat, int pixelStride, uint8_t *pixels); private: void InitBackbufferRenderPass(); @@ -170,11 +176,13 @@ private: void PerformCopy(const VKRStep &pass, VkCommandBuffer cmd); void PerformBlit(const VKRStep &pass, VkCommandBuffer cmd); void PerformReadback(const VKRStep &pass, VkCommandBuffer cmd); + void PerformReadbackImage(const VKRStep &pass, VkCommandBuffer cmd); void LogRenderPass(const VKRStep &pass); void LogCopy(const VKRStep &pass); void LogBlit(const VKRStep &pass); void LogReadback(const VKRStep &pass); + void LogReadbackImage(const VKRStep &pass); static void SetupTransitionToTransferSrc(VKRImage &img, VkImageMemoryBarrier &barrier, VkPipelineStageFlags &stage, VkImageAspectFlags aspect); static void SetupTransitionToTransferDst(VKRImage &img, VkImageMemoryBarrier &barrier, VkPipelineStageFlags &stage, VkImageAspectFlags aspect); diff --git a/ext/native/thin3d/VulkanRenderManager.cpp b/ext/native/thin3d/VulkanRenderManager.cpp index 88009d666a..c239bdb5ff 100644 --- a/ext/native/thin3d/VulkanRenderManager.cpp +++ b/ext/native/thin3d/VulkanRenderManager.cpp @@ -401,8 +401,41 @@ void VulkanRenderManager::CopyFramebufferToMemorySync(VKRFramebuffer *src, int a FlushSync(); - // Need to call this after FlushSyfnc so the pixels are guaranteed to be ready in CPU-accessible VRAM. - queueRunner_.CopyReadbackBuffer(w, h, destFormat, pixelStride, pixels); + Draw::DataFormat srcFormat; + if (aspectBits & VK_IMAGE_ASPECT_COLOR_BIT) { + switch (src->color.format) { + case VK_FORMAT_R8G8B8A8_UNORM: srcFormat = Draw::DataFormat::R8G8B8A8_UNORM; break; + default: assert(false); + } + } else if (aspectBits & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) { + switch (src->depth.format) { + case VK_FORMAT_D24_UNORM_S8_UINT: srcFormat = Draw::DataFormat::D24_S8; break; + case VK_FORMAT_D32_SFLOAT_S8_UINT: srcFormat = Draw::DataFormat::D32F_S8; break; + // TODO: Wrong. + case VK_FORMAT_D16_UNORM_S8_UINT: srcFormat = Draw::DataFormat::D16; break; + default: assert(false); + } + } else { + assert(false); + } + // Need to call this after FlushSync so the pixels are guaranteed to be ready in CPU-accessible VRAM. + queueRunner_.CopyReadbackBuffer(w, h, srcFormat, destFormat, pixelStride, pixels); +} + +void VulkanRenderManager::CopyImageToMemorySync(VkImage image, int mipLevel, int x, int y, int w, int h, Draw::DataFormat destFormat, uint8_t *pixels, int pixelStride) { + VKRStep *step = new VKRStep{ VKRStepType::READBACK_IMAGE }; + step->readback_image.image = image; + step->readback_image.srcRect.offset = { x, y }; + step->readback_image.srcRect.extent = { (uint32_t)w, (uint32_t)h }; + step->readback_image.mipLevel = mipLevel; + steps_.push_back(step); + + curRenderStep_ = nullptr; + + FlushSync(); + + // Need to call this after FlushSync so the pixels are guaranteed to be ready in CPU-accessible VRAM. + queueRunner_.CopyReadbackBuffer(w, h, destFormat, destFormat, pixelStride, pixels); } void VulkanRenderManager::InitBackbufferFramebuffers(int width, int height) { @@ -635,6 +668,10 @@ void VulkanRenderManager::Wipe() { case VKRStepType::READBACK: step->readback.src->Release(); break; + case VKRStepType::READBACK_IMAGE: + break; + default: + assert(false); } delete step; } diff --git a/ext/native/thin3d/VulkanRenderManager.h b/ext/native/thin3d/VulkanRenderManager.h index 22f2649da0..607beaf9ab 100644 --- a/ext/native/thin3d/VulkanRenderManager.h +++ b/ext/native/thin3d/VulkanRenderManager.h @@ -102,6 +102,7 @@ public: void BindFramebufferAsRenderTarget(VKRFramebuffer *fb, VKRRenderPassAction color, VKRRenderPassAction depth, uint32_t clearColor, float clearDepth, uint8_t clearStencil); VkImageView BindFramebufferAsTexture(VKRFramebuffer *fb, int binding, int aspectBit, int attachment); void CopyFramebufferToMemorySync(VKRFramebuffer *src, int aspectBits, int x, int y, int w, int h, Draw::DataFormat destFormat, uint8_t *pixels, int pixelStride); + void CopyImageToMemorySync(VkImage image, int mipLevel, int x, int y, int w, int h, Draw::DataFormat destFormat, uint8_t *pixels, int pixelStride); void CopyFramebuffer(VKRFramebuffer *src, VkRect2D srcRect, VKRFramebuffer *dst, VkOffset2D dstPos, int aspectMask); void BlitFramebuffer(VKRFramebuffer *src, VkRect2D srcRect, VKRFramebuffer *dst, VkRect2D dstRect, int aspectMask, VkFilter filter); diff --git a/ext/native/thin3d/thin3d.h b/ext/native/thin3d/thin3d.h index bd1a25d99a..9373489833 100644 --- a/ext/native/thin3d/thin3d.h +++ b/ext/native/thin3d/thin3d.h @@ -644,12 +644,6 @@ protected: int targetHeight_; }; -size_t DataFormatSizeInBytes(DataFormat fmt); -bool DataFormatIsDepthStencil(DataFormat fmt); -inline bool DataFormatIsColor(DataFormat fmt) { - return !DataFormatIsDepthStencil(fmt); -} - DrawContext *T3DCreateGLContext(); extern const UniformBufferDesc UBPresetDesc; From fc32a7b24efb7a80651d261d643201d9fae8e622 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 Nov 2017 18:04:56 -0800 Subject: [PATCH 4/5] Vulkan: Show textures in GE debugger. --- Common/Vulkan/VulkanImage.h | 3 +- GPU/Vulkan/TextureCacheVulkan.cpp | 60 +++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/Common/Vulkan/VulkanImage.h b/Common/Vulkan/VulkanImage.h index cc303a7460..b84913c0d0 100644 --- a/Common/Vulkan/VulkanImage.h +++ b/Common/Vulkan/VulkanImage.h @@ -35,7 +35,6 @@ public: void UploadMip(VkCommandBuffer cmd, int mip, int mipWidth, int mipHeight, VkBuffer buffer, uint32_t offset, size_t rowLength); // rowLength is in pixels void EndCreate(VkCommandBuffer cmd); - int GetNumMips() const { return numMips_; } void Destroy(); // Used in image copies, etc. @@ -46,6 +45,8 @@ public: int32_t GetWidth() const { return tex_width; } int32_t GetHeight() const { return tex_height; } + int32_t GetNumMips() const { return numMips_; } + VkFormat GetFormat() const { return format_; } private: void CreateMappableImage(); diff --git a/GPU/Vulkan/TextureCacheVulkan.cpp b/GPU/Vulkan/TextureCacheVulkan.cpp index 8826278327..20664c7831 100644 --- a/GPU/Vulkan/TextureCacheVulkan.cpp +++ b/GPU/Vulkan/TextureCacheVulkan.cpp @@ -809,6 +809,62 @@ void TextureCacheVulkan::LoadTextureLevel(TexCacheEntry &entry, uint8_t *writePt } bool TextureCacheVulkan::GetCurrentTextureDebug(GPUDebugBuffer &buffer, int level) { - // TODO - return false; + ApplyTexture(); + SetTexture(false); + if (!nextTexture_) + return false; + + // TODO: Centralize? + if (nextTexture_->framebuffer) { + VirtualFramebuffer *vfb = nextTexture_->framebuffer; + bool flipY = g_Config.iGPUBackend == GPU_BACKEND_OPENGL && g_Config.iRenderingMode != FB_NON_BUFFERED_MODE; + buffer.Allocate(vfb->bufferWidth, vfb->bufferHeight, GPU_DBG_FORMAT_8888, flipY); + bool retval = draw_->CopyFramebufferToMemorySync(vfb->fbo, Draw::FB_COLOR_BIT, 0, 0, vfb->bufferWidth, vfb->bufferHeight, Draw::DataFormat::R8G8B8A8_UNORM, buffer.GetData(), vfb->bufferWidth); + // Vulkan requires us to re-apply all dynamic state for each command buffer, and the above will cause us to start a new cmdbuf. + // So let's dirty the things that are involved in Vulkan dynamic state. Readbacks are not frequent so this won't hurt other backends. + gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE); + // We may have blitted to a temp FBO. + framebufferManager_->RebindFramebuffer(); + return retval; + } + + if (!nextTexture_->vkTex || !nextTexture_->vkTex->texture_) + return false; + VulkanTexture *texture = nextTexture_->vkTex->texture_; + VulkanRenderManager *renderManager = (VulkanRenderManager *)draw_->GetNativeObject(Draw::NativeObject::RENDER_MANAGER); + + GPUDebugBufferFormat bufferFormat; + Draw::DataFormat drawFormat; + switch (texture->GetFormat()) { + case VULKAN_565_FORMAT: + bufferFormat = GPU_DBG_FORMAT_565; + drawFormat = Draw::DataFormat::B5G6R5_UNORM_PACK16; + break; + case VULKAN_1555_FORMAT: + bufferFormat = GPU_DBG_FORMAT_5551; + drawFormat = Draw::DataFormat::B5G5R5A1_UNORM_PACK16; + break; + case VULKAN_4444_FORMAT: + bufferFormat = GPU_DBG_FORMAT_4444; + drawFormat = Draw::DataFormat::B4G4R4A4_UNORM_PACK16; + break; + case VULKAN_8888_FORMAT: + default: + bufferFormat = GPU_DBG_FORMAT_8888; + drawFormat = Draw::DataFormat::R8G8B8A8_UNORM; + break; + } + + int w = texture->GetWidth(); + int h = texture->GetHeight(); + buffer.Allocate(w, h, bufferFormat); + + renderManager->CopyImageToMemorySync(texture->GetImage(), level, 0, 0, w, h, drawFormat, (uint8_t *)buffer.GetData(), w); + + // Vulkan requires us to re-apply all dynamic state for each command buffer, and the above will cause us to start a new cmdbuf. + // So let's dirty the things that are involved in Vulkan dynamic state. Readbacks are not frequent so this won't hurt other backends. + gstate_c.Dirty(DIRTY_VIEWPORTSCISSOR_STATE | DIRTY_BLEND_STATE | DIRTY_DEPTHSTENCIL_STATE); + framebufferManager_->RebindFramebuffer(); + + return true; } From 3a84b637777e8307cf4ecbd20537f7fe3d10ef6d Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 Nov 2017 18:40:27 -0800 Subject: [PATCH 5/5] Vulkan: Convert depth format on readback. Although, the GPU debugger stuff already handles more formats, it's hard to get the current format to avoid the conversion... --- ext/native/thin3d/DataFormat.h | 1 + ext/native/thin3d/VulkanQueueRunner.cpp | 6 ++- ext/native/thin3d/VulkanRenderManager.cpp | 8 ++-- ext/native/thin3d/thin3d.cpp | 47 +++++++++++++++++++++++ 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/ext/native/thin3d/DataFormat.h b/ext/native/thin3d/DataFormat.h index bc89107c56..831b34d626 100644 --- a/ext/native/thin3d/DataFormat.h +++ b/ext/native/thin3d/DataFormat.h @@ -73,5 +73,6 @@ inline bool DataFormatIsColor(DataFormat fmt) { } void ConvertFromRGBA8888(uint8_t *dst, const uint8_t *src, uint32_t dstStride, uint32_t srcStride, uint32_t width, uint32_t height, DataFormat format); +void ConvertToD32F(uint8_t *dst, const uint8_t *src, uint32_t dstStride, uint32_t srcStride, uint32_t width, uint32_t height, DataFormat format); } // namespace diff --git a/ext/native/thin3d/VulkanQueueRunner.cpp b/ext/native/thin3d/VulkanQueueRunner.cpp index d06596488c..fbd6dd40fa 100644 --- a/ext/native/thin3d/VulkanQueueRunner.cpp +++ b/ext/native/thin3d/VulkanQueueRunner.cpp @@ -891,7 +891,7 @@ void VulkanQueueRunner::PerformReadbackImage(const VKRStep &step, VkCommandBuffe void VulkanQueueRunner::CopyReadbackBuffer(int width, int height, Draw::DataFormat srcFormat, Draw::DataFormat destFormat, int pixelStride, uint8_t *pixels) { // Read back to the requested address in ram from buffer. void *mappedData; - const size_t srcPixelSize = DataFormatSizeInBytes(destFormat); + const size_t srcPixelSize = DataFormatSizeInBytes(srcFormat); VkResult res = vkMapMemory(vulkan_->GetDevice(), readbackMemory_, 0, width * height * srcPixelSize, 0, &mappedData); assert(res == VK_SUCCESS); @@ -905,8 +905,10 @@ void VulkanQueueRunner::CopyReadbackBuffer(int width, int height, Draw::DataForm src += width * srcPixelSize; dst += pixelStride * srcPixelSize; } + } else if (destFormat == Draw::DataFormat::D32F) { + ConvertToD32F(pixels, (const uint8_t *)mappedData, pixelStride, width, width, height, srcFormat); } else { - // TODO: Maybe depth conversion or something? + // TODO: Maybe a depth conversion or something? assert(false); } vkUnmapMemory(vulkan_->GetDevice(), readbackMemory_); diff --git a/ext/native/thin3d/VulkanRenderManager.cpp b/ext/native/thin3d/VulkanRenderManager.cpp index c239bdb5ff..773a7fa59b 100644 --- a/ext/native/thin3d/VulkanRenderManager.cpp +++ b/ext/native/thin3d/VulkanRenderManager.cpp @@ -407,11 +407,13 @@ void VulkanRenderManager::CopyFramebufferToMemorySync(VKRFramebuffer *src, int a case VK_FORMAT_R8G8B8A8_UNORM: srcFormat = Draw::DataFormat::R8G8B8A8_UNORM; break; default: assert(false); } - } else if (aspectBits & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) { + } else if (aspectBits & VK_IMAGE_ASPECT_STENCIL_BIT) { + // Copies from stencil are always S8. + srcFormat = Draw::DataFormat::S8; + } else if (aspectBits & VK_IMAGE_ASPECT_DEPTH_BIT) { switch (src->depth.format) { case VK_FORMAT_D24_UNORM_S8_UINT: srcFormat = Draw::DataFormat::D24_S8; break; - case VK_FORMAT_D32_SFLOAT_S8_UINT: srcFormat = Draw::DataFormat::D32F_S8; break; - // TODO: Wrong. + case VK_FORMAT_D32_SFLOAT_S8_UINT: srcFormat = Draw::DataFormat::D32F; break; case VK_FORMAT_D16_UNORM_S8_UINT: srcFormat = Draw::DataFormat::D16; break; default: assert(false); } diff --git a/ext/native/thin3d/thin3d.cpp b/ext/native/thin3d/thin3d.cpp index 39bdc447bc..70a7fb0feb 100644 --- a/ext/native/thin3d/thin3d.cpp +++ b/ext/native/thin3d/thin3d.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -13,6 +14,7 @@ size_t DataFormatSizeInBytes(DataFormat fmt) { case DataFormat::R8G8_UNORM: return 2; case DataFormat::R8G8B8_UNORM: return 3; + case DataFormat::R4G4_UNORM_PACK8: return 1; case DataFormat::R4G4B4A4_UNORM_PACK16: return 2; case DataFormat::B4G4R4A4_UNORM_PACK16: return 2; case DataFormat::A4R4G4B4_UNORM_PACK16: return 2; @@ -38,6 +40,13 @@ size_t DataFormatSizeInBytes(DataFormat fmt) { case DataFormat::R32G32B32_FLOAT: return 12; case DataFormat::R32G32B32A32_FLOAT: return 16; + case DataFormat::S8: return 1; + case DataFormat::D16: return 2; + case DataFormat::D24_S8: return 4; + case DataFormat::D32F: return 4; + // Or maybe 8... + case DataFormat::D32F_S8: return 5; + default: return 0; } @@ -372,5 +381,43 @@ void ConvertFromRGBA8888(uint8_t *dst, const uint8_t *src, uint32_t dstStride, u } } +void ConvertToD32F(uint8_t *dst, const uint8_t *src, uint32_t dstStride, uint32_t srcStride, uint32_t width, uint32_t height, DataFormat format) { + if (format == Draw::DataFormat::D32F) { + const float *src32 = (const float *)src; + float *dst32 = (float *)dst; + if (src == dst) { + return; + } else { + for (uint32_t y = 0; y < height; ++y) { + memcpy(dst32, src32, width * 4); + src32 += srcStride; + dst32 += dstStride; + } + } + } else if (format == Draw::DataFormat::D16) { + const uint16_t *src16 = (const uint16_t *)src; + float *dst32 = (float *)dst; + for (uint32_t y = 0; y < height; ++y) { + for (uint32_t x = 0; x < width; ++x) { + dst32[x] = (float)(int)src16[x] / 65535.0f; + } + src16 += srcStride; + dst32 += dstStride; + } + } else if (format == Draw::DataFormat::D24_S8) { + const uint32_t *src32 = (const uint32_t *)src; + float *dst32 = (float *)dst; + for (uint32_t y = 0; y < height; ++y) { + for (uint32_t x = 0; x < width; ++x) { + dst32[x] = (src32[x] & 0x00FFFFFF) / 16777215.0f; + } + src32 += srcStride; + dst32 += dstStride; + } + } else { + assert(false); + } +} + } // namespace Draw