Skip to content

Commit

Permalink
rt(d3d12): allow creating input view without a CPU handle
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyran committed Sep 29, 2024
1 parent d2e3fb5 commit 4338283
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
2 changes: 1 addition & 1 deletion librashader-cli/src/render/d3d12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl RenderTest for Direct3D12 {
&cmd,
D3D12InputImage {
resource: self.texture.to_ref(),
descriptor: *self._heap_slot.as_ref(),
descriptor: None,
},
&viewport,
frame,
Expand Down
4 changes: 2 additions & 2 deletions librashader-runtime-d3d12/src/filter_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl FilterChainD3D12 {
let rtv_heap = unsafe {
D3D12DescriptorHeap::new(
device,
(MAX_BINDINGS_COUNT as usize) * shader_count
(1 + MAX_BINDINGS_COUNT as usize) * shader_count
+ MIPMAP_RESERVED_WORKHEAP_DESCRIPTORS
+ lut_count,
)
Expand Down Expand Up @@ -735,7 +735,7 @@ impl FilterChainD3D12 {
Some(fbo.create_shader_resource_view(&mut self.staging_heap, filter, wrap_mode)?);
}

let original = unsafe { InputTexture::new_from_raw(input, filter, wrap_mode) };
let original = unsafe { InputTexture::new_from_raw(input, filter, wrap_mode, &self.common.d3d12, &mut self.staging_heap)? };
let mut source = original.clone();

// swap output and feedback **before** recording command buffers
Expand Down
60 changes: 44 additions & 16 deletions librashader-runtime-d3d12/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ use librashader_common::{FilterMode, GetSize, Size, WrapMode};
use std::mem::ManuallyDrop;
use std::ops::Deref;
use windows::core::InterfaceRef;
use windows::Win32::Graphics::Direct3D12::{
ID3D12Device, ID3D12Resource, D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_RENDER_TARGET_VIEW_DESC,
D3D12_RENDER_TARGET_VIEW_DESC_0, D3D12_RESOURCE_DIMENSION_TEXTURE2D,
D3D12_RTV_DIMENSION_TEXTURE2D, D3D12_TEX2D_RTV,
};
use windows::Win32::Graphics::Direct3D12::{ID3D12Device, ID3D12Resource, D3D12_CPU_DESCRIPTOR_HANDLE, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING, D3D12_RENDER_TARGET_VIEW_DESC, D3D12_RENDER_TARGET_VIEW_DESC_0, D3D12_RESOURCE_DIMENSION_TEXTURE2D, D3D12_RTV_DIMENSION_TEXTURE2D, D3D12_SHADER_RESOURCE_VIEW_DESC, D3D12_SHADER_RESOURCE_VIEW_DESC_0, D3D12_SRV_DIMENSION_TEXTURE2D, D3D12_TEX2D_RTV, D3D12_TEX2D_SRV};
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT;
use crate::error::FilterChainError;

/// A **non-owning** reference to a ID3D12Resource.
/// This does not `AddRef` or `Release` the underlying interface.
Expand All @@ -21,7 +18,16 @@ pub type D3D12ResourceRef<'a> = InterfaceRef<'a, ID3D12Resource>;
#[derive(Clone)]
pub struct D3D12InputImage<'a> {
pub resource: InterfaceRef<'a, ID3D12Resource>,
pub descriptor: D3D12_CPU_DESCRIPTOR_HANDLE,
pub descriptor: Option<D3D12_CPU_DESCRIPTOR_HANDLE>,
}

impl<'a> From<InterfaceRef<'a, ID3D12Resource>> for D3D12InputImage<'a> {
fn from(value: InterfaceRef<'a, ID3D12Resource>) -> Self {
Self {
resource: value,
descriptor: None
}
}
}

#[derive(Clone)]
Expand All @@ -30,12 +36,6 @@ pub(crate) enum InputDescriptor {
Raw(D3D12_CPU_DESCRIPTOR_HANDLE),
}

impl InputDescriptor {
fn is_raw(&self) -> bool {
matches!(self, InputDescriptor::Raw(_))
}
}

#[derive(Clone)]
pub(crate) enum OutputDescriptor {
Owned(D3D12DescriptorHeapSlot<RenderTargetHeap>),
Expand Down Expand Up @@ -186,17 +186,45 @@ impl InputTexture {
image: D3D12InputImage,
filter: FilterMode,
wrap_mode: WrapMode,
) -> InputTexture {
device: &ID3D12Device,
heap: &mut D3D12DescriptorHeap<CpuStagingHeap>,
) -> error::Result<InputTexture> {
let desc = unsafe { image.resource.GetDesc() };
InputTexture {
if desc.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D {
return Err(FilterChainError::InvalidDimensionError(desc.Dimension));
}

let descriptor = image.descriptor.map_or_else(|| {
let slot = heap.allocate_descriptor()?;
unsafe {
let srv_desc = D3D12_SHADER_RESOURCE_VIEW_DESC {
Format: desc.Format,
ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2D,
Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
Anonymous: D3D12_SHADER_RESOURCE_VIEW_DESC_0 {
Texture2D: D3D12_TEX2D_SRV {
MipLevels: desc.MipLevels as u32,
..Default::default()
},
},
};
device.CreateShaderResourceView(image.resource.deref(), Some(&srv_desc), *slot.as_ref());
}

Ok::<_, FilterChainError>(InputDescriptor::Owned(slot))
}, |raw| Ok(InputDescriptor::Raw(raw)))?;

Ok(InputTexture {
resource: unsafe { std::mem::transmute(image.resource) },
descriptor: InputDescriptor::Raw(image.descriptor),
descriptor,
size: Size::new(desc.Width as u32, desc.Height),
format: desc.Format,
wrap_mode,
filter,
}
})
}


}

impl Clone for InputTexture {
Expand Down
2 changes: 1 addition & 1 deletion librashader-runtime-d3d12/tests/hello_triangle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ pub mod d3d12_hello_triangle {
command_list,
D3D12InputImage {
resource: resources.framebuffer.to_ref(),
descriptor: framebuffer,
descriptor: Some(framebuffer),
},
&Viewport {
x: 0.0,
Expand Down

0 comments on commit 4338283

Please sign in to comment.