Skip to content

Commit

Permalink
do not depend on spirv on wasm target (bevyengine#689)
Browse files Browse the repository at this point in the history
do not use spirv for wasm target
  • Loading branch information
joshuajbouw committed Oct 24, 2020
1 parent 3dc2a54 commit e5750da
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 17 deletions.
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions crates/bevy_render/src/shader/mod.rs
Original file line number Diff line number Diff line change
@@ -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<BindGroupDescriptor>,
pub vertex_buffer_descriptors: Vec<VertexBufferDescriptor>,
pub entry_point: String,
}

pub const GL_VERTEX_INDEX: &str = "gl_VertexIndex";
33 changes: 32 additions & 1 deletion crates/bevy_render/src/shader/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,21 @@ impl Shader {
}
}

#[cfg(not(target_arch = "wasm32"))]
pub fn get_spirv(&self, macros: Option<&[String]>) -> Vec<u32> {
match self.source {
ShaderSource::Spirv(ref bytes) => bytes.clone(),
ShaderSource::Glsl(ref source) => glsl_to_spirv(&source, self.stage, macros),
}
}

#[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,
}
}
Expand All @@ -118,17 +123,43 @@ impl Shader {
}

/// All stages in a shader program
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct ShaderStages {
pub vertex: Handle<Shader>,
pub fragment: Option<Handle<Shader>>,
}

pub struct ShaderStagesIterator<'a> {
shader_stages: &'a ShaderStages,
state: u32,
}

impl<'a> Iterator for ShaderStagesIterator<'a> {
type Item = Handle<Shader>;

fn next(&mut self) -> Option<Self::Item> {
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<Shader>) -> Self {
ShaderStages {
vertex: vertex_shader,
fragment: None,
}
}

pub fn iter(&self) -> ShaderStagesIterator {
ShaderStagesIterator {
shader_stages: &self,
state: 0,
}
}
}
11 changes: 1 addition & 10 deletions crates/bevy_render/src/shader/shader_reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<BindGroupDescriptor>,
pub vertex_buffer_descriptors: Vec<VertexBufferDescriptor>,
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()) {
Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_render/src/shader/shader_reflect_wasm.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit e5750da

Please sign in to comment.