# OpenGL Processing "GL.XML"

This topic is 1072 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

for my current project, I need an OpenGL Loader Generator, which I want to programm myself.

Obviously there have been some created before yet, but those do not respect my "architecture" or however I'd pronounce it.

Now I want to parse the OpenGL XML Spec, but I am a bit confused.

It is structured as follow:

<feature api="gl" name="GL_VERSION_4_2" number="4.2">
<require comment="New aliases for old tokens">
<enum name="GL_COPY_WRITE_BUFFER_BINDING"/>
<enum name="GL_TRANSFORM_FEEDBACK_ACTIVE"/>
<enum name="GL_TRANSFORM_FEEDBACK_PAUSED"/>
</require>
<require comment="Reuse tokens from ARB_base_instance (none)">
</require>
<require comment="Reuse tokens from ARB_shading_language_420pack (none)">
</require>
<require comment="Reuse tokens from ARB_transform_feedback_instanced (none)">
</require>
<require comment="Reuse tokens from ARB_compressed_texture_pixel_storage">
...


The overall structure is comprehensible. However, what makes me wonder, are those "<command>" tags.
They tell which command is supposed to be introduced with this specific set, but there are too few of them in this specific set.
Not enough for a "rendering api". This leads me to the assumption, that OpenGL 4.2 is only an addition, a feature, to another version.
But which version?
Do I have to parse it all up from OpenGL 3.2 and then it all just sums up?

I hope someone can help.
Greetings,
Techieman

##### Share on other sites
You perhaps didn't copy in the XML right, as there are no <command> tags in your sample...

But in any case, yes, sort of, your assumption is correct.

If you look at gl.xml you see that the file is broken into several sections. The full definitions of each function (command), enum, etc. is listed first.

Afterwards is a list of <feature> elements that are meant to be parsed in order. Each feature extends from the previous feature. This means that you actually have to start at the very first 1.0, not 3.2. For each <requires> thare are some attributes like profile that you can use to skip or ignore those sections of the feature. Note that there are also <remove> elements, notable in 3.2 under the "core" profile, that has the opposite effect of requires.

Essentially, parse forward all the feature levels up to the level you want to support. For each feature, parse all of its requires and removes elements; filter them out based on profile (core or compatibility). For any enum or command in a <requires> tag, add the name to a set. For any enum or command in a <remove> tag, remove the name from the set. After building up the full list of set, process the corresponding enums and commands from the earlier part of the document.

##### Share on other sites

Thank you both,

Currently I am trying it this way:
My parser gets an number passed as parameter, which describes the version we want.

Then every <feature>-Set is being parsed. Every <require> and <remove> is being parsed as well. (require -> add enum/function to list; remove -> vice versa).
At the end of each feature it compares, if the number passed to the parser equals the number defined as attribute of the feature, then the parser will stop.

After this, every <enum> and <command> is being parsed and checked against if it is part in of a feature set.

If it is part, then it's added to a list.

Currently I am just encountering a bug in my XML parser for the following:

        <command>
<proto group="Boolean"><ptype>GLboolean</ptype> <name>glIsBufferARB</name></proto>
<param><ptype>GLuint</ptype> <name>buffer</name></param>
<alias name="glIsBuffer"/>
</command>


This XML-excerpt is being parsed by the following code:

        for command in root.find("commands").findall("command"):
if command.find("proto").find("name").text in used_functions:
function_name = command.find("proto").find("name").text
function_return = command.find("proto").text
function_param_list = list()

for parameters in command.findall("params"):
function_param_list.append(parameters.find("ptype").text)


It return None, although there is some text.

Again, thanks for your help, mates!

EDIT:
Recognized, that those <commands>, which the parser is used to return None for, when looking up the return type,
have their return types wrapped in another tag. Not sure why it's that.

Well, it's been late lately and I am tired.

Edited by Techieman

##### Share on other sites

So, I've got an pretty basic parser to work. With little more effort, I could group constants into "Enum classes" as known from C++ 11.

Disclaimer:
The following code doesn't produce anything yet. This code can be easily reused, to generate headers and/or binding for other languages such as C#.

#
# GLoo - an opengl loader generator
#
# GLoo is an opengl generator written in Python 3.
# It creates files based on specifications provided
# by khronos.
#
# By MyNameIsJulien (J. Kirsch)
# @MyNameIsJulien
#
# You can do anything you want with this script,
# as long i am in the credits.

import urllib.request

url = "https://cvs.khronos.org/svn/repos/ogl/trunk/doc/registry/public/api/" + spec + ".xml"

class Command:

def __init__(self, name: str, argument_list: list(), return_type: str):
self.name = name
self.return_type = return_type
self.argument_list = argument_list

class Constant:

def __init__(self, name: str, value: str):
self.name = name
self.value = value

class Spec:

def __init__(self, specification: str, feature_level: str):
self.specification = specification
self.functions = list()
self.constants = list()

import xml.etree.ElementTree as Xml

root = Xml.fromstring(self.specification)

used_functions = list()
used_constants = list()

for feature_set in root.findall("feature"):

for requirement in feature_set.findall("require"):
for command in requirement.findall("command"):
used_functions.append(command.attrib['name'])

for enum in requirement.findall("enum"):
used_constants.append(enum.attrib['name'])

for removals in feature_set.findall("remove"):
for command in removals.findall("command"):
if command.attrib['name'] in self.functions:
self.functions.remove(command.attrib['name'])

for enum in removals.findall("command"):
if enum.attrib['name'] in self.constants:
self.constants.remove(enum.attrib['name'])

# Break after the feature level has been processed
if feature_level in feature_set.attrib['name']:
break

# Finding each individual command and parsing its definition
for command in root.find("commands").findall("command"):
if command.find("proto").find("name").text in used_functions:
function_name = command.find("proto").find("name").text
function_return = command.find("proto").text
function_param_list = list()

if function_return is None:
function_return = command.find("proto").find("ptype").text

for parameters in command.findall("params"):
function_param_list.append(parameters.find("ptype").text)

self.functions.append(Command(function_name, function_param_list, function_return))

for enums in root.findall("enums"):
for enum in enums.findall("enum"):
if enum.attrib['name'] in used_constants:
constant_name = enum.attrib['name']
constant_value = enum.attrib['value']
self.constants.append(Constant(constant_name, constant_value))

print(len(self.functions))
print(len(self.constants))

def get_functions(self):
return self.functions

def get_constants(self):
return self.constants

# Code generation will be done here
def generate(spec_name: str,spec: Spec):
pass

def main():
parse = Spec(spec, "4.2")
generate("gl", parse)

if __name__ == '__main__':
main()



EDIT:
Fucked up something with the parameters. Every returned function has no parameters. LOL if game programming would be that easy

Edited by Techieman

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5

• 28
• 40
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631738
• Total Posts
3001950
×