| 1 |
"""Generate gl.py and gl_compat.py |
| 2 |
See : tools/requirements.txt. |
| 3 |
|
| 4 |
We are using the opengl-registry project to extract this information from |
| 5 |
https://raw.githubusercontent.com/KhronosGroup/OpenGL-Registry/master/xml/gl.xml |
| 6 |
|
| 7 |
A local version gl.xml can also be used. |
| 8 |
|
| 9 |
Usage: |
| 10 |
|
| 11 |
# Fetch gl.xml from Khronos Github repo |
| 12 |
python gengl.py |
| 13 |
python gengl.py --source url |
| 14 |
|
| 15 |
# Use local gl.xml |
| 16 |
python gengl.py --source local |
| 17 |
""" |
| 18 |
import sys |
| 19 |
from argparse import ArgumentParser, Namespace |
| 20 |
from pathlib import Path |
| 21 |
from typing import TextIO, Iterable |
| 22 |
|
| 23 |
from opengl_registry import Registry, RegistryReader |
| 24 |
|
| 25 |
REPO_ROOT = Path(__file__).parent.parent.resolve() |
| 26 |
DEST_PATH = REPO_ROOT / "pyglet" / "gl" |
| 27 |
|
| 28 |
|
| 29 |
def main() -> None: |
| 30 |
values = parse_args(sys.argv[1:]) |
| 31 |
if values.source == "url": |
| 32 |
|
| 33 |
reader = RegistryReader.from_url() |
| 34 |
else: |
| 35 |
|
| 36 |
reader = RegistryReader.from_file(Path(REPO_ROOT / "tools" / "gl.xml")) |
| 37 |
|
| 38 |
registry = reader.read() |
| 39 |
|
| 40 |
|
| 41 |
extensions = [ |
| 42 |
"GL_ARB_multisample", |
| 43 |
"EXT_texture_compression_s3tc", |
| 44 |
"GL_EXT_framebuffer_object", |
| 45 |
"GL_ARB_bindless_texture", |
| 46 |
"GL_ARB_gpu_shader_int64", |
| 47 |
"GL_NV_mesh_shader", |
| 48 |
] |
| 49 |
|
| 50 |
core_profile = registry.get_profile( |
| 51 |
api="gl", |
| 52 |
profile="core", |
| 53 |
version="4.6", |
| 54 |
extensions=extensions, |
| 55 |
) |
| 56 |
compat_profile = registry.get_profile( |
| 57 |
api="gl", |
| 58 |
profile="compat", |
| 59 |
version="4.6", |
| 60 |
extensions=extensions, |
| 61 |
) |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
core_writer = PygletGLWriter(registry=core_profile, out_module=DEST_PATH / "gl") |
| 70 |
core_writer.run() |
| 71 |
compat_writer = PygletGLWriter(registry=compat_profile, out_module=DEST_PATH / "gl_compat") |
| 72 |
compat_writer.run() |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
def parse_args(args: str) -> Namespace: |
| 78 |
parser = ArgumentParser() |
| 79 |
parser.add_argument("--source", choices=["local", "url"], default="url") |
| 80 |
return parser.parse_args(args) |
| 81 |
|
| 82 |
|
| 83 |
class PygletGLWriter: |
| 84 |
"""Writes gl.py, gl_compat.py, and gl_es.py.""" |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
types = { |
| 89 |
"GLenum": "c_uint", |
| 90 |
"GLboolean": "c_ubyte", |
| 91 |
"GLbitfield": "c_uint", |
| 92 |
"GLvoid": "None", |
| 93 |
"GLbyte": "c_byte", |
| 94 |
"GLubyte": "c_ubyte", |
| 95 |
"GLshort": "c_short", |
| 96 |
"GLushort": "c_ushort", |
| 97 |
"GLint": "c_int", |
| 98 |
"GLuint": "c_uint", |
| 99 |
"GLclampx": "c_uint", |
| 100 |
"GLsizei": "c_int", |
| 101 |
"GLfloat": "c_float", |
| 102 |
"GLclampf": "c_float", |
| 103 |
"GLdouble": "c_double", |
| 104 |
"GLclampd": "c_double", |
| 105 |
"GLchar": "c_char", |
| 106 |
"GLintptr": "c_ptrdiff_t", |
| 107 |
"GLsizeiptr": "c_ptrdiff_t", |
| 108 |
"GLint64": "c_int64", |
| 109 |
"GLuint64": "c_uint64", |
| 110 |
"GLuint64EXT": "c_uint64", |
| 111 |
"GLsync": "POINTER(struct___GLsync)", |
| 112 |
"GLDEBUGPROC": "CFUNCTYPE(None, GLenum, GLenum, GLuint, GLenum, GLsizei, POINTER(GLchar), POINTER(GLvoid))", |
| 113 |
} |
| 114 |
|
| 115 |
pythontypes = { |
| 116 |
"GLenum": "int", |
| 117 |
"GLboolean": "int", |
| 118 |
"GLbitfield": "int", |
| 119 |
"GLvoid": "None", |
| 120 |
"GLbyte": "bytes", |
| 121 |
"GLubyte": "int", |
| 122 |
"GLshort": "int", |
| 123 |
"GLushort": "int", |
| 124 |
"GLint": "int", |
| 125 |
"GLuint": "int", |
| 126 |
"GLclampx": "int", |
| 127 |
"GLsizei": "int", |
| 128 |
"GLfloat": "float", |
| 129 |
"GLclampf": "float", |
| 130 |
"GLdouble": "float", |
| 131 |
"GLclampd": "float", |
| 132 |
"GLchar": "bytes", |
| 133 |
|
| 134 |
|
| 135 |
"GLint64": "int", |
| 136 |
"GLuint64": "int", |
| 137 |
"GLuint64EXT": "int", |
| 138 |
} |
| 139 |
exclude_commands = set() |
| 140 |
|
| 141 |
def __init__(self, *, registry: Registry, out_module: Path): |
| 142 |
self._registry = registry |
| 143 |
self._out_module = out_module |
| 144 |
self._out = None |
| 145 |
self._stub = None |
| 146 |
self._all = [] |
| 147 |
self._commands = [] |
| 148 |
|
| 149 |
def run(self): |
| 150 |
"""Write the file and close""" |
| 151 |
with open(self._out_module.with_suffix(".py"), mode='w') as out: |
| 152 |
self.write_template(out, REPO_ROOT / "tools" / "gl.template") |
| 153 |
self.write_types(out) |
| 154 |
self.write_enums(out) |
| 155 |
self.write_commands(out) |
| 156 |
self.write_footer(out) |
| 157 |
|
| 158 |
with open(self._out_module.with_suffix(".pyi"), mode='w') as stub: |
| 159 |
self.write_template(stub, REPO_ROOT / "tools" / "gl_stub.template") |
| 160 |
self.write_types(stub) |
| 161 |
self.write_enum_stubs(stub) |
| 162 |
self.write_command_stubs(stub) |
| 163 |
|
| 164 |
def write_lines(self, fp: TextIO, lines: Iterable[str]) -> None: |
| 165 |
"""Write one or several lines to the out file.""" |
| 166 |
for line in lines: |
| 167 |
fp.write(line) |
| 168 |
fp.write("\n") |
| 169 |
|
| 170 |
def write_template(self, fp: TextIO, template: Path) -> None: |
| 171 |
"""Write the header.""" |
| 172 |
with open(template) as fd: |
| 173 |
fp.write(fd.read()) |
| 174 |
|
| 175 |
def write_types(self, fp: TextIO) -> None: |
| 176 |
"""Write all types.""" |
| 177 |
self.write_lines(fp, ["# GL type definitions"]) |
| 178 |
self.write_lines(fp, [f"{k} = {v}" for k, v in self.types.items()]) |
| 179 |
self.write_lines(fp, [""]) |
| 180 |
self._all.extend(self.types.keys()) |
| 181 |
|
| 182 |
def write_enums(self, fp: TextIO) -> None: |
| 183 |
"""Write all enums.""" |
| 184 |
self.write_lines(fp, ["# GL enumerant (token) definitions"]) |
| 185 |
self.write_lines(fp, [ |
| 186 |
f"{e.name} = {e.value_int}" |
| 187 |
for e in sorted(self._registry.enums.values()) |
| 188 |
]) |
| 189 |
self.write_lines(fp, [""]) |
| 190 |
self._all.extend(self._registry.enums.keys()) |
| 191 |
|
| 192 |
def write_enum_stubs(self, fp: TextIO) -> None: |
| 193 |
"""Write type annotations for all enums.""" |
| 194 |
self.write_lines(fp, ["# GL enumerant (token) definitions"]) |
| 195 |
self.write_lines(fp, [ |
| 196 |
f"{e.name}: int" |
| 197 |
for e in sorted(self._registry.enums.values()) |
| 198 |
]) |
| 199 |
self.write_lines(fp, [""]) |
| 200 |
|
| 201 |
def write_commands(self, fp: TextIO) -> None: |
| 202 |
"""Write all commands.""" |
| 203 |
self.write_lines(fp, ["# GL command definitions"]) |
| 204 |
|
| 205 |
|
| 206 |
for cmd in sorted(self._registry.commands.values()): |
| 207 |
if cmd.name in self.exclude_commands: |
| 208 |
continue |
| 209 |
|
| 210 |
|
| 211 |
if "*" in cmd.proto: |
| 212 |
restype = f"POINTER({cmd.ptype})" |
| 213 |
else: |
| 214 |
restype = cmd.ptype or "None" |
| 215 |
|
| 216 |
|
| 217 |
arguments = [] |
| 218 |
for param in cmd.params: |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if "void" in param.value: |
| 223 |
arguments.append("POINTER(GLvoid)") |
| 224 |
else: |
| 225 |
|
| 226 |
if not self.types.get(param.ptype): |
| 227 |
raise ValueError(f"ptype {param.ptype} not a known type") |
| 228 |
|
| 229 |
if param.value.count("*") == 2: |
| 230 |
arguments.append(f"POINTER(POINTER({param.ptype}))") |
| 231 |
elif param.value.count("*") == 1: |
| 232 |
arguments.append(f"POINTER({param.ptype})") |
| 233 |
else: |
| 234 |
arguments.append(param.ptype) |
| 235 |
|
| 236 |
argtypes = ", ".join(arguments) |
| 237 |
requires = f"OpenGL {cmd.requires}" if cmd.requires else "None" |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
self.write_lines(fp, [ |
| 242 |
f"{cmd.name} = _link_function('{cmd.name}', {restype}, [{argtypes}], requires='{requires}')", |
| 243 |
|
| 244 |
]) |
| 245 |
self._all.append(cmd.name) |
| 246 |
|
| 247 |
self.write_lines(fp, [""]) |
| 248 |
|
| 249 |
def write_footer(self, fp: TextIO) -> None: |
| 250 |
"""Write __all__ section.""" |
| 251 |
self.write_lines(fp, [ |
| 252 |
"", |
| 253 |
"__all__ = [", |
| 254 |
*[f" '{name}'," for name in self._all], |
| 255 |
"]", |
| 256 |
]) |
| 257 |
|
| 258 |
def write_command_stubs(self, fp: TextIO) -> None: |
| 259 |
"""Write type annotations for all commands.""" |
| 260 |
self.write_lines(fp, ["# GL command definitions"]) |
| 261 |
|
| 262 |
|
| 263 |
for cmd in sorted(self._registry.commands.values()): |
| 264 |
if cmd.name in self.exclude_commands: |
| 265 |
continue |
| 266 |
|
| 267 |
|
| 268 |
if "*" in cmd.proto: |
| 269 |
restype = f"_Pointer[{cmd.ptype}]" |
| 270 |
else: |
| 271 |
restype = cmd.ptype or "None" |
| 272 |
|
| 273 |
|
| 274 |
arguments = [] |
| 275 |
names = [] |
| 276 |
for param in cmd.params: |
| 277 |
|
| 278 |
names.append(param.name) |
| 279 |
|
| 280 |
|
| 281 |
if "void" in param.value: |
| 282 |
|
| 283 |
|
| 284 |
arguments.append("_Pointer[GLvoid] | Any") |
| 285 |
else: |
| 286 |
|
| 287 |
if not self.types.get(param.ptype): |
| 288 |
raise ValueError(f"ptype {param.ptype} not a known type") |
| 289 |
|
| 290 |
if param.value.count("*") == 2: |
| 291 |
arguments.append(f"_Pointer[_Pointer[{param.ptype}]] | Any") |
| 292 |
elif param.value.count("*") == 1: |
| 293 |
arguments.append(f"_Pointer[{param.ptype}] | Any") |
| 294 |
else: |
| 295 |
arguments.append(param.ptype) |
| 296 |
|
| 297 |
|
| 298 |
argannotations = ", ".join( |
| 299 |
f"{name}: {f'{arg} | {self.pythontypes[arg]}' if arg in self.pythontypes else arg}" for name, arg in |
| 300 |
zip(names, arguments)) |
| 301 |
|
| 302 |
self.write_lines(fp, [ |
| 303 |
f"def {cmd.name}({argannotations}) -> {self.pythontypes.get(restype, restype)}: ..." |
| 304 |
]) |
| 305 |
|
| 306 |
|
| 307 |
if __name__ == "__main__": |
| 308 |
main() |