SoftwareGLContext.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "Clipper.h"
  8. #include "GLContext.h"
  9. #include "GLStruct.h"
  10. #include "SoftwareRasterizer.h"
  11. #include "Tex/NameAllocator.h"
  12. #include "Tex/Texture.h"
  13. #include "Tex/TextureUnit.h"
  14. #include <AK/HashMap.h>
  15. #include <AK/RefPtr.h>
  16. #include <AK/Tuple.h>
  17. #include <AK/Variant.h>
  18. #include <AK/Vector.h>
  19. #include <LibGfx/Bitmap.h>
  20. #include <LibGfx/Matrix4x4.h>
  21. #include <LibGfx/Vector3.h>
  22. namespace GL {
  23. class SoftwareGLContext : public GLContext {
  24. public:
  25. SoftwareGLContext(Gfx::Bitmap&);
  26. virtual void gl_begin(GLenum mode) override;
  27. virtual void gl_clear(GLbitfield mask) override;
  28. virtual void gl_clear_color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha) override;
  29. virtual void gl_clear_depth(GLdouble depth) override;
  30. virtual void gl_color(GLdouble r, GLdouble g, GLdouble b, GLdouble a) override;
  31. virtual void gl_delete_textures(GLsizei n, const GLuint* textures) override;
  32. virtual void gl_end() override;
  33. virtual void gl_frustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) override;
  34. virtual void gl_gen_textures(GLsizei n, GLuint* textures) override;
  35. virtual GLenum gl_get_error() override;
  36. virtual GLubyte* gl_get_string(GLenum name) override;
  37. virtual void gl_load_identity() override;
  38. virtual void gl_load_matrix(const FloatMatrix4x4& matrix) override;
  39. virtual void gl_matrix_mode(GLenum mode) override;
  40. virtual void gl_ortho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near_val, GLdouble far_val) override;
  41. virtual void gl_push_matrix() override;
  42. virtual void gl_pop_matrix() override;
  43. virtual void gl_rotate(GLdouble angle, GLdouble x, GLdouble y, GLdouble z) override;
  44. virtual void gl_scale(GLdouble x, GLdouble y, GLdouble z) override;
  45. virtual void gl_translate(GLdouble x, GLdouble y, GLdouble z) override;
  46. virtual void gl_vertex(GLdouble x, GLdouble y, GLdouble z, GLdouble w) override;
  47. virtual void gl_viewport(GLint x, GLint y, GLsizei width, GLsizei height) override;
  48. virtual void gl_enable(GLenum) override;
  49. virtual void gl_disable(GLenum) override;
  50. virtual void gl_front_face(GLenum) override;
  51. virtual void gl_cull_face(GLenum) override;
  52. virtual GLuint gl_gen_lists(GLsizei range) override;
  53. virtual void gl_call_list(GLuint list) override;
  54. virtual void gl_delete_lists(GLuint list, GLsizei range) override;
  55. virtual void gl_end_list(void) override;
  56. virtual void gl_new_list(GLuint list, GLenum mode) override;
  57. virtual void gl_flush() override;
  58. virtual void gl_finish() override;
  59. virtual void gl_blend_func(GLenum src_factor, GLenum dst_factor) override;
  60. virtual void gl_shade_model(GLenum mode) override;
  61. virtual void gl_alpha_func(GLenum func, GLclampf ref) override;
  62. virtual void gl_hint(GLenum target, GLenum mode) override;
  63. virtual void gl_read_buffer(GLenum mode) override;
  64. virtual void gl_read_pixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, GLvoid* pixels) override;
  65. 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;
  66. virtual void gl_tex_coord(GLfloat s, GLfloat t, GLfloat r, GLfloat q) override;
  67. virtual void gl_bind_texture(GLenum target, GLuint texture) override;
  68. virtual void gl_active_texture(GLenum texture) override;
  69. virtual void present() override;
  70. private:
  71. template<typename T>
  72. T* store_in_listing(T value)
  73. {
  74. VERIFY(m_current_listing_index.has_value());
  75. auto& listing = m_current_listing_index->listing;
  76. listing.saved_arguments.empend(make<Listing::ExtraSavedArguments>(move(value)));
  77. return listing.saved_arguments.last()->template get_pointer<T>();
  78. }
  79. template<auto member, typename... Args>
  80. void append_to_listing(Args&&... args)
  81. {
  82. VERIFY(m_current_listing_index.has_value());
  83. m_current_listing_index->listing.entries.empend(member, Listing::ArgumentsFor<member> { forward<Args>(args)... });
  84. }
  85. [[nodiscard]] bool should_append_to_listing() const { return m_current_listing_index.has_value(); }
  86. [[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; }
  87. GLenum m_current_draw_mode;
  88. GLenum m_current_matrix_mode;
  89. FloatMatrix4x4 m_projection_matrix;
  90. FloatMatrix4x4 m_model_view_matrix;
  91. FloatMatrix4x4 m_current_matrix;
  92. Vector<FloatMatrix4x4> m_projection_matrix_stack;
  93. Vector<FloatMatrix4x4> m_model_view_matrix_stack;
  94. FloatVector4 m_clear_color = { 0.0f, 0.0f, 0.0f, 0.0f };
  95. double m_clear_depth = { 1.0 };
  96. FloatVector4 m_current_vertex_color = { 1.0f, 1.0f, 1.0f, 1.0f };
  97. Vector<GLVertex, 96> vertex_list;
  98. Vector<GLTriangle, 32> triangle_list;
  99. Vector<GLTriangle, 32> processed_triangles;
  100. GLenum m_error = GL_NO_ERROR;
  101. bool m_in_draw_state = false;
  102. bool m_depth_test_enabled = false;
  103. bool m_cull_faces = false;
  104. GLenum m_front_face = GL_CCW;
  105. GLenum m_culled_sides = GL_BACK;
  106. bool m_blend_enabled = false;
  107. GLenum m_blend_source_factor = GL_ONE;
  108. GLenum m_blend_destination_factor = GL_ZERO;
  109. bool m_alpha_test_enabled = false;
  110. GLenum m_alpha_test_func = GL_ALWAYS;
  111. GLclampf m_alpha_test_ref_value = 0;
  112. GLenum m_current_read_buffer = GL_BACK;
  113. GLuint m_bound_texture_2d = 0;
  114. NonnullRefPtr<Gfx::Bitmap> m_frontbuffer;
  115. Clipper m_clipper;
  116. // Texture objects
  117. TextureNameAllocator m_name_allocator;
  118. HashMap<GLuint, RefPtr<Texture>> m_allocated_textures;
  119. Array<TextureUnit, 32> m_texture_units;
  120. TextureUnit* m_active_texture_unit { &m_texture_units[0] };
  121. SoftwareRasterizer m_rasterizer;
  122. struct Listing {
  123. template<typename F>
  124. struct TupleTypeForArgumentListOf_;
  125. template<typename Ret, typename C, typename... Args>
  126. struct TupleTypeForArgumentListOf_<Ret (C::*)(Args...)> {
  127. using Type = Tuple<Args...>;
  128. };
  129. template<typename F>
  130. using TupleTypeForArgumentListOf = typename TupleTypeForArgumentListOf_<F>::Type;
  131. template<auto member>
  132. using ArgumentsFor = TupleTypeForArgumentListOf<decltype(member)>;
  133. template<typename... Fns>
  134. struct FunctionAndArgs {
  135. Variant<Fns...> function;
  136. Variant<TupleTypeForArgumentListOf<Fns>...> arguments;
  137. };
  138. using FunctionsAndArgs = FunctionAndArgs<
  139. decltype(&SoftwareGLContext::gl_begin),
  140. decltype(&SoftwareGLContext::gl_clear),
  141. decltype(&SoftwareGLContext::gl_clear_color),
  142. decltype(&SoftwareGLContext::gl_clear_depth),
  143. decltype(&SoftwareGLContext::gl_color),
  144. decltype(&SoftwareGLContext::gl_end),
  145. decltype(&SoftwareGLContext::gl_frustum),
  146. decltype(&SoftwareGLContext::gl_load_identity),
  147. decltype(&SoftwareGLContext::gl_load_matrix),
  148. decltype(&SoftwareGLContext::gl_matrix_mode),
  149. decltype(&SoftwareGLContext::gl_ortho),
  150. decltype(&SoftwareGLContext::gl_push_matrix),
  151. decltype(&SoftwareGLContext::gl_pop_matrix),
  152. decltype(&SoftwareGLContext::gl_rotate),
  153. decltype(&SoftwareGLContext::gl_scale),
  154. decltype(&SoftwareGLContext::gl_translate),
  155. decltype(&SoftwareGLContext::gl_vertex),
  156. decltype(&SoftwareGLContext::gl_viewport),
  157. decltype(&SoftwareGLContext::gl_enable),
  158. decltype(&SoftwareGLContext::gl_disable),
  159. decltype(&SoftwareGLContext::gl_front_face),
  160. decltype(&SoftwareGLContext::gl_cull_face),
  161. decltype(&SoftwareGLContext::gl_call_list),
  162. decltype(&SoftwareGLContext::gl_blend_func),
  163. decltype(&SoftwareGLContext::gl_shade_model),
  164. decltype(&SoftwareGLContext::gl_alpha_func),
  165. decltype(&SoftwareGLContext::gl_hint),
  166. decltype(&SoftwareGLContext::gl_read_buffer)>;
  167. using ExtraSavedArguments = Variant<
  168. FloatMatrix4x4>;
  169. Vector<NonnullOwnPtr<ExtraSavedArguments>> saved_arguments;
  170. Vector<FunctionsAndArgs> entries;
  171. };
  172. static constexpr size_t max_allowed_gl_call_depth { 128 };
  173. size_t m_gl_call_depth { 0 };
  174. Vector<Listing> m_listings;
  175. struct CurrentListing {
  176. Listing listing;
  177. size_t index { 0 };
  178. GLenum mode { GL_COMPILE };
  179. };
  180. Optional<CurrentListing> m_current_listing_index;
  181. };
  182. }