#applications #wgpu #able #shaders #integrate #devices #buffer

shady

A shadertoy-like library to be able to easily integrate shadertoy-like stuff in your applications

5 releases (stable)

new 2.0.0 Feb 21, 2025
1.1.1 Feb 21, 2025
1.0.0 Feb 21, 2025
0.1.0 Feb 21, 2025

#349 in Graphics APIs

Download history

60 downloads per month

GPL-3.0-or-later

51KB
1K SLoC

shady-lib

The main library which takes care of the uniform/storage buffers, vertices and templates.

The idea is that other applications who wish to include shadertoy-like shaders into their application to use this library which takes care most of the data to be able to run those shaders.

State

It's useable, however I'm a bit unsure about the architecture because I don't really know what a good API looks like for a graphics-programmer.

Example

A simple example can be seen here: https://github.com/TornaxO7/shady/blob/main/shady-lib/examples/mini-simple.rs if you want to include it to your app. All relevant places where you have to "interact" with shady are annoted with the // SHADY comments.


lib.rs:

A shadertoy like library to be able to easily integrate shadertoy-like stuff in your applications. It provides functions to setup the following uniform buffers (which will be also called Resources within this doc):

  • iAudio: Contains frequency bars of an audio source.
  • iFrame: Contains the current frame count.
  • iMouse: Contains the coordinate points of the user's mouse.
  • iResolution: Contains the height and width of the surface which will be drawed on.
  • iTime: The playback time of the shader.

Note:

  • You should be familiar with wgpu code in order to be able to use this.
  • shady is not compatible with shadertoy's shaders so you can't simply copy+paste the fragment code from shadertoy to application which are using shady (but porting them should be very easy in general).

Feature flags

Each resource is behind a feature gate so if you don't want to use some of them, just disable their feature gate.

Example

An (mini) example can be seen here: https://github.com/TornaxO7/shady/blob/main/shady-lib/examples/mini-simple.rs

But here's a rough structure how it's meant to be used:

use shady::{Shady, ShadyDescriptor};

struct State {
    shady: Shady,

    // ... and your other wgpu stuff, like `wgpu::Device`, etc.
    queue: wgpu::Queue,
    device: wgpu::Device,
}

impl State {
    pub fn new() -> Self {
        // .. your stuff

        let shady = Shady::new(ShadyDescriptor {
            // ... set the attributes
        });

        // ...
    }

    pub fn prepare_next_frame(&mut self) {
        // here you can change some properties of shady or change
        // the values of the uniform buffers of the fragment shader
        self.shady.inc_frame();

        // ... afterwards tell shady to move the values into the uniform buffer
        self.shady.update_frame_buffer(&mut self.queue);
        // ... and other buffers you'd like to update
    }

    pub fn render(&mut self) {
        // ...

        let view = ...;
        let mut encoder = self.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
           label: Some("Some random encoder"),
        });

        // shady will add a render pass and you are good to go!
        self.shady.add_render_pass(&mut encoder, &view);
    }

    pub fn load_fragment_code<'a>(&mut self, shader_source: wgpu::ShaderSource<'a>) {
        // set the render pipeline which should execute the given fragment code
        self.shady.set_render_pipeline(&self.device, shader_source);
    }
}

Dependencies

~12–43MB
~672K SLoC