mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-25 07:46:01 +00:00
feat: non-Adreno graphics stack (Venus, open driver pairs, gamescope patches) (#2029)
* Retain compositor resources when GPU completion is unconfirmed * Stop quarantined GPU callers before retries and mapped writes * Support Android hardware-buffer presentation over Wayland * Support PowerVR swapchains and linear image imports in Gamescope * Bundle verified PowerVR graphics components through existing build preparation * Detect and select the system Vulkan backend on PowerVR * Apply PowerVR Steam and game compatibility during normal launches * Show PowerVR support through existing driver and component controls * Update existing hardware guidance for system Vulkan support * Import the shared system Vulkan readiness check * Discover and propagate the actual PowerVR render node * Prune obsolete PowerVR runtimes after guest processes exit * Retain external DMA-BUF ownership after GPU quarantine * Name the PowerVR HAL selection explicitly * Isolate component UI tests from application startup * Cover the inherited FEX preset in PowerVR profile tests * Log test worker failures for CI diagnosis * Remove temporary test logging after diagnostic CI * Prefer system dependencies for the Android Vulkan loader * Capture raw stderr before test worker termination * Remove the temporary test worker crash probe * Import pinned DXVK recipe prerequisite from PowerVR PR570 Copy only the standalone build recipe, source pin, BC-format prerequisite and presentation-sync patch/check from wallentx/DroidDeck atd0f7264ea8. Source: https://github.com/Droid-Deck/DroidDeck/pull/570 Preserve that proposal attribution; no Android app, compositor, managed pack, private data or fork development history is imported. * Add opt-in low32 GPU mapping adapter and integrity probes * Add opt-in D3D9 single-sample workaround and fix HUD texel format * Document verified production game workaround and publication scope * Restrict reset diagnostics and create private output atomically * Target official runtime review and document PR570 prerequisite provenance * Fix runtime tool output isolation, mmap64 chaining and DST ambiguity * Normalize swapchain backbuffers for the opt-in D3D9 single-sample path * Experimental Samsung Xclipse support on the system Vulkan path Detect Samsung's Vulkan HAL (vulkan.samsung.so) and its sgpu render node alongside PowerVR, and run Xclipse through the same libhybris bridge: - VK_LAYER_DROIDDECK_xclipse (tools/linuxfs/xclipse-layer), built with the glibc preloads and enabled only on Xclipse: sets bionic's environ for the driver's shader compiler, avoids immutable YCbCr samplers in compute layouts, forwards the KHR calibrated-timestamp entry points to the EXT ones, and reports textureCompressionBC and BC4/BC5 filtering over the compat layer's BC emulation. - Upstream DXVK 2.6.2, pinned as a tar.gz component and selected on Xclipse: the driver has 128 bytes of push constants, DXVK 3 needs 256. - android_wlegl accepts a zero stride and passes it through unchanged: Samsung's gralloc records none for GPU-only buffers. Tested on a Galaxy A56 (Xclipse 540, driver 24.0.560): Steam Big Picture and PICO PARK (D3D9). * Xclipse layer: copy the device chain and track samplers per device - Clear textureCompressionBC in a copy of the device-create chain (every structure up to the VkPhysicalDeviceFeatures2 that enables it) instead of writing the caller's structure; a chain led by a structure of unknown size is passed on unchanged. - Track YCbCr samplers per device in a growable list, drop them on vkDestroySampler and device teardown, and fail sampler creation rather than leave one untracked when the list cannot grow. - Fail descriptor set layout creation with VK_ERROR_OUT_OF_HOST_MEMORY when a replacement sampler array cannot be allocated, instead of passing the original YCbCr samplers to the driver. * Xclipse layer: copy any device-chain structure the headers define The chain copy that clears the emulated textureCompressionBC only knew a handful of structure sizes, so a valid chain such as VkPhysicalDeviceTimelineSemaphoreFeatures -> VkPhysicalDeviceFeatures2 was passed on unchanged and the compat layer refused the bit. Size every structure that may extend VkDeviceCreateInfo from a table generated from the Vulkan registry (gen_device_chain.py, Vulkan-Headers 1.3.275, the headers the build compiles with), and copy onto the heap instead of a fixed buffer. Only a structure newer than those headers still passes the chain unchanged. Add a regression test that runs the layer between a simulated loader and a mock driver: the timeline-semaphore chain, pEnabledFeatures, an unknown structure, the caller's structures left untouched, per-device YCbCr sampler tracking with handle reuse, and the KHR calibrated-timestamp forwarding. CI installs libvulkan-dev for it. * Mali bridge (#517, by ASEbrahim) on the non-Adreno test branch Squash of Droid-Deck/DroidDeck#517 (ASEbrahim/DroidDeck mali-bridge @6b823341): the Vulkan bridge (tools/vkbridge) that runs the guest's Vulkan on the phone's own driver, with BC1-BC7, clip/cull distance, vertex-divisor and depth-clip emulation and dma-heap memory sharing for Mali. All of the bridge is ASEbrahim's work; squashed only to carry it on this test branch. Combined with #570 on this branch, a device draws through exactly one path: - Adreno: Turnip, unchanged (checked first; the bridge setting is ignored there). - PowerVR / Xclipse with the system HAL and a render node: the system-Vulkan path (#570). - Anything else with the Setup toggle on (Mali): the bridge. * Test branch: keep #570's DXVK build recipe so its pinned PowerVR components verify #602 adds output checks to tools/dxvk/build.sh; tools/powervr/fetch.py pins the exact recipe the published PowerVR DXVK was built from, so the branch carries #570's copy. * Test branch: make #570, #602 and #560/#561 pass each other's tests - tools/dxvk/build.sh: #602's copy (its output checks) and the PowerVR pin in tools/powervr/release.json moved to that file's hash; the recipe's outputs are unchanged. - droiddeck-session: the presentation block sets _bl_cef for #570's PowerVR path, and the UI block keeps main's order, defaulting to -cef-use-angle=vulkan when the block has not run. - Compositor test harnesses: AHardwareBuffer stubs for #570's AHB import, and a stub for vkp_effects_forget_image (main #597) that #561's caller harness predates. * Auto drivers for Mali and Xclipse on the non-Adreno test branch Auto now picks an open driver pair for the GPU, the way it does for Adreno: - Mali (no KGSL, /dev/mali0): the GPU's name ("Mali-G720-Immortalis MC12", from the vendor Vulkan driver or kbase's gpuinfo) picks the PanVK build: G52-G78 (Job Manager), G610/G710, G615 or G720. A Mali none of them covers (G925...), or one whose pair is not published yet, draws through the Mali bridge, switched on by Auto. - Xclipse keeps the system-driver path as Auto; the RADV pair is offered as a manual choice. - The pairs come from The412Banner/droiddeck-nonadreno-drivers (one pre-release per driver, each a DriverBundle zip). Only a device without an Adreno reads that repo. - The importers accept PanVK and RADV libraries (libvulkan_panfrost / libvulkan_radeon). - An open driver pair and the Mali bridge are alternatives: a selected pair turns the bridge off. - TU_DEBUG is only set for Turnip, on an Adreno. Every new branch is reached only when the device has no Adreno; an Adreno's detection, release sources, Auto choice and session environment are unchanged. * Auto: the one-binary PanVK kbase build for Mali v9-v13 on Android 15+ The412Banner/droiddeck-nonadreno-drivers adds panvk-mali-kbase: one PanVK build for G57/G68/G77/G78, G610/G710, G615/G715, G720/G725 and G925, whose display half needs Android 15. On Android 15+ Auto prefers it for every Mali it covers; below 15, and whenever its release is not listed, Auto falls back to the GPU generation's own pair. G52/G76 keep the Job Manager build; a G715/G725/G925 below Android 15, or any Mali no pair covers, draws through the bridge. Every pair stays selectable by hand. * Test: the G925 is in the kbase build's family now * Offer faux123's device PanVK builds (RP4 Pro, Ace, RG556) as manual pairs for their Mali * Detailed graphics logs for non-Adreno test reports - graphics-report.txt in every session folder: the graphics path the session draws with and why (Turnip, system Vulkan #570, Mali bridge #517, or an open driver pair with its release), the driver mode and choices, what Auto would pick, the GPU (family, Vulkan device name, kbase gpuinfo, Android, SoC, kernel), the GPU nodes the app can see (access, device numbers, SELinux label; nothing is opened) and the guest's graphics environment. At the end it adds the compositor's driver lines from app.log and the game launches that were logged. - vulkan-summary.txt: the session's own vulkaninfo --summary with the driver as exported, in the background with a 10 s timeout. - Per-game logs under games/<time>-<appid>/: Proton's log, DXVK's and vkd3d-proton's, Mesa's and PanVK's startup messages and the launch environment, from droiddeck-game-env. Each file is cut to 20 MB and the folder to 120 MB at the session's end. - The per-game logs and the Vulkan summary are on for a GPU that is not an Adreno, and on an Adreno only with Setup's new "Detailed graphics logs" switch. Share logs already takes the whole session folder, so all of it is in the zip, scrubbed as before. * Mali pairs: a stand-in render node, the driver's real library name, and a Mali known by its chip - Auto on Jim's Dimensity 9300+ chose the bridge: the GPU had no name (the app had not asked Vulkan yet and kbase's gpuinfo is unreadable under SELinux), so a Mali-G720 counted as a Mali no build covers. The name now falls back to the GPU the chip is known to carry (Dimensity, Helio, Kompanio, Tensor); and when nothing names it, Auto asks the vendor Vulkan driver off the main thread before it settles for the bridge. Auto logs what it decided and why. - An open Mali driver pair gets /dev/mali0 as the guest's render node, as the bridge does (bindGpuNode, BL_DRM_STANDIN): gamescope needs the node the driver reports. The session shim only answers libdrm's PRIME and GEM-close calls on it, which PanVK on kbase never makes. - A PanVK or RADV runtime half is installed as libvulkan_panfrost.so / libvulkan_radeon.so rather than under Turnip's name; Turnip keeps libvulkan_freedreno.so. - graphics-report.txt names the render node the guest got, adds the session script's driver lines and flags a failed Mali-bridge self-test at the top. * Auto takes the RADV pair on a Samsung Xclipse, with the system route as its fallback A tester's Galaxy S24 FE (Xclipse 940) boots to Steam with the RADV-Xclipse pair, where the system-driver route fails at Steam's OpenGL (glamor and GLX cannot load a driver). Auto now downloads and selects RADV on an Xclipse like the Mali pairs, and falls back to the system route only when RADV is not published. #570's reset of both drivers at each start no longer undoes an open driver pair. PowerVR (vulkan.powervr.so) keeps the system driver as before. * An Exynos with a Mali is a Mali, not an Xclipse The system route took vulkan.samsung.so as Samsung's Xclipse, but an Exynos 1280/1380 (Galaxy Tab S9 FE: Mali-G68) may name its HAL the same; with /dev/mali0 present that HAL is not the system route's, and the device is detected as the Mali it is. The chip table adds the Exynos 1380 and 1280 (s5e8835, s5e8825: Mali-G68). * Auto fetches the open drivers itself; Xclipse named as such; sound and frame-gen in the logs - Auto looks online again when its saved release list predates this device's open drivers, so a Samsung or Mali phone gets its pair without pressing refresh first. - A Samsung Xclipse is labelled "Samsung Xclipse" (not PowerVR), with its own support line. - Per-game logs add audio-streams.txt: PulseAudio's sink inputs 20 s and 60 s into the launch, so a silent game shows whether its stream reached Pulse. - graphics-report.txt states the frame-generation setting and keeps the LSFG gate, FrameGen and audio lines from app.log. * Client sound retries a briefly disconnected AAudio; kbase ioctl refusals explained in the report A Galaxy S24 FE's session had no sound at all: the DirectAudio sink's AAudio stream opened with AAUDIO_ERROR_DISCONNECTED while the output route was still changing at session start, and the plain AAudio fallback failed the same moment. The sink guard now retries DirectAudio, then the plain sink, five times over ten seconds and says which one it got. graphics-report.txt keeps the client-sink and kbase lines, and explains a KBASE_IOCTL_MEM_EXEC_INIT EPERM as the kbase driver's own answer rather than the session's sandbox. * Mali pairs draw the display on the phone's own Mali driver; Xwayland falls back without glamor - Testers' Mali pairs got the guest drawing (gamescope on the stand-in node, Zink on PanVK) but the compositor's PanVK Android half failed on Mali gralloc: it probes AHB formats 0x38/0x3b that mali_gralloc does not have, and "vkCreateImage w/ native buffer failed". Under Auto a Mali pair now leaves the display on the phone's own Vulkan driver (dma-buf, modifiers and AHB all pass the testers' checks) and runs the pair's PanVK in the guest; the Android half stays installed for a manual pick. "Pair in use" now follows the runtime half. - A session after one whose Xwayland crashed (segfault, then gamescope's X11 I/O error) starts Xwayland with XWAYLAND_NO_GLAMOR=1 on a non-Adreno device, and says so in app.log. - graphics-report.txt names the display driver the compositor used, and keeps gralloc, dma-buf, modifier and import lines from app.log and Xwayland's own lines from the session log. * Non-Adreno sound: the daemon falls back on its own; the build's commit in device.txt and the report The S24 FE's session still had no sound: its AAudio refuses DirectAudio's low-latency stream with AAUDIO_ERROR_DISCONNECTED, and the sink guard never got to retry because pactl did not reach the daemon in time ("the daemon did not answer in 15 s"). On a GPU that is not an Adreno the daemon's own start-up script now asks DirectAudio again without low latency and loads the plain AAudio sink underneath it, so a default sink exists whatever pactl does. device.txt and graphics-report.txt name the build label and source commit, so a log says which build it came from. * Non-Adreno sound: no second DirectAudio load (it hung the daemon); the plain AAudio sink underneath On the S24 FE the daemon stopped after DirectAudio's failed low-latency load: the second DirectAudio load in the same daemon never returned, so nothing after it ran and pactl never connected. The daemon now loads only the plain AAudio sink (no low latency) after DirectAudio, inside .nofail, and sets whichever exists as the default. * gamescope: explicit sync off where the kernel or the stand-in node cannot signal its timelines Adds tools/gamescope/patches/0126 (GAMESCOPE_DISABLE_EXPLICIT_SYNC=1 for the nested backends) and sets it on a GPU that is not an Adreno when the kernel is older than 6.6 (no DRM_IOCTL_SYNCOBJ_EVENTFD) or the guest's render node is Mali's stand-in. The graphics report lists it with the rest of the guest's graphics environment. * Test branch: gamescope with the explicit-sync switch (0126), pinned by sha Built by build-gamescope.yml run 38335744351 from0136fb61and published as a component release of The412Banner/droiddeck-nonadreno-drivers; the apk build fetches it by tag and checks the sha. * Sound: nothing that can block in the daemon's start-up; Steam's web compositing in software on open pairs - The S24 FE (US) daemon hung loading the plain AAudio sink from default.pa (its AAudio open blocks rather than fails), so it never finished starting. default.pa is back to DirectAudio alone inside .nofail; the fallbacks are loaded from the app once the daemon answers, each pactl bounded by its timeout (DirectAudio, then the plain sink with and without low latency). The guard waits 30 s and logs, with timestamps, how long the daemon took, whether its socket and pactl exist, and what it finally got. - With an open driver pair on a GPU that is not an Adreno, Steam's CEF composites in software (-cef-disable-gpu-compositing) with GPU raster kept: RADV on Xclipse showed stale, half-written tiles, as #570 saw on PowerVR. The session log says so. * Mali under Auto: the display always on Android's own Vulkan loader; a reboot hint for refused audio Axel's Tab S9 FE still drew its display with the PanVK Android half, set by an earlier build, and stalled at "Waiting for Steam's first frame" on Mali gralloc's format probes. Under Auto a Mali's display now goes to Android's own loader whatever was set before (DriverMenus resets it, and the compositor's driver choice ignores a pair's Android half), with no HAL path to find first. A pair counts as in use when its runtime half is. A manual pick of the PanVK display is kept. When audio.log shows AAUDIO_ERROR_DISCONNECTED, the report and the client-sink line say Android refused the output and that rebooting the phone usually fixes it. * Fix: call SessionPrefs.gpuDriverMode through its instance from Java * Compositor: log each new dma-buf format/modifier/size it imports (for colour and swizzle reports) Steam's software web compositing already covers every open driver pair, Mali included. * Open driver pairs: vkd3d-proton reports the driver's caps; buffer attach/release tracing; Mesa log - With a PanVK or RADV pair (BL_OPEN_DRIVER=1), droiddeck-game-env drops the app-wide VKD3D_FEATURE_LEVEL=12_2 and VKD3D_SHADER_MODEL=6_9 so vkd3d-proton reports what the driver has (an Unreal DX12 game crashed at start on PanVK with the overrides); a game's own setting still wins, and launch.txt records DROIDDECK_VKD3D_CAPS=driver. - The compositor traces, on a GPU that is not an Adreno, the first 400 dma-buf attaches and buffer releases by wl_buffer id and sending line, and logs any release of a buffer already released and not attached again (gamescope's "Compositor released us but we were not acquired" on G925). - Per-game logs no longer set MESA_DEBUG=silent, which kept mesa.log empty. * Tests: the dma-buf quarantine harness follows send_buffer_release's tracing macro * Mali (Venus): David Cimo's Venus route sources and recipes From DavidCimo/droiddeck-mali, branch mali-venus @ 4f838891 (prototype 13; his 12 commits on top of DroidDeck 0.3.1, GPL-3.0), by David Cimo: - tools/venus: Mesa Venus vtest patch (incl. the per-queue submit_mutex fix and the cached-memory frame readback), virglrenderer Android patch, the Mali compatibility layer (the six features DXVK needs: fill modes, multi-viewport, robustness2, clip/cull distance, BC1-BC7) and his tools. - tools/linuxfs/preload/venus.c: keeps Zink's kopper under Venus. - tools/gamescope/patches/0127 (his 0114): shm buffers uploaded on every commit. - docs/development/mali-venus.md: his design notes. - tools/venus/ci-build.sh and build-venus.yml: his build recipes as one CI job that packs the component (vtest server, ICD, layer) as venus.tzst for the apk. Co-authored-by: Dersim Abbas <derabb0620@gmail.com> * Mali (Venus) as a selectable route, by David Cimo Ports David Cimo's Venus route (DavidCimo/droiddeck-mali, mali-venus @ 4f838891) onto this branch as "Mali (Venus)", one tap in Setup next to the Mali bridge: - VenusComponent (his): stages the Venus ICD and runs virglrenderer's vtest server over the phone's own Vulkan driver; the guest gets VK_ICD_FILENAMES, VN_DEBUG=vtest, VTEST_SOCKET_NAME and MESA_VK_WSI_DEBUG=sw. - Selection rule: Venus when Setup's "Mali (Venus)" is on, on a GPU that is neither an Adreno nor a system-driver one (PowerVR, Xclipse), and the apk carries it. It then replaces the Mali bridge and any open driver pair for the session, and the display stays on the phone's own driver. Auto is unchanged (the PanVK pair). Adreno never takes any of it. - The compositor enables only the dma-buf extensions the driver has, logs a vendor driver's extension list, and gives a zero row pitch a packed row (his vk_present.c changes). - The Mali compatibility layer is staged into the runtime on non-Adreno devices only. - The component (vtest server, ICD, layer) comes from build-venus.yml as venus.tzst, published on The412Banner/droiddeck-nonadreno-drivers and pinned by sha in tools/venus/release.env. - gamescope 3.16.29-p15 adds his shm upload patch (0127) next to 0126; pinned by sha. - device.txt and graphics-report.txt name the route. Co-authored-by: Dersim Abbas <derabb0620@gmail.com> * Mali: Venus gamescope without robustness2, the bridge switch honoured and kept, clean driver switches - gamescope 0128: VK_EXT_robustness2 is optional. A Mali driver through Venus has none, and gamescope stopped at "Missing required extension: VK_EXT_robustness2" (the wayland_surface error after it was only the knock-on). Without it, unused descriptor slots point at a 1x1 texture instead of VK_NULL_HANDLE. (David Cimo's Pixel 9 driver has robustness2, so his build never needed this.) gamescope component rebuilt and pinned by sha. - Open-files limit: on a GPU that is not an Adreno the session script raises the soft limit to the hard limit for the whole session tree (a PanVK-kbase session froze on "Too many open files" after ten minutes) and graphics-report.txt shows before and after. A stopgap; the driver leak is fixed separately. - The Mali bridge switch is honoured: when on and usable it wins over a PanVK pair for the session (Venus still wins over it). Selecting a pair no longer switches the bridge off, which is why the switch did not survive an app restart (Auto re-selected the pair at start). Setup says when this phone cannot use the bridge at all. - Driver switching: picking a Mali pair by hand keeps the display on the phone's own driver, as Auto does (a PanVK Android display stalls on Mali gralloc at "Waiting for Steam's first frame", and stayed set after switching back); installing a bundle again replaces it instead of adding a "-2" copy beside the old one. - graphics-report.txt names the runtime driver's folder and libraries, and the previous session's drivers, flagged when they changed. * gamescope p16 (0128 robustness2 optional), pinned by sha; graphics-report says who chose the display driver * Bundle replace only for open non-Adreno pairs; Adreno bundles keep their numbered copies * Rebuilt open drivers reach phones: re-downloaded when their digest changes, release list hourly on non-Adreno The non-Adreno driver releases replace their assets in place (the PanVK kbase sync-file fd leak fix). A download is now remembered with its GitHub sha256, and an asset whose digest changed counts as not installed, so Auto fetches it again and the bundle replaces the old one; the release list is re-read after an hour instead of a day on a GPU that is not an Adreno. * Mali compatibility layer: multiDrawIndirect, depthClipEnable and robustness2 for DXVK on Venus A Galaxy A15 (Mali-G57) on Venus got gamescope and Steam up, but DXVK refused the GPU: "Device does not support required feature 'multiDrawIndirect'". DXVK 3.1 stops at the first missing feature, so the rest of its required list (dxvk_device_info.cpp) was checked against what the testers' Mali drivers cannot have, and covered in the same pass, on top of David Cimo's six: - multiDrawIndirect: reported, taken off the device, and an indirect draw with drawCount > 1 is recorded as single draws at offset + i * stride; maxDrawIndirectCount reported as 2^30. (drawIndirectCount is optional for DXVK and stays as the driver says.) - VK_EXT_depth_clip_enable (required): offered when the driver lacks it; depthClipEnable off in a pipeline becomes depthClampEnable, and the extension's structures never reach the driver. - VK_EXT_robustness2 missing entirely (gamescope saw the same on these phones): offered with robustBufferAccess2 (as robustBufferAccess, as before) and nullDescriptor, emulated by small stand-ins (a zeroed buffer, a 1x1 image) for VK_NULL_HANDLE buffers, image views and vertex buffers in plain, template and push descriptor writes and vertex-buffer binds. A null texel-buffer view stays null (its format is not known). The layer logs every feature it emulates per device. * Venus component22694a38(layer: multiDrawIndirect, depthClipEnable, robustness2), pinned by sha * Non-Adreno round 3: bridge images, Venus GL, shared buffers, Steam GPU drawing - vkbridge: the memory model skips lazily allocated types. Mali-G615 lists DEVICE_LOCAL|LAZILY_ALLOCATED first; taken as the device-local type it left images with no memory type (self-test "image clear/draw: setup" FAIL, gamescope "No compatible image memory type"). - Session: a bridge self-test that fails images or draws falls back to the runtime's driver pair for that session (BL_VKBRIDGE_FALLBACK_ICD); Setup and the graphics report say so. - Venus: the Mali compatibility layer runs in every process but gamescope (DROIDDECK_MALI_COMPAT=1), so Zink in Steam and Xwayland gets nullDescriptor; null texel-buffer views get stand-ins too. Without nullDescriptor the session's GL goes to llvmpipe. - gamescope 0129: a refused compute pipeline is named with its SPIR-V capabilities. - Compositor: a buffer on two surfaces at once (gamescope's planes while a notification is up) is released only when the last one lets go. - Open driver pairs: processes.txt timeline for the first three minutes; Setup "Steam interface: GPU drawing" turns off software web compositing. * Pin gamescope p17 (0129) and Venus55b30fbf* Non-Adreno round 4: Mali GPU-drawing block, gamescope YCbCr retry, GL probe, bridge draw sync - Setup's "Steam interface: GPU drawing" is hidden on Mali and a saved "on" is cleared: with PanVK it rebooted an Edge 60 (G615) as steamwebhelper started. Kept for Xclipse. - gamescope 0130: a composite pipeline refused at start-up is retried with a layout without immutable YCbCr samplers (G615 through Venus refused every composite shader; their SPIR-V has no Float16, so fp16 was not the cause). - Venus: droiddeck-glprobe opens GL through GLX the way the Steam client does, with the loader's reasons logged; when it fails the client's GL goes to llvmpipe. - vkbridge self-test: the draw check waits for the render pass's stores before copying out (a tiler read zeros without the dependency). * Pin gamescope p18 (0130) * Non-Adreno round 5: gamescope memory fallback, Venus destroy guard, OpenAL depot - gamescope 0131: DEVICE_LOCAL|HOST_VISIBLE requests fall back to host-visible memory (the bridge's unified-memory model lists none with both), with the request, type bits and memory types logged on failure; 0130's probe pipeline is kept and skipped when forced. - Venus: GAMESCOPE_NO_YCBCR_SAMPLERS=1 from the start; virglrenderer ignores the destroy of a pipeline it never created instead of marking the context fatal (vtest server exit 137 on a Mali-G615). - Steam's OpenAL depot (229020) maps to oalinst_dll, so games that list it (Hotline Miami) get OpenAL in their prefix. * Pin gamescope p19 (0131) and Venus6199a93f* Non-Adreno round 6: PowerVR PRIME on old kernels, thermal, hang-up names, bridge flippable images, Venus Zink variants - drm.c: PR #570's current PRIME ownership check (F_DUPFD_QUERY, then kcmp, then the F_SETOWN_EX owner marker): on a kernel before 6.10 with kcmp blocked, drmPrimeFDToHandle answered EOPNOTSUPP for every PowerVR import (Pixel 10, Android 17: Zink "failed drmPrimeFDToHandle", Xwayland error 7, gamescope X11 I/O error). The Mali stand-in node is kept. - A /sys/class/thermal the kernel will not list gets an empty directory, so mangoapp no longer aborts on it. - gamescope 0132: a hung-up waitable is named before the abort; 0133: the bridge's flippable images use supported-but-unexportable modifiers instead of asserting. - Venus: when Zink does not start, the loader's view is logged and Zink is retried without ZINK_DEBUG=compact, with ZINK_DESCRIPTORS=auto and without kopper before falling back to llvmpipe. * Pin gamescope p20 (0132, 0133) * Non-Adreno round 7: bridge command-buffer guard, Venus Zink trace, PowerVR mangoapp, Xclipse label - vkbridge server: every command buffer a device hands out is tracked; Begin, End (stream replay) and QueueSubmit refuse one it never allocated or has freed (VK_ERROR_DEVICE_LOST, logged) instead of passing it to the driver, where Mali's vkBeginCommandBuffer crashed the app (SIGSEGV 0x10, Steam's Zink on an Edge 60 and a Tab S11). - vkbridge server: a fault inside the driver while serving a request cuts that client off (SIGSEGV/SIGBUS guard per request) and leaves its objects alone; the app and the other clients carry on. Other faults go to the previous handler. - vkbridge server: the pipeline cache directory is copied; the JNI string it pointed at was freed (garbage in the cache path). - Venus: DROIDDECK_MALI_COMPAT_TRACE=1 makes the Mali compatibility layer log what an application queries before vkCreateDevice; a failed glprobe adds a traced run and vulkaninfo --summary under the same environment. - PowerVR: mangoapp draws with llvmpipe (Zink could not make it a swapchain and it crashed at every start). - Setup: an Xclipse GPU reads "Experimental: RADV or the system Xclipse driver", not the PowerVR line. * Pin Venuse40cd361(layer trace) * Non-Adreno round 8: bridge sharing fallback and crash detail, Venus diagnostics and 1.2 gate, compositor disconnect reasons, RADV upstream - Mali bridge: the self-test fails "memory sharing" when the server offers no host-visible memory type (no sharing method passed, RP4 Pro G77); the session then falls back to the PanVK pair like a failed draw, and Setup says the bridge cannot share memory with that driver. Without a pair to fall back on, the log says so. - Mali bridge: a guarded driver crash now logs the sub-command it was in, the fault address and pc (library + offset via dladdr), the command buffer's level, begin flags, pool flags and state, and the last 32 replayed commands. Begin on an executable buffer from a pool without RESET_COMMAND_BUFFER_BIT is logged once (implicit reset). - Venus: vulkan-summary.txt gets the device extension list and key features (full vulkaninfo in vulkan-full.txt); a failed GL probe runs once with VN_DEBUG=init,result, ZINK_DEBUG=mem, MESA_LOG_LEVEL=debug and the layer trace into glprobe-trace.txt. The Mali compatibility layer's trace covers every pre-device query (devices, memory, queue families, formats, image formats, external handles) and names the extensions. - Venus: off when the phone's driver reports Vulkan below 1.2 (gamescope skips such a device); Setup and the graphics report say why and the session uses the PanVK pair. - Compositor: libwayland's own disconnect reasons go to wayland.log, and a client's buffer may grow to 1 MB before a slow reader is dropped. A session that ended without teardown records Android's exit reasons. - Drivers: RADV-Xclipse upstream (radv-xclipse-upstream) is a manual choice for Xclipse; Auto keeps radv-xclipse. * Tests: the terminal-callers harness stubs the Quest layer and Cardboard modules and Android's system properties * Pin gamescope p21 (main's patches + 0120-0133) and Venusa4d47cf3(layer trace) * Display driver Auto: Quest's patched Turnip first; the system driver only for a Mali, other GPUs keep the generic fallback * Open driver pairs: the installed build's version, its upload date, and an Update when the release is rebuilt in place - The release check records each asset's GitHub id and updated_at; a download records them (with the digest) for an open non-Adreno pair. - Components > GPU drivers: an installed open pair shows its manifest version ("android <tag> + linux <commit>"), one not yet installed shows "built YYYY-MM-DD". A different digest, asset id or upload time on the same tag marks the installed pair "Update available" with an Update action: the pair is downloaded again and replaced in place, and stays selected only if it was. Adreno and DD-Turnip pairs are unchanged. - graphics-report.txt: the pair release line gives the installed and the listed upload dates and flags a newer build; an exact bundle id match is preferred (RADV-Xclipse-upstream is not RADV-Xclipse). * Session preload: drop oom_score_adj raises so lmkd does not treat games as cached apps steam-launch-wrapper starts every game with --oom-score-adjust 900; on Android 900 is CACHED_APP_MIN_ADJ, so lmkd kills the running game first when memory runs short (and on Linux 6.1 the raise becomes the process's permanent minimum). A process in the app cannot go below the score it inherits, so the writes that succeed from inside are raises: every write-open of /proc/<pid|self|thread-self>[/task/<tid>]/oom_score_adj (or oom_adj), through open/openat or fopen, goes to /dev/null, logged once. Based on the oom_score_adj redirect in the preload's opens.c by jdmg28 (https://github.com/jdmg28/DroidDeck-Mali/commit/6be00e00) * Display: why presenting failed on the session-ended screen, and the display driver that really loaded - The compositor records why it cannot present (no Vulkan driver loaded, vkCreateInstance/vkCreateDevice failed, no GPU or graphics queue, no swapchain on the screen with the driver's reason, device lost, GPU work unconfirmed). Each is a DD_PRESENT_FAULT line in wayland.log, read back over JNI or from wayland.log; the session-ended screen shows it instead of a black screen, and graphics-report.txt gets "!! DISPLAY FAILED". A first frame or a working swapchain clears it. - After the display device is created, the driver that answered is logged from VkPhysicalDeviceDriverProperties (device, driverName, driverInfo, driver id, Vulkan version) and reported in graphics-report.txt. When a driver pair's display half was requested but a vendor driver answered (adrenotools falls back to the system driver when the requested one does not load), it says so. Based on DD_PRESENT_FAULT in vk_present.c / waylandcomp_jni.c / SessionActivity by jdmg28 (https://github.com/jdmg28/DroidDeck-Mali/blob/arena/cd8ab18a-droiddeck-mali/app/src/main/cpp/waylandcomp/src/vk_present.c) Based on the adrenotools silent-fallback finding by ryanistr (https://github.com/ryanistr/DroidDeck-Mali/blob/main/docs/development/panvk-mali-g77-bringup.md) * tests: steam presentation test sets 0.5.0's steam_client * Non-Adreno: find every driver pair, Venus before the bridge, fix the bridge's begin info From the logs testers sent sincec9ebb247: - The app read the drivers repo's newest 15 releases only; the component releases (gamescope, Venus) pushed every PanVK pair and RADV-Xclipse past them, so Auto found no pair on any Mali and switched the Mali bridge on. The non-Adreno repo is now read in one page of 100. - Auto's stand-in for a missing pair: Mali (Venus) where the apk carries it (Steam runs on it from the G57 to the G1-Ultra), the bridge otherwise, nothing on a driver below Vulkan 1.2. It no longer clears the Linux driver, does nothing when the list could not be read, records that it chose the route, and turns it off again once a pair is installed. A bridge that earlier builds left on under Auto is taken as Auto's. - Mali bridge: vkBeginCommandBuffer sent its begin info without the count the server decodes, so Mali got a zeroed begin info with stale flags and crashed at 0x10 (Pixel 8 Pro G715, Lenovo G615, Xiaomi G925, Tab S11). - Mali-G1 series (MT6993, arch v14) known by its chip; its label says Venus, not the bridge. - A session Android killed for memory is said so on the next start (the S24 FE "crashes" were low-memory kills, not driver faults). - The in-app updater never installs an official (Adreno-only) build over this test build. * Venus: destroy the device's object mutex after the objects that lock it Bionic aborts on locking a destroyed mutex ("pthread_mutex_lock called on a destroyed mutex", Tab S9 FE G68, Pixel 9a), so a client that exited holding objects took the render worker down. From BattleTempest's DroidDeck-Pixel (kevinbryant551-ux/DroidDeck-Pixel, GPL-3.0). * release check: a session helper may be extracted to another runtime path droiddeck-steam-taskset (#570) goes to usr/local/libexec/droiddeck-steam/taskset, where PATH puts it in front of Steam's own taskset for the client only; 0.5.0's check wanted every droiddeck-* helper at its own path. * Venus component93038b7d(destroyed-mutex fix) * Update MaliBridge to v0.5.1 (#1135) * Mali Auto: never leave a session on the runtime's Turnip Logs from a Retroid Pocket 4 Pro (G77), Galaxy S21 Ultra (G78), Tab S11 Ultra (G925) and Xiaomi G610 showed Auto ending on the runtime's Turnip, where gamescope finds no GPU ("failed to find physical device"): after a pair's trial rolled back, when the pair is on the bad list, and when the driver list could not be read. In each case Auto now switches on its stand-in (Venus, or the bridge) when no open driver, Venus or bridge is set. Under Auto the Mali display also leaves any imported driver, not only a pair's PanVK half (a lone PanVK_G720 import gave a black screen). * Mali bridge: MaliBridge 224b6e36 (v0.5.3 source, by ASEbrahim) From c65062b6 (v0.5.1). v0.5.2/v0.5.3: Steam mode on older Mali drivers (G77/G710/G610/G57) gets past "starting display and audio", OpenGL 4.6 on most Malis through dynamic-rendering and vertex-format emulation, Dimensity 9500 texture fixes, async pipeline compile, RE2 flicker on G925, DirectX on the Pixel 8 family. JNI entry and session env names are unchanged. * gamescope 0134: linear when the backend offers no modifier; Venus on Mesa 26.2.5 0134: CVulkanTexture::BInit asserted !modifiers.empty() when the nested Wayland backend offered no modifier for a flippable image's format (NieR Replicant on a Pixel 9a under Venus, session exit 134). It now tries linear and logs it. Venus ICD: mesa-26.2.5 (venus TLS teardown from an unloaded driver, virtio-gpu devices without 3D skipped); virglrenderer stays on 1.3.0. * gamescope p22-nonadreno (0134 + main's 0116 cursor fix; build run 40159674290) * CI: build the non-Adreno additions before the preload check * build.gradle: drop verifyHandModel (main moved the hand models into the tracking pack) * Venus component venus-proto13-e24ed8ab (Mesa 26.2.5; build run 40161447324) * CI: fetch the Mali (Venus) component again (lost when build.yml took main's jobs) Builda7a1ebf1(run 40161734101) had no assets/venus or libvirgl_test_server.so, so VenusComponent.available() was false and Auto's stand-in would have been the bridge. * Auto: the Mali bridge stands in on a Vulkan 1.1 Mali driver Auto refused both Venus and the bridge when the phone's driver reports Vulkan below 1.2, so on the G77, G78 and G610 (r32p1/r38, Vulkan 1.1) a rolled-back or missing pair left the runtime's Turnip: "failed to find physical device" (Retroid Pocket 4 Pro after the JM pair's trial, Xiaomi 13T). Only Venus needs 1.2; the bridge raises the driver for gamescope and runs on these phones. A Venus switch that cannot start on such a driver no longer counts as a route. * Tests: stub main's g_release_unpaced in the DMA-BUF quarantine probe The probe copies release_buffer out of compositor.c; since the merge it reads g_release_unpaced, which the harness did not declare (setUpClass failed to compile). * Update MaliBridge to v0.5.4 (ASEbrahim) 48477fe6 (tag v0.5.4): Dimensity 9500 green squares and red-tinted textures, Balatro and other OpenGL text, fewer crashes when memory runs low, one screen update per game frame, picture kept after a long background pause (SIGHUP no longer stops the server), wrong colours after changing graphics settings on the G720, more in the session report. The app-side changes of his DroidDeck Mali app (Steam menus setting, per-game setup, Send logs, updates) are not part of the MaliBridge repository and are not in this build. * gamescope 0135: linear when the driver rejects every offered modifier An r32p1 Mali-G610 (Vulkan 1.1) behind the Mali bridge answered both of the backend's modifiers for AR24 with VK_ERROR_FORMAT_NOT_SUPPORTED, so the first flippable image failed and gamescope asserted in vulkan_create_flat_texture (Xiaomi 13T). Linear, when offered, is tried anyway; the bridge makes such images without the handle type and exports them through a dma-heap. If the image cannot be made, BInit fails as before. * gamescope: pin the non-Adreno p23 build (0135) * Tests: the terminal-callers harness stubs take_shm's game-frame detection Main's take_shm now reads surface damage, parent/role/mapped and the HUD state to spot game frames; the harness declares them so the copied function compiles (setUpClass failed in Build APK run 40505286127). No damage, so it stays off. * gamescope: number the test branch's patches 0920-0935 Main now has its own 0121-0125 (window override walk, host-event wake, commit fences, done at import, HUD frame times), which sorted in between this branch's 0120-0135. The branch's sixteen patches move to 0920-0935 with the same last two digits, so they apply after main's set and stay clear of its next numbers. Applying all 38 to gamescope 3.16.29 in the old order and the new one gives the same tree (checked with patch -p1, as build-in-arch.sh does). PATCHES.md and the one code comment that named 0130 follow the new numbers. * check_session_assets: helpers count in usr/local/bin or the Steam taskset folder only The merge kept this branch's pattern (any usr/ target), which main's new test rejects: a helper extracted into a subfolder of usr/local/bin must not count. The pattern now takes usr/local/bin under any name, as on main, plus usr/local/libexec/droiddeck-steam for droiddeck-steam-taskset. Every helper in the overlay is still found extracted in SessionFiles.kt. * gamescope: pin 3.16.29 p24 (non-Adreno) Built from this branch @d13b9d88 (run 41119284267): main a5b035b2's patches plus this branch's, now 0920-0935. All 38 applied, gamescope's tests 2/2. sha256 1d1088d4. * Update MaliBridge to v0.5.4.2 (ASEbrahim) 95a1e600 (tag v0.5.4.2), was v0.5.4 (48477fe6). For the bridge: memory sharing without the handle type where that is all the self-test passed (unflagged buffers, client/memory.c, server_memory.c), the older-Mali (G52-G77) game-mode start fixes, the r38p1 beta geometry-shader erratum (gs_hide), present timeouts and an API cap per process. Client, server and the in-app library all build from the submodule with MaliBridge's own scripts, which did not change; the protocol only gained fields an older server sends as 0. * Session end: the compositor fault query no longer fails the hint when the native library is absent presentationFault() (6778a224) called WaylandCompositor.compositorFault() directly. Main's new SessionEndHintTest runs sessionEndHint() in Robolectric, where libdroiddeckwayland cannot load, so all six cases ended in UnsatisfiedLinkError (Build APK run 41125749537). The call is now wrapped like runtimeDamageHint()'s, and without an answer the hint reads wayland.log as before. * test: stub the GPU busy reader in the caller harness * fix: release build is not debuggable, Mali uses the system display driver, staging failure ends its own session * fix: address non-Adreno review findings * fix: preserve current gamescope and GPU warning behavior * fix: retain graphics failure hints and launch diagnostics * fix: identify non-Adreno GPUs in driver failure hints * test: harnesses follow the external client barrier and effects upscaling changes * fix: client sources return to foreign ownership after the overlay draws --------- Co-authored-by: Leask Wong <i@leaskh.com> Co-authored-by: wallentx <william.allentx@gmail.com> Co-authored-by: Stefan Zijlstra <6730062+stefanz92@users.noreply.github.com> Co-authored-by: William Allen <wallentx@users.noreply.github.com> Co-authored-by: The412Banner <the412banner@users.noreply.github.com> Co-authored-by: Dersim Abbas <derabb0620@gmail.com> Co-authored-by: root <root@localhost.localdomain> Co-authored-by: ASEbrahim <ahmadsmain@gmail.com> Co-authored-by: The412Banner <the412banner@gmail.com> Co-authored-by: The412Banner <205237651+The412Banner@users.noreply.github.com>
This commit is contained in:
215 files changed
+19024
-428
No files matched your search
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
# Function, source line and disassembly at offsets into an aarch64 library, with the NDK's tools.
|
||||
# Run in WSL: bash addr2line.sh <library> <offset>...
|
||||
# The offset is the crash pc minus the library's start in the process's maps (tools/venus/watch-maps.sh).
|
||||
LIB=${1:?library}
|
||||
shift
|
||||
B=/home/delo/android-sdk/ndk/27.3.13750724/toolchains/llvm/prebuilt/linux-x86_64/bin
|
||||
md5sum "$LIB"
|
||||
$B/llvm-addr2line -f -C -i -e "$LIB" "$@"
|
||||
for a in "$@"; do
|
||||
start=$(printf "0x%x" $((a - 48)))
|
||||
stop=$(printf "0x%x" $((a + 16)))
|
||||
$B/llvm-objdump -d --no-show-raw-insn --start-address=$start --stop-address=$stop "$LIB" | tail -n +7
|
||||
done
|
||||
Executable
+146
@@ -0,0 +1,146 @@
|
||||
#!/bin/bash
|
||||
# Builds the Mali (Venus) component in CI, from David Cimo's tools/venus recipes (build-venus-icd.sh,
|
||||
# build-virgl-android.sh, build-layer.sh), into venus.tzst rooted at app/src/main:
|
||||
# jniLibs/arm64-v8a/ libvirgl_test_server.so, libvirgl_render_server.so, libvirglrenderer.so,
|
||||
# libepoxy.so (bionic: the vtest server the app runs over the phone's Vulkan)
|
||||
# assets/venus/ libvulkan_virtio.so (glibc: Mesa's Venus ICD for the session)
|
||||
# assets/linuxfs/usr/local/... the Mali compatibility layer and its manifest
|
||||
# Needs: docker (Debian trixie for the glibc parts), the NDK in $ANDROID_NDK_HOME, meson, ninja.
|
||||
set -euo pipefail
|
||||
REPO=$(cd "$(dirname "$0")/../.." && pwd)
|
||||
W=${WORK:-$PWD/venus-work}
|
||||
OUT=$W/out
|
||||
rm -rf "$OUT" && mkdir -p "$OUT/jniLibs/arm64-v8a" "$OUT/assets/venus" \
|
||||
"$OUT/assets/linuxfs/usr/local/lib/droiddeck-mali" "$OUT/assets/linuxfs/usr/local/share/vulkan/implicit_layer.d"
|
||||
|
||||
# --- Linux side: Mesa 26.2.5's Venus ICD and the Mali compatibility layer (aarch64 glibc) ---
|
||||
docker run --rm --platform linux/amd64 -v "$W":/w -v "$REPO/tools/venus":/venus:ro -w /tmp debian:trixie bash -c '
|
||||
set -euo pipefail
|
||||
export DEBIAN_FRONTEND=noninteractive
|
||||
dpkg --add-architecture arm64
|
||||
apt-get update -qq
|
||||
apt-get install -y -qq --no-install-recommends git ca-certificates curl crossbuild-essential-arm64 meson ninja-build \
|
||||
pkg-config python3-mako python3-yaml python3-packaging bison flex glslang-tools libwayland-bin wayland-protocols \
|
||||
libdrm-dev:arm64 libwayland-dev:arm64 libx11-dev:arm64 libx11-xcb-dev:arm64 libxcb1-dev:arm64 \
|
||||
libxcb-dri3-dev:arm64 libxcb-present-dev:arm64 libxcb-randr0-dev:arm64 libxcb-shm0-dev:arm64 \
|
||||
libxcb-sync-dev:arm64 libxcb-xfixes0-dev:arm64 libxshmfence-dev:arm64 libxrandr-dev:arm64 \
|
||||
libexpat1-dev:arm64 zlib1g-dev:arm64 libzstd-dev:arm64 >/dev/null
|
||||
cat > /tmp/arm64.ini <<EOC
|
||||
[binaries]
|
||||
c = '"'"'aarch64-linux-gnu-gcc'"'"'
|
||||
cpp = '"'"'aarch64-linux-gnu-g++'"'"'
|
||||
ar = '"'"'aarch64-linux-gnu-ar'"'"'
|
||||
strip = '"'"'aarch64-linux-gnu-strip'"'"'
|
||||
nm = '"'"'aarch64-linux-gnu-nm'"'"'
|
||||
pkg-config = '"'"'pkg-config'"'"'
|
||||
[properties]
|
||||
pkg_config_libdir = ['"'"'/usr/lib/aarch64-linux-gnu/pkgconfig'"'"', '"'"'/usr/share/pkgconfig'"'"']
|
||||
[host_machine]
|
||||
system = '"'"'linux'"'"'
|
||||
cpu_family = '"'"'aarch64'"'"'
|
||||
cpu = '"'"'armv8-a'"'"'
|
||||
endian = '"'"'little'"'"'
|
||||
EOC
|
||||
git clone -q --depth 1 -b mesa-26.2.5 https://gitlab.freedesktop.org/mesa/mesa.git
|
||||
(cd mesa && git apply /venus/mesa-venus-vtest.patch)
|
||||
meson setup venus-build mesa --cross-file /tmp/arm64.ini --prefix=/usr --libdir=lib \
|
||||
--buildtype=release -Db_ndebug=true \
|
||||
-Dvulkan-drivers=virtio -Dgallium-drivers= -Dplatforms=x11,wayland \
|
||||
-Dllvm=disabled -Dglx=disabled -Degl=disabled -Dgbm=disabled -Dopengl=false -Dgles1=disabled -Dgles2=disabled \
|
||||
-Dvideo-codecs= -Dvulkan-layers= -Dtools= -Dbuild-tests=false -Dvalgrind=disabled -Dlibunwind=disabled \
|
||||
-Dzstd=enabled -Dshared-glapi=disabled >/dev/null
|
||||
ninja -C venus-build src/virtio/vulkan/libvulkan_virtio.so
|
||||
aarch64-linux-gnu-strip --strip-unneeded -o /w/out/assets/venus/libvulkan_virtio.so venus-build/src/virtio/vulkan/libvulkan_virtio.so
|
||||
echo "venus icd min glibc: $(aarch64-linux-gnu-objdump -T /w/out/assets/venus/libvulkan_virtio.so | grep -o "GLIBC_[0-9.]*" | sort -Vu | tail -1)"
|
||||
SDK=vulkan-sdk-1.4.357.0
|
||||
git -c advice.detachedHead=false clone -q --depth 1 -b $SDK https://github.com/KhronosGroup/Vulkan-Headers.git /tmp/vulkan
|
||||
git -c advice.detachedHead=false clone -q --depth 1 -b $SDK https://github.com/KhronosGroup/SPIRV-Headers.git /tmp/spirv
|
||||
mkdir -p /tmp/bcdec
|
||||
curl -sSfL -o /tmp/bcdec/bcdec.h https://raw.githubusercontent.com/iOrange/bcdec/80859ed3b7afb1c527a2a99d70c61457bea72d0c/bcdec.h
|
||||
L=/w/out/assets/linuxfs/usr/local
|
||||
aarch64-linux-gnu-gcc -shared -fPIC -O2 -Wall -Wextra -Wno-unused-parameter -fvisibility=hidden -pthread \
|
||||
-I/tmp/vulkan/include -I/tmp/spirv/include -I/tmp/bcdec \
|
||||
-o $L/lib/droiddeck-mali/libVkLayer_droiddeck_mali_compat.so /venus/layer/*.c
|
||||
aarch64-linux-gnu-strip --strip-unneeded $L/lib/droiddeck-mali/libVkLayer_droiddeck_mali_compat.so
|
||||
cp /venus/layer/VkLayer_droiddeck_mali_compat.json $L/share/vulkan/implicit_layer.d/
|
||||
chmod -R a+rwX /w/out
|
||||
'
|
||||
|
||||
# --- Android side: libepoxy + virglrenderer 1.3.0 (venus, vtest) for arm64 with the NDK ---
|
||||
NDK=${ANDROID_NDK_HOME:?}
|
||||
TC=$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin
|
||||
API=29
|
||||
A=$W/android
|
||||
AOUT=$A/out
|
||||
mkdir -p "$A/cross" "$A/compat/log" "$A/compat/cutils" "$AOUT/lib/pkgconfig"
|
||||
cd "$A"
|
||||
git clone -q --depth 1 -b 1.5.10 https://github.com/anholt/libepoxy.git
|
||||
git clone -q --depth 1 -b v1.4.365 https://github.com/KhronosGroup/Vulkan-Headers.git
|
||||
git clone -q --depth 1 -b virglrenderer-1.3.0 https://gitlab.freedesktop.org/virgl/virglrenderer.git
|
||||
git -C virglrenderer apply "$REPO/tools/venus/virglrenderer-android.patch"
|
||||
git -C virglrenderer apply "$REPO/tools/venus/virglrenderer-destroy-unknown-pipeline.patch"
|
||||
git -C virglrenderer apply "$REPO/tools/venus/virglrenderer-destroy-mutex-after-objects.patch"
|
||||
cat > compat/log/log.h <<'EOC'
|
||||
#pragma once
|
||||
#include <android/log.h>
|
||||
#define LOG_PRI(prio, tag, ...) __android_log_print(prio, tag, __VA_ARGS__)
|
||||
EOC
|
||||
cat > compat/cutils/properties.h <<'EOC'
|
||||
#pragma once
|
||||
#include <string.h>
|
||||
#include <sys/system_properties.h>
|
||||
#define PROPERTY_VALUE_MAX PROP_VALUE_MAX
|
||||
#define PROPERTY_KEY_MAX 32
|
||||
static inline int property_get(const char *key, char *value, const char *def)
|
||||
{
|
||||
int len = __system_property_get(key, value);
|
||||
if (len <= 0 && def) { strcpy(value, def); len = (int)strlen(def); }
|
||||
return len;
|
||||
}
|
||||
EOC
|
||||
cat > "$AOUT/lib/pkgconfig/vulkan.pc" <<EOC
|
||||
prefix=$A/Vulkan-Headers
|
||||
includedir=\${prefix}/include
|
||||
Name: vulkan
|
||||
Description: Vulkan headers
|
||||
Version: 1.4.365
|
||||
Cflags: -I\${includedir}
|
||||
Libs: -lvulkan
|
||||
EOC
|
||||
cat > cross/android.ini <<EOC
|
||||
[binaries]
|
||||
c = '$TC/aarch64-linux-android$API-clang'
|
||||
cpp = '$TC/aarch64-linux-android$API-clang++'
|
||||
ar = '$TC/llvm-ar'
|
||||
strip = '$TC/llvm-strip'
|
||||
pkg-config = '/usr/bin/pkg-config'
|
||||
[built-in options]
|
||||
c_args = ['-I$A/Vulkan-Headers/include', '-I$A/compat']
|
||||
c_link_args = ['-llog', '-lnativewindow', '-Wl,-rpath=\$ORIGIN', '-Wl,-z,max-page-size=16384']
|
||||
cpp_link_args = ['-Wl,-rpath=\$ORIGIN', '-Wl,-z,max-page-size=16384']
|
||||
[properties]
|
||||
pkg_config_libdir = '$AOUT/lib/pkgconfig'
|
||||
[host_machine]
|
||||
system = 'android'
|
||||
cpu_family = 'aarch64'
|
||||
cpu = 'armv8'
|
||||
endian = 'little'
|
||||
EOC
|
||||
export PKG_CONFIG_LIBDIR=$AOUT/lib/pkgconfig PKG_CONFIG_PATH=
|
||||
meson setup epoxy-build libepoxy --cross-file cross/android.ini --prefix="$AOUT" --libdir=lib \
|
||||
--buildtype=release -Degl=yes -Dglx=no -Dx11=false -Dtests=false >/dev/null
|
||||
ninja -C epoxy-build install >/dev/null
|
||||
meson setup virgl-build virglrenderer --cross-file cross/android.ini --prefix="$AOUT" --libdir=lib \
|
||||
--buildtype=release -Dvenus=true -Dplatforms=auto -Drender-server-worker=thread \
|
||||
-Dvulkan-dload=true -Dcheck-gl-errors=false -Dtests=false >/dev/null
|
||||
ninja -C virgl-build install >/dev/null
|
||||
J=$OUT/jniLibs/arm64-v8a
|
||||
cp "$AOUT/bin/virgl_test_server" "$J/libvirgl_test_server.so"
|
||||
cp "$AOUT/libexec/virgl_render_server" "$J/libvirgl_render_server.so"
|
||||
cp "$AOUT/lib/libvirglrenderer.so" "$AOUT/lib/libepoxy.so" "$J/"
|
||||
for f in "$J"/*.so; do "$TC/llvm-strip" --strip-unneeded "$f"; done
|
||||
|
||||
cd "$OUT"
|
||||
find . -type f | sort
|
||||
tar --owner=0 --group=0 --numeric-owner --sort=name -cf - . | zstd -19 -T0 -q -o "$W/venus.tzst"
|
||||
sha256sum "$W/venus.tzst" | tee "$W/venus.tzst.sha256"
|
||||
@@ -0,0 +1,69 @@
|
||||
/*
|
||||
* droiddeck-glprobe: does OpenGL through GLX start on this session's display, the way the Steam
|
||||
* client's first window asks for it (glXChooseVisual, then a context)? Run once before the client
|
||||
* on the Mali (Venus) route, where Zink failed on a Mali-G615 with nothing but "failed to load
|
||||
* driver: zink" and the client gave up ("UpdateUI GL not available"). Exit 0 when a visual and a
|
||||
* context come back, 1 when GL does not start, 2 when there is no X display or no libGL.
|
||||
* libX11 and libGL are opened at run time, so this builds without their headers.
|
||||
*/
|
||||
#include <dlfcn.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef void *(*open_display_fn)(const char *);
|
||||
typedef int (*close_display_fn)(void *);
|
||||
typedef int (*default_screen_fn)(void *);
|
||||
typedef void *(*choose_visual_fn)(void *, int, int *);
|
||||
typedef void *(*create_context_fn)(void *, void *, void *, int);
|
||||
typedef void (*destroy_context_fn)(void *, void *);
|
||||
typedef int (*free_fn)(void *);
|
||||
|
||||
#define GLX_RGBA 4
|
||||
#define GLX_DOUBLEBUFFER 5
|
||||
#define GLX_RED_SIZE 8
|
||||
#define GLX_GREEN_SIZE 9
|
||||
#define GLX_BLUE_SIZE 10
|
||||
|
||||
int main(void)
|
||||
{
|
||||
void *x11 = dlopen("libX11.so.6", RTLD_NOW | RTLD_GLOBAL);
|
||||
void *gl = dlopen("libGL.so.1", RTLD_NOW | RTLD_GLOBAL);
|
||||
if (!x11 || !gl) {
|
||||
printf("glprobe: cannot open %s: %s\n", x11 ? "libGL.so.1" : "libX11.so.6", dlerror());
|
||||
return 2;
|
||||
}
|
||||
open_display_fn open_display = (open_display_fn)dlsym(x11, "XOpenDisplay");
|
||||
close_display_fn close_display = (close_display_fn)dlsym(x11, "XCloseDisplay");
|
||||
default_screen_fn default_screen = (default_screen_fn)dlsym(x11, "XDefaultScreen");
|
||||
free_fn x_free = (free_fn)dlsym(x11, "XFree");
|
||||
choose_visual_fn choose_visual = (choose_visual_fn)dlsym(gl, "glXChooseVisual");
|
||||
create_context_fn create_context = (create_context_fn)dlsym(gl, "glXCreateContext");
|
||||
destroy_context_fn destroy_context = (destroy_context_fn)dlsym(gl, "glXDestroyContext");
|
||||
if (!open_display || !close_display || !default_screen || !x_free || !choose_visual || !create_context ||
|
||||
!destroy_context) {
|
||||
printf("glprobe: missing X11/GLX entry points\n");
|
||||
return 2;
|
||||
}
|
||||
void *dpy = open_display(NULL);
|
||||
if (!dpy) {
|
||||
printf("glprobe: no X display\n");
|
||||
return 2;
|
||||
}
|
||||
int attrs[] = {GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, 0};
|
||||
void *visual = choose_visual(dpy, default_screen(dpy), attrs);
|
||||
if (!visual) {
|
||||
printf("glprobe: FAIL glXChooseVisual\n");
|
||||
close_display(dpy);
|
||||
return 1;
|
||||
}
|
||||
void *ctx = create_context(dpy, visual, NULL, 1);
|
||||
x_free(visual);
|
||||
if (!ctx) {
|
||||
printf("glprobe: FAIL glXCreateContext\n");
|
||||
close_display(dpy);
|
||||
return 1;
|
||||
}
|
||||
destroy_context(dpy, ctx);
|
||||
close_display(dpy);
|
||||
printf("glprobe: OK (visual and context)\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#!/bin/bash
|
||||
# vulkaninfo as the session's Linux processes see the GPU: through Venus, and through the Mali
|
||||
# compatibility layer when DROIDDECK_MALI_COMPAT is 1. Needs a running session (the vtest server).
|
||||
# Run from Git Bash: MSYS_NO_PATHCONV=1 bash tools/venus/guest-vulkaninfo.sh [0|1] > vulkaninfo.txt
|
||||
set -euo pipefail
|
||||
COMPAT=${1:-1}
|
||||
A=/c/Users/derab/tools/platform-tools/adb.exe
|
||||
PKG=com.droiddeck.launcher
|
||||
F=/data/user/0/$PKG/files
|
||||
APK=$($A shell pm path $PKG | head -1 | tr -d '\r' | sed 's/^package://')
|
||||
L=${APK%/base.apk}/lib/arm64
|
||||
$A exec-out "run-as $PKG sh -c 'PROOT_TMP_DIR=/data/user/0/$PKG/cache PROOT_LOADER=$L/libproot-loader.so \
|
||||
$L/libproot.so -r $F/linuxfs -w /root -b /dev -b /proc -b /sys -b $F /usr/bin/env -i HOME=/root \
|
||||
PATH=/usr/bin:/bin DROIDDECK_MALI_COMPAT=$COMPAT VK_ICD_FILENAMES=$F/venus/virtio_icd.json VN_DEBUG=vtest \
|
||||
VTEST_SOCKET_NAME=$F/venus/vtest.sock vulkaninfo 2>&1'"
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"file_format_version": "1.2.0",
|
||||
"layer": {
|
||||
"name": "VK_LAYER_DROIDDECK_mali_compat",
|
||||
"type": "GLOBAL",
|
||||
"library_path": "/usr/local/lib/droiddeck-mali/libVkLayer_droiddeck_mali_compat.so",
|
||||
"api_version": "1.4.357",
|
||||
"implementation_version": "1",
|
||||
"description": "Features DXVK requires and Mali lacks, made up for",
|
||||
"functions": {
|
||||
"vkNegotiateLoaderLayerInterfaceVersion": "vkNegotiateLoaderLayerInterfaceVersion"
|
||||
},
|
||||
"disable_environment": {
|
||||
"DROIDDECK_MALI_COMPAT_DISABLE": "1"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,869 @@
|
||||
/*
|
||||
* BC1-BC7 textures on a GPU without them: each BC image is created in an uncompressed format, and
|
||||
* every upload into one is decoded on the CPU while the copy is recorded. The decoded texels go
|
||||
* through a staging buffer of the layer's own, which lives until the command buffer is reset.
|
||||
* Uploads must come from memory the application has mapped, as DXVK's staging memory always is.
|
||||
*/
|
||||
#include "mali_compat.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BCDEC_IMPLEMENTATION
|
||||
#define BCDEC_STATIC
|
||||
#define BCDEC_BC4BC5_PRECISE
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wunused-function"
|
||||
#include "bcdec.h"
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
#define HANDLE(h) ((uint64_t)(uintptr_t)(h))
|
||||
#define STAGING_BLOCK_SIZE (16u << 20)
|
||||
#define STAGING_KEPT (64u << 20)
|
||||
|
||||
enum bc_kind { BC1, BC2, BC3, BC4, BC5, BC6H, BC7 };
|
||||
|
||||
struct bc_format {
|
||||
VkFormat format;
|
||||
VkFormat substitute;
|
||||
enum bc_kind kind;
|
||||
uint32_t block_bytes;
|
||||
uint32_t texel_bytes;
|
||||
bool is_signed;
|
||||
/* BC1's RGB formats have no alpha; its 1-bit transparency must read as opaque. */
|
||||
bool opaque;
|
||||
};
|
||||
|
||||
static const struct bc_format bc_formats[] = {
|
||||
{VK_FORMAT_BC1_RGB_UNORM_BLOCK, VK_FORMAT_R8G8B8A8_UNORM, BC1, 8, 4, false, true},
|
||||
{VK_FORMAT_BC1_RGB_SRGB_BLOCK, VK_FORMAT_R8G8B8A8_SRGB, BC1, 8, 4, false, true},
|
||||
{VK_FORMAT_BC1_RGBA_UNORM_BLOCK, VK_FORMAT_R8G8B8A8_UNORM, BC1, 8, 4, false, false},
|
||||
{VK_FORMAT_BC1_RGBA_SRGB_BLOCK, VK_FORMAT_R8G8B8A8_SRGB, BC1, 8, 4, false, false},
|
||||
{VK_FORMAT_BC2_UNORM_BLOCK, VK_FORMAT_R8G8B8A8_UNORM, BC2, 16, 4, false, false},
|
||||
{VK_FORMAT_BC2_SRGB_BLOCK, VK_FORMAT_R8G8B8A8_SRGB, BC2, 16, 4, false, false},
|
||||
{VK_FORMAT_BC3_UNORM_BLOCK, VK_FORMAT_R8G8B8A8_UNORM, BC3, 16, 4, false, false},
|
||||
{VK_FORMAT_BC3_SRGB_BLOCK, VK_FORMAT_R8G8B8A8_SRGB, BC3, 16, 4, false, false},
|
||||
{VK_FORMAT_BC4_UNORM_BLOCK, VK_FORMAT_R8_UNORM, BC4, 8, 1, false, false},
|
||||
{VK_FORMAT_BC4_SNORM_BLOCK, VK_FORMAT_R8_SNORM, BC4, 8, 1, true, false},
|
||||
{VK_FORMAT_BC5_UNORM_BLOCK, VK_FORMAT_R8G8_UNORM, BC5, 16, 2, false, false},
|
||||
{VK_FORMAT_BC5_SNORM_BLOCK, VK_FORMAT_R8G8_SNORM, BC5, 16, 2, true, false},
|
||||
{VK_FORMAT_BC6H_UFLOAT_BLOCK, VK_FORMAT_R16G16B16A16_SFLOAT, BC6H, 16, 8, false, false},
|
||||
{VK_FORMAT_BC6H_SFLOAT_BLOCK, VK_FORMAT_R16G16B16A16_SFLOAT, BC6H, 16, 8, true, false},
|
||||
{VK_FORMAT_BC7_UNORM_BLOCK, VK_FORMAT_R8G8B8A8_UNORM, BC7, 16, 4, false, false},
|
||||
{VK_FORMAT_BC7_SRGB_BLOCK, VK_FORMAT_R8G8B8A8_SRGB, BC7, 16, 4, false, false},
|
||||
};
|
||||
|
||||
static const struct bc_format *find_bc(VkFormat format)
|
||||
{
|
||||
for (size_t i = 0; i < sizeof(bc_formats) / sizeof(bc_formats[0]); i++) {
|
||||
if (bc_formats[i].format == format)
|
||||
return &bc_formats[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
VkFormat bc_substitute(VkFormat format)
|
||||
{
|
||||
const struct bc_format *bc = find_bc(format);
|
||||
return bc ? bc->substitute : VK_FORMAT_UNDEFINED;
|
||||
}
|
||||
|
||||
/* What a real BC format allows: sampling and copies. */
|
||||
#define BC_FEATURES \
|
||||
(VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT | \
|
||||
VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_MINMAX_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT | \
|
||||
VK_FORMAT_FEATURE_TRANSFER_DST_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT)
|
||||
#define BC_USAGE (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT)
|
||||
|
||||
static const struct bc_format *physical_bc(VkPhysicalDevice physical_device, VkFormat format, struct instance **inst)
|
||||
{
|
||||
*inst = instance_from_handle(physical_device);
|
||||
if (!(physical_device_gaps(*inst, physical_device) & GAP_BC))
|
||||
return NULL;
|
||||
return find_bc(format);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceFormatProperties(VkPhysicalDevice physical_device, VkFormat format,
|
||||
VkFormatProperties *properties)
|
||||
{
|
||||
struct instance *inst;
|
||||
const struct bc_format *bc = physical_bc(physical_device, format, &inst);
|
||||
inst->GetPhysicalDeviceFormatProperties(physical_device, bc ? bc->substitute : format, properties);
|
||||
if (bc) {
|
||||
properties->optimalTilingFeatures &= BC_FEATURES;
|
||||
properties->linearTilingFeatures = 0;
|
||||
properties->bufferFeatures = 0;
|
||||
}
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physical_device, VkFormat format,
|
||||
VkFormatProperties2 *properties)
|
||||
{
|
||||
struct instance *inst;
|
||||
const struct bc_format *bc = physical_bc(physical_device, format, &inst);
|
||||
inst->GetPhysicalDeviceFormatProperties2(physical_device, bc ? bc->substitute : format, properties);
|
||||
if (!bc)
|
||||
return;
|
||||
properties->formatProperties.optimalTilingFeatures &= BC_FEATURES;
|
||||
properties->formatProperties.linearTilingFeatures = 0;
|
||||
properties->formatProperties.bufferFeatures = 0;
|
||||
for (VkBaseOutStructure *s = properties->pNext; s; s = s->pNext) {
|
||||
if (s->sType == VK_STRUCTURE_TYPE_FORMAT_PROPERTIES_3) {
|
||||
VkFormatProperties3 *p3 = (VkFormatProperties3 *)s;
|
||||
p3->optimalTilingFeatures &= (VkFormatFeatureFlags2)BC_FEATURES;
|
||||
p3->linearTilingFeatures = 0;
|
||||
p3->bufferFeatures = 0;
|
||||
} else if (s->sType == VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_EXT) {
|
||||
((VkDrmFormatModifierPropertiesListEXT *)s)->drmFormatModifierCount = 0;
|
||||
} else if (s->sType == VK_STRUCTURE_TYPE_DRM_FORMAT_MODIFIER_PROPERTIES_LIST_2_EXT) {
|
||||
((VkDrmFormatModifierPropertiesList2EXT *)s)->drmFormatModifierCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL GetPhysicalDeviceImageFormatProperties(
|
||||
VkPhysicalDevice physical_device, VkFormat format, VkImageType type, VkImageTiling tiling,
|
||||
VkImageUsageFlags usage, VkImageCreateFlags flags, VkImageFormatProperties *properties)
|
||||
{
|
||||
struct instance *inst;
|
||||
const struct bc_format *bc = physical_bc(physical_device, format, &inst);
|
||||
if (!bc)
|
||||
return inst->GetPhysicalDeviceImageFormatProperties(physical_device, format, type, tiling, usage, flags,
|
||||
properties);
|
||||
if (tiling != VK_IMAGE_TILING_OPTIMAL || (usage & ~BC_USAGE))
|
||||
return VK_ERROR_FORMAT_NOT_SUPPORTED;
|
||||
return inst->GetPhysicalDeviceImageFormatProperties(physical_device, bc->substitute, type, tiling, usage,
|
||||
flags & ~VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT,
|
||||
properties);
|
||||
}
|
||||
|
||||
/* A view format list naming BC formats, as one naming their substitutes. */
|
||||
struct format_list {
|
||||
VkImageFormatListCreateInfo *list;
|
||||
const VkFormat *original;
|
||||
VkFormat formats[16];
|
||||
};
|
||||
|
||||
static void substitute_format_list(const void *chain, VkFormat substitute, struct format_list *saved)
|
||||
{
|
||||
saved->list = NULL;
|
||||
for (const VkBaseInStructure *s = chain; s; s = s->pNext) {
|
||||
if (s->sType != VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO)
|
||||
continue;
|
||||
VkImageFormatListCreateInfo *list = (VkImageFormatListCreateInfo *)s;
|
||||
uint32_t count = 0;
|
||||
saved->formats[count++] = substitute;
|
||||
for (uint32_t i = 0; i < list->viewFormatCount && count < 16; i++) {
|
||||
VkFormat format = bc_substitute(list->pViewFormats[i]);
|
||||
/* Uncompressed views of the compressed blocks have nothing to alias any more. */
|
||||
if (format == VK_FORMAT_UNDEFINED)
|
||||
continue;
|
||||
bool seen = false;
|
||||
for (uint32_t k = 0; k < count; k++)
|
||||
seen |= saved->formats[k] == format;
|
||||
if (!seen)
|
||||
saved->formats[count++] = format;
|
||||
}
|
||||
saved->list = list;
|
||||
saved->original = list->pViewFormats;
|
||||
list->pViewFormats = saved->formats;
|
||||
list->viewFormatCount = count;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void restore_format_list(struct format_list *saved, uint32_t original_count)
|
||||
{
|
||||
if (saved->list) {
|
||||
saved->list->pViewFormats = saved->original;
|
||||
saved->list->viewFormatCount = original_count;
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t format_list_count(const void *chain)
|
||||
{
|
||||
for (const VkBaseInStructure *s = chain; s; s = s->pNext) {
|
||||
if (s->sType == VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO)
|
||||
return ((const VkImageFormatListCreateInfo *)s)->viewFormatCount;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL GetPhysicalDeviceImageFormatProperties2(
|
||||
VkPhysicalDevice physical_device, const VkPhysicalDeviceImageFormatInfo2 *info,
|
||||
VkImageFormatProperties2 *properties)
|
||||
{
|
||||
struct instance *inst;
|
||||
const struct bc_format *bc = physical_bc(physical_device, info->format, &inst);
|
||||
if (!bc)
|
||||
return inst->GetPhysicalDeviceImageFormatProperties2(physical_device, info, properties);
|
||||
if (info->tiling != VK_IMAGE_TILING_OPTIMAL || (info->usage & ~BC_USAGE))
|
||||
return VK_ERROR_FORMAT_NOT_SUPPORTED;
|
||||
VkPhysicalDeviceImageFormatInfo2 copy = *info;
|
||||
copy.format = bc->substitute;
|
||||
copy.flags &= ~VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT;
|
||||
uint32_t list_count = format_list_count(info->pNext);
|
||||
struct format_list saved;
|
||||
substitute_format_list(info->pNext, bc->substitute, &saved);
|
||||
VkResult result = inst->GetPhysicalDeviceImageFormatProperties2(physical_device, ©, properties);
|
||||
restore_format_list(&saved, list_count);
|
||||
return result;
|
||||
}
|
||||
|
||||
struct bc_image {
|
||||
const struct bc_format *format;
|
||||
uint32_t array_layers;
|
||||
};
|
||||
|
||||
static void substitute_image_info(const struct bc_format *bc, VkImageCreateInfo *info)
|
||||
{
|
||||
info->format = bc->substitute;
|
||||
info->flags &= ~VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT;
|
||||
info->usage &= BC_USAGE;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL CreateImage(VkDevice device, const VkImageCreateInfo *info,
|
||||
const VkAllocationCallbacks *allocator, VkImage *image)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
const struct bc_format *bc = find_bc(info->format);
|
||||
if (!bc)
|
||||
return dev->CreateImage(device, info, allocator, image);
|
||||
|
||||
VkImageCreateInfo copy = *info;
|
||||
substitute_image_info(bc, ©);
|
||||
uint32_t list_count = format_list_count(info->pNext);
|
||||
struct format_list saved;
|
||||
substitute_format_list(info->pNext, bc->substitute, &saved);
|
||||
VkResult result = dev->CreateImage(device, ©, allocator, image);
|
||||
restore_format_list(&saved, list_count);
|
||||
if (result != VK_SUCCESS)
|
||||
return result;
|
||||
|
||||
struct bc_image *tracked = malloc(sizeof(*tracked));
|
||||
tracked->format = bc;
|
||||
tracked->array_layers = info->arrayLayers;
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
map_put(&dev->images, HANDLE(*image), tracked);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL DestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *allocator)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
free(map_remove(&dev->images, HANDLE(image)));
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
dev->DestroyImage(device, image, allocator);
|
||||
}
|
||||
|
||||
static const struct bc_image *tracked_image(struct device *dev, VkImage image)
|
||||
{
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
const struct bc_image *tracked = map_get(&dev->images, HANDLE(image));
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
return tracked;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL CreateImageView(VkDevice device, const VkImageViewCreateInfo *info,
|
||||
const VkAllocationCallbacks *allocator, VkImageView *view)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
const struct bc_image *tracked = tracked_image(dev, info->image);
|
||||
if (!tracked)
|
||||
return dev->CreateImageView(device, info, allocator, view);
|
||||
VkImageViewCreateInfo copy = *info;
|
||||
VkFormat format = bc_substitute(info->format);
|
||||
copy.format = format != VK_FORMAT_UNDEFINED ? format : tracked->format->substitute;
|
||||
return dev->CreateImageView(device, ©, allocator, view);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL GetDeviceImageMemoryRequirements(VkDevice device,
|
||||
const VkDeviceImageMemoryRequirements *info,
|
||||
VkMemoryRequirements2 *requirements)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
const struct bc_format *bc = find_bc(info->pCreateInfo->format);
|
||||
if (!bc) {
|
||||
dev->GetDeviceImageMemoryRequirements(device, info, requirements);
|
||||
return;
|
||||
}
|
||||
VkImageCreateInfo image = *info->pCreateInfo;
|
||||
substitute_image_info(bc, &image);
|
||||
VkDeviceImageMemoryRequirements copy = *info;
|
||||
copy.pCreateInfo = ℑ
|
||||
dev->GetDeviceImageMemoryRequirements(device, ©, requirements);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL GetDeviceImageSparseMemoryRequirements(
|
||||
VkDevice device, const VkDeviceImageMemoryRequirements *info, uint32_t *count,
|
||||
VkSparseImageMemoryRequirements2 *requirements)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
const struct bc_format *bc = find_bc(info->pCreateInfo->format);
|
||||
if (!bc) {
|
||||
dev->GetDeviceImageSparseMemoryRequirements(device, info, count, requirements);
|
||||
return;
|
||||
}
|
||||
VkImageCreateInfo image = *info->pCreateInfo;
|
||||
substitute_image_info(bc, &image);
|
||||
VkDeviceImageMemoryRequirements copy = *info;
|
||||
copy.pCreateInfo = ℑ
|
||||
dev->GetDeviceImageSparseMemoryRequirements(device, ©, count, requirements);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL GetDeviceImageSubresourceLayout(VkDevice device,
|
||||
const VkDeviceImageSubresourceInfo *info,
|
||||
VkSubresourceLayout2 *layout)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
const struct bc_format *bc = find_bc(info->pCreateInfo->format);
|
||||
if (!bc) {
|
||||
dev->GetDeviceImageSubresourceLayout(device, info, layout);
|
||||
return;
|
||||
}
|
||||
VkImageCreateInfo image = *info->pCreateInfo;
|
||||
substitute_image_info(bc, &image);
|
||||
VkDeviceImageSubresourceInfo copy = *info;
|
||||
copy.pCreateInfo = ℑ
|
||||
dev->GetDeviceImageSubresourceLayout(device, ©, layout);
|
||||
}
|
||||
|
||||
struct bound_buffer {
|
||||
VkDeviceMemory memory;
|
||||
VkDeviceSize offset;
|
||||
};
|
||||
|
||||
static void track_binding(struct device *dev, VkBuffer buffer, VkDeviceMemory memory, VkDeviceSize offset)
|
||||
{
|
||||
struct bound_buffer *bound = malloc(sizeof(*bound));
|
||||
bound->memory = memory;
|
||||
bound->offset = offset;
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
free(map_remove(&dev->buffers, HANDLE(buffer)));
|
||||
map_put(&dev->buffers, HANDLE(buffer), bound);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL BindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
|
||||
VkDeviceSize offset)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
VkResult result = dev->BindBufferMemory(device, buffer, memory, offset);
|
||||
if (result == VK_SUCCESS)
|
||||
track_binding(dev, buffer, memory, offset);
|
||||
return result;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL BindBufferMemory2(VkDevice device, uint32_t count, const VkBindBufferMemoryInfo *infos)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
VkResult result = dev->BindBufferMemory2(device, count, infos);
|
||||
if (result == VK_SUCCESS) {
|
||||
for (uint32_t i = 0; i < count; i++)
|
||||
track_binding(dev, infos[i].buffer, infos[i].memory, infos[i].memoryOffset);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL DestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *allocator)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
free(map_remove(&dev->buffers, HANDLE(buffer)));
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
dev->DestroyBuffer(device, buffer, allocator);
|
||||
}
|
||||
|
||||
/* The memory's start as the application sees it mapped. */
|
||||
static void track_mapping(struct device *dev, VkDeviceMemory memory, VkDeviceSize offset, void *data)
|
||||
{
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
map_put(&dev->memories, HANDLE(memory), (uint8_t *)data - offset);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
}
|
||||
|
||||
static void forget_mapping(struct device *dev, VkDeviceMemory memory)
|
||||
{
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
map_remove(&dev->memories, HANDLE(memory));
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL MapMemory(VkDevice device, VkDeviceMemory memory, VkDeviceSize offset,
|
||||
VkDeviceSize size, VkMemoryMapFlags flags, void **data)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
VkResult result = dev->MapMemory(device, memory, offset, size, flags, data);
|
||||
if (result == VK_SUCCESS)
|
||||
track_mapping(dev, memory, offset, *data);
|
||||
return result;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL MapMemory2(VkDevice device, const VkMemoryMapInfo *info, void **data)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
VkResult result = dev->MapMemory2(device, info, data);
|
||||
if (result == VK_SUCCESS)
|
||||
track_mapping(dev, info->memory, info->offset, *data);
|
||||
return result;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL UnmapMemory(VkDevice device, VkDeviceMemory memory)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
forget_mapping(dev, memory);
|
||||
dev->UnmapMemory(device, memory);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL UnmapMemory2(VkDevice device, const VkMemoryUnmapInfo *info)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
forget_mapping(dev, info->memory);
|
||||
return dev->UnmapMemory2(device, info);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL FreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks *allocator)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
forget_mapping(dev, memory);
|
||||
dev->FreeMemory(device, memory, allocator);
|
||||
}
|
||||
|
||||
struct staging_block {
|
||||
VkBuffer buffer;
|
||||
VkDeviceMemory memory;
|
||||
uint8_t *data;
|
||||
VkDeviceSize size;
|
||||
VkDeviceSize used;
|
||||
struct staging_block *next;
|
||||
};
|
||||
|
||||
struct command_buffer {
|
||||
VkCommandPool pool;
|
||||
struct staging_block *staging;
|
||||
};
|
||||
|
||||
static void destroy_block(struct device *dev, struct staging_block *block)
|
||||
{
|
||||
dev->DestroyBuffer(dev->handle, block->buffer, NULL);
|
||||
dev->FreeMemory(dev->handle, block->memory, NULL);
|
||||
free(block);
|
||||
}
|
||||
|
||||
static struct staging_block *create_block(struct device *dev, VkDeviceSize size)
|
||||
{
|
||||
struct staging_block *block = calloc(1, sizeof(*block));
|
||||
block->size = size;
|
||||
VkBufferCreateInfo buffer_info = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||
.size = size,
|
||||
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
||||
};
|
||||
if (dev->CreateBuffer(dev->handle, &buffer_info, NULL, &block->buffer) != VK_SUCCESS) {
|
||||
free(block);
|
||||
return NULL;
|
||||
}
|
||||
VkMemoryRequirements requirements;
|
||||
dev->GetBufferMemoryRequirements(dev->handle, block->buffer, &requirements);
|
||||
const VkMemoryPropertyFlags wanted = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
|
||||
uint32_t type = UINT32_MAX;
|
||||
for (uint32_t i = 0; i < dev->memory_properties.memoryTypeCount && type == UINT32_MAX; i++) {
|
||||
if ((requirements.memoryTypeBits & (1u << i)) &&
|
||||
(dev->memory_properties.memoryTypes[i].propertyFlags & wanted) == wanted)
|
||||
type = i;
|
||||
}
|
||||
VkMemoryAllocateInfo allocate_info = {
|
||||
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
||||
.allocationSize = requirements.size,
|
||||
.memoryTypeIndex = type,
|
||||
};
|
||||
void *data = NULL;
|
||||
if (type == UINT32_MAX || dev->AllocateMemory(dev->handle, &allocate_info, NULL, &block->memory) != VK_SUCCESS) {
|
||||
dev->DestroyBuffer(dev->handle, block->buffer, NULL);
|
||||
free(block);
|
||||
return NULL;
|
||||
}
|
||||
if (dev->BindBufferMemory(dev->handle, block->buffer, block->memory, 0) != VK_SUCCESS ||
|
||||
dev->MapMemory(dev->handle, block->memory, 0, VK_WHOLE_SIZE, 0, &data) != VK_SUCCESS) {
|
||||
destroy_block(dev, block);
|
||||
return NULL;
|
||||
}
|
||||
block->data = data;
|
||||
return block;
|
||||
}
|
||||
|
||||
/* Called with the device lock held: the command buffer is done with its staging. */
|
||||
static void recycle_staging(struct device *dev, struct command_buffer *state)
|
||||
{
|
||||
VkDeviceSize kept = 0;
|
||||
for (struct staging_block *b = dev->free_staging; b; b = b->next)
|
||||
kept += b->size;
|
||||
while (state->staging) {
|
||||
struct staging_block *block = state->staging;
|
||||
state->staging = block->next;
|
||||
if (kept + block->size > STAGING_KEPT) {
|
||||
destroy_block(dev, block);
|
||||
continue;
|
||||
}
|
||||
block->used = 0;
|
||||
block->next = dev->free_staging;
|
||||
dev->free_staging = block;
|
||||
kept += block->size;
|
||||
}
|
||||
}
|
||||
|
||||
/* Room for `size` bytes in a staging buffer that lives as long as the command buffer's recording. */
|
||||
static struct staging_block *staging_for(struct device *dev, VkCommandBuffer cmd, VkDeviceSize size,
|
||||
VkDeviceSize *offset)
|
||||
{
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
struct command_buffer *state = map_get(&dev->command_buffers, HANDLE(cmd));
|
||||
if (!state) {
|
||||
state = calloc(1, sizeof(*state));
|
||||
map_put(&dev->command_buffers, HANDLE(cmd), state);
|
||||
}
|
||||
struct staging_block *block = state->staging;
|
||||
VkDeviceSize start = block ? (block->used + 63) & ~(VkDeviceSize)63 : 0;
|
||||
if (!block || start + size > block->size) {
|
||||
struct staging_block **link = &dev->free_staging;
|
||||
while (*link && (*link)->size < size)
|
||||
link = &(*link)->next;
|
||||
block = *link;
|
||||
if (block)
|
||||
*link = block->next;
|
||||
else
|
||||
block = create_block(dev, size > STAGING_BLOCK_SIZE ? size : STAGING_BLOCK_SIZE);
|
||||
if (block) {
|
||||
block->next = state->staging;
|
||||
state->staging = block;
|
||||
}
|
||||
start = 0;
|
||||
}
|
||||
if (block)
|
||||
block->used = start + size;
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
*offset = start;
|
||||
return block;
|
||||
}
|
||||
|
||||
/* Where the application has the buffer's contents mapped, or NULL. */
|
||||
static const uint8_t *mapped_buffer(struct device *dev, VkBuffer buffer)
|
||||
{
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
const struct bound_buffer *bound = map_get(&dev->buffers, HANDLE(buffer));
|
||||
const uint8_t *base = bound ? map_get(&dev->memories, HANDLE(bound->memory)) : NULL;
|
||||
const uint8_t *data = base ? base + bound->offset : NULL;
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
return data;
|
||||
}
|
||||
|
||||
static void decode_block(const struct bc_format *bc, const uint8_t *block, uint8_t *texels)
|
||||
{
|
||||
switch (bc->kind) {
|
||||
case BC1:
|
||||
bcdec_bc1(block, texels, 16);
|
||||
if (bc->opaque) {
|
||||
for (int i = 0; i < 16; i++)
|
||||
texels[i * 4 + 3] = 0xff;
|
||||
}
|
||||
break;
|
||||
case BC2:
|
||||
bcdec_bc2(block, texels, 16);
|
||||
break;
|
||||
case BC3:
|
||||
bcdec_bc3(block, texels, 16);
|
||||
break;
|
||||
case BC4:
|
||||
bcdec_bc4(block, texels, 4, bc->is_signed);
|
||||
break;
|
||||
case BC5:
|
||||
bcdec_bc5(block, texels, 8, bc->is_signed);
|
||||
break;
|
||||
case BC6H: {
|
||||
uint16_t rgb[16 * 3];
|
||||
bcdec_bc6h_half(block, rgb, 12, bc->is_signed);
|
||||
uint16_t *rgba = (uint16_t *)texels;
|
||||
for (int i = 0; i < 16; i++) {
|
||||
rgba[i * 4 + 0] = rgb[i * 3 + 0];
|
||||
rgba[i * 4 + 1] = rgb[i * 3 + 1];
|
||||
rgba[i * 4 + 2] = rgb[i * 3 + 2];
|
||||
rgba[i * 4 + 3] = 0x3c00;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case BC7:
|
||||
bcdec_bc7(block, texels, 16);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
struct upload {
|
||||
VkDeviceSize offset;
|
||||
uint32_t row_length;
|
||||
uint32_t image_height;
|
||||
VkImageSubresourceLayers subresource;
|
||||
VkOffset3D image_offset;
|
||||
VkExtent3D extent;
|
||||
};
|
||||
|
||||
static uint32_t warned;
|
||||
|
||||
static void warn_once(uint32_t bit, const char *message)
|
||||
{
|
||||
if (!(__atomic_fetch_or(&warned, bit, __ATOMIC_RELAXED) & bit))
|
||||
layer_log("%s", message);
|
||||
}
|
||||
|
||||
static void upload_region(struct device *dev, VkCommandBuffer cmd, const struct bc_image *image,
|
||||
const uint8_t *source, VkImage dst, VkImageLayout layout, const struct upload *region)
|
||||
{
|
||||
const struct bc_format *bc = image->format;
|
||||
uint32_t width = region->extent.width, height = region->extent.height;
|
||||
uint32_t layers = region->subresource.layerCount;
|
||||
if (layers == VK_REMAINING_ARRAY_LAYERS)
|
||||
layers = image->array_layers - region->subresource.baseArrayLayer;
|
||||
uint32_t slices = region->extent.depth * layers;
|
||||
uint32_t row_texels = region->row_length ? region->row_length : width;
|
||||
uint32_t height_texels = region->image_height ? region->image_height : height;
|
||||
size_t source_row = (size_t)((row_texels + 3) / 4) * bc->block_bytes;
|
||||
size_t source_slice = (size_t)((height_texels + 3) / 4) * source_row;
|
||||
size_t row_bytes = (size_t)width * bc->texel_bytes;
|
||||
VkDeviceSize size = (VkDeviceSize)row_bytes * height * slices;
|
||||
if (!size)
|
||||
return;
|
||||
|
||||
VkDeviceSize offset;
|
||||
struct staging_block *block = staging_for(dev, cmd, size, &offset);
|
||||
if (!block) {
|
||||
warn_once(1u << 0, "out of staging memory for BC texture uploads; textures left empty");
|
||||
return;
|
||||
}
|
||||
uint8_t *out = block->data + offset;
|
||||
const uint8_t *in = source + region->offset;
|
||||
uint8_t texels[16 * 8];
|
||||
for (uint32_t s = 0; s < slices; s++) {
|
||||
for (uint32_t by = 0; by < (height + 3) / 4; by++) {
|
||||
for (uint32_t bx = 0; bx < (width + 3) / 4; bx++) {
|
||||
decode_block(bc, in + s * source_slice + by * source_row + (size_t)bx * bc->block_bytes, texels);
|
||||
for (uint32_t y = 0; y < 4 && by * 4 + y < height; y++) {
|
||||
uint32_t columns = width - bx * 4 < 4 ? width - bx * 4 : 4;
|
||||
memcpy(out + ((size_t)s * height + by * 4 + y) * row_bytes + (size_t)bx * 4 * bc->texel_bytes,
|
||||
texels + (size_t)y * 4 * bc->texel_bytes, (size_t)columns * bc->texel_bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
VkBufferImageCopy copy = {
|
||||
.bufferOffset = offset,
|
||||
.imageSubresource = region->subresource,
|
||||
.imageOffset = region->image_offset,
|
||||
.imageExtent = region->extent,
|
||||
};
|
||||
copy.imageSubresource.layerCount = layers;
|
||||
dev->CmdCopyBufferToImage(cmd, block->buffer, dst, layout, 1, ©);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyBufferToImage(VkCommandBuffer cmd, VkBuffer buffer, VkImage image,
|
||||
VkImageLayout layout, uint32_t count,
|
||||
const VkBufferImageCopy *regions)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
const struct bc_image *tracked = tracked_image(dev, image);
|
||||
if (!tracked) {
|
||||
dev->CmdCopyBufferToImage(cmd, buffer, image, layout, count, regions);
|
||||
return;
|
||||
}
|
||||
const uint8_t *source = mapped_buffer(dev, buffer);
|
||||
if (!source) {
|
||||
warn_once(1u << 1, "BC texture upload from memory that is not mapped; texture left empty");
|
||||
return;
|
||||
}
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
struct upload region = {regions[i].bufferOffset, regions[i].bufferRowLength, regions[i].bufferImageHeight,
|
||||
regions[i].imageSubresource, regions[i].imageOffset, regions[i].imageExtent};
|
||||
upload_region(dev, cmd, tracked, source, image, layout, ®ion);
|
||||
}
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyBufferToImage2(VkCommandBuffer cmd, const VkCopyBufferToImageInfo2 *info)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
const struct bc_image *tracked = tracked_image(dev, info->dstImage);
|
||||
if (!tracked) {
|
||||
dev->CmdCopyBufferToImage2(cmd, info);
|
||||
return;
|
||||
}
|
||||
const uint8_t *source = mapped_buffer(dev, info->srcBuffer);
|
||||
if (!source) {
|
||||
warn_once(1u << 1, "BC texture upload from memory that is not mapped; texture left empty");
|
||||
return;
|
||||
}
|
||||
for (uint32_t i = 0; i < info->regionCount; i++) {
|
||||
const VkBufferImageCopy2 *r = &info->pRegions[i];
|
||||
struct upload region = {r->bufferOffset, r->bufferRowLength, r->bufferImageHeight,
|
||||
r->imageSubresource, r->imageOffset, r->imageExtent};
|
||||
upload_region(dev, cmd, tracked, source, info->dstImage, info->dstImageLayout, ®ion);
|
||||
}
|
||||
}
|
||||
|
||||
/* Reading compressed blocks back would hand out decoded texels the buffer has no room for. */
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyImageToBuffer(VkCommandBuffer cmd, VkImage image, VkImageLayout layout,
|
||||
VkBuffer buffer, uint32_t count, const VkBufferImageCopy *regions)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
if (tracked_image(dev, image)) {
|
||||
warn_once(1u << 2, "BC texture read back into a buffer; skipped");
|
||||
return;
|
||||
}
|
||||
dev->CmdCopyImageToBuffer(cmd, image, layout, buffer, count, regions);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyImageToBuffer2(VkCommandBuffer cmd, const VkCopyImageToBufferInfo2 *info)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
if (tracked_image(dev, info->srcImage)) {
|
||||
warn_once(1u << 2, "BC texture read back into a buffer; skipped");
|
||||
return;
|
||||
}
|
||||
dev->CmdCopyImageToBuffer2(cmd, info);
|
||||
}
|
||||
|
||||
/* Between two BC images the texels line up; between BC and anything else they cannot. */
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyImage(VkCommandBuffer cmd, VkImage src, VkImageLayout src_layout, VkImage dst,
|
||||
VkImageLayout dst_layout, uint32_t count, const VkImageCopy *regions)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
if (!tracked_image(dev, src) != !tracked_image(dev, dst)) {
|
||||
warn_once(1u << 3, "copy between a BC texture and an uncompressed one; skipped");
|
||||
return;
|
||||
}
|
||||
dev->CmdCopyImage(cmd, src, src_layout, dst, dst_layout, count, regions);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyImage2(VkCommandBuffer cmd, const VkCopyImageInfo2 *info)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
if (!tracked_image(dev, info->srcImage) != !tracked_image(dev, info->dstImage)) {
|
||||
warn_once(1u << 3, "copy between a BC texture and an uncompressed one; skipped");
|
||||
return;
|
||||
}
|
||||
dev->CmdCopyImage2(cmd, info);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *info,
|
||||
VkCommandBuffer *buffers)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
VkResult result = dev->AllocateCommandBuffers(device, info, buffers);
|
||||
if (result != VK_SUCCESS)
|
||||
return result;
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
for (uint32_t i = 0; i < info->commandBufferCount; i++) {
|
||||
struct command_buffer *state = calloc(1, sizeof(*state));
|
||||
state->pool = info->commandPool;
|
||||
map_put(&dev->command_buffers, HANDLE(buffers[i]), state);
|
||||
}
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL FreeCommandBuffers(VkDevice device, VkCommandPool pool, uint32_t count,
|
||||
const VkCommandBuffer *buffers)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
struct command_buffer *state = buffers[i] ? map_remove(&dev->command_buffers, HANDLE(buffers[i])) : NULL;
|
||||
if (state) {
|
||||
recycle_staging(dev, state);
|
||||
free(state);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
dev->FreeCommandBuffers(device, pool, count, buffers);
|
||||
}
|
||||
|
||||
static void recycle_command_buffer(struct device *dev, VkCommandBuffer cmd)
|
||||
{
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
struct command_buffer *state = map_get(&dev->command_buffers, HANDLE(cmd));
|
||||
if (state)
|
||||
recycle_staging(dev, state);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL BeginCommandBuffer(VkCommandBuffer cmd, const VkCommandBufferBeginInfo *info)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
recycle_command_buffer(dev, cmd);
|
||||
return dev->BeginCommandBuffer(cmd, info);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL ResetCommandBuffer(VkCommandBuffer cmd, VkCommandBufferResetFlags flags)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
recycle_command_buffer(dev, cmd);
|
||||
return dev->ResetCommandBuffer(cmd, flags);
|
||||
}
|
||||
|
||||
/* Every command buffer of the pool is done with its staging; `forget` drops them as well. */
|
||||
static void recycle_pool(struct device *dev, VkCommandPool pool, bool forget)
|
||||
{
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
struct handle_map *map = &dev->command_buffers;
|
||||
for (uint32_t i = 0; i < map->capacity;) {
|
||||
struct command_buffer *state = map->values[i];
|
||||
if (!map->keys[i] || state->pool != pool) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
recycle_staging(dev, state);
|
||||
if (forget) {
|
||||
/* Removal shifts a later entry into this slot, so look at it again. */
|
||||
free(map_remove(map, map->keys[i]));
|
||||
continue;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL ResetCommandPool(VkDevice device, VkCommandPool pool, VkCommandPoolResetFlags flags)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
recycle_pool(dev, pool, false);
|
||||
return dev->ResetCommandPool(device, pool, flags);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL DestroyCommandPool(VkDevice device, VkCommandPool pool,
|
||||
const VkAllocationCallbacks *allocator)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
recycle_pool(dev, pool, true);
|
||||
dev->DestroyCommandPool(device, pool, allocator);
|
||||
}
|
||||
|
||||
void bc_destroy_device(struct device *dev)
|
||||
{
|
||||
struct handle_map *maps[] = {&dev->images, &dev->buffers, &dev->command_buffers};
|
||||
for (size_t m = 0; m < sizeof(maps) / sizeof(maps[0]); m++) {
|
||||
for (uint32_t i = 0; i < maps[m]->capacity; i++) {
|
||||
if (!maps[m]->keys[i])
|
||||
continue;
|
||||
if (maps[m] == &dev->command_buffers)
|
||||
recycle_staging(dev, maps[m]->values[i]);
|
||||
free(maps[m]->values[i]);
|
||||
}
|
||||
map_free(maps[m]);
|
||||
}
|
||||
map_free(&dev->memories);
|
||||
while (dev->free_staging) {
|
||||
struct staging_block *block = dev->free_staging;
|
||||
dev->free_staging = block->next;
|
||||
destroy_block(dev, block);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,452 @@
|
||||
/*
|
||||
* The Mali compatibility layer's draw and descriptor emulation (added to David Cimo's layer):
|
||||
*
|
||||
* - multiDrawIndirect: an indirect draw with drawCount > 1 is recorded as drawCount single draws
|
||||
* at offset + i * stride. Mali-G57 (Bifrost/Valhall v9) has none, and DXVK refuses the device.
|
||||
* - robustness2's nullDescriptor: VK_NULL_HANDLE buffers, image views and vertex buffers in
|
||||
* descriptor writes (plain, template and push) and vertex-buffer binds are replaced with small
|
||||
* stand-ins the layer owns. robustBufferAccess2 itself maps to robustBufferAccess (layer.c).
|
||||
* A null texel-buffer view gets an RGBA8 (uniform) or R32_UINT (storage) view of the stand-in
|
||||
* buffer: its format is not known here, but Zink leaves them unbound and never reads them.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mali_compat.h"
|
||||
|
||||
/* --- multiDrawIndirect --- */
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdDrawIndirect(VkCommandBuffer cmd, VkBuffer buffer, VkDeviceSize offset,
|
||||
uint32_t draw_count, uint32_t stride)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
if (draw_count <= 1) {
|
||||
dev->CmdDrawIndirect(cmd, buffer, offset, draw_count, stride);
|
||||
return;
|
||||
}
|
||||
for (uint32_t i = 0; i < draw_count; i++)
|
||||
dev->CmdDrawIndirect(cmd, buffer, offset + (VkDeviceSize)i * stride, 1, stride);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdDrawIndexedIndirect(VkCommandBuffer cmd, VkBuffer buffer, VkDeviceSize offset,
|
||||
uint32_t draw_count, uint32_t stride)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
if (draw_count <= 1) {
|
||||
dev->CmdDrawIndexedIndirect(cmd, buffer, offset, draw_count, stride);
|
||||
return;
|
||||
}
|
||||
for (uint32_t i = 0; i < draw_count; i++)
|
||||
dev->CmdDrawIndexedIndirect(cmd, buffer, offset + (VkDeviceSize)i * stride, 1, stride);
|
||||
}
|
||||
|
||||
/* --- nullDescriptor stand-ins --- */
|
||||
|
||||
/* A template's entries, to find the null handles in the data it is given. */
|
||||
struct template_info {
|
||||
uint32_t count;
|
||||
VkDescriptorUpdateTemplateEntry *entries;
|
||||
size_t size; /* bytes the data spans */
|
||||
};
|
||||
|
||||
#define NULL_BUFFER_SIZE 65536
|
||||
|
||||
static uint32_t memory_type(const struct device *dev, uint32_t bits, VkMemoryPropertyFlags want)
|
||||
{
|
||||
for (uint32_t i = 0; i < dev->memory_properties.memoryTypeCount; i++)
|
||||
if ((bits & (1u << i)) && (dev->memory_properties.memoryTypes[i].propertyFlags & want) == want)
|
||||
return i;
|
||||
for (uint32_t i = 0; i < dev->memory_properties.memoryTypeCount; i++)
|
||||
if (bits & (1u << i))
|
||||
return i;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Made once per device, under its lock, the first time a null descriptor is seen. */
|
||||
static bool null_resources(struct device *dev)
|
||||
{
|
||||
if (dev->null_ready)
|
||||
return dev->null_ready > 0;
|
||||
dev->null_ready = -1;
|
||||
|
||||
VkBufferCreateInfo binfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
|
||||
.size = NULL_BUFFER_SIZE,
|
||||
.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
|
||||
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
|
||||
VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT,
|
||||
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
||||
};
|
||||
if (dev->CreateBuffer(dev->handle, &binfo, NULL, &dev->null_buffer) != VK_SUCCESS)
|
||||
return false;
|
||||
VkMemoryRequirements breq;
|
||||
dev->GetBufferMemoryRequirements(dev->handle, dev->null_buffer, &breq);
|
||||
VkMemoryAllocateInfo balloc = {
|
||||
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
||||
.allocationSize = breq.size,
|
||||
.memoryTypeIndex = memory_type(dev, breq.memoryTypeBits,
|
||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT),
|
||||
};
|
||||
if (dev->AllocateMemory(dev->handle, &balloc, NULL, &dev->null_buffer_memory) != VK_SUCCESS)
|
||||
return false;
|
||||
dev->BindBufferMemory(dev->handle, dev->null_buffer, dev->null_buffer_memory, 0);
|
||||
/* A null buffer reads as zeros. */
|
||||
void *data = NULL;
|
||||
if ((dev->memory_properties.memoryTypes[balloc.memoryTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) &&
|
||||
dev->MapMemory(dev->handle, dev->null_buffer_memory, 0, VK_WHOLE_SIZE, 0, &data) == VK_SUCCESS) {
|
||||
memset(data, 0, (size_t)breq.size);
|
||||
dev->UnmapMemory(dev->handle, dev->null_buffer_memory);
|
||||
}
|
||||
|
||||
VkImageCreateInfo iinfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
|
||||
.imageType = VK_IMAGE_TYPE_2D,
|
||||
.format = VK_FORMAT_R8G8B8A8_UNORM,
|
||||
.extent = {1, 1, 1},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 1,
|
||||
.samples = VK_SAMPLE_COUNT_1_BIT,
|
||||
.tiling = VK_IMAGE_TILING_OPTIMAL,
|
||||
.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT,
|
||||
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
|
||||
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
|
||||
};
|
||||
if (dev->CreateImage(dev->handle, &iinfo, NULL, &dev->null_image) != VK_SUCCESS)
|
||||
return false;
|
||||
VkMemoryRequirements ireq;
|
||||
dev->GetImageMemoryRequirements(dev->handle, dev->null_image, &ireq);
|
||||
VkMemoryAllocateInfo ialloc = {
|
||||
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
||||
.allocationSize = ireq.size,
|
||||
.memoryTypeIndex = memory_type(dev, ireq.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT),
|
||||
};
|
||||
if (dev->AllocateMemory(dev->handle, &ialloc, NULL, &dev->null_image_memory) != VK_SUCCESS)
|
||||
return false;
|
||||
dev->BindImageMemory(dev->handle, dev->null_image, dev->null_image_memory, 0);
|
||||
VkImageViewCreateInfo vinfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
|
||||
.image = dev->null_image,
|
||||
.viewType = VK_IMAGE_VIEW_TYPE_2D,
|
||||
.format = VK_FORMAT_R8G8B8A8_UNORM,
|
||||
.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1},
|
||||
};
|
||||
if (dev->CreateImageView(dev->handle, &vinfo, NULL, &dev->null_view) != VK_SUCCESS)
|
||||
return false;
|
||||
/* Texel views are optional: a write that needs one and has none keeps its null. */
|
||||
VkBufferViewCreateInfo tinfo = {
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO,
|
||||
.buffer = dev->null_buffer,
|
||||
.format = VK_FORMAT_R8G8B8A8_UNORM,
|
||||
.range = VK_WHOLE_SIZE,
|
||||
};
|
||||
if (dev->CreateBufferView(dev->handle, &tinfo, NULL, &dev->null_uniform_texel) != VK_SUCCESS)
|
||||
dev->null_uniform_texel = VK_NULL_HANDLE;
|
||||
tinfo.format = VK_FORMAT_R32_UINT;
|
||||
if (dev->CreateBufferView(dev->handle, &tinfo, NULL, &dev->null_storage_texel) != VK_SUCCESS)
|
||||
dev->null_storage_texel = VK_NULL_HANDLE;
|
||||
dev->null_ready = 1;
|
||||
layer_log("nullDescriptor: stand-in buffer and 1x1 image ready");
|
||||
return true;
|
||||
}
|
||||
|
||||
void null_resources_destroy(struct device *dev)
|
||||
{
|
||||
for (uint32_t i = 0; i < dev->templates.capacity; i++) {
|
||||
struct template_info *t = dev->templates.values ? dev->templates.values[i] : NULL;
|
||||
if (t) {
|
||||
free(t->entries);
|
||||
free(t);
|
||||
}
|
||||
}
|
||||
map_free(&dev->templates);
|
||||
if (dev->null_uniform_texel) dev->DestroyBufferView(dev->handle, dev->null_uniform_texel, NULL);
|
||||
if (dev->null_storage_texel) dev->DestroyBufferView(dev->handle, dev->null_storage_texel, NULL);
|
||||
if (dev->null_view) dev->DestroyImageView(dev->handle, dev->null_view, NULL);
|
||||
if (dev->null_image) dev->DestroyImage(dev->handle, dev->null_image, NULL);
|
||||
if (dev->null_image_memory) dev->FreeMemory(dev->handle, dev->null_image_memory, NULL);
|
||||
if (dev->null_buffer) dev->DestroyBuffer(dev->handle, dev->null_buffer, NULL);
|
||||
if (dev->null_buffer_memory) dev->FreeMemory(dev->handle, dev->null_buffer_memory, NULL);
|
||||
}
|
||||
|
||||
static bool is_buffer_type(VkDescriptorType t)
|
||||
{
|
||||
return t == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || t == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER ||
|
||||
t == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC || t == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC;
|
||||
}
|
||||
|
||||
static bool is_image_type(VkDescriptorType t)
|
||||
{
|
||||
return t == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE || t == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE ||
|
||||
t == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER || t == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT;
|
||||
}
|
||||
|
||||
static bool is_texel_type(VkDescriptorType t)
|
||||
{
|
||||
return t == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER || t == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
|
||||
}
|
||||
|
||||
static void fix_texel(struct device *dev, VkDescriptorType t, VkBufferView *view)
|
||||
{
|
||||
if (*view != VK_NULL_HANDLE || !null_resources(dev))
|
||||
return;
|
||||
*view = t == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER ? dev->null_storage_texel : dev->null_uniform_texel;
|
||||
}
|
||||
|
||||
static void fix_buffer(struct device *dev, VkDescriptorBufferInfo *info)
|
||||
{
|
||||
if (info->buffer != VK_NULL_HANDLE || !null_resources(dev))
|
||||
return;
|
||||
info->buffer = dev->null_buffer;
|
||||
info->offset = 0;
|
||||
info->range = VK_WHOLE_SIZE;
|
||||
}
|
||||
|
||||
static void fix_image(struct device *dev, VkDescriptorImageInfo *info)
|
||||
{
|
||||
if (info->imageView != VK_NULL_HANDLE || !null_resources(dev))
|
||||
return;
|
||||
info->imageView = dev->null_view;
|
||||
}
|
||||
|
||||
/* Copies of the writes with stand-ins for null handles; NULL when nothing needed changing. */
|
||||
static VkWriteDescriptorSet *fix_writes(struct device *dev, uint32_t count, const VkWriteDescriptorSet *writes,
|
||||
void ***scratch, uint32_t *scratch_count)
|
||||
{
|
||||
VkWriteDescriptorSet *copies = NULL;
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
const VkWriteDescriptorSet *w = &writes[i];
|
||||
bool null_found = false;
|
||||
for (uint32_t j = 0; j < w->descriptorCount && !null_found; j++) {
|
||||
if (is_buffer_type(w->descriptorType) && w->pBufferInfo && w->pBufferInfo[j].buffer == VK_NULL_HANDLE)
|
||||
null_found = true;
|
||||
if (is_image_type(w->descriptorType) && w->pImageInfo && w->pImageInfo[j].imageView == VK_NULL_HANDLE &&
|
||||
w->descriptorType != VK_DESCRIPTOR_TYPE_SAMPLER)
|
||||
null_found = true;
|
||||
if (is_texel_type(w->descriptorType) && w->pTexelBufferView && w->pTexelBufferView[j] == VK_NULL_HANDLE)
|
||||
null_found = true;
|
||||
}
|
||||
if (!null_found)
|
||||
continue;
|
||||
if (!copies) {
|
||||
copies = malloc(count * sizeof(*copies));
|
||||
memcpy(copies, writes, count * sizeof(*copies));
|
||||
*scratch = calloc(count, sizeof(void *));
|
||||
}
|
||||
if (is_buffer_type(w->descriptorType)) {
|
||||
VkDescriptorBufferInfo *b = malloc(w->descriptorCount * sizeof(*b));
|
||||
memcpy(b, w->pBufferInfo, w->descriptorCount * sizeof(*b));
|
||||
for (uint32_t j = 0; j < w->descriptorCount; j++)
|
||||
fix_buffer(dev, &b[j]);
|
||||
copies[i].pBufferInfo = b;
|
||||
(*scratch)[(*scratch_count)++] = b;
|
||||
} else if (is_texel_type(w->descriptorType)) {
|
||||
VkBufferView *v = malloc(w->descriptorCount * sizeof(*v));
|
||||
memcpy(v, w->pTexelBufferView, w->descriptorCount * sizeof(*v));
|
||||
for (uint32_t j = 0; j < w->descriptorCount; j++)
|
||||
fix_texel(dev, w->descriptorType, &v[j]);
|
||||
copies[i].pTexelBufferView = v;
|
||||
(*scratch)[(*scratch_count)++] = v;
|
||||
} else {
|
||||
VkDescriptorImageInfo *im = malloc(w->descriptorCount * sizeof(*im));
|
||||
memcpy(im, w->pImageInfo, w->descriptorCount * sizeof(*im));
|
||||
for (uint32_t j = 0; j < w->descriptorCount; j++)
|
||||
fix_image(dev, &im[j]);
|
||||
copies[i].pImageInfo = im;
|
||||
(*scratch)[(*scratch_count)++] = im;
|
||||
}
|
||||
}
|
||||
return copies;
|
||||
}
|
||||
|
||||
static void free_scratch(void **scratch, uint32_t count)
|
||||
{
|
||||
for (uint32_t i = 0; i < count; i++)
|
||||
free(scratch[i]);
|
||||
free(scratch);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL UpdateDescriptorSets(VkDevice device, uint32_t write_count, const VkWriteDescriptorSet *writes,
|
||||
uint32_t copy_count, const VkCopyDescriptorSet *copies)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
void **scratch = NULL;
|
||||
uint32_t scratch_count = 0;
|
||||
VkWriteDescriptorSet *fixed = fix_writes(dev, write_count, writes, &scratch, &scratch_count);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
dev->UpdateDescriptorSets(device, write_count, fixed ? fixed : writes, copy_count, copies);
|
||||
free(fixed);
|
||||
free_scratch(scratch, scratch_count);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdPushDescriptorSet(VkCommandBuffer cmd, VkPipelineBindPoint bind_point,
|
||||
VkPipelineLayout layout, uint32_t set, uint32_t write_count,
|
||||
const VkWriteDescriptorSet *writes)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
void **scratch = NULL;
|
||||
uint32_t scratch_count = 0;
|
||||
VkWriteDescriptorSet *fixed = fix_writes(dev, write_count, writes, &scratch, &scratch_count);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
dev->CmdPushDescriptorSet(cmd, bind_point, layout, set, write_count, fixed ? fixed : writes);
|
||||
free(fixed);
|
||||
free_scratch(scratch, scratch_count);
|
||||
}
|
||||
|
||||
static size_t entry_size(VkDescriptorType t)
|
||||
{
|
||||
if (is_buffer_type(t))
|
||||
return sizeof(VkDescriptorBufferInfo);
|
||||
if (is_image_type(t) || t == VK_DESCRIPTOR_TYPE_SAMPLER)
|
||||
return sizeof(VkDescriptorImageInfo);
|
||||
if (t == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK)
|
||||
return 1;
|
||||
return sizeof(uint64_t);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL CreateDescriptorUpdateTemplate(VkDevice device,
|
||||
const VkDescriptorUpdateTemplateCreateInfo *info,
|
||||
const VkAllocationCallbacks *allocator,
|
||||
VkDescriptorUpdateTemplate *tmpl)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
VkResult result = dev->CreateDescriptorUpdateTemplate(device, info, allocator, tmpl);
|
||||
if (result != VK_SUCCESS)
|
||||
return result;
|
||||
struct template_info *t = calloc(1, sizeof(*t));
|
||||
t->count = info->descriptorUpdateEntryCount;
|
||||
t->entries = malloc(t->count * sizeof(*t->entries));
|
||||
memcpy(t->entries, info->pDescriptorUpdateEntries, t->count * sizeof(*t->entries));
|
||||
for (uint32_t i = 0; i < t->count; i++) {
|
||||
const VkDescriptorUpdateTemplateEntry *e = &t->entries[i];
|
||||
size_t end = e->descriptorType == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK
|
||||
? e->offset + e->descriptorCount
|
||||
: e->offset + (e->descriptorCount ? (e->descriptorCount - 1) * e->stride : 0) + entry_size(e->descriptorType);
|
||||
if (end > t->size)
|
||||
t->size = end;
|
||||
}
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
map_put(&dev->templates, (uint64_t)*tmpl, t);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL DestroyDescriptorUpdateTemplate(VkDevice device, VkDescriptorUpdateTemplate tmpl,
|
||||
const VkAllocationCallbacks *allocator)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
struct template_info *t = map_remove(&dev->templates, (uint64_t)tmpl);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
if (t) {
|
||||
free(t->entries);
|
||||
free(t);
|
||||
}
|
||||
dev->DestroyDescriptorUpdateTemplate(device, tmpl, allocator);
|
||||
}
|
||||
|
||||
/* A copy of the template's data with stand-ins for null handles; NULL when nothing changed. */
|
||||
static void *fix_template_data(struct device *dev, VkDescriptorUpdateTemplate tmpl, const void *data)
|
||||
{
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
struct template_info *t = map_get(&dev->templates, (uint64_t)tmpl);
|
||||
void *copy = NULL;
|
||||
if (t && data) {
|
||||
for (uint32_t i = 0; i < t->count; i++) {
|
||||
const VkDescriptorUpdateTemplateEntry *e = &t->entries[i];
|
||||
bool buffer = is_buffer_type(e->descriptorType);
|
||||
bool image = is_image_type(e->descriptorType);
|
||||
bool texel = is_texel_type(e->descriptorType);
|
||||
if (!buffer && !image && !texel)
|
||||
continue;
|
||||
for (uint32_t j = 0; j < e->descriptorCount; j++) {
|
||||
size_t at = e->offset + (size_t)j * e->stride;
|
||||
const char *src = (const char *)(copy ? copy : data) + at;
|
||||
bool null_handle = buffer ? ((const VkDescriptorBufferInfo *)src)->buffer == VK_NULL_HANDLE
|
||||
: texel ? *(const VkBufferView *)src == VK_NULL_HANDLE
|
||||
: ((const VkDescriptorImageInfo *)src)->imageView == VK_NULL_HANDLE;
|
||||
if (!null_handle)
|
||||
continue;
|
||||
if (!copy) {
|
||||
copy = malloc(t->size);
|
||||
memcpy(copy, data, t->size);
|
||||
}
|
||||
if (buffer)
|
||||
fix_buffer(dev, (VkDescriptorBufferInfo *)((char *)copy + at));
|
||||
else if (texel)
|
||||
fix_texel(dev, e->descriptorType, (VkBufferView *)((char *)copy + at));
|
||||
else
|
||||
fix_image(dev, (VkDescriptorImageInfo *)((char *)copy + at));
|
||||
}
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
return copy;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL UpdateDescriptorSetWithTemplate(VkDevice device, VkDescriptorSet set,
|
||||
VkDescriptorUpdateTemplate tmpl, const void *data)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
void *fixed = fix_template_data(dev, tmpl, data);
|
||||
dev->UpdateDescriptorSetWithTemplate(device, set, tmpl, fixed ? fixed : data);
|
||||
free(fixed);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdPushDescriptorSetWithTemplate(VkCommandBuffer cmd, VkDescriptorUpdateTemplate tmpl,
|
||||
VkPipelineLayout layout, uint32_t set, const void *data)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
void *fixed = fix_template_data(dev, tmpl, data);
|
||||
dev->CmdPushDescriptorSetWithTemplate(cmd, tmpl, layout, set, fixed ? fixed : data);
|
||||
free(fixed);
|
||||
}
|
||||
|
||||
/* Null vertex buffers read as zeros: bound to the stand-in at offset 0. */
|
||||
VKAPI_ATTR void VKAPI_CALL CmdBindVertexBuffers(VkCommandBuffer cmd, uint32_t first, uint32_t count,
|
||||
const VkBuffer *buffers, const VkDeviceSize *offsets)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
VkBuffer fixed[64];
|
||||
VkDeviceSize fixed_offsets[64];
|
||||
bool any = false;
|
||||
for (uint32_t i = 0; i < count && i < 64; i++) any |= buffers[i] == VK_NULL_HANDLE;
|
||||
if (!any || count > 64) {
|
||||
dev->CmdBindVertexBuffers(cmd, first, count, buffers, offsets);
|
||||
return;
|
||||
}
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
bool ready = null_resources(dev);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
fixed[i] = buffers[i] == VK_NULL_HANDLE && ready ? dev->null_buffer : buffers[i];
|
||||
fixed_offsets[i] = buffers[i] == VK_NULL_HANDLE ? 0 : offsets[i];
|
||||
}
|
||||
dev->CmdBindVertexBuffers(cmd, first, count, fixed, fixed_offsets);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdBindVertexBuffers2(VkCommandBuffer cmd, uint32_t first, uint32_t count,
|
||||
const VkBuffer *buffers, const VkDeviceSize *offsets,
|
||||
const VkDeviceSize *sizes, const VkDeviceSize *strides)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
VkBuffer fixed[64];
|
||||
VkDeviceSize fixed_offsets[64], fixed_sizes[64];
|
||||
bool any = false;
|
||||
for (uint32_t i = 0; i < count && i < 64; i++) any |= buffers[i] == VK_NULL_HANDLE;
|
||||
if (!any || count > 64) {
|
||||
dev->CmdBindVertexBuffers2(cmd, first, count, buffers, offsets, sizes, strides);
|
||||
return;
|
||||
}
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
bool ready = null_resources(dev);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
bool null_buffer = buffers[i] == VK_NULL_HANDLE;
|
||||
fixed[i] = null_buffer && ready ? dev->null_buffer : buffers[i];
|
||||
fixed_offsets[i] = null_buffer ? 0 : offsets[i];
|
||||
fixed_sizes[i] = sizes ? (null_buffer ? NULL_BUFFER_SIZE : sizes[i]) : 0;
|
||||
}
|
||||
dev->CmdBindVertexBuffers2(cmd, first, count, fixed, fixed_offsets, sizes ? fixed_sizes : NULL, strides);
|
||||
}
|
||||
@@ -0,0 +1,812 @@
|
||||
#define _GNU_SOURCE
|
||||
#include "mali_compat.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LAYER_NAME "VK_LAYER_DROIDDECK_mali_compat"
|
||||
|
||||
static pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static struct instance *instances;
|
||||
static struct device *devices;
|
||||
|
||||
void layer_log(const char *format, ...)
|
||||
{
|
||||
char line[512];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(line, sizeof(line), format, args);
|
||||
va_end(args);
|
||||
fprintf(stderr, "droiddeck-mali-compat: %s\n", line);
|
||||
}
|
||||
|
||||
/* The loader's dispatch table pointer: shared by an instance and its physical devices, and by a
|
||||
* device and its queues and command buffers. */
|
||||
static void *dispatch_key(const void *dispatchable)
|
||||
{
|
||||
return *(void *const *)dispatchable;
|
||||
}
|
||||
|
||||
struct instance *instance_from_handle(const void *dispatchable)
|
||||
{
|
||||
void *key = dispatch_key(dispatchable);
|
||||
pthread_mutex_lock(&global_lock);
|
||||
struct instance *inst = instances;
|
||||
while (inst && inst->key != key)
|
||||
inst = inst->next;
|
||||
pthread_mutex_unlock(&global_lock);
|
||||
return inst;
|
||||
}
|
||||
|
||||
struct device *device_from_handle(const void *dispatchable)
|
||||
{
|
||||
void *key = dispatch_key(dispatchable);
|
||||
pthread_mutex_lock(&global_lock);
|
||||
struct device *dev = devices;
|
||||
while (dev && dev->key != key)
|
||||
dev = dev->next;
|
||||
pthread_mutex_unlock(&global_lock);
|
||||
return dev;
|
||||
}
|
||||
|
||||
static uint64_t hash(uint64_t key)
|
||||
{
|
||||
key ^= key >> 33;
|
||||
key *= 0xff51afd7ed558ccdull;
|
||||
key ^= key >> 33;
|
||||
return key;
|
||||
}
|
||||
|
||||
void *map_get(const struct handle_map *map, uint64_t key)
|
||||
{
|
||||
if (!map->capacity)
|
||||
return NULL;
|
||||
for (uint32_t i = hash(key) & (map->capacity - 1);; i = (i + 1) & (map->capacity - 1)) {
|
||||
if (map->keys[i] == key)
|
||||
return map->values[i];
|
||||
if (!map->keys[i])
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void map_grow(struct handle_map *map)
|
||||
{
|
||||
struct handle_map bigger = {
|
||||
.capacity = map->capacity ? map->capacity * 2 : 64,
|
||||
};
|
||||
bigger.keys = calloc(bigger.capacity, sizeof(*bigger.keys));
|
||||
bigger.values = calloc(bigger.capacity, sizeof(*bigger.values));
|
||||
for (uint32_t i = 0; i < map->capacity; i++) {
|
||||
if (map->keys[i])
|
||||
map_put(&bigger, map->keys[i], map->values[i]);
|
||||
}
|
||||
free(map->keys);
|
||||
free(map->values);
|
||||
*map = bigger;
|
||||
}
|
||||
|
||||
void map_put(struct handle_map *map, uint64_t key, void *value)
|
||||
{
|
||||
if ((map->count + 1) * 4 > map->capacity * 3)
|
||||
map_grow(map);
|
||||
uint32_t i = hash(key) & (map->capacity - 1);
|
||||
while (map->keys[i] && map->keys[i] != key)
|
||||
i = (i + 1) & (map->capacity - 1);
|
||||
if (!map->keys[i])
|
||||
map->count++;
|
||||
map->keys[i] = key;
|
||||
map->values[i] = value;
|
||||
}
|
||||
|
||||
void *map_remove(struct handle_map *map, uint64_t key)
|
||||
{
|
||||
if (!map->capacity)
|
||||
return NULL;
|
||||
uint32_t mask = map->capacity - 1;
|
||||
uint32_t i = hash(key) & mask;
|
||||
while (map->keys[i] != key) {
|
||||
if (!map->keys[i])
|
||||
return NULL;
|
||||
i = (i + 1) & mask;
|
||||
}
|
||||
void *value = map->values[i];
|
||||
map->count--;
|
||||
/* Backward-shift deletion keeps every remaining key reachable from its home slot. */
|
||||
for (uint32_t j = (i + 1) & mask; map->keys[j]; j = (j + 1) & mask) {
|
||||
uint32_t home = hash(map->keys[j]) & mask;
|
||||
if (((j - home) & mask) >= ((j - i) & mask)) {
|
||||
map->keys[i] = map->keys[j];
|
||||
map->values[i] = map->values[j];
|
||||
i = j;
|
||||
}
|
||||
}
|
||||
map->keys[i] = 0;
|
||||
map->values[i] = NULL;
|
||||
return value;
|
||||
}
|
||||
|
||||
void map_free(struct handle_map *map)
|
||||
{
|
||||
free(map->keys);
|
||||
free(map->values);
|
||||
*map = (struct handle_map){0};
|
||||
}
|
||||
|
||||
/* Wine processes by default. DROIDDECK_MALI_COMPAT=1 (the app's Venus route) turns it on everywhere
|
||||
* but gamescope, so Zink in the Steam client and Xwayland gets robustness2's nullDescriptor, which it
|
||||
* requires; gamescope runs fine on the driver's own features. =all includes gamescope too. */
|
||||
static bool wanted(void)
|
||||
{
|
||||
const char *setting = getenv("DROIDDECK_MALI_COMPAT");
|
||||
if (setting) {
|
||||
if (strcmp(setting, "0") == 0)
|
||||
return false;
|
||||
if (strcmp(setting, "all") == 0)
|
||||
return true;
|
||||
return strcmp(program_invocation_short_name, "gamescope") != 0;
|
||||
}
|
||||
return getenv("WINEPREFIX") != NULL;
|
||||
}
|
||||
|
||||
/* DROIDDECK_MALI_COMPAT_TRACE=1: what an application asks before it creates a device (Zink on a
|
||||
* Mali-G615 through Venus gave up before vkCreateDevice without a word). */
|
||||
static bool tracing(void)
|
||||
{
|
||||
static int on = -1;
|
||||
if (on < 0) {
|
||||
const char *v = getenv("DROIDDECK_MALI_COMPAT_TRACE");
|
||||
on = v && *v && strcmp(v, "0") != 0;
|
||||
}
|
||||
return on;
|
||||
}
|
||||
|
||||
static bool has_extension(struct instance *inst, VkPhysicalDevice physical_device, const char *name)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
inst->EnumerateDeviceExtensionProperties(physical_device, NULL, &count, NULL);
|
||||
VkExtensionProperties *extensions = calloc(count, sizeof(*extensions));
|
||||
inst->EnumerateDeviceExtensionProperties(physical_device, NULL, &count, extensions);
|
||||
bool found = false;
|
||||
for (uint32_t i = 0; i < count && !found; i++)
|
||||
found = strcmp(extensions[i].extensionName, name) == 0;
|
||||
free(extensions);
|
||||
return found;
|
||||
}
|
||||
|
||||
uint32_t physical_device_gaps(struct instance *inst, VkPhysicalDevice physical_device)
|
||||
{
|
||||
pthread_mutex_lock(&inst->lock);
|
||||
for (uint32_t i = 0; i < inst->physical_device_count; i++) {
|
||||
if (inst->physical_devices[i].handle == physical_device) {
|
||||
uint32_t gaps = inst->physical_devices[i].gaps;
|
||||
pthread_mutex_unlock(&inst->lock);
|
||||
return gaps;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&inst->lock);
|
||||
|
||||
bool robustness2 = has_extension(inst, physical_device, "VK_EXT_robustness2");
|
||||
VkPhysicalDeviceRobustness2FeaturesEXT robust = {
|
||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT,
|
||||
};
|
||||
VkPhysicalDeviceFeatures2 features = {
|
||||
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
|
||||
.pNext = robustness2 ? &robust : NULL,
|
||||
};
|
||||
inst->GetPhysicalDeviceFeatures2(physical_device, &features);
|
||||
uint32_t gaps = 0;
|
||||
if (!features.features.fillModeNonSolid)
|
||||
gaps |= GAP_FILL_MODE;
|
||||
if (!features.features.multiViewport)
|
||||
gaps |= GAP_MULTI_VIEWPORT;
|
||||
if (!features.features.shaderClipDistance)
|
||||
gaps |= GAP_CLIP_DISTANCE;
|
||||
if (!features.features.shaderCullDistance)
|
||||
gaps |= GAP_CULL_DISTANCE;
|
||||
if (!features.features.textureCompressionBC)
|
||||
gaps |= GAP_BC;
|
||||
if (robustness2 && !robust.robustBufferAccess2)
|
||||
gaps |= GAP_ROBUSTNESS2;
|
||||
if (!features.features.multiDrawIndirect)
|
||||
gaps |= GAP_MULTI_DRAW;
|
||||
if (!has_extension(inst, physical_device, VK_EXT_DEPTH_CLIP_ENABLE_EXTENSION_NAME))
|
||||
gaps |= GAP_DEPTH_CLIP;
|
||||
/* No robustness2 at all: the extension is offered with robustBufferAccess2 (as robustBufferAccess)
|
||||
* and nullDescriptor (stand-ins, draw.c). */
|
||||
if (!robustness2)
|
||||
gaps |= GAP_ROBUSTNESS2 | GAP_NULL_DESCRIPTOR;
|
||||
|
||||
pthread_mutex_lock(&inst->lock);
|
||||
if (inst->physical_device_count < sizeof(inst->physical_devices) / sizeof(inst->physical_devices[0])) {
|
||||
inst->physical_devices[inst->physical_device_count++] =
|
||||
(struct physical_device){.handle = physical_device, .gaps = gaps};
|
||||
}
|
||||
pthread_mutex_unlock(&inst->lock);
|
||||
return gaps;
|
||||
}
|
||||
|
||||
/* Structures of extensions the driver does not have are taken out of a chain for the call down and
|
||||
* put back after it: the driver (and Venus) must not see them. */
|
||||
struct unlinked {
|
||||
VkBaseOutStructure *prev, *node;
|
||||
};
|
||||
|
||||
static uint32_t unlink_types(void *head, const VkStructureType *types, uint32_t type_count,
|
||||
struct unlinked *out, uint32_t max)
|
||||
{
|
||||
uint32_t n = 0;
|
||||
VkBaseOutStructure *prev = head;
|
||||
while (prev && prev->pNext && n < max) {
|
||||
VkBaseOutStructure *s = prev->pNext;
|
||||
bool drop = false;
|
||||
for (uint32_t i = 0; i < type_count; i++)
|
||||
drop |= s->sType == types[i];
|
||||
if (drop) {
|
||||
out[n++] = (struct unlinked){prev, s};
|
||||
prev->pNext = s->pNext;
|
||||
} else {
|
||||
prev = s;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static void relink(struct unlinked *list, uint32_t n)
|
||||
{
|
||||
while (n--)
|
||||
list[n].prev->pNext = list[n].node;
|
||||
}
|
||||
|
||||
static uint32_t foreign_types(uint32_t gaps, VkStructureType *types)
|
||||
{
|
||||
uint32_t n = 0;
|
||||
if (gaps & GAP_DEPTH_CLIP)
|
||||
types[n++] = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT;
|
||||
if (gaps & GAP_NULL_DESCRIPTOR)
|
||||
types[n++] = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT;
|
||||
return n;
|
||||
}
|
||||
|
||||
static void report_features(VkPhysicalDeviceFeatures *features, uint32_t gaps)
|
||||
{
|
||||
if (gaps & GAP_FILL_MODE)
|
||||
features->fillModeNonSolid = VK_TRUE;
|
||||
if (gaps & GAP_MULTI_VIEWPORT)
|
||||
features->multiViewport = VK_TRUE;
|
||||
if (gaps & GAP_CLIP_DISTANCE)
|
||||
features->shaderClipDistance = VK_TRUE;
|
||||
if (gaps & GAP_CULL_DISTANCE)
|
||||
features->shaderCullDistance = VK_TRUE;
|
||||
if (gaps & GAP_BC)
|
||||
features->textureCompressionBC = VK_TRUE;
|
||||
if (gaps & GAP_MULTI_DRAW)
|
||||
features->multiDrawIndirect = VK_TRUE;
|
||||
}
|
||||
|
||||
/* Which made-up features the application asked for, and off with them before the driver sees them. */
|
||||
static uint32_t take_features(VkPhysicalDeviceFeatures *features, uint32_t gaps)
|
||||
{
|
||||
uint32_t requested = 0;
|
||||
if ((gaps & GAP_FILL_MODE) && features->fillModeNonSolid) {
|
||||
requested |= GAP_FILL_MODE;
|
||||
features->fillModeNonSolid = VK_FALSE;
|
||||
}
|
||||
if ((gaps & GAP_MULTI_VIEWPORT) && features->multiViewport) {
|
||||
requested |= GAP_MULTI_VIEWPORT;
|
||||
features->multiViewport = VK_FALSE;
|
||||
}
|
||||
if ((gaps & GAP_CLIP_DISTANCE) && features->shaderClipDistance) {
|
||||
requested |= GAP_CLIP_DISTANCE;
|
||||
features->shaderClipDistance = VK_FALSE;
|
||||
}
|
||||
if ((gaps & GAP_CULL_DISTANCE) && features->shaderCullDistance) {
|
||||
requested |= GAP_CULL_DISTANCE;
|
||||
features->shaderCullDistance = VK_FALSE;
|
||||
}
|
||||
if ((gaps & GAP_BC) && features->textureCompressionBC) {
|
||||
requested |= GAP_BC;
|
||||
features->textureCompressionBC = VK_FALSE;
|
||||
}
|
||||
if ((gaps & GAP_MULTI_DRAW) && features->multiDrawIndirect) {
|
||||
requested |= GAP_MULTI_DRAW;
|
||||
features->multiDrawIndirect = VK_FALSE;
|
||||
}
|
||||
return requested;
|
||||
}
|
||||
|
||||
static void report_limits(VkPhysicalDeviceLimits *limits, uint32_t gaps)
|
||||
{
|
||||
if (gaps & GAP_MULTI_VIEWPORT)
|
||||
limits->maxViewports = 16;
|
||||
if ((gaps & GAP_MULTI_DRAW) && limits->maxDrawIndirectCount < (1u << 30))
|
||||
limits->maxDrawIndirectCount = 1u << 30;
|
||||
if (gaps & GAPS_SHADER) {
|
||||
limits->maxClipDistances = 8;
|
||||
limits->maxCullDistances = 8;
|
||||
limits->maxCombinedClipAndCullDistances = 8;
|
||||
}
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceFeatures(VkPhysicalDevice physical_device,
|
||||
VkPhysicalDeviceFeatures *features)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(physical_device);
|
||||
inst->GetPhysicalDeviceFeatures(physical_device, features);
|
||||
report_features(features, physical_device_gaps(inst, physical_device));
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceFeatures2(VkPhysicalDevice physical_device,
|
||||
VkPhysicalDeviceFeatures2 *features)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(physical_device);
|
||||
uint32_t gaps = physical_device_gaps(inst, physical_device);
|
||||
VkStructureType types[4];
|
||||
struct unlinked dropped[8];
|
||||
uint32_t dropped_count = unlink_types(features, types, foreign_types(gaps, types), dropped, 8);
|
||||
inst->GetPhysicalDeviceFeatures2(physical_device, features);
|
||||
relink(dropped, dropped_count);
|
||||
report_features(&features->features, gaps);
|
||||
if (tracing()) {
|
||||
char chain[512] = "";
|
||||
size_t used = 0;
|
||||
for (VkBaseOutStructure *s = features->pNext; s && used + 16 < sizeof(chain); s = s->pNext)
|
||||
used += (size_t)snprintf(chain + used, sizeof(chain) - used, " %d", (int)s->sType);
|
||||
layer_log("trace: vkGetPhysicalDeviceFeatures2 (gaps 0x%x, %u structures held back), chain:%s", gaps, dropped_count, chain);
|
||||
}
|
||||
for (VkBaseOutStructure *s = features->pNext; s; s = s->pNext) {
|
||||
if (s->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT && (gaps & GAP_ROBUSTNESS2)) {
|
||||
((VkPhysicalDeviceRobustness2FeaturesEXT *)s)->robustBufferAccess2 = VK_TRUE;
|
||||
if (gaps & GAP_NULL_DESCRIPTOR)
|
||||
((VkPhysicalDeviceRobustness2FeaturesEXT *)s)->nullDescriptor = VK_TRUE;
|
||||
}
|
||||
if (s->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_CLIP_ENABLE_FEATURES_EXT && (gaps & GAP_DEPTH_CLIP))
|
||||
((VkPhysicalDeviceDepthClipEnableFeaturesEXT *)s)->depthClipEnable = VK_TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceProperties(VkPhysicalDevice physical_device,
|
||||
VkPhysicalDeviceProperties *properties)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(physical_device);
|
||||
inst->GetPhysicalDeviceProperties(physical_device, properties);
|
||||
report_limits(&properties->limits, physical_device_gaps(inst, physical_device));
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceProperties2(VkPhysicalDevice physical_device,
|
||||
VkPhysicalDeviceProperties2 *properties)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(physical_device);
|
||||
inst->GetPhysicalDeviceProperties2(physical_device, properties);
|
||||
report_limits(&properties->properties.limits, physical_device_gaps(inst, physical_device));
|
||||
if (tracing())
|
||||
layer_log("trace: vkGetPhysicalDeviceProperties2: %s, Vulkan %u.%u.%u, type %d", properties->properties.deviceName,
|
||||
VK_API_VERSION_MAJOR(properties->properties.apiVersion), VK_API_VERSION_MINOR(properties->properties.apiVersion),
|
||||
VK_API_VERSION_PATCH(properties->properties.apiVersion), (int)properties->properties.deviceType);
|
||||
}
|
||||
|
||||
/* The extensions the layer stands in for, added to the driver's list. */
|
||||
static VKAPI_ATTR VkResult VKAPI_CALL EnumerateDeviceExtensionProperties(VkPhysicalDevice physical_device,
|
||||
const char *layer_name, uint32_t *count,
|
||||
VkExtensionProperties *properties)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(physical_device);
|
||||
if (layer_name)
|
||||
return inst->EnumerateDeviceExtensionProperties(physical_device, layer_name, count, properties);
|
||||
uint32_t gaps = physical_device_gaps(inst, physical_device);
|
||||
VkExtensionProperties extra[2];
|
||||
uint32_t extra_count = 0;
|
||||
if (gaps & GAP_DEPTH_CLIP)
|
||||
extra[extra_count++] = (VkExtensionProperties){VK_EXT_DEPTH_CLIP_ENABLE_EXTENSION_NAME, 1};
|
||||
if (gaps & GAP_NULL_DESCRIPTOR)
|
||||
extra[extra_count++] = (VkExtensionProperties){VK_EXT_ROBUSTNESS_2_EXTENSION_NAME, 1};
|
||||
uint32_t own = 0;
|
||||
VkResult result = inst->EnumerateDeviceExtensionProperties(physical_device, NULL, &own, NULL);
|
||||
if (result != VK_SUCCESS)
|
||||
return result;
|
||||
if (!properties) {
|
||||
*count = own + extra_count;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
VkExtensionProperties *all = calloc(own + extra_count, sizeof(*all));
|
||||
inst->EnumerateDeviceExtensionProperties(physical_device, NULL, &own, all);
|
||||
memcpy(all + own, extra, extra_count * sizeof(*extra));
|
||||
uint32_t total = own + extra_count;
|
||||
if (tracing()) {
|
||||
layer_log("trace: vkEnumerateDeviceExtensionProperties: %u from the driver, %u added by the layer", own, extra_count);
|
||||
trace_extension_names(all, total);
|
||||
}
|
||||
uint32_t n = *count < total ? *count : total;
|
||||
memcpy(properties, all, n * sizeof(*all));
|
||||
free(all);
|
||||
*count = n;
|
||||
return n < total ? VK_INCOMPLETE : VK_SUCCESS;
|
||||
}
|
||||
|
||||
static VKAPI_ATTR VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *info,
|
||||
const VkAllocationCallbacks *allocator, VkInstance *instance)
|
||||
{
|
||||
VkLayerInstanceCreateInfo *link = (VkLayerInstanceCreateInfo *)info->pNext;
|
||||
while (link && !(link->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && link->function == VK_LAYER_LINK_INFO))
|
||||
link = (VkLayerInstanceCreateInfo *)link->pNext;
|
||||
if (!link)
|
||||
return VK_ERROR_INITIALIZATION_FAILED;
|
||||
PFN_vkGetInstanceProcAddr gipa = link->u.pLayerInfo->pfnNextGetInstanceProcAddr;
|
||||
link->u.pLayerInfo = link->u.pLayerInfo->pNext;
|
||||
|
||||
PFN_vkCreateInstance create = (PFN_vkCreateInstance)gipa(VK_NULL_HANDLE, "vkCreateInstance");
|
||||
VkResult result = create(info, allocator, instance);
|
||||
if (tracing())
|
||||
layer_log("trace: vkCreateInstance for %s (API %u.%u): %d, layer %s", info->pApplicationInfo && info->pApplicationInfo->pApplicationName ?
|
||||
info->pApplicationInfo->pApplicationName : "?", info->pApplicationInfo ? VK_API_VERSION_MAJOR(info->pApplicationInfo->apiVersion) : 0,
|
||||
info->pApplicationInfo ? VK_API_VERSION_MINOR(info->pApplicationInfo->apiVersion) : 0, (int)result, wanted() ? "active" : "idle");
|
||||
if (result != VK_SUCCESS)
|
||||
return result;
|
||||
|
||||
struct instance *inst = calloc(1, sizeof(*inst));
|
||||
inst->key = dispatch_key(*instance);
|
||||
inst->handle = *instance;
|
||||
inst->active = wanted();
|
||||
inst->gipa = gipa;
|
||||
pthread_mutex_init(&inst->lock, NULL);
|
||||
#define LOAD(name) inst->name = (PFN_vk##name)gipa(*instance, "vk" #name);
|
||||
#define LOAD_KHR(name) \
|
||||
if (!inst->name) \
|
||||
inst->name = (PFN_vk##name)gipa(*instance, "vk" #name "KHR");
|
||||
LOAD(DestroyInstance)
|
||||
LOAD(EnumerateDeviceExtensionProperties)
|
||||
LOAD(GetPhysicalDeviceFeatures)
|
||||
LOAD(GetPhysicalDeviceFeatures2)
|
||||
LOAD_KHR(GetPhysicalDeviceFeatures2)
|
||||
LOAD(GetPhysicalDeviceProperties)
|
||||
LOAD(GetPhysicalDeviceProperties2)
|
||||
LOAD_KHR(GetPhysicalDeviceProperties2)
|
||||
LOAD(GetPhysicalDeviceMemoryProperties)
|
||||
LOAD(GetPhysicalDeviceFormatProperties)
|
||||
LOAD(GetPhysicalDeviceFormatProperties2)
|
||||
LOAD_KHR(GetPhysicalDeviceFormatProperties2)
|
||||
LOAD(GetPhysicalDeviceImageFormatProperties)
|
||||
LOAD(GetPhysicalDeviceImageFormatProperties2)
|
||||
LOAD_KHR(GetPhysicalDeviceImageFormatProperties2)
|
||||
#undef LOAD
|
||||
#undef LOAD_KHR
|
||||
if (tracing())
|
||||
trace_load(inst);
|
||||
/* Without the Vulkan 1.1 queries there is no way to answer the chained ones. */
|
||||
if (!inst->GetPhysicalDeviceFeatures2 || !inst->GetPhysicalDeviceProperties2 ||
|
||||
!inst->GetPhysicalDeviceFormatProperties2 || !inst->GetPhysicalDeviceImageFormatProperties2)
|
||||
inst->active = false;
|
||||
|
||||
pthread_mutex_lock(&global_lock);
|
||||
inst->next = instances;
|
||||
instances = inst;
|
||||
pthread_mutex_unlock(&global_lock);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL DestroyInstance(VkInstance instance, const VkAllocationCallbacks *allocator)
|
||||
{
|
||||
void *key = dispatch_key(instance);
|
||||
pthread_mutex_lock(&global_lock);
|
||||
struct instance **link = &instances;
|
||||
while (*link && (*link)->key != key)
|
||||
link = &(*link)->next;
|
||||
struct instance *inst = *link;
|
||||
if (inst)
|
||||
*link = inst->next;
|
||||
pthread_mutex_unlock(&global_lock);
|
||||
if (!inst)
|
||||
return;
|
||||
inst->DestroyInstance(instance, allocator);
|
||||
pthread_mutex_destroy(&inst->lock);
|
||||
free(inst);
|
||||
}
|
||||
|
||||
static VKAPI_ATTR VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice physical_device, const VkDeviceCreateInfo *info,
|
||||
const VkAllocationCallbacks *allocator, VkDevice *device)
|
||||
{
|
||||
VkLayerDeviceCreateInfo *link = (VkLayerDeviceCreateInfo *)info->pNext;
|
||||
while (link && !(link->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && link->function == VK_LAYER_LINK_INFO))
|
||||
link = (VkLayerDeviceCreateInfo *)link->pNext;
|
||||
if (!link)
|
||||
return VK_ERROR_INITIALIZATION_FAILED;
|
||||
PFN_vkGetInstanceProcAddr gipa = link->u.pLayerInfo->pfnNextGetInstanceProcAddr;
|
||||
PFN_vkGetDeviceProcAddr gdpa = link->u.pLayerInfo->pfnNextGetDeviceProcAddr;
|
||||
link->u.pLayerInfo = link->u.pLayerInfo->pNext;
|
||||
|
||||
struct instance *inst = instance_from_handle(physical_device);
|
||||
PFN_vkCreateDevice create = (PFN_vkCreateDevice)gipa(inst->handle, "vkCreateDevice");
|
||||
uint32_t gaps = inst->active ? physical_device_gaps(inst, physical_device) : 0;
|
||||
|
||||
/* The application's own structures are put back after the call: DXVK reads its enabled
|
||||
* features again later, and must keep seeing what it asked for. */
|
||||
VkDeviceCreateInfo copy = *info;
|
||||
VkPhysicalDeviceFeatures features;
|
||||
VkPhysicalDeviceFeatures2 *features2 = NULL;
|
||||
VkPhysicalDeviceRobustness2FeaturesEXT *robust = NULL;
|
||||
for (VkBaseOutStructure *s = (VkBaseOutStructure *)info->pNext; s; s = s->pNext) {
|
||||
if (s->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2)
|
||||
features2 = (VkPhysicalDeviceFeatures2 *)s;
|
||||
else if (s->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT)
|
||||
robust = (VkPhysicalDeviceRobustness2FeaturesEXT *)s;
|
||||
}
|
||||
VkPhysicalDeviceFeatures *enabled = NULL;
|
||||
VkPhysicalDeviceFeatures saved_features = {0};
|
||||
if (info->pEnabledFeatures) {
|
||||
features = *info->pEnabledFeatures;
|
||||
copy.pEnabledFeatures = &features;
|
||||
enabled = &features;
|
||||
} else if (features2) {
|
||||
saved_features = features2->features;
|
||||
enabled = &features2->features;
|
||||
}
|
||||
uint32_t requested = enabled ? take_features(enabled, gaps) : 0;
|
||||
VkBool32 saved_robust = robust ? robust->robustBufferAccess2 : VK_FALSE;
|
||||
if (robust && (gaps & GAP_ROBUSTNESS2) && robust->robustBufferAccess2) {
|
||||
requested |= GAP_ROBUSTNESS2;
|
||||
robust->robustBufferAccess2 = VK_FALSE;
|
||||
/* The first version of robust buffer access is the nearest the driver has. */
|
||||
if (enabled)
|
||||
enabled->robustBufferAccess = VK_TRUE;
|
||||
}
|
||||
|
||||
/* The extensions the layer stands in for leave the list, and their structures the chain. */
|
||||
const char **names = NULL;
|
||||
if (gaps & (GAP_DEPTH_CLIP | GAP_NULL_DESCRIPTOR)) {
|
||||
names = malloc((info->enabledExtensionCount ? info->enabledExtensionCount : 1) * sizeof(*names));
|
||||
copy.enabledExtensionCount = 0;
|
||||
for (uint32_t i = 0; i < info->enabledExtensionCount; i++) {
|
||||
const char *name = info->ppEnabledExtensionNames[i];
|
||||
if ((gaps & GAP_DEPTH_CLIP) && !strcmp(name, VK_EXT_DEPTH_CLIP_ENABLE_EXTENSION_NAME)) {
|
||||
requested |= GAP_DEPTH_CLIP;
|
||||
continue;
|
||||
}
|
||||
if ((gaps & GAP_NULL_DESCRIPTOR) && !strcmp(name, VK_EXT_ROBUSTNESS_2_EXTENSION_NAME)) {
|
||||
requested |= GAP_NULL_DESCRIPTOR;
|
||||
if (enabled)
|
||||
enabled->robustBufferAccess = VK_TRUE;
|
||||
continue;
|
||||
}
|
||||
names[copy.enabledExtensionCount++] = name;
|
||||
}
|
||||
copy.ppEnabledExtensionNames = names;
|
||||
}
|
||||
VkStructureType foreign[4];
|
||||
struct unlinked dropped[8];
|
||||
uint32_t dropped_count = unlink_types(©, foreign, foreign_types(gaps, foreign), dropped, 8);
|
||||
|
||||
VkResult result = create(physical_device, ©, allocator, device);
|
||||
|
||||
relink(dropped, dropped_count);
|
||||
free(names);
|
||||
if (features2 && !info->pEnabledFeatures)
|
||||
features2->features = saved_features;
|
||||
if (robust)
|
||||
robust->robustBufferAccess2 = saved_robust;
|
||||
if (result != VK_SUCCESS)
|
||||
return result;
|
||||
|
||||
struct device *dev = calloc(1, sizeof(*dev));
|
||||
dev->key = dispatch_key(*device);
|
||||
dev->handle = *device;
|
||||
dev->gdpa = gdpa;
|
||||
dev->emulated = requested;
|
||||
pthread_mutex_init(&dev->lock, NULL);
|
||||
#define LOAD(name) \
|
||||
dev->name = (PFN_vk##name)gdpa(*device, "vk" #name); \
|
||||
if (!dev->name) \
|
||||
dev->name = (PFN_vk##name)gdpa(*device, "vk" #name "KHR"); \
|
||||
if (!dev->name) \
|
||||
dev->name = (PFN_vk##name)gdpa(*device, "vk" #name "EXT");
|
||||
DEVICE_FUNCTIONS(LOAD)
|
||||
#undef LOAD
|
||||
inst->GetPhysicalDeviceMemoryProperties(physical_device, &dev->memory_properties);
|
||||
VkPhysicalDeviceProperties properties;
|
||||
inst->GetPhysicalDeviceProperties(physical_device, &properties);
|
||||
uint32_t components = properties.limits.maxVertexOutputComponents;
|
||||
if (properties.limits.maxFragmentInputComponents < components)
|
||||
components = properties.limits.maxFragmentInputComponents;
|
||||
dev->max_varying_locations = components / 4;
|
||||
|
||||
pthread_mutex_lock(&global_lock);
|
||||
dev->next = devices;
|
||||
devices = dev;
|
||||
pthread_mutex_unlock(&global_lock);
|
||||
|
||||
if (requested) {
|
||||
layer_log("emulating%s%s%s%s%s%s%s%s%s for %s", (requested & GAP_FILL_MODE) ? " fillModeNonSolid" : "",
|
||||
(requested & GAP_MULTI_VIEWPORT) ? " multiViewport" : "",
|
||||
(requested & GAP_CLIP_DISTANCE) ? " shaderClipDistance" : "",
|
||||
(requested & GAP_CULL_DISTANCE) ? " shaderCullDistance" : "",
|
||||
(requested & GAP_ROBUSTNESS2) ? " robustBufferAccess2" : "",
|
||||
(requested & GAP_BC) ? " textureCompressionBC" : "",
|
||||
(requested & GAP_MULTI_DRAW) ? " multiDrawIndirect" : "",
|
||||
(requested & GAP_DEPTH_CLIP) ? " depthClipEnable" : "",
|
||||
(requested & GAP_NULL_DESCRIPTOR) ? " nullDescriptor" : "", properties.deviceName);
|
||||
}
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *allocator)
|
||||
{
|
||||
void *key = dispatch_key(device);
|
||||
pthread_mutex_lock(&global_lock);
|
||||
struct device **link = &devices;
|
||||
while (*link && (*link)->key != key)
|
||||
link = &(*link)->next;
|
||||
struct device *dev = *link;
|
||||
if (dev)
|
||||
*link = dev->next;
|
||||
pthread_mutex_unlock(&global_lock);
|
||||
if (!dev)
|
||||
return;
|
||||
bc_destroy_device(dev);
|
||||
null_resources_destroy(dev);
|
||||
dev->DestroyDevice(device, allocator);
|
||||
for (uint32_t i = 0; i < dev->shader_modules.capacity; i++) {
|
||||
struct spirv_code *code = dev->shader_modules.values[i];
|
||||
if (code) {
|
||||
free(code->words);
|
||||
free(code);
|
||||
}
|
||||
}
|
||||
map_free(&dev->shader_modules);
|
||||
pthread_mutex_destroy(&dev->lock);
|
||||
free(dev);
|
||||
}
|
||||
|
||||
struct hook {
|
||||
const char *name;
|
||||
PFN_vkVoidFunction function;
|
||||
uint32_t gaps;
|
||||
};
|
||||
|
||||
#define HOOK(name, gaps) {"vk" #name, (PFN_vkVoidFunction)name, gaps}
|
||||
#define HOOK_ALIAS(name, alias, gaps) {"vk" #alias, (PFN_vkVoidFunction)name, gaps}
|
||||
|
||||
static const struct hook device_hooks[] = {
|
||||
HOOK(CreateShaderModule, GAPS_SHADER),
|
||||
HOOK(DestroyShaderModule, GAPS_SHADER),
|
||||
HOOK(CreateGraphicsPipelines, GAPS_PIPELINE),
|
||||
HOOK(CmdSetViewport, GAP_MULTI_VIEWPORT),
|
||||
HOOK(CmdSetScissor, GAP_MULTI_VIEWPORT),
|
||||
HOOK(CmdSetViewportWithCount, GAP_MULTI_VIEWPORT),
|
||||
HOOK_ALIAS(CmdSetViewportWithCount, CmdSetViewportWithCountEXT, GAP_MULTI_VIEWPORT),
|
||||
HOOK(CmdSetScissorWithCount, GAP_MULTI_VIEWPORT),
|
||||
HOOK_ALIAS(CmdSetScissorWithCount, CmdSetScissorWithCountEXT, GAP_MULTI_VIEWPORT),
|
||||
HOOK(CreateImage, GAP_BC),
|
||||
HOOK(DestroyImage, GAP_BC),
|
||||
HOOK(CreateImageView, GAP_BC),
|
||||
HOOK(GetDeviceImageMemoryRequirements, GAP_BC),
|
||||
HOOK_ALIAS(GetDeviceImageMemoryRequirements, GetDeviceImageMemoryRequirementsKHR, GAP_BC),
|
||||
HOOK(GetDeviceImageSparseMemoryRequirements, GAP_BC),
|
||||
HOOK_ALIAS(GetDeviceImageSparseMemoryRequirements, GetDeviceImageSparseMemoryRequirementsKHR, GAP_BC),
|
||||
HOOK(GetDeviceImageSubresourceLayout, GAP_BC),
|
||||
HOOK_ALIAS(GetDeviceImageSubresourceLayout, GetDeviceImageSubresourceLayoutKHR, GAP_BC),
|
||||
HOOK(CmdCopyBufferToImage, GAP_BC),
|
||||
HOOK(CmdCopyBufferToImage2, GAP_BC),
|
||||
HOOK_ALIAS(CmdCopyBufferToImage2, CmdCopyBufferToImage2KHR, GAP_BC),
|
||||
HOOK(CmdCopyImageToBuffer, GAP_BC),
|
||||
HOOK(CmdCopyImageToBuffer2, GAP_BC),
|
||||
HOOK_ALIAS(CmdCopyImageToBuffer2, CmdCopyImageToBuffer2KHR, GAP_BC),
|
||||
HOOK(CmdCopyImage, GAP_BC),
|
||||
HOOK(CmdCopyImage2, GAP_BC),
|
||||
HOOK_ALIAS(CmdCopyImage2, CmdCopyImage2KHR, GAP_BC),
|
||||
HOOK(BindBufferMemory, GAP_BC),
|
||||
HOOK(BindBufferMemory2, GAP_BC),
|
||||
HOOK_ALIAS(BindBufferMemory2, BindBufferMemory2KHR, GAP_BC),
|
||||
HOOK(DestroyBuffer, GAP_BC),
|
||||
HOOK(FreeMemory, GAP_BC),
|
||||
HOOK(MapMemory, GAP_BC),
|
||||
HOOK(MapMemory2, GAP_BC),
|
||||
HOOK_ALIAS(MapMemory2, MapMemory2KHR, GAP_BC),
|
||||
HOOK(UnmapMemory, GAP_BC),
|
||||
HOOK(UnmapMemory2, GAP_BC),
|
||||
HOOK_ALIAS(UnmapMemory2, UnmapMemory2KHR, GAP_BC),
|
||||
HOOK(AllocateCommandBuffers, GAP_BC),
|
||||
HOOK(FreeCommandBuffers, GAP_BC),
|
||||
HOOK(BeginCommandBuffer, GAP_BC),
|
||||
HOOK(ResetCommandBuffer, GAP_BC),
|
||||
HOOK(ResetCommandPool, GAP_BC),
|
||||
HOOK(DestroyCommandPool, GAP_BC),
|
||||
HOOK(CmdDrawIndirect, GAP_MULTI_DRAW),
|
||||
HOOK(CmdDrawIndexedIndirect, GAP_MULTI_DRAW),
|
||||
HOOK(UpdateDescriptorSets, GAP_NULL_DESCRIPTOR),
|
||||
HOOK(CmdPushDescriptorSet, GAP_NULL_DESCRIPTOR),
|
||||
HOOK_ALIAS(CmdPushDescriptorSet, CmdPushDescriptorSetKHR, GAP_NULL_DESCRIPTOR),
|
||||
HOOK(CreateDescriptorUpdateTemplate, GAP_NULL_DESCRIPTOR),
|
||||
HOOK_ALIAS(CreateDescriptorUpdateTemplate, CreateDescriptorUpdateTemplateKHR, GAP_NULL_DESCRIPTOR),
|
||||
HOOK(DestroyDescriptorUpdateTemplate, GAP_NULL_DESCRIPTOR),
|
||||
HOOK_ALIAS(DestroyDescriptorUpdateTemplate, DestroyDescriptorUpdateTemplateKHR, GAP_NULL_DESCRIPTOR),
|
||||
HOOK(UpdateDescriptorSetWithTemplate, GAP_NULL_DESCRIPTOR),
|
||||
HOOK_ALIAS(UpdateDescriptorSetWithTemplate, UpdateDescriptorSetWithTemplateKHR, GAP_NULL_DESCRIPTOR),
|
||||
HOOK(CmdPushDescriptorSetWithTemplate, GAP_NULL_DESCRIPTOR),
|
||||
HOOK_ALIAS(CmdPushDescriptorSetWithTemplate, CmdPushDescriptorSetWithTemplateKHR, GAP_NULL_DESCRIPTOR),
|
||||
HOOK(CmdBindVertexBuffers, GAP_NULL_DESCRIPTOR),
|
||||
HOOK(CmdBindVertexBuffers2, GAP_NULL_DESCRIPTOR),
|
||||
HOOK_ALIAS(CmdBindVertexBuffers2, CmdBindVertexBuffers2EXT, GAP_NULL_DESCRIPTOR),
|
||||
};
|
||||
|
||||
static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char *name);
|
||||
|
||||
static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetDeviceProcAddr(VkDevice device, const char *name)
|
||||
{
|
||||
if (!strcmp(name, "vkGetDeviceProcAddr"))
|
||||
return (PFN_vkVoidFunction)GetDeviceProcAddr;
|
||||
struct device *dev = device_from_handle(device);
|
||||
if (!dev)
|
||||
return NULL;
|
||||
if (!strcmp(name, "vkDestroyDevice"))
|
||||
return (PFN_vkVoidFunction)DestroyDevice;
|
||||
PFN_vkVoidFunction next = dev->gdpa(device, name);
|
||||
if (!next)
|
||||
return NULL;
|
||||
for (size_t i = 0; i < sizeof(device_hooks) / sizeof(device_hooks[0]); i++) {
|
||||
if ((dev->emulated & device_hooks[i].gaps) && !strcmp(name, device_hooks[i].name))
|
||||
return device_hooks[i].function;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
static const struct hook physical_device_hooks[] = {
|
||||
HOOK(EnumerateDeviceExtensionProperties, 0),
|
||||
HOOK(GetPhysicalDeviceFeatures, 0),
|
||||
HOOK(GetPhysicalDeviceFeatures2, 0),
|
||||
HOOK_ALIAS(GetPhysicalDeviceFeatures2, GetPhysicalDeviceFeatures2KHR, 0),
|
||||
HOOK(GetPhysicalDeviceProperties, 0),
|
||||
HOOK(GetPhysicalDeviceProperties2, 0),
|
||||
HOOK_ALIAS(GetPhysicalDeviceProperties2, GetPhysicalDeviceProperties2KHR, 0),
|
||||
HOOK(GetPhysicalDeviceFormatProperties, 0),
|
||||
HOOK(GetPhysicalDeviceFormatProperties2, 0),
|
||||
HOOK_ALIAS(GetPhysicalDeviceFormatProperties2, GetPhysicalDeviceFormatProperties2KHR, 0),
|
||||
HOOK(GetPhysicalDeviceImageFormatProperties, 0),
|
||||
HOOK(GetPhysicalDeviceImageFormatProperties2, 0),
|
||||
HOOK_ALIAS(GetPhysicalDeviceImageFormatProperties2, GetPhysicalDeviceImageFormatProperties2KHR, 0),
|
||||
};
|
||||
|
||||
static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL GetInstanceProcAddr(VkInstance instance, const char *name)
|
||||
{
|
||||
if (!strcmp(name, "vkGetInstanceProcAddr"))
|
||||
return (PFN_vkVoidFunction)GetInstanceProcAddr;
|
||||
if (!strcmp(name, "vkCreateInstance"))
|
||||
return (PFN_vkVoidFunction)CreateInstance;
|
||||
if (!strcmp(name, "vkGetDeviceProcAddr"))
|
||||
return (PFN_vkVoidFunction)GetDeviceProcAddr;
|
||||
if (!instance)
|
||||
return NULL;
|
||||
struct instance *inst = instance_from_handle(instance);
|
||||
if (!inst)
|
||||
return NULL;
|
||||
if (!strcmp(name, "vkDestroyInstance"))
|
||||
return (PFN_vkVoidFunction)DestroyInstance;
|
||||
/* Always ours, even when inactive: each layer moves the device chain one link on. */
|
||||
if (!strcmp(name, "vkCreateDevice"))
|
||||
return (PFN_vkVoidFunction)CreateDevice;
|
||||
PFN_vkVoidFunction next = inst->gipa(instance, name);
|
||||
if (next && tracing()) {
|
||||
PFN_vkVoidFunction traced = trace_hook(inst, name);
|
||||
if (traced)
|
||||
return traced;
|
||||
}
|
||||
if (!next || !inst->active)
|
||||
return next;
|
||||
for (size_t i = 0; i < sizeof(physical_device_hooks) / sizeof(physical_device_hooks[0]); i++) {
|
||||
if (!strcmp(name, physical_device_hooks[i].name))
|
||||
return physical_device_hooks[i].function;
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
__attribute__((visibility("default"))) VKAPI_ATTR VkResult VKAPI_CALL
|
||||
vkNegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *interface)
|
||||
{
|
||||
if (interface->loaderLayerInterfaceVersion < 2)
|
||||
return VK_ERROR_INITIALIZATION_FAILED;
|
||||
interface->loaderLayerInterfaceVersion = 2;
|
||||
interface->pfnGetInstanceProcAddr = GetInstanceProcAddr;
|
||||
interface->pfnGetDeviceProcAddr = GetDeviceProcAddr;
|
||||
interface->pfnGetPhysicalDeviceProcAddr = NULL;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
/*
|
||||
* VK_LAYER_DROIDDECK_mali_compat: makes up for what Mali lacks and DXVK requires.
|
||||
*
|
||||
* DXVK refuses any GPU without fillModeNonSolid, multiViewport, shaderClipDistance,
|
||||
* shaderCullDistance, textureCompressionBC and robustBufferAccess2, and Mali has none of them.
|
||||
* The layer reports each missing feature as supported, keeps it out of the driver's device, and
|
||||
* handles what uses it: polygon modes draw filled, only the first viewport is kept, clip distances
|
||||
* become a varying the fragment shader discards on, cull distances are dropped, and BC textures
|
||||
* are decoded on the CPU into uncompressed images as they are uploaded.
|
||||
*/
|
||||
#ifndef MALI_COMPAT_H
|
||||
#define MALI_COMPAT_H
|
||||
|
||||
#include <pthread.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <vulkan/vk_layer.h>
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
enum gap {
|
||||
GAP_FILL_MODE = 1u << 0,
|
||||
GAP_MULTI_VIEWPORT = 1u << 1,
|
||||
GAP_CLIP_DISTANCE = 1u << 2,
|
||||
GAP_CULL_DISTANCE = 1u << 3,
|
||||
GAP_ROBUSTNESS2 = 1u << 4,
|
||||
GAP_BC = 1u << 5,
|
||||
/* draw.c and pipeline.c (added for Mali-G57 and drivers without these, on top of David Cimo's six) */
|
||||
GAP_MULTI_DRAW = 1u << 6, /* multiDrawIndirect: indirect draws looped one by one */
|
||||
GAP_DEPTH_CLIP = 1u << 7, /* VK_EXT_depth_clip_enable: depthClipEnable off becomes depth clamp */
|
||||
GAP_NULL_DESCRIPTOR = 1u << 8, /* VK_EXT_robustness2 missing entirely: nullDescriptor stand-ins */
|
||||
};
|
||||
|
||||
#define GAPS_SHADER (GAP_CLIP_DISTANCE | GAP_CULL_DISTANCE)
|
||||
#define GAPS_PIPELINE (GAP_FILL_MODE | GAP_MULTI_VIEWPORT | GAPS_SHADER | GAP_DEPTH_CLIP)
|
||||
|
||||
void layer_log(const char *format, ...) __attribute__((format(printf, 1, 2)));
|
||||
|
||||
/* Open addressing from a Vulkan handle to a pointer. Not locked: the owner holds its lock. */
|
||||
struct handle_map {
|
||||
uint64_t *keys;
|
||||
void **values;
|
||||
uint32_t capacity;
|
||||
uint32_t count;
|
||||
};
|
||||
|
||||
void *map_get(const struct handle_map *map, uint64_t key);
|
||||
void map_put(struct handle_map *map, uint64_t key, void *value);
|
||||
void *map_remove(struct handle_map *map, uint64_t key);
|
||||
void map_free(struct handle_map *map);
|
||||
|
||||
struct physical_device {
|
||||
VkPhysicalDevice handle;
|
||||
uint32_t gaps;
|
||||
};
|
||||
|
||||
struct instance {
|
||||
void *key;
|
||||
VkInstance handle;
|
||||
bool active;
|
||||
PFN_vkGetInstanceProcAddr gipa;
|
||||
PFN_vkDestroyInstance DestroyInstance;
|
||||
PFN_vkEnumerateDeviceExtensionProperties EnumerateDeviceExtensionProperties;
|
||||
PFN_vkGetPhysicalDeviceFeatures GetPhysicalDeviceFeatures;
|
||||
PFN_vkGetPhysicalDeviceFeatures2 GetPhysicalDeviceFeatures2;
|
||||
PFN_vkGetPhysicalDeviceProperties GetPhysicalDeviceProperties;
|
||||
PFN_vkGetPhysicalDeviceProperties2 GetPhysicalDeviceProperties2;
|
||||
PFN_vkGetPhysicalDeviceMemoryProperties GetPhysicalDeviceMemoryProperties;
|
||||
PFN_vkGetPhysicalDeviceFormatProperties GetPhysicalDeviceFormatProperties;
|
||||
PFN_vkGetPhysicalDeviceFormatProperties2 GetPhysicalDeviceFormatProperties2;
|
||||
PFN_vkGetPhysicalDeviceImageFormatProperties GetPhysicalDeviceImageFormatProperties;
|
||||
PFN_vkGetPhysicalDeviceImageFormatProperties2 GetPhysicalDeviceImageFormatProperties2;
|
||||
PFN_vkEnumeratePhysicalDevices EnumeratePhysicalDevices;
|
||||
PFN_vkGetPhysicalDeviceMemoryProperties2 GetPhysicalDeviceMemoryProperties2;
|
||||
PFN_vkGetPhysicalDeviceQueueFamilyProperties GetPhysicalDeviceQueueFamilyProperties;
|
||||
PFN_vkGetPhysicalDeviceQueueFamilyProperties2 GetPhysicalDeviceQueueFamilyProperties2;
|
||||
PFN_vkGetPhysicalDeviceExternalBufferProperties GetPhysicalDeviceExternalBufferProperties;
|
||||
PFN_vkGetPhysicalDeviceExternalSemaphoreProperties GetPhysicalDeviceExternalSemaphoreProperties;
|
||||
PFN_vkGetPhysicalDeviceExternalFenceProperties GetPhysicalDeviceExternalFenceProperties;
|
||||
pthread_mutex_t lock;
|
||||
struct physical_device physical_devices[8];
|
||||
uint32_t physical_device_count;
|
||||
struct instance *next;
|
||||
};
|
||||
|
||||
#define DEVICE_FUNCTIONS(X) \
|
||||
X(DestroyDevice) \
|
||||
X(CreateShaderModule) \
|
||||
X(DestroyShaderModule) \
|
||||
X(CreateGraphicsPipelines) \
|
||||
X(CmdSetViewport) \
|
||||
X(CmdSetScissor) \
|
||||
X(CmdSetViewportWithCount) \
|
||||
X(CmdSetScissorWithCount) \
|
||||
X(CreateImage) \
|
||||
X(DestroyImage) \
|
||||
X(CreateImageView) \
|
||||
X(GetDeviceImageMemoryRequirements) \
|
||||
X(GetDeviceImageSparseMemoryRequirements) \
|
||||
X(GetDeviceImageSubresourceLayout) \
|
||||
X(CmdCopyBufferToImage) \
|
||||
X(CmdCopyBufferToImage2) \
|
||||
X(CmdCopyImageToBuffer) \
|
||||
X(CmdCopyImageToBuffer2) \
|
||||
X(CmdCopyImage) \
|
||||
X(CmdCopyImage2) \
|
||||
X(CreateBuffer) \
|
||||
X(DestroyBuffer) \
|
||||
X(GetBufferMemoryRequirements) \
|
||||
X(BindBufferMemory) \
|
||||
X(BindBufferMemory2) \
|
||||
X(AllocateMemory) \
|
||||
X(FreeMemory) \
|
||||
X(MapMemory) \
|
||||
X(MapMemory2) \
|
||||
X(UnmapMemory) \
|
||||
X(UnmapMemory2) \
|
||||
X(AllocateCommandBuffers) \
|
||||
X(FreeCommandBuffers) \
|
||||
X(BeginCommandBuffer) \
|
||||
X(ResetCommandBuffer) \
|
||||
X(ResetCommandPool) \
|
||||
X(DestroyCommandPool) \
|
||||
X(CmdDrawIndirect) \
|
||||
X(CmdDrawIndexedIndirect) \
|
||||
X(GetImageMemoryRequirements) \
|
||||
X(BindImageMemory) \
|
||||
X(DestroyImageView) \
|
||||
X(CreateBufferView) \
|
||||
X(DestroyBufferView) \
|
||||
X(UpdateDescriptorSets) \
|
||||
X(CmdPushDescriptorSet) \
|
||||
X(CreateDescriptorUpdateTemplate) \
|
||||
X(DestroyDescriptorUpdateTemplate) \
|
||||
X(UpdateDescriptorSetWithTemplate) \
|
||||
X(CmdPushDescriptorSetWithTemplate) \
|
||||
X(CmdBindVertexBuffers) \
|
||||
X(CmdBindVertexBuffers2)
|
||||
|
||||
struct staging_block;
|
||||
|
||||
struct device {
|
||||
void *key;
|
||||
VkDevice handle;
|
||||
PFN_vkGetDeviceProcAddr gdpa;
|
||||
/* The gaps the application enabled a made-up feature for. */
|
||||
uint32_t emulated;
|
||||
/* Locations a clip distance varying may use, from the vertex output and fragment input limits. */
|
||||
uint32_t max_varying_locations;
|
||||
VkPhysicalDeviceMemoryProperties memory_properties;
|
||||
#define DECLARE(name) PFN_vk##name name;
|
||||
DEVICE_FUNCTIONS(DECLARE)
|
||||
#undef DECLARE
|
||||
pthread_mutex_t lock;
|
||||
struct handle_map shader_modules; /* VkShaderModule -> struct spirv_code, for modules with distances */
|
||||
struct handle_map images; /* VkImage -> struct bc_image */
|
||||
struct handle_map buffers; /* VkBuffer -> struct bound_buffer */
|
||||
struct handle_map memories; /* VkDeviceMemory -> struct mapped_memory */
|
||||
struct handle_map command_buffers; /* VkCommandBuffer -> struct command_buffer */
|
||||
struct staging_block *free_staging;
|
||||
struct handle_map templates; /* VkDescriptorUpdateTemplate -> struct template_info (draw.c) */
|
||||
/* nullDescriptor stand-ins (draw.c): 0 not made yet, 1 ready, -1 failed. */
|
||||
int null_ready;
|
||||
VkBuffer null_buffer;
|
||||
VkDeviceMemory null_buffer_memory;
|
||||
VkImage null_image;
|
||||
VkDeviceMemory null_image_memory;
|
||||
VkImageView null_view;
|
||||
/* Texel-buffer stand-ins: RGBA8 for uniform (sampled) texel buffers, R32_UINT for storage. */
|
||||
VkBufferView null_uniform_texel;
|
||||
VkBufferView null_storage_texel;
|
||||
struct device *next;
|
||||
};
|
||||
|
||||
struct device *device_from_handle(const void *dispatchable);
|
||||
|
||||
struct spirv_code {
|
||||
uint32_t *words;
|
||||
size_t count;
|
||||
};
|
||||
|
||||
/* spirv.c */
|
||||
bool spirv_uses_distances(const uint32_t *words, size_t count);
|
||||
/* The module with clip and cull distances taken out; false when it cannot be rewritten. */
|
||||
bool spirv_strip_distances(const uint32_t *words, size_t count, struct spirv_code *out);
|
||||
|
||||
enum clip_role { CLIP_STRIP, CLIP_PRODUCER, CLIP_CONSUMER };
|
||||
|
||||
struct clip_info {
|
||||
bool has_clip; /* a standalone ClipDistance variable this stage could hand on */
|
||||
uint32_t clip_count; /* its array length */
|
||||
uint32_t locations_used; /* one past the highest varying location, outputs or inputs per role */
|
||||
};
|
||||
|
||||
void spirv_clip_info(const uint32_t *words, size_t count, enum clip_role role, struct clip_info *info);
|
||||
/*
|
||||
* Producer: the last stage before rasterization writes its clip distances to `location` instead.
|
||||
* Consumer: the fragment shader reads them there and discards when any is negative.
|
||||
* Both drop cull distances. False when the module cannot be rewritten.
|
||||
*/
|
||||
bool spirv_emulate_clip(const uint32_t *words, size_t count, enum clip_role role, uint32_t location,
|
||||
uint32_t clip_count, struct spirv_code *out);
|
||||
|
||||
/* pipeline.c */
|
||||
VKAPI_ATTR VkResult VKAPI_CALL CreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *info,
|
||||
const VkAllocationCallbacks *allocator, VkShaderModule *module);
|
||||
VKAPI_ATTR void VKAPI_CALL DestroyShaderModule(VkDevice device, VkShaderModule module,
|
||||
const VkAllocationCallbacks *allocator);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL CreateGraphicsPipelines(VkDevice device, VkPipelineCache cache, uint32_t count,
|
||||
const VkGraphicsPipelineCreateInfo *infos,
|
||||
const VkAllocationCallbacks *allocator, VkPipeline *pipelines);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdSetViewport(VkCommandBuffer cmd, uint32_t first, uint32_t count,
|
||||
const VkViewport *viewports);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdSetScissor(VkCommandBuffer cmd, uint32_t first, uint32_t count,
|
||||
const VkRect2D *scissors);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdSetViewportWithCount(VkCommandBuffer cmd, uint32_t count,
|
||||
const VkViewport *viewports);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdSetScissorWithCount(VkCommandBuffer cmd, uint32_t count, const VkRect2D *scissors);
|
||||
|
||||
/* bc.c */
|
||||
VkFormat bc_substitute(VkFormat format);
|
||||
VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceFormatProperties(VkPhysicalDevice physical_device, VkFormat format,
|
||||
VkFormatProperties *properties);
|
||||
VKAPI_ATTR void VKAPI_CALL GetPhysicalDeviceFormatProperties2(VkPhysicalDevice physical_device, VkFormat format,
|
||||
VkFormatProperties2 *properties);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL GetPhysicalDeviceImageFormatProperties(
|
||||
VkPhysicalDevice physical_device, VkFormat format, VkImageType type, VkImageTiling tiling,
|
||||
VkImageUsageFlags usage, VkImageCreateFlags flags, VkImageFormatProperties *properties);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL GetPhysicalDeviceImageFormatProperties2(
|
||||
VkPhysicalDevice physical_device, const VkPhysicalDeviceImageFormatInfo2 *info,
|
||||
VkImageFormatProperties2 *properties);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL CreateImage(VkDevice device, const VkImageCreateInfo *info,
|
||||
const VkAllocationCallbacks *allocator, VkImage *image);
|
||||
VKAPI_ATTR void VKAPI_CALL DestroyImage(VkDevice device, VkImage image, const VkAllocationCallbacks *allocator);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL CreateImageView(VkDevice device, const VkImageViewCreateInfo *info,
|
||||
const VkAllocationCallbacks *allocator, VkImageView *view);
|
||||
VKAPI_ATTR void VKAPI_CALL GetDeviceImageMemoryRequirements(VkDevice device,
|
||||
const VkDeviceImageMemoryRequirements *info,
|
||||
VkMemoryRequirements2 *requirements);
|
||||
VKAPI_ATTR void VKAPI_CALL GetDeviceImageSparseMemoryRequirements(
|
||||
VkDevice device, const VkDeviceImageMemoryRequirements *info, uint32_t *count,
|
||||
VkSparseImageMemoryRequirements2 *requirements);
|
||||
VKAPI_ATTR void VKAPI_CALL GetDeviceImageSubresourceLayout(VkDevice device,
|
||||
const VkDeviceImageSubresourceInfo *info,
|
||||
VkSubresourceLayout2 *layout);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyBufferToImage(VkCommandBuffer cmd, VkBuffer buffer, VkImage image,
|
||||
VkImageLayout layout, uint32_t count,
|
||||
const VkBufferImageCopy *regions);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyBufferToImage2(VkCommandBuffer cmd, const VkCopyBufferToImageInfo2 *info);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyImageToBuffer(VkCommandBuffer cmd, VkImage image, VkImageLayout layout,
|
||||
VkBuffer buffer, uint32_t count, const VkBufferImageCopy *regions);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyImageToBuffer2(VkCommandBuffer cmd, const VkCopyImageToBufferInfo2 *info);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyImage(VkCommandBuffer cmd, VkImage src, VkImageLayout src_layout, VkImage dst,
|
||||
VkImageLayout dst_layout, uint32_t count, const VkImageCopy *regions);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdCopyImage2(VkCommandBuffer cmd, const VkCopyImageInfo2 *info);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL BindBufferMemory(VkDevice device, VkBuffer buffer, VkDeviceMemory memory,
|
||||
VkDeviceSize offset);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL BindBufferMemory2(VkDevice device, uint32_t count,
|
||||
const VkBindBufferMemoryInfo *infos);
|
||||
VKAPI_ATTR void VKAPI_CALL DestroyBuffer(VkDevice device, VkBuffer buffer, const VkAllocationCallbacks *allocator);
|
||||
VKAPI_ATTR void VKAPI_CALL FreeMemory(VkDevice device, VkDeviceMemory memory, const VkAllocationCallbacks *allocator);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL MapMemory(VkDevice device, VkDeviceMemory memory, VkDeviceSize offset,
|
||||
VkDeviceSize size, VkMemoryMapFlags flags, void **data);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL MapMemory2(VkDevice device, const VkMemoryMapInfo *info, void **data);
|
||||
VKAPI_ATTR void VKAPI_CALL UnmapMemory(VkDevice device, VkDeviceMemory memory);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL UnmapMemory2(VkDevice device, const VkMemoryUnmapInfo *info);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL AllocateCommandBuffers(VkDevice device, const VkCommandBufferAllocateInfo *info,
|
||||
VkCommandBuffer *buffers);
|
||||
VKAPI_ATTR void VKAPI_CALL FreeCommandBuffers(VkDevice device, VkCommandPool pool, uint32_t count,
|
||||
const VkCommandBuffer *buffers);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL BeginCommandBuffer(VkCommandBuffer cmd, const VkCommandBufferBeginInfo *info);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL ResetCommandBuffer(VkCommandBuffer cmd, VkCommandBufferResetFlags flags);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL ResetCommandPool(VkDevice device, VkCommandPool pool, VkCommandPoolResetFlags flags);
|
||||
VKAPI_ATTR void VKAPI_CALL DestroyCommandPool(VkDevice device, VkCommandPool pool,
|
||||
const VkAllocationCallbacks *allocator);
|
||||
void bc_destroy_device(struct device *dev);
|
||||
|
||||
/* layer.c */
|
||||
struct instance *instance_from_handle(const void *dispatchable);
|
||||
/* What the GPU lacks of the features the layer can make up for. */
|
||||
uint32_t physical_device_gaps(struct instance *inst, VkPhysicalDevice physical_device);
|
||||
|
||||
/* trace.c (DROIDDECK_MALI_COMPAT_TRACE=1) */
|
||||
void trace_load(struct instance *inst);
|
||||
PFN_vkVoidFunction trace_hook(struct instance *inst, const char *name);
|
||||
void trace_extension_names(const VkExtensionProperties *ext, uint32_t n);
|
||||
|
||||
|
||||
/* draw.c */
|
||||
VKAPI_ATTR void VKAPI_CALL CmdDrawIndirect(VkCommandBuffer cmd, VkBuffer buffer, VkDeviceSize offset, uint32_t draw_count, uint32_t stride);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdDrawIndexedIndirect(VkCommandBuffer cmd, VkBuffer buffer, VkDeviceSize offset, uint32_t draw_count, uint32_t stride);
|
||||
VKAPI_ATTR void VKAPI_CALL UpdateDescriptorSets(VkDevice device, uint32_t write_count, const VkWriteDescriptorSet *writes, uint32_t copy_count, const VkCopyDescriptorSet *copies);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdPushDescriptorSet(VkCommandBuffer cmd, VkPipelineBindPoint bind_point, VkPipelineLayout layout, uint32_t set, uint32_t write_count, const VkWriteDescriptorSet *writes);
|
||||
VKAPI_ATTR VkResult VKAPI_CALL CreateDescriptorUpdateTemplate(VkDevice device, const VkDescriptorUpdateTemplateCreateInfo *info, const VkAllocationCallbacks *allocator, VkDescriptorUpdateTemplate *tmpl);
|
||||
VKAPI_ATTR void VKAPI_CALL DestroyDescriptorUpdateTemplate(VkDevice device, VkDescriptorUpdateTemplate tmpl, const VkAllocationCallbacks *allocator);
|
||||
VKAPI_ATTR void VKAPI_CALL UpdateDescriptorSetWithTemplate(VkDevice device, VkDescriptorSet set, VkDescriptorUpdateTemplate tmpl, const void *data);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdPushDescriptorSetWithTemplate(VkCommandBuffer cmd, VkDescriptorUpdateTemplate tmpl, VkPipelineLayout layout, uint32_t set, const void *data);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdBindVertexBuffers(VkCommandBuffer cmd, uint32_t first, uint32_t count, const VkBuffer *buffers, const VkDeviceSize *offsets);
|
||||
VKAPI_ATTR void VKAPI_CALL CmdBindVertexBuffers2(VkCommandBuffer cmd, uint32_t first, uint32_t count, const VkBuffer *buffers, const VkDeviceSize *offsets, const VkDeviceSize *sizes, const VkDeviceSize *strides);
|
||||
void null_resources_destroy(struct device *dev);
|
||||
#endif
|
||||
@@ -0,0 +1,308 @@
|
||||
#include "mali_compat.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define HANDLE(h) ((uint64_t)(uintptr_t)(h))
|
||||
#define MAX_STAGES 8
|
||||
|
||||
/* A module with distances goes to the driver without them; its original stays here, for the
|
||||
* pipeline that can emulate its clip distances. */
|
||||
VKAPI_ATTR VkResult VKAPI_CALL CreateShaderModule(VkDevice device, const VkShaderModuleCreateInfo *info,
|
||||
const VkAllocationCallbacks *allocator, VkShaderModule *module)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
size_t count = info->codeSize / sizeof(uint32_t);
|
||||
if (!spirv_uses_distances(info->pCode, count))
|
||||
return dev->CreateShaderModule(device, info, allocator, module);
|
||||
|
||||
struct spirv_code stripped;
|
||||
if (!spirv_strip_distances(info->pCode, count, &stripped)) {
|
||||
layer_log("could not take the clip and cull distances out of a shader module");
|
||||
return dev->CreateShaderModule(device, info, allocator, module);
|
||||
}
|
||||
VkShaderModuleCreateInfo copy = *info;
|
||||
copy.pCode = stripped.words;
|
||||
copy.codeSize = stripped.count * sizeof(uint32_t);
|
||||
VkResult result = dev->CreateShaderModule(device, ©, allocator, module);
|
||||
free(stripped.words);
|
||||
if (result != VK_SUCCESS)
|
||||
return result;
|
||||
|
||||
struct spirv_code *original = malloc(sizeof(*original));
|
||||
original->count = count;
|
||||
original->words = malloc(info->codeSize);
|
||||
memcpy(original->words, info->pCode, info->codeSize);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
map_put(&dev->shader_modules, HANDLE(*module), original);
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL DestroyShaderModule(VkDevice device, VkShaderModule module,
|
||||
const VkAllocationCallbacks *allocator)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
struct spirv_code *original = map_remove(&dev->shader_modules, HANDLE(module));
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
if (original) {
|
||||
free(original->words);
|
||||
free(original);
|
||||
}
|
||||
dev->DestroyShaderModule(device, module, allocator);
|
||||
}
|
||||
|
||||
struct stage_code {
|
||||
const uint32_t *words;
|
||||
size_t count;
|
||||
/* The application's own, when the code comes inline in the stage. */
|
||||
VkShaderModuleCreateInfo *inline_info;
|
||||
/* A module whose distances were already taken out at creation. */
|
||||
bool stripped_module;
|
||||
};
|
||||
|
||||
static void find_stage_code(struct device *dev, const VkPipelineShaderStageCreateInfo *stage, struct stage_code *code)
|
||||
{
|
||||
memset(code, 0, sizeof(*code));
|
||||
for (const VkBaseInStructure *s = stage->pNext; s; s = s->pNext) {
|
||||
if (s->sType == VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO) {
|
||||
code->inline_info = (VkShaderModuleCreateInfo *)s;
|
||||
code->words = code->inline_info->pCode;
|
||||
code->count = code->inline_info->codeSize / sizeof(uint32_t);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (stage->module == VK_NULL_HANDLE)
|
||||
return;
|
||||
pthread_mutex_lock(&dev->lock);
|
||||
struct spirv_code *original = map_get(&dev->shader_modules, HANDLE(stage->module));
|
||||
pthread_mutex_unlock(&dev->lock);
|
||||
if (original) {
|
||||
code->words = original->words;
|
||||
code->count = original->count;
|
||||
code->stripped_module = true;
|
||||
}
|
||||
}
|
||||
|
||||
struct pipeline_rewrite {
|
||||
VkPipelineRasterizationStateCreateInfo rasterization;
|
||||
/* VK_EXT_depth_clip_enable's state, taken out of the application's chain for the call. */
|
||||
VkBaseOutStructure *clip_prev, *clip_node;
|
||||
VkPipelineViewportStateCreateInfo viewport;
|
||||
VkPipelineShaderStageCreateInfo stages[MAX_STAGES];
|
||||
VkShaderModuleCreateInfo modules[MAX_STAGES];
|
||||
struct spirv_code code[MAX_STAGES];
|
||||
VkShaderModule temporary[MAX_STAGES];
|
||||
/* Rewritten where they are, when they are not first in their stage's chain. */
|
||||
VkShaderModuleCreateInfo *in_place[MAX_STAGES];
|
||||
const uint32_t *in_place_code[MAX_STAGES];
|
||||
size_t in_place_size[MAX_STAGES];
|
||||
};
|
||||
|
||||
static void use_code(struct device *dev, VkGraphicsPipelineCreateInfo *info, struct pipeline_rewrite *rw,
|
||||
const struct stage_code *code, uint32_t i)
|
||||
{
|
||||
if (info->pStages != rw->stages) {
|
||||
memcpy(rw->stages, info->pStages, info->stageCount * sizeof(*rw->stages));
|
||||
info->pStages = rw->stages;
|
||||
}
|
||||
const struct spirv_code *out = &rw->code[i];
|
||||
if (code->inline_info && rw->stages[i].pNext == code->inline_info) {
|
||||
rw->modules[i] = *code->inline_info;
|
||||
rw->modules[i].pCode = out->words;
|
||||
rw->modules[i].codeSize = out->count * sizeof(uint32_t);
|
||||
rw->stages[i].pNext = &rw->modules[i];
|
||||
} else if (code->inline_info) {
|
||||
rw->in_place[i] = code->inline_info;
|
||||
rw->in_place_code[i] = code->inline_info->pCode;
|
||||
rw->in_place_size[i] = code->inline_info->codeSize;
|
||||
code->inline_info->pCode = out->words;
|
||||
code->inline_info->codeSize = out->count * sizeof(uint32_t);
|
||||
} else {
|
||||
VkShaderModuleCreateInfo module_info = {
|
||||
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
|
||||
.codeSize = out->count * sizeof(uint32_t),
|
||||
.pCode = out->words,
|
||||
};
|
||||
if (dev->CreateShaderModule(dev->handle, &module_info, NULL, &rw->temporary[i]) == VK_SUCCESS)
|
||||
rw->stages[i].module = rw->temporary[i];
|
||||
}
|
||||
}
|
||||
|
||||
static int stage_index(const VkGraphicsPipelineCreateInfo *info, VkShaderStageFlagBits stage)
|
||||
{
|
||||
for (uint32_t i = 0; i < info->stageCount; i++) {
|
||||
if (info->pStages[i].stage == stage)
|
||||
return (int)i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Clip distances are emulated when the last stage before rasterization is the only one that
|
||||
* hands them on: a vertex shader feeding a geometry shader would need a varying of its own.
|
||||
* Anywhere else they are dropped, along with every cull distance.
|
||||
*/
|
||||
static void rewrite_shaders(struct device *dev, VkGraphicsPipelineCreateInfo *info, struct pipeline_rewrite *rw)
|
||||
{
|
||||
uint32_t n = info->stageCount;
|
||||
if (!n || n > MAX_STAGES || !info->pStages)
|
||||
return;
|
||||
struct stage_code code[MAX_STAGES];
|
||||
bool uses[MAX_STAGES];
|
||||
bool any = false;
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
find_stage_code(dev, &info->pStages[i], &code[i]);
|
||||
uses[i] = code[i].words && spirv_uses_distances(code[i].words, code[i].count);
|
||||
any |= uses[i];
|
||||
}
|
||||
if (!any)
|
||||
return;
|
||||
|
||||
int producer = stage_index(info, VK_SHADER_STAGE_GEOMETRY_BIT);
|
||||
if (producer < 0)
|
||||
producer = stage_index(info, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT);
|
||||
if (producer < 0)
|
||||
producer = stage_index(info, VK_SHADER_STAGE_VERTEX_BIT);
|
||||
int consumer = stage_index(info, VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||
bool emulate = (dev->emulated & GAP_CLIP_DISTANCE) && producer >= 0 && consumer >= 0 && uses[producer] &&
|
||||
code[consumer].words;
|
||||
for (uint32_t i = 0; i < n && emulate; i++) {
|
||||
if ((int)i != producer && (int)i != consumer && uses[i])
|
||||
emulate = false;
|
||||
}
|
||||
if (emulate) {
|
||||
struct clip_info produced, consumed;
|
||||
spirv_clip_info(code[producer].words, code[producer].count, CLIP_PRODUCER, &produced);
|
||||
spirv_clip_info(code[consumer].words, code[consumer].count, CLIP_CONSUMER, &consumed);
|
||||
uint32_t location = produced.locations_used > consumed.locations_used ? produced.locations_used
|
||||
: consumed.locations_used;
|
||||
emulate = produced.has_clip && location != UINT32_MAX &&
|
||||
location + produced.clip_count <= dev->max_varying_locations &&
|
||||
spirv_emulate_clip(code[producer].words, code[producer].count, CLIP_PRODUCER, location,
|
||||
produced.clip_count, &rw->code[producer]) &&
|
||||
spirv_emulate_clip(code[consumer].words, code[consumer].count, CLIP_CONSUMER, location,
|
||||
produced.clip_count, &rw->code[consumer]);
|
||||
if (!emulate) {
|
||||
free(rw->code[producer].words);
|
||||
free(rw->code[consumer].words);
|
||||
rw->code[producer] = rw->code[consumer] = (struct spirv_code){0};
|
||||
}
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
if (!rw->code[i].words && uses[i] && !code[i].stripped_module &&
|
||||
!spirv_strip_distances(code[i].words, code[i].count, &rw->code[i])) {
|
||||
layer_log("could not take the clip and cull distances out of a pipeline's shader");
|
||||
}
|
||||
if (rw->code[i].words)
|
||||
use_code(dev, info, rw, &code[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
static void rewrite_pipeline(struct device *dev, VkGraphicsPipelineCreateInfo *info, struct pipeline_rewrite *rw)
|
||||
{
|
||||
if ((dev->emulated & GAP_FILL_MODE) && info->pRasterizationState &&
|
||||
info->pRasterizationState->polygonMode != VK_POLYGON_MODE_FILL) {
|
||||
rw->rasterization = *info->pRasterizationState;
|
||||
rw->rasterization.polygonMode = VK_POLYGON_MODE_FILL;
|
||||
info->pRasterizationState = &rw->rasterization;
|
||||
}
|
||||
/* No VK_EXT_depth_clip_enable: depth clipping off is the nearest the driver has as depth clamp. */
|
||||
if ((dev->emulated & GAP_DEPTH_CLIP) && info->pRasterizationState) {
|
||||
VkBaseOutStructure *prev = (VkBaseOutStructure *)info->pRasterizationState;
|
||||
while (prev->pNext && prev->pNext->sType != VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_DEPTH_CLIP_STATE_CREATE_INFO_EXT)
|
||||
prev = prev->pNext;
|
||||
VkBaseOutStructure *node = prev->pNext;
|
||||
if (node) {
|
||||
bool clip = ((VkPipelineRasterizationDepthClipStateCreateInfoEXT *)node)->depthClipEnable;
|
||||
if (info->pRasterizationState != &rw->rasterization) {
|
||||
rw->rasterization = *info->pRasterizationState;
|
||||
if (prev == (VkBaseOutStructure *)info->pRasterizationState)
|
||||
prev = (VkBaseOutStructure *)&rw->rasterization;
|
||||
info->pRasterizationState = &rw->rasterization;
|
||||
}
|
||||
if (!clip)
|
||||
rw->rasterization.depthClampEnable = VK_TRUE;
|
||||
if (prev != (VkBaseOutStructure *)&rw->rasterization) {
|
||||
rw->clip_prev = prev;
|
||||
rw->clip_node = node;
|
||||
}
|
||||
prev->pNext = node->pNext;
|
||||
}
|
||||
}
|
||||
if ((dev->emulated & GAP_MULTI_VIEWPORT) && info->pViewportState &&
|
||||
(info->pViewportState->viewportCount > 1 || info->pViewportState->scissorCount > 1)) {
|
||||
rw->viewport = *info->pViewportState;
|
||||
if (rw->viewport.viewportCount > 1)
|
||||
rw->viewport.viewportCount = 1;
|
||||
if (rw->viewport.scissorCount > 1)
|
||||
rw->viewport.scissorCount = 1;
|
||||
info->pViewportState = &rw->viewport;
|
||||
}
|
||||
if (dev->emulated & GAPS_SHADER)
|
||||
rewrite_shaders(dev, info, rw);
|
||||
}
|
||||
|
||||
static void undo_rewrite(struct device *dev, struct pipeline_rewrite *rw)
|
||||
{
|
||||
if (rw->clip_prev)
|
||||
rw->clip_prev->pNext = rw->clip_node;
|
||||
for (uint32_t i = 0; i < MAX_STAGES; i++) {
|
||||
if (rw->in_place[i]) {
|
||||
rw->in_place[i]->pCode = rw->in_place_code[i];
|
||||
rw->in_place[i]->codeSize = rw->in_place_size[i];
|
||||
}
|
||||
if (rw->temporary[i] != VK_NULL_HANDLE)
|
||||
dev->DestroyShaderModule(dev->handle, rw->temporary[i], NULL);
|
||||
free(rw->code[i].words);
|
||||
}
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult VKAPI_CALL CreateGraphicsPipelines(VkDevice device, VkPipelineCache cache, uint32_t count,
|
||||
const VkGraphicsPipelineCreateInfo *infos,
|
||||
const VkAllocationCallbacks *allocator, VkPipeline *pipelines)
|
||||
{
|
||||
struct device *dev = device_from_handle(device);
|
||||
VkGraphicsPipelineCreateInfo *copies = malloc(count * sizeof(*copies));
|
||||
struct pipeline_rewrite *rewrites = calloc(count, sizeof(*rewrites));
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
copies[i] = infos[i];
|
||||
rewrite_pipeline(dev, &copies[i], &rewrites[i]);
|
||||
}
|
||||
VkResult result = dev->CreateGraphicsPipelines(device, cache, count, copies, allocator, pipelines);
|
||||
for (uint32_t i = 0; i < count; i++)
|
||||
undo_rewrite(dev, &rewrites[i]);
|
||||
free(rewrites);
|
||||
free(copies);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* The driver has one viewport and one scissor; anything past the first is left out. */
|
||||
VKAPI_ATTR void VKAPI_CALL CmdSetViewport(VkCommandBuffer cmd, uint32_t first, uint32_t count,
|
||||
const VkViewport *viewports)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
if (first == 0 && count > 0)
|
||||
dev->CmdSetViewport(cmd, 0, 1, viewports);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdSetScissor(VkCommandBuffer cmd, uint32_t first, uint32_t count, const VkRect2D *scissors)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
if (first == 0 && count > 0)
|
||||
dev->CmdSetScissor(cmd, 0, 1, scissors);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdSetViewportWithCount(VkCommandBuffer cmd, uint32_t count, const VkViewport *viewports)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
dev->CmdSetViewportWithCount(cmd, count > 1 ? 1 : count, viewports);
|
||||
}
|
||||
|
||||
VKAPI_ATTR void VKAPI_CALL CmdSetScissorWithCount(VkCommandBuffer cmd, uint32_t count, const VkRect2D *scissors)
|
||||
{
|
||||
struct device *dev = device_from_handle(cmd);
|
||||
dev->CmdSetScissorWithCount(cmd, count > 1 ? 1 : count, scissors);
|
||||
}
|
||||
@@ -0,0 +1,833 @@
|
||||
/*
|
||||
* Clip and cull distances out of SPIR-V, for a GPU without either.
|
||||
*
|
||||
* Stripping: every ClipDistance or CullDistance variable becomes a Private one, so the shader still
|
||||
* writes and reads it but the rasterizer never sees it. Members of a gl_PerVertex block stay
|
||||
* declared (as glslang declares them unused anyway) and their accesses move to a Private stand-in.
|
||||
* Emulating: the last stage before rasterization writes its clip distances to a free varying
|
||||
* location instead, and the fragment shader reads them there and discards when one is negative.
|
||||
*/
|
||||
#include "mali_compat.h"
|
||||
|
||||
#include <spirv/unified1/spirv.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define OPCODE(word) ((word) & 0xffffu)
|
||||
#define LENGTH(word) ((word) >> 16)
|
||||
#define VERSION_1_4 0x00010400u
|
||||
|
||||
struct words {
|
||||
uint32_t *data;
|
||||
size_t count;
|
||||
size_t capacity;
|
||||
};
|
||||
|
||||
static void push(struct words *words, uint32_t word)
|
||||
{
|
||||
if (words->count == words->capacity) {
|
||||
words->capacity = words->capacity ? words->capacity * 2 : 64;
|
||||
words->data = realloc(words->data, words->capacity * sizeof(uint32_t));
|
||||
}
|
||||
words->data[words->count++] = word;
|
||||
}
|
||||
|
||||
static void emit(struct words *words, SpvOp op, size_t count, const uint32_t *operands)
|
||||
{
|
||||
push(words, (uint32_t)(count + 1) << 16 | op);
|
||||
for (size_t i = 0; i < count; i++)
|
||||
push(words, operands[i]);
|
||||
}
|
||||
|
||||
#define EMIT(words, op, ...) \
|
||||
do { \
|
||||
const uint32_t operands_[] = {__VA_ARGS__}; \
|
||||
emit(words, op, sizeof(operands_) / sizeof(operands_[0]), operands_); \
|
||||
} while (0)
|
||||
|
||||
enum place { BEFORE, INSTEAD, AFTER };
|
||||
|
||||
struct edit {
|
||||
size_t offset;
|
||||
enum place place;
|
||||
struct words words;
|
||||
};
|
||||
|
||||
struct new_type {
|
||||
SpvOp op;
|
||||
uint32_t operands[3];
|
||||
uint32_t count;
|
||||
uint32_t id;
|
||||
};
|
||||
|
||||
struct module {
|
||||
uint32_t *w;
|
||||
size_t count;
|
||||
uint32_t version;
|
||||
uint32_t original_bound;
|
||||
uint32_t bound;
|
||||
size_t *def;
|
||||
size_t first_declaration;
|
||||
size_t first_function;
|
||||
struct edit *edits;
|
||||
size_t edit_count;
|
||||
struct new_type *new_types;
|
||||
size_t new_type_count;
|
||||
struct words globals;
|
||||
uint8_t *private_ids;
|
||||
uint32_t *interface_add;
|
||||
size_t interface_add_count;
|
||||
uint8_t *interface_remove;
|
||||
bool failed;
|
||||
};
|
||||
|
||||
static bool declares_result_first(uint32_t op)
|
||||
{
|
||||
return (op >= SpvOpTypeVoid && op <= SpvOpTypePipe) || op == SpvOpLabel || op == SpvOpExtInstImport ||
|
||||
op == SpvOpString;
|
||||
}
|
||||
|
||||
static bool declares_result_second(uint32_t op)
|
||||
{
|
||||
switch (op) {
|
||||
case SpvOpConstantTrue:
|
||||
case SpvOpConstantFalse:
|
||||
case SpvOpConstant:
|
||||
case SpvOpConstantComposite:
|
||||
case SpvOpConstantNull:
|
||||
case SpvOpSpecConstantTrue:
|
||||
case SpvOpSpecConstantFalse:
|
||||
case SpvOpSpecConstant:
|
||||
case SpvOpSpecConstantComposite:
|
||||
case SpvOpSpecConstantOp:
|
||||
case SpvOpUndef:
|
||||
case SpvOpVariable:
|
||||
case SpvOpFunction:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static bool is_declaration(uint32_t op)
|
||||
{
|
||||
return (op >= SpvOpTypeVoid && op <= SpvOpTypeForwardPointer) || declares_result_second(op);
|
||||
}
|
||||
|
||||
static void unload(struct module *m)
|
||||
{
|
||||
free(m->w);
|
||||
free(m->def);
|
||||
for (size_t i = 0; i < m->edit_count; i++)
|
||||
free(m->edits[i].words.data);
|
||||
free(m->edits);
|
||||
free(m->new_types);
|
||||
free(m->globals.data);
|
||||
free(m->private_ids);
|
||||
free(m->interface_add);
|
||||
free(m->interface_remove);
|
||||
}
|
||||
|
||||
static bool load(struct module *m, const uint32_t *words, size_t count)
|
||||
{
|
||||
memset(m, 0, sizeof(*m));
|
||||
if (count < 5 || words[0] != SpvMagicNumber || words[3] == 0 || words[3] > (1u << 22))
|
||||
return false;
|
||||
m->w = malloc(count * sizeof(uint32_t));
|
||||
memcpy(m->w, words, count * sizeof(uint32_t));
|
||||
m->count = count;
|
||||
m->version = words[1];
|
||||
m->original_bound = m->bound = words[3];
|
||||
m->def = calloc(m->bound, sizeof(size_t));
|
||||
/* Room for every id the rewrite can add. */
|
||||
m->private_ids = calloc(m->bound + 4096, 1);
|
||||
m->interface_remove = calloc(m->bound, 1);
|
||||
for (size_t i = 5; i < count;) {
|
||||
uint32_t length = LENGTH(m->w[i]);
|
||||
uint32_t op = OPCODE(m->w[i]);
|
||||
if (length == 0 || i + length > count) {
|
||||
unload(m);
|
||||
return false;
|
||||
}
|
||||
uint32_t result = 0;
|
||||
if (declares_result_first(op) && length > 1)
|
||||
result = m->w[i + 1];
|
||||
else if (declares_result_second(op) && length > 2)
|
||||
result = m->w[i + 2];
|
||||
if (result && result < m->bound)
|
||||
m->def[result] = i;
|
||||
if (!m->first_declaration && is_declaration(op))
|
||||
m->first_declaration = i;
|
||||
if (!m->first_function && op == SpvOpFunction)
|
||||
m->first_function = i;
|
||||
i += length;
|
||||
}
|
||||
if (!m->first_declaration || !m->first_function) {
|
||||
unload(m);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static struct words *edit(struct module *m, size_t offset, enum place place)
|
||||
{
|
||||
m->edits = realloc(m->edits, (m->edit_count + 1) * sizeof(*m->edits));
|
||||
struct edit *e = &m->edits[m->edit_count++];
|
||||
*e = (struct edit){.offset = offset, .place = place};
|
||||
return &e->words;
|
||||
}
|
||||
|
||||
static uint32_t new_id(struct module *m)
|
||||
{
|
||||
return m->bound++;
|
||||
}
|
||||
|
||||
static bool is_private(const struct module *m, uint32_t id)
|
||||
{
|
||||
return id < m->original_bound + 4096 && m->private_ids[id];
|
||||
}
|
||||
|
||||
static void mark_private(struct module *m, uint32_t id)
|
||||
{
|
||||
if (id < m->original_bound + 4096)
|
||||
m->private_ids[id] = 1;
|
||||
else
|
||||
m->failed = true;
|
||||
}
|
||||
|
||||
/* The defining instruction of an id from the original module, or 0. */
|
||||
static size_t definition(const struct module *m, uint32_t id)
|
||||
{
|
||||
return id < m->original_bound ? m->def[id] : 0;
|
||||
}
|
||||
|
||||
static uint32_t find_type(struct module *m, SpvOp op, const uint32_t *operands, uint32_t count)
|
||||
{
|
||||
for (size_t i = 5; i < m->first_function; i += LENGTH(m->w[i])) {
|
||||
if (OPCODE(m->w[i]) == op && LENGTH(m->w[i]) == count + 2 &&
|
||||
!memcmp(&m->w[i + 2], operands, count * sizeof(uint32_t)))
|
||||
return m->w[i + 1];
|
||||
}
|
||||
for (size_t i = 0; i < m->new_type_count; i++) {
|
||||
struct new_type *t = &m->new_types[i];
|
||||
if (t->op == op && t->count == count && !memcmp(t->operands, operands, count * sizeof(uint32_t)))
|
||||
return t->id;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t add_type(struct module *m, struct words *where, SpvOp op, const uint32_t *operands, uint32_t count)
|
||||
{
|
||||
uint32_t id = new_id(m);
|
||||
push(where, (count + 2) << 16 | op);
|
||||
push(where, id);
|
||||
for (uint32_t i = 0; i < count; i++)
|
||||
push(where, operands[i]);
|
||||
m->new_types = realloc(m->new_types, (m->new_type_count + 1) * sizeof(*m->new_types));
|
||||
struct new_type *t = &m->new_types[m->new_type_count++];
|
||||
*t = (struct new_type){.op = op, .count = count, .id = id};
|
||||
memcpy(t->operands, operands, count * sizeof(uint32_t));
|
||||
return id;
|
||||
}
|
||||
|
||||
/* Right after its pointee, so a global variable declared before the end of the types can use it. */
|
||||
static uint32_t pointer_type(struct module *m, uint32_t storage, uint32_t pointee)
|
||||
{
|
||||
uint32_t operands[2] = {storage, pointee};
|
||||
uint32_t id = find_type(m, SpvOpTypePointer, operands, 2);
|
||||
if (id)
|
||||
return id;
|
||||
size_t pointee_def = definition(m, pointee);
|
||||
struct words *where = pointee_def ? edit(m, pointee_def, AFTER) : &m->globals;
|
||||
return add_type(m, where, SpvOpTypePointer, operands, 2);
|
||||
}
|
||||
|
||||
static uint32_t scalar_type(struct module *m, SpvOp op, const uint32_t *operands, uint32_t count)
|
||||
{
|
||||
uint32_t id = find_type(m, op, operands, count);
|
||||
return id ? id : add_type(m, &m->globals, op, operands, count);
|
||||
}
|
||||
|
||||
static uint32_t float_type(struct module *m)
|
||||
{
|
||||
const uint32_t width = 32;
|
||||
return scalar_type(m, SpvOpTypeFloat, &width, 1);
|
||||
}
|
||||
|
||||
static uint32_t uint_type(struct module *m)
|
||||
{
|
||||
const uint32_t operands[2] = {32, 0};
|
||||
return scalar_type(m, SpvOpTypeInt, operands, 2);
|
||||
}
|
||||
|
||||
static uint32_t bool_type(struct module *m)
|
||||
{
|
||||
return scalar_type(m, SpvOpTypeBool, NULL, 0);
|
||||
}
|
||||
|
||||
static uint32_t constant(struct module *m, uint32_t type, uint32_t value)
|
||||
{
|
||||
for (size_t i = 5; i < m->first_function; i += LENGTH(m->w[i])) {
|
||||
if (OPCODE(m->w[i]) == SpvOpConstant && LENGTH(m->w[i]) == 4 && m->w[i + 1] == type && m->w[i + 3] == value)
|
||||
return m->w[i + 2];
|
||||
}
|
||||
uint32_t id = new_id(m);
|
||||
EMIT(&m->globals, SpvOpConstant, type, id, value);
|
||||
return id;
|
||||
}
|
||||
|
||||
static bool constant_value(const struct module *m, uint32_t id, uint32_t *value)
|
||||
{
|
||||
size_t d = definition(m, id);
|
||||
if (!d || OPCODE(m->w[d]) != SpvOpConstant || LENGTH(m->w[d]) != 4)
|
||||
return false;
|
||||
*value = m->w[d + 3];
|
||||
return true;
|
||||
}
|
||||
|
||||
/* What a pointer type points to, and where. */
|
||||
static bool pointer_parts(const struct module *m, uint32_t pointer, uint32_t *storage, uint32_t *pointee)
|
||||
{
|
||||
size_t d = definition(m, pointer);
|
||||
if (!d || OPCODE(m->w[d]) != SpvOpTypePointer)
|
||||
return false;
|
||||
*storage = m->w[d + 2];
|
||||
*pointee = m->w[d + 3];
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool is_float32(const struct module *m, uint32_t type)
|
||||
{
|
||||
size_t d = definition(m, type);
|
||||
return d && OPCODE(m->w[d]) == SpvOpTypeFloat && m->w[d + 2] == 32;
|
||||
}
|
||||
|
||||
/* Varying locations a type takes up. */
|
||||
static uint32_t location_span(const struct module *m, uint32_t type, int depth)
|
||||
{
|
||||
size_t d = definition(m, type);
|
||||
if (!d || depth > 8)
|
||||
return 1;
|
||||
uint32_t value;
|
||||
switch (OPCODE(m->w[d])) {
|
||||
case SpvOpTypeArray:
|
||||
return constant_value(m, m->w[d + 3], &value) ? value * location_span(m, m->w[d + 2], depth + 1) : 1;
|
||||
case SpvOpTypeMatrix:
|
||||
return m->w[d + 3] * location_span(m, m->w[d + 2], depth + 1);
|
||||
case SpvOpTypeStruct: {
|
||||
uint32_t span = 0;
|
||||
for (uint32_t i = 2; i < LENGTH(m->w[d]); i++)
|
||||
span += location_span(m, m->w[d + i], depth + 1);
|
||||
return span;
|
||||
}
|
||||
case SpvOpTypeVector: {
|
||||
size_t component = definition(m, m->w[d + 2]);
|
||||
bool wide = component && m->w[component + 2] == 64;
|
||||
return wide && m->w[d + 3] > 2 ? 2 : 1;
|
||||
}
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
struct distance_var {
|
||||
uint32_t id;
|
||||
uint32_t built_in;
|
||||
uint32_t storage;
|
||||
size_t decoration;
|
||||
};
|
||||
|
||||
struct distance_member {
|
||||
uint32_t structure;
|
||||
uint32_t member;
|
||||
};
|
||||
|
||||
struct analysis {
|
||||
struct distance_var vars[8];
|
||||
uint32_t var_count;
|
||||
struct distance_member members[8];
|
||||
uint32_t member_count;
|
||||
bool member_locations;
|
||||
};
|
||||
|
||||
static bool is_distance(uint32_t built_in)
|
||||
{
|
||||
return built_in == SpvBuiltInClipDistance || built_in == SpvBuiltInCullDistance;
|
||||
}
|
||||
|
||||
static void analyze(struct module *m, struct analysis *a)
|
||||
{
|
||||
memset(a, 0, sizeof(*a));
|
||||
for (size_t i = 5; i < m->first_declaration; i += LENGTH(m->w[i])) {
|
||||
uint32_t op = OPCODE(m->w[i]);
|
||||
if (op == SpvOpDecorate && LENGTH(m->w[i]) == 4 && m->w[i + 2] == SpvDecorationBuiltIn &&
|
||||
is_distance(m->w[i + 3])) {
|
||||
uint32_t storage, pointee;
|
||||
size_t d = definition(m, m->w[i + 1]);
|
||||
if (a->var_count < 8 && d && OPCODE(m->w[d]) == SpvOpVariable &&
|
||||
pointer_parts(m, m->w[d + 1], &storage, &pointee)) {
|
||||
a->vars[a->var_count++] =
|
||||
(struct distance_var){.id = m->w[i + 1], .built_in = m->w[i + 3], .storage = storage, .decoration = i};
|
||||
} else {
|
||||
m->failed = true;
|
||||
}
|
||||
} else if (op == SpvOpMemberDecorate && LENGTH(m->w[i]) == 5 && m->w[i + 3] == SpvDecorationBuiltIn &&
|
||||
is_distance(m->w[i + 4])) {
|
||||
if (a->member_count < 8)
|
||||
a->members[a->member_count++] = (struct distance_member){m->w[i + 1], m->w[i + 2]};
|
||||
else
|
||||
m->failed = true;
|
||||
} else if (op == SpvOpMemberDecorate && LENGTH(m->w[i]) >= 5 && m->w[i + 3] == SpvDecorationLocation) {
|
||||
a->member_locations = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool uses_distances(const uint32_t *words, size_t count)
|
||||
{
|
||||
for (size_t i = 5; i + 1 < count && LENGTH(words[i]); i += LENGTH(words[i])) {
|
||||
uint32_t op = OPCODE(words[i]);
|
||||
if (op == SpvOpCapability && (words[i + 1] == SpvCapabilityClipDistance || words[i + 1] == SpvCapabilityCullDistance))
|
||||
return true;
|
||||
if (op != SpvOpCapability && op != SpvOpExtension && op != SpvOpExtInstImport && op != SpvOpMemoryModel)
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool spirv_uses_distances(const uint32_t *words, size_t count)
|
||||
{
|
||||
return count > 5 && words[0] == SpvMagicNumber && uses_distances(words, count);
|
||||
}
|
||||
|
||||
static void remove_capabilities(struct module *m)
|
||||
{
|
||||
for (size_t i = 5; i < m->first_declaration; i += LENGTH(m->w[i])) {
|
||||
if (OPCODE(m->w[i]) == SpvOpCapability &&
|
||||
(m->w[i + 1] == SpvCapabilityClipDistance || m->w[i + 1] == SpvCapabilityCullDistance))
|
||||
edit(m, i, INSTEAD);
|
||||
}
|
||||
}
|
||||
|
||||
static void add_to_interface(struct module *m, uint32_t id)
|
||||
{
|
||||
m->interface_add = realloc(m->interface_add, (m->interface_add_count + 1) * sizeof(uint32_t));
|
||||
m->interface_add[m->interface_add_count++] = id;
|
||||
}
|
||||
|
||||
/* A standalone distance variable becomes Private, and its decorations go. */
|
||||
static void privatize(struct module *m, const struct distance_var *var)
|
||||
{
|
||||
size_t d = definition(m, var->id);
|
||||
uint32_t storage, pointee;
|
||||
pointer_parts(m, m->w[d + 1], &storage, &pointee);
|
||||
m->w[d + 1] = pointer_type(m, SpvStorageClassPrivate, pointee);
|
||||
m->w[d + 3] = SpvStorageClassPrivate;
|
||||
mark_private(m, var->id);
|
||||
/* Before SPIR-V 1.4 an entry point lists only its inputs and outputs. */
|
||||
if (m->version < VERSION_1_4)
|
||||
m->interface_remove[var->id] = 1;
|
||||
for (size_t i = 5; i < m->first_declaration; i += LENGTH(m->w[i])) {
|
||||
if (OPCODE(m->w[i]) == SpvOpDecorate && m->w[i + 1] == var->id)
|
||||
edit(m, i, INSTEAD);
|
||||
}
|
||||
}
|
||||
|
||||
struct block_var {
|
||||
uint32_t id;
|
||||
uint32_t structure;
|
||||
bool arrayed;
|
||||
uint32_t array_length;
|
||||
};
|
||||
|
||||
struct stand_in {
|
||||
uint32_t block;
|
||||
uint32_t member;
|
||||
uint32_t variable;
|
||||
};
|
||||
|
||||
/* Every variable of a block type with a distance member, and a Private stand-in per member. */
|
||||
static uint32_t make_stand_ins(struct module *m, const struct analysis *a, struct stand_in *stand_ins, uint32_t max)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
for (size_t i = m->first_declaration; i < m->first_function; i += LENGTH(m->w[i])) {
|
||||
if (OPCODE(m->w[i]) != SpvOpVariable)
|
||||
continue;
|
||||
uint32_t storage, pointee;
|
||||
if (!pointer_parts(m, m->w[i + 1], &storage, &pointee))
|
||||
continue;
|
||||
struct block_var var = {.id = m->w[i + 2], .structure = pointee};
|
||||
size_t d = definition(m, pointee);
|
||||
if (d && OPCODE(m->w[d]) == SpvOpTypeArray) {
|
||||
var.arrayed = true;
|
||||
var.structure = m->w[d + 2];
|
||||
var.array_length = m->w[d + 3];
|
||||
}
|
||||
for (uint32_t k = 0; k < a->member_count; k++) {
|
||||
if (a->members[k].structure != var.structure)
|
||||
continue;
|
||||
if (count == max) {
|
||||
m->failed = true;
|
||||
return count;
|
||||
}
|
||||
size_t s = definition(m, var.structure);
|
||||
uint32_t member_type = m->w[s + 2 + a->members[k].member];
|
||||
uint32_t type = member_type;
|
||||
if (var.arrayed) {
|
||||
const uint32_t operands[2] = {member_type, var.array_length};
|
||||
type = add_type(m, &m->globals, SpvOpTypeArray, operands, 2);
|
||||
}
|
||||
uint32_t variable = new_id(m);
|
||||
EMIT(&m->globals, SpvOpVariable, pointer_type(m, SpvStorageClassPrivate, type), variable,
|
||||
SpvStorageClassPrivate);
|
||||
if (m->version >= VERSION_1_4)
|
||||
add_to_interface(m, variable);
|
||||
stand_ins[count++] = (struct stand_in){var.id, a->members[k].member, variable};
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
static const struct stand_in *find_stand_in(const struct module *m, const struct stand_in *stand_ins, uint32_t count,
|
||||
uint32_t base, uint32_t member_index)
|
||||
{
|
||||
uint32_t member;
|
||||
if (!constant_value(m, member_index, &member))
|
||||
return NULL;
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
if (stand_ins[i].block == base && stand_ins[i].member == member)
|
||||
return &stand_ins[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static bool is_arrayed_block(const struct module *m, uint32_t variable)
|
||||
{
|
||||
size_t d = definition(m, variable);
|
||||
uint32_t storage, pointee;
|
||||
if (!d || !pointer_parts(m, m->w[d + 1], &storage, &pointee))
|
||||
return false;
|
||||
size_t p = definition(m, pointee);
|
||||
return p && OPCODE(m->w[p]) == SpvOpTypeArray;
|
||||
}
|
||||
|
||||
/*
|
||||
* One pass over the function bodies: accesses to a distance member go to its stand-in, and every
|
||||
* pointer derived from a Private one becomes Private too. A Private pointer handed to a function
|
||||
* or merged through a phi would need its types changed beyond this, so that fails the rewrite.
|
||||
*/
|
||||
static void retarget_accesses(struct module *m, const struct stand_in *stand_ins, uint32_t stand_in_count)
|
||||
{
|
||||
for (size_t i = m->first_function; i < m->count; i += LENGTH(m->w[i])) {
|
||||
uint32_t op = OPCODE(m->w[i]);
|
||||
uint32_t length = LENGTH(m->w[i]);
|
||||
switch (op) {
|
||||
case SpvOpAccessChain:
|
||||
case SpvOpInBoundsAccessChain: {
|
||||
uint32_t base = m->w[i + 3];
|
||||
bool arrayed = is_arrayed_block(m, base);
|
||||
uint32_t member_position = arrayed ? 5 : 4;
|
||||
const struct stand_in *stand_in =
|
||||
length > member_position
|
||||
? find_stand_in(m, stand_ins, stand_in_count, base, m->w[i + member_position])
|
||||
: NULL;
|
||||
uint32_t storage, pointee;
|
||||
if (stand_in) {
|
||||
if (!pointer_parts(m, m->w[i + 1], &storage, &pointee)) {
|
||||
m->failed = true;
|
||||
break;
|
||||
}
|
||||
/* Before edit(): a new pointer type is an edit too, and would move the chain's buffer. */
|
||||
uint32_t type = pointer_type(m, SpvStorageClassPrivate, pointee);
|
||||
struct words *chain = edit(m, i, INSTEAD);
|
||||
push(chain, (length - 1) << 16 | op);
|
||||
push(chain, type);
|
||||
push(chain, m->w[i + 2]);
|
||||
push(chain, stand_in->variable);
|
||||
for (uint32_t k = 4; k < length; k++) {
|
||||
if (k != member_position)
|
||||
push(chain, m->w[i + k]);
|
||||
}
|
||||
mark_private(m, m->w[i + 2]);
|
||||
} else if (is_private(m, base)) {
|
||||
if (!pointer_parts(m, m->w[i + 1], &storage, &pointee)) {
|
||||
m->failed = true;
|
||||
break;
|
||||
}
|
||||
m->w[i + 1] = pointer_type(m, SpvStorageClassPrivate, pointee);
|
||||
mark_private(m, m->w[i + 2]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case SpvOpPtrAccessChain:
|
||||
case SpvOpCopyObject:
|
||||
if (is_private(m, m->w[i + 3])) {
|
||||
uint32_t storage, pointee;
|
||||
if (!pointer_parts(m, m->w[i + 1], &storage, &pointee)) {
|
||||
m->failed = true;
|
||||
break;
|
||||
}
|
||||
m->w[i + 1] = pointer_type(m, SpvStorageClassPrivate, pointee);
|
||||
mark_private(m, m->w[i + 2]);
|
||||
}
|
||||
break;
|
||||
case SpvOpFunctionCall:
|
||||
for (uint32_t k = 4; k < length; k++) {
|
||||
if (is_private(m, m->w[i + k]))
|
||||
m->failed = true;
|
||||
}
|
||||
break;
|
||||
case SpvOpPhi:
|
||||
case SpvOpSelect:
|
||||
for (uint32_t k = 3; k < length; k++) {
|
||||
if (is_private(m, m->w[i + k]))
|
||||
m->failed = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t string_words(const uint32_t *words, uint32_t available)
|
||||
{
|
||||
for (uint32_t i = 0; i < available; i++) {
|
||||
uint32_t word = words[i];
|
||||
if (!(word & 0xff) || !(word & 0xff00) || !(word & 0xff0000) || !(word & 0xff000000))
|
||||
return i + 1;
|
||||
}
|
||||
return available;
|
||||
}
|
||||
|
||||
static void rewrite_interfaces(struct module *m)
|
||||
{
|
||||
for (size_t i = 5; i < m->first_declaration; i += LENGTH(m->w[i])) {
|
||||
if (OPCODE(m->w[i]) != SpvOpEntryPoint)
|
||||
continue;
|
||||
uint32_t length = LENGTH(m->w[i]);
|
||||
uint32_t first = 3 + string_words(&m->w[i + 3], length - 3);
|
||||
struct words *entry = edit(m, i, INSTEAD);
|
||||
push(entry, 0);
|
||||
for (uint32_t k = 1; k < first; k++)
|
||||
push(entry, m->w[i + k]);
|
||||
for (uint32_t k = first; k < length; k++) {
|
||||
uint32_t id = m->w[i + k];
|
||||
if (id >= m->original_bound || !m->interface_remove[id])
|
||||
push(entry, id);
|
||||
}
|
||||
for (size_t k = 0; k < m->interface_add_count; k++)
|
||||
push(entry, m->interface_add[k]);
|
||||
entry->data[0] = (uint32_t)entry->count << 16 | SpvOpEntryPoint;
|
||||
}
|
||||
}
|
||||
|
||||
static void emit_edits(const struct module *m, struct words *out, size_t offset, enum place place, bool *replaced)
|
||||
{
|
||||
for (size_t k = 0; k < m->edit_count; k++) {
|
||||
const struct edit *e = &m->edits[k];
|
||||
if (e->offset != offset || e->place != place)
|
||||
continue;
|
||||
for (size_t j = 0; j < e->words.count; j++)
|
||||
push(out, e->words.data[j]);
|
||||
if (replaced)
|
||||
*replaced = true;
|
||||
}
|
||||
}
|
||||
|
||||
static bool finish(struct module *m, struct spirv_code *out)
|
||||
{
|
||||
if (m->failed)
|
||||
return false;
|
||||
rewrite_interfaces(m);
|
||||
struct words result = {0};
|
||||
for (int i = 0; i < 5; i++)
|
||||
push(&result, m->w[i]);
|
||||
result.data[3] = m->bound;
|
||||
for (size_t i = 5; i < m->count; i += LENGTH(m->w[i])) {
|
||||
if (i == m->first_function) {
|
||||
for (size_t j = 0; j < m->globals.count; j++)
|
||||
push(&result, m->globals.data[j]);
|
||||
}
|
||||
emit_edits(m, &result, i, BEFORE, NULL);
|
||||
bool replaced = false;
|
||||
emit_edits(m, &result, i, INSTEAD, &replaced);
|
||||
if (!replaced) {
|
||||
for (uint32_t j = 0; j < LENGTH(m->w[i]); j++)
|
||||
push(&result, m->w[i + j]);
|
||||
}
|
||||
emit_edits(m, &result, i, AFTER, NULL);
|
||||
}
|
||||
out->words = result.data;
|
||||
out->count = result.count;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void strip(struct module *m, const struct analysis *a, uint32_t keep)
|
||||
{
|
||||
for (uint32_t i = 0; i < a->var_count; i++) {
|
||||
if (a->vars[i].id != keep)
|
||||
privatize(m, &a->vars[i]);
|
||||
}
|
||||
struct stand_in stand_ins[16];
|
||||
uint32_t count = make_stand_ins(m, a, stand_ins, 16);
|
||||
retarget_accesses(m, stand_ins, count);
|
||||
remove_capabilities(m);
|
||||
}
|
||||
|
||||
bool spirv_strip_distances(const uint32_t *words, size_t count, struct spirv_code *out)
|
||||
{
|
||||
struct module m;
|
||||
if (!load(&m, words, count))
|
||||
return false;
|
||||
struct analysis a;
|
||||
analyze(&m, &a);
|
||||
strip(&m, &a, 0);
|
||||
bool done = finish(&m, out);
|
||||
unload(&m);
|
||||
return done;
|
||||
}
|
||||
|
||||
static const struct distance_var *clip_output(const struct module *m, const struct analysis *a, uint32_t *length)
|
||||
{
|
||||
for (uint32_t i = 0; i < a->var_count; i++) {
|
||||
const struct distance_var *var = &a->vars[i];
|
||||
if (var->built_in != SpvBuiltInClipDistance || var->storage != SpvStorageClassOutput)
|
||||
continue;
|
||||
uint32_t storage, pointee;
|
||||
pointer_parts(m, m->w[definition(m, var->id) + 1], &storage, &pointee);
|
||||
size_t d = definition(m, pointee);
|
||||
if (d && OPCODE(m->w[d]) == SpvOpTypeArray && is_float32(m, m->w[d + 2]) &&
|
||||
constant_value(m, m->w[d + 3], length))
|
||||
return var;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void spirv_clip_info(const uint32_t *words, size_t count, enum clip_role role, struct clip_info *info)
|
||||
{
|
||||
memset(info, 0, sizeof(*info));
|
||||
struct module m;
|
||||
if (!load(&m, words, count)) {
|
||||
info->locations_used = UINT32_MAX;
|
||||
return;
|
||||
}
|
||||
struct analysis a;
|
||||
analyze(&m, &a);
|
||||
if (role == CLIP_PRODUCER)
|
||||
info->has_clip = clip_output(&m, &a, &info->clip_count) != NULL;
|
||||
uint32_t storage_wanted = role == CLIP_PRODUCER ? SpvStorageClassOutput : SpvStorageClassInput;
|
||||
for (size_t i = 5; i < m.first_declaration; i += LENGTH(m.w[i])) {
|
||||
if (OPCODE(m.w[i]) != SpvOpDecorate || LENGTH(m.w[i]) != 4 || m.w[i + 2] != SpvDecorationLocation)
|
||||
continue;
|
||||
size_t d = definition(&m, m.w[i + 1]);
|
||||
uint32_t storage, pointee;
|
||||
if (!d || OPCODE(m.w[d]) != SpvOpVariable || !pointer_parts(&m, m.w[d + 1], &storage, &pointee) ||
|
||||
storage != storage_wanted)
|
||||
continue;
|
||||
uint32_t end = m.w[i + 3] + location_span(&m, pointee, 0);
|
||||
if (end > info->locations_used)
|
||||
info->locations_used = end;
|
||||
}
|
||||
if (a.member_locations || m.failed)
|
||||
info->locations_used = UINT32_MAX;
|
||||
unload(&m);
|
||||
}
|
||||
|
||||
/* Fragment shader: read the clip distances at `location` and discard when any is negative. */
|
||||
static void add_clip_test(struct module *m, uint32_t location, uint32_t clip_count)
|
||||
{
|
||||
uint32_t float_id = float_type(m);
|
||||
uint32_t uint_id = uint_type(m);
|
||||
uint32_t bool_id = bool_type(m);
|
||||
const uint32_t array_operands[2] = {float_id, constant(m, uint_id, clip_count)};
|
||||
uint32_t array_id = add_type(m, &m->globals, SpvOpTypeArray, array_operands, 2);
|
||||
uint32_t input_pointer = pointer_type(m, SpvStorageClassInput, array_id);
|
||||
uint32_t element_pointer = pointer_type(m, SpvStorageClassInput, float_id);
|
||||
uint32_t zero = constant(m, float_id, 0);
|
||||
uint32_t indices[8];
|
||||
for (uint32_t i = 0; i < clip_count; i++)
|
||||
indices[i] = constant(m, uint_id, i);
|
||||
uint32_t input = new_id(m);
|
||||
EMIT(&m->globals, SpvOpVariable, input_pointer, input, SpvStorageClassInput);
|
||||
EMIT(edit(m, m->first_declaration, BEFORE), SpvOpDecorate, input, SpvDecorationLocation, location);
|
||||
add_to_interface(m, input);
|
||||
|
||||
for (size_t i = 5; i < m->first_declaration; i += LENGTH(m->w[i])) {
|
||||
if (OPCODE(m->w[i]) != SpvOpEntryPoint || m->w[i + 1] != SpvExecutionModelFragment)
|
||||
continue;
|
||||
size_t function = definition(m, m->w[i + 2]);
|
||||
if (!function) {
|
||||
m->failed = true;
|
||||
return;
|
||||
}
|
||||
size_t label = function + LENGTH(m->w[function]);
|
||||
if (label >= m->count || OPCODE(m->w[label]) != SpvOpLabel) {
|
||||
m->failed = true;
|
||||
return;
|
||||
}
|
||||
/* Function variables must open the entry block, so the test goes after them. */
|
||||
size_t last = label;
|
||||
for (size_t k = label + LENGTH(m->w[label]); k < m->count; k += LENGTH(m->w[k])) {
|
||||
uint32_t op = OPCODE(m->w[k]);
|
||||
if (op != SpvOpVariable && op != SpvOpLine && op != SpvOpNoLine)
|
||||
break;
|
||||
last = k;
|
||||
}
|
||||
uint32_t original_label = m->w[label + 1];
|
||||
uint32_t entry = new_id(m), kill = new_id(m), join = new_id(m);
|
||||
EMIT(edit(m, label, INSTEAD), SpvOpLabel, entry);
|
||||
struct words *test = edit(m, last, AFTER);
|
||||
uint32_t any = 0;
|
||||
for (uint32_t c = 0; c < clip_count; c++) {
|
||||
uint32_t pointer = new_id(m), value = new_id(m), negative = new_id(m);
|
||||
EMIT(test, SpvOpAccessChain, element_pointer, pointer, input, indices[c]);
|
||||
EMIT(test, SpvOpLoad, float_id, value, pointer);
|
||||
EMIT(test, SpvOpFOrdLessThan, bool_id, negative, value, zero);
|
||||
if (any) {
|
||||
uint32_t either = new_id(m);
|
||||
EMIT(test, SpvOpLogicalOr, bool_id, either, any, negative);
|
||||
any = either;
|
||||
} else {
|
||||
any = negative;
|
||||
}
|
||||
}
|
||||
EMIT(test, SpvOpSelectionMerge, join, SpvSelectionControlMaskNone);
|
||||
EMIT(test, SpvOpBranchConditional, any, kill, join);
|
||||
EMIT(test, SpvOpLabel, kill);
|
||||
push(test, 1u << 16 | SpvOpKill);
|
||||
EMIT(test, SpvOpLabel, join);
|
||||
EMIT(test, SpvOpBranch, original_label);
|
||||
EMIT(test, SpvOpLabel, original_label);
|
||||
}
|
||||
}
|
||||
|
||||
bool spirv_emulate_clip(const uint32_t *words, size_t count, enum clip_role role, uint32_t location,
|
||||
uint32_t clip_count, struct spirv_code *out)
|
||||
{
|
||||
if (clip_count == 0 || clip_count > 8)
|
||||
return false;
|
||||
struct module m;
|
||||
if (!load(&m, words, count))
|
||||
return false;
|
||||
struct analysis a;
|
||||
analyze(&m, &a);
|
||||
uint32_t keep = 0;
|
||||
if (role == CLIP_PRODUCER) {
|
||||
uint32_t length = 0;
|
||||
const struct distance_var *clip = clip_output(&m, &a, &length);
|
||||
if (!clip || length != clip_count) {
|
||||
unload(&m);
|
||||
return false;
|
||||
}
|
||||
keep = clip->id;
|
||||
m.w[clip->decoration + 2] = SpvDecorationLocation;
|
||||
m.w[clip->decoration + 3] = location;
|
||||
}
|
||||
strip(&m, &a, keep);
|
||||
if (role == CLIP_CONSUMER)
|
||||
add_clip_test(&m, location, clip_count);
|
||||
bool done = finish(&m, out);
|
||||
unload(&m);
|
||||
return done;
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
/*
|
||||
* DROIDDECK_MALI_COMPAT_TRACE=1: the physical-device queries an application makes before
|
||||
* vkCreateDevice, logged as they pass through. Zink on Venus over an older Mali driver gave up
|
||||
* after vkGetPhysicalDeviceProperties2 without a word (its release builds print nothing there),
|
||||
* so the last query in the log is where it stopped.
|
||||
*/
|
||||
#include "mali_compat.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static uint32_t format_queries;
|
||||
|
||||
static bool format_worth_logging(uint32_t n, VkFormatFeatureFlags linear, VkFormatFeatureFlags optimal, VkFormatFeatureFlags buffer)
|
||||
{
|
||||
return n == 1 || n % 25 == 0 || (!linear && !optimal && !buffer);
|
||||
}
|
||||
|
||||
static VKAPI_ATTR VkResult VKAPI_CALL TraceEnumeratePhysicalDevices(VkInstance instance, uint32_t *count, VkPhysicalDevice *devices)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(instance);
|
||||
VkResult r = inst->EnumeratePhysicalDevices(instance, count, devices);
|
||||
layer_log("trace: vkEnumeratePhysicalDevices (%s): %d, %u device(s)", devices ? "list" : "count", (int)r, count ? *count : 0);
|
||||
return r;
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL TraceGetPhysicalDeviceMemoryProperties(VkPhysicalDevice pd, VkPhysicalDeviceMemoryProperties *p)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(pd);
|
||||
inst->GetPhysicalDeviceMemoryProperties(pd, p);
|
||||
char types[384] = "";
|
||||
size_t o = 0;
|
||||
for (uint32_t i = 0; i < p->memoryTypeCount && o + 16 < sizeof(types); i++)
|
||||
o += (size_t)snprintf(types + o, sizeof(types) - o, " %u:h%u/0x%x", i, p->memoryTypes[i].heapIndex,
|
||||
(unsigned)p->memoryTypes[i].propertyFlags);
|
||||
layer_log("trace: vkGetPhysicalDeviceMemoryProperties: %u heaps, %u types:%s", p->memoryHeapCount, p->memoryTypeCount, types);
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL TraceGetPhysicalDeviceMemoryProperties2(VkPhysicalDevice pd, VkPhysicalDeviceMemoryProperties2 *p)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(pd);
|
||||
inst->GetPhysicalDeviceMemoryProperties2(pd, p);
|
||||
char chain[160] = "";
|
||||
size_t o = 0;
|
||||
for (VkBaseOutStructure *s = p->pNext; s && o + 16 < sizeof(chain); s = s->pNext)
|
||||
o += (size_t)snprintf(chain + o, sizeof(chain) - o, " %d", (int)s->sType);
|
||||
layer_log("trace: vkGetPhysicalDeviceMemoryProperties2: %u heaps, %u types, chain:%s", p->memoryProperties.memoryHeapCount,
|
||||
p->memoryProperties.memoryTypeCount, chain);
|
||||
}
|
||||
|
||||
static void log_queue_families(const char *what, uint32_t n, const VkQueueFamilyProperties *(*at)(const void *, uint32_t), const void *arr)
|
||||
{
|
||||
char fams[256] = "";
|
||||
size_t o = 0;
|
||||
for (uint32_t i = 0; arr && i < n && o + 24 < sizeof(fams); i++) {
|
||||
const VkQueueFamilyProperties *q = at(arr, i);
|
||||
o += (size_t)snprintf(fams + o, sizeof(fams) - o, " %u:0x%x*%u", i, (unsigned)q->queueFlags, q->queueCount);
|
||||
}
|
||||
layer_log("trace: %s: %u famil%s%s", what, n, n == 1 ? "y" : "ies", fams);
|
||||
}
|
||||
|
||||
static const VkQueueFamilyProperties *qf1(const void *a, uint32_t i) { return &((const VkQueueFamilyProperties *)a)[i]; }
|
||||
static const VkQueueFamilyProperties *qf2(const void *a, uint32_t i) { return &((const VkQueueFamilyProperties2 *)a)[i].queueFamilyProperties; }
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL TraceGetPhysicalDeviceQueueFamilyProperties(VkPhysicalDevice pd, uint32_t *n, VkQueueFamilyProperties *p)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(pd);
|
||||
inst->GetPhysicalDeviceQueueFamilyProperties(pd, n, p);
|
||||
log_queue_families("vkGetPhysicalDeviceQueueFamilyProperties", n ? *n : 0, qf1, p);
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL TraceGetPhysicalDeviceQueueFamilyProperties2(VkPhysicalDevice pd, uint32_t *n, VkQueueFamilyProperties2 *p)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(pd);
|
||||
inst->GetPhysicalDeviceQueueFamilyProperties2(pd, n, p);
|
||||
log_queue_families("vkGetPhysicalDeviceQueueFamilyProperties2", n ? *n : 0, qf2, p);
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL TraceGetPhysicalDeviceFormatProperties(VkPhysicalDevice pd, VkFormat format, VkFormatProperties *p)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(pd);
|
||||
if (inst->active) GetPhysicalDeviceFormatProperties(pd, format, p);
|
||||
else inst->GetPhysicalDeviceFormatProperties(pd, format, p);
|
||||
uint32_t n = __atomic_add_fetch(&format_queries, 1, __ATOMIC_RELAXED);
|
||||
if (format_worth_logging(n, p->linearTilingFeatures, p->optimalTilingFeatures, p->bufferFeatures))
|
||||
layer_log("trace: vkGetPhysicalDeviceFormatProperties #%u: format %d linear 0x%x optimal 0x%x buffer 0x%x", n, (int)format,
|
||||
(unsigned)p->linearTilingFeatures, (unsigned)p->optimalTilingFeatures, (unsigned)p->bufferFeatures);
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL TraceGetPhysicalDeviceFormatProperties2(VkPhysicalDevice pd, VkFormat format, VkFormatProperties2 *p)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(pd);
|
||||
if (inst->active) GetPhysicalDeviceFormatProperties2(pd, format, p);
|
||||
else inst->GetPhysicalDeviceFormatProperties2(pd, format, p);
|
||||
uint32_t n = __atomic_add_fetch(&format_queries, 1, __ATOMIC_RELAXED);
|
||||
const VkFormatProperties *f = &p->formatProperties;
|
||||
if (format_worth_logging(n, f->linearTilingFeatures, f->optimalTilingFeatures, f->bufferFeatures))
|
||||
layer_log("trace: vkGetPhysicalDeviceFormatProperties2 #%u: format %d linear 0x%x optimal 0x%x buffer 0x%x", n, (int)format,
|
||||
(unsigned)f->linearTilingFeatures, (unsigned)f->optimalTilingFeatures, (unsigned)f->bufferFeatures);
|
||||
}
|
||||
|
||||
static VKAPI_ATTR VkResult VKAPI_CALL TraceGetPhysicalDeviceImageFormatProperties(VkPhysicalDevice pd, VkFormat format, VkImageType type,
|
||||
VkImageTiling tiling, VkImageUsageFlags usage,
|
||||
VkImageCreateFlags flags, VkImageFormatProperties *p)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(pd);
|
||||
VkResult r = inst->active ? GetPhysicalDeviceImageFormatProperties(pd, format, type, tiling, usage, flags, p)
|
||||
: inst->GetPhysicalDeviceImageFormatProperties(pd, format, type, tiling, usage, flags, p);
|
||||
if (r != VK_SUCCESS)
|
||||
layer_log("trace: vkGetPhysicalDeviceImageFormatProperties: format %d type %d tiling %d usage 0x%x flags 0x%x -> %d", (int)format,
|
||||
(int)type, (int)tiling, (unsigned)usage, (unsigned)flags, (int)r);
|
||||
return r;
|
||||
}
|
||||
|
||||
static VKAPI_ATTR VkResult VKAPI_CALL TraceGetPhysicalDeviceImageFormatProperties2(VkPhysicalDevice pd, const VkPhysicalDeviceImageFormatInfo2 *info,
|
||||
VkImageFormatProperties2 *p)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(pd);
|
||||
VkResult r = inst->active ? GetPhysicalDeviceImageFormatProperties2(pd, info, p) : inst->GetPhysicalDeviceImageFormatProperties2(pd, info, p);
|
||||
if (r != VK_SUCCESS)
|
||||
layer_log("trace: vkGetPhysicalDeviceImageFormatProperties2: format %d type %d tiling %d usage 0x%x flags 0x%x -> %d", (int)info->format,
|
||||
(int)info->type, (int)info->tiling, (unsigned)info->usage, (unsigned)info->flags, (int)r);
|
||||
return r;
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL TraceGetPhysicalDeviceExternalBufferProperties(VkPhysicalDevice pd, const VkPhysicalDeviceExternalBufferInfo *info,
|
||||
VkExternalBufferProperties *p)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(pd);
|
||||
inst->GetPhysicalDeviceExternalBufferProperties(pd, info, p);
|
||||
layer_log("trace: vkGetPhysicalDeviceExternalBufferProperties: handle 0x%x usage 0x%x -> features 0x%x", (unsigned)info->handleType,
|
||||
(unsigned)info->usage, (unsigned)p->externalMemoryProperties.externalMemoryFeatures);
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL TraceGetPhysicalDeviceExternalSemaphoreProperties(VkPhysicalDevice pd,
|
||||
const VkPhysicalDeviceExternalSemaphoreInfo *info,
|
||||
VkExternalSemaphoreProperties *p)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(pd);
|
||||
inst->GetPhysicalDeviceExternalSemaphoreProperties(pd, info, p);
|
||||
layer_log("trace: vkGetPhysicalDeviceExternalSemaphoreProperties: handle 0x%x -> features 0x%x", (unsigned)info->handleType,
|
||||
(unsigned)p->externalSemaphoreFeatures);
|
||||
}
|
||||
|
||||
static VKAPI_ATTR void VKAPI_CALL TraceGetPhysicalDeviceExternalFenceProperties(VkPhysicalDevice pd, const VkPhysicalDeviceExternalFenceInfo *info,
|
||||
VkExternalFenceProperties *p)
|
||||
{
|
||||
struct instance *inst = instance_from_handle(pd);
|
||||
inst->GetPhysicalDeviceExternalFenceProperties(pd, info, p);
|
||||
layer_log("trace: vkGetPhysicalDeviceExternalFenceProperties: handle 0x%x -> features 0x%x", (unsigned)info->handleType,
|
||||
(unsigned)p->externalFenceFeatures);
|
||||
}
|
||||
|
||||
void trace_extension_names(const VkExtensionProperties *ext, uint32_t n)
|
||||
{
|
||||
char line[400];
|
||||
size_t o = 0;
|
||||
line[0] = 0;
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
size_t len = strlen(ext[i].extensionName);
|
||||
if (o && o + len + 2 > sizeof(line)) {
|
||||
layer_log("trace: extensions:%s", line);
|
||||
o = 0;
|
||||
line[0] = 0;
|
||||
}
|
||||
o += (size_t)snprintf(line + o, sizeof(line) - o, " %s", ext[i].extensionName);
|
||||
}
|
||||
if (o) layer_log("trace: extensions:%s", line);
|
||||
}
|
||||
|
||||
void trace_load(struct instance *inst)
|
||||
{
|
||||
#define T_LOAD(name) inst->name = (PFN_vk##name)inst->gipa(inst->handle, "vk" #name);
|
||||
#define T_LOAD_KHR(name) \
|
||||
if (!inst->name) \
|
||||
inst->name = (PFN_vk##name)inst->gipa(inst->handle, "vk" #name "KHR");
|
||||
T_LOAD(EnumeratePhysicalDevices)
|
||||
T_LOAD(GetPhysicalDeviceMemoryProperties2)
|
||||
T_LOAD_KHR(GetPhysicalDeviceMemoryProperties2)
|
||||
T_LOAD(GetPhysicalDeviceQueueFamilyProperties)
|
||||
T_LOAD(GetPhysicalDeviceQueueFamilyProperties2)
|
||||
T_LOAD_KHR(GetPhysicalDeviceQueueFamilyProperties2)
|
||||
T_LOAD(GetPhysicalDeviceExternalBufferProperties)
|
||||
T_LOAD_KHR(GetPhysicalDeviceExternalBufferProperties)
|
||||
T_LOAD(GetPhysicalDeviceExternalSemaphoreProperties)
|
||||
T_LOAD_KHR(GetPhysicalDeviceExternalSemaphoreProperties)
|
||||
T_LOAD(GetPhysicalDeviceExternalFenceProperties)
|
||||
T_LOAD_KHR(GetPhysicalDeviceExternalFenceProperties)
|
||||
#undef T_LOAD
|
||||
#undef T_LOAD_KHR
|
||||
}
|
||||
|
||||
struct trace_hook {
|
||||
const char *name;
|
||||
PFN_vkVoidFunction function;
|
||||
size_t next_offset;
|
||||
};
|
||||
|
||||
#define TH(fn, field) {"vk" #fn, (PFN_vkVoidFunction)Trace##field, offsetof(struct instance, field)}
|
||||
|
||||
static const struct trace_hook trace_hooks[] = {
|
||||
TH(EnumeratePhysicalDevices, EnumeratePhysicalDevices),
|
||||
TH(GetPhysicalDeviceMemoryProperties, GetPhysicalDeviceMemoryProperties),
|
||||
TH(GetPhysicalDeviceMemoryProperties2, GetPhysicalDeviceMemoryProperties2),
|
||||
TH(GetPhysicalDeviceMemoryProperties2KHR, GetPhysicalDeviceMemoryProperties2),
|
||||
TH(GetPhysicalDeviceQueueFamilyProperties, GetPhysicalDeviceQueueFamilyProperties),
|
||||
TH(GetPhysicalDeviceQueueFamilyProperties2, GetPhysicalDeviceQueueFamilyProperties2),
|
||||
TH(GetPhysicalDeviceQueueFamilyProperties2KHR, GetPhysicalDeviceQueueFamilyProperties2),
|
||||
TH(GetPhysicalDeviceFormatProperties, GetPhysicalDeviceFormatProperties),
|
||||
TH(GetPhysicalDeviceFormatProperties2, GetPhysicalDeviceFormatProperties2),
|
||||
TH(GetPhysicalDeviceFormatProperties2KHR, GetPhysicalDeviceFormatProperties2),
|
||||
TH(GetPhysicalDeviceImageFormatProperties, GetPhysicalDeviceImageFormatProperties),
|
||||
TH(GetPhysicalDeviceImageFormatProperties2, GetPhysicalDeviceImageFormatProperties2),
|
||||
TH(GetPhysicalDeviceImageFormatProperties2KHR, GetPhysicalDeviceImageFormatProperties2),
|
||||
TH(GetPhysicalDeviceExternalBufferProperties, GetPhysicalDeviceExternalBufferProperties),
|
||||
TH(GetPhysicalDeviceExternalBufferPropertiesKHR, GetPhysicalDeviceExternalBufferProperties),
|
||||
TH(GetPhysicalDeviceExternalSemaphoreProperties, GetPhysicalDeviceExternalSemaphoreProperties),
|
||||
TH(GetPhysicalDeviceExternalSemaphorePropertiesKHR, GetPhysicalDeviceExternalSemaphoreProperties),
|
||||
TH(GetPhysicalDeviceExternalFenceProperties, GetPhysicalDeviceExternalFenceProperties),
|
||||
TH(GetPhysicalDeviceExternalFencePropertiesKHR, GetPhysicalDeviceExternalFenceProperties),
|
||||
};
|
||||
|
||||
PFN_vkVoidFunction trace_hook(struct instance *inst, const char *name)
|
||||
{
|
||||
for (size_t i = 0; i < sizeof(trace_hooks) / sizeof(trace_hooks[0]); i++) {
|
||||
if (strcmp(name, trace_hooks[i].name)) continue;
|
||||
void *next = *(void **)((char *)inst + trace_hooks[i].next_offset);
|
||||
return next ? trace_hooks[i].function : NULL;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
diff --git a/src/virtio/vulkan/vn_wsi.c b/src/virtio/vulkan/vn_wsi.c
|
||||
index f0b41b1..5c08312 100644
|
||||
--- a/src/virtio/vulkan/vn_wsi.c
|
||||
+++ b/src/virtio/vulkan/vn_wsi.c
|
||||
@@ -23,6 +23,33 @@
|
||||
#define DRM_FORMAT_MOD_LINEAR 0
|
||||
#endif
|
||||
|
||||
+/* MEASURE: async present costs, logged every 120 presents. */
|
||||
+static struct {
|
||||
+ uint32_t presents, fence_waits, flushes;
|
||||
+ int64_t present_sum, present_max, fence_sum, fence_max, flush_sum, flush_max;
|
||||
+} vn_measure;
|
||||
+
|
||||
+#define VN_MEASURE_ADD(name, ns) \
|
||||
+ do { \
|
||||
+ vn_measure.name##_sum += (ns); \
|
||||
+ vn_measure.name##_max = MAX2(vn_measure.name##_max, (ns)); \
|
||||
+ } while (0)
|
||||
+
|
||||
+static void
|
||||
+vn_measure_present(int64_t ns)
|
||||
+{
|
||||
+ VN_MEASURE_ADD(present, ns);
|
||||
+ if (++vn_measure.presents < 120)
|
||||
+ return;
|
||||
+ fprintf(stderr, "MEASURE vn: present thread %.2f/%.2f ms | fence wait %.2f/%.2f ms (%u) | app blocked in flush %.2f/%.2f ms (%u)\n",
|
||||
+ vn_measure.present_sum / 120 / 1e6, vn_measure.present_max / 1e6,
|
||||
+ vn_measure.fence_waits ? vn_measure.fence_sum / vn_measure.fence_waits / 1e6 : 0.0,
|
||||
+ vn_measure.fence_max / 1e6, vn_measure.fence_waits,
|
||||
+ vn_measure.flushes ? vn_measure.flush_sum / vn_measure.flushes / 1e6 : 0.0,
|
||||
+ vn_measure.flush_max / 1e6, vn_measure.flushes);
|
||||
+ memset(&vn_measure, 0, sizeof(vn_measure));
|
||||
+}
|
||||
+
|
||||
/* The common WSI support makes some assumptions about the driver.
|
||||
*
|
||||
* In wsi_device_init, it assumes VK_EXT_pci_bus_info is available. In
|
||||
@@ -331,6 +358,7 @@ vn_wsi_memory_type_index(const struct vn_device *dev,
|
||||
!(type_bits & BITFIELD_BIT(readback_type)))
|
||||
return alloc_info->memoryTypeIndex;
|
||||
|
||||
+ fprintf(stderr, "MEASURE vn: WSI readback buffer in memory type %d\n", readback_type);
|
||||
return readback_type;
|
||||
}
|
||||
|
||||
@@ -479,8 +507,11 @@ vn_wsi_fence_wait(VkQueue queue_handle)
|
||||
simple_mtx_unlock(&queue->async_present.queue_mutex);
|
||||
vn_wsi_chains_unlock(dev, queue->async_present.info, /*all=*/false);
|
||||
|
||||
+ int64_t measure_start = os_time_get_nano();
|
||||
result = vn_WaitForFences(dev_handle, 1, &queue->async_present.fence, true,
|
||||
UINT64_MAX);
|
||||
+ VN_MEASURE_ADD(fence, os_time_get_nano() - measure_start);
|
||||
+ vn_measure.fence_waits++;
|
||||
|
||||
vn_wsi_chains_lock(dev, queue->async_present.info, /*all=*/false);
|
||||
if (can_unlock_queue)
|
||||
@@ -537,8 +568,11 @@ vn_wsi_flush(VkQueue queue_handle)
|
||||
/* Being able to acquire the lock ensures async present queue access
|
||||
* has completed.
|
||||
*/
|
||||
+ int64_t measure_start = os_time_get_nano();
|
||||
simple_mtx_lock(&queue->async_present.queue_mutex);
|
||||
simple_mtx_unlock(&queue->async_present.queue_mutex);
|
||||
+ VN_MEASURE_ADD(flush, os_time_get_nano() - measure_start);
|
||||
+ vn_measure.flushes++;
|
||||
}
|
||||
|
||||
static VkPresentInfoKHR *
|
||||
@@ -811,9 +845,11 @@ vn_wsi_present_thread(void *data)
|
||||
|
||||
queue->async_present.pending = false;
|
||||
|
||||
+ int64_t measure_start = os_time_get_nano();
|
||||
queue->async_present.result =
|
||||
wsi_common_queue_present(queue_vk->base.device->physical->wsi_device,
|
||||
queue_vk, queue->async_present.info);
|
||||
+ vn_measure_present(os_time_get_nano() - measure_start);
|
||||
|
||||
vn_wsi_chains_unlock(dev, queue->async_present.info, /*all=*/true);
|
||||
simple_mtx_unlock(&queue->async_present.queue_mutex);
|
||||
diff --git a/src/vulkan/wsi/wsi_common_wayland.c b/src/vulkan/wsi/wsi_common_wayland.c
|
||||
index 047c8b3..a153a39 100644
|
||||
--- a/src/vulkan/wsi/wsi_common_wayland.c
|
||||
+++ b/src/vulkan/wsi/wsi_common_wayland.c
|
||||
@@ -3277,6 +3277,38 @@ set_timestamp(struct wsi_wl_swapchain *chain,
|
||||
return true;
|
||||
}
|
||||
|
||||
+/* MEASURE: per-frame cost of the shm copy, logged every 120 presents. */
|
||||
+static struct {
|
||||
+ uint32_t frames;
|
||||
+ int64_t last_present, interval_sum, interval_max, copy_sum, copy_max, total_sum, total_max;
|
||||
+} wl_measure;
|
||||
+
|
||||
+static void
|
||||
+wl_measure_frame(struct wsi_wl_swapchain *chain, int64_t start, int64_t copy_ns, int64_t end)
|
||||
+{
|
||||
+ if (wl_measure.last_present) {
|
||||
+ int64_t interval = start - wl_measure.last_present;
|
||||
+ wl_measure.interval_sum += interval;
|
||||
+ wl_measure.interval_max = MAX2(wl_measure.interval_max, interval);
|
||||
+ }
|
||||
+ wl_measure.last_present = start;
|
||||
+ wl_measure.copy_sum += copy_ns;
|
||||
+ wl_measure.copy_max = MAX2(wl_measure.copy_max, copy_ns);
|
||||
+ wl_measure.total_sum += end - start;
|
||||
+ wl_measure.total_max = MAX2(wl_measure.total_max, end - start);
|
||||
+ if (++wl_measure.frames < 120)
|
||||
+ return;
|
||||
+ fprintf(stderr, "MEASURE wl: %ux%u type %d | interval %.2f/%.2f ms | copy %.2f/%.2f ms (%u KB) | present %.2f/%.2f ms\n",
|
||||
+ chain->extent.width, chain->extent.height, chain->buffer_type,
|
||||
+ wl_measure.interval_sum / 119 / 1e6, wl_measure.interval_max / 1e6,
|
||||
+ wl_measure.copy_sum / 120 / 1e6, wl_measure.copy_max / 1e6,
|
||||
+ chain->images[0].base.row_pitches[0] * chain->extent.height / 1024,
|
||||
+ wl_measure.total_sum / 120 / 1e6, wl_measure.total_max / 1e6);
|
||||
+ int64_t last = wl_measure.last_present;
|
||||
+ memset(&wl_measure, 0, sizeof(wl_measure));
|
||||
+ wl_measure.last_present = last;
|
||||
+}
|
||||
+
|
||||
static VkResult
|
||||
wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
|
||||
uint32_t image_index,
|
||||
@@ -3286,6 +3318,7 @@ wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
|
||||
struct wsi_wl_swapchain *chain = (struct wsi_wl_swapchain *)wsi_chain;
|
||||
bool timestamped = false;
|
||||
bool queue_dispatched = false;
|
||||
+ int64_t measure_start = os_time_get_nano(), measure_copy = 0;
|
||||
|
||||
MESA_TRACE_FUNC_FLOW(&chain->images[image_index].wayland_buffer.flow);
|
||||
|
||||
@@ -3314,9 +3347,11 @@ wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
|
||||
|
||||
if (chain->buffer_type == WSI_WL_BUFFER_SHM_MEMCPY) {
|
||||
struct wsi_wl_image *image = &chain->images[image_index];
|
||||
+ int64_t copy_start = os_time_get_nano();
|
||||
wsi_image_invalidate_cpu_map(&chain->base, &image->base);
|
||||
memcpy(image->shm_ptr, image->base.cpu_map,
|
||||
image->base.row_pitches[0] * chain->extent.height);
|
||||
+ measure_copy = os_time_get_nano() - copy_start;
|
||||
}
|
||||
|
||||
VkResult ret = wsi_wl_swapchain_update_colorspace(chain);
|
||||
@@ -3515,6 +3550,7 @@ wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
|
||||
|
||||
memset(&chain->timing_request, 0, sizeof(chain->timing_request));
|
||||
|
||||
+ wl_measure_frame(chain, measure_start, measure_copy, os_time_get_nano());
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
// Prints the system Vulkan driver's memory types and heaps, as Android apps see them; Venus
|
||||
// rewrites them for the guest. Build in WSL and run over ADB (from Git Bash, MSYS_NO_PATHCONV=1):
|
||||
// /home/delo/android-sdk/ndk/27.3.13750724/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android31-clang \
|
||||
// -O1 -o memtypes memtypes.c -lvulkan
|
||||
// adb push memtypes /data/local/tmp/ && adb shell "chmod 755 /data/local/tmp/memtypes && /data/local/tmp/memtypes"
|
||||
#include <stdio.h>
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
VkApplicationInfo app = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO, .apiVersion = VK_API_VERSION_1_1 };
|
||||
VkInstanceCreateInfo ici = { .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, .pApplicationInfo = &app };
|
||||
VkInstance inst;
|
||||
if (vkCreateInstance(&ici, NULL, &inst) != VK_SUCCESS) {
|
||||
printf("vkCreateInstance failed\n");
|
||||
return 1;
|
||||
}
|
||||
uint32_t count = 1;
|
||||
VkPhysicalDevice pd;
|
||||
vkEnumeratePhysicalDevices(inst, &count, &pd);
|
||||
VkPhysicalDeviceProperties props;
|
||||
vkGetPhysicalDeviceProperties(pd, &props);
|
||||
printf("%s\n", props.deviceName);
|
||||
VkPhysicalDeviceMemoryProperties mp;
|
||||
vkGetPhysicalDeviceMemoryProperties(pd, &mp);
|
||||
for (uint32_t i = 0; i < mp.memoryHeapCount; i++)
|
||||
printf("heap %u: %llu MiB flags 0x%x\n", i, (unsigned long long)(mp.memoryHeaps[i].size >> 20), mp.memoryHeaps[i].flags);
|
||||
for (uint32_t i = 0; i < mp.memoryTypeCount; i++) {
|
||||
VkMemoryPropertyFlags f = mp.memoryTypes[i].propertyFlags;
|
||||
printf("type %u: heap %u flags 0x%x%s%s%s%s%s%s\n", i, mp.memoryTypes[i].heapIndex, f,
|
||||
f & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT ? " DEVICE_LOCAL" : "",
|
||||
f & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT ? " HOST_VISIBLE" : "",
|
||||
f & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT ? " HOST_COHERENT" : "",
|
||||
f & VK_MEMORY_PROPERTY_HOST_CACHED_BIT ? " HOST_CACHED" : "",
|
||||
f & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT ? " LAZY" : "",
|
||||
f & VK_MEMORY_PROPERTY_PROTECTED_BIT ? " PROTECTED" : "");
|
||||
}
|
||||
vkDestroyInstance(inst, NULL);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,541 @@
|
||||
diff --git a/src/virtio/vulkan/vn_device.c b/src/virtio/vulkan/vn_device.c
|
||||
index f42b9f9..6ed3051 100644
|
||||
--- a/src/virtio/vulkan/vn_device.c
|
||||
+++ b/src/virtio/vulkan/vn_device.c
|
||||
@@ -45,6 +45,7 @@ vn_queue_fini(struct vn_queue *queue)
|
||||
if (queue->wait_fence != VK_NULL_HANDLE) {
|
||||
vn_DestroyFence(dev_handle, queue->wait_fence, NULL);
|
||||
}
|
||||
+ simple_mtx_destroy(&queue->submit_mutex);
|
||||
vn_cached_storage_fini(&queue->storage);
|
||||
vn_queue_base_fini(&queue->base);
|
||||
}
|
||||
@@ -65,6 +66,7 @@ vn_queue_init(struct vn_device *dev,
|
||||
queue->index = array_index;
|
||||
|
||||
vn_cached_storage_init(&queue->storage, &dev->base.vk.alloc);
|
||||
+ simple_mtx_init(&queue->submit_mutex, mtx_plain);
|
||||
|
||||
if (dev->physical_device->emulate_second_queue ==
|
||||
queue_info->queueFamilyIndex &&
|
||||
diff --git a/src/virtio/vulkan/vn_device_memory.c b/src/virtio/vulkan/vn_device_memory.c
|
||||
index 8b6eb4c..be146b6 100644
|
||||
--- a/src/virtio/vulkan/vn_device_memory.c
|
||||
+++ b/src/virtio/vulkan/vn_device_memory.c
|
||||
@@ -93,11 +93,12 @@ vn_device_memory_bo_init(struct vn_device *dev, struct vn_device_memory *mem)
|
||||
}
|
||||
|
||||
const struct vk_device_memory *mem_vk = &mem->base.vk;
|
||||
- const VkMemoryType *mem_type = &dev->physical_device->memory_properties
|
||||
- .memoryTypes[mem_vk->memory_type_index];
|
||||
+ /* The renderer's flags: WSI maps a type the app sees as not mappable. */
|
||||
+ const VkMemoryPropertyFlags flags =
|
||||
+ dev->physical_device->renderer_memory_flags[mem_vk->memory_type_index];
|
||||
return vn_renderer_bo_create_from_device_memory(
|
||||
- dev->renderer, batch, mem_vk->size, mem->base.id,
|
||||
- mem_type->propertyFlags, mem_vk->export_handle_types, &mem->base_bo);
|
||||
+ dev->renderer, batch, mem_vk->size, mem->base.id, flags,
|
||||
+ mem_vk->export_handle_types, &mem->base_bo);
|
||||
}
|
||||
|
||||
static inline void
|
||||
@@ -374,6 +375,14 @@ vn_AllocateMemory(VkDevice device,
|
||||
{
|
||||
struct vn_device *dev = vn_device_from_handle(device);
|
||||
|
||||
+ VkMemoryAllocateInfo local_alloc_info;
|
||||
+ const uint32_t type_index = vn_wsi_memory_type_index(dev, pAllocateInfo);
|
||||
+ if (type_index != pAllocateInfo->memoryTypeIndex) {
|
||||
+ local_alloc_info = *pAllocateInfo;
|
||||
+ local_alloc_info.memoryTypeIndex = type_index;
|
||||
+ pAllocateInfo = &local_alloc_info;
|
||||
+ }
|
||||
+
|
||||
struct vn_device_memory *mem = vk_device_memory_create(
|
||||
&dev->base.vk, pAllocateInfo, pAllocator, sizeof(*mem));
|
||||
if (!mem)
|
||||
diff --git a/src/virtio/vulkan/vn_physical_device.c b/src/virtio/vulkan/vn_physical_device.c
|
||||
index f3581aa..c9c50e5 100644
|
||||
--- a/src/virtio/vulkan/vn_physical_device.c
|
||||
+++ b/src/virtio/vulkan/vn_physical_device.c
|
||||
@@ -982,6 +982,7 @@ vn_physical_device_init_memory_properties(
|
||||
ring, vn_physical_device_to_handle(physical_dev), &props2);
|
||||
|
||||
physical_dev->memory_properties = props2.memoryProperties;
|
||||
+ physical_dev->wsi_readback_memory_type = -1;
|
||||
|
||||
/* Kernel makes every mapping coherent. If a memory type is truly
|
||||
* incoherent, it's better to remove the host-visible flag than silently
|
||||
@@ -996,12 +997,18 @@ vn_physical_device_init_memory_properties(
|
||||
VkMemoryPropertyFlags *flags = &props->memoryTypes[i].propertyFlags;
|
||||
const bool coherent = *flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
|
||||
const bool cached = *flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
|
||||
+ physical_dev->renderer_memory_flags[i] = *flags;
|
||||
if (coherent) {
|
||||
if (first_coherent == VK_MAX_MEMORY_TYPES)
|
||||
first_coherent = i;
|
||||
if (cached)
|
||||
has_coherent_cached = true;
|
||||
} else if (cached) {
|
||||
+ /* vtest maps the renderer's dma-buf itself and can sync the CPU
|
||||
+ * cache around a read, which WSI does before copying a frame out.
|
||||
+ */
|
||||
+ if (VN_DEBUG(VTEST) && physical_dev->wsi_readback_memory_type < 0)
|
||||
+ physical_dev->wsi_readback_memory_type = i;
|
||||
*flags &= ~(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
|
||||
VK_MEMORY_PROPERTY_HOST_CACHED_BIT);
|
||||
}
|
||||
@@ -1060,7 +1067,9 @@ vn_physical_device_init_external_fence_handles(
|
||||
{
|
||||
physical_dev->external_fence_handles = 0;
|
||||
|
||||
- if (physical_dev->instance->renderer->info.has_external_sync) {
|
||||
+ /* Over vtest an export waits on the CPU and returns -1 (vn_sync.c). */
|
||||
+ if (physical_dev->instance->renderer->info.has_external_sync ||
|
||||
+ VN_DEBUG(VTEST)) {
|
||||
#if !DETECT_OS_WINDOWS
|
||||
physical_dev->external_fence_handles =
|
||||
VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;
|
||||
@@ -1095,7 +1104,8 @@ vn_physical_device_init_external_semaphore_handles(
|
||||
physical_dev->external_binary_semaphore_handles = 0;
|
||||
physical_dev->external_timeline_semaphore_handles = 0;
|
||||
|
||||
- if (physical_dev->instance->renderer->info.has_external_sync) {
|
||||
+ if (physical_dev->instance->renderer->info.has_external_sync ||
|
||||
+ VN_DEBUG(VTEST)) {
|
||||
#if !DETECT_OS_WINDOWS
|
||||
physical_dev->external_binary_semaphore_handles =
|
||||
VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT;
|
||||
@@ -1173,7 +1183,8 @@ vn_physical_device_get_native_extensions(
|
||||
exts->KHR_map_memory2 = true;
|
||||
#if !DETECT_OS_WINDOWS
|
||||
exts->EXT_map_memory_placed = true;
|
||||
- exts->EXT_physical_device_drm = true;
|
||||
+ /* Over vtest there is no DRM device behind Venus. */
|
||||
+ exts->EXT_physical_device_drm = !VN_DEBUG(VTEST);
|
||||
#endif
|
||||
/* use common implementation */
|
||||
exts->EXT_tooling_info = true;
|
||||
diff --git a/src/virtio/vulkan/vn_physical_device.h b/src/virtio/vulkan/vn_physical_device.h
|
||||
index 2c2f4a9..c002a84 100644
|
||||
--- a/src/virtio/vulkan/vn_physical_device.h
|
||||
+++ b/src/virtio/vulkan/vn_physical_device.h
|
||||
@@ -112,6 +112,12 @@ struct vn_physical_device {
|
||||
int emulate_second_queue;
|
||||
|
||||
VkPhysicalDeviceMemoryProperties memory_properties;
|
||||
+ /* The renderer's flags, before memory_properties hides the host-visible
|
||||
+ * bit of cached types that are not coherent.
|
||||
+ */
|
||||
+ VkMemoryPropertyFlags renderer_memory_flags[VK_MAX_MEMORY_TYPES];
|
||||
+ /* A hidden cached type for the CPU copy of WSI images, or -1. */
|
||||
+ int wsi_readback_memory_type;
|
||||
|
||||
struct {
|
||||
VkExternalMemoryHandleTypeFlagBits renderer_handle_type;
|
||||
diff --git a/src/virtio/vulkan/vn_queue.c b/src/virtio/vulkan/vn_queue.c
|
||||
index 057842a..dee1244 100644
|
||||
--- a/src/virtio/vulkan/vn_queue.c
|
||||
+++ b/src/virtio/vulkan/vn_queue.c
|
||||
@@ -1049,11 +1049,16 @@ vn_queue_submission_init_syncs(struct vn_queue_submission *submit)
|
||||
assert(fence->payload->type == VN_SYNC_TYPE_SYNC);
|
||||
|
||||
submit->temp.syncs[sync_index] = fence->payload->sync;
|
||||
- submit->temp.sync_vals[sync_index] = 1;
|
||||
+ submit->temp.sync_vals[sync_index++] = 1;
|
||||
|
||||
/* fence is backed by renderer sync without renderer object */
|
||||
submit->fence_handle = VK_NULL_HANDLE;
|
||||
}
|
||||
+
|
||||
+ /* A signal semaphore counted for an imported sync_file has no renderer
|
||||
+ * sync to fill in, and the slot would hand vtest stale storage.
|
||||
+ */
|
||||
+ submit->sync_count = sync_index;
|
||||
}
|
||||
|
||||
static VkResult
|
||||
@@ -1248,9 +1253,13 @@ vn_queue_submit(struct vn_queue_submission *submit)
|
||||
struct vn_device *dev = vn_device_from_vk(queue->base.vk.base.device);
|
||||
VkResult result;
|
||||
|
||||
+ simple_mtx_lock(&queue->submit_mutex);
|
||||
+
|
||||
result = vn_queue_submission_prepare_submit(submit);
|
||||
- if (result != VK_SUCCESS)
|
||||
+ if (result != VK_SUCCESS) {
|
||||
+ simple_mtx_unlock(&queue->submit_mutex);
|
||||
return vn_error(dev->instance, result);
|
||||
+ }
|
||||
|
||||
result = vn_queue_submission_do_submit(submit);
|
||||
if (result != VK_SUCCESS)
|
||||
@@ -1260,6 +1269,7 @@ vn_queue_submit(struct vn_queue_submission *submit)
|
||||
|
||||
out_cleanup:
|
||||
vn_queue_submission_cleanup(submit);
|
||||
+ simple_mtx_unlock(&queue->submit_mutex);
|
||||
return vn_result(dev->instance, result);
|
||||
}
|
||||
|
||||
diff --git a/src/virtio/vulkan/vn_queue.h b/src/virtio/vulkan/vn_queue.h
|
||||
index 3ea6d9d..4160e56 100644
|
||||
--- a/src/virtio/vulkan/vn_queue.h
|
||||
+++ b/src/virtio/vulkan/vn_queue.h
|
||||
@@ -52,6 +52,11 @@ struct vn_queue {
|
||||
/* for vn_queue_submission storage */
|
||||
struct vn_cached_storage storage;
|
||||
|
||||
+ /* Serializes submissions. The async present thread submits to this queue
|
||||
+ * while the application does, after vn_wsi_fence_wait drops queue_mutex.
|
||||
+ */
|
||||
+ simple_mtx_t submit_mutex;
|
||||
+
|
||||
/* for async queue present */
|
||||
struct {
|
||||
/* Protects VkQueue host access except async present states. */
|
||||
diff --git a/src/virtio/vulkan/vn_renderer_vtest.c b/src/virtio/vulkan/vn_renderer_vtest.c
|
||||
index fc59d32..c7b20f5 100644
|
||||
--- a/src/virtio/vulkan/vn_renderer_vtest.c
|
||||
+++ b/src/virtio/vulkan/vn_renderer_vtest.c
|
||||
@@ -13,9 +13,11 @@
|
||||
#include <sys/mman.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
+#include <sys/ioctl.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
+#include "drm-uapi/dma-buf.h"
|
||||
#include "drm-uapi/virtgpu_drm.h"
|
||||
#include "util/os_file.h"
|
||||
#include "util/os_misc.h"
|
||||
@@ -38,8 +40,10 @@ struct vtest_bo {
|
||||
struct vn_renderer_bo base;
|
||||
|
||||
uint32_t blob_flags;
|
||||
- /* might be closed after mmap */
|
||||
+ /* might be closed after mmap, but not when the memory is incoherent */
|
||||
int res_fd;
|
||||
+ /* the CPU cache is synced through the dma-buf on flush and invalidate */
|
||||
+ bool incoherent;
|
||||
};
|
||||
|
||||
struct vtest_sync {
|
||||
@@ -607,13 +611,35 @@ vtest_sync_create(struct vn_renderer *renderer,
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
+/* The guest maps the renderer's dma-buf, so a CPU cache sync on it covers the
|
||||
+ * mapping. Coherent memory needs none.
|
||||
+ */
|
||||
+static void
|
||||
+vtest_bo_sync_cpu_cache(struct vtest *vtest,
|
||||
+ struct vtest_bo *bo,
|
||||
+ uint64_t direction)
|
||||
+{
|
||||
+ if (!bo->incoherent || bo->res_fd < 0)
|
||||
+ return;
|
||||
+
|
||||
+ struct dma_buf_sync sync = { .flags = DMA_BUF_SYNC_START | direction };
|
||||
+ int ret = ioctl(bo->res_fd, DMA_BUF_IOCTL_SYNC, &sync);
|
||||
+ if (!ret) {
|
||||
+ sync.flags = DMA_BUF_SYNC_END | direction;
|
||||
+ ret = ioctl(bo->res_fd, DMA_BUF_IOCTL_SYNC, &sync);
|
||||
+ }
|
||||
+ if (ret)
|
||||
+ vn_log(vtest->instance, "dma-buf cache sync failed: %s", strerror(errno));
|
||||
+}
|
||||
+
|
||||
static void
|
||||
vtest_bo_invalidate(struct vn_renderer *renderer,
|
||||
struct vn_renderer_bo *bo,
|
||||
VkDeviceSize offset,
|
||||
VkDeviceSize size)
|
||||
{
|
||||
- /* nop */
|
||||
+ vtest_bo_sync_cpu_cache((struct vtest *)renderer, (struct vtest_bo *)bo,
|
||||
+ DMA_BUF_SYNC_READ);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -622,7 +648,8 @@ vtest_bo_flush(struct vn_renderer *renderer,
|
||||
VkDeviceSize offset,
|
||||
VkDeviceSize size)
|
||||
{
|
||||
- /* nop */
|
||||
+ vtest_bo_sync_cpu_cache((struct vtest *)renderer, (struct vtest_bo *)bo,
|
||||
+ DMA_BUF_SYNC_WRITE);
|
||||
}
|
||||
|
||||
static void *
|
||||
@@ -651,7 +678,7 @@ vtest_bo_map(struct vn_renderer *renderer,
|
||||
} else {
|
||||
bo->base.mmap_ptr = ptr;
|
||||
/* we don't need the fd anymore */
|
||||
- if (!shareable) {
|
||||
+ if (!shareable && !bo->incoherent) {
|
||||
close(bo->res_fd);
|
||||
bo->res_fd = -1;
|
||||
}
|
||||
@@ -735,6 +762,8 @@ vtest_bo_create_from_device_memory(
|
||||
},
|
||||
.res_fd = res_fd,
|
||||
.blob_flags = blob_flags,
|
||||
+ .incoherent = (flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) &&
|
||||
+ !(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT),
|
||||
};
|
||||
|
||||
*out_bo = &bo->base;
|
||||
diff --git a/src/virtio/vulkan/vn_sync.c b/src/virtio/vulkan/vn_sync.c
|
||||
index ee0a539..4687520 100644
|
||||
--- a/src/virtio/vulkan/vn_sync.c
|
||||
+++ b/src/virtio/vulkan/vn_sync.c
|
||||
@@ -26,7 +26,8 @@ vn_sync_payload_release_internal(struct vn_device *dev,
|
||||
vn_renderer_sync_destroy(dev->renderer, payload->sync);
|
||||
break;
|
||||
case VN_SYNC_TYPE_TIMELINE_SYNC:
|
||||
- for (uint32_t i = 0; i < dev->queue_count; i++)
|
||||
+ /* One sync per queue plus one for host signals. */
|
||||
+ for (uint32_t i = 0; i <= dev->queue_count; i++)
|
||||
vn_renderer_sync_destroy(dev->renderer, payload->syncs[i]);
|
||||
vk_free2(&dev->base.vk.alloc, alloc, payload->syncs);
|
||||
break;
|
||||
@@ -49,6 +50,24 @@ vn_sync_payload_release(struct vn_device *dev,
|
||||
vn_sync_payload_release_internal(dev, payload, NULL);
|
||||
}
|
||||
|
||||
+/* vtest cannot export a sync_file: wait for the payload on the CPU and
|
||||
+ * return -1, the sync fd that stands for an already signaled payload.
|
||||
+ */
|
||||
+static int
|
||||
+vn_sync_export_signaled(struct vn_device *dev, struct vn_renderer_sync *sync)
|
||||
+{
|
||||
+ uint64_t val = 1;
|
||||
+ const struct vn_renderer_wait wait = {
|
||||
+ .timeout = UINT64_MAX,
|
||||
+ .syncs = &sync,
|
||||
+ .sync_values = &val,
|
||||
+ .sync_count = 1,
|
||||
+ };
|
||||
+ vn_renderer_wait(dev->renderer, &wait);
|
||||
+ vn_renderer_sync_reset(dev->renderer, sync);
|
||||
+ return -1;
|
||||
+}
|
||||
+
|
||||
/* fence commands */
|
||||
|
||||
static VkResult
|
||||
@@ -298,7 +317,10 @@ vn_GetFenceFdKHR(VkDevice device,
|
||||
assert(sync_file);
|
||||
|
||||
int fd = -1;
|
||||
- if (payload->type == VN_SYNC_TYPE_SYNC) {
|
||||
+ if (payload->type == VN_SYNC_TYPE_SYNC &&
|
||||
+ !dev->renderer->info.has_external_sync) {
|
||||
+ fd = vn_sync_export_signaled(dev, payload->sync);
|
||||
+ } else if (payload->type == VN_SYNC_TYPE_SYNC) {
|
||||
fd = vn_renderer_sync_export_syncobj(dev->renderer,
|
||||
fence->payload->sync, sync_file);
|
||||
vn_renderer_sync_reset(dev->renderer, fence->payload->sync);
|
||||
@@ -830,7 +852,11 @@ vn_GetSemaphoreFdKHR(VkDevice device,
|
||||
assert(sync_file);
|
||||
|
||||
int fd = -1;
|
||||
- if (payload->type == VN_SYNC_TYPE_SYNC) {
|
||||
+ if (payload->type == VN_SYNC_TYPE_SYNC &&
|
||||
+ !dev->renderer->info.has_external_sync) {
|
||||
+ assert(sem->sync_fd_export);
|
||||
+ fd = vn_sync_export_signaled(dev, payload->sync);
|
||||
+ } else if (payload->type == VN_SYNC_TYPE_SYNC) {
|
||||
assert(sem->sync_fd_export);
|
||||
fd = vn_renderer_sync_export_syncobj(dev->renderer, payload->sync,
|
||||
sync_file);
|
||||
diff --git a/src/virtio/vulkan/vn_wsi.c b/src/virtio/vulkan/vn_wsi.c
|
||||
index e73ab48..f0b41b1 100644
|
||||
--- a/src/virtio/vulkan/vn_wsi.c
|
||||
+++ b/src/virtio/vulkan/vn_wsi.c
|
||||
@@ -287,6 +287,53 @@ vn_wsi_memory_info_init(struct vn_device_memory *mem,
|
||||
}
|
||||
}
|
||||
|
||||
+/* The buffer WSI copies each frame into for the CPU (software presentation)
|
||||
+ * gets the hidden cached type: on vtest the host-visible type the app sees is
|
||||
+ * uncached, and reading a frame from it took most of a present. WSI
|
||||
+ * invalidates the buffer before each read.
|
||||
+ */
|
||||
+uint32_t
|
||||
+vn_wsi_memory_type_index(const struct vn_device *dev,
|
||||
+ const VkMemoryAllocateInfo *alloc_info)
|
||||
+{
|
||||
+ const struct vn_physical_device *physical_dev = dev->physical_device;
|
||||
+ const int readback_type = physical_dev->wsi_readback_memory_type;
|
||||
+ if (readback_type < 0 || !physical_dev->wsi_device.sw)
|
||||
+ return alloc_info->memoryTypeIndex;
|
||||
+
|
||||
+ const VkMemoryDedicatedAllocateInfo *dedicated_info = NULL;
|
||||
+ bool wsi = false;
|
||||
+ vk_foreach_struct_const(pnext, alloc_info->pNext) {
|
||||
+ switch ((uint32_t)pnext->sType) {
|
||||
+ case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO:
|
||||
+ dedicated_info = (const void *)pnext;
|
||||
+ break;
|
||||
+ case VK_STRUCTURE_TYPE_WSI_MEMORY_ALLOCATE_INFO_MESA:
|
||||
+ wsi = true;
|
||||
+ break;
|
||||
+ case VK_STRUCTURE_TYPE_EXPORT_MEMORY_ALLOCATE_INFO:
|
||||
+ case VK_STRUCTURE_TYPE_IMPORT_MEMORY_HOST_POINTER_INFO_EXT:
|
||||
+ return alloc_info->memoryTypeIndex;
|
||||
+ default:
|
||||
+ break;
|
||||
+ }
|
||||
+ }
|
||||
+ if (!wsi || !dedicated_info || dedicated_info->buffer == VK_NULL_HANDLE)
|
||||
+ return alloc_info->memoryTypeIndex;
|
||||
+
|
||||
+ const VkMemoryPropertyFlags flags =
|
||||
+ physical_dev->memory_properties.memoryTypes[alloc_info->memoryTypeIndex]
|
||||
+ .propertyFlags;
|
||||
+ const struct vn_buffer *buf = vn_buffer_from_handle(dedicated_info->buffer);
|
||||
+ const uint32_t type_bits =
|
||||
+ buf->requirements.memory.memoryRequirements.memoryTypeBits;
|
||||
+ if (!(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) ||
|
||||
+ !(type_bits & BITFIELD_BIT(readback_type)))
|
||||
+ return alloc_info->memoryTypeIndex;
|
||||
+
|
||||
+ return readback_type;
|
||||
+}
|
||||
+
|
||||
static uint32_t
|
||||
vn_modifier_plane_count(struct vn_physical_device *physical_dev,
|
||||
VkFormat format,
|
||||
diff --git a/src/virtio/vulkan/vn_wsi.h b/src/virtio/vulkan/vn_wsi.h
|
||||
index 2231556..1429feb 100644
|
||||
--- a/src/virtio/vulkan/vn_wsi.h
|
||||
+++ b/src/virtio/vulkan/vn_wsi.h
|
||||
@@ -34,6 +34,10 @@ void
|
||||
vn_wsi_memory_info_init(struct vn_device_memory *mem,
|
||||
const VkMemoryAllocateInfo *alloc_info);
|
||||
|
||||
+uint32_t
|
||||
+vn_wsi_memory_type_index(const struct vn_device *dev,
|
||||
+ const VkMemoryAllocateInfo *alloc_info);
|
||||
+
|
||||
bool
|
||||
vn_wsi_validate_image_format_info(
|
||||
struct vn_physical_device *physical_dev,
|
||||
@@ -77,6 +81,13 @@ vn_wsi_memory_info_init(struct vn_device_memory *mem,
|
||||
{
|
||||
}
|
||||
|
||||
+static inline uint32_t
|
||||
+vn_wsi_memory_type_index(const struct vn_device *dev,
|
||||
+ const VkMemoryAllocateInfo *alloc_info)
|
||||
+{
|
||||
+ return alloc_info->memoryTypeIndex;
|
||||
+}
|
||||
+
|
||||
static inline bool
|
||||
vn_wsi_validate_image_format_info(struct vn_physical_device *physical_dev,
|
||||
const VkPhysicalDeviceImageFormatInfo2 *info)
|
||||
diff --git a/src/vulkan/wsi/wsi_common.c b/src/vulkan/wsi/wsi_common.c
|
||||
index 411ee3e..ae38115 100644
|
||||
--- a/src/vulkan/wsi/wsi_common.c
|
||||
+++ b/src/vulkan/wsi/wsi_common.c
|
||||
@@ -235,6 +235,7 @@ wsi_device_init(struct wsi_device *wsi,
|
||||
WSI_GET_CB(WaitForFences);
|
||||
WSI_GET_CB(MapMemory);
|
||||
WSI_GET_CB(UnmapMemory);
|
||||
+ WSI_GET_CB(InvalidateMappedMemoryRanges);
|
||||
if (wsi->has_present_wait)
|
||||
WSI_GET_CB(WaitSemaphores);
|
||||
#undef WSI_GET_CB
|
||||
@@ -3577,6 +3578,22 @@ wsi_create_cpu_linear_image_mem(const struct wsi_swapchain *chain,
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
+void
|
||||
+wsi_image_invalidate_cpu_map(const struct wsi_swapchain *chain,
|
||||
+ const struct wsi_image *image)
|
||||
+{
|
||||
+ /* A driver may back the CPU image with cached memory that is not
|
||||
+ * coherent; for coherent memory this does nothing.
|
||||
+ */
|
||||
+ const VkMappedMemoryRange range = {
|
||||
+ .sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE,
|
||||
+ .memory = image->blit.memory != VK_NULL_HANDLE ? image->blit.memory
|
||||
+ : image->memory,
|
||||
+ .size = VK_WHOLE_SIZE,
|
||||
+ };
|
||||
+ chain->wsi->InvalidateMappedMemoryRanges(chain->device, 1, &range);
|
||||
+}
|
||||
+
|
||||
static VkResult
|
||||
wsi_create_cpu_buffer_image_mem(const struct wsi_swapchain *chain,
|
||||
const struct wsi_image_info *info,
|
||||
diff --git a/src/vulkan/wsi/wsi_common.h b/src/vulkan/wsi/wsi_common.h
|
||||
index 803d140..432ba43 100644
|
||||
--- a/src/vulkan/wsi/wsi_common.h
|
||||
+++ b/src/vulkan/wsi/wsi_common.h
|
||||
@@ -247,6 +247,7 @@ struct wsi_device {
|
||||
WSI_CB(WaitForFences);
|
||||
WSI_CB(MapMemory);
|
||||
WSI_CB(UnmapMemory);
|
||||
+ WSI_CB(InvalidateMappedMemoryRanges);
|
||||
WSI_CB(WaitSemaphores);
|
||||
#undef WSI_CB
|
||||
|
||||
diff --git a/src/vulkan/wsi/wsi_common_drm.c b/src/vulkan/wsi/wsi_common_drm.c
|
||||
index dbf65b2..2a6b701 100644
|
||||
--- a/src/vulkan/wsi/wsi_common_drm.c
|
||||
+++ b/src/vulkan/wsi/wsi_common_drm.c
|
||||
@@ -204,6 +204,10 @@ wsi_signal_dma_buf_from_semaphore(const struct wsi_swapchain *chain,
|
||||
if (result != VK_SUCCESS)
|
||||
return result;
|
||||
|
||||
+ /* -1 is an already signaled payload: there is nothing to attach. */
|
||||
+ if (sync_file_fd < 0)
|
||||
+ return VK_SUCCESS;
|
||||
+
|
||||
result = wsi_dma_buf_import_sync_file(image->dma_buf_fd, sync_file_fd);
|
||||
close(sync_file_fd);
|
||||
return result;
|
||||
diff --git a/src/vulkan/wsi/wsi_common_private.h b/src/vulkan/wsi/wsi_common_private.h
|
||||
index 3d2cd7d..e469414 100644
|
||||
--- a/src/vulkan/wsi/wsi_common_private.h
|
||||
+++ b/src/vulkan/wsi/wsi_common_private.h
|
||||
@@ -410,6 +410,10 @@ wsi_configure_cpu_image(const struct wsi_swapchain *chain,
|
||||
const struct wsi_cpu_image_params *params,
|
||||
struct wsi_image_info *info);
|
||||
|
||||
+void
|
||||
+wsi_image_invalidate_cpu_map(const struct wsi_swapchain *chain,
|
||||
+ const struct wsi_image *image);
|
||||
+
|
||||
VkResult
|
||||
wsi_create_buffer_blit_context(const struct wsi_swapchain *chain,
|
||||
const struct wsi_image_info *info,
|
||||
diff --git a/src/vulkan/wsi/wsi_common_wayland.c b/src/vulkan/wsi/wsi_common_wayland.c
|
||||
index 33a4059..047c8b3 100644
|
||||
--- a/src/vulkan/wsi/wsi_common_wayland.c
|
||||
+++ b/src/vulkan/wsi/wsi_common_wayland.c
|
||||
@@ -3314,6 +3314,7 @@ wsi_wl_swapchain_queue_present(struct wsi_swapchain *wsi_chain,
|
||||
|
||||
if (chain->buffer_type == WSI_WL_BUFFER_SHM_MEMCPY) {
|
||||
struct wsi_wl_image *image = &chain->images[image_index];
|
||||
+ wsi_image_invalidate_cpu_map(&chain->base, &image->base);
|
||||
memcpy(image->shm_ptr, image->base.cpu_map,
|
||||
image->base.row_pitches[0] * chain->extent.height);
|
||||
}
|
||||
diff --git a/src/vulkan/wsi/wsi_common_x11.c b/src/vulkan/wsi/wsi_common_x11.c
|
||||
index a077afe..579ba13 100644
|
||||
--- a/src/vulkan/wsi/wsi_common_x11.c
|
||||
+++ b/src/vulkan/wsi/wsi_common_x11.c
|
||||
@@ -1854,6 +1854,7 @@ x11_present_to_x11_sw(struct x11_swapchain *chain, uint32_t image_index)
|
||||
xcb_get_geometry_cookie_t geom_cookie = xcb_get_geometry(chain->conn, chain->window);
|
||||
|
||||
xcb_void_cookie_t cookie;
|
||||
+ wsi_image_invalidate_cpu_map(&chain->base, &image->base);
|
||||
void *myptr = image->base.cpu_map;
|
||||
size_t hdr_len = sizeof(xcb_put_image_request_t);
|
||||
int stride_b = image->base.row_pitches[0];
|
||||
@@ -0,0 +1,3 @@
|
||||
VENUS_REPO=The412Banner/droiddeck-nonadreno-drivers
|
||||
VENUS_TAG=venus-proto13-e24ed8ab
|
||||
VENUS_SHA256=25f236bbc32c8490fc61c20a6016b0bea4a8c3267a69de6b91496da2afe6bc04
|
||||
@@ -0,0 +1,322 @@
|
||||
diff --git a/src/venus/vkr_device_memory.c b/src/venus/vkr_device_memory.c
|
||||
index ffe621b..01cd811 100644
|
||||
--- a/src/venus/vkr_device_memory.c
|
||||
+++ b/src/venus/vkr_device_memory.c
|
||||
@@ -227,6 +227,136 @@ vkr_gbm_get_fd_info_from_allocation_info(UNUSED struct vkr_physical_device *phys
|
||||
|
||||
#endif /* ENABLE_GBM_ALLOCATION */
|
||||
|
||||
+#ifdef __ANDROID__
|
||||
+#include <android/hardware_buffer.h>
|
||||
+#include <fcntl.h>
|
||||
+#include <linux/dma-heap.h>
|
||||
+#include <sys/ioctl.h>
|
||||
+#include <sys/socket.h>
|
||||
+
|
||||
+static int vkr_dma_heap_fd = -1;
|
||||
+static int vkr_dma_heap_cached_fd = -1;
|
||||
+
|
||||
+/* Uncached for coherent memory types: nobody brackets CPU access with
|
||||
+ * DMA_BUF_IOCTL_SYNC (the guest maps the memory as host-coherent), so on a
|
||||
+ * cached heap the CPU and the GPU see each other's writes torn at cache-line
|
||||
+ * granularity. Cached for cached types, which are not coherent: whoever maps
|
||||
+ * them syncs the CPU cache through the dma-buf.
|
||||
+ */
|
||||
+static void
|
||||
+vkr_dma_heap_init_once(void)
|
||||
+{
|
||||
+ vkr_dma_heap_cached_fd = open("/dev/dma_heap/system", O_RDONLY | O_CLOEXEC);
|
||||
+ vkr_dma_heap_fd = open("/dev/dma_heap/system-uncached", O_RDONLY | O_CLOEXEC);
|
||||
+ if (vkr_dma_heap_fd < 0) {
|
||||
+ vkr_log("no uncached dma-buf heap (%s): using the cached one", strerror(errno));
|
||||
+ vkr_dma_heap_fd = vkr_dma_heap_cached_fd;
|
||||
+ }
|
||||
+ if (vkr_dma_heap_fd < 0)
|
||||
+ vkr_log("no dma-buf heap (%s): allocating AHardwareBuffers", strerror(errno));
|
||||
+}
|
||||
+
|
||||
+/* An AHardwareBuffer's dma-buf: the first fd of its native handle, which the
|
||||
+ * NDK hands out only through a unix socket.
|
||||
+ */
|
||||
+static int
|
||||
+vkr_ahb_alloc_dma_buf(size_t size)
|
||||
+{
|
||||
+ const AHardwareBuffer_Desc desc = {
|
||||
+ .width = (uint32_t)size,
|
||||
+ .height = 1,
|
||||
+ .layers = 1,
|
||||
+ .format = AHARDWAREBUFFER_FORMAT_BLOB,
|
||||
+ .usage = AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN |
|
||||
+ AHARDWAREBUFFER_USAGE_GPU_DATA_BUFFER,
|
||||
+ };
|
||||
+ AHardwareBuffer *ahb = NULL;
|
||||
+ if (AHardwareBuffer_allocate(&desc, &ahb)) {
|
||||
+ vkr_log("AHardwareBuffer_allocate of %zu failed", size);
|
||||
+ return -1;
|
||||
+ }
|
||||
+
|
||||
+ int fd = -1;
|
||||
+ int sv[2];
|
||||
+ if (!socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, sv)) {
|
||||
+ if (!AHardwareBuffer_sendHandleToUnixSocket(ahb, sv[0])) {
|
||||
+ char data[4096];
|
||||
+ char control[CMSG_SPACE(sizeof(int) * 16)];
|
||||
+ struct iovec iov = { .iov_base = data, .iov_len = sizeof(data) };
|
||||
+ struct msghdr msg = {
|
||||
+ .msg_iov = &iov,
|
||||
+ .msg_iovlen = 1,
|
||||
+ .msg_control = control,
|
||||
+ .msg_controllen = sizeof(control),
|
||||
+ };
|
||||
+ if (recvmsg(sv[1], &msg, MSG_CMSG_CLOEXEC) > 0) {
|
||||
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
|
||||
+ if (cmsg && cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
|
||||
+ const int count = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
|
||||
+ const int *fds = (const int *)CMSG_DATA(cmsg);
|
||||
+ for (int i = 0; i < count; i++) {
|
||||
+ if (i == 0)
|
||||
+ fd = fds[i];
|
||||
+ else
|
||||
+ close(fds[i]);
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ close(sv[0]);
|
||||
+ close(sv[1]);
|
||||
+ }
|
||||
+ AHardwareBuffer_release(ahb);
|
||||
+ if (fd < 0)
|
||||
+ vkr_log("could not get the dma-buf of an AHardwareBuffer");
|
||||
+ return fd;
|
||||
+}
|
||||
+
|
||||
+static VkResult
|
||||
+vkr_android_get_fd_info_from_allocation_info(const VkMemoryAllocateInfo *alloc_info,
|
||||
+ bool cached,
|
||||
+ int *out_dma_buf_fd,
|
||||
+ VkImportMemoryFdInfoKHR *out_fd_info)
|
||||
+{
|
||||
+ static once_flag dma_heap_once_flag = ONCE_FLAG_INIT;
|
||||
+ call_once(&dma_heap_once_flag, vkr_dma_heap_init_once);
|
||||
+
|
||||
+ const size_t size = align(alloc_info->allocationSize, getpagesize());
|
||||
+ const int heap_fd =
|
||||
+ cached && vkr_dma_heap_cached_fd >= 0 ? vkr_dma_heap_cached_fd : vkr_dma_heap_fd;
|
||||
+ int dma_buf_fd = -1;
|
||||
+ if (heap_fd >= 0) {
|
||||
+ struct dma_heap_allocation_data data = {
|
||||
+ .len = size,
|
||||
+ .fd_flags = O_RDWR | O_CLOEXEC,
|
||||
+ };
|
||||
+ if (!ioctl(heap_fd, DMA_HEAP_IOCTL_ALLOC, &data))
|
||||
+ dma_buf_fd = data.fd;
|
||||
+ else
|
||||
+ vkr_log("dma-buf heap allocation of %zu failed (%s)", size, strerror(errno));
|
||||
+ }
|
||||
+ if (dma_buf_fd < 0)
|
||||
+ dma_buf_fd = vkr_ahb_alloc_dma_buf(size);
|
||||
+ if (dma_buf_fd < 0)
|
||||
+ return VK_ERROR_OUT_OF_DEVICE_MEMORY;
|
||||
+
|
||||
+ const int fd = os_dupfd_cloexec(dma_buf_fd);
|
||||
+ if (fd < 0) {
|
||||
+ close(dma_buf_fd);
|
||||
+ return VK_ERROR_OUT_OF_HOST_MEMORY;
|
||||
+ }
|
||||
+
|
||||
+ *out_dma_buf_fd = dma_buf_fd;
|
||||
+ *out_fd_info = (VkImportMemoryFdInfoKHR){
|
||||
+ .sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
|
||||
+ .pNext = alloc_info->pNext,
|
||||
+ .fd = fd,
|
||||
+ .handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
|
||||
+ };
|
||||
+ return VK_SUCCESS;
|
||||
+}
|
||||
+#endif /* __ANDROID__ */
|
||||
+
|
||||
static void
|
||||
vkr_dispatch_vkAllocateMemory(struct vn_dispatch_context *dispatch,
|
||||
struct vn_command_vkAllocateMemory *args)
|
||||
@@ -281,7 +411,14 @@ vkr_dispatch_vkAllocateMemory(struct vn_dispatch_context *dispatch,
|
||||
int udmabuf_fd = -1;
|
||||
void *gbm_bo = NULL;
|
||||
VkExportMemoryAllocateInfo local_export_info;
|
||||
- if ((property_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) && !res_info) {
|
||||
+ /* Android without driver export: a dma-buf export of any memory type is
|
||||
+ * served by a dma-buf the server allocates and imports.
|
||||
+ */
|
||||
+ const bool android_export =
|
||||
+ physical_dev->android_dma_buf_import && export_info &&
|
||||
+ (export_info->handleTypes & VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
|
||||
+ if (((property_flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) || android_export) &&
|
||||
+ !res_info) {
|
||||
/* An implementation can support dma_buf import along with opaque fd export/import.
|
||||
* If the client driver is using external memory and requesting dma_buf, without
|
||||
* dma_buf fd export support, we must use gbm bo import path instead of forcing
|
||||
@@ -346,6 +483,13 @@ vkr_dispatch_vkAllocateMemory(struct vn_dispatch_context *dispatch,
|
||||
*/
|
||||
args->ret = vkr_udmabuf_get_fd_info_from_allocation_info(
|
||||
physical_dev, alloc_info, &udmabuf_fd, &local_import_info);
|
||||
+#ifdef __ANDROID__
|
||||
+ } else if (physical_dev->android_dma_buf_import) {
|
||||
+ /* Kept in udmabuf_fd: exported and closed the same way. */
|
||||
+ args->ret = vkr_android_get_fd_info_from_allocation_info(
|
||||
+ alloc_info, property_flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
|
||||
+ &udmabuf_fd, &local_import_info);
|
||||
+#endif
|
||||
} else {
|
||||
args->ret = vkr_gbm_get_fd_info_from_allocation_info(
|
||||
physical_dev, alloc_info, &gbm_bo, &local_import_info);
|
||||
@@ -367,6 +511,12 @@ vkr_dispatch_vkAllocateMemory(struct vn_dispatch_context *dispatch,
|
||||
|
||||
struct vkr_device_memory *mem = vkr_device_memory_create_and_add(ctx, args);
|
||||
if (!mem) {
|
||||
+ vkr_log("vkAllocateMemory failed (%d): type %u flags 0x%x size %" PRIu64 "%s",
|
||||
+ args->ret, mem_type_index, property_flags,
|
||||
+ (uint64_t)alloc_info->allocationSize,
|
||||
+ udmabuf_fd >= 0 ? ", imported dma-buf" : "");
|
||||
+ if (udmabuf_fd >= 0)
|
||||
+ close(udmabuf_fd);
|
||||
if (local_import_info.fd >= 0)
|
||||
close(local_import_info.fd);
|
||||
if (gbm_bo)
|
||||
diff --git a/src/venus/vkr_physical_device.c b/src/venus/vkr_physical_device.c
|
||||
index e2c3baa..69ec875 100644
|
||||
--- a/src/venus/vkr_physical_device.c
|
||||
+++ b/src/venus/vkr_physical_device.c
|
||||
@@ -207,31 +207,54 @@ vkr_physical_device_init_memory_properties(struct vkr_physical_device *physical_
|
||||
.sType = VK_STRUCTURE_TYPE_EXTERNAL_BUFFER_PROPERTIES,
|
||||
};
|
||||
|
||||
+ /* Exportable and compatible with itself is enough. Requiring the type in
|
||||
+ * exportFromImportedHandleTypes as well rules out drivers (Mali on Android)
|
||||
+ * that export fine but do not re-export imported memory.
|
||||
+ */
|
||||
if (physical_dev->EXT_external_memory_dma_buf) {
|
||||
info.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
|
||||
vk->GetPhysicalDeviceExternalBufferProperties(handle, &info, &props);
|
||||
+ const VkExternalMemoryProperties *p = &props.externalMemoryProperties;
|
||||
+ vkr_log("dma_buf buffer: features 0x%x exportFrom 0x%x compatible 0x%x",
|
||||
+ p->externalMemoryFeatures, p->exportFromImportedHandleTypes,
|
||||
+ p->compatibleHandleTypes);
|
||||
physical_dev->is_dma_buf_fd_export_supported =
|
||||
- (props.externalMemoryProperties.externalMemoryFeatures &
|
||||
- VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT) &&
|
||||
- (props.externalMemoryProperties.exportFromImportedHandleTypes &
|
||||
+ (p->externalMemoryFeatures & VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT) &&
|
||||
+ ((p->exportFromImportedHandleTypes | p->compatibleHandleTypes) &
|
||||
VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
|
||||
}
|
||||
|
||||
if (physical_dev->KHR_external_memory_fd) {
|
||||
info.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT,
|
||||
vk->GetPhysicalDeviceExternalBufferProperties(handle, &info, &props);
|
||||
+ const VkExternalMemoryProperties *p = &props.externalMemoryProperties;
|
||||
+ vkr_log("opaque_fd buffer: features 0x%x exportFrom 0x%x compatible 0x%x",
|
||||
+ p->externalMemoryFeatures, p->exportFromImportedHandleTypes,
|
||||
+ p->compatibleHandleTypes);
|
||||
physical_dev->is_opaque_fd_export_supported =
|
||||
- (props.externalMemoryProperties.externalMemoryFeatures &
|
||||
- VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT) &&
|
||||
- (props.externalMemoryProperties.exportFromImportedHandleTypes &
|
||||
+ (p->externalMemoryFeatures & VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT) &&
|
||||
+ ((p->exportFromImportedHandleTypes | p->compatibleHandleTypes) &
|
||||
VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT);
|
||||
}
|
||||
+ vkr_log("memory export: dma_buf %d opaque_fd %d",
|
||||
+ physical_dev->is_dma_buf_fd_export_supported,
|
||||
+ physical_dev->is_opaque_fd_export_supported);
|
||||
|
||||
/* fallback to gbm allocation with dma-buf import */
|
||||
+ physical_dev->android_dma_buf_import = false;
|
||||
+#ifdef __ANDROID__
|
||||
+ if (!physical_dev->is_dma_buf_fd_export_supported &&
|
||||
+ !physical_dev->is_opaque_fd_export_supported &&
|
||||
+ physical_dev->EXT_external_memory_dma_buf) {
|
||||
+ physical_dev->android_dma_buf_import = true;
|
||||
+ vkr_log("memory export: none; allocating dma-bufs and importing them");
|
||||
+ }
|
||||
+#else
|
||||
if (!physical_dev->is_dma_buf_fd_export_supported &&
|
||||
!physical_dev->is_opaque_fd_export_supported &&
|
||||
physical_dev->EXT_external_memory_dma_buf)
|
||||
physical_dev->gbm_device = vkr_physical_device_get_gbm_device();
|
||||
+#endif
|
||||
|
||||
physical_dev->udmabuf_dev_fd = -1;
|
||||
if (VKR_DEBUG(UDMABUF)) {
|
||||
@@ -703,6 +726,25 @@ vkr_dispatch_vkGetPhysicalDeviceFormatProperties2(
|
||||
args->pFormatProperties);
|
||||
}
|
||||
|
||||
+/* Android without driver export: the server allocates a dma-buf and imports
|
||||
+ * it (vkr_device_memory.c), so a dma-buf the driver can import, the guest
|
||||
+ * can export.
|
||||
+ */
|
||||
+static void
|
||||
+vkr_android_dma_buf_export_fixup(const struct vkr_physical_device *physical_dev,
|
||||
+ VkExternalMemoryHandleTypeFlagBits handle_type,
|
||||
+ VkExternalMemoryProperties *props)
|
||||
+{
|
||||
+ if (!physical_dev->android_dma_buf_import ||
|
||||
+ handle_type != VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT ||
|
||||
+ !(props->externalMemoryFeatures & VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT))
|
||||
+ return;
|
||||
+
|
||||
+ props->externalMemoryFeatures |= VK_EXTERNAL_MEMORY_FEATURE_EXPORTABLE_BIT;
|
||||
+ props->exportFromImportedHandleTypes |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
|
||||
+ props->compatibleHandleTypes |= VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT;
|
||||
+}
|
||||
+
|
||||
static void
|
||||
vkr_dispatch_vkGetPhysicalDeviceImageFormatProperties2(
|
||||
UNUSED struct vn_dispatch_context *dispatch,
|
||||
@@ -715,6 +757,20 @@ vkr_dispatch_vkGetPhysicalDeviceImageFormatProperties2(
|
||||
vn_replace_vkGetPhysicalDeviceImageFormatProperties2_args_handle(args);
|
||||
args->ret = vk->GetPhysicalDeviceImageFormatProperties2(
|
||||
args->physicalDevice, args->pImageFormatInfo, args->pImageFormatProperties);
|
||||
+
|
||||
+ const VkPhysicalDeviceExternalImageFormatInfo *external_info = vkr_find_struct(
|
||||
+ args->pImageFormatInfo->pNext,
|
||||
+ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO);
|
||||
+ VkExternalImageFormatProperties *external_props = vkr_find_struct(
|
||||
+ args->pImageFormatProperties->pNext, VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES);
|
||||
+ if (physical_dev->android_dma_buf_import && external_info && external_props) {
|
||||
+ if (args->ret == VK_SUCCESS)
|
||||
+ vkr_android_dma_buf_export_fixup(physical_dev, external_info->handleType,
|
||||
+ &external_props->externalMemoryProperties);
|
||||
+ else
|
||||
+ vkr_log("image format %d tiling %d: no external memory (%d)",
|
||||
+ args->pImageFormatInfo->format, args->pImageFormatInfo->tiling, args->ret);
|
||||
+ }
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -743,6 +799,10 @@ vkr_dispatch_vkGetPhysicalDeviceExternalBufferProperties(
|
||||
vn_replace_vkGetPhysicalDeviceExternalBufferProperties_args_handle(args);
|
||||
vk->GetPhysicalDeviceExternalBufferProperties(
|
||||
args->physicalDevice, args->pExternalBufferInfo, args->pExternalBufferProperties);
|
||||
+
|
||||
+ vkr_android_dma_buf_export_fixup(
|
||||
+ physical_dev, args->pExternalBufferInfo->handleType,
|
||||
+ &args->pExternalBufferProperties->externalMemoryProperties);
|
||||
}
|
||||
|
||||
static void
|
||||
diff --git a/src/venus/vkr_physical_device.h b/src/venus/vkr_physical_device.h
|
||||
index b0b6691..28ba0b7 100644
|
||||
--- a/src/venus/vkr_physical_device.h
|
||||
+++ b/src/venus/vkr_physical_device.h
|
||||
@@ -33,6 +33,10 @@ struct vkr_physical_device {
|
||||
bool is_opaque_fd_export_supported;
|
||||
void *gbm_device;
|
||||
int udmabuf_dev_fd;
|
||||
+ /* Android: host-visible memory is a dma-buf the server allocates (dma-buf
|
||||
+ * heap, else AHardwareBuffer) and imports, since the driver cannot export.
|
||||
+ */
|
||||
+ bool android_dma_buf_import;
|
||||
|
||||
VkQueueFamilyProperties *queue_family_properties;
|
||||
uint32_t queue_family_property_count;
|
||||
@@ -0,0 +1,30 @@
|
||||
venus: destroy a device's object mutex after the objects that lock it
|
||||
|
||||
From BattleTempest's DroidDeck-Pixel (kevinbryant551-ux/DroidDeck-Pixel, GPL-3.0),
|
||||
tools/venus/patches/0103-destroy-object-mutex-after-objects.patch. Bionic aborts on locking a
|
||||
destroyed mutex ("pthread_mutex_lock called on a destroyed mutex", seen on a Tab S9 FE G68
|
||||
and a Pixel 9a), so a client that exited holding objects took the render worker down.
|
||||
|
||||
diff --git a/src/venus/vkr_device.c b/src/venus/vkr_device.c
|
||||
index 9144180..7b7c904 100644
|
||||
--- a/src/venus/vkr_device.c
|
||||
+++ b/src/venus/vkr_device.c
|
||||
@@ -324,13 +324,16 @@ vkr_device_destroy(struct vkr_context *ctx, struct vkr_device *dev, bool destroy
|
||||
vkr_log("vkDeviceWaitIdle(%p) failed(%d)", dev, (int32_t)result);
|
||||
}
|
||||
|
||||
- mtx_destroy(&dev->object_mutex);
|
||||
-
|
||||
+ /* The leftover objects are removed under object_mutex: destroy it after them. glibc lets a
|
||||
+ * destroyed mutex be locked; bionic aborts ("pthread_mutex_lock called on a destroyed mutex"),
|
||||
+ * so every client that exited holding objects took its render worker down. */
|
||||
if (!list_is_empty(&dev->objects)) {
|
||||
list_for_each_entry_safe (struct vkr_object, obj, &dev->objects, track_head)
|
||||
vkr_device_object_destroy(ctx, dev, obj);
|
||||
}
|
||||
|
||||
+ mtx_destroy(&dev->object_mutex);
|
||||
+
|
||||
list_for_each_entry_safe (struct vkr_queue, queue, &dev->queues, base.track_head)
|
||||
vkr_queue_destroy(ctx, queue);
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
diff --git a/src/venus/venus-protocol/vn_protocol_renderer_pipeline.h b/src/venus/venus-protocol/vn_protocol_renderer_pipeline.h
|
||||
index ce82e59..8798de9 100644
|
||||
--- a/src/venus/venus-protocol/vn_protocol_renderer_pipeline.h
|
||||
+++ b/src/venus/venus-protocol/vn_protocol_renderer_pipeline.h
|
||||
@@ -3290,7 +3290,11 @@ static inline void vn_encode_vkCreateComputePipelines_reply(struct vn_cs_encoder
|
||||
static inline void vn_decode_vkDestroyPipeline_args_temp(struct vn_cs_decoder *dec, struct vn_command_vkDestroyPipeline *args)
|
||||
{
|
||||
vn_decode_VkDevice_lookup(dec, &args->device);
|
||||
- vn_decode_VkPipeline_lookup(dec, &args->pipeline);
|
||||
+ {
|
||||
+ uint64_t id;
|
||||
+ vn_decode_uint64_t(dec, &id);
|
||||
+ args->pipeline = (VkPipeline)(uintptr_t)vkr_cs_decoder_lookup_object_for_destroy((const struct vkr_cs_decoder *)dec, id, VK_OBJECT_TYPE_PIPELINE);
|
||||
+ }
|
||||
if (vn_decode_simple_pointer(dec)) {
|
||||
vn_cs_decoder_set_fatal(dec);
|
||||
} else {
|
||||
diff --git a/src/venus/vkr_cs.h b/src/venus/vkr_cs.h
|
||||
index c72c5cc..b45061f 100644
|
||||
--- a/src/venus/vkr_cs.h
|
||||
+++ b/src/venus/vkr_cs.h
|
||||
@@ -324,6 +324,32 @@ vkr_cs_decoder_lookup_object(const struct vkr_cs_decoder *dec,
|
||||
return obj;
|
||||
}
|
||||
|
||||
+/* For a destroy command only: an id the renderer never created (the driver refused the create,
|
||||
+ * which the guest may not have waited for) is logged and destroyed as VK_NULL_HANDLE, a no-op,
|
||||
+ * instead of marking the whole context fatal - that took the vtest server down (exit 137) when
|
||||
+ * gamescope destroyed a compute pipeline a Mali-G615 had refused. */
|
||||
+static inline struct vkr_object *
|
||||
+vkr_cs_decoder_lookup_object_for_destroy(const struct vkr_cs_decoder *dec,
|
||||
+ vkr_object_id id,
|
||||
+ VkObjectType type)
|
||||
+{
|
||||
+ struct vkr_object *obj;
|
||||
+
|
||||
+ if (!id)
|
||||
+ return NULL;
|
||||
+
|
||||
+ mtx_lock(dec->object_mutex);
|
||||
+ const struct hash_entry *entry =
|
||||
+ _mesa_hash_table_search((struct hash_table *)dec->object_table, &id);
|
||||
+ obj = likely(entry) ? entry->data : NULL;
|
||||
+ mtx_unlock(dec->object_mutex);
|
||||
+ if (unlikely(!obj || obj->type != type)) {
|
||||
+ vkr_log("destroy of unknown object %" PRIu64 " of type %d ignored", id, type);
|
||||
+ return NULL;
|
||||
+ }
|
||||
+ return obj;
|
||||
+}
|
||||
+
|
||||
static inline void
|
||||
vkr_cs_decoder_reset_temp_pool(struct vkr_cs_decoder *dec)
|
||||
{
|
||||
@@ -0,0 +1,19 @@
|
||||
#!/system/bin/sh
|
||||
# Keeps the memory map of a running game process in the app's cache directory, so a crash address
|
||||
# in Wine's log (handle_syscall_fault pc=...) can be matched to a library afterwards.
|
||||
# On the phone, as the app (from Git Bash, with MSYS_NO_PATHCONV=1):
|
||||
# adb push tools/venus/watch-maps.sh /data/local/tmp/
|
||||
# adb shell "run-as com.droiddeck.launcher sh /data/local/tmp/watch-maps.sh 'Hollow Knight Silksong.exe'"
|
||||
# Runs 15 minutes. Then: adb exec-out "run-as com.droiddeck.launcher cat cache/game-maps.txt" > maps.txt
|
||||
name=${1:?process name, as ps shows it}
|
||||
cd /data/user/0/com.droiddeck.launcher || exit 1
|
||||
rm -f cache/game-maps.txt
|
||||
i=0
|
||||
while [ $i -lt 900 ]; do
|
||||
p=$(ps -A -o PID,NAME | grep -F "$name" | grep -v Crash | head -1 | awk '{print $1}')
|
||||
if [ -n "$p" ]; then
|
||||
cat /proc/$p/maps > cache/game-maps.tmp 2>/dev/null && mv cache/game-maps.tmp cache/game-maps.txt
|
||||
fi
|
||||
sleep 1
|
||||
i=$((i + 1))
|
||||
done
|
||||
Reference in new issue
Block a user