summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-01-21 22:00:39 +0100
committermetamuffin <metamuffin@disroot.org>2025-01-21 22:00:39 +0100
commit12bf2f3302efc9042f12ca17104928c35700c229 (patch)
tree7abf77d5b94308993c9d67f65413f8a8d698c685 /client
parent20d5d575ad84da3d37bd581425fc335016f33c82 (diff)
downloadweareserver-12bf2f3302efc9042f12ca17104928c35700c229.tar
weareserver-12bf2f3302efc9042f12ca17104928c35700c229.tar.bz2
weareserver-12bf2f3302efc9042f12ca17104928c35700c229.tar.zst
split shaders to individual files
Diffstat (limited to 'client')
-rw-r--r--client/src/scene_prepare.rs34
-rw-r--r--client/src/scene_render.rs13
-rw-r--r--client/src/shaders/fragment_pbr.wgsl (renamed from client/src/shader.wgsl)26
-rw-r--r--client/src/shaders/fragment_ui.wgsl29
-rw-r--r--client/src/shaders/vertex_ui.wgsl (renamed from client/src/ui.wgsl)8
-rw-r--r--client/src/shaders/vertex_world.wgsl46
-rw-r--r--client/src/shaders/vertex_world_skin.wgsl48
-rw-r--r--client/src/ui.rs12
8 files changed, 172 insertions, 44 deletions
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<Buffer>,
pub va_tangent: Arc<Buffer>,
pub va_texcoord: Arc<Buffer>,
+ pub va_joint_index: Option<Arc<Buffer>>,
+ pub va_joint_weight: Option<Arc<Buffer>>,
pub tex_albedo: Arc<BindGroup>,
pub tex_normal: Arc<BindGroup>,
pub material: Arc<BindGroup>,
@@ -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/shaders/fragment_pbr.wgsl
index 9cf386c..ac2638d 100644
--- a/client/src/shader.wgsl
+++ b/client/src/shaders/fragment_pbr.wgsl
@@ -13,12 +13,6 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-struct VertexIn {
- @location(0) position: vec3<f32>,
- @location(1) normal: vec3<f32>,
- @location(2) tangent: vec3<f32>, // TODO maybe compress this
- @location(3) texcoord: vec2<f32>,
-}
struct VertexOut {
@builtin(position) clip: vec4<f32>,
@location(0) normal: vec3<f32>,
@@ -26,11 +20,6 @@ struct VertexOut {
@location(2) texcoord: vec2<f32>,
}
-struct PushConst {
- modelview: mat4x4<f32>,
- model_basis: mat3x3<f32>,
-}
-
struct Material {
roughness: f32,
metallic: f32,
@@ -42,25 +31,12 @@ struct Material {
@group(0) @binding(1) var tex_albedo_sampler: sampler;
@group(1) @binding(0) var tex_normal: texture_2d<f32>;
@group(1) @binding(1) var tex_normal_sampler: sampler;
- var<push_constant> pc: PushConst;
@group(2) @binding(0) var<uniform> material: Material;
const LIGHT: vec3<f32> = 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<f32> {
+fn main(vo: VertexOut) -> @location(0) vec4<f32> {
let t_albedo = textureSample(tex_albedo, tex_albedo_sampler, vo.texcoord);
let t_normal = textureSample(tex_normal, tex_normal_sampler, vo.texcoord);
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 <https://www.gnu.org/licenses/>.
+
+
+struct VertexOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) uv: vec2<f32>,
+ @location(1) color: vec4<f32>,
+}
+
+@group(0) @binding(0) var texture: texture_2d<f32>;
+@group(0) @binding(1) var texture_sampler: sampler;
+
+@fragment
+fn main(vo: VertexOut) -> @location(0) vec4<f32> {
+ return pow(textureSample(texture, texture_sampler, vo.uv) * vo.color, vec4(2.2));
+}
diff --git a/client/src/ui.wgsl b/client/src/shaders/vertex_ui.wgsl
index a444f71..7cbac8b 100644
--- a/client/src/ui.wgsl
+++ b/client/src/shaders/vertex_ui.wgsl
@@ -24,8 +24,6 @@ struct VertexOut {
@location(1) color: vec4<f32>,
}
-@group(0) @binding(0) var texture: texture_2d<f32>;
-@group(0) @binding(1) var texture_sampler: sampler;
var<push_constant> project: mat4x4<f32>;
fn unpack_color(color: u32) -> vec4<f32> {
@@ -38,12 +36,8 @@ fn unpack_color(color: u32) -> vec4<f32> {
}
@vertex
-fn vs_main(@builtin(vertex_index) vindex: u32, vi: VertexIn) -> VertexOut {
+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;
}
-@fragment
-fn fs_main(vo: VertexOut) -> @location(0) vec4<f32> {
- return pow(textureSample(texture, texture_sampler, vo.uv) * vo.color, vec4(2.2));
-}
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 <https://www.gnu.org/licenses/>.
+
+struct VertexIn {
+ @location(0) position: vec3<f32>,
+ @location(1) normal: vec3<f32>,
+ @location(2) tangent: vec3<f32>, // TODO maybe compress this
+ @location(3) texcoord: vec2<f32>,
+}
+struct VertexOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) normal: vec3<f32>,
+ @location(1) tangent: vec3<f32>,
+ @location(2) texcoord: vec2<f32>,
+}
+
+struct PushConst {
+ modelview: mat4x4<f32>,
+ model_basis: mat3x3<f32>,
+}
+
+var<push_constant> 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 <https://www.gnu.org/licenses/>.
+
+struct VertexIn {
+ @location(0) position: vec3<f32>,
+ @location(1) normal: vec3<f32>,
+ @location(2) tangent: vec3<f32>, // TODO maybe compress this
+ @location(3) texcoord: vec2<f32>,
+ @location(4) joint_index: vec4<u32>,
+ @location(5) joint_weight: vec4<f32>,
+}
+struct VertexOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) normal: vec3<f32>,
+ @location(1) tangent: vec3<f32>,
+ @location(2) texcoord: vec2<f32>,
+}
+
+struct PushConst {
+ modelview: mat4x4<f32>,
+ model_basis: mat3x3<f32>,
+}
+
+var<push_constant> 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<Device>, queue: Arc<Queue>, 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::<Vertex>() as u64,
step_mode: VertexStepMode::Vertex,