From 12bf2f3302efc9042f12ca17104928c35700c229 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 21 Jan 2025 22:00:39 +0100 Subject: split shaders to individual files --- client/src/scene_prepare.rs | 34 ++++++++++++- client/src/scene_render.rs | 13 +++-- client/src/shader.wgsl | 79 ------------------------------- client/src/shaders/fragment_pbr.wgsl | 55 +++++++++++++++++++++ client/src/shaders/fragment_ui.wgsl | 29 ++++++++++++ client/src/shaders/vertex_ui.wgsl | 43 +++++++++++++++++ client/src/shaders/vertex_world.wgsl | 46 ++++++++++++++++++ client/src/shaders/vertex_world_skin.wgsl | 48 +++++++++++++++++++ client/src/ui.rs | 12 +++-- client/src/ui.wgsl | 49 ------------------- 10 files changed, 268 insertions(+), 140 deletions(-) delete mode 100644 client/src/shader.wgsl create mode 100644 client/src/shaders/fragment_pbr.wgsl create mode 100644 client/src/shaders/fragment_ui.wgsl create mode 100644 client/src/shaders/vertex_ui.wgsl create mode 100644 client/src/shaders/vertex_world.wgsl create mode 100644 client/src/shaders/vertex_world_skin.wgsl delete mode 100644 client/src/ui.wgsl (limited to 'client/src') diff --git a/client/src/scene_prepare.rs b/client/src/scene_prepare.rs index 3a93485..05e9533 100644 --- a/client/src/scene_prepare.rs +++ b/client/src/scene_prepare.rs @@ -110,6 +110,8 @@ pub struct RMeshPart { pub va_normal: Arc, pub va_tangent: Arc, pub va_texcoord: Arc, + pub va_joint_index: Option>, + pub va_joint_weight: Option>, pub tex_albedo: Arc, pub tex_normal: Arc, pub material: Arc, @@ -392,6 +394,21 @@ impl ScenePreparer { }) }; + let joint_weight = if let Some(res) = part.va_joint_weight.clone() { + self.vertex_buffers + .try_get(Resource(res.0, PhantomData)) + .map(Some) + } else { + Some(None) + }; + let joint_index = if let Some(res) = part.va_joint_index.clone() { + self.vertex_buffers + .try_get(Resource(res.0, PhantomData)) + .map(Some) + } else { + Some(None) + }; + let mut tex_albedo = None; if let Some(albedores) = part.tex_albedo { if let Some((_tex, bg)) = self.textures.try_get((albedores, false)) { @@ -447,13 +464,24 @@ impl ScenePreparer { Some(va_tangent), Some(va_texcoord), Some(va_position), + Some(va_joint_index), + Some(va_joint_weight), Some(tex_normal), Some(tex_albedo), Some(material), ) = ( - index, normal, tangent, texcoord, position, tex_normal, tex_albedo, + index, + normal, + tangent, + texcoord, + position, + joint_index, + joint_weight, + tex_normal, + tex_albedo, material, ) { + let double_sided = part.g_double_sided.is_some(); debug!("part created (took {:?}) {pres}", start.elapsed()); self.mesh_parts.insert( pres, @@ -464,10 +492,12 @@ impl ScenePreparer { va_tangent, va_position, va_texcoord, + va_joint_index, + va_joint_weight, tex_albedo, tex_normal, material, - double_sided: part.g_double_sided.is_some(), + double_sided, }), 0, ); diff --git a/client/src/scene_render.rs b/client/src/scene_render.rs index 2100d28..3698ff7 100644 --- a/client/src/scene_render.rs +++ b/client/src/scene_render.rs @@ -38,7 +38,10 @@ pub struct ScenePipeline { impl ScenePipeline { pub fn new(device: &Device, format: TextureFormat) -> (Self, BindGroupLayout, BindGroupLayout) { - let module = device.create_shader_module(include_wgsl!("shader.wgsl")); + let fragment_pbr = device.create_shader_module(include_wgsl!("shaders/fragment_pbr.wgsl")); + let vertex_world = device.create_shader_module(include_wgsl!("shaders/vertex_world.wgsl")); + let _vertex_world_skin = + device.create_shader_module(include_wgsl!("shaders/vertex_world_skin.wgsl")); let texture_bgl = device.create_bind_group_layout(&BindGroupLayoutDescriptor { entries: &[ @@ -87,8 +90,8 @@ impl ScenePipeline { label: None, layout: Some(&pipeline_layout), fragment: Some(FragmentState { - module: &module, - entry_point: Some("fs_main"), + module: &fragment_pbr, + entry_point: Some("main"), targets: &[Some(ColorTargetState { blend: Some(BlendState::PREMULTIPLIED_ALPHA_BLENDING), format, @@ -97,8 +100,8 @@ impl ScenePipeline { compilation_options: PipelineCompilationOptions::default(), }), vertex: VertexState { - module: &module, - entry_point: Some("vs_main"), + module: &vertex_world, + entry_point: Some("main"), buffers: &[ // position VertexBufferLayout { diff --git a/client/src/shader.wgsl b/client/src/shader.wgsl deleted file mode 100644 index 9cf386c..0000000 --- a/client/src/shader.wgsl +++ /dev/null @@ -1,79 +0,0 @@ -// wearechat - generic multiplayer game with voip -// Copyright (C) 2025 metamuffin -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, version 3 of the License only. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -struct VertexIn { - @location(0) position: vec3, - @location(1) normal: vec3, - @location(2) tangent: vec3, // TODO maybe compress this - @location(3) texcoord: vec2, -} -struct VertexOut { - @builtin(position) clip: vec4, - @location(0) normal: vec3, - @location(1) tangent: vec3, - @location(2) texcoord: vec2, -} - -struct PushConst { - modelview: mat4x4, - model_basis: mat3x3, -} - -struct Material { - roughness: f32, - metallic: f32, - albedo_alpha: vec4, - emission: vec3, -} - -@group(0) @binding(0) var tex_albedo: texture_2d; -@group(0) @binding(1) var tex_albedo_sampler: sampler; -@group(1) @binding(0) var tex_normal: texture_2d; -@group(1) @binding(1) var tex_normal_sampler: sampler; - var pc: PushConst; -@group(2) @binding(0) var material: Material; - -const LIGHT: vec3 = vec3(0.64, 0.64, 0.64); - -@vertex -fn vs_main(vi: VertexIn) -> VertexOut { - let clip = pc.modelview * vec4(vi.position, 1.); - let vo = VertexOut( - clip, - normalize(pc.model_basis * vi.normal), - normalize(pc.model_basis * vi.tangent), - vi.texcoord - ); - return vo; -} - -@fragment -fn fs_main(vo: VertexOut) -> @location(0) vec4 { - let t_albedo = textureSample(tex_albedo, tex_albedo_sampler, vo.texcoord); - let t_normal = textureSample(tex_normal, tex_normal_sampler, vo.texcoord); - - let tangent_basis = mat3x3(vo.tangent, cross(vo.tangent, vo.normal), vo.normal); - let normal = tangent_basis * (t_normal.rgb * 2. - 1.); - - let alpha = t_albedo.a; - let lighting = mix(1., saturate(dot(LIGHT, normal)), 1.); - - let color = t_albedo.rgb * lighting; - - if fract(dot(sin(vo.clip * 123.) * 1213., vec4(3., 2., 1., 4.))) > alpha { - discard; - } - return vec4(color, 1.); -} diff --git a/client/src/shaders/fragment_pbr.wgsl b/client/src/shaders/fragment_pbr.wgsl new file mode 100644 index 0000000..ac2638d --- /dev/null +++ b/client/src/shaders/fragment_pbr.wgsl @@ -0,0 +1,55 @@ +// wearechat - generic multiplayer game with voip +// Copyright (C) 2025 metamuffin +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, version 3 of the License only. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +struct VertexOut { + @builtin(position) clip: vec4, + @location(0) normal: vec3, + @location(1) tangent: vec3, + @location(2) texcoord: vec2, +} + +struct Material { + roughness: f32, + metallic: f32, + albedo_alpha: vec4, + emission: vec3, +} + +@group(0) @binding(0) var tex_albedo: texture_2d; +@group(0) @binding(1) var tex_albedo_sampler: sampler; +@group(1) @binding(0) var tex_normal: texture_2d; +@group(1) @binding(1) var tex_normal_sampler: sampler; +@group(2) @binding(0) var material: Material; + +const LIGHT: vec3 = vec3(0.64, 0.64, 0.64); + +@fragment +fn main(vo: VertexOut) -> @location(0) vec4 { + let t_albedo = textureSample(tex_albedo, tex_albedo_sampler, vo.texcoord); + let t_normal = textureSample(tex_normal, tex_normal_sampler, vo.texcoord); + + let tangent_basis = mat3x3(vo.tangent, cross(vo.tangent, vo.normal), vo.normal); + let normal = tangent_basis * (t_normal.rgb * 2. - 1.); + + let alpha = t_albedo.a; + let lighting = mix(1., saturate(dot(LIGHT, normal)), 1.); + + let color = t_albedo.rgb * lighting; + + if fract(dot(sin(vo.clip * 123.) * 1213., vec4(3., 2., 1., 4.))) > alpha { + discard; + } + return vec4(color, 1.); +} diff --git a/client/src/shaders/fragment_ui.wgsl b/client/src/shaders/fragment_ui.wgsl new file mode 100644 index 0000000..6638628 --- /dev/null +++ b/client/src/shaders/fragment_ui.wgsl @@ -0,0 +1,29 @@ +// wearechat - generic multiplayer game with voip +// Copyright (C) 2025 metamuffin +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, version 3 of the License only. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + + +struct VertexOut { + @builtin(position) clip: vec4, + @location(0) uv: vec2, + @location(1) color: vec4, +} + +@group(0) @binding(0) var texture: texture_2d; +@group(0) @binding(1) var texture_sampler: sampler; + +@fragment +fn main(vo: VertexOut) -> @location(0) vec4 { + return pow(textureSample(texture, texture_sampler, vo.uv) * vo.color, vec4(2.2)); +} diff --git a/client/src/shaders/vertex_ui.wgsl b/client/src/shaders/vertex_ui.wgsl new file mode 100644 index 0000000..7cbac8b --- /dev/null +++ b/client/src/shaders/vertex_ui.wgsl @@ -0,0 +1,43 @@ +// wearechat - generic multiplayer game with voip +// Copyright (C) 2025 metamuffin +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, version 3 of the License only. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +struct VertexIn { + @location(0) pos: vec2, + @location(1) uv: vec2, + @location(2) color: u32, +} +struct VertexOut { + @builtin(position) clip: vec4, + @location(0) uv: vec2, + @location(1) color: vec4, +} + +var project: mat4x4; + +fn unpack_color(color: u32) -> vec4 { + return vec4( + f32(color & 255u), + f32((color >> 8u) & 255u), + f32((color >> 16u) & 255u), + f32((color >> 24u) & 255u), + ) / 255.0; +} + +@vertex +fn main(@builtin(vertex_index) vindex: u32, vi: VertexIn) -> VertexOut { + var clip = project * vec4(vi.pos, 0., 1.); + let vo = VertexOut(clip, vi.uv, unpack_color(vi.color)); + return vo; +} diff --git a/client/src/shaders/vertex_world.wgsl b/client/src/shaders/vertex_world.wgsl new file mode 100644 index 0000000..5d69acd --- /dev/null +++ b/client/src/shaders/vertex_world.wgsl @@ -0,0 +1,46 @@ +// wearechat - generic multiplayer game with voip +// Copyright (C) 2025 metamuffin +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, version 3 of the License only. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +struct VertexIn { + @location(0) position: vec3, + @location(1) normal: vec3, + @location(2) tangent: vec3, // TODO maybe compress this + @location(3) texcoord: vec2, +} +struct VertexOut { + @builtin(position) clip: vec4, + @location(0) normal: vec3, + @location(1) tangent: vec3, + @location(2) texcoord: vec2, +} + +struct PushConst { + modelview: mat4x4, + model_basis: mat3x3, +} + +var pc: PushConst; + +@vertex +fn main(vi: VertexIn) -> VertexOut { + let clip = pc.modelview * vec4(vi.position, 1.); + let vo = VertexOut( + clip, + normalize(pc.model_basis * vi.normal), + normalize(pc.model_basis * vi.tangent), + vi.texcoord + ); + return vo; +} diff --git a/client/src/shaders/vertex_world_skin.wgsl b/client/src/shaders/vertex_world_skin.wgsl new file mode 100644 index 0000000..85dda64 --- /dev/null +++ b/client/src/shaders/vertex_world_skin.wgsl @@ -0,0 +1,48 @@ +// wearechat - generic multiplayer game with voip +// Copyright (C) 2025 metamuffin +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, version 3 of the License only. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +struct VertexIn { + @location(0) position: vec3, + @location(1) normal: vec3, + @location(2) tangent: vec3, // TODO maybe compress this + @location(3) texcoord: vec2, + @location(4) joint_index: vec4, + @location(5) joint_weight: vec4, +} +struct VertexOut { + @builtin(position) clip: vec4, + @location(0) normal: vec3, + @location(1) tangent: vec3, + @location(2) texcoord: vec2, +} + +struct PushConst { + modelview: mat4x4, + model_basis: mat3x3, +} + +var pc: PushConst; + +@vertex +fn main(vi: VertexIn) -> VertexOut { + let clip = pc.modelview * vec4(vi.position, 1.); + let vo = VertexOut( + clip, + normalize(pc.model_basis * vi.normal), + normalize(pc.model_basis * vi.tangent), + vi.texcoord + ); + return vo; +} diff --git a/client/src/ui.rs b/client/src/ui.rs index 92de3c8..78ca29d 100644 --- a/client/src/ui.rs +++ b/client/src/ui.rs @@ -75,7 +75,9 @@ pub enum UiEvent { impl UiRenderer { pub fn new(device: Arc, queue: Arc, format: TextureFormat) -> Self { - let module = device.create_shader_module(include_wgsl!("ui.wgsl")); + let frag_shader = device.create_shader_module(include_wgsl!("shaders/fragment_ui.wgsl")); + let vert_shader = device.create_shader_module(include_wgsl!("shaders/vertex_ui.wgsl")); + let bind_group_layout = device.create_bind_group_layout(&BindGroupLayoutDescriptor { entries: &[ BindGroupLayoutEntry { @@ -109,8 +111,8 @@ impl UiRenderer { label: None, layout: Some(&pipeline_layout), fragment: Some(FragmentState { - module: &module, - entry_point: Some("fs_main"), + module: &frag_shader, + entry_point: Some("main"), targets: &[Some(ColorTargetState { blend: Some(BlendState::PREMULTIPLIED_ALPHA_BLENDING), format, @@ -119,8 +121,8 @@ impl UiRenderer { compilation_options: PipelineCompilationOptions::default(), }), vertex: VertexState { - module: &module, - entry_point: Some("vs_main"), + module: &vert_shader, + entry_point: Some("main"), buffers: &[VertexBufferLayout { array_stride: size_of::() as u64, step_mode: VertexStepMode::Vertex, diff --git a/client/src/ui.wgsl b/client/src/ui.wgsl deleted file mode 100644 index a444f71..0000000 --- a/client/src/ui.wgsl +++ /dev/null @@ -1,49 +0,0 @@ -// wearechat - generic multiplayer game with voip -// Copyright (C) 2025 metamuffin -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published by -// the Free Software Foundation, version 3 of the License only. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see . - -struct VertexIn { - @location(0) pos: vec2, - @location(1) uv: vec2, - @location(2) color: u32, -} -struct VertexOut { - @builtin(position) clip: vec4, - @location(0) uv: vec2, - @location(1) color: vec4, -} - -@group(0) @binding(0) var texture: texture_2d; -@group(0) @binding(1) var texture_sampler: sampler; -var project: mat4x4; - -fn unpack_color(color: u32) -> vec4 { - return vec4( - f32(color & 255u), - f32((color >> 8u) & 255u), - f32((color >> 16u) & 255u), - f32((color >> 24u) & 255u), - ) / 255.0; -} - -@vertex -fn vs_main(@builtin(vertex_index) vindex: u32, vi: VertexIn) -> VertexOut { - var clip = project * vec4(vi.pos, 0., 1.); - let vo = VertexOut(clip, vi.uv, unpack_color(vi.color)); - return vo; -} -@fragment -fn fs_main(vo: VertexOut) -> @location(0) vec4 { - return pow(textureSample(texture, texture_sampler, vo.uv) * vo.color, vec4(2.2)); -} -- cgit v1.2.3-70-g09d2