forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThreadedGraphicsContext.cs
727 lines (632 loc) · 18.1 KB
/
ThreadedGraphicsContext.cs
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#region Copyright & License Information
/*
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Runtime.ExceptionServices;
using System.Threading;
using OpenRA.Graphics;
using OpenRA.Primitives;
namespace OpenRA.Platforms.Default
{
/// <summary>
/// Creates a dedicated thread for the graphics device. An internal message queue is used to perform actions on the
/// device. This allows calls to be enqueued to be processed asynchronously and thus free up the calling thread.
/// </summary>
sealed class ThreadedGraphicsContext : IGraphicsContext
{
// PERF: Maintain several object pools to reduce allocations.
readonly Stack<Vertex[]> verticesPool = new Stack<Vertex[]>();
readonly Stack<Message> messagePool = new Stack<Message>();
readonly Queue<Message> messages = new Queue<Message>();
public readonly int BatchSize;
readonly object syncObject = new object();
readonly Thread renderThread;
volatile ExceptionDispatchInfo messageException;
// Delegates that perform actions on the real device.
Func<object> doClear;
Action doClearDepthBuffer;
Action doDisableDepthBuffer;
Action doEnableDepthBuffer;
Action doDisableScissor;
Action doPresent;
Func<string> getGLVersion;
Func<ITexture> getCreateTexture;
Func<object, IFrameBuffer> getCreateFrameBuffer;
Func<object, IShader> getCreateShader;
Func<object, IVertexBuffer<Vertex>> getCreateVertexBuffer;
Action<object> doDrawPrimitives;
Action<object> doEnableScissor;
Action<object> doSetBlendMode;
Action<object> doSetVSync;
public ThreadedGraphicsContext(Sdl2GraphicsContext context, int batchSize)
{
BatchSize = batchSize;
renderThread = new Thread(RenderThread)
{
Name = "ThreadedGraphicsContext RenderThread",
IsBackground = true
};
lock (syncObject)
{
// Start and wait for the rendering thread to have initialized before returning.
// Otherwise, the delegates may not have been set yet.
renderThread.Start(context);
Monitor.Wait(syncObject);
}
}
void RenderThread(object contextObject)
{
using (var context = (Sdl2GraphicsContext)contextObject)
{
// This lock allows the constructor to block until initialization completes.
lock (syncObject)
{
context.InitializeOpenGL();
doClear = () => { context.Clear(); return null; };
doClearDepthBuffer = () => context.ClearDepthBuffer();
doDisableDepthBuffer = () => context.DisableDepthBuffer();
doEnableDepthBuffer = () => context.EnableDepthBuffer();
doDisableScissor = () => context.DisableScissor();
doPresent = () => context.Present();
getGLVersion = () => context.GLVersion;
getCreateTexture = () => new ThreadedTexture(this, (ITextureInternal)context.CreateTexture());
getCreateFrameBuffer =
tuple =>
{
var t = (ValueTuple<Size, Color>)tuple;
return new ThreadedFrameBuffer(this,
context.CreateFrameBuffer(t.Item1, (ITextureInternal)CreateTexture(), t.Item2));
};
getCreateShader = name => new ThreadedShader(this, context.CreateShader((string)name));
getCreateVertexBuffer = length => new ThreadedVertexBuffer(this, context.CreateVertexBuffer((int)length));
doDrawPrimitives =
tuple =>
{
var t = (ValueTuple<PrimitiveType, int, int>)tuple;
context.DrawPrimitives(t.Item1, t.Item2, t.Item3);
};
doEnableScissor =
tuple =>
{
var t = (ValueTuple<int, int, int, int>)tuple;
context.EnableScissor(t.Item1, t.Item2, t.Item3, t.Item4);
};
doSetBlendMode = mode => { context.SetBlendMode((BlendMode)mode); };
doSetVSync = enabled => { context.SetVSyncEnabled((bool)enabled); };
Monitor.Pulse(syncObject);
}
// Run a message loop.
// Only this rendering thread can perform actions on the real device,
// so other threads must send us a message which we process here.
Message message;
while (true)
{
lock (messages)
{
if (messages.Count == 0)
{
if (messageException != null)
break;
Monitor.Wait(messages);
}
message = messages.Dequeue();
}
if (message == null)
break;
message.Execute();
}
}
}
internal Vertex[] GetVertices(int size)
{
lock (verticesPool)
if (size <= BatchSize && verticesPool.Count > 0)
return verticesPool.Pop();
return new Vertex[size < BatchSize ? BatchSize : size];
}
internal void ReturnVertices(Vertex[] vertices)
{
if (vertices.Length == BatchSize)
lock (verticesPool)
verticesPool.Push(vertices);
}
class Message
{
public Message(ThreadedGraphicsContext device)
{
this.device = device;
}
readonly AutoResetEvent completed = new AutoResetEvent(false);
readonly ThreadedGraphicsContext device;
volatile Action action;
volatile Action<object> actionWithParam;
volatile Func<object> func;
volatile Func<object, object> funcWithParam;
volatile object param;
volatile object result;
volatile ExceptionDispatchInfo edi;
public void SetAction(Action method)
{
action = method;
}
public void SetAction(Action<object> method, object state)
{
actionWithParam = method;
param = state;
}
public void SetAction(Func<object> method)
{
func = method;
}
public void SetAction(Func<object, object> method, object state)
{
funcWithParam = method;
param = state;
}
public void Execute()
{
var wasSend = action != null || actionWithParam != null;
try
{
if (action != null)
{
action();
result = null;
action = null;
}
else if (actionWithParam != null)
{
actionWithParam(param);
result = null;
actionWithParam = null;
param = null;
}
else if (func != null)
{
result = func();
func = null;
}
else
{
result = funcWithParam(param);
funcWithParam = null;
param = null;
}
}
catch (Exception ex)
{
edi = ExceptionDispatchInfo.Capture(ex);
if (wasSend)
device.messageException = edi;
result = null;
param = null;
action = null;
actionWithParam = null;
func = null;
funcWithParam = null;
}
if (wasSend)
{
lock (device.messagePool)
device.messagePool.Push(this);
}
else
{
completed.Set();
}
}
public object Result()
{
completed.WaitOne();
var localEdi = edi;
edi = null;
var localResult = result;
result = null;
localEdi?.Throw();
return localResult;
}
}
Message GetMessage()
{
lock (messagePool)
if (messagePool.Count > 0)
return messagePool.Pop();
return new Message(this);
}
void QueueMessage(Message message)
{
var exception = messageException;
exception?.Throw();
lock (messages)
{
messages.Enqueue(message);
if (messages.Count == 1)
Monitor.Pulse(messages);
}
}
object RunMessage(Message message)
{
QueueMessage(message);
var result = message.Result();
lock (messagePool)
messagePool.Push(message);
return result;
}
/// <summary>
/// Sends a message to the rendering thread.
/// This method blocks until the message is processed, and returns the result.
/// </summary>
public T Send<T>(Func<T> method) where T : class
{
if (renderThread == Thread.CurrentThread)
return method();
var message = GetMessage();
message.SetAction(method);
return (T)RunMessage(message);
}
/// <summary>
/// Sends a message to the rendering thread.
/// This method blocks until the message is processed, and returns the result.
/// </summary>
public T Send<T>(Func<object, T> method, object state) where T : class
{
if (renderThread == Thread.CurrentThread)
return method(state);
var message = GetMessage();
message.SetAction(method, state);
return (T)RunMessage(message);
}
/// <summary>
/// Posts a message to the rendering thread.
/// This method then returns immediately and does not wait for the message to be processed.
/// </summary>
public void Post(Action method)
{
if (renderThread == Thread.CurrentThread)
{
method();
return;
}
var message = GetMessage();
message.SetAction(method);
QueueMessage(message);
}
/// <summary>
/// Posts a message to the rendering thread.
/// This method then returns immediately and does not wait for the message to be processed.
/// </summary>
public void Post(Action<object> method, object state)
{
if (renderThread == Thread.CurrentThread)
{
method(state);
return;
}
var message = GetMessage();
message.SetAction(method, state);
QueueMessage(message);
}
public void Dispose()
{
// Use a null message to signal the rendering thread to clean up, then wait for it to complete.
QueueMessage(null);
renderThread.Join();
}
public string GLVersion
{
get
{
return Send(getGLVersion);
}
}
public void Clear()
{
// We send the clear even though we could just post it.
// This ensures all previous messages have been processed before we return.
// This prevents us from queuing up work faster than it can be processed if rendering is behind.
Send(doClear);
}
public void ClearDepthBuffer()
{
Post(doClearDepthBuffer);
}
public IFrameBuffer CreateFrameBuffer(Size s)
{
return Send(getCreateFrameBuffer, (s, Color.FromArgb(0)));
}
public IFrameBuffer CreateFrameBuffer(Size s, Color clearColor)
{
return Send(getCreateFrameBuffer, (s, clearColor));
}
public IShader CreateShader(string name)
{
return Send(getCreateShader, name);
}
public ITexture CreateTexture()
{
return Send(getCreateTexture);
}
public IVertexBuffer<Vertex> CreateVertexBuffer(int length)
{
return Send(getCreateVertexBuffer, length);
}
public void DisableDepthBuffer()
{
Post(doDisableDepthBuffer);
}
public void DisableScissor()
{
Post(doDisableScissor);
}
public void DrawPrimitives(PrimitiveType type, int firstVertex, int numVertices)
{
Post(doDrawPrimitives, (type, firstVertex, numVertices));
}
public void EnableDepthBuffer()
{
Post(doEnableDepthBuffer);
}
public void EnableScissor(int left, int top, int width, int height)
{
Post(doEnableScissor, (left, top, width, height));
}
public void Present()
{
Post(doPresent);
}
public void SetBlendMode(BlendMode mode)
{
Post(doSetBlendMode, mode);
}
public void SetVSyncEnabled(bool enabled)
{
Post(doSetVSync, enabled);
}
}
class ThreadedFrameBuffer : IFrameBuffer
{
readonly ThreadedGraphicsContext device;
readonly Func<ITexture> getTexture;
readonly Action bind;
readonly Action unbind;
readonly Action dispose;
readonly Action<object> enableScissor;
readonly Action disableScissor;
public ThreadedFrameBuffer(ThreadedGraphicsContext device, IFrameBuffer frameBuffer)
{
this.device = device;
getTexture = () => frameBuffer.Texture;
bind = frameBuffer.Bind;
unbind = frameBuffer.Unbind;
dispose = frameBuffer.Dispose;
enableScissor = rect => frameBuffer.EnableScissor((Rectangle)rect);
disableScissor = frameBuffer.DisableScissor;
}
public ITexture Texture
{
get
{
return device.Send(getTexture);
}
}
public void Bind()
{
device.Post(bind);
}
public void Unbind()
{
device.Post(unbind);
}
public void EnableScissor(Rectangle rect)
{
device.Post(enableScissor, rect);
}
public void DisableScissor()
{
device.Post(disableScissor);
}
public void Dispose()
{
device.Post(dispose);
}
}
class ThreadedVertexBuffer : IVertexBuffer<Vertex>
{
readonly ThreadedGraphicsContext device;
readonly Action bind;
readonly Action<object> setData1;
readonly Action<object> setData2;
readonly Func<object, object> setData3;
readonly Action dispose;
public ThreadedVertexBuffer(ThreadedGraphicsContext device, IVertexBuffer<Vertex> vertexBuffer)
{
this.device = device;
bind = vertexBuffer.Bind;
setData1 = tuple => { var t = (ValueTuple<Vertex[], int>)tuple; vertexBuffer.SetData(t.Item1, t.Item2); device.ReturnVertices(t.Item1); };
setData2 = tuple => { var t = (ValueTuple<Vertex[], int, int, int>)tuple; vertexBuffer.SetData(t.Item1, t.Item2, t.Item3, t.Item4); device.ReturnVertices(t.Item1); };
setData3 = tuple => { setData2(tuple); return null; };
dispose = vertexBuffer.Dispose;
}
public void Bind()
{
device.Post(bind);
}
public void SetData(Vertex[] vertices, int length)
{
var buffer = device.GetVertices(length);
Array.Copy(vertices, buffer, length);
device.Post(setData1, (buffer, length));
}
public void SetData(Vertex[] vertices, int offset, int start, int length)
{
if (length <= device.BatchSize)
{
// If we are able to use a buffer without allocation, post a message to avoid blocking.
var buffer = device.GetVertices(length);
Array.Copy(vertices, offset, buffer, 0, length);
device.Post(setData2, (buffer, 0, start, length));
}
else
{
// If the length is too large for a buffer, send a message and block to avoid allocations.
device.Send(setData3, (vertices, offset, start, length));
}
}
public void Dispose()
{
device.Post(dispose);
}
}
class ThreadedTexture : ITextureInternal
{
readonly ThreadedGraphicsContext device;
readonly uint id;
readonly Func<object> getScaleFilter;
readonly Action<object> setScaleFilter;
readonly Func<object> getSize;
readonly Action<object> setEmpty;
readonly Func<byte[]> getData;
readonly Func<object, object> setData1;
readonly Action<object> setData2;
readonly Func<object, object> setData3;
readonly Action dispose;
public ThreadedTexture(ThreadedGraphicsContext device, ITextureInternal texture)
{
this.device = device;
id = texture.ID;
getScaleFilter = () => texture.ScaleFilter;
setScaleFilter = value => texture.ScaleFilter = (TextureScaleFilter)value;
getSize = () => texture.Size;
setEmpty = tuple => { var t = (ValueTuple<int, int>)tuple; texture.SetEmpty(t.Item1, t.Item2); };
getData = () => texture.GetData();
setData1 = colors => { texture.SetData((uint[,])colors); return null; };
setData2 = tuple => { var t = (ValueTuple<byte[], int, int>)tuple; texture.SetData(t.Item1, t.Item2, t.Item3); };
setData3 = tuple => { setData2(tuple); return null; };
dispose = texture.Dispose;
}
public uint ID
{
get
{
return id;
}
}
public TextureScaleFilter ScaleFilter
{
get
{
return (TextureScaleFilter)device.Send(getScaleFilter);
}
set
{
device.Post(setScaleFilter, value);
}
}
public Size Size
{
get
{
return (Size)device.Send(getSize);
}
}
public void SetEmpty(int width, int height)
{
device.Post(setEmpty, (width, height));
}
public byte[] GetData()
{
return device.Send(getData);
}
public void SetData(uint[,] colors)
{
// We can't return until we are finished with the data, so we must Send here.
device.Send(setData1, colors);
}
public void SetData(byte[] colors, int width, int height)
{
// Objects 85000 bytes or more will be directly allocated in the Large Object Heap (LOH).
// https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/large-object-heap
if (colors.Length < 85000)
{
// If we are able to create a small array the GC can collect easily, post a message to avoid blocking.
var temp = new byte[colors.Length];
Array.Copy(colors, temp, temp.Length);
device.Post(setData2, (temp, width, height));
}
else
{
// If the length is large and would result in an array on the Large Object Heap (LOH),
// send a message and block to avoid LOH allocation as this requires a Gen2 collection.
device.Send(setData3, (colors, width, height));
}
}
public void Dispose()
{
device.Post(dispose);
}
}
class ThreadedShader : IShader
{
readonly ThreadedGraphicsContext device;
readonly Action prepareRender;
readonly Action<object> setBool;
readonly Action<object> setMatrix;
readonly Action<object> setTexture;
readonly Action<object> setVec1;
readonly Action<object> setVec2;
readonly Action<object> setVec3;
readonly Action<object> setVec4;
public ThreadedShader(ThreadedGraphicsContext device, IShader shader)
{
this.device = device;
prepareRender = shader.PrepareRender;
setBool = tuple => { var t = (ValueTuple<string, bool>)tuple; shader.SetBool(t.Item1, t.Item2); };
setMatrix = tuple => { var t = (ValueTuple<string, float[]>)tuple; shader.SetMatrix(t.Item1, t.Item2); };
setTexture = tuple => { var t = (ValueTuple<string, ITexture>)tuple; shader.SetTexture(t.Item1, t.Item2); };
setVec1 = tuple => { var t = (ValueTuple<string, float>)tuple; shader.SetVec(t.Item1, t.Item2); };
setVec2 = tuple => { var t = (ValueTuple<string, float[], int>)tuple; shader.SetVec(t.Item1, t.Item2, t.Item3); };
setVec3 = tuple => { var t = (ValueTuple<string, float, float>)tuple; shader.SetVec(t.Item1, t.Item2, t.Item3); };
setVec4 = tuple => { var t = (ValueTuple<string, float, float, float>)tuple; shader.SetVec(t.Item1, t.Item2, t.Item3, t.Item4); };
}
public void PrepareRender()
{
device.Post(prepareRender);
}
public void SetBool(string name, bool value)
{
device.Post(setBool, (name, value));
}
public void SetMatrix(string param, float[] mtx)
{
device.Post(setMatrix, (param, mtx));
}
public void SetTexture(string param, ITexture texture)
{
device.Post(setTexture, (param, texture));
}
public void SetVec(string name, float x)
{
device.Post(setVec1, (name, x));
}
public void SetVec(string name, float[] vec, int length)
{
device.Post(setVec2, (name, vec, length));
}
public void SetVec(string name, float x, float y)
{
device.Post(setVec3, (name, x, y));
}
public void SetVec(string name, float x, float y, float z)
{
device.Post(setVec4, (name, x, y, z));
}
}
}