mesa/src/vulkan/screenshot-layer
Antonio Ospite ddf2aa3a4d build: avoid redefining unreachable() which is standard in C23
In the C23 standard unreachable() is now a predefined function-like
macro in <stddef.h>

See https://android.googlesource.com/platform/bionic/+/HEAD/docs/c23.md#is-now-a-predefined-function_like-macro-in

And this causes build errors when building for C23:

-----------------------------------------------------------------------
In file included from ../src/util/log.h:30,
                 from ../src/util/log.c:30:
../src/util/macros.h:123:9: warning: "unreachable" redefined
  123 | #define unreachable(str)    \
      |         ^~~~~~~~~~~
In file included from ../src/util/macros.h:31:
/usr/lib/gcc/x86_64-linux-gnu/14/include/stddef.h:456:9: note: this is the location of the previous definition
  456 | #define unreachable() (__builtin_unreachable ())
      |         ^~~~~~~~~~~
-----------------------------------------------------------------------

So don't redefine it with the same name, but use the name UNREACHABLE()
to also signify it's a macro.

Using a different name also makes sense because the behavior of the
macro was extending the one of __builtin_unreachable() anyway, and it
also had a different signature, accepting one argument, compared to the
standard unreachable() with no arguments.

This change improves the chances of building mesa with the C23 standard,
which for instance is the default in recent AOSP versions.

All the instances of the macro, including the definition, were updated
with the following command line:

  git grep -l '[^_]unreachable(' -- "src/**" | sort | uniq | \
  while read file; \
  do \
    sed -e 's/\([^_]\)unreachable(/\1UNREACHABLE(/g' -i "$file"; \
  done && \
  sed -e 's/#undef unreachable/#undef UNREACHABLE/g' -i src/intel/isl/isl_aux_info.c

Reviewed-by: Erik Faye-Lund <erik.faye-lund@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36437>
2025-07-31 17:49:42 +00:00
..
LICENSE.txt
mesa-screenshot-control.py vulkan/screenshot-layer: Add region command option 2024-11-14 16:35:24 +00:00
meson.build meson: include VkLayer_MESA_screenshot in the devenv 2025-07-30 23:53:51 +00:00
README.rst vulkan/screenshot-layer: Add region command option 2024-11-14 16:35:24 +00:00
screenshot.cpp build: avoid redefining unreachable() which is standard in C23 2025-07-31 17:49:42 +00:00
screenshot_params.c vulkan/screenshot-layer: Add region command option 2024-11-14 16:35:24 +00:00
screenshot_params.h vulkan/screenshot-layer: Add region command option 2024-11-14 16:35:24 +00:00
VkLayer_MESA_screenshot.json.in meson: include VkLayer_MESA_screenshot in the devenv 2025-07-30 23:53:51 +00:00

A Vulkan layer to take efficient screenshots to reduce performance loss in Vulkan applications.

Building
========

The screenshot layer will be built if :code:`screenshot` is passed as a :code:`vulkan-layers` argument. For example:

.. code-block:: sh

  meson -Dvulkan-layers=device-select,screenshot builddir/
  ninja -C builddir/
  sudo ninja -C builddir/ install

See `docs/install.rst <https://gitlab.freedesktop.org/mesa/mesa/-/blob/master/docs/install.rst>`__ for more information.

Basic Usage
===========

Turn on the layer:

.. code-block:: sh

  VK_LOADER_LAYERS_ENABLE=VK_LAYER_MESA_screenshot /path/to/my_vulkan_app


List the help menu:

.. code-block:: sh

  VK_LOADER_LAYERS_ENABLE=VK_LAYER_MESA_screenshot VK_LAYER_MESA_SCREENSHOT_CONFIG=help /path/to/my_vulkan_app

Enable log output in stdout/stderr:

.. code-block:: sh

  VK_LOADER_LAYERS_ENABLE=VK_LAYER_MESA_screenshot VK_LAYER_MESA_SCREENSHOT_CONFIG=log_type=<info|debug> /path/to/my_vulkan_app

