Skip to content

Commit ab515db

Browse files
Copilotphilstopford
andcommitted
Fix OpenGL backend by restoring original VeldridDriver.cs and improve Vulkan widget embedding by removing problematic ShowAll() and premature visibility calls
Co-authored-by: philstopford <1983851+philstopford@users.noreply.github.com>
1 parent d9b4055 commit ab515db

File tree

2 files changed

+15
-121
lines changed

2 files changed

+15
-121
lines changed

Eto/Eto.Veldrid.Gtk/GtkVeldridSurfaceHandler.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ public GtkVeldridSurfaceHandler()
5555
return null;
5656
}
5757

58-
// Only ensure native window if we're properly embedded and the widget is visible
59-
if (drawingArea.Window != null && drawingArea.Parent != null && drawingArea.Visible)
60-
{
61-
Console.WriteLine("[DEBUG] Creating native window for Vulkan operations");
62-
X11Interop.gdk_window_ensure_native(drawingArea.Window.Handle);
63-
}
64-
6558
// Get display info for debugging
6659
var gdkDisplay = drawingArea.Display.Handle;
6760
bool isWayland = X11Interop.IsWaylandDisplay(gdkDisplay);
@@ -189,7 +182,7 @@ private void glArea_InitializeGraphicsBackend(object? sender, EventArgs e)
189182

190183
private void DrawingArea_InitializeGraphicsBackend(object? sender, EventArgs e)
191184
{
192-
Console.WriteLine("[DEBUG] DrawingArea realized and ready for graphics initialization");
185+
Console.WriteLine("[DEBUG] DrawingArea mapped and ready for graphics initialization");
193186

194187
// Validate proper widget embedding before proceeding
195188
if (drawingArea?.Parent == null)
@@ -198,6 +191,13 @@ private void DrawingArea_InitializeGraphicsBackend(object? sender, EventArgs e)
198191
return;
199192
}
200193

194+
// Only ensure native window after widget is properly embedded
195+
if (drawingArea?.Window != null)
196+
{
197+
X11Interop.gdk_window_ensure_native(drawingArea.Window.Handle);
198+
Console.WriteLine("[DEBUG] Ensured native window for DrawingArea");
199+
}
200+
201201
// Get display info for debugging
202202
var gdkDisplay = drawingArea!.Display.Handle;
203203
bool isWayland = X11Interop.IsWaylandDisplay(gdkDisplay);
@@ -422,12 +422,12 @@ private void TriggerVulkanDraw()
422422
// Set minimum size to ensure proper widget allocation
423423
drawingArea.SetSizeRequest(100, 100);
424424

425-
// Ensure proper widget properties for embedding
425+
// Ensure native window creation for proper Vulkan surface access
426+
// This is critical for Wayland support
426427
drawingArea.AppPaintable = true;
427428

428-
// Use Realized event instead of Mapped to ensure proper widget hierarchy
429-
// Realized occurs after the widget is properly embedded in its parent
430-
drawingArea.Realized += DrawingArea_InitializeGraphicsBackend;
429+
// Use Map event for initialization - ensures widget is visible and ready for graphics operations
430+
drawingArea.Mapped += DrawingArea_InitializeGraphicsBackend;
431431

432432
Console.WriteLine("[DEBUG] Created DrawingArea for Vulkan backend");
433433
return drawingArea;

Eto/Eto.VeldridSurface/VeldridDriver.cs

Lines changed: 3 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,6 @@ public void SetUpVeldrid()
4949

