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

iOS: use shaderc-rs for glsl to spirv compilation #324

Merged
merged 1 commit into from
Sep 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion crates/bevy_render/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ bevy_utils = { path = "../bevy_utils", 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 @@ -43,6 +42,12 @@ hex = "0.4.2"
hexasphere = "1.0.0"
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