Skip to content

Commit

Permalink
Avoid fragment attributes (#513)
Browse files Browse the repository at this point in the history
* Do not use FragmentAttributes

* Remove FragmentAttributes

* Docs
  • Loading branch information
asny authored Nov 25, 2024
1 parent 7e6bfdc commit 022ee81
Show file tree
Hide file tree
Showing 35 changed files with 179 additions and 606 deletions.
7 changes: 0 additions & 7 deletions examples/fireworks/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ impl Material for FireworksMaterial {
fn fragment_shader_source(&self, _lights: &[&dyn Light]) -> String {
include_str!("particles.frag").to_string()
}
fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
uv: true,
color: true,
..FragmentAttributes::NONE
}
}
fn use_uniforms(&self, program: &Program, _camera: &Camera, _lights: &[&dyn Light]) {
program.use_uniform("color", self.color.to_linear_srgb());
program.use_uniform("fade", self.fade);
Expand Down
7 changes: 0 additions & 7 deletions examples/logo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ impl Material for LogoMaterial<'_> {
EffectMaterialId(0b1u16)
}

fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
uv: true,
..FragmentAttributes::NONE
}
}

fn use_uniforms(&self, program: &Program, _camera: &Camera, _lights: &[&dyn Light]) {
program.use_texture("image", &self.image);
}
Expand Down
7 changes: 0 additions & 7 deletions examples/mandelbrot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ impl Material for MandelbrotMaterial {
include_str!("mandelbrot.frag").to_string()
}

fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
position: true,
..FragmentAttributes::NONE
}
}

