GLContext.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. * Copyright (c) 2021-2022, Jesse Buhagiar <jooster669@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <AK/NonnullOwnPtr.h>
  10. #include <AK/Optional.h>
  11. #include <AK/RefPtr.h>
  12. #include <AK/Tuple.h>
  13. #include <AK/Variant.h>
  14. #include <AK/Vector.h>
  15. #include <LibGL/Tex/NameAllocator.h>
  16. #include <LibGL/Tex/Texture.h>
  17. #include <LibGL/Tex/TextureUnit.h>
  18. #include <LibGPU/Device.h>
  19. #include <LibGPU/DeviceInfo.h>
  20. #include <LibGPU/Light.h>
  21. #include <LibGPU/Vertex.h>
  22. #include <LibGfx/Bitmap.h>
  23. #include <LibGfx/Matrix4x4.h>
  24. #include <LibGfx/Rect.h>
  25. #include <LibGfx/Vector3.h>
  26. namespace GL {
  27. struct ContextParameter {
  28. GLenum type;
  29. bool is_capability { false };
  30. u8 count { 1 };
  31. union {
  32. bool boolean_value;
  33. GLint integer_value;
  34. GLint integer_list[4];
  35. GLdouble double_value;
  36. GLdouble double_list[4];
  37. } value;
  38. };
  39. enum Face {
  40. Front = 0,
  41. Back = 1,
  42. };
  43. class GLContext final {
  44. public:
  45. GLContext(Gfx::Bitmap&);
  46. ~GLContext();
  47. void gl_begin(GLenum mode);
  48. void gl_clear(GLbitfield mask);
  49. void gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha);
  50. void gl_clear_depth(GLdouble depth);
  51. void gl_clear_stencil(GLint s);
  52. void gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a);
  53. void gl_delete_textures(GLsizei n, GLuint const* textures);
  54. void gl_end();
  55. void gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
  56. void gl_gen_textures(GLsizei n, GLuint* textures);
  57. GLenum gl_get_error();
  58. GLubyte* gl_get_string(GLenum name);
  59. void gl_load_identity();
  60. void gl_load_matrix(FloatMatrix4x4 const& matrix);
  61. void gl_matrix_mode(GLenum mode);
  62. void gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val);
  63. void gl_push_matrix();
  64. void gl_pop_matrix();
  65. void gl_mult_matrix(FloatMatrix4x4 const& matrix);
  66. void gl_rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
  67. void gl_scale(GLdouble x, GLdouble y, GLdouble z);
  68. void gl_translate(GLdouble x, GLdouble y, GLdouble z);
  69. void gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w);
  70. void gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height);
  71. void gl_enable(GLenum);
  72. void gl_disable(GLenum);
  73. GLboolean gl_is_enabled(GLenum);
  74. void gl_front_face(GLenum);
  75. void gl_cull_face(GLenum);
  76. GLuint gl_gen_lists(GLsizei range);
  77. void gl_call_list(GLuint list);
  78. void gl_call_lists(GLsizei n, GLenum type, void const* lists);
  79. void gl_delete_lists(GLuint list, GLsizei range);
  80. void gl_list_base(GLuint base);
  81. void gl_end_list(void);
  82. void gl_new_list(GLuint list, GLenum mode);
  83. GLboolean gl_is_list(GLuint list);
  84. void gl_flush();
  85. void gl_finish();
  86. void gl_blend_func(GLenum src_factor, GLenum dst_factor);
  87. void gl_shade_model(GLenum mode);
  88. void gl_alpha_func(GLenum func, GLclampf ref);
  89. void gl_hint(GLenum target, GLenum mode);
  90. void gl_read_buffer(GLenum mode);
  91. void gl_draw_buffer(GLenum buffer);
  92. void gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels);
  93. 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);
  94. 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);
  95. void gl_tex_parameter(GLenum target, GLenum pname, GLfloat param);
  96. void gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q);
  97. void gl_multi_tex_coord(GLenum target, GLfloat s, GLfloat t, GLfloat r, GLfloat q);
  98. void gl_tex_env(GLenum target, GLenum pname, GLfloat param);
  99. void gl_bind_texture(GLenum target, GLuint texture);
  100. GLboolean gl_is_texture(GLuint texture);
  101. void gl_active_texture(GLenum texture);
  102. void gl_depth_mask(GLboolean flag);
  103. void gl_enable_client_state(GLenum cap);
  104. void gl_disable_client_state(GLenum cap);
  105. void gl_client_active_texture(GLenum target);
  106. void gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer);
  107. void gl_color_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer);
  108. void gl_tex_coord_pointer(GLint size, GLenum type, GLsizei stride, void const* pointer);
  109. void gl_draw_arrays(GLenum mode, GLint first, GLsizei count);
  110. void gl_draw_elements(GLenum mode, GLsizei count, GLenum type, void const* indices);
  111. void gl_draw_pixels(GLsizei width, GLsizei height, GLenum format, GLenum type, void const* data);
  112. void gl_color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);
  113. void gl_get_booleanv(GLenum pname, GLboolean* data);
  114. void gl_get_doublev(GLenum pname, GLdouble* params);
  115. void gl_get_floatv(GLenum pname, GLfloat* params);
  116. void gl_get_integerv(GLenum pname, GLint* data);
  117. void gl_depth_range(GLdouble min, GLdouble max);
  118. void gl_depth_func(GLenum func);
  119. void gl_polygon_mode(GLenum face, GLenum mode);
  120. void gl_polygon_offset(GLfloat factor, GLfloat units);
  121. void gl_fogfv(GLenum pname, GLfloat* params);
  122. void gl_fogf(GLenum pname, GLfloat param);
  123. void gl_fogi(GLenum pname, GLint param);
  124. void gl_pixel_storei(GLenum pname, GLint param);
  125. void gl_scissor(GLint x, GLint y, GLsizei width, GLsizei height);
  126. void gl_stencil_func_separate(GLenum face, GLenum func, GLint ref, GLuint mask);
  127. void gl_stencil_mask_separate(GLenum face, GLuint mask);
  128. void gl_stencil_op_separate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass);
  129. void gl_normal(GLfloat nx, GLfloat ny, GLfloat nz);
  130. void gl_normal_pointer(GLenum type, GLsizei stride, void const* pointer);
  131. void gl_raster_pos(GLfloat x, GLfloat y, GLfloat z, GLfloat w);
  132. void gl_line_width(GLfloat width);
  133. void gl_push_attrib(GLbitfield mask);
  134. void gl_pop_attrib();
  135. void gl_light_model(GLenum pname, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
  136. void gl_bitmap(GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, GLubyte const* bitmap);
  137. void gl_copy_tex_image_2d(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border);
  138. void gl_get_tex_parameter_integerv(GLenum target, GLint level, GLenum pname, GLint* params);
  139. void gl_rect(GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2);
  140. void gl_tex_gen(GLenum coord, GLenum pname, GLint param);
  141. void gl_tex_gen_floatv(GLenum coord, GLenum pname, GLfloat const* params);
  142. void gl_lightf(GLenum light, GLenum pname, GLfloat param);
  143. void gl_lightfv(GLenum light, GLenum pname, GLfloat const* params);
  144. void gl_lightiv(GLenum light, GLenum pname, GLint const* params);
  145. void gl_materialf(GLenum face, GLenum pname, GLfloat param);
  146. void gl_materialfv(GLenum face, GLenum pname, GLfloat const* params);
  147. void gl_materialiv(GLenum face, GLenum pname, GLint const* params);
  148. void gl_color_material(GLenum face, GLenum mode);
  149. void gl_get_light(GLenum light, GLenum pname, void* params, GLenum type);
  150. void gl_get_material(GLenum face, GLenum pname, void* params, GLenum type);
  151. void present();
  152. private:
  153. void sync_device_config();
  154. void sync_device_sampler_config();
  155. void sync_device_texcoord_config();
  156. void sync_light_state();
  157. void sync_stencil_configuration();
  158. void build_extension_string();
  159. template<typename T>
  160. T* store_in_listing(T value)
  161. {
  162. VERIFY(m_current_listing_index.has_value());
  163. auto& listing = m_current_listing_index->listing;
  164. listing.saved_arguments.empend(make<Listing::ExtraSavedArguments>(move(value)));
  165. return listing.saved_arguments.last()->template get_pointer<T>();
  166. }
  167. template<auto member, typename... Args>
  168. void append_to_listing(Args&&... args)
  169. {
  170. VERIFY(m_current_listing_index.has_value());
  171. m_current_listing_index->listing.entries.empend(member, Listing::ArgumentsFor<member> { forward<Args>(args)... });
  172. }
  173. Optional<ContextParameter> get_context_parameter(GLenum pname);
  174. template<typename T>
  175. void get_floating_point(GLenum pname, T* params);
  176. template<typename T>
  177. void get_light_param(GLenum light, GLenum pname, T* params);
  178. template<typename T>
  179. void get_material_param(Face face, GLenum pname, T* params);
  180. void invoke_list(size_t list_index);
  181. [[nodiscard]] bool should_append_to_listing() const { return m_current_listing_index.has_value(); }
  182. [[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; }
  183. GLenum m_current_draw_mode;
  184. GLenum m_current_matrix_mode { GL_MODELVIEW };
  185. FloatMatrix4x4 m_projection_matrix { FloatMatrix4x4::identity() };
  186. FloatMatrix4x4 m_model_view_matrix { FloatMatrix4x4::identity() };
  187. FloatMatrix4x4 m_texture_matrix { FloatMatrix4x4::identity() };
  188. FloatMatrix4x4* m_current_matrix { &m_model_view_matrix };
  189. Vector<FloatMatrix4x4> m_projection_matrix_stack;
  190. Vector<FloatMatrix4x4> m_model_view_matrix_stack;
  191. // FIXME: implement multi-texturing: the texture matrix stack should live inside a texture unit
  192. Vector<FloatMatrix4x4> m_texture_matrix_stack;
  193. Vector<FloatMatrix4x4>* m_current_matrix_stack { &m_model_view_matrix_stack };
  194. Gfx::IntRect m_viewport;
  195. FloatVector4 m_clear_color { 0.0f, 0.0f, 0.0f, 0.0f };
  196. float m_clear_depth { 1.f };
  197. u8 m_clear_stencil { 0 };
  198. FloatVector4 m_current_vertex_color { 1.0f, 1.0f, 1.0f, 1.0f };
  199. Vector<FloatVector4> m_current_vertex_tex_coord;
  200. FloatVector3 m_current_vertex_normal { 0.0f, 0.0f, 1.0f };
  201. Vector<GPU::Vertex> m_vertex_list;
  202. GLenum m_error = GL_NO_ERROR;
  203. bool m_in_draw_state = false;
  204. bool m_depth_test_enabled { false };
  205. bool m_depth_offset_enabled { false };
  206. bool m_cull_faces = false;
  207. GLenum m_front_face = GL_CCW;
  208. GLenum m_culled_sides = GL_BACK;
  209. bool m_blend_enabled = false;
  210. GLenum m_blend_source_factor = GL_ONE;
  211. GLenum m_blend_destination_factor = GL_ZERO;
  212. bool m_alpha_test_enabled = false;
  213. GLenum m_alpha_test_func = GL_ALWAYS;
  214. GLclampf m_alpha_test_ref_value = 0;
  215. bool m_dither_enabled { true };
  216. bool m_normalize { false };
  217. // Stencil configuration
  218. bool m_stencil_test_enabled { false };
  219. bool m_stencil_configuration_dirty { true };
  220. struct StencilFunctionOptions {
  221. GLenum func { GL_ALWAYS };
  222. GLint reference_value { 0 };
  223. GLuint mask { NumericLimits<GLuint>::max() };
  224. };
  225. Array<StencilFunctionOptions, 2u> m_stencil_function;
  226. struct StencilOperationOptions {
  227. GLenum op_fail { GL_KEEP };
  228. GLenum op_depth_fail { GL_KEEP };
  229. GLenum op_pass { GL_KEEP };
  230. GLuint write_mask { NumericLimits<GLuint>::max() };
  231. };
  232. Array<StencilOperationOptions, 2u> m_stencil_operation;
  233. GLenum m_current_read_buffer = GL_BACK;
  234. GLenum m_current_draw_buffer = GL_BACK;
  235. // Client side arrays
  236. bool m_client_side_vertex_array_enabled { false };
  237. bool m_client_side_color_array_enabled { false };
  238. Vector<bool> m_client_side_texture_coord_array_enabled;
  239. size_t m_client_active_texture = 0;
  240. bool m_client_side_normal_array_enabled { false };
  241. NonnullRefPtr<Gfx::Bitmap> m_frontbuffer;
  242. // Texture objects
  243. template<typename T>
  244. RefPtr<T> get_default_texture(GLenum target)
  245. {
  246. auto default_texture = m_default_textures.get(target);
  247. VERIFY(default_texture.has_value());
  248. return static_cast<T*>(default_texture.value());
  249. }
  250. TextureNameAllocator m_name_allocator;
  251. HashMap<GLuint, RefPtr<Texture>> m_allocated_textures;
  252. HashMap<GLenum, RefPtr<Texture>> m_default_textures;
  253. Vector<TextureUnit> m_texture_units;
  254. TextureUnit* m_active_texture_unit;
  255. size_t m_active_texture_unit_index { 0 };
  256. // Texture coordinate generation state
  257. struct TextureCoordinateGeneration {
  258. bool enabled { false };
  259. GLenum generation_mode { GL_EYE_LINEAR };
  260. FloatVector4 object_plane_coefficients { 0.0f, 0.0f, 0.0f, 0.0f };
  261. FloatVector4 eye_plane_coefficients { 0.0f, 0.0f, 0.0f, 0.0f };
  262. };
  263. Vector<Array<TextureCoordinateGeneration, 4>> m_texture_coordinate_generation;
  264. bool m_texcoord_generation_dirty { true };
  265. ALWAYS_INLINE TextureCoordinateGeneration& texture_coordinate_generation(size_t texture_unit, GLenum capability)
  266. {
  267. return m_texture_coordinate_generation[texture_unit][capability - GL_TEXTURE_GEN_S];
  268. }
  269. NonnullOwnPtr<GPU::Device> m_rasterizer;
  270. GPU::DeviceInfo const m_device_info;
  271. bool m_sampler_config_is_dirty { true };
  272. bool m_light_state_is_dirty { true };
  273. struct Listing {
  274. template<typename F>
  275. struct TupleTypeForArgumentListOf_;
  276. template<typename Ret, typename C, typename... Args>
  277. struct TupleTypeForArgumentListOf_<Ret (C::*)(Args...)> {
  278. using Type = Tuple<Args...>;
  279. };
  280. template<typename F>
  281. using TupleTypeForArgumentListOf = typename TupleTypeForArgumentListOf_<F>::Type;
  282. template<auto member>
  283. using ArgumentsFor = TupleTypeForArgumentListOf<decltype(member)>;
  284. template<typename... Fns>
  285. struct FunctionAndArgs {
  286. Variant<Fns...> function;
  287. Variant<TupleTypeForArgumentListOf<Fns>...> arguments;
  288. };
  289. using FunctionsAndArgs = FunctionAndArgs<
  290. decltype(&GLContext::gl_begin),
  291. decltype(&GLContext::gl_clear),
  292. decltype(&GLContext::gl_clear_color),
  293. decltype(&GLContext::gl_clear_depth),
  294. decltype(&GLContext::gl_clear_stencil),
  295. decltype(&GLContext::gl_color),
  296. decltype(&GLContext::gl_end),
  297. decltype(&GLContext::gl_frustum),
  298. decltype(&GLContext::gl_load_identity),
  299. decltype(&GLContext::gl_load_matrix),
  300. decltype(&GLContext::gl_matrix_mode),
  301. decltype(&GLContext::gl_ortho),
  302. decltype(&GLContext::gl_push_matrix),
  303. decltype(&GLContext::gl_pop_matrix),
  304. decltype(&GLContext::gl_mult_matrix),
  305. decltype(&GLContext::gl_rotate),
  306. decltype(&GLContext::gl_scale),
  307. decltype(&GLContext::gl_translate),
  308. decltype(&GLContext::gl_vertex),
  309. decltype(&GLContext::gl_viewport),
  310. decltype(&GLContext::gl_enable),
  311. decltype(&GLContext::gl_disable),
  312. decltype(&GLContext::gl_front_face),
  313. decltype(&GLContext::gl_cull_face),
  314. decltype(&GLContext::gl_call_list),
  315. decltype(&GLContext::gl_call_lists),
  316. decltype(&GLContext::gl_blend_func),
  317. decltype(&GLContext::gl_shade_model),
  318. decltype(&GLContext::gl_alpha_func),
  319. decltype(&GLContext::gl_hint),
  320. decltype(&GLContext::gl_read_buffer),
  321. decltype(&GLContext::gl_tex_parameter),
  322. decltype(&GLContext::gl_depth_mask),
  323. decltype(&GLContext::gl_draw_arrays),
  324. decltype(&GLContext::gl_draw_elements),
  325. decltype(&GLContext::gl_draw_pixels),
  326. decltype(&GLContext::gl_depth_range),
  327. decltype(&GLContext::gl_polygon_offset),
  328. decltype(&GLContext::gl_scissor),
  329. decltype(&GLContext::gl_stencil_func_separate),
  330. decltype(&GLContext::gl_stencil_mask_separate),
  331. decltype(&GLContext::gl_stencil_op_separate),
  332. decltype(&GLContext::gl_normal),
  333. decltype(&GLContext::gl_raster_pos),
  334. decltype(&GLContext::gl_line_width),
  335. decltype(&GLContext::gl_push_attrib),
  336. decltype(&GLContext::gl_pop_attrib),
  337. decltype(&GLContext::gl_light_model),
  338. decltype(&GLContext::gl_bitmap),
  339. decltype(&GLContext::gl_copy_tex_image_2d),
  340. decltype(&GLContext::gl_rect),
  341. decltype(&GLContext::gl_tex_gen),
  342. decltype(&GLContext::gl_tex_gen_floatv),
  343. decltype(&GLContext::gl_fogf),
  344. decltype(&GLContext::gl_fogfv),
  345. decltype(&GLContext::gl_fogi),
  346. decltype(&GLContext::gl_lightf),
  347. decltype(&GLContext::gl_lightfv),
  348. decltype(&GLContext::gl_lightiv),
  349. decltype(&GLContext::gl_materialf),
  350. decltype(&GLContext::gl_materialfv),
  351. decltype(&GLContext::gl_materialiv),
  352. decltype(&GLContext::gl_color_material),
  353. decltype(&GLContext::gl_get_light)>;
  354. using ExtraSavedArguments = Variant<
  355. FloatMatrix4x4>;
  356. Vector<NonnullOwnPtr<ExtraSavedArguments>> saved_arguments;
  357. Vector<FunctionsAndArgs> entries;
  358. };
  359. static constexpr size_t max_allowed_gl_call_depth { 128 };
  360. size_t m_gl_call_depth { 0 };
  361. Vector<Listing> m_listings;
  362. size_t m_list_base { 0 };
  363. struct CurrentListing {
  364. Listing listing;
  365. size_t index { 0 };
  366. GLenum mode { GL_COMPILE };
  367. };
  368. Optional<CurrentListing> m_current_listing_index;
  369. struct VertexAttribPointer {
  370. GLint size { 4 };
  371. GLenum type { GL_FLOAT };
  372. bool normalize { true };
  373. GLsizei stride { 0 };
  374. void const* pointer { 0 };
  375. };
  376. static void read_from_vertex_attribute_pointer(VertexAttribPointer const&, int index, float* elements);
  377. VertexAttribPointer m_client_vertex_pointer;
  378. VertexAttribPointer m_client_color_pointer;
  379. Vector<VertexAttribPointer> m_client_tex_coord_pointer;
  380. VertexAttribPointer m_client_normal_pointer;
  381. u8 m_pack_alignment { 4 };
  382. GLsizei m_unpack_row_length { 0 };
  383. u8 m_unpack_alignment { 4 };
  384. float m_line_width { 1.0f };
  385. // Lighting configuration
  386. bool m_lighting_enabled { false };
  387. Vector<GPU::Light> m_light_states;
  388. Array<GPU::Material, 2u> m_material_states;
  389. // Color material
  390. bool m_color_material_enabled { false };
  391. GLenum m_color_material_face { GL_FRONT_AND_BACK };
  392. GLenum m_color_material_mode { GL_AMBIENT_AND_DIFFUSE };
  393. // GL Extension string
  394. String m_extensions;
  395. };
  396. NonnullOwnPtr<GLContext> create_context(Gfx::Bitmap&);
  397. void make_context_current(GLContext*);
  398. void present_context(GLContext*);
  399. }