From bcc91b3db8cd50795c21860cd7797581199d5589 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 19 Aug 2026 22:34:34 +0200 Subject: refactor(engine): move default shader bindings system to main_render_pass mod --- engine/res/default_shader.slang | 270 ---------------------------------------- engine/res/main_3d_shader.slang | 270 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 270 insertions(+), 270 deletions(-) delete mode 100644 engine/res/default_shader.slang create mode 100644 engine/res/main_3d_shader.slang (limited to 'engine/res') diff --git a/engine/res/default_shader.slang b/engine/res/default_shader.slang deleted file mode 100644 index 7da164c..0000000 --- a/engine/res/default_shader.slang +++ /dev/null @@ -1,270 +0,0 @@ -#define MAX_LIGHT_CNT 64 - -struct Material -{ - float3 ambient; - float3 diffuse; - float3 specular; - float shininess; -}; - -struct LightPhong -{ - float3 diffuse; - float3 specular; -}; - -struct AttenuationProperties -{ - float constant; - float linear; - float quadratic; -}; - -struct PointLight -{ - LightPhong phong; - float3 position; - AttenuationProperties attenuation_props; -}; - -struct DirectionalLight -{ - LightPhong phong; - float3 direction; -}; - -struct CalculatedLight -{ - float3 diffuse; - float3 specular; -}; - -struct BlinnPhongLighting -{ - float3 view_pos; - Material material; - - DirectionalLight directional_lights[MAX_LIGHT_CNT]; - uint directional_light_cnt; - - PointLight point_lights[MAX_LIGHT_CNT]; - uint point_light_cnt; - - float4 evaluate(in VertexData vertex_data) - { - float3 ambient_light = this.calc_ambient_light(vertex_data.texture_coords); - - float3 directional_light_sum = this.calc_dir_light_sum(vertex_data); - float3 point_light_sum = this.calc_point_light_sum(vertex_data); - - return float4((ambient_light + directional_light_sum + point_light_sum), 1.0); - } - - float3 calc_dir_light_sum(in VertexData vertex_data) - { - float3 directional_light_sum = float3(0.0, 0.0, 0.0); - - for (uint index = 0; index < this.directional_light_cnt; index++) { - CalculatedLight calculated_dir_light; - - this.calc_light( - // Negated since we want the light to point from the light - // direction - normalize(-this.directional_lights[index].direction), - this.directional_lights[index].phong, - vertex_data, - calculated_dir_light - ); - - directional_light_sum += - calculated_dir_light.diffuse + calculated_dir_light.specular; - } - - return directional_light_sum; - } - - float3 calc_point_light_sum(in VertexData vertex_data) - { - float3 point_light_sum = float3(0.0, 0.0, 0.0); - - for (uint index = 0; index < this.point_light_cnt; index++) { - float3 light_direction = normalize( - this.point_lights[index].position - vertex_data.world_space_pos - ); - - CalculatedLight calculated_point_light; - - this.calc_light( - light_direction, - this.point_lights[index].phong, - vertex_data, - calculated_point_light - ); - - float attenuation = this.calc_attenuation( - this.point_lights[index], vertex_data.world_space_pos - ); - - calculated_point_light.diffuse *= attenuation; - calculated_point_light.specular *= attenuation; - - point_light_sum += - calculated_point_light.diffuse + calculated_point_light.specular; - } - - return point_light_sum; - } - - void calc_light( - in float3 light_direction, - in LightPhong light_phong, - in VertexData vertex_data, - out CalculatedLight calculated_light - ) - { - float3 norm = normalize(vertex_data.world_space_normal); - - calculated_light.diffuse = this.calc_diffuse_light( - light_phong, light_direction, norm, vertex_data.texture_coords - ); - - calculated_light.specular = this.calc_specular_light( - light_phong, - light_direction, - norm, - vertex_data.world_space_pos, - vertex_data.texture_coords - ); - } - - float3 calc_ambient_light(in float2 texture_coords) - { - return ambient_map.Sample(texture_coords).xyz * this.material.ambient; - } - - float3 calc_diffuse_light( - in LightPhong light_phong, - in float3 light_dir, - in float3 norm, - in float2 texture_coords - ) - { - float diff = max(dot(norm, light_dir), 0.0); - - return light_phong.diffuse * - (diff * (diffuse_map.Sample(texture_coords).xyz * this.material.diffuse)); - } - - float3 calc_specular_light( - in LightPhong light_phong, - in float3 light_dir, - in float3 norm, - in float3 frag_pos, - in float2 texture_coords - ) - { - // The result of the below pow is undefined if both arguments is 0. This stops - // that problem. This prevents buggy specular light when shininess is 0. - if (this.material.shininess == 0.0) { - return light_phong.specular * - (specular_map.Sample(texture_coords).xyz * this.material.specular); - } - - float3 view_direction = normalize(this.view_pos - frag_pos); - - float3 halfway_direction = normalize(light_dir + view_direction); - - float spec = pow(max(dot(norm, halfway_direction), 0.0), this.material.shininess); - - return light_phong.specular * (spec * (specular_map.Sample(texture_coords).xyz * - this.material.specular)); - } - - float calc_attenuation(in PointLight point_light, in float3 position) - { - float light_distance = length(point_light.position - position); - - return 1.0 / (point_light.attenuation_props.constant + - point_light.attenuation_props.linear * light_distance + - point_light.attenuation_props.quadratic * pow(light_distance, 2)); - } -}; - -struct Model3D -{ - float4x4 model; - float4x4 model_inverted; - float4x4 view; - float4x4 projection; -} - -cbuffer Uniforms -{ - Model3D model_3d; - BlinnPhongLighting lighting; -} - -Sampler2D ambient_map; -Sampler2D diffuse_map; -Sampler2D specular_map; - -struct VertexData -{ - float2 texture_coords; - float3 world_space_pos; - float3 world_space_normal; -}; - -struct VertexStageOutput -{ - VertexData vertex_data : VertexData; - float4 sv_position : SV_Position; -}; - -struct Vertex -{ - float3 pos : STD_POSITION; - float2 texture_coords : STD_UV; - float3 normal : STD_NORMAL; -}; - -struct Fragment -{ - float4 color; -}; - -[shader("vertex")] -VertexStageOutput vertex_main(Vertex vertex) -{ - VertexStageOutput stage_output; - - float4x4 proj_view = mul(model_3d.projection, model_3d.view); - - float4x4 proj_view_model = mul(proj_view, model_3d.model); - - stage_output.sv_position = mul(proj_view_model, float4(vertex.pos, 1.0)); - - float4 vertex_pos = float4(vertex.pos, 1.0); - - stage_output.vertex_data.world_space_pos = - float3(mul(model_3d.model, vertex_pos).xyz); - - stage_output.vertex_data.texture_coords = vertex.texture_coords; - - stage_output.vertex_data.world_space_normal = - mul(float3x3(transpose(model_3d.model_inverted)), vertex.normal); - - return stage_output; -} - -[shader("fragment")] -Fragment fragment_main(VertexData vertex_data: VertexData) : SV_Target -{ - Fragment fragment; - - fragment.color = lighting.evaluate(vertex_data); - - return fragment; -} - diff --git a/engine/res/main_3d_shader.slang b/engine/res/main_3d_shader.slang new file mode 100644 index 0000000..7da164c --- /dev/null +++ b/engine/res/main_3d_shader.slang @@ -0,0 +1,270 @@ +#define MAX_LIGHT_CNT 64 + +struct Material +{ + float3 ambient; + float3 diffuse; + float3 specular; + float shininess; +}; + +struct LightPhong +{ + float3 diffuse; + float3 specular; +}; + +struct AttenuationProperties +{ + float constant; + float linear; + float quadratic; +}; + +struct PointLight +{ + LightPhong phong; + float3 position; + AttenuationProperties attenuation_props; +}; + +struct DirectionalLight +{ + LightPhong phong; + float3 direction; +}; + +struct CalculatedLight +{ + float3 diffuse; + float3 specular; +}; + +struct BlinnPhongLighting +{ + float3 view_pos; + Material material; + + DirectionalLight directional_lights[MAX_LIGHT_CNT]; + uint directional_light_cnt; + + PointLight point_lights[MAX_LIGHT_CNT]; + uint point_light_cnt; + + float4 evaluate(in VertexData vertex_data) + { + float3 ambient_light = this.calc_ambient_light(vertex_data.texture_coords); + + float3 directional_light_sum = this.calc_dir_light_sum(vertex_data); + float3 point_light_sum = this.calc_point_light_sum(vertex_data); + + return float4((ambient_light + directional_light_sum + point_light_sum), 1.0); + } + + float3 calc_dir_light_sum(in VertexData vertex_data) + { + float3 directional_light_sum = float3(0.0, 0.0, 0.0); + + for (uint index = 0; index < this.directional_light_cnt; index++) { + CalculatedLight calculated_dir_light; + + this.calc_light( + // Negated since we want the light to point from the light + // direction + normalize(-this.directional_lights[index].direction), + this.directional_lights[index].phong, + vertex_data, + calculated_dir_light + ); + + directional_light_sum += + calculated_dir_light.diffuse + calculated_dir_light.specular; + } + + return directional_light_sum; + } + + float3 calc_point_light_sum(in VertexData vertex_data) + { + float3 point_light_sum = float3(0.0, 0.0, 0.0); + + for (uint index = 0; index < this.point_light_cnt; index++) { + float3 light_direction = normalize( + this.point_lights[index].position - vertex_data.world_space_pos + ); + + CalculatedLight calculated_point_light; + + this.calc_light( + light_direction, + this.point_lights[index].phong, + vertex_data, + calculated_point_light + ); + + float attenuation = this.calc_attenuation( + this.point_lights[index], vertex_data.world_space_pos + ); + + calculated_point_light.diffuse *= attenuation; + calculated_point_light.specular *= attenuation; + + point_light_sum += + calculated_point_light.diffuse + calculated_point_light.specular; + } + + return point_light_sum; + } + + void calc_light( + in float3 light_direction, + in LightPhong light_phong, + in VertexData vertex_data, + out CalculatedLight calculated_light + ) + { + float3 norm = normalize(vertex_data.world_space_normal); + + calculated_light.diffuse = this.calc_diffuse_light( + light_phong, light_direction, norm, vertex_data.texture_coords + ); + + calculated_light.specular = this.calc_specular_light( + light_phong, + light_direction, + norm, + vertex_data.world_space_pos, + vertex_data.texture_coords + ); + } + + float3 calc_ambient_light(in float2 texture_coords) + { + return ambient_map.Sample(texture_coords).xyz * this.material.ambient; + } + + float3 calc_diffuse_light( + in LightPhong light_phong, + in float3 light_dir, + in float3 norm, + in float2 texture_coords + ) + { + float diff = max(dot(norm, light_dir), 0.0); + + return light_phong.diffuse * + (diff * (diffuse_map.Sample(texture_coords).xyz * this.material.diffuse)); + } + + float3 calc_specular_light( + in LightPhong light_phong, + in float3 light_dir, + in float3 norm, + in float3 frag_pos, + in float2 texture_coords + ) + { + // The result of the below pow is undefined if both arguments is 0. This stops + // that problem. This prevents buggy specular light when shininess is 0. + if (this.material.shininess == 0.0) { + return light_phong.specular * + (specular_map.Sample(texture_coords).xyz * this.material.specular); + } + + float3 view_direction = normalize(this.view_pos - frag_pos); + + float3 halfway_direction = normalize(light_dir + view_direction); + + float spec = pow(max(dot(norm, halfway_direction), 0.0), this.material.shininess); + + return light_phong.specular * (spec * (specular_map.Sample(texture_coords).xyz * + this.material.specular)); + } + + float calc_attenuation(in PointLight point_light, in float3 position) + { + float light_distance = length(point_light.position - position); + + return 1.0 / (point_light.attenuation_props.constant + + point_light.attenuation_props.linear * light_distance + + point_light.attenuation_props.quadratic * pow(light_distance, 2)); + } +}; + +struct Model3D +{ + float4x4 model; + float4x4 model_inverted; + float4x4 view; + float4x4 projection; +} + +cbuffer Uniforms +{ + Model3D model_3d; + BlinnPhongLighting lighting; +} + +Sampler2D ambient_map; +Sampler2D diffuse_map; +Sampler2D specular_map; + +struct VertexData +{ + float2 texture_coords; + float3 world_space_pos; + float3 world_space_normal; +}; + +struct VertexStageOutput +{ + VertexData vertex_data : VertexData; + float4 sv_position : SV_Position; +}; + +struct Vertex +{ + float3 pos : STD_POSITION; + float2 texture_coords : STD_UV; + float3 normal : STD_NORMAL; +}; + +struct Fragment +{ + float4 color; +}; + +[shader("vertex")] +VertexStageOutput vertex_main(Vertex vertex) +{ + VertexStageOutput stage_output; + + float4x4 proj_view = mul(model_3d.projection, model_3d.view); + + float4x4 proj_view_model = mul(proj_view, model_3d.model); + + stage_output.sv_position = mul(proj_view_model, float4(vertex.pos, 1.0)); + + float4 vertex_pos = float4(vertex.pos, 1.0); + + stage_output.vertex_data.world_space_pos = + float3(mul(model_3d.model, vertex_pos).xyz); + + stage_output.vertex_data.texture_coords = vertex.texture_coords; + + stage_output.vertex_data.world_space_normal = + mul(float3x3(transpose(model_3d.model_inverted)), vertex.normal); + + return stage_output; +} + +[shader("fragment")] +Fragment fragment_main(VertexData vertex_data: VertexData) : SV_Target +{ + Fragment fragment; + + fragment.color = lighting.evaluate(vertex_data); + + return fragment; +} + -- cgit v1.2.3-18-g5258