2022-12-01 11:05:12 +11:00
|
|
|
use crate::error;
|
2022-11-30 17:52:08 +11:00
|
|
|
use crate::texture::{DxImageView, Texture};
|
2022-11-29 17:57:04 +11:00
|
|
|
use crate::util::d3d11_get_closest_format;
|
2022-11-30 17:38:05 +11:00
|
|
|
use librashader_common::{ImageFormat, Size};
|
|
|
|
use librashader_presets::Scale2D;
|
2022-12-01 11:05:12 +11:00
|
|
|
use windows::core::Interface;
|
2022-11-30 17:38:05 +11:00
|
|
|
use windows::Win32::Graphics::Direct3D::D3D_SRV_DIMENSION_TEXTURE2D;
|
2022-12-10 17:50:54 +11:00
|
|
|
use windows::Win32::Graphics::Direct3D11::{ID3D11Device, ID3D11RenderTargetView, ID3D11ShaderResourceView, ID3D11Texture2D, D3D11_BIND_RENDER_TARGET, D3D11_BIND_SHADER_RESOURCE, D3D11_CPU_ACCESS_WRITE, D3D11_FORMAT_SUPPORT_RENDER_TARGET, D3D11_FORMAT_SUPPORT_SHADER_SAMPLE, D3D11_FORMAT_SUPPORT_TEXTURE2D, D3D11_RENDER_TARGET_VIEW_DESC, D3D11_RENDER_TARGET_VIEW_DESC_0, D3D11_RTV_DIMENSION_TEXTURE2D, D3D11_SHADER_RESOURCE_VIEW_DESC, D3D11_SHADER_RESOURCE_VIEW_DESC_0, D3D11_TEX2D_RTV, D3D11_TEX2D_SRV, D3D11_TEXTURE2D_DESC, D3D11_USAGE_DEFAULT, D3D11_VIEWPORT, D3D11_RESOURCE_MISC_GENERATE_MIPS, ID3D11DeviceContext, D3D11_BOX};
|
2022-11-30 17:38:05 +11:00
|
|
|
use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT, DXGI_SAMPLE_DESC};
|
2022-12-22 13:13:35 +11:00
|
|
|
use librashader_runtime::scaling::ViewportSize;
|
2022-11-29 17:57:04 +11:00
|
|
|
|
2022-11-27 18:21:36 +11:00
|
|
|
#[derive(Debug, Clone)]
|
2022-12-01 11:05:12 +11:00
|
|
|
pub(crate) struct OwnedFramebuffer {
|
2022-11-27 18:21:36 +11:00
|
|
|
pub texture: ID3D11Texture2D,
|
2022-11-29 17:57:04 +11:00
|
|
|
pub size: Size<u32>,
|
|
|
|
pub format: DXGI_FORMAT,
|
|
|
|
device: ID3D11Device,
|
2022-12-01 16:11:41 +11:00
|
|
|
context: ID3D11DeviceContext,
|
2022-11-30 17:38:05 +11:00
|
|
|
is_raw: bool,
|
2022-11-27 18:21:36 +11:00
|
|
|
}
|
|
|
|
|
2022-11-29 17:57:04 +11:00
|
|
|
impl OwnedFramebuffer {
|
2022-11-30 17:38:05 +11:00
|
|
|
pub fn new(
|
|
|
|
device: &ID3D11Device,
|
2022-12-01 16:11:41 +11:00
|
|
|
context: &ID3D11DeviceContext,
|
2022-11-30 17:38:05 +11:00
|
|
|
size: Size<u32>,
|
|
|
|
format: ImageFormat,
|
2022-12-01 11:05:12 +11:00
|
|
|
) -> error::Result<OwnedFramebuffer> {
|
2022-11-29 17:57:04 +11:00
|
|
|
unsafe {
|
2022-11-30 17:38:05 +11:00
|
|
|
let format = d3d11_get_closest_format(
|
|
|
|
device,
|
|
|
|
DXGI_FORMAT::from(format),
|
|
|
|
D3D11_FORMAT_SUPPORT_TEXTURE2D.0
|
|
|
|
| D3D11_FORMAT_SUPPORT_SHADER_SAMPLE.0
|
|
|
|
| D3D11_FORMAT_SUPPORT_RENDER_TARGET.0,
|
|
|
|
);
|
2022-12-01 16:11:41 +11:00
|
|
|
let desc = default_desc(size, format, 1);
|
2022-11-29 17:57:04 +11:00
|
|
|
let texture = device.CreateTexture2D(&desc, None)?;
|
|
|
|
|
|
|
|
Ok(OwnedFramebuffer {
|
|
|
|
texture,
|
|
|
|
size,
|
|
|
|
format,
|
|
|
|
device: device.clone(),
|
2022-12-01 16:11:41 +11:00
|
|
|
context: context.clone(),
|
2022-11-29 17:57:04 +11:00
|
|
|
is_raw: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 14:50:57 +11:00
|
|
|
|
2022-11-29 17:57:04 +11:00
|
|
|
pub(crate) fn scale(
|
|
|
|
&mut self,
|
|
|
|
scaling: Scale2D,
|
|
|
|
format: ImageFormat,
|
|
|
|
viewport_size: &Size<u32>,
|
|
|
|
_original: &Texture,
|
2022-12-01 14:50:57 +11:00
|
|
|
source: &Texture
|
2022-12-01 11:05:12 +11:00
|
|
|
) -> error::Result<Size<u32>> {
|
2022-11-29 17:57:04 +11:00
|
|
|
if self.is_raw {
|
|
|
|
return Ok(self.size);
|
|
|
|
}
|
|
|
|
|
2022-12-22 13:13:35 +11:00
|
|
|
let size = source.view.size.scale_viewport(scaling, *viewport_size);
|
2022-11-29 17:57:04 +11:00
|
|
|
|
|
|
|
if self.size != size {
|
|
|
|
self.size = size;
|
2022-11-27 18:21:36 +11:00
|
|
|
|
2022-11-29 17:57:04 +11:00
|
|
|
self.init(
|
|
|
|
size,
|
|
|
|
if format == ImageFormat::Unknown {
|
|
|
|
ImageFormat::R8G8B8A8Unorm
|
|
|
|
} else {
|
|
|
|
format
|
|
|
|
},
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
Ok(size)
|
|
|
|
}
|
|
|
|
|
2022-12-01 11:05:12 +11:00
|
|
|
pub fn init(&mut self, size: Size<u32>, format: ImageFormat) -> error::Result<()> {
|
2022-11-29 17:57:04 +11:00
|
|
|
if self.is_raw {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2022-11-30 17:38:05 +11:00
|
|
|
let format = d3d11_get_closest_format(
|
|
|
|
&self.device,
|
|
|
|
DXGI_FORMAT::from(format),
|
|
|
|
D3D11_FORMAT_SUPPORT_TEXTURE2D.0
|
|
|
|
| D3D11_FORMAT_SUPPORT_SHADER_SAMPLE.0
|
|
|
|
| D3D11_FORMAT_SUPPORT_RENDER_TARGET.0,
|
|
|
|
);
|
2022-11-30 11:36:42 +11:00
|
|
|
|
2022-12-01 16:11:41 +11:00
|
|
|
let desc = default_desc(size, format, 1);
|
2022-11-29 17:57:04 +11:00
|
|
|
unsafe {
|
|
|
|
let mut texture = self.device.CreateTexture2D(&desc, None)?;
|
|
|
|
std::mem::swap(&mut self.texture, &mut texture);
|
|
|
|
drop(texture)
|
|
|
|
}
|
|
|
|
self.format = format;
|
2022-11-30 17:52:08 +11:00
|
|
|
self.size = size;
|
2022-11-29 17:57:04 +11:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-12-01 11:05:12 +11:00
|
|
|
pub fn create_shader_resource_view(&self) -> error::Result<ID3D11ShaderResourceView> {
|
2022-11-29 17:57:04 +11:00
|
|
|
unsafe {
|
2022-11-30 17:38:05 +11:00
|
|
|
Ok(self.device.CreateShaderResourceView(
|
|
|
|
&self.texture,
|
|
|
|
Some(&D3D11_SHADER_RESOURCE_VIEW_DESC {
|
|
|
|
Format: self.format,
|
|
|
|
ViewDimension: D3D_SRV_DIMENSION_TEXTURE2D,
|
|
|
|
Anonymous: D3D11_SHADER_RESOURCE_VIEW_DESC_0 {
|
|
|
|
Texture2D: D3D11_TEX2D_SRV {
|
|
|
|
MostDetailedMip: 0,
|
|
|
|
MipLevels: u32::MAX,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
)?)
|
2022-11-29 17:57:04 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 11:05:12 +11:00
|
|
|
pub fn create_render_target_view(&self) -> error::Result<ID3D11RenderTargetView> {
|
2022-11-29 17:57:04 +11:00
|
|
|
unsafe {
|
2022-11-30 17:38:05 +11:00
|
|
|
Ok(self.device.CreateRenderTargetView(
|
|
|
|
&self.texture,
|
|
|
|
Some(&D3D11_RENDER_TARGET_VIEW_DESC {
|
|
|
|
Format: self.format,
|
|
|
|
ViewDimension: D3D11_RTV_DIMENSION_TEXTURE2D,
|
|
|
|
Anonymous: D3D11_RENDER_TARGET_VIEW_DESC_0 {
|
|
|
|
Texture2D: D3D11_TEX2D_RTV { MipSlice: 0 },
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
)?)
|
2022-11-29 17:57:04 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 11:05:12 +11:00
|
|
|
pub fn as_output_framebuffer(&self) -> error::Result<OutputFramebuffer> {
|
2022-11-29 17:57:04 +11:00
|
|
|
Ok(OutputFramebuffer {
|
|
|
|
rtv: self.create_render_target_view()?,
|
|
|
|
size: self.size,
|
2022-11-30 17:38:05 +11:00
|
|
|
viewport: default_viewport(self.size),
|
2022-11-29 17:57:04 +11:00
|
|
|
})
|
|
|
|
}
|
2022-11-30 17:35:20 +11:00
|
|
|
|
2022-12-01 11:05:12 +11:00
|
|
|
pub fn copy_from(&mut self, image: &DxImageView) -> error::Result<()> {
|
2022-12-10 17:50:54 +11:00
|
|
|
let original_resource: ID3D11Texture2D = unsafe {
|
2022-11-30 17:52:08 +11:00
|
|
|
let mut resource = None;
|
|
|
|
image.handle.GetResource(&mut resource);
|
2022-12-01 09:59:55 +11:00
|
|
|
let Some(resource) = resource else {
|
|
|
|
return Ok(())
|
|
|
|
};
|
|
|
|
resource.cast()?
|
2022-11-30 17:52:08 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
let format = unsafe {
|
|
|
|
let mut desc = Default::default();
|
2022-12-10 17:50:54 +11:00
|
|
|
original_resource.GetDesc(&mut desc);
|
2022-11-30 17:52:08 +11:00
|
|
|
desc.Format
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.size != image.size || format != self.format {
|
|
|
|
eprintln!("[history] resizing");
|
|
|
|
self.init(image.size, ImageFormat::from(format))?;
|
2022-11-30 17:35:20 +11:00
|
|
|
}
|
2022-11-30 17:52:08 +11:00
|
|
|
|
2022-12-01 16:24:24 +11:00
|
|
|
// todo: improve mipmap generation?
|
|
|
|
// will need a staging texture + full so might not be worth it.
|
|
|
|
unsafe {
|
|
|
|
self.context.CopySubresourceRegion(
|
|
|
|
&self.texture,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2022-12-10 17:50:54 +11:00
|
|
|
&original_resource,
|
2022-12-01 16:24:24 +11:00
|
|
|
0,
|
|
|
|
Some(&D3D11_BOX {
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
front: 0,
|
|
|
|
right: self.size.width,
|
|
|
|
bottom: self.size.height,
|
|
|
|
back: 1,
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-10 17:50:54 +11:00
|
|
|
let srvs = self.create_shader_resource_view()?;
|
|
|
|
unsafe {
|
|
|
|
self.context.GenerateMips(&srvs);
|
|
|
|
}
|
2022-11-30 17:35:20 +11:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-11-29 17:57:04 +11:00
|
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
2022-12-01 11:05:12 +11:00
|
|
|
pub(crate) struct OutputFramebuffer {
|
2022-11-27 18:21:36 +11:00
|
|
|
pub rtv: ID3D11RenderTargetView,
|
|
|
|
pub size: Size<u32>,
|
2022-11-30 17:38:05 +11:00
|
|
|
pub viewport: D3D11_VIEWPORT,
|
2022-11-29 17:57:04 +11:00
|
|
|
}
|
|
|
|
|
2022-12-01 14:50:57 +11:00
|
|
|
fn default_desc(size: Size<u32>, format: DXGI_FORMAT, mip_levels: u32) -> D3D11_TEXTURE2D_DESC {
|
2022-11-29 17:57:04 +11:00
|
|
|
D3D11_TEXTURE2D_DESC {
|
|
|
|
Width: size.width,
|
|
|
|
Height: size.height,
|
2022-12-01 14:50:57 +11:00
|
|
|
MipLevels: mip_levels,
|
2022-11-29 17:57:04 +11:00
|
|
|
ArraySize: 1,
|
|
|
|
Format: format,
|
|
|
|
SampleDesc: DXGI_SAMPLE_DESC {
|
|
|
|
Count: 1,
|
|
|
|
Quality: 0,
|
|
|
|
},
|
|
|
|
Usage: D3D11_USAGE_DEFAULT,
|
|
|
|
BindFlags: D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET,
|
|
|
|
CPUAccessFlags: D3D11_CPU_ACCESS_WRITE,
|
2022-12-01 16:11:41 +11:00
|
|
|
MiscFlags: D3D11_RESOURCE_MISC_GENERATE_MIPS
|
2022-11-29 17:57:04 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub const fn default_viewport(size: Size<u32>) -> D3D11_VIEWPORT {
|
|
|
|
D3D11_VIEWPORT {
|
|
|
|
TopLeftX: 0.0,
|
|
|
|
TopLeftY: 0.0,
|
|
|
|
Width: size.width as f32,
|
|
|
|
Height: size.height as f32,
|
|
|
|
MinDepth: 0.0,
|
|
|
|
MaxDepth: 1.0,
|
|
|
|
}
|
2022-11-30 17:38:05 +11:00
|
|
|
}
|