From fda618618c224c31559b2e295fcc05aec17ea710 Mon Sep 17 00:00:00 2001 From: wallentx Date: Sun, 11 Oct 2026 04:01:08 -0500 Subject: [PATCH] Bundle verified PowerVR graphics components through existing build preparation --- .github/workflows/build.yml | 1 + .gitignore | 1 + tools/build_local.sh | 1 + tools/dxvk/build.sh | 31 ++ tools/dxvk/check_present_sync.py | 116 ++++ .../0001-check-direct3d-bc-formats.patch | 53 ++ ...resent-semaphore-per-swapchain-image.patch | 26 + tools/dxvk/source.env | 1 + tools/hybris/build_runtime.sh | 51 ++ .../hybris/patches/0001-dxvk-bc-profile.patch | 505 ++++++++++++++++++ tools/hybris/powervr-hal-version.patch | 12 + tools/hybris/run-proton.py | 83 +++ tools/hybris/source.env | 2 + tools/powervr/fetch.py | 316 +++++++++++ tools/powervr/release.json | 250 +++++++++ tools/release/check_session_assets.py | 11 + tools/release/runtime_inputs.py | 13 +- tools/tests/test_powervr_runtime.py | 224 ++++++++ tools/tests/test_runtime_inputs.py | 19 + tools/vulkan-wsi/build.sh | 34 ++ .../licenses/Vulkan-Headers/LICENSE.md | 19 + .../licenses/Vulkan-Headers/MIT.txt | 9 + .../0001-link-x11-external-memory.patch | 16 + tools/vulkan-wsi/source.env | 2 + 24 files changed, 1795 insertions(+), 1 deletion(-) create mode 100755 tools/dxvk/build.sh create mode 100644 tools/dxvk/check_present_sync.py create mode 100644 tools/dxvk/patches/0001-check-direct3d-bc-formats.patch create mode 100644 tools/dxvk/patches/0002-present-semaphore-per-swapchain-image.patch create mode 100644 tools/dxvk/source.env create mode 100755 tools/hybris/build_runtime.sh create mode 100644 tools/hybris/patches/0001-dxvk-bc-profile.patch create mode 100644 tools/hybris/powervr-hal-version.patch create mode 100644 tools/hybris/run-proton.py create mode 100644 tools/hybris/source.env create mode 100644 tools/powervr/fetch.py create mode 100644 tools/powervr/release.json create mode 100644 tools/tests/test_powervr_runtime.py create mode 100755 tools/vulkan-wsi/build.sh create mode 100644 tools/vulkan-wsi/licenses/Vulkan-Headers/LICENSE.md create mode 100644 tools/vulkan-wsi/licenses/Vulkan-Headers/MIT.txt create mode 100644 tools/vulkan-wsi/patches/0001-link-x11-external-memory.patch create mode 100644 tools/vulkan-wsi/source.env diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 62fda2a5e0..20b85261b4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -457,6 +457,7 @@ jobs: SGDB_API_KEY: ${{ secrets.SGDB_API_KEY }} run: | set -o pipefail + python3 tools/powervr/fetch.py python3 tools/release/runtime_inputs.py record ./gradlew assembleRelease --console=plain -PndkVersion="$NDK_VERSION" 2>&1 | tee /tmp/build.log python3 tools/release/check_session_assets.py app/build/outputs/apk/release/app-release.apk diff --git a/.gitignore b/.gitignore index a9e068c0d1..91530dc111 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ __pycache__/ # Local signing with the release key (tools/build_local.sh); holds a password. .signing.env app/src/main/assets/directaudio/ +app/src/main/assets/hybris/ diff --git a/tools/build_local.sh b/tools/build_local.sh index 836a2c52d7..36bceae269 100755 --- a/tools/build_local.sh +++ b/tools/build_local.sh @@ -316,6 +316,7 @@ else fi cd "${repo_root}" +python3 tools/powervr/fetch.py python3 tools/release/runtime_inputs.py record ./gradlew "${gradle_task}" --console=plain -PndkVersion="${ndk_version}" ${gradle_rust_arg} python3 tools/release/check_session_assets.py "app/build/outputs/apk/${build_variant}/app-${build_variant}.apk" diff --git a/tools/dxvk/build.sh b/tools/dxvk/build.sh new file mode 100755 index 0000000000..8243ec0fd4 --- /dev/null +++ b/tools/dxvk/build.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash +# CI component build; this does not change the APK or an installed Proton depot. +set -euo pipefail +root=$(cd "$(dirname "$0")/../.." && pwd) +. "$root/tools/dxvk/source.env" +mkdir -p "${1:?output directory required}" +out=$(cd "$1" && pwd) +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +git init -q "$work/source" +git -C "$work/source" fetch --depth=1 https://github.com/doitsujin/dxvk.git "$DXVK_REVISION" +git -C "$work/source" checkout -q --detach FETCH_HEAD +test "$(git -C "$work/source" rev-parse HEAD)" = "$DXVK_REVISION" +git -C "$work/source" submodule update --init --recursive --depth=1 +for patch in "$root"/tools/dxvk/patches/*.patch; do + git -C "$work/source" apply --check "$patch" + git -C "$work/source" apply "$patch" +done +python3 "$root/tools/dxvk/check_present_sync.py" "$work/source/src/dxvk/dxvk_presenter.cpp" +for arch in 32 64; do + meson setup "$work/build$arch" "$work/source" --cross-file "$work/source/build-win$arch.txt" \ + --buildtype release --prefix "$out" --bindir "x$arch" --libdir "x$arch" -Db_ndebug=if-release --strip + meson compile -C "$work/build$arch" -j 4 + meson install -C "$work/build$arch" +done +cp "$work/source/LICENSE" "$out/LICENSE" +printf '%s\n' "$DXVK_REVISION" > "$out/source-commit" +git -C "$work/source" submodule status --recursive > "$out/submodules.txt" +git -C "$work/source" diff > "$out/downstream.patch" +tar --exclude-vcs --zstd -cf "$out/source.tar.zst" -C "$work/source" . +(cd "$out" && find x32 x64 -name '*.dll' -type f -print0 | sort -z | xargs -0 sha256sum > SHA256SUMS) diff --git a/tools/dxvk/check_present_sync.py b/tools/dxvk/check_present_sync.py new file mode 100644 index 0000000000..ad47a8aded --- /dev/null +++ b/tools/dxvk/check_present_sync.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Replay asynchronous WSI ownership using the pinned presenter's actual selections. + +This checks semaphore selection, not the full Vulkan presenter. Real Vulkan +validation and device testing remain separate gates. Requires a native C++ compiler. +""" +import argparse +import os +from pathlib import Path +import re +import subprocess +import tempfile + + +def selection_code(source): + match = re.search( + r"// Return relevant Vulkan objects for the acquired image\s*" + r"(?P.*?)\s*m_presentPending = true;", source, re.S) + wait = re.search(r"info\.pWaitSemaphores\s*=.*?;", source, re.S) + if not match or not wait: + raise ValueError("unsupported pinned presenter layout") + return match["acquire"], wait[0] + + +HARNESS = r''' +#include +#include +#include +#include +using VkSemaphore = uint64_t; +struct Sync { VkSemaphore acquire, present; }; +struct Info { const VkSemaphore* pWaitSemaphores; }; + +struct Presenter { + std::vector m_semaphores; + std::vector m_images = {0, 1, 2, 3}; + unsigned m_frameIndex = 0, m_imageIndex = 0; + bool m_hasSwapchainMaintenance1; + explicit Presenter(bool maintenance): m_hasSwapchainMaintenance1(maintenance) { + for (unsigned i = 0; i < (maintenance ? 4u : 8u); ++i) + m_semaphores.push_back({100 + i, 1 + i}); + } + Sync acquire() { + Sync sync{}; unsigned image; + ACQUIRE_SELECTION + (void)image; + return sync; + } + VkSemaphore present() { + auto& currSync = m_semaphores.at(m_frameIndex); + Info info{}; + PRESENT_SELECTION + return *info.pWaitSemaphores; + } +}; + +int replay(bool maintenance) { + Presenter p(maintenance); + std::array outstandingByImage{}; + std::array outstandingByFence{}; + // Deliberately delay reacquisition of image 2 beyond a full frame-ring cycle. + const std::array imageOrder = { + 2,0,3,0,1,3,0,1,1,2,3,1,0,3,2,2,0,1,3,0,2 + }; + for (unsigned iteration = 0; iteration < 200; ++iteration) { + p.m_imageIndex = imageOrder[iteration % imageOrder.size()]; + if (maintenance) { + // The existing fence path waits this frame's previous present fence. + const auto completed = outstandingByFence.at(p.m_frameIndex); + for (auto& pending : outstandingByImage) + if (pending == completed) pending = 0; + } + // Acquisition and its GPU wait complete the last present of THIS image. + outstandingByImage.at(p.m_imageIndex) = 0; + const auto signal = p.acquire().present; + const auto wait = p.present(); + if (signal != wait) { + std::fprintf(stderr, "FAIL: render signal differs from present wait\n"); + return 2; + } + for (auto pending : outstandingByImage) { + if (pending == signal) { + std::fprintf(stderr, "FAIL: semaphore reused before presentation completion at frame %u\n", iteration); + return 2; + } + } + outstandingByImage.at(p.m_imageIndex) = signal; + if (maintenance) outstandingByFence.at(p.m_frameIndex) = signal; + p.m_frameIndex = (p.m_frameIndex + 1) % p.m_semaphores.size(); + } + std::printf("PASS: %s, 200 out-of-order acquisitions\n", maintenance ? "present-fence path" : "no-present-fence path"); + return 0; +} +int main() { + if (int status = replay(false)) return status; + return replay(true); +} +''' + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("source", type=Path, help="pinned dxvk_presenter.cpp, after patching") + args = parser.parse_args() + acquire, present = selection_code(args.source.read_text()) + code = HARNESS.replace("ACQUIRE_SELECTION", acquire).replace("PRESENT_SELECTION", present) + with tempfile.TemporaryDirectory(prefix="dxvk-present-sync-") as directory: + cpp = Path(directory) / "replay.cpp" + exe = Path(directory) / "replay" + cpp.write_text(code) + subprocess.run([os.environ.get("HOST_CXX", "c++"), "-std=c++17", "-Wall", "-Wextra", str(cpp), "-o", str(exe)], check=True) + return subprocess.run([str(exe)]).returncode + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/dxvk/patches/0001-check-direct3d-bc-formats.patch b/tools/dxvk/patches/0001-check-direct3d-bc-formats.patch new file mode 100644 index 0000000000..db327f0388 --- /dev/null +++ b/tools/dxvk/patches/0001-check-direct3d-bc-formats.patch @@ -0,0 +1,53 @@ +diff --git a/src/dxvk/dxvk_device_info.cpp b/src/dxvk/dxvk_device_info.cpp +index db09fba..8e9a76e 100644 +--- a/src/dxvk/dxvk_device_info.cpp ++++ b/src/dxvk/dxvk_device_info.cpp +@@ -118,6 +118,17 @@ namespace dxvk { + initQueueProperties(instance, adapter, deviceInfo); + initMemoryProperties(instance, adapter); + ++ m_hasBcFormats = true; ++ for (uint32_t value = VK_FORMAT_BC1_RGBA_UNORM_BLOCK; value <= VK_FORMAT_BC7_SRGB_BLOCK; value++) { ++ VkFormatProperties properties = { }; ++ vk->vkGetPhysicalDeviceFormatProperties(adapter, VkFormat(value), &properties); ++ constexpr VkFormatFeatureFlags required = VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT ++ | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT ++ | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | VK_FORMAT_FEATURE_TRANSFER_DST_BIT; ++ if ((properties.optimalTilingFeatures & required) != required) ++ m_hasBcFormats = false; ++ } ++ + disableUnusedFeatures(instance, safeMode); + + enableFeaturesAndExtensions(); +@@ -785,6 +796,9 @@ namespace dxvk { + } + } + ++ if (!m_hasBcFormats) ++ return std::string("Device does not support the BC formats required by Direct3D"); ++ + if (!m_featuresEnabled.extDescriptorHeap.descriptorHeap && m_properties.core.properties.limits.maxPushConstantsSize < MaxTotalPushDataSize) + return str::format("Device does not support ", MaxTotalPushDataSize, " of push data"); + +@@ -886,7 +900,7 @@ namespace dxvk { + ENABLE_FEATURE(core.features, shaderResourceResidency, false), + ENABLE_FEATURE(core.features, shaderResourceMinLod, false), + ENABLE_FEATURE(core.features, tessellationShader, false), +- ENABLE_FEATURE(core.features, textureCompressionBC, true), ++ ENABLE_FEATURE(core.features, textureCompressionBC, false), + ENABLE_FEATURE(core.features, variableMultisampleRate, false), + ENABLE_FEATURE(core.features, vertexPipelineStoresAndAtomics, false), + ENABLE_FEATURE(core.features, wideLines, false), +diff --git a/src/dxvk/dxvk_device_info.h b/src/dxvk/dxvk_device_info.h +index 0a55f90..8bbc770 100644 +--- a/src/dxvk/dxvk_device_info.h ++++ b/src/dxvk/dxvk_device_info.h +@@ -394,6 +394,7 @@ namespace dxvk { + + DxvkDeviceQueueMapping m_queueMapping = { }; + ++ bool m_hasBcFormats = false; + bool m_hasMeshShader = false; + bool m_hasFmask = false; + diff --git a/tools/dxvk/patches/0002-present-semaphore-per-swapchain-image.patch b/tools/dxvk/patches/0002-present-semaphore-per-swapchain-image.patch new file mode 100644 index 0000000000..bd22bdc52a --- /dev/null +++ b/tools/dxvk/patches/0002-present-semaphore-per-swapchain-image.patch @@ -0,0 +1,26 @@ +diff --git a/src/dxvk/dxvk_presenter.cpp b/src/dxvk/dxvk_presenter.cpp +index 643f7c5..4dc8c4d 100644 +--- a/src/dxvk/dxvk_presenter.cpp ++++ b/src/dxvk/dxvk_presenter.cpp +@@ -161,6 +161,10 @@ namespace dxvk { + + // Return relevant Vulkan objects for the acquired image + sync = m_semaphores.at(m_frameIndex); ++ // Without a present fence, reacquiring this image is the only guarantee ++ // that its previous presentation has consumed the wait semaphore. ++ if (!m_hasSwapchainMaintenance1) ++ sync.present = m_semaphores.at(m_imageIndex).present; + image = m_images.at(m_imageIndex); + + m_presentPending = true; +@@ -239,7 +243,9 @@ namespace dxvk { + + VkPresentInfoKHR info = { VK_STRUCTURE_TYPE_PRESENT_INFO_KHR }; + info.waitSemaphoreCount = 1; +- info.pWaitSemaphores = &currSync.present; ++ info.pWaitSemaphores = m_hasSwapchainMaintenance1 ++ ? &currSync.present ++ : &m_semaphores.at(m_imageIndex).present; + info.swapchainCount = 1; + info.pSwapchains = &m_swapchain; + info.pImageIndices = &m_imageIndex; diff --git a/tools/dxvk/source.env b/tools/dxvk/source.env new file mode 100644 index 0000000000..7aac170c6e --- /dev/null +++ b/tools/dxvk/source.env @@ -0,0 +1 @@ +DXVK_REVISION=25ca63f17f34bdc05a39873ee63907d3cbbfa030 diff --git a/tools/hybris/build_runtime.sh b/tools/hybris/build_runtime.sh new file mode 100755 index 0000000000..53c7d9481e --- /dev/null +++ b/tools/hybris/build_runtime.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# CI-only full libhybris build, using the upstream pinned cross-toolchain recipe. +set -euo pipefail +root=$(cd "$(dirname "$0")/../.." && pwd) +out=${1:?output directory required} +mkdir -p "$out" +out=$(cd "$out" && pwd) +. "$root/tools/hybris/source.env" +work=$(mktemp -d "${TMPDIR:-/tmp}/droiddeck-hybris.XXXXXX") +trap 'rm -rf "$work"' EXIT +fetch() { + git init -q "$3" + git -C "$3" fetch --depth=1 "$1" "$2" + git -C "$3" checkout -q --detach FETCH_HEAD + test "$(git -C "$3" rev-parse HEAD)" = "$2" +} +fetch https://github.com/taowen/libhybris.git "$HYBRIS_REVISION" "$work/libhybris" +fetch https://github.com/taowen/arlinux-rootfs.git "$PROTOCOL_REVISION" "$work/rootfs" +git -C "$work/libhybris" apply --check "$root/tools/hybris/powervr-hal-version.patch" +git -C "$work/libhybris" apply "$root/tools/hybris/powervr-hal-version.patch" +for patch in "$root"/tools/hybris/patches/*.patch; do + git -C "$work/libhybris" apply --check "$patch" + git -C "$work/libhybris" apply "$patch" +done +export ARLINUX_WSI_PROTOCOL_DIR="$work/rootfs/graphics-protocols" +export CONTAINER_ENGINE=podman +"$work/libhybris/tools/build-aarch64.sh" --out "$work/build" + +mkdir -p "$work/package/lib" "$work/package/licenses" +cp -a "$work/build/install/usr/lib/hybris/." "$work/package/lib/" +find "$work/package/lib" -name '*.la' -delete +rm -rf "$work/package/lib/include" "$work/package/lib/pkgconfig" "$work/package/lib/bin" +# Resolve the wrapper's private dependencies without putting its EGL/Vulkan frontends +# on the session's LD_LIBRARY_PATH. The Linux runtime keeps owning its Mesa loader. +while IFS= read -r -d '' library; do + patchelf --set-rpath '$ORIGIN:$ORIGIN/../..' "$library" +done < <(find "$work/package/lib" -type f -name '*.so*' -print0) +for library in libhybris-vulkan-icd.so.0 libhybris-common.so.1 libVkLayer_hybris_compat.so libhybris/linker/q.so; do + test -s "$work/package/lib/$library" +done +cp "$work/libhybris"/LICENSE.* "$work/package/licenses/" +cp "$work/build/manifest.json" "$work/package/build-manifest.json" +printf '%s\n' "$HYBRIS_REVISION" > "$work/package/source-commit" +# Ship the exact patched source and protocol license with the runtime. +git -C "$work/libhybris" diff > "$work/package/licenses/powervr.patch" +git -C "$work/libhybris" archive HEAD | zstd -T0 -q -o "$work/package/licenses/libhybris-source.tar.zst" +cp "$ARLINUX_WSI_PROTOCOL_DIR/wayland-android.xml" "$work/package/licenses/" +(cd "$work/package" && find lib -type f -print0 | sort -z | xargs -0 sha256sum > SHA256SUMS) +tar --zstd -cf "$out/runtime.tzst" -C "$work/package" . +sha256sum "$out/runtime.tzst" | cut -d' ' -f1 > "$out/version.txt" +printf 'Built libhybris %s for Android HAL access from AArch64 Linux\n' "$HYBRIS_REVISION" diff --git a/tools/hybris/patches/0001-dxvk-bc-profile.patch b/tools/hybris/patches/0001-dxvk-bc-profile.patch new file mode 100644 index 0000000000..0076f53f87 --- /dev/null +++ b/tools/hybris/patches/0001-dxvk-bc-profile.patch @@ -0,0 +1,505 @@ +diff --git a/hybris/vulkan/compat/bc_commands.c b/hybris/vulkan/compat/bc_commands.c +index 90dac59..cf62d7d 100644 +--- a/hybris/vulkan/compat/bc_commands.c ++++ b/hybris/vulkan/compat/bc_commands.c +@@ -2,6 +2,7 @@ + #define VK_NO_PROTOTYPES + #include "bc_context.h" + #include ++#include + + #define PROC(name) PFN_vk##name name = (PFN_vk##name)device->resolver(device->handle, "vk" #name) + static VkResult VKAPI_CALL allocate_commands(VkDevice handle, const VkCommandBufferAllocateInfo *info, +@@ -76,6 +77,7 @@ static VkResult VKAPI_CALL end_command(VkCommandBuffer handle) + PROC(EndCommandBuffer); + VkResult result = EndCommandBuffer(handle); + VkResult recorded = command->state.error; ++ if (result != VK_SUCCESS || recorded != VK_SUCCESS) fprintf(stderr, "HYBRIS_BC end: native=%d recorded=%d\n", result, recorded); + if (recorded == VK_SUCCESS) return result; + if (recorded == VK_ERROR_OUT_OF_HOST_MEMORY || recorded == VK_ERROR_OUT_OF_DEVICE_MEMORY || + recorded == VK_ERROR_DEVICE_LOST) return recorded; +@@ -198,6 +200,37 @@ static void VKAPI_CALL push_constants(VkCommandBuffer handle, VkPipelineLayout l + CmdPushConstants(handle, layout, stages, offset, size, values); + if (device->format_mask) hybris_bc_save_push(&command->state, layout, stages, offset, size, values); + } ++static void bind_descriptors2(VkCommandBuffer handle, const VkBindDescriptorSetsInfo *info, const char *name) ++{ ++ struct hybris_bc_device *device; ++ struct hybris_bc_command *command = hybris_bc_command_find(handle, &device); ++ if (!command) return; ++ PFN_vkCmdBindDescriptorSets2 call = (void *)device->resolver(device->handle, name); ++ call(handle, info); ++ if (device->format_mask && (info->stageFlags & VK_SHADER_STAGE_COMPUTE_BIT)) ++ hybris_bc_save_descriptors(&command->state, VK_PIPELINE_BIND_POINT_COMPUTE, ++ info->layout, info->firstSet, info->descriptorSetCount, info->pDescriptorSets, ++ info->dynamicOffsetCount, info->pDynamicOffsets); ++} ++static void VKAPI_CALL bind_descriptors2_core(VkCommandBuffer handle, const VkBindDescriptorSetsInfo *info) ++{ bind_descriptors2(handle, info, "vkCmdBindDescriptorSets2"); } ++static void VKAPI_CALL bind_descriptors2_khr(VkCommandBuffer handle, const VkBindDescriptorSetsInfo *info) ++{ bind_descriptors2(handle, info, "vkCmdBindDescriptorSets2KHR"); } ++static void push_constants2(VkCommandBuffer handle, const VkPushConstantsInfo *info, const char *name) ++{ ++ struct hybris_bc_device *device; ++ struct hybris_bc_command *command = hybris_bc_command_find(handle, &device); ++ if (!command) return; ++ PFN_vkCmdPushConstants2 call = (void *)device->resolver(device->handle, name); ++ call(handle, info); ++ if (device->format_mask) ++ hybris_bc_save_push(&command->state, info->layout, info->stageFlags, ++ info->offset, info->size, info->pValues); ++} ++static void VKAPI_CALL push_constants2_core(VkCommandBuffer handle, const VkPushConstantsInfo *info) ++{ push_constants2(handle, info, "vkCmdPushConstants2"); } ++static void VKAPI_CALL push_constants2_khr(VkCommandBuffer handle, const VkPushConstantsInfo *info) ++{ push_constants2(handle, info, "vkCmdPushConstants2KHR"); } + PFN_vkVoidFunction hybris_bc_commands_proc(const char *name) + { + static const struct { const char *name; PFN_vkVoidFunction function; } commands[] = { +@@ -207,7 +240,9 @@ PFN_vkVoidFunction hybris_bc_commands_proc(const char *name) + ENTRY(ResetCommandBuffer, reset_command), ENTRY(ResetCommandPool, reset_pool), + ENTRY(DestroyCommandPool, destroy_pool), ENTRY(CreatePipelineLayout, create_layout), + ENTRY(DestroyPipelineLayout, destroy_layout), ENTRY(CmdBindPipeline, bind_pipeline), +- ENTRY(CmdBindDescriptorSets, bind_descriptors), ENTRY(CmdPushConstants, push_constants) ++ ENTRY(CmdBindDescriptorSets, bind_descriptors), ENTRY(CmdPushConstants, push_constants), ++ ENTRY(CmdBindDescriptorSets2, bind_descriptors2_core), ENTRY(CmdBindDescriptorSets2KHR, bind_descriptors2_khr), ++ ENTRY(CmdPushConstants2, push_constants2_core), ENTRY(CmdPushConstants2KHR, push_constants2_khr) + #undef ENTRY + }; + for (size_t i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i) +diff --git a/hybris/vulkan/compat/bc_image.c b/hybris/vulkan/compat/bc_image.c +index fe25344..44faaf6 100644 +--- a/hybris/vulkan/compat/bc_image.c ++++ b/hybris/vulkan/compat/bc_image.c +@@ -5,6 +5,19 @@ + + #define PROC(name) PFN_vk##name name = (PFN_vk##name)image->resolver(image->device, "vk" #name) + ++int hybris_bc_srgb_family(VkFormat format) ++{ ++ return (format >= VK_FORMAT_BC1_RGB_UNORM_BLOCK && format <= VK_FORMAT_BC3_SRGB_BLOCK) || ++ format == VK_FORMAT_BC7_UNORM_BLOCK || format == VK_FORMAT_BC7_SRGB_BLOCK; ++} ++int hybris_bc_view_compatible(VkFormat image, VkFormat view) ++{ ++ if (image == view) return 1; ++ return hybris_bc_srgb_family(image) && ++ ((image - VK_FORMAT_BC1_RGB_UNORM_BLOCK) / 2 == ++ (view - VK_FORMAT_BC1_RGB_UNORM_BLOCK) / 2) && hybris_bc_srgb_family(view); ++} ++ + VkFormat hybris_bc_image_format(VkFormat format, unsigned rgb_mask) + { + switch (format) { +@@ -71,19 +84,28 @@ VkResult hybris_bc_image_describe(const VkImageCreateInfo *info, struct hybris_b + info->imageType != VK_IMAGE_TYPE_2D || info->tiling != VK_IMAGE_TILING_OPTIMAL || + info->samples != VK_SAMPLE_COUNT_1_BIT || + (info->initialLayout != VK_IMAGE_LAYOUT_UNDEFINED && info->initialLayout != VK_IMAGE_LAYOUT_PREINITIALIZED) || +- (info->flags & ~VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT) || ++ (info->flags & ~(VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT | VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) || ++ ((info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) && !hybris_bc_srgb_family(info->format)) || + !info->usage || + (info->usage & ~(VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT)) || + !info->extent.width || !info->extent.height || info->extent.depth != 1 || + !info->mipLevels || info->mipLevels > 32 || !info->arrayLayers) + return VK_ERROR_FORMAT_NOT_SUPPORTED; ++ image->flags = info->flags; ++ image->view_mask = (info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) ? 3 : 1; + /* These chains only constrain views or request no external/swapchain + * binding. The decoded image has the same view restriction in our wrapper. */ + for (const VkBaseInStructure *next = info->pNext; next; next = next->pNext) { + if (next->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) { + const VkImageFormatListCreateInfo *list = (const void *)next; +- for (uint32_t i = 0; i < list->viewFormatCount; ++i) +- if (list->pViewFormats[i] != info->format) return VK_ERROR_FORMAT_NOT_SUPPORTED; ++ if (list->viewFormatCount) image->view_mask = 0; ++ for (uint32_t i = 0; i < list->viewFormatCount; ++i) { ++ VkFormat view = list->pViewFormats[i]; ++ if (!hybris_bc_view_compatible(info->format, view) || ++ (view != info->format && !(info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT))) ++ return VK_ERROR_FORMAT_NOT_SUPPORTED; ++ image->view_mask |= view == info->format ? 1u : 2u; ++ } + } else if (next->sType == VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO) { + if (((const VkExternalMemoryImageCreateInfo *)next)->handleTypes) + return VK_ERROR_FORMAT_NOT_SUPPORTED; +diff --git a/hybris/vulkan/compat/bc_image.h b/hybris/vulkan/compat/bc_image.h +index 9e62eb3..c950cc2 100644 +--- a/hybris/vulkan/compat/bc_image.h ++++ b/hybris/vulkan/compat/bc_image.h +@@ -12,10 +12,14 @@ struct hybris_bc_image { + VkMemoryRequirements requirements; + VkBool32 requires_dedicated, prefers_dedicated; + VkFormat format, decoded_format; ++ VkImageCreateFlags flags; ++ unsigned view_mask; + VkExtent3D extent; + uint32_t mip_levels, layers, block_bytes; + VkDeviceSize mip_offset[32], block_size; + }; ++int hybris_bc_srgb_family(VkFormat format); ++int hybris_bc_view_compatible(VkFormat image, VkFormat view); + VkFormat hybris_bc_image_format(VkFormat format, unsigned rgb_mask); + VkResult hybris_bc_image_describe(const VkImageCreateInfo *info, struct hybris_bc_image *image); + void hybris_bc_image_backing_info(const struct hybris_bc_image *image, +diff --git a/hybris/vulkan/compat/bc_policy.c b/hybris/vulkan/compat/bc_policy.c +index c5856f6..3124a25 100644 +--- a/hybris/vulkan/compat/bc_policy.c ++++ b/hybris/vulkan/compat/bc_policy.c +@@ -5,6 +5,7 @@ + #include "bc_context.h" + #include "../layer/layer.h" + #include ++#include + #include + #include + +@@ -16,12 +17,18 @@ static void initialize_policy(void) + const char *value = getenv("HYBRIS_BC_TEXTURES"); + if (!value || !strcmp(value, "missing") || !strcmp(value, "1")) policy = 1; + else if (value && !strcmp(value, "force")) policy = 2; ++ else if (value && !strcmp(value, "dxvk")) policy = 3; + } + int hybris_bc_enabled(void) + { + pthread_once(&policy_once, initialize_policy); + return policy != 0; + } ++static int dxvk_profile(void) ++{ ++ pthread_once(&policy_once, initialize_policy); ++ return policy == 3; ++} + #define PHYSICAL_PROC(name) PFN_vk##name name = (PFN_vk##name)context.resolver(context.instance, "vk" #name) + /* Preserve three-channel border replacement when the native image format + * supports the full sampled/transfer domain. Selection is per physical device; +@@ -52,7 +59,7 @@ unsigned hybris_bc_physical_mask(VkPhysicalDevice physical) + VkPhysicalDeviceProperties properties; + GetPhysicalDeviceProperties(physical, &properties); + /* Core 1.4 includes descriptor-state commands that are not intercepted yet. */ +- if (properties.apiVersion < VK_API_VERSION_1_1 || properties.apiVersion >= VK_MAKE_API_VERSION(0, 1, 4, 0) || ++ if (properties.apiVersion < VK_API_VERSION_1_1 || (!dxvk_profile() && properties.apiVersion >= VK_MAKE_API_VERSION(0, 1, 4, 0)) || + properties.limits.maxStorageBufferRange < 256 || + properties.limits.minStorageBufferOffsetAlignment >= properties.limits.maxStorageBufferRange) return 0; + uint32_t count = 0; +@@ -72,6 +79,9 @@ unsigned hybris_bc_physical_mask(VkPhysicalDevice physical) + unsigned mask = 0; + for (unsigned i = 0; i < 16; ++i) { + VkFormat format = VK_FORMAT_BC1_RGB_UNORM_BLOCK + i; ++ /* D3D uses BC1 RGBA. Keep RGB-only BC1 unadvertised until absent-alpha ++ * border semantics are implemented on devices without RGB8 images. */ ++ if (dxvk_profile() && i < 2) continue; + VkFormat backing = hybris_bc_image_format(format, physical_rgb_mask(physical)); + if (backing == VK_FORMAT_UNDEFINED) continue; + VkFormatProperties native, decoded; +@@ -84,6 +94,33 @@ unsigned hybris_bc_physical_mask(VkPhysicalDevice physical) + } + return mask; + } ++/* The emulation profile implements Vulkan 1.3 plus maintenance6. Do not expose ++ * 1.4 core host-copy/push-descriptor operations that bypass our state tracking. */ ++static void cap_properties(VkPhysicalDevice physical, VkPhysicalDeviceProperties *out) ++{ ++ if (dxvk_profile() && hybris_bc_physical_mask(physical) && out->apiVersion > VK_API_VERSION_1_3) ++ out->apiVersion = VK_API_VERSION_1_3; ++} ++static void VKAPI_CALL properties(VkPhysicalDevice physical, VkPhysicalDeviceProperties *out) ++{ ++ struct hybris_layer_physical context; ++ if (!hybris_layer_lookup_physical(physical, &context)) return; ++ PHYSICAL_PROC(GetPhysicalDeviceProperties); ++ GetPhysicalDeviceProperties(physical, out); ++ cap_properties(physical, out); ++} ++static void properties2(VkPhysicalDevice physical, VkPhysicalDeviceProperties2 *out, const char *name) ++{ ++ struct hybris_layer_physical context; ++ if (!hybris_layer_lookup_physical(physical, &context)) return; ++ PFN_vkGetPhysicalDeviceProperties2 query = (void *)context.resolver(context.instance, name); ++ query(physical, out); ++ cap_properties(physical, &out->properties); ++} ++static void VKAPI_CALL properties2_core(VkPhysicalDevice physical, VkPhysicalDeviceProperties2 *out) ++{ properties2(physical, out, "vkGetPhysicalDeviceProperties2"); } ++static void VKAPI_CALL properties2_khr(VkPhysicalDevice physical, VkPhysicalDeviceProperties2 *out) ++{ properties2(physical, out, "vkGetPhysicalDeviceProperties2KHR"); } + static int emulates(VkPhysicalDevice physical, VkFormat format) + { + return format >= VK_FORMAT_BC1_RGB_UNORM_BLOCK && format <= VK_FORMAT_BC7_SRGB_BLOCK && +@@ -109,10 +146,13 @@ int hybris_bc_extension_allowed(const char *name) + "VK_KHR_maintenance6", "VK_KHR_maintenance8", "VK_EXT_host_image_copy", + "VK_QCOM_rotated_copy_commands", "VK_EXT_device_generated_commands", + "VK_NV_device_generated_commands", "VK_NV_device_generated_commands_compute", +- "VK_NV_dedicated_allocation", "VK_NV_copy_memory_indirect" ++ "VK_NV_dedicated_allocation", "VK_NV_copy_memory_indirect", "VK_NV_per_stage_descriptor_set" + }; + for (size_t i = 0; i < sizeof(unsupported) / sizeof(unsupported[0]); ++i) +- if (!strcmp(name, unsupported[i])) return 0; ++ if (!strcmp(name, unsupported[i])) { ++ if (dxvk_profile() && !strcmp(name, "VK_KHR_maintenance6")) continue; ++ return 0; ++ } + return 1; + } + /* Extension feature queries must agree with the filtered extension list. Each +@@ -121,8 +161,12 @@ static unsigned unsupported_feature_count(VkStructureType type) + { + switch (type) { + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_BUFFER_FEATURES_EXT: return 4; ++ case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_4_FEATURES: ++ return dxvk_profile() ? (offsetof(VkPhysicalDeviceVulkan14Features, pushDescriptor) - ++ offsetof(VkPhysicalDeviceVulkan14Features, globalPriorityQuery)) / sizeof(VkBool32) + 1 : 0; ++ case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_6_FEATURES_KHR: return dxvk_profile() ? 0 : 1; ++ case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PER_STAGE_DESCRIPTOR_SET_FEATURES_NV: return 2; + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_OBJECT_FEATURES_EXT: +- case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_6_FEATURES_KHR: + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_IMAGE_COPY_FEATURES_EXT: + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COPY_MEMORY_INDIRECT_FEATURES_NV: + case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEVICE_GENERATED_COMMANDS_FEATURES_NV: return 1; +@@ -252,11 +296,12 @@ static void VKAPI_CALL sparse_properties2_core(VkPhysicalDevice physical, const + static void VKAPI_CALL sparse_properties2_khr(VkPhysicalDevice physical, const VkPhysicalDeviceSparseImageFormatInfo2 *info, + uint32_t *count, VkSparseImageFormatProperties2 *out) + { sparse_properties2(physical, info, count, out, "vkGetPhysicalDeviceSparseImageFormatProperties2KHR"); } +-static int image_supported(VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags) ++static int image_supported(VkFormat format, VkImageType type, VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags) + { + return type == VK_IMAGE_TYPE_2D && tiling == VK_IMAGE_TILING_OPTIMAL && usage && + !(usage & ~(VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT)) && +- !(flags & ~VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT); ++ !(flags & ~(VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT | VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT)) && ++ (!(flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT) || hybris_bc_srgb_family(format)); + } + static VkResult VKAPI_CALL image_properties(VkPhysicalDevice physical, VkFormat format, VkImageType type, + VkImageTiling tiling, VkImageUsageFlags usage, VkImageCreateFlags flags, VkImageFormatProperties *out) +@@ -265,7 +310,7 @@ static VkResult VKAPI_CALL image_properties(VkPhysicalDevice physical, VkFormat + if (!hybris_layer_lookup_physical(physical, &context)) return VK_ERROR_INITIALIZATION_FAILED; + PHYSICAL_PROC(GetPhysicalDeviceImageFormatProperties); + if (emulates(physical, format)) { +- if (!image_supported(type, tiling, usage, flags)) { *out = (VkImageFormatProperties){0}; return VK_ERROR_FORMAT_NOT_SUPPORTED; } ++ if (!image_supported(format, type, tiling, usage, flags)) { *out = (VkImageFormatProperties){0}; return VK_ERROR_FORMAT_NOT_SUPPORTED; } + VkResult result = GetPhysicalDeviceImageFormatProperties(physical, hybris_bc_image_format(format, physical_rgb_mask(physical)), type, + tiling, usage | VK_IMAGE_USAGE_TRANSFER_DST_BIT, flags, out); + if (result == VK_SUCCESS) out->sampleCounts &= VK_SAMPLE_COUNT_1_BIT; +@@ -282,14 +327,16 @@ static VkResult image_properties2(VkPhysicalDevice physical, const VkPhysicalDev + (PFN_vkGetPhysicalDeviceImageFormatProperties2)context.resolver(context.instance, name); + if (!emulates(physical, info->format)) return query(physical, info, out); + out->imageFormatProperties = (VkImageFormatProperties){0}; +- if (!image_supported(info->type, info->tiling, info->usage, info->flags)) return VK_ERROR_FORMAT_NOT_SUPPORTED; ++ if (!image_supported(info->format, info->type, info->tiling, info->usage, info->flags)) return VK_ERROR_FORMAT_NOT_SUPPORTED; + for (const VkBaseInStructure *node = info->pNext; node; node = node->pNext) { + if (node->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO) { + if (((const VkPhysicalDeviceExternalImageFormatInfo *)node)->handleType) return VK_ERROR_FORMAT_NOT_SUPPORTED; + } else if (node->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO) { + const VkImageFormatListCreateInfo *list = (const void *)node; + for (uint32_t i = 0; i < list->viewFormatCount; ++i) +- if (list->pViewFormats[i] != info->format) return VK_ERROR_FORMAT_NOT_SUPPORTED; ++ if (!hybris_bc_view_compatible(info->format, list->pViewFormats[i]) || ++ (list->pViewFormats[i] != info->format && !(info->flags & VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT))) ++ return VK_ERROR_FORMAT_NOT_SUPPORTED; + } else if (node->sType != VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_VIEW_IMAGE_FORMAT_INFO_EXT) { + return VK_ERROR_FORMAT_NOT_SUPPORTED; + } +@@ -322,6 +369,9 @@ PFN_vkVoidFunction hybris_bc_policy_proc(const char *name) + if (!hybris_bc_enabled()) return NULL; + static const struct { const char *name; PFN_vkVoidFunction function; } commands[] = { + #define ENTRY(name, function) {"vk" #name, (PFN_vkVoidFunction)function} ++ ENTRY(GetPhysicalDeviceProperties, properties), ++ ENTRY(GetPhysicalDeviceProperties2, properties2_core), ++ ENTRY(GetPhysicalDeviceProperties2KHR, properties2_khr), + ENTRY(GetPhysicalDeviceQueueFamilyProperties, queue_properties), + ENTRY(GetPhysicalDeviceQueueFamilyProperties2, queue_properties2_core), + ENTRY(GetPhysicalDeviceQueueFamilyProperties2KHR, queue_properties2_khr), +diff --git a/hybris/vulkan/compat/bc_resources.c b/hybris/vulkan/compat/bc_resources.c +index 526c3f1..74cc0c9 100644 +--- a/hybris/vulkan/compat/bc_resources.c ++++ b/hybris/vulkan/compat/bc_resources.c +@@ -210,8 +210,10 @@ static VkResult VKAPI_CALL create_view(VkDevice handle, const VkImageViewCreateI + struct hybris_bc_resource *resource = hybris_bc_resource_find(device, info->image); + VkImageViewCreateInfo translated = *info; + if (resource && resource->emulated.image) { +- if (info->format != resource->format) return VK_ERROR_FORMAT_NOT_SUPPORTED; +- translated.format = resource->emulated.decoded_format; ++ unsigned bit = info->format == resource->format ? 1u : 2u; ++ if (!hybris_bc_view_compatible(resource->format, info->format) || ++ !(resource->emulated.view_mask & bit)) return VK_ERROR_FORMAT_NOT_SUPPORTED; ++ translated.format = hybris_bc_image_format(info->format, device->rgb_mask); + } + PROC(CreateImageView); + return CreateImageView(handle, &translated, allocator, view); +diff --git a/hybris/vulkan/compat/bc_transfer.c b/hybris/vulkan/compat/bc_transfer.c +index 6edb652..799fb62 100644 +--- a/hybris/vulkan/compat/bc_transfer.c ++++ b/hybris/vulkan/compat/bc_transfer.c +@@ -2,6 +2,7 @@ + #define VK_NO_PROTOTYPES + #include "bc_transfer.h" + #include ++#include + #include + + #define PROC(name) PFN_vk##name name = (PFN_vk##name)t->decoder->resolver(t->decoder->device, "vk" #name) +@@ -59,16 +60,29 @@ static VkResult scratch(struct hybris_bc_transfer *t) + if (type == t->memory->memoryTypeCount) type = i; + if (t->memory->memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) { type = i; break; } + } +- if (type == t->memory->memoryTypeCount) { result = VK_ERROR_OUT_OF_DEVICE_MEMORY; goto fail; } ++ if (type == t->memory->memoryTypeCount) { ++ fprintf(stderr, "HYBRIS_BC scratch: no memory type (bits=0x%x count=%u size=%llu)\n", ++ requirements.memoryRequirements.memoryTypeBits, t->memory->memoryTypeCount, ++ (unsigned long long)requirements.memoryRequirements.size); ++ result = VK_ERROR_OUT_OF_DEVICE_MEMORY; goto fail; ++ } + VkMemoryDedicatedAllocateInfo owner = { + .sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO, .buffer = buffer}; + VkMemoryAllocateInfo allocation = {.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, + .pNext = dedicated.requiresDedicatedAllocation || dedicated.prefersDedicatedAllocation ? &owner : NULL, + .allocationSize = requirements.memoryRequirements.size, .memoryTypeIndex = type}; + result = AllocateMemory(device, &allocation, NULL, &memory); +- if (result != VK_SUCCESS) goto fail; ++ if (result != VK_SUCCESS) { ++ fprintf(stderr, "HYBRIS_BC scratch allocation: result=%d size=%llu type=%u bits=0x%x\n", ++ result, (unsigned long long)allocation.allocationSize, type, requirements.memoryRequirements.memoryTypeBits); ++ goto fail; ++ } + result = BindBufferMemory(device, buffer, memory, 0); +- if (result != VK_SUCCESS) goto fail; ++ if (result != VK_SUCCESS) { ++ fprintf(stderr, "HYBRIS_BC scratch bind: result=%d size=%llu type=%u\n", ++ result, (unsigned long long)allocation.allocationSize, type); ++ goto fail; ++ } + t->scratch = buffer; + t->scratch_memory = memory; + t->scratch_size = size; +@@ -193,7 +207,7 @@ VkResult hybris_bc_decode_image(struct hybris_bc_transfer *t, + buffer_barrier(t, command, image->blocks, VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_ACCESS_SHADER_READ_BIT); + VkResult result = decode(t, command, image, layout, region); +- if (result != VK_SUCCESS) { state->error = result; return result; } ++ if (result != VK_SUCCESS) { fprintf(stderr, "HYBRIS_BC transfer failed: %d\n", result); state->error = result; return result; } + buffer_barrier(t, command, image->blocks, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, + VK_ACCESS_SHADER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT); +diff --git a/hybris/vulkan/compat/shader_policy.c b/hybris/vulkan/compat/shader_policy.c +index a1dcf6d..21a4cc7 100644 +--- a/hybris/vulkan/compat/shader_policy.c ++++ b/hybris/vulkan/compat/shader_policy.c +@@ -92,7 +92,8 @@ static void properties2(VkPhysicalDevice physical, VkPhysicalDeviceProperties2 * + { + struct hybris_layer_physical context; + if (!hybris_layer_lookup_physical(physical, &context)) return; +- PFN_vkGetPhysicalDeviceProperties2 query = (PFN_vkGetPhysicalDeviceProperties2)context.resolver(context.instance, name); ++ PFN_vkGetPhysicalDeviceProperties2 query = (void *)hybris_bc_policy_proc(name); ++ if (!query) query = (void *)context.resolver(context.instance, name); + query(physical, out); + VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *legacy = NULL; + for (VkBaseOutStructure *node = out->pNext; node; node = node->pNext) +diff --git a/tests/baseline/probe_bc_images.c b/tests/baseline/probe_bc_images.c +index 85ba6e6..55e069c 100644 +--- a/tests/baseline/probe_bc_images.c ++++ b/tests/baseline/probe_bc_images.c +@@ -95,9 +95,10 @@ int bc_images_probe(int validate, int route) + } + free(queues); + if (family == UINT32_MAX) return 3; ++ unsigned first_format = getenv("BC_D3D_FORMATS") ? 2 : 0; + VkBool32 linear_filter[BC_IMAGE_FORMAT_COUNT] = {0}; + unsigned unsupported_formats = 0; +- for (unsigned f = 0; f < BC_IMAGE_FORMAT_COUNT; ++f) { ++ for (unsigned f = first_format; f < BC_IMAGE_FORMAT_COUNT; ++f) { + VkFormatProperties format; + p_vkGetPhysicalDeviceFormatProperties(physical, bc_formats[f], &format); + VkFormatProperties2 format2 = {.sType = VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_2}; +@@ -164,19 +165,28 @@ int bc_images_probe(int validate, int route) + p_vkGetPhysicalDeviceFeatures2(physical, &queried); + maintenance4 = maintenance.maintenance4; + } +- synchronization.pNext = maintenance4 ? &maintenance : NULL; +- const char *device_extensions[4]; ++ int maintenance6 = getenv("BC_MAINTENANCE6") != NULL; ++ VkPhysicalDeviceMaintenance6FeaturesKHR maintenance_six = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_6_FEATURES_KHR}; ++ if (maintenance6) { ++ VkPhysicalDeviceFeatures2 q = {.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, .pNext = &maintenance_six}; ++ p_vkGetPhysicalDeviceFeatures2(physical, &q); ++ if (!maintenance_six.maintenance6) { puts("MISSING maintenance6"); return 3; } ++ } ++ maintenance.pNext = maintenance6 ? &maintenance_six : NULL; ++ synchronization.pNext = maintenance4 ? (void *)&maintenance : maintenance6 ? (void *)&maintenance_six : NULL; ++ const char *device_extensions[5]; + uint32_t device_extension_count = 0; + if (copies2) device_extensions[device_extension_count++] = VK_KHR_COPY_COMMANDS_2_EXTENSION_NAME; + if (sync2) device_extensions[device_extension_count++] = VK_KHR_SYNCHRONIZATION_2_EXTENSION_NAME; + if (maintenance4) device_extensions[device_extension_count++] = VK_KHR_MAINTENANCE_4_EXTENSION_NAME; + if (format_list) device_extensions[device_extension_count++] = VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME; ++ if (maintenance6) device_extensions[device_extension_count++] = VK_KHR_MAINTENANCE_6_EXTENSION_NAME; + float priority = 1; + VkDeviceQueueCreateInfo qc = {.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, + .queueFamilyIndex = family, .queueCount = 1, .pQueuePriorities = &priority}; + VkDeviceCreateInfo dc = {.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + .queueCreateInfoCount = 1, .pQueueCreateInfos = &qc, +- .pNext = sync2 ? (void *)&synchronization : maintenance4 ? (void *)&maintenance : NULL, ++ .pNext = sync2 ? (void *)&synchronization : maintenance4 ? (void *)&maintenance : maintenance6 ? (void *)&maintenance_six : NULL, + .enabledExtensionCount = device_extension_count, .ppEnabledExtensionNames = device_extensions}; + VkDebugUtilsMessengerEXT messenger = VK_NULL_HANDLE; + PFN_vkDestroyDebugUtilsMessengerEXT destroy_messenger = NULL; +@@ -307,8 +317,12 @@ int bc_images_probe(int validate, int route) + VkFenceCreateInfo fence_info = {.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO}; + VkFence fence; + CHECK(p_vkCreateFence(device, &fence_info, NULL, &fence)); ++ PFN_vkCmdBindDescriptorSets2KHR bind2 = (PFN_vkCmdBindDescriptorSets2KHR)p_vkGetDeviceProcAddr(device, "vkCmdBindDescriptorSets2KHR"); ++ PFN_vkCmdPushConstants2KHR push2 = (PFN_vkCmdPushConstants2KHR)p_vkGetDeviceProcAddr(device, "vkCmdPushConstants2KHR"); ++ if (maintenance6 && (!bind2 || !push2)) return 2; ++ printf("BC_EXTENDED maintenance6=%d mutable=%d\n", maintenance6, getenv("BC_MUTABLE") != NULL); + unsigned failures = 0, readbacks = 0, copies2_cases = 0, sync2_cases = 0; +- for (unsigned f = 0; f < BC_IMAGE_FORMAT_COUNT; ++f) { ++ for (unsigned f = first_format; f < BC_IMAGE_FORMAT_COUNT; ++f) { + for (unsigned shape_index = 0; shape_index < 2; ++shape_index) { + uint32_t base_width = shape_index ? 32 : 9, base_height = shape_index ? 32 : 7; + for (uint32_t mip = 0; mip < 4; ++mip) { +@@ -337,6 +351,13 @@ int bc_images_probe(int validate, int route) + (i != 2 ? VK_IMAGE_USAGE_SAMPLED_BIT : 0), .sharingMode = VK_SHARING_MODE_EXCLUSIVE}; + VkImageFormatListCreateInfo formats = {.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO, + .viewFormatCount = 1, .pViewFormats = &image_info.format}; ++ VkFormat paired[2] = {bc_formats[f], bc_formats[f ^ 1]}; ++ if (getenv("BC_MUTABLE") && (f < 8 || (f >= 12 && f < 14)) && i < 2) { ++ image_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; ++ image_info.format = paired[1]; ++ formats.viewFormatCount = 2; ++ formats.pViewFormats = paired; ++ } + if (format_list) image_info.pNext = &formats; + VkMemoryRequirements2 described = {.sType = VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2}; + if (maintenance4) { +@@ -414,9 +435,19 @@ int bc_images_probe(int validate, int route) + VkCommandBufferBeginInfo begin = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO}; + CHECK(p_vkBeginCommandBuffer(command, &begin)); + p_vkCmdBindPipeline(command, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline); +- p_vkCmdBindDescriptorSets(command, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_layout, 0, 1, &descriptors, 1, &dynamic_offset); ++ if (maintenance6) { ++ VkBindDescriptorSetsInfoKHR bind = {.sType = VK_STRUCTURE_TYPE_BIND_DESCRIPTOR_SETS_INFO_KHR, ++ .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, .layout = pipeline_layout, ++ .descriptorSetCount = 1, .pDescriptorSets = &descriptors, ++ .dynamicOffsetCount = 1, .pDynamicOffsets = &dynamic_offset}; ++ bind2(command, &bind); ++ } else p_vkCmdBindDescriptorSets(command, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline_layout, 0, 1, &descriptors, 1, &dynamic_offset); + uint32_t shape[] = {width, height, LAYERS, f >= 14 ? 3 : f >= 8 && f < 12 ? 1 + (f & 1) : 0, linear_filter[f] != 0}; +- p_vkCmdPushConstants(command, pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(shape), shape); ++ if (maintenance6) { ++ VkPushConstantsInfoKHR push = {.sType = VK_STRUCTURE_TYPE_PUSH_CONSTANTS_INFO_KHR, ++ .layout = pipeline_layout, .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT, .size = sizeof(shape), .pValues = shape}; ++ push2(command, &push); ++ } else p_vkCmdPushConstants(command, pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(shape), shape); + VkMemoryBarrier before = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER, + .srcAccessMask = VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, + .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT}; +@@ -614,7 +645,7 @@ int bc_images_probe(int validate, int route) + p_vkDestroyDevice(device, NULL); + if (destroy_messenger) destroy_messenger(instance, messenger, NULL); + p_vkDestroyInstance(instance, NULL); dlclose(h); +- printf("BC_IMAGES_SUMMARY formats=16 shapes=2 readbacks=%u copy2_cases=%u sync2_cases=%u maintenance4=%u format_list=%u failures=%u validation_errors=%u route=%d\n", +- readbacks, copies2_cases, sync2_cases, maintenance4, format_list, failures, validation.errors, route); +- return failures || validation.errors || readbacks != BC_IMAGE_FORMAT_COUNT * 2 * 4 * 3 ? 2 : 0; ++ printf("BC_IMAGES_SUMMARY formats=%u shapes=2 readbacks=%u copy2_cases=%u sync2_cases=%u maintenance4=%u format_list=%u failures=%u validation_errors=%u route=%d\n", ++ (unsigned)BC_IMAGE_FORMAT_COUNT - first_format, readbacks, copies2_cases, sync2_cases, maintenance4, format_list, failures, validation.errors, route); ++ return failures || validation.errors || readbacks != (BC_IMAGE_FORMAT_COUNT - first_format) * 2 * 4 * 3 ? 2 : 0; + } diff --git a/tools/hybris/powervr-hal-version.patch b/tools/hybris/powervr-hal-version.patch new file mode 100644 index 0000000000..63b72c7599 --- /dev/null +++ b/tools/hybris/powervr-hal-version.patch @@ -0,0 +1,12 @@ +--- a/hybris/vulkan/icd/icd.c ++++ b/hybris/vulkan/icd/icd.c +@@ -71,7 +71,8 @@ + int result = module->methods->open(module, HWVULKAN_DEVICE_0, + (hw_device_t **)&candidate); + if (result || !candidate || candidate->common.tag != HARDWARE_DEVICE_TAG || +- candidate->common.version != HWVULKAN_DEVICE_API_VERSION_0_1 || ++ (candidate->common.version != HWVULKAN_DEVICE_API_VERSION_0_1 && ++ candidate->common.version != HARDWARE_DEVICE_API_VERSION(0, 1)) || + !candidate->CreateInstance || !candidate->GetInstanceProcAddr || + !candidate->EnumerateInstanceExtensionProperties) { + fprintf(stderr, "hybris ICD: incompatible Vulkan HAL device (open=%d)\n", result); diff --git a/tools/hybris/run-proton.py b/tools/hybris/run-proton.py new file mode 100644 index 0000000000..4349f029fe --- /dev/null +++ b/tools/hybris/run-proton.py @@ -0,0 +1,83 @@ +#!/usr/bin/python3 +"""Select a staged DXVK payload for one DroidDeck Proton game launch. + +Place this script beside dxvk/x32/ and dxvk/x64/, then select its absolute +path with DROIDDECK_PROTON_WRAPPER in the game's environment settings. +Run `python3 run-proton.py --check /path/to/proton` to check the payload and +Proton source layout without starting Proton or changing a game prefix. + +This wrapper does not install the graphics runtime, select Vulkan layers, +change synchronization settings, or provide game-specific workarounds. +""" + +import argparse +from pathlib import Path +import sys + + +DLL_NAMES = ("d3d8.dll", "d3d9.dll", "d3d10core.dll", "d3d11.dll", "dxgi.dll") +PREFIX_MARKER = "droiddeck-dxvk-profile-v1" + + +def prepare(proton, dxvk): + """Validate all inputs and compile an in-memory copy of the Proton script.""" + for architecture in ("x32", "x64"): + for name in DLL_NAMES: + dll = dxvk / architecture / name + if not dll.is_file(): + raise ValueError(f"missing DXVK file: {dll}") + with dll.open("rb") as stream: + if not stream.read(1): + raise ValueError(f"empty DXVK file: {dll}") + + source = proton.read_text(encoding="utf-8") + for is32, architecture in (("False", "x64"), ("True", "x32")): + needle = f'g_proton.arch_pe_dir("wine/dxvk", {is32}) + f + ".dll"' + if source.count(needle) != 1: + raise ValueError(f"unsupported Proton layout: expected one {architecture} DXVK copy expression") + directory = str(dxvk / architecture) + "/" + source = source.replace(needle, repr(directory) + ' + f + ".dll"', 1) + + needle = " # check whether any prefix config has changed" + if source.count(needle) != 1: + raise ValueError("unsupported Proton layout: expected one prefix configuration marker") + # The supported Proton copies DXVK DLLs on every launch. This marker records + # profile activation; it does not need changing whenever the payload changes. + marker = repr("\n" + PREFIX_MARKER) + source = source.replace(needle, f" prefix_info += {marker}\n" + needle, 1) + return compile(source, str(proton), "exec") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--check", action="store_true", help="validate inputs without running Proton") + parser.add_argument("proton", help="selected Proton Python script") + parser.add_argument("proton_args", nargs=argparse.REMAINDER) + args = parser.parse_args() + if not args.check and (len(args.proton_args) < 2 or args.proton_args[0] != "waitforexitandrun"): + parser.error("expected: PROTON waitforexitandrun GAME [ARGUMENTS...]") + + proton = Path(args.proton) + dxvk = Path(__file__).resolve().parent / "dxvk" + try: + code = prepare(proton, dxvk) + except (OSError, ValueError, SyntaxError) as error: + print(f"DroidDeck DXVK profile: {error}", file=sys.stderr) + return 1 + + if args.check: + print(f"DroidDeck DXVK profile: payload present and Proton layout supported ({dxvk})") + return 0 + + print(f"DROIDDECK_GRAPHICS profile={PREFIX_MARKER} dxvk={dxvk}", flush=True) + # Preserve the selected Proton path, including symlinks, and its arguments. + # Proton uses argv[0] and sibling Python imports to locate its own depot. + sys.argv = [args.proton, *args.proton_args] + sys.path.insert(0, str(proton.absolute().parent)) + exec(code, {"__name__": "__main__", "__file__": args.proton, + "__package__": None, "__spec__": None}) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tools/hybris/source.env b/tools/hybris/source.env new file mode 100644 index 0000000000..30dd70e1cf --- /dev/null +++ b/tools/hybris/source.env @@ -0,0 +1,2 @@ +HYBRIS_REVISION=bc8cb3586b79d208fd21b925fa0209b3e432e008 +PROTOCOL_REVISION=c6ae3c22a804da3cda017f4c8eaa6c6a3da55457 diff --git a/tools/powervr/fetch.py b/tools/powervr/fetch.py new file mode 100644 index 0000000000..91bf3cfed0 --- /dev/null +++ b/tools/powervr/fetch.py @@ -0,0 +1,316 @@ +#!/usr/bin/env python3 +"""Assemble the pinned PowerVR components into the APK's isolated Linux runtime. + +Like DirectAudio, APK builds download published components rather than compile +them. Their source, licenses and patches travel with the payload. Build recipes +remain in tools/{hybris,dxvk,vulkan-wsi}; release.json pins their reviewed inputs. +""" +import argparse +import hashlib +import io +import json +import os +from pathlib import Path, PurePosixPath +import posixpath +import stat +import subprocess +import tarfile +import tempfile +from urllib.parse import urlparse +from urllib.request import urlopen +from zipfile import ZipFile + + +ROOT = Path(__file__).resolve().parents[2] +ASSETS = Path("app/src/main/assets/hybris") +REQUIRED_LIBRARIES = ( + "lib/libhybris-vulkan-icd.so.0", "lib/libhybris-common.so.1", + "lib/libhybris/linker/q.so", "lib/libVkLayer_hybris_compat.so", + "wsi/libVkLayer_window_system_integration.so", +) +MAX_UNPACKED = 256 * 1024 * 1024 +BUNDLED_LICENSES = { + 'tools/vulkan-wsi/licenses/Vulkan-Headers/' + name: 'licenses/Vulkan-Headers/' + name + for name in ('LICENSE.md', 'MIT.txt') +} + + +def sha256(data): + return hashlib.sha256(data).hexdigest() + + +def json_bytes(value): + return (json.dumps(value, sort_keys=True, indent=2) + "\n").encode() + + +def safe_name(name): + path = PurePosixPath(name) + if not name or path.is_absolute() or ".." in path.parts or "\\" in name: + raise ValueError("unsafe archive path: " + name) + return str(path) + + +def check_tree(entries): + # Never extract through a symlink, even one pointing inside the tree. + for name, entry in entries.items(): + for parent in PurePosixPath(name).parents: + if str(parent) in entries: + raise ValueError("archive member below a file or symlink: " + name) + if entry[0] == "symlink": + read(entries, name) + + +def read(entries, name, seen=()): + if name in seen or len(seen) > 16: + raise ValueError("runtime symlink cycle: " + name) + if name not in entries: + raise ValueError("missing runtime file: " + name) + kind, mode, data = entries[name] + if kind == "symlink": + if PurePosixPath(data).is_absolute() or "\\" in data: + raise ValueError("unsafe runtime symlink: " + name) + target = safe_name(posixpath.normpath(posixpath.join(posixpath.dirname(name), data))) + return read(entries, target, (*seen, name)) + return data + + +def read_tar(data): + unpacked = subprocess.run(["zstd", "-q", "-dc"], input=data, + capture_output=True, check=True).stdout + if len(unpacked) > MAX_UNPACKED: + raise ValueError("runtime archive exceeds size limit") + entries, seen = {}, set() + with tarfile.open(fileobj=io.BytesIO(unpacked)) as package: + for member in package: + name = safe_name(member.name) + if name in seen: + raise ValueError("duplicate archive path: " + name) + seen.add(name) + if member.isdir(): + continue + if name == ".": + raise ValueError("archive root must be a directory") + if member.issym(): + entries[name] = ("symlink", 0o777, member.linkname) + elif member.isfile(): + entries[name] = ("file", 0o755 if member.mode & 0o111 else 0o644, + package.extractfile(member).read()) + else: + raise ValueError("unsupported archive entry type: " + name) + check_tree(entries) + return entries + + +def tree_digest(entries): + return sha256(json_bytes({name: [kind, mode, sha256(data) if kind == "file" else data] + for name, (kind, mode, data) in sorted(entries.items())})) + + +def load_release(root): + release = json.loads((root / "tools/powervr/release.json").read_bytes()) + if release.get("format") != 1: + raise ValueError("unsupported PowerVR release pins") + # These identify the reviewed recipe, not a new attestation from the old + # producer. The original build metadata is preserved inside the runtime. + for component in [release["hybris"], *release["archives"]]: + for name, expected in component["recipe_inputs"].items(): + if sha256((root / name).read_bytes()) != expected: + raise ValueError("PowerVR component recipe changed; update its release pin: " + name) + return release + + +def assembly_inputs(root): + return {"format": 1, "inputs": { + name: sha256((root / name).read_bytes()) for name in + ("tools/powervr/release.json", "tools/powervr/fetch.py", "tools/hybris/run-proton.py", *BUNDLED_LICENSES)}} + + +def fetch(component, cache): + url = component["url"] + if urlparse(url).scheme != "https": + raise ValueError("component URL must use HTTPS") + cache.mkdir(parents=True, exist_ok=True) + target = cache / (component["sha256"] + "-" + PurePosixPath(urlparse(url).path).name) + + def valid(path): + return (path.is_file() and path.stat().st_size == component["size"] + and sha256(path.read_bytes()) == component["sha256"]) + + if valid(target): + return target.read_bytes() + # No unverified response is ever parsed or promoted into the cache. + with tempfile.NamedTemporaryFile(dir=cache, prefix=".download-", delete=False) as output: + temporary = Path(output.name) + try: + with urlopen(url, timeout=60) as response: + remaining = component["size"] + while block := response.read(min(1024 * 1024, remaining + 1)): + remaining -= len(block) + if remaining < 0: + raise ValueError("component download exceeds pinned size") + output.write(block) + output.flush() + if not valid(temporary): + raise ValueError("component download checksum mismatch: " + url) + os.replace(temporary, target) + finally: + temporary.unlink(missing_ok=True) + return target.read_bytes() + + +def read_zip(data, component): + entries, seen = {}, set() + expected = {item["name"]: item for item in component["files"]} + allowed = set(expected) | set(component["ignored"]) + total = 0 + with ZipFile(io.BytesIO(data)) as archive: + for member in archive.infolist(): + name = safe_name(member.filename) + if name in seen: + raise ValueError("duplicate archive path: " + name) + seen.add(name) + mode = member.external_attr >> 16 + kind = stat.S_IFMT(mode) + if member.is_dir() and kind in (0, stat.S_IFDIR): + continue + if kind not in (0, stat.S_IFREG) or name not in allowed: + raise ValueError("unexpected component archive entry: " + name) + total += member.file_size + if total > MAX_UNPACKED: + raise ValueError("component archive exceeds size limit") + if name not in expected: + continue + pin = expected[name] + content = archive.read(member) + if len(content) != pin["size"] or sha256(content) != pin["sha256"]: + raise ValueError("component member checksum mismatch: " + name) + path = safe_name(pin["path"]) + if path in entries: + raise ValueError("duplicate component destination: " + path) + entries[path] = ("file", pin["mode"], content) + if set(expected) - seen: + raise ValueError("component archive missing files: " + ", ".join(sorted(set(expected) - seen))) + check_tree(entries) + return entries + + +def check_bundle(entries, release, root): + check_tree(entries) + if read(entries, "assembly-inputs.json") != json_bytes(assembly_inputs(root)): + raise ValueError("PowerVR assembly inputs changed; run tools/powervr/fetch.py") + if entries["run-proton.py"] != ("file", 0o755, (root / "tools/hybris/run-proton.py").read_bytes()): + raise ValueError("PowerVR Proton wrapper is stale or not executable") + compile(read(entries, "run-proton.py"), "run-proton.py", "exec") + bridge = dict(entries) + del bridge["assembly-inputs.json"], bridge["run-proton.py"] + for source, target in BUNDLED_LICENSES.items(): + if entries.get(target) != ("file", 0o644, (root / source).read_bytes()): + raise ValueError("Vulkan-Headers license missing or stale: " + target) + for component in release["archives"]: + for pin in component["files"]: + content = read(entries, pin["path"]) + if (entries[pin["path"]][:2] != ("file", pin["mode"]) + or len(content) != pin["size"] or sha256(content) != pin["sha256"]): + raise ValueError("PowerVR component mismatch: " + pin["path"]) + del bridge[pin["path"]] + for target in BUNDLED_LICENSES.values(): + bridge.pop(target, None) + if tree_digest(bridge) != release["hybris"]["tree_sha256"]: + raise ValueError("PowerVR bridge differs from the pinned archive") + for name in REQUIRED_LIBRARIES: + content = read(entries, name) + if content[:6] != b"\x7fELF\x02\x01" or content[18:20] != b"\xb7\x00": + raise ValueError("runtime library is not ELF64 AArch64: " + name) + for architecture, machine, magic in (("x32", b"\x4c\x01", b"\x0b\x01"), + ("x64", b"\x64\x86", b"\x0b\x02")): + for name in ("d3d8.dll", "d3d9.dll", "d3d10core.dll", "d3d11.dll", "dxgi.dll"): + path = "dxvk/" + architecture + "/" + name + content = read(entries, path) + pe = int.from_bytes(content[60:64], "little") + if (content[:2] != b"MZ" or pe < 64 or content[pe:pe + 4] != b"PE\0\0" + or content[pe + 4:pe + 6] != machine or content[pe + 24:pe + 26] != magic): + raise ValueError("DXVK library has wrong PE architecture: " + path) + + +def pack(entries): + stream = io.BytesIO() + directories = {str(parent) for name in entries for parent in PurePosixPath(name).parents + if str(parent) != "."} + with tarfile.open(fileobj=stream, mode="w", format=tarfile.USTAR_FORMAT) as archive: + for name in sorted(directories | set(entries)): + info = tarfile.TarInfo(name) + info.mtime = info.uid = info.gid = 0 + if name in directories: + info.type, info.mode = tarfile.DIRTYPE, 0o755 + archive.addfile(info) + continue + kind, info.mode, data = entries[name] + if kind == "symlink": + info.type, info.linkname = tarfile.SYMTYPE, data + archive.addfile(info) + else: + info.size = len(data) + archive.addfile(info, io.BytesIO(data)) + return subprocess.run(["zstd", "-q", "-10", "-T1", "-c"], input=stream.getvalue(), + capture_output=True, check=True).stdout + + +def verify_archive(archive, version, root=ROOT): + release = load_release(root) + if sha256(archive) != version.strip(): + raise ValueError("PowerVR runtime archive checksum mismatch") + check_bundle(read_tar(archive), release, root) + + +def verify(root=ROOT): + verify_archive((root / ASSETS / "runtime.tzst").read_bytes(), + (root / ASSETS / "version.txt").read_text(), root) + + +def assemble(root, cache): + release = load_release(root) + entries = read_tar(fetch(release["hybris"], cache)) + if tree_digest(entries) != release["hybris"]["tree_sha256"]: + raise ValueError("PowerVR bridge tree checksum mismatch") + for component in release["archives"]: + additions = read_zip(fetch(component, cache), component) + if set(entries) & set(additions): + raise ValueError("component overwrites existing runtime files") + entries.update(additions) + for source, target in BUNDLED_LICENSES.items(): + license_entry = ("file", 0o644, (root / source).read_bytes()) + if target in entries and entries[target] != license_entry: + raise ValueError("bundled license overwrites component file: " + target) + entries[target] = license_entry + entries["run-proton.py"] = ("file", 0o755, (root / "tools/hybris/run-proton.py").read_bytes()) + entries["assembly-inputs.json"] = ("file", 0o644, json_bytes(assembly_inputs(root))) + check_bundle(entries, release, root) + archive = pack(entries) + assets = root / ASSETS + assets.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(dir=assets.parent, prefix=".hybris-") as directory: + stage = Path(directory) + (stage / "runtime.tzst").write_bytes(archive) + (stage / "version.txt").write_text(sha256(archive) + "\n") + # version.txt is the readiness marker; an interrupted replacement fails + # closed instead of presenting a partially updated payload as ready. + (assets / "version.txt").unlink(missing_ok=True) + os.replace(stage / "runtime.tzst", assets / "runtime.tzst") + os.replace(stage / "version.txt", assets / "version.txt") + print("PowerVR runtime prepared: " + sha256(archive)) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--verify", action="store_true") + parser.add_argument("--cache-dir", type=Path, default=Path(os.environ.get( + "DROIDDECK_BUILD_CACHE", str(Path.home() / ".cache/droiddeck-build")))) + args = parser.parse_args() + if args.verify: + verify() + else: + assemble(ROOT, args.cache_dir) + + +if __name__ == "__main__": + main() diff --git a/tools/powervr/release.json b/tools/powervr/release.json new file mode 100644 index 0000000000..a9bf29f0e3 --- /dev/null +++ b/tools/powervr/release.json @@ -0,0 +1,250 @@ +{ + "archives": [ + { + "files": [ + { + "mode": 493, + "name": "x32/d3d8.dll", + "path": "dxvk/x32/d3d8.dll", + "sha256": "c9612be6aac99390156cdcd3b22054fba62cf68bfc948d753cc39282d37f67ab", + "size": 1658894 + }, + { + "mode": 493, + "name": "x32/d3d9.dll", + "path": "dxvk/x32/d3d9.dll", + "sha256": "fbd0e7d7aae39d5d623757ead228930c4be78cb3b16db6f352796577831ce36e", + "size": 7598094 + }, + { + "mode": 493, + "name": "x32/d3d10core.dll", + "path": "dxvk/x32/d3d10core.dll", + "sha256": "cd6052b1f6a02ab94fea5bfa3519e556fb353f585da4b64e3c810ef4cc37c808", + "size": 192526 + }, + { + "mode": 493, + "name": "x32/d3d11.dll", + "path": "dxvk/x32/d3d11.dll", + "sha256": "9bf51b5ab913e1bd84513b2b1608586bc0a651788df9e664b8135840b8c8c54d", + "size": 7942158 + }, + { + "mode": 493, + "name": "x32/dxgi.dll", + "path": "dxvk/x32/dxgi.dll", + "sha256": "d3772dc86267fe94eee7afc30c993f44297fd43b50210f1691f3d03ebf4d4402", + "size": 5738510 + }, + { + "mode": 493, + "name": "x64/d3d8.dll", + "path": "dxvk/x64/d3d8.dll", + "sha256": "cd870a8f5cdbfef33e7ccf7884d6f1c442e2c8ec22d6adea60fea3dbe83c32e2", + "size": 1499136 + }, + { + "mode": 493, + "name": "x64/d3d9.dll", + "path": "dxvk/x64/d3d9.dll", + "sha256": "4f80af96991073bb3dc25f3b93ec2c07ba25fcabb8a6e30ffd5eaae38c8465c7", + "size": 6934528 + }, + { + "mode": 493, + "name": "x64/d3d10core.dll", + "path": "dxvk/x64/d3d10core.dll", + "sha256": "4950e952ca291b13c0645322f1831414e77df87183ed2f81d5dde02c1fd43ff9", + "size": 978944 + }, + { + "mode": 493, + "name": "x64/d3d11.dll", + "path": "dxvk/x64/d3d11.dll", + "sha256": "3497d5f7dc78fa7affdba6777d3ab4aac7ec744d7e2c795fbf07dbff54058e6c", + "size": 7143424 + }, + { + "mode": 493, + "name": "x64/dxgi.dll", + "path": "dxvk/x64/dxgi.dll", + "sha256": "d6e0ca242dba6cb8606106a7fcb65268225520f03c68e36a365f44d65f863e74", + "size": 5230592 + }, + { + "mode": 420, + "name": "LICENSE", + "path": "licenses/dxvk-LICENSE", + "sha256": "a5cb1a6ded7d2d7e92d550ba28edd21be2d1d4044662b399887351023e30ce64", + "size": 1074 + }, + { + "mode": 420, + "name": "source-commit", + "path": "provenance/dxvk-source-commit", + "sha256": "cb748e54f2fcdca516189435647ea001dbd14f97e2df5b36fd2f13fc185a5dbf", + "size": 41 + }, + { + "mode": 420, + "name": "SHA256SUMS", + "path": "provenance/dxvk-SHA256SUMS", + "sha256": "8a106cb90288052cc91768b569980ed7381c89591fb7cea3bc6d0312c6f30a47", + "size": 802 + }, + { + "mode": 420, + "name": "downstream.patch", + "path": "licenses/dxvk-downstream.patch", + "sha256": "e44b8d656a37c3b1059bbf3dcbcf11922a00634301f2509024fb263a45e9f50e", + "size": 3604 + }, + { + "mode": 420, + "name": "source.tar.zst", + "path": "licenses/dxvk-source.tar.zst", + "sha256": "dfdebe8aa71b85af638f2565c1d5abe31417c6db643a827bcf0988e29ded55e7", + "size": 6043001 + }, + { + "mode": 420, + "name": "submodules.txt", + "path": "provenance/dxvk-submodules.txt", + "sha256": "01e74c750ca473590e17f1ca33f204b8272f84898ee6e8a8509be9740919092c", + "size": 472 + } + ], + "id": "dxvk", + "ignored": [ + "x32/d3d10core.dll.a", + "x32/d3d11.dll.a", + "x32/d3d8.dll.a", + "x32/d3d9.dll.a", + "x32/dxgi.dll.a", + "x64/d3d10core.dll.a", + "x64/d3d11.dll.a", + "x64/d3d8.dll.a", + "x64/d3d9.dll.a", + "x64/dxgi.dll.a" + ], + "provenance": { + "ci_run": "https://github.com/wallentx/DroidDeck/actions/runs/38119192562", + "repository_commit": "aee0ebd43557ef9964ba7f6a11d7d30efaf983ac" + }, + "recipe_inputs": { + "tools/dxvk/build.sh": "f37efdc1dbcc511b0399e1027d582dfd09f689bddb434eed31c33d88867460ad", + "tools/dxvk/check_present_sync.py": "42e2a590e17c1f53e5cd4100ef3d19a5bdcce31a6d7a18fbf9961f4ef27d5063", + "tools/dxvk/patches/0001-check-direct3d-bc-formats.patch": "d38fc4d773b7974498dcb377dbaa883c9bba36aefa3235b39bca69d6dd220c77", + "tools/dxvk/patches/0002-present-semaphore-per-swapchain-image.patch": "c802789a33099d3efc3413a297e2b752f8eb84c18cbfbdfc04e188f4090410d0", + "tools/dxvk/source.env": "e49b0757f203f88783360c06118f60945242fde8738419e0b27cf2531bf3a365" + }, + "sha256": "d08dd962ced3482883ad9e9eb4903a4d27a3756b4f989a97ecdfcccb53fbee66", + "size": 24063241, + "url": "https://github.com/wallentx/DroidDeck/releases/download/v0.3.2-progress.20261011.3/dxvk-aee0ebd.zip" + }, + { + "files": [ + { + "mode": 420, + "name": "libVkLayer_window_system_integration.so", + "path": "wsi/libVkLayer_window_system_integration.so", + "sha256": "d07b961181b9547fd48ad9f05cc6d3b808b824676eb5a186fe3feb8caa013f19", + "size": 436888 + }, + { + "mode": 420, + "name": "VkLayer_window_system_integration.json", + "path": "wsi/VkLayer_window_system_integration.json", + "sha256": "a1df8b1cee7315015d8fe82534451e373ae4d9cb2adfc232c68346ecbf2818ee", + "size": 3683 + }, + { + "mode": 420, + "name": "LICENSE", + "path": "licenses/wsi-LICENSE", + "sha256": "5eb8d2da52230d2d66ce078a89ae0173aac63e2e73bed5f907a34736d114ea30", + "size": 1086 + }, + { + "mode": 420, + "name": "source-commit", + "path": "provenance/wsi-source-commit", + "sha256": "8367150b20502af36c472651850694a15a3e2a1fd6b6beb1f6e12a85a97824f5", + "size": 41 + }, + { + "mode": 420, + "name": "SHA256SUMS", + "path": "provenance/wsi-SHA256SUMS", + "sha256": "fda6f61028659648801de0e3a6fe1676efd99e1551881a5db8630970eae86158", + "size": 108 + }, + { + "mode": 420, + "name": "downstream.patch", + "path": "licenses/wsi-downstream.patch", + "sha256": "e2ed113d22f05563248ef606c774c22658b184fbc05912229007cd5281027e78", + "size": 769 + }, + { + "mode": 420, + "name": "source.tar.zst", + "path": "licenses/wsi-source.tar.zst", + "sha256": "22ae7c2d8aa3dfffdda601c3442d1966ab53ad225acdfadee69ba38974d0c4c5", + "size": 209468 + }, + { + "mode": 420, + "name": "vulkan-headers-commit", + "path": "provenance/wsi-vulkan-headers-commit", + "sha256": "16706d6cdbeb788426a90d66574dfd71068c4d993d39e76ec5c281b1f30dc7a6", + "size": 41 + } + ], + "id": "wsi", + "ignored": [], + "provenance": { + "ci_run": "https://github.com/wallentx/DroidDeck/actions/runs/38047627963", + "repository_commit": "abe58618a5194f1f15499cbd5f9ec75503782556", + "vulkan_headers_licenses": { + "note": "Added from tracked assembly inputs; the original WSI archive and compiled library are unchanged.", + "revision": "8d6039a455a7ecc7d2a592ff97f62db4e59b70bf", + "source_urls": [ + "https://raw.githubusercontent.com/KhronosGroup/Vulkan-Headers/8d6039a455a7ecc7d2a592ff97f62db4e59b70bf/LICENSE.md", + "https://raw.githubusercontent.com/KhronosGroup/Vulkan-Headers/8d6039a455a7ecc7d2a592ff97f62db4e59b70bf/LICENSES/MIT.txt" + ] + } + }, + "recipe_inputs": { + "tools/vulkan-wsi/build.sh": "9e403151c58e6b2ee15241f525c4bacb2495f072191da6a63065e7fcc25b3b3c", + "tools/vulkan-wsi/licenses/Vulkan-Headers/LICENSE.md": "95ad366d23fadf701d355bc45fb8b82ae2d700239471d35d41286ac3b08ff903", + "tools/vulkan-wsi/licenses/Vulkan-Headers/MIT.txt": "1ca3502222d967f3be5751c55f6b7ee735b5383909c3b501495f54b216dbf227", + "tools/vulkan-wsi/patches/0001-link-x11-external-memory.patch": "e2ed113d22f05563248ef606c774c22658b184fbc05912229007cd5281027e78", + "tools/vulkan-wsi/source.env": "71a88087265a09b4ce06e7c9b98ef6799c6a7657cc6ef9084de663e96f5cfe63" + }, + "sha256": "348f9ec42159c1ee6e2baba4312a5e845d26687238f29e3f4a1b0c3c97e13b3c", + "size": 362717, + "url": "https://github.com/wallentx/DroidDeck/releases/download/v0.3.2-progress.20261010.4/wsi-abe5861.zip" + } + ], + "format": 1, + "hybris": { + "provenance": { + "apk_ci_run": "https://github.com/wallentx/DroidDeck/actions/runs/38123715308", + "extracted_from_apk": "https://github.com/wallentx/DroidDeck/releases/download/v0.3.2-progress.20261011.3/DroidDeck-0.3.2-1b6185b.apk", + "note": "APK build reused a cached bridge. Original source and compiler metadata are preserved in build-manifest.json; no APK commit is claimed as the bridge producer.", + "upstream_source_commit": "bc8cb3586b79d208fd21b925fa0209b3e432e008" + }, + "recipe_inputs": { + "tools/hybris/build_runtime.sh": "bc83472ecfd886841c8c2c436080cdee0a27ce0d860ffbffe45a75fbc6b2b21d", + "tools/hybris/patches/0001-dxvk-bc-profile.patch": "b90605e51256595de4453352865aa198e59bd2d392ac6b7c2a60f38c57727c9d", + "tools/hybris/powervr-hal-version.patch": "d3ed8ca03e5a874b9dc46651983117760c7490b96127335625d0480a2b85e66f", + "tools/hybris/source.env": "9ae7985f2052dbcc8b9666494bc8a711ec85013f6a27d6d72d9729c32b919dd8" + }, + "sha256": "f4f12c26ed21003c2c98a7cececd76c8a3b332d79ece9d1f963cf3b9e8943978", + "size": 5552486, + "tree_sha256": "de9cc2c4107bf396e4f963d7e91f936a966593fb1df680a0b5d43db0f9fef4e1", + "url": "https://github.com/wallentx/DroidDeck/releases/download/v0.3.2-progress.20261011.3/hybris-runtime-f4f12c26.tzst" + } +} diff --git a/tools/release/check_session_assets.py b/tools/release/check_session_assets.py index 89d05f01b3..11564e894a 100644 --- a/tools/release/check_session_assets.py +++ b/tools/release/check_session_assets.py @@ -1,5 +1,6 @@ """Reject an APK with missing or stale session helpers, including cached local bundles.""" from pathlib import Path +import importlib.util import sys from zipfile import ZipFile @@ -33,6 +34,16 @@ def check(apk, overlay=OVERLAY): continue if content != source.read_bytes(): errors.append('stale ' + asset) + if overlay == OVERLAY: + spec = importlib.util.spec_from_file_location('powervr_fetch', + OVERLAY.parents[1] / 'powervr/fetch.py') + runtime = importlib.util.module_from_spec(spec) + spec.loader.exec_module(runtime) + try: + runtime.verify_archive(package.read('assets/hybris/runtime.tzst'), + package.read('assets/hybris/version.txt').decode('ascii')) + except (KeyError, ValueError, OSError) as error: + errors.append('invalid PowerVR runtime: ' + str(error)) return errors diff --git a/tools/release/runtime_inputs.py b/tools/release/runtime_inputs.py index b332ee36ad..3fab564f1b 100644 --- a/tools/release/runtime_inputs.py +++ b/tools/release/runtime_inputs.py @@ -1,6 +1,7 @@ """Attest prepared runtime inputs and reject stale local or CI APK payloads.""" import hashlib import gzip +import importlib.util import json from pathlib import Path import subprocess @@ -16,6 +17,7 @@ SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app 'tools/linuxfs', 'tools/proot', 'tools/aaudio-sink', 'tools/directaudio', 'tools/gamescope/release.env', 'tools/droiddeck-esync/release.env', 'tools/mangoapp', 'tools/msitools', 'tools/build_local.sh', + 'tools/powervr', 'tools/hybris', 'tools/dxvk', 'tools/vulkan-wsi', 'tools/release/runtime_inputs.py', 'app/src/main/assets/desktop-removal'] @@ -40,7 +42,7 @@ def sources(root): def outputs(root): result = {} - for tree in ['linuxfs', 'directaudio', 'droiddeck-esync', 'desktop-removal']: + for tree in ['linuxfs', 'directaudio', 'droiddeck-esync', 'desktop-removal', 'hybris']: base = root / 'app/src/main/assets' / tree for path in base.rglob('*'): if path.is_file() and not any(part.startswith('.') for part in path.relative_to(base).parts): @@ -74,9 +76,17 @@ def required(): paths += ['assets/desktop-removal/' + name for name in ['lxqt-r1.tsv.gzip', 'kde-remove.tsv.gzip', 'kde-keep.txt.gzip', 'emulators-keep.txt.gzip', 'versions.txt', 'sources.json']] + paths += ['assets/hybris/runtime.tzst', 'assets/hybris/version.txt'] return paths +def verify_powervr(root): + spec = importlib.util.spec_from_file_location('powervr_fetch', ROOT / 'tools/powervr/fetch.py') + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + module.verify(root) + + def record(root=ROOT): # Refresh scripts even on a CI native-cache hit; these are source, not cached binaries. if root == ROOT: @@ -89,6 +99,7 @@ def record(root=ROOT): missing = [name for name in required() if name not in paths or not (root / paths[name]).is_file()] if missing: raise ValueError('Missing runtime inputs:\n' + '\n'.join(missing)) + verify_powervr(root) for name, path in paths.items(): if name.startswith('assets/desktop-removal/') and name.endswith('.gzip'): try: diff --git a/tools/tests/test_powervr_runtime.py b/tools/tests/test_powervr_runtime.py new file mode 100644 index 0000000000..d6f3830791 --- /dev/null +++ b/tools/tests/test_powervr_runtime.py @@ -0,0 +1,224 @@ +import importlib.util +import io +import json +from pathlib import Path +import subprocess +import tarfile +import tempfile +import unittest +from unittest.mock import patch +from zipfile import ZipFile, ZipInfo + + +spec = importlib.util.spec_from_file_location('powervr_fetch', + Path(__file__).resolve().parents[1] / 'powervr/fetch.py') +runtime = importlib.util.module_from_spec(spec) +spec.loader.exec_module(runtime) + + +def elf(): + content = bytearray(64) + content[:6], content[18:20] = b'\x7fELF\x02\x01', b'\xb7\x00' + return bytes(content) + + +def pe(architecture): + content = bytearray(128) + content[:2], content[60:64], content[64:68] = b'MZ', (64).to_bytes(4, 'little'), b'PE\0\0' + content[68:70] = b'\x4c\x01' if architecture == 'x32' else b'\x64\x86' + content[88:90] = b'\x0b\x01' if architecture == 'x32' else b'\x0b\x02' + return bytes(content) + + +def tar(entries): + stream = io.BytesIO() + with tarfile.open(fileobj=stream, mode='w') as archive: + for name, kind, content in entries: + info = tarfile.TarInfo(name) + info.type = kind + if kind in (tarfile.SYMTYPE, tarfile.LNKTYPE): + info.linkname = content + archive.addfile(info) + elif kind == tarfile.REGTYPE: + info.size = len(content) + archive.addfile(info, io.BytesIO(content)) + else: + archive.addfile(info) + return subprocess.run(['zstd', '-q', '-c'], input=stream.getvalue(), + capture_output=True, check=True).stdout + + +class PowerVRRuntimeTest(unittest.TestCase): + def setUp(self): + self.temporary = tempfile.TemporaryDirectory() + self.addCleanup(self.temporary.cleanup) + self.root = Path(self.temporary.name) + self.cache = self.root / 'cache' + self.cache.mkdir() + for name, content in { + 'tools/powervr/fetch.py': b'fixture assembler', + 'tools/hybris/run-proton.py': b'print("fixture Proton wrapper")\n', + 'tools/hybris/source.env': b'HYBRIS_REVISION=fixture\n', + 'tools/vulkan-wsi/licenses/Vulkan-Headers/LICENSE.md': b'Khronos headers attribution\n', + 'tools/vulkan-wsi/licenses/Vulkan-Headers/MIT.txt': b'MIT license fixture\n', + }.items(): + path = self.root / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(content) + bridge = {name: ('file', 0o755, elf()) for name in runtime.REQUIRED_LIBRARIES if name.startswith('lib/')} + bridge['lib/libhybris-common.so.1.0.0'] = bridge.pop('lib/libhybris-common.so.1') + bridge['lib/libhybris-common.so.1'] = ('symlink', 0o777, 'libhybris-common.so.1.0.0') + data = runtime.pack(bridge) + self.release = {'format': 1, 'hybris': self.component('bridge.tzst', data), 'archives': []} + self.release['hybris'].update({ + 'tree_sha256': runtime.tree_digest(bridge), + 'recipe_inputs': {'tools/hybris/source.env': runtime.sha256((self.root / 'tools/hybris/source.env').read_bytes())}}) + files = {'dxvk/' + arch + '/' + name: pe(arch) + for arch in ('x32', 'x64') + for name in ('d3d8.dll', 'd3d9.dll', 'd3d10core.dll', 'd3d11.dll', 'dxgi.dll')} + files['wsi/libVkLayer_window_system_integration.so'] = elf() + stream = io.BytesIO() + with ZipFile(stream, 'w') as archive: + for name, content in files.items(): + archive.writestr(name, content) + component = self.component('graphics.zip', stream.getvalue()) + component.update({'ignored': [], 'files': [ + {'name': name, 'path': name, 'size': len(content), 'sha256': runtime.sha256(content), 'mode': 0o644} + for name, content in files.items()]}) + self.release['archives'].append(component) + self.pins = self.root / 'tools/powervr/release.json' + self.pins.write_bytes(runtime.json_bytes(self.release)) + + def component(self, name, data): + component = {'url': 'https://example.invalid/' + name, 'size': len(data), + 'sha256': runtime.sha256(data), 'recipe_inputs': {}} + (self.cache / (component['sha256'] + '-' + name)).write_bytes(data) + return component + + def assembled(self): + runtime.assemble(self.root, self.cache) + return runtime.read_tar((self.root / runtime.ASSETS / 'runtime.tzst').read_bytes()) + + def test_complete_payload_preserves_symlinks_and_is_reproducible(self): + self.assembled() + first = (self.root / runtime.ASSETS / 'runtime.tzst').read_bytes() + runtime.verify(self.root) + self.assembled() + self.assertEqual(first, (self.root / runtime.ASSETS / 'runtime.tzst').read_bytes()) + self.assertEqual(runtime.read_tar(first)['lib/libhybris-common.so.1'][0], 'symlink') + + def test_stale_wrapper_cannot_pass_with_an_updated_archive_checksum(self): + self.assembled() + (self.root / 'tools/hybris/run-proton.py').write_text('print("new wrapper")\n') + with self.assertRaisesRegex(ValueError, 'assembly inputs changed'): + runtime.verify(self.root) + + def test_header_licenses_are_bundled_verbatim(self): + entries = self.assembled() + for source, target in runtime.BUNDLED_LICENSES.items(): + self.assertEqual(runtime.read(entries, target), (self.root / source).read_bytes()) + + def test_missing_or_modified_header_license_is_rejected(self): + original = self.assembled() + target = 'licenses/Vulkan-Headers/MIT.txt' + for replacement in (None, ('file', 0o644, b'stale license')): + with self.subTest(replacement=replacement): + entries = dict(original) + if replacement is None: + del entries[target] + else: + entries[target] = replacement + with self.assertRaisesRegex(ValueError, 'license missing or stale'): + runtime.check_bundle(entries, self.release, self.root) + + def test_changed_tracked_header_license_rejects_old_bundle(self): + self.assembled() + source = next(iter(runtime.BUNDLED_LICENSES)) + (self.root / source).write_bytes(b'changed license text') + with self.assertRaisesRegex(ValueError, 'assembly inputs changed'): + runtime.verify(self.root) + + def test_changed_component_recipe_requires_new_release_pin(self): + self.assembled() + (self.root / 'tools/hybris/source.env').write_text('HYBRIS_REVISION=new-source\n') + with self.assertRaisesRegex(ValueError, 'recipe changed'): + runtime.verify(self.root) + + def test_old_dxvk_cannot_pass_by_updating_only_the_bundle_checksum(self): + entries = self.assembled() + entries['dxvk/x64/d3d11.dll'] = ('file', 0o644, pe('x64') + b'old DLL') + archive = runtime.pack(entries) + with self.assertRaisesRegex(ValueError, 'component mismatch'): + runtime.verify_archive(archive, runtime.sha256(archive), self.root) + + def test_wrong_pe_architecture_rejected_even_with_matching_member_pin(self): + entries = self.assembled() + name = 'dxvk/x32/d3d11.dll' + entries[name] = ('file', 0o644, pe('x64')) + pin = next(item for item in self.release['archives'][0]['files'] if item['path'] == name) + pin['sha256'] = runtime.sha256(pe('x64')) + with self.assertRaisesRegex(ValueError, 'wrong PE architecture'): + runtime.check_bundle(entries, self.release, self.root) + + def test_unpinned_bridge_file_is_rejected(self): + entries = self.assembled() + entries['lib/extra-loader.so'] = ('file', 0o755, elf()) + with self.assertRaisesRegex(ValueError, 'bridge differs'): + runtime.check_bundle(entries, self.release, self.root) + + def test_failed_download_leaves_the_previous_payload_ready(self): + self.assembled() + before = (self.root / runtime.ASSETS / 'version.txt').read_bytes() + component = self.release['archives'][0] + (self.cache / (component['sha256'] + '-graphics.zip')).write_bytes(b'bad cache') + with patch.object(runtime, 'urlopen', return_value=io.BytesIO(b'corrupt response')): + with self.assertRaisesRegex(ValueError, 'checksum mismatch'): + runtime.assemble(self.root, self.cache) + self.assertEqual(before, (self.root / runtime.ASSETS / 'version.txt').read_bytes()) + runtime.verify(self.root) + + def test_tar_rejects_traversal_duplicate_special_files_and_symlink_escape(self): + cases = [ + [('../escape', tarfile.REGTYPE, b'x')], + [('file', tarfile.REGTYPE, b'x'), ('./file', tarfile.REGTYPE, b'y')], + [('device', tarfile.CHRTYPE, b'')], + [('file', tarfile.LNKTYPE, 'other')], + [('lib/link', tarfile.SYMTYPE, '../../outside')], + [('lib/link', tarfile.SYMTYPE, '/absolute')], + [('lib/a', tarfile.SYMTYPE, 'b'), ('lib/b', tarfile.SYMTYPE, 'a')], + [('lib', tarfile.SYMTYPE, 'target'), ('target', tarfile.REGTYPE, b'x'), + ('lib/child', tarfile.REGTYPE, b'x')], + ] + for members in cases: + with self.subTest(members=members): + with self.assertRaises(ValueError): + runtime.read_tar(tar(members)) + + def test_relative_symlink_that_stays_inside_runtime_is_preserved(self): + entries = runtime.read_tar(tar([ + ('lib/a', tarfile.REGTYPE, b'library'), ('lib/child/link', tarfile.SYMTYPE, '../a')])) + self.assertEqual(runtime.read(entries, 'lib/child/link'), b'library') + + def test_zip_rejects_symlink_even_if_name_is_allowed(self): + stream = io.BytesIO() + with ZipFile(stream, 'w') as archive: + info = ZipInfo('allowed') + info.create_system = 3 + info.external_attr = 0o120777 << 16 + archive.writestr(info, '../outside') + with self.assertRaisesRegex(ValueError, 'unexpected component archive entry'): + runtime.read_zip(stream.getvalue(), {'files': [], 'ignored': ['allowed']}) + + def test_zip_rejects_unknown_and_duplicate_members(self): + for names in (['unexpected'], ['allowed', 'allowed'], ['../escape']): + with self.subTest(names=names): + stream = io.BytesIO() + with ZipFile(stream, 'w') as archive: + for name in names: + archive.writestr(name, b'file') + with self.assertRaises(ValueError): + runtime.read_zip(stream.getvalue(), {'files': [], 'ignored': ['allowed']}) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/tests/test_runtime_inputs.py b/tools/tests/test_runtime_inputs.py index b2f2f02a55..643416df61 100644 --- a/tools/tests/test_runtime_inputs.py +++ b/tools/tests/test_runtime_inputs.py @@ -6,6 +6,7 @@ import subprocess import tarfile import tempfile import unittest +from unittest.mock import patch from zipfile import ZipFile @@ -19,6 +20,10 @@ class RuntimeInputsTest(unittest.TestCase): self.temp = tempfile.TemporaryDirectory() self.addCleanup(self.temp.cleanup) self.root = Path(self.temp.name) + # Component archive validation has its own binary/archive fixtures. + component_check = patch.object(runtime, 'verify_powervr') + component_check.start() + self.addCleanup(component_check.stop) paths = runtime.outputs(self.root) for name in runtime.required(): path = self.root / paths.get(name, 'app/src/main/' + name) @@ -55,6 +60,20 @@ class RuntimeInputsTest(unittest.TestCase): with self.assertRaisesRegex(ValueError, 'sources changed'): runtime.verify(self.root) + def test_changed_powervr_wrapper_rejects_old_outputs(self): + wrapper = self.root / 'tools/hybris/run-proton.py' + wrapper.parent.mkdir(parents=True) + wrapper.write_text('current wrapper') + runtime.record(self.root) + wrapper.write_text('changed wrapper') + with self.assertRaisesRegex(ValueError, 'sources changed'): + runtime.verify(self.root) + + def test_missing_powervr_bundle_cannot_be_attested(self): + (self.root / 'app/src/main/assets/hybris/runtime.tzst').unlink() + with self.assertRaisesRegex(ValueError, 'assets/hybris/runtime.tzst'): + runtime.record(self.root) + def test_modified_prebuilt_binary_is_rejected(self): runtime.record(self.root) (self.root / 'app/src/main/jniLibs/arm64-v8a/libproot.so').write_bytes(b'old APK binary') diff --git a/tools/vulkan-wsi/build.sh b/tools/vulkan-wsi/build.sh new file mode 100755 index 0000000000..cf5a1c3560 --- /dev/null +++ b/tools/vulkan-wsi/build.sh @@ -0,0 +1,34 @@ +#!/usr/bin/env bash +# CI build of a vendor-neutral X11 surface/swapchain layer. +set -euo pipefail +root=$(cd "$(dirname "$0")/../.." && pwd) +. "$root/tools/vulkan-wsi/source.env" +mkdir -p "${1:?output directory required}" +out=$(cd "$1" && pwd) +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +fetch() { + git init -q "$3" + git -C "$3" fetch --depth=1 "$1" "$2" + git -C "$3" checkout -q --detach FETCH_HEAD + test "$(git -C "$3" rev-parse HEAD)" = "$2" +} +fetch https://github.com/ginkage/vulkan-wsi-layer.git "$WSI_REVISION" "$work/source" +fetch https://github.com/KhronosGroup/Vulkan-Headers.git "$VULKAN_HEADERS_REVISION" "$work/headers" +git -C "$work/source" apply --check "$root/tools/vulkan-wsi/patches/0001-link-x11-external-memory.patch" +git -C "$work/source" apply "$root/tools/vulkan-wsi/patches/0001-link-x11-external-memory.patch" +cmake -S "$work/source" -B "$work/build" -G Ninja -DCMAKE_BUILD_TYPE=Release \ + -DCMAKE_INSTALL_PREFIX=/usr -DVULKAN_CXX_INCLUDE="$work/headers/include" \ + -DBUILD_WSI_HEADLESS=OFF -DBUILD_WSI_WAYLAND=OFF -DBUILD_WSI_DISPLAY=OFF -DBUILD_WSI_X11=ON \ + -DSELECT_EXTERNAL_ALLOCATOR=dma_buf_heaps -DWSIALLOC_MEMORY_HEAP_NAME=system -DKERNEL_HEADER_DIR=/usr/include +cmake --build "$work/build" --parallel 4 +DESTDIR="$work/stage" cmake --install "$work/build" +cp "$work/stage/usr/share/vulkan/implicit_layer.d/"* "$out/" +cp "$root/tools/vulkan-wsi/patches/0001-link-x11-external-memory.patch" "$out/downstream.patch" +cp "$work/source/LICENSE" "$out/LICENSE" +mkdir -p "$out/licenses/Vulkan-Headers" +cp "$root/tools/vulkan-wsi/licenses/Vulkan-Headers/"* "$out/licenses/Vulkan-Headers/" +printf '%s\n' "$WSI_REVISION" > "$out/source-commit" +printf '%s\n' "$VULKAN_HEADERS_REVISION" > "$out/vulkan-headers-commit" +tar --exclude-vcs --zstd -cf "$out/source.tar.zst" -C "$work/source" . +(cd "$out" && sha256sum ./*.so > SHA256SUMS) diff --git a/tools/vulkan-wsi/licenses/Vulkan-Headers/LICENSE.md b/tools/vulkan-wsi/licenses/Vulkan-Headers/LICENSE.md new file mode 100644 index 0000000000..17cf4a17d6 --- /dev/null +++ b/tools/vulkan-wsi/licenses/Vulkan-Headers/LICENSE.md @@ -0,0 +1,19 @@ +Copyright 2015-2026 The Khronos Group Inc. + +Files in this repository fall under one of these licenses: + +- `Apache-2.0` +- `MIT` + +Note: With the exception of `parse_dependency.py`, which is based on an +external file, files using the `MIT` license also fall under `Apache-2.0`. +Example: + +``` +SPDX-License-Identifier: Apache-2.0 OR MIT +``` + +Full license text of these licenses is available at: + + * Apache-2.0: https://opensource.org/licenses/Apache-2.0 + * MIT: https://opensource.org/licenses/MIT diff --git a/tools/vulkan-wsi/licenses/Vulkan-Headers/MIT.txt b/tools/vulkan-wsi/licenses/Vulkan-Headers/MIT.txt new file mode 100644 index 0000000000..8495fe9e90 --- /dev/null +++ b/tools/vulkan-wsi/licenses/Vulkan-Headers/MIT.txt @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2015-2023 The Khronos Group Inc. + +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. diff --git a/tools/vulkan-wsi/patches/0001-link-x11-external-memory.patch b/tools/vulkan-wsi/patches/0001-link-x11-external-memory.patch new file mode 100644 index 0000000000..da605feaaa --- /dev/null +++ b/tools/vulkan-wsi/patches/0001-link-x11-external-memory.patch @@ -0,0 +1,16 @@ +--- a/CMakeLists.txt ++++ b/CMakeLists.txt +@@ -421,6 +421,13 @@ + cp ${PROJECT_SOURCE_DIR}/layer/VkLayer_window_system_integration.json ${CMAKE_CURRENT_BINARY_DIR} + ${JSON_COMMANDS}) + ++# X11 DRI3 uses these shared sources even when Wayland and display are disabled. ++if(BUILD_WSI_X11 AND NOT BUILD_WSI_WAYLAND AND NOT BUILD_WSI_DISPLAY) ++ target_sources(${PROJECT_NAME} PRIVATE ++ wsi/extensions/external_memory_extension.cpp ++ wsi/image_backing_memory_external.cpp) ++endif() ++ + # Install the layer and its manifest into a Vulkan implicit layer directory. Upstream leaves this to + # manual copying; the fork adds install rules so the Debian package picks up the .so and JSON. + install(TARGETS ${PROJECT_NAME} DESTINATION share/vulkan/implicit_layer.d/) diff --git a/tools/vulkan-wsi/source.env b/tools/vulkan-wsi/source.env new file mode 100644 index 0000000000..4c6e9f7a3b --- /dev/null +++ b/tools/vulkan-wsi/source.env @@ -0,0 +1,2 @@ +WSI_REVISION=40cfec09d0e2d42c5e1f2642cf4cba11e7008112 +VULKAN_HEADERS_REVISION=8d6039a455a7ecc7d2a592ff97f62db4e59b70bf