adding generated opengl function binding

This commit is contained in:
Youmu Liu
2025-12-29 17:06:04 +08:00
parent b3dcd1b5c0
commit f9288c1b3d
47 changed files with 61260 additions and 3101 deletions
+146
View File
@@ -0,0 +1,146 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="CsTypeMapper"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using System;
namespace GenerateGL.CodeGen
{
internal static class CsTypeMapper
{
public static string MapDelegateParameter(
GlParam param
)
{
// For delegate signatures, handles are kept as uint (not GlHandle) to match function pointer fields.
if (param.CType.Equals("GLchar", StringComparison.Ordinal) && param.IsPointer)
{
return "byte*";
}
string mapped = MapType(param.CType, allowVoid:false);
if (param.IsPointer)
{
if (mapped == "void") {
return "void*";
}
return $"{mapped}*";
}
return mapped;
}
public static string MapDelegateReturn(
string cType
)
{
return MapType(cType, allowVoid:true);
}
public static string MapParameter(
GlParam param
)
{
// Handle GlHandle-like cases: if the C type is GLuint and name hints an object, map to GlHandle
if (param.CType.Equals("GLuint", StringComparison.Ordinal) &&
LooksLikeHandle(param.Name) &&
!param.IsPointer)
{
return "GlHandle";
}
string mapped = MapType(param.CType, allowVoid:false);
if (param.IsPointer)
{
// const GLchar* -> byte*; other pointers -> void*/IntPtr depending on base
if (param.CType.Equals("GLchar", StringComparison.Ordinal))
{
return "byte*";
}
if (mapped == "void") {
return "void*";
}
return $"{mapped}*";
}
return mapped;
}
public static string MapReturn(
string cType
)
{
return MapType(cType, allowVoid:true);
}
private static bool LooksLikeHandle(
string name
)
{
if (string.IsNullOrWhiteSpace(name)) {
return false;
}
return name.Contains("program", StringComparison.OrdinalIgnoreCase) ||
name.Contains("shader", StringComparison.OrdinalIgnoreCase) ||
name.Contains("buffer", StringComparison.OrdinalIgnoreCase) ||
name.Contains("texture", StringComparison.OrdinalIgnoreCase) ||
name.Contains("framebuffer", StringComparison.OrdinalIgnoreCase) ||
name.Contains("renderbuffer", StringComparison.OrdinalIgnoreCase) ||
name.Contains("vertexarray", StringComparison.OrdinalIgnoreCase) ||
name.Contains("query", StringComparison.OrdinalIgnoreCase) ||
name.Contains("sampler", StringComparison.OrdinalIgnoreCase) ||
name.Contains("transformfeedback", StringComparison.OrdinalIgnoreCase);
}
private static string MapType(
string cType,
bool allowVoid
)
{
return cType switch
{
"void" when allowVoid => "void",
"GLenum" or "GLbitfield" => "uint",
"GLboolean" => "byte",
"GLuint" or "GLhandleARB" => "uint",
"GLint" or "GLsizei" or "GLfixed" => "int",
"GLshort" => "short",
"GLbyte" => "sbyte",
"GLubyte" => "byte",
"GLushort" => "ushort",
"GLfloat" => "float",
"GLdouble" => "double",
"GLclampf" or "GLclampd" => "float",
"GLintptr" => "nint",
"GLsizeiptr" => "nuint",
"GLchar" => "byte",
"GLsync" => "GLsync",
_ => allowVoid && cType == "void" ? "void" : "IntPtr"
};
}
}
}