Device.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/Matrix3x3.h>
  12. #include <LibGfx/Matrix4x4.h>
  13. #include <LibGfx/Rect.h>
  14. #include <LibGfx/Vector4.h>
  15. #include <LibSoftGPU/Clipper.h>
  16. #include <LibSoftGPU/Config.h>
  17. #include <LibSoftGPU/DepthBuffer.h>
  18. #include <LibSoftGPU/DeviceInfo.h>
  19. #include <LibSoftGPU/Enums.h>
  20. #include <LibSoftGPU/Image.h>
  21. #include <LibSoftGPU/ImageFormat.h>
  22. #include <LibSoftGPU/Sampler.h>
  23. #include <LibSoftGPU/Triangle.h>
  24. #include <LibSoftGPU/Vertex.h>
  25. namespace SoftGPU {
  26. struct TexCoordGenerationConfig {
  27. TexCoordGenerationMode mode { TexCoordGenerationMode::EyeLinear };
  28. FloatVector4 coefficients {};
  29. };
  30. struct RasterizerOptions {
  31. bool shade_smooth { true };
  32. bool enable_depth_test { false };
  33. bool enable_depth_write { true };
  34. bool enable_alpha_test { false };
  35. AlphaTestFunction alpha_test_func { AlphaTestFunction::Always };
  36. float alpha_test_ref_value { 0 };
  37. bool enable_blending { false };
  38. BlendFactor blend_source_factor { BlendFactor::One };
  39. BlendFactor blend_destination_factor { BlendFactor::One };
  40. u32 color_mask { 0xffffffff };
  41. float depth_min { 0.f };
  42. float depth_max { 1.f };
  43. DepthTestFunction depth_func { DepthTestFunction::Less };
  44. PolygonMode polygon_mode { PolygonMode::Fill };
  45. FloatVector4 fog_color { 0.0f, 0.0f, 0.0f, 0.0f };
  46. float fog_density { 1.0f };
  47. FogMode fog_mode { FogMode::Exp };
  48. bool fog_enabled { false };
  49. float fog_start { 0.0f };
  50. float fog_end { 1.0f };
  51. bool scissor_enabled { false };
  52. bool normalization_enabled { false };
  53. Gfx::IntRect scissor_box;
  54. bool enable_color_write { true };
  55. float depth_offset_factor { 0 };
  56. float depth_offset_constant { 0 };
  57. bool enable_culling { false };
  58. WindingOrder front_face { WindingOrder::CounterClockwise };
  59. bool cull_back { true };
  60. bool cull_front { false };
  61. u8 texcoord_generation_enabled_coordinates { TexCoordGenerationCoordinate::None };
  62. Array<TexCoordGenerationConfig, 4> texcoord_generation_config {};
  63. };
  64. class Device final {
  65. public:
  66. Device(const Gfx::IntSize& min_size);
  67. DeviceInfo info() const;
  68. void draw_primitives(PrimitiveType, FloatMatrix4x4 const& model_view_transform, FloatMatrix3x3 const& normal_transform, FloatMatrix4x4 const& projection_transform, FloatMatrix4x4 const& texture_transform, Vector<Vertex> const& vertices, Vector<size_t> const& enabled_texture_units);
  69. void resize(const Gfx::IntSize& min_size);
  70. void clear_color(const FloatVector4&);
  71. void clear_depth(float);
  72. void blit(Gfx::Bitmap const&, int x, int y);
  73. void blit_to(Gfx::Bitmap&);
  74. void wait_for_all_threads() const;
  75. void set_options(const RasterizerOptions&);
  76. RasterizerOptions options() const { return m_options; }
  77. Gfx::RGBA32 get_backbuffer_pixel(int x, int y);
  78. float get_depthbuffer_value(int x, int y);
  79. NonnullRefPtr<Image> create_image(ImageFormat, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers);
  80. void set_sampler_config(unsigned, SamplerConfig const&);
  81. private:
  82. void submit_triangle(Triangle const& triangle, Vector<size_t> const& enabled_texture_units);
  83. void draw_statistics_overlay(Gfx::Bitmap&);
  84. private:
  85. RefPtr<Gfx::Bitmap> m_render_target;
  86. OwnPtr<DepthBuffer> m_depth_buffer;
  87. RasterizerOptions m_options;
  88. Clipper m_clipper;
  89. Vector<Triangle> m_triangle_list;
  90. Vector<Triangle> m_processed_triangles;
  91. Vector<Vertex> m_clipped_vertices;
  92. Array<Sampler, NUM_SAMPLERS> m_samplers;
  93. };
  94. }