28 releases (8 breaking)

0.9.0 Jul 25, 2023
0.7.0 Apr 21, 2023
0.6.1 Mar 20, 2023
0.4.0 Dec 21, 2022
0.1.0 Oct 23, 2020

#44 in Graphics APIs

Download history 859/week @ 2024-01-29 925/week @ 2024-02-05 956/week @ 2024-02-12 1057/week @ 2024-02-19 1113/week @ 2024-02-26 1240/week @ 2024-03-04 1417/week @ 2024-03-11 1446/week @ 2024-03-18 1480/week @ 2024-03-25 1321/week @ 2024-04-01 917/week @ 2024-04-08 1231/week @ 2024-04-15 1263/week @ 2024-04-22 1019/week @ 2024-04-29 900/week @ 2024-05-06 1002/week @ 2024-05-13

4,353 downloads per month
Used in 66 crates (4 directly)

MIT/Apache

205KB
4.5K SLoC

spirv-std

Core functions, traits, and more that make up a “standard library” for SPIR-V for use in rust-gpu.

This crate gives a rust-gpu shader access to the required #![spirv(..)] attribute, as well as povide all kinds of APIs that allows a shader to access GPU resources such as textures and buffers. Optionally, through the use of the "glam" feature, it includes some boilerplate trait implementations to make glam vector types compatible with these APIs.

Example

Sky shader

Here is a small excerpt to see what a shader would look like. See source for full details of the shader that generates above image.

use spirv_std::spirv;
use glam::{Vec3, Vec4, vec2, vec3};

#[spirv(fragment)]
pub fn main(
    #[spirv(frag_coord)] in_frag_coord: &Vec4,
    #[spirv(push_constant)] constants: &ShaderConstants,
    output: &mut Vec4,
) {
    let frag_coord = vec2(in_frag_coord.x, in_frag_coord.y);
    let mut uv = (frag_coord - 0.5 * vec2(constants.width as f32, constants.height as f32))
        / constants.height as f32;
    uv.y = -uv.y;

    let eye_pos = vec3(0.0, 0.0997, 0.2);
    let sun_pos = vec3(0.0, 75.0, -1000.0);
    let dir = get_ray_dir(uv, eye_pos, sun_pos);

    // evaluate Preetham sky model
    let color = sky(dir, sun_pos);

    *output = tonemap(color).extend(1.0)
}

Getting started

Check out The rust-gpu Dev Guide for information on how to get started with using it in your projects.

Experiment with rust-gpu shaders in-browser at SHADERed.

Dependencies

~5MB
~133K SLoC