librashader/librashader-runtime-d3d12/src/texture.rs

135 lines
3.6 KiB
Rust
Raw Normal View History

2023-02-05 16:17:23 -05:00
use crate::descriptor_heap::{CpuStagingHeap, D3D12DescriptorHeapSlot, RenderTargetHeap};
use librashader_common::{FilterMode, Size, WrapMode};
2023-02-01 18:09:34 -05:00
use std::ops::Deref;
2023-02-05 18:03:38 -05:00
use windows::Win32::Graphics::Direct3D12::{ID3D12Resource, D3D12_CPU_DESCRIPTOR_HANDLE};
2023-02-01 01:25:39 -05:00
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT;
2023-01-31 17:50:47 -05:00
2023-02-06 01:25:06 -05:00
/// An image for use as shader resource view.
2023-02-06 01:05:19 -05:00
pub struct D3D12InputImage {
pub resource: ID3D12Resource,
pub descriptor: D3D12_CPU_DESCRIPTOR_HANDLE,
}
2023-02-01 18:09:34 -05:00
#[derive(Clone)]
2023-01-31 19:23:57 -05:00
pub(crate) enum InputDescriptor {
2023-01-31 17:50:47 -05:00
Owned(D3D12DescriptorHeapSlot<CpuStagingHeap>),
2023-02-05 16:17:23 -05:00
Raw(D3D12_CPU_DESCRIPTOR_HANDLE),
2023-01-31 17:50:47 -05:00
}
2023-02-01 18:09:34 -05:00
#[derive(Clone)]
2023-01-31 19:23:57 -05:00
pub(crate) enum OutputDescriptor {
2023-02-01 00:16:06 -05:00
Owned(D3D12DescriptorHeapSlot<RenderTargetHeap>),
2023-02-05 16:17:23 -05:00
Raw(D3D12_CPU_DESCRIPTOR_HANDLE),
2023-01-31 17:50:47 -05:00
}
impl AsRef<D3D12_CPU_DESCRIPTOR_HANDLE> for InputDescriptor {
fn as_ref(&self) -> &D3D12_CPU_DESCRIPTOR_HANDLE {
match self {
2023-02-01 18:09:34 -05:00
InputDescriptor::Owned(h) => h.deref().as_ref(),
2023-02-05 16:17:23 -05:00
InputDescriptor::Raw(h) => h,
2023-01-31 17:50:47 -05:00
}
}
}
impl AsRef<D3D12_CPU_DESCRIPTOR_HANDLE> for OutputDescriptor {
fn as_ref(&self) -> &D3D12_CPU_DESCRIPTOR_HANDLE {
match self {
2023-02-01 18:09:34 -05:00
OutputDescriptor::Owned(h) => h.deref().as_ref(),
2023-02-05 16:17:23 -05:00
OutputDescriptor::Raw(h) => h,
2023-01-31 17:50:47 -05:00
}
}
}
2023-02-06 01:25:06 -05:00
/// An image view for use as a render target.
///
/// Can be created from a CPU descriptor handle, and a size.
2023-02-01 18:09:34 -05:00
#[derive(Clone)]
2023-02-06 01:05:19 -05:00
pub struct D3D12OutputView {
2023-02-01 00:16:06 -05:00
pub(crate) descriptor: OutputDescriptor,
pub(crate) size: Size<u32>,
pub(crate) format: DXGI_FORMAT,
2023-01-31 17:50:47 -05:00
}
2023-02-06 01:05:19 -05:00
impl D3D12OutputView {
pub(crate) fn new(
2023-02-05 16:17:23 -05:00
handle: D3D12DescriptorHeapSlot<RenderTargetHeap>,
size: Size<u32>,
format: DXGI_FORMAT,
2023-02-06 01:05:19 -05:00
) -> D3D12OutputView {
2023-01-31 17:50:47 -05:00
let descriptor = OutputDescriptor::Owned(handle);
D3D12OutputView {
descriptor,
size,
format,
}
2023-01-31 17:50:47 -05:00
}
// unsafe since the lifetime of the handle has to survive
2023-02-05 16:17:23 -05:00
pub unsafe fn new_from_raw(
handle: D3D12_CPU_DESCRIPTOR_HANDLE,
size: Size<u32>,
format: DXGI_FORMAT,
2023-02-06 01:05:19 -05:00
) -> D3D12OutputView {
2023-01-31 17:50:47 -05:00
let descriptor = OutputDescriptor::Raw(handle);
D3D12OutputView {
descriptor,
size,
format,
}
2023-01-31 17:50:47 -05:00
}
}
2023-02-01 18:09:34 -05:00
#[derive(Clone)]
2023-01-31 17:50:47 -05:00
pub struct InputTexture {
2023-02-05 18:03:38 -05:00
pub(crate) resource: ID3D12Resource,
2023-01-31 19:23:57 -05:00
pub(crate) descriptor: InputDescriptor,
pub(crate) size: Size<u32>,
2023-02-05 18:03:38 -05:00
pub(crate) format: DXGI_FORMAT,
2023-01-31 19:23:57 -05:00
pub(crate) wrap_mode: WrapMode,
2023-02-05 16:17:23 -05:00
pub(crate) filter: FilterMode,
2023-01-31 17:50:47 -05:00
}
impl InputTexture {
2023-02-05 16:17:23 -05:00
pub fn new(
2023-02-05 18:03:38 -05:00
resource: ID3D12Resource,
2023-02-05 16:17:23 -05:00
handle: D3D12DescriptorHeapSlot<CpuStagingHeap>,
size: Size<u32>,
format: DXGI_FORMAT,
2023-02-05 16:17:23 -05:00
filter: FilterMode,
2023-02-06 01:05:19 -05:00
wrap_mode: WrapMode,
2023-02-05 16:17:23 -05:00
) -> InputTexture {
2023-01-31 17:50:47 -05:00
let srv = InputDescriptor::Owned(handle);
InputTexture {
2023-02-05 18:03:38 -05:00
resource,
2023-01-31 17:50:47 -05:00
descriptor: srv,
size,
format,
2023-01-31 17:50:47 -05:00
wrap_mode,
2023-02-05 16:17:23 -05:00
filter,
2023-01-31 17:50:47 -05:00
}
}
// unsafe since the lifetime of the handle has to survive
2023-02-05 16:17:23 -05:00
pub unsafe fn new_from_raw(
2023-02-06 01:05:19 -05:00
image: D3D12InputImage,
2023-02-05 16:17:23 -05:00
filter: FilterMode,
2023-02-06 01:05:19 -05:00
wrap_mode: WrapMode,
2023-01-31 17:50:47 -05:00
) -> InputTexture {
let desc = unsafe { image.resource.GetDesc() };
2023-01-31 17:50:47 -05:00
InputTexture {
2023-02-06 01:05:19 -05:00
resource: image.resource,
descriptor: InputDescriptor::Raw(image.descriptor),
size: Size::new(desc.Width as u32, desc.Height),
format: desc.Format,
2023-01-31 17:50:47 -05:00
wrap_mode,
2023-02-05 16:17:23 -05:00
filter,
2023-01-31 17:50:47 -05:00
}
}
}
2023-01-31 19:23:57 -05:00
impl AsRef<InputTexture> for InputTexture {
fn as_ref(&self) -> &InputTexture {
self
}
2023-02-05 16:17:23 -05:00
}