Device.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  3. * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Array.h>
  9. #include <AK/NonnullRefPtr.h>
  10. #include <AK/RefPtr.h>
  11. #include <AK/Vector.h>
  12. #include <LibGfx/Bitmap.h>
  13. #include <LibGfx/Matrix3x3.h>
  14. #include <LibGfx/Matrix4x4.h>
  15. #include <LibGfx/Rect.h>
  16. #include <LibGfx/Vector4.h>
  17. #include <LibSoftGPU/AlphaBlendFactors.h>
  18. #include <LibSoftGPU/Buffer/FrameBuffer.h>
  19. #include <LibSoftGPU/Buffer/Typed2DBuffer.h>
  20. #include <LibSoftGPU/Clipper.h>
  21. #include <LibSoftGPU/Config.h>
  22. #include <LibSoftGPU/DeviceInfo.h>
  23. #include <LibSoftGPU/Enums.h>
  24. #include <LibSoftGPU/Image.h>
  25. #include <LibSoftGPU/ImageFormat.h>
  26. #include <LibSoftGPU/Light/Light.h>
  27. #include <LibSoftGPU/Light/Material.h>
  28. #include <LibSoftGPU/Sampler.h>
  29. #include <LibSoftGPU/Triangle.h>
  30. #include <LibSoftGPU/Vertex.h>
  31. namespace SoftGPU {
  32. struct TexCoordGenerationConfig {
  33. TexCoordGenerationMode mode { TexCoordGenerationMode::EyeLinear };
  34. FloatVector4 coefficients {};
  35. };
  36. struct RasterizerOptions {
  37. bool shade_smooth { true };
  38. bool enable_stencil_test { false };
  39. bool enable_depth_test { false };
  40. bool enable_depth_write { true };
  41. bool enable_alpha_test { false };
  42. AlphaTestFunction alpha_test_func { AlphaTestFunction::Always };
  43. float alpha_test_ref_value { 0 };
  44. bool enable_blending { false };
  45. BlendFactor blend_source_factor { BlendFactor::One };
  46. BlendFactor blend_destination_factor { BlendFactor::One };
  47. u32 color_mask { 0xffffffff };
  48. float depth_min { 0.f };
  49. float depth_max { 1.f };
  50. DepthTestFunction depth_func { DepthTestFunction::Less };
  51. PolygonMode polygon_mode { PolygonMode::Fill };
  52. FloatVector4 fog_color { 0.0f, 0.0f, 0.0f, 0.0f };
  53. float fog_density { 1.0f };
  54. FogMode fog_mode { FogMode::Exp };
  55. bool fog_enabled { false };
  56. float fog_start { 0.0f };
  57. float fog_end { 1.0f };
  58. bool scissor_enabled { false };
  59. bool normalization_enabled { false };
  60. Gfx::IntRect scissor_box;
  61. bool enable_color_write { true };
  62. float depth_offset_factor { 0 };
  63. float depth_offset_constant { 0 };
  64. bool depth_offset_enabled { false };
  65. bool enable_culling { false };
  66. WindingOrder front_face { WindingOrder::CounterClockwise };
  67. bool cull_back { true };
  68. bool cull_front { false };
  69. Array<u8, NUM_SAMPLERS> texcoord_generation_enabled_coordinates {};
  70. Array<Array<TexCoordGenerationConfig, 4>, NUM_SAMPLERS> texcoord_generation_config {};
  71. Gfx::IntRect viewport;
  72. bool lighting_enabled { false };
  73. bool color_material_enabled { false };
  74. ColorMaterialFace color_material_face { ColorMaterialFace::FrontAndBack };
  75. ColorMaterialMode color_material_mode { ColorMaterialMode::AmbientAndDiffuse };
  76. };
  77. struct LightModelParameters {
  78. FloatVector4 scene_ambient_color { 0.2f, 0.2f, 0.2f, 1.0f };
  79. bool viewer_at_infinity { false };
  80. unsigned int single_color { 0x81F9 }; // This is the value of `GL_SINGLE_COLOR`. Considering we definitely don't leak gl.h stuff into here, we fix it to the gl.h macro value.
  81. bool two_sided_lighting { false };
  82. };
  83. struct PixelQuad;
  84. struct RasterPosition {
  85. FloatVector4 window_coordinates { 0.0f, 0.0f, 0.0f, 1.0f };
  86. float eye_coordinate_distance { 0.0f };
  87. bool valid { true };
  88. FloatVector4 color_rgba { 1.0f, 1.0f, 1.0f, 1.0f };
  89. float color_index { 1.0f };
  90. FloatVector4 texture_coordinates { 0.0f, 0.0f, 0.0f, 1.0f };
  91. };
  92. struct StencilConfiguration {
  93. StencilTestFunction test_function;
  94. StencilType reference_value;
  95. StencilType test_mask;
  96. StencilOperation on_stencil_test_fail;
  97. StencilOperation on_depth_test_fail;
  98. StencilOperation on_pass;
  99. StencilType write_mask;
  100. };
  101. class Device final {
  102. public:
  103. Device(const Gfx::IntSize& min_size);
  104. DeviceInfo info() const;
  105. 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);
  106. void resize(Gfx::IntSize const& min_size);
  107. void clear_color(FloatVector4 const&);
  108. void clear_depth(DepthType);
  109. void clear_stencil(StencilType);
  110. void blit_color_buffer_to(Gfx::Bitmap& target);
  111. void blit_to_color_buffer_at_raster_position(Gfx::Bitmap const&);
  112. void blit_to_depth_buffer_at_raster_position(Vector<DepthType> const&, int, int);
  113. void set_options(const RasterizerOptions&);
  114. void set_light_model_params(const LightModelParameters&);
  115. RasterizerOptions options() const { return m_options; }
  116. LightModelParameters light_model() const { return m_lighting_model; }
  117. ColorType get_color_buffer_pixel(int x, int y);
  118. DepthType get_depthbuffer_value(int x, int y);
  119. NonnullRefPtr<Image> create_image(ImageFormat format, unsigned width, unsigned height, unsigned depth, unsigned levels, unsigned layers);
  120. void set_sampler_config(unsigned, SamplerConfig const&);
  121. void set_light_state(unsigned, Light const&);
  122. void set_material_state(Face, Material const&);
  123. void set_stencil_configuration(Face, StencilConfiguration const&);
  124. RasterPosition raster_position() const { return m_raster_position; }
  125. void set_raster_position(RasterPosition const& raster_position);
  126. void set_raster_position(FloatVector4 const& position, FloatMatrix4x4 const& model_view_transform, FloatMatrix4x4 const& projection_transform);
  127. private:
  128. void draw_statistics_overlay(Gfx::Bitmap&);
  129. Gfx::IntRect raster_rect_in_target_coordinates(Gfx::IntSize size);
  130. Gfx::IntRect window_coordinates_to_target_coordinates(Gfx::IntRect const&);
  131. void rasterize_triangle(const Triangle& triangle);
  132. void setup_blend_factors();
  133. void shade_fragments(PixelQuad&);
  134. bool test_alpha(PixelQuad&);
  135. RefPtr<FrameBuffer<ColorType, DepthType, StencilType>> m_frame_buffer {};
  136. RasterizerOptions m_options;
  137. LightModelParameters m_lighting_model;
  138. Clipper m_clipper;
  139. Vector<Triangle> m_triangle_list;
  140. Vector<Triangle> m_processed_triangles;
  141. Vector<Vertex> m_clipped_vertices;
  142. Array<Sampler, NUM_SAMPLERS> m_samplers;
  143. Vector<size_t> m_enabled_texture_units;
  144. AlphaBlendFactors m_alpha_blend_factors;
  145. Array<Light, NUM_LIGHTS> m_lights;
  146. Array<Material, 2u> m_materials;
  147. RasterPosition m_raster_position;
  148. Array<StencilConfiguration, 2u> m_stencil_configuration;
  149. };
  150. }