Redirect screenshots taken to a different directory:

.. code-block:: sh

  VK_LOADER_LAYERS_ENABLE=VK_LAYER_MESA_screenshot VK_LAYER_MESA_SCREENSHOT_CONFIG=output_dir=/path/to/new_dir /path/to/my_vulkan_app

Capture pre-determined screenshots:

.. code-block:: sh

  VK_LOADER_LAYERS_ENABLE=VK_LAYER_MESA_screenshot VK_LAYER_MESA_SCREENSHOT_CONFIG=frames=1/5/7/15-4-5 /path/to/my_vulkan_app

Note:
 - Individual frames are separated by '/' and must be listed before the frame range
 - The frame range is determined by <range start>-<range count>-<range interval>
 - Example: '1/5/7/15-4-5' gives individual frames [1,5,7], then the frame range gives [15,20,25,30], combining into [1,5,7,15,20,25,30]

To capture all frames:

.. code-block:: sh

  VK_LOADER_LAYERS_ENABLE=VK_LAYER_MESA_screenshot VK_LAYER_MESA_SCREENSHOT_CONFIG=frames=all /path/to/my_vulkan_app

To capture a ImageRegion:

.. code-block:: sh

  VK_LOADER_LAYERS_ENABLE=VK_LAYER_MESA_screenshot VK_LAYER_MESA_SCREENSHOT_CONFIG=region=0.20/0.25/0.60/0.75 /path/to/my_vulkan_app

Note:
 - Using a region will capture a portion of the image on the GPU, meaning the copy time to the CPU memory can be reduced significantly if using a small enough region size, relative to the original image.
 - The regions are percentages, represented by floating point values. the syntax for a region is 'region=<startX>/<startY>/<endX>/<endY>', where percentages are given for the starting width and starting height, along with ending width and ending height.
 - - Example with vkcube:

- - - Original size: 500x500 image

.. code-block:: sh

  mesa-screenshot: DEBUG: Screenshot Authorized!
  mesa-screenshot: DEBUG: Needs 2 steps
  mesa-screenshot: DEBUG: Time to copy: 123530 nanoseconds


- - - Using '0.4/0.4/0.6/0.6' region: 100x100 image

.. code-block:: sh

  mesa-screenshot: DEBUG: Screenshot Authorized!
  mesa-screenshot: DEBUG: Using region: startX = 40% (200), startY = 40% (200), endX = 60% (300), endY = 60% (300)
  mesa-screenshot: DEBUG: Needs 2 steps
  mesa-screenshot: DEBUG: Time to copy: 12679 nanoseconds

Reducing a 500x500 image to a 100x100 image caused a reduction in the copy time by a factor of 10, meaning that we spend less time impacting the frame time and are able to run the workload with a negligible performance impact from the screenshot layer.

Direct Socket Control
---------------------

Enabling communication with the client can be done with the following setup:

.. code-block:: sh

  VK_LOADER_LAYERS_ENABLE=VK_LAYER_MESA_screenshot VK_LAYER_MESA_SCREENSHOT_CONFIG=comms /path/to/my_vulkan_app

The Unix socket may be used directly if needed. Once a client connects to the socket, the screenshot layer will immediately
send the following commands to the client:

.. code-block:: sh

  :MesaScreenshotControlVersion=1;
  :DeviceName=<device name>;
  :MesaVersion=<mesa version>;

The client connected to the screenshot layer can trigger a screenshot to be taken by sending the command:

.. code-block:: sh

  :capture=<screenshot_name.png>;

Note that the screenshot name must include '.png', other image types are not supported.

To capture a region, the region information must be added to the 'region' command, along with the 'capture' command, separated by a comma:

.. code-block:: sh

  :region=0.25/0.25/0.75/0.75,capture=<screenshot_name.png>;

.. _docs/install.rst: ../../docs/install.rst