-
Notifications
You must be signed in to change notification settings - Fork 6
Disable external compression #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -369,3 +369,23 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_vkCreateImage<user_tag>(VkDevice device, | |
| const VkImageCreateInfo* pCreateInfo, | ||
| const VkAllocationCallbacks* pAllocator, | ||
| VkImage* pImage); | ||
|
|
||
|
|
||
| // Functions for swapchains | ||
|
|
||
| /* See Vulkan API for documentation. */ | ||
| template <> | ||
| VKAPI_ATTR VkResult VKAPI_CALL layer_vkCreateSwapchainKHR<user_tag>(VkDevice device, | ||
| const VkSwapchainCreateInfoKHR* pCreateInfo, | ||
| const VkAllocationCallbacks* pAllocator, | ||
| VkSwapchainKHR* pSwapchain | ||
|
||
| ); | ||
|
|
||
| // Functions for external DMA-buf | ||
|
|
||
| template <> | ||
| VKAPI_ATTR VkResult VKAPI_CALL layer_vkAllocateMemory<user_tag>(VkDevice device, | ||
| const VkMemoryAllocateInfo* pAllocateInfo, | ||
| const VkAllocationCallbacks* pAllocator, | ||
| VkDeviceMemory* pMemory | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /* | ||
| * SPDX-License-Identifier: MIT | ||
| * ---------------------------------------------------------------------------- | ||
| * Copyright (c) 2025 Arm Limited | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to | ||
| * deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| * sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
| * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||
| * IN THE SOFTWARE. | ||
| * ---------------------------------------------------------------------------- | ||
| */ | ||
| #include <vulkan/utility/vk_safe_struct.hpp> | ||
| #include <vulkan/utility/vk_struct_helper.hpp> | ||
|
|
||
| #include "device.hpp" | ||
| #include "framework/device_dispatch_table.hpp" | ||
|
|
||
| #include <bit> | ||
| #include <mutex> | ||
|
|
||
| extern std::mutex g_vulkanLock; | ||
|
|
||
| /** | ||
| * @brief Intercept vkAllocateMemory and hard-fail on external memory imports | ||
| * that could mandate external compression; otherwise pass through to the driver. | ||
| * | ||
| * @details Scans @p pAllocateInfo->pNext for import structs. If it finds | ||
| * VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID or | ||
| * VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR with | ||
| * VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, it returns | ||
| * VK_ERROR_FEATURE_NOT_PRESENT to preserve the guarantee that | ||
| * external compression is not used. When no such imports are present, the | ||
| * call is forwarded unchanged. | ||
| * | ||
| * @param device Logical device allocating the memory. | ||
| * @param pAllocateInfo Allocation parameters; its pNext chain is inspected | ||
| * for import info. | ||
| * @param pAllocator Optional allocation callbacks. | ||
| * @param pMemory Receives the allocated device memory on success. | ||
| * | ||
| * @return VK_SUCCESS on success, or a VkResult propagated from the driver. | ||
| * Returns VK_ERROR_FEATURE_NOT_PRESENT when disallowed external | ||
| * import types are detected in the pNext chain. | ||
| * | ||
| * @note This is a strict policy check; additional import structures may be | ||
| * rejected in the future to maintain the compression-off guarantee. | ||
| */ | ||
|
|
||
| /* See Vulkan API for documentation. */ | ||
| template <> | ||
| VKAPI_ATTR VkResult VKAPI_CALL layer_vkAllocateMemory<user_tag>( | ||
| VkDevice device, | ||
| const VkMemoryAllocateInfo* pAllocateInfo, | ||
| const VkAllocationCallbacks* pAllocator, | ||
| VkDeviceMemory* pMemory | ||
| ) { | ||
|
|
||
marleo02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| LAYER_TRACE(__func__); | ||
|
|
||
| //fprintf(stderr, "[libGPULayers] HIT %s\n", __func__); fflush(stderr); | ||
marleo02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| std::unique_lock<std::mutex> lock { g_vulkanLock }; | ||
| auto* layer = Device::retrieve(device); | ||
| const auto& config = layer->instance->config; | ||
|
|
||
| int disable_external_compression = config.disable_external_compression(); | ||
|
|
||
| //absolute passthrough if feature is off | ||
marleo02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (disable_external_compression == 0) { | ||
| lock.unlock(); | ||
| return layer->driver.vkAllocateMemory(device, pAllocateInfo, pAllocator, pMemory); | ||
| } | ||
|
|
||
| // Scan pNext for imports that we cannot sanitize -> hard-fail to keep the 100% guarantee that we can disable external compression | ||
| for (const VkBaseInStructure* n = (const VkBaseInStructure*)pAllocateInfo->pNext; n; n = n->pNext) { | ||
marleo02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (n->sType == VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID) { | ||
| printf("[libGPULayers] vkAllocateMemory: AHardwareBuffer IMPORT detected. " | ||
marleo02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "Cannot guarantee external compression is disabled (buffer created outside Vulkan). Failing by policy.\n"); | ||
| return VK_ERROR_FEATURE_NOT_PRESENT; | ||
| } | ||
|
|
||
| if (n->sType == VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR) { | ||
| const VkImportMemoryFdInfoKHR* fdInfo = (const VkImportMemoryFdInfoKHR*)n; | ||
| if (fdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT) { | ||
| printf("[libGPULayers] vkAllocateMemory: DMA-BUF memory IMPORT detected (fd=%d). " | ||
| "Cannot guarantee external compression (DRM modifier may imply compression). Failing by policy.\n", | ||
| fdInfo->fd); | ||
| return VK_ERROR_FEATURE_NOT_PRESENT; | ||
| } | ||
| } | ||
|
|
||
marleo02 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Release the lock to call into the driver | ||
| lock.unlock(); | ||
| return layer->driver.vkAllocateMemory(device, pAllocateInfo, pAllocator, pMemory); | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.