From be9939d82c6be3d1065776990cc972328c815727 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Sun, 11 Oct 2026 21:03:07 -0400 Subject: [PATCH] Fix black Steam output with FSR 1 (#597) --- .../main/cpp/waylandcomp/src/effects_chain.c | 10 ++- .../main/cpp/waylandcomp/src/effects_chain.h | 4 + app/src/main/cpp/waylandcomp/src/vk_present.c | 2 + docs/fsr-steam-black-screen.md | 26 ++++++ tools/tests/effects_source_cache_harness.c | 85 +++++++++++++++++++ tools/tests/test_effects_source_cache.py | 63 ++++++++++++++ 6 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 docs/fsr-steam-black-screen.md create mode 100644 tools/tests/effects_source_cache_harness.c create mode 100644 tools/tests/test_effects_source_cache.py diff --git a/app/src/main/cpp/waylandcomp/src/effects_chain.c b/app/src/main/cpp/waylandcomp/src/effects_chain.c index 0ab39af65..7e446e299 100644 --- a/app/src/main/cpp/waylandcomp/src/effects_chain.c +++ b/app/src/main/cpp/waylandcomp/src/effects_chain.c @@ -255,7 +255,11 @@ static void target_destroy(struct fx_target *t) { static int target_ensure(struct fx_target *t, int w, int h, const char *what) { if (t->img && t->w == w && t->h == h) return 0; target_destroy(t); - VkImageCreateInfo ii = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, + /* Unrestricted mutable views keep these render-then-blit targets uncompressed on Turnip. + * UBWC targets corrupt even a constant-colour pass on Adreno 740 (FSR becomes black). + * Only the chain's intermediate images need this; the scene and guest images stay unchanged. */ + VkImageCreateInfo ii = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .flags = VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT, .imageType = VK_IMAGE_TYPE_2D, .format = g_fx_fmt, .extent = {(uint32_t)w, (uint32_t)h, 1}, .mipLevels = 1, .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT, .tiling = VK_IMAGE_TILING_OPTIMAL, .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | @@ -292,6 +296,10 @@ static void scene_release(void) { memset(&g_scene, 0, sizeof(g_scene)); } +void vkp_effects_forget_image(VkImage img) { + if (img && g_scene.img == img) scene_release(); +} + static int scene_ensure(VkImage img, int w, int h) { if (g_scene.img == img && g_scene.w == w && g_scene.h == h && g_scene.ds) return 0; scene_release(); diff --git a/app/src/main/cpp/waylandcomp/src/effects_chain.h b/app/src/main/cpp/waylandcomp/src/effects_chain.h index 74203bccc..9b07420df 100644 --- a/app/src/main/cpp/waylandcomp/src/effects_chain.h +++ b/app/src/main/cpp/waylandcomp/src/effects_chain.h @@ -55,6 +55,10 @@ VkImage vkp_effects_run(VkCommandBuffer cmd, VkImage scene, int scene_w, int sce /* Compositor thread: drop every Vulkan object (device teardown / device lost). */ void vkp_effects_destroy(void); +/* Compositor thread: release the cached source view/descriptor before its owner destroys img. + * A later image can reuse the same Vulkan handle and dimensions; that is not the same resource. */ +void vkp_effects_forget_image(VkImage img); + /* Compositor thread, before vkp_effects_run(): the format of the scene image it will be handed and the * format its own targets use. R8G8B8A8 / R8G8B8A8 for every SDR frame (the default); A2B10G10R10 / * A2B10G10R10 while an HDR picture runs through it (hdr_compose.h). A target-format change rebuilds the diff --git a/app/src/main/cpp/waylandcomp/src/vk_present.c b/app/src/main/cpp/waylandcomp/src/vk_present.c index 4551c4806..f50e44712 100644 --- a/app/src/main/cpp/waylandcomp/src/vk_present.c +++ b/app/src/main/cpp/waylandcomp/src/vk_present.c @@ -1164,6 +1164,7 @@ static void record_draw(VkCommandBuffer cmd, const struct vkp_draw *d, const VkI } static void destroy_scene_image(void) { + if (g_scene.img) vkp_effects_forget_image(g_scene.img); if (g_scene.img) hdrc_forget_image(g_scene.img); /* the HDR pass may have drawn into it */ if (g_scene.img) blendp_forget_image(g_scene.img); if (g_scene.img) g_vk.DestroyImage(g_dev, g_scene.img, NULL); @@ -1203,6 +1204,7 @@ static int ensure_scene_image(int w, int h) { static int ensure_img(struct vkp_img_slot *s, int w, int h, VkFormat fmt, VkImageUsageFlags usage, const char *what) { if (s->img && s->w == w && s->h == h && s->fmt == fmt) return 0; if (s->img) hdrc_forget_image(s->img); + if (s->img) vkp_effects_forget_image(s->img); if (s->img) blendp_forget_image(s->img); if (s->img) g_vk.DestroyImage(g_dev, s->img, NULL); if (s->mem) g_vk.FreeMemory(g_dev, s->mem, NULL); diff --git a/docs/fsr-steam-black-screen.md b/docs/fsr-steam-black-screen.md new file mode 100644 index 000000000..a6e4f4107 --- /dev/null +++ b/docs/fsr-steam-black-screen.md @@ -0,0 +1,26 @@ +# Steam black screen with FSR 1 + +Branch `fix/fsr-steam-black-screen` starts at `origin/main` `e4c375ad` after PR #596 merged. + +## Diagnosis + +On AYN Thor (Adreno 740, WN-Turnip-1.19-p_Axxx), Steam reached READY and retained focus while the entire screen was black with FSR 1 at 720p / 85% sharpness. Switching the same running session to Linear immediately restored the picture. Production SGSR also went black. At native 1080p, FSR stayed selected but its upscale pass was unnecessary and Steam remained visible. + +A temporary SGSR shader drew a constant red strip beside a normal texture sample. The strip and sampled picture were corrupted in a tiled pattern. Allowing unrestricted mutable formats on the post-processing targets made both render correctly. Restoring the original shaders then restored the first production FSR launch. A second launch exposed a separate source-view lifetime bug: the scene owner destroyed its image while the effect chain retained a view and descriptor; a recycled image handle at the same dimensions could hit that stale cache. + +## Change + +Create the effect chain's intermediate images with `VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT`, without a restricted view-format list. Turnip consequently avoids UBWC compression on these render-then-sample/blit targets. The input scene and guest images retain their existing allocation policy. Release the cached source view and descriptor before an SDR, HDR or frame-generation scene image is destroyed. This makes recycled Vulkan handles create fresh sampling resources. Scaling remains enabled and the FSR EASU/RCAS shaders and sharpness settings are unchanged. + +Driver behavior is documented in Mesa's [image allocation implementation](https://chromium.googlesource.com/external/gitlab.freedesktop.org/mesa/mesa/+/3fcec4762f3fec551c00da52f349c83443152159/src/freedreno/vulkan/tu_image.cc), including the mutable-format check and `format_list_ubwc_possible`. + +## Validation + +- Four native cache regressions passed: recycled handles, unrelated/null images, repeated invalidation, and dimension changes. The fixture executes the actual cache implementation with Vulkan lifetime callbacks. +- Release build and 14 focused Android tests passed: SessionScalingPrefsTest and SessionDisplayTest. +- A separate Vulkan/MoltenVK smoke test ran the actual effect chain: a 16x16 source scaled by FSR to 32x32 retained RGBA 64/128/191/255. +- Signed APK installed over Thor's existing app, with its device-side transfer hash verified. +- Production FSR at 720p / 85% rendered visible Steam, including two consecutive stop/start cycles with the app/compositor retained (sessions 42 and 43). Live SGSR, Linear and return-to-FSR switching also rendered correctly. +- Session preferences match the pre-test snapshot; FSR 1 / 85%, 720p and the existing driver selections are retained. + +Local screenshots, state snapshots and renderer logs are under `captures/fsr-black-screen/` (ignored). Temporary probe shaders are excluded from the final source change. The signed APK hash is `2b8b7ea18c49bf53f1f3cb00eea8f73d122258a900c18ffe660c85c5654bedae`. The old cache behavior fails the recycled-handle regression; the corrected implementation passes all four cases. diff --git a/tools/tests/effects_source_cache_harness.c b/tools/tests/effects_source_cache_harness.c new file mode 100644 index 000000000..e45e4bb78 --- /dev/null +++ b/tools/tests/effects_source_cache_harness.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include + +/* Exercise the real cache with Vulkan lifetime callbacks, without an Android surface or GPU. */ +#define VK_PRESENT_H +const char *vkp_gpu_name(void) { return "cache test"; } +void droiddeck_log(const char *tag, const char *fmt, ...) { (void)tag; (void)fmt; } +#include "vk_loader.h" +struct vk_api g_vk; +#include "effects_chain.c" + +static unsigned views, sets, freed_views, freed_sets; +static VkImageView bound_view; +static VkResult VKAPI_CALL create_view(VkDevice dev, const VkImageViewCreateInfo *info, + const VkAllocationCallbacks *alloc, VkImageView *out) { + (void)dev; (void)alloc; + assert(info->image != VK_NULL_HANDLE); + *out = (VkImageView)(uintptr_t)(100 + ++views); + return VK_SUCCESS; +} +static VkResult VKAPI_CALL allocate_set(VkDevice dev, const VkDescriptorSetAllocateInfo *info, + VkDescriptorSet *out) { + (void)dev; + assert(info->descriptorPool != VK_NULL_HANDLE && info->descriptorSetCount == 1); + *out = (VkDescriptorSet)(uintptr_t)(200 + ++sets); + return VK_SUCCESS; +} +static void VKAPI_CALL update_set(VkDevice dev, uint32_t count, const VkWriteDescriptorSet *writes, + uint32_t copies, const VkCopyDescriptorSet *copy) { + (void)dev; (void)copies; (void)copy; + assert(count == 1); + bound_view = writes->pImageInfo->imageView; +} +static VkResult VKAPI_CALL free_set(VkDevice dev, VkDescriptorPool pool, uint32_t count, + const VkDescriptorSet *set) { + (void)dev; (void)pool; + assert(pool != VK_NULL_HANDLE && count == 1 && *set != VK_NULL_HANDLE); + freed_sets++; + return VK_SUCCESS; +} +static void VKAPI_CALL destroy_view(VkDevice dev, VkImageView view, const VkAllocationCallbacks *alloc) { + (void)dev; (void)alloc; + assert(view != VK_NULL_HANDLE); + freed_views++; +} + +int main(int argc, char **argv) { + assert(argc == 2); + g_dev = (VkDevice)(uintptr_t)1; + g_dpool = (VkDescriptorPool)(uintptr_t)2; + g_dsl = (VkDescriptorSetLayout)(uintptr_t)3; + g_sampler = (VkSampler)(uintptr_t)4; + g_vk.CreateImageView = create_view; + g_vk.AllocateDescriptorSets = allocate_set; + g_vk.UpdateDescriptorSets = update_set; + g_vk.FreeDescriptorSets = free_set; + g_vk.DestroyImageView = destroy_view; + VkImage source = (VkImage)(uintptr_t)10; + assert(scene_ensure(source, 1280, 720) == 0); + VkImageView original = bound_view; + if (!strcmp(argv[1], "recycled")) { + /* The owner destroys the image, then Vulkan reuses its handle at the same size. */ + vkp_effects_forget_image(source); + assert(freed_sets == 1 && freed_views == 1); + assert(scene_ensure(source, 1280, 720) == 0); + assert(bound_view != original && views == 2 && sets == 2); + } else if (!strcmp(argv[1], "unrelated")) { + vkp_effects_forget_image((VkImage)(uintptr_t)11); + vkp_effects_forget_image(VK_NULL_HANDLE); + assert(scene_ensure(source, 1280, 720) == 0); + assert(bound_view == original && views == 1 && !freed_views && !freed_sets); + } else if (!strcmp(argv[1], "repeated")) { + vkp_effects_forget_image(source); + vkp_effects_forget_image(source); + assert(freed_sets == 1 && freed_views == 1); + } else { + assert(!strcmp(argv[1], "resize")); + assert(scene_ensure(source, 1920, 1080) == 0); + assert(bound_view != original && views == 2 && freed_views == 1 && freed_sets == 1); + } + puts("PASS"); + return 0; +} diff --git a/tools/tests/test_effects_source_cache.py b/tools/tests/test_effects_source_cache.py new file mode 100644 index 000000000..4ad95710a --- /dev/null +++ b/tools/tests/test_effects_source_cache.py @@ -0,0 +1,63 @@ +import os +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + + +REPO = Path(__file__).resolve().parents[2] + + +class EffectsSourceCacheTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + compiler = shutil.which(os.environ.get('CC', 'cc')) + if compiler is None: + raise unittest.SkipTest('A host C compiler is required for the Vulkan cache test') + candidates = [Path('/usr/include/vulkan'), Path('/opt/homebrew/include/vulkan')] + for name in ('ANDROID_NDK_HOME', 'ANDROID_NDK_ROOT', 'NDK_HOME'): + if os.environ.get(name): + candidates.extend(Path(os.environ[name]).glob('toolchains/llvm/prebuilt/*/sysroot/usr/include/vulkan')) + for sdk in {Path(os.environ.get('ANDROID_HOME', '/nonexistent')), + Path(os.environ.get('ANDROID_SDK_ROOT', '/nonexistent')), + Path.home() / 'Library/Android/sdk', Path.home() / 'Android/Sdk'}: + candidates.extend(sdk.glob('ndk/*/toolchains/llvm/prebuilt/*/sysroot/usr/include/vulkan')) + headers = next((p for p in candidates if (p / 'vulkan.h').is_file()), None) + if headers is None: + raise unittest.SkipTest('Vulkan headers from the Android NDK or system SDK are required') + cls.temp = tempfile.TemporaryDirectory(prefix='droiddeck-effects-cache-') + cls.addClassCleanup(cls.temp.cleanup) + folder = Path(cls.temp.name) + # Copy only Vulkan headers so Android's libc headers cannot shadow the host's libc. + shutil.copytree(headers, folder / 'include/vulkan', ignore=shutil.ignore_patterns('*.hpp')) + if (headers.parent / 'vk_video').is_dir(): + shutil.copytree(headers.parent / 'vk_video', folder / 'include/vk_video') + cls.binary = folder / 'cache-test' + build = subprocess.run([compiler, '-std=c11', '-pthread', '-I' + str(folder / 'include'), + '-I' + str(REPO / 'app/src/main/cpp/waylandcomp/src'), + '-I' + str(REPO / 'app/src/main/cpp/shaders'), + str(REPO / 'tools/tests/effects_source_cache_harness.c'), + '-o', str(cls.binary)], capture_output=True, text=True) + if build.returncode: + raise RuntimeError(build.stderr) + + def check_cache(self, case): + result = subprocess.run([str(self.binary), case], check=True, capture_output=True, text=True) + self.assertEqual('PASS', result.stdout.strip()) + + def test_recycled_image_handle_gets_a_new_view_and_descriptor(self): + self.check_cache('recycled') + + def test_unrelated_and_null_images_preserve_the_current_view(self): + self.check_cache('unrelated') + + def test_repeated_owner_invalidation_does_not_double_free(self): + self.check_cache('repeated') + + def test_resize_replaces_the_cached_view_and_descriptor(self): + self.check_cache('resize') + + +if __name__ == '__main__': + unittest.main()