Generated opengl function binding added. All opengl core profile function is now available but not in C# flavour.
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Collections.Frozen;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
@@ -41,6 +42,11 @@ namespace GenerateGL.CodeGen
|
||||
"3.3", "4.0", "4.1", "4.2", "4.3", "4.4", "4.5", "4.6"
|
||||
};
|
||||
|
||||
private static readonly FrozenDictionary<string, string> ReservedNames =
|
||||
new Dictionary<string, string>(StringComparer.Ordinal) {
|
||||
{ "string", "@string" }, { "class", "@class" }, { "namespace", "@namespace" }, { "checked", "@checked" }, { "unchecked", "@unchecked" }, { "ref", "@ref" }, { "out", "@out" }, { "in", "@in" }, { "object", "@object" }, { "event", "@event" }, { "params", "@params" }, { "base", "@base" }, { "this", "@this" }, { "void", "@void" }, { "int", "@int" }, { "uint", "@uint" }, { "long", "@long" }, { "ulong", "@ulong" }, { "short", "@short" }, { "ushort", "@ushort" }, { "float", "@float" }, { "double", "@double" }, { "decimal", "@decimal" }, { "bool", "@bool" }, { "byte", "@byte" }, { "sbyte", "@sbyte" }, { "char", "@char" }, { "struct", "@struct" }, { "public", "@public" }, { "private", "@private" }, { "protected", "@protected" }, { "internal", "@internal" }, { "fixed", "@fixed" }
|
||||
}.ToFrozenDictionary(StringComparer.Ordinal);
|
||||
|
||||
private readonly IReadOnlyDictionary<string, GlCommand> _commands;
|
||||
private readonly IReadOnlyList<GlFeature> _features;
|
||||
private readonly string _outputRoot;
|
||||
@@ -63,14 +69,76 @@ namespace GenerateGL.CodeGen
|
||||
}
|
||||
Directory.CreateDirectory(_outputRoot);
|
||||
List<GlCommand> all = new List<GlCommand>();
|
||||
HashSet<string> previous = new HashSet<string>(StringComparer.Ordinal);
|
||||
foreach (string v in TargetVersions)
|
||||
{
|
||||
IReadOnlyList<GlCommand> cmds = CollectCommandsUpToVersion(v);
|
||||
all.AddRange(cmds);
|
||||
WriteOpenGLFunctionFile(v, cmds);
|
||||
WriteGlContextFile(v, cmds);
|
||||
IReadOnlyList<GlCommand> cmdsUpTo = CollectCommandsUpToVersion(v);
|
||||
List<GlCommand> versionCmds = cmdsUpTo.Where(c => !previous.Contains(c.Name))
|
||||
.ToList();
|
||||
foreach (GlCommand cmd in versionCmds) {
|
||||
previous.Add(cmd.Name);
|
||||
}
|
||||
all.AddRange(versionCmds);
|
||||
WriteOpenGLFunctionFile(v, versionCmds);
|
||||
WriteGlContextFile(v, versionCmds);
|
||||
}
|
||||
WriteMergedGlContextFile(all);
|
||||
WriteCommandHistoryFile(BuildCommandHistory());
|
||||
}
|
||||
|
||||
private IReadOnlyDictionary<string, List<CommandHistoryEntry>> BuildCommandHistory()
|
||||
{
|
||||
Dictionary<string, List<CommandHistoryEntry>> history = new(StringComparer.Ordinal);
|
||||
|
||||
IEnumerable<GlFeature> ordered = _features.Where(f => Version.TryParse(f.Version, out _))
|
||||
.OrderBy(f => Version.Parse(f.Version));
|
||||
|
||||
foreach (GlFeature feature in ordered)
|
||||
{
|
||||
string version = feature.Version;
|
||||
|
||||
foreach (string name in feature.RequiresCommands.Distinct(StringComparer.Ordinal))
|
||||
{
|
||||
if (!_commands.ContainsKey(name) || ShouldSkip(name))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!history.TryGetValue(name, out List<CommandHistoryEntry>? list))
|
||||
{
|
||||
list = new List<CommandHistoryEntry>();
|
||||
history[name] = list;
|
||||
}
|
||||
|
||||
if (list.Count == 0 ||
|
||||
list[^1].Action != CommandHistoryAction.Added ||
|
||||
list[^1].Version != version) {
|
||||
list.Add(new CommandHistoryEntry(version, CommandHistoryAction.Added));
|
||||
}
|
||||
}
|
||||
|
||||
foreach (string name in feature.RemovedCommands.Distinct(StringComparer.Ordinal))
|
||||
{
|
||||
if (!_commands.ContainsKey(name) || ShouldSkip(name))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!history.TryGetValue(name, out List<CommandHistoryEntry>? list))
|
||||
{
|
||||
list = new List<CommandHistoryEntry>();
|
||||
history[name] = list;
|
||||
}
|
||||
|
||||
if (list.Count == 0 ||
|
||||
list[^1].Action != CommandHistoryAction.Removed ||
|
||||
list[^1].Version != version) {
|
||||
list.Add(new CommandHistoryEntry(version, CommandHistoryAction.Removed));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return history;
|
||||
}
|
||||
|
||||
private IReadOnlyList<GlCommand> CollectCommandsUpToVersion(
|
||||
@@ -78,15 +146,30 @@ namespace GenerateGL.CodeGen
|
||||
)
|
||||
{
|
||||
Version target = Version.Parse(version);
|
||||
IEnumerable<GlCommand?> selected = _features.Where(f => Version.TryParse(f.Version, out Version? fv) &&
|
||||
fv <= target)
|
||||
.SelectMany(f => f.RequiresCommands)
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.Select(name => _commands.TryGetValue(name, out GlCommand? c) ?
|
||||
c :
|
||||
null)
|
||||
.Where(c => c != null)!;
|
||||
return selected.ToList()!;
|
||||
IEnumerable<GlFeature> ordered = _features.Where(f => Version.TryParse(f.Version, out Version? fv) &&
|
||||
fv <= target)
|
||||
.OrderBy(f => Version.Parse(f.Version));
|
||||
|
||||
HashSet<string> active = new HashSet<string>(StringComparer.Ordinal);
|
||||
|
||||
foreach (GlFeature feature in ordered)
|
||||
{
|
||||
foreach (string name in feature.RequiresCommands.Distinct(StringComparer.Ordinal))
|
||||
{
|
||||
if (_commands.ContainsKey(name)) {
|
||||
active.Add(name);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (string name in feature.RemovedCommands.Distinct(StringComparer.Ordinal)) {
|
||||
active.Remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
return active.Select(name => _commands.TryGetValue(name, out GlCommand? c) ? c : null)
|
||||
.Where(c => c != null)!
|
||||
.Cast<GlCommand>()
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private static string DelegateSignature(
|
||||
@@ -104,14 +187,15 @@ namespace GenerateGL.CodeGen
|
||||
string name
|
||||
)
|
||||
{
|
||||
return string.IsNullOrWhiteSpace(name) ?
|
||||
"arg" :
|
||||
name switch
|
||||
{
|
||||
"params" => "@params",
|
||||
"event" => "@event",
|
||||
_ => name
|
||||
};
|
||||
if (string.IsNullOrWhiteSpace(name)) {
|
||||
return "arg";
|
||||
}
|
||||
|
||||
if (ReservedNames.TryGetValue(name, out string? mapped)) {
|
||||
return mapped;
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
private static bool ShouldSkip(
|
||||
@@ -132,6 +216,43 @@ namespace GenerateGL.CodeGen
|
||||
return name;
|
||||
}
|
||||
|
||||
private void WriteCommandHistoryFile(
|
||||
IReadOnlyDictionary<string, List<CommandHistoryEntry>> history
|
||||
)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.AppendLine("// <auto-generated>");
|
||||
sb.AppendLine($"// Generated by StgSharp.GenerateGL on {DateTime.UtcNow.ToString("u", CultureInfo.InvariantCulture)}");
|
||||
sb.AppendLine("// Command availability history");
|
||||
sb.AppendLine("// </auto-generated>");
|
||||
sb.AppendLine("using System;\nusing System.Collections.Frozen;\nusing System.Collections.Generic;\n");
|
||||
sb.AppendLine("namespace StgSharp.Graphics.OpenGL");
|
||||
sb.AppendLine("{");
|
||||
sb.AppendLine(" internal enum CommandHistoryAction { Added, Removed }");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine(" internal readonly record struct CommandHistoryEntry(string Version, CommandHistoryAction Action);");
|
||||
sb.AppendLine();
|
||||
sb.AppendLine(" internal static class GlCommandHistory");
|
||||
sb.AppendLine(" {");
|
||||
sb.AppendLine(" internal static readonly FrozenDictionary<string, CommandHistoryEntry[]> History = new Dictionary<string, CommandHistoryEntry[]>(StringComparer.Ordinal)");
|
||||
sb.AppendLine(" {");
|
||||
|
||||
foreach (KeyValuePair<string, List<CommandHistoryEntry>> kvp in history.OrderBy(k => k.Key, StringComparer.Ordinal))
|
||||
{
|
||||
string events = string.Join(", ", kvp.Value
|
||||
.OrderBy(v => Version.Parse(v.Version))
|
||||
.Select(v => $"new CommandHistoryEntry(\"{v.Version}\", CommandHistoryAction.{v.Action})"));
|
||||
sb.AppendLine($" {{ \"{kvp.Key}\", new CommandHistoryEntry[] {{ {events} }} }},");
|
||||
}
|
||||
|
||||
sb.AppendLine(" }.ToFrozenDictionary(StringComparer.Ordinal);");
|
||||
sb.AppendLine(" }");
|
||||
sb.AppendLine("}");
|
||||
|
||||
string fileName = Path.Combine(_outputRoot, "glCommandHistory.cs");
|
||||
File.WriteAllText(fileName, sb.ToString(), Encoding.UTF8);
|
||||
}
|
||||
|
||||
private void WriteGlContextFile(
|
||||
string version,
|
||||
IReadOnlyList<GlCommand> commands
|
||||
@@ -149,7 +270,7 @@ namespace GenerateGL.CodeGen
|
||||
sb.AppendLine(" {");
|
||||
foreach (GlCommand cmd in commands.OrderBy(c => c!.Name, StringComparer.Ordinal))
|
||||
{
|
||||
string ret = CsTypeMapper.MapDelegateReturn(cmd.ReturnType);
|
||||
string ret = CsTypeMapper.MapDelegateReturn(cmd.ReturnType, cmd.ReturnPointerLevel);
|
||||
List<string> paramTypes = new List<string>();
|
||||
foreach (GlParam p in cmd.Parameters)
|
||||
{
|
||||
@@ -188,7 +309,7 @@ namespace GenerateGL.CodeGen
|
||||
sb.AppendLine(" {");
|
||||
foreach (GlCommand cmd in distinct)
|
||||
{
|
||||
string ret = CsTypeMapper.MapDelegateReturn(cmd.ReturnType);
|
||||
string ret = CsTypeMapper.MapDelegateReturn(cmd.ReturnType, cmd.ReturnPointerLevel);
|
||||
List<string> paramTypes = new List<string>();
|
||||
foreach (GlParam p in cmd.Parameters)
|
||||
{
|
||||
@@ -215,29 +336,38 @@ namespace GenerateGL.CodeGen
|
||||
sb.AppendLine($"// Generated by StgSharp.GenerateGL on {DateTime.UtcNow.ToString("u", CultureInfo.InvariantCulture)}");
|
||||
sb.AppendLine($"// Version: {version}");
|
||||
sb.AppendLine("// </auto-generated>");
|
||||
sb.AppendLine("using System;\nusing System.Runtime.CompilerServices;\nusing StgSharp.Graphics.OpenGL;\n");
|
||||
sb.AppendLine("using System;\nusing System.Runtime.CompilerServices;\nusing System.Runtime.InteropServices;\nusing StgSharp.Graphics.OpenGL;\n");
|
||||
sb.AppendLine("namespace StgSharp.Graphics.OpenGL");
|
||||
sb.AppendLine("{");
|
||||
sb.AppendLine(" public unsafe partial class OpenGLFunction");
|
||||
sb.AppendLine(" {");
|
||||
foreach (GlCommand cmd in commands.OrderBy(c => c!.Name, StringComparer.Ordinal))
|
||||
{
|
||||
string ret = CsTypeMapper.MapReturn(cmd.ReturnType);
|
||||
string ret = CsTypeMapper.MapReturn(cmd.ReturnType, cmd.ReturnPointerLevel);
|
||||
string rawName = cmd.Name; // original glXxx
|
||||
string methodName = TrimGlPrefix(rawName);
|
||||
List<string> paramList = new List<string>();
|
||||
List<string> callArgs = new List<string>();
|
||||
List<(string Type,string Name)> pinned = new();
|
||||
foreach (GlParam p in cmd.Parameters)
|
||||
{
|
||||
string mapped = CsTypeMapper.MapParameter(p);
|
||||
string pname = SafeName(p.Name);
|
||||
paramList.Add($"{mapped} {pname}");
|
||||
if (mapped == "GlHandle")
|
||||
if (CsTypeMapper.TryMapConstPointerToSpan(p, out string spanElementType))
|
||||
{
|
||||
callArgs.Add($"{pname}.Value");
|
||||
paramList.Add($"ReadOnlySpan<{spanElementType}> {pname}");
|
||||
pinned.Add((spanElementType, pname));
|
||||
callArgs.Add($"{pname}Ptr");
|
||||
} else
|
||||
{
|
||||
callArgs.Add(pname);
|
||||
string mapped = CsTypeMapper.MapParameter(p);
|
||||
paramList.Add($"{mapped} {pname}");
|
||||
if (mapped == "GlHandle")
|
||||
{
|
||||
callArgs.Add($"{pname}.Value");
|
||||
} else
|
||||
{
|
||||
callArgs.Add(pname);
|
||||
}
|
||||
}
|
||||
}
|
||||
string signature = string.Join(", ", paramList);
|
||||
@@ -246,7 +376,25 @@ namespace GenerateGL.CodeGen
|
||||
sb.AppendLine($" public {ret} {methodName}({signature})");
|
||||
sb.AppendLine(" {");
|
||||
string prefix = ret == "void" ? string.Empty : "return ";
|
||||
sb.AppendLine($" {prefix}Context.{rawName}({call});");
|
||||
if (pinned.Count == 0)
|
||||
{
|
||||
sb.AppendLine($" {prefix}Context.{rawName}({call});");
|
||||
} else
|
||||
{
|
||||
string indent = " ";
|
||||
foreach ((string Type, string Name) pin in pinned)
|
||||
{
|
||||
sb.AppendLine($"{indent}fixed ({pin.Type}* {pin.Name}Ptr = {pin.Name})");
|
||||
sb.AppendLine($"{indent}{{");
|
||||
indent += " ";
|
||||
}
|
||||
sb.AppendLine($"{indent}{prefix}Context.{rawName}({call});");
|
||||
for (int i = 0; i < pinned.Count; i++)
|
||||
{
|
||||
indent = indent.Substring(0, indent.Length - 4);
|
||||
sb.AppendLine($"{indent}}}");
|
||||
}
|
||||
}
|
||||
sb.AppendLine(" }");
|
||||
sb.AppendLine();
|
||||
}
|
||||
@@ -257,5 +405,18 @@ namespace GenerateGL.CodeGen
|
||||
File.WriteAllText(fileName, sb.ToString(), Encoding.UTF8);
|
||||
}
|
||||
|
||||
private enum CommandHistoryAction
|
||||
{
|
||||
|
||||
Added,
|
||||
Removed
|
||||
|
||||
}
|
||||
|
||||
private readonly record struct CommandHistoryEntry(
|
||||
string Version,
|
||||
CommandHistoryAction Action
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user