Skip to content

Commit

Permalink
iOS: use shaderc-rs for glsl to spirv compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHills committed Aug 25, 2020
1 parent d00ce1c commit 3192405
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
7 changes: 6 additions & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ bevy_window = { path = "../bevy_window", version = "0.1" }

# rendering
spirv-reflect = "0.2.3"
bevy-glsl-to-spirv = "0.1.7"
image = { version = "0.23", default-features = false }

# misc
Expand All @@ -41,6 +40,12 @@ anyhow = "1.0"
hexasphere = "0.1.5"
parking_lot = "0.10"

[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]
png = ["image/png"]
hdr = ["image/hdr"]
47 changes: 44 additions & 3 deletions crates/bevy_render/src/shader/shader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::ShaderLayout;
use bevy_asset::Handle;
use bevy_glsl_to_spirv::compile;
use std::{io::Read, marker::Copy};
use std::marker::Copy;

/// The stage of a shader
#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug)]
Expand All @@ -11,6 +10,7 @@ pub enum ShaderStage {
Compute,
}

#[cfg(not(target_os = "ios"))]
impl Into<bevy_glsl_to_spirv::ShaderType> for ShaderStage {
fn into(self) -> bevy_glsl_to_spirv::ShaderType {
match self {
Expand All @@ -21,17 +21,58 @@ impl Into<bevy_glsl_to_spirv::ShaderType> for ShaderStage {
}
}

#[cfg(not(target_os = "ios"))]
fn glsl_to_spirv(
glsl_source: &str,
stage: ShaderStage,
shader_defs: Option<&[String]>,
) -> Vec<u32> {
let mut output = compile(glsl_source, stage.into(), shader_defs).unwrap();
use std::io::Read;

let mut output = bevy_glsl_to_spirv::compile(glsl_source, stage.into(), shader_defs).unwrap();
let mut spv_bytes = Vec::new();
output.read_to_end(&mut spv_bytes).unwrap();
bytes_to_words(&spv_bytes)
}

#[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 3192405

Please sign in to comment.