Skip to content

Commit c3fb5d6

Browse files
Wumpfdavnotdev
authored andcommitted
Expose FeaturesWGPU & FeaturesWebGPU via wgpu crate & implement From (gfx-rs#7086)
* move tests into conditionally compiled mod and place them at the end. add some whitespace for readability * allow creation of `Features` from `FeaturesWGPU` & `FeaturesWebGPU`
1 parent e8b359c commit c3fb5d6

File tree

4 files changed

+138
-79
lines changed

4 files changed

+138
-79
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ changes from this. This means there are also namespaces (as well as the old `Fea
6363
features and webgpu feature (`FeaturesWGPU` and `FeaturesWebGPU` respectively) and `Features::from_internal_flags` which
6464
allow you to be explicit about whether features you need are available on the web too.
6565

66-
By @Vecvec in [#6905](https://github.com/gfx-rs/wgpu/pull/6905).
66+
By @Vecvec in [#6905](https://github.com/gfx-rs/wgpu/pull/6905), [#7086](https://github.com/gfx-rs/wgpu/pull/7086)
6767

6868
##### Refactored internal trace path parameter
6969

wgpu-types/src/features.rs

Lines changed: 123 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -400,70 +400,20 @@ macro_rules! bitflags_array {
400400
)*
401401
)*
402402
}
403-
};
404-
}
405403

406-
#[cfg(feature = "serde")]
407-
#[test]
408-
fn check_hex() {
409-
let mut hex = alloc::string::String::new();
410-
FeatureBits::ALL.write_hex(&mut hex).unwrap();
411-
assert_eq!(
412-
FeatureBits::parse_hex(hex.as_str()).unwrap(),
413-
FeatureBits::ALL
414-
);
415-
hex.clear();
416-
FeatureBits::EMPTY.write_hex(&mut hex).unwrap();
417-
assert_eq!(
418-
FeatureBits::parse_hex(hex.as_str()).unwrap(),
419-
FeatureBits::EMPTY
420-
);
421-
for feature in Features::FLAGS {
422-
hex.clear();
423-
feature.value().bits().write_hex(&mut hex).unwrap();
424-
assert_eq!(
425-
FeatureBits::parse_hex(hex.as_str()).unwrap(),
426-
feature.value().bits(),
427-
"{hex}"
428-
);
429-
}
430-
}
431-
432-
#[test]
433-
fn check_features_display() {
434-
use alloc::format;
435-
let feature = Features::CLEAR_TEXTURE;
436-
assert_eq!(format!("{}", feature), "CLEAR_TEXTURE");
437-
let feature = Features::CLEAR_TEXTURE | Features::BGRA8UNORM_STORAGE;
438-
assert_eq!(format!("{}", feature), "CLEAR_TEXTURE | BGRA8UNORM_STORAGE");
439-
}
440-
441-
#[test]
442-
fn check_features_bits() {
443-
let bits = Features::all().bits();
444-
assert_eq!(Features::from_bits_retain(bits), Features::all());
445-
let bits = Features::empty().bits();
446-
assert_eq!(Features::from_bits_retain(bits), Features::empty());
447-
for feature in Features::FLAGS {
448-
let bits = feature.value().bits();
449-
assert_eq!(Features::from_bits_retain(bits), *feature.value());
450-
}
451-
let bits = Features::all().bits();
452-
assert_eq!(Features::from_bits_truncate(bits), Features::all());
453-
let bits = Features::empty().bits();
454-
assert_eq!(Features::from_bits_truncate(bits), Features::empty());
455-
for feature in Features::FLAGS {
456-
let bits = feature.value().bits();
457-
assert_eq!(Features::from_bits_truncate(bits), *feature.value());
458-
}
459-
let bits = Features::all().bits();
460-
assert_eq!(Features::from_bits(bits).unwrap(), Features::all());
461-
let bits = Features::empty().bits();
462-
assert_eq!(Features::from_bits(bits).unwrap(), Features::empty());
463-
for feature in Features::FLAGS {
464-
let bits = feature.value().bits();
465-
assert_eq!(Features::from_bits(bits).unwrap(), *feature.value());
466-
}
404+
$(
405+
impl From<$inner_name> for Features {
406+
// We need this for structs with only a member.
407+
#[allow(clippy::needless_update)]
408+
fn from($lower_inner_name: $inner_name) -> Self {
409+
Self {
410+
$lower_inner_name,
411+
..Self::empty()
412+
}
413+
}
414+
}
415+
)*
416+
};
467417
}
468418

469419
impl From<FeatureBits> for Features {
@@ -1357,3 +1307,113 @@ impl Features {
13571307
formats
13581308
}
13591309
}
1310+
1311+
#[cfg(test)]
1312+
mod tests {
1313+
use crate::{Features, FeaturesWGPU, FeaturesWebGPU};
1314+
1315+
#[cfg(feature = "serde")]
1316+
#[test]
1317+
fn check_hex() {
1318+
use crate::FeatureBits;
1319+
1320+
use bitflags::{
1321+
parser::{ParseHex as _, WriteHex as _},
1322+
Bits as _,
1323+
};
1324+
1325+
let mut hex = alloc::string::String::new();
1326+
FeatureBits::ALL.write_hex(&mut hex).unwrap();
1327+
assert_eq!(
1328+
FeatureBits::parse_hex(hex.as_str()).unwrap(),
1329+
FeatureBits::ALL
1330+
);
1331+
1332+
hex.clear();
1333+
FeatureBits::EMPTY.write_hex(&mut hex).unwrap();
1334+
assert_eq!(
1335+
FeatureBits::parse_hex(hex.as_str()).unwrap(),
1336+
FeatureBits::EMPTY
1337+
);
1338+
1339+
for feature in Features::FLAGS {
1340+
hex.clear();
1341+
feature.value().bits().write_hex(&mut hex).unwrap();
1342+
assert_eq!(
1343+
FeatureBits::parse_hex(hex.as_str()).unwrap(),
1344+
feature.value().bits(),
1345+
"{hex}"
1346+
);
1347+
}
1348+
}
1349+
1350+
#[test]
1351+
fn check_features_display() {
1352+
use alloc::format;
1353+
1354+
let feature = Features::CLEAR_TEXTURE;
1355+
assert_eq!(format!("{}", feature), "CLEAR_TEXTURE");
1356+
1357+
let feature = Features::CLEAR_TEXTURE | Features::BGRA8UNORM_STORAGE;
1358+
assert_eq!(format!("{}", feature), "CLEAR_TEXTURE | BGRA8UNORM_STORAGE");
1359+
}
1360+
1361+
#[test]
1362+
fn check_features_bits() {
1363+
let bits = Features::all().bits();
1364+
assert_eq!(Features::from_bits_retain(bits), Features::all());
1365+
1366+
let bits = Features::empty().bits();
1367+
assert_eq!(Features::from_bits_retain(bits), Features::empty());
1368+
1369+
for feature in Features::FLAGS {
1370+
let bits = feature.value().bits();
1371+
assert_eq!(Features::from_bits_retain(bits), *feature.value());
1372+
}
1373+
1374+
let bits = Features::all().bits();
1375+
assert_eq!(Features::from_bits_truncate(bits), Features::all());
1376+
1377+
let bits = Features::empty().bits();
1378+
assert_eq!(Features::from_bits_truncate(bits), Features::empty());
1379+
1380+
for feature in Features::FLAGS {
1381+
let bits = feature.value().bits();
1382+
assert_eq!(Features::from_bits_truncate(bits), *feature.value());
1383+
}
1384+
1385+
let bits = Features::all().bits();
1386+
assert_eq!(Features::from_bits(bits).unwrap(), Features::all());
1387+
1388+
let bits = Features::empty().bits();
1389+
assert_eq!(Features::from_bits(bits).unwrap(), Features::empty());
1390+
1391+
for feature in Features::FLAGS {
1392+
let bits = feature.value().bits();
1393+
assert_eq!(Features::from_bits(bits).unwrap(), *feature.value());
1394+
}
1395+
}
1396+
1397+
#[test]
1398+
fn create_features_from_parts() {
1399+
let features: Features = FeaturesWGPU::TEXTURE_ATOMIC.into();
1400+
assert_eq!(features, Features::TEXTURE_ATOMIC);
1401+
1402+
let features: Features = FeaturesWebGPU::TIMESTAMP_QUERY.into();
1403+
assert_eq!(features, Features::TIMESTAMP_QUERY);
1404+
1405+
let features: Features = Features::from(FeaturesWGPU::TEXTURE_ATOMIC)
1406+
| Features::from(FeaturesWebGPU::TIMESTAMP_QUERY);
1407+
assert_eq!(
1408+
features,
1409+
Features::TEXTURE_ATOMIC | Features::TIMESTAMP_QUERY
1410+
);
1411+
assert_eq!(
1412+
features,
1413+
Features::from_internal_flags(
1414+
FeaturesWGPU::TEXTURE_ATOMIC,
1415+
FeaturesWebGPU::TIMESTAMP_QUERY
1416+
)
1417+
);
1418+
}
1419+
}

wgpu-types/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ pub mod instance;
3636
pub mod math;
3737

3838
pub use counters::*;
39-
pub use instance::*;
40-
4139
pub use features::*;
40+
pub use instance::*;
4241

4342
/// Integral type used for buffer offsets.
4443
pub type BufferAddress = u64;

wgpu/src/lib.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,19 @@ pub use wgt::{
6464
Color, ColorTargetState, ColorWrites, CommandBufferDescriptor, CompareFunction,
6565
CompositeAlphaMode, CopyExternalImageDestInfo, CoreCounters, DepthBiasState, DepthStencilState,
6666
DeviceLostReason, DeviceType, DownlevelCapabilities, DownlevelFlags, DownlevelLimits,
67-
Dx12BackendOptions, Dx12Compiler, DynamicOffset, Extent3d, Face, Features, FilterMode,
68-
FrontFace, GlBackendOptions, Gles3MinorVersion, HalCounters, ImageSubresourceRange,
69-
IndexFormat, InstanceDescriptor, InstanceFlags, InternalCounters, Limits, MaintainResult,
70-
MemoryHints, MultisampleState, Origin2d, Origin3d, PipelineStatisticsTypes, PolygonMode,
71-
PowerPreference, PredefinedColorSpace, PresentMode, PresentationTimestamp, PrimitiveState,
72-
PrimitiveTopology, PushConstantRange, QueryType, RenderBundleDepthStencil, SamplerBindingType,
73-
SamplerBorderColor, ShaderLocation, ShaderModel, ShaderRuntimeChecks, ShaderStages,
74-
StencilFaceState, StencilOperation, StencilState, StorageTextureAccess, SurfaceCapabilities,
75-
SurfaceStatus, TexelCopyBufferLayout, TextureAspect, TextureDimension, TextureFormat,
76-
TextureFormatFeatureFlags, TextureFormatFeatures, TextureSampleType, TextureTransition,
77-
TextureUsages, TextureUses, TextureViewDimension, VertexAttribute, VertexFormat,
78-
VertexStepMode, WasmNotSend, WasmNotSendSync, WasmNotSync, COPY_BUFFER_ALIGNMENT,
79-
COPY_BYTES_PER_ROW_ALIGNMENT, MAP_ALIGNMENT, PUSH_CONSTANT_ALIGNMENT,
67+
Dx12BackendOptions, Dx12Compiler, DynamicOffset, Extent3d, Face, Features, FeaturesWGPU,
68+
FeaturesWebGPU, FilterMode, FrontFace, GlBackendOptions, Gles3MinorVersion, HalCounters,
69+
ImageSubresourceRange, IndexFormat, InstanceDescriptor, InstanceFlags, InternalCounters,
70+
Limits, MaintainResult, MemoryHints, MultisampleState, Origin2d, Origin3d,
71+
PipelineStatisticsTypes, PolygonMode, PowerPreference, PredefinedColorSpace, PresentMode,
72+
PresentationTimestamp, PrimitiveState, PrimitiveTopology, PushConstantRange, QueryType,
73+
RenderBundleDepthStencil, SamplerBindingType, SamplerBorderColor, ShaderLocation, ShaderModel,
74+
ShaderRuntimeChecks, ShaderStages, StencilFaceState, StencilOperation, StencilState,
75+
StorageTextureAccess, SurfaceCapabilities, SurfaceStatus, TexelCopyBufferLayout, TextureAspect,
76+
TextureDimension, TextureFormat, TextureFormatFeatureFlags, TextureFormatFeatures,
77+
TextureSampleType, TextureTransition, TextureUsages, TextureUses, TextureViewDimension,
78+
VertexAttribute, VertexFormat, VertexStepMode, WasmNotSend, WasmNotSendSync, WasmNotSync,
79+
COPY_BUFFER_ALIGNMENT, COPY_BYTES_PER_ROW_ALIGNMENT, MAP_ALIGNMENT, PUSH_CONSTANT_ALIGNMENT,
8080
QUERY_RESOLVE_BUFFER_ALIGNMENT, QUERY_SET_MAX_QUERIES, QUERY_SIZE, VERTEX_STRIDE_ALIGNMENT,
8181
};
8282
#[expect(deprecated)]

0 commit comments

Comments
 (0)