From 21a54615840d9ccbce4e886177b43dde89f5cc90 Mon Sep 17 00:00:00 2001 From: Michael Hills Date: Sun, 23 Aug 2020 14:19:36 +1000 Subject: [PATCH] Switch over to shaderc-rs for glsl to spirv compilation --- crates/bevy_render/Cargo.toml | 2 +- crates/bevy_render/src/shader/shader.rs | 36 +++++++++++++++---------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/crates/bevy_render/Cargo.toml b/crates/bevy_render/Cargo.toml index 5d7c1324ef8bdc..3ad36f0cb8987b 100644 --- a/crates/bevy_render/Cargo.toml +++ b/crates/bevy_render/Cargo.toml @@ -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 diff --git a/crates/bevy_render/src/shader/shader.rs b/crates/bevy_render/src/shader/shader.rs index f963c0c90aa553..2e710cd0ae7b0d 100644 --- a/crates/bevy_render/src/shader/shader.rs +++ b/crates/bevy_render/src/shader/shader.rs @@ -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)] @@ -11,14 +10,14 @@ pub enum ShaderStage { Compute, } -impl Into 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 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( @@ -26,10 +25,19 @@ fn glsl_to_spirv( stage: ShaderStage, shader_defs: Option<&[String]>, ) -> Vec { - 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 {