Skip to content

Commit

Permalink
Update library to latest webgpu-native headers
Browse files Browse the repository at this point in the history
This goes up to webgpu-native/webgpu-headers@2b59747

Things I *didn't* do:

* I didn't update the library to make sure "instance dropped" callback
  error codes are guaranteed to happen, like they seem to be in Dawn.

List of changes (roughly in order of header commits):

Various enum and struct renames

Updated callbacks to use the new *CallbackInfo structs and 2-userdata system. Also updated functions to return WGPUFuture, though the WGPUFuture thing is just stubbed out at the moment as I don't think wgpu-core has the necessary functionality for it. wgpuInstanceWaitAny is unimplemented!()

DepthClipControl merged into PrimitiveState, related code simplified.

Updated depthWriteEnabled to use an optional bool, mostly matters due to added validation.

Add TODOs for missing features (sliced 3D compressed textures)

*Reference() became *AddRef()

Added unorm10-10-10-2 vertex format

Usage field in TextureViewDescriptor, just used for validation as wgpu-core doesn't allow specifying it anyways.

Removed maxInterStageShaderComponents

Added clang_macro_fallback to bindgen config, since the headers switched to using UINT32_MAX etc. UINT64_MAX still doesn't work so I had to manually define those.

Renamed flags enums. Added a conversion helper function to convert them from u64 -> u32 for mapping. (means added direct dependency on bitflags crate)

Removed device argument from (unimplemented) wgpuGetProcAddress

Suboptimal surface texture acquisition moved to enum return value, was easy since wgpu-core already returns it like that.

"Undefined" present mode added, it just selects FIFO.
  • Loading branch information
PJB3005 committed Sep 19, 2024
1 parent fad19f5 commit 880cca0
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 216 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ log = "0.4"
thiserror = "1"
parking_lot = "0.12"
smallvec = "1"
bitflags = "2.6.0"

