Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
AvaloniaGpuRenderSurfaceControl.cs
1using System.Diagnostics;
2using Avalonia.OpenGL;
3using Avalonia.OpenGL.Controls;
6using SkiaSharp;
7
9
32public class AvaloniaGpuRenderSurfaceControl : OpenGlControlBase, IDisposable
33{
34 // GL_RGBA8 pixel format for the SkiaSharp framebuffer wrapper.
35 private const uint GlRgba8 = 0x8058;
36
37 private GRGlInterface? _glInterface;
38 private GRContext? _grContext;
39 private GpuBackbuffer? _gpuBackbuffer;
40 private readonly object _lifecycleGate = new();
41 private int _disposeRequested;
42 private int _contextFailureReported;
43
44 // Last physical dimensions used to detect resize inside OnOpenGlRender.
45 private int _lastPhysW;
46 private int _lastPhysH;
47
52
56 public RenderSurfaceHost<GpuBackbuffer> Host { get; }
57
62 {
63 var scaling = VisualRoot?.RenderScaling ?? 1.0;
64 var physW = Math.Max(1, (int)Math.Round(Bounds.Width * scaling));
65 var physH = Math.Max(1, (int)Math.Round(Bounds.Height * scaling));
66
67 Adapter = new AvaloniaGpuRenderSurfaceAdapter(physW, physH);
68 Host = new RenderSurfaceHost<GpuBackbuffer>(Adapter);
69 _gpuBackbuffer = (GpuBackbuffer)Host.Backbuffer;
70
71 // Pacing: post RequestNextFrameRendering() after each engine foreground cycle.
72 Adapter.AttachToEngine(RequestNextFrameRendering);
73 }
74
75 // ── OpenGL lifecycle callbacks (called on the GL thread by Avalonia) ─────
76
78 protected override void OnOpenGlInit(GlInterface gl)
79 {
80 // Create a SkiaSharp GRContext from the current OpenGL context.
81 // GRGlInterface.Create() auto-detects the current platform GL/EGL/WGL context
82 // (which Avalonia has made current before this callback) and loads function
83 // pointers from it without requiring any explicit proc-address lookup delegate.
84 lock (_lifecycleGate)
85 {
86 if (_disposeRequested != 0)
87 return;
88
89 _glInterface = GRGlInterface.Create();
90
91 if (_glInterface == null)
92 {
93 Trace.TraceError(
94 "Gondwana Avalonia GPU: could not create a GRGlInterface from the current OpenGL context. Check the Windows GPU backend and driver.");
95
96 return;
97 }
98
99 _grContext = GRContext.CreateGl(_glInterface);
100
101 if (_grContext == null)
102 {
103 Trace.TraceError(
104 "Gondwana Avalonia GPU: SkiaSharp could not create a GRContext from the current OpenGL context.");
105
106 _glInterface.Dispose();
107 _glInterface = null;
108 return;
109 }
110 }
111
112 var scaling = VisualRoot?.RenderScaling ?? 1.0;
113 var physW = (int)Math.Round(Bounds.Width * scaling);
114 var physH = (int)Math.Round(Bounds.Height * scaling);
115
116 if (physW <= 0 || physH <= 0)
117 return;
118
119 _lastPhysW = physW;
120 _lastPhysH = physH;
121
122 Adapter.UpdateDimensions(physW, physH);
123 _gpuBackbuffer?.EnsureInitialized(_grContext);
124 }
125
127 protected override void OnOpenGlRender(GlInterface gl, int fb)
128 {
129 long telemetryGeneration = Host?.Telemetry?.BeginSample() ?? 0;
130
131 if (Volatile.Read(ref _disposeRequested) != 0 || _gpuBackbuffer == null)
132 return;
133
134 if (_grContext == null)
135 {
136 if (Interlocked.Exchange(ref _contextFailureReported, 1) == 0)
137 Trace.TraceError("Gondwana Avalonia GPU: render requested without a SkiaSharp OpenGL context.");
138
139 return;
140 }
141
142 // Notify SkiaSharp that external GL code (Avalonia's compositor) may have
143 // modified the GL state since the last Skia draw call.
144 _grContext.ResetContext();
145
146 var scaling = VisualRoot?.RenderScaling ?? 1.0;
147 var physW = (int)Math.Round(Bounds.Width * scaling);
148 var physH = (int)Math.Round(Bounds.Height * scaling);
149
150 if (physW <= 0 || physH <= 0)
151 {
152 _lastPhysW = _lastPhysH = 0;
153 Adapter.UpdateDimensions(0, 0);
154 return;
155 }
156
157 // Resize presentation resources only; the logical render target remains unchanged.
158 if (physW != _lastPhysW || physH != _lastPhysH)
159 {
160 _lastPhysW = physW;
161 _lastPhysH = physH;
162 Adapter.UpdateDimensions(physW, physH);
163 }
164
165 _gpuBackbuffer.EnsureInitialized(_grContext);
166
167 // Wrap the Avalonia-provided framebuffer in a SkiaSharp surface for compositing.
168 // The framebuffer uses OpenGL bottom-left origin, RGBA8 color format.
169 var fbInfo = new GRGlFramebufferInfo((uint)fb, GlRgba8);
170
171 using var renderTarget = new GRBackendRenderTarget(
172 physW,
173 physH,
174 sampleCount: 0,
175 stencilBits: 0,
176 fbInfo);
177
178 using var fbSurface = SKSurface.Create(
179 _grContext,
180 renderTarget,
181 GRSurfaceOrigin.BottomLeft,
182 SKColorType.Rgba8888);
183
184 if (fbSurface == null)
185 {
186 if (Interlocked.Exchange(ref _contextFailureReported, 1) == 0)
187 Trace.TraceError("Gondwana Avalonia GPU: unable to wrap Avalonia's framebuffer as an SKSurface.");
188
189 return;
190 }
191
192 var canvas = fbSurface.Canvas;
193
194 // Replay the newest immutable Engine snapshot on GL and blit the result.
195 using var img = Host.GlRenderAndSnapshot();
196
197 if (img != null)
198 {
199 Adapter.DrawImage(canvas, img, Adapter.ClearColor);
200 }
201 else
202 {
203 canvas.Clear(Adapter.ClearColor);
204 }
205
206 _grContext.Flush();
207 _gpuBackbuffer.RecordFrame();
208
209 if (telemetryGeneration != 0)
210 Host!.Telemetry!.Record(telemetryGeneration, "presentation.count", 1);
211 }
212
214 protected override void OnOpenGlDeinit(GlInterface gl)
215 {
216 lock (_lifecycleGate)
217 {
218 // Avalonia may deinitialize and recreate GL without ending the game's
219 // logical lifecycle. Release surfaces before their owning context.
220 _gpuBackbuffer?.ReleaseContext();
221 _grContext?.Dispose();
222 _grContext = null;
223 _glInterface?.Dispose();
224 _glInterface = null;
225
226 if (_disposeRequested != 0)
227 _gpuBackbuffer?.Dispose();
228 }
229 }
230
236 public void Dispose()
237 {
238 if (Interlocked.Exchange(ref _disposeRequested, 1) != 0)
239 return;
240
241 Host.Dispose();
242 Adapter.Dispose();
243
244 lock (_lifecycleGate)
245 {
246 // If GL is already gone (or never existed), only CPU resources remain.
247 if (_grContext is null)
248 _gpuBackbuffer?.Dispose();
249 }
250
251 GC.SuppressFinalize(this);
252 }
253}
Provides a GPU-accelerated render surface adapter for Avalonia, backed by global::Avalonia....
void Dispose()
Ends the logical surface lifecycle. The hosting class calls this on shutdown; custom hosts should cal...
AvaloniaGpuRenderSurfaceAdapter Adapter
Gets the GPU render surface adapter used to drive this control.
RenderSurfaceHost< GpuBackbuffer > Host
Gets the RenderSurfaceHost<T> bound to this control.
AvaloniaGpuRenderSurfaceControl()
Initializes a new instance of the AvaloniaGpuRenderSurfaceControl class.
Provides a fully GPU-rendered backbuffer implementation using SkiaSharp and OpenGL.