Mesh shaders - initial wgpu hal changes (#7089)

* Initial(untested commit), vulkan and gles only supported

* Maybe fixed compiles for metal and dx12

* Hopefully fixed compiles for other backends and updated to functional(?) vulkan thing

* Fixed the clippy warning

* Fixed silly documentation mistake

* Fixed issue with multiview feature

* Dummy commit for dummy CI

The CI pooped itself, hopefully this fixes that. Will probably be undone either way.

* Re trigger CI checks, to avoid #7126

* Changes based on code review

* Fixed clippy warning, broken cargo.lock

* Unfucked cargo.lock for real this time

* Switched match to if-let in accordance with review

* Updated changelog

* Fix CI error

Done from web out of impatience

* CI is very angry 😡

Made CI less angry by fixing formatting(hopefully). This commit was also done from GitHub web.

* Removed comment in following request

* Update wgpu-hal/src/vulkan/adapter.rs

---------

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
This commit is contained in:
SupaMaggie70IncorporatedandConnor Fitzgerald authored and GitHub committed 2025-03-04 00:12:38 +00:00
1 parent 8394b10e6b
commit a6109bf69b
17 files changed
+696 -11

No files matched your search

+2
View File
@@ -130,6 +130,8 @@ By @jamienicol in [#6929](https://github.com/gfx-rs/wgpu/pull/6929) and [#7080](
### New Features
- Added mesh shader support to `wgpu_hal`. By @SupaMaggie70Incorporated in [#7089](https://github.com/gfx-rs/wgpu/pull/7089)
#### General
- It is now possible to create a dummy `wgpu` device even when no GPU is available. This may be useful for testing of code which manages graphics resources. Currently, it supports reading and writing buffers, and other resource types can be created but do nothing.
+26
View File
@@ -1179,6 +1179,14 @@ impl crate::CommandEncoder for super::CommandEncoder {
)
}
}
unsafe fn draw_mesh_tasks(
&mut self,
_group_count_x: u32,
_group_count_y: u32,
_group_count_z: u32,
) {
unreachable!()
}
unsafe fn draw_indirect(
&mut self,
buffer: &super::Buffer,
@@ -1215,6 +1223,14 @@ impl crate::CommandEncoder for super::CommandEncoder {
)
}
}
unsafe fn draw_mesh_tasks_indirect(
&mut self,
_buffer: &<Self::A as crate::Api>::Buffer,
_offset: wgt::BufferAddress,
_draw_count: u32,
) {
unreachable!()
}
unsafe fn draw_indirect_count(
&mut self,
buffer: &super::Buffer,
@@ -1255,6 +1271,16 @@ impl crate::CommandEncoder for super::CommandEncoder {
)
}
}
unsafe fn draw_mesh_tasks_indirect_count(
&mut self,
_buffer: &<Self::A as crate::Api>::Buffer,
_offset: wgt::BufferAddress,
_count_buffer: &<Self::A as crate::Api>::Buffer,
_count_offset: wgt::BufferAddress,
_max_count: u32,
) {
unreachable!()
}
// compute
+12
View File
@@ -1883,6 +1883,18 @@ impl crate::Device for super::Device {
vertex_strides,
})
}
unsafe fn create_mesh_pipeline(
&self,
_desc: &crate::MeshPipelineDescriptor<
<Self::A as crate::Api>::PipelineLayout,
<Self::A as crate::Api>::ShaderModule,
<Self::A as crate::Api>::PipelineCache,
>,
) -> Result<<Self::A as crate::Api>::RenderPipeline, crate::PipelineError> {
unreachable!()
}
unsafe fn destroy_render_pipeline(&self, _pipeline: super::RenderPipeline) {
self.counters.render_pipelines.sub(1);
}
+61
View File
@@ -130,6 +130,12 @@ pub trait DynCommandEncoder: DynResource + core::fmt::Debug {
first_instance: u32,
instance_count: u32,
);
unsafe fn draw_mesh_tasks(
&mut self,
group_count_x: u32,
group_count_y: u32,
group_count_z: u32,
);
unsafe fn draw_indirect(
&mut self,
buffer: &dyn DynBuffer,
@@ -142,6 +148,12 @@ pub trait DynCommandEncoder: DynResource + core::fmt::Debug {
offset: wgt::BufferAddress,
draw_count: u32,
);
unsafe fn draw_mesh_tasks_indirect(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
draw_count: u32,
);
unsafe fn draw_indirect_count(
&mut self,
buffer: &dyn DynBuffer,
@@ -158,6 +170,14 @@ pub trait DynCommandEncoder: DynResource + core::fmt::Debug {
count_offset: wgt::BufferAddress,
max_count: u32,
);
unsafe fn draw_mesh_tasks_indirect_count(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
count_buffer: &dyn DynBuffer,
count_offset: wgt::BufferAddress,
max_count: u32,
);
unsafe fn begin_compute_pass(&mut self, desc: &ComputePassDescriptor<dyn DynQuerySet>);
unsafe fn end_compute_pass(&mut self);
@@ -473,6 +493,15 @@ impl<C: CommandEncoder + DynResource> DynCommandEncoder for C {
};
}
unsafe fn draw_mesh_tasks(
&mut self,
group_count_x: u32,
group_count_y: u32,
group_count_z: u32,
) {
unsafe { C::draw_mesh_tasks(self, group_count_x, group_count_y, group_count_z) };
}
unsafe fn draw_indirect(
&mut self,
buffer: &dyn DynBuffer,
@@ -493,6 +522,16 @@ impl<C: CommandEncoder + DynResource> DynCommandEncoder for C {
unsafe { C::draw_indexed_indirect(self, buffer, offset, draw_count) };
}
unsafe fn draw_mesh_tasks_indirect(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
draw_count: u32,
) {
let buffer = buffer.expect_downcast_ref();
unsafe { C::draw_mesh_tasks_indirect(self, buffer, offset, draw_count) };
}
unsafe fn draw_indirect_count(
&mut self,
buffer: &dyn DynBuffer,
@@ -530,6 +569,28 @@ impl<C: CommandEncoder + DynResource> DynCommandEncoder for C {
};
}
unsafe fn draw_mesh_tasks_indirect_count(
&mut self,
buffer: &dyn DynBuffer,
offset: wgt::BufferAddress,
count_buffer: &dyn DynBuffer,
count_offset: wgt::BufferAddress,
max_count: u32,
) {
let buffer = buffer.expect_downcast_ref();
let count_buffer = count_buffer.expect_downcast_ref();
unsafe {
C::draw_mesh_tasks_indirect_count(
self,
buffer,
offset,
count_buffer,
count_offset,
max_count,
)
};
}
unsafe fn begin_compute_pass(&mut self, desc: &ComputePassDescriptor<dyn DynQuerySet>) {
let desc = ComputePassDescriptor {
label: desc.label,
+38 -4
View File
@@ -4,10 +4,10 @@ use crate::{
AccelerationStructureBuildSizes, AccelerationStructureDescriptor, Api, BindGroupDescriptor,
BindGroupLayoutDescriptor, BufferDescriptor, BufferMapping, CommandEncoderDescriptor,
ComputePipelineDescriptor, Device, DeviceError, FenceValue,
GetAccelerationStructureBuildSizesDescriptor, Label, MemoryRange, PipelineCacheDescriptor,
PipelineCacheError, PipelineError, PipelineLayoutDescriptor, RenderPipelineDescriptor,
SamplerDescriptor, ShaderError, ShaderInput, ShaderModuleDescriptor, TextureDescriptor,
TextureViewDescriptor, TlasInstance,
GetAccelerationStructureBuildSizesDescriptor, Label, MemoryRange, MeshPipelineDescriptor,
PipelineCacheDescriptor, PipelineCacheError, PipelineError, PipelineLayoutDescriptor,
RenderPipelineDescriptor, SamplerDescriptor, ShaderError, ShaderInput, ShaderModuleDescriptor,
TextureDescriptor, TextureViewDescriptor, TlasInstance,
};
use super::{
@@ -100,6 +100,14 @@ pub trait DynDevice: DynResource {
dyn DynPipelineCache,
>,
) -> Result<Box<dyn DynRenderPipeline>, PipelineError>;
unsafe fn create_mesh_pipeline(
&self,
desc: &MeshPipelineDescriptor<
dyn DynPipelineLayout,
dyn DynShaderModule,
dyn DynPipelineCache,
>,
) -> Result<Box<dyn DynRenderPipeline>, PipelineError>;
unsafe fn destroy_render_pipeline(&self, pipeline: Box<dyn DynRenderPipeline>);
unsafe fn create_compute_pipeline(
@@ -393,6 +401,32 @@ impl<D: Device + DynResource> DynDevice for D {
.map(|b| -> Box<dyn DynRenderPipeline> { Box::new(b) })
}
unsafe fn create_mesh_pipeline(
&self,
desc: &MeshPipelineDescriptor<
dyn DynPipelineLayout,
dyn DynShaderModule,
dyn DynPipelineCache,
>,
) -> Result<Box<dyn DynRenderPipeline>, PipelineError> {
let desc = MeshPipelineDescriptor {
label: desc.label,
layout: desc.layout.expect_downcast_ref(),
task_stage: desc.task_stage.clone().map(|f| f.expect_downcast()),
mesh_stage: desc.mesh_stage.clone().expect_downcast(),
primitive: desc.primitive,
depth_stencil: desc.depth_stencil.clone(),
multisample: desc.multisample,
fragment_stage: desc.fragment_stage.clone().map(|f| f.expect_downcast()),
color_targets: desc.color_targets,
multiview: desc.multiview,
cache: desc.cache.map(|c| c.expect_downcast_ref()),
};
unsafe { D::create_mesh_pipeline(self, &desc) }
.map(|b| -> Box<dyn DynRenderPipeline> { Box::new(b) })
}
unsafe fn destroy_render_pipeline(&self, pipeline: Box<dyn DynRenderPipeline>) {
unsafe { D::destroy_render_pipeline(self, pipeline.unbox()) };
}
+26
View File
@@ -1073,6 +1073,14 @@ impl crate::CommandEncoder for super::CommandEncoder {
first_instance_location: self.state.first_instance_location.clone(),
});
}
unsafe fn draw_mesh_tasks(
&mut self,
_group_count_x: u32,
_group_count_y: u32,
_group_count_z: u32,
) {
unreachable!()
}
unsafe fn draw_indirect(
&mut self,
buffer: &super::Buffer,
@@ -1116,6 +1124,14 @@ impl crate::CommandEncoder for super::CommandEncoder {
});
}
}
unsafe fn draw_mesh_tasks_indirect(
&mut self,
_buffer: &<Self::A as crate::Api>::Buffer,
_offset: wgt::BufferAddress,
_draw_count: u32,
) {
unreachable!()
}
unsafe fn draw_indirect_count(
&mut self,
_buffer: &super::Buffer,
@@ -1136,6 +1152,16 @@ impl crate::CommandEncoder for super::CommandEncoder {
) {
unreachable!()
}
unsafe fn draw_mesh_tasks_indirect_count(
&mut self,
_buffer: &<Self::A as crate::Api>::Buffer,
_offset: wgt::BufferAddress,
_count_buffer: &<Self::A as crate::Api>::Buffer,
_count_offset: wgt::BufferAddress,
_max_count: u32,
) {
unreachable!()
}
// compute
+10
View File
@@ -1415,6 +1415,16 @@ impl crate::Device for super::Device {
alpha_to_coverage_enabled: desc.multisample.alpha_to_coverage_enabled,
})
}
unsafe fn create_mesh_pipeline(
&self,
_desc: &crate::MeshPipelineDescriptor<
<Self::A as crate::Api>::PipelineLayout,
<Self::A as crate::Api>::ShaderModule,
<Self::A as crate::Api>::PipelineCache,
>,
) -> Result<<Self::A as crate::Api>::RenderPipeline, crate::PipelineError> {
unreachable!()
}
unsafe fn destroy_render_pipeline(&self, pipeline: super::RenderPipeline) {
// If the pipeline only has 2 strong references remaining, they're `pipeline` and `program_cache`
+58 -1
View File
@@ -305,7 +305,8 @@ use wgt::WasmNotSendSync;
// - Vertex + Fragment
// - Compute
pub const MAX_CONCURRENT_SHADER_STAGES: usize = 2;
// Task + Mesh + Fragment
pub const MAX_CONCURRENT_SHADER_STAGES: usize = 3;
pub const MAX_ANISOTROPY: u8 = 16;
pub const MAX_BIND_GROUPS: usize = 8;
pub const MAX_VERTEX_BUFFERS: usize = 16;
@@ -901,6 +902,15 @@ pub trait Device: WasmNotSendSync {
<Self::A as Api>::PipelineCache,
>,
) -> Result<<Self::A as Api>::RenderPipeline, PipelineError>;
#[allow(clippy::type_complexity)]
unsafe fn create_mesh_pipeline(
&self,
desc: &MeshPipelineDescriptor<
<Self::A as Api>::PipelineLayout,
<Self::A as Api>::ShaderModule,
<Self::A as Api>::PipelineCache,
>,
) -> Result<<Self::A as Api>::RenderPipeline, PipelineError>;
unsafe fn destroy_render_pipeline(&self, pipeline: <Self::A as Api>::RenderPipeline);
#[allow(clippy::type_complexity)]
@@ -1458,6 +1468,26 @@ pub trait CommandEncoder: WasmNotSendSync + fmt::Debug {
count_offset: wgt::BufferAddress,
max_count: u32,
);
unsafe fn draw_mesh_tasks(
&mut self,
group_count_x: u32,
group_count_y: u32,
group_count_z: u32,
);
unsafe fn draw_mesh_tasks_indirect(
&mut self,
buffer: &<Self::A as Api>::Buffer,
offset: wgt::BufferAddress,
draw_count: u32,
);
unsafe fn draw_mesh_tasks_indirect_count(
&mut self,
buffer: &<Self::A as Api>::Buffer,
offset: wgt::BufferAddress,
count_buffer: &<Self::A as Api>::Buffer,
count_offset: wgt::BufferAddress,
max_count: u32,
);
// compute passes
@@ -2158,6 +2188,33 @@ pub struct RenderPipelineDescriptor<
/// The cache which will be used and filled when compiling this pipeline
pub cache: Option<&'a Pc>,
}
pub struct MeshPipelineDescriptor<
'a,
Pl: DynPipelineLayout + ?Sized,
M: DynShaderModule + ?Sized,
Pc: DynPipelineCache + ?Sized,
> {
pub label: Label<'a>,
/// The layout of bind groups for this pipeline.
pub layout: &'a Pl,
pub task_stage: Option<ProgrammableStage<'a, M>>,
pub mesh_stage: ProgrammableStage<'a, M>,
/// The properties of the pipeline at the primitive assembly and rasterization level.
pub primitive: wgt::PrimitiveState,
/// The effect of draw calls on the depth and stencil aspects of the output target, if any.
pub depth_stencil: Option<wgt::DepthStencilState>,
/// The multi-sampling properties of the pipeline.
pub multisample: wgt::MultisampleState,
/// The fragment stage for this pipeline.
pub fragment_stage: Option<ProgrammableStage<'a, M>>,
/// The effect of draw calls on the color aspect of the output target.
pub color_targets: &'a [Option<wgt::ColorTargetState>],
/// If the pipeline will be used with a multiview render pass, this indicates how many array
/// layers the attachments will have.
pub multiview: Option<NonZeroU32>,
/// The cache which will be used and filled when compiling this pipeline
pub cache: Option<&'a Pc>,
}
#[derive(Debug, Clone)]
pub struct SurfaceConfiguration {
+29
View File
@@ -1100,6 +1100,15 @@ impl crate::CommandEncoder for super::CommandEncoder {
}
}
unsafe fn draw_mesh_tasks(
&mut self,
_group_count_x: u32,
_group_count_y: u32,
_group_count_z: u32,
) {
unreachable!()
}
unsafe fn draw_indirect(
&mut self,
buffer: &super::Buffer,
@@ -1134,6 +1143,15 @@ impl crate::CommandEncoder for super::CommandEncoder {
}
}
unsafe fn draw_mesh_tasks_indirect(
&mut self,
_buffer: &<Self::A as crate::Api>::Buffer,
_offset: wgt::BufferAddress,
_draw_count: u32,
) {
unreachable!()
}
unsafe fn draw_indirect_count(
&mut self,
_buffer: &super::Buffer,
@@ -1155,6 +1173,17 @@ impl crate::CommandEncoder for super::CommandEncoder {
//TODO
}
unsafe fn draw_mesh_tasks_indirect_count(
&mut self,
_buffer: &<Self::A as crate::Api>::Buffer,
_offset: wgt::BufferAddress,
_count_buffer: &<Self::A as crate::Api>::Buffer,
_count_offset: wgt::BufferAddress,
_max_count: u32,
) {
unreachable!()
}
// compute
unsafe fn begin_compute_pass(&mut self, desc: &crate::ComputePassDescriptor<super::QuerySet>) {
+11
View File
@@ -1269,6 +1269,17 @@ impl crate::Device for super::Device {
})
}
unsafe fn create_mesh_pipeline(
&self,
_desc: &crate::MeshPipelineDescriptor<
<Self::A as crate::Api>::PipelineLayout,
<Self::A as crate::Api>::ShaderModule,
<Self::A as crate::Api>::PipelineCache,
>,
) -> Result<<Self::A as crate::Api>::RenderPipeline, crate::PipelineError> {
unreachable!()
}
unsafe fn destroy_render_pipeline(&self, _pipeline: super::RenderPipeline) {
self.counters.render_pipelines.sub(1);
}
+23
View File
@@ -207,6 +207,13 @@ impl crate::CommandEncoder for CommandBuffer {
instance_count: u32,
) {
}
unsafe fn draw_mesh_tasks(
&mut self,
group_count_x: u32,
group_count_y: u32,
group_count_z: u32,
) {
}
unsafe fn draw_indirect(
&mut self,
buffer: &Buffer,
@@ -221,6 +228,13 @@ impl crate::CommandEncoder for CommandBuffer {
draw_count: u32,
) {
}
unsafe fn draw_mesh_tasks_indirect(
&mut self,
buffer: &<Self::A as crate::Api>::Buffer,
offset: wgt::BufferAddress,
draw_count: u32,
) {
}
unsafe fn draw_indirect_count(
&mut self,
buffer: &Buffer,
@@ -239,6 +253,15 @@ impl crate::CommandEncoder for CommandBuffer {
max_count: u32,
) {
}
unsafe fn draw_mesh_tasks_indirect_count(
&mut self,
buffer: &<Self::A as crate::Api>::Buffer,
offset: wgt::BufferAddress,
count_buffer: &<Self::A as crate::Api>::Buffer,
count_offset: wgt::BufferAddress,
max_count: u32,
) {
}
// compute
+10
View File
@@ -368,6 +368,16 @@ impl crate::Device for Context {
) -> Result<Resource, crate::PipelineError> {
Ok(Resource)
}
unsafe fn create_mesh_pipeline(
&self,
desc: &crate::MeshPipelineDescriptor<
<Self::A as crate::Api>::PipelineLayout,
<Self::A as crate::Api>::ShaderModule,
<Self::A as crate::Api>::PipelineCache,
>,
) -> Result<<Self::A as crate::Api>::RenderPipeline, crate::PipelineError> {
Ok(Resource)
}
unsafe fn destroy_render_pipeline(&self, pipeline: Resource) {}
unsafe fn create_compute_pipeline(
&self,
+84 -5
View File
@@ -118,6 +118,12 @@ pub struct PhysicalDeviceFeatures {
/// Features provided by `VK_EXT_subgroup_size_control`, promoted to Vulkan 1.3.
subgroup_size_control: Option<vk::PhysicalDeviceSubgroupSizeControlFeatures<'static>>,
/// Features proved by `VK_KHR_maintenance4`, needed for mesh shaders
maintenance4: Option<vk::PhysicalDeviceMaintenance4FeaturesKHR<'static>>,
/// Features proved by `VK_EXT_mesh_shader`
mesh_shader: Option<vk::PhysicalDeviceMeshShaderFeaturesEXT<'static>>,
}
impl PhysicalDeviceFeatures {
@@ -142,6 +148,9 @@ impl PhysicalDeviceFeatures {
if let Some(ref mut feature) = self.robustness2 {
info = info.push_next(feature);
}
if let Some(ref mut feature) = self.multiview {
info = info.push_next(feature);
}
if let Some(ref mut feature) = self.astc_hdr {
info = info.push_next(feature);
}
@@ -173,6 +182,12 @@ impl PhysicalDeviceFeatures {
if let Some(ref mut feature) = self.subgroup_size_control {
info = info.push_next(feature);
}
if let Some(ref mut feature) = self.maintenance4 {
info = info.push_next(feature);
}
if let Some(ref mut feature) = self.mesh_shader {
info = info.push_next(feature);
}
info
}
@@ -203,12 +218,14 @@ impl PhysicalDeviceFeatures {
/// [`add_to_device_create`]: PhysicalDeviceFeatures::add_to_device_create
/// [`Adapter::required_device_extensions`]: super::Adapter::required_device_extensions
fn from_extensions_and_requested_features(
device_api_version: u32,
phd_capabilities: &PhysicalDeviceProperties,
_phd_features: &PhysicalDeviceFeatures,
enabled_extensions: &[&'static CStr],
requested_features: wgt::Features,
downlevel_flags: wgt::DownlevelFlags,
private_caps: &super::PrivateCapabilities,
) -> Self {
let device_api_version = phd_capabilities.device_api_version;
let needs_bindless = requested_features.intersects(
wgt::Features::TEXTURE_BINDING_ARRAY
| wgt::Features::BUFFER_BINDING_ARRAY
@@ -465,6 +482,24 @@ impl PhysicalDeviceFeatures {
} else {
None
},
mesh_shader: if enabled_extensions.contains(&ext::mesh_shader::NAME) {
let needed = requested_features.contains(wgt::Features::MESH_SHADER);
Some(
vk::PhysicalDeviceMeshShaderFeaturesEXT::default()
.mesh_shader(needed)
.task_shader(needed)
// Multiview needs some special work https://github.com/gfx-rs/wgpu/issues/7262
.multiview_mesh_shader(false),
)
} else {
None
},
maintenance4: if enabled_extensions.contains(&khr::maintenance4::NAME) {
let needed = requested_features.contains(wgt::Features::MESH_SHADER);
Some(vk::PhysicalDeviceMaintenance4FeaturesKHR::default().maintenance4(needed))
} else {
None
},
}
}
@@ -784,7 +819,10 @@ impl PhysicalDeviceFeatures {
F::VULKAN_EXTERNAL_MEMORY_WIN32,
caps.supports_extension(khr::external_memory_win32::NAME),
);
features.set(
F::MESH_SHADER,
caps.supports_extension(ext::mesh_shader::NAME),
);
(features, dl_flags)
}
}
@@ -846,6 +884,10 @@ pub struct PhysicalDeviceProperties {
/// `VK_EXT_robustness2` extension.
robustness2: Option<vk::PhysicalDeviceRobustness2PropertiesEXT<'static>>,
/// Additional `vk::PhysicalDevice` properties from the
/// `VK_EXT_mesh_shader` extension.
_mesh_shader: Option<vk::PhysicalDeviceMeshShaderPropertiesEXT<'static>>,
/// The device API version.
///
/// Which is the version of Vulkan supported for device-level functionality.
@@ -947,6 +989,10 @@ impl PhysicalDeviceProperties {
}
}
if requested_features.intersects(wgt::Features::MESH_SHADER) {
extensions.push(khr::spirv_1_4::NAME);
}
//extensions.push(khr::sampler_mirror_clamp_to_edge::NAME);
//extensions.push(ext::sampler_filter_minmax::NAME);
}
@@ -961,6 +1007,10 @@ impl PhysicalDeviceProperties {
if requested_features.contains(wgt::Features::SUBGROUP) {
extensions.push(ext::subgroup_size_control::NAME);
}
if requested_features.intersects(wgt::Features::MESH_SHADER) {
extensions.push(khr::maintenance4::NAME);
}
}
// Optional `VK_KHR_swapchain_mutable_format`
@@ -1035,6 +1085,10 @@ impl PhysicalDeviceProperties {
extensions.push(google::display_timing::NAME);
}
if requested_features.contains(wgt::Features::MESH_SHADER) {
extensions.push(ext::mesh_shader::NAME);
}
extensions
}
@@ -1216,6 +1270,8 @@ impl super::InstanceShared {
let supports_acceleration_structure =
capabilities.supports_extension(khr::acceleration_structure::NAME);
let supports_mesh_shader = capabilities.supports_extension(ext::mesh_shader::NAME);
let mut properties2 = vk::PhysicalDeviceProperties2KHR::default();
if supports_maintenance3 {
let next = capabilities
@@ -1266,6 +1322,13 @@ impl super::InstanceShared {
properties2 = properties2.push_next(next);
}
if supports_mesh_shader {
let next = capabilities
._mesh_shader
.insert(vk::PhysicalDeviceMeshShaderPropertiesEXT::default());
properties2 = properties2.push_next(next);
}
unsafe {
get_device_properties.get_physical_device_properties2(phd, &mut properties2)
};
@@ -1412,6 +1475,13 @@ impl super::InstanceShared {
features2 = features2.push_next(next);
}
if capabilities.supports_extension(ext::mesh_shader::NAME) {
let next = features
.mesh_shader
.insert(vk::PhysicalDeviceMeshShaderFeaturesEXT::default());
features2 = features2.push_next(next);
}
unsafe { get_device_properties.get_physical_device_features2(phd, &mut features2) };
features2.features
} else {
@@ -1471,7 +1541,6 @@ impl super::Instance {
},
backend: wgt::Backend::Vulkan,
};
let (available_features, downlevel_flags) =
phd_features.to_wgpu(&self.shared.raw, phd, &phd_capabilities);
let mut workarounds = super::Workarounds::empty();
@@ -1626,7 +1695,7 @@ impl super::Instance {
| vk::MemoryPropertyFlags::HOST_CACHED
| vk::MemoryPropertyFlags::LAZILY_ALLOCATED,
phd_capabilities,
//phd_features,
phd_features,
downlevel_flags,
private_caps,
workarounds,
@@ -1691,7 +1760,8 @@ impl super::Adapter {
features: wgt::Features,
) -> PhysicalDeviceFeatures {
PhysicalDeviceFeatures::from_extensions_and_requested_features(
self.phd_capabilities.device_api_version,
&self.phd_capabilities,
&self.phd_features,
enabled_extensions,
features,
self.downlevel_flags,
@@ -1780,6 +1850,14 @@ impl super::Adapter {
} else {
None
};
let mesh_shading_fns = if enabled_extensions.contains(&ext::mesh_shader::NAME) {
Some(ext::mesh_shader::Device::new(
&self.instance.raw,
&raw_device,
))
} else {
None
};
let naga_options = {
use naga::back::spv;
@@ -1955,6 +2033,7 @@ impl super::Adapter {
draw_indirect_count: indirect_count_fn,
timeline_semaphore: timeline_semaphore_fn,
ray_tracing: ray_tracing_fns,
mesh_shading: mesh_shading_fns,
},
pipeline_cache_validation_key,
vendor_id: self.phd_capabilities.properties.vendor_id,
+61
View File
@@ -1047,6 +1047,20 @@ impl crate::CommandEncoder for super::CommandEncoder {
)
};
}
unsafe fn draw_mesh_tasks(
&mut self,
group_count_x: u32,
group_count_y: u32,
group_count_z: u32,
) {
if let Some(ref t) = self.device.extension_fns.mesh_shading {
unsafe {
t.cmd_draw_mesh_tasks(self.active, group_count_x, group_count_y, group_count_z);
};
} else {
panic!("Feature `MESH_SHADING` not enabled");
}
}
unsafe fn draw_indirect(
&mut self,
buffer: &super::Buffer,
@@ -1079,6 +1093,26 @@ impl crate::CommandEncoder for super::CommandEncoder {
)
};
}
unsafe fn draw_mesh_tasks_indirect(
&mut self,
buffer: &<Self::A as crate::Api>::Buffer,
offset: wgt::BufferAddress,
draw_count: u32,
) {
if let Some(ref t) = self.device.extension_fns.mesh_shading {
unsafe {
t.cmd_draw_mesh_tasks_indirect(
self.active,
buffer.raw,
offset,
draw_count,
size_of::<wgt::DispatchIndirectArgs>() as u32,
);
};
} else {
panic!("Feature `MESH_SHADING` not enabled");
}
}
unsafe fn draw_indirect_count(
&mut self,
buffer: &super::Buffer,
@@ -1131,6 +1165,33 @@ impl crate::CommandEncoder for super::CommandEncoder {
None => panic!("Feature `DRAW_INDIRECT_COUNT` not enabled"),
}
}
unsafe fn draw_mesh_tasks_indirect_count(
&mut self,
buffer: &<Self::A as crate::Api>::Buffer,
offset: wgt::BufferAddress,
count_buffer: &super::Buffer,
count_offset: wgt::BufferAddress,
max_count: u32,
) {
if self.device.extension_fns.draw_indirect_count.is_none() {
panic!("Feature `DRAW_INDIRECT_COUNT` not enabled");
}
if let Some(ref t) = self.device.extension_fns.mesh_shading {
unsafe {
t.cmd_draw_mesh_tasks_indirect_count(
self.active,
buffer.raw,
offset,
count_buffer.raw,
count_offset,
max_count,
size_of::<wgt::DispatchIndirectArgs>() as u32,
);
};
} else {
panic!("Feature `MESH_SHADING` not enabled");
}
}
// compute
+231
View File
@@ -2120,6 +2120,237 @@ impl crate::Device for super::Device {
Ok(super::RenderPipeline { raw })
}
unsafe fn create_mesh_pipeline(
&self,
desc: &crate::MeshPipelineDescriptor<
<Self::A as crate::Api>::PipelineLayout,
<Self::A as crate::Api>::ShaderModule,
<Self::A as crate::Api>::PipelineCache,
>,
) -> Result<<Self::A as crate::Api>::RenderPipeline, crate::PipelineError> {
let dynamic_states = [
vk::DynamicState::VIEWPORT,
vk::DynamicState::SCISSOR,
vk::DynamicState::BLEND_CONSTANTS,
vk::DynamicState::STENCIL_REFERENCE,
];
let mut compatible_rp_key = super::RenderPassKey {
sample_count: desc.multisample.count,
multiview: desc.multiview,
..Default::default()
};
let mut stages = ArrayVec::<_, { crate::MAX_CONCURRENT_SHADER_STAGES }>::new();
let vk_input_assembly = vk::PipelineInputAssemblyStateCreateInfo::default()
.topology(conv::map_topology(desc.primitive.topology))
.primitive_restart_enable(desc.primitive.strip_index_format.is_some());
let compiled_ts = match desc.task_stage {
Some(ref stage) => {
// TODO: add proper naga stages
let mut compiled = self.compile_stage(
stage,
naga::ShaderStage::Compute,
&desc.layout.binding_arrays,
)?;
compiled.create_info.stage = vk::ShaderStageFlags::TASK_EXT;
stages.push(compiled.create_info);
Some(compiled)
}
None => None,
};
// TODO: add proper naga stages
let mut compiled_ms = self.compile_stage(
&desc.mesh_stage,
naga::ShaderStage::Compute,
&desc.layout.binding_arrays,
)?;
compiled_ms.create_info.stage = vk::ShaderStageFlags::MESH_EXT;
stages.push(compiled_ms.create_info);
let compiled_fs = match desc.fragment_stage {
Some(ref stage) => {
let compiled = self.compile_stage(
stage,
naga::ShaderStage::Fragment,
&desc.layout.binding_arrays,
)?;
stages.push(compiled.create_info);
Some(compiled)
}
None => None,
};
let mut vk_rasterization = vk::PipelineRasterizationStateCreateInfo::default()
.polygon_mode(conv::map_polygon_mode(desc.primitive.polygon_mode))
.front_face(conv::map_front_face(desc.primitive.front_face))
.line_width(1.0)
.depth_clamp_enable(desc.primitive.unclipped_depth);
if let Some(face) = desc.primitive.cull_mode {
vk_rasterization = vk_rasterization.cull_mode(conv::map_cull_face(face))
}
let mut vk_rasterization_conservative_state =
vk::PipelineRasterizationConservativeStateCreateInfoEXT::default()
.conservative_rasterization_mode(
vk::ConservativeRasterizationModeEXT::OVERESTIMATE,
);
if desc.primitive.conservative {
vk_rasterization = vk_rasterization.push_next(&mut vk_rasterization_conservative_state);
}
let mut vk_depth_stencil = vk::PipelineDepthStencilStateCreateInfo::default();
if let Some(ref ds) = desc.depth_stencil {
let vk_format = self.shared.private_caps.map_texture_format(ds.format);
let vk_layout = if ds.is_read_only(desc.primitive.cull_mode) {
vk::ImageLayout::DEPTH_STENCIL_READ_ONLY_OPTIMAL
} else {
vk::ImageLayout::DEPTH_STENCIL_ATTACHMENT_OPTIMAL
};
compatible_rp_key.depth_stencil = Some(super::DepthStencilAttachmentKey {
base: super::AttachmentKey::compatible(vk_format, vk_layout),
stencil_ops: crate::AttachmentOps::all(),
});
if ds.is_depth_enabled() {
vk_depth_stencil = vk_depth_stencil
.depth_test_enable(true)
.depth_write_enable(ds.depth_write_enabled)
.depth_compare_op(conv::map_comparison(ds.depth_compare));
}
if ds.stencil.is_enabled() {
let s = &ds.stencil;
let front = conv::map_stencil_face(&s.front, s.read_mask, s.write_mask);
let back = conv::map_stencil_face(&s.back, s.read_mask, s.write_mask);
vk_depth_stencil = vk_depth_stencil
.stencil_test_enable(true)
.front(front)
.back(back);
}
if ds.bias.is_enabled() {
vk_rasterization = vk_rasterization
.depth_bias_enable(true)
.depth_bias_constant_factor(ds.bias.constant as f32)
.depth_bias_clamp(ds.bias.clamp)
.depth_bias_slope_factor(ds.bias.slope_scale);
}
}
let vk_viewport = vk::PipelineViewportStateCreateInfo::default()
.flags(vk::PipelineViewportStateCreateFlags::empty())
.scissor_count(1)
.viewport_count(1);
let vk_sample_mask = [
desc.multisample.mask as u32,
(desc.multisample.mask >> 32) as u32,
];
let vk_multisample = vk::PipelineMultisampleStateCreateInfo::default()
.rasterization_samples(vk::SampleCountFlags::from_raw(desc.multisample.count))
.alpha_to_coverage_enable(desc.multisample.alpha_to_coverage_enabled)
.sample_mask(&vk_sample_mask);
let mut vk_attachments = Vec::with_capacity(desc.color_targets.len());
for cat in desc.color_targets {
let (key, attarchment) = if let Some(cat) = cat.as_ref() {
let mut vk_attachment = vk::PipelineColorBlendAttachmentState::default()
.color_write_mask(vk::ColorComponentFlags::from_raw(cat.write_mask.bits()));
if let Some(ref blend) = cat.blend {
let (color_op, color_src, color_dst) = conv::map_blend_component(&blend.color);
let (alpha_op, alpha_src, alpha_dst) = conv::map_blend_component(&blend.alpha);
vk_attachment = vk_attachment
.blend_enable(true)
.color_blend_op(color_op)
.src_color_blend_factor(color_src)
.dst_color_blend_factor(color_dst)
.alpha_blend_op(alpha_op)
.src_alpha_blend_factor(alpha_src)
.dst_alpha_blend_factor(alpha_dst);
}
let vk_format = self.shared.private_caps.map_texture_format(cat.format);
(
Some(super::ColorAttachmentKey {
base: super::AttachmentKey::compatible(
vk_format,
vk::ImageLayout::COLOR_ATTACHMENT_OPTIMAL,
),
resolve: None,
}),
vk_attachment,
)
} else {
(None, vk::PipelineColorBlendAttachmentState::default())
};
compatible_rp_key.colors.push(key);
vk_attachments.push(attarchment);
}
let vk_color_blend =
vk::PipelineColorBlendStateCreateInfo::default().attachments(&vk_attachments);
let vk_dynamic_state =
vk::PipelineDynamicStateCreateInfo::default().dynamic_states(&dynamic_states);
let raw_pass = self.shared.make_render_pass(compatible_rp_key)?;
let vk_infos = [{
vk::GraphicsPipelineCreateInfo::default()
.layout(desc.layout.raw)
.stages(&stages)
.input_assembly_state(&vk_input_assembly)
.rasterization_state(&vk_rasterization)
.viewport_state(&vk_viewport)
.multisample_state(&vk_multisample)
.depth_stencil_state(&vk_depth_stencil)
.color_blend_state(&vk_color_blend)
.dynamic_state(&vk_dynamic_state)
.render_pass(raw_pass)
}];
let pipeline_cache = desc
.cache
.map(|it| it.raw)
.unwrap_or(vk::PipelineCache::null());
let mut raw_vec = {
profiling::scope!("vkCreateGraphicsPipelines");
unsafe {
self.shared
.raw
.create_graphics_pipelines(pipeline_cache, &vk_infos, None)
.map_err(|(_, e)| super::map_pipeline_err(e))
}?
};
let raw = raw_vec.pop().unwrap();
if let Some(label) = desc.label {
unsafe { self.shared.set_object_name(raw, label) };
}
// NOTE: this could leak shaders in case of an error.
if let Some(CompiledStage {
temp_raw_module: Some(raw_module),
..
}) = compiled_ts
{
unsafe { self.shared.raw.destroy_shader_module(raw_module, None) };
}
if let Some(raw_module) = compiled_ms.temp_raw_module {
unsafe { self.shared.raw.destroy_shader_module(raw_module, None) };
}
if let Some(CompiledStage {
temp_raw_module: Some(raw_module),
..
}) = compiled_fs
{
unsafe { self.shared.raw.destroy_shader_module(raw_module, None) };
}
self.counters.render_pipelines.add(1);
Ok(super::RenderPipeline { raw })
}
unsafe fn destroy_render_pipeline(&self, pipeline: super::RenderPipeline) {
unsafe { self.shared.raw.destroy_pipeline(pipeline.raw, None) };
+2 -1
View File
@@ -464,7 +464,7 @@ pub struct Adapter {
//queue_families: Vec<vk::QueueFamilyProperties>,
known_memory_flags: vk::MemoryPropertyFlags,
phd_capabilities: adapter::PhysicalDeviceProperties,
//phd_features: adapter::PhysicalDeviceFeatures,
phd_features: adapter::PhysicalDeviceFeatures,
downlevel_flags: wgt::DownlevelFlags,
private_caps: PrivateCapabilities,
workarounds: Workarounds,
@@ -483,6 +483,7 @@ struct DeviceExtensionFunctions {
draw_indirect_count: Option<khr::draw_indirect_count::Device>,
timeline_semaphore: Option<ExtensionFn<khr::timeline_semaphore::Device>>,
ray_tracing: Option<RayTracingDeviceExtensionFunctions>,
mesh_shading: Option<ext::mesh_shader::Device>,
}
struct RayTracingDeviceExtensionFunctions {
+12
View File
@@ -1153,6 +1153,18 @@ bitflags_array! {
///
/// This is a native only feature.
const UNIFORM_BUFFER_BINDING_ARRAYS = 1 << 46;
/// Enables mesh shaders and task shaders in mesh shader pipelines.
///
/// Supported platforms:
/// - Vulkan (with [VK_EXT_mesh_shader](https://registry.khronos.org/vulkan/specs/latest/man/html/VK_EXT_mesh_shader.html))
///
/// Potential Platforms:
/// - DX12
/// - Metal
///
/// This is a native only feature.
const MESH_SHADER = 1 << 47;
}
/// Features that are not guaranteed to be supported.