Device.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/Array.h>
  8. #include <AK/OwnPtr.h>
  9. #include <LibGL/GL/gl.h>
  10. #include <LibGL/Tex/Texture2D.h>
  11. #include <LibGL/Tex/TextureUnit.h>
  12. #include <LibGfx/Bitmap.h>
  13. #include <LibGfx/Matrix4x4.h>
  14. #include <LibGfx/Rect.h>
  15. #include <LibGfx/Vector4.h>
  16. #include <LibSoftGPU/Clipper.h>
  17. #include <LibSoftGPU/DepthBuffer.h>
  18. #include <LibSoftGPU/Triangle.h>
  19. #include <LibSoftGPU/Vertex.h>
  20. namespace SoftGPU {
  21. struct RasterizerOptions {
  22. bool shade_smooth { true };
  23. bool enable_depth_test { false };
  24. bool enable_depth_write { true };
  25. bool enable_alpha_test { false };
  26. GLenum alpha_test_func { GL_ALWAYS };
  27. float alpha_test_ref_value { 0 };
  28. bool enable_blending { false };
  29. GLenum blend_source_factor { GL_ONE };
  30. GLenum blend_destination_factor { GL_ONE };
  31. u32 color_mask { 0xffffffff };
  32. float depth_min { 0 };
  33. float depth_max { 1 };
  34. GLenum depth_func { GL_LESS };
  35. GLenum polygon_mode { GL_FILL };
  36. FloatVector4 fog_color {
  37. 0.0f,
  38. 0.0f,
  39. 0.0f,
  40. 0.0f,
  41. };
  42. GLfloat fog_density { 1.0f };
  43. GLenum fog_mode { GL_EXP };
  44. GLboolean fog_enabled { false };
  45. GLfloat fog_start { 0.0f };
  46. GLfloat fog_end { 1.0f };
  47. bool scissor_enabled { false };
  48. Gfx::IntRect scissor_box;
  49. GLenum draw_buffer { GL_BACK };
  50. GLfloat depth_offset_factor { 0 };
  51. GLfloat depth_offset_constant { 0 };
  52. bool enable_culling { false };
  53. GLenum front_face { GL_CCW };
  54. GLenum culled_sides { GL_BACK };
  55. };
  56. class Device final {
  57. public:
  58. Device(const Gfx::IntSize& min_size);
  59. void draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<Vertex> const& vertices, GL::TextureUnit::BoundList const& bound_texture_units);
  60. void resize(const Gfx::IntSize& min_size);
  61. void clear_color(const FloatVector4&);
  62. void clear_depth(float);
  63. void blit(Gfx::Bitmap const&, int x, int y);
  64. void blit_to(Gfx::Bitmap&);
  65. void wait_for_all_threads() const;
  66. void set_options(const RasterizerOptions&);
  67. RasterizerOptions options() const { return m_options; }
  68. Gfx::RGBA32 get_backbuffer_pixel(int x, int y);
  69. float get_depthbuffer_value(int x, int y);
  70. private:
  71. void submit_triangle(Triangle const& triangle, GL::TextureUnit::BoundList const& bound_texture_units);
  72. private:
  73. RefPtr<Gfx::Bitmap> m_render_target;
  74. OwnPtr<DepthBuffer> m_depth_buffer;
  75. RasterizerOptions m_options;
  76. Clipper m_clipper;
  77. Vector<Triangle> m_triangle_list;
  78. Vector<Triangle> m_processed_triangles;
  79. Vector<Vertex> m_clipped_vertices;
  80. };
  81. }