GenerateGLAPIWrapper.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Array.h>
  7. #include <AK/DeprecatedString.h>
  8. #include <AK/JsonObject.h>
  9. #include <AK/NumericLimits.h>
  10. #include <AK/Optional.h>
  11. #include <AK/SourceGenerator.h>
  12. #include <AK/StringBuilder.h>
  13. #include <AK/StringView.h>
  14. #include <AK/Vector.h>
  15. #include <LibCore/ArgsParser.h>
  16. #include <LibCore/File.h>
  17. #include <LibMain/Main.h>
  18. struct ArgumentDefinition {
  19. Optional<DeprecatedString> name;
  20. Optional<DeprecatedString> cpp_type;
  21. DeprecatedString expression;
  22. Optional<DeprecatedString> cast_to;
  23. };
  24. struct FunctionDefinition {
  25. DeprecatedString name;
  26. DeprecatedString return_type;
  27. Vector<ArgumentDefinition> arguments;
  28. DeprecatedString implementation;
  29. bool unimplemented;
  30. DeprecatedString variant_gl_type;
  31. };
  32. struct VariantType {
  33. DeprecatedString encoded_type;
  34. Optional<DeprecatedString> implementation;
  35. bool unimplemented;
  36. };
  37. struct Variants {
  38. Vector<DeprecatedString> api_suffixes { "" };
  39. Vector<u32> argument_counts { NumericLimits<u32>::max() };
  40. Vector<DeprecatedString> argument_defaults { "" };
  41. bool convert_range { false };
  42. Vector<VariantType> types {
  43. {
  44. .encoded_type = "",
  45. .implementation = Optional<DeprecatedString> {},
  46. .unimplemented = false,
  47. }
  48. };
  49. DeprecatedString pointer_argument { "" };
  50. };
  51. struct EncodedTypeEntry {
  52. StringView encoded_type;
  53. StringView cpp_type;
  54. StringView gl_type;
  55. };
  56. // clang-format off
  57. constexpr static Array<EncodedTypeEntry, 9> type_definitions = {
  58. EncodedTypeEntry { "b"sv, "GLbyte"sv, "GL_BYTE"sv },
  59. EncodedTypeEntry { "d"sv, "GLdouble"sv, "GL_DOUBLE"sv },
  60. EncodedTypeEntry { "f"sv, "GLfloat"sv, "GL_FLOAT"sv },
  61. EncodedTypeEntry { "i"sv, "GLint"sv, "GL_INT"sv },
  62. EncodedTypeEntry { "s"sv, "GLshort"sv, "GL_SHORT"sv },
  63. EncodedTypeEntry { "ub"sv, "GLubyte"sv, "GL_UNSIGNED_BYTE"sv },
  64. EncodedTypeEntry { "ui"sv, "GLuint"sv, "GL_UNSIGNED_INT"sv },
  65. EncodedTypeEntry { "us"sv, "GLushort"sv, "GL_UNSIGNED_SHORT"sv },
  66. EncodedTypeEntry { "x"sv, "GLfixed"sv, "GL_INT"sv },
  67. };
  68. // clang-format on
  69. struct EncodedType {
  70. EncodedTypeEntry type_entry;
  71. DeprecatedString cpp_type;
  72. DeprecatedString function_name_suffix;
  73. bool is_pointer;
  74. bool is_const_pointer;
  75. };
  76. Vector<DeprecatedString> get_name_list(Optional<JsonValue const&> name_definition)
  77. {
  78. if (!name_definition.has_value() || name_definition->is_null())
  79. return {};
  80. Vector<DeprecatedString, 1> names;
  81. if (name_definition->is_string()) {
  82. names.append(name_definition->as_string());
  83. } else if (name_definition->is_array()) {
  84. name_definition->as_array().for_each([&names](auto& value) {
  85. VERIFY(value.is_string());
  86. names.append(value.as_string());
  87. });
  88. } else {
  89. VERIFY_NOT_REACHED();
  90. }
  91. return names;
  92. }
  93. Optional<EncodedType> get_encoded_type(DeprecatedString encoded_type)
  94. {
  95. bool is_const_pointer = !encoded_type.ends_with('!');
  96. if (!is_const_pointer)
  97. encoded_type = encoded_type.substring_view(0, encoded_type.length() - 1);
  98. DeprecatedString function_name_suffix = encoded_type;
  99. bool is_pointer = encoded_type.ends_with('v');
  100. if (is_pointer)
  101. encoded_type = encoded_type.substring_view(0, encoded_type.length() - 1);
  102. VERIFY(is_const_pointer || is_pointer);
  103. Optional<EncodedTypeEntry> type_definition;
  104. for (size_t i = 0; i < type_definitions.size(); ++i) {
  105. if (type_definitions[i].encoded_type == encoded_type) {
  106. type_definition = type_definitions[i];
  107. break;
  108. }
  109. }
  110. if (!type_definition.has_value())
  111. return {};
  112. return EncodedType {
  113. .type_entry = type_definition.value(),
  114. .cpp_type = DeprecatedString::formatted(
  115. "{}{}{}",
  116. type_definition->cpp_type,
  117. is_pointer && is_const_pointer ? " const" : "",
  118. is_pointer ? "*" : ""),
  119. .function_name_suffix = function_name_suffix,
  120. .is_pointer = is_pointer,
  121. .is_const_pointer = is_const_pointer,
  122. };
  123. }
  124. DeprecatedString wrap_expression_in_range_conversion(DeprecatedString source_type, DeprecatedString target_type, DeprecatedString expression)
  125. {
  126. VERIFY(target_type == "GLfloat" || target_type == "GLdouble");
  127. // No range conversion required
  128. if (source_type == target_type || source_type == "GLdouble")
  129. return expression;
  130. if (source_type == "GLbyte")
  131. return DeprecatedString::formatted("({} + 128.) / 127.5 - 1.", expression);
  132. else if (source_type == "GLfloat")
  133. return DeprecatedString::formatted("static_cast<GLdouble>({})", expression);
  134. else if (source_type == "GLint")
  135. return DeprecatedString::formatted("({} + 2147483648.) / 2147483647.5 - 1.", expression);
  136. else if (source_type == "GLshort")
  137. return DeprecatedString::formatted("({} + 32768.) / 32767.5 - 1.", expression);
  138. else if (source_type == "GLubyte")
  139. return DeprecatedString::formatted("{} / 255.", expression);
  140. else if (source_type == "GLuint")
  141. return DeprecatedString::formatted("{} / 4294967296.", expression);
  142. else if (source_type == "GLushort")
  143. return DeprecatedString::formatted("{} / 65536.", expression);
  144. VERIFY_NOT_REACHED();
  145. }
  146. Variants read_variants_settings(JsonObject const& variants_obj)
  147. {
  148. Variants variants;
  149. if (variants_obj.has_array("argument_counts"sv)) {
  150. variants.argument_counts.clear_with_capacity();
  151. variants_obj.get_array("argument_counts"sv)->for_each([&](auto const& argument_count_value) {
  152. variants.argument_counts.append(argument_count_value.to_u32());
  153. });
  154. }
  155. if (variants_obj.has_array("argument_defaults"sv)) {
  156. variants.argument_defaults.clear_with_capacity();
  157. variants_obj.get_array("argument_defaults"sv)->for_each([&](auto const& argument_default_value) {
  158. variants.argument_defaults.append(argument_default_value.as_string());
  159. });
  160. }
  161. if (variants_obj.has_bool("convert_range"sv)) {
  162. variants.convert_range = variants_obj.get_bool("convert_range"sv).value();
  163. }
  164. if (variants_obj.has_array("api_suffixes"sv)) {
  165. variants.api_suffixes.clear_with_capacity();
  166. variants_obj.get_array("api_suffixes"sv)->for_each([&](auto const& suffix_value) {
  167. variants.api_suffixes.append(suffix_value.as_string());
  168. });
  169. }
  170. if (variants_obj.has_string("pointer_argument"sv)) {
  171. variants.pointer_argument = variants_obj.get_deprecated_string("pointer_argument"sv).value();
  172. }
  173. if (variants_obj.has_object("types"sv)) {
  174. variants.types.clear_with_capacity();
  175. variants_obj.get_object("types"sv)->for_each_member([&](auto const& key, auto const& type_value) {
  176. auto const& type = type_value.as_object();
  177. variants.types.append(VariantType {
  178. .encoded_type = key,
  179. .implementation = type.get_deprecated_string("implementation"sv),
  180. .unimplemented = type.get_bool("unimplemented"sv).value_or(false),
  181. });
  182. });
  183. }
  184. return variants;
  185. }
  186. Vector<ArgumentDefinition> copy_arguments_for_variant(Vector<ArgumentDefinition> arguments, Variants variants,
  187. u32 argument_count, EncodedType encoded_type)
  188. {
  189. Vector<ArgumentDefinition> variant_arguments = arguments;
  190. auto base_cpp_type = encoded_type.type_entry.cpp_type;
  191. size_t variadic_index = 0;
  192. for (size_t i = 0; i < variant_arguments.size(); ++i) {
  193. // Skip arguments with a fixed type
  194. if (variant_arguments[i].cpp_type.has_value())
  195. continue;
  196. variant_arguments[i].cpp_type = encoded_type.cpp_type;
  197. auto cast_to = variant_arguments[i].cast_to;
  198. // Pointer argument
  199. if (encoded_type.is_pointer) {
  200. variant_arguments[i].name = (variadic_index == 0) ? variants.pointer_argument : Optional<DeprecatedString> {};
  201. if (variadic_index >= argument_count) {
  202. // If this variable argument is past the argument count, fall back to the defaults
  203. variant_arguments[i].expression = variants.argument_defaults[variadic_index];
  204. variant_arguments[i].cast_to = Optional<DeprecatedString> {};
  205. } else if (argument_count == 1 && variants.argument_counts.size() == 1) {
  206. // Otherwise, if the pointer is the only variadic argument, pass it through unchanged
  207. variant_arguments[i].cast_to = Optional<DeprecatedString> {};
  208. } else {
  209. // Otherwise, index into the pointer argument
  210. auto indexed_expression = DeprecatedString::formatted("{}[{}]", variants.pointer_argument, variadic_index);
  211. if (variants.convert_range && cast_to.has_value())
  212. indexed_expression = wrap_expression_in_range_conversion(base_cpp_type, cast_to.value(), indexed_expression);
  213. variant_arguments[i].expression = indexed_expression;
  214. }
  215. } else {
  216. // Regular argument
  217. if (variadic_index >= argument_count) {
  218. // If the variable argument is past the argument count, fall back to the defaults
  219. variant_arguments[i].name = Optional<DeprecatedString> {};
  220. variant_arguments[i].expression = variants.argument_defaults[variadic_index];
  221. variant_arguments[i].cast_to = Optional<DeprecatedString> {};
  222. } else if (variants.convert_range && cast_to.has_value()) {
  223. // Otherwise, if we need to convert the input values, wrap the expression in a range conversion
  224. variant_arguments[i].expression = wrap_expression_in_range_conversion(
  225. base_cpp_type,
  226. cast_to.value(),
  227. variant_arguments[i].expression);
  228. }
  229. }
  230. // Determine if we can skip casting to the target type
  231. if (cast_to == base_cpp_type || (variants.convert_range && cast_to == "GLdouble"))
  232. variant_arguments[i].cast_to = Optional<DeprecatedString> {};
  233. variadic_index++;
  234. }
  235. return variant_arguments;
  236. }
  237. Vector<FunctionDefinition> create_function_definitions(DeprecatedString function_name, JsonObject const& function_definition)
  238. {
  239. // A single function definition can expand to multiple generated functions by way of:
  240. // - differing API suffices (ARB, EXT, etc.);
  241. // - differing argument counts;
  242. // - differing argument types.
  243. // These can all be combined.
  244. // Parse base argument definitions first; these may later be modified by variants
  245. Vector<ArgumentDefinition> argument_definitions;
  246. JsonArray const& arguments = function_definition.get_array("arguments"sv).value_or(JsonArray {});
  247. arguments.for_each([&argument_definitions](auto const& argument_value) {
  248. VERIFY(argument_value.is_object());
  249. auto const& argument = argument_value.as_object();
  250. auto type = argument.get_deprecated_string("type"sv);
  251. auto argument_names = get_name_list(argument.get("name"sv));
  252. auto expression = argument.get_deprecated_string("expression"sv).value_or("@argument_name@");
  253. auto cast_to = argument.get_deprecated_string("cast_to"sv);
  254. // Add an empty dummy name when all we have is an expression
  255. if (argument_names.is_empty() && !expression.is_empty())
  256. argument_names.append("");
  257. for (auto const& argument_name : argument_names) {
  258. argument_definitions.append({ .name = argument_name.is_empty() ? Optional<DeprecatedString> {} : argument_name,
  259. .cpp_type = type,
  260. .expression = expression,
  261. .cast_to = cast_to });
  262. }
  263. });
  264. // Create functions for each name and/or variant
  265. Vector<FunctionDefinition> functions;
  266. auto return_type = function_definition.get_deprecated_string("return_type"sv).value_or("void");
  267. auto function_implementation = function_definition.get_deprecated_string("implementation"sv).value_or(function_name.to_snakecase());
  268. auto function_unimplemented = function_definition.get_bool("unimplemented"sv).value_or(false);
  269. if (!function_definition.has("variants"sv)) {
  270. functions.append({
  271. .name = function_name,
  272. .return_type = return_type,
  273. .arguments = argument_definitions,
  274. .implementation = function_implementation,
  275. .unimplemented = function_unimplemented,
  276. .variant_gl_type = "",
  277. });
  278. return functions;
  279. }
  280. // Read variants settings for this function
  281. auto variants_obj = function_definition.get_object("variants"sv).value();
  282. auto variants = read_variants_settings(variants_obj);
  283. for (auto argument_count : variants.argument_counts) {
  284. for (auto const& variant_type : variants.types) {
  285. auto encoded_type = get_encoded_type(variant_type.encoded_type);
  286. auto variant_arguments = encoded_type.has_value()
  287. ? copy_arguments_for_variant(argument_definitions, variants, argument_count, encoded_type.value())
  288. : argument_definitions;
  289. auto variant_type_implementation = variant_type.implementation.has_value()
  290. ? variant_type.implementation.value()
  291. : function_implementation;
  292. for (auto const& api_suffix : variants.api_suffixes) {
  293. functions.append({
  294. .name = DeprecatedString::formatted(
  295. "{}{}{}{}",
  296. function_name,
  297. variants.argument_counts.size() > 1 ? DeprecatedString::formatted("{}", argument_count) : "",
  298. encoded_type.has_value() && variants.types.size() > 1 ? encoded_type->function_name_suffix : "",
  299. api_suffix),
  300. .return_type = return_type,
  301. .arguments = variant_arguments,
  302. .implementation = variant_type_implementation,
  303. .unimplemented = variant_type.unimplemented || function_unimplemented,
  304. .variant_gl_type = encoded_type.has_value() ? encoded_type->type_entry.gl_type : ""sv,
  305. });
  306. }
  307. }
  308. }
  309. return functions;
  310. }
  311. ErrorOr<void> generate_header_file(JsonObject& api_data, Core::File& file)
  312. {
  313. StringBuilder builder;
  314. SourceGenerator generator { builder };
  315. generator.appendln("#pragma once");
  316. generator.append("\n");
  317. generator.appendln("#include <LibGL/GL/glplatform.h>");
  318. generator.append("\n");
  319. generator.appendln("#ifdef __cplusplus");
  320. generator.appendln("extern \"C\" {");
  321. generator.appendln("#endif");
  322. generator.append("\n");
  323. api_data.for_each_member([&](auto& function_name, auto& value) {
  324. VERIFY(value.is_object());
  325. auto const& function = value.as_object();
  326. auto function_definitions = create_function_definitions(function_name, function);
  327. for (auto const& function_definition : function_definitions) {
  328. auto function_generator = generator.fork();
  329. function_generator.set("name", function_definition.name);
  330. function_generator.set("return_type", function_definition.return_type);
  331. function_generator.append("GLAPI @return_type@ gl@name@(");
  332. bool first = true;
  333. for (auto const& argument_definition : function_definition.arguments) {
  334. if (!argument_definition.name.has_value() || !argument_definition.cpp_type.has_value())
  335. continue;
  336. auto argument_generator = function_generator.fork();
  337. argument_generator.set("argument_type", argument_definition.cpp_type.value());
  338. argument_generator.set("argument_name", argument_definition.name.value());
  339. if (!first)
  340. argument_generator.append(", ");
  341. first = false;
  342. argument_generator.append("@argument_type@ @argument_name@");
  343. }
  344. function_generator.appendln(");");
  345. }
  346. });
  347. generator.appendln("#ifdef __cplusplus");
  348. generator.appendln("}");
  349. generator.appendln("#endif");
  350. TRY(file.write_until_depleted(generator.as_string_view().bytes()));
  351. return {};
  352. }
  353. ErrorOr<void> generate_implementation_file(JsonObject& api_data, Core::File& file)
  354. {
  355. StringBuilder builder;
  356. SourceGenerator generator { builder };
  357. generator.appendln("#include <LibGL/GL/glapi.h>");
  358. generator.appendln("#include <LibGL/GLContext.h>");
  359. generator.append("\n");
  360. generator.appendln("extern GL::GLContext* g_gl_context;");
  361. generator.append("\n");
  362. api_data.for_each_member([&](auto& function_name, auto& value) {
  363. VERIFY(value.is_object());
  364. JsonObject const& function = value.as_object();
  365. auto function_definitions = create_function_definitions(function_name, function);
  366. for (auto const& function_definition : function_definitions) {
  367. auto function_generator = generator.fork();
  368. auto return_type = function_definition.return_type;
  369. function_generator.set("name"sv, function_definition.name);
  370. function_generator.set("return_type"sv, return_type);
  371. function_generator.set("implementation"sv, function_definition.implementation);
  372. function_generator.set("variant_gl_type"sv, function_definition.variant_gl_type);
  373. function_generator.append("@return_type@ gl@name@(");
  374. bool first = true;
  375. for (auto const& argument_definition : function_definition.arguments) {
  376. if (!argument_definition.name.has_value() || !argument_definition.cpp_type.has_value())
  377. continue;
  378. auto argument_generator = function_generator.fork();
  379. argument_generator.set("argument_type", argument_definition.cpp_type.value());
  380. argument_generator.set("argument_name", argument_definition.name.value());
  381. if (!first)
  382. argument_generator.append(", ");
  383. first = false;
  384. argument_generator.append("@argument_type@ @argument_name@");
  385. }
  386. function_generator.appendln(")");
  387. function_generator.appendln("{");
  388. if (function_definition.unimplemented) {
  389. function_generator.append(" dbgln(\"gl@name@(");
  390. first = true;
  391. for (auto const& argument_definition : function_definition.arguments) {
  392. if (!argument_definition.name.has_value())
  393. continue;
  394. if (!first)
  395. function_generator.append(", ");
  396. first = false;
  397. if (argument_definition.cpp_type.value().ends_with('*'))
  398. function_generator.append("{:p}");
  399. else if (argument_definition.cpp_type.value() == "GLenum")
  400. function_generator.append("{:#x}");
  401. else
  402. function_generator.append("{}");
  403. }
  404. function_generator.append("): unimplemented\"");
  405. for (auto const& argument_definition : function_definition.arguments) {
  406. if (!argument_definition.name.has_value())
  407. continue;
  408. function_generator.append(", ");
  409. function_generator.append(argument_definition.name.value());
  410. }
  411. function_generator.appendln(");");
  412. function_generator.appendln(" TODO();");
  413. } else {
  414. function_generator.appendln(" if (!g_gl_context)");
  415. if (return_type.ends_with('*'))
  416. function_generator.appendln(" return nullptr;");
  417. else if (return_type == "GLboolean"sv)
  418. function_generator.appendln(" return GL_FALSE;");
  419. else if (return_type == "GLenum"sv)
  420. function_generator.appendln(" return GL_INVALID_OPERATION;");
  421. else if (return_type == "GLuint"sv)
  422. function_generator.appendln(" return 0;");
  423. else if (return_type == "void"sv)
  424. function_generator.appendln(" return;");
  425. else
  426. VERIFY_NOT_REACHED();
  427. function_generator.append(" ");
  428. if (return_type != "void"sv)
  429. function_generator.append("return ");
  430. function_generator.append("g_gl_context->gl_@implementation@(");
  431. first = true;
  432. for (auto const& argument_definition : function_definition.arguments) {
  433. auto argument_generator = function_generator.fork();
  434. auto cast_to = argument_definition.cast_to;
  435. argument_generator.set("argument_name", argument_definition.name.value_or(""));
  436. argument_generator.set("cast_to", cast_to.value_or(""));
  437. if (!first)
  438. argument_generator.append(", ");
  439. first = false;
  440. if (cast_to.has_value())
  441. argument_generator.append("static_cast<@cast_to@>(");
  442. argument_generator.append(argument_definition.expression);
  443. if (cast_to.has_value())
  444. argument_generator.append(")");
  445. }
  446. function_generator.appendln(");");
  447. }
  448. function_generator.appendln("}");
  449. function_generator.append("\n");
  450. }
  451. });
  452. TRY(file.write_until_depleted(generator.as_string_view().bytes()));
  453. return {};
  454. }
  455. ErrorOr<JsonValue> read_entire_file_as_json(StringView filename)
  456. {
  457. auto file = TRY(Core::File::open(filename, Core::File::OpenMode::Read));
  458. auto json_size = TRY(file->size());
  459. auto json_data = TRY(ByteBuffer::create_uninitialized(json_size));
  460. TRY(file->read_until_filled(json_data.bytes()));
  461. return JsonValue::from_string(json_data);
  462. }
  463. ErrorOr<int> serenity_main(Main::Arguments arguments)
  464. {
  465. StringView generated_header_path;
  466. StringView generated_implementation_path;
  467. StringView api_json_path;
  468. Core::ArgsParser args_parser;
  469. args_parser.add_option(generated_header_path, "Path to the OpenGL API header file to generate", "generated-header-path", 'h', "generated-header-path");
  470. args_parser.add_option(generated_implementation_path, "Path to the OpenGL API implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
  471. args_parser.add_option(api_json_path, "Path to the JSON file to read from", "json-path", 'j', "json-path");
  472. args_parser.parse(arguments);
  473. auto json = TRY(read_entire_file_as_json(api_json_path));
  474. VERIFY(json.is_object());
  475. auto api_data = json.as_object();
  476. auto generated_header_file = TRY(Core::File::open(generated_header_path, Core::File::OpenMode::Write));
  477. auto generated_implementation_file = TRY(Core::File::open(generated_implementation_path, Core::File::OpenMode::Write));
  478. TRY(generate_header_file(api_data, *generated_header_file));
  479. TRY(generate_implementation_file(api_data, *generated_implementation_file));
  480. return 0;
  481. }