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

Replace glsl-to-spirv with shaderc #2731

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ before_install:
# see https://docs.travis-ci.com/user/trusty-ci-environment/
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then export DISPLAY=:99.0 && sh -e /etc/init.d/xvfb start && make travis-sdl2 && export CXX=g++-5; fi
- if [[ $TRAVIS_OS_NAME == "osx" ]]; then brew update && brew install sdl2; fi
- if [[ $TRAVIS_OS_NAME == "windows" ]]; then choco install make; fi
- if [[ $TRAVIS_OS_NAME == "windows" ]]; then choco install make ninja; fi
- rustup self update
- rustup target add $TARGET; true

Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ env_logger = "0.5"
image = "0.19"
log = "0.4"
winit = "0.18"
glsl-to-spirv = "0.1.4"
gfx-hal = { path = "../src/hal", version = "0.1" }
gfx-backend-empty = { path = "../src/backend/empty", version = "0.1" }
shaderc = "0.3.16"

[dependencies.gfx-backend-gl]
path = "../src/backend/gl"
Expand Down
38 changes: 25 additions & 13 deletions examples/colour-uniform/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ extern crate gfx_backend_vulkan as back;
extern crate log;
extern crate env_logger;
extern crate gfx_hal as hal;
extern crate glsl_to_spirv;
extern crate image;
extern crate shaderc;
extern crate winit;

