1
0
Fork 0
forked from wry/wry

vulkan: add legacy shaders

This commit is contained in:
Julian Orth 2025-02-27 11:10:50 +01:00
parent 26db79e249
commit cb9dc4c182
9 changed files with 108 additions and 11 deletions

View file

@ -15,16 +15,22 @@ pub fn main() -> anyhow::Result<()> {
compile_simple("tex.frag")?;
compile_simple("out.vert")?;
compile_simple("out.frag")?;
compile_simple("legacy/fill.frag")?;
compile_simple("legacy/fill.vert")?;
compile_simple("legacy/tex.vert")?;
compile_simple("legacy/tex.frag")?;
Ok(())
}
fn compile_simple(name: &str) -> anyhow::Result<()> {
compile_shader(name, &format!("{name}.spv"), None).with_context(|| name.to_string())
let out = format!("{name}.spv").replace("/", "_");
compile_shader(name, &out).with_context(|| name.to_string())
}
fn compile_shader(name: &str, out: &str, options: Option<CompileOptions>) -> anyhow::Result<()> {
let read = |path: &str| std::fs::read_to_string(format!("{}/{}", ROOT, path));
let mut options = options.unwrap_or_else(|| CompileOptions::new().unwrap());
fn compile_shader(name: &str, out: &str) -> anyhow::Result<()> {
let root = Path::new(ROOT).join(Path::new(name).parent().unwrap());
let read = |path: &str| std::fs::read_to_string(root.join(path));
let mut options = CompileOptions::new().unwrap();
options.set_include_callback(|name, _, _, _| {
Ok(ResolvedInclude {
resolved_name: name.to_string(),
@ -40,7 +46,7 @@ fn compile_shader(name: &str, out: &str, options: Option<CompileOptions>) -> any
"vert" => shaderc::ShaderKind::Vertex,
n => bail!("Unknown shader stage {}", n),
};
let src = read(name)?;
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", Some(&options))

View file

@ -22,8 +22,9 @@ use {
sampler::VulkanSampler,
semaphore::VulkanSemaphore,
shaders::{
FILL_FRAG, FILL_VERT, FillPushConstants, OUT_FRAG, OUT_VERT, OutPushConstants,
TEX_FRAG, TEX_VERT, TexPushConstants, VulkanShader,
FILL_FRAG, FILL_VERT, FillPushConstants, LEGACY_FILL_FRAG, LEGACY_FILL_VERT,
LEGACY_TEX_FRAG, LEGACY_TEX_VERT, OUT_FRAG, OUT_VERT, OutPushConstants, TEX_FRAG,
TEX_VERT, TexPushConstants, VulkanShader,
},
},
io_uring::IoUring,
@ -195,8 +196,21 @@ impl VulkanDevice {
eng: &Rc<AsyncEngine>,
ring: &Rc<IoUring>,
) -> Result<Rc<VulkanRenderer>, VulkanError> {
let fill_vert_shader = self.create_shader(FILL_VERT)?;
let fill_frag_shader = self.create_shader(FILL_FRAG)?;
let fill_vert_shader;
let fill_frag_shader;
let tex_vert_shader;
let tex_frag_shader;
if self.descriptor_buffer.is_some() {
tex_vert_shader = self.create_shader(TEX_VERT)?;
tex_frag_shader = self.create_shader(TEX_FRAG)?;
fill_vert_shader = self.create_shader(FILL_VERT)?;
fill_frag_shader = self.create_shader(FILL_FRAG)?;
} else {
tex_vert_shader = self.create_shader(LEGACY_TEX_VERT)?;
tex_frag_shader = self.create_shader(LEGACY_TEX_FRAG)?;
fill_vert_shader = self.create_shader(LEGACY_FILL_VERT)?;
fill_frag_shader = self.create_shader(LEGACY_FILL_FRAG)?;
}
let sampler = self.create_sampler()?;
let tex_descriptor_set_layout = self.create_tex_descriptor_set_layout(&sampler)?;
let out_descriptor_set_layout = self
@ -206,8 +220,6 @@ impl VulkanDevice {
.transpose()?;
let out_vert_shader = self.create_shader(OUT_VERT)?;
let out_frag_shader = self.create_shader(OUT_FRAG)?;
let tex_vert_shader = self.create_shader(TEX_VERT)?;
let tex_frag_shader = self.create_shader(TEX_FRAG)?;
let gfx_command_buffers = self.create_command_pool(self.graphics_queue_idx)?;
let transfer_command_buffers = self
.distinct_transfer_queue_family_idx

View file

@ -11,6 +11,12 @@ pub const TEX_VERT: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/tex.vert.s
pub const TEX_FRAG: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/tex.frag.spv"));
pub const OUT_VERT: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/out.vert.spv"));
pub const OUT_FRAG: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/out.frag.spv"));
pub const LEGACY_FILL_VERT: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/legacy_fill.vert.spv"));
pub const LEGACY_FILL_FRAG: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/legacy_fill.frag.spv"));
pub const LEGACY_TEX_VERT: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/legacy_tex.vert.spv"));
pub const LEGACY_TEX_FRAG: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/legacy_tex.frag.spv"));
pub struct VulkanShader {
pub(super) device: Rc<VulkanDevice>,

View file

@ -0,0 +1,4 @@
layout(push_constant, std430) uniform Data {
layout(offset = 0) vec2 pos[4];
layout(offset = 32) vec4 color;
} data;

View file

@ -0,0 +1,10 @@
#version 450
#include "../frag_spec_const.glsl"
#include "fill.common.glsl"
layout(location = 0) out vec4 out_color;
void main() {
out_color = data.color;
}

View file

@ -0,0 +1,16 @@
#version 450
//#extension GL_EXT_debug_printf : enable
#include "fill.common.glsl"
void main() {
vec2 pos;
switch (gl_VertexIndex) {
case 0: pos = data.pos[0]; break;
case 1: pos = data.pos[1]; break;
case 2: pos = data.pos[2]; break;
case 3: pos = data.pos[3]; break;
}
gl_Position = vec4(pos, 0.0, 1.0);
// debugPrintfEXT("gl_Position = %v4f", gl_Position);
}

View file

@ -0,0 +1,5 @@
layout(push_constant, std430) uniform Data {
layout(offset = 0) vec2 pos[4];
layout(offset = 32) vec2 tex_pos[4];
layout(offset = 64) float mul;
} data;

View file

@ -0,0 +1,20 @@
#version 450
#include "../frag_spec_const.glsl"
#include "tex.common.glsl"
layout(set = 0, binding = 0) uniform sampler2D tex;
layout(location = 0) in vec2 tex_pos;
layout(location = 0) out vec4 out_color;
void main() {
vec4 c = textureLod(tex, tex_pos, 0);
if (has_alpha_multiplier) {
if (src_has_alpha) {
c *= data.mul;
} else {
c = vec4(c.rgb * data.mul, data.mul);
}
}
out_color = c;
}

View file

@ -0,0 +1,18 @@
#version 450
//#extension GL_EXT_debug_printf : enable
#include "tex.common.glsl"
layout(location = 0) out vec2 tex_pos;
void main() {
vec2 pos;
switch (gl_VertexIndex) {
case 0: pos = data.pos[0]; tex_pos = data.tex_pos[0]; break;
case 1: pos = data.pos[1]; tex_pos = data.tex_pos[1]; break;
case 2: pos = data.pos[2]; tex_pos = data.tex_pos[2]; break;
case 3: pos = data.pos[3]; tex_pos = data.tex_pos[3]; break;
}
gl_Position = vec4(pos, 0.0, 1.0);
// debugPrintfEXT("gl_Position = %v4f, tex_pos = %v2f", gl_Position, tex_pos);
}