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
RenderSurfaceHost.Gpu.cs
1using System.Drawing;
9
10namespace Gondwana.Rendering;
11
12public sealed partial class RenderSurfaceHost<TBackbuffer>
13 where TBackbuffer : BackbufferBase
14{
47 private void RenderToBackbufferGpuFull(long tick, BackbufferBase? destination = null)
48 {
49 var target = destination ?? Backbuffer;
50 long telemetryGeneration = Telemetry?.BeginSample() ?? 0;
51 bool legacyDiagnostics = GpuRenderFrameDiagnosticsCalculated is not null;
52 bool collectDiagnostics = legacyDiagnostics || telemetryGeneration != 0;
53 var metrics = new GpuFrameMetrics
54 {
55 TelemetryGeneration = telemetryGeneration,
56 LegacyDiagnostics = legacyDiagnostics,
57 CollectDiagnostics = collectDiagnostics,
58 StartTick = collectDiagnostics ? HighResTimer.GetCurrentTick() : 0,
59 LayerDiagnostics = legacyDiagnostics
60 ? new List<GpuLayerRenderDiagnostics>(Scene.VisibleSceneLayers.Count)
61 : null
62 };
63
64 // When there are no views at all, clear the whole surface and bail.
65 // If there ARE views but no scene layers, we fall through so view-mode
66 // DirectDrawings (e.g. a splash screen overlay) are still rendered.
67 if (ViewManager.Views.Count == 0)
68 {
69 target.ClearRect(
70 new Rectangle(
71 0,
72 0,
73 target.Width,
74 target.Height));
75
76 Scene.FullRefreshNeeded = false;
77
78 if (metrics.TelemetryGeneration != 0 && destination is not RecordingBackbuffer)
79 {
80 long diagnosticsEndTick = HighResTimer.GetCurrentTick();
81
82 Telemetry!.Record(
83 metrics.TelemetryGeneration,
84 "build.cpu.ms",
85 HighResTimer.GetDuration(metrics.StartTick, diagnosticsEndTick) * 1000d);
86 }
87
88 return;
89 }
90
91 // A single clear lets translucent, wiped, or translated views reveal the
92 // views beneath them. Each view paints its own background as part of its
93 // presentation group below.
94 target.ClearRect(
95 new Rectangle(
96 0,
97 0,
98 target.Width,
99 target.Height));
100
101 foreach (var view in ViewManager.Views)
102 RenderGpuView(view, tick, target, ref metrics);
103
104 Scene.FullRefreshNeeded = false;
105
106 // Desktop snapshots record these hooks on the Engine thread; WebGL invokes
107 // them synchronously against its GPU canvas.
108 InvokePostSceneCanvasHooks(target);
109
110 if (metrics.CollectDiagnostics)
111 PublishGpuFrameDiagnostics(destination, in metrics);
112 }
113
114 private void RenderGpuView(View view, long tick, BackbufferBase target, ref GpuFrameMetrics metrics)
115 {
116 RenderContext.Push(view, tick);
117
118 try
119 {
120 // 1) View overlays are drawn unconditionally by this full-frame path.
121 // They must not enqueue dirty regions that GL rendering never consumes.
122 var overlays = DirectDrawingManager.Instance.GetDrawingsForView(view);
123
124 var vp = view.GetRenderViewportTargetRectPx();
125
126 // 2) Clip to this view's viewport, excluding areas covered by higher Z-order views.
127 BeginViewportClip(view, vp, target);
128
129 int viewPresentation = BeginPresentation(
130 view.GetPresentationBoundsPx(),
131 view.EffectOpacity,
132 view.EffectReveal,
133 view.EffectRevealDirection,
134 target);
135
136 if (viewPresentation > 0)
137 {
138 // The view background belongs inside the group so it fades,
139 // wipes, and slides with the rest of the view.
140 target.ClearRect(view.GetPresentationBoundsPx().ToPixelAlignedRect());
141
142 // 4) Render every visible layer for the full viewport extent (layers are drawn
143 // back-to-front by ascending Z-order, which VisibleSceneLayers already provides).
144 var sceneLayers = Scene.VisibleSceneLayers;
145
146 for (int i = 0; i < sceneLayers.Count; i++)
147 {
148 RenderGpuLayer(view, sceneLayers[i], i, vp, target, ref metrics);
149 }
150
151 // 5) Render view-based DirectDrawings on top.
152 long overlayStartTick = metrics.CollectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
153
154 for (int i = 0; i < overlays.Count; i++)
155 overlays[i].Draw(target, overlays[i].GetDrawLocationScreen(view));
156
157 long overlayEndTick = metrics.CollectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
158
159 if (metrics.CollectDiagnostics)
160 metrics.OverlayTicks += overlayEndTick - overlayStartTick;
161
162 EndPresentation(viewPresentation, target);
163 }
164
165 target.Canvas.Restore();
166 }
167 finally
168 {
169 RenderContext.Pop();
170 }
171 }
172
173 private void RenderGpuLayer(
174 View view,
175 SceneLayer layer,
176 int layerIndex,
177 Rectangle vp,
178 BackbufferBase target,
179 ref GpuFrameMetrics metrics)
180 {
181 int layerPresentation = BeginPresentation(
182 GetLayerPresentationBounds(view, layer),
183 layer.EffectOpacity,
184 layer.EffectReveal,
185 layer.EffectRevealDirection,
186 target);
187
188 if (layerPresentation == 0)
189 return;
190
191 var (queryStartTick, queryEndTick, layerSortTicks, drawStartTick, drawEndTick, layerDrawableCount, layerTileCount) =
192 DrawGpuLayerContent(view, layer, vp, target, metrics.CollectDiagnostics);
193
194 if (metrics.CollectDiagnostics)
195 {
196 long layerQueryAndSortTicks = queryEndTick - queryStartTick;
197
198 long layerQueryTicks = Math.Max(
199 0L,
200 layerQueryAndSortTicks - layerSortTicks);
201
202 long layerDrawTicks = drawEndTick - drawStartTick;
203 metrics.QueryAndSortTicks += layerQueryAndSortTicks;
204 metrics.QueryTicks += layerQueryTicks;
205 metrics.SortTicks += layerSortTicks;
206 metrics.DrawTicks += layerDrawTicks;
207 metrics.DrawableCount += layerDrawableCount;
208 metrics.TileCount += layerTileCount;
209
210 if (metrics.TelemetryGeneration != 0 && layerIndex < Diagnostics.RenderTelemetry.Layers.Length)
211 {
212 var keys = Diagnostics.RenderTelemetry.Layers[layerIndex];
213
214 Telemetry!.Record(
215 metrics.TelemetryGeneration,
216 keys[0],
217 HighResTimer.GetDuration(0, layerQueryTicks) * 1000d);
218
219 Telemetry.Record(
220 metrics.TelemetryGeneration,
221 keys[1],
222 HighResTimer.GetDuration(0, layerDrawTicks) * 1000d);
223
224 Telemetry.Record(metrics.TelemetryGeneration, keys[2], layerDrawableCount);
225 Telemetry.Record(metrics.TelemetryGeneration, keys[3], layerTileCount);
226 Telemetry.Record(metrics.TelemetryGeneration, keys[4], layer.TransformedTiles.Count);
227 Telemetry.Record(metrics.TelemetryGeneration, keys[5], layer.TileWidth);
228 Telemetry.Record(metrics.TelemetryGeneration, keys[6], layer.TileHeight);
229 Telemetry.Record(metrics.TelemetryGeneration, keys[7], layer.ZOrder);
230 }
231
232 metrics.LayerDiagnostics?.Add(
234 layerIndex,
235 layer.ID,
236 layer.ZOrder,
237 layerDrawableCount,
238 layerTileCount,
239 layer.TransformedTiles.Count,
240 layer.TileWidth,
241 layer.TileHeight,
242 HighResTimer.GetDuration(0, layerQueryAndSortTicks) * 1000d,
243 HighResTimer.GetDuration(0, layerDrawTicks) * 1000d));
244 }
245
246 EndPresentation(layerPresentation, target);
247 }
248
249 private static (long QueryStartTick, long QueryEndTick, long SortTicks, long DrawStartTick, long DrawEndTick,
250 int DrawableCount, int TileCount) DrawGpuLayerContent(
251 View view,
252 SceneLayer layer,
253 Rectangle vp,
254 BackbufferBase target,
255 bool collectDiagnostics)
256 {
257 // Compute the world-space rect visible through this viewport for this layer,
258 // expanded by one tile in each direction to cover boundary rounding.
259 var layerWorldRectF = view.ScreenRectToWorldRect(layer, vp);
260 layerWorldRectF.Inflate(layer.TileWidth, layer.TileHeight);
261 var layerWorldRect = layerWorldRectF.ToPixelAlignedRect();
262
263 long queryStartTick =
264 collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
265
266 long layerSortTicks = 0;
267 int layerDrawableCount;
268 int layerTileCount;
269
270 var recording = target as RecordingBackbuffer;
271 RecordingBackbuffer.FixedGridRenderPlan fixedGridPlan = default;
272
273 bool streamed = recording is not null &&
274 recording.TryPrepareFixedGridLayer(
275 layer,
276 layerWorldRect,
277 out fixedGridPlan);
278
279 long queryEndTick;
280 long drawStartTick;
281 long drawEndTick;
282
283 if (streamed)
284 {
285 queryEndTick =
286 collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
287
288 drawStartTick =
289 collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
290
291 layerTileCount = recording!.DrawFixedGridLayer(
292 view,
293 fixedGridPlan,
294 vp);
295
296 layerDrawableCount = layerTileCount;
297
298 drawEndTick =
299 collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
300 }
301 else
302 {
303 List<IDrawable> drawables;
304
305 if (collectDiagnostics)
306 {
307 drawables = layer.GetDrawablesInWorldRectWithDiagnostics(
308 layerWorldRect,
309 out layerSortTicks);
310 }
311 else
312 {
313 drawables = layer.GetDrawablesInWorldRect(layerWorldRect);
314 }
315
316 queryEndTick =
317 collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
318
319 layerDrawableCount = drawables.Count;
320 layerTileCount = 0;
321
322 if (collectDiagnostics)
323 {
324 for (int d = 0; d < drawables.Count; d++)
325 {
326 IDrawable drawable = drawables[d] is WrappedDrawable wrapped
327 ? wrapped.Owner
328 : drawables[d];
329
330 if (drawable is Tile)
331 layerTileCount++;
332 }
333 }
334
335 drawStartTick =
336 collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
337
338 target.DrawDrawables(view, drawables, vp);
339
340 drawEndTick =
341 collectDiagnostics ? HighResTimer.GetCurrentTick() : 0;
342 }
343
344 return (queryStartTick, queryEndTick, layerSortTicks, drawStartTick, drawEndTick, layerDrawableCount, layerTileCount);
345 }
346
347 private void PublishGpuFrameDiagnostics(BackbufferBase? destination, in GpuFrameMetrics metrics)
348 {
349 long diagnosticsEndTick = HighResTimer.GetCurrentTick();
350
351 if (metrics.TelemetryGeneration != 0)
352 {
353 if (destination is not RecordingBackbuffer)
354 Telemetry!.Record(
355 metrics.TelemetryGeneration,
356 "build.cpu.ms",
357 HighResTimer.GetDuration(metrics.StartTick, diagnosticsEndTick) * 1000d);
358
359 Telemetry!.Record(metrics.TelemetryGeneration, "query.cpu.ms", HighResTimer.GetDuration(0, metrics.QueryTicks) * 1000d);
360 Telemetry.Record(metrics.TelemetryGeneration, "sort.cpu.ms", HighResTimer.GetDuration(0, metrics.SortTicks) * 1000d);
361 Telemetry.Record(metrics.TelemetryGeneration, "record.cpu.ms", HighResTimer.GetDuration(0, metrics.DrawTicks) * 1000d);
362
363 Telemetry.Record(
364 metrics.TelemetryGeneration,
365 "overlay.cpu.ms",
366 HighResTimer.GetDuration(0, metrics.OverlayTicks) * 1000d);
367
368 Telemetry.Record(metrics.TelemetryGeneration, "visible.drawables", metrics.DrawableCount);
369 Telemetry.Record(metrics.TelemetryGeneration, "visible.tiles", metrics.TileCount);
370
371 Telemetry.Record(
372 metrics.TelemetryGeneration,
373 "layers.omitted",
374 Math.Max(0, Scene.VisibleSceneLayers.Count - Diagnostics.RenderTelemetry.Layers.Length));
375
376 if (Scene.VisibleSceneLayers.Count > Diagnostics.RenderTelemetry.Layers.Length)
377 Telemetry.MarkTruncated(metrics.TelemetryGeneration);
378 }
379
380 if (!metrics.LegacyDiagnostics)
381 return;
382
383 var diagnostics = new GpuRenderFrameDiagnostics(
384 HighResTimer.GetDuration(metrics.StartTick, diagnosticsEndTick) * 1000d,
385 HighResTimer.GetDuration(0, metrics.QueryAndSortTicks) * 1000d,
386 HighResTimer.GetDuration(0, metrics.DrawTicks) * 1000d,
387 HighResTimer.GetDuration(0, metrics.OverlayTicks) * 1000d,
388 metrics.DrawableCount,
389 metrics.TileCount,
390 metrics.LayerDiagnostics!)
391 {
392 QueryMilliseconds = HighResTimer.GetDuration(0, metrics.QueryTicks) * 1000d,
393 SortMilliseconds = HighResTimer.GetDuration(0, metrics.SortTicks) * 1000d
394 };
395
396 if (destination is RecordingBackbuffer)
397 _recordingDiagnostics = diagnostics;
398 else
399 GpuRenderFrameDiagnosticsCalculated?.Invoke(diagnostics);
400 }
401
402 private struct GpuFrameMetrics
403 {
405 public bool LegacyDiagnostics;
407 public long StartTick;
408 public long QueryAndSortTicks;
409 public long QueryTicks;
410 public long SortTicks;
411 public long DrawTicks;
412 public long OverlayTicks;
413 public int DrawableCount;
414 public int TileCount;
415 public List<GpuLayerRenderDiagnostics>? LayerDiagnostics;
416 }
417}
SKCanvas Canvas
Gets the SkiaSharp canvas used for drawing operations on this backbuffer.
List< GpuLayerRenderDiagnostics >? LayerDiagnostics
RectangleF ScreenRectToWorldRect(SceneLayer layer, RectangleF screenRect)
Converts a screen-space rectangle (on the logical Backbuffer) into a world-space rectangle for the gi...
Definition View.cs:395
string ID
Gets or sets the unique identifier for this layer.
int TileHeight
Gets or sets the height of each tile in this layer, measured in pixels.
int ZOrder
Gets or sets the z-order (rendering depth) of this layer within its parent scene.
int TileWidth
Gets or sets the width of each tile in this layer, measured in pixels.
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
@ View
Performs direct drawing at the view level using screen-space coordinates.
@ SceneLayer
Performs direct drawing within a scene layer using world-space coordinates.
record GpuLayerRenderDiagnostics(int LayerIndex, string LayerId, int ZOrder, int DrawableCount, int TileCount, int TransformedTileCount, int TileWidth, int TileHeight, double QueryAndSortMilliseconds, double DrawMilliseconds)
Captures the measured work performed for one scene layer during a full-frame GPU render.
record GpuRenderFrameDiagnostics(double TotalRenderMilliseconds, double QueryAndSortMilliseconds, double DrawMilliseconds, double OverlayMilliseconds, int DrawableCount, int TileCount, IReadOnlyList< GpuLayerRenderDiagnostics > Layers)
Captures measured scene-render work for one full-frame GPU render.
@ Scene
Displays scene layer, grid-cell, and active tile-animation counts.
@ Backbuffer
Displays logical backbuffer dimensions.