1
0
Fork 0
forked from wry/wry

wayland: implement alpha_modifier_v1

This commit is contained in:
Julian Orth 2024-03-18 19:18:30 +01:00
parent 131f0481e8
commit ff54a8ab96
37 changed files with 655 additions and 89 deletions

View file

@ -1,6 +1,7 @@
use {
crate::open,
anyhow::{bail, Context},
shaderc::CompileOptions,
std::{io::Write, path::Path},
};
@ -8,15 +9,32 @@ const ROOT: &str = "src/gfx_apis/vulkan/shaders";
pub fn main() -> anyhow::Result<()> {
println!("cargo:rerun-if-changed={}", ROOT);
for shader in std::fs::read_dir(ROOT)? {
let shader = shader?;
let name = shader.file_name().to_string_lossy().into_owned();
compile_shader(&name).context(name)?;
}
compile_simple("fill.frag")?;
compile_simple("fill.vert")?;
compile_simple("tex.vert")?;
compile_tex_frag("tex.frag.spv", false, false)?;
compile_tex_frag("tex.frag.mult+opaque.spv", false, true)?;
compile_tex_frag("tex.frag.mult+alpha.spv", true, true)?;
Ok(())
}
fn compile_shader(name: &str) -> anyhow::Result<()> {
fn compile_tex_frag(out: &str, alpha: bool, alpha_multiplier: bool) -> anyhow::Result<()> {
let mut opts = CompileOptions::new().unwrap();
if alpha {
opts.add_macro_definition("ALPHA", None);
}
if alpha_multiplier {
opts.add_macro_definition("ALPHA_MULTIPLIER", None);
}
compile_shader("tex.frag", out, Some(&opts)).with_context(|| out.to_string())?;
Ok(())
}
fn compile_simple(name: &str) -> anyhow::Result<()> {
compile_shader(name, &format!("{name}.spv"), None).with_context(|| name.to_string())
}
fn compile_shader(name: &str, out: &str, options: Option<&CompileOptions>) -> anyhow::Result<()> {
let stage = match Path::new(name)
.extension()
.and_then(|e| e.to_str())
@ -29,9 +47,9 @@ fn compile_shader(name: &str) -> anyhow::Result<()> {
let src = std::fs::read_to_string(format!("{}/{}", ROOT, name))?;
let compiler = shaderc::Compiler::new().unwrap();
let binary = compiler
.compile_into_spirv(&src, stage, name, "main", None)
.compile_into_spirv(&src, stage, name, "main", options)
.unwrap();
let mut file = open(&format!("{}.spv", name))?;
let mut file = open(out)?;
file.write_all(binary.as_binary_u8())?;
file.flush()?;
Ok(())