-
Notifications
You must be signed in to change notification settings - Fork 27
/
ShaderWindow.cs
369 lines (332 loc) · 11.6 KB
/
ShaderWindow.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
using Bonsai.Resources;
using Bonsai.Shaders.Configuration;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using OpenTK.Input;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reactive.Subjects;
using System.Threading;
namespace Bonsai.Shaders
{
/// <summary>
/// Creates and renders a window using the specified resources and a programmable
/// shader pipeline.
/// </summary>
public class ShaderWindow : GameWindow
{
RectangleF viewport;
RectangleF scissor;
readonly List<Shader> shaders;
readonly ClearBufferMask clearMask;
readonly ResourceManager resourceManager;
readonly ShaderWindowSettings settings;
const string DefaultTitle = "Bonsai Shader Window";
static readonly RectangleF DefaultViewport = new RectangleF(0, 0, 1, 1);
static readonly object syncRoot = string.Intern("A1105A50-BBB0-4EC6-B8B2-B5EF38A9CC3E");
readonly Subject<FrameEvent> updateFrame;
readonly Subject<FrameEvent> renderFrame;
readonly bool swapSync;
event Action update;
/// <summary>
/// Initializes a new instance of the <see cref="ShaderWindow"/> class using
/// the specified window configuration settings.
/// </summary>
/// <param name="configuration">
/// The configuration settings used to initialize the shader window.
/// </param>
public ShaderWindow(ShaderWindowSettings configuration)
: this(configuration, DisplayDevice.GetDisplay(configuration.DisplayDevice) ?? DisplayDevice.Default)
{
}
private ShaderWindow(ShaderWindowSettings configuration, DisplayDevice display)
: base(configuration.Width, configuration.Height,
configuration.GraphicsMode == null ? GraphicsMode.Default : configuration.GraphicsMode.CreateGraphicsMode(),
DefaultTitle, GameWindowFlags.Default, display)
{
settings = configuration;
VSync = configuration.VSync;
swapSync = configuration.SwapSync;
ClearColor = configuration.ClearColor;
clearMask = configuration.ClearMask;
Title = configuration.Title ?? DefaultTitle;
CursorVisible = configuration.CursorVisible;
Location = configuration.Location.GetValueOrDefault(Location);
WindowBorder = configuration.WindowBorder;
WindowState = configuration.WindowState;
Viewport = DefaultViewport;
Scissor = DefaultViewport;
TargetRenderFrequency = configuration.TargetRenderFrequency;
TargetUpdateFrequency = configuration.TargetUpdateFrequency.GetValueOrDefault(
VSync != VSyncMode.Off && configuration.TargetRenderFrequency == 0
? display.RefreshRate
: configuration.TargetRenderFrequency);
updateFrame = new Subject<FrameEvent>();
renderFrame = new Subject<FrameEvent>();
resourceManager = new ResourceManager();
shaders = new List<Shader>();
}
internal IObservable<FrameEvent> UpdateFrameAsync
{
get { return updateFrame; }
}
internal IObservable<FrameEvent> RenderFrameAsync
{
get { return renderFrame; }
}
/// <summary>
/// Gets or sets the color used to clear the framebuffer before rendering.
/// </summary>
public Color ClearColor { get; set; }
/// <summary>
/// Gets or sets the active viewport for rendering, in normalized coordinates.
/// </summary>
public RectangleF Viewport
{
get { return viewport; }
set
{
viewport = value;
UpdateViewport();
}
}
/// <summary>
/// Gets or sets the active scissor box, in normalized coordinates. Any fragments
/// falling outside the scissor box will be discarded.
/// </summary>
public RectangleF Scissor
{
get { return scissor; }
set
{
scissor = value;
UpdateScissor();
}
}
/// <summary>
/// Gets the collection of shaders specifying the active render pipeline.
/// </summary>
public IEnumerable<Shader> Shaders
{
get { return shaders; }
}
/// <summary>
/// Gets the resource manager used to load and release sets of render
/// resources to the shader window.
/// </summary>
public ResourceManager ResourceManager
{
get { return resourceManager; }
}
/// <summary>
/// Gets or sets the size of the OpenGL surface in window coordinates.
/// The coordinates are specified in device-dependent pixels.
/// </summary>
public new Size ClientSize
{
get { return WindowBorder == WindowBorder.Hidden ? Size : base.ClientSize; }
set
{
if (WindowBorder == WindowBorder.Hidden)
{
Size = value;
}
else base.ClientSize = value;
}
}
/// <summary>
/// Gets or sets the width of the OpenGL surface in window coordinates.
/// The coordinates are specified in device-dependent pixels.
/// </summary>
public new int Width
{
get { return WindowBorder == WindowBorder.Hidden ? Size.Width : base.Width; }
set
{
if (WindowBorder == WindowBorder.Hidden)
{
Size = new Size(value, Size.Height);
}
else base.Width = value;
}
}
/// <summary>
/// Gets or sets the height of the OpenGL surface in window coordinates.
/// The coordinates are specified in device-dependent pixels.
/// </summary>
public new int Height
{
get { return WindowBorder == WindowBorder.Hidden ? Size.Height : base.Height; }
set
{
if (WindowBorder == WindowBorder.Hidden)
{
Size = new Size(Size.Width, value);
}
else base.Height = value;
}
}
internal void AddShader(Shader shader)
{
shaders.Add(shader);
}
internal void UpdateViewport()
{
var size = ClientSize;
UpdateViewport(size.Width, size.Height);
}
internal void UpdateViewport(float width, float height)
{
GL.Viewport(
(int)(viewport.X * width),
(int)(viewport.Y * height),
(int)(viewport.Width * width),
(int)(viewport.Height * height));
}
internal void UpdateScissor()
{
var size = ClientSize;
UpdateScissor(size.Width, size.Height);
}
internal void UpdateScissor(float width, float height)
{
GL.Scissor(
(int)(scissor.X * width),
(int)(scissor.Y * height),
(int)(scissor.Width * width),
(int)(scissor.Height * height));
}
/// <summary>
/// Queues a render command or state update.
/// </summary>
/// <param name="action">
/// The action that will execute when the next frame is rendered.
/// </param>
public void Update(Action action)
{
update += action;
}
/// <inheritdoc/>
protected override void OnLoad(EventArgs e)
{
var windowManager = new WindowManagerConfiguration(this);
var resources = new List<IResourceConfiguration>();
resources.Add(windowManager);
#pragma warning disable CS0612 // Type or member is obsolete
resources.AddRange(settings.Textures);
resources.AddRange(settings.Meshes);
resources.AddRange(settings.Shaders);
#pragma warning restore CS0612 // Type or member is obsolete
resourceManager.Load(resources);
foreach (var state in settings.RenderState)
{
state.Execute(this);
}
base.OnLoad(e);
}
/// <inheritdoc/>
protected override void OnKeyDown(KeyboardKeyEventArgs e)
{
if (e.Key == Key.F11)
{
if (WindowState == WindowState.Fullscreen)
{
WindowState = WindowState.Normal;
}
else WindowState = WindowState.Fullscreen;
}
base.OnKeyDown(e);
}
/// <inheritdoc/>
protected override void OnResize(EventArgs e)
{
UpdateViewport();
UpdateScissor();
base.OnResize(e);
}
/// <inheritdoc/>
protected override void OnUpdateFrame(FrameEventArgs e)
{
OnFrameEvent(updateFrame, TargetUpdatePeriod, e);
base.OnUpdateFrame(e);
}
/// <inheritdoc/>
protected override void OnRenderFrame(FrameEventArgs e)
{
if (clearMask != ClearBufferMask.None)
{
if (viewport != DefaultViewport) Viewport = DefaultViewport;
if (scissor != DefaultViewport) Scissor = DefaultViewport;
GL.DepthMask(true);
GL.ClearColor(ClearColor);
GL.Clear(clearMask);
}
var action = Interlocked.Exchange(ref update, null);
if (action != null)
{
action();
}
OnFrameEvent(renderFrame, TargetRenderPeriod, e);
base.OnRenderFrame(e);
shaders.RemoveAll(shader =>
{
if (shader.Program != 0)
{
shader.Dispatch();
return false;
}
return true;
});
if (!swapSync) SwapBuffers();
else lock (syncRoot) SwapBuffers();
}
private void OnFrameEvent(Subject<FrameEvent> subject, double elapsedTime, FrameEventArgs e)
{
if (subject.HasObservers)
{
var frameEvent = new FrameEvent(this, elapsedTime, e);
subject.OnNext(frameEvent);
}
}
/// <inheritdoc/>
protected override void OnClosed(EventArgs e)
{
updateFrame.OnCompleted();
renderFrame.OnCompleted();
base.OnClosed(e);
}
/// <inheritdoc/>
protected override void OnUnload(EventArgs e)
{
shaders.Clear();
resourceManager.Dispose();
base.OnUnload(e);
}
class WindowManagerConfiguration : ResourceConfiguration<WindowManager>
{
readonly ShaderWindow window;
internal WindowManagerConfiguration(ShaderWindow owner)
{
Name = string.Empty;
window = owner;
}
public override WindowManager CreateResource(ResourceManager resourceManager)
{
return new WindowManager(window);
}
}
}
class WindowManager : IDisposable
{
internal WindowManager(ShaderWindow window)
{
Window = window;
}
internal ShaderWindow Window { get; private set; }
public void Dispose()
{
}
}
}