Skip to content

Commit

Permalink
hal: port the rest of wgpu-core
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jun 8, 2021
1 parent c61ee12 commit 0a82c23
Show file tree
Hide file tree
Showing 26 changed files with 1,599 additions and 1,809 deletions.
2 changes: 1 addition & 1 deletion wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ thiserror = "1"
[dependencies.naga]
git = "https://github.com/gfx-rs/naga"
tag = "gfx-25"
features = ["spv-in", "spv-out", "wgsl-in"]
features = ["spv-in", "wgsl-in"]

[dependencies.wgt]
path = "../wgpu-types"
Expand Down
4 changes: 2 additions & 2 deletions wgpu-core/src/binding_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::{
device::{descriptor::DescriptorSet, DeviceError, MissingFeatures, SHADER_STAGE_COUNT},
device::{DeviceError, MissingFeatures, SHADER_STAGE_COUNT},
hub::Resource,
id::{BindGroupLayoutId, BufferId, DeviceId, SamplerId, TextureViewId, Valid},
memory_init_tracker::MemoryInitTrackerAction,
Expand Down Expand Up @@ -633,7 +633,7 @@ pub struct BindGroupDynamicBindingData {

#[derive(Debug)]
pub struct BindGroup<A: hal::Api> {
pub(crate) raw: DescriptorSet<A>,
pub(crate) raw: A::BindGroup,
pub(crate) device_id: Stored<DeviceId>,
pub(crate) layout_id: Valid<BindGroupLayoutId>,
pub(crate) life_guard: LifeGuard,
Expand Down
6 changes: 3 additions & 3 deletions wgpu-core/src/command/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
device::SHADER_STAGE_COUNT,
hub::{HalApi, Storage},
id::{BindGroupId, BindGroupLayoutId, PipelineLayoutId, Valid},
Stored, MAX_BIND_GROUPS,
Stored,
};

use arrayvec::ArrayVec;
Expand Down Expand Up @@ -42,7 +42,7 @@ mod compat {

#[derive(Debug)]
pub struct Manager<T> {
entries: [Entry<T>; crate::MAX_BIND_GROUPS],
entries: [Entry<T>; hal::MAX_BIND_GROUPS],
}

impl<T: Copy + PartialEq> Manager<T> {
Expand Down Expand Up @@ -145,7 +145,7 @@ pub(super) struct EntryPayload {
pub(super) struct Binder {
pub(super) pipeline_layout_id: Option<Valid<PipelineLayoutId>>, //TODO: strongly `Stored`
manager: compat::Manager<Valid<BindGroupLayoutId>>,
payloads: [EntryPayload; MAX_BIND_GROUPS],
payloads: [EntryPayload; hal::MAX_BIND_GROUPS],
}

impl Binder {
Expand Down
98 changes: 45 additions & 53 deletions wgpu-core/src/command/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,20 @@ use crate::{
StateChange,
},
conv,
device::{
AttachmentData, Device, DeviceError, RenderPassContext, MAX_VERTEX_BUFFERS,
SHADER_STAGE_COUNT,
},
hal::BufferUse,
device::{AttachmentData, Device, DeviceError, RenderPassContext, SHADER_STAGE_COUNT},
hub::{GlobalIdentityHandlerFactory, HalApi, Hub, Resource, Storage, Token},
id,
memory_init_tracker::{MemoryInitKind, MemoryInitTrackerAction},
track::{TrackerSet, UsageConflict},
validation::check_buffer_usage,
Label, LabelHelpers, LifeGuard, Stored, MAX_BIND_GROUPS,
Label, LabelHelpers, LifeGuard, Stored,
};
use arrayvec::ArrayVec;
use std::{borrow::Cow, iter, mem, ops::Range};
use std::{borrow::Cow, mem, ops::Range};
use thiserror::Error;

use hal::CommandBuffer as _;

/// Describes a [`RenderBundleEncoder`].
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "trace", derive(serde::Serialize))]
Expand Down Expand Up @@ -105,7 +103,7 @@ impl RenderBundleEncoder {
if sc == 0 || sc > 32 || !conv::is_power_of_two(sc) {
return Err(CreateRenderBundleError::InvalidSampleCount(sc));
}
sc as u8
sc
},
},
})
Expand Down Expand Up @@ -150,10 +148,12 @@ impl RenderBundleEncoder {
let mut state = State {
trackers: TrackerSet::new(self.parent_id.backend()),
index: IndexState::new(),
vertex: (0..MAX_VERTEX_BUFFERS)
vertex: (0..hal::MAX_VERTEX_BUFFERS)
.map(|_| VertexState::new())
.collect(),
bind: (0..MAX_BIND_GROUPS).map(|_| BindState::new()).collect(),
bind: (0..hal::MAX_BIND_GROUPS)
.map(|_| BindState::new())
.collect(),
push_constant_ranges: PushConstantState::new(),
raw_dynamic_offsets: Vec::new(),
flat_dynamic_offsets: Vec::new(),
Expand Down Expand Up @@ -260,7 +260,7 @@ impl RenderBundleEncoder {
let buffer = state
.trackers
.buffers
.use_extend(&*buffer_guard, buffer_id, (), BufferUse::INDEX)
.use_extend(&*buffer_guard, buffer_id, (), hal::BufferUse::INDEX)
.unwrap();
check_buffer_usage(buffer.usage, wgt::BufferUsage::INDEX)
.map_pass_err(scope)?;
Expand All @@ -287,7 +287,7 @@ impl RenderBundleEncoder {
let buffer = state
.trackers
.buffers
.use_extend(&*buffer_guard, buffer_id, (), BufferUse::VERTEX)
.use_extend(&*buffer_guard, buffer_id, (), hal::BufferUse::VERTEX)
.unwrap();
check_buffer_usage(buffer.usage, wgt::BufferUsage::VERTEX)
.map_pass_err(scope)?;
Expand Down Expand Up @@ -408,7 +408,7 @@ impl RenderBundleEncoder {
let buffer = state
.trackers
.buffers
.use_extend(&*buffer_guard, buffer_id, (), BufferUse::INDIRECT)
.use_extend(&*buffer_guard, buffer_id, (), hal::BufferUse::INDIRECT)
.unwrap();
check_buffer_usage(buffer.usage, wgt::BufferUsage::INDIRECT)
.map_pass_err(scope)?;
Expand Down Expand Up @@ -444,7 +444,7 @@ impl RenderBundleEncoder {
let buffer = state
.trackers
.buffers
.use_extend(&*buffer_guard, buffer_id, (), BufferUse::INDIRECT)
.use_extend(&*buffer_guard, buffer_id, (), hal::BufferUse::INDIRECT)
.map_err(|err| RenderCommandError::Buffer(buffer_id, err))
.map_pass_err(scope)?;
check_buffer_usage(buffer.usage, wgt::BufferUsage::INDIRECT)
Expand Down Expand Up @@ -567,7 +567,7 @@ impl RenderBundle {
/// The only failure condition is if some of the used buffers are destroyed.
pub(crate) unsafe fn execute<A: HalApi>(
&self,
cmd_buf: &mut B::CommandBuffer,
cmd_buf: &mut A::CommandBuffer,
pipeline_layout_guard: &Storage<
crate::binding_model::PipelineLayout<A>,
id::PipelineLayoutId,
Expand All @@ -576,12 +576,10 @@ impl RenderBundle {
pipeline_guard: &Storage<crate::pipeline::RenderPipeline<A>, id::RenderPipelineId>,
buffer_guard: &Storage<crate::resource::Buffer<A>, id::BufferId>,
) -> Result<(), ExecutionError> {
use hal::command::CommandBuffer as _;

let mut offsets = self.base.dynamic_offsets.as_slice();
let mut pipeline_layout_id = None::<id::Valid<id::PipelineLayoutId>>;
if let Some(ref label) = self.base.label {
cmd_buf.begin_debug_marker(label, 0);
cmd_buf.begin_debug_marker(label);
}

for command in self.base.commands.iter() {
Expand All @@ -592,17 +590,17 @@ impl RenderBundle {
bind_group_id,
} => {
let bind_group = bind_group_guard.get(bind_group_id).unwrap();
cmd_buf.bind_graphics_descriptor_sets(
cmd_buf.set_bind_group(
&pipeline_layout_guard[pipeline_layout_id.unwrap()].raw,
index as usize,
iter::once(bind_group.raw.raw()),
offsets.iter().take(num_dynamic_offsets as usize).cloned(),
index as u32,
&bind_group.raw,
&offsets[num_dynamic_offsets as usize..],
);
offsets = &offsets[num_dynamic_offsets as usize..];
}
RenderCommand::SetPipeline(pipeline_id) => {
let pipeline = pipeline_guard.get(pipeline_id).unwrap();
cmd_buf.bind_graphics_pipeline(&pipeline.raw);
cmd_buf.set_render_pipeline(&pipeline.raw);

pipeline_layout_id = Some(pipeline.layout_id.value);
}
Expand All @@ -612,37 +610,37 @@ impl RenderBundle {
offset,
size,
} => {
let index_type = conv::map_index_format(index_format);

let &(ref buffer, _) = buffer_guard
let buffer = buffer_guard
.get(buffer_id)
.unwrap()
.raw
.as_ref()
.ok_or(ExecutionError::DestroyedBuffer(buffer_id))?;
let range = hal::buffer::SubRange {
let bb = hal::BufferBinding {
buffer,
offset,
size: size.map(|s| s.get()),
size,
};
cmd_buf.bind_index_buffer(buffer, range, index_type);
cmd_buf.set_index_buffer(bb, index_format);
}
RenderCommand::SetVertexBuffer {
slot,
buffer_id,
offset,
size,
} => {
let &(ref buffer, _) = buffer_guard
let buffer = buffer_guard
.get(buffer_id)
.unwrap()
.raw
.as_ref()
.ok_or(ExecutionError::DestroyedBuffer(buffer_id))?;
let range = hal::buffer::SubRange {
let bb = hal::BufferBinding {
buffer,
offset,
size: size.map(|s| s.get()),
size,
};
cmd_buf.bind_vertex_buffers(slot, iter::once((buffer, range)));
cmd_buf.set_vertex_buffer(slot, bb);
}
RenderCommand::SetPushConstant {
stages,
Expand All @@ -659,20 +657,15 @@ impl RenderBundle {
let data_slice = &self.base.push_constant_data
[(values_offset as usize)..values_end_offset];

cmd_buf.push_graphics_constants(
&pipeline_layout.raw,
conv::map_shader_stage_flags(stages),
offset,
&data_slice,
)
cmd_buf.set_push_constants(&pipeline_layout.raw, stages, offset, data_slice)
} else {
super::push_constant_clear(
offset,
size_bytes,
|clear_offset, clear_data| {
cmd_buf.push_graphics_constants(
cmd_buf.set_push_constants(
&pipeline_layout.raw,
conv::map_shader_stage_flags(stages),
stages,
clear_offset,
clear_data,
);
Expand All @@ -686,10 +679,7 @@ impl RenderBundle {
first_vertex,
first_instance,
} => {
cmd_buf.draw(
first_vertex..first_vertex + vertex_count,
first_instance..first_instance + instance_count,
);
cmd_buf.draw(first_vertex, vertex_count, first_instance, instance_count);
}
RenderCommand::DrawIndexed {
index_count,
Expand All @@ -699,9 +689,11 @@ impl RenderBundle {
first_instance,
} => {
cmd_buf.draw_indexed(
first_index..first_index + index_count,
first_index,
index_count,
base_vertex,
first_instance..first_instance + instance_count,
first_instance,
instance_count,
);
}
RenderCommand::MultiDrawIndirect {
Expand All @@ -710,27 +702,27 @@ impl RenderBundle {
count: None,
indexed: false,
} => {
let &(ref buffer, _) = buffer_guard
let buffer = buffer_guard
.get(buffer_id)
.unwrap()
.raw
.as_ref()
.ok_or(ExecutionError::DestroyedBuffer(buffer_id))?;
cmd_buf.draw_indirect(buffer, offset, 1, 0);
cmd_buf.draw_indirect(buffer, offset, 1);
}
RenderCommand::MultiDrawIndirect {
buffer_id,
offset,
count: None,
indexed: true,
} => {
let &(ref buffer, _) = buffer_guard
let buffer = buffer_guard
.get(buffer_id)
.unwrap()
.raw
.as_ref()
.ok_or(ExecutionError::DestroyedBuffer(buffer_id))?;
cmd_buf.draw_indexed_indirect(buffer, offset, 1, 0);
cmd_buf.draw_indexed_indirect(buffer, offset, 1);
}
RenderCommand::MultiDrawIndirect { .. }
| RenderCommand::MultiDrawIndirectCount { .. } => {
Expand Down Expand Up @@ -943,8 +935,8 @@ struct VertexLimitState {
struct State {
trackers: TrackerSet,
index: IndexState,
vertex: ArrayVec<[VertexState; MAX_VERTEX_BUFFERS]>,
bind: ArrayVec<[BindState; MAX_BIND_GROUPS]>,
vertex: ArrayVec<[VertexState; hal::MAX_VERTEX_BUFFERS]>,
bind: ArrayVec<[BindState; hal::MAX_BIND_GROUPS]>,
push_constant_ranges: PushConstantState,
raw_dynamic_offsets: Vec<wgt::DynamicOffset>,
flat_dynamic_offsets: Vec<wgt::DynamicOffset>,
Expand Down
Loading

0 comments on commit 0a82c23

Please sign in to comment.