]> git.nega.tv - josh/narcissus/commitdiff
shark: Move shaders into their own crate
authorJoshua Simmons <josh@nega.tv>
Sun, 28 Apr 2024 15:08:27 +0000 (17:08 +0200)
committerJoshua Simmons <josh@nega.tv>
Sun, 28 Apr 2024 15:08:27 +0000 (17:08 +0200)
We can build them with cargo and a build script.

17 files changed:
Cargo.lock
Cargo.toml
title/shark-shaders/Cargo.toml [new file with mode: 0644]
title/shark-shaders/build.rs [new file with mode: 0644]
title/shark-shaders/shaders/basic.frag.glsl [moved from title/shark/src/shaders/basic.frag.glsl with 85% similarity]
title/shark-shaders/shaders/basic.vert.glsl [moved from title/shark/src/shaders/basic.vert.glsl with 100% similarity]
title/shark-shaders/shaders/text.frag.glsl [moved from title/shark/src/shaders/text.frag.glsl with 100% similarity]
title/shark-shaders/shaders/text.vert.glsl [moved from title/shark/src/shaders/text.vert.glsl with 100% similarity]
title/shark-shaders/src/lib.rs [new file with mode: 0644]
title/shark/Cargo.toml
title/shark/src/pipelines/basic.rs
title/shark/src/pipelines/text.rs
title/shark/src/shaders/basic.frag.spv [deleted file]
title/shark/src/shaders/basic.vert.spv [deleted file]
title/shark/src/shaders/build.sh [deleted file]
title/shark/src/shaders/text.frag.spv [deleted file]
title/shark/src/shaders/text.vert.spv [deleted file]

index 9f2873bea2c31207d30ec23e4393ffab2fc31f5f..3bfe90d8422457d3cbac11b609f3ec4d1b41369c 100644 (file)
@@ -183,8 +183,13 @@ dependencies = [
  "narcissus-gpu",
  "narcissus-image",
  "narcissus-maths",
+ "shark-shaders",
 ]
 
+[[package]]
+name = "shark-shaders"
+version = "0.1.0"
+
 [[package]]
 name = "sqlite-sys"
 version = "0.1.0"
