GLContext.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 <LibGfx/Bitmap.h>
  19. #include <LibGfx/Matrix4x4.h>
  20. #include <LibGfx/Rect.h>
  21. #include <LibGfx/Vector3.h>
  22. #include <LibSoftGPU/Clipper.h>
  23. #include <LibSoftGPU/Device.h>
  24. #include <LibSoftGPU/Light/Light.h>
  25. #include <LibSoftGPU/Vertex.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, const GLuint* 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(const FloatMatrix4x4& 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, const void* pointer);
  107. void gl_color_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer);
  108. void gl_tex_coord_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer);
  109. void gl_draw_arrays(GLenum mode, GLint first, GLsizei count);
  110. void gl_draw_elements(GLenum mode, GLsizei count, GLenum type, const void* indices);
  111. void gl_draw_pixels(GLsizei width, GLsizei height, GLenum format, GLenum type, const void* 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;
  185. FloatMatrix4x4 m_projection_matrix = FloatMatrix4x4::identity();
  186. FloatMatrix4x4 m_model_view_matrix = FloatMatrix4x4::identity();
  187. FloatMatrix4x4 m_texture_matrix = FloatMatrix4x4::identity();
  188. Vector<FloatMatrix4x4> m_projection_matrix_stack;
  189. Vector<FloatMatrix4x4> m_model_view_matrix_stack;
  190. // FIXME: implement multi-texturing: the texture matrix stack should live inside a texture unit
  191. Vector<FloatMatrix4x4> m_texture_matrix_stack;
  192. Gfx::IntRect m_viewport;
  193. FloatVector4 m_clear_color { 0.0f, 0.0f, 0.0f, 0.0f };
  194. float m_clear_depth { 1.f };
  195. u8 m_clear_stencil { 0 };
  196. FloatVector4 m_current_vertex_color { 1.0f, 1.0f, 1.0f, 1.0f };
  197. Vector<FloatVector4> m_current_vertex_tex_coord;
  198. FloatVector3 m_current_vertex_normal { 0.0f, 0.0f, 1.0f };
  199. Vector<SoftGPU::Vertex> m_vertex_list;
  200. GLenum m_error = GL_NO_ERROR;
  201. bool m_in_draw_state = false;
  202. bool m_depth_test_enabled { false };
  203. bool m_depth_offset_enabled { false };
  204. bool m_cull_faces = false;
  205. GLenum m_front_face = GL_CCW;
  206. GLenum m_culled_sides = GL_BACK;
  207. bool m_blend_enabled = false;
  208. GLenum m_blend_source_factor = GL_ONE;
  209. GLenum m_blend_destination_factor = GL_ZERO;
  210. bool m_alpha_test_enabled = false;
  211. GLenum m_alpha_test_func = GL_ALWAYS;
  212. GLclampf m_alpha_test_ref_value = 0;
  213. bool m_dither_enabled { true };
  214. bool m_normalize { false };
  215. // Stencil configuration
  216. bool m_stencil_test_enabled { false };
  217. bool m_stencil_configuration_dirty { true };
  218. struct StencilFunctionOptions {
  219. GLenum func { GL_ALWAYS };
  220. GLint reference_value { 0 };
  221. GLuint mask { NumericLimits<GLuint>::max() };
  222. };
  223. Array<StencilFunctionOptions, 2u> m_stencil_function;
  224. struct StencilOperationOptions {
  225. GLenum op_fail { GL_KEEP };
  226. GLenum op_depth_fail { GL_KEEP };
  227. GLenum op_pass { GL_KEEP };
  228. GLuint write_mask { NumericLimits<GLuint>::max() };
  229. };
  230. Array<StencilOperationOptions, 2u> m_stencil_operation;
  231. GLenum m_current_read_buffer = GL_BACK;
  232. GLenum m_current_draw_buffer = GL_BACK;
  233. // Client side arrays
  234. bool m_client_side_vertex_array_enabled { false };
  235. bool m_client_side_color_array_enabled { false };
  236. Vector<bool> m_client_side_texture_coord_array_enabled;
  237. size_t m_client_active_texture = 0;
  238. bool m_client_side_normal_array_enabled { false };
  239. NonnullRefPtr<Gfx::Bitmap> m_frontbuffer;
  240. // Texture objects
  241. template<typename T>
  242. RefPtr<T> get_default_texture(GLenum target)
  243. {
  244. auto default_texture = m_default_textures.get(target);
  245. VERIFY(default_texture.has_value());
  246. return static_cast<T*>(default_texture.value());
  247. }
  248. TextureNameAllocator m_name_allocator;
  249. HashMap<GLuint, RefPtr<Texture>> m_allocated_textures;
  250. HashMap<GLenum, RefPtr<Texture>> m_default_textures;
  251. Vector<TextureUnit> m_texture_units;
  252. TextureUnit* m_active_texture_unit;
  253. size_t m_active_texture_unit_index { 0 };
  254. // Texture coordinate generation state
  255. struct TextureCoordinateGeneration {
  256. bool enabled { false };
  257. GLenum generation_mode { GL_EYE_LINEAR };
  258. FloatVector4 object_plane_coefficients { 0.0f, 0.0f, 0.0f, 0.0f };
  259. FloatVector4 eye_plane_coefficients { 0.0f, 0.0f, 0.0f, 0.0f };
  260. };
  261. Vector<Array<TextureCoordinateGeneration, 4>> m_texture_coordinate_generation;
  262. bool m_texcoord_generation_dirty { true };
  263. ALWAYS_INLINE TextureCoordinateGeneration& texture_coordinate_generation(size_t texture_unit, GLenum capability)
  264. {
  265. return m_texture_coordinate_generation[texture_unit][capability - GL_TEXTURE_GEN_S];
  266. }
  267. SoftGPU::Device m_rasterizer;
  268. SoftGPU::DeviceInfo const m_device_info;
  269. bool m_sampler_config_is_dirty { true };
  270. bool m_light_state_is_dirty { true };
  271. struct Listing {
  272. template<typename F>
  273. struct TupleTypeForArgumentListOf_;
  274. template<typename Ret, typename C, typename... Args>
  275. struct TupleTypeForArgumentListOf_<Ret (C::*)(Args...)> {
  276. using Type = Tuple<Args...>;
  277. };
  278. template<typename F>
  279. using TupleTypeForArgumentListOf = typename TupleTypeForArgumentListOf_<F>::Type;
  280. template<auto member>
  281. using ArgumentsFor = TupleTypeForArgumentListOf<decltype(member)>;
  282. template<typename... Fns>
  283. struct FunctionAndArgs {
  284. Variant<Fns...> function;
  285. Variant<TupleTypeForArgumentListOf<Fns>...> arguments;
  286. };
  287. using FunctionsAndArgs = FunctionAndArgs<
  288. decltype(&GLContext::gl_begin),
  289. decltype(&GLContext::gl_clear),
  290. decltype(&GLContext::gl_clear_color),
  291. decltype(&GLContext::gl_clear_depth),
  292. decltype(&GLContext::gl_clear_stencil),
  293. decltype(&GLContext::gl_color),
  294. decltype(&GLContext::gl_end),
  295. decltype(&GLContext::gl_frustum),
  296. decltype(&GLContext::gl_load_identity),
  297. decltype(&GLContext::gl_load_matrix),
  298. decltype(&GLContext::gl_matrix_mode),
  299. decltype(&GLContext::gl_ortho),
  300. decltype(&GLContext::gl_push_matrix),
  301. decltype(&GLContext::gl_pop_matrix),
  302. decltype(&GLContext::gl_mult_matrix),
  303. decltype(&GLContext::gl_rotate),
  304. decltype(&GLContext::gl_scale),
  305. decltype(&GLContext::gl_translate),
  306. decltype(&GLContext::gl_vertex),
  307. decltype(&GLContext::gl_viewport),
  308. decltype(&GLContext::gl_enable),
  309. decltype(&GLContext::gl_disable),
  310. decltype(&GLContext::gl_front_face),
  311. decltype(&GLContext::gl_cull_face),
  312. decltype(&GLContext::gl_call_list),
  313. decltype(&GLContext::gl_call_lists),
  314. decltype(&GLContext::gl_blend_func),
  315. decltype(&GLContext::gl_shade_model),
  316. decltype(&GLContext::gl_alpha_func),
  317. decltype(&GLContext::gl_hint),
  318. decltype(&GLContext::gl_read_buffer),
  319. decltype(&GLContext::gl_tex_parameter),
  320. decltype(&GLContext::gl_depth_mask),
  321. decltype(&GLContext::gl_draw_arrays),
  322. decltype(&GLContext::gl_draw_elements),
  323. decltype(&GLContext::gl_draw_pixels),
  324. decltype(&GLContext::gl_depth_range),
  325. decltype(&GLContext::gl_polygon_offset),
  326. decltype(&GLContext::gl_scissor),
  327. decltype(&GLContext::gl_stencil_func_separate),
  328. decltype(&GLContext::gl_stencil_mask_separate),
  329. decltype(&GLContext::gl_stencil_op_separate),
  330. decltype(&GLContext::gl_normal),
  331. decltype(&GLContext::gl_raster_pos),
  332. decltype(&GLContext::gl_line_width),
  333. decltype(&GLContext::gl_push_attrib),
  334. decltype(&GLContext::gl_pop_attrib),
  335. decltype(&GLContext::gl_light_model),
  336. decltype(&GLContext::gl_bitmap),
  337. decltype(&GLContext::gl_copy_tex_image_2d),
  338. decltype(&GLContext::gl_rect),
  339. decltype(&GLContext::gl_tex_gen),
  340. decltype(&GLContext::gl_tex_gen_floatv),
  341. decltype(&GLContext::gl_fogf),
  342. decltype(&GLContext::gl_fogfv),
  343. decltype(&GLContext::gl_fogi),
  344. decltype(&GLContext::gl_lightf),
  345. decltype(&GLContext::gl_lightfv),
  346. decltype(&GLContext::gl_lightiv),
  347. decltype(&GLContext::gl_materialf),
  348. decltype(&GLContext::gl_materialfv),
  349. decltype(&GLContext::gl_materialiv),
  350. decltype(&GLContext::gl_color_material),
  351. decltype(&GLContext::gl_get_light)>;
  352. using ExtraSavedArguments = Variant<
  353. FloatMatrix4x4>;
  354. Vector<NonnullOwnPtr<ExtraSavedArguments>> saved_arguments;
  355. Vector<FunctionsAndArgs> entries;
  356. };
  357. static constexpr size_t max_allowed_gl_call_depth { 128 };
  358. size_t m_gl_call_depth { 0 };
  359. Vector<Listing> m_listings;
  360. size_t m_list_base { 0 };
  361. struct CurrentListing {
  362. Listing listing;
  363. size_t index { 0 };
  364. GLenum mode { GL_COMPILE };
  365. };
  366. Optional<CurrentListing> m_current_listing_index;
  367. struct VertexAttribPointer {
  368. GLint size { 4 };
  369. GLenum type { GL_FLOAT };
  370. GLsizei stride { 0 };
  371. const void* pointer { 0 };
  372. };
  373. static void read_from_vertex_attribute_pointer(VertexAttribPointer const&, int index, float* elements, bool normalize);
  374. VertexAttribPointer m_client_vertex_pointer;
  375. VertexAttribPointer m_client_color_pointer;
  376. Vector<VertexAttribPointer> m_client_tex_coord_pointer;
  377. VertexAttribPointer m_client_normal_pointer;
  378. u8 m_pack_alignment { 4 };
  379. GLsizei m_unpack_row_length { 0 };
  380. u8 m_unpack_alignment { 4 };
  381. float m_line_width { 1.0f };
  382. // Lighting configuration
  383. bool m_lighting_enabled { false };
  384. Vector<SoftGPU::Light> m_light_states;
  385. Array<SoftGPU::Material, 2u> m_material_states;
  386. // Color material
  387. bool m_color_material_enabled { false };
  388. GLenum m_color_material_face { GL_FRONT_AND_BACK };
  389. GLenum m_color_material_mode { GL_AMBIENT_AND_DIFFUSE };
  390. // GL Extension string
  391. String m_extensions;
  392. };
  393. NonnullOwnPtr<GLContext> create_context(Gfx::Bitmap&);
  394. void make_context_current(GLContext*);
  395. void present_context(GLContext*);
  396. }