X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=scripts%2Fextgen.py;h=45094a4df35df26fe2608e0f07a4005378ec125e;hp=7a1b18629cafa4b2e5a116b316a2b9beb8e63346;hb=55dbeb5e04516699b8415104e346243d5e4c48c9;hpb=f159ebc4120fb057c9d59efb0e5c7c36bff6f40a diff --git a/scripts/extgen.py b/scripts/extgen.py index 7a1b1862..45094a4d 100755 --- a/scripts/extgen.py +++ b/scripts/extgen.py @@ -10,8 +10,8 @@ import itertools if len(sys.argv)<2: print """Usage: - extgen.py [] [ ...] - extgen.py [] + extgen.py [api] [] [ ...] + extgen.py [api] [] Reads gl.xml and generates files to use . Any promoted functions are exposed with their promoted names. If extensions are given, @@ -23,34 +23,50 @@ absent, the extension's lowercased name is used. Anything after the last dot in is removed and replaced with cpp and h.""" sys.exit(0) -ext = sys.argv[1] +target_api = "gl" + +i = 1 +if sys.argv[i].startswith("gl"): + target_api = sys.argv[i] + i += 1 + +target_ext = sys.argv[i] +backport_ext = None out_base = None -if ext.endswith(".glext"): - fn = ext - ext = None - ver = None +ignore_things = [] +if target_ext.endswith(".glext"): + fn = target_ext + target_ext = None + core_version = None secondary = [] for line in open(fn): parts = line.split() if parts[0]=="extension": - ext = parts[1] + target_ext = parts[1] elif parts[0]=="core_version": - ver = parts[1] + if parts[1]==target_api: + core_version = parts[2] elif parts[0]=="secondary": secondary.append(parts[1]) - if len(sys.argv)>=3: - out_base = os.path.splitext(sys.argv[2])[0] + elif parts[0]=="backport": + backport_ext = parts[1] + elif parts[0]=="ignore": + ignore_things.append(parts[1]) + if i+11: + print "Warning: multiple backport extension candidates: %s"%(" ".join(e.name for e in backport_ext_candidates)) + + for e in backport_ext_candidates: + if e.base_name==target_ext.base_name: + backport_ext = e + + if not backport_ext and len(backport_ext_candidates)==1: + print "Warning: potential backport extension has mismatched name: %s"%backport_ext_candidates[0].name for f in funcs: - if f.source: - # Typedefs for early core functions are not available in all - # implementations - f.typedef = "PFN%sPROC"%f.source.name.upper() + f.typedef = "FPtr_%s"%f.name + +if target_api in target_ext.supported_apis: + source_ext = target_ext +else: + candidates = {} + for t in itertools.chain(funcs, enums): + for s in t.sources: + if target_api in s.supported_apis: + candidates[s.extension.name] = candidates.get(s.extension.name, 0)+1 + if candidates: + source_ext = extensions[max(candidates.iteritems(), key=(lambda x: x[1]))[0]] else: - f.typedef = "PFN%sPROC"%f.name.upper() + source_ext = None -if ver: - ver = map(int, ver.split('.')) +if funcs or enums: + any_supported = False + all_supported = True + for t in itertools.chain(funcs, enums): + if target_api in t.supported_apis: + any_supported = True + else: + all_supported = False + + if not any_supported: + print "Warning: %s is not supported by the target API"%target_ext.name + elif not all_supported: + print "Warning: %s is only partially supported by the target API"%target_ext.name + unsupported = "" + label = "Warning: Unsupported tokens: " + for t in itertools.chain(funcs, enums): + if target_api not in t.supported_apis: + if unsupported and len(label)+len(unsupported)+2+len(t.name)>78: + print label+unsupported + label = " "*len(label) + unsupported = "" + if unsupported: + unsupported += ", " + unsupported += t.name + if unsupported: + print label+unsupported ### Output ### out = file(out_base+".h", "w") -out.write("#ifndef MSP_GL_%s_\n"%ext.upper()) -out.write("#define MSP_GL_%s_\n"%ext.upper()) +out.write("#ifndef MSP_GL_%s_\n"%target_ext.name.upper()) +out.write("#define MSP_GL_%s_\n"%target_ext.name.upper()) out.write(""" #include @@ -257,24 +416,37 @@ namespace GL { if funcs or enums: if funcs: - out.write("#if defined(__APPLE__) || !defined(GL_%s)\n"%ext) for f in funcs: - out.write("typedef %s (*%s)(%s);\n"%(f.return_type, f.typedef, ", ".join(f.params))) - out.write("#endif\n\n") + out.write("typedef %s (APIENTRY *%s)(%s);\n"%(f.return_type, f.typedef, ", ".join(f.params))) + out.write("\n") if enums: - if ver: - out.write("#ifndef GL_VERSION_%s\n"%"_".join(map(str, ver))) - else: - out.write("#ifndef GL_%s\n"%ext) + api_prefix = "GL" + if target_api=="gles2": + api_prefix = "GL_ES" + + enums_by_category = {} for e in enums: - out.write("#define %s 0x%04X\n"%(e.name, e.value)) - out.write("#endif\n\n") + cat = None + if e.version: + cat = api_prefix+"_VERSION_"+"_".join(map(str, e.version)) + elif e.extension: + cat = "GL_"+e.extension.name + enums_by_category.setdefault(cat, []).append(e) + + for cat in sorted(enums_by_category.keys()): + if cat: + out.write("#ifndef %s\n"%cat) + for e in enums_by_category[cat]: + out.write("#define %s 0x%04X\n"%(e.name, e.value)) + if cat: + out.write("#endif\n") + out.write("\n") for f in funcs: out.write("extern %s %s;\n"%(f.typedef, f.name)) -out.write("extern Extension %s;\n"%ext) +out.write("extern Extension %s;\n"%target_ext.name) out.write(""" } // namespace GL @@ -284,15 +456,23 @@ out.write(""" """) out = file(out_base+".cpp", "w") -out.write("#include \"%s.h\"\n"%ext.lower()) +out.write("#include \"%s.h\"\n"%target_ext.name.lower()) -out.write(""" +if funcs: + out.write(""" #ifdef __APPLE__ -#define GET_PROC_ADDRESS(x) ::x +#define GET_PROC_ADDRESS(x) &::x #else #define GET_PROC_ADDRESS(x) get_proc_address(#x) #endif +#ifdef WIN32 +#define GET_PROC_ADDRESS_1_1(x) &::x +#else +#define GET_PROC_ADDRESS_1_1(x) GET_PROC_ADDRESS(x) +#endif +""") +out.write(""" namespace Msp { namespace GL { @@ -301,33 +481,44 @@ namespace GL { for f in funcs: out.write("%s %s = 0;\n"%(f.typedef, f.name)) -out.write("\nExtension::SupportLevel init_%s()\n{\n"%ext.lower()) -out.write("#ifdef GL_%s\n"%ext) -if ver: - out.write("\tif(is_version_at_least(%d, %d)"%tuple(ver)) +out.write("\nExtension::SupportLevel init_%s()\n{\n"%target_ext.name.lower()) +if core_version: + out.write("\tif(is_version_at_least(%d, %d)"%tuple(core_version)) if backport_ext: - out.write(" || is_supported(\"GL_%s\")"%backport_ext) + out.write(" || is_supported(\"GL_%s\")"%backport_ext.name) out.write(")\n\t{\n") - if funcs: - for f in funcs: - out.write("\t\t%s = reinterpret_cast<%s>(GET_PROC_ADDRESS(%s));\n"%(f.name, f.typedef, f.name)) + for f in funcs: + if target_api in f.supported_apis: + gpa_suffix = "" + if f.version is not None and f.version<=[1, 1]: + gpa_suffix = "_1_1" + out.write("\t\t%s = reinterpret_cast<%s>(GET_PROC_ADDRESS%s(%s));\n"%(f.name, f.typedef, gpa_suffix, f.name)) out.write("\t\treturn Extension::CORE;\n") out.write("\t}\n") -if ext!=backport_ext: - out.write("\tif(is_supported(\"GL_%s\"))\n\t{\n"%(ext)) - if funcs: - for f in funcs: - n = f.name - if f.source: - n = f.source.name - out.write("\t\t%s = reinterpret_cast<%s>(GET_PROC_ADDRESS(%s));\n"%(f.name, f.typedef, n)) +if source_ext and source_ext!=backport_ext: + out.write("\tif(is_supported(\"GL_%s\"))\n\t{\n"%(source_ext.name)) + for f in funcs: + if f.sources: + src = None + for s in f.sources: + if s.name.endswith(source_ext.ext_type): + src = s + break + if not src: + src = f.sources[0] + else: + src = f + + if target_api in src.supported_apis: + if not src.name.endswith(source_ext.ext_type): + print "Warning: %s does not match extension type %s"%(src.name, source_ext.ext_type) + out.write("\t\t%s = reinterpret_cast<%s>(GET_PROC_ADDRESS(%s));\n"%(f.name, f.typedef, src.name)) out.write("\t\treturn Extension::EXTENSION;\n") out.write("\t}\n") -out.write("#endif\n") out.write("\treturn Extension::UNSUPPORTED;\n") out.write("}\n") -out.write("\nExtension %s(\"GL_%s\", init_%s);\n"%(ext, ext, ext.lower())) +out.write("\nExtension %s(\"GL_%s\", init_%s);\n"%(target_ext.name, target_ext.name, target_ext.name.lower())) out.write(""" } // namespace GL