Device.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 DepthTestFunction {
  49. Never,
  50. Always,
  51. Less,
  52. LessOrEqual,
  53. Equal,
  54. NotEqual,
  55. GreaterOrEqual,
  56. Greater,
  57. };
  58. enum FogMode {
  59. Linear,
  60. Exp,
  61. Exp2
  62. };
  63. enum class PolygonMode {
  64. Point,
  65. Line,
  66. Fill,
  67. };
  68. enum class WindingOrder {
  69. Clockwise,
  70. CounterClockwise,
  71. };
  72. struct RasterizerOptions {
  73. bool shade_smooth { true };
  74. bool enable_depth_test { false };
  75. bool enable_depth_write { true };
  76. bool enable_alpha_test { false };
  77. AlphaTestFunction alpha_test_func { AlphaTestFunction::Always };
  78. float alpha_test_ref_value { 0 };
  79. bool enable_blending { false };
  80. BlendFactor blend_source_factor { BlendFactor::One };
  81. BlendFactor blend_destination_factor { BlendFactor::One };
  82. u32 color_mask { 0xffffffff };
  83. float depth_min { 0 };
  84. float depth_max { 1 };
  85. DepthTestFunction depth_func { DepthTestFunction::Less };
  86. PolygonMode polygon_mode { PolygonMode::Fill };
  87. FloatVector4 fog_color { 0.0f, 0.0f, 0.0f, 0.0f };
  88. float fog_density { 1.0f };
  89. FogMode fog_mode { FogMode::Exp };
  90. bool fog_enabled { false };
  91. float fog_start { 0.0f };
  92. float fog_end { 1.0f };
  93. bool scissor_enabled { false };
  94. Gfx::IntRect scissor_box;
  95. bool enable_color_write { true };
  96. float depth_offset_factor { 0 };
  97. float depth_offset_constant { 0 };
  98. bool enable_culling { false };
  99. WindingOrder front_face { WindingOrder::CounterClockwise };
  100. bool cull_back { true };
  101. bool cull_front { false };
  102. };
  103. inline static constexpr size_t const num_samplers = 32;
  104. class Device final {
  105. public:
  106. Device(const Gfx::IntSize& min_size);
  107. void draw_primitives(GLenum primitive_type, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<Vertex> const& vertices, Vector<size_t> const& enabled_texture_units);
  108. void resize(const Gfx::IntSize& min_size);
  109. void clear_color(const FloatVector4&);
  110. void clear_depth(float);
  111. void blit(Gfx::Bitmap const&, int x, int y);
  112. void blit_to(Gfx::Bitmap&);
  113. void wait_for_all_threads() const;
  114. void set_options(const RasterizerOptions&);
  115. RasterizerOptions options() const { return m_options; }
  116. Gfx::RGBA32 get_backbuffer_pixel(int x, int y);
  117. float get_depthbuffer_value(int x, int y);
  118. NonnullRefPtr<Image> create_image(ImageFormat, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers);
  119. void set_sampler_config(unsigned, SamplerConfig const&);
  120. private:
  121. void submit_triangle(Triangle const& triangle, Vector<size_t> const& enabled_texture_units);
  122. private:
  123. RefPtr<Gfx::Bitmap> m_render_target;
  124. OwnPtr<DepthBuffer> m_depth_buffer;
  125. RasterizerOptions m_options;
  126. Clipper m_clipper;
  127. Vector<Triangle> m_triangle_list;
  128. Vector<Triangle> m_processed_triangles;
  129. Vector<Vertex> m_clipped_vertices;
  130. Sampler m_samplers[num_samplers];
  131. };
  132. }