index e1766e3115f3cb2b1abc9749dc5dd13be4abcead..3cc5ecadd26099c5abc29bb3e3b54e22259144ee 100644 (file)
@@ -2,6 +2,7 @@
 
 members = [
     "title/shark",
+    "title/shark-shaders",
     "external/blake3-smol",
     "external/renderdoc-sys",
     "external/sdl2-sys",
diff --git a/title/shark-shaders/Cargo.toml b/title/shark-shaders/Cargo.toml
new file mode 100644 (file)
index 0000000..ccd34fd
--- /dev/null
@@ -0,0 +1,8 @@
+[package]
+name = "shark-shaders"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/title/shark-shaders/build.rs b/title/shark-shaders/build.rs
new file mode 100644 (file)
index 0000000..c543f80
--- /dev/null
@@ -0,0 +1,105 @@
+use std::io::Write;
+use std::process::Command;
+
+const SHADER_ROOT: &str = "shaders";
+
+#[derive(Clone, Copy)]
+enum ShaderStage {
+    Vertex,
+    Fragment,
+}
+
+impl ShaderStage {
+    fn name(self) -> &'static str {
+        match self {
+            ShaderStage::Vertex => "vert",
+            ShaderStage::Fragment => "frag",
+        }
+    }
+}
+
+#[derive(Clone, Copy)]
+struct Shader {
+    stage: ShaderStage,
+    name: &'static str,
+}
+
+const SHADERS: [Shader; 4] = [
+    Shader {
+        stage: ShaderStage::Vertex,
+        name: "basic",
+    },
+    Shader {
+        stage: ShaderStage::Fragment,
+        name: "basic",
+    },
+    Shader {
+        stage: ShaderStage::Vertex,
+        name: "text",
+    },
+    Shader {
+        stage: ShaderStage::Fragment,
+        name: "text",
+    },
+];
+
+fn main() {
+    let out_dir = std::env::var("OUT_DIR").unwrap();
+    let debug = match std::env::var("DEBUG").unwrap().as_str() {
+        "true" | "2" | "1" => "",
+        _ => "0",
+    };
+
+    let commands = SHADERS.map(|shader| {
+        Command::new("glslang")
+            .args(["--target-env", "vulkan1.3"])
+            .arg(&format!("-g{debug}"))
+            .args([
+                "-o",
+                &format!("{out_dir}/{}.{}.spv", shader.name, shader.stage.name()),
+            ])
+            .arg(&format!(
+                "{SHADER_ROOT}/{}.{}.glsl",
+                shader.name,
+                shader.stage.name()
+            ))
+            .spawn()
+            .unwrap()
+    });
+
+    let dst_path = std::path::Path::new(&out_dir).join("shaders.rs");
+    let mut file = std::fs::File::create(dst_path).unwrap();
+
+    writeln!(
+        file,
+        "#[repr(align(4))]\nstruct SpirvBytes<const LEN: usize>([u8; LEN]);"
+    )
+    .unwrap();
+
+    for shader in SHADERS {
+        writeln!(
+            file,
+            "pub const {}_{}_SPV: &'static [u8] = &SpirvBytes(*include_bytes!(\"{out_dir}/{}.{}.spv\")).0;",
+            shader.name.to_ascii_uppercase(),
+            shader.stage.name().to_ascii_uppercase(),
+            shader.name,
+            shader.stage.name()
+        )
+        .unwrap();
+    }
+
+    for mut command in commands {
+        let status = command.wait().unwrap();
+        assert!(status.success());
+    }
+
+    for shader in SHADERS {
+        println!(
+            "cargo::rerun-if-changed={SHADER_ROOT}/{}.{}.glsl",
+            shader.name,
+            shader.stage.name()
+        )
+    }
+
+    println!("cargo::rerun-if-changed=build.rs");
+}
similarity index 85%
rename from title/shark/src/shaders/basic.frag.glsl
rename to title/shark-shaders/shaders/basic.frag.glsl
index c09e33056ca227a0e5f6ec91c95e19369e303c02..bf30d60ae98d69d110cacfe981919586c2176090 100644 (file)
@@ -8,7 +8,7 @@ layout(location = 1) in vec3 normal;
 layout(location = 0) out vec4 outColor;
 
 void main() {
-    float NdotL = max(dot(normal, vec3(0.0, 1.0, 0.0)), 0.1f);
+    float NdotL = max(dot(normal, vec3(0.0, 1.0, 0.0)), 0.1);
     vec3 rgb = texture(sampler2D(tex, texSampler), vec2(texcoord.x, texcoord.y)).rgb;
     outColor = vec4(rgb * NdotL, 1.0);
 }
diff --git a/title/shark-shaders/src/lib.rs b/title/shark-shaders/src/lib.rs
new file mode 100644 (file)
index 0000000..9f574b0
--- /dev/null
@@ -0,0 +1 @@
+include!(concat!(env!("OUT_DIR"), "/shaders.rs"));
index 85b4ca5dec16bac0e262b4f35b1262fa0c473f4b..8b6d1aa9c6c8927d7ac6444b685f0fd0658f950d 100644 (file)
@@ -11,4 +11,6 @@ narcissus-core = { path = "../../engine/narcissus-core" }
 narcissus-font = { path = "../../engine/narcissus-font" }
 narcissus-maths = { path = "../../engine/narcissus-maths" }
 narcissus-image = { path = "../../engine/narcissus-image" }
