Skip to content

Commit

Permalink
"Documentation"
Browse files Browse the repository at this point in the history
  • Loading branch information
thedocruby committed Oct 26, 2023
1 parent 2ea3027 commit f909e3e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn main() {
let shader = Shader::from(
// Tip: Try changing the shader!
// Options: CIRCLE, RAINBOW, OCEAN
CIRCLE
OCEAN
);

// Execute shader
Expand Down
32 changes: 23 additions & 9 deletions examples/shaders/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,34 @@ pub fn reflect(ray: Vec3, normal: Vec3) -> Vec3 {
ray - normal * 2.0 * ray.dot(normal)
}

// NOTE: This function is for converting particularly stubborn Shadertoy shaders to the proper linear color space.
fn to_linear_f32(color: f32) -> f32 {
if color <= 0.04045 {
color / 12.92
} else {
((color + 0.055) / 1.055).powf(2.4)
}
}

/// NOTE: This function is for converting particularly stubborn Shadertoy shaders to the proper linear color space.
///
/// If you are porting a a GLSL shader from Shadertoy to Gravy, and the colors look wrong,
/// first try to find any part of the code that is doing something like this:
/// `pow(color, vec3(1.0/2.2));`
/// Or, alternatively:
/// `pow(color, vec3(.4545));`
///
/// If you find any similar bits of code,
/// you can simply replace the entire thing with `color`. This will fix the colors.
///
/// If you do not find any bits like that, then to fix the colors,
/// pass the final color through this function before returning it.
///
/// (example in `rainbow.rs`)
pub fn to_linear(color: Vec4) -> Vec4 {
vec4(
to_linear_f32(color.x),
to_linear_f32(color.y),
to_linear_f32(color.z),
color.w,
)
}

fn to_linear_f32(color: f32) -> f32 {
if color <= 0.04045 {
color / 12.92
} else {
((color + 0.055) / 1.055).powf(2.4)
}
}
50 changes: 49 additions & 1 deletion examples/shaders/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,52 @@ use common::*;
phantom: std::marker::PhantomData,
};

// ** OCEAN
// ** OCEAN

// ~~~~~ FORMAT ~~~~~
// Example for the shader `test.rs`:
//
// mod test; // <--- This is the name of the shader in snake_case
//
// Constants are unique for each shader, they can be any subset of `gravylib_helpers::Constants`
// #[derive(Copy, Clone, Pod, Zeroable)]
// #[repr(C)]
// pub struct TestConstants { // Replace "Test" with the name of the shader in PascalCase
// pub width: u32,
// pub height: u32,
// pub time: f32,
// }
//
// impl From<Constants> for TestConstants { // Here again, replace "Test" with the name of the shader in PascalCase
// fn from(constants: Constants) -> Self {
// Self {
// width: constants.width,
// height: constants.height,
// time: constants.time,
// }
// }
// }
//
// This is the entry point of the shader. It must be named the same as the shader file, in snake_case
// #[spirv(fragment)]
// pub fn test( // Replace "test" with the name of the shader in snake_case
// #[spirv(frag_coord)] in_frag_coord: Vec4,
// #[spirv(push_constant)] constants: &TestConstants, // Replace "Test" with the name of the shader in PascalCase
// output: &mut Vec4,
// ) {
// let frag_coord = vec2(in_frag_coord.x, in_frag_coord.y);
// Call the shader function from the `test.rs` file.
// The name of the function must be the same as the shader file, in snake_case
// The shader function must take the constants and the frag_coord (a Vec2) as arguments, and return a Vec4 RGBA color
// *output = test::test(constants, frag_coord); // Replace "test" with the name of the shader in snake_case (both times)
// }
//
// This is the RawShader struct. It must be named the same as the shader file, in CONSTANT_CASE
// #[cfg(not(target_arch = "spirv"))]
// #[allow(dead_code)]
// pub const TEST: &RawShader<TestConstants> = &RawShader { // Replace "TEST" and "Test" with the name of the shader in CONSTANT_CASE and PascalCase, respectively.
// shader_type: ShaderType::Pixel,
// crate_name: env!("CARGO_CRATE_NAME"),
// entry_point: "test", // Replace "test" with the name of the shader in snake_case
// phantom: std::marker::PhantomData,
// };

0 comments on commit f909e3e

Please sign in to comment.