Skip to content

Commit

Permalink
Merge #3424
Browse files Browse the repository at this point in the history
3424: Flexible swapchain usage r=msiglreith,grovesNL a=kvark

Fixes #3388 (some backend-specific code needs to be added, but at this point it becomes an implemented feature with bugs rather than an unimplemented one).
Collaterally, removes WGL support from the GL backend.
Also includes a few Serious Sam-related DX12 fixes, implements `fill_buffer`.
Also includes a hack to allow multiple frames in flight in DX12.
PR checklist:
- [x] `make` succeeds (on *nix)
- [x] `make reftests` succeeds (everywhere, including DX12!)
- [ ] tested examples with the following backends:


Co-authored-by: Dzmitry Malyshau <[email protected]>
  • Loading branch information
bors[bot] and kvark authored Oct 22, 2020
2 parents fb6bba7 + b72a41b commit f09b062
Show file tree
Hide file tree
Showing 31 changed files with 611 additions and 809 deletions.
55 changes: 41 additions & 14 deletions src/backend/dx11/src/conv.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use auxil::ShaderStage;
use hal::{
format::Format,
image::{Filter, WrapMode},
format::{Aspects, Format, FormatDesc},
image,
pso::{
BlendDesc, BlendOp, ColorBlendDesc, Comparison, DepthBias, DepthStencilDesc, Face, Factor,
FrontFace, InputAssemblerDesc, Multisampling, PolygonMode, Rasterizer, Rect, Sided, State, StencilFace,
Expand Down Expand Up @@ -784,28 +784,29 @@ pub fn map_stage(stage: ShaderStage) -> spirv::ExecutionModel {
}
}

pub fn map_wrapping(wrap: WrapMode) -> D3D11_TEXTURE_ADDRESS_MODE {
pub fn map_wrapping(wrap: image::WrapMode) -> D3D11_TEXTURE_ADDRESS_MODE {
use hal::image::WrapMode as Wm;
match wrap {
WrapMode::Tile => D3D11_TEXTURE_ADDRESS_WRAP,
WrapMode::Mirror => D3D11_TEXTURE_ADDRESS_MIRROR,
WrapMode::Clamp => D3D11_TEXTURE_ADDRESS_CLAMP,
WrapMode::Border => D3D11_TEXTURE_ADDRESS_BORDER,
WrapMode::MirrorClamp => D3D11_TEXTURE_ADDRESS_MIRROR_ONCE,
Wm::Tile => D3D11_TEXTURE_ADDRESS_WRAP,
Wm::Mirror => D3D11_TEXTURE_ADDRESS_MIRROR,
Wm::Clamp => D3D11_TEXTURE_ADDRESS_CLAMP,
Wm::Border => D3D11_TEXTURE_ADDRESS_BORDER,
Wm::MirrorClamp => D3D11_TEXTURE_ADDRESS_MIRROR_ONCE,
}
}

fn map_filter_type(filter: Filter) -> D3D11_FILTER_TYPE {
fn map_filter_type(filter: image::Filter) -> D3D11_FILTER_TYPE {
match filter {
Filter::Nearest => D3D11_FILTER_TYPE_POINT,
Filter::Linear => D3D11_FILTER_TYPE_LINEAR,
image::Filter::Nearest => D3D11_FILTER_TYPE_POINT,
image::Filter::Linear => D3D11_FILTER_TYPE_LINEAR,
}
}

// Hopefully works just as well in d3d11 :)
pub fn map_filter(
mag_filter: Filter,
min_filter: Filter,
mip_filter: Filter,
mag_filter: image::Filter,
min_filter: image::Filter,
mip_filter: image::Filter,
reduction: D3D11_FILTER_REDUCTION_TYPE,
anisotropy_clamp: Option<u8>,
) -> D3D11_FILTER {
Expand All @@ -821,3 +822,29 @@ pub fn map_filter(
.map(|_| D3D11_FILTER_ANISOTROPIC)
.unwrap_or(0)
}

pub fn map_image_usage(usage: image::Usage, format_desc: FormatDesc) -> D3D11_BIND_FLAG {
let mut bind = 0;

if usage.intersects(image::Usage::TRANSFER_SRC | image::Usage::SAMPLED | image::Usage::STORAGE)
{
bind |= D3D11_BIND_SHADER_RESOURCE;
}

// we cant get RTVs or UAVs on compressed & depth formats
if !format_desc.is_compressed() && !format_desc.aspects.contains(Aspects::DEPTH) {
if usage.intersects(image::Usage::COLOR_ATTACHMENT | image::Usage::TRANSFER_DST) {
bind |= D3D11_BIND_RENDER_TARGET;
}

if usage.intersects(image::Usage::TRANSFER_DST | image::Usage::STORAGE) {
bind |= D3D11_BIND_UNORDERED_ACCESS;
}
}

if usage.contains(image::Usage::DEPTH_STENCIL_ATTACHMENT) {
bind |= D3D11_BIND_DEPTH_STENCIL;
}

bind
}
33 changes: 3 additions & 30 deletions src/backend/dx11/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,38 +1297,12 @@ impl device::Device<Backend> for Device {
usage: image::Usage,
view_caps: image::ViewCapabilities,
) -> Result<Image, image::CreationError> {
use image::Usage;
//
// TODO: create desc

let surface_desc = format.base_format().0.desc();
let bytes_per_texel = surface_desc.bits / 8;
let ext = kind.extent();
let size = (ext.width * ext.height * ext.depth) as u64 * bytes_per_texel as u64;
let compressed = surface_desc.is_compressed();
let depth = format.is_depth();

let mut bind = 0;

if usage.intersects(Usage::TRANSFER_SRC | Usage::SAMPLED | Usage::STORAGE) {
bind |= d3d11::D3D11_BIND_SHADER_RESOURCE;
}

// we cant get RTVs or UAVs on compressed & depth formats
if !compressed && !depth {
if usage.intersects(Usage::COLOR_ATTACHMENT | Usage::TRANSFER_DST) {
bind |= d3d11::D3D11_BIND_RENDER_TARGET;
}

if usage.intersects(Usage::TRANSFER_DST | Usage::STORAGE) {
bind |= d3d11::D3D11_BIND_UNORDERED_ACCESS;
}
}

if usage.contains(Usage::DEPTH_STENCIL_ATTACHMENT) {
bind |= d3d11::D3D11_BIND_DEPTH_STENCIL;
}

let bind = conv::map_image_usage(usage, surface_desc);
debug!("{:b}", bind);

Ok(Image {
Expand Down Expand Up @@ -2151,12 +2125,11 @@ impl device::Device<Backend> for Device {
unsafe fn destroy_buffer(&self, _buffer: Buffer) {}

unsafe fn destroy_buffer_view(&self, _view: BufferView) {
unimplemented!()
//unimplemented!()
}

unsafe fn destroy_image(&self, _image: Image) {
// TODO:
// unimplemented!()
//unimplemented!()
}

unsafe fn destroy_image_view(&self, _view: ImageView) {
Expand Down
99 changes: 78 additions & 21 deletions src/backend/dx11/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,16 @@ impl hal::Instance<Backend> for Instance {
],
// TODO: would using *VideoMemory and *SystemMemory from
// DXGI_ADAPTER_DESC be too optimistic? :)
memory_heaps: vec![!0, !0],
memory_heaps: vec![
adapter::MemoryHeap {
size: !0,
flags: memory::HeapFlags::DEVICE_LOCAL,
},
adapter::MemoryHeap {
size: !0,
flags: memory::HeapFlags::empty(),
},
],
};

let limits = get_limits(feature_level);
Expand Down Expand Up @@ -825,6 +834,7 @@ struct Presentation {
format: format::Format,
size: window::Extent2D,
mode: window::PresentMode,
image: Arc<Image>,
is_init: bool,
}

Expand Down Expand Up @@ -895,8 +905,24 @@ impl window::Surface<Backend> for Surface {
}
}

#[derive(Debug)]
pub struct SwapchainImage {
image: Arc<Image>,
view: ImageView,
}
impl Borrow<Image> for SwapchainImage {
fn borrow(&self) -> &Image {
&*self.image
}
}
impl Borrow<ImageView> for SwapchainImage {
fn borrow(&self) -> &ImageView {
&self.view
}
}

impl window::PresentationSurface<Backend> for Surface {
type SwapchainImage = ImageView;
type SwapchainImage = SwapchainImage;

unsafe fn configure_swapchain(
&mut self,
Expand Down Expand Up @@ -945,27 +971,47 @@ impl window::PresentationSurface<Backend> for Surface {

let kind = image::Kind::D2(config.extent.width, config.extent.height, 1, 1);
let format = conv::map_format(config.format).unwrap();
let decomposed = conv::DecomposedDxgiFormat::from_dxgi_format(format);
let decomposed_format = conv::DecomposedDxgiFormat::from_dxgi_format(format);

let view_info = ViewInfo {
resource,
kind,
caps: image::ViewCapabilities::empty(),
view_kind: image::ViewKind::D2,
format: decomposed.rtv.unwrap(),
format: decomposed_format.rtv.unwrap(),
levels: 0..1,
layers: 0..1,
};
let view = device.view_image_as_render_target(&view_info).unwrap();

(*resource).Release();

self.presentation = Some(Presentation {
swapchain,
view,
format: config.format,
size: config.extent,
mode: config.present_mode,
image: Arc::new(Image {
kind,
usage: config.image_usage,
format: config.format,
view_caps: image::ViewCapabilities::empty(),
decomposed_format,
mip_levels: 1,
internal: InternalImage {
raw: resource,
copy_srv: None, //TODO
srv: None, //TODO
unordered_access_views: Vec::new(),
depth_stencil_views: Vec::new(),
render_target_views: Vec::new(),
},
bind: conv::map_image_usage(config.image_usage, config.format.surface_desc()),
requirements: memory::Requirements {
size: 0,
alignment: 1,
type_mask: 0,
},
}),
is_init: true,
});
Ok(())
Expand All @@ -978,18 +1024,21 @@ impl window::PresentationSurface<Backend> for Surface {
unsafe fn acquire_image(
&mut self,
_timeout_ns: u64, //TODO: use the timeout
) -> Result<(ImageView, Option<window::Suboptimal>), window::AcquireError> {
) -> Result<(SwapchainImage, Option<window::Suboptimal>), window::AcquireError> {
let present = self.presentation.as_ref().unwrap();
let image_view = ImageView {
subresource: d3d11::D3D11CalcSubresource(0, 0, 1),
format: present.format,
rtv_handle: Some(present.view.clone()),
dsv_handle: None,
srv_handle: None,
uav_handle: None,
rodsv_handle: None,
let swapchain_image = SwapchainImage {
image: Arc::clone(&present.image),
view: ImageView {
subresource: d3d11::D3D11CalcSubresource(0, 0, 1),
format: present.format,
rtv_handle: Some(present.view.clone()),
dsv_handle: None,
srv_handle: None,
uav_handle: None,
rodsv_handle: None,
},
};
Ok((image_view, None))
Ok((swapchain_image, None))
}
}

Expand Down Expand Up @@ -1070,7 +1119,7 @@ impl queue::CommandQueue<Backend> for CommandQueue {
unsafe fn present(
&mut self,
surface: &mut Surface,
_image: ImageView,
_image: SwapchainImage,
_wait_semaphore: Option<&Semaphore>,
) -> Result<Option<window::Suboptimal>, window::PresentError> {
let mut presentation = surface.presentation.as_mut().unwrap();
Expand Down Expand Up @@ -3107,11 +3156,11 @@ unsafe impl Send for Image {}
unsafe impl Sync for Image {}

impl Image {
pub fn calc_subresource(&self, mip_level: UINT, layer: UINT) -> UINT {
fn calc_subresource(&self, mip_level: UINT, layer: UINT) -> UINT {
mip_level + (layer * self.mip_levels as UINT)
}

pub fn get_uav(
fn get_uav(
&self,
mip_level: image::Level,
_layer: image::Layer,
Expand All @@ -3121,7 +3170,7 @@ impl Image {
.get(self.calc_subresource(mip_level as _, 0) as usize)
}

pub fn get_dsv(
fn get_dsv(
&self,
mip_level: image::Level,
layer: image::Layer,
Expand All @@ -3131,7 +3180,7 @@ impl Image {
.get(self.calc_subresource(mip_level as _, layer as _) as usize)
}

pub fn get_rtv(
fn get_rtv(
&self,
mip_level: image::Level,
layer: image::Layer,
Expand All @@ -3142,6 +3191,14 @@ impl Image {
}
}

impl Drop for Image {
fn drop(&mut self) {
unsafe {
(*self.internal.raw).Release();
}
}
}

#[derive(Clone)]
pub struct ImageView {
subresource: UINT,
Expand Down
Loading

0 comments on commit f09b062

Please sign in to comment.