Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shader lib custom include #81

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
5 changes: 3 additions & 2 deletions crates/geng-asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct ManagerImpl {
window: geng_window::Window,
#[cfg(feature = "audio")]
audio: geng_audio::Audio,
shader_lib: shader::Library,
shader_lib: Rc<shader::Library>,
hot_reload_enabled: bool,
}

Expand All @@ -36,6 +36,7 @@ impl Manager {
pub fn new(
window: &geng_window::Window,
#[cfg(feature = "audio")] audio: &geng_audio::Audio,
shader_lib: Rc<shader::Library>,
hot_reload: bool,
) -> Self {
Self {
Expand All @@ -44,7 +45,7 @@ impl Manager {
ugli: window.ugli().clone(),
#[cfg(feature = "audio")]
audio: audio.clone(),
shader_lib: shader::Library::new(window.ugli(), true, None),
shader_lib,
hot_reload_enabled: hot_reload,
}),
}
Expand Down
11 changes: 7 additions & 4 deletions crates/ugli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,13 @@ macro_rules! uniforms {
($name:ident : $value:expr) => {
$crate::SingleUniform::new(stringify!($name), $value)
};
($name:ident : $value:expr, $($names:ident : $values:expr),+) => {
($crate::uniforms!($name : $value), $crate::uniforms!($($names : $values),+))
($name:literal : $value:expr) => {
$crate::SingleUniform::new($name, $value)
};
($($name:ident : $value:expr),*,) => {
$crate::uniforms!($($name : $value),*)
($($name:ident)?$($str_name:literal)? : $value:expr, $($($names:ident)?$($str_names:literal)? $([$indices: literal])? : $values:expr),+) => {
($crate::uniforms!($($name)?$($str_name)? : $value), $crate::uniforms!($($($names)?$($str_names)? : $values),+))
};
($($($name:ident)?$($str_name:literal)? : $value:expr),*,) => {
$crate::uniforms!($($($name)?$($str_name)? : $value),*)
}
}
135 changes: 135 additions & 0 deletions crates/ugli/src/uniform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,81 @@ impl Uniform for [[f32; 4]; 4] {
}
}

impl Uniform for &[[[f32; 2]; 2]] {
fn apply(&self, gl: &raw::Context, info: &UniformInfo) {
gl.uniform_matrix2fv(
&info.location,
self.len() as _,
raw::FALSE,
&self
.iter()
.flat_map(|mat| unsafe { mem::transmute::<&[[f32; 2]; 2], &[f32; 2 * 2]>(mat) })
.copied()
.collect::<Vec<_>>(),
);
}
}

impl Uniform for &[[[f32; 3]; 3]] {
fn apply(&self, gl: &raw::Context, info: &UniformInfo) {
gl.uniform_matrix3fv(
&info.location,
self.len() as _,
raw::FALSE,
&self
.iter()
.flat_map(|mat| unsafe { mem::transmute::<&[[f32; 3]; 3], &[f32; 3 * 3]>(mat) })
.copied()
.collect::<Vec<_>>(),
);
}
}

impl Uniform for &[[[f32; 4]; 4]] {
fn apply(&self, gl: &raw::Context, info: &UniformInfo) {
gl.uniform_matrix4fv(
&info.location,
self.len() as _,
raw::FALSE,
&self
.iter()
.flat_map(|mat| unsafe { mem::transmute::<&[[f32; 4]; 4], &[f32; 4 * 4]>(mat) })
.copied()
.collect::<Vec<_>>(),
);
}
}

impl Uniform for Vec<[[f32; 3]; 3]> {
fn apply(&self, gl: &raw::Context, info: &UniformInfo) {
gl.uniform_matrix3fv(
&info.location,
self.len() as _,
raw::FALSE,
&self
.iter()
.flat_map(|mat| unsafe { mem::transmute::<&[[f32; 3]; 3], &[f32; 3 * 3]>(mat) })
.copied()
.collect::<Vec<_>>(),
);
}
}

impl Uniform for Vec<[[f32; 4]; 4]> {
fn apply(&self, gl: &raw::Context, info: &UniformInfo) {
gl.uniform_matrix4fv(
&info.location,
self.len() as _,
raw::FALSE,
&self
.iter()
.flat_map(|mat| unsafe { mem::transmute::<&[[f32; 4]; 4], &[f32; 4 * 4]>(mat) })
.copied()
.collect::<Vec<_>>(),
);
}
}

impl Uniform for mat3<f32> {
fn apply(&self, gl: &raw::Context, info: &UniformInfo) {
gl.uniform_matrix3fv(&info.location, 1, raw::FALSE, self.as_flat_array());
Expand All @@ -99,6 +174,66 @@ impl Uniform for mat4<f32> {
}
}

impl Uniform for &[mat3<f32>] {
fn apply(&self, gl: &raw::Context, info: &UniformInfo) {
gl.uniform_matrix3fv(
&info.location,
self.len() as _,
raw::FALSE,
&self
.iter()
.flat_map(|mat| mat.as_flat_array())
.copied()
.collect::<Vec<_>>(),
);
}
}

impl Uniform for &[mat4<f32>] {
fn apply(&self, gl: &raw::Context, info: &UniformInfo) {
gl.uniform_matrix4fv(
&info.location,
self.len() as _,
raw::FALSE,
&self
.iter()
.flat_map(|mat| mat.as_flat_array())
.copied()
.collect::<Vec<_>>(),
);
}
}

impl Uniform for Vec<mat3<f32>> {
fn apply(&self, gl: &raw::Context, info: &UniformInfo) {
gl.uniform_matrix3fv(
&info.location,
self.len() as _,
raw::FALSE,
&self
.iter()
.flat_map(|mat| mat.as_flat_array())
.copied()
.collect::<Vec<_>>(),
);
}
}

impl Uniform for Vec<mat4<f32>> {
fn apply(&self, gl: &raw::Context, info: &UniformInfo) {
gl.uniform_matrix4fv(
&info.location,
self.len() as _,
raw::FALSE,
&self
.iter()
.flat_map(|mat| mat.as_flat_array())
.copied()
.collect::<Vec<_>>(),
);
}
}

impl<P: TexturePixel> Uniform for Texture2d<P> {
fn apply(&self, gl: &raw::Context, info: &UniformInfo) {
gl.active_texture(raw::TEXTURE0 + unsafe { UNIFORM_TEXTURE_COUNT } as raw::Enum);
Expand Down
22 changes: 22 additions & 0 deletions crates/ugli/src/uniform/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ impl<A: Uniforms, B: Uniforms, C: Uniforms, D: Uniforms> Uniforms for (A, B, C,
}
}

impl<'a, U: Uniforms> Uniforms for &'a [U] {
fn walk_uniforms<C>(&self, visitor: &mut C)
where
C: UniformVisitor,
{
for uniform in *self {
uniform.walk_uniforms(visitor);
}
}
}

impl<U: Uniforms> Uniforms for Vec<U> {
fn walk_uniforms<C>(&self, visitor: &mut C)
where
C: UniformVisitor,
{
for uniform in self {
uniform.walk_uniforms(visitor);
}
}
}

impl<U: Uniforms> Uniforms for Option<U> {
fn walk_uniforms<C>(&self, visitor: &mut C)
where
Expand Down
15 changes: 12 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub(crate) struct GengImpl {
window: Window,
#[cfg(feature = "audio")]
audio: Audio,
shader_lib: shader::Library,
shader_lib: Rc<shader::Library>,
pub(crate) draw2d: Rc<draw2d::Helper>,
asset_manager: asset::Manager,
default_font: Rc<Font>,
Expand All @@ -27,6 +27,7 @@ pub struct ContextOptions {
pub fixed_delta_time: f64,
pub max_delta_time: f64,
pub shader_prefix: Option<(String, String)>,
pub shader_lib: HashMap<String, String>,
pub target_ui_resolution: Option<vec2<f64>>,
pub hot_reload: bool,
}
Expand All @@ -48,6 +49,7 @@ impl Default for ContextOptions {
fixed_delta_time: 0.05,
max_delta_time: 0.1,
shader_prefix: None,
shader_lib: HashMap::new(),
target_ui_resolution: None,
hot_reload: cfg!(debug_assertions),
}
Expand Down Expand Up @@ -75,11 +77,17 @@ impl Geng {
setup_panic_handler();
window::run(&options.window.clone(), |window| async move {
let ugli = window.ugli().clone();
let shader_lib = shader::Library::new(
let mut shader_lib = shader::Library::new(
&ugli,
options.window.antialias,
options.shader_prefix.clone(),
);
for (name, source) in options.shader_lib.iter() {
shader_lib.add(name, source);
}

let shader_lib = Rc::new(shader_lib);

let draw2d = Rc::new(draw2d::Helper::new(&ugli, options.window.antialias));
let default_font = Rc::new(Font::default(window.ugli()));
#[cfg(feature = "audio")]
Expand All @@ -89,12 +97,13 @@ impl Geng {
window: window.clone(),
#[cfg(feature = "audio")]
audio: audio.clone(),
shader_lib,
shader_lib: shader_lib.clone(),
draw2d,
asset_manager: asset::Manager::new(
&window,
#[cfg(feature = "audio")]
&audio,
shader_lib,
options.hot_reload,
),
default_font,
Expand Down
Loading