# See `NixOS.md` at section "Graphic drivers".
# There more details are given including commands to verify working hardware acceleration.

{ config, pkgs, ... }:
let
  extra = [
  ];
in
{
  hardware = {
    graphics = {
      # This is enabled by default. We have this for cosmetical reasons only.
      # It provides the RADV driver.
      enable = true;
      # Required for wine.
      enable32Bit = true;

      # Additional packages to add to the default graphics driver lookup path. This can be used to add OpenCL drivers, VA-API/VDPAU drivers, etc.
      extraPackages = extra;
      # Required for wine.
      extraPackages32 = extra;
    };

    amdgpu = {
      # Load amdgpu kernelModule in stage 1. Can fix lower resolution in boot screen during initramfs phase.
      #initrd.enable = true;

      # OpenCL (Open Computing Language) is a framework for writing programs that execute across heterogeneous platforms.
      opencl.enable = true;

      # AMD driver for Vulkan.
      #
      # => Please use RADV instead except if some Windows games don't run with it.
      # https://www.reddit.com/r/NixOS/comments/1dmkim8/comment/l9zaz75/
      # > Side note: you don't need to enable amdvlk unless you have a very specific reason to. Mesa has radv by default which is better for vulkan.
      #
      #amdvlk.enable = true;
      #amdvlk.support32Bit.enable = true;
    };
  };

  nixpkgs.config = {
    # Supported by most modern AMD GPUs.
    rocmSupport = true;
  };

  environment.variables = {
    # VDPAU does not pick up correct driver. Thus, we need to configure it manually.
    # https://wiki.archlinux.org/title/Hardware_video_acceleration#Failed_to_open_VDPAU_backend
    #
    # https://discourse.nixos.org/t/24-11-amd-gpu-how-to-use-mesa-radv-instead-of-amdvlk/57110/5
    # https://github.com/NixOS/nixpkgs/issues/221535#issuecomment-1473815585
    "VDPAU_DRIVER" = "radeonsi";
  };
}