8000 Use custom symbol loading by Perksey · Pull Request #32 · dotnet/Silk.NET · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use custom symbol loading #32

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

Merged
merged 10 commits into from
Aug 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 6 additions & 3 deletions build/binder_bakery_info/GL-All.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"ClassName": "GLCoreLibraryNameContainer"
},
"FunctionPrefix": "gl",
"ClassName": "GL"
"ClassName": "GL",
"SymbolLoader": "Silk.NET.Windowing.SilkLoader.OpenGL"
},
{
"Name": "OpenGL (Compatibility Profile)",
Expand Down Expand Up @@ -73,7 +74,8 @@
"ClassName": "OpenGLLibraryNameContainer"
},
"FunctionPrefix": "gl",
"ClassName": "GL"
"ClassName": "GL",
"SymbolLoader": "Silk.NET.Windowing.SilkLoader.OpenGL"
},
{
"Name": "GLES",
Expand All @@ -96,6 +98,7 @@
"ClassName": "OpenGLESLibraryNameContainer"
},
"FunctionPrefix": "gl",
"ClassName": "GL"
"ClassName": "GL",
"SymbolLoader": "Silk.NET.Windowing.SilkLoader.OpenGLES"
}
]
1 change: 1 addition & 0 deletions build/binder_specifications/GLES.json
Original file line number Diff line number Diff line change
Expand Up @@ -81977,6 +81977,7 @@
"ClassName": "GL",
"Version": null,
"FunctionPrefix": "gl",
"SymbolLoaderName": "Silk.NET.Windowing.SilkLoader.OpenGLES",
"Names": {
"Linux": "libGLESv2.so",
"Windows": "libGLESv2.dll",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288881,6 +288881,7 @@
"ClassName": "GL",
"Version": null,
"FunctionPrefix": "gl",
"SymbolLoaderName": "Silk.NET.Windowing.SilkLoader.OpenGL",
"Names": {
"Linux": "libGL.so.1",
"Windows": "opengl32.dll",
Expand Down
1 change: 1 addition & 0 deletions build/binder_specifications/OpenGL.json
Original file line number Diff line number Diff line change
Expand Up @@ -155897,6 +155897,7 @@
"ClassName": "GL",
"Version": null,
"FunctionPrefix": "gl",
"SymbolLoaderName": "Silk.NET.Windowing.SilkLoader.OpenGL",
"Names": {
"Linux": "libGL.so.1",
"Windows": "opengl32.dll",
Expand Down
12 changes: 8 additions & 4 deletions examples/Triangle/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.

using System;
using System.Drawing;
using Silk.NET.OpenGL;
using Silk.NET.Windowing;
Expand Down Expand Up @@ -48,11 +49,14 @@ public void OnLoad()
{
gl.ClearColor(1.0f, 0.4f, 0.3f, 1.0f);

vbo = gl.GenBuffer(1);
gl.BindBuffer((int)GLEnum.ArrayBuffer, vbo);
vbo = gl.GenBuffer();
gl.BindBuffer(GLEnum.ArrayBuffer, vbo);

//var size = (uint)vertices.Length * sizeof(float);
//gl.BufferData((int)GLEnum.ArrayBuffer, new UIntPtr(size), vertices, (int)GLEnum.StaticDraw);
var size = (uint)vertices.Length * sizeof(float);
fixed (float* floats = vertices)
{
gl.BufferData(GLEnum.ArrayBuffer, new UIntPtr(size), floats, GLEnum.StaticDraw);
}
}

public void OnRender(double delta)
Expand Down
8 changes: 8 additions & 0 deletions src/Core/BuildTools/Bind/ProfileWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ public static void WriteMixedModeClasses(this Project project, Profile profile,
sw.WriteLine(" : base(path, opts)");
sw.WriteLine(" {");
sw.WriteLine(" }");
if (profile.SymbolLoaderName != null)
{
sw.WriteLine();
sw.WriteLine($" static {profile.ClassName}()");
sw.WriteLine(" {");
sw.WriteLine($" LibraryLoader.CreateBuilder<{profile.ClassName}>({profile.SymbolLoaderName});");
sw.WriteLine(" }");
}
sw.WriteLine(" }");
sw.WriteLine("}");
sw.WriteLine();
Expand Down
5 changes: 5 additions & 0 deletions src/Core/BuildTools/Common/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public class Profile
/// </summary>
public string FunctionPrefix { get; set; }

/// <summary>
/// Gets or sets the name of the symbol loader.
/// </summary>
public string SymbolLoaderName { get; set; }

/// <summary>
/// Gets or sets the name container for this profile.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Core/BuildTools/Convert/Baking/ProfileBakery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public static void Bake(ProfileBakeryInformation information, string folder, boo
Namespace = information.Namespace,
ExtensionsNamespace = information.ExtensionsNamespace,
OutputFolder = information.OutputFolder,
ClassName = information.ClassName
ClassName = information.ClassName,
SymbolLoaderName = information.SymbolLoader
};
profile.Projects.Add
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public class ProfileBakeryInformation
/// Gets or sets the class name for this profile.
/// </summary>
public string ClassName { get; set; }

/// <summary>
/// Gets or sets the symbol loader for this profile.
/// </summary>
public string SymbolLoader { get; set; }
// ReSharper disable InconsistentNaming

/// <summary>
Expand All @@ -64,6 +69,7 @@ public class ProfileBakeryInformation
.WithName("GLES")
.WithOutputFolder("OpenGL")
.WithClassName("GL")
.WithSymbolLoader("Silk.NET.Windowing.SilkLoader.OpenGLES")
.WithLibraries
(
"OpenGLESLibraryNameContainer",
Expand All @@ -89,6 +95,7 @@ public class ProfileBakeryInformation
.WithName("OpenGL (Compatibility Profile)")
.WithOutputFolder("OpenGL")
.WithClassName("GL")
.WithSymbolLoader("Silk.NET.Windowing.SilkLoader.OpenGL")
.WithLibraries
(
"OpenGLLibraryNameContainer",
Expand All @@ -114,6 +121,7 @@ public class ProfileBakeryInformation
.WithNamespaces("Silk.NET.OpenGL", "Silk.NET.OpenGL.Extensions")
.WithName("OpenGL")
.WithOutputFolder("OpenGL")
.WithSymbolLoader("Silk.NET.Windowing.SilkLoader.OpenGL")
.WithClassName("GL")
.WithLibraries
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,11 @@ public ProfileBakeryInformationBuilder WithClassName(string className)
Result.ClassName = className;
return this;
}

public ProfileBakeryInformationBuilder WithSymbolLoader(string loader)
{
Result.SymbolLoader = loader;
return this;
}
}
}
28 changes: 19 additions & 9 deletions src/Core/Silk.NET.Core/Loader/LibraryLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.

using System;
using System.Collections.Generic;
using System.Reflection;
using AdvancedDLSupport;
using AdvancedDLSupport.Loaders;
using Silk.NET.Core.Attributes;
using Silk.NET.Core.Native;

Expand All @@ -16,26 +19,33 @@ public static class LibraryLoader
ImplementationOptions.UseLazyBinding |
ImplementationOptions.SuppressSecurity |
ImplementationOptions.EnableOptimizations;

private static readonly NativeLibraryBuilder _builder;

static LibraryLoader()
{
_builder = new NativeLibraryBuilder(Options);
}

private static Dictionary<Type, NativeLibraryBuilder> _builders = new Dictionary<Type, NativeLibraryBuilder>();

public static T1 Load<T1>(SearchPathContainer nameContainer) where T1 : NativeAPI
{
return _builder.ActivateClass<T1>(nameContainer.GetLibraryName());
CreateBuilder<T1>();
return _builders[typeof(T1)].ActivateClass<T1>(nameContainer.GetLibraryName());
}

public static T1 Load<T1, T2>(T2 baseApi) where T1 : NativeExtension<T2> where T2 : NativeAPI
{
CreateBuilder<T2>();
return baseApi.IsExtensionPresent(GetExtensionAttribute(typeof(T1)).Name)
? _builder.ActivateClass<T1>(baseApi.SearchPaths.GetLibraryName())
? _builders[typeof(T2)].ActivateClass<T1>(baseApi.SearchPaths.GetLibraryName())
: null;
}

public static void CreateBuilder<T1>(IPlatformLoader loader = null) where T1:NativeAPI
{
if (!_builders.ContainsKey(typeof(T1)) || loader != null)
{
_builders[typeof(T1)] = new NativeLibraryBuilder(Options)
.WithLibraryLoader(x => loader ?? x)
.WithSymbolLoader(x => loader ?? x);
}
}

private static ExtensionAttribute GetExtensionAttribute(MemberInfo type)
{
return type.GetCustomAttribute<ExtensionAttribute>();
Expand Down
12 changes: 10 additions & 2 deletions src/Core/Silk.NET.Core/Native/NativeAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// of the MIT license. See the LICENSE file for details.

using AdvancedDLSupport;
using AdvancedDLSupport.Loaders;
using Silk.NET.Core.Loader;

namespace Silk.NET.Core.Native
Expand All @@ -12,12 +13,19 @@ namespace Silk.NET.Core.Native
public abstract class NativeAPI : NativeLibraryBase
{
/// <inheritdoc />
protected NativeAPI(string path, ImplementationOptions options) : base(path, options)
protected NativeAPI
(
string path,
ImplementationOptions options,
ILibraryLoader libLoader = null,
ISymbolLoader symLoader = null
)
: base(path, options, libLoader, symLoader)
{
}

public abstract SearchPathContainer SearchPaths { get; }

public abstract bool IsExtensionPresent(string name);
}
}
}
11 changes: 10 additions & 1 deletion src/Core/Silk.NET.Core/Native/NativeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
// of the MIT license. See the LICENSE file for details.

using AdvancedDLSupport;
using AdvancedDLSupport.Loaders;

namespace Silk.NET.Core.Native
{
public abstract class NativeExtension<T> : NativeLibraryBase where T : NativeAPI
{
protected NativeExtension(string path, ImplementationOptions options) : base(path, options)
/// <inheritdoc />
protected NativeExtension
(
string path,
ImplementationOptions options,
ILibraryLoader libLoader = null,
ISymbolLoader symLoader = null
)
: base(path, options, libLoader, symLoader)
{
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Silk.NET.Core/Silk.NET.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AdvancedDLSupport" Version="3.0.0" />
<PackageReference Include="Ultz.Private.AdvancedDLSupport" Version="4.0.0" />
<PackageReference Include="System.Numerics.Vectors" Version="4.6.0-preview5.19224.8" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

<ItemGroup>
<PackageReference Include="System.Buffers" Version="4.5.0" />
<PackageReference Include="System.Memory" Version="4.5.1" />
</ItemGroup>

<Import Project="..\..\..\..\build\props\bindings.props" />
Expand Down
5 changes: 5 additions & 0 deletions src/OpenAL/Silk.NET.OpenAL/AL/AL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ namespace Silk.NET.OpenAL
/// </summary>
public abstract class AL : NativeAPI, IAL
{
static AL()
{
LibraryLoader.CreateBuilder<AL>(ALLoader.Instance);
}

/// <inheritdoc cref="NativeLibraryBase" />
protected AL(string path, ImplementationOptions options)
: base(path, options)
Expand Down
42 changes: 42 additions & 0 deletions src/OpenAL/Silk.NET.OpenAL/AL/ALLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This file is part of Silk.NET.
//
// You may modify and distribute Silk.NET under the terms
// of the MIT license. See the LICENSE file for details.

using System;
using AdvancedDLSupport;
using AdvancedDLSupport.Loaders;

namespace Silk.NET.OpenAL
{
public class ALLoader : IPlatformLoader
{
[NativeSymbols(Prefix = "al")]
public interface IInternalAL
{
IntPtr GetProcAddress(string proc);
}

private static IInternalAL _al = NativeLibraryBuilder.Default.ActivateInterface<IInternalAL>
(
new OpenALLibraryNameContainer().GetLibraryName()
);

public static ALLoader Instance { get; } = new ALLoader();

public IntPtr LoadSymbol(IntPtr library, string symbolName)
{
return _al.GetProcAddress(symbolName);
}

public IntPtr LoadLibrary(string path)
{
return IntPtr.Zero;
}

public bool CloseLibrary(IntPtr library)
{
return true;
}
}
}
6 changes: 6 additions & 0 deletions src/OpenAL/Silk.NET.OpenAL/ALC/ALContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ namespace Silk.NET.OpenAL
/// </summary>
public abstract class ALContext : NativeAPI, IALC
{
static ALContext()
{
LibraryLoader.CreateBuilder<ALContext>(ALLoader.Instance);
}


/// <inheritdoc cref="NativeLibraryBase" />
protected ALContext(string path, ImplementationOptions options)
: base(path, options)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Windowing\Silk.NET.Windowing\Silk.NET.Windowing.csproj" />
</ItemGroup>

<Import Project="..\..\..\build\props\bindings.props" />
Expand Down
3 changes: 2 additions & 1 deletion src/OpenGL/Silk.NET.OpenGL/Silk.NET.OpenGL.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand All @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Windowing\Silk.NET.Windowing\Silk.NET.Windowing.csproj" />
</ItemGroup>

<Import Project="..\..\..\build\props\bindings.props" />
Expand Down
Loading
0