fn use_uniforms(&self, _program: &Program, _camera: &Camera, _lights: &[&dyn Light]) {}
fn render_states(&self) -> RenderStates {
RenderStates {
Expand Down
31 changes: 6 additions & 25 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,31 +412,21 @@ pub fn render_with_material(
material: impl Material,
lights: &[&dyn Light],
) {
let fragment_attributes = material.fragment_attributes();
let id = combine_ids(
geometry.id(fragment_attributes),
material.id(),
lights.iter().map(|l| l.id()),
);
let id = combine_ids(geometry.id(), material.id(), lights.iter().map(|l| l.id()));

let mut programs = context.programs.write().unwrap();
let program = programs.entry(id).or_insert_with(|| {
match Program::from_source(
context,
&geometry.vertex_shader_source(fragment_attributes),
&geometry.vertex_shader_source(),
&material.fragment_shader_source(lights),
) {
Ok(program) => program,
Err(err) => panic!("{}", err.to_string()),
}
});
material.use_uniforms(program, camera, lights);
geometry.draw(
camera,
program,
material.render_states(),
fragment_attributes,
);
geometry.draw(camera, program, material.render_states());
}

///
Expand All @@ -453,9 +443,8 @@ pub fn render_with_effect(
color_texture: Option<ColorTexture>,
depth_texture: Option<DepthTexture>,
) {
let fragment_attributes = effect.fragment_attributes();
let id = combine_ids(
geometry.id(fragment_attributes),
geometry.id(),
effect.id(color_texture, depth_texture),
lights.iter().map(|l| l.id()),
);
Expand All @@ -464,15 +453,15 @@ pub fn render_with_effect(
let program = programs.entry(id).or_insert_with(|| {
match Program::from_source(
context,
&geometry.vertex_shader_source(fragment_attributes),
&geometry.vertex_shader_source(),
&effect.fragment_shader_source(lights, color_texture, depth_texture),
) {
Ok(program) => program,
Err(err) => panic!("{}", err.to_string()),
}
});
effect.use_uniforms(program, camera, lights, color_texture, depth_texture);
geometry.draw(camera, program, effect.render_states(), fragment_attributes);
geometry.draw(camera, program, effect.render_states());
}

///
Expand All @@ -486,10 +475,6 @@ pub fn apply_screen_material(
camera: &Camera,
lights: &[&dyn Light],
) {
let fragment_attributes = material.fragment_attributes();
if fragment_attributes.normal || fragment_attributes.position || fragment_attributes.tangents {
panic!("Not possible to use the given material to render full screen, the full screen geometry only provides uv coordinates and color");
}
let id = combine_ids(
GeometryId::Screen,
material.id(),
Expand Down Expand Up @@ -529,10 +514,6 @@ pub fn apply_screen_effect(
color_texture: Option<ColorTexture>,
depth_texture: Option<DepthTexture>,
) {
let fragment_attributes = effect.fragment_attributes();
if fragment_attributes.normal || fragment_attributes.position || fragment_attributes.tangents {
panic!("Not possible to use the given effect to render full screen, the full screen geometry only provides uv coordinates and color");
}
let id = combine_ids(
GeometryId::Screen,
effect.id(color_texture, depth_texture),
Expand Down
14 changes: 0 additions & 14 deletions src/renderer/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ macro_rules! impl_effect_body {
self.$inner().id(color_texture, depth_texture)
}

fn fragment_attributes(&self) -> FragmentAttributes {
self.$inner().fragment_attributes()
}

fn use_uniforms(
&self,
program: &Program,
Expand Down Expand Up @@ -96,12 +92,6 @@ pub trait Effect {
depth_texture: Option<DepthTexture>,
) -> EffectMaterialId;

///
/// Returns a [FragmentAttributes] struct that describes which fragment attributes,
/// ie. the input from the vertex shader, are required for rendering with this effect.
///
fn fragment_attributes(&self) -> FragmentAttributes;

///
/// Sends the uniform data needed for this effect to the fragment shader.
///
Expand Down Expand Up @@ -164,10 +154,6 @@ impl<T: Effect> Effect for std::sync::RwLock<T> {
self.read().unwrap().id(color_texture, depth_texture)
}

fn fragment_attributes(&self) -> FragmentAttributes {
self.read().unwrap().fragment_attributes()
}

fn use_uniforms(
&self,
program: &Program,
Expand Down
7 changes: 0 additions & 7 deletions src/renderer/effect/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ impl Effect for CopyEffect {
EffectMaterialId::CopyEffect(color_texture, depth_texture)
}

fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
uv: true,
..FragmentAttributes::NONE
}
}

fn use_uniforms(
&self,
program: &Program,
Expand Down
7 changes: 0 additions & 7 deletions src/renderer/effect/fog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ impl Effect for FogEffect {
)
}

fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
uv: true,
..FragmentAttributes::NONE
}
}

fn use_uniforms(
&self,
program: &Program,
Expand Down
7 changes: 0 additions & 7 deletions src/renderer/effect/full_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ impl Effect for ScreenEffect {
EffectMaterialId::ScreenEffect(color_texture, depth_texture)
}

fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
uv: true,
..FragmentAttributes::NONE
}
}

fn use_uniforms(
&self,
program: &Program,
Expand Down
7 changes: 0 additions & 7 deletions src/renderer/effect/fxaa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ impl Effect for FxaaEffect {
)
}

fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
uv: true,
..FragmentAttributes::NONE
}
}

fn use_uniforms(
&self,
program: &Program,
Expand Down
7 changes: 0 additions & 7 deletions src/renderer/effect/lighting_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ impl Effect for LightingPassEffect {
EffectMaterialId::LightingPassEffect(color_texture.unwrap(), depth_texture.unwrap())
}

fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
uv: true,
..FragmentAttributes::NONE
}
}

fn use_uniforms(
&self,
program: &Program,
Expand Down
9 changes: 0 additions & 9 deletions src/renderer/effect/water.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,6 @@ impl Effect for WaterEffect {
)
}

fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
position: true,
normal: true,
uv: true,
..FragmentAttributes::NONE
}
}

fn render_states(&self) -> RenderStates {
RenderStates {
blend: Blend::TRANSPARENCY,
Expand Down
Loading

0 comments on commit 022ee81

Please sign in to comment.