egra: prepare agg mini before calling `onPaint()` (and cleanup afterwards)
[iv.d.git] / glbinds / _generator / zgl.d
blob90b33c7d034f68f0b807dd0e0e3311c5a2f4dca9
1 import std.regex;
3 import arsd.dom;
5 import iv.strex;
6 import iv.vfs.io;
8 Document gldom;
11 // ////////////////////////////////////////////////////////////////////////// //
12 struct TypeAndName {
13 string name;
14 string type;
15 bool needReturn;
17 this (Element pp) {
18 assert(pp !is null);
20 string getName (Element nr) {
21 auto ne = nr.querySelector("name");
22 assert(ne !is null);
23 auto res = ne.innerText.xstrip;
24 assert(res.length);
25 return res;
28 string textNoNameIntr (Element el) {
29 string s;
30 foreach (Element child; el.children) {
31 if (child.tagName == "name") continue;
32 if (child.nodeType != NodeType.Text) s ~= textNoNameIntr(child); else s ~= child.nodeValue();
34 return s;
37 string fixTypes (string s) {
38 s = s.xstrip;
40 static struct Repl {
41 StaticRegex!char re;
42 string repl;
43 bool cont;
46 static Repl[$] replaces = [
47 Repl(ctRegex!(`^(Display|DMparams|XVisualInfo|GLXVideoCaptureDeviceNV)\s*\*$`), "/*$1*/void*"),
48 Repl(ctRegex!(`^(GLXFBConfigSGIX|GLXFBConfig|GLXContext)\s*\*$`), "/*$1*/void**"),
49 Repl(ctRegex!(`^(GLXFBConfigSGIX|GLXFBConfig|GLXContext)$`), "/*$1*/void*"),
50 // shitdoze start
51 Repl(ctRegex!(`^(HVIDEOINPUTDEVICENV|HVIDEOOUTPUTDEVICENV|HGPUNV|HPVIDEODEV)\s*\*$`), "/*$1*/void**"),
52 Repl(ctRegex!(`^(HPBUFFERARB|HPBUFFEREXT|HVIDEOOUTPUTDEVICENV|HPVIDEODEV|HPGPUNV|HGPUNV|HVIDEOINPUTDEVICENV|PGPU_DEVICE)$`), "/*$1*/void*"),
53 Repl(ctRegex!(`^const\s+(HGPUNV)\s*\*$`), "/*$1*/const(void)**"),
54 // shitdoze end
55 Repl(ctRegex!(`^const\s+(GLXContext)$`), "/*$1*/const(void)*"),
56 Repl(ctRegex!(`\bunsigned\s+int\b`), "uint", true),
57 Repl(ctRegex!(`\bunsigned\s+long\b`), "c_ulong", true),
58 Repl(ctRegex!(`\blong\b`), "c_long", true),
59 Repl(ctRegex!(`\bBool\b`), "int", true),
60 Repl(ctRegex!(`\bint32_t\b`), "int", true),
61 Repl(ctRegex!(`\bint64_t\b`), "long", true),
62 Repl(ctRegex!(`\bGLXVideoDeviceNV\b`), "uint", true), // unsigned int
63 Repl(ctRegex!(`^(Status|Font|GLXPbufferSGIX|Window|GLXDrawable|GLXVideoCaptureDeviceNV|GLXPbuffer|GLXPixmap|Pixmap|Colormap|GLXVideoSourceSGIX|GLXWindow|GLXVideoCaptureDeviceNV|GLXContextID)$`), "/*$1*/c_ulong"), // XID
64 //Repl(ctRegex!(`^(GLXVideoCaptureDeviceNV)$`), "/*$1*/c_ulong"), // XID
65 Repl(ctRegex!(`^(DMbuffer)$`), "/*$1*/void*"),
66 Repl(ctRegex!(`^const\s+([^\s*]+)\s*(\*+)$`), "const($1)$2"),
69 //if (s.indexOf("Display") >= 0) stderr.writeln("|", s, "|");
71 foreach (ref repl; replaces[]) {
72 auto mt = s.matchFirst(repl.re);
73 if (!mt.empty) {
74 s = s.replaceAll(repl.re, repl.repl);
75 if (!repl.cont) break;
78 for (;;) {
79 auto pos = s.indexOf("* ");
80 if (pos < 0) break;
81 ++pos;
82 while (pos < s.length && s[pos] == ' ') s = s[0..pos]~s[pos+1..$];
84 for (;;) {
85 auto pos = s.indexOf(" *");
86 if (pos < 0) break;
87 while (pos > 0 && s[pos-1] == ' ') --pos;
88 while (pos < s.length && s[pos] == ' ') s = s[0..pos]~s[pos+1..$];
90 if (s == "const GLchar*const*") s = "const(GLchar)**";
91 if (s == "const void*const*") s = "const(void)**";
92 return s;
95 string textNoName (Element el) {
96 return fixTypes(textNoNameIntr(el));
99 name = getName(pp);
100 if (name == "ref" || name == "in" || name == "out" || name == "version" || name == "align") name ~= "_";
102 type = textNoName(pp);
103 needReturn = (type != "void");
108 // ////////////////////////////////////////////////////////////////////////// //
109 void parseCommand (Element cmd) {
110 assert(cmd !is null);
111 auto nameres = cmd.querySelector("proto");
112 assert(nameres !is null);
114 auto fntype = TypeAndName(nameres);
116 switch (fntype.name) {
117 case "glCreateSyncFromCLeventARB": return;
118 case "glPathGlyphIndexRangeNV": return; // [2]
119 case "SwapBuffers": return; // shitdoze
120 case "ChoosePixelFormat": return; // shitdoze
121 case "DescribePixelFormat": return; // shitdoze
122 case "GetPixelFormat": return; // shitdoze
123 case "SetPixelFormat": return; // shitdoze
124 case "wglGetProcAddress": return; // shitdoze
125 case "glXGetProcAddress": return;
126 case "glXGetProcAddressARB": return;
127 default: break;
130 if (fntype.name.endsWith("SGIX")) {
131 //stderr.writeln(fntype.name);
132 return;
135 TypeAndName[] args;
136 foreach (Element param; cmd.querySelectorAll("param")) args ~= TypeAndName(param);
138 if (fntype.name == "glXChooseVisual") {
139 args[2].type = "const(int)*";
142 if (fntype.name == "glXGetProcAddress" || fntype.name == "glXGetProcAddressARB") {
143 args[0].type = "const(char)*";
146 string getArgs () {
147 string res = "(";
148 bool needComma = false;
149 foreach (const ref TypeAndName tn; args) {
150 if (needComma) res ~= ", "; else needComma = true;
151 res ~= tn.type;
152 res ~= " ";
153 res ~= tn.name;
155 res ~= ")";
156 return res;
159 string getCall () {
160 string res = "(";
161 bool needComma = false;
162 foreach (const ref TypeAndName tn; args) {
163 if (needComma) res ~= ", "; else needComma = true;
164 res ~= tn.name;
166 res ~= ")";
167 return res;
170 string intrName = fntype.name~"_Z_Z_";
171 string ldrName = intrName~"_loader_";
173 writeln("alias ", fntype.name, " = ", intrName, ";");
174 writeln("__gshared ", intrName, " = function ", fntype.type, " ", getArgs(), " { ", (fntype.needReturn ? "return " : ""), ldrName, getCall(), "; };");
175 // loader
176 writeln("private ", fntype.type, " ", ldrName, " ", getArgs(), " {");
177 writeln(" *cast(void**)&", intrName, " = glbindGetProcAddress(`", fntype.name, "`);");
178 writeln(" if (*cast(void**)&", intrName, " is null) assert(0, `OpenGL function '", fntype.name, "' not found!`);");
179 writeln(" ", (fntype.needReturn ? "return " : ""), intrName, getCall(), ";");
180 writeln("}");
183 private const(char)* glbfn_glGetString_loader (uint a00) {
184 *cast(void**)&glGetString_Z = glbindGetProcAddress(`glGetString`);
185 if (*cast(void**)&glGetString_Z is null) assert(0, `OpenGL function 'glGetString' not found!`);
186 return glGetString_Z(a00,);
191 write(fixPointers(nameres.innerText.xstrip), "_Z_Z (");
192 bool needComma = false;
193 foreach (Element param; cmd.querySelectorAll("param")) {
194 if (needComma) write(", "); else needComma = true;
195 write(fixPointers(param.innerText.xstrip));
197 writeln(");");
202 void main (string[] args) {
203 gldom = new XmlDocument(readTextFile(args.length == 1 ? "gl.xml" : args[1]));
204 writeln("// enums");
205 foreach (Element glenum; gldom.querySelectorAll("enums")) {
206 //writeln(glenum.getAttribute("namespace"), ":", glenum.getAttribute("group"), " -- ", glenum.getAttribute("comment"));
207 //<enum value="0x00000001" name="GL_CURRENT_BIT"/>
208 foreach (Element it; glenum.querySelectorAll("enum[value][name]")) {
209 string name = it.getAttribute("name").xstrip;
210 assert(name.length);
211 if (name[0] == '_') continue;
212 if (name.endsWith("SGIX")) continue;
213 string value = it.getAttribute("value").xstrip;
214 assert(value.length);
215 if (value[0] == '"') continue;
216 string api = it.getAttribute("api");
217 if (api == "gles2") continue; // fuck off
218 if (api.length && api != "gl") {
219 if (api.length) assert(0, api);
221 string type = it.getAttribute("type");
222 string pfx = "U";
223 if (type.length == 0 || type == "u") {
224 type = "uint";
225 } else if (type == "ull") {
226 type = "ulong";
227 pfx = "UL";
228 } else if (type.length) {
229 assert(0, type);
231 writeln("enum ", type, " ", name, " = ", value, pfx, ";");
234 writeln;
235 writeln("// API");
236 foreach (Element cmd; gldom.querySelectorAll("commands > command")) {
237 try {
238 parseCommand(cmd);
239 } catch (Throwable e) {
240 writeln("-------------------------");
241 writeln(cmd.outerHTML);
242 writeln("-------------------------");
243 throw e;
250 <command>
251 <proto>void <name>glApplyTextureEXT</name></proto>
252 <param group="LightTextureModeEXT"><ptype>GLenum</ptype> <name>mode</name></param>
253 </command>
254 <command>
255 <proto><ptype>GLboolean</ptype> <name>glAcquireKeyedMutexWin32EXT</name></proto>
256 <param><ptype>GLuint</ptype> <name>memory</name></param>
257 <param><ptype>GLuint64</ptype> <name>key</name></param>
258 <param><ptype>GLuint</ptype> <name>timeout</name></param>
259 </command>