-narcissus-gpu = { path = "../../engine/narcissus-gpu" }
\ No newline at end of file
+narcissus-gpu = { path = "../../engine/narcissus-gpu" }
+
+shark-shaders = { path = "../shark-shaders" }
\ No newline at end of file
index 063d885082961a555f9981bb499549991263ed23..110a8666168fddc3c92493c0b35a049c7c1969a0 100644 (file)
@@ -1,4 +1,4 @@
-use narcissus_core::{default, include_bytes_align};
+use narcissus_core::default;
 use narcissus_gpu::{
     Bind, BindGroupLayout, BindGroupLayoutDesc, BindGroupLayoutEntryDesc, BindingType, BlendMode,
     BufferUsageFlags, CmdEncoder, CompareOp, CullingMode, Device, DeviceExt, Frame, FrontFace,
@@ -8,9 +8,6 @@ use narcissus_gpu::{
 };
 use narcissus_maths::{Affine3, Mat4};
 
-const VERT_SPV: &[u8] = include_bytes_align!(4, "../shaders/basic.vert.spv");
-const FRAG_SPV: &[u8] = include_bytes_align!(4, "../shaders/basic.frag.spv");
-
 #[allow(unused)]
 #[repr(C)]
 pub struct BasicUniforms {
@@ -84,11 +81,11 @@ impl BasicPipeline {
         let pipeline = device.create_graphics_pipeline(&GraphicsPipelineDesc {
             vertex_shader: ShaderDesc {
                 entry: c"main",
-                code: VERT_SPV,
+                code: shark_shaders::BASIC_VERT_SPV,
             },
             fragment_shader: ShaderDesc {
                 entry: c"main",
-                code: FRAG_SPV,
+                code: shark_shaders::BASIC_FRAG_SPV,
             },
             bind_group_layouts: &[uniforms_bind_group_layout, storage_bind_group_layout],
             layout: GraphicsPipelineLayout {
index 6239ea295773914dcf9214a43a0e412782d69c10..ab1ba51a03abe409a26425424c0a10ad6e934b49 100644 (file)
@@ -1,4 +1,4 @@
-use narcissus_core::{default, include_bytes_align};
+use narcissus_core::default;
 use narcissus_font::{TouchedGlyph, TouchedGlyphIndex};
 use narcissus_gpu::{
     Bind, BindGroupLayout, BindGroupLayoutDesc, BindGroupLayoutEntryDesc, BindingType, BlendMode,
@@ -7,9 +7,7 @@ use narcissus_gpu::{
     PolygonMode, Sampler, SamplerAddressMode, SamplerDesc, SamplerFilter, ShaderDesc,
     ShaderStageFlags, ThreadToken, Topology, TypedBind,
 };
-
-const VERT_SPV: &[u8] = include_bytes_align!(4, "../shaders/text.vert.spv");
-const FRAG_SPV: &[u8] = include_bytes_align!(4, "../shaders/text.frag.spv");
+use shark_shaders;
 
 #[allow(unused)]
 #[repr(C)]
@@ -107,11 +105,11 @@ impl TextPipeline {
         let pipeline = device.create_graphics_pipeline(&GraphicsPipelineDesc {
             vertex_shader: ShaderDesc {
                 entry: c"main",
-                code: VERT_SPV,
+                code: shark_shaders::TEXT_VERT_SPV,
             },
             fragment_shader: ShaderDesc {
                 entry: c"main",
-                code: FRAG_SPV,
+                code: shark_shaders::TEXT_FRAG_SPV,
             },
             bind_group_layouts: &[bind_group_layout],
             layout: GraphicsPipelineLayout {
diff --git a/title/shark/src/shaders/basic.frag.spv b/title/shark/src/shaders/basic.frag.spv
deleted file mode 100644 (file)
index aabdd66..0000000
Binary files a/title/shark/src/shaders/basic.frag.spv and /dev/null differ
diff --git a/title/shark/src/shaders/basic.vert.spv b/title/shark/src/shaders/basic.vert.spv
deleted file mode 100644 (file)
index 4e644f2..0000000
Binary files a/title/shark/src/shaders/basic.vert.spv and /dev/null differ
diff --git a/title/shark/src/shaders/build.sh b/title/shark/src/shaders/build.sh
deleted file mode 100755 (executable)
index 4d379fc..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/bash
-
-pushd "${0%/*}"
-glslc --target-env=vulkan1.3 -O -fshader-stage=vert -o basic.vert.spv basic.vert.glsl
-glslc --target-env=vulkan1.3 -O -fshader-stage=frag -o basic.frag.spv basic.frag.glsl
-glslc --target-env=vulkan1.3 -O -fshader-stage=vert -o text.vert.spv text.vert.glsl
-glslc --target-env=vulkan1.3 -O -fshader-stage=frag -o text.frag.spv text.frag.glsl
-echo "built shaders"
-popd
diff --git a/title/shark/src/shaders/text.frag.spv b/title/shark/src/shaders/text.frag.spv
deleted file mode 100644 (file)
index d858dfd..0000000
Binary files a/title/shark/src/shaders/text.frag.spv and /dev/null differ
diff --git a/title/shark/src/shaders/text.vert.spv b/title/shark/src/shaders/text.vert.spv
deleted file mode 100644 (file)
index 3dd51db..0000000
Binary files a/title/shark/src/shaders/text.vert.spv and /dev/null differ