Class: MilkTea::OpenGLRegistry::Generator
- Inherits:
-
Object
- Object
- MilkTea::OpenGLRegistry::Generator
- Defined in:
- lib/milk_tea/bindings/opengl_registry.rb
Constant Summary collapse
- KHRONOS_TYPE_REWRITES =
{ "khronos_float_t" => "float", "khronos_int8_t" => "int8_t", "khronos_uint8_t" => "uint8_t", "khronos_int16_t" => "int16_t", "khronos_uint16_t" => "uint16_t", "khronos_int32_t" => "int32_t", "khronos_uint32_t" => "uint32_t", "khronos_int64_t" => "int64_t", "khronos_uint64_t" => "uint64_t", "khronos_intptr_t" => "intptr_t", "khronos_ssize_t" => "ptrdiff_t", }.freeze
Instance Method Summary collapse
- #active_surface(registry) ⇒ Object
- #api_list_matches?(api_list) ⇒ Boolean
- #api_matches?(api) ⇒ Boolean
- #apply_blocks(blocks, enums:, commands:) ⇒ Object
- #block_matches?(block) ⇒ Boolean
- #command_argument_list(command) ⇒ Object
- #command_parameter_list(command) ⇒ Object
- #command_proc_type_name(command) ⇒ Object
- #command_proc_var_name(command) ⇒ Object
- #compare_versions(left, right) ⇒ Object
- #emit_command_definition(command) ⇒ Object
- #emit_command_definitions(commands) ⇒ Object
- #emit_command_proc_typedefs(commands) ⇒ Object
- #emit_command_storage(commands) ⇒ Object
- #emit_enums(enums) ⇒ Object
- #emit_header(types:, enums:, commands:) ⇒ Object
- #emit_loader_api ⇒ Object
- #emit_loader_implementation(commands) ⇒ Object
- #emit_public_command_declarations(commands) ⇒ Object
- #emit_types(types) ⇒ Object
- #flatten_node(node, omit_name: false) ⇒ Object
- #generate ⇒ Object
-
#initialize(xml_path: nil, xml_source: nil, api: DEFAULT_API, version: DEFAULT_VERSION, profile: DEFAULT_PROFILE, extensions: []) ⇒ Generator
constructor
A new instance of Generator.
- #normalize_c_fragment(fragment) ⇒ Object
- #parse_blocks(parent_element) ⇒ Object
- #parse_commands(root) ⇒ Object
- #parse_enums(root) ⇒ Object
- #parse_extensions(root) ⇒ Object
- #parse_features(root) ⇒ Object
- #parse_registry ⇒ Object
- #parse_types(root) ⇒ Object
- #profile_matches?(profile) ⇒ Boolean
- #resolve_enum_value(entry, enums_by_name, seen = Set.new) ⇒ Object
- #rewrite_type_source(source) ⇒ Object
- #version_at_most?(left, right) ⇒ Boolean
- #version_key(version) ⇒ Object
Constructor Details
#initialize(xml_path: nil, xml_source: nil, api: DEFAULT_API, version: DEFAULT_VERSION, profile: DEFAULT_PROFILE, extensions: []) ⇒ Generator
Returns a new instance of Generator.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 69 def initialize(xml_path: nil, xml_source: nil, api: DEFAULT_API, version: DEFAULT_VERSION, profile: DEFAULT_PROFILE, extensions: []) raise ArgumentError, "xml_path or xml_source is required" if xml_path.nil? && xml_source.nil? @xml_source = xml_source || File.read(xml_path) @source_label = xml_path ? File.(xml_path.to_s) : "<memory>" @api = api @version = version @profile = profile @extensions = extensions.to_set end |
Instance Method Details
#active_surface(registry) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 207 def active_surface(registry) enums = Set.new commands = Set.new registry.fetch(:features) .select { |feature| api_matches?(feature.api) && version_at_most?(feature.number, @version) } .sort_by { |feature| version_key(feature.number) } .each { |feature| apply_blocks(feature.blocks, enums:, commands:) } @extensions.each do |extension_name| extension = registry.fetch(:extensions)[extension_name] next if extension.nil? next unless api_list_matches?(extension.supported) apply_blocks(extension.blocks, enums:, commands:) end active_enums = enums.map { |name| registry.fetch(:enums).fetch(name) }.sort_by(&:order) active_commands = commands.map { |name| registry.fetch(:commands).fetch(name) }.sort_by(&:order) [active_enums, active_commands] end |
#api_list_matches?(api_list) ⇒ Boolean
256 257 258 259 260 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 256 def api_list_matches?(api_list) return true if api_list.nil? || api_list.empty? api_list.split(/[\s,|]+/).include?(@api) end |
#api_matches?(api) ⇒ Boolean
252 253 254 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 252 def api_matches?(api) api == @api end |
#apply_blocks(blocks, enums:, commands:) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 229 def apply_blocks(blocks, enums:, commands:) blocks.each do |block| next unless block_matches?(block) case block.kind when :require block.enums.each { |name| enums << name } block.commands.each { |name| commands << name } when :remove block.enums.each { |name| enums.delete(name) } block.commands.each { |name| commands.delete(name) } end end end |
#block_matches?(block) ⇒ Boolean
244 245 246 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 244 def block_matches?(block) profile_matches?(block.profile) && api_list_matches?(block.api) end |
#command_argument_list(command) ⇒ Object
497 498 499 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 497 def command_argument_list(command) command.params.map(&:name).join(", ") end |
#command_parameter_list(command) ⇒ Object
491 492 493 494 495 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 491 def command_parameter_list(command) return "void" if command.params.empty? command.params.map(&:source).join(", ") end |
#command_proc_type_name(command) ⇒ Object
483 484 485 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 483 def command_proc_type_name(command) "mtlang_gl_proc_#{command.name}" end |
#command_proc_var_name(command) ⇒ Object
487 488 489 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 487 def command_proc_var_name(command) "mtlang_gl_ptr_#{command.name}" end |
#compare_versions(left, right) ⇒ Object
270 271 272 273 274 275 276 277 278 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 270 def compare_versions(left, right) lhs = version_key(left) rhs = version_key(right) width = [lhs.length, rhs.length].max lhs.fill(0, lhs.length...width) rhs.fill(0, rhs.length...width) lhs <=> rhs end |
#emit_command_definition(command) ⇒ Object
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 461 def emit_command_definition(command) lines = [] lines << "#{command.return_type} MTLANG_GL_APIENTRY #{command.name}(#{command_parameter_list(command)})" lines << "{" lines << " if (#{command_proc_var_name(command)} == NULL)" lines << " {" lines << " #{command_proc_var_name(command)} = (#{command_proc_type_name(command)}) mtlang_gl_require_proc(\"#{command.name}\");" lines << " }" call = "#{command_proc_var_name(command)}(#{command_argument_list(command)})" if command.return_type == "void" lines << "" lines << " #{call};" else lines << "" lines << " return #{call};" end lines << "}" lines.join("\n") end |
#emit_command_definitions(commands) ⇒ Object
455 456 457 458 459 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 455 def emit_command_definitions(commands) return "" if commands.empty? commands.map { |command| emit_command_definition(command) }.join("\n\n") end |
#emit_command_proc_typedefs(commands) ⇒ Object
361 362 363 364 365 366 367 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 361 def emit_command_proc_typedefs(commands) return "" if commands.empty? commands.map do |command| "typedef #{command.return_type} (MTLANG_GL_APIENTRY *#{command_proc_type_name(command)})(#{command_parameter_list(command)});" end.join("\n") end |
#emit_command_storage(commands) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 369 def emit_command_storage(commands) return "" if commands.empty? lines = [ "typedef void (*mtlang_gl_function)(void);", "typedef mtlang_gl_function (*mtlang_gl_loader_proc)(const char *name);", "", "static mtlang_gl_loader_proc mtlang_gl_loader;", ] lines.concat(commands.map { |command| "static #{command_proc_type_name(command)} #{command_proc_var_name(command)};" }) lines.join("\n") end |
#emit_enums(enums) ⇒ Object
335 336 337 338 339 340 341 342 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 335 def emit_enums(enums) return "" if enums.empty? enums_by_name = enums.each_with_object({}) { |entry, hash| hash[entry.name] = entry } enums.map do |entry| "#define #{entry.name} #{resolve_enum_value(entry, enums_by_name)}" end.join("\n") end |
#emit_header(types:, enums:, commands:) ⇒ Object
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 280 def emit_header(types:, enums:, commands:) [ "/* generated by mtc opengl-registry from #{@source_label} */", "#ifndef MT_LANG_GL_REGISTRY_HELPERS_H", "#define MT_LANG_GL_REGISTRY_HELPERS_H", "", "#include <stddef.h>", "#include <stdint.h>", "", "#if defined(_WIN32) && !defined(__CYGWIN__)", "#define MTLANG_GL_APIENTRY __stdcall", "#else", "#define MTLANG_GL_APIENTRY", "#endif", "", emit_types(types), emit_enums(enums), emit_loader_api, emit_public_command_declarations(commands), "#ifdef #{IMPLEMENTATION_DEFINE}", "#if defined(MT_LANG_GL_REGISTRY_HAVE_GLFW)", "#include <GLFW/glfw3.h>", "#endif", "#if defined(MT_LANG_GL_REGISTRY_HAVE_SDL3)", "#include <SDL3/SDL.h>", "#endif", "#if defined(MT_LANG_GL_REGISTRY_HAVE_RAYLIB)", "void *rlGetProcAddress(const char *proc_name);", "#endif", "#include <stdio.h>", "#include <stdlib.h>", "", emit_command_proc_typedefs(commands), emit_command_storage(commands), emit_loader_implementation(commands), emit_command_definitions(commands), "#endif", "", "#endif", "", ].reject(&:empty?).join("\n") end |
#emit_loader_api ⇒ Object
344 345 346 347 348 349 350 351 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 344 def emit_loader_api <<~C.rstrip void mt_gl_reset_loader(void); void mt_gl_use_glfw_loader(void); void mt_gl_use_sdl_loader(void); void mt_gl_use_raylib_loader(void); C end |
#emit_loader_implementation(commands) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 382 def emit_loader_implementation(commands) reset_lines = commands.map { |command| " #{command_proc_var_name(command)} = NULL;" } <<~C.rstrip static void mtlang_gl_reset_cache(void) { #{reset_lines.join("\n")} } static mtlang_gl_function mtlang_gl_require_proc(const char *name) { mtlang_gl_function proc; if (mtlang_gl_loader == NULL) { fprintf(stderr, "OpenGL loader is not configured before calling %s\\n", name); abort(); } proc = mtlang_gl_loader(name); if (proc == NULL) { fprintf(stderr, "OpenGL symbol %s is unavailable for the current context\\n", name); abort(); } return proc; } void mt_gl_reset_loader(void) { mtlang_gl_loader = NULL; mtlang_gl_reset_cache(); } void mt_gl_set_loader_proc(mtlang_gl_loader_proc loader) { mtlang_gl_loader = loader; mtlang_gl_reset_cache(); } void mt_gl_use_glfw_loader(void) { #if defined(MT_LANG_GL_REGISTRY_HAVE_GLFW) mt_gl_set_loader_proc((mtlang_gl_loader_proc) glfwGetProcAddress); #else fprintf(stderr, "OpenGL GLFW loader support is unavailable in this build\\n"); abort(); #endif } void mt_gl_use_sdl_loader(void) { #if defined(MT_LANG_GL_REGISTRY_HAVE_SDL3) mt_gl_set_loader_proc((mtlang_gl_loader_proc) SDL_GL_GetProcAddress); #else fprintf(stderr, "OpenGL SDL3 loader support is unavailable in this build\\n"); abort(); #endif } void mt_gl_use_raylib_loader(void) { #if defined(MT_LANG_GL_REGISTRY_HAVE_RAYLIB) mt_gl_set_loader_proc((mtlang_gl_loader_proc) rlGetProcAddress); #else fprintf(stderr, "OpenGL raylib loader support is unavailable in this build\\n"); abort(); #endif } C end |
#emit_public_command_declarations(commands) ⇒ Object
353 354 355 356 357 358 359 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 353 def emit_public_command_declarations(commands) return "" if commands.empty? commands.map do |command| "#{command.return_type} MTLANG_GL_APIENTRY #{command.name}(#{command_parameter_list(command)});" end.join("\n") end |
#emit_types(types) ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 323 def emit_types(types) rendered = types.filter_map do |type| next if type.name == "khrplatform" rewrite_type_source(type.source) end return "" if rendered.empty? rendered.join("\n\n") end |
#flatten_node(node, omit_name: false) ⇒ Object
528 529 530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 528 def flatten_node(node, omit_name: false) case node when REXML::Text node.value when REXML::Element return "" if omit_name && node.name == "name" return "MTLANG_GL_APIENTRY" if node.name == "apientry" node.children.map { |child| flatten_node(child, omit_name:) }.join else "" end end |
#generate ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 80 def generate registry = parse_registry active_enums, active_commands = active_surface(registry) emit_header( types: registry.fetch(:types), enums: active_enums, commands: active_commands, ) end |
#normalize_c_fragment(fragment) ⇒ Object
524 525 526 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 524 def normalize_c_fragment(fragment) fragment.gsub(/[ \t\n\r]+/, " ").strip end |
#parse_blocks(parent_element) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 192 def parse_blocks(parent_element) parent_element.elements.each_with_object([]) do |child, blocks| next unless %w[require remove].include?(child.name) blocks << BlockEntry.new( kind: child.name.to_sym, profile: child.attributes["profile"], api: child.attributes["api"], types: child.get_elements("type").filter_map { |element| element.attributes["name"] }, enums: child.get_elements("enum").filter_map { |element| element.attributes["name"] }, commands: child.get_elements("command").filter_map { |element| element.attributes["name"] }, ) end end |
#parse_commands(root) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 140 def parse_commands(root) order = 0 commands = {} root.elements.each("commands/command") do |command_element| proto = command_element.elements["proto"] next if proto.nil? name = proto.elements["name"]&.text&.strip next if name.nil? || name.empty? commands[name] ||= CommandEntry.new( name:, return_type: normalize_c_fragment(flatten_node(proto, omit_name: true)), params: command_element.get_elements("param").map do |param_element| CommandParam.new( name: param_element.elements["name"]&.text&.strip, source: normalize_c_fragment(flatten_node(param_element)), ) end, order:, ) order += 1 end commands end |
#parse_enums(root) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 120 def parse_enums(root) order = 0 enums = {} root.elements.each("enums/enum") do |enum_element| name = enum_element.attributes["name"] next if name.nil? || name.empty? enums[name] ||= EnumEntry.new( name:, value: enum_element.attributes["value"], alias_of: enum_element.attributes["alias"], order:, ) order += 1 end enums end |
#parse_extensions(root) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 179 def parse_extensions(root) root.get_elements("extensions/extension").each_with_object({}) do |extension_element, extensions| name = extension_element.attributes["name"] next if name.nil? || name.empty? extensions[name] = ExtensionEntry.new( name:, supported: extension_element.attributes["supported"], blocks: parse_blocks(extension_element), ) end end |
#parse_features(root) ⇒ Object
168 169 170 171 172 173 174 175 176 177 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 168 def parse_features(root) root.get_elements("feature").map do |feature_element| FeatureEntry.new( name: feature_element.attributes["name"], api: feature_element.attributes["api"], number: feature_element.attributes["number"], blocks: parse_blocks(feature_element), ) end end |
#parse_registry ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 91 def parse_registry document = REXML::Document.new(@xml_source) root = document.root { types: parse_types(root), enums: parse_enums(root), commands: parse_commands(root), features: parse_features(root), extensions: parse_extensions(root), } end |
#parse_types(root) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 104 def parse_types(root) order = 0 types = [] root.elements.each("types/type") do |type_element| name = type_element.attributes["name"] || type_element.elements["name"]&.text&.strip next if name.nil? || name.empty? source = flatten_node(type_element).strip types << TypeEntry.new(name:, source:, order:) order += 1 end types end |
#profile_matches?(profile) ⇒ Boolean
248 249 250 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 248 def profile_matches?(profile) profile.nil? || profile.empty? || profile == @profile end |
#resolve_enum_value(entry, enums_by_name, seen = Set.new) ⇒ Object
501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 501 def resolve_enum_value(entry, enums_by_name, seen = Set.new) return entry.value unless entry.value.nil? || entry.value.empty? return entry.alias_of if entry.alias_of.nil? || entry.alias_of.empty? return entry.alias_of if seen.include?(entry.name) seen << entry.name aliased = enums_by_name[entry.alias_of] return entry.alias_of if aliased.nil? resolve_enum_value(aliased, enums_by_name, seen) end |
#rewrite_type_source(source) ⇒ Object
513 514 515 516 517 518 519 520 521 522 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 513 def rewrite_type_source(source) rewritten = source.dup rewritten.gsub!("#include <KHR/khrplatform.h>", "") rewritten.gsub!(/\bGL_APIENTRY\b/, "MTLANG_GL_APIENTRY") rewritten.gsub!(/\bAPIENTRY\b/, "MTLANG_GL_APIENTRY") KHRONOS_TYPE_REWRITES.each do |original, replacement| rewritten.gsub!(/\b#{Regexp.escape(original)}\b/, replacement) end rewritten.strip end |
#version_at_most?(left, right) ⇒ Boolean
262 263 264 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 262 def version_at_most?(left, right) compare_versions(left, right) <= 0 end |
#version_key(version) ⇒ Object
266 267 268 |
# File 'lib/milk_tea/bindings/opengl_registry.rb', line 266 def version_key(version) version.to_s.split(".").map(&:to_i) end |