Skip to content

Commit

Permalink
iOS: use shaderc-rs for glsl to spirv compilation (bevyengine#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHills authored and mrk-its committed Oct 6, 2020
1 parent 0ea5f15 commit 7b2da82
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
12 changes: 9 additions & 3 deletions crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ image = { version = "0.23", default-features = false }
log = { version = "0.4", features = ["release_max_level_info"] }
uuid = { version = "0.8", features = ["v4", "serde"] }
serde = { version = "1", features = ["derive"] }
bitflags = "1.0"
bitflags = "1.2.1"
smallvec = "1.4.0"
# TODO: replace once_cell with std equivalent if/when this lands: https://github.com/rust-lang/rfcs/pull/2788
once_cell = "1.4.0"
downcast-rs = "1.1.1"
downcast-rs = "1.2.0"
thiserror = "1.0"
anyhow = "1.0"
hex = "0.4.2"
hexasphere = "1.0.0"
parking_lot = "0.10"
parking_lot = "0.11.0"

[dependencies.naga]
# Temporarily git, either get kvark to publish a new version, or mirror
Expand All @@ -53,6 +53,12 @@ features = ["glsl-new", "spirv"]
[dev-dependencies]
pretty_assertions = "0.6.1"

# [target.'cfg(not(target_os = "ios"))'.dependencies]
# bevy-glsl-to-spirv = "0.1.7"

# [target.'cfg(target_os = "ios")'.dependencies]
# shaderc = "0.6.2"

[features]
# default = ["bevy-glsl-to-spirv", "spirv-reflect"]
default = ["naga-glsl", "naga-reflect"]
Expand Down
39 changes: 39 additions & 0 deletions crates/bevy_render/src/shader/shader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl Into<bevy_glsl_to_spirv::ShaderType> for ShaderStage {
}
}

#[cfg(not(target_os = "ios"))]
fn glsl_to_spirv(
glsl_source: &str,
stage: ShaderStage,
Expand Down Expand Up @@ -80,6 +81,44 @@ fn glsl_to_spirv(
}
}

#[cfg(target_os = "ios")]
impl Into<shaderc::ShaderKind> for ShaderStage {
fn into(self) -> shaderc::ShaderKind {
match self {
ShaderStage::Vertex => shaderc::ShaderKind::Vertex,
ShaderStage::Fragment => shaderc::ShaderKind::Fragment,
ShaderStage::Compute => shaderc::ShaderKind::Compute,
}
}
}

#[cfg(target_os = "ios")]
fn glsl_to_spirv(
glsl_source: &str,
stage: ShaderStage,
shader_defs: Option<&[String]>,
) -> Vec<u32> {
let mut compiler = shaderc::Compiler::new().unwrap();
let mut options = shaderc::CompileOptions::new().unwrap();
if let Some(shader_defs) = shader_defs {
for def in shader_defs.iter() {
options.add_macro_definition(def, None);
}
}

let binary_result = compiler
.compile_into_spirv(
glsl_source,
stage.into(),
"shader.glsl",
"main",
Some(&options),
)
.unwrap();

binary_result.as_binary().to_vec()
}

fn bytes_to_words(bytes: &[u8]) -> Vec<u32> {
let mut words = Vec::new();
for bytes4 in bytes.chunks(4) {
Expand Down

0 comments on commit 7b2da82

Please sign in to comment.