g2bmx.bmx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. Strict
  2. Framework brl.basic
  3. Import brl.map
  4. Import brl.retro
  5. Import brl.system
  6. '
  7. ' Generates bmx source from glad.h
  8. '
  9. Global OutDir:String = "_out_"
  10. Local OutOpenGLName$="opengl.bmx"
  11. Local OutGladName$="glfwopengl.bmx"
  12. Const LINES_PER_PAGE:Int = 500
  13. CreateDir(outDir, True)
  14. Global OutOpenGL:TStream = WriteStream(outDir + "/" + OutOpenGLName$)
  15. Global OutGlad :TStream = WriteStream(outDir + "/" + OutGladName$)
  16. Global Out :TStream = OutOpenGL
  17. Local generator:TCodeGenerator = New TCodeGenerator
  18. generator.Generate()
  19. Type TCodeGenerator
  20. Const STAGE_COUNT:Int = 0
  21. Const STAGE_GENERATE:Int = 1
  22. Const TYPE_OPENGL:Int = 0
  23. Const TYPE_GLAD:Int = 1
  24. Field timestamp:String = CurrentDate()
  25. Field stage:Int
  26. Field generateType:Int
  27. Field curr:String
  28. Field Text:String
  29. Field gladSource:String[]
  30. Field totalLines:Int
  31. Field pageCount:Int
  32. Field page:Int = -1
  33. Field lineCount:Int
  34. Method Init()
  35. gladSource = LoadText("../glfw.mod/glfw/deps/glad/gl.h").Split("~n")
  36. End Method
  37. Method Generate()
  38. Init()
  39. stage = STAGE_COUNT
  40. Process()
  41. pageCount = totalLines / LINES_PER_PAGE
  42. If totalLines Mod LINES_PER_PAGE > 0 Then
  43. pageCount :+ 1
  44. End If
  45. stage = STAGE_GENERATE
  46. MainGlad()
  47. HeaderOpenGL()
  48. NextGlad()
  49. Process()
  50. WriteLine OutOpenGL , ""
  51. WriteLine OutOpenGL , "End Extern"
  52. OutOpenGL.Close
  53. WriteLine OutGlad , ""
  54. WriteLine OutGlad , "End Extern"
  55. WriteLine OutGlad , ""
  56. WriteLine OutGlad , "Extern ~qC~q"
  57. WriteLine OutGlad , "Function gladLoadGL:Int(func(name:Byte Ptr))=~qint gladLoadGL( GLADloadfunc)!~q"
  58. WriteLine OutGlad , "End Extern"
  59. OutGlad.Close
  60. End Method
  61. Method GenerateApi (chaine$, bbDoc$)
  62. Select stage
  63. Case STAGE_COUNT
  64. Select generateType
  65. Case TYPE_GLAD
  66. totalLines :+ 1
  67. Case TYPE_OPENGL
  68. If Instr ( chaine , "glViewport" )
  69. generateType = TYPE_GLAD
  70. End If
  71. End Select
  72. Case STAGE_GENERATE
  73. 'WriteLine Out,"rem"
  74. 'WriteLine Out,"bbdoc: "+bbDoc
  75. 'WriteLine Out,"endrem"
  76. WriteLine Out,chaine
  77. Select generateType
  78. Case TYPE_GLAD
  79. lineCount :+ 1
  80. If lineCount = LINES_PER_PAGE Then
  81. FooterGlad()
  82. NextGlad()
  83. Out = OutGlad
  84. lineCount = 0
  85. End If
  86. Case TYPE_OPENGL
  87. If Instr ( chaine , "glViewport" )
  88. Out = OutGlad
  89. generateType = TYPE_GLAD
  90. End If
  91. End Select
  92. End Select
  93. End Method
  94. Method NextGlad()
  95. page :+ 1
  96. OutGlad = WriteStream(outDir + "/" + GladFile(page))
  97. HeaderGlad()
  98. End Method
  99. Method GladFile:String(page:Int)
  100. Local s:String = "00" + page
  101. Return "glad" + s[s.length-2..] + ".bmx"
  102. End Method
  103. Method Process()
  104. generateType = TYPE_OPENGL
  105. Local funMap:TMap=New TMap
  106. Local constMap:TMap=New TMap
  107. Local delayedMap:TMap = New TMap
  108. For Text:String = EachIn gladSource
  109. bump
  110. If curr="GLAD_API_CALL"
  111. bump
  112. Local ty:Tty = New Tty
  113. Local funty$=gltype(ty)
  114. If funty<>"x" And curr="GLAD_API_PTR"
  115. Local id$=bump()
  116. If id[..2]="gl" And bump()="("
  117. Local p:Tproto = New TProto
  118. Local proto$=glproto(p)
  119. If proto<>"x"
  120. GenerateApi ( "Function "+id+funty+"("+proto+")", "GL Function "+id+funty )
  121. EndIf
  122. EndIf
  123. EndIf
  124. Else If curr="#"
  125. If bump()="define"
  126. Local id$=bump()
  127. If id[..11]="GL_VERSION_"
  128. Else If id[..3]="GL_"
  129. If Not constMap.ValueForKey(id)
  130. Local n$=bump()
  131. If n[..2]="0x"
  132. GenerateApi ( "Const "+id+"=$"+n[2..], "GL Const "+id )
  133. Else If n.length
  134. If isdigit(n[0]) 'And n<>"1"
  135. GenerateApi ( "Const "+id+"="+n, "GL Const "+id )
  136. Else
  137. Local v:String = String(constMap.ValueForKey(n))
  138. If v Then
  139. If v[..2]="0x" Then
  140. GenerateApi ( "Const "+id+"=$" + v[2..], "GL Const "+id )
  141. Else
  142. GenerateApi ( "Const "+id+"=" + v, "GL Const "+id )
  143. End If
  144. Else
  145. delayedMap.Insert(id, n)
  146. End If
  147. End If
  148. EndIf
  149. constMap.Insert id,n
  150. EndIf
  151. Else If id[..5]="GLAD_"
  152. If bump()="GLAD_GET_VAR" And bump()="("
  153. Local sym$=bump()
  154. If sym[..7]="__GLAD_" And bump()=")"
  155. If Not constMap.ValueForKey("GL_"+id[5..])
  156. GenerateApi ( "Global GL_"+id[5..]+":Byte=~q"+sym+"~q", "GL Global "+id )
  157. End If
  158. EndIf
  159. EndIf
  160. Else If id[..2]="gl"
  161. 'If bump()="GLAD_GET_FUN" And bump()="("
  162. Local sym$=bump()
  163. If sym[..5]="glad_"' And bump()=")"
  164. Local key$="PFN"+sym[5..].ToUpper()+"PROC"
  165. Local val:Tproto=Tproto( funMap.ValueForKey( key ) )
  166. If val
  167. Local prefix:String
  168. If (id = "glShaderSource" Or id = "glGetShaderInfoLog" Or id = "glGetProgramInfoLog" Or id = "glGetShaderSource" Or id = "glGetUniformLocation") prefix = "_"
  169. GenerateApi ( "Global "+prefix+id+val.proto+"=~q"+val.exproto.Replace("XXXXXXXX", sym).Trim()+"~q", "GL Global "+id+val.proto )
  170. Else
  171. DebugLog "***** "+sym+" *****"
  172. EndIf
  173. EndIf
  174. 'EndIf
  175. EndIf
  176. EndIf
  177. Else If curr="typedef"
  178. bump
  179. Local ty:Tty = New Tty
  180. Local funty$=gltype(ty)
  181. If funty<>"x" And curr="(" And bump()="GLAD_API_PTR"
  182. Local id$=bump()
  183. If id = "*" Then id = bump()
  184. If id[..5]="PFNGL" And bump()=")" And bump()="("
  185. Local p:Tproto = New TProto
  186. Local proto$=glproto(p)
  187. If proto<>"x"
  188. p.proto = funty+"("+proto+")"
  189. p.exproto = ty.exty + " XXXXXXXX(" + p.exproto + ")!"
  190. funMap.Insert id, p
  191. EndIf
  192. EndIf
  193. EndIf
  194. EndIf
  195. Next
  196. For Local id:String = EachIn delayedMap.Keys()
  197. Local v:String = String(delayedMap.ValueForKey(id))
  198. If v Then
  199. Local c:String = String(constMap.ValueForKey(v))
  200. If c Then
  201. If c[..2]="0x" Then
  202. GenerateApi ( "Const "+id+"=$" + c[2..], "GL Const "+id )
  203. Else
  204. GenerateApi ( "Const "+id+"=" + c, "GL Const "+id )
  205. End If
  206. End If
  207. End If
  208. Next
  209. End Method
  210. Method glproto$(proto:Tproto)
  211. If bump()=")" Return ""
  212. Local err,argid
  213. Repeat
  214. Local ty:Tty = New Tty
  215. Local argty$=gltype(ty)
  216. If argty="x" Return argty
  217. Local id$
  218. If curr<>"," And curr<>")" And curr.length And (isalpha(curr[0]) Or curr[0]=Asc("_"))
  219. id$=curr
  220. If bump()="["
  221. While bump()<>"]"
  222. Wend
  223. bump
  224. If Not argty argty="Byte"
  225. argty:+" Ptr"
  226. EndIf
  227. Else
  228. id="arg"+argid
  229. EndIf
  230. argid:+1
  231. If proto.proto Then
  232. proto.proto:+","
  233. proto.exproto:+","
  234. End If
  235. proto.proto:+id+"_"+argty
  236. proto.exproto :+ ty.exty
  237. If curr=")"
  238. bump
  239. If proto.proto="arg0_" proto.proto=""
  240. Return proto.proto
  241. EndIf
  242. If curr<>"," Return "x"
  243. bump
  244. Forever
  245. End Method
  246. Method gltype$(ty:TTy)
  247. 'Local ty$
  248. 'Local exty:String
  249. If curr="const"
  250. ty.exty :+ curr
  251. bump
  252. EndIf
  253. Select curr
  254. Case "void","GLvoid"
  255. ty.ty=""
  256. Case "char","GLbyte","GLubyte","GLchar","GLboolean","GLcharARB"
  257. ty.ty="Byte"
  258. Case "GLshort","GLushort","GLhalf"
  259. ty.ty="Short"
  260. Case "GLint","GLuint","GLenum","GLsizei","GLbitfield"
  261. ty.ty="Int"
  262. Case "GLintptr","GLsizeiptr","GLintptrARB","GLsizeiptrARB"
  263. ty.ty="Int"
  264. Case "GLhandleARB"
  265. ty.ty="Int"
  266. Case "GLint64EXT","GLuint64EXT", "GLint64", "GLuint64"
  267. ty.ty="Long"
  268. Case "GLfloat","GLclampf"
  269. ty.ty="Float"
  270. Case "GLdouble","GLclampd"
  271. ty.ty="Double"
  272. Default
  273. Return "x"
  274. End Select
  275. ty.exty :+ " " + curr
  276. Repeat
  277. bump
  278. If curr="const" Then
  279. ty.exty :+ " " + curr
  280. bump
  281. End If
  282. If curr<>"*" Exit
  283. ty.exty :+ curr
  284. If Not ty.ty ty.ty="Byte"
  285. ty.ty:+" Ptr"
  286. Forever
  287. If ty.ty ty.ty=":"+ty.ty
  288. Return ty.ty
  289. End Method
  290. Method isalpha( c )
  291. Return (c>=Asc("A") And c<=Asc("Z")) Or (c>=Asc("a") And c<=Asc("z"))
  292. End Method
  293. Method isdigit( c )
  294. Return c>=Asc("0") And c<=Asc("9")
  295. End Method
  296. Method isalnum( c )
  297. Return isalpha(c) Or isdigit(c)
  298. End Method
  299. Method isxdigit( c )
  300. Return (c>=Asc("A") And c<=Asc("F")) Or (c>=Asc("a") And c<=Asc("f")) Or isdigit(c)
  301. End Method
  302. Method bump$()
  303. Local i=0
  304. While i<Text.length And Text[i]<=Asc(" ")
  305. i:+1
  306. Wend
  307. If i=Text.length
  308. curr=""
  309. Text=""
  310. Return curr
  311. EndIf
  312. Text=Text[i..]
  313. Local c=Text[0]
  314. i=1
  315. If isalpha(c) Or c=Asc("_")
  316. While i<Text.length And (isalnum( Text[i] ) Or Text[i]=Asc("_"))
  317. i:+1
  318. Wend
  319. Else If c>=Asc("0") And c<=Asc("9")
  320. If i<Text.length And c=Asc("0") And Text[i]=Asc("x")
  321. i:+1
  322. While i<Text.length And isxdigit(Text[i])
  323. i:+1
  324. Wend
  325. Else
  326. While i<Text.length And isdigit(Text[i])
  327. i:+1
  328. Wend
  329. EndIf
  330. EndIf
  331. curr=Text[..i]
  332. Text=Text[i..]
  333. Return curr
  334. End Method
  335. Method HeaderOpenGL()
  336. Warning(OutOpenGL)
  337. WriteLine OutOpenGL, "Strict"
  338. 'WriteLine OutOpenGL, "Rem"
  339. 'WriteLine OutOpenGL, "bbdoc: Graphics/OpenGL 1.1"
  340. 'WriteLine OutOpenGL, "End Rem"
  341. 'WriteLine OutOpenGL, "Module Pub.OpenGL"
  342. WriteLine OutOpenGL, ""
  343. 'WriteLine OutOpenGL, "ModuleInfo ~qVersion: 1.02~q"
  344. 'WriteLine OutOpenGL, "ModuleInfo ~qLicense: SGI Free Software License B~q"
  345. 'WriteLine OutOpenGL, "ModuleInfo ~qModserver: brl~q"
  346. WriteLine OutOpenGL, "Import ~q../glfw.mod/glfw/deps/*.h~q"
  347. WriteLine OutOpenGL, "Import ~q../glfw.mod/glfw/deps/glad/gl.h~q"
  348. 'WriteLine OutOpenGL, "Import ~q-lglad -lglapi -ldrm_nouveau~q"
  349. 'WriteLine OutOpenGL, "ModuleInfo ~qHistory: 1.02 Release~q"
  350. 'WriteLine OutOpenGL, "ModuleInfo ~qHistory: Restored GLU~q"
  351. 'WriteLine OutOpenGL, ""
  352. 'WriteLine OutOpenGL, "?MacOS"
  353. 'WriteLine OutOpenGL, "Import ~q-Framework AGL~q"
  354. 'WriteLine OutOpenGL, "Import ~q-Framework OpenGL~q"
  355. 'WriteLine OutOpenGL, "?Win32"
  356. 'WriteLine OutOpenGL, "Import ~q-lglu32~q"
  357. 'WriteLine OutOpenGL, "Import ~q-lopengl32~q"
  358. 'WriteLine OutOpenGL, "?Linux"
  359. 'WriteLine OutOpenGL, "Import ~q-lGL~q"
  360. 'WriteLine OutOpenGL, "Import ~q-lGLU~q"
  361. 'WriteLine OutOpenGL, "?"
  362. 'WriteLine OutOpenGL, ""
  363. 'WriteLine OutOpenGL, "Import ~qglu.bmx~q"
  364. WriteLine OutOpenGL, ""
  365. WriteLine OutOpenGL, "Extern"
  366. WriteLine OutOpenGL, ""
  367. End Method
  368. Method HeaderGlad()
  369. Warning(OutGlad)
  370. WriteLine OutGlad, "Strict"
  371. WriteLine OutGlad, ""
  372. WriteLine OutGlad, "Import ~q../glfw.mod/glfw/deps/*.h~q"
  373. WriteLine OutGlad, "Import ~q../glfw.mod/glfw/deps/glad/gl.h~q"
  374. WriteLine OutGlad, ""
  375. WriteLine OutGlad, "Extern"
  376. WriteLine OutGlad, ""
  377. End Method
  378. Method FooterGlad()
  379. WriteLine OutGlad, ""
  380. WriteLine OutGlad, "End Extern"
  381. End Method
  382. Method MainGlad()
  383. Warning(OutGlad)
  384. WriteLine OutGlad, "Strict"
  385. WriteLine OutGlad, ""
  386. 'WriteLine OutGlad, "Rem"
  387. 'WriteLine OutGlad, "bbdoc: Graphics/Glad"
  388. 'WriteLine OutGlad, "End Rem"
  389. WriteLine OutGlad, "Module GLFW.GLFWOpenGL"
  390. WriteLine OutGlad, ""
  391. WriteLine OutGlad, "ModuleInfo ~qVersion: 1.00~q"
  392. WriteLine OutGlad, "ModuleInfo ~qAuthor: Milan Ikits, Marcelo Magallon~q"
  393. WriteLine OutGlad, "ModuleInfo ~qLicense: SGI Free Software License B~q"
  394. WriteLine OutGlad, "ModuleInfo ~qCopyright: Milan Ikits, Marcelo Magallon~q"
  395. 'WriteLine OutGlad, "ModuleInfo ~qModserver: BRL~q"
  396. WriteLine OutGlad, ""
  397. WriteLine OutGlad, "ModuleInfo ~qHistory: 1.00~q"
  398. WriteLine OutGlad, "ModuleInfo ~qHistory: Initial version.~q"
  399. WriteLine OutGlad, ""
  400. WriteLine OutGlad, "Import ~q../glfw.mod/glfw/deps/*.h~q"
  401. 'WriteLine OutGlad, "?nx"
  402. WriteLine OutGlad, ""
  403. 'WriteLine OutGlad, "ModuleInfo ~qLD_OPTS: -L%NX.DEVKITPRO%/portlibs/switch/lib~q"
  404. 'WriteLine OutGlad, ""
  405. WriteLine OutGlad, "Import ~qopengl.bmx~q"
  406. For Local i:Int = 0 Until pageCount
  407. WriteLine OutGlad, "Import ~q" + GladFile(i) + "~q"
  408. Next
  409. WriteLine OutGlad, ""
  410. WriteLine OutGlad, "Function glShaderSource(shader_:Int,count_:Int,source:String)"
  411. WriteLine OutGlad, "~tLocal s:Byte Ptr = source.ToUTF8String()"
  412. WriteLine OutGlad, "~t_glShaderSource(shader_, count_, Varptr s, Null)"
  413. WriteLine OutGlad, "~tMemFree s"
  414. WriteLine OutGlad, "End Function"
  415. WriteLine OutGlad, ""
  416. WriteLine OutGlad, "Function glGetShaderInfoLog:String(shader:Int)"
  417. WriteLine OutGlad, "~tLocal buf:Byte[2048]"
  418. WriteLine OutGlad, "~tLocal length:Int"
  419. WriteLine OutGlad, "~t_glGetShaderInfoLog(shader, 2048, Null, buf)"
  420. WriteLine OutGlad, "~tReturn String.FromUTF8String(buf)"
  421. WriteLine OutGlad, "End Function"
  422. WriteLine OutGlad, ""
  423. WriteLine OutGlad, "Function glGetProgramInfoLog:String(program:Int)"
  424. WriteLine OutGlad, "~tLocal buf:Byte[2048]"
  425. WriteLine OutGlad, "~tLocal length:Int"
  426. WriteLine OutGlad, "~t_glGetShaderInfoLog(program, 2048, Null, buf)"
  427. WriteLine OutGlad, "~tReturn String.FromUTF8String(buf)"
  428. WriteLine OutGlad, "End Function"
  429. WriteLine OutGlad, ""
  430. WriteLine OutGlad, "Function glGetShaderSource:String(shader:Int)"
  431. WriteLine OutGlad, "~tLocal length:Int"
  432. WriteLine OutGlad, "~tglGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, Varptr length)"
  433. WriteLine OutGlad, "~tIf length Then"
  434. WriteLine OutGlad, "~tLocal buf:Byte[length + 1]"
  435. WriteLine OutGlad, "~t_glGetShaderSource(shader, length + 1, Null, buf)"
  436. WriteLine OutGlad, "~tReturn String.FromUTF8String(buf)"
  437. WriteLine OutGlad, "~tEnd If"
  438. WriteLine OutGlad, "End Function"
  439. WriteLine OutGlad, ""
  440. WriteLine OutGlad, "Function glGetUniformLocation(program:Int, name:String)"
  441. WriteLine OutGlad, "~tLocal n:Byte Ptr = name.ToUTF8String()"
  442. WriteLine OutGlad, "~tLocal res:Int = _glGetUniformLocation(program, n)"
  443. WriteLine OutGlad, "~tMemFree(n)"
  444. WriteLine OutGlad, "~tReturn res"
  445. WriteLine OutGlad, "End Function"
  446. 'WriteLine OutGlad, "?"
  447. OutGlad.Close
  448. End Method
  449. Method Warning(Out:TStream)
  450. WriteLine Out, "'"
  451. WriteLine Out, "' NOTE : Generated file. Do not edit. Your changes may be lost on the next update!"
  452. WriteLine Out, "' Generated by g2bmx on " + timestamp
  453. WriteLine Out, "'"
  454. End Method
  455. End Type
  456. Type Tty
  457. Field ty:String
  458. Field exty:String
  459. End Type
  460. Type Tproto
  461. Field proto:String
  462. Field exproto:String
  463. End Type