Device.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <LibGfx/Bitmap.h>
  11. #include <LibGfx/Matrix4x4.h>
  12. #include <LibGfx/Rect.h>
  13. #include <LibGfx/Vector4.h>
  14. #include <LibSoftGPU/Clipper.h>
  15. #include <LibSoftGPU/DepthBuffer.h>
  16. #include <LibSoftGPU/Image.h>
  17. #include <LibSoftGPU/ImageFormat.h>
  18. #include <LibSoftGPU/Sampler.h>
  19. #include <LibSoftGPU/Triangle.h>
  20. #include <LibSoftGPU/Vertex.h>
  21. namespace SoftGPU {
  22. enum class AlphaTestFunction {
  23. Never,
  24. Always,
  25. Less,
  26. LessOrEqual,
  27. Equal,
  28. NotEqual,
  29. GreaterOrEqual,
  30. Greater,
  31. };
  32. enum class BlendFactor {
  33. Zero,
  34. One,
  35. SrcAlpha,
  36. OneMinusSrcAlpha,
  37. SrcColor,
  38. OneMinusSrcColor,
  39. DstAlpha,
  40. OneMinusDstAlpha,
  41. DstColor,
  42. OneMinusDstColor,
  43. SrcAlphaSaturate,
  44. };
  45. enum class DepthTestFunction {
  46. Never,
  47. Always,
  48. Less,
  49. LessOrEqual,
  50. Equal,
  51. NotEqual,
  52. GreaterOrEqual,
  53. Greater,
  54. };
  55. enum FogMode {
  56. Linear,
  57. Exp,
  58. Exp2
  59. };
  60. enum class PolygonMode {
  61. Point,
  62. Line,
  63. Fill,
  64. };
  65. enum class WindingOrder {
  66. Clockwise,
  67. CounterClockwise,
  68. };
  69. enum class PrimitiveType {
  70. Triangles,
  71. TriangleStrip,
  72. TriangleFan,
  73. Quads,
  74. };
  75. struct RasterizerOptions {
  76. bool shade_smooth { true };
  77. bool enable_depth_test { false };
  78. bool enable_depth_write { true };
  79. bool enable_alpha_test { false };
  80. AlphaTestFunction alpha_test_func { AlphaTestFunction::Always };
  81. float alpha_test_ref_value { 0 };
  82. bool enable_blending { false };
  83. BlendFactor blend_source_factor { BlendFactor::One };
  84. BlendFactor blend_destination_factor { BlendFactor::One };
  85. u32 color_mask { 0xffffffff };
  86. float depth_min { 0 };
  87. float depth_max { 1 };
  88. DepthTestFunction depth_func { DepthTestFunction::Less };
  89. PolygonMode polygon_mode { PolygonMode::Fill };
  90. FloatVector4 fog_color { 0.0f, 0.0f, 0.0f, 0.0f };
  91. float fog_density { 1.0f };
  92. FogMode fog_mode { FogMode::Exp };
  93. bool fog_enabled { false };
  94. float fog_start { 0.0f };
  95. float fog_end { 1.0f };
  96. bool scissor_enabled { false };
  97. Gfx::IntRect scissor_box;
  98. bool enable_color_write { true };
  99. float depth_offset_factor { 0 };
  100. float depth_offset_constant { 0 };
  101. bool enable_culling { false };
  102. WindingOrder front_face { WindingOrder::CounterClockwise };
  103. bool cull_back { true };
  104. bool cull_front { false };
  105. };
  106. inline static constexpr size_t const num_samplers = 32;
  107. class Device final {
  108. public:
  109. Device(const Gfx::IntSize& min_size);
  110. void draw_primitives(PrimitiveType, FloatMatrix4x4 const& transform, FloatMatrix4x4 const& texture_matrix, Vector<Vertex> const& vertices, Vector<size_t> const& enabled_texture_units);
  111. void resize(const Gfx::IntSize& min_size);
  112. void clear_color(const FloatVector4&);
  113. void clear_depth(float);
  114. void blit(Gfx::Bitmap const&, int x, int y);
  115. void blit_to(Gfx::Bitmap&);
  116. void wait_for_all_threads() const;
  117. void set_options(const RasterizerOptions&);
  118. RasterizerOptions options() const { return m_options; }
  119. Gfx::RGBA32 get_backbuffer_pixel(int x, int y);
  120. float get_depthbuffer_value(int x, int y);
  121. NonnullRefPtr<Image> create_image(ImageFormat, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers);
  122. void set_sampler_config(unsigned, SamplerConfig const&);
  123. private:
  124. void submit_triangle(Triangle const& triangle, Vector<size_t> const& enabled_texture_units);
  125. private:
  126. RefPtr<Gfx::Bitmap> m_render_target;
  127. OwnPtr<DepthBuffer> m_depth_buffer;
  128. RasterizerOptions m_options;
  129. Clipper m_clipper;
  130. Vector<Triangle> m_triangle_list;
  131. Vector<Triangle> m_processed_triangles;
  132. Vector<Vertex> m_clipped_vertices;
  133. Sampler m_samplers[num_samplers];
  134. };
  135. }