Skip to content

Commit

Permalink
hal/mtl: queue
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jun 10, 2021
1 parent 3109b1b commit 782c72d
Show file tree
Hide file tree
Showing 25 changed files with 212 additions and 145 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 wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ version = "0.8"
path = "../wgpu-hal"
package = "wgpu-hal"
version = "0.1"
features = ["empty"]

[target.'cfg(all(not(target_arch = "wasm32"), any(target_os = "ios", target_os = "macos")))'.dependencies]
hal = { path = "../wgpu-hal", package = "wgpu-hal", features = ["metal"] }
Expand Down
2 changes: 2 additions & 0 deletions wgpu-core/src/command/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ impl<A: hal::Api> QueryResetMap<A> {

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum SimplifiedQueryType {
Occlusion,
Timestamp,
PipelineStatistics,
}
impl From<wgt::QueryType> for SimplifiedQueryType {
fn from(q: wgt::QueryType) -> Self {
match q {
wgt::QueryType::Occlusion => SimplifiedQueryType::Occlusion,
wgt::QueryType::Timestamp => SimplifiedQueryType::Timestamp,
wgt::QueryType::PipelineStatistics(..) => SimplifiedQueryType::PipelineStatistics,
}
Expand Down
10 changes: 10 additions & 0 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub const SHADER_STAGE_COUNT: usize = 3;
const CLEANUP_WAIT_MS: u32 = 5000;

const IMPLICIT_FAILURE: &str = "failed implicit";
const EP_FAILURE: &str = "EP is invalid";

pub type DeviceDescriptor<'a> = wgt::DeviceDescriptor<Label<'a>>;

Expand Down Expand Up @@ -1719,6 +1720,9 @@ impl<A: HalApi> Device<A> {
hal::PipelineError::Linkage(_stages, msg) => {
pipeline::CreateComputePipelineError::Internal(msg)
}
hal::PipelineError::EntryPoint(_stage) => {
pipeline::CreateComputePipelineError::Internal(EP_FAILURE.to_string())
}
},
)?;

Expand Down Expand Up @@ -2097,6 +2101,12 @@ impl<A: HalApi> Device<A> {
hal::PipelineError::Linkage(stage, msg) => {
pipeline::CreateRenderPipelineError::Internal { stage, error: msg }
}
hal::PipelineError::EntryPoint(stage) => {
pipeline::CreateRenderPipelineError::Internal {
stage: hal::aux::map_naga_stage(stage),
error: EP_FAILURE.to_string(),
}
}
},
)?;

Expand Down
6 changes: 4 additions & 2 deletions wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ license = "MIT OR Apache-2.0"
[lib]

[features]
default = ["metal"]
metal = ["foreign-types", "mtl", "objc", "parking_lot", "naga/msl-out"]
default = []
empty = []
metal = ["block", "foreign-types", "mtl", "objc", "parking_lot", "naga/msl-out"]

[dependencies]
arrayvec = "0.5"
Expand All @@ -25,6 +26,7 @@ thiserror = "1"
wgt = { package = "wgpu-types", path = "../wgpu-types" }

# backends
block = { version = "0.1", optional = true }
foreign-types = { version = "0.3", optional = true }
mtl = { package = "metal", version = "0.22", optional = true }
objc = { version = "0.2.5", optional = true }
Expand Down
17 changes: 7 additions & 10 deletions wgpu-hal/examples/bunnymark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ impl<A: hal::Api> Example<A> {
};
let shader_desc = hal::ShaderModuleDescriptor { label: None };
let shader = unsafe {
match device.create_shader_module(&shader_desc, naga_shader) {
Ok(shader) => shader,
Err((error, _shader)) => panic!("{}", error),
}
device.create_shader_module(&shader_desc, naga_shader).unwrap()
};

let global_bgl_desc = hal::BindGroupLayoutDescriptor {
Expand Down Expand Up @@ -379,10 +376,10 @@ impl<A: hal::Api> Example<A> {
};

unsafe {
let fence = device.create_fence().unwrap();
let mut fence = device.create_fence().unwrap();
init_cmd.finish();
queue
.submit(iter::once(init_cmd), Some((&fence, 1)))
.submit(iter::once(init_cmd), Some((&mut fence, 1)))
.unwrap();
device.wait(&fence, 1, !0).unwrap();
device.destroy_fence(fence);
Expand Down Expand Up @@ -528,17 +525,17 @@ impl<A: hal::Api> Example<A> {
}
}

#[cfg(feature = "metal")]
type Api = hal::api::Metal;

fn main() {
let event_loop = winit::event_loop::EventLoop::new();
let window = winit::window::WindowBuilder::new()
.with_title("hal-bunnymark")
.build(&event_loop)
.unwrap();

#[cfg(feature = "metal")]
let example_result = Example::<hal::api::Metal>::init(&window);
#[cfg(not(any(feature = "metal")))]
let example_result = Example::<hal::api::Empty>::init(&window);
let example_result = Example::<Api>::init(&window);
let mut example = example_result.expect("Selected backend is not supported");

let mut last_frame_inst = Instant::now();
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/src/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl crate::Queue<Api> for Context {
unsafe fn submit<I>(
&mut self,
command_buffers: I,
signal_fence: Option<(&Resource, crate::FenceValue)>,
signal_fence: Option<(&mut Resource, crate::FenceValue)>,
) -> DeviceResult<()> {
Ok(())
}
Expand Down Expand Up @@ -364,7 +364,7 @@ impl crate::CommandBuffer<Api> for Encoder {

// compute

unsafe fn begin_compute_pass(&mut self) {}
unsafe fn begin_compute_pass(&mut self, desc: &crate::ComputePassDescriptor) {}
unsafe fn end_compute_pass(&mut self) {}

unsafe fn set_compute_pipeline(&mut self, pipeline: &Resource) {}
Expand Down
41 changes: 24 additions & 17 deletions wgpu-hal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
)]

pub mod aux;
pub mod empty;
#[cfg(feature = "empty")]
mod empty;
#[cfg(feature = "metal")]
mod metal;

pub mod api {
#[cfg(feature = "empty")]
pub use super::empty::Api as Empty;
#[cfg(feature = "metal")]
pub use super::metal::Api as Metal;
Expand Down Expand Up @@ -128,15 +130,15 @@ pub trait Api: Clone + Sized {
type QuerySet: fmt::Debug + Send + Sync;
type Fence: fmt::Debug + Send + Sync;

type BindGroupLayout;
type BindGroupLayout: Send + Sync;
type BindGroup: fmt::Debug + Send + Sync;
type PipelineLayout;
type PipelineLayout: Send + Sync;
type ShaderModule: fmt::Debug + Send + Sync;
type RenderPipeline;
type ComputePipeline;
type RenderPipeline: Send + Sync;
type ComputePipeline: Send + Sync;
}

pub trait Instance<A: Api>: Sized {
pub trait Instance<A: Api>: Sized + Send + Sync {
unsafe fn init() -> Result<Self, InstanceError>;
unsafe fn create_surface(
&self,
Expand All @@ -146,7 +148,7 @@ pub trait Instance<A: Api>: Sized {
unsafe fn enumerate_adapters(&self) -> Vec<ExposedAdapter<A>>;
}

pub trait Surface<A: Api> {
pub trait Surface<A: Api>: Send + Sync {
unsafe fn configure(
&mut self,
device: &A::Device,
Expand All @@ -163,7 +165,7 @@ pub trait Surface<A: Api> {
unsafe fn discard_texture(&mut self, texture: A::SurfaceTexture);
}

pub trait Adapter<A: Api> {
pub trait Adapter<A: Api>: Send + Sync {
unsafe fn open(&self, features: wgt::Features) -> Result<OpenDevice<A>, DeviceError>;
unsafe fn close(&self, device: A::Device);

Expand All @@ -179,7 +181,7 @@ pub trait Adapter<A: Api> {
unsafe fn surface_capabilities(&self, surface: &A::Surface) -> Option<SurfaceCapabilities>;
}

pub trait Device<A: Api> {
pub trait Device<A: Api>: Send + Sync {
/// Creates a new buffer.
///
/// The initial usage is `BufferUse::empty()`.
Expand Down Expand Up @@ -275,22 +277,22 @@ pub trait Device<A: Api> {
unsafe fn stop_capture(&self);
}

pub trait Queue<A: Api> {
unsafe fn submit<I: Iterator<Item = A::CommandBuffer>>(
pub trait Queue<A: Api>: Send + Sync {
unsafe fn submit<I>(
&mut self,
command_buffers: I,
signal_fence: Option<(&A::Fence, FenceValue)>,
) -> Result<(), DeviceError>;
signal_fence: Option<(&mut A::Fence, FenceValue)>,
) -> Result<(), DeviceError>
where
I: Iterator<Item = A::CommandBuffer>;
unsafe fn present(
&mut self,
surface: &mut A::Surface,
texture: A::SurfaceTexture,
) -> Result<(), SurfaceError>;
}

pub trait SwapChain<A: Api> {}

pub trait CommandBuffer<A: Api> {
pub trait CommandBuffer<A: Api>: Send + Sync {
unsafe fn finish(&mut self);

unsafe fn transition_buffers<'a, T>(&mut self, barriers: T)
Expand Down Expand Up @@ -437,7 +439,7 @@ pub trait CommandBuffer<A: Api> {
// compute passes

// Begins a compute pass, clears all active bindings.
unsafe fn begin_compute_pass(&mut self);
unsafe fn begin_compute_pass(&mut self, desc: &ComputePassDescriptor);
unsafe fn end_compute_pass(&mut self);

unsafe fn set_compute_pipeline(&mut self, pipeline: &A::ComputePipeline);
Expand Down Expand Up @@ -988,6 +990,11 @@ pub struct RenderPassDescriptor<'a, A: Api> {
pub depth_stencil_attachment: Option<DepthStencilAttachment<'a, A>>,
}

#[derive(Clone, Debug)]
pub struct ComputePassDescriptor<'a> {
pub label: Label<'a>,
}

#[test]
fn test_default_limits() {
let limits = wgt::Limits::default();
Expand Down
4 changes: 3 additions & 1 deletion wgpu-hal/src/metal/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ impl crate::Adapter<super::Api> for super::Adapter {
shared: Arc::clone(&self.shared),
features,
},
queue: super::Queue {},
queue: super::Queue {
shared: Arc::clone(&self.shared),
},
})
}

Expand Down
53 changes: 48 additions & 5 deletions wgpu-hal/src/metal/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,51 @@ impl crate::CommandBuffer<super::Api> for super::CommandBuffer {
index: u32,
binding: crate::BufferBinding<'a, super::Api>,
) {
let buffer_index = self.max_buffers_per_stage as u64 - 1 - index as u64;
let encoder = self.render.as_ref().unwrap();
encoder.set_vertex_buffer(buffer_index, Some(&binding.buffer.raw), binding.offset);
}

unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth_range: Range<f32>) {
let zfar = if self.disabilities.broken_viewport_near_depth {
depth_range.end - depth_range.start
} else {
depth_range.end
};
let encoder = self.render.as_ref().unwrap();
encoder.set_viewport(mtl::MTLViewport {
originX: rect.x as _,
originY: rect.y as _,
width: rect.w as _,
height: rect.h as _,
znear: depth_range.start as _,
zfar: zfar as _,
});
}
unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) {
//TODO: support empty scissors by modifying the viewport
let scissor = mtl::MTLScissorRect {
x: rect.x as _,
y: rect.y as _,
width: rect.w as _,
height: rect.h as _,
};
let encoder = self.render.as_ref().unwrap();
encoder.set_scissor_rect(scissor);
}
unsafe fn set_stencil_reference(&mut self, value: u32) {
let encoder = self.render.as_ref().unwrap();
encoder.set_stencil_front_back_reference_value(value, value);
}
unsafe fn set_blend_constants(&mut self, color: &wgt::Color) {
let encoder = self.render.as_ref().unwrap();
encoder.set_blend_color(
color.r as f32,
color.g as f32,
color.b as f32,
color.a as f32,
);
}
unsafe fn set_viewport(&mut self, rect: &crate::Rect<f32>, depth_range: Range<f32>) {}
unsafe fn set_scissor_rect(&mut self, rect: &crate::Rect<u32>) {}
unsafe fn set_stencil_reference(&mut self, value: u32) {}
unsafe fn set_blend_constants(&mut self, color: &wgt::Color) {}

unsafe fn draw(
&mut self,
Expand Down Expand Up @@ -607,9 +647,12 @@ impl crate::CommandBuffer<super::Api> for super::CommandBuffer {

// compute

unsafe fn begin_compute_pass(&mut self) {
unsafe fn begin_compute_pass(&mut self, desc: &crate::ComputePassDescriptor) {
self.leave_blit();
let encoder = self.raw.new_compute_command_encoder();
if let Some(label) = desc.label {
encoder.set_label(label);
}
self.compute = Some(encoder.to_owned());
}
unsafe fn end_compute_pass(&mut self) {
Expand Down
16 changes: 13 additions & 3 deletions wgpu-hal/src/metal/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl crate::Device<super::Api> for super::Device {
None => texture.array_layers - desc.range.base_array_layer,
};

texture.raw.new_texture_view_from_slice(
let raw = texture.raw.new_texture_view_from_slice(
raw_format,
raw_type,
mtl::NSRange {
Expand All @@ -277,7 +277,11 @@ impl crate::Device<super::Api> for super::Device {
location: desc.range.base_array_layer as _,
length: array_layer_count as _,
},
)
);
if let Some(label) = desc.label {
raw.set_label(label);
}
raw
};

let aspects = crate::FormatAspect::from(desc.format);
Expand Down Expand Up @@ -327,6 +331,9 @@ impl crate::Device<super::Api> for super::Device {
descriptor.set_border_color(conv::map_border_color(border_color));
}

if let Some(label) = desc.label {
descriptor.set_label(label);
}
let raw = self.shared.device.lock().new_sampler(&descriptor);

Ok(super::Sampler { raw })
Expand All @@ -350,6 +357,8 @@ impl crate::Device<super::Api> for super::Device {
raw_primitive_type: mtl::MTLPrimitiveType::Point,
index_state: None,
raw_wg_size: mtl::MTLSize::new(0, 0, 0),
max_buffers_per_stage: self.shared.private_caps.max_buffers_per_stage,
disabilities: self.shared.disabilities.clone(),
})
}
unsafe fn destroy_command_buffer(&self, _cmd_buf: super::CommandBuffer) {}
Expand Down Expand Up @@ -807,6 +816,7 @@ impl crate::Device<super::Api> for super::Device {
naga::ShaderStage::Compute,
)?;
descriptor.set_compute_function(Some(&cs.function));

if let Some(name) = desc.label {
descriptor.set_label(name);
}
Expand Down Expand Up @@ -861,7 +871,7 @@ impl crate::Device<super::Api> for super::Device {

unsafe fn create_fence(&self) -> DeviceResult<super::Fence> {
Ok(super::Fence {
completed_value: atomic::AtomicU64::new(0),
completed_value: Arc::new(atomic::AtomicU64::new(0)),
pending_command_buffers: Vec::new(),
})
}
Expand Down
Loading

0 comments on commit 782c72d

Please sign in to comment.