Skip to content

Conversation

@victzhan
Copy link
Contributor

@victzhan victzhan commented Jan 9, 2026

Motivation

HIP GL interop fails when applications switch GL contexts (e.g., multi-window or context recreation). The existing std::call_once approach only sets up interop once and cannot handle context changes.

Technical Details

Commit 1: Code style fixes - typos, [[fallthrough]], nullptr, constexpr, base class cast in hipGraphicsUnregisterResource
Commit 2: Improve flag handling
Add convertHipGraphicsFlagsToCL() to properly map HIP flags to CL memory flags
Use bitmask validation to reject invalid flag combinations
Commit 3: Thread-safe GL context tracking
Track associated GL context, detect changes, and re-associate automatically
GLInteropGuard RAII class with shared_mutex (read-write lock pattern)
Cache GL function pointers to avoid library handle mismatches
Apply guard to all GL interop APIs

JIRA ID

Resolves SWDEV-559353

Test Plan

Verified GL interop works with context switches
Tested multi-threaded GL interop operations
Existing GL interop tests pass

Test Result

All tests passed

Submission Checklist

victzhan_amdeng added 3 commits January 8, 2026 22:56
…e code style in GL interop

- Fix spelling errors in comments (implmented, suppoerted, numbers)
- Replace NULL with nullptr
- Use constexpr instead of static const for compile-time constant
- Change while(1) to while(true) for clarity
- Add [[fallthrough]] attributes to suppress compiler warnings
- Cast to amd::Memory* base class in hipGraphicsUnregisterResource
  since resource can be either BufferGL or ImageGL
- Improve error log messages for consistency
… CL flag conversion

- Add convertHipGraphicsFlagsToCL() helper to properly map HIP graphics
  register flags to OpenCL memory flags (read-only, write-only, read-write)
- Replace individual flag checks with bitmask validation approach
  to correctly reject invalid flag combinations
- Pass converted CL flags to BufferGL and ImageGL constructors instead
  of raw HIP flags, ensuring proper memory access semantics
- Remove unused gliMapped variable
…c re-association

Problem: HIP GL interop used std::call_once for one-time GL context setup,
which failed when applications switch GL contexts (e.g., multi-window or
context recreation scenarios).

Solution:
- Track the associated GL context and detect when it changes
- Use shared_mutex (read-write lock) for thread safety:
  - Shared lock for fast path when context is stable (parallel access)
  - Exclusive lock for slow path during context switches
- Add GLInteropGuard RAII class that validates/re-associates GL context
  at the start of each GL interop operation
- Cache GL function pointers using std::call_once to avoid repeated
  library lookups and handle library loading edge cases
- Pass actual GL context/display values in properties to handle cases
  where glenv is already initialized from a previous setup

The guard is applied to all GL interop APIs:
- hipGLGetDevices
- hipGraphicsSubResourceGetMappedArray
- hipGraphicsGLRegisterImage
- hipGraphicsGLRegisterBuffer
- hipGraphicsMapResources
- hipGraphicsResourceGetMappedPointer
- hipGraphicsUnmapResources

Thread-safety note: HIP GL interop operations are now thread-safe with
respect to each other. Applications must not call wglMakeCurrent/
glXMakeCurrent concurrently with HIP GL interop operations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants