diff --git a/ffi/wgpu-remote.h b/ffi/wgpu-remote.h index 3247c6008..4f8665ecd 100644 --- a/ffi/wgpu-remote.h +++ b/ffi/wgpu-remote.h @@ -28,9 +28,13 @@ typedef struct { WGPUEpoch _1; } WGPUId; -typedef WGPUId WGPUDeviceId; +typedef WGPUId WGPUGenericId_DeviceHandle; -typedef WGPUId WGPUAdapterId; +typedef WGPUGenericId_DeviceHandle WGPUDeviceId; + +typedef WGPUId WGPUGenericId_AdapterHandle; + +typedef WGPUGenericId_AdapterHandle WGPUAdapterId; typedef struct { bool anisotropic_filtering; diff --git a/ffi/wgpu.h b/ffi/wgpu.h index 4aba690a1..342616f09 100644 --- a/ffi/wgpu.h +++ b/ffi/wgpu.h @@ -248,9 +248,13 @@ typedef struct { WGPUEpoch _1; } WGPUId; -typedef WGPUId WGPUDeviceId; +typedef WGPUId WGPUGenericId_DeviceHandle; -typedef WGPUId WGPUAdapterId; +typedef WGPUGenericId_DeviceHandle WGPUDeviceId; + +typedef WGPUId WGPUGenericId_AdapterHandle; + +typedef WGPUGenericId_AdapterHandle WGPUAdapterId; typedef struct { bool anisotropic_filtering; @@ -265,9 +269,13 @@ typedef struct { WGPULimits limits; } WGPUDeviceDescriptor; -typedef WGPUId WGPUBindGroupId; +typedef WGPUId WGPUGenericId_BindGroupHandle; -typedef WGPUId WGPUBufferId; +typedef WGPUGenericId_BindGroupHandle WGPUBindGroupId; + +typedef WGPUId WGPUGenericId_BufferHandle; + +typedef WGPUGenericId_BufferHandle WGPUBufferId; typedef uint64_t WGPUBufferAddress; @@ -275,7 +283,9 @@ typedef void (*WGPUBufferMapReadCallback)(WGPUBufferMapAsyncStatus status, const typedef void (*WGPUBufferMapWriteCallback)(WGPUBufferMapAsyncStatus status, uint8_t *data, uint8_t *userdata); -typedef WGPUId WGPUCommandBufferId; +typedef WGPUId WGPUGenericId_CommandBufferHandle; + +typedef WGPUGenericId_CommandBufferHandle WGPUCommandBufferId; typedef struct { WGPUBufferId buffer; @@ -284,7 +294,9 @@ typedef struct { uint32_t image_height; } WGPUBufferCopyView; -typedef WGPUId WGPUTextureId; +typedef WGPUId WGPUGenericId_TextureHandle; + +typedef WGPUGenericId_TextureHandle WGPUTextureId; typedef struct { float x; @@ -306,13 +318,19 @@ typedef struct { uint32_t depth; } WGPUExtent3d; -typedef WGPUId WGPUComputePassId; +typedef WGPUId WGPUGenericId_ComputePassHandle; + +typedef WGPUGenericId_ComputePassHandle WGPUComputePassId; typedef WGPUCommandBufferId WGPUCommandEncoderId; -typedef WGPUId WGPURenderPassId; +typedef WGPUId WGPUGenericId_RenderPassHandle; -typedef WGPUId WGPUTextureViewId; +typedef WGPUGenericId_RenderPassHandle WGPURenderPassId; + +typedef WGPUId WGPUGenericId_TextureViewHandle; + +typedef WGPUGenericId_TextureViewHandle WGPUTextureViewId; typedef struct { double r; @@ -357,11 +375,17 @@ typedef struct { typedef const char *WGPURawString; -typedef WGPUId WGPUComputePipelineId; +typedef WGPUId WGPUGenericId_ComputePipelineHandle; -typedef WGPUId WGPUInstanceId; +typedef WGPUGenericId_ComputePipelineHandle WGPUComputePipelineId; -typedef WGPUId WGPUBindGroupLayoutId; +typedef WGPUId WGPUGenericId_InstanceHandle; + +typedef WGPUGenericId_InstanceHandle WGPUInstanceId; + +typedef WGPUId WGPUGenericId_BindGroupLayoutHandle; + +typedef WGPUGenericId_BindGroupLayoutHandle WGPUBindGroupLayoutId; typedef struct { WGPUBufferId buffer; @@ -369,7 +393,9 @@ typedef struct { WGPUBufferAddress size; } WGPUBufferBinding; -typedef WGPUId WGPUSamplerId; +typedef WGPUId WGPUGenericId_SamplerHandle; + +typedef WGPUGenericId_SamplerHandle WGPUSamplerId; typedef enum { WGPUBindingResource_Buffer, @@ -451,9 +477,13 @@ typedef struct { uint32_t todo; } WGPUCommandEncoderDescriptor; -typedef WGPUId WGPUPipelineLayoutId; +typedef WGPUId WGPUGenericId_PipelineLayoutHandle; -typedef WGPUId WGPUShaderModuleId; +typedef WGPUGenericId_PipelineLayoutHandle WGPUPipelineLayoutId; + +typedef WGPUId WGPUGenericId_ShaderModuleHandle; + +typedef WGPUGenericId_ShaderModuleHandle WGPUShaderModuleId; typedef struct { WGPUShaderModuleId module; @@ -470,7 +500,9 @@ typedef struct { uintptr_t bind_group_layouts_length; } WGPUPipelineLayoutDescriptor; -typedef WGPUId WGPURenderPipelineId; +typedef WGPUId WGPUGenericId_RenderPipelineHandle; + +typedef WGPUGenericId_RenderPipelineHandle WGPURenderPipelineId; typedef struct { WGPUFrontFace front_face; @@ -575,7 +607,9 @@ typedef struct { WGPUU32Array code; } WGPUShaderModuleDescriptor; -typedef WGPUId WGPUSurfaceId; +typedef WGPUId WGPUGenericId_SurfaceHandle; + +typedef WGPUGenericId_SurfaceHandle WGPUSurfaceId; typedef WGPUSurfaceId WGPUSwapChainId; @@ -612,7 +646,9 @@ typedef struct { WGPUPowerPreference power_preference; } WGPURequestAdapterOptions; -typedef WGPUId WGPURenderBundleId; +typedef WGPUId WGPUGenericId_RenderBundleHandle; + +typedef WGPUGenericId_RenderBundleHandle WGPURenderBundleId; typedef struct { WGPUTextureId texture_id; diff --git a/wgpu-native/src/id.rs b/wgpu-native/src/id.rs new file mode 100644 index 000000000..f44edf045 --- /dev/null +++ b/wgpu-native/src/id.rs @@ -0,0 +1,134 @@ +use crate::{Epoch, Index}; +use std::marker::PhantomData; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + +#[repr(C)] +#[derive(Clone, Copy, Debug, Hash, PartialEq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct Id(Index, Epoch); + +#[repr(transparent)] +#[derive(Debug)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +pub struct GenericId(Id, PhantomData); + +impl Copy for GenericId {} + +impl Clone for GenericId { + fn clone(&self) -> Self { + Self(self.0, PhantomData) + } +} + +impl std::hash::Hash for GenericId { + fn hash(&self, state: &mut H) { + self.0.hash(state); + } +} + +impl PartialEq for GenericId { + fn eq(&self, other: &Self) -> bool { + self.0 == other.0 + } +} + +pub trait TypedId { + fn new(index: Index, epoch: Epoch) -> Self; + fn index(&self) -> Index; + fn epoch(&self) -> Epoch; +} + +impl GenericId { + fn raw(self) -> Id { + self.0 + } +} + +impl TypedId for GenericId { + fn new(index: Index, epoch: Epoch) -> Self { + Self(Id(index, epoch), PhantomData) + } + + fn index(&self) -> Index { + (self.raw()).0 + } + + fn epoch(&self) -> Epoch { + (self.raw()).1 + } +} + +#[cfg(not(feature = "gfx-backend-gl"))] +pub type InstanceId = GenericId; + +#[cfg(feature = "gfx-backend-gl")] +pub type InstanceId = SurfaceId; + +#[cfg(not(feature = "gfx-backend-gl"))] +pub type InstanceHandle = back::Instance; + +pub type AdapterId = GenericId; +pub type AdapterHandle = hal::Adapter; + +pub type DeviceId = GenericId; +pub type DeviceHandle = crate::Device; + +pub type QueueId = DeviceId; + +// Resource +pub type BufferId = GenericId; +pub type BufferHandle = crate::Buffer; + +pub type TextureViewId = GenericId; +pub type TextureViewHandle = crate::TextureView; + +pub type TextureId = GenericId; +pub type TextureHandle = crate::Texture; + +pub type SamplerId = GenericId; +pub type SamplerHandle = crate::Sampler; + +// Binding model +pub type BindGroupLayoutId = GenericId; +pub type BindGroupLayoutHandle = crate::BindGroupLayout; + +pub type PipelineLayoutId = GenericId; +pub type PipelineLayoutHandle = crate::PipelineLayout; + +pub type BindGroupId = GenericId; +pub type BindGroupHandle = crate::BindGroup; + +// Pipeline +pub type InputStateId = GenericId; +pub enum InputStateHandle {} + +pub type ShaderModuleId = GenericId; +pub type ShaderModuleHandle = crate::ShaderModule; + +pub type RenderPipelineId = GenericId; +pub type RenderPipelineHandle = crate::RenderPipeline; + +pub type ComputePipelineId = GenericId; +pub type ComputePipelineHandle = crate::ComputePipeline; + +// Command +pub type CommandBufferId = GenericId; +pub type CommandBufferHandle = crate::CommandBuffer; + +pub type CommandEncoderId = CommandBufferId; + +pub type RenderBundleId = GenericId; +pub enum RenderBundleHandle {} + +pub type RenderPassId = GenericId; +pub type RenderPassHandle = crate::RenderPass; + +pub type ComputePassId = GenericId; +pub type ComputePassHandle = crate::ComputePass; + +// Swap chain +pub type SurfaceId = GenericId; +pub type SurfaceHandle = crate::Surface; + +pub type SwapChainId = SurfaceId; diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index 5b0a98bab..4224b8fb3 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -30,6 +30,7 @@ mod pipeline; mod resource; mod swap_chain; mod track; +mod id; pub use self::binding_model::*; pub use self::command::*; @@ -40,9 +41,8 @@ pub use self::instance::*; pub use self::pipeline::*; pub use self::resource::*; pub use self::swap_chain::*; +pub use self::id::*; pub use hal::pso::read_spirv; -#[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; #[cfg(feature = "glutin")] pub use back::glutin; @@ -197,120 +197,3 @@ pub struct U32Array { pub bytes: *const u32, pub length: usize, } - -#[repr(C)] -#[derive(Clone, Copy, Debug, Hash, PartialEq)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -struct Id(Index, Epoch); - -pub trait TypedId { - fn new(index: Index, epoch: Epoch) -> Self; - fn index(&self) -> Index; - fn epoch(&self) -> Epoch; -} - -macro_rules! define_id { - ($i:ident) => { - transparent!($i); - typed_id!($i); - }; -} - -macro_rules! transparent { - ($i:ident) => { - #[repr(transparent)] - #[derive(Clone, Copy, Debug, Hash, PartialEq)] - #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] - pub struct $i(Id); - }; -} - -macro_rules! typed_id { - ($i:ident) => { - impl $i { - fn raw(&self) -> Id { - self.0 - } - } - impl TypedId for $i { - fn new(index: Index, epoch: Epoch) -> Self { - let id = Id(index, epoch); - $i(id) - } - - fn index(&self) -> Index { - (self.raw()).0 - } - - fn epoch(&self) -> Epoch { - (self.raw()).1 - } - } - }; -} - -#[cfg(not(feature = "gfx-backend-gl"))] -define_id!(InstanceId); -#[cfg(not(feature = "gfx-backend-gl"))] -type InstanceHandle = back::Instance; -#[cfg(feature = "gfx-backend-gl")] -pub type InstanceId = SurfaceId; - -define_id!(AdapterId); -type AdapterHandle = hal::Adapter; - -define_id!(DeviceId); -type DeviceHandle = Device; -pub type QueueId = DeviceId; - -define_id!(BufferId); -type BufferHandle = Buffer; - -// Resource -define_id!(TextureViewId); -type TextureViewHandle = TextureView; - -define_id!(TextureId); -type TextureHandle = Texture; - -define_id!(SamplerId); -type SamplerHandle = Sampler; - -// Binding model -define_id!(BindGroupLayoutId); -type BindGroupLayoutHandle = BindGroupLayout; - -define_id!(PipelineLayoutId); -type PipelineLayoutHandle = PipelineLayout; - -define_id!(BindGroupId); -type BindGroupHandle = BindGroup; - -// Pipeline -define_id!(InputStateId); -define_id!(ShaderModuleId); -type ShaderModuleHandle = ShaderModule; - -define_id!(RenderPipelineId); -type RenderPipelineHandle = RenderPipeline; - -define_id!(ComputePipelineId); -type ComputePipelineHandle = ComputePipeline; - -// Command -define_id!(CommandBufferId); -type CommandBufferHandle = CommandBuffer; -pub type CommandEncoderId = CommandBufferId; - -define_id!(RenderBundleId); - -define_id!(RenderPassId); -type RenderPassHandle = RenderPass; - -define_id!(ComputePassId); -type ComputePassHandle = ComputePass; - -// Swap chain -define_id!(SurfaceId); -type SurfaceHandle = Surface; -pub type SwapChainId = SurfaceId;