From 107929eae003ba53d2cf9a5982bbc56966f5ab42 Mon Sep 17 00:00:00 2001 From: Ketmar Dark Date: Fri, 5 Feb 2016 01:50:48 +0200 Subject: [PATCH] lazy ogl function loading --- glbinds.d | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 9 deletions(-) diff --git a/glbinds.d b/glbinds.d index 3b6b6a3..be528ff 100644 --- a/glbinds.d +++ b/glbinds.d @@ -1,5 +1,12 @@ module glbinds; +// lazy loading +version = glbind_lazy_load; + +// show loaded functions +//version = glbind_debug; + + static if (__traits(compiles, () { import arsd.simpledisplay; })) { public import arsd.simpledisplay; } else { @@ -151,21 +158,55 @@ alias glbfn_glGetIntegerv = void function (GLenum, GLint*); // ////////////////////////////////////////////////////////////////////////// // -private string glbindCreateInternalVars () { - string res; - foreach (name; __traits(allMembers, mixin(__MODULE__))) { - static if (name.length > 6 && name[0..6] == "glbfn_") { - //pragma(msg, name); - // create pointer - res ~= "__gshared "~name~" "~name[6..$]~";\n"; +version(glbind_lazy_load) { + private string glbindCreateInternalVars () { + string res; + foreach (name; __traits(allMembers, mixin(__MODULE__))) { + static if (name.length > 6 && name[0..6] == "glbfn_") { + //pragma(msg, mixin(name)); + import std.traits; + //pragma(msg, ReturnType!(mixin(name))); + //pragma(msg, Parameters!(mixin(name))); + // create pointer + string pars, call; + foreach (immutable idx, immutable ptype; Parameters!(mixin(name))) { + import std.conv : to; + pars ~= ", "~ptype.stringof~" a"~to!string(idx); + call ~= "a"~to!string(idx)~","; + } + if (pars.length) pars = pars[2..$]; + res ~= "__gshared "~name~" "~name[6..$]~" = function "~ + ReturnType!(mixin(name)).stringof~" "~ + /*Parameters!(mixin(name)).stringof*/"("~pars~") nothrow{\n"~ + // build loader + " "~name[6..$]~" = cast("~name~")glGetProcAddress(`"~name[6..$]~"`);\n"~ + " if ("~name[6..$]~" is null) assert(0, `OpenGL function '"~name[6..$]~"' not found!`);\n"~ + " version(glbind_debug) { import core.stdc.stdio; fprintf(stderr, \"GLBIND: '"~name[6..$]~"'\\n\"); }\n"~ + " "~(is(ReturnType!(mixin(name)) == void) ? "" : "return ")~name[6..$]~"("~call~");\n"~ + //" assert(0, `"~name[6..$]~"`);\n"~ + "};\n"; + } } + return res; + } +} else { + private string glbindCreateInternalVars () { + string res; + foreach (name; __traits(allMembers, mixin(__MODULE__))) { + static if (name.length > 6 && name[0..6] == "glbfn_") { + //pragma(msg, name); + // create pointer + res ~= "__gshared "~name~" "~name[6..$]~";\n"; + } + } + return res; } - return res; } mixin(glbindCreateInternalVars()); public void glbindLoadFunctions () { + version(glbind_lazy_load) {} else { foreach (name; __traits(allMembers, mixin(__MODULE__))) { static if (name.length > 6 && name[0..6] == "glbfn_") { //pragma(msg, name); @@ -174,7 +215,7 @@ public void glbindLoadFunctions () { mixin("if ("~name[6..$]~" is null) assert(0, `OpenGL function '"~name[6..$]~"' not found!`);"); } } - //assert(0, "boo"); + } } -- 2.11.4.GIT