11using System ;
22using System . CodeDom . Compiler ;
3- using System . Collections . Generic ;
4- using System . IO ;
5- using System . Linq ;
6- using System . Runtime . CompilerServices ;
7- using System . Text ;
8- using CXType = ClangSharp . Type ;
9-
10- // welcome to the jungle
11-
12- // moved to my BindingGen project
13-
14- // Todo: Clean up code
15- // Todo: Make into a usable api
16- // Todo: Use that api in the BindingGen main project
17- // Todo: Download xhtml files from https://github.com/BSVino/docs.gl and add the to the project
18- // Todo: Use downloaded xhtml files to generate inline xml docs for opengl functions.
19-
20-
213using ClangSharp ;
224using ClangSharp . Interop ;
235using System . Xml ;
246using System . Globalization ;
257
8+ using CXType = ClangSharp . Type ;
9+
2610HashSet < string > requiredFunctions = [
2711 "glCullFace" ,
2812 "glClearColor" ,
2913 "glClearDepth" ,
3014 "glClear" ,
31- "glGetNamedBufferSubData" ,
32- "glGenTextures"
15+ "glGenTextures" ,
16+ "glEnable" ,
17+ "glGetIntegerv" ,
18+ "glBindTexture" ,
19+ "glTexImage2D" ,
20+ "glTexParameteri" ,
21+ "glPixelStorei" ,
22+ "glTexSubImage2D" ,
23+ "glUniformMatrix4fv" ,
24+ "glActiveTexture" ,
25+ "glUniform1i" ,
26+ "glUniform4f" ,
27+ "glUniform1f" ,
28+ "glUniformMatrix4fv" ,
29+ "glCreateShader" ,
30+ "glCreateProgram" ,
31+ "glAttachShader" ,
32+ "glLinkProgram" ,
33+ "glDeleteShader" ,
34+ "glUseProgram" ,
35+ "glFinish" ,
36+ "glGenBuffers" ,
37+ "glBindBuffer" ,
38+ "glBufferData" ,
39+ "glBufferSubData" ,
40+ "glDeleteBuffers" ,
41+ "glDrawArrays" ,
42+ "glDrawElements" ,
43+ "glDrawElementsInstanced" ,
44+ "glDisable" ,
45+ "glGenFramebuffers" ,
46+ "glBindFramebuffer" ,
47+ "glFramebufferTexture2D" ,
48+ "glCheckFramebufferStatus" ,
49+ "glBindFramebuffer" ,
50+ "glGenVertexArrays" ,
51+ "glBlendFunc" ,
52+ "glDrawArraysInstanced" ,
53+ "glBindVertexArray" ,
54+ "glViewport" ,
55+ "glFlush" ,
56+ "glDeleteFramebuffers" ,
57+ "glDeleteVertexArrays" ,
58+ "glClearDepthf" ,
59+ "glDepthFunc" ,
60+ "glDepthMask" ,
61+ "glDepthMask" ,
62+ "glReadPixels" ,
63+ "glMapBufferRange" ,
64+ "glUnmapBuffer" ,
65+ "glGenerateMipmap" ,
66+ "glFramebufferTexture" ,
67+ "glClearStencil" ,
68+ "glDeleteTextures" ,
69+ "glDeleteFramebuffers" ,
70+ "glReadPixels" ,
71+ "glStencilFunc" ,
72+ "glStencilMask" ,
73+ "glStencilOp" ,
74+ "glBlitFramebuffer" ,
75+ "glFramebufferTexture" ,
76+ "glBlitFramebuffer" ,
77+ "glMemoryBarrier" ,
78+ "glFramebufferTexture" ,
79+ "glStencilOp" ,
80+ "glStencilOp" ,
81+ "glStencilOp" ,
82+ "glDeleteTextures" ,
83+ "glReadPixels" ,
84+ "glGetProgramiv" ,
85+ "glGetProgramInfoLog" ,
86+ "glShaderSource" ,
87+ "glCompileShader" ,
88+ "glGetShaderiv" ,
89+ "glGetShaderInfoLog" ,
90+ "glGetUniformLocation" ,
91+ "glBindBufferBase" ,
92+ "glGetProgramResourceIndex" ,
93+ "glGetProgramResourceiv" ,
94+ "glGetProgramResourceIndex" ,
95+ "glGetProgramResourceiv" ,
96+ "glGetBufferParameteriv" ,
97+ "glMapBufferRange" ,
98+ "glUnmapBuffer" ,
99+ "glMapBufferRange" ,
100+ "glUnmapBuffer" ,
101+ "glGetUniformLocation" ,
102+ "glUniform1ui" ,
103+ "glBindBufferBase" ,
104+ "glUniform1iv" ,
105+ "glUniform2iv" ,
106+ "glUniform3iv" ,
107+ "glUniform4iv" ,
108+ "glUniform1fv" ,
109+ "glUniform2fv" ,
110+ "glUniform3fv" ,
111+ "glUniform4fv" ,
112+ "glUniformMatrix3x2fv" ,
113+ "glGetUniformLocation" ,
114+ "glEnableVertexAttribArray" ,
115+ "glVertexAttribPointer" ,
116+ "glVertexAttribDivisor" ,
117+ "glGenerateMipmap" ,
118+ "glDeleteTextures" ,
119+ "glReadPixels" ,
120+ "glGetProgramiv" ,
121+ "glGetProgramInfoLog" ,
122+ "glShaderSource" ,
123+ "glCompileShader" ,
124+ "glGetShaderiv" ,
125+ "glGetShaderInfoLog" ,
126+ "glGetUniformLocation" ,
127+ ] ;
128+
129+ HashSet < string > optionalFunctions = [
130+ "glDispatchCompute" ,
131+ "glMemoryBarrier" ,
132+ "glDebugMessageCallback" ,
133+ "glMemoryBarrier" ,
33134] ;
34135
35136CXIndex index = CXIndex . Create ( ) ;
40141string source = File . ReadAllText ( glHeader ) ;
41142
42143StringWriter sw = new ( ) ;
43- Generator generator = new ( source , tu , requiredFunctions , sw ) ;
144+ Generator generator = new ( source , tu , requiredFunctions , optionalFunctions , sw ) ;
44145generator . GenerateBindings ( ) ;
45146
46147File . WriteAllText ( "./../../../../../SimulationFramework.OpenGL/OpenGL.gen.cs" , sw . ToString ( ) ) ;
@@ -49,16 +150,18 @@ class Generator
49150{
50151 TranslationUnit translationUnit ;
51152 HashSet < string > requiredFunctions ;
153+ HashSet < string > optionalFunctions ;
52154 IndentedTextWriter writer ;
53155 string source ;
54156 private Dictionary < string , string > macroValues = [ ] ;
55157
56- public Generator ( string source , TranslationUnit translationUnit , HashSet < string > requiredFunctions , StringWriter writer )
158+ public Generator ( string source , TranslationUnit translationUnit , HashSet < string > requiredFunctions , HashSet < string > optionalFunctions , StringWriter writer )
57159 {
58160 this . source = source ;
59161 this . writer = new ( writer ) ;
60162 this . translationUnit = translationUnit ;
61163 this . requiredFunctions = requiredFunctions ;
164+ this . optionalFunctions = optionalFunctions ;
62165
63166 FindMacroValues ( ) ;
64167 }
@@ -103,7 +206,7 @@ internal static unsafe class OpenGL
103206
104207 public static void glInitialize(FunctionLoader functionLoader)
105208 {
106- static nint LoadFunction (FunctionLoader functionLoader, string name)
209+ static nint LoadRequiredFunction (FunctionLoader functionLoader, string name)
107210 {
108211 nint value = functionLoader(name);
109212 if (value == 0)
@@ -112,6 +215,12 @@ static nint LoadFunction(FunctionLoader functionLoader, string name)
112215 }
113216 return value;
114217 }
218+
219+ static nint LoadOptionalFunction(FunctionLoader functionLoader, string name)
220+ {
221+ nint value = functionLoader(name);
222+ return value;
223+ }
115224""" ) ;
116225 writer . Indent ++ ;
117226 writer . Indent ++ ;
@@ -121,7 +230,14 @@ static nint LoadFunction(FunctionLoader functionLoader, string name)
121230 {
122231 if ( ShouldEmitFunction ( decl ) )
123232 {
124- writer . WriteLine ( "pfn_" + decl . Spelling + $ " = LoadFunction(functionLoader, \" { decl . Spelling } \" );") ;
233+ if ( requiredFunctions . Contains ( decl . Spelling ) )
234+ {
235+ writer . WriteLine ( "pfn_" + decl . Spelling + $ " = LoadRequiredFunction(functionLoader, \" { decl . Spelling } \" );") ;
236+ }
237+ if ( optionalFunctions . Contains ( decl . Spelling ) )
238+ {
239+ writer . WriteLine ( "pfn_" + decl . Spelling + $ " = LoadOptionalFunction(functionLoader, \" { decl . Spelling } \" );") ;
240+ }
125241 }
126242 }
127243 writer . Indent -- ;
@@ -159,10 +275,6 @@ static nint LoadFunction(FunctionLoader functionLoader, string name)
159275
160276 private bool ShouldEmitFunction ( Cursor cursor )
161277 {
162- Console . WriteLine ( "function: " + cursor . Spelling ) ;
163- if ( cursor . Spelling == "GL_APIENTRY" )
164- Console . WriteLine ( "hello" ) ;
165-
166278 if ( cursor is not FunctionDecl fn )
167279 return false ;
168280
@@ -171,8 +283,10 @@ private bool ShouldEmitFunction(Cursor cursor)
171283 if ( name . Length < 2 )
172284 return false ;
173285
174- bool isExtension = char . IsUpper ( name [ ^ 1 ] ) && char . IsUpper ( name [ ^ 2 ] ) ;
175- return name . StartsWith ( "gl" ) && ! isExtension ;
286+ if ( char . IsUpper ( name [ ^ 1 ] ) && char . IsUpper ( name [ ^ 2 ] ) )
287+ return false ;
288+
289+ return name . StartsWith ( "gl" ) ;
176290 }
177291
178292 private void EmitFunction ( FunctionDecl decl )
@@ -333,4 +447,4 @@ static nint LoadFunction(FunctionLoader functionLoader, string name)
333447 public static void glCullFace ( uint mode ) => ( ( delegate * unmanaged[ Stdcall] < uint , void > ) pfn_glCullFace ) ( mode ) ;
334448 private static nint pfn_glClearColor ;
335449 public static void glClearColor ( int red , int green , int blue , int alpha ) => ( ( delegate * unmanaged[ Stdcall] < int , int , int , int , void > ) pfn_glClearColor ) ( red , green , blue , alpha ) ;
336- }
450+ }
0 commit comments