5050
private void CreateResources()
5151
{
52-
// Check if swapchain is available - if not, defer resource creation
53-
if (Surface?.Swapchain == null)
54-
{
55-
Console.WriteLine("[DEBUG] Swapchain not available yet, deferring resource creation");
56-
// For Wayland systems, the swapchain might not be available immediately
57-
// This is normal and expected - we'll retry when VeldridInitialized is called again
58-
return;
59-
}
60-
61-
// Add debugging for graphics device backend
62-
Console.WriteLine($"[DEBUG] Graphics device backend type: {Surface.GraphicsDevice?.BackendType}");
63-
Console.WriteLine($"[DEBUG] Expected backend type: {Surface.Backend}");
64-
65-
// Check for backend mismatch
66-
if (Surface.GraphicsDevice?.BackendType != Surface.Backend)
67-
{
68-
Console.WriteLine($"[ERROR] Backend mismatch! Expected: {Surface.Backend}, Got: {Surface.GraphicsDevice?.BackendType}");
69-
throw new InvalidOperationException($"Graphics device backend mismatch. Expected: {Surface.Backend}, Got: {Surface.GraphicsDevice?.BackendType}");
70-
}
71-
7252
// Veldrid.SPIRV is an additional library that complements Veldrid
7353
// by simplifying the development of cross-backend shaders, and is
7454
// currently the recommended approach to doing so:
@@ -81,14 +61,6 @@ private void CreateResources()
8161
byte[] vertexShaderSpirvBytes = LoadSpirvBytes(ShaderStages.Vertex);
8262
byte[] fragmentShaderSpirvBytes = LoadSpirvBytes(ShaderStages.Fragment);
8363

84-
Console.WriteLine($"[DEBUG] Vertex shader bytes loaded: {vertexShaderSpirvBytes?.Length ?? 0} bytes");
85-
Console.WriteLine($"[DEBUG] Fragment shader bytes loaded: {fragmentShaderSpirvBytes?.Length ?? 0} bytes");
86-
87-
if (vertexShaderSpirvBytes == null || fragmentShaderSpirvBytes == null)
88-
{
89-
throw new InvalidOperationException("Failed to load shader bytecode");
90-
}
91-
9264
CrossCompileOptions? options = new();
9365
switch (Surface!.GraphicsDevice!.BackendType)
9466
{
@@ -136,54 +108,7 @@ private void CreateResources()
136108

137109
ShaderDescription vertex = new(ShaderStages.Vertex, vertexShaderSpirvBytes, "main", true);
138110
ShaderDescription fragment = new(ShaderStages.Fragment, fragmentShaderSpirvBytes, "main", true);
139-
140-
Console.WriteLine($"[DEBUG] About to create shaders using CreateFromSpirv for backend: {Surface.GraphicsDevice.BackendType}");
141-
Console.WriteLine($"[DEBUG] ResourceFactory type: {factory.GetType().FullName}");
142-
143-
Shader[] shaders;
144-
try
145-
{
146-
// Test if SPIRV compilation is available by attempting a simple operation
147-
Console.WriteLine("[DEBUG] Testing SPIRV library availability...");
148-
149-
shaders = factory.CreateFromSpirv(vertex, fragment, options);
150-
Console.WriteLine($"[DEBUG] Successfully created {shaders.Length} shaders using SPIRV");
151-
}
152-
catch (DllNotFoundException dllEx)
153-
{
154-
Console.WriteLine($"[ERROR] SPIRV native library not found: {dllEx.Message}");
155-
Console.WriteLine("[ERROR] This is likely due to missing libveldrid-spirv.so");
156-
Console.WriteLine("[ERROR] Falling back to alternative shader creation method...");
157-
158-
// For now, throw a more descriptive error
159-
throw new InvalidOperationException(
160-
"SPIRV shader compilation is not available. This is likely due to missing native dependencies (libveldrid-spirv.so). " +
161-
"Please ensure the Veldrid.SPIRV native library is properly installed.", dllEx);
162-
}
163-
catch (ArgumentNullException argEx)
164-
{
165-
Console.WriteLine($"[ERROR] Null argument in shader creation: {argEx.Message}");
166-
Console.WriteLine($"[ERROR] This might be due to backend mismatch or SPIRV compilation failure");
167-
168-
// Check if the bytes are actually null
169-
Console.WriteLine($"[DEBUG] Vertex shader bytes null: {vertexShaderSpirvBytes == null}");
170-
Console.WriteLine($"[DEBUG] Fragment shader bytes null: {fragmentShaderSpirvBytes == null}");
171-
172-
throw new InvalidOperationException(
173-
$"Shader creation failed with null bytes. Backend: {Surface.GraphicsDevice.BackendType}, " +
174-
$"Vertex bytes: {vertexShaderSpirvBytes?.Length ?? 0}, Fragment bytes: {fragmentShaderSpirvBytes?.Length ?? 0}", argEx);
175-
}
176-
catch (Exception ex)
177-
{
178-
Console.WriteLine($"[ERROR] Failed to create shaders: {ex.GetType().Name}: {ex.Message}");
179-
Console.WriteLine($"[ERROR] Stack trace: {ex.StackTrace}");
180-
181-
// Provide more context in the error message
182-
throw new InvalidOperationException(
183-
$"Shader creation failed for {Surface.GraphicsDevice.BackendType} backend. " +
184-
$"This might be due to missing native dependencies or backend compatibility issues. " +
185-
$"Original error: {ex.GetType().Name}: {ex.Message}", ex);
186-
}
111+
Shader[] shaders = factory.CreateFromSpirv(vertex, fragment, options);
187112

188113
ResourceLayout modelMatrixLayout = factory.CreateResourceLayout(
189114
new ResourceLayoutDescription(
@@ -218,23 +143,6 @@ private void CreateResources()
218143
create_pipelines(ref factory, ref viewMatrixLayout, ref modelMatrixLayout, ref shaders, ref vertexLayout);
219144

220145
CommandList = factory.CreateCommandList();
221-
222-
// VeldridDriver resources successfully created - no debug message needed
223-
}
224-
225-
public void CompleteResourceCreation()
226-
{
227-
// This method can be called when the swapchain becomes available
228-
// to complete resource creation that was deferred
229-
if (Surface?.Swapchain != null && !Ready)
230-
{
231-
Console.WriteLine("[DEBUG] Completing deferred VeldridDriver resource creation");
232-
CreateResources();
233-
if (CommandList != null)
234-
{
235-
Ready = true;
236-
}
237-
}
238146
}
239147

240148
private void create_pipelines(ref ResourceFactory factory, ref ResourceLayout viewMatrixLayout,
@@ -309,30 +217,16 @@ private byte[] LoadSpirvBytes(ShaderStages stage)
309217
string name = $"VertexColor-{stage.ToString().ToLowerInvariant()}.450.glsl";
310218
string full = $"Eto.VeldridSurface.shaders.{name}";
311219

312-
Console.WriteLine($"[DEBUG] Loading shader: {name} from resource: {full}");
313-
314220
// Precompiled SPIR-V bytecode can speed up program start by saving
315221
// the need to load text files and compile them before converting
316222
// the result to the final backend shader format. If they're not
317223
// available, though, the plain .glsl files will do just fine. Look
318224
// up glslangValidator to learn how to compile SPIR-V binary files.
319225

320226
using (Stream? stream = GetType().Assembly.GetManifestResourceStream(full))
227+
using (BinaryReader? reader = new(stream!))
321228
{
322-
if (stream == null)
323-
{
324-
Console.WriteLine($"[ERROR] Could not find shader resource: {full}");
325-
throw new InvalidOperationException($"Could not find shader resource: {full}");
326-
}
327-
328-
Console.WriteLine($"[DEBUG] Shader resource found, length: {stream.Length} bytes");
329-
330-
using (BinaryReader? reader = new(stream))
331-
{
332-
byte[] result = reader.ReadBytes((int)stream.Length);
333-
Console.WriteLine($"[DEBUG] Successfully loaded {result.Length} bytes for {stage} shader");
334-
return result;
335-
}
229+
return reader.ReadBytes((int)stream!.Length);
336230
}
337231
}
338232
}

0 commit comments

Comments
 (0)