Skip to content

Commit

Permalink
Added multiple shader source option
Browse files Browse the repository at this point in the history
  • Loading branch information
syth-trader committed Feb 28, 2024
1 parent 1d4ee0a commit db90739
Show file tree
Hide file tree
Showing 8 changed files with 373 additions and 63 deletions.
2 changes: 1 addition & 1 deletion example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "example"
version = "0.7.0"
version = "0.7.4"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion example/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn main() -> Result<()> {
.derive_serde(false)
.output_file("src/shader.rs")
.short_constructor(2)
.shader_source_type(WgslShaderSourceType::UseComposerWithIncludeStr)
.shader_source_type(WgslShaderSourceType::UseBothComposerWithPathAndIncludeStr)
.build()?
.generate()
.into_diagnostic()
Expand Down
255 changes: 225 additions & 30 deletions example/src/shader.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// File automatically generated by wgsl_bindgen^
//
// ^ wgsl_bindgen version 0.7.0
// ^ wgsl_bindgen version 0.7.4
// Changes made to this file will not be saved.
// SourceHash: 6f469152513ffb30656f1e8a34dc974a59e090971e235acd89be6ecbd333d8f4
// SourceHash: 7db63b2890a69cf5dc81933a5710b1c63b8ab9863314b37ebd28c9dce576cfe1

#![allow(unused, non_snake_case, non_camel_case_types)]
mod _root {
Expand Down Expand Up @@ -856,38 +856,171 @@ pub mod testbed {
},
)
}
pub fn init_composer() -> naga_oil::compose::Composer {
pub fn create_shader_module(device: &wgpu::Device) -> wgpu::ShaderModule {
let source = std::borrow::Cow::Borrowed(SHADER_STRING);
device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(source),
})
}
const SHADER_STRING: &'static str = r#"
struct rtsStructX_naga_oil_mod_XEIXC4L3NN5ZGKLLTNBQWIZLSFVTGS3DFOMXXEZLBMNUG2ZJCX {
other_data: i32,
the_array: array<u32>,
}
struct ScalarsX_naga_oil_mod_XOR4XAZLTX {
a: u32,
b: i32,
c: f32,
}
struct VectorsU32X_naga_oil_mod_XOR4XAZLTX {
a: vec2<u32>,
b: vec3<u32>,
c: vec4<u32>,
}
struct VectorsI32X_naga_oil_mod_XOR4XAZLTX {
a: vec2<i32>,
b: vec3<i32>,
c: vec4<i32>,
}
struct VectorsF32X_naga_oil_mod_XOR4XAZLTX {
a: vec2<f32>,
b: vec3<f32>,
c: vec4<f32>,
}
struct MatricesF32X_naga_oil_mod_XOR4XAZLTX {
a: mat4x4<f32>,
b: mat4x3<f32>,
c: mat4x2<f32>,
d: mat3x4<f32>,
e: mat3x3<f32>,
f: mat3x2<f32>,
g: mat2x4<f32>,
h: mat2x3<f32>,
i: mat2x2<f32>,
}
struct StaticArraysX_naga_oil_mod_XOR4XAZLTX {
a: array<u32, 5>,
b: array<f32, 3>,
c: array<mat4x4<f32>, 512>,
d: array<vec3<f32>, 4>,
}
struct NestedX_naga_oil_mod_XOR4XAZLTX {
a: MatricesF32X_naga_oil_mod_XOR4XAZLTX,
b: VectorsF32X_naga_oil_mod_XOR4XAZLTX,
}
struct Uniforms {
color_rgb: vec4<f32>,
scalars: ScalarsX_naga_oil_mod_XOR4XAZLTX,
}
struct VertexInput {
@location(0) position: vec3<f32>,
}
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
}
@group(1) @binding(1)
var<uniform> ONEX_naga_oil_mod_XEIXC4L3NN5ZGKLLTNBQWIZLSFVTGS3DFOMXXEZLBMNUG2ZJCX: f32;
@group(2) @binding(1)
var<storage> rts: rtsStructX_naga_oil_mod_XEIXC4L3NN5ZGKLLTNBQWIZLSFVTGS3DFOMXXEZLBMNUG2ZJCX;
@group(2) @binding(2)
var<storage> a: ScalarsX_naga_oil_mod_XOR4XAZLTX;
@group(2) @binding(3)
var<storage> b: VectorsU32X_naga_oil_mod_XOR4XAZLTX;
@group(2) @binding(4)
var<storage> c: VectorsI32X_naga_oil_mod_XOR4XAZLTX;
@group(2) @binding(5)
var<storage> d: VectorsF32X_naga_oil_mod_XOR4XAZLTX;
@group(2) @binding(6)
var<storage> f: MatricesF32X_naga_oil_mod_XOR4XAZLTX;
@group(2) @binding(8)
var<storage> h: StaticArraysX_naga_oil_mod_XOR4XAZLTX;
@group(2) @binding(9)
var<storage> i: NestedX_naga_oil_mod_XOR4XAZLTX;
@group(0) @binding(0)
var color_texture: texture_2d<f32>;
@group(0) @binding(1)
var color_sampler: sampler;
@group(1) @binding(0)
var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.clip_position = vec4<f32>(in.position.xyz, 1f);
let _e13 = ONEX_naga_oil_mod_XEIXC4L3NN5ZGKLLTNBQWIZLSFVTGS3DFOMXXEZLBMNUG2ZJCX;
out.tex_coords = ((in.position.xy * 0.5f) + vec2((0.5f * _e13)));
let _e18 = out;
return _e18;
}
@fragment
fn fs_main(in_1: VertexOutput) -> @location(0) vec4<f32> {
let _e4 = textureSample(color_texture, color_sampler, in_1.tex_coords);
let color = _e4.xyz;
let _e8 = uniforms.color_rgb;
return vec4<f32>((color * _e8.xyz), 1f);
}
"#;
pub fn init_composer(
entry_dir_path: &std::path::Path,
) -> Result<naga_oil::compose::Composer, std::io::Error> {
let mut composer = naga_oil::compose::Composer::default();
let mut source_path = entry_dir_path.to_path_buf();
source_path.push("../more-shader-files/reachme.wgsl");
let source = std::fs::read_to_string(&source_path)?;
composer
.add_composable_module(naga_oil::compose::ComposableModuleDescriptor {
source: include_str!("shader/../more-shader-files/reachme.wgsl"),
file_path: "shader/../more-shader-files/reachme.wgsl",
source: &source,
file_path: source_path.to_str().unwrap(),
language: naga_oil::compose::ShaderLanguage::Wgsl,
as_name: Some("\"../more-shader-files/reachme\"".into()),
..Default::default()
})
.expect("failed to add composer module");
let mut source_path = entry_dir_path.to_path_buf();
source_path.push("types.wgsl");
let source = std::fs::read_to_string(&source_path)?;
composer
.add_composable_module(naga_oil::compose::ComposableModuleDescriptor {
source: include_str!("shader/types.wgsl"),
file_path: "shader/types.wgsl",
source: &source,
file_path: source_path.to_str().unwrap(),
language: naga_oil::compose::ShaderLanguage::Wgsl,
as_name: Some("types".into()),
..Default::default()
})
.expect("failed to add composer module");
composer
Ok(composer)
}
pub fn make_naga_module(
entry_dir_path: &std::path::Path,
composer: &mut naga_oil::compose::Composer,
) -> wgpu::naga::Module {
composer
) -> Result<wgpu::naga::Module, std::io::Error> {
let mut source_path = entry_dir_path.to_path_buf();
source_path.push("testbed.wgsl");
let source = std::fs::read_to_string(&source_path)?;
let module = composer
.make_naga_module(naga_oil::compose::NagaModuleDescriptor {
source: include_str!("shader/testbed.wgsl"),
file_path: "shader/testbed.wgsl",
source: &source,
file_path: source_path.to_str().unwrap(),
..Default::default()
})
.expect("failed to build naga module")
.expect("failed to build naga module");
Ok(module)
}
pub fn naga_module_to_string(module: &wgpu::naga::Module) -> String {
let info = wgpu::naga::valid::Validator::new(
Expand All @@ -903,16 +1036,20 @@ pub mod testbed {
)
.expect("failed to convert naga module to source")
}
pub fn create_shader_module(device: &wgpu::Device) -> wgpu::ShaderModule {
let mut composer = init_composer();
let module = make_naga_module(&mut composer);
pub fn create_shader_module_from_dir(
entry_dir_path: &std::path::Path,
device: &wgpu::Device,
) -> Result<wgpu::ShaderModule, std::io::Error> {
let mut composer = init_composer(entry_dir_path)?;
let module = make_naga_module(entry_dir_path, &mut composer)?;
let source = naga_module_to_string(&module);
let source = std::borrow::Cow::Owned(source);
device
let module = device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(source),
})
});
Ok(module)
}
}
pub mod triangle {
Expand Down Expand Up @@ -1138,20 +1275,74 @@ pub mod triangle {
},
)
}
pub fn init_composer() -> naga_oil::compose::Composer {
pub fn create_shader_module(device: &wgpu::Device) -> wgpu::ShaderModule {
let source = std::borrow::Cow::Borrowed(SHADER_STRING);
device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(source),
})
}
const SHADER_STRING: &'static str = r#"
struct Uniforms {
color_rgb: vec4<f32>,
}
struct VertexInput {
@location(0) position: vec3<f32>,
}
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
}
@group(0) @binding(0)
var color_texture: texture_2d<f32>;
@group(0) @binding(1)
var color_sampler: sampler;
@group(1) @binding(0)
var<uniform> uniforms: Uniforms;
@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
out.clip_position = vec4<f32>(in.position.xyz, 1f);
out.tex_coords = ((in.position.xy * 0.5f) + vec2(0.5f));
let _e15 = out;
return _e15;
}
@fragment
fn fs_main(in_1: VertexOutput) -> @location(0) vec4<f32> {
let _e4 = textureSample(color_texture, color_sampler, in_1.tex_coords);
let color = _e4.xyz;
let _e8 = uniforms.color_rgb;
return vec4<f32>((color * _e8.xyz), 1f);
}
"#;
pub fn init_composer(
entry_dir_path: &std::path::Path,
) -> Result<naga_oil::compose::Composer, std::io::Error> {
let mut composer = naga_oil::compose::Composer::default();
composer
Ok(composer)
}
pub fn make_naga_module(
entry_dir_path: &std::path::Path,
composer: &mut naga_oil::compose::Composer,
) -> wgpu::naga::Module {
composer
) -> Result<wgpu::naga::Module, std::io::Error> {
let mut source_path = entry_dir_path.to_path_buf();
source_path.push("triangle.wgsl");
let source = std::fs::read_to_string(&source_path)?;
let module = composer
.make_naga_module(naga_oil::compose::NagaModuleDescriptor {
source: include_str!("shader/triangle.wgsl"),
file_path: "shader/triangle.wgsl",
source: &source,
file_path: source_path.to_str().unwrap(),
..Default::default()
})
.expect("failed to build naga module")
.expect("failed to build naga module");
Ok(module)
}
pub fn naga_module_to_string(module: &wgpu::naga::Module) -> String {
let info = wgpu::naga::valid::Validator::new(
Expand All @@ -1167,15 +1358,19 @@ pub mod triangle {
)
.expect("failed to convert naga module to source")
}
pub fn create_shader_module(device: &wgpu::Device) -> wgpu::ShaderModule {
let mut composer = init_composer();
let module = make_naga_module(&mut composer);
pub fn create_shader_module_from_dir(
entry_dir_path: &std::path::Path,
device: &wgpu::Device,
) -> Result<wgpu::ShaderModule, std::io::Error> {
let mut composer = init_composer(entry_dir_path)?;
let module = make_naga_module(entry_dir_path, &mut composer)?;
let source = naga_module_to_string(&module);
let source = std::borrow::Cow::Owned(source);
device
let module = device
.create_shader_module(wgpu::ShaderModuleDescriptor {
label: None,
source: wgpu::ShaderSource::Wgsl(source),
})
});
Ok(module)
}
}
2 changes: 1 addition & 1 deletion wgsl_bindgen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wgsl_bindgen"
version = "0.7.0"
version = "0.7.4"
authors = ["Swoorup", "ScanMountGoat(Original)"]
description = "Generate typesafe Rust bindings for wgsl shaders in wgpu"
license = "MIT"
Expand Down
4 changes: 4 additions & 0 deletions wgsl_bindgen/src/bindgen/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub enum WgslShaderSourceType {
/// Use Composer with shader path relative to executing directory, including helper functions which will be executed on runtime
/// This is useful for hot-reloading
UseComposerWithPath,

/// Use both `UseSingleString` and `UseComposerWithPath` option.
UseBothComposerWithPathAndIncludeStr,
}

/// A struct representing a directory to scan for additional source files.
Expand Down Expand Up @@ -258,6 +261,7 @@ impl WgslBindgenOptionBuilder {
self
}

/// Adds custom struct mappings to the type map.
pub fn custom_struct_mapping(
&mut self,
mappings: impl IntoIterator<Item = impl Into<CustomStructMapping>>,
Expand Down
Loading

0 comments on commit db90739

Please sign in to comment.