Skip to content

Dynamic meshes#251

Open
zakarumych wants to merge 2 commits intoamethyst:masterfrom
zakarumych:dynmesh
Open

Dynamic meshes#251
zakarumych wants to merge 2 commits intoamethyst:masterfrom
zakarumych:dynmesh

Conversation

@zakarumych
Copy link
Copy Markdown
Member

No description provided.

Comment thread core/src/casts.rs

/// Cast slice of some arbitrary type into slice of bytes.
pub unsafe fn cast_arbitrary_slice<T, U>(slice: &[T]) -> &[U] {
let bytes = size_of::<T>() * slice.len();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is terribly unsafe in so many ways, a comment should definitely mention that it's effectively equivalen to transmute. You are only using it on Copy types, so it should probably be typeguarded as such. That way we at least get rid of Drop related issues.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also intended as internal method. Shouldn't be pub.

Comment thread mesh/src/dynamic.rs
}
}

Ok(unsafe { cast_arbitrary_slice_mut(&mut vertices.bytes[bytes_range]) })
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a safety issue. We probably should annotate AsVertex trait as unsafe so it is a responsibility of implementer to make sure it's safe to represent it as a byte slice (basically enforce #[repr(C)]).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is always safe to represent any type as bytes.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And should be also safe to represent back. Alignment should be preserved.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is not always safe. See this thread https://internals.rust-lang.org/t/pre-rfc-v2-safe-transmute/11431

In fact, there are crates that help with this today. See bytemuck. Notice that Pod trait is actually unsafe and has quite some safety rules attached to it.

Comment thread mesh/src/dynamic.rs
&mut self,
index: usize,
range: Range<u32>,
fill: V,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename fill to default_value or something like that. I interpreted those arguments as if the function would fill the whole provided range with fill value.

Comment thread mesh/src/dynamic.rs
/// # Panics
///
/// This function will panic if wrong index type is requested.
pub fn modify_indices_16(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that it would be better to have a single modify_indices that returns either &mut [u16] or &mut [u32], then possibly have those two methods use it. There is quite some duplication here.

Also, doc is inaccurate. It doesn't panic, it returns an error on mismatch.

Comment thread mesh/src/dynamic.rs
Ok(unsafe { cast_arbitrary_slice_mut(&mut indices.bytes[bytes_range]) })
}

pub fn update(&mut self, queue: QueueId, factory: &Factory<B>) -> Result<(), UploadError> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this should be called upload or flush. Also the upload should only really happen if it was updated since last time (it should have a dirty flag or something).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does use dirty regions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh shit, you are totally right. I somehow skipped that in reading. Then it's only the naming.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants