Skip to content

cuda: enable CUDA feature extraction on Windows (MSYS2/MinGW)#1472

Open
birkdev wants to merge 2 commits intoNetflix:masterfrom
birkdev:windows-cuda-support
Open

cuda: enable CUDA feature extraction on Windows (MSYS2/MinGW)#1472
birkdev wants to merge 2 commits intoNetflix:masterfrom
birkdev:windows-cuda-support

Conversation

@birkdev
Copy link
Copy Markdown

@birkdev birkdev commented Mar 16, 2026

Summary

Enables building libvmaf with -Denable_cuda=true on Windows using MSYS2/MinGW, closing the gap described in #1154.

On Windows, nvcc requires MSVC's cl.exe as its host compiler for preprocessing .cu files, even when the rest of the project is built with MinGW GCC. This creates two categories of issues that this PR addresses:

Source portability (commit 1): Headers included by .cu files contain constructs that cl.exe cannot process — <pthread.h> (POSIX-only), <ffnvcodec/dynlink_*.h> (installed in MinGW paths), C99 designated initializers (not supported by nvcc in C++ mode with MSVC). These are resolved with #ifdef DEVICE_CODE / #ifndef __CUDACC__ guards that are no-ops on Linux.

Build system (commit 2): The meson build auto-detects cl.exe via vswhere (without polluting PATH, which would cause meson to pick MSVC as the default compiler), discovers MSVC and Windows SDK include directories, and passes them to nvcc as -I flags since cl.exe runs outside a vcvars environment.

Prerequisites on Windows

  • MSYS2 with MinGW GCC toolchain
  • NVIDIA CUDA Toolkit (provides nvcc, bin2c)
  • nv-codec-headers built from git master (commit 876af32 or later). The latest release (n13.0.19.0) is missing several CudaFunctions members that vmaf uses (cuMemFreeHost, cuStreamCreateWithPriority, cuLaunchHostFunc, etc.). This is a pre-existing issue, not specific to this PR.
  • MSVC Build Tools (provides cl.exe, needed by nvcc for preprocessing)
  • Windows SDK (provides UCRT headers)

Test results

Tested on Windows 11 with:

  • CUDA Toolkit 13.2
  • MSVC Build Tools (VS 18), Windows 11 SDK
  • MinGW GCC 15.2.0 (MSYS2)
  • RTX 5090

The full pipeline works end-to-end:

  • meson setup -Denable_cuda=true configures successfully
  • ninja compiles all 7 .cu files to fatbin, library links
  • FFmpeg libvmaf_cuda filter scores at 1,135 fps on 1080p60 content (37-minute video scored in 2 minutes)

Transparency note

This patch was developed with assistance from Claude (Anthropic's AI), as reflected in the Co-Authored-By lines. All changes were manually tested on real hardware and real video content.

Closes #1154

🤖 Generated with Claude Code

birkdev and others added 2 commits March 16, 2026 03:09
On Windows, nvcc uses MSVC's cl.exe as its host compiler for
preprocessing. Several headers included by .cu files contain
MinGW/GCC-specific constructs that cl.exe cannot process:

- Remove unnecessary #include <pthread.h> from cuda/common.h.
  No declarations in this header use pthread types; consumers
  that need pthread (ring_buffer.c, libvmaf.c) include it directly.

- In cuda_helper.cuh, use <cuda.h> (from CUDA toolkit) instead of
  <ffnvcodec/dynlink_cuda.h> (from MinGW) for the DEVICE_CODE path.
  Device code only needs CUDA driver API types, not the dynamic
  loader machinery.

- In picture.h, use <cuda.h> and forward-declare VmafCudaState for
  the DEVICE_CODE path, avoiding ffnvcodec and libvmaf_cuda.h which
  pull in host-only dependencies.

- Guard C99 designated initializers in integer_adm.h with
  #ifndef __CUDACC__, as nvcc compiles .cu files as C++ where
  this syntax is not portable across host compilers.

- Guard #include "feature_collector.h" in ADM .cu files with
  #ifndef DEVICE_CODE. This header is host-only (contains pthread
  usage) and is never referenced by device kernel code.

These changes are no-ops on Linux where nvcc uses GCC (which handles
all of the above natively). They enable CUDA compilation on Windows
where nvcc must use MSVC's cl.exe.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Enable CUDA feature extraction on Windows (MSYS2/MinGW) by handling
the nvcc + MSVC toolchain requirements in the meson build:

- Auto-detect MSVC cl.exe via vswhere without adding it to PATH
  (which would cause meson to pick MSVC as the default C compiler
  instead of GCC). Pass it to nvcc via -ccbin.

- Discover MSVC and Windows SDK include directories and pass them
  as -I flags to nvcc, since cl.exe runs outside a vcvars
  environment and cannot find system headers otherwise.

- Add -D_USE_MATH_DEFINES so MSVC's math.h exposes M_PI.

On Linux, all new code paths are skipped (guarded by
host_machine.system() == 'windows').

Tested with CUDA 13.2, MSVC Build Tools (VS 18), Windows 11 SDK,
and MinGW GCC 15.2.0 on MSYS2. The full pipeline (nvcc -> fatbin ->
bin2c -> static lib -> shared lib) completes successfully, and the
resulting libvmaf integrates with FFmpeg's libvmaf_cuda filter.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@kylophone
Copy link
Copy Markdown
Collaborator

I believe nv-codec-headers should work on Windows.

@birkdev
Copy link
Copy Markdown
Author

birkdev commented Mar 16, 2026

You're right, nv-codec-headers works fine on Windows for the host code (compiled by GCC/MinGW). The <cuda.h> substitution only applies to the DEVICE_CODE path, where nvcc uses MSVC's cl.exe for preprocessing. cl.exe can't find headers installed in MinGW's include paths, so we use <cuda.h> from the CUDA toolkit for the device code path instead.

Separately, the CUDA code in vmaf requires nv-codec-headers built from git master (specifically commit 876af32 "add a few missing functions"), not the latest release n13.0.19.0. The release is missing several CudaFunctions members that vmaf uses (cuMemFreeHost, cuStreamCreateWithPriority, cuLaunchHostFunc, etc.). This is a pre-existing dependency issue, not specific to this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Request: CUDA feature extraction for Windows

2 participants