mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-23 16:01:04 +01:00
* fix more compile errors * fix err * fix all compile errors * fix clippy errors * fix issues in ray-traced-triangle example * fix clippy warnings * fix wasm not compiling * fix test * fix clippy (again) * fix some rustfmt issues * fix more rustfmt issues * fix even more rustfmt issues * fix yet more rustfmt issues * add #[allow(dead_code)] to some structs to fix clippy on web assembly * more format fixes * even more format fixes * close to final format fixes * final format fixes (hopefully) * actual final format fixes (hopefully) * move ray_cube_cube example and fix bugs * move other examples * fix crash when dropping * fix test * apply clippy and rustfmt changes * fix some memory leaks * fix incompatibility with new wgpu trunk changes * fix clippy & fmt * fix clippy errors on github * update to trunk * remove a line that printed log "Created blas ..." in device create tlas * fix merge issues * fix merge * fix fmt * restored example ray scene functionality * WIP fix rebase * Fix compile issues * More fixes * Fix rebase * Add wgls backend write_value_type AccelerationStructure * Add back xtask Cargo.lock * Revert serde path * Doc typos * Add -diff * Revert more serde * Revert serde again? * Fix doc typo * add test * clippy & fmt * fix error "blas does not exist" * fix various things * fix unused acceleration structures causing invalid SPIR-V * fix merge * fix use after free in a different way (now moved tlas instances to wgpu_core) * refactor & fmt * remove accidentally created file * remove accidentally created file (actually) * merge & fix * Fix problems * fix validation error * fix validation * fix merge * fmt * fix merge * use staging buffer trackers in creating the ray-tracing staging buffers (not the tlas_s) * change one missed staging buffer's tracker * wip: example * finished shadow! * fix merge * clippy * remove some uses of blas ids * fmt * fix merge * deduplicate code shared between command_encoder_build_acceleration_structures and command_encoder_build_acceleration_structures_unsafe_tlas * clippy * fix merge * fix warnings * clippy & fmt * re-add matching to get_raw_tlas_instance_size and tlas_instance_into_bytes * fmt * fix command * wip: fix merge * fix merge * fmt & docs * fix wasm clippy * fix issues with tests * fix issues with tests * fmt & clippy (wasm) * wip ray-traced-triangle (non hal) * unify camera location in ray_shadows * fix tests * add readme to ray_traced_triangle * note that vertex format must be f32x3 (not yet validated) * changelog * require VertexFormat::Float32x3 * fix compile error * when creating and building acceleration structures require Features::RAY_TRACING_ACCELERATION_STRUCTURE is enabled * remove duplicate workaround * fix most feedback * remove all changes from .gitattributes * fix compile error in test * more changes along same lines * format * apply feedback to more places * fix `cargo clippy` not just `cargo clippy --all-features` * cache max instances from tlas creation * fix clippy * fix examples * fix tests * remove now useless docs * add more docs * yet more docs * still more docs * Documentation. * Running out of unique commit messages to say writing docs. * Add more references to docs. * Get BindGroup in scope * Remove unintentional changes * Remove #[allow(dead_code)] * Remove old comments * Add device.poll * make ray_scene start right way up * fmt * move scratch buffer to its own module * actually remove intentional changes (turns out wgt is inconsistent) * use better names in ray_scene/shader.wgsl * fmt * remove more unintentional changes. * work on ray_tracing use after free * spelling Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com> * grammar Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com> * move things into struct (should be all vectors with 3 item types or more) * remove long-standing locks. * fmt * move not using index buffer to test * impl index & index mut for tlas package * impl index & index mut for tlas package * fix errors * fix test errors * move everything out of ray_tracing.rs * more index_mut * fmt * fix docs links * move to separate impl block * fix older request review * fmt * remove missed deref * fix wasm clippy * change to suggestions * fmt * add a note as to why `command_encoder_build_acceleration_structures_unsafe_tlas` and `command_encoder_build_acceleration_structures` are currently separate * add a todo to reconsider soon `command_encoder_build_acceleration_structures_unsafe_tlas` * switch to mat4 in ray-tracing examples * Combine RT Tests Under Single Submodule * Add More Raytracing Tests and Re-Organize * Documentation and Formatting Fixes * Remove Forced Alignment in wgpu-hal * work on out_of_order_as_build and add out_of_order_as_build_use * fmt * update test label naming * fix one validation error * fix clippy & fmt * add acceleration structure creation tests * make mis-matched index test use the correct function * move validation into validate_command_buffer * fix tests * add blas and tlas to pending writes to remove (now unneeded) `device.poll()`s * remove commented out code from examples and properly use the constants in ray_shadow * add examples to examples\README.md * rename ray tracing to ray queries in readme * remove unneeded usage from scratch buffer creation * use snatchables for raw acceleration structures * format * add EXPERIMENTAL_ to features * fix missed comment --------- Co-authored-by: Daniel Keitel <daniel.mg.keitel@fau.de> Co-authored-by: JMS55 <47158642+JMS55@users.noreply.github.com> Co-authored-by: atlas dostal <rodol@rivalrebels.com> Co-authored-by: vero <email@atlasdostal.com> Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
297 lines
7.5 KiB
Rust
297 lines
7.5 KiB
Rust
use crate::{Epoch, Index};
|
|
use std::{
|
|
cmp::Ordering,
|
|
fmt::{self, Debug},
|
|
hash::Hash,
|
|
marker::PhantomData,
|
|
mem::size_of,
|
|
num::NonZeroU64,
|
|
};
|
|
use wgt::WasmNotSendSync;
|
|
|
|
const _: () = {
|
|
if size_of::<Index>() != 4 {
|
|
panic!()
|
|
}
|
|
};
|
|
const _: () = {
|
|
if size_of::<Epoch>() != 4 {
|
|
panic!()
|
|
}
|
|
};
|
|
const _: () = {
|
|
if size_of::<RawId>() != 8 {
|
|
panic!()
|
|
}
|
|
};
|
|
|
|
/// The raw underlying representation of an identifier.
|
|
#[repr(transparent)]
|
|
#[cfg_attr(
|
|
any(feature = "serde", feature = "trace"),
|
|
derive(serde::Serialize),
|
|
serde(into = "SerialId")
|
|
)]
|
|
#[cfg_attr(
|
|
any(feature = "serde", feature = "replay"),
|
|
derive(serde::Deserialize),
|
|
serde(from = "SerialId")
|
|
)]
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
pub struct RawId(NonZeroU64);
|
|
|
|
impl RawId {
|
|
/// Zip together an identifier and return its raw underlying representation.
|
|
pub fn zip(index: Index, epoch: Epoch) -> RawId {
|
|
let v = (index as u64) | ((epoch as u64) << 32);
|
|
Self(NonZeroU64::new(v).unwrap())
|
|
}
|
|
|
|
/// Unzip a raw identifier into its components.
|
|
pub fn unzip(self) -> (Index, Epoch) {
|
|
(self.0.get() as Index, (self.0.get() >> 32) as Epoch)
|
|
}
|
|
}
|
|
|
|
/// An identifier for a wgpu object.
|
|
///
|
|
/// An `Id<T>` value identifies a value stored in a [`Global`]'s [`Hub`].
|
|
///
|
|
/// ## Note on `Id` typing
|
|
///
|
|
/// You might assume that an `Id<T>` can only be used to retrieve a resource of
|
|
/// type `T`, but that is not quite the case. The id types in `wgpu-core`'s
|
|
/// public API ([`TextureId`], for example) can refer to resources belonging to
|
|
/// any backend, but the corresponding resource types ([`Texture<A>`], for
|
|
/// example) are always parameterized by a specific backend `A`.
|
|
///
|
|
/// So the `T` in `Id<T>` is usually a resource type like `Texture<Empty>`,
|
|
/// where [`Empty`] is the `wgpu_hal` dummy back end. These empty types are
|
|
/// never actually used, beyond just making sure you access each `Storage` with
|
|
/// the right kind of identifier. The members of [`Hub<A>`] pair up each
|
|
/// `X<Empty>` type with the resource type `X<A>`, for some specific backend
|
|
/// `A`.
|
|
///
|
|
/// [`Global`]: crate::global::Global
|
|
/// [`Hub`]: crate::hub::Hub
|
|
/// [`Hub<A>`]: crate::hub::Hub
|
|
/// [`Texture<A>`]: crate::resource::Texture
|
|
/// [`Registry`]: crate::hub::Registry
|
|
/// [`Empty`]: hal::api::Empty
|
|
#[repr(transparent)]
|
|
#[cfg_attr(any(feature = "serde", feature = "trace"), derive(serde::Serialize))]
|
|
#[cfg_attr(any(feature = "serde", feature = "replay"), derive(serde::Deserialize))]
|
|
#[cfg_attr(
|
|
any(feature = "serde", feature = "trace", feature = "replay"),
|
|
serde(transparent)
|
|
)]
|
|
pub struct Id<T: Marker>(RawId, PhantomData<T>);
|
|
|
|
// This type represents Id in a more readable (and editable) way.
|
|
#[allow(dead_code)]
|
|
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
|
enum SerialId {
|
|
// The only variant forces RON to not ignore "Id"
|
|
Id(Index, Epoch),
|
|
}
|
|
|
|
impl From<RawId> for SerialId {
|
|
fn from(id: RawId) -> Self {
|
|
let (index, epoch) = id.unzip();
|
|
Self::Id(index, epoch)
|
|
}
|
|
}
|
|
|
|
impl From<SerialId> for RawId {
|
|
fn from(id: SerialId) -> Self {
|
|
match id {
|
|
SerialId::Id(index, epoch) => RawId::zip(index, epoch),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T> Id<T>
|
|
where
|
|
T: Marker,
|
|
{
|
|
/// # Safety
|
|
///
|
|
/// The raw id must be valid for the type.
|
|
pub unsafe fn from_raw(raw: RawId) -> Self {
|
|
Self(raw, PhantomData)
|
|
}
|
|
|
|
/// Coerce the identifiers into its raw underlying representation.
|
|
pub fn into_raw(self) -> RawId {
|
|
self.0
|
|
}
|
|
|
|
#[inline]
|
|
pub fn zip(index: Index, epoch: Epoch) -> Self {
|
|
Id(RawId::zip(index, epoch), PhantomData)
|
|
}
|
|
|
|
#[inline]
|
|
pub fn unzip(self) -> (Index, Epoch) {
|
|
self.0.unzip()
|
|
}
|
|
}
|
|
|
|
impl<T> Copy for Id<T> where T: Marker {}
|
|
|
|
impl<T> Clone for Id<T>
|
|
where
|
|
T: Marker,
|
|
{
|
|
#[inline]
|
|
fn clone(&self) -> Self {
|
|
*self
|
|
}
|
|
}
|
|
|
|
impl<T> Debug for Id<T>
|
|
where
|
|
T: Marker,
|
|
{
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
let (index, epoch) = self.unzip();
|
|
write!(formatter, "Id({index},{epoch})")?;
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
impl<T> Hash for Id<T>
|
|
where
|
|
T: Marker,
|
|
{
|
|
#[inline]
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
self.0.hash(state);
|
|
}
|
|
}
|
|
|
|
impl<T> PartialEq for Id<T>
|
|
where
|
|
T: Marker,
|
|
{
|
|
#[inline]
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.0 == other.0
|
|
}
|
|
}
|
|
|
|
impl<T> Eq for Id<T> where T: Marker {}
|
|
|
|
impl<T> PartialOrd for Id<T>
|
|
where
|
|
T: Marker,
|
|
{
|
|
#[inline]
|
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
Some(self.cmp(other))
|
|
}
|
|
}
|
|
|
|
impl<T> Ord for Id<T>
|
|
where
|
|
T: Marker,
|
|
{
|
|
#[inline]
|
|
fn cmp(&self, other: &Self) -> Ordering {
|
|
self.0.cmp(&other.0)
|
|
}
|
|
}
|
|
|
|
/// Marker trait used to determine which types uniquely identify a resource.
|
|
///
|
|
/// For example, `Device<A>` will have the same type of identifier as
|
|
/// `Device<B>` because `Device<T>` for any `T` defines the same maker type.
|
|
pub trait Marker: 'static + WasmNotSendSync {}
|
|
|
|
// This allows `()` to be used as a marker type for tests.
|
|
//
|
|
// We don't want these in production code, since they essentially remove type
|
|
// safety, like how identifiers across different types can be compared.
|
|
#[cfg(test)]
|
|
impl Marker for () {}
|
|
|
|
/// Define identifiers for each resource.
|
|
macro_rules! ids {
|
|
($(
|
|
$(#[$($meta:meta)*])*
|
|
pub type $name:ident $marker:ident;
|
|
)*) => {
|
|
/// Marker types for each resource.
|
|
pub mod markers {
|
|
$(
|
|
#[derive(Debug)]
|
|
pub enum $marker {}
|
|
impl super::Marker for $marker {}
|
|
)*
|
|
}
|
|
|
|
$(
|
|
$(#[$($meta)*])*
|
|
pub type $name = Id<self::markers::$marker>;
|
|
)*
|
|
}
|
|
}
|
|
|
|
ids! {
|
|
pub type AdapterId Adapter;
|
|
pub type SurfaceId Surface;
|
|
pub type DeviceId Device;
|
|
pub type QueueId Queue;
|
|
pub type BufferId Buffer;
|
|
pub type StagingBufferId StagingBuffer;
|
|
pub type TextureViewId TextureView;
|
|
pub type TextureId Texture;
|
|
pub type SamplerId Sampler;
|
|
pub type BindGroupLayoutId BindGroupLayout;
|
|
pub type PipelineLayoutId PipelineLayout;
|
|
pub type BindGroupId BindGroup;
|
|
pub type ShaderModuleId ShaderModule;
|
|
pub type RenderPipelineId RenderPipeline;
|
|
pub type ComputePipelineId ComputePipeline;
|
|
pub type PipelineCacheId PipelineCache;
|
|
pub type CommandEncoderId CommandEncoder;
|
|
pub type CommandBufferId CommandBuffer;
|
|
pub type RenderPassEncoderId RenderPassEncoder;
|
|
pub type ComputePassEncoderId ComputePassEncoder;
|
|
pub type RenderBundleEncoderId RenderBundleEncoder;
|
|
pub type RenderBundleId RenderBundle;
|
|
pub type QuerySetId QuerySet;
|
|
pub type BlasId Blas;
|
|
pub type TlasId Tlas;
|
|
pub type TlasInstanceId TlasInstance;
|
|
}
|
|
|
|
// The CommandBuffer type serves both as encoder and
|
|
// buffer, which is why the 2 functions below exist.
|
|
|
|
impl CommandEncoderId {
|
|
pub fn into_command_buffer_id(self) -> CommandBufferId {
|
|
Id(self.0, PhantomData)
|
|
}
|
|
}
|
|
|
|
impl CommandBufferId {
|
|
pub fn into_command_encoder_id(self) -> CommandEncoderId {
|
|
Id(self.0, PhantomData)
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn test_id() {
|
|
let indexes = [0, Index::MAX / 2 - 1, Index::MAX / 2 + 1, Index::MAX];
|
|
let epochs = [1, Epoch::MAX / 2 - 1, Epoch::MAX / 2 + 1, Epoch::MAX];
|
|
for &i in &indexes {
|
|
for &e in &epochs {
|
|
let id = Id::<()>::zip(i, e);
|
|
let (index, epoch) = id.unzip();
|
|
assert_eq!(index, i);
|
|
assert_eq!(epoch, e);
|
|
}
|
|
}
|
|
}
|