LunarG / vktrace

Vulkan vktrace / vkreplay
Other
6 stars 8 forks source link

Memory sync problem in vktrace on Linux/Android #24

Open justuswang opened 6 years ago

justuswang commented 6 years ago

Is there any way to distinguish memory read and memory write like the pageguard on Windows? So we can detect memory read to the copy of mapped memory and do a sync from the real mapped memory to the copy of that memory. I haven't found a way to do it using mprotect and exception handler.

The default configuration of vktrace is not able to know there's a read to a mapped memory on Linux and Android platforms. Which means it is not going to sync data back from the real mapped memory (which was mapped via vkMapMemory) to the copy of that memory for Vulkan application to read.

This is the default behavior when PAGEGUARD_ADD_PAGEGUARD_ON_REAL_MAPPED_MEMORY is disabled in vktrace/vktrace_layer/vktrace_lib_pageguardcapture.h.

And it will cause problem when tracing a title which needs to read back rendering results for environment probes.

davidlunarg commented 6 years ago

This is a shortcoming of Linux that we haven't figured out how to work around. We are simply not able to accurately trace an app where a graphics processor modifies a mapped buffer and the app reads that buffer.

justuswang commented 6 years ago

I have tried with following workaround locally and it works with an unpublished demo.

Sync real mapped memory to the copy of that memory after vkCmdCopyImageToBuffer being executed. The real mapped memory is bound with the destination buffer.

The sync (memcpy) is added in __HOOKED_vkWaitForFences when a fence from vkQueueSubmit is signaled and the command buffer submitted contains vkCmdCopyImageToBuffer command.

This will at least make it possible for an app to read the correct buffer data back. It won't solve memory read problem when a linear image's memory is mapped to CPU memory and application wants to read from that CPU memory. But I think for an application, it is expected to always read from buffer instead of read from image. So the workaround should work for most applications which need to read buffer back.

Is this workaround acceptable? Not sure if I should create a PR for it since it is only a workaround and not able to fully resolve the memory read detection issue on Linux/Android.

justuswang commented 6 years ago

Created PR LunarG/VulkanTools#520 in case this workaround is useful for other users.