-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add motion vectors support for animated meshes #9902
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::mem; | ||
|
||
use bevy_ecs::prelude::Entity; | ||
use bevy_render::render_resource::{BufferUsages, BufferVec}; | ||
use bevy_utils::EntityHashMap; | ||
use bytemuck::Pod; | ||
|
||
/// A double buffer of `T`. | ||
/// | ||
/// Use [`DoubleBuffer::swap`] to swap buffer, | ||
/// access the current buffer with [`DoubleBuffer::current`], | ||
/// and the previous one with [`DoubleBuffer::previous`]. | ||
#[derive(Default)] | ||
pub struct DoubleBuffer<T> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a big deal for now, but I'd like to have some There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds like a fun thing to implement. But I think it should be part of a different PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be in bevy_render. |
||
pub previous: T, | ||
pub current: T, | ||
} | ||
|
||
impl<T> DoubleBuffer<T> { | ||
nicopap marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn swap(&mut self, swap_buffer: bool) { | ||
if swap_buffer { | ||
mem::swap(&mut self.current, &mut self.previous); | ||
} | ||
} | ||
} | ||
|
||
pub type DoubleBufferVec<T> = DoubleBuffer<BufferVec<T>>; | ||
|
||
impl<T: Pod> DoubleBufferVec<T> { | ||
pub const fn new(buffer_usage: BufferUsages) -> Self { | ||
DoubleBufferVec { | ||
previous: BufferVec::new(buffer_usage), | ||
current: BufferVec::new(buffer_usage), | ||
} | ||
} | ||
|
||
pub fn clear(&mut self, swap_buffer: bool) { | ||
nicopap marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.swap(swap_buffer); | ||
self.current.clear(); | ||
} | ||
} | ||
|
||
pub type DoubleEntityMap<T> = DoubleBuffer<EntityHashMap<Entity, T>>; | ||
|
||
impl<T> DoubleEntityMap<T> { | ||
pub fn clear(&mut self, swap_buffer: bool) { | ||
self.swap(swap_buffer); | ||
self.current.clear(); | ||
} | ||
|
||
pub fn insert(&mut self, entity: Entity, value: T) { | ||
nicopap marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.current.insert(entity, value); | ||
} | ||
|
||
pub fn missing_previous(&self, entity: &Entity) -> bool { | ||
nicopap marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let current = self.current.contains_key(entity); | ||
let previous = self.previous.contains_key(entity); | ||
// Either it's already missing (therefore there is no "previous" to miss) | ||
// or it's not missing and there is no "previous", so we miss previous. | ||
current && !previous | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't new, so not blocking, but I wonder if this branch is helpful or harmful. It probably has to run both branches regardless I would think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a really good question. My GPU-fu is not strong enough to be able to answer it. I don't remember the motivation behind it.