|
| 1 | +# Eto.Veldrid.Gtk Backend Switching Implementation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This implementation provides a clean, modular architecture for switching between OpenGL and Vulkan backends in the Eto.Veldrid.Gtk project. The solution supports both X11 and Wayland windowing systems and retains all existing functionality. |
| 6 | + |
| 7 | +## Architecture |
| 8 | + |
| 9 | +### Core Components |
| 10 | + |
| 11 | +1. **`IVeldridBackendHandler`** - Interface for backend-specific implementations |
| 12 | + - `CreateWidget()` - Creates appropriate GTK widget (GLArea or EventBox) |
| 13 | + - `CreateSwapchain()` - Creates backend-specific swapchain |
| 14 | + - `InitializeGraphicsDevice()` - Initializes graphics device |
| 15 | + - `HandleResize()` - Handles window resize events |
| 16 | + - `Invalidate()` - Forces redraw/invalidation |
| 17 | + - `SetupEventHandlers()` - Sets up GTK event handling |
| 18 | + |
| 19 | +2. **`OpenGLBackendHandler`** - OpenGL implementation |
| 20 | + - Uses GTK's `GLArea` widget |
| 21 | + - Manages OpenGL context lifecycle |
| 22 | + - Implements `VeldridSurface.IOpenGL` interface |
| 23 | + - Handles context switching between GTK and Veldrid worker threads |
| 24 | + |
| 25 | +3. **`VulkanBackendHandler`** - Vulkan implementation |
| 26 | + - Uses `EtoEventBox` widget for raw surface creation |
| 27 | + - Supports both X11 and Wayland windowing systems |
| 28 | + - Runtime detection of windowing system |
| 29 | + - Creates appropriate SwapchainSource for each platform |
| 30 | + |
| 31 | +4. **`WindowingSystemDetector`** - Runtime environment detection |
| 32 | + - Detects X11 vs Wayland vs XWayland |
| 33 | + - Uses environment variables and GDK display type detection |
| 34 | + - Cached results for performance |
| 35 | + |
| 36 | +5. **`VeldridBackendFactory`** - Backend management |
| 37 | + - Creates appropriate backend handlers |
| 38 | + - Checks backend support |
| 39 | + - Determines preferred backend (Vulkan > OpenGL) |
| 40 | + |
| 41 | +6. **`GtkVeldridSurfaceHandler`** - Updated main handler |
| 42 | + - Delegates to backend-specific implementations |
| 43 | + - Maintains compatibility with existing Eto interface |
| 44 | + - Handles lifecycle management |
| 45 | + |
| 46 | +## Usage |
| 47 | + |
| 48 | +### Command Line Backend Selection |
| 49 | + |
| 50 | +The TestEtoVeldrid.Gtk application now supports backend selection via command line: |
| 51 | + |
| 52 | +```bash |
| 53 | +# Use default/preferred backend (Vulkan if available, otherwise OpenGL) |
| 54 | +./TestEtoVeldrid.Gtk |
| 55 | + |
| 56 | +# Force OpenGL backend |
| 57 | +./TestEtoVeldrid.Gtk OpenGL |
| 58 | + |
| 59 | +# Force Vulkan backend |
| 60 | +./TestEtoVeldrid.Gtk Vulkan |
| 61 | +``` |
| 62 | + |
| 63 | +### Programmatic Backend Selection |
| 64 | + |
| 65 | +```csharp |
| 66 | +// Create VeldridSurface with specific backend |
| 67 | +var surface = new VeldridSurface(GraphicsBackend.Vulkan); |
| 68 | + |
| 69 | +// Check backend support |
| 70 | +bool vulkanSupported = GraphicsDevice.IsBackendSupported(GraphicsBackend.Vulkan); |
| 71 | +bool openglSupported = GraphicsDevice.IsBackendSupported(GraphicsBackend.OpenGL); |
| 72 | + |
| 73 | +// Use preferred backend |
| 74 | +var defaultSurface = new VeldridSurface(); // Uses VeldridSurface.PreferredBackend |
| 75 | +``` |
| 76 | + |
| 77 | +## Platform Support |
| 78 | + |
| 79 | +### Windowing Systems |
| 80 | + |
| 81 | +- **X11** - Full support for both OpenGL (GLArea) and Vulkan (Xlib surfaces) |
| 82 | +- **Wayland** - Full support for both OpenGL (GLArea) and Vulkan (Wayland surfaces) |
| 83 | +- **XWayland** - Detected and treated as Wayland for optimal compatibility |
| 84 | + |
| 85 | +### Backend Support |
| 86 | + |
| 87 | +- **OpenGL** - Uses GTK's GLArea widget, requires OpenGL 3.3+ |
| 88 | +- **Vulkan** - Uses raw surface creation with platform-specific extensions |
| 89 | + |
| 90 | +## Input Handling |
| 91 | + |
| 92 | +All existing input handling is preserved: |
| 93 | + |
| 94 | +- **Mouse Input**: Scroll to zoom, right-click context menu, left-click drag |
| 95 | +- **Keyboard Input**: WASD panning, zoom controls, viewport lock (F key) |
| 96 | +- **Viewport Controls**: Zoom extents, selection, etc. |
| 97 | + |
| 98 | +The backend abstraction layer ensures input events are properly routed regardless of the graphics backend in use. |
| 99 | + |
| 100 | +## Key Improvements |
| 101 | + |
| 102 | +### 1. Clean Architecture |
| 103 | +- Separation of concerns between OpenGL and Vulkan code paths |
| 104 | +- Pluggable backend system for future extensions |
| 105 | +- Clear interfaces and responsibilities |
| 106 | + |
| 107 | +### 2. Cross-Platform Compatibility |
| 108 | +- Runtime windowing system detection |
| 109 | +- Proper Wayland support alongside X11 |
| 110 | +- Automatic fallback mechanisms |
| 111 | + |
| 112 | +### 3. Preserved Functionality |
| 113 | +- All existing features work unchanged |
| 114 | +- Input handling preserved across all backends |
| 115 | +- Backward compatibility maintained |
| 116 | + |
| 117 | +### 4. Backend Switching |
| 118 | +- Runtime backend selection |
| 119 | +- Command-line configuration |
| 120 | +- Fallback to supported backends |
| 121 | + |
| 122 | +## Implementation Details |
| 123 | + |
| 124 | +### OpenGL Backend (`OpenGLBackendHandler`) |
| 125 | + |
| 126 | +- Creates `GLArea` widget with OpenGL 3.3 requirement |
| 127 | +- Manages OpenGL context switching between GTK main thread and Veldrid worker thread |
| 128 | +- Implements proper context handoff for rendering operations |
| 129 | +- Handles depth/stencil buffer configuration |
| 130 | + |
| 131 | +### Vulkan Backend (`VulkanBackendHandler`) |
| 132 | + |
| 133 | +- Creates `EtoEventBox` for raw surface access |
| 134 | +- Detects runtime windowing system (X11/Wayland) |
| 135 | +- Creates appropriate SwapchainSource: |
| 136 | + - X11: Uses `SwapchainSource.CreateXlib()` |
| 137 | + - Wayland: Uses `SwapchainSource.CreateWayland()` |
| 138 | +- Handles Vulkan graphics device creation |
| 139 | + |
| 140 | +### Windowing System Detection (`WindowingSystemDetector`) |
| 141 | + |
| 142 | +Detection logic: |
| 143 | +1. Check `WAYLAND_DISPLAY` and `DISPLAY` environment variables |
| 144 | +2. Detect XWayland (both variables set) |
| 145 | +3. Check `XDG_SESSION_TYPE` environment variable |
| 146 | +4. Inspect GDK display type |
| 147 | +5. Default to X11 if uncertain |
| 148 | + |
| 149 | +## Compatibility |
| 150 | + |
| 151 | +### Backward Compatibility |
| 152 | +- All existing code using `VeldridSurface` continues to work unchanged |
| 153 | +- No breaking changes to public APIs |
| 154 | +- Default behavior maintained (uses preferred backend) |
| 155 | + |
| 156 | +### WPF Projects |
| 157 | +- No changes made to Eto.Veldrid.Wpf or TestEtoVeldrid.Wpf |
| 158 | +- WPF implementation remains isolated and unaffected |
| 159 | +- Cross-platform code sharing maintained |
| 160 | + |
| 161 | +## Testing |
| 162 | + |
| 163 | +The implementation builds successfully and provides: |
| 164 | +- Clean compilation with only nullable reference warnings (existing codebase style) |
| 165 | +- Proper backend handler creation and lifecycle management |
| 166 | +- Runtime windowing system detection |
| 167 | +- Command-line backend selection in test application |
| 168 | + |
| 169 | +Note: Full runtime testing requires a graphics environment with proper Vulkan/OpenGL drivers, which may not be available in containerized CI environments. |
| 170 | + |
| 171 | +## Future Enhancements |
| 172 | + |
| 173 | +The modular architecture enables future enhancements: |
| 174 | +- Additional backend support (e.g., DirectX on Windows via Wine) |
| 175 | +- Backend-specific optimizations |
| 176 | +- Advanced windowing system features |
| 177 | +- Performance profiling and metrics |
0 commit comments