struct Dimensions<T> {
Expand Down Expand Up @@ -1321,25 +1321,37 @@ impl<B: Backend> PipelineState<B> {
.create_pipeline_layout(desc_layouts, &[(pso::ShaderStageFlags::VERTEX, 0..8)])
.expect("Can't create pipeline layout");

let mut shader_compiler = shaderc::Compiler::new().unwrap();

let pipeline = {
let vs_module = {
let glsl = fs::read_to_string("colour-uniform/data/quad.vert").unwrap();
let spirv: Vec<u8> =
glsl_to_spirv::compile(&glsl, glsl_to_spirv::ShaderType::Vertex)
.unwrap()
.bytes()
.map(|b| b.unwrap())
.collect();
let spirv: Vec<u8> = shader_compiler
.compile_into_spirv(
&glsl,
shaderc::ShaderKind::Vertex,
"quad.vert",
"main",
None,
)
.unwrap()
.as_binary_u8()
.to_vec();
device.create_shader_module(&spirv).unwrap()
};
let fs_module = {
let glsl = fs::read_to_string("colour-uniform/data/quad.frag").unwrap();
let spirv: Vec<u8> =
glsl_to_spirv::compile(&glsl, glsl_to_spirv::ShaderType::Fragment)
.unwrap()
.bytes()
.map(|b| b.unwrap())
.collect();
let spirv: Vec<u8> = shader_compiler
.compile_into_spirv(
&glsl,
shaderc::ShaderKind::Fragment,
"quad.frag",
"main",
None,
)
.unwrap()
.as_binary_u8()
.to_vec();
device.create_shader_module(&spirv).unwrap()
};

Expand Down
18 changes: 13 additions & 5 deletions examples/compute/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::str::FromStr;
use hal::{buffer, command, memory, pool, pso};
use hal::{Backend, Compute, DescriptorPool, Device, Instance, PhysicalDevice, QueueFamily};

extern crate glsl_to_spirv;
extern crate shaderc;

use std::fs;
use std::io::Read;
Expand Down Expand Up @@ -53,12 +53,20 @@ fn main() {
let memory_properties = adapter.physical_device.memory_properties();
let (device, mut queue_group) = adapter.open_with::<_, Compute>(1, |_family| true).unwrap();

let mut shader_compiler = shaderc::Compiler::new().unwrap();

let glsl = fs::read_to_string("compute/shader/collatz.comp").unwrap();
let spirv: Vec<u8> = glsl_to_spirv::compile(&glsl, glsl_to_spirv::ShaderType::Compute)
let spirv: Vec<u8> = shader_compiler
.compile_into_spirv(
&glsl,
shaderc::ShaderKind::Compute,
"collatz.comp",
"main",
None,
)
.unwrap()
.bytes()
.map(|b| b.unwrap())
.collect();
.as_binary_u8()
.to_vec();
let shader = unsafe { device.create_shader_module(&spirv) }.unwrap();

let (pipeline_layout, pipeline, set_layout, mut desc_pool) = {
Expand Down
32 changes: 23 additions & 9 deletions examples/quad/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ extern crate gfx_backend_metal as back;
extern crate gfx_backend_vulkan as back;
extern crate gfx_hal as hal;

extern crate glsl_to_spirv;
extern crate image;
extern crate shaderc;
extern crate winit;

use hal::format::{AsFormat, ChannelType, Rgba8Srgb as ColorFormat, Swizzle};
Expand Down Expand Up @@ -509,6 +509,8 @@ fn main() {
cmd_buffers.push(cmd_pools[i].acquire_command_buffer::<command::MultiShot>());
}

let mut shader_compiler = shaderc::Compiler::new().unwrap();

let pipeline_layout = unsafe {
device.create_pipeline_layout(
std::iter::once(&set_layout),
Expand All @@ -519,20 +521,32 @@ fn main() {
let pipeline = {
let vs_module = {
let glsl = fs::read_to_string("quad/data/quad.vert").unwrap();
let spirv: Vec<u8> = glsl_to_spirv::compile(&glsl, glsl_to_spirv::ShaderType::Vertex)
let spirv: Vec<u8> = shader_compiler
.compile_into_spirv(
&glsl,
shaderc::ShaderKind::Vertex,
"quad.vert",
"main",
None,
)
.unwrap()
.bytes()
.map(|b| b.unwrap())
.collect();
.as_binary_u8()
.to_vec();
unsafe { device.create_shader_module(&spirv) }.unwrap()
};
let fs_module = {
let glsl = fs::read_to_string("quad/data/quad.frag").unwrap();
let spirv: Vec<u8> = glsl_to_spirv::compile(&glsl, glsl_to_spirv::ShaderType::Fragment)
let spirv: Vec<u8> = shader_compiler
.compile_into_spirv(
&glsl,
shaderc::ShaderKind::Fragment,
"quad.frag",
"main",
None,
)
.unwrap()
.bytes()
.map(|b| b.unwrap())
.collect();
.as_binary_u8()
.to_vec();
unsafe { device.create_shader_module(&spirv) }.unwrap()
};

Expand Down
8 changes: 4 additions & 4 deletions src/warden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ edition = "2018"
name = "gfx_warden"

[features]
default = ["glsl-to-spirv"]
default = ["shaderc"]
vulkan = ["gfx-backend-vulkan"]
dx12 = ["gfx-backend-dx12"]
metal = ["gfx-backend-metal"]
gl = ["gfx-backend-gl"]
gl-headless = ["gfx-backend-gl"] # "glsl-to-spirv"
gl-headless = ["gfx-backend-gl"] # "shaderc"

#TODO: keep Warden backend-agnostic?

Expand All @@ -33,7 +33,7 @@ log = "0.4"
ron = "0.2.1"
serde = { version = "1", features = ["serde_derive"] }
env_logger = { version = "0.5", optional = true }
glsl-to-spirv = { version = "0.1", optional = true }
shaderc = { version = "0.3.16", optional = true }

[dependencies.gfx-backend-vulkan]
path = "../../src/backend/vulkan"
Expand All @@ -59,4 +59,4 @@ optional = true

[[example]]
name = "basic"
required-features = ["gl", "glsl-to-spirv"]
required-features = ["gl", "shaderc"]
2 changes: 1 addition & 1 deletion src/warden/src/bin/reftest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Harness {
//println!("\t{:?}", adapter.info);
println!("\tScene '{}':", tg.name);

#[cfg(not(feature = "glsl-to-spirv"))]
#[cfg(not(feature = "shaderc"))]
{
let all_spirv = tg.scene.resources.values().all(|res| match *res {
warden::raw::Resource::Shader(ref name) => name.ends_with(".spirv"),
Expand Down
63 changes: 43 additions & 20 deletions src/warden/src/gpu.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use failure::Error;
#[cfg(feature = "glsl-to-spirv")]
use glsl_to_spirv;
#[cfg(feature = "shaderc")]
use shaderc;

use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::{iter, slice};

use crate::hal::{self, buffer as b, command as c, format as f, image as i, memory, pso};
Expand Down Expand Up @@ -196,6 +197,8 @@ impl<B: hal::Backend> Scene<B, hal::General> {
unsafe {
init_cmd.begin(false);
}
#[cfg(feature = "shaderc")]
let mut shader_compiler = shaderc::Compiler::new().unwrap();
// Pass[1]: images, buffers, passes, descriptor set layouts/pools
for (name, resource) in &raw.resources {
match *resource {
Expand Down Expand Up @@ -559,26 +562,46 @@ impl<B: hal::Backend> Scene<B, hal::General> {
resources.render_passes.insert(name.clone(), rp);
}
raw::Resource::Shader(ref local_path) => {
#[cfg(feature = "glsl-to-spirv")]
fn transpile(mut file: File, ty: glsl_to_spirv::ShaderType) -> File {
let mut code = String::new();
file.read_to_string(&mut code).unwrap();
glsl_to_spirv::compile(&code, ty).unwrap()
}
let full_path = data_path.join(local_path);
let base_file = File::open(&full_path).unwrap();
let mut file = match &*full_path.extension().unwrap().to_string_lossy() {
"spirv" => base_file,
#[cfg(feature = "glsl-to-spirv")]
"vert" => transpile(base_file, glsl_to_spirv::ShaderType::Vertex),
#[cfg(feature = "glsl-to-spirv")]
"frag" => transpile(base_file, glsl_to_spirv::ShaderType::Fragment),
#[cfg(feature = "glsl-to-spirv")]
"comp" => transpile(base_file, glsl_to_spirv::ShaderType::Compute),
#[cfg(feature = "shaderc")]
fn transpile<P: AsRef<Path>>(
compiler: &mut shaderc::Compiler,
full_path: P,
local_path: &str,
ty: shaderc::ShaderKind,
) -> Vec<u8> {
let code = fs::read_to_string(full_path).unwrap();
compiler
.compile_into_spirv(&code, ty, local_path, "main", None)
.unwrap()
.as_binary_u8()
.to_vec()
}
let spirv: Vec<u8> = match &*full_path.extension().unwrap().to_string_lossy() {
"spirv" => fs::read(&full_path).unwrap(),
#[cfg(feature = "shaderc")]
"vert" => transpile(
&mut shader_compiler,
&full_path,
&local_path,
shaderc::ShaderKind::Vertex,
),
#[cfg(feature = "shaderc")]
"frag" => transpile(
&mut shader_compiler,
&full_path,
&local_path,
shaderc::ShaderKind::Fragment,
),
#[cfg(feature = "shaderc")]
"comp" => transpile(
&mut shader_compiler,
&full_path,
&local_path,
shaderc::ShaderKind::Compute,
),
other => panic!("Unknown shader extension: {}", other),
};
let mut spirv = Vec::new();
file.read_to_end(&mut spirv).unwrap();
let module = unsafe { device.create_shader_module(&spirv) }.unwrap();
resources.shaders.insert(name.clone(), module);
}
Expand Down
4 changes: 2 additions & 2 deletions src/warden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ extern crate log;
#[macro_use]
extern crate serde;
extern crate failure;
#[cfg(feature = "glsl-to-spirv")]
extern crate glsl_to_spirv;
#[cfg(feature = "shaderc")]
extern crate shaderc;

pub mod gpu;
pub mod raw;