SoftwareRasterizer.h 2.7 KB

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