Device.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/NonnullRefPtr.h>
  9. #include <AK/OwnPtr.h>
  10. #include <LibGL/GL/gl.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. #include <LibSoftGPU/Image.h>
  20. #include <LibSoftGPU/ImageFormat.h>
  21. #include <LibSoftGPU/Sampler.h>
  22. #include <LibSoftGPU/Triangle.h>
  23. #include <LibSoftGPU/Vertex.h>
  24. namespace SoftGPU {
  25. struct RasterizerOptions {
  26. bool shade_smooth { true };
  27. bool enable_depth_test { false };
  28. bool enable_depth_write { true };
  29. bool enable_alpha_test { false };
  30. GLenum alpha_test_func { GL_ALWAYS };
  31. float alpha_test_ref_value { 0 };
  32. bool enable_blending { false };
  33. GLenum blend_source_factor { GL_ONE };
  34. GLenum blend_destination_factor { GL_ONE };
  35. u32 color_mask { 0xffffffff };
  36. float depth_min { 0 };
  37. float depth_max { 1 };
  38. GLenum depth_func { GL_LESS };
  39. GLenum polygon_mode { GL_FILL };
  40. FloatVector4 fog_color {
  41. 0.0f,
  42. 0.0f,
  43. 0.0f,
  44. 0.0f,
  45. };
  46. GLfloat fog_density { 1.0f };
  47. GLenum fog_mode { GL_EXP };
  48. GLboolean fog_enabled { false };
  49. GLfloat fog_start { 0.0f };
  50. GLfloat fog_end { 1.0f };
  51. bool scissor_enabled { false };
  52. Gfx::IntRect scissor_box;
  53. GLenum draw_buffer { GL_BACK };
  54. GLfloat depth_offset_factor { 0 };
  55. GLfloat depth_offset_constant { 0 };
  56. bool enable_culling { false };
  57. GLenum front_face { GL_CCW };
  58. GLenum culled_sides { GL_BACK };
  59. };
  60. inline static constexpr size_t const num_samplers = 32;
  61. class Device final {
  62. public:
  63. Device(const Gfx::IntSize& min_size);
  64. void draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<Vertex> const& vertices, Vector<size_t> const& enabled_texture_units);
  65. void resize(const Gfx::IntSize& min_size);
  66. void clear_color(const FloatVector4&);
  67. void clear_depth(float);
  68. void blit(Gfx::Bitmap const&, int x, int y);
  69. void blit_to(Gfx::Bitmap&);
  70. void wait_for_all_threads() const;
  71. void set_options(const RasterizerOptions&);
  72. RasterizerOptions options() const { return m_options; }
  73. Gfx::RGBA32 get_backbuffer_pixel(int x, int y);
  74. float get_depthbuffer_value(int x, int y);
  75. NonnullRefPtr<Image> create_image(ImageFormat, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers);
  76. void set_sampler_config(unsigned, SamplerConfig const&);
  77. private:
  78. void submit_triangle(Triangle const& triangle, Vector<size_t> const& enabled_texture_units);
  79. private:
  80. RefPtr<Gfx::Bitmap> m_render_target;
  81. OwnPtr<DepthBuffer> m_depth_buffer;
  82. RasterizerOptions m_options;
  83. Clipper m_clipper;
  84. Vector<Triangle> m_triangle_list;
  85. Vector<Triangle> m_processed_triangles;
  86. Vector<Vertex> m_clipped_vertices;
  87. Sampler m_samplers[num_samplers];
  88. };
  89. }