-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
include shader-crate-template in this repo
- Loading branch information
Showing
8 changed files
with
281 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
[workspace] | ||
members = [ | ||
"crates/cargo-gpu", | ||
"crates/shader-crate-template" | ||
] | ||
|
||
exclude = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |