2021-04-24 01:57:01 +02:00
/*
2021-05-29 12:38:28 +02:00
* Copyright ( c ) 2021 , Stephan Unverwerth < s . unverwerth @ serenityos . org >
2021-04-24 01:57:01 +02:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# pragma once
2021-05-30 21:39:31 +10:00
# include <AK/Array.h>
2021-12-19 00:02:32 +01:00
# include <AK/NonnullRefPtr.h>
2021-05-08 23:17:13 +02:00
# include <AK/OwnPtr.h>
2021-04-24 01:57:01 +02:00
# include <LibGfx/Bitmap.h>
2021-12-30 00:27:17 +01:00
# include <LibGfx/Matrix3x3.h>
2021-12-16 21:26:15 +01:00
# include <LibGfx/Matrix4x4.h>
2021-11-27 19:00:16 +01:00
# include <LibGfx/Rect.h>
2021-04-24 01:57:01 +02:00
# include <LibGfx/Vector4.h>
2022-01-06 18:47:35 +01:00
# include <LibSoftGPU/AlphaBlendFactors.h>
2021-12-16 21:26:15 +01:00
# include <LibSoftGPU/Clipper.h>
2021-12-29 21:59:13 +01:00
# include <LibSoftGPU/Config.h>
2021-12-16 20:32:38 +01:00
# include <LibSoftGPU/DepthBuffer.h>
2021-12-23 02:38:20 +01:00
# include <LibSoftGPU/DeviceInfo.h>
2021-12-23 02:07:11 +01:00
# include <LibSoftGPU/Enums.h>
2021-12-19 00:02:32 +01:00
# include <LibSoftGPU/Image.h>
# include <LibSoftGPU/ImageFormat.h>
2022-01-08 01:41:46 +11:00
# include <LibSoftGPU/Light/Light.h>
2022-01-08 17:46:58 +11:00
# include <LibSoftGPU/Light/Material.h>
2021-12-20 16:14:40 +01:00
# include <LibSoftGPU/Sampler.h>
2021-12-16 22:43:39 +01:00
# include <LibSoftGPU/Triangle.h>
# include <LibSoftGPU/Vertex.h>
2021-04-24 01:57:01 +02:00
2021-12-16 20:32:38 +01:00
namespace SoftGPU {
2021-04-24 01:57:01 +02:00
2021-12-30 00:56:41 +01:00
struct TexCoordGenerationConfig {
TexCoordGenerationMode mode { TexCoordGenerationMode : : EyeLinear } ;
FloatVector4 coefficients { } ;
} ;
2021-04-24 01:57:01 +02:00
struct RasterizerOptions {
2021-05-15 22:55:40 +02:00
bool shade_smooth { true } ;
2021-05-06 23:17:35 +02:00
bool enable_depth_test { false } ;
2021-08-13 01:06:26 +02:00
bool enable_depth_write { true } ;
2021-05-16 16:43:09 +02:00
bool enable_alpha_test { false } ;
2021-12-22 22:59:07 +01:00
AlphaTestFunction alpha_test_func { AlphaTestFunction : : Always } ;
2021-05-16 16:43:09 +02:00
float alpha_test_ref_value { 0 } ;
2021-05-15 22:55:40 +02:00
bool enable_blending { false } ;
2021-12-22 23:11:35 +01:00
BlendFactor blend_source_factor { BlendFactor : : One } ;
BlendFactor blend_destination_factor { BlendFactor : : One } ;
2021-08-13 22:03:39 +10:00
u32 color_mask { 0xffffffff } ;
2021-12-30 00:42:45 +01:00
float depth_min { 0.f } ;
float depth_max { 1.f } ;
2021-12-22 23:38:13 +01:00
DepthTestFunction depth_func { DepthTestFunction : : Less } ;
2021-12-22 23:51:28 +01:00
PolygonMode polygon_mode { PolygonMode : : Fill } ;
2021-12-22 22:50:33 +01:00
FloatVector4 fog_color { 0.0f , 0.0f , 0.0f , 0.0f } ;
float fog_density { 1.0f } ;
2021-12-22 23:44:29 +01:00
FogMode fog_mode { FogMode : : Exp } ;
2021-12-22 22:50:33 +01:00
bool fog_enabled { false } ;
float fog_start { 0.0f } ;
float fog_end { 1.0f } ;
2021-11-27 19:00:16 +01:00
bool scissor_enabled { false } ;
2021-12-30 00:27:17 +01:00
bool normalization_enabled { false } ;
2021-11-28 22:27:35 +01:00
Gfx : : IntRect scissor_box ;
2021-12-22 23:31:44 +01:00
bool enable_color_write { true } ;
2021-12-22 22:50:33 +01:00
float depth_offset_factor { 0 } ;
float depth_offset_constant { 0 } ;
2021-12-16 21:10:53 +01:00
bool enable_culling { false } ;
2021-12-22 23:16:58 +01:00
WindingOrder front_face { WindingOrder : : CounterClockwise } ;
2021-12-22 23:23:15 +01:00
bool cull_back { true } ;
bool cull_front { false } ;
2021-12-30 00:56:41 +01:00
u8 texcoord_generation_enabled_coordinates { TexCoordGenerationCoordinate : : None } ;
Array < TexCoordGenerationConfig , 4 > texcoord_generation_config { } ;
2022-01-08 14:43:03 +01:00
Gfx : : IntRect viewport ;
2022-01-08 18:52:12 +11:00
bool lighting_enabled { false } ;
2022-01-13 03:03:35 +01:00
bool color_material_enabled { false } ;
ColorMaterialFace color_material_face { ColorMaterialFace : : FrontAndBack } ;
ColorMaterialMode color_material_mode { ColorMaterialMode : : AmbientAndDiffuse } ;
2021-04-24 01:57:01 +02:00
} ;
2022-01-09 00:40:39 +11:00
struct LightModelParameters {
FloatVector4 scene_ambient_color { 0.2f , 0.2f , 0.2f , 1.0f } ;
bool viewer_at_infinity { false } ;
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.
bool two_sided_lighting { false } ;
} ;
2022-01-06 17:52:07 +01:00
struct PixelQuad ;
2021-12-18 14:07:47 +01:00
class Device final {
2021-04-24 01:57:01 +02:00
public :
2021-12-18 14:07:47 +01:00
Device ( const Gfx : : IntSize & min_size ) ;
2021-04-24 01:57:01 +02:00
2021-12-23 02:38:20 +01:00
DeviceInfo info ( ) const ;
2021-12-30 00:47:53 +01:00
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 ) ;
2021-04-24 01:57:01 +02:00
void resize ( const Gfx : : IntSize & min_size ) ;
void clear_color ( const FloatVector4 & ) ;
void clear_depth ( float ) ;
2021-12-01 17:19:22 +01:00
void blit ( Gfx : : Bitmap const & , int x , int y ) ;
2021-04-24 01:57:01 +02:00
void blit_to ( Gfx : : Bitmap & ) ;
void wait_for_all_threads ( ) const ;
void set_options ( const RasterizerOptions & ) ;
2022-01-09 00:40:39 +11:00
void set_light_model_params ( const LightModelParameters & ) ;
2021-04-24 01:57:01 +02:00
RasterizerOptions options ( ) const { return m_options ; }
2022-01-09 00:40:39 +11:00
LightModelParameters light_model ( ) const { return m_lighting_model ; }
2021-05-24 17:52:24 +02:00
Gfx : : RGBA32 get_backbuffer_pixel ( int x , int y ) ;
float get_depthbuffer_value ( int x , int y ) ;
2021-04-24 01:57:01 +02:00
2021-12-19 00:02:32 +01:00
NonnullRefPtr < Image > create_image ( ImageFormat , unsigned width , unsigned height , unsigned depth , unsigned levels , unsigned layers ) ;
2021-12-20 16:14:40 +01:00
void set_sampler_config ( unsigned , SamplerConfig const & ) ;
2022-01-08 01:41:46 +11:00
void set_light_state ( unsigned , Light const & ) ;
2022-01-08 17:46:58 +11:00
void set_material_state ( unsigned , Material const & ) ;
2021-12-20 16:14:40 +01:00
2021-12-16 21:26:15 +01:00
private :
2021-12-30 01:04:55 +01:00
void draw_statistics_overlay ( Gfx : : Bitmap & ) ;
2021-12-16 21:26:15 +01:00
2022-01-06 17:52:07 +01:00
void rasterize_triangle ( const Triangle & triangle ) ;
2022-01-06 18:47:35 +01:00
void setup_blend_factors ( ) ;
2022-01-06 17:52:07 +01:00
void shade_fragments ( PixelQuad & ) ;
2022-01-06 21:28:16 +01:00
bool test_alpha ( PixelQuad & ) ;
2022-01-06 17:52:07 +01:00
2021-04-24 01:57:01 +02:00
private :
RefPtr < Gfx : : Bitmap > m_render_target ;
2021-05-08 23:17:13 +02:00
OwnPtr < DepthBuffer > m_depth_buffer ;
2021-04-24 01:57:01 +02:00
RasterizerOptions m_options ;
2022-01-09 00:40:39 +11:00
LightModelParameters m_lighting_model ;
2021-12-16 21:26:15 +01:00
Clipper m_clipper ;
2021-12-16 22:43:39 +01:00
Vector < Triangle > m_triangle_list ;
Vector < Triangle > m_processed_triangles ;
Vector < Vertex > m_clipped_vertices ;
2021-12-29 21:59:13 +01:00
Array < Sampler , NUM_SAMPLERS > m_samplers ;
2022-01-06 17:52:07 +01:00
Vector < size_t > m_enabled_texture_units ;
2022-01-06 18:47:35 +01:00
AlphaBlendFactors m_alpha_blend_factors ;
2022-01-08 01:41:46 +11:00
Array < Light , NUM_LIGHTS > m_lights ;
2022-01-08 17:46:58 +11:00
Array < Material , 2u > m_materials ;
2021-04-24 01:57:01 +02:00
} ;
}