Skip to content

Commit

Permalink
code dump bevyengine#3
Browse files Browse the repository at this point in the history
  • Loading branch information
bilsen committed Oct 4, 2021
1 parent b1f8153 commit 6f6eacd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
47 changes: 29 additions & 18 deletions examples/3d/taa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ impl Plugin for TaaPlugin {
.add_system_to_stage(RenderStage::Queue, queue_taa_mesh_bind_group)
.add_system_to_stage(RenderStage::Queue, queue_taa_view_bind_group)
.add_system_to_stage(RenderStage::Queue, queue_velocity)
.add_system_to_stage(RenderStage::Cleanup, save_view_uniform)
.add_system_to_stage(RenderStage::Prepare, add_previous_view_offsets.exclusive_system().at_start())
.init_resource::<DrawFunctions<Velocity>>();

render_app.add_render_command::<Velocity, DrawTaaVelocity>();
Expand Down Expand Up @@ -385,26 +387,26 @@ pub struct PreviousViewUniforms {
pub uniforms: DynamicUniformVec<ViewUniform>,
}


#[derive(Clone)]
pub struct PreviousViewUniformOffset {
pub offset: u32
pub offset: u32,
}

pub struct PreviousViewUniformOffsets {
pub offsets: HashMap<Entity, PreviousViewUniformOffset>
pub offsets: HashMap<Entity, PreviousViewUniformOffset>,
}


fn save_view_uniform(
device: Res<RenderDevice>,
queue: Res<RenderQueue>,
view_uniforms: Res<ViewUniforms>,
mut previous: ResMut<PreviousViewUniforms>,
mut previous_offsets: ResMut<PreviousViewUniformOffsets>,
query: Query<(Entity, &ViewUniformOffset)>
query: Query<(Entity, &ViewUniformOffset)>,
) {

previous.uniforms.reserve_and_clear(view_uniforms.uniforms.len(), device);
previous
.uniforms
.reserve_and_clear(view_uniforms.uniforms.len(), &*device);

for (view_uniform) in view_uniforms.uniforms.iter() {
previous.uniforms.push(view_uniform.clone());
Expand All @@ -413,16 +415,24 @@ fn save_view_uniform(

previous_offsets.offsets.clear();
for (entity, offset) in query.iter() {
previous_offsets.offsets.insert(entity, PreviousViewUniformOffset { offset: offset.offset });
previous_offsets.offsets.insert(
entity,
PreviousViewUniformOffset {
offset: offset.offset,
},
);
}

}

fn add_previous_view_offsets(
mut commands: Commands,
previous_offsets: Res<PreviousViewUniformOffsets>
mut commands: Commands,
previous_offsets: Res<PreviousViewUniformOffsets>,
) {
let insertable: Vec<_> = previous_offsets.offsets.iter().map(|(entity, offset)| (entity, (offset,))).collect();
let insertable: Vec<_> = previous_offsets
.offsets
.iter()
.map(|(entity, offset)| (*entity, (offset.clone(),)))
.collect();

commands.insert_or_spawn_batch(insertable);
}
Expand Down Expand Up @@ -470,11 +480,11 @@ fn queue_taa_view_bind_group(
taa_shader: Res<TaaVelocityShaders>,
render_device: Res<RenderDevice>,
view_uniforms: Res<ComponentUniforms<ViewUniform>>,
previous_view_uniforms: Res<ComponentUniforms<Previous<ViewUniform>>>,
previous_view_uniforms: Res<PreviousViewUniforms>,
) {
if let (Some(binding), Some(binding2)) = (
view_uniforms.uniforms().binding(),
previous_view_uniforms.uniforms().binding(),
previous_view_uniforms.uniforms.binding(),
) {
commands.insert_resource(TaaViewBindGroup {
value: render_device.create_bind_group(&BindGroupDescriptor {
Expand Down Expand Up @@ -712,8 +722,8 @@ struct SetTaaViewBindGroup<const I: usize>;
impl<const I: usize> RenderCommand<Velocity> for SetTaaViewBindGroup<I> {
type Param = (
SQuery<(
Read<DynamicUniformIndex<ViewUniform>>,
Read<DynamicUniformIndex<Previous<ViewUniform>>>,
Read<ViewUniformOffset>,
Read<PreviousViewUniformOffset>,
)>,
SRes<TaaViewBindGroup>,
);
Expand All @@ -728,7 +738,7 @@ impl<const I: usize> RenderCommand<Velocity> for SetTaaViewBindGroup<I> {
pass.set_bind_group(
I,
&bind_group.into_inner().value,
&[view_index.index(), previous_view_index.index()],
&[view_index.offset, previous_view_index.offset],
);
}
}
Expand Down Expand Up @@ -840,5 +850,6 @@ fn main() {
.add_plugins(PipelinedDefaultPlugins)
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(TaaPlugin);
.add_plugin(TaaPlugin)
.run();
}
8 changes: 5 additions & 3 deletions pipelined/bevy_render2/src/render_resource/uniform_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl<T: AsStd140> UniformVec<T> {
self.values.clear();
}

pub fn iter(&self) -> impl Iterator<Item=&T> {
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.values.iter()
}
}
Expand Down Expand Up @@ -170,7 +170,9 @@ impl<T: AsStd140> DynamicUniformVec<T> {
self.uniform_vec.clear();
}

pub fn iter(&self) -> impl Iterator<Item=&T> {
self.uniform_vec.iter().map(|dynamic_uniform| &dynamic_uniform.0)
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.uniform_vec
.iter()
.map(|dynamic_uniform| &dynamic_uniform.0)
}
}

0 comments on commit 6f6eacd

Please sign in to comment.