Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not depend on spirv on wasm target #689

Merged
merged 3 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 4 additions & 2 deletions 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 All @@ -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]
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";
37 changes: 34 additions & 3 deletions crates/bevy_render/src/shader/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bevy_glsl_to_spirv::ShaderType> for ShaderStage {
fn into(self) -> bevy_glsl_to_spirv::ShaderType {
match self {
Expand All @@ -21,7 +21,7 @@ impl Into<bevy_glsl_to_spirv::ShaderType> 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,
Expand Down Expand Up @@ -116,16 +116,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 @@ -143,17 +148,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");
}
}