Skip to main content

renderling/
compositor.rs

1//! Compositor for alpha-blending a source texture onto a target framebuffer.
2//!
3//! This is used by the `renderling-ui` crate to overlay MSAA-resolved UI
4//! content onto a 3D scene without overwriting existing framebuffer content.
5
6use glam::{Vec2, Vec4};
7use spirv_std::{image::Image2d, spirv, Sampler};
8
9/// Fullscreen quad vertex shader for compositing.
10///
11/// Generates 6 vertices (two triangles) covering the full clip-space quad
12/// and passes through UV coordinates for texture sampling.
13#[spirv(vertex)]
14pub fn compositor_vertex(
15    #[spirv(vertex_index)] vertex_id: u32,
16    out_uv: &mut Vec2,
17    #[spirv(position)] out_pos: &mut Vec4,
18) {
19    let i = vertex_id as usize;
20    *out_uv = crate::math::UV_COORD_QUAD_CCW[i];
21    *out_pos = crate::math::CLIP_SPACE_COORD_QUAD_CCW[i];
22}
23
24/// Passthrough fragment shader for compositing.
25///
26/// Samples the source texture at the given UV and outputs the color.
27/// Alpha blending is handled by the pipeline's blend state, not the shader.
28#[spirv(fragment)]
29pub fn compositor_fragment(
30    #[spirv(descriptor_set = 0, binding = 0)] texture: &Image2d,
31    #[spirv(descriptor_set = 0, binding = 1)] sampler: &Sampler,
32    in_uv: Vec2,
33    frag_color: &mut Vec4,
34) {
35    *frag_color = texture.sample(*sampler, in_uv);
36}
37
38#[cfg(not(target_arch = "spirv"))]
39mod cpu;
40#[cfg(not(target_arch = "spirv"))]
41pub use cpu::*;