Crate spirv_std

Source
Expand description

§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 provide 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.

Re-exports§

pub extern crate spirv_std_macros as macros;
pub use byte_addressable_buffer::ByteAddressableBuffer;
pub use num_traits;
pub use glam;

Modules§

arch
SPIR-V Intrinsics
byte_addressable_buffer
Container for an untyped blob of data.
debug_printf
support functions for debug printf
float
Traits and helper functions related to floats.
image
Image types
indirect_command
Indirect command structs from vulkan
matrix
a set of common SPIR-V Matrices, used for intrinsics
memory
Types for handling memory ordering constraints for concurrent memory access.
ray_tracing
Ray-tracing data types

Macros§

Image
A macro for creating SPIR-V OpTypeImage types. Always produces a spirv_std::image::Image<...> type.
debug_printf
Print a formatted string using the debug printf extension.
debug_printfln
Similar to debug_printf but appends a newline to the format string.
ray_query
Constructs an uninitialized ray query variable. Using the syntax let (mut)? <name>. Where name is the name of the ray query variable.

Structs§

RuntimeArray
SPIR-V “runtime array”, similar to [T], but with no way of knowing its length.
Sampler
An opaque reference to settings that describe how to access, filter, or sample an image.
TypedBuffer
Explicit (uniform/storage) buffer handle for descriptor indexing.

Traits§

Float
Abstract trait representing a SPIR-V floating point type.
Integer
Abstract trait representing any SPIR-V integer type.
Number
Abstract trait representing a SPIR-V integer or floating-point type. Unlike Scalar, excludes the boolean type.
Scalar
Abstract trait representing a SPIR-V scalar type, which includes:
ScalarOrVector
Abstract trait representing either a Scalar or Vector type.
SignedInteger
Abstract trait representing any SPIR-V signed integer type.
UnsignedInteger
Abstract trait representing any SPIR-V unsigned integer type.
Vector
Abstract trait representing a SPIR-V vector type.
VectorTruncateInto
Trait that implements slicing of a vector into a scalar or vector of lower dimensions, by ignoring the higher dimensions

Attribute Macros§

spirv
Replaces all (nested) occurrences of the #[spirv(..)] attribute with #[cfg_attr(target_arch="spirv", rust_gpu::spirv(..))].