Device.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. enum class AlphaTestFunction {
  26. Never,
  27. Always,
  28. Less,
  29. LessOrEqual,
  30. Equal,
  31. NotEqual,
  32. GreaterOrEqual,
  33. Greater,
  34. };
  35. enum class BlendFactor {
  36. Zero,
  37. One,
  38. SrcAlpha,
  39. OneMinusSrcAlpha,
  40. SrcColor,
  41. OneMinusSrcColor,
  42. DstAlpha,
  43. OneMinusDstAlpha,
  44. DstColor,
  45. OneMinusDstColor,
  46. SrcAlphaSaturate,
  47. };
  48. enum class WindingOrder {
  49. Clockwise,
  50. CounterClockwise,
  51. };
  52. struct RasterizerOptions {
  53. bool shade_smooth { true };
  54. bool enable_depth_test { false };
  55. bool enable_depth_write { true };
  56. bool enable_alpha_test { false };
  57. AlphaTestFunction alpha_test_func { AlphaTestFunction::Always };
  58. float alpha_test_ref_value { 0 };
  59. bool enable_blending { false };
  60. BlendFactor blend_source_factor { BlendFactor::One };
  61. BlendFactor blend_destination_factor { BlendFactor::One };
  62. u32 color_mask { 0xffffffff };
  63. float depth_min { 0 };
  64. float depth_max { 1 };
  65. GLenum depth_func { GL_LESS };
  66. GLenum polygon_mode { GL_FILL };
  67. FloatVector4 fog_color { 0.0f, 0.0f, 0.0f, 0.0f };
  68. float fog_density { 1.0f };
  69. GLenum fog_mode { GL_EXP };
  70. bool fog_enabled { false };
  71. float fog_start { 0.0f };
  72. float fog_end { 1.0f };
  73. bool scissor_enabled { false };
  74. Gfx::IntRect scissor_box;
  75. GLenum draw_buffer { GL_BACK };
  76. float depth_offset_factor { 0 };
  77. float depth_offset_constant { 0 };
  78. bool enable_culling { false };
  79. WindingOrder front_face { WindingOrder::CounterClockwise };
  80. GLenum culled_sides { GL_BACK };
  81. };
  82. inline static constexpr size_t const num_samplers = 32;
  83. class Device final {
  84. public:
  85. Device(const Gfx::IntSize& min_size);
  86. void draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<Vertex> const& vertices, Vector<size_t> const& enabled_texture_units);
  87. void resize(const Gfx::IntSize& min_size);
  88. void clear_color(const FloatVector4&);
  89. void clear_depth(float);
  90. void blit(Gfx::Bitmap const&, int x, int y);
  91. void blit_to(Gfx::Bitmap&);
  92. void wait_for_all_threads() const;
  93. void set_options(const RasterizerOptions&);
  94. RasterizerOptions options() const { return m_options; }
  95. Gfx::RGBA32 get_backbuffer_pixel(int x, int y);
  96. float get_depthbuffer_value(int x, int y);
  97. NonnullRefPtr<Image> create_image(ImageFormat, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers);
  98. void set_sampler_config(unsigned, SamplerConfig const&);
  99. private:
  100. void submit_triangle(Triangle const& triangle, Vector<size_t> const& enabled_texture_units);
  101. private:
  102. RefPtr<Gfx::Bitmap> m_render_target;
  103. OwnPtr<DepthBuffer> m_depth_buffer;
  104. RasterizerOptions m_options;
  105. Clipper m_clipper;
  106. Vector<Triangle> m_triangle_list;
  107. Vector<Triangle> m_processed_triangles;
  108. Vector<Vertex> m_clipped_vertices;
  109. Sampler m_samplers[num_samplers];
  110. };
  111. }