2023-02-06 10:34:30 +11:00
|
|
|
use crate::descriptor_heap::{CpuStagingHeap, D3D12DescriptorHeap};
|
2023-01-25 15:15:43 +11:00
|
|
|
use crate::error;
|
2023-07-20 15:13:22 +10:00
|
|
|
use crate::filter_chain::FrameResiduals;
|
2023-02-06 08:17:23 +11:00
|
|
|
use crate::mipmap::MipmapGenContext;
|
|
|
|
use crate::texture::InputTexture;
|
2023-01-25 17:32:10 +11:00
|
|
|
use crate::util::{d3d12_get_closest_format, d3d12_resource_transition, d3d12_update_subresources};
|
2024-08-25 15:24:05 +10:00
|
|
|
use gpu_allocator::d3d12::{
|
|
|
|
Allocator, Resource, ResourceCategory, ResourceCreateDesc, ResourceStateOrBarrierLayout,
|
|
|
|
ResourceType,
|
|
|
|
};
|
|
|
|
use gpu_allocator::MemoryLocation;
|
2023-02-06 10:34:30 +11:00
|
|
|
use librashader_common::{FilterMode, ImageFormat, WrapMode};
|
2023-01-24 18:02:27 +11:00
|
|
|
use librashader_runtime::image::Image;
|
2023-01-27 09:57:54 +11:00
|
|
|
use librashader_runtime::scaling::MipmapSize;
|
2024-08-25 15:24:05 +10:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
use std::mem::ManuallyDrop;
|
2023-02-06 08:17:23 +11:00
|
|
|
use std::ops::Deref;
|
2024-08-25 15:24:05 +10:00
|
|
|
use std::sync::Arc;
|
2023-02-06 08:17:23 +11:00
|
|
|
use windows::Win32::Graphics::Direct3D12::{
|
2024-08-25 15:24:05 +10:00
|
|
|
ID3D12Device, ID3D12GraphicsCommandList, D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
|
|
|
|
D3D12_FEATURE_DATA_FORMAT_SUPPORT, D3D12_FORMAT_SUPPORT1_MIP,
|
|
|
|
D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE, D3D12_FORMAT_SUPPORT1_TEXTURE2D,
|
2023-02-06 10:34:30 +11:00
|
|
|
D3D12_PLACED_SUBRESOURCE_FOOTPRINT, D3D12_RESOURCE_DESC, D3D12_RESOURCE_DIMENSION_BUFFER,
|
|
|
|
D3D12_RESOURCE_DIMENSION_TEXTURE2D, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS,
|
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_GENERIC_READ,
|
|
|
|
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_SHADER_RESOURCE_VIEW_DESC,
|
|
|
|
D3D12_SHADER_RESOURCE_VIEW_DESC_0, D3D12_SRV_DIMENSION_TEXTURE2D, D3D12_SUBRESOURCE_DATA,
|
|
|
|
D3D12_TEX2D_SRV, D3D12_TEXTURE_LAYOUT_ROW_MAJOR,
|
2023-02-06 08:17:23 +11:00
|
|
|
};
|
|
|
|
use windows::Win32::Graphics::Dxgi::Common::DXGI_SAMPLE_DESC;
|
2023-01-24 18:02:27 +11:00
|
|
|
|
|
|
|
pub struct LutTexture {
|
2024-08-25 15:24:05 +10:00
|
|
|
resource: ManuallyDrop<Resource>,
|
2023-02-01 11:23:57 +11:00
|
|
|
view: InputTexture,
|
2023-01-28 17:38:55 +11:00
|
|
|
miplevels: Option<u16>,
|
2024-08-25 15:24:05 +10:00
|
|
|
// Staging heap needs to be kept alive until the command list is submitted, which is
|
|
|
|
// really annoying. We could probably do better but it's safer to keep it around.
|
|
|
|
staging: ManuallyDrop<Resource>,
|
|
|
|
allocator: Arc<Mutex<Allocator>>,
|
2023-01-24 18:02:27 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LutTexture {
|
2023-04-23 15:13:31 +10:00
|
|
|
pub(crate) fn new(
|
2023-01-24 18:02:27 +11:00
|
|
|
device: &ID3D12Device,
|
2024-08-25 15:24:05 +10:00
|
|
|
allocator: &Arc<Mutex<Allocator>>,
|
2023-02-01 09:50:47 +11:00
|
|
|
heap: &mut D3D12DescriptorHeap<CpuStagingHeap>,
|
2023-01-25 15:15:43 +11:00
|
|
|
cmd: &ID3D12GraphicsCommandList,
|
2023-01-24 18:02:27 +11:00
|
|
|
source: &Image,
|
|
|
|
filter: FilterMode,
|
|
|
|
wrap_mode: WrapMode,
|
2023-01-25 15:15:43 +11:00
|
|
|
mipmap: bool,
|
2023-04-23 15:13:31 +10:00
|
|
|
gc: &mut FrameResiduals,
|
2023-02-11 18:42:33 +11:00
|
|
|
) -> error::Result<LutTexture> {
|
2023-01-28 17:38:55 +11:00
|
|
|
let miplevels = source.size.calculate_miplevels() as u16;
|
2023-01-25 15:15:43 +11:00
|
|
|
let mut desc = D3D12_RESOURCE_DESC {
|
|
|
|
Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,
|
|
|
|
Alignment: 0,
|
|
|
|
Width: source.size.width as u64,
|
|
|
|
Height: source.size.height,
|
|
|
|
DepthOrArraySize: 1,
|
2023-01-28 17:38:55 +11:00
|
|
|
MipLevels: if mipmap { miplevels } else { 1 },
|
2023-01-25 15:15:43 +11:00
|
|
|
Format: ImageFormat::R8G8B8A8Unorm.into(),
|
|
|
|
SampleDesc: DXGI_SAMPLE_DESC {
|
|
|
|
Count: 1,
|
|
|
|
Quality: 0,
|
|
|
|
},
|
|
|
|
Layout: Default::default(),
|
|
|
|
Flags: Default::default(),
|
|
|
|
};
|
|
|
|
|
2023-01-28 17:38:55 +11:00
|
|
|
let mut format_support = D3D12_FEATURE_DATA_FORMAT_SUPPORT {
|
2023-01-25 15:15:43 +11:00
|
|
|
Format: desc.Format,
|
|
|
|
Support1: D3D12_FORMAT_SUPPORT1_TEXTURE2D | D3D12_FORMAT_SUPPORT1_SHADER_SAMPLE,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2023-01-28 17:38:55 +11:00
|
|
|
if mipmap {
|
|
|
|
desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
|
|
|
|
format_support.Support1 |= D3D12_FORMAT_SUPPORT1_MIP;
|
|
|
|
}
|
|
|
|
|
2023-02-13 15:27:18 +11:00
|
|
|
desc.Format = d3d12_get_closest_format(device, format_support);
|
2023-01-25 15:15:43 +11:00
|
|
|
let descriptor = heap.alloc_slot()?;
|
|
|
|
|
|
|
|
// create handles on GPU
|
2024-08-25 15:24:05 +10:00
|
|
|
let resource = allocator.lock().create_resource(&ResourceCreateDesc {
|
|
|
|
name: "lut alloc",
|
|
|
|
memory_location: MemoryLocation::GpuOnly,
|
|
|
|
resource_category: ResourceCategory::OtherTexture,
|
|
|
|
resource_desc: &desc,
|
|
|
|
castable_formats: &[],
|
|
|
|
clear_value: None,
|
|
|
|
initial_state_or_layout: ResourceStateOrBarrierLayout::ResourceState(
|
2023-01-25 15:15:43 +11:00
|
|
|
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
|
2024-08-25 15:24:05 +10:00
|
|
|
),
|
|
|
|
resource_type: &ResourceType::Placed,
|
|
|
|
})?;
|
|
|
|
|
2023-01-25 15:15:43 +11:00
|
|
|
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 {
|
2023-02-01 17:25:39 +11:00
|
|
|
MipLevels: u32::MAX,
|
2023-01-25 15:15:43 +11:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-02-06 08:17:23 +11:00
|
|
|
device.CreateShaderResourceView(
|
2024-08-25 15:24:05 +10:00
|
|
|
resource.resource(),
|
2023-02-06 08:17:23 +11:00
|
|
|
Some(&srv_desc),
|
|
|
|
*descriptor.deref().as_ref(),
|
|
|
|
);
|
2023-01-25 15:15:43 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut buffer_desc = D3D12_RESOURCE_DESC {
|
|
|
|
Dimension: D3D12_RESOURCE_DIMENSION_BUFFER,
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut layout = D3D12_PLACED_SUBRESOURCE_FOOTPRINT::default();
|
|
|
|
let mut total = 0;
|
|
|
|
// texture upload
|
|
|
|
unsafe {
|
|
|
|
device.GetCopyableFootprints(
|
|
|
|
&desc,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
Some(&mut layout),
|
2023-01-27 09:57:54 +11:00
|
|
|
None,
|
|
|
|
None,
|
2023-01-25 15:15:43 +11:00
|
|
|
Some(&mut total),
|
|
|
|
);
|
|
|
|
|
|
|
|
buffer_desc.Width = total;
|
|
|
|
buffer_desc.Height = 1;
|
|
|
|
buffer_desc.DepthOrArraySize = 1;
|
|
|
|
buffer_desc.MipLevels = 1;
|
|
|
|
buffer_desc.SampleDesc.Count = 1;
|
|
|
|
buffer_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
|
|
|
}
|
|
|
|
|
2024-08-25 15:24:05 +10:00
|
|
|
let upload = allocator.lock().create_resource(&ResourceCreateDesc {
|
|
|
|
name: "lut staging",
|
|
|
|
memory_location: MemoryLocation::CpuToGpu,
|
|
|
|
resource_category: ResourceCategory::Buffer,
|
|
|
|
resource_desc: &buffer_desc,
|
|
|
|
castable_formats: &[],
|
|
|
|
clear_value: None,
|
|
|
|
initial_state_or_layout: ResourceStateOrBarrierLayout::ResourceState(
|
2023-01-25 15:15:43 +11:00
|
|
|
D3D12_RESOURCE_STATE_GENERIC_READ,
|
2024-08-25 15:24:05 +10:00
|
|
|
),
|
|
|
|
resource_type: &ResourceType::Placed,
|
|
|
|
})?;
|
|
|
|
|
2023-02-10 13:03:55 +11:00
|
|
|
let subresource = [D3D12_SUBRESOURCE_DATA {
|
|
|
|
pData: source.bytes.as_ptr().cast(),
|
|
|
|
RowPitch: 4 * source.size.width as isize,
|
|
|
|
SlicePitch: (4 * source.size.width * source.size.height) as isize,
|
|
|
|
}];
|
|
|
|
|
2024-08-26 13:54:36 +10:00
|
|
|
gc.dispose_barriers(d3d12_resource_transition(
|
2023-02-10 13:03:55 +11:00
|
|
|
cmd,
|
2024-08-25 15:24:05 +10:00
|
|
|
&resource.resource(),
|
2023-02-10 13:03:55 +11:00
|
|
|
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
|
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST,
|
2024-08-26 13:54:36 +10:00
|
|
|
));
|
2023-01-25 15:15:43 +11:00
|
|
|
|
2024-08-25 15:24:05 +10:00
|
|
|
d3d12_update_subresources(
|
|
|
|
cmd,
|
|
|
|
&resource.resource(),
|
|
|
|
&upload.resource(),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
1,
|
|
|
|
&subresource,
|
|
|
|
gc,
|
|
|
|
)?;
|
2023-01-25 15:15:43 +11:00
|
|
|
|
2024-08-26 13:54:36 +10:00
|
|
|
gc.dispose_barriers(d3d12_resource_transition(
|
2023-02-10 13:03:55 +11:00
|
|
|
cmd,
|
2024-08-25 15:24:05 +10:00
|
|
|
&resource.resource(),
|
2023-02-10 13:03:55 +11:00
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
|
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
|
2024-08-26 13:54:36 +10:00
|
|
|
));
|
2023-01-27 09:57:54 +11:00
|
|
|
|
2023-02-01 11:23:57 +11:00
|
|
|
let view = InputTexture::new(
|
2024-08-25 15:24:05 +10:00
|
|
|
resource.resource().clone(),
|
2023-01-25 15:15:43 +11:00
|
|
|
descriptor,
|
2023-02-01 11:23:57 +11:00
|
|
|
source.size,
|
2023-02-13 15:27:18 +11:00
|
|
|
ImageFormat::R8G8B8A8Unorm.into(),
|
2023-02-01 11:23:57 +11:00
|
|
|
filter,
|
2023-02-06 17:05:19 +11:00
|
|
|
wrap_mode,
|
2023-02-01 11:23:57 +11:00
|
|
|
);
|
2023-02-11 18:42:33 +11:00
|
|
|
Ok(LutTexture {
|
2024-08-25 15:24:05 +10:00
|
|
|
resource: ManuallyDrop::new(resource),
|
|
|
|
staging: ManuallyDrop::new(upload),
|
2023-02-11 18:42:33 +11:00
|
|
|
view,
|
|
|
|
miplevels: if mipmap { Some(miplevels) } else { None },
|
2024-08-25 15:24:05 +10:00
|
|
|
allocator: Arc::clone(&allocator),
|
2023-02-11 18:42:33 +11:00
|
|
|
})
|
2023-01-24 18:02:27 +11:00
|
|
|
}
|
2023-01-28 17:38:55 +11:00
|
|
|
|
|
|
|
pub fn generate_mipmaps(&self, gen_mips: &mut MipmapGenContext) -> error::Result<()> {
|
|
|
|
if let Some(miplevels) = self.miplevels {
|
2023-02-06 08:17:23 +11:00
|
|
|
gen_mips.generate_mipmaps(
|
2024-08-25 15:24:05 +10:00
|
|
|
&self.resource.resource(),
|
2023-02-06 08:17:23 +11:00
|
|
|
miplevels,
|
|
|
|
self.view.size,
|
|
|
|
ImageFormat::R8G8B8A8Unorm.into(),
|
|
|
|
)?
|
2023-01-28 17:38:55 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-02-01 11:23:57 +11:00
|
|
|
}
|
2023-02-01 09:50:47 +11:00
|
|
|
|
2023-02-01 11:23:57 +11:00
|
|
|
impl AsRef<InputTexture> for LutTexture {
|
|
|
|
fn as_ref(&self) -> &InputTexture {
|
|
|
|
&self.view
|
2023-02-01 09:50:47 +11:00
|
|
|
}
|
2023-02-06 08:17:23 +11:00
|
|
|
}
|
2024-08-25 15:24:05 +10:00
|
|
|
|
|
|
|
impl Drop for LutTexture {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let resource = unsafe { ManuallyDrop::take(&mut self.resource) };
|
|
|
|
if let Err(e) = self.allocator.lock().free_resource(resource) {
|
|
|
|
println!("librashader-runtime-d3d12: [warn] failed to deallocate lut buffer memory {e}")
|
|
|
|
}
|
|
|
|
|
|
|
|
let staging = unsafe { ManuallyDrop::take(&mut self.staging) };
|
|
|
|
if let Err(e) = self.allocator.lock().free_resource(staging) {
|
|
|
|
println!("librashader-runtime-d3d12: [warn] failed to deallocate lut staging buffer memory {e}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|