-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
342 additions
and
146 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
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
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
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
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,66 @@ | ||
use crate::{ | ||
sl::{ReadInput, Ret}, | ||
types::{self, VectorType}, | ||
}; | ||
|
||
pub use dunge_shader::vertex::*; | ||
|
||
pub trait InputProjection { | ||
const TYPE: VectorType; | ||
type Field; | ||
fn input_projection(id: u32, index: u32) -> Self::Field; | ||
} | ||
|
||
impl InputProjection for [f32; 2] { | ||
const TYPE: VectorType = VectorType::Vec2f; | ||
type Field = Ret<ReadInput, types::Vec2<f32>>; | ||
|
||
fn input_projection(id: u32, index: u32) -> Self::Field { | ||
ReadInput::new(id, index) | ||
} | ||
} | ||
|
||
impl InputProjection for [f32; 3] { | ||
const TYPE: VectorType = VectorType::Vec3f; | ||
type Field = Ret<ReadInput, types::Vec3<f32>>; | ||
|
||
fn input_projection(id: u32, index: u32) -> Self::Field { | ||
ReadInput::new(id, index) | ||
} | ||
} | ||
|
||
impl InputProjection for [f32; 4] { | ||
const TYPE: VectorType = VectorType::Vec4f; | ||
type Field = Ret<ReadInput, types::Vec4<f32>>; | ||
|
||
fn input_projection(id: u32, index: u32) -> Self::Field { | ||
ReadInput::new(id, index) | ||
} | ||
} | ||
|
||
impl InputProjection for glam::Vec2 { | ||
const TYPE: VectorType = VectorType::Vec2f; | ||
type Field = Ret<ReadInput, types::Vec2<f32>>; | ||
|
||
fn input_projection(id: u32, index: u32) -> Self::Field { | ||
ReadInput::new(id, index) | ||
} | ||
} | ||
|
||
impl InputProjection for glam::Vec3 { | ||
const TYPE: VectorType = VectorType::Vec3f; | ||
type Field = Ret<ReadInput, types::Vec3<f32>>; | ||
|
||
fn input_projection(id: u32, index: u32) -> Self::Field { | ||
ReadInput::new(id, index) | ||
} | ||
} | ||
|
||
impl InputProjection for glam::Vec4 { | ||
const TYPE: VectorType = VectorType::Vec4f; | ||
type Field = Ret<ReadInput, types::Vec4<f32>>; | ||
|
||
fn input_projection(id: u32, index: u32) -> Self::Field { | ||
ReadInput::new(id, index) | ||
} | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,90 @@ | ||
use { | ||
dunge::{ | ||
color::Rgba, | ||
context::Context, | ||
draw, mesh, | ||
sl::{self, Input, Out}, | ||
state::{Options, Render}, | ||
texture::{self, Format}, | ||
Vertex, | ||
}, | ||
glam::{Vec2, Vec3}, | ||
helpers::Image, | ||
std::{error, fs}, | ||
}; | ||
|
||
type Error = Box<dyn error::Error>; | ||
|
||
#[test] | ||
fn render() -> Result<(), Error> { | ||
const SIZE: (u32, u32) = (300, 300); | ||
|
||
#[repr(C)] | ||
#[derive(Vertex)] | ||
struct Vert { | ||
pos: Vec2, | ||
col: Vec3, | ||
} | ||
|
||
let triangle = |input: Input<Vert>| Out { | ||
place: sl::compose(input.pos, Vec2::new(0., 1.)), | ||
color: sl::vec4_with(sl::fragment(input.col), 1.), | ||
}; | ||
|
||
let cx = helpers::block_on(Context::new())?; | ||
let shader = cx.make_shader(triangle); | ||
let layer = cx.make_layer(Format::RgbAlpha, &shader); | ||
let view = { | ||
use texture::Data; | ||
|
||
let data = Data::empty(SIZE, Format::RgbAlpha)?.with_draw().with_copy(); | ||
cx.make_texture(data) | ||
}; | ||
|
||
let mesh = { | ||
use mesh::Data; | ||
|
||
const VERTS: [Vert; 3] = [ | ||
Vert { | ||
pos: Vec2::new(0., -0.75), | ||
col: Vec3::new(1., 0., 0.), | ||
}, | ||
Vert { | ||
pos: Vec2::new(0.866, 0.75), | ||
col: Vec3::new(0., 1., 0.), | ||
}, | ||
Vert { | ||
pos: Vec2::new(-0.866, 0.75), | ||
col: Vec3::new(0., 0., 1.), | ||
}, | ||
]; | ||
|
||
let data = Data::from_verts(&VERTS); | ||
cx.make_mesh(&data) | ||
}; | ||
|
||
let buffer = cx.make_copy_buffer(SIZE); | ||
let options = Options::default().with_clear(Rgba::from_standard([0., 0., 0., 1.])); | ||
let draw = draw::from_fn(|mut frame| { | ||
frame.layer(&layer, options).bind_empty().draw(&mesh); | ||
frame.copy_texture(&buffer, &view); | ||
}); | ||
|
||
let mut render = Render::default(); | ||
cx.draw_to_texture(&mut render, &view, draw); | ||
|
||
let mapped = helpers::block_on({ | ||
let (tx, rx) = helpers::oneshot(); | ||
cx.map_view(buffer.view(), tx, rx) | ||
}); | ||
|
||
let data = mapped.data(); | ||
let image = Image::from_fn(SIZE, |x, y| { | ||
let (width, _) = buffer.size(); | ||
let idx = x + y * width; | ||
data[idx as usize] | ||
}); | ||
|
||
fs::write("tests/triangle_color.png", image.encode())?; | ||
Ok(()) | ||
} |
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
Oops, something went wrong.