-
Notifications
You must be signed in to change notification settings - Fork 703
Expand file tree
/
Copy pathClydeHeadless.cs
More file actions
624 lines (508 loc) · 18.3 KB
/
Copy pathClydeHeadless.cs
File metadata and controls
624 lines (508 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
using JetBrains.Annotations;
using Robust.Client.Input;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.GameObjects;
using Robust.Shared.Graphics;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using System.Threading.Tasks;
using Color = Robust.Shared.Maths.Color;
namespace Robust.Client.Graphics.Clyde
{
/// <summary>
/// Hey look, it's Clyde's evil twin brother!
/// </summary>
[UsedImplicitly]
internal sealed class ClydeHeadless : IClydeInternal
{
// Would it make sense to report a fake resolution like 720p here so code doesn't break? idk.
public bool IsInitialized { get; private set; }
public IClydeWindow MainWindow { get; }
public Vector2i ScreenSize => (1280, 720);
public IEnumerable<IClydeWindow> AllWindows => _windows;
public Vector2 DefaultWindowScale => new Vector2(1, 1);
public bool IsFocused => true;
private readonly List<IClydeWindow> _windows = new();
private int _nextWindowId = 2;
private long _nextViewportId = 1;
public ShaderInstance InstanceShader(ShaderSourceResource handle, bool? light = null, ShaderBlendMode? blend = null)
{
return new DummyShaderInstance();
}
public ClydeHeadless()
{
SixLabors.ImageSharp.Configuration.Default.PreferContiguousImageBuffers = true;
var mainRt = new DummyRenderWindow(this);
var window = new DummyWindow(mainRt) { Id = new WindowId(1) };
_windows.Add(window);
MainWindow = window;
}
public ScreenCoordinates MouseScreenPosition => default;
public IClydeDebugInfo DebugInfo { get; } = new DummyDebugInfo();
public IClydeDebugStats DebugStats { get; } = new DummyDebugStats();
public event Action<TextEnteredEventArgs>? TextEntered { add { } remove { } }
public event Action<TextEditingEventArgs>? TextEditing { add { } remove { } }
public event Action<MouseMoveEventArgs>? MouseMove { add { } remove { } }
public event Action<MouseEnterLeaveEventArgs>? MouseEnterLeave { add { } remove { } }
public event Action<KeyEventArgs>? KeyUp { add { } remove { } }
public event Action<KeyEventArgs>? KeyDown { add { } remove { } }
public event Action<MouseWheelEventArgs>? MouseWheel { add { } remove { } }
public event Action<WindowRequestClosedEventArgs>? CloseWindow { add { } remove { } }
public event Action<WindowDestroyedEventArgs>? DestroyWindow { add { } remove { } }
public Texture GetStockTexture(ClydeStockTexture stockTexture)
{
return new DummyTexture((1, 1));
}
public IEnumerable<(Clyde.ClydeTexture, Clyde.LoadedTexture)> GetLoadedTextures()
{
return [];
}
public IEnumerable<(Clyde.RenderTargetBase, Clyde.LoadedRenderTarget)> GetLoadedRenderTextures()
{
return [];
}
public ClydeDebugLayers DebugLayers { get; set; }
public string GetKeyName(Keyboard.Key key) => string.Empty;
public void Shutdown()
{
// Nada.
}
public uint? GetX11WindowId()
{
return null;
}
public void RegisterGridEcsEvents()
{
// Nada.
}
public void ShutdownGridEcsEvents()
{
}
public void SetWindowTitle(string title)
{
// Nada.
}
public void SetWindowTitleBarVisible(bool visible)
{
}
public void SetWindowMonitor(IClydeMonitor monitor)
{
// Nada.
}
public void RequestWindowAttention()
{
// Nada.
}
public event Action<WindowResizedEventArgs> OnWindowResized
{
add { }
remove { }
}
public event Action<WindowFocusedEventArgs> OnWindowFocused
{
add { }
remove { }
}
public event Action<WindowContentScaleEventArgs> OnWindowScaleChanged
{
add { }
remove { }
}
public void Render()
{
// Nada.
}
public void FrameProcess(FrameEventArgs eventArgs)
{
// Nada.
}
public void ProcessInput(FrameEventArgs frameEventArgs)
{
// Nada.
}
public bool SeparateWindowThread => false;
public bool InitializePreWindowing()
{
return true;
}
public void TerminateWindowLoop()
{
throw new InvalidOperationException("ClydeHeadless does not use windowing threads");
}
public void EnterWindowLoop()
{
throw new InvalidOperationException("ClydeHeadless does not use windowing threads");
}
public bool InitializePostWindowing()
{
IsInitialized = true;
return true;
}
public OwnedTexture LoadTextureFromPNGStream(Stream stream, string? name = null,
TextureLoadParameters? loadParams = null)
{
using (var image = Image.Load<Rgba32>(stream))
{
return LoadTextureFromImage(image, name, loadParams);
}
}
public OwnedTexture LoadTextureFromImage<T>(Image<T> image, string? name = null,
TextureLoadParameters? loadParams = null) where T : unmanaged, IPixel<T>
{
return new DummyTexture((image.Width, image.Height));
}
public OwnedTexture CreateBlankTexture<T>(
Vector2i size,
string? name = null,
in TextureLoadParameters? loadParams = null)
where T : unmanaged, IPixel<T>
{
return new DummyTexture(size);
}
/// <inheritdoc />
public Color GetClearColor(EntityUid mapUid)
{
return Color.Transparent;
}
public void BlurRenderTarget(IClydeViewport viewport, IRenderTarget target, IRenderTarget blurBuffer, IEye eye, float multiplier)
{
// NOOP
}
public IRenderTexture CreateLightRenderTarget(Vector2i size, string? name = null, bool depthStencil = true)
{
return CreateRenderTarget(size, new RenderTargetFormatParameters(RenderTargetColorFormat.R8, hasDepthStencil: depthStencil), null, name: name);
}
public IRenderTexture CreateRenderTarget(Vector2i size, RenderTargetFormatParameters format,
TextureSampleParameters? sampleParameters = null, string? name = null)
{
return new DummyRenderTexture(size, new DummyTexture(size));
}
public ICursor GetStandardCursor(StandardCursorShape shape)
{
return new DummyCursor();
}
public ICursor CreateCursor(Image<Rgba32> image, Vector2i hotSpot)
{
return new DummyCursor();
}
public void SetCursor(ICursor? cursor)
{
// Nada.
}
public void Screenshot(ScreenshotType type, CopyPixelsDelegate<Rgb24> callback, UIBox2i? subRegion = null)
{
// Immediately call callback with an empty buffer.
var (x, y) = ClydeBase.ClampSubRegion(ScreenSize, subRegion);
callback(new Image<Rgb24>(x, y));
}
public IClydeViewport CreateViewport(Vector2i size, TextureSampleParameters? sampleParameters,
string? name = null)
{
return new Viewport(_nextViewportId++, size);
}
public IEnumerable<IClydeMonitor> EnumerateMonitors()
{
// TODO: Actually return something.
yield break;
}
public IClydeWindow CreateWindow(WindowCreateParameters parameters)
{
var window = new DummyWindow(CreateRenderTarget((123, 123), default))
{
Id = new WindowId(_nextWindowId++)
};
_windows.Add(window);
return window;
}
public void TextInputSetRect(UIBox2i rect)
{
// Nada.
}
public void TextInputStart()
{
// Nada.
}
public void TextInputStop()
{
// Nada.
}
public ClydeHandle LoadShader(ParsedShader shader, string? name = null, Dictionary<string, string>? defines = null)
{
return default;
}
public void ReloadShader(ClydeHandle handle, ParsedShader newShader)
{
// Nada.
}
public void Ready()
{
// Nada.
}
public Task<string> GetText()
{
return Task.FromResult(string.Empty);
}
public void SetText(string text)
{
// Nada.
}
public void RunOnWindowThread(Action action)
{
action();
}
public IFileDialogManagerImplementation? FileDialogImpl => null;
public bool VsyncEnabled { get; set; }
#if TOOLS
public void ViewportsClearAllCached()
{
throw new NotImplementedException();
}
#endif // TOOLS
public void RenderNow(IRenderTarget renderTarget, Action<IRenderHandle> callback)
{
}
private sealed class DummyCursor : ICursor
{
public void Dispose()
{
// Nada.
}
}
private sealed class DummyTexture : OwnedTexture
{
public DummyTexture(Vector2i size) : base(size)
{
}
public override void SetSubImage<T>(Vector2i topLeft, Image<T> sourceImage, in UIBox2i sourceRegion)
{
// Just do nothing on mutate.
}
public override void SetSubImage<T>(Vector2i topLeft, Vector2i size, ReadOnlySpan<T> buffer)
{
// Just do nothing on mutate.
}
public override Color GetPixel(int x, int y)
{
return Color.Black;
}
}
private sealed class DummyShaderInstance : ShaderInstance
{
private protected override ShaderInstance DuplicateImpl()
{
return new DummyShaderInstance();
}
private protected override void SetParameterImpl(string name, float value)
{
}
private protected override void SetParameterImpl(string name, float[] value)
{
}
private protected override void SetParameterImpl(string name, Vector2 value)
{
}
private protected override void SetParameterImpl(string name, Vector2[] value)
{
}
private protected override void SetParameterImpl(string name, Vector3 value)
{
}
private protected override void SetParameterImpl(string name, Vector4 value)
{
}
private protected override void SetParameterImpl(string name, Color value)
{
}
private protected override void SetParameterImpl(string name, Color[] value)
{
}
private protected override void SetParameterImpl(string name, int value)
{
}
private protected override void SetParameterImpl(string name, Vector2i value)
{
}
private protected override void SetParameterImpl(string name, bool value)
{
}
private protected override void SetParameterImpl(string name, bool[] value)
{
}
private protected override void SetParameterImpl(string name, in Matrix3x2 value)
{
}
private protected override void SetParameterImpl(string name, in Matrix4x4 value)
{
}
private protected override void SetParameterImpl(string name, Texture value)
{
}
private protected override void SetStencilImpl(StencilParameters value)
{
}
public override void Dispose()
{
}
}
private sealed class DummyRenderTexture : IRenderTexture
{
public DummyRenderTexture(Vector2i size, Texture texture)
{
Size = size;
Texture = texture;
}
public Vector2i Size { get; }
public void CopyPixelsToMemory<T>(CopyPixelsDelegate<T> callback, UIBox2i? subRegion) where T : unmanaged, IPixel<T>
{
var (x, y) = ClydeBase.ClampSubRegion(Size, subRegion);
callback(new Image<T>(x, y));
}
public Texture Texture { get; }
public void Dispose()
{
}
}
private sealed class DummyRenderWindow : IRenderTarget
{
private readonly ClydeHeadless _clyde;
public DummyRenderWindow(ClydeHeadless clyde)
{
_clyde = clyde;
}
public Vector2i Size => _clyde.ScreenSize;
public void CopyPixelsToMemory<T>(CopyPixelsDelegate<T> callback, UIBox2i? subRegion) where T : unmanaged, IPixel<T>
{
var (x, y) = ClydeBase.ClampSubRegion(Size, subRegion);
callback(new Image<T>(x, y));
}
public void Dispose()
{
}
}
private sealed class DummyDebugStats : IClydeDebugStats
{
public int LastGLDrawCalls => 0;
public int LastClydeDrawCalls => 0;
public int LastBatches => 0;
public (int vertices, int indices) LargestBatchSize => (0, 0);
public int TotalLights => 0;
public int ShadowLights => 0;
public int Occluders => 0;
public int Entities => 0;
}
private sealed class DummyDebugInfo : IClydeDebugInfo
{
public OpenGLVersion OpenGLVersion { get; } = new(3, 3, isES: false, isCore: true);
public string Renderer => "ClydeHeadless";
public string Vendor => "Space Wizards Federation";
public string VersionString { get; } = $"3.3.0 WIZARDS {typeof(DummyDebugInfo).Assembly.GetName().Version}";
public bool Overriding => false;
public string WindowingApi => "The vast abyss";
}
private sealed class Viewport : IClydeViewport
{
public Viewport(long id, Vector2i size)
{
Size = size;
Id = id;
}
public void Dispose()
{
ClearCachedResources?.Invoke(new ClearCachedViewportResourcesEvent(Id, null));
}
public long Id { get; }
public IRenderTexture RenderTarget { get; } =
new DummyRenderTexture(Vector2i.One, new DummyTexture(Vector2i.One));
public IRenderTexture LightRenderTarget { get; } =
new DummyRenderTexture(Vector2i.One, new DummyTexture(Vector2i.One));
public IEye? Eye { get; set; }
public Vector2i Size { get; }
public event Action<ClearCachedViewportResourcesEvent>? ClearCachedResources;
public Color? ClearColor { get; set; } = Color.Black;
public bool ClearWhenMissingEye { get; set; }
public Vector2 RenderScale { get; set; }
public bool AutomaticRender { get; set; }
public void Render()
{
// Nada
}
public MapCoordinates LocalToWorld(Vector2 point)
{
return default;
}
public Matrix3x2 GetWorldToLocalMatrix() => default;
public Vector2 WorldToLocal(Vector2 point)
{
return default;
}
public void RenderScreenOverlaysBelow(
IRenderHandle handle,
IViewportControl control,
in UIBox2i viewportBounds)
{
// Nada
}
public void RenderScreenOverlaysAbove(
IRenderHandle handle,
IViewportControl control,
in UIBox2i viewportBounds)
{
// Nada
}
}
private sealed class DummyWindow : IClydeWindow
{
public DummyWindow(IRenderTarget renderTarget)
{
RenderTarget = renderTarget;
}
public Vector2i Size { get; set; } = default;
public bool IsDisposed { get; private set; }
public WindowId Id { get; set; }
public IRenderTarget RenderTarget { get; }
public string Title { get; set; } = "";
public bool IsFocused => false;
public bool IsMinimized => false;
public bool IsVisible { get; set; } = true;
public bool IsBordered { get; set; } = true;
public Vector2 ContentScale => Vector2.One;
public bool DisposeOnClose { get; set; }
public event Action<WindowRequestClosedEventArgs>? RequestClosed { add { } remove { } }
public event Action<WindowDestroyedEventArgs>? Destroyed;
public event Action<WindowResizedEventArgs>? Resized { add { } remove { } }
public void SetWindowProgress(WindowProgressState state, float value)
{
// Nop.
}
public void TextInputSetRect(UIBox2i rect, int cursor)
{
// Nop.
}
public void TextInputStart()
{
// Nop.
}
public void TextInputStop()
{
// Nop.
}
public void MaximizeOnMonitor(IClydeMonitor monitor)
{
}
public void Dispose()
{
IsDisposed = true;
Destroyed?.Invoke(new WindowDestroyedEventArgs(this));
}
}
}
}