[build-dependencies]
bindgen = "0.70"
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ fn main() {
.prepend_enum_name(false)
.size_t_is_usize(true)
.ignore_functions()
.layout_tests(true);
.layout_tests(true)
.clang_macro_fallback();

for (old_name, new_name) in types_to_rename {
let line = format!("pub type {old_name} = *const crate::{new_name};");
Expand Down
2 changes: 1 addition & 1 deletion ffi/webgpu-headers
4 changes: 2 additions & 2 deletions ffi/wgpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ typedef struct WGPUSupportedLimitsExtras {
} WGPUSupportedLimitsExtras;

typedef struct WGPUPushConstantRange {
WGPUShaderStageFlags stages;
WGPUShaderStage stages;
uint32_t start;
uint32_t end;
} WGPUPushConstantRange;
Expand Down Expand Up @@ -281,7 +281,7 @@ void wgpuSetLogLevel(WGPULogLevel level);

uint32_t wgpuGetVersion(void);

void wgpuRenderPassEncoderSetPushConstants(WGPURenderPassEncoder encoder, WGPUShaderStageFlags stages, uint32_t offset, uint32_t sizeBytes, void const * data);
void wgpuRenderPassEncoderSetPushConstants(WGPURenderPassEncoder encoder, WGPUShaderStage stages, uint32_t offset, uint32_t sizeBytes, void const * data);

void wgpuRenderPassEncoderMultiDrawIndirect(WGPURenderPassEncoder encoder, WGPUBuffer buffer, uint64_t offset, uint32_t count);
void wgpuRenderPassEncoderMultiDrawIndexedIndirect(WGPURenderPassEncoder encoder, WGPUBuffer buffer, uint64_t offset, uint32_t count);
Expand Down
73 changes: 37 additions & 36 deletions src/conv.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::utils::{make_slice, ptr_into_label, ptr_into_pathbuf};
use crate::{follow_chain, map_enum};
use crate::{follow_chain, map_enum, new_userdata};
use crate::{native, UncapturedErrorCallback};
use std::num::{NonZeroIsize, NonZeroU32, NonZeroU64};
use std::ptr::NonNull;
Expand Down Expand Up @@ -164,7 +164,8 @@ map_enum!(
Sint32,
Sint32x2,
Sint32x3,
Sint32x4
Sint32x4,
Unorm10_10_10_2
);

#[cfg(feature = "glsl")]
Expand Down Expand Up @@ -208,9 +209,10 @@ map_enum!(
ReadWrite
);

pub const WGPU_WHOLE_SIZE: ::std::os::raw::c_ulonglong = native::WGPU_WHOLE_SIZE as _;
pub const WGPU_LIMIT_U64_UNDEFINED: ::std::os::raw::c_ulonglong =
native::WGPU_LIMIT_U64_UNDEFINED as _;
// These are defined as UINT64_MAX in the header, but bindgen currently can't process that define.
// See https://github.com/rust-lang/rust-bindgen/issues/2822
pub const WGPU_WHOLE_SIZE: u64 = u64::MAX;
pub const WGPU_LIMIT_U64_UNDEFINED: u64 = u64::MAX;
// it's SIZE_MAX in headers but it's not available in some compilers
pub const WGPU_WHOLE_MAP_SIZE: usize = usize::MAX;

Expand Down Expand Up @@ -338,7 +340,7 @@ pub(crate) fn map_device_descriptor<'a>(
None => None,
callback => Some(UncapturedErrorCallback {
callback,
userdata: des.uncapturedErrorCallbackInfo.userdata,
userdata: new_userdata!(des.uncapturedErrorCallbackInfo),
}),
},
)
Expand All @@ -363,7 +365,7 @@ pub unsafe fn map_pipeline_layout_descriptor<'a>(
make_slice(extras.pushConstantRanges, extras.pushConstantRangeCount)
.iter()
.map(|range| wgt::PushConstantRange {
stages: wgt::ShaderStages::from_bits(range.stages)
stages: from_u64_bits(range.stages)
.expect("invalid shader stage for push constant range"),
range: range.start..range.end,
})
Expand Down Expand Up @@ -408,7 +410,6 @@ pub fn write_limits_struct(
limits.maxVertexBufferArrayStride = wgt_limits.max_vertex_buffer_array_stride;
limits.minUniformBufferOffsetAlignment = wgt_limits.min_uniform_buffer_offset_alignment;
limits.minStorageBufferOffsetAlignment = wgt_limits.min_storage_buffer_offset_alignment;
limits.maxInterStageShaderComponents = wgt_limits.max_inter_stage_shader_components;
// TODO: not yet in wgt
// limits.maxInterStageShaderVariables = wgt_limits.max_inter_stage_shader_variables;
// TODO: not yet in wgt
Expand Down Expand Up @@ -494,10 +495,10 @@ pub fn map_required_limits(
if limits.maxUniformBuffersPerShaderStage != native::WGPU_LIMIT_U32_UNDEFINED {
wgt_limits.max_uniform_buffers_per_shader_stage = limits.maxUniformBuffersPerShaderStage;
}
if limits.maxUniformBufferBindingSize != native::WGPU_LIMIT_U64_UNDEFINED as u64 {
if limits.maxUniformBufferBindingSize != WGPU_LIMIT_U64_UNDEFINED {
wgt_limits.max_uniform_buffer_binding_size = limits.maxUniformBufferBindingSize as u32;
}
if limits.maxStorageBufferBindingSize != native::WGPU_LIMIT_U64_UNDEFINED as u64 {
if limits.maxStorageBufferBindingSize != WGPU_LIMIT_U64_UNDEFINED {
wgt_limits.max_storage_buffer_binding_size = limits.maxStorageBufferBindingSize as u32;
}
if limits.minUniformBufferOffsetAlignment != native::WGPU_LIMIT_U32_UNDEFINED {
Expand All @@ -509,7 +510,7 @@ pub fn map_required_limits(
if limits.maxVertexBuffers != native::WGPU_LIMIT_U32_UNDEFINED {
wgt_limits.max_vertex_buffers = limits.maxVertexBuffers;
}
if limits.maxBufferSize != native::WGPU_LIMIT_U64_UNDEFINED as u64 {
if limits.maxBufferSize != WGPU_LIMIT_U64_UNDEFINED {
wgt_limits.max_buffer_size = limits.maxBufferSize;
}
if limits.maxVertexAttributes != native::WGPU_LIMIT_U32_UNDEFINED {
Expand All @@ -518,9 +519,6 @@ pub fn map_required_limits(
if limits.maxVertexBufferArrayStride != native::WGPU_LIMIT_U32_UNDEFINED {
wgt_limits.max_vertex_buffer_array_stride = limits.maxVertexBufferArrayStride;
}
if limits.maxInterStageShaderComponents != native::WGPU_LIMIT_U32_UNDEFINED {
wgt_limits.max_inter_stage_shader_components = limits.maxInterStageShaderComponents;
}
// TODO: not yet in wgt
// if limits.maxInterStageShaderVariables != native::WGPU_LIMIT_U32_UNDEFINED {
// wgt_limits.max_inter_stage_shader_variables = limits.maxInterStageShaderVariables;
Expand Down Expand Up @@ -576,8 +574,8 @@ pub enum ShaderParseError {
#[inline]
pub fn map_shader_module<'a>(
_: &native::WGPUShaderModuleDescriptor,
spirv: Option<&native::WGPUShaderModuleSPIRVDescriptor>,
wgsl: Option<&native::WGPUShaderModuleWGSLDescriptor>,
spirv: Option<&native::WGPUShaderSourceSPIRV>,
wgsl: Option<&native::WGPUShaderSourceWGSL>,
glsl: Option<&native::WGPUShaderModuleGLSLDescriptor>,
) -> Result<wgc::pipeline::ShaderModuleSource<'a>, ShaderParseError> {
#[cfg(feature = "wgsl")]
Expand Down Expand Up @@ -975,18 +973,6 @@ pub fn map_stencil_face_state(
}
}

#[inline]
pub fn map_primitive_state(
_: &native::WGPUPrimitiveState,
depth_clip_control: Option<&native::WGPUPrimitiveDepthClipControl>,
) -> bool {
if let Some(depth_clip_control) = depth_clip_control {
return depth_clip_control.unclippedDepth != 0;
}

false
}

#[inline]
pub fn map_storage_report(report: &wgc::registry::RegistryReport) -> native::WGPURegistryReport {
native::WGPURegistryReport {
Expand Down Expand Up @@ -1230,8 +1216,10 @@ pub fn map_feature(feature: native::WGPUFeatureName) -> Option<wgt::Features> {
native::WGPUFeatureName_DepthClipControl => Some(Features::DEPTH_CLIP_CONTROL),
native::WGPUFeatureName_Depth32FloatStencil8 => Some(Features::DEPTH32FLOAT_STENCIL8),
native::WGPUFeatureName_TextureCompressionBC => Some(Features::TEXTURE_COMPRESSION_BC),
// TODO: WGPUFeatureName_TextureCompressionBCSliced3D
native::WGPUFeatureName_TextureCompressionETC2 => Some(Features::TEXTURE_COMPRESSION_ETC2),
native::WGPUFeatureName_TextureCompressionASTC => Some(Features::TEXTURE_COMPRESSION_ASTC),
// TODO: WGPUFeatureName_TextureCompressionASTCSliced3D
native::WGPUFeatureName_TimestampQuery => Some(Features::TIMESTAMP_QUERY),
native::WGPUFeatureName_IndirectFirstInstance => Some(Features::INDIRECT_FIRST_INSTANCE),
native::WGPUFeatureName_ShaderF16 => Some(Features::SHADER_F16),
Expand Down Expand Up @@ -1476,7 +1464,7 @@ pub fn map_bind_group_layout_entry(
wgt::BindGroupLayoutEntry {
ty,
binding: entry.binding,
visibility: wgt::ShaderStages::from_bits(entry.visibility)
visibility: from_u64_bits(entry.visibility)
.expect("invalid visibility for bind group layout entry"),
count: extras.and_then(|v| NonZeroU32::new(v.count)),
}
Expand Down Expand Up @@ -1582,12 +1570,12 @@ pub enum CreateSurfaceParams {

pub unsafe fn map_surface(
_: &native::WGPUSurfaceDescriptor,
win: Option<&native::WGPUSurfaceDescriptorFromWindowsHWND>,
xcb: Option<&native::WGPUSurfaceDescriptorFromXcbWindow>,
xlib: Option<&native::WGPUSurfaceDescriptorFromXlibWindow>,
wl: Option<&native::WGPUSurfaceDescriptorFromWaylandSurface>,
_metal: Option<&native::WGPUSurfaceDescriptorFromMetalLayer>,
android: Option<&native::WGPUSurfaceDescriptorFromAndroidNativeWindow>,
win: Option<&native::WGPUSurfaceSourceWindowsHWND>,
xcb: Option<&native::WGPUSurfaceSourceXCBWindow>,
xlib: Option<&native::WGPUSurfaceSourceXlibWindow>,
wl: Option<&native::WGPUSurfaceSourceWaylandSurface>,
_metal: Option<&native::WGPUSurfaceSourceMetalLayer>,
android: Option<&native::WGPUSurfaceSourceAndroidNativeWindow>,
) -> CreateSurfaceParams {
if let Some(win) = win {
let display_handle = raw_window_handle::WindowsDisplayHandle::new();
Expand Down Expand Up @@ -1660,13 +1648,18 @@ pub fn map_surface_configuration(
config: &native::WGPUSurfaceConfiguration,
extras: Option<&native::WGPUSurfaceConfigurationExtras>,
) -> wgt::SurfaceConfiguration<Vec<wgt::TextureFormat>> {
let present_mode = match config.presentMode {
native::WGPUPresentMode_Undefined => wgt::PresentMode::Fifo,
_ => map_present_mode(config.presentMode),
};

wgt::SurfaceConfiguration {
usage: map_texture_usage_flags(config.usage as native::WGPUTextureUsage),
format: map_texture_format(config.format)
.expect("invalid format for surface configuration"),
width: config.width,
height: config.height,
present_mode: map_present_mode(config.presentMode),
present_mode,
alpha_mode: map_composite_alpha_mode(config.alphaMode)
.expect("invalid alpha mode for surface configuration"),
view_formats: make_slice(config.viewFormats, config.viewFormatCount)
Expand Down Expand Up @@ -1701,3 +1694,11 @@ pub fn map_adapter_type(device_type: wgt::DeviceType) -> native::WGPUAdapterType
wgt::DeviceType::Cpu => native::WGPUAdapterType_CPU,
}
}

pub fn from_u64_bits<T: bitflags::Flags<Bits = u32>>(value: u64) -> Option<T> {
if value > u32::MAX.into() {
return None;
}

T::from_bits(value as u32)
}
Loading

0 comments on commit 880cca0

Please sign in to comment.