-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathGLRenderer.cs
489 lines (385 loc) · 19.6 KB
/
GLRenderer.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
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Graphics.OpenGL.Buffers;
using osu.Framework.Graphics.OpenGL.Textures;
using osu.Framework.Graphics.OpenGL.Batches;
using osu.Framework.Graphics.OpenGL.Shaders;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Textures;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Framework.Statistics;
using osuTK;
using osuTK.Graphics.ES30;
using osuTK.Graphics;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Image = SixLabors.ImageSharp.Image;
using GL4 = osuTK.Graphics.OpenGL;
namespace osu.Framework.Graphics.OpenGL
{
internal class GLRenderer : Renderer
{
private IOpenGLGraphicsSurface openGLSurface = null!;
protected internal override bool VerticalSync
{
get => openGLSurface.VerticalSync;
set => openGLSurface.VerticalSync = value;
}
protected internal override bool AllowTearing { get; set; }
public override bool IsDepthRangeZeroToOne => false;
public override bool IsUvOriginTopLeft => false;
public override bool IsClipSpaceYInverted => false;
public bool UseStructuredBuffers { get; private set; }
/// <summary>
/// The maximum allowed render buffer size.
/// </summary>
public int MaxRenderBufferSize { get; private set; } = 4096; // default value is to allow roughly normal flow in cases we don't have a GL context, like headless CI.
/// <summary>
/// Whether the current platform is embedded.
/// </summary>
public bool IsEmbedded { get; private set; }
private int backbufferFramebuffer;
private readonly Dictionary<string, IGLUniformBuffer> boundUniformBuffers = new Dictionary<string, IGLUniformBuffer>();
private bool? lastBlendingEnabledState;
private int lastBoundVertexArray;
protected override void Initialise(IGraphicsSurface graphicsSurface)
{
if (graphicsSurface.Type != GraphicsSurfaceType.OpenGL)
throw new InvalidOperationException($"{nameof(GLRenderer)} only supports OpenGL graphics surfaces.");
openGLSurface = (IOpenGLGraphicsSurface)graphicsSurface;
openGLSurface.MakeCurrent(openGLSurface.WindowContext);
backbufferFramebuffer = openGLSurface.BackbufferFramebuffer ?? 0;
string version = GL.GetString(StringName.Version);
IsEmbedded = version.Contains("OpenGL ES"); // As defined by https://www.khronos.org/registry/OpenGL-Refpages/es2.0/xhtml/glGetString.xml
MaxTextureSize = GL.GetInteger(GetPName.MaxTextureSize);
MaxRenderBufferSize = GL.GetInteger(GetPName.MaxRenderbufferSize);
GL.Disable(EnableCap.StencilTest);
GL.Enable(EnableCap.Blend);
string extensions = GetExtensions();
Logger.Log($@"GL Initialized
GL Version: {GL.GetString(StringName.Version)}
GL Renderer: {GL.GetString(StringName.Renderer)}
GL Shader Language version: {GL.GetString(StringName.ShadingLanguageVersion)}
GL Vendor: {GL.GetString(StringName.Vendor)}
GL Extensions: {extensions}");
UseStructuredBuffers = extensions.Contains(@"GL_ARB_shader_storage_buffer_object") && !FrameworkEnvironment.NoStructuredBuffers;
Logger.Log($"{nameof(UseStructuredBuffers)}: {UseStructuredBuffers}");
openGLSurface.ClearCurrent();
}
protected virtual string GetExtensions()
{
#pragma warning disable CS0618
GL.GetInteger(All.NumExtensions, out int numExtensions);
#pragma warning restore CS0618
var extensionsBuilder = new StringBuilder();
for (int i = 0; i < numExtensions; i++)
extensionsBuilder.Append($"{GL.GetString(StringNameIndexed.Extensions, i)} ");
return extensionsBuilder.ToString().TrimEnd();
}
protected internal override void BeginFrame(Vector2 windowSize)
{
lastBlendingEnabledState = null;
lastBoundVertexArray = 0;
boundUniformBuffers.Clear();
// Seems to be required on some drivers as the context is lost from the draw thread.
MakeCurrent();
GL.UseProgram(0);
base.BeginFrame(windowSize);
}
protected internal override void WaitUntilNextFrameReady()
{
}
protected internal override void MakeCurrent() => openGLSurface.MakeCurrent(openGLSurface.WindowContext);
protected internal override void ClearCurrent() => openGLSurface.ClearCurrent();
protected internal override void SwapBuffers() => openGLSurface.SwapBuffers();
protected internal override void WaitUntilIdle() => GL.Finish();
public bool BindVertexArray(int vaoId)
{
if (lastBoundVertexArray == vaoId)
return false;
lastBoundVertexArray = vaoId;
GL.BindVertexArray(vaoId);
FrameStatistics.Increment(StatisticsCounterType.VBufBinds);
return true;
}
protected override void SetShaderImplementation(IShader shader) => GL.UseProgram((GLShader)shader);
protected override void SetUniformImplementation<T>(IUniformWithValue<T> uniform)
{
switch (uniform)
{
case IUniformWithValue<bool> b:
GL.Uniform1(uniform.Location, b.GetValue() ? 1 : 0);
break;
case IUniformWithValue<int> i:
GL.Uniform1(uniform.Location, i.GetValue());
break;
case IUniformWithValue<float> f:
GL.Uniform1(uniform.Location, f.GetValue());
break;
case IUniformWithValue<Vector2> v2:
GL.Uniform2(uniform.Location, ref v2.GetValueByRef());
break;
case IUniformWithValue<Vector3> v3:
GL.Uniform3(uniform.Location, ref v3.GetValueByRef());
break;
case IUniformWithValue<Vector4> v4:
GL.Uniform4(uniform.Location, ref v4.GetValueByRef());
break;
case IUniformWithValue<Matrix2> m2:
GL.UniformMatrix2(uniform.Location, false, ref m2.GetValueByRef());
break;
case IUniformWithValue<Matrix3> m3:
GL.UniformMatrix3(uniform.Location, false, ref m3.GetValueByRef());
break;
case IUniformWithValue<Matrix4> m4:
GL.UniformMatrix4(uniform.Location, false, ref m4.GetValueByRef());
break;
}
}
protected override void SetUniformBufferImplementation(string blockName, IUniformBuffer buffer)
=> boundUniformBuffers[blockName] = (IGLUniformBuffer)buffer;
protected override bool SetTextureImplementation(INativeTexture? texture, int unit)
{
if (texture == null)
{
GL.ActiveTexture(TextureUnit.Texture0 + unit);
GL.BindTexture(TextureTarget.Texture2D, 0);
return true;
}
switch (texture)
{
case GLVideoTexture glVideo:
if (glVideo.TextureIds == null)
return false;
for (int i = 0; i < glVideo.TextureIds.Length; i++)
{
GL.ActiveTexture(TextureUnit.Texture0 + unit + i);
GL.BindTexture(TextureTarget.Texture2D, glVideo.TextureIds[i]);
}
break;
case GLTexture glTexture:
if (glTexture.TextureId <= 0)
return false;
GL.ActiveTexture(TextureUnit.Texture0 + unit);
GL.BindTexture(TextureTarget.Texture2D, glTexture.TextureId);
break;
}
return true;
}
protected override void SetFrameBufferImplementation(IFrameBuffer? frameBuffer) =>
GL.BindFramebuffer(FramebufferTarget.Framebuffer, ((GLFrameBuffer?)frameBuffer)?.FrameBuffer ?? backbufferFramebuffer);
protected override void DeleteFrameBufferImplementation(IFrameBuffer frameBuffer)
=> GL.DeleteFramebuffer(((GLFrameBuffer)frameBuffer).FrameBuffer);
protected override void ClearImplementation(ClearInfo clearInfo)
{
if (clearInfo.Colour != CurrentClearInfo.Colour)
GL.ClearColor(clearInfo.Colour);
if (clearInfo.Depth != CurrentClearInfo.Depth)
{
if (IsEmbedded)
{
// GL ES only supports glClearDepthf
// See: https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glClearDepthf.xhtml
GL.ClearDepth((float)clearInfo.Depth);
}
else
{
// Older desktop platforms don't support glClearDepthf, so standard GL's double version is used instead
// See: https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glClearDepth.xhtml
osuTK.Graphics.OpenGL.GL.ClearDepth(clearInfo.Depth);
}
}
if (clearInfo.Stencil != CurrentClearInfo.Stencil)
GL.ClearStencil(clearInfo.Stencil);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.StencilBufferBit);
}
public override void DrawVerticesImplementation(PrimitiveTopology topology, int vertexStart, int verticesCount)
{
var glShader = (GLShader)Shader!;
int currentUniformBinding = 0;
int currentStorageBinding = 0;
foreach ((string name, IGLUniformBuffer buffer) in boundUniformBuffers)
{
if (glShader.GetUniformBlockIndex(name) is not int index)
continue;
buffer.Flush();
if (buffer is IGLShaderStorageBufferObject && UseStructuredBuffers)
{
GL4.GL.ShaderStorageBlockBinding(glShader, index, currentStorageBinding);
GL4.GL.BindBufferBase(GL4.BufferRangeTarget.ShaderStorageBuffer, currentStorageBinding, buffer.Id);
currentStorageBinding++;
}
else
{
GL.UniformBlockBinding(glShader, index, currentUniformBinding);
GL.BindBufferBase(BufferRangeTarget.UniformBuffer, currentUniformBinding, buffer.Id);
currentUniformBinding++;
}
}
GL.DrawElements(GLUtils.ToPrimitiveType(topology), verticesCount, DrawElementsType.UnsignedShort, vertexStart * sizeof(ushort));
}
protected override void SetScissorStateImplementation(bool enabled)
{
if (enabled)
GL.Enable(EnableCap.ScissorTest);
else
GL.Disable(EnableCap.ScissorTest);
}
protected override void SetBlendImplementation(BlendingParameters blendingParameters)
{
if (blendingParameters.IsDisabled)
{
if (!lastBlendingEnabledState.HasValue || lastBlendingEnabledState.Value)
GL.Disable(EnableCap.Blend);
lastBlendingEnabledState = false;
}
else
{
if (!lastBlendingEnabledState.HasValue || !lastBlendingEnabledState.Value)
GL.Enable(EnableCap.Blend);
lastBlendingEnabledState = true;
GL.BlendEquationSeparate(blendingParameters.RGBEquationMode, blendingParameters.AlphaEquationMode);
GL.BlendFuncSeparate(blendingParameters.SourceBlendingFactor, blendingParameters.DestinationBlendingFactor,
blendingParameters.SourceAlphaBlendingFactor, blendingParameters.DestinationAlphaBlendingFactor);
}
}
protected override void SetBlendMaskImplementation(BlendingMask blendingMask)
{
GL.ColorMask(blendingMask.HasFlagFast(BlendingMask.Red),
blendingMask.HasFlagFast(BlendingMask.Green),
blendingMask.HasFlagFast(BlendingMask.Blue),
blendingMask.HasFlagFast(BlendingMask.Alpha));
}
protected override void SetViewportImplementation(RectangleI viewport) => GL.Viewport(viewport.Left, viewport.Top, viewport.Width, viewport.Height);
protected override void SetScissorImplementation(RectangleI scissor) => GL.Scissor(scissor.X, Viewport.Height - scissor.Bottom, scissor.Width, scissor.Height);
protected override void SetDepthInfoImplementation(DepthInfo depthInfo)
{
if (depthInfo.DepthTest)
{
GL.Enable(EnableCap.DepthTest);
GL.DepthFunc(GLUtils.ToDepthFunction(depthInfo.Function));
}
else
GL.Disable(EnableCap.DepthTest);
GL.DepthMask(depthInfo.WriteDepth);
}
protected override void SetStencilInfoImplementation(StencilInfo stencilInfo)
{
if (stencilInfo.StencilTest)
{
GL.Enable(EnableCap.StencilTest);
GL.StencilFunc(GLUtils.ToStencilFunction(stencilInfo.TestFunction), stencilInfo.TestValue, stencilInfo.Mask);
GL.StencilOp(
GLUtils.ToStencilOperation(stencilInfo.StencilTestFailOperation),
GLUtils.ToStencilOperation(stencilInfo.DepthTestFailOperation),
GLUtils.ToStencilOperation(stencilInfo.TestPassedOperation));
}
else
GL.Disable(EnableCap.StencilTest);
}
protected internal override Image<Rgba32> TakeScreenshot()
{
var size = ((IGraphicsSurface)openGLSurface).GetDrawableSize();
var data = MemoryAllocator.Default.Allocate<Rgba32>(size.Width * size.Height);
GL.ReadPixels(0, 0, size.Width, size.Height, PixelFormat.Rgba, PixelType.UnsignedByte, ref MemoryMarshal.GetReference(data.Memory.Span));
var image = Image.LoadPixelData<Rgba32>(data.Memory.Span, size.Width, size.Height);
image.Mutate(i => i.Flip(FlipMode.Vertical));
return image;
}
protected override IShaderPart CreateShaderPart(IShaderStore store, string name, byte[]? rawData, ShaderPartType partType)
{
ShaderType glType;
switch (partType)
{
case ShaderPartType.Fragment:
glType = ShaderType.FragmentShader;
break;
case ShaderPartType.Vertex:
glType = ShaderType.VertexShader;
break;
default:
throw new ArgumentException($"Unsupported shader part type: {partType}", nameof(partType));
}
return new GLShaderPart(this, name, rawData, glType, store);
}
protected override IShader CreateShader(string name, IShaderPart[] parts, ShaderCompilationStore compilationStore)
=> new GLShader(this, name, parts.Cast<GLShaderPart>().ToArray(), compilationStore);
public override IFrameBuffer CreateFrameBuffer(RenderBufferFormat[]? renderBufferFormats = null, TextureFilteringMode filteringMode = TextureFilteringMode.Linear)
{
All glFilteringMode;
RenderbufferInternalFormat[]? glFormats = null;
switch (filteringMode)
{
case TextureFilteringMode.Linear:
glFilteringMode = All.Linear;
break;
case TextureFilteringMode.Nearest:
glFilteringMode = All.Nearest;
break;
default:
throw new ArgumentException($"Unsupported filtering mode: {filteringMode}", nameof(filteringMode));
}
if (renderBufferFormats != null)
{
glFormats = new RenderbufferInternalFormat[renderBufferFormats.Length];
for (int i = 0; i < renderBufferFormats.Length; i++)
{
switch (renderBufferFormats[i])
{
case RenderBufferFormat.D16:
glFormats[i] = RenderbufferInternalFormat.DepthComponent16;
break;
case RenderBufferFormat.D32:
glFormats[i] = RenderbufferInternalFormat.DepthComponent32f;
break;
case RenderBufferFormat.D24S8:
glFormats[i] = RenderbufferInternalFormat.Depth24Stencil8;
break;
case RenderBufferFormat.D32S8:
glFormats[i] = RenderbufferInternalFormat.Depth32fStencil8;
break;
default:
throw new ArgumentException($"Unsupported render buffer format: {renderBufferFormats[i]}", nameof(renderBufferFormats));
}
}
}
return new GLFrameBuffer(this, glFormats, glFilteringMode);
}
protected override IUniformBuffer<TData> CreateUniformBuffer<TData>()
=> new GLUniformBuffer<TData>(this);
protected override IShaderStorageBufferObject<TData> CreateShaderStorageBufferObject<TData>(int uboSize, int ssboSize)
=> new GLShaderStorageBufferObject<TData>(this, uboSize, ssboSize);
protected override INativeTexture CreateNativeTexture(int width, int height, bool manualMipmaps = false, TextureFilteringMode filteringMode = TextureFilteringMode.Linear,
Color4? initialisationColour = null)
{
All glFilteringMode;
switch (filteringMode)
{
case TextureFilteringMode.Linear:
glFilteringMode = All.Linear;
break;
case TextureFilteringMode.Nearest:
glFilteringMode = All.Nearest;
break;
default:
throw new ArgumentException($"Unsupported filtering mode: {filteringMode}", nameof(filteringMode));
}
return new GLTexture(this, width, height, manualMipmaps, glFilteringMode, initialisationColour);
}
protected override INativeTexture CreateNativeVideoTexture(int width, int height) => new GLVideoTexture(this, width, height);
protected override IVertexBatch<TVertex> CreateLinearBatch<TVertex>(int size, int maxBuffers, PrimitiveTopology topology)
=> new GLLinearBatch<TVertex>(this, size, maxBuffers, topology);
protected override IVertexBatch<TVertex> CreateQuadBatch<TVertex>(int size, int maxBuffers) => new GLQuadBatch<TVertex>(this, size, maxBuffers);
}
}