SoftwareGLContext.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/Optional.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/Tuple.h>
  11. #include <AK/Variant.h>
  12. #include <AK/Vector.h>
  13. #include <LibGL/GLContext.h>
  14. #include <LibGL/Tex/NameAllocator.h>
  15. #include <LibGL/Tex/Texture.h>
  16. #include <LibGL/Tex/TextureUnit.h>
  17. #include <LibGfx/Bitmap.h>
  18. #include <LibGfx/Matrix4x4.h>
  19. #include <LibGfx/Rect.h>
  20. #include <LibGfx/Vector3.h>
  21. #include <LibSoftGPU/Clipper.h>
  22. #include <LibSoftGPU/Device.h>
  23. #include <LibSoftGPU/Vertex.h>
  24. namespace GL {
  25. struct ContextParameter {
  26. GLenum type;
  27. u8 count { 1 };
  28. union {
  29. bool boolean_value;
  30. GLint integer_value;
  31. GLint integer_list[4];
  32. GLdouble double_value;
  33. GLdouble double_list[4];
  34. } value;
  35. };
  36. class SoftwareGLContext : public GLContext {
  37. public:
  38. SoftwareGLContext(Gfx::Bitmap&);
  39. virtual void gl_begin(GLenum mode) override;
  40. virtual void gl_clear(GLbitfield mask) override;
  41. virtual void gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) override;
  42. virtual void gl_clear_depth(GLdouble depth) override;
  43. virtual void gl_clear_stencil(GLint s) override;
  44. virtual void gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a) override;
  45. virtual void gl_delete_textures(GLsizei n, const GLuint* textures) override;
  46. virtual void gl_end() override;
  47. virtual void gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) override;
  48. virtual void gl_gen_textures(GLsizei n, GLuint* textures) override;
  49. virtual GLenum gl_get_error() override;
  50. virtual GLubyte* gl_get_string(GLenum name) override;
  51. virtual void gl_load_identity() override;
  52. virtual void gl_load_matrix(const FloatMatrix4x4& matrix) override;
  53. virtual void gl_matrix_mode(GLenum mode) override;
  54. virtual void gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) override;
  55. virtual void gl_push_matrix() override;
  56. virtual void gl_pop_matrix() override;
  57. virtual void gl_mult_matrix(FloatMatrix4x4 const& matrix) override;
  58. virtual void gl_rotate(GLdouble angle, GLdouble x, GLdouble y, GLdouble z) override;
  59. virtual void gl_scale(GLdouble x, GLdouble y, GLdouble z) override;
  60. virtual void gl_translate(GLdouble x, GLdouble y, GLdouble z) override;
  61. virtual void gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w) override;
  62. virtual void gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height) override;
  63. virtual void gl_enable(GLenum) override;
  64. virtual void gl_disable(GLenum) override;
  65. virtual GLboolean gl_is_enabled(GLenum) override;
  66. virtual void gl_front_face(GLenum) override;
  67. virtual void gl_cull_face(GLenum) override;
  68. virtual GLuint gl_gen_lists(GLsizei range) override;
  69. virtual void gl_call_list(GLuint list) override;
  70. virtual void gl_call_lists(GLsizei n, GLenum type, void const* lists) override;
  71. virtual void gl_delete_lists(GLuint list, GLsizei range) override;
  72. virtual void gl_list_base(GLuint base) override;
  73. virtual void gl_end_list(void) override;
  74. virtual void gl_new_list(GLuint list, GLenum mode) override;
  75. virtual GLboolean gl_is_list(GLuint list) override;
  76. virtual void gl_flush() override;
  77. virtual void gl_finish() override;
  78. virtual void gl_blend_func(GLenum src_factor, GLenum dst_factor) override;
  79. virtual void gl_shade_model(GLenum mode) override;
  80. virtual void gl_alpha_func(GLenum func, GLclampf ref) override;
  81. virtual void gl_hint(GLenum target, GLenum mode) override;
  82. virtual void gl_read_buffer(GLenum mode) override;
  83. virtual void gl_draw_buffer(GLenum buffer) override;
  84. virtual void gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels) override;
  85. virtual void gl_tex_image_2d(GLenum target, GLint level, GLint internal_format, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data) override;
  86. virtual void gl_tex_sub_image_2d(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid* data) override;
  87. virtual void gl_tex_parameter(GLenum target, GLenum pname, GLfloat param) override;
  88. virtual void gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q) override;
  89. virtual void gl_tex_env(GLenum target, GLenum pname, GLfloat param) override;
  90. virtual void gl_bind_texture(GLenum target, GLuint texture) override;
  91. virtual void gl_active_texture(GLenum texture) override;
  92. virtual void gl_depth_mask(GLboolean flag) override;
  93. virtual void gl_enable_client_state(GLenum cap) override;
  94. virtual void gl_disable_client_state(GLenum cap) override;
  95. virtual void gl_vertex_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer) override;
  96. virtual void gl_color_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer) override;
  97. virtual void gl_tex_coord_pointer(GLint size, GLenum type, GLsizei stride, const void* pointer) override;
  98. virtual void gl_draw_arrays(GLenum mode, GLint first, GLsizei count) override;
  99. virtual void gl_draw_elements(GLenum mode, GLsizei count, GLenum type, const void* indices) override;
  100. virtual void gl_draw_pixels(GLsizei width, GLsizei height, GLenum format, GLenum type, const void* data) override;
  101. virtual void gl_color_mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha) override;
  102. virtual void gl_get_booleanv(GLenum pname, GLboolean* data) override;
  103. virtual void gl_get_doublev(GLenum pname, GLdouble* params) override;
  104. virtual void gl_get_floatv(GLenum pname, GLfloat* params) override;
  105. virtual void gl_get_integerv(GLenum pname, GLint* data) override;
  106. virtual void gl_depth_range(GLdouble min, GLdouble max) override;
  107. virtual void gl_depth_func(GLenum func) override;
  108. virtual void gl_polygon_mode(GLenum face, GLenum mode) override;
  109. virtual void gl_polygon_offset(GLfloat factor, GLfloat units) override;
  110. virtual void gl_fogfv(GLenum pname, GLfloat* params) override;
  111. virtual void gl_fogf(GLenum pname, GLfloat param) override;
  112. virtual void gl_fogi(GLenum pname, GLint param) override;
  113. virtual void gl_pixel_storei(GLenum pname, GLint param) override;
  114. virtual void gl_scissor(GLint x, GLint y, GLsizei width, GLsizei height) override;
  115. virtual void gl_stencil_func_separate(GLenum face, GLenum func, GLint ref, GLuint mask) override;
  116. virtual void gl_stencil_op_separate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass) override;
  117. virtual void gl_normal(GLfloat nx, GLfloat ny, GLfloat nz) override;
  118. virtual void gl_raster_pos(GLfloat x, GLfloat y, GLfloat z, GLfloat w) override;
  119. virtual void gl_materialv(GLenum face, GLenum pname, GLfloat const* params) override;
  120. virtual void gl_line_width(GLfloat width) override;
  121. virtual void gl_push_attrib(GLbitfield mask) override;
  122. virtual void gl_pop_attrib() override;
  123. virtual void gl_light_model(GLenum pname, GLfloat x, GLfloat y, GLfloat z, GLfloat w) override;
  124. virtual void gl_bitmap(GLsizei width, GLsizei height, GLfloat xorig, GLfloat yorig, GLfloat xmove, GLfloat ymove, GLubyte const* bitmap) override;
  125. virtual void gl_copy_tex_image_2d(GLenum target, GLint level, GLenum internalformat, GLint x, GLint y, GLsizei width, GLsizei height, GLint border) override;
  126. virtual void present() override;
  127. private:
  128. template<typename T>
  129. T* store_in_listing(T value)
  130. {
  131. VERIFY(m_current_listing_index.has_value());
  132. auto& listing = m_current_listing_index->listing;
  133. listing.saved_arguments.empend(make<Listing::ExtraSavedArguments>(move(value)));
  134. return listing.saved_arguments.last()->template get_pointer<T>();
  135. }
  136. template<auto member, typename... Args>
  137. void append_to_listing(Args&&... args)
  138. {
  139. VERIFY(m_current_listing_index.has_value());
  140. m_current_listing_index->listing.entries.empend(member, Listing::ArgumentsFor<member> { forward<Args>(args)... });
  141. }
  142. Optional<ContextParameter> get_context_parameter(GLenum pname);
  143. template<typename T>
  144. void get_floating_point(GLenum pname, T* params);
  145. void invoke_list(size_t list_index);
  146. [[nodiscard]] bool should_append_to_listing() const { return m_current_listing_index.has_value(); }
  147. [[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; }
  148. GLenum m_current_draw_mode;
  149. GLenum m_current_matrix_mode;
  150. FloatMatrix4x4 m_projection_matrix = FloatMatrix4x4::identity();
  151. FloatMatrix4x4 m_model_view_matrix = FloatMatrix4x4::identity();
  152. FloatMatrix4x4 m_texture_matrix = FloatMatrix4x4::identity();
  153. Vector<FloatMatrix4x4> m_projection_matrix_stack;
  154. Vector<FloatMatrix4x4> m_model_view_matrix_stack;
  155. // FIXME: implement multi-texturing: the texture matrix stack should live inside a texture unit
  156. Vector<FloatMatrix4x4> m_texture_matrix_stack;
  157. FloatVector4 m_clear_color { 0.0f, 0.0f, 0.0f, 0.0f };
  158. double m_clear_depth { 1.0 };
  159. GLint m_clear_stencil { 0 };
  160. FloatVector4 m_current_vertex_color = { 1.0f, 1.0f, 1.0f, 1.0f };
  161. FloatVector4 m_current_vertex_tex_coord = { 0.0f, 0.0f, 0.0f, 1.0f };
  162. FloatVector3 m_current_vertex_normal = { 0.0f, 0.0f, 1.0f };
  163. Vector<SoftGPU::Vertex> m_vertex_list;
  164. GLenum m_error = GL_NO_ERROR;
  165. bool m_in_draw_state = false;
  166. bool m_depth_test_enabled = false;
  167. bool m_cull_faces = false;
  168. GLenum m_front_face = GL_CCW;
  169. GLenum m_culled_sides = GL_BACK;
  170. bool m_blend_enabled = false;
  171. GLenum m_blend_source_factor = GL_ONE;
  172. GLenum m_blend_destination_factor = GL_ZERO;
  173. bool m_alpha_test_enabled = false;
  174. GLenum m_alpha_test_func = GL_ALWAYS;
  175. GLclampf m_alpha_test_ref_value = 0;
  176. bool m_dither_enabled { true };
  177. // Stencil configuration
  178. bool m_stencil_test_enabled { false };
  179. struct StencilFunctionOptions {
  180. GLenum func { GL_ALWAYS };
  181. GLint reference_value { 0 };
  182. GLuint mask { NumericLimits<GLuint>::max() };
  183. };
  184. StencilFunctionOptions m_stencil_backfacing_func;
  185. StencilFunctionOptions m_stencil_frontfacing_func;
  186. struct StencilOperationOptions {
  187. GLenum op_fail { GL_KEEP };
  188. GLenum op_depth_fail { GL_KEEP };
  189. GLenum op_pass { GL_KEEP };
  190. };
  191. StencilOperationOptions m_stencil_backfacing_op;
  192. StencilOperationOptions m_stencil_frontfacing_op;
  193. GLenum m_current_read_buffer = GL_BACK;
  194. GLenum m_current_draw_buffer = GL_BACK;
  195. // Client side arrays
  196. bool m_client_side_vertex_array_enabled = false;
  197. bool m_client_side_color_array_enabled = false;
  198. bool m_client_side_texture_coord_array_enabled = false;
  199. NonnullRefPtr<Gfx::Bitmap> m_frontbuffer;
  200. // Texture objects
  201. TextureNameAllocator m_name_allocator;
  202. HashMap<GLuint, RefPtr<Texture>> m_allocated_textures;
  203. Array<TextureUnit, 32> m_texture_units;
  204. TextureUnit* m_active_texture_unit { &m_texture_units[0] };
  205. TextureUnit::BoundList m_bound_texture_units;
  206. SoftGPU::Device m_rasterizer;
  207. struct Listing {
  208. template<typename F>
  209. struct TupleTypeForArgumentListOf_;
  210. template<typename Ret, typename C, typename... Args>
  211. struct TupleTypeForArgumentListOf_<Ret (C::*)(Args...)> {
  212. using Type = Tuple<Args...>;
  213. };
  214. template<typename F>
  215. using TupleTypeForArgumentListOf = typename TupleTypeForArgumentListOf_<F>::Type;
  216. template<auto member>
  217. using ArgumentsFor = TupleTypeForArgumentListOf<decltype(member)>;
  218. template<typename... Fns>
  219. struct FunctionAndArgs {
  220. Variant<Fns...> function;
  221. Variant<TupleTypeForArgumentListOf<Fns>...> arguments;
  222. };
  223. using FunctionsAndArgs = FunctionAndArgs<
  224. decltype(&SoftwareGLContext::gl_begin),
  225. decltype(&SoftwareGLContext::gl_clear),
  226. decltype(&SoftwareGLContext::gl_clear_color),
  227. decltype(&SoftwareGLContext::gl_clear_depth),
  228. decltype(&SoftwareGLContext::gl_clear_stencil),
  229. decltype(&SoftwareGLContext::gl_color),
  230. decltype(&SoftwareGLContext::gl_end),
  231. decltype(&SoftwareGLContext::gl_frustum),
  232. decltype(&SoftwareGLContext::gl_load_identity),
  233. decltype(&SoftwareGLContext::gl_load_matrix),
  234. decltype(&SoftwareGLContext::gl_matrix_mode),
  235. decltype(&SoftwareGLContext::gl_ortho),
  236. decltype(&SoftwareGLContext::gl_push_matrix),
  237. decltype(&SoftwareGLContext::gl_pop_matrix),
  238. decltype(&SoftwareGLContext::gl_mult_matrix),
  239. decltype(&SoftwareGLContext::gl_rotate),
  240. decltype(&SoftwareGLContext::gl_scale),
  241. decltype(&SoftwareGLContext::gl_translate),
  242. decltype(&SoftwareGLContext::gl_vertex),
  243. decltype(&SoftwareGLContext::gl_viewport),
  244. decltype(&SoftwareGLContext::gl_enable),
  245. decltype(&SoftwareGLContext::gl_disable),
  246. decltype(&SoftwareGLContext::gl_front_face),
  247. decltype(&SoftwareGLContext::gl_cull_face),
  248. decltype(&SoftwareGLContext::gl_call_list),
  249. decltype(&SoftwareGLContext::gl_call_lists),
  250. decltype(&SoftwareGLContext::gl_blend_func),
  251. decltype(&SoftwareGLContext::gl_shade_model),
  252. decltype(&SoftwareGLContext::gl_alpha_func),
  253. decltype(&SoftwareGLContext::gl_hint),
  254. decltype(&SoftwareGLContext::gl_read_buffer),
  255. decltype(&SoftwareGLContext::gl_tex_parameter),
  256. decltype(&SoftwareGLContext::gl_depth_mask),
  257. decltype(&SoftwareGLContext::gl_draw_arrays),
  258. decltype(&SoftwareGLContext::gl_draw_elements),
  259. decltype(&SoftwareGLContext::gl_draw_pixels),
  260. decltype(&SoftwareGLContext::gl_depth_range),
  261. decltype(&SoftwareGLContext::gl_polygon_offset),
  262. decltype(&SoftwareGLContext::gl_scissor),
  263. decltype(&SoftwareGLContext::gl_stencil_func_separate),
  264. decltype(&SoftwareGLContext::gl_stencil_op_separate),
  265. decltype(&SoftwareGLContext::gl_normal),
  266. decltype(&SoftwareGLContext::gl_raster_pos),
  267. decltype(&SoftwareGLContext::gl_materialv),
  268. decltype(&SoftwareGLContext::gl_line_width),
  269. decltype(&SoftwareGLContext::gl_push_attrib),
  270. decltype(&SoftwareGLContext::gl_pop_attrib),
  271. decltype(&SoftwareGLContext::gl_light_model),
  272. decltype(&SoftwareGLContext::gl_bitmap),
  273. decltype(&SoftwareGLContext::gl_copy_tex_image_2d)>;
  274. using ExtraSavedArguments = Variant<
  275. FloatMatrix4x4>;
  276. Vector<NonnullOwnPtr<ExtraSavedArguments>> saved_arguments;
  277. Vector<FunctionsAndArgs> entries;
  278. };
  279. static constexpr size_t max_allowed_gl_call_depth { 128 };
  280. size_t m_gl_call_depth { 0 };
  281. Vector<Listing> m_listings;
  282. size_t m_list_base { 0 };
  283. struct CurrentListing {
  284. Listing listing;
  285. size_t index { 0 };
  286. GLenum mode { GL_COMPILE };
  287. };
  288. Optional<CurrentListing> m_current_listing_index;
  289. struct VertexAttribPointer {
  290. GLint size { 4 };
  291. GLenum type { GL_FLOAT };
  292. GLsizei stride { 0 };
  293. const void* pointer { 0 };
  294. };
  295. static void read_from_vertex_attribute_pointer(VertexAttribPointer const&, int index, float* elements, bool normalize);
  296. VertexAttribPointer m_client_vertex_pointer;
  297. VertexAttribPointer m_client_color_pointer;
  298. VertexAttribPointer m_client_tex_coord_pointer;
  299. u8 m_pack_alignment { 4 };
  300. GLsizei m_unpack_row_length { 0 };
  301. u8 m_unpack_alignment { 4 };
  302. struct RasterPosition {
  303. FloatVector3 window_coordinates { 0.0f, 0.0f, 0.0f };
  304. float clip_coordinate_value { 1.0f };
  305. float eye_coordinate_distance { 0.0f };
  306. bool valid { true };
  307. FloatVector4 color_rgba { 1.0f, 1.0f, 1.0f, 1.0f };
  308. float color_index { 1.0f };
  309. FloatVector4 texture_coordinates { 0.0f, 0.0f, 0.0f, 1.0f };
  310. };
  311. RasterPosition m_current_raster_position;
  312. float m_line_width { 1.0f };
  313. // Lighting configuration
  314. bool m_lighting_enabled { false };
  315. FloatVector4 m_light_model_ambient { 0.2f, 0.2f, 0.2f, 1.0f };
  316. GLfloat m_light_model_two_side { 0.0f };
  317. };
  318. }