mirror of
https://github.com/gfx-rs/wgpu.git
synced 2024-11-22 14:55:05 +00:00
Merge #294
294: Manually expand ID macros r=kvark a=grovesNL Fixes #293 Co-authored-by: Joshua Groves <josh@joshgroves.com>
This commit is contained in:
commit
cb56ce3a0a
@ -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;
|
||||
|
72
ffi/wgpu.h
72
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;
|
||||
|
134
wgpu-native/src/id.rs
Normal file
134
wgpu-native/src/id.rs
Normal file
@ -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<T>(Id, PhantomData<T>);
|
||||
|
||||
impl<T> Copy for GenericId<T> {}
|
||||
|
||||
impl<T> Clone for GenericId<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::hash::Hash for GenericId<T> {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
self.0.hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PartialEq for GenericId<T> {
|
||||
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<T> GenericId<T> {
|
||||
fn raw(self) -> Id {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> TypedId for GenericId<T> {
|
||||
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<InstanceHandle>;
|
||||
|
||||
#[cfg(feature = "gfx-backend-gl")]
|
||||
pub type InstanceId = SurfaceId;
|
||||
|
||||
#[cfg(not(feature = "gfx-backend-gl"))]
|
||||
pub type InstanceHandle = back::Instance;
|
||||
|
||||
pub type AdapterId = GenericId<AdapterHandle>;
|
||||
pub type AdapterHandle = hal::Adapter<back::Backend>;
|
||||
|
||||
pub type DeviceId = GenericId<DeviceHandle>;
|
||||
pub type DeviceHandle = crate::Device<back::Backend>;
|
||||
|
||||
pub type QueueId = DeviceId;
|
||||
|
||||
// Resource
|
||||
pub type BufferId = GenericId<BufferHandle>;
|
||||
pub type BufferHandle = crate::Buffer<back::Backend>;
|
||||
|
||||
pub type TextureViewId = GenericId<TextureViewHandle>;
|
||||
pub type TextureViewHandle = crate::TextureView<back::Backend>;
|
||||
|
||||
pub type TextureId = GenericId<TextureHandle>;
|
||||
pub type TextureHandle = crate::Texture<back::Backend>;
|
||||
|
||||
pub type SamplerId = GenericId<SamplerHandle>;
|
||||
pub type SamplerHandle = crate::Sampler<back::Backend>;
|
||||
|
||||
// Binding model
|
||||
pub type BindGroupLayoutId = GenericId<BindGroupLayoutHandle>;
|
||||
pub type BindGroupLayoutHandle = crate::BindGroupLayout<back::Backend>;
|
||||
|
||||
pub type PipelineLayoutId = GenericId<PipelineLayoutHandle>;
|
||||
pub type PipelineLayoutHandle = crate::PipelineLayout<back::Backend>;
|
||||
|
||||
pub type BindGroupId = GenericId<BindGroupHandle>;
|
||||
pub type BindGroupHandle = crate::BindGroup<back::Backend>;
|
||||
|
||||
// Pipeline
|
||||
pub type InputStateId = GenericId<InputStateHandle>;
|
||||
pub enum InputStateHandle {}
|
||||
|
||||
pub type ShaderModuleId = GenericId<ShaderModuleHandle>;
|
||||
pub type ShaderModuleHandle = crate::ShaderModule<back::Backend>;
|
||||
|
||||
pub type RenderPipelineId = GenericId<RenderPipelineHandle>;
|
||||
pub type RenderPipelineHandle = crate::RenderPipeline<back::Backend>;
|
||||
|
||||
pub type ComputePipelineId = GenericId<ComputePipelineHandle>;
|
||||
pub type ComputePipelineHandle = crate::ComputePipeline<back::Backend>;
|
||||
|
||||
// Command
|
||||
pub type CommandBufferId = GenericId<CommandBufferHandle>;
|
||||
pub type CommandBufferHandle = crate::CommandBuffer<back::Backend>;
|
||||
|
||||
pub type CommandEncoderId = CommandBufferId;
|
||||
|
||||
pub type RenderBundleId = GenericId<RenderBundleHandle>;
|
||||
pub enum RenderBundleHandle {}
|
||||
|
||||
pub type RenderPassId = GenericId<RenderPassHandle>;
|
||||
pub type RenderPassHandle = crate::RenderPass<back::Backend>;
|
||||
|
||||
pub type ComputePassId = GenericId<ComputePassHandle>;
|
||||
pub type ComputePassHandle = crate::ComputePass<back::Backend>;
|
||||
|
||||
// Swap chain
|
||||
pub type SurfaceId = GenericId<SurfaceHandle>;
|
||||
pub type SurfaceHandle = crate::Surface<back::Backend>;
|
||||
|
||||
pub type SwapChainId = SurfaceId;
|
@ -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<back::Backend>;
|
||||
|
||||
define_id!(DeviceId);
|
||||
type DeviceHandle = Device<back::Backend>;
|
||||
pub type QueueId = DeviceId;
|
||||
|
||||
define_id!(BufferId);
|
||||
type BufferHandle = Buffer<back::Backend>;
|
||||
|
||||
// Resource
|
||||
define_id!(TextureViewId);
|
||||
type TextureViewHandle = TextureView<back::Backend>;
|
||||
|
||||
define_id!(TextureId);
|
||||
type TextureHandle = Texture<back::Backend>;
|
||||
|
||||
define_id!(SamplerId);
|
||||
type SamplerHandle = Sampler<back::Backend>;
|
||||
|
||||
// Binding model
|
||||
define_id!(BindGroupLayoutId);
|
||||
type BindGroupLayoutHandle = BindGroupLayout<back::Backend>;
|
||||
|
||||
define_id!(PipelineLayoutId);
|
||||
type PipelineLayoutHandle = PipelineLayout<back::Backend>;
|
||||
|
||||
define_id!(BindGroupId);
|
||||
type BindGroupHandle = BindGroup<back::Backend>;
|
||||
|
||||
// Pipeline
|
||||
define_id!(InputStateId);
|
||||
define_id!(ShaderModuleId);
|
||||
type ShaderModuleHandle = ShaderModule<back::Backend>;
|
||||
|
||||
define_id!(RenderPipelineId);
|
||||
type RenderPipelineHandle = RenderPipeline<back::Backend>;
|
||||
|
||||
define_id!(ComputePipelineId);
|
||||
type ComputePipelineHandle = ComputePipeline<back::Backend>;
|
||||
|
||||
// Command
|
||||
define_id!(CommandBufferId);
|
||||
type CommandBufferHandle = CommandBuffer<back::Backend>;
|
||||
pub type CommandEncoderId = CommandBufferId;
|
||||
|
||||
define_id!(RenderBundleId);
|
||||
|
||||
define_id!(RenderPassId);
|
||||
type RenderPassHandle = RenderPass<back::Backend>;
|
||||
|
||||
define_id!(ComputePassId);
|
||||
type ComputePassHandle = ComputePass<back::Backend>;
|
||||
|
||||
// Swap chain
|
||||
define_id!(SurfaceId);
|
||||
type SurfaceHandle = Surface<back::Backend>;
|
||||
pub type SwapChainId = SurfaceId;
|
||||
|
Loading…
Reference in New Issue
Block a user