Skip to content

Commit

Permalink
include shader-crate-template in this repo
Browse files Browse the repository at this point in the history
  • Loading branch information
schell committed Dec 7, 2024
1 parent 2afcef9 commit c5c88d0
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 8 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ jobs:
- run: rustup update
- run: cargo install --path crates/cargo-gpu
- run: cargo gpu install
- run: git clone https://github.com/Rust-GPU/shader-crate-template.git
- run: cargo gpu build --shader-crate shader-crate-template --output-dir shaders
- run: ls -lah shaders
- run: cat shaders/manifest.json
- run: cargo gpu build --shader-crate crates/shader-crate-template --output-dir test-shaders
- run: ls -lah test-shaders
- run: cat test-shaders/manifest.json

clippy:
runs-on: ubuntu-latest
Expand Down
89 changes: 85 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"crates/cargo-gpu",
"crates/shader-crate-template"
]

exclude = [
Expand Down
1 change: 1 addition & 0 deletions crates/shader-crate-template/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
123 changes: 123 additions & 0 deletions crates/shader-crate-template/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions crates/shader-crate-template/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "rust-gpu-shader-crate-template"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["rlib", "cdylib"]

# Dependencies for CPU and GPU code
[dependencies]
spirv-std = { version = "0.9" }

# dependencies for GPU code
[target.'cfg(target_arch = "spirv")'.dependencies]
glam = { version = "0.29", default-features = false, features = ["libm"] }


# dependencies for CPU code
[target.'cfg(not(target_arch = "spirv"))'.dependencies]
glam = { version = "0.29", features = ["std"] }

5 changes: 5 additions & 0 deletions crates/shader-crate-template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# shader-crate

This is a shader crate that can be compiled to SPIR-V using `rust-gpu`.

This crate can also be used from CPU Rust code, just like any other crate.
42 changes: 42 additions & 0 deletions crates/shader-crate-template/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Shader entry points.
//!
//! Contains an example vertex shader, fragment shader and one example compute
//! shader.
#![no_std]
use glam::{Vec2, Vec4};
use spirv_std::spirv;

pub const CLIP_SPACE_COORD_QUAD_CCW: [Vec4; 6] = {
let tl = Vec4::new(-1.0, 1.0, 0.5, 1.0);
let tr = Vec4::new(1.0, 1.0, 0.5, 1.0);
let bl = Vec4::new(-1.0, -1.0, 0.5, 1.0);
let br = Vec4::new(1.0, -1.0, 0.5, 1.0);
[bl, br, tr, tr, tl, bl]
};

pub const UV_COORD_QUAD_CCW: [Vec2; 6] = {
let tl = Vec2::new(0.0, 0.0);
let tr = Vec2::new(1.0, 0.0);
let bl = Vec2::new(0.0, 1.0);
let br = Vec2::new(1.0, 1.0);
[bl, br, tr, tr, tl, bl]
};

/// Vertex shader that renders an implicit quad.
#[spirv(vertex)]
pub fn vertex(
#[spirv(vertex_index)] vertex_id: u32,
out_uv: &mut Vec2,
#[spirv(position)] clip_pos: &mut Vec4,
) {
let index = vertex_id as usize % 6;
*out_uv = UV_COORD_QUAD_CCW[index];
*clip_pos = CLIP_SPACE_COORD_QUAD_CCW[index];
}

/// Fragment shader that uses UV coords passed in from the vertex shader
/// to render a simple gradient.
#[spirv(fragment)]
pub fn fragment(in_uv: Vec2, frag_color: &mut Vec4) {
*frag_color = Vec4::new(in_uv.x, in_uv.y, 0.0, 1.0);
}

0 comments on commit c5c88d0

Please sign in to comment.