Git Product home page Git Product logo

shame's Introduction

logo logo

shame

shader metaprogramming in rust

shame lets you write shader recipes in rust, which can be turned into shaders and pipelines at runtime!

You can generate parts of the shaders based on runtime conditions, use the rust type system, output entire pipeline layouts and more!

shame is very experimental!

a single rust function generates vertex/fragment shaders and pipeline information a single rust function generates vertex/fragment shaders and pipeline information

shame pipelines are written as small rust functions which are "recorded" and return...

  • ...shader Strings
    • vertex/fragment or compute shaders
  • ...pipeline layout information
    • face culling,
    • z-test/z-write,
    • blending,
    • index format/primitive topology,
    • vertex attributes and buffer layouts,
    • color/depth targets and formats,
    • bind group layouts,
    • push constant type

Code example (more in examples folder)

use shame::prelude::*;

fn my_render_pipeline(mut f: RenderFeatures) {
    // use `f` to build your shader

    // define a vertex layout
    #[derive(shame::Fields)]
    struct MyVertex {
        pos: float3,
        color: float4,
    }

    // use the vertex layout in this shader
    let vert: MyVertex = f.io.vertex_buffer();
    // use an index buffer containing u32s
    let topology: TriangleList<u32> = f.io.index_buffer();

    // read from the 0th bind group
    let mut group0 = f.io.group();
    // which has a uniform block at binding #0
    let matrix: float4x4 = group0.uniform_block();

    // use the uniform data in calculations
    let clip_xyzw = matrix * (vert.pos, 1.0);

    // rasterize triangles at clip space positions with clockwise culling
    let polygon = f.raster.rasterize(
        clip_xyzw, Cull::CW, topology,
    );

    // vertex -> fragment interpolation
    let mut frag_color = polygon.lerp(vert.color);

    // read the push constant as a float
    let time: float = f.io.push_constant();
    frag_color += time.sin() * 0.1;

    // write to an sRGB render-target with alpha.
    // use alpha blending with frag_color as src color
    f.io.color::<RGBA_8888_sRGB>().blend(
        Blend::alpha(), 
        frag_color
    );
}

fn main() {
    // generate the shaders and pipeline layout
    let out = shame::record_render_pipeline(my_render_pipeline);
    
    let (vertex_shader, fragment_shader) = &out.shaders_glsl;
    let pipeline_info = &out.info;

    println!("{out}");
}

// more examples in the examples folder!
Feature Status
GLSL (Vulkan) out โœ…๐Ÿšง supported, not feature complete yet
GLSL (OpenGL) out โœ–๏ธโœ‹ will probably implement if people ask for it
WGSL out ๐Ÿ—“๏ธ planned
render pipeline layout โœ… supported, not feature complete yet
compute pipeline layout โœ… supported
vertex shaders โœ… supported
fragment shaders โœ… supported
compute shaders โœ… supported
geometry shaders โœ–๏ธ unsupported, not planned
hull/domain shaders โœ–๏ธ unsupported, not planned
๐Ÿ”ฅ hot reloading โœ… supported, see examples

Community

We have a discord server where you can ask questions, give feedback, contribute or show your creations! https://discord.gg/Xm5Ck7CCJk

you might also like

  • wgpu: recommended graphics library for use with shame
  • naga: if you want to convert shame's GLSL output to SPIR-V etc.
  • other ways to write shaders in rust:
    • rust-gpu: rust as a first-class language and ecosystem for GPU graphics & compute shaders
    • rasen: generates SPIR-V bytecode from an operation graph (+ DSL)
    • shades: a different approach of a shader EDSL in rust
    • rendiation: Rendiation Rendering Framework

license

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in shame by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

shame's People

Contributors

raymarch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.