replace glsl_to_spirv with shaderc

This commit is contained in:
Carter Anderson 2020-01-15 00:51:32 -08:00
parent 35705dfad2
commit 50722089ef
7 changed files with 34 additions and 19 deletions

View File

@ -10,7 +10,6 @@ legion_transform = { path = "src/transform" }
wgpu = { git = "https://github.com/gfx-rs/wgpu-rs.git", rev = "4a0da16fe6764c4e1dc918a31cbd7467d404df51"} wgpu = { git = "https://github.com/gfx-rs/wgpu-rs.git", rev = "4a0da16fe6764c4e1dc918a31cbd7467d404df51"}
glam = "0.8.4" glam = "0.8.4"
winit = "0.20.0-alpha6" winit = "0.20.0-alpha6"
glsl-to-spirv = "0.1"
zerocopy = "0.2" zerocopy = "0.2"
log = "0.4" log = "0.4"
env_logger = "0.7" env_logger = "0.7"
@ -21,6 +20,7 @@ serde_json = "1.0"
uuid = { version = "0.8", features = ["v4"] } uuid = { version = "0.8", features = ["v4"] }
erased-serde = "0.3" erased-serde = "0.3"
type-uuid = "0.1" type-uuid = "0.1"
shaderc = "0.6"
[profile.release] [profile.release]
debug = true debug = true

View File

@ -22,9 +22,9 @@ impl ForwardPipeline {
impl Pipeline for ForwardPipeline { impl Pipeline for ForwardPipeline {
fn initialize(&mut self, render_graph: &mut RenderGraphData, _: &mut World) { fn initialize(&mut self, render_graph: &mut RenderGraphData, _: &mut World) {
let vs_bytes = shader::load_glsl(include_str!("forward.vert"), shader::ShaderStage::Vertex); let vs_bytes = shader::glsl_to_spirv(include_str!("forward.vert"), shader::ShaderStage::Vertex);
let fs_bytes = let fs_bytes =
shader::load_glsl(include_str!("forward.frag"), shader::ShaderStage::Fragment); shader::glsl_to_spirv(include_str!("forward.frag"), shader::ShaderStage::Fragment);
let bind_group_layout = let bind_group_layout =
render_graph render_graph

View File

@ -136,11 +136,11 @@ impl ForwardInstancedPipeline {
impl Pipeline for ForwardInstancedPipeline { impl Pipeline for ForwardInstancedPipeline {
fn initialize(&mut self, render_graph: &mut RenderGraphData, world: &mut World) { fn initialize(&mut self, render_graph: &mut RenderGraphData, world: &mut World) {
let vs_bytes = shader::load_glsl( let vs_bytes = shader::glsl_to_spirv(
include_str!("forward_instanced.vert"), include_str!("forward_instanced.vert"),
shader::ShaderStage::Vertex, shader::ShaderStage::Vertex,
); );
let fs_bytes = shader::load_glsl( let fs_bytes = shader::glsl_to_spirv(
include_str!("forward_instanced.frag"), include_str!("forward_instanced.frag"),
shader::ShaderStage::Fragment, shader::ShaderStage::Fragment,
); );

View File

@ -20,11 +20,11 @@ impl ForwardShadowPassNew {
impl Pipeline for ForwardShadowPassNew { impl Pipeline for ForwardShadowPassNew {
fn initialize(&mut self, render_graph: &mut RenderGraphData, _world: &mut World) { fn initialize(&mut self, render_graph: &mut RenderGraphData, _world: &mut World) {
let vs_bytes = shader::load_glsl( let vs_bytes = shader::glsl_to_spirv(
include_str!("forward_shadow.vert"), include_str!("forward_shadow.vert"),
shader::ShaderStage::Vertex, shader::ShaderStage::Vertex,
); );
let fs_bytes = shader::load_glsl( let fs_bytes = shader::glsl_to_spirv(
include_str!("forward_shadow.frag"), include_str!("forward_shadow.frag"),
shader::ShaderStage::Fragment, shader::ShaderStage::Fragment,
); );

View File

@ -102,9 +102,9 @@ impl Pipeline for ShadowPipeline {
let vertex_buffer_descriptor = get_vertex_buffer_descriptor(); let vertex_buffer_descriptor = get_vertex_buffer_descriptor();
// Create the render pipeline // Create the render pipeline
let vs_bytes = shader::load_glsl(include_str!("shadow.vert"), shader::ShaderStage::Vertex); let vs_bytes = shader::glsl_to_spirv(include_str!("shadow.vert"), shader::ShaderStage::Vertex);
let fs_bytes = let fs_bytes =
shader::load_glsl(include_str!("shadow.frag"), shader::ShaderStage::Fragment); shader::glsl_to_spirv(include_str!("shadow.frag"), shader::ShaderStage::Fragment);
let vs_module = render_graph.device.create_shader_module(&vs_bytes); let vs_module = render_graph.device.create_shader_module(&vs_bytes);
let fs_module = render_graph.device.create_shader_module(&fs_bytes); let fs_module = render_graph.device.create_shader_module(&fs_bytes);

View File

@ -95,8 +95,8 @@ impl UiPipeline {
impl Pipeline for UiPipeline { impl Pipeline for UiPipeline {
fn initialize(&mut self, render_graph: &mut RenderGraphData, world: &mut World) { fn initialize(&mut self, render_graph: &mut RenderGraphData, world: &mut World) {
let vs_bytes = shader::load_glsl(include_str!("ui.vert"), shader::ShaderStage::Vertex); let vs_bytes = shader::glsl_to_spirv(include_str!("ui.vert"), shader::ShaderStage::Vertex);
let fs_bytes = shader::load_glsl(include_str!("ui.frag"), shader::ShaderStage::Fragment); let fs_bytes = shader::glsl_to_spirv(include_str!("ui.frag"), shader::ShaderStage::Fragment);
let bind_group_layout = let bind_group_layout =
render_graph render_graph

View File

@ -5,12 +5,27 @@ pub enum ShaderStage {
Compute, Compute,
} }
pub fn load_glsl(code: &str, stage: ShaderStage) -> Vec<u32> { impl Into<shaderc::ShaderKind> for ShaderStage {
let ty = match stage { fn into(self) -> shaderc::ShaderKind {
ShaderStage::Vertex => glsl_to_spirv::ShaderType::Vertex, match self {
ShaderStage::Fragment => glsl_to_spirv::ShaderType::Fragment, ShaderStage::Vertex => shaderc::ShaderKind::Vertex,
ShaderStage::Compute => glsl_to_spirv::ShaderType::Compute, ShaderStage::Fragment => shaderc::ShaderKind::Fragment,
}; ShaderStage::Compute => shaderc::ShaderKind::Compute,
}
wgpu::read_spirv(glsl_to_spirv::compile(&code, ty).unwrap()).unwrap() }
} }
pub fn glsl_to_spirv(glsl_source: &str, stage: ShaderStage) -> Vec<u32> {
let shader_kind: shaderc::ShaderKind = stage.into();
let mut compiler = shaderc::Compiler::new().unwrap();
let options = shaderc::CompileOptions::new().unwrap();
// options.add_macro_definition("EP", Some("main"));
let binary_result = compiler.compile_into_spirv(
glsl_source, shader_kind,
"shader.glsl", "main", Some(&options)).unwrap();
// let text_result = compiler.compile_into_spirv_assembly(
// glsl_source, shader_kind,
// "shader.glsl", "main", Some(&options)).unwrap();
binary_result.as_binary().into()
}