use anyhow::{Context, Result};
use par_term_emu_core_rust::cursor::CursorStyle;
use std::path::Path;
use std::time::Instant;
use wgpu::*;
mod cubemap;
mod cursor;
mod hot_reload;
pub mod pipeline;
pub mod textures;
pub mod transpiler;
pub mod types;
mod uniforms;
use cubemap::CubemapTexture;
use pipeline::{create_bind_group, create_bind_group_layout, create_render_pipeline};
use textures::{ChannelTexture, load_channel_textures};
use transpiler::transpile_glsl_to_wgsl;
pub struct CustomShaderRenderer {
pub(crate) pipeline: RenderPipeline,
pub(crate) bind_group: BindGroup,
pub(crate) uniform_buffer: Buffer,
pub(crate) intermediate_texture: Texture,
pub(crate) intermediate_texture_view: TextureView,
pub(crate) start_time: Instant,
pub(crate) animation_enabled: bool,
pub(crate) animation_speed: f32,
pub(crate) texture_width: u32,
pub(crate) texture_height: u32,
pub(crate) surface_format: TextureFormat,
pub(crate) bind_group_layout: BindGroupLayout,
pub(crate) sampler: Sampler,
pub(crate) scale_factor: f32,
pub(crate) window_opacity: f32,
pub(crate) keep_text_opaque: bool,
pub(crate) full_content_mode: bool,
pub(crate) brightness: f32,
pub(crate) frame_count: u32,
pub(crate) last_frame_time: Instant,
pub(crate) mouse_position: [f32; 2],
pub(crate) mouse_click_position: [f32; 2],
pub(crate) mouse_button_down: bool,
pub(crate) frame_time_accumulator: f32,
pub(crate) frames_in_second: u32,
pub(crate) current_frame_rate: f32,
pub(crate) current_cursor_pos: (usize, usize),
pub(crate) previous_cursor_pos: (usize, usize),
pub(crate) current_cursor_color: [f32; 4],
pub(crate) previous_cursor_color: [f32; 4],
pub(crate) current_cursor_opacity: f32,
pub(crate) previous_cursor_opacity: f32,
pub(crate) cursor_change_time: f32,
pub(crate) current_cursor_style: CursorStyle,
pub(crate) previous_cursor_style: CursorStyle,
pub(crate) cursor_cell_width: f32,
pub(crate) cursor_cell_height: f32,
pub(crate) cursor_window_padding: f32,
pub(crate) cursor_content_offset_y: f32,
pub(crate) cursor_content_offset_x: f32,
pub(crate) cursor_shader_color: [f32; 4],
pub(crate) cursor_trail_duration: f32,
pub(crate) cursor_glow_radius: f32,
pub(crate) cursor_glow_intensity: f32,
pub(crate) key_press_time: f32,
pub(crate) channel_textures: [ChannelTexture; 4],
pub(crate) cubemap: CubemapTexture,
pub(crate) use_background_as_channel0: bool,
pub(crate) background_channel_texture: Option<ChannelTexture>,
pub(crate) background_color: [f32; 4],
pub(crate) progress_data: [f32; 4],
pub(crate) content_inset_right: f32,
}
pub struct CustomShaderRendererConfig<'a> {
pub surface_format: TextureFormat,
pub shader_path: &'a Path,
pub width: u32,
pub height: u32,
pub animation_enabled: bool,
pub animation_speed: f32,
pub window_opacity: f32,
pub full_content_mode: bool,
pub channel_paths: &'a [Option<std::path::PathBuf>; 4],
pub cubemap_path: Option<&'a Path>,
}
impl CustomShaderRenderer {
pub fn new(
device: &Device,
queue: &Queue,
config: CustomShaderRendererConfig<'_>,
) -> Result<Self> {
let CustomShaderRendererConfig {
surface_format,
shader_path,
width,
height,
animation_enabled,
animation_speed,
window_opacity,
full_content_mode,
channel_paths,
cubemap_path,
} = config;
let glsl_source = std::fs::read_to_string(shader_path)
.with_context(|| format!("Failed to read shader file: {}", shader_path.display()))?;
let wgsl_source = transpile_glsl_to_wgsl(&glsl_source, shader_path)?;
log::info!(
"Loaded custom shader from {} ({} bytes GLSL -> {} bytes WGSL)",
shader_path.display(),
glsl_source.len(),
wgsl_source.len()
);
log::debug!("Generated WGSL:\n{}", wgsl_source);
let shader_name = shader_path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("unknown");
let debug_filename = format!("/tmp/par_term_{}_shader.wgsl", shader_name);
if let Err(e) = std::fs::write(&debug_filename, &wgsl_source) {
log::warn!("Failed to write debug shader: {}", e);
} else {
log::info!("Wrote debug shader to {}", debug_filename);
}
let module = naga::front::wgsl::parse_str(&wgsl_source)
.context("Custom shader WGSL parse failed")?;
let _info = naga::valid::Validator::new(
naga::valid::ValidationFlags::all(),
naga::valid::Capabilities::empty(),
)
.validate(&module)
.context("Custom shader WGSL validation failed")?;
let shader_module = device.create_shader_module(ShaderModuleDescriptor {
label: Some("Custom Shader Module"),
source: ShaderSource::Wgsl(wgsl_source.clone().into()),
});
let (intermediate_texture, intermediate_texture_view) =
Self::create_intermediate_texture(device, surface_format, width, height);
let sampler = device.create_sampler(&SamplerDescriptor {
label: Some("Custom Shader Sampler"),
address_mode_u: AddressMode::ClampToEdge,
address_mode_v: AddressMode::ClampToEdge,
address_mode_w: AddressMode::ClampToEdge,
mag_filter: FilterMode::Nearest,
min_filter: FilterMode::Nearest,
mipmap_filter: FilterMode::Nearest,
..Default::default()
});
let channel_textures = load_channel_textures(device, queue, channel_paths);
let cubemap = match cubemap_path {
Some(path) => match CubemapTexture::from_prefix(device, queue, path) {
Ok(cm) => cm,
Err(e) => {
log::error!("Failed to load cubemap '{}': {}", path.display(), e);
CubemapTexture::placeholder(device, queue)
}
},
None => CubemapTexture::placeholder(device, queue),
};
let uniform_buffer = Self::create_uniform_buffer(device);
let bind_group_layout = create_bind_group_layout(device);
let bind_group = create_bind_group(
device,
&bind_group_layout,
&uniform_buffer,
&intermediate_texture_view,
&sampler,
&channel_textures,
&cubemap,
);
let pipeline = create_render_pipeline(
device,
&shader_module,
&bind_group_layout,
surface_format,
Some("Custom Shader Pipeline"),
);
let now = Instant::now();
Ok(Self {
pipeline,
bind_group,
uniform_buffer,
intermediate_texture,
intermediate_texture_view,
start_time: now,
animation_enabled,
animation_speed,
texture_width: width,
texture_height: height,
surface_format,
bind_group_layout,
sampler,
window_opacity,
keep_text_opaque: false,
scale_factor: 1.0,
full_content_mode,
brightness: 1.0,
frame_count: 0,
last_frame_time: now,
mouse_position: [0.0, 0.0],
mouse_click_position: [0.0, 0.0],
mouse_button_down: false,
frame_time_accumulator: 0.0,
frames_in_second: 0,
current_frame_rate: 60.0,
current_cursor_pos: (0, 0),
previous_cursor_pos: (0, 0),
current_cursor_color: [1.0, 1.0, 1.0, 1.0],
previous_cursor_color: [1.0, 1.0, 1.0, 1.0],
current_cursor_opacity: 1.0,
previous_cursor_opacity: 1.0,
cursor_change_time: 0.0,
current_cursor_style: CursorStyle::SteadyBlock,
previous_cursor_style: CursorStyle::SteadyBlock,
cursor_cell_width: 10.0,
cursor_cell_height: 20.0,
cursor_window_padding: 0.0,
cursor_content_offset_y: 0.0,
cursor_content_offset_x: 0.0,
cursor_shader_color: [1.0, 1.0, 1.0, 1.0],
cursor_trail_duration: 0.5,
cursor_glow_radius: 80.0,
cursor_glow_intensity: 0.3,
key_press_time: 0.0,
channel_textures,
cubemap,
use_background_as_channel0: false,
background_channel_texture: None,
background_color: [0.0, 0.0, 0.0, 0.0], progress_data: [0.0, 0.0, 0.0, 0.0],
content_inset_right: 0.0,
})
}
pub fn intermediate_texture_view(&self) -> &TextureView {
&self.intermediate_texture_view
}
pub fn render(
&mut self,
device: &Device,
queue: &Queue,
output_view: &TextureView,
apply_opacity: bool,
) -> Result<()> {
self.render_with_clear_color(
device,
queue,
output_view,
apply_opacity,
Color::TRANSPARENT,
)
}
pub fn render_with_clear_color(
&mut self,
device: &Device,
queue: &Queue,
output_view: &TextureView,
apply_opacity: bool,
clear_color: Color,
) -> Result<()> {
let now = Instant::now();
let time = if self.animation_enabled {
self.start_time.elapsed().as_secs_f32() * self.animation_speed.max(0.0)
} else {
0.0
};
let time_delta = now.duration_since(self.last_frame_time).as_secs_f32();
self.last_frame_time = now;
self.frame_time_accumulator += time_delta;
self.frames_in_second += 1;
if self.frame_time_accumulator >= 1.0 {
self.current_frame_rate = self.frames_in_second as f32 / self.frame_time_accumulator;
self.frame_time_accumulator = 0.0;
self.frames_in_second = 0;
}
self.frame_count = self.frame_count.wrapping_add(1);
let uniforms = self.build_uniforms(time, time_delta, apply_opacity);
queue.write_buffer(&self.uniform_buffer, 0, bytemuck::cast_slice(&[uniforms]));
let mut encoder = device.create_command_encoder(&CommandEncoderDescriptor {
label: Some("Custom Shader Encoder"),
});
{
let mut render_pass = encoder.begin_render_pass(&RenderPassDescriptor {
label: Some("Custom Shader Render Pass"),
color_attachments: &[Some(RenderPassColorAttachment {
view: output_view,
resolve_target: None,
ops: Operations {
load: LoadOp::Clear(clear_color),
store: StoreOp::Store,
},
depth_slice: None,
})],
depth_stencil_attachment: None,
timestamp_writes: None,
occlusion_query_set: None,
});
render_pass.set_pipeline(&self.pipeline);
render_pass.set_bind_group(0, &self.bind_group, &[]);
render_pass.draw(0..4, 0..1);
}
queue.submit(std::iter::once(encoder.finish()));
Ok(())
}
pub fn animation_enabled(&self) -> bool {
self.animation_enabled
}
pub fn set_animation_enabled(&mut self, enabled: bool) {
self.animation_enabled = enabled;
if enabled {
self.start_time = Instant::now();
}
}
pub fn set_animation_speed(&mut self, speed: f32) {
self.animation_speed = speed.max(0.0);
}
pub fn set_opacity(&mut self, opacity: f32) {
self.window_opacity = opacity.clamp(0.0, 1.0);
}
pub fn set_brightness(&mut self, brightness: f32) {
self.brightness = brightness.clamp(0.05, 1.0);
}
pub fn set_full_content_mode(&mut self, enabled: bool) {
self.full_content_mode = enabled;
}
pub fn full_content_mode(&self) -> bool {
self.full_content_mode
}
pub fn set_keep_text_opaque(&mut self, keep_opaque: bool) {
self.keep_text_opaque = keep_opaque;
}
pub fn set_mouse_position(&mut self, x: f32, y: f32) {
self.mouse_position = [x, y];
}
pub fn set_mouse_button(&mut self, pressed: bool, x: f32, y: f32) {
self.mouse_button_down = pressed;
if pressed {
self.mouse_click_position = [x, y];
}
}
pub fn update_key_press(&mut self) {
self.key_press_time = if self.animation_enabled {
self.start_time.elapsed().as_secs_f32() * self.animation_speed.max(0.0)
} else {
0.0
};
log::trace!("Key pressed at shader time={:.3}", self.key_press_time);
}
pub fn update_channel_texture(
&mut self,
device: &Device,
queue: &Queue,
channel: u8,
path: Option<&std::path::Path>,
) -> Result<()> {
if !(1..=4).contains(&channel) {
anyhow::bail!("Invalid channel index: {} (must be 1-4)", channel);
}
let index = (channel - 1) as usize;
let new_texture = match path {
Some(p) => ChannelTexture::from_file(device, queue, p)?,
None => ChannelTexture::placeholder(device, queue),
};
self.channel_textures[index] = new_texture;
self.recreate_bind_group(device);
log::info!(
"Updated iChannel{} texture: {}",
channel,
path.map(|p| p.display().to_string())
.unwrap_or_else(|| "placeholder".to_string())
);
Ok(())
}
pub fn update_cubemap(
&mut self,
device: &Device,
queue: &Queue,
path: Option<&std::path::Path>,
) -> Result<()> {
let new_cubemap = match path {
Some(p) => CubemapTexture::from_prefix(device, queue, p)?,
None => CubemapTexture::placeholder(device, queue),
};
self.cubemap = new_cubemap;
self.recreate_bind_group(device);
log::info!(
"Updated cubemap texture: {}",
path.map(|p| p.display().to_string())
.unwrap_or_else(|| "placeholder".to_string())
);
Ok(())
}
pub fn set_use_background_as_channel0(&mut self, use_background: bool) {
if self.use_background_as_channel0 != use_background {
self.use_background_as_channel0 = use_background;
log::info!("use_background_as_channel0 set to {}", use_background);
}
}
pub fn use_background_as_channel0(&self) -> bool {
self.use_background_as_channel0
}
pub fn set_background_texture(&mut self, device: &Device, texture: Option<ChannelTexture>) {
self.background_channel_texture = texture;
if self.use_background_as_channel0 {
self.recreate_bind_group(device);
}
}
pub fn set_background_color(&mut self, color: [f32; 3], active: bool) {
self.background_color = [color[0], color[1], color[2], if active { 1.0 } else { 0.0 }];
}
pub fn update_progress(&mut self, state: f32, percent: f32, is_active: f32, active_count: f32) {
self.progress_data = [state, percent, is_active, active_count];
}
pub fn update_use_background_as_channel0(&mut self, device: &Device, use_background: bool) {
if self.use_background_as_channel0 != use_background {
self.use_background_as_channel0 = use_background;
self.recreate_bind_group(device);
log::info!("use_background_as_channel0 toggled to {}", use_background);
}
}
pub fn set_content_inset_right(&mut self, inset: f32) {
self.content_inset_right = inset;
}
}