diff --git a/CHANGELOG.md b/CHANGELOG.md index e98d9eabaa..c2586c5a52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,19 +3,19 @@ ## Unreleased ### Added - +- [Do not depend on spirv on wasm32 target][689] - [Another fast compile flag for macOS][552] ### Changed - Breaking Change: [sRGB awareness for `Color`][616] - - Color is now assumed to be provided in the non-linear sRGB colorspace, and constructors such as `Color::rgb` and `Color::rgba` will be converted to linear sRGB under-the-hood. + - Color is now assumed to be provided in the non-linear sRGB colorspace, and constructors such as `Color::rgb` and `Color::rgba` will be converted to linear sRGB under-the-hood. - This allows drop-in use of colors from most applications. - New methods `Color::rgb_linear` and `Color::rgba_linear` will accept colors already in linear sRGB (the old behavior) - Individual color-components must now be accessed through setters and getters: `.r`, `.g`, `.b`, `.a`, `.set_r`, `.set_g`, `.set_b`, `.set_a`, and the corresponding methods with the `*_linear` suffix. - Despawning an entity multiple times causes a debug-level log message to be emitted instead of a panic [649] [651] - +[689]: https://github.com/bevyengine/bevy/pull/689 [552]: https://github.com/bevyengine/bevy/pull/552 [616]: https://github.com/bevyengine/bevy/pull/616 [649]: https://github.com/bevyengine/bevy/pull/649 @@ -43,7 +43,7 @@ - e.g. `query.iter().par_iter(batch_size).for_each(/* ... */)` - [Added gamepad support using Gilrs][280] - [Implement WASM support for bevy_winit][503] -- [Create winit canvas under WebAssembly][506] +- [Create winit canvas under WebAssembly][506] - [Implement single threaded task scheduler for WebAssembly][496] - [Support for binary glTF (.glb).][271] - [Support for `Or` in ECS queries.][358] @@ -72,7 +72,7 @@ - [Add `AppBuilder::add_startup_stage_|before/after`][505] ### Changed - + - [Transform rewrite][374] - [Use generational entity ids and other optimizations][504] - [Optimize transform systems to only run on changes.][417] diff --git a/crates/bevy_render/Cargo.toml b/crates/bevy_render/Cargo.toml index 3c1826cda2..63734d83f2 100644 --- a/crates/bevy_render/Cargo.toml +++ b/crates/bevy_render/Cargo.toml @@ -27,7 +27,6 @@ bevy_window = { path = "../bevy_window", version = "0.2.1" } bevy_utils = { path = "../bevy_utils", version = "0.2.1" } # rendering -spirv-reflect = "0.2.3" image = { version = "0.23", default-features = false } # misc @@ -45,7 +44,10 @@ hex = "0.4.2" hexasphere = "1.0.0" parking_lot = "0.11.0" -[target.'cfg(not(target_os = "ios"))'.dependencies] +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +spirv-reflect = "0.2.3" + +[target.'cfg(all(not(target_os = "ios"), not(target_arch = "wasm32")))'.dependencies] bevy-glsl-to-spirv = "0.1.7" [target.'cfg(target_os = "ios")'.dependencies] diff --git a/crates/bevy_render/src/shader/mod.rs b/crates/bevy_render/src/shader/mod.rs index b3fa7d61cc..47a6a0fed3 100644 --- a/crates/bevy_render/src/shader/mod.rs +++ b/crates/bevy_render/src/shader/mod.rs @@ -1,8 +1,26 @@ #[allow(clippy::module_inception)] mod shader; mod shader_defs; + +#[cfg(not(target_arch = "wasm32"))] +mod shader_reflect; + +#[cfg(target_arch = "wasm32")] +#[path = "shader_reflect_wasm.rs"] mod shader_reflect; pub use shader::*; pub use shader_defs::*; pub use shader_reflect::*; + +use crate::pipeline::{BindGroupDescriptor, VertexBufferDescriptor}; + +/// Defines the memory layout of a shader +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ShaderLayout { + pub bind_groups: Vec, + pub vertex_buffer_descriptors: Vec, + pub entry_point: String, +} + +pub const GL_VERTEX_INDEX: &str = "gl_VertexIndex"; diff --git a/crates/bevy_render/src/shader/shader.rs b/crates/bevy_render/src/shader/shader.rs index be05c924b4..1581242617 100644 --- a/crates/bevy_render/src/shader/shader.rs +++ b/crates/bevy_render/src/shader/shader.rs @@ -10,7 +10,7 @@ pub enum ShaderStage { Compute, } -#[cfg(not(target_os = "ios"))] +#[cfg(all(not(target_os = "ios"), not(target_arch = "wasm32")))] impl Into for ShaderStage { fn into(self) -> bevy_glsl_to_spirv::ShaderType { match self { @@ -21,7 +21,7 @@ impl Into for ShaderStage { } } -#[cfg(not(target_os = "ios"))] +#[cfg(all(not(target_os = "ios"), not(target_arch = "wasm32")))] fn glsl_to_spirv( glsl_source: &str, stage: ShaderStage, @@ -116,6 +116,7 @@ impl Shader { } } + #[cfg(not(target_arch = "wasm32"))] pub fn get_spirv(&self, macros: Option<&[String]>) -> Vec { match self.source { ShaderSource::Spirv(ref bytes) => bytes.clone(), @@ -123,9 +124,13 @@ impl Shader { } } + #[allow(unused_variables)] pub fn get_spirv_shader(&self, macros: Option<&[String]>) -> Shader { Shader { + #[cfg(not(target_arch = "wasm32"))] source: ShaderSource::Spirv(self.get_spirv(macros)), + #[cfg(target_arch = "wasm32")] + source: self.source.clone(), stage: self.stage, } } @@ -143,12 +148,31 @@ impl Shader { } /// All stages in a shader program -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Eq, PartialEq, Hash)] pub struct ShaderStages { pub vertex: Handle, pub fragment: Option>, } +pub struct ShaderStagesIterator<'a> { + shader_stages: &'a ShaderStages, + state: u32, +} + +impl<'a> Iterator for ShaderStagesIterator<'a> { + type Item = Handle; + + fn next(&mut self) -> Option { + let ret = match self.state { + 0 => Some(self.shader_stages.vertex), + 1 => self.shader_stages.fragment, + _ => None, + }; + self.state += 1; + ret + } +} + impl ShaderStages { pub fn new(vertex_shader: Handle) -> Self { ShaderStages { @@ -156,4 +180,11 @@ impl ShaderStages { fragment: None, } } + + pub fn iter(&self) -> ShaderStagesIterator { + ShaderStagesIterator { + shader_stages: &self, + state: 0, + } + } } diff --git a/crates/bevy_render/src/shader/shader_reflect.rs b/crates/bevy_render/src/shader/shader_reflect.rs index 9b926433f0..3233818d84 100644 --- a/crates/bevy_render/src/shader/shader_reflect.rs +++ b/crates/bevy_render/src/shader/shader_reflect.rs @@ -3,6 +3,7 @@ use crate::{ BindGroupDescriptor, BindType, BindingDescriptor, BindingShaderStage, InputStepMode, UniformProperty, VertexAttributeDescriptor, VertexBufferDescriptor, VertexFormat, }, + shader::{ShaderLayout, GL_VERTEX_INDEX}, texture::{TextureComponentType, TextureViewDimension}, }; use bevy_core::AsBytes; @@ -16,16 +17,6 @@ use spirv_reflect::{ ShaderModule, }; -/// Defines the memory layout of a shader -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ShaderLayout { - pub bind_groups: Vec, - pub vertex_buffer_descriptors: Vec, - pub entry_point: String, -} - -pub const GL_VERTEX_INDEX: &str = "gl_VertexIndex"; - impl ShaderLayout { pub fn from_spirv(spirv_data: &[u32], bevy_conventions: bool) -> ShaderLayout { match ShaderModule::load_u8_data(spirv_data.as_bytes()) { diff --git a/crates/bevy_render/src/shader/shader_reflect_wasm.rs b/crates/bevy_render/src/shader/shader_reflect_wasm.rs new file mode 100644 index 0000000000..aa40454716 --- /dev/null +++ b/crates/bevy_render/src/shader/shader_reflect_wasm.rs @@ -0,0 +1,7 @@ +use crate::shader::ShaderLayout; + +impl ShaderLayout { + pub fn from_spirv(_spirv_data: &[u32], _bevy_conventions: bool) -> ShaderLayout { + panic!("reflecting shader layout from spirv data is not available"); + } +}