GLContext.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. * Copyright (c) 2021-2022, Jesse Buhagiar <jooster669@gmail.com>
  4. * Copyright (c) 2022-2024, Jelle Raaijmakers <jelle@gmta.nl>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/ByteBuffer.h>
  10. #include <AK/Debug.h>
  11. #include <AK/HashMap.h>
  12. #include <AK/NonnullOwnPtr.h>
  13. #include <AK/Optional.h>
  14. #include <AK/RefPtr.h>
  15. #include <AK/Tuple.h>
  16. #include <AK/Variant.h>
  17. #include <AK/Vector.h>
  18. #include <LibGL/Buffer/Buffer.h>
  19. #include <LibGL/NameAllocator.h>
  20. #include <LibGL/Shaders/Program.h>
  21. #include <LibGL/Shaders/Shader.h>
  22. #include <LibGL/Tex/Texture.h>
  23. #include <LibGL/Tex/TextureUnit.h>
  24. #include <LibGPU/Device.h>
  25. #include <LibGPU/DeviceInfo.h>
  26. #include <LibGPU/Driver.h>
  27. #include <LibGPU/Light.h>
  28. #include <LibGPU/Vertex.h>
  29. #include <LibGfx/Bitmap.h>
  30. #include <LibGfx/Matrix4x4.h>
  31. #include <LibGfx/Rect.h>
  32. #include <LibGfx/Vector3.h>
  33. namespace GL {
  34. #define APPEND_TO_CALL_LIST_AND_RETURN_IF_NEEDED(name, ...) \
  35. if (should_append_to_listing()) { \
  36. append_to_listing<&GLContext::name>(__VA_ARGS__); \
  37. if (!should_execute_after_appending_to_listing()) \
  38. return; \
  39. }
  40. #define APPEND_TO_CALL_LIST_WITH_ARG_AND_RETURN_IF_NEEDED(name, arg) \
  41. if (should_append_to_listing()) { \
  42. auto ptr = store_in_listing(arg); \
  43. append_to_listing<&GLContext::name>(*ptr); \
  44. if (!should_execute_after_appending_to_listing()) \
  45. return; \
  46. }
  47. #define RETURN_WITH_ERROR_IF(condition, error) \
  48. if (condition) { \
  49. dbgln_if(GL_DEBUG, "{}(): error {:#x}", __func__, error); \
  50. if (m_error == GL_NO_ERROR) \
  51. m_error = error; \
  52. return; \
  53. }
  54. #define RETURN_VALUE_WITH_ERROR_IF(condition, error, return_value) \
  55. if (condition) { \
  56. dbgln_if(GL_DEBUG, "{}(): error {:#x}", __func__, error); \
  57. if (m_error == GL_NO_ERROR) \
  58. m_error = error; \
  59. return return_value; \
  60. }
  61. constexpr size_t MODELVIEW_MATRIX_STACK_LIMIT = 64;
  62. constexpr size_t PROJECTION_MATRIX_STACK_LIMIT = 8;
  63. constexpr size_t TEXTURE_MATRIX_STACK_LIMIT = 8;
  64. struct ContextParameter {
  65. GLenum type;
  66. bool is_capability { false };
  67. u8 count { 1 };
  68. union {
  69. bool boolean_value;
  70. GLint integer_value;
  71. GLint integer_list[4];
  72. GLdouble double_value;
  73. GLdouble double_list[4];
  74. } value;
  75. };
  76. struct VertexAttribPointer {
  77. GLint size { 4 };
  78. GLenum type { GL_FLOAT };
  79. bool normalize;
  80. GLsizei stride { 0 };
  81. void const* pointer { 0 };
  82. };
  83. enum Face {
  84. Front = 0,
  85. Back = 1,
  86. };
  87. enum class PackingType {
  88. Pack,
  89. Unpack,
  90. };
  91. class GLContext final {
  92. public:
  93. GLContext(RefPtr<GPU::Driver> driver, NonnullOwnPtr<GPU::Device>, Gfx::Bitmap&);
  94. ~GLContext();
  95. NonnullRefPtr<Gfx::Bitmap> frontbuffer() const { return m_frontbuffer; }
  96. void present();
  97. void gl_begin(GLenum mode);
  98. void gl_clear(GLbitfield mask);
  99. void gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
  100. void gl_clear_depth(GLfloat depth);
  101. void gl_clear_stencil(GLint s);
  102. void gl_color(GLfloat r, GLfloat g, GLfloat b, GLfloat a);
  103. void gl_delete_textures(GLsizei n, GLuint const* textures);
  104. void gl_end();
  105. void gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
  106. void gl_gen_textures(GLsizei n, GLuint* textures);
  107. GLenum gl_get_error();
  108. GLubyte const* gl_get_string(GLenum name);
  109. void gl_load_identity();
  110. void gl_load_matrix(FloatMatrix4x4 const& matrix);
  111. void gl_matrix_mode(GLenum mode);
  112. void gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
  113. void gl_push_matrix();
  114. void gl_pop_matrix();
  115. void gl_mult_matrix(FloatMatrix4x4 const& matrix);
  116. void gl_rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
  117. void gl_scale(GLfloat x, GLfloat y, GLfloat z);
  118. void gl_translate(GLfloat x, GLfloat y, GLfloat z);
  119. void gl_vertex(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
  120. void gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height);
  121. void gl_enable(GLenum);
  122. void gl_disable(GLenum);
  123. GLboolean gl_is_enabled(GLenum);
  124. void gl_front_face(GLenum);
  125. void gl_cull_face(GLenum);
  126. GLuint gl_gen_lists(GLsizei range);
  127. void gl_call_list(GLuint list);
  128. void gl_call_lists(GLsizei n, GLenum type, void const* lists);
  129. void gl_delete_lists(GLuint list, GLsizei range);
  130. void gl_list_base(GLuint base);
  131. void gl_end_list(void);
  132. void gl_new_list(GLuint list, GLenum mode);
  133. GLboolean gl_is_list(GLuint list);
  134. void gl_flush();
  135. void gl_finish();
  136. void gl_blend_equation_separate(GLenum rgb_mode, GLenum alpha_mode);
  137. void gl_blend_func(GLenum src_factor, GLenum dst_factor);
  138. void gl_shade_model(GLenum mode);
  139. void gl_alpha_func(GLenum func, GLclampf ref);
  140. void gl_hint(GLenum target, GLenum mode);
  141. void gl_read_buffer(GLenum mode);
  142. void gl_draw_buffer(GLenum buffer);
  143. void gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels);
  144. void gl_tex_image_2d(GLenum target, GLint level, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, GLvoid const* data);
  145. void gl_tex_sub_image_2d(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid const* data);
  146. void gl_tex_parameter(GLenum target, GLenum pname, GLfloat param);
  147. void gl_tex_parameterfv(GLenum target, GLenum pname, GLfloat const* params);
  148. void gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q);
  149. void gl_multi_tex_coord(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q);
  150. void gl_tex_env(GLenum target, GLenum pname, FloatVector4 params);
  151. void gl_tex_envv(GLenum target, GLenum pname, void const* params, GLenum type);
  152. void gl_bind_texture(GLenum target, GLuint texture);
  153. GLboolean gl_is_texture(GLuint texture);
  154. void gl_active_texture(GLenum texture);
  155. void gl_depth_mask(GLboolean flag);
  156. void gl_enable_client_state(GLenum cap);
  157. void gl_disable_client_state(GLenum cap);
  158. void gl_client_active_texture(GLenum target);
  159. void gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer);
  160. void gl_color_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer);
  161. void gl_tex_coord_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer);
  162. void gl_draw_arrays(GLenum mode, GLint first, GLsizei count);
  163. void gl_draw_elements(GLenum mode, GLsizei count, GLenum type, void const* indices);
  164. void gl_draw_pixels(GLsizei width, GLsizei height, GLenum format, GLenum type, void const* data);
  165. void gl_color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
  166. void gl_get_booleanv(GLenum pname, GLboolean* data);
  167. void gl_get_doublev(GLenum pname, GLdouble* params);
  168. void gl_get_floatv(GLenum pname, GLfloat* params);
  169. void gl_get_integerv(GLenum pname, GLint* data);
  170. void gl_depth_range(GLdouble min, GLdouble max);
  171. void gl_depth_func(GLenum func);
  172. void gl_polygon_mode(GLenum face, GLenum mode);
  173. void gl_polygon_offset(GLfloat factor, GLfloat units);
  174. void gl_fogfv(GLenum pname, GLfloat const* params);
  175. void gl_fogf(GLenum pname, GLfloat param);
  176. void gl_fogi(GLenum pname, GLint param);
  177. void gl_pixel_storei(GLenum pname, GLint param);
  178. void gl_scissor(GLint x, GLint y, GLsizei width, GLsizei height);
  179. void gl_stencil_func_separate(GLenum face, GLenum func, GLint ref, GLuint mask);
  180. void gl_stencil_mask_separate(GLenum face, GLuint mask);
  181. void gl_stencil_op_separate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass);
  182. void gl_normal(GLfloat nx, GLfloat ny, GLfloat nz);
  183. void gl_normal_pointer(GLenum type, GLsizei stride, void const* pointer);
  184. void gl_raster_pos(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
  185. void gl_line_width(GLfloat width);
  186. void gl_push_attrib(GLbitfield mask);
  187. void gl_pop_attrib();
  188. void gl_light_model(GLenum pname, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
  189. void gl_light_modelv(GLenum pname, void const* params, GLenum type);
  190. void gl_bitmap(GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, GLubyte const* bitmap);
  191. void gl_copy_tex_image_2d(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border);
  192. void gl_get_tex_image(GLenum target, GLint level, GLenum format, GLenum type, void* pixels);
  193. void gl_get_tex_parameter_integerv(GLenum target, GLint level, GLenum pname, GLint* params);
  194. void gl_rect(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2);
  195. void gl_tex_gen(GLenum coord, GLenum pname, GLint param);
  196. void gl_tex_gen_floatv(GLenum coord, GLenum pname, GLfloat const* params);
  197. void gl_lightf(GLenum light, GLenum pname, GLfloat param);
  198. void gl_lightfv(GLenum light, GLenum pname, GLfloat const* params);
  199. void gl_lightiv(GLenum light, GLenum pname, GLint const* params);
  200. void gl_materialf(GLenum face, GLenum pname, GLfloat param);
  201. void gl_materialfv(GLenum face, GLenum pname, GLfloat const* params);
  202. void gl_materialiv(GLenum face, GLenum pname, GLint const* params);
  203. void gl_color_material(GLenum face, GLenum mode);
  204. void gl_get_light(GLenum light, GLenum pname, void* params, GLenum type);
  205. void gl_get_material(GLenum face, GLenum pname, void* params, GLenum type);
  206. void gl_clip_plane(GLenum plane, GLdouble const* equation);
  207. void gl_get_clip_plane(GLenum plane, GLdouble* equation);
  208. void gl_array_element(GLint i);
  209. void gl_copy_tex_sub_image_2d(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, GLsizei width, GLsizei height);
  210. void gl_point_size(GLfloat size);
  211. void gl_bind_buffer(GLenum target, GLuint buffer);
  212. void gl_buffer_data(GLenum target, GLsizeiptr size, void const* data, GLenum usage);
  213. void gl_buffer_sub_data(GLenum target, GLintptr offset, GLsizeiptr size, void const* data);
  214. void gl_delete_buffers(GLsizei n, GLuint const* buffers);
  215. void gl_gen_buffers(GLsizei n, GLuint* buffers);
  216. GLuint gl_create_shader(GLenum shader_type);
  217. void gl_delete_shader(GLuint shader);
  218. void gl_shader_source(GLuint shader, GLsizei count, GLchar const** string, GLint const* length);
  219. void gl_compile_shader(GLuint shader);
  220. void gl_get_shader(GLuint shader, GLenum pname, GLint* params);
  221. GLuint gl_create_program();
  222. void gl_delete_program(GLuint program);
  223. void gl_attach_shader(GLuint program, GLuint shader);
  224. void gl_link_program(GLuint program);
  225. void gl_use_program(GLuint program);
  226. void gl_get_program(GLuint program, GLenum pname, GLint* params);
  227. private:
  228. void sync_clip_planes();
  229. void sync_device_config();
  230. void sync_device_sampler_config();
  231. void sync_device_texture_units();
  232. void sync_light_state();
  233. void sync_matrices();
  234. void sync_stencil_configuration();
  235. ErrorOr<ByteBuffer> build_extension_string();
  236. template<typename T>
  237. T* store_in_listing(T value)
  238. {
  239. VERIFY(m_current_listing_index.has_value());
  240. auto& listing = m_current_listing_index->listing;
  241. listing.saved_arguments.empend(make<Listing::ExtraSavedArguments>(move(value)));
  242. return listing.saved_arguments.last()->template get_pointer<T>();
  243. }
  244. template<auto member, typename... Args>
  245. void append_to_listing(Args&&... args)
  246. {
  247. VERIFY(m_current_listing_index.has_value());
  248. m_current_listing_index->listing.entries.empend(member, Listing::ArgumentsFor<member> { forward<Args>(args)... });
  249. }
  250. Optional<ContextParameter> get_context_parameter(GLenum pname);
  251. GPU::PackingSpecification get_packing_specification(PackingType);
  252. template<typename T>
  253. void get_floating_point(GLenum pname, T* params);
  254. template<typename T>
  255. void get_light_param(GLenum light, GLenum pname, T* params);
  256. template<typename T>
  257. void get_material_param(Face face, GLenum pname, T* params);
  258. void invoke_list(size_t list_index);
  259. [[nodiscard]] bool should_append_to_listing() const { return m_current_listing_index.has_value(); }
  260. [[nodiscard]] bool should_execute_after_appending_to_listing() const { return m_current_listing_index.has_value() && m_current_listing_index->mode == GL_COMPILE_AND_EXECUTE; }
  261. // FIXME: we store GPU::Texture objects that do not point back to either the driver or device, so we need
  262. // to destruct the latter two at the very end. Fix this by making all GPU objects point back to
  263. // the device that created them, and the device back to the driver.
  264. RefPtr<GPU::Driver> m_driver;
  265. NonnullOwnPtr<GPU::Device> m_rasterizer;
  266. GPU::DeviceInfo const m_device_info;
  267. GLenum m_current_draw_mode;
  268. GLenum m_current_matrix_mode { GL_MODELVIEW };
  269. FloatMatrix4x4& projection_matrix() { return m_projection_matrix_stack.last(); }
  270. FloatMatrix4x4& model_view_matrix() { return m_model_view_matrix_stack.last(); }
  271. Vector<FloatMatrix4x4> m_projection_matrix_stack { FloatMatrix4x4::identity() };
  272. Vector<FloatMatrix4x4> m_model_view_matrix_stack { FloatMatrix4x4::identity() };
  273. Vector<FloatMatrix4x4>* m_current_matrix_stack { &m_model_view_matrix_stack };
  274. FloatMatrix4x4* m_current_matrix { &m_current_matrix_stack->last() };
  275. bool m_matrices_dirty { true };
  276. ALWAYS_INLINE void update_current_matrix(FloatMatrix4x4 const& new_matrix)
  277. {
  278. *m_current_matrix = new_matrix;
  279. m_matrices_dirty = true;
  280. if (m_current_matrix_mode == GL_TEXTURE)
  281. m_texture_units_dirty = true;
  282. }
  283. Gfx::IntRect m_viewport;
  284. FloatVector4 m_clear_color { 0.0f, 0.0f, 0.0f, 0.0f };
  285. float m_clear_depth { 1.f };
  286. u8 m_clear_stencil { 0 };
  287. FloatVector4 m_current_vertex_color { 1.0f, 1.0f, 1.0f, 1.0f };
  288. Vector<FloatVector4> m_current_vertex_tex_coord;
  289. FloatVector3 m_current_vertex_normal { 0.0f, 0.0f, 1.0f };
  290. Vector<GPU::Vertex> m_vertex_list;
  291. GLenum m_error = GL_NO_ERROR;
  292. bool m_in_draw_state = false;
  293. bool m_depth_test_enabled { false };
  294. bool m_depth_offset_enabled { false };
  295. bool m_cull_faces = false;
  296. GLenum m_front_face = GL_CCW;
  297. GLenum m_culled_sides = GL_BACK;
  298. bool m_blend_enabled = false;
  299. GLenum m_blend_source_factor = GL_ONE;
  300. GLenum m_blend_destination_factor = GL_ZERO;
  301. GLenum m_blend_equation_rgb = GL_FUNC_ADD;
  302. GLenum m_blend_equation_alpha = GL_FUNC_ADD;
  303. bool m_alpha_test_enabled = false;
  304. GLenum m_alpha_test_func = GL_ALWAYS;
  305. GLclampf m_alpha_test_ref_value = 0;
  306. bool m_dither_enabled { true };
  307. bool m_normalize { false };
  308. // Stencil configuration
  309. bool m_stencil_test_enabled { false };
  310. bool m_stencil_configuration_dirty { true };
  311. struct StencilFunctionOptions {
  312. GLenum func { GL_ALWAYS };
  313. GLint reference_value { 0 };
  314. GLuint mask { NumericLimits<GLuint>::max() };
  315. };
  316. Array<StencilFunctionOptions, 2u> m_stencil_function;
  317. struct StencilOperationOptions {
  318. GLenum op_fail { GL_KEEP };
  319. GLenum op_depth_fail { GL_KEEP };
  320. GLenum op_pass { GL_KEEP };
  321. GLuint write_mask { NumericLimits<GLuint>::max() };
  322. };
  323. Array<StencilOperationOptions, 2u> m_stencil_operation;
  324. GLenum m_current_read_buffer = GL_BACK;
  325. GLenum m_current_draw_buffer = GL_BACK;
  326. // User-defined clip planes
  327. struct ClipPlaneAttributes {
  328. Array<FloatVector4, 6> eye_clip_plane; // TODO: Change to use device-defined constant
  329. GLuint enabled { 0 };
  330. } m_clip_plane_attributes;
  331. bool m_clip_planes_dirty { true };
  332. // Client side arrays
  333. bool m_client_side_vertex_array_enabled { false };
  334. bool m_client_side_color_array_enabled { false };
  335. Vector<bool> m_client_side_texture_coord_array_enabled;
  336. size_t m_client_active_texture { 0 };
  337. bool m_client_side_normal_array_enabled { false };
  338. NonnullRefPtr<Gfx::Bitmap> m_frontbuffer;
  339. // Texture objects
  340. template<typename T>
  341. RefPtr<T> get_default_texture(GLenum target)
  342. {
  343. auto default_texture = m_default_textures.get(target);
  344. VERIFY(default_texture.has_value());
  345. return static_cast<T*>(default_texture.value());
  346. }
  347. NameAllocator m_texture_name_allocator;
  348. HashMap<GLuint, RefPtr<Texture>> m_allocated_textures;
  349. HashMap<GLenum, RefPtr<Texture>> m_default_textures;
  350. Vector<TextureUnit> m_texture_units;
  351. TextureUnit* m_active_texture_unit;
  352. size_t m_active_texture_unit_index { 0 };
  353. bool m_texture_units_dirty { true };
  354. // Texture coordinate generation state
  355. struct TextureCoordinateGeneration {
  356. bool enabled { false };
  357. GLenum generation_mode { GL_EYE_LINEAR };
  358. FloatVector4 object_plane_coefficients { 0.0f, 0.0f, 0.0f, 0.0f };
  359. FloatVector4 eye_plane_coefficients { 0.0f, 0.0f, 0.0f, 0.0f };
  360. };
  361. Vector<Array<TextureCoordinateGeneration, 4>> m_texture_coordinate_generation;
  362. ALWAYS_INLINE TextureCoordinateGeneration& texture_coordinate_generation(size_t texture_unit, GLenum capability)
  363. {
  364. return m_texture_coordinate_generation[texture_unit][capability - GL_TEXTURE_GEN_S];
  365. }
  366. bool m_sampler_config_is_dirty { true };
  367. bool m_light_state_is_dirty { true };
  368. NameAllocator m_shader_name_allocator;
  369. NameAllocator m_program_name_allocator;
  370. HashMap<GLuint, RefPtr<Shader>> m_allocated_shaders;
  371. HashMap<GLuint, RefPtr<Program>> m_allocated_programs;
  372. RefPtr<Program> m_current_program;
  373. struct Listing {
  374. template<typename F>
  375. struct TupleTypeForArgumentListOf_;
  376. template<typename Ret, typename C, typename... Args>
  377. struct TupleTypeForArgumentListOf_<Ret (C::*)(Args...)> {
  378. using Type = Tuple<Args...>;
  379. };
  380. template<typename F>
  381. using TupleTypeForArgumentListOf = typename TupleTypeForArgumentListOf_<F>::Type;
  382. template<auto member>
  383. using ArgumentsFor = TupleTypeForArgumentListOf<decltype(member)>;
  384. template<typename... Fns>
  385. struct FunctionAndArgs {
  386. Variant<Fns...> function;
  387. Variant<TupleTypeForArgumentListOf<Fns>...> arguments;
  388. };
  389. using FunctionsAndArgs = FunctionAndArgs<
  390. decltype(&GLContext::gl_begin),
  391. decltype(&GLContext::gl_clear),
  392. decltype(&GLContext::gl_clear_color),
  393. decltype(&GLContext::gl_clear_depth),
  394. decltype(&GLContext::gl_clear_stencil),
  395. decltype(&GLContext::gl_color),
  396. decltype(&GLContext::gl_end),
  397. decltype(&GLContext::gl_frustum),
  398. decltype(&GLContext::gl_load_identity),
  399. decltype(&GLContext::gl_load_matrix),
  400. decltype(&GLContext::gl_matrix_mode),
  401. decltype(&GLContext::gl_ortho),
  402. decltype(&GLContext::gl_push_matrix),
  403. decltype(&GLContext::gl_pop_matrix),
  404. decltype(&GLContext::gl_mult_matrix),
  405. decltype(&GLContext::gl_rotate),
  406. decltype(&GLContext::gl_scale),
  407. decltype(&GLContext::gl_translate),
  408. decltype(&GLContext::gl_vertex),
  409. decltype(&GLContext::gl_viewport),
  410. decltype(&GLContext::gl_enable),
  411. decltype(&GLContext::gl_disable),
  412. decltype(&GLContext::gl_front_face),
  413. decltype(&GLContext::gl_cull_face),
  414. decltype(&GLContext::gl_call_list),
  415. decltype(&GLContext::gl_call_lists),
  416. decltype(&GLContext::gl_blend_equation_separate),
  417. decltype(&GLContext::gl_blend_func),
  418. decltype(&GLContext::gl_shade_model),
  419. decltype(&GLContext::gl_alpha_func),
  420. decltype(&GLContext::gl_hint),
  421. decltype(&GLContext::gl_read_buffer),
  422. decltype(&GLContext::gl_tex_parameter),
  423. decltype(&GLContext::gl_tex_parameterfv),
  424. decltype(&GLContext::gl_depth_mask),
  425. decltype(&GLContext::gl_draw_pixels),
  426. decltype(&GLContext::gl_depth_range),
  427. decltype(&GLContext::gl_polygon_offset),
  428. decltype(&GLContext::gl_scissor),
  429. decltype(&GLContext::gl_stencil_func_separate),
  430. decltype(&GLContext::gl_stencil_mask_separate),
  431. decltype(&GLContext::gl_stencil_op_separate),
  432. decltype(&GLContext::gl_normal),
  433. decltype(&GLContext::gl_raster_pos),
  434. decltype(&GLContext::gl_line_width),
  435. decltype(&GLContext::gl_push_attrib),
  436. decltype(&GLContext::gl_pop_attrib),
  437. decltype(&GLContext::gl_light_model),
  438. decltype(&GLContext::gl_bitmap),
  439. decltype(&GLContext::gl_copy_tex_image_2d),
  440. decltype(&GLContext::gl_rect),
  441. decltype(&GLContext::gl_tex_env),
  442. decltype(&GLContext::gl_tex_gen),
  443. decltype(&GLContext::gl_tex_gen_floatv),
  444. decltype(&GLContext::gl_fogf),
  445. decltype(&GLContext::gl_fogfv),
  446. decltype(&GLContext::gl_fogi),
  447. decltype(&GLContext::gl_lightf),
  448. decltype(&GLContext::gl_lightfv),
  449. decltype(&GLContext::gl_lightiv),
  450. decltype(&GLContext::gl_materialf),
  451. decltype(&GLContext::gl_materialfv),
  452. decltype(&GLContext::gl_materialiv),
  453. decltype(&GLContext::gl_color_material),
  454. decltype(&GLContext::gl_get_light),
  455. decltype(&GLContext::gl_clip_plane),
  456. decltype(&GLContext::gl_copy_tex_sub_image_2d),
  457. decltype(&GLContext::gl_point_size)>;
  458. using ExtraSavedArguments = Variant<
  459. FloatMatrix4x4>;
  460. Vector<NonnullOwnPtr<ExtraSavedArguments>> saved_arguments;
  461. Vector<FunctionsAndArgs> entries;
  462. };
  463. static constexpr size_t max_allowed_gl_call_depth { 128 };
  464. size_t m_gl_call_depth { 0 };
  465. Vector<Listing> m_listings;
  466. size_t m_list_base { 0 };
  467. struct CurrentListing {
  468. Listing listing;
  469. size_t index { 0 };
  470. GLenum mode { GL_COMPILE };
  471. };
  472. Optional<CurrentListing> m_current_listing_index;
  473. VertexAttribPointer m_client_vertex_pointer;
  474. VertexAttribPointer m_client_color_pointer;
  475. Vector<VertexAttribPointer> m_client_tex_coord_pointer;
  476. VertexAttribPointer m_client_normal_pointer;
  477. struct PixelParameters {
  478. i32 image_height { 0 };
  479. bool least_significant_bit_first { false };
  480. u8 pack_alignment { 4 };
  481. i32 row_length { 0 };
  482. i32 skip_images { 0 };
  483. i32 skip_pixels { 0 };
  484. i32 skip_rows { 0 };
  485. bool swap_bytes { false };
  486. };
  487. PixelParameters m_packing_parameters;
  488. PixelParameters m_unpacking_parameters;
  489. // Point drawing configuration
  490. bool m_point_smooth { false };
  491. float m_point_size { 1.f };
  492. // Line drawing configuration
  493. bool m_line_smooth { false };
  494. float m_line_width { 1.f };
  495. // Lighting configuration
  496. bool m_lighting_enabled { false };
  497. Vector<GPU::Light> m_light_states;
  498. Array<GPU::Material, 2u> m_material_states;
  499. // Color material
  500. bool m_color_material_enabled { false };
  501. GLenum m_color_material_face { GL_FRONT_AND_BACK };
  502. GLenum m_color_material_mode { GL_AMBIENT_AND_DIFFUSE };
  503. // GL Extension string
  504. ByteBuffer m_extensions;
  505. // Buffer objects
  506. NameAllocator m_buffer_name_allocator;
  507. HashMap<GLuint, RefPtr<Buffer>> m_allocated_buffers;
  508. RefPtr<Buffer> m_array_buffer;
  509. RefPtr<Buffer> m_element_array_buffer;
  510. };
  511. // Transposes input matrices (column-major) to our Matrix (row-major).
  512. template<typename I>
  513. constexpr FloatMatrix4x4 transpose_input_matrix(I const* matrix)
  514. {
  515. Array<float, 16> elements;
  516. for (size_t i = 0; i < 16; ++i)
  517. elements[i] = static_cast<float>(matrix[i]);
  518. // clang-format off
  519. return {
  520. elements[0], elements[4], elements[8], elements[12],
  521. elements[1], elements[5], elements[9], elements[13],
  522. elements[2], elements[6], elements[10], elements[14],
  523. elements[3], elements[7], elements[11], elements[15],
  524. };
  525. // clang-format on
  526. }
  527. template<>
  528. constexpr FloatMatrix4x4 transpose_input_matrix(float const* matrix)
  529. {
  530. // clang-format off
  531. return {
  532. matrix[0], matrix[4], matrix[8], matrix[12],
  533. matrix[1], matrix[5], matrix[9], matrix[13],
  534. matrix[2], matrix[6], matrix[10], matrix[14],
  535. matrix[3], matrix[7], matrix[11], matrix[15],
  536. };
  537. // clang-format on
  538. }
  539. ErrorOr<NonnullOwnPtr<GLContext>> create_context(Gfx::Bitmap&);
  540. void make_context_current(GLContext*);
  541. }