Skip to content

Commit

Permalink
Merge gfx-rs#342
Browse files Browse the repository at this point in the history
342: Fallback to D32 when D24 isn't available r=kvark a=grovesNL

The rationale is that D32 seems to be supported on more devices. Using D24 could be a future memory/performance optimization instead.

Otherwise if it's fairly trivial I could add the fallback path for D32 here instead, but I'm not sure how we intend for values in the `D24Unorm` depth range to work in general (i.e. compared to `D32Sfloat`). Maybe I missed some discussion about this, but I don't see it in the minutes.

cc @Yatekii 

Co-authored-by: Joshua Groves <josh@joshgroves.com>
  • Loading branch information
bors[bot] and grovesNL authored Sep 28, 2019
2 parents cbe197e + 5080a20 commit f523f2e
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 21 deletions.
4 changes: 3 additions & 1 deletion wgpu-native/src/command/allocator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::CommandBuffer;
use crate::{hub::GfxBackend, track::TrackerSet, DeviceId, LifeGuard, Stored, SubmissionIndex};
use crate::{hub::GfxBackend, track::TrackerSet, DeviceId, Features, LifeGuard, Stored, SubmissionIndex};

use hal::{command::CommandBuffer as _, device::Device as _, pool::CommandPool as _};
use log::trace;
Expand Down Expand Up @@ -63,6 +63,7 @@ impl<B: GfxBackend> CommandAllocator<B> {
&self,
device_id: Stored<DeviceId>,
device: &B::Device,
features: Features,
) -> CommandBuffer<B> {
//debug_assert_eq!(device_id.backend(), B::VARIANT);
let thread_id = thread::current().id();
Expand All @@ -88,6 +89,7 @@ impl<B: GfxBackend> CommandAllocator<B> {
life_guard: LifeGuard::new(),
trackers: TrackerSet::new(B::VARIANT),
used_swap_chain: None,
features,
}
}

Expand Down
8 changes: 5 additions & 3 deletions wgpu-native/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::{
CommandEncoderId,
ComputePassId,
DeviceId,
Features,
LifeGuard,
RenderPassId,
Stored,
Expand Down Expand Up @@ -119,6 +120,7 @@ pub struct CommandBuffer<B: hal::Backend> {
pub(crate) life_guard: LifeGuard,
pub(crate) trackers: TrackerSet,
pub(crate) used_swap_chain: Option<(Stored<TextureViewId>, B::Framebuffer)>,
pub(crate) features: Features,
}

impl<B: GfxBackend> CommandBuffer<B> {
Expand Down Expand Up @@ -324,7 +326,7 @@ pub fn command_encoder_begin_render_pass<B: GfxBackend>(
}
};
hal::pass::Attachment {
format: Some(conv::map_texture_format(view.format)),
format: Some(conv::map_texture_format(view.format, device.features)),
samples: view.samples,
ops: conv::map_load_store_ops(at.depth_load_op, at.depth_store_op),
stencil_ops: conv::map_load_store_ops(at.stencil_load_op, at.stencil_store_op),
Expand Down Expand Up @@ -400,7 +402,7 @@ pub fn command_encoder_begin_render_pass<B: GfxBackend>(
};

colors.push(hal::pass::Attachment {
format: Some(conv::map_texture_format(view.format)),
format: Some(conv::map_texture_format(view.format, device.features)),
samples: view.samples,
ops: conv::map_load_store_ops(at.load_op, at.store_op),
stencil_ops: hal::pass::AttachmentOps::DONT_CARE,
Expand Down Expand Up @@ -472,7 +474,7 @@ pub fn command_encoder_begin_render_pass<B: GfxBackend>(
};

resolves.push(hal::pass::Attachment {
format: Some(conv::map_texture_format(view.format)),
format: Some(conv::map_texture_format(view.format, device.features)),
samples: view.samples,
ops: hal::pass::AttachmentOps::new(
hal::pass::AttachmentLoadOp::DontCare,
Expand Down
5 changes: 2 additions & 3 deletions wgpu-native/src/command/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ pub fn command_encoder_copy_buffer_to_texture<B: GfxBackend>(
) {
let hub = B::hub();
let mut token = Token::root();

let (mut cmb_guard, mut token) = hub.command_buffers.write(&mut token);
let cmb = &mut cmb_guard[command_encoder_id];
let (buffer_guard, mut token) = hub.buffers.read(&mut token);
Expand Down Expand Up @@ -181,7 +180,7 @@ pub fn command_encoder_copy_buffer_to_texture<B: GfxBackend>(
});

let aspects = dst_texture.full_range.aspects;
let bytes_per_texel = conv::map_texture_format(dst_texture.format)
let bytes_per_texel = conv::map_texture_format(dst_texture.format, cmb.features)
.surface_desc()
.bits as u32
/ BITS_PER_BYTE;
Expand Down Expand Up @@ -271,7 +270,7 @@ pub fn command_encoder_copy_texture_to_buffer<B: GfxBackend>(
});

let aspects = src_texture.full_range.aspects;
let bytes_per_texel = conv::map_texture_format(src_texture.format)
let bytes_per_texel = conv::map_texture_format(src_texture.format, cmb.features)
.surface_desc()
.bits as u32
/ BITS_PER_BYTE;
Expand Down
16 changes: 12 additions & 4 deletions wgpu-native/src/conv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{binding_model, command, pipeline, resource, Color, Extent3d, Origin3d};
use crate::{binding_model, command, pipeline, resource, Color, Extent3d, Features, Origin3d};

pub fn map_buffer_usage(
usage: resource::BufferUsage,
Expand Down Expand Up @@ -307,7 +307,7 @@ fn map_stencil_operation(stencil_operation: pipeline::StencilOperation) -> hal::
}
}

pub fn map_texture_format(texture_format: resource::TextureFormat) -> hal::format::Format {
pub(crate) fn map_texture_format(texture_format: resource::TextureFormat, features: Features) -> hal::format::Format {
use crate::resource::TextureFormat as Tf;
use hal::format::Format as H;
match texture_format {
Expand Down Expand Up @@ -367,8 +367,16 @@ pub fn map_texture_format(texture_format: resource::TextureFormat) -> hal::forma

// Depth and stencil formats
Tf::Depth32Float => H::D32Sfloat,
Tf::Depth24Plus => H::D24UnormS8Uint, //TODO: substitute
Tf::Depth24PlusStencil8 => H::D24UnormS8Uint, //TODO: substitute
Tf::Depth24Plus => if features.supports_texture_d24_s8 {
H::D24UnormS8Uint
} else {
H::D32Sfloat
}
Tf::Depth24PlusStencil8 => if features.supports_texture_d24_s8 {
H::D24UnormS8Uint
} else {
H::D32SfloatS8Uint
},
}
}

Expand Down
29 changes: 23 additions & 6 deletions wgpu-native/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{
CommandEncoderId,
ComputePipelineId,
DeviceId,
Features,
LifeGuard,
PipelineLayoutId,
QueueId,
Expand All @@ -36,7 +37,9 @@ use crate::{
SurfaceId,
SwapChainId,
TextureDimension,
TextureFormat,
TextureId,
TextureUsage,
TextureViewId,
};

Expand Down Expand Up @@ -496,6 +499,7 @@ pub struct Device<B: hal::Backend> {
pub(crate) render_passes: Mutex<FastHashMap<RenderPassKey, B::RenderPass>>,
pub(crate) framebuffers: Mutex<FastHashMap<FramebufferKey, B::Framebuffer>>,
pending: Mutex<PendingResources<B>>,
pub(crate) features: Features,
}

impl<B: GfxBackend> Device<B> {
Expand All @@ -504,6 +508,7 @@ impl<B: GfxBackend> Device<B> {
adapter_id: AdapterId,
queue_group: hal::queue::QueueGroup<B>,
mem_props: hal::adapter::MemoryProperties,
supports_texture_d24_s8: bool,
) -> Self {
// don't start submission index at zero
let life_guard = LifeGuard::new();
Expand Down Expand Up @@ -549,6 +554,9 @@ impl<B: GfxBackend> Device<B> {
free: Vec::new(),
ready_to_map: Vec::new(),
}),
features: Features {
supports_texture_d24_s8,
}
}
}

Expand Down Expand Up @@ -660,13 +668,22 @@ impl<B: GfxBackend> Device<B> {
desc: &resource::TextureDescriptor,
) -> resource::Texture<B> {
debug_assert_eq!(self_id.backend(), B::VARIANT);

// Ensure `D24Plus` textures cannot be copied
match desc.format {
TextureFormat::Depth24Plus | TextureFormat::Depth24PlusStencil8 => {
assert!(!desc.usage.intersects(TextureUsage::COPY_SRC | TextureUsage::COPY_DST));
}
_ => {}
}

let kind = conv::map_texture_dimension_size(
desc.dimension,
desc.size,
desc.array_layer_count,
desc.sample_count,
);
let format = conv::map_texture_format(desc.format);
let format = conv::map_texture_format(desc.format, self.features);
let aspects = format.surface_desc().aspects;
let usage = conv::map_texture_usage(desc.usage, aspects);

Expand Down Expand Up @@ -933,7 +950,7 @@ pub fn texture_create_view<B: GfxBackend>(
.create_image_view(
&texture.raw,
view_kind,
conv::map_texture_format(format),
conv::map_texture_format(format, device.features),
hal::format::Swizzle::NO,
range.clone(),
)
Expand Down Expand Up @@ -1425,7 +1442,7 @@ pub fn device_create_command_encoder<B: GfxBackend>(
value: device_id,
ref_count: device.life_guard.ref_count.clone(),
};
let mut comb = device.com_allocator.allocate(dev_stored, &device.raw);
let mut comb = device.com_allocator.allocate(dev_stored, &device.raw, device.features);
unsafe {
comb.raw.last_mut().unwrap().begin(
hal::command::CommandBufferFlags::ONE_TIME_SUBMIT,
Expand Down Expand Up @@ -1727,7 +1744,7 @@ pub fn device_create_render_pipeline<B: GfxBackend>(
colors: color_states
.iter()
.map(|at| hal::pass::Attachment {
format: Some(conv::map_texture_format(at.format)),
format: Some(conv::map_texture_format(at.format, device.features)),
samples: sc,
ops: hal::pass::AttachmentOps::PRESERVE,
stencil_ops: hal::pass::AttachmentOps::DONT_CARE,
Expand All @@ -1740,7 +1757,7 @@ pub fn device_create_render_pipeline<B: GfxBackend>(
// or depth/stencil resolve modes but satisfy the other compatibility conditions.
resolves: ArrayVec::new(),
depth_stencil: depth_stencil_state.map(|at| hal::pass::Attachment {
format: Some(conv::map_texture_format(at.format)),
format: Some(conv::map_texture_format(at.format, device.features)),
samples: sc,
ops: hal::pass::AttachmentOps::PRESERVE,
stencil_ops: hal::pass::AttachmentOps::PRESERVE,
Expand Down Expand Up @@ -1970,7 +1987,7 @@ pub fn device_create_swap_chain<B: GfxBackend>(
let num_frames = swap_chain::DESIRED_NUM_FRAMES
.max(*caps.image_count.start())
.min(*caps.image_count.end());
let config = desc.to_hal(num_frames);
let config = desc.to_hal(num_frames, &device.features);

if let Some(formats) = formats {
assert!(
Expand Down
9 changes: 8 additions & 1 deletion wgpu-native/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ pub struct Adapter<B: hal::Backend> {
pub(crate) raw: hal::adapter::Adapter<B>,
}


#[repr(C)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "remote", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -459,11 +458,19 @@ pub fn adapter_request_device<B: GfxBackend>(
);

let mem_props = adapter.physical_device.memory_properties();

let supports_texture_d24_s8 = adapter
.physical_device
.format_properties(Some(hal::format::Format::D24UnormS8Uint))
.optimal_tiling
.contains(hal::format::ImageFeature::DEPTH_STENCIL_ATTACHMENT);

Device::new(
gpu.device,
adapter_id,
gpu.queue_groups.swap_remove(0),
mem_props,
supports_texture_d24_s8,
)
};

Expand Down
5 changes: 5 additions & 0 deletions wgpu-native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,8 @@ macro_rules! gfx_select {
}
};
}

#[derive(Clone, Copy, Debug)]
pub(crate) struct Features {
pub supports_texture_d24_s8: bool,
}
7 changes: 4 additions & 3 deletions wgpu-native/src/swap_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use crate::{
resource,
DeviceId,
Extent3d,
Features,
Input,
LifeGuard,
Stored,
Expand Down Expand Up @@ -83,11 +84,11 @@ pub struct SwapChainDescriptor {
}

impl SwapChainDescriptor {
pub(crate) fn to_hal(&self, num_frames: u32) -> hal::window::SwapchainConfig {
pub(crate) fn to_hal(&self, num_frames: u32, features: &Features) -> hal::window::SwapchainConfig {
let mut config = hal::window::SwapchainConfig::new(
self.width,
self.height,
conv::map_texture_format(self.format),
conv::map_texture_format(self.format, *features),
num_frames,
);
//TODO: check for supported
Expand Down Expand Up @@ -146,7 +147,7 @@ pub fn swap_chain_get_next_texture<B: GfxBackend>(
}
Err(e) => {
log::warn!("acquire_image() failed ({:?}), reconfiguring swapchain", e);
let desc = sc.desc.to_hal(sc.num_frames);
let desc = sc.desc.to_hal(sc.num_frames, &device.features);
unsafe {
suf
.configure_swapchain(&device.raw, desc)
Expand Down

0 comments on commit f523f2e

Please sign in to comment.