Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenGL Core renderer and use as default #5655

Merged
merged 59 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 52 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
22b0f3a
Add GL core renderer
smoogipoo Feb 17, 2023
188f56a
Fix extensions string
smoogipoo Feb 17, 2023
e221321
Fix use of unsupported method on desktop platforms
smoogipoo Feb 17, 2023
abdc9dd
Use in/out in shaders
smoogipoo Feb 19, 2023
1764ea7
Remove deprecated constructs from shaders
smoogipoo Feb 19, 2023
b58d4cd
Remove nullable disables
smoogipoo Feb 21, 2023
ffe38a3
Add GL/GLCore shader compatibility layers
smoogipoo Feb 21, 2023
ac4f8cb
Update shaders to use macros
smoogipoo Feb 21, 2023
6aab647
Add compatibility layer for fragment shader output
smoogipoo Feb 21, 2023
02b6b64
Add compatibility profile type
smoogipoo Feb 21, 2023
1cace44
Fix compatibility profile on mobile devices
peppy Feb 21, 2023
8eda0ab
Add newlines and fix nullref
smoogipoo Feb 21, 2023
ea4a72e
Fix Veldrid nullref
smoogipoo Feb 21, 2023
f00a789
Add startup-time surface selection
smoogipoo Feb 21, 2023
3948ea8
Fix inspection + tests
smoogipoo Feb 21, 2023
b195670
Fix formatting
smoogipoo Feb 21, 2023
58ec18c
Fix exception message with subclasses
smoogipoo Feb 21, 2023
953f36f
Fix Windows preferential renderer selection
smoogipoo Feb 21, 2023
c3b65b5
Update some missed shaders
smoogipoo Feb 22, 2023
d91d314
Explicitly request RGB8
smoogipoo Feb 24, 2023
c5a45d6
Disable SRGB backbuffer
smoogipoo Feb 24, 2023
e90e308
Remove GL compatibility renderer
smoogipoo Feb 24, 2023
32035de
Add support for GL uniform buffers
smoogipoo Feb 21, 2023
574d712
Use uniform buffer for global uniforms
smoogipoo Feb 22, 2023
d313f6f
Ensure batch is flushed when UBO is assigned
smoogipoo Feb 23, 2023
62e7a52
Cleanup buffer binding
smoogipoo Feb 23, 2023
e7dc702
Cleanup uniform data types
smoogipoo Feb 23, 2023
e67bfbe
Add UBO type validation
smoogipoo Feb 24, 2023
06dc5d0
Adjust wording
smoogipoo Feb 24, 2023
e448cdc
Remove macros in favour of cross compiling
smoogipoo Feb 24, 2023
056d048
Update all other shaders to UBOs
smoogipoo Feb 24, 2023
5db568f
Fix regex/test
smoogipoo Feb 24, 2023
aebf7a1
Revert unintended changes
smoogipoo Feb 24, 2023
af8caf0
Improve exception output
smoogipoo Feb 24, 2023
751a582
Add missing disposals
smoogipoo Feb 24, 2023
d1a6230
Cleanup
smoogipoo Feb 24, 2023
091edfd
Remove use of glBindAttribLocation
smoogipoo Feb 24, 2023
a8397f7
Remove unused struct
smoogipoo Feb 24, 2023
dcc3d4d
Compile shaders for ESSL where applicable
smoogipoo Feb 24, 2023
f344f5d
Add SPIRV native libraries for iOS
frenzibyte Feb 24, 2023
60407de
Parse index using invariant culture
frenzibyte Feb 24, 2023
1451aae
Combine `GL_ES` condition into global uniform member
frenzibyte Feb 25, 2023
4ded6b3
Remove precision header file
frenzibyte Feb 25, 2023
400bfa0
Add renderer/surface type output
smoogipoo Feb 27, 2023
5487a83
Ignore case for surface argument
smoogipoo Feb 27, 2023
749adb7
Fix stutters during shader compilation
peppy Feb 28, 2023
699af72
Use cross-compile reflection in GL shader
smoogipoo Feb 28, 2023
eeef6a3
Ensure shader is compiled before BindUniformBlock()
smoogipoo Mar 6, 2023
a5bf178
Mode env variables to FrameworkEnvironment
smoogipoo Mar 6, 2023
10b7eec
Use env variables for renderer/surface
smoogipoo Mar 6, 2023
4f691c5
Unbox nullable
smoogipoo Mar 6, 2023
8e090c3
Add SPIRV native libraries for Android
frenzibyte Mar 7, 2023
f85aef5
Merge branch 'master' into opengl-core-renderer
peppy Mar 8, 2023
cc19945
Use PascalCase for env vars
smoogipoo Mar 9, 2023
e755c07
Revert change to GL.Hint()
smoogipoo Mar 9, 2023
7122320
Output readable type names to exceptions
smoogipoo Mar 9, 2023
e8d2f12
Remove broken/unsupported test scene
smoogipoo Mar 9, 2023
255d760
Change tuple `with` suggestion to hint level
peppy Mar 10, 2023
86c00bc
Merge branch 'master' into opengl-core-renderer
peppy Mar 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- Contains required properties for osu!framework projects. -->
<Project>
<PropertyGroup Label="C#">
<LangVersion>9.0</LangVersion>
<LangVersion>10.0</LangVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file added osu.Framework.Android/x86/libveldrid-spirv.so
Binary file not shown.
12 changes: 6 additions & 6 deletions osu.Framework.SourceGeneration.Tests/AbstractGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ protected void GetTestSources(
out (string filename, string content)[] commonGenerated,
out (string filename, string content)[] generated)
{
string commonSourcesNamespace = $"{resources_namespace}.CommonSources";
string commonGeneratedNamespace = $"{resources_namespace}.CommonGenerated";
const string common_sources_namespace = $"{resources_namespace}.CommonSources";
const string common_generated_namespace = $"{resources_namespace}.CommonGenerated";
string sourcesNamespace = $"{resources_namespace}.{name}.Sources";
string generatedNamespace = $"{resources_namespace}.{name}.Generated";

Expand All @@ -64,11 +64,11 @@ protected void GetTestSources(
var commonGeneratedFiles = new List<(string filename, string content)>();
var generatedFiles = new List<(string filename, string content)>();

foreach (string? file in resourceNames.Where(n => n.StartsWith(commonSourcesNamespace, StringComparison.Ordinal)))
commonSourceFiles.Add((getFileNameFromResourceName(commonSourcesNamespace, file), readResourceStream(assembly, file)));
foreach (string? file in resourceNames.Where(n => n.StartsWith(common_sources_namespace, StringComparison.Ordinal)))
commonSourceFiles.Add((getFileNameFromResourceName(common_sources_namespace, file), readResourceStream(assembly, file)));

foreach (string? file in resourceNames.Where(n => n.StartsWith(commonGeneratedNamespace, StringComparison.Ordinal)))
commonGeneratedFiles.Add((getFileNameFromResourceName(commonGeneratedNamespace, file), readResourceStream(assembly, file)));
foreach (string? file in resourceNames.Where(n => n.StartsWith(common_generated_namespace, StringComparison.Ordinal)))
commonGeneratedFiles.Add((getFileNameFromResourceName(common_generated_namespace, file), readResourceStream(assembly, file)));

foreach (string? file in resourceNames.Where(n => n.StartsWith(sourcesNamespace, StringComparison.Ordinal)))
sourceFiles.Add((getFileNameFromResourceName(sourcesNamespace, file), readResourceStream(assembly, file)));
Expand Down
17 changes: 7 additions & 10 deletions osu.Framework.Tests/Graphics/ShaderRegexTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#nullable disable

using System.Text.RegularExpressions;
using NUnit.Framework;
using osu.Framework.Graphics.OpenGL.Shaders;

Expand All @@ -12,8 +11,6 @@ namespace osu.Framework.Tests.Graphics
[TestFixture]
public class ShaderRegexTest
{
private readonly Regex shaderAttributeRegex = new Regex(GLShaderPart.SHADER_ATTRIBUTE_PATTERN);

[Test]
public void TestComment()
{
Expand All @@ -24,42 +21,42 @@ public void TestComment()
[Test]
public void TestNonAttribute()
{
const string test_string = "varying vec3 name;";
const string test_string = "wangs vec3 name;";
performInvalidAttributeTest(test_string);
}

[Test]
public void TestValidAttribute()
{
const string test_string = "attribute lowp float name;";
const string test_string = "layout(location = 0) in lowp float name;";
performValidAttributeTest(test_string);
}

[Test]
public void TestSpacedAttribute()
{
const string test_string = " attribute float name ;";
const string test_string = " layout( location =0 )in float name ;";
performValidAttributeTest(test_string);
}

[Test]
public void TestNoPrecisionQualifier()
{
const string test_string = "attribute float name;";
const string test_string = "layout(location = 0) in float name;";
performValidAttributeTest(test_string);
}

private void performValidAttributeTest(string testString)
{
var match = shaderAttributeRegex.Match(testString);
var match = GLShaderPart.SHADER_INPUT_PATTERN.Match(testString);

Assert.IsTrue(match.Success);
Assert.AreEqual("name", match.Groups[1].Value.Trim());
Assert.AreEqual("name", match.Groups[3].Value.Trim());
}

private void performInvalidAttributeTest(string testString)
{
var match = shaderAttributeRegex.Match(testString);
var match = GLShaderPart.SHADER_INPUT_PATTERN.Match(testString);

Assert.IsFalse(match.Success);
}
Expand Down
2 changes: 0 additions & 2 deletions osu.Framework.Tests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Linq;
using osu.Framework.Platform;
Expand Down
8 changes: 2 additions & 6 deletions osu.Framework.Tests/Shaders/TestSceneShaderDisposal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,17 @@ internal override IShader CreateShader(IRenderer renderer, string name, params I

private class TestGLShader : GLShader
{
private readonly GLRenderer renderer;

internal TestGLShader(GLRenderer renderer, string name, GLShaderPart[] parts)
: base(renderer, name, parts)
: base(renderer, name, parts, null)
{
this.renderer = renderer;
}

private protected override int CreateProgram() => 1337;

private protected override bool CompileInternal() => true;

private protected override void SetupUniforms()
public override void BindUniformBlock(string blockName, IUniformBuffer buffer)
{
Uniforms.Add("test", new Uniform<int>(renderer, this, "test", 1));
}

private protected override string GetProgramLog() => string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private void clickOutsideStep()
});
}

private void addBoxStep(Action<Drawable> boxFunc, int actionCount) => addBoxStep(boxFunc, Enumerable.Repeat<Action>(() => { }, actionCount).ToArray());
private void addBoxStep(Action<Drawable> boxFunc, int actionCount) => addBoxStep(boxFunc, Enumerable.Repeat(() => { }, actionCount).ToArray());

private void addBoxStep(Action<Drawable> boxFunc, params Action[] actions)
{
Expand Down
1 change: 1 addition & 0 deletions osu.Framework.iOS.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<NativeReference Include="$(MSBuildThisFileDirectory)osu.Framework.iOS\runtimes\ios\native\libavutil.xcframework" Kind="Framework" SmartLink="false" ForceLoad="true" />
<NativeReference Include="$(MSBuildThisFileDirectory)osu.Framework.iOS\runtimes\ios\native\libswresample.xcframework" Kind="Framework" SmartLink="false" ForceLoad="true" />
<NativeReference Include="$(MSBuildThisFileDirectory)osu.Framework.iOS\runtimes\ios\native\libswscale.xcframework" Kind="Framework" SmartLink="false" ForceLoad="true" />
<NativeReference Include="$(MSBuildThisFileDirectory)osu.Framework.iOS\runtimes\ios\native\veldrid-spirv.xcframework" Kind="Framework" SmartLink="false" ForceLoad="true" />
</ItemGroup>
<!-- Veldrid references libraries which cannot be AOT'd on iOS, replace them with stub assemblies.
See: https://github.com/mellinoe/veldrid/issues/472#issuecomment-1356461410 -->
Expand Down
2 changes: 2 additions & 0 deletions osu.Framework.iOS/GameAppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using SixLabors.ImageSharp.Formats.Png;
using SixLabors.ImageSharp.PixelFormats;
using UIKit;
using Veldrid.SPIRV;

namespace osu.Framework.iOS
{
Expand All @@ -37,6 +38,7 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l
NativeLibrary.SetDllImportResolver(typeof(Bass).Assembly, (_, assembly, path) => NativeLibrary.Load("@rpath/bass.framework/bass", assembly, path));
NativeLibrary.SetDllImportResolver(typeof(BassFx).Assembly, (_, assembly, path) => NativeLibrary.Load("@rpath/bass_fx.framework/bass_fx", assembly, path));
NativeLibrary.SetDllImportResolver(typeof(BassMix).Assembly, (_, assembly, path) => NativeLibrary.Load("@rpath/bassmix.framework/bassmix", assembly, path));
NativeLibrary.SetDllImportResolver(typeof(SpirvCompilation).Assembly, (_, assembly, path) => NativeLibrary.Load("@rpath/veldrid-spirv.framework/veldrid-spirv", assembly, path));

Window = new UIWindow(UIScreen.MainScreen.Bounds);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AvailableLibraries</key>
<array>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64</string>
<key>LibraryPath</key>
<string>veldrid-spirv.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
</dict>
<dict>
<key>LibraryIdentifier</key>
<string>ios-arm64_x86_64-simulator</string>
<key>LibraryPath</key>
<string>veldrid-spirv.framework</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
</array>
<key>CFBundlePackageType</key>
<string>XFWK</string>
<key>XCFrameworkFormatVersion</key>
<string>1.0</string>
</dict>
</plist>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
26 changes: 26 additions & 0 deletions osu.Framework/FrameworkEnvironment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using osu.Framework.Platform;

namespace osu.Framework
{
public static class FrameworkEnvironment
{
public static readonly ExecutionMode? STARTUP_EXECUTION_MODE;
public static readonly bool NO_TEST_TIMEOUT;
public static readonly bool FORCE_TEST_GC;
public static readonly GraphicsSurfaceType? PREFERRED_GRAPHICS_SURFACE;
public static readonly string? PREFERRED_GRAPHICS_RENDERER;
peppy marked this conversation as resolved.
Show resolved Hide resolved

static FrameworkEnvironment()
{
STARTUP_EXECUTION_MODE = Enum.TryParse<ExecutionMode>(Environment.GetEnvironmentVariable("OSU_EXECUTION_MODE"), true, out var mode) ? mode : null;
NO_TEST_TIMEOUT = Environment.GetEnvironmentVariable("OSU_TESTS_NO_TIMEOUT") == "1";
FORCE_TEST_GC = Environment.GetEnvironmentVariable("OSU_TESTS_FORCED_GC") == "1";
PREFERRED_GRAPHICS_SURFACE = Enum.TryParse<GraphicsSurfaceType>(Environment.GetEnvironmentVariable("OSU_GRAPHICS_SURFACE"), true, out var surface) ? surface : null;
PREFERRED_GRAPHICS_RENDERER = Environment.GetEnvironmentVariable("OSU_GRAPHICS_RENDERER")?.ToLowerInvariant();
}
}
}
39 changes: 31 additions & 8 deletions osu.Framework/Graphics/Containers/BufferedContainer_DrawNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Shaders;
using System;
using System.Runtime.InteropServices;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Shaders.Types;
using osu.Framework.Utils;

namespace osu.Framework.Graphics.Containers
Expand Down Expand Up @@ -92,25 +94,30 @@ protected override void DrawContents(IRenderer renderer)
base.DrawContents(renderer);
}

private IUniformBuffer<BlurParameters> blurParametersBuffer;

private void drawBlurredFrameBuffer(IRenderer renderer, int kernelRadius, float sigma, float blurRotation)
{
blurParametersBuffer ??= renderer.CreateUniformBuffer<BlurParameters>();

IFrameBuffer current = SharedData.CurrentEffectBuffer;
IFrameBuffer target = SharedData.GetNextEffectBuffer();

renderer.SetBlend(BlendingParameters.None);

using (BindFrameBuffer(target))
{
blurShader.GetUniform<int>(@"g_Radius").UpdateValue(ref kernelRadius);
blurShader.GetUniform<float>(@"g_Sigma").UpdateValue(ref sigma);

Vector2 size = current.Size;
blurShader.GetUniform<Vector2>(@"g_TexSize").UpdateValue(ref size);

float radians = -MathUtils.DegreesToRadians(blurRotation);
Vector2 blur = new Vector2(MathF.Cos(radians), MathF.Sin(radians));
blurShader.GetUniform<Vector2>(@"g_BlurDirection").UpdateValue(ref blur);

blurParametersBuffer.Data = blurParametersBuffer.Data with
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sold on the readability of with...

More details for others unfamiliar with this new expression.

Also correct me if I'm wrong but this looks to be initialising every property in this case, so I think using new would suffice right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also see at least one code inspection where this is to be applied.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, new would suffice here. with is only really that helpful in Renderer itself, where there's a massive buffer that's changed by many methods. I think new is actually usable for most cases other than that one...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go with it for now. I think it makes sense to use with in that case because it will handle a property potentially being added to the record in the future more correctly.

{
Radius = kernelRadius,
Sigma = sigma,
TexSize = current.Size,
Direction = new Vector2(MathF.Cos(radians), MathF.Sin(radians))
};

blurShader.BindUniformBlock("m_BlurParameters", blurParametersBuffer);
blurShader.Bind();
renderer.DrawFrameBuffer(current, new RectangleF(0, 0, current.Texture.Width, current.Texture.Height), ColourInfo.SingleColour(Color4.White));
blurShader.Unbind();
Expand All @@ -124,6 +131,22 @@ public List<DrawNode> Children
}

public bool AddChildDrawNodes => RequiresRedraw;

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
blurParametersBuffer?.Dispose();
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
private record struct BlurParameters
{
public UniformVector2 TexSize;
public UniformInt Radius;
public UniformFloat Sigma;
public UniformVector2 Direction;
private readonly UniformPadding8 pad1;
}
}

private class BufferedContainerDrawNodeSharedData : BufferedDrawNodeSharedData
Expand Down
12 changes: 3 additions & 9 deletions osu.Framework/Graphics/OpenGL/Buffers/GLLinearBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,22 @@ protected override void Initialise()
{
base.Initialise();

// Must be outside the conditional below as it needs to be added to the VAO
GL.BindBuffer(BufferTarget.ElementArrayBuffer, GLLinearIndexData.EBO_ID);

if (amountVertices > GLLinearIndexData.MaxAmountIndices)
{
ushort[] indices = new ushort[amountVertices];

for (int i = 0; i < amountVertices; i++)
indices[i] = (ushort)i;

Renderer.BindBuffer(BufferTarget.ElementArrayBuffer, GLLinearIndexData.EBO_ID);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(amountVertices * sizeof(ushort)), indices, BufferUsageHint.StaticDraw);

GLLinearIndexData.MaxAmountIndices = amountVertices;
}
}

public override void Bind(bool forRendering)
{
base.Bind(forRendering);

if (forRendering)
Renderer.BindBuffer(BufferTarget.ElementArrayBuffer, GLLinearIndexData.EBO_ID);
}

protected override PrimitiveType Type { get; }
}
}
12 changes: 3 additions & 9 deletions osu.Framework/Graphics/OpenGL/Buffers/GLQuadBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ protected override void Initialise()
{
base.Initialise();

// Must be outside the conditional below as it needs to be added to the VAO
GL.BindBuffer(BufferTarget.ElementArrayBuffer, GLQuadIndexData.EBO_ID);

if (amountIndices > GLQuadIndexData.MaxAmountIndices)
{
ushort[] indices = new ushort[amountIndices];
Expand All @@ -50,21 +53,12 @@ protected override void Initialise()
indices[j + 5] = (ushort)(i + 1);
}

Renderer.BindBuffer(BufferTarget.ElementArrayBuffer, GLQuadIndexData.EBO_ID);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(amountIndices * sizeof(ushort)), indices, BufferUsageHint.StaticDraw);

GLQuadIndexData.MaxAmountIndices = amountIndices;
}
}

public override void Bind(bool forRendering)
{
base.Bind(forRendering);

if (forRendering)
Renderer.BindBuffer(BufferTarget.ElementArrayBuffer, GLQuadIndexData.EBO_ID);
}

protected override int ToElements(int vertices) => 3 * vertices / 2;

protected override int ToElementIndex(int vertexIndex) => 3 * vertexIndex / 2;
Expand Down
Loading