Skip to content

Commit

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

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

# misc
Expand Down
36 changes: 22 additions & 14 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,25 +10,34 @@ pub enum ShaderStage {
Compute,
}

impl Into<bevy_glsl_to_spirv::ShaderType> for ShaderStage {
fn into(self) -> bevy_glsl_to_spirv::ShaderType {
match self {
ShaderStage::Vertex => bevy_glsl_to_spirv::ShaderType::Vertex,
ShaderStage::Fragment => bevy_glsl_to_spirv::ShaderType::Fragment,
ShaderStage::Compute => bevy_glsl_to_spirv::ShaderType::Compute,
}
}
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,
}
}
}

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();
let mut spv_bytes = Vec::new();
output.read_to_end(&mut spv_bytes).unwrap();
bytes_to_words(&spv_bytes)
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> {
Expand Down

0 comments on commit 21a5461

Please sign in to comment.