rt(d3d11): get rid of input/output view wrappers
This commit is contained in:
parent
35f499f5e1
commit
4d6793d305
11 changed files with 92 additions and 105 deletions
|
@ -4,11 +4,12 @@ use crate::ctypes::{
|
|||
use crate::error::{assert_non_null, assert_some_ptr, LibrashaderError};
|
||||
use crate::ffi::extern_fn;
|
||||
use librashader::runtime::d3d11::{
|
||||
D3D11InputView, D3D11OutputView, FilterChain, FilterChainOptions, FrameOptions,
|
||||
FilterChain, FilterChainOptions, FrameOptions,
|
||||
};
|
||||
use std::ffi::c_char;
|
||||
use std::ffi::CStr;
|
||||
use std::mem::{ManuallyDrop, MaybeUninit};
|
||||
use std::ops::Deref;
|
||||
use std::ptr::NonNull;
|
||||
use std::slice;
|
||||
use windows::Win32::Graphics::Direct3D11::{
|
||||
|
@ -23,25 +24,12 @@ use librashader::runtime::{FilterChainParameters, Size, Viewport};
|
|||
pub struct libra_source_image_d3d11_t {
|
||||
/// A shader resource view into the source image
|
||||
pub handle: ManuallyDrop<ID3D11ShaderResourceView>,
|
||||
/// The width of the source image.
|
||||
/// This is currently ignored.
|
||||
pub width: u32,
|
||||
/// The height of the source image.
|
||||
/// This is currently ignored.
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
impl TryFrom<libra_source_image_d3d11_t> for D3D11InputView {
|
||||
type Error = LibrashaderError;
|
||||
|
||||
fn try_from(value: libra_source_image_d3d11_t) -> Result<Self, Self::Error> {
|
||||
let handle = value.handle.clone();
|
||||
|
||||
Ok(D3D11InputView {
|
||||
handle: ManuallyDrop::into_inner(handle),
|
||||
size: Size::new(value.width, value.height),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Options for Direct3D 11 filter chain creation.
|
||||
#[repr(C)]
|
||||
#[derive(Default, Debug, Clone)]
|
||||
|
@ -251,19 +239,14 @@ extern_fn! {
|
|||
let viewport = Viewport {
|
||||
x: viewport.x,
|
||||
y: viewport.y,
|
||||
output: D3D11OutputView {
|
||||
size: Size::new(viewport.width, viewport.height),
|
||||
handle: ManuallyDrop::into_inner(out.clone()),
|
||||
},
|
||||
output: ManuallyDrop::into_inner(out.clone()),
|
||||
mvp,
|
||||
};
|
||||
|
||||
let options = options.map(FromUninit::from_uninit);
|
||||
|
||||
let image = image.try_into()?;
|
||||
|
||||
unsafe {
|
||||
chain.frame(device_context.as_deref(), image, &viewport, frame_count, options.as_ref())?;
|
||||
chain.frame(device_context.as_deref(), image.handle.deref(), &viewport, frame_count, options.as_ref())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::texture::{D3D11InputView, InputTexture};
|
||||
use crate::texture::InputTexture;
|
||||
use librashader_common::{ImageFormat, Size, Viewport};
|
||||
|
||||
use librashader_common::map::FastHashMap;
|
||||
|
@ -21,8 +21,8 @@ use crate::graphics_pipeline::D3D11State;
|
|||
use crate::luts::LutTexture;
|
||||
use crate::options::{FilterChainOptionsD3D11, FrameOptionsD3D11};
|
||||
use crate::samplers::SamplerSet;
|
||||
use crate::util::d3d11_compile_bound_shader;
|
||||
use crate::{error, util, D3D11OutputView};
|
||||
use crate::util::{d3d11_compile_bound_shader, GetSize};
|
||||
use crate::{error, util};
|
||||
use librashader_cache::cache_shader_object;
|
||||
use librashader_cache::CachedCompilation;
|
||||
use librashader_presets::context::VideoDriver;
|
||||
|
@ -36,7 +36,8 @@ use librashader_runtime::scaling::ScaleFramebuffer;
|
|||
use librashader_runtime::uniforms::UniformStorage;
|
||||
use rayon::prelude::*;
|
||||
use windows::Win32::Graphics::Direct3D11::{
|
||||
ID3D11Buffer, ID3D11Device, ID3D11DeviceContext, D3D11_BIND_CONSTANT_BUFFER, D3D11_BUFFER_DESC,
|
||||
ID3D11Buffer, ID3D11Device, ID3D11DeviceContext, ID3D11RenderTargetView,
|
||||
ID3D11ShaderResourceView, D3D11_BIND_CONSTANT_BUFFER, D3D11_BUFFER_DESC,
|
||||
D3D11_CPU_ACCESS_WRITE, D3D11_CREATE_DEVICE_SINGLETHREADED, D3D11_RESOURCE_MISC_GENERATE_MIPS,
|
||||
D3D11_TEXTURE2D_DESC, D3D11_USAGE_DEFAULT, D3D11_USAGE_DYNAMIC,
|
||||
};
|
||||
|
@ -347,7 +348,7 @@ impl FilterChainD3D11 {
|
|||
fn push_history(
|
||||
&mut self,
|
||||
ctx: &ID3D11DeviceContext,
|
||||
input: &D3D11InputView,
|
||||
input: &ID3D11ShaderResourceView,
|
||||
) -> error::Result<()> {
|
||||
if let Some(mut back) = self.history_framebuffers.pop_back() {
|
||||
back.copy_from(ctx, input)?;
|
||||
|
@ -399,8 +400,8 @@ impl FilterChainD3D11 {
|
|||
pub unsafe fn frame(
|
||||
&mut self,
|
||||
ctx: Option<&ID3D11DeviceContext>,
|
||||
input: D3D11InputView,
|
||||
viewport: &Viewport<D3D11OutputView>,
|
||||
input: &ID3D11ShaderResourceView,
|
||||
viewport: &Viewport<ID3D11RenderTargetView>,
|
||||
frame_count: usize,
|
||||
options: Option<&FrameOptionsD3D11>,
|
||||
) -> error::Result<()> {
|
||||
|
@ -447,8 +448,8 @@ impl FilterChainD3D11 {
|
|||
// rescale render buffers to ensure all bindings are valid.
|
||||
OwnedImage::scale_framebuffers(
|
||||
source.size(),
|
||||
viewport.output.size,
|
||||
original.view.size,
|
||||
viewport.output.size()?,
|
||||
original.view.size()?,
|
||||
&mut self.output_framebuffers,
|
||||
&mut self.feedback_framebuffers,
|
||||
passes,
|
||||
|
@ -481,7 +482,6 @@ impl FilterChainD3D11 {
|
|||
source.filter = pass.config.filter;
|
||||
source.wrap_mode = pass.config.wrap_mode;
|
||||
let target = &self.output_framebuffers[index];
|
||||
let size = target.size;
|
||||
pass.draw(
|
||||
ctx,
|
||||
index,
|
||||
|
@ -491,15 +491,12 @@ impl FilterChainD3D11 {
|
|||
viewport,
|
||||
&original,
|
||||
&source,
|
||||
RenderTarget::identity(&target.as_output()?),
|
||||
RenderTarget::identity(&target.create_render_target_view()?),
|
||||
QuadType::Offscreen,
|
||||
)?;
|
||||
|
||||
source = InputTexture {
|
||||
view: D3D11InputView {
|
||||
handle: target.create_shader_resource_view()?,
|
||||
size,
|
||||
},
|
||||
view: target.create_shader_resource_view()?,
|
||||
filter: pass.config.filter,
|
||||
wrap_mode: pass.config.wrap_mode,
|
||||
};
|
||||
|
|
|
@ -16,13 +16,14 @@ use librashader_runtime::filter_pass::FilterPassMeta;
|
|||
use librashader_runtime::quad::QuadType;
|
||||
use librashader_runtime::render_target::RenderTarget;
|
||||
use windows::Win32::Graphics::Direct3D11::{
|
||||
ID3D11Buffer, ID3D11DeviceContext, ID3D11InputLayout, ID3D11PixelShader, ID3D11SamplerState,
|
||||
ID3D11ShaderResourceView, ID3D11VertexShader, D3D11_MAPPED_SUBRESOURCE,
|
||||
D3D11_MAP_WRITE_DISCARD, D3D11_VIEWPORT,
|
||||
ID3D11Buffer, ID3D11DeviceContext, ID3D11InputLayout, ID3D11PixelShader,
|
||||
ID3D11RenderTargetView, ID3D11SamplerState, ID3D11ShaderResourceView, ID3D11VertexShader,
|
||||
D3D11_MAPPED_SUBRESOURCE, D3D11_MAP_WRITE_DISCARD, D3D11_VIEWPORT,
|
||||
};
|
||||
|
||||
use crate::error;
|
||||
use crate::samplers::SamplerSet;
|
||||
use crate::{error, D3D11OutputView};
|
||||
use crate::util::GetSize;
|
||||
use librashader_runtime::uniforms::{UniformStorage, UniformStorageAccess};
|
||||
|
||||
pub struct ConstantBufferBinding {
|
||||
|
@ -55,7 +56,7 @@ const NULL_TEXTURES: &[Option<ID3D11ShaderResourceView>; 16] = &[
|
|||
|
||||
impl TextureInput for InputTexture {
|
||||
fn size(&self) -> Size<u32> {
|
||||
self.view.size
|
||||
self.view.size().unwrap_or(Size::default())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,7 +78,7 @@ impl BindSemantics for FilterPass {
|
|||
_device: &Self::DeviceContext,
|
||||
) {
|
||||
let (texture_binding, sampler_binding) = descriptors;
|
||||
texture_binding[binding.binding as usize] = Some(texture.view.handle.clone());
|
||||
texture_binding[binding.binding as usize] = Some(texture.view.clone());
|
||||
sampler_binding[binding.binding as usize] =
|
||||
Some(samplers.get(texture.wrap_mode, texture.filter).clone());
|
||||
}
|
||||
|
@ -149,15 +150,15 @@ impl FilterPass {
|
|||
parent: &FilterCommon,
|
||||
frame_count: u32,
|
||||
options: &FrameOptionsD3D11,
|
||||
viewport: &Viewport<D3D11OutputView>,
|
||||
viewport: &Viewport<ID3D11RenderTargetView>,
|
||||
original: &InputTexture,
|
||||
source: &InputTexture,
|
||||
output: RenderTarget<D3D11OutputView>,
|
||||
output: RenderTarget<ID3D11RenderTargetView>,
|
||||
vbo_type: QuadType,
|
||||
) -> error::Result<()> {
|
||||
if self.config.mipmap_input && !parent.disable_mipmaps {
|
||||
unsafe {
|
||||
ctx.GenerateMips(&source.view.handle);
|
||||
ctx.GenerateMips(&source.view);
|
||||
}
|
||||
}
|
||||
unsafe {
|
||||
|
@ -170,14 +171,17 @@ impl FilterPass {
|
|||
let mut samplers: [Option<ID3D11SamplerState>; 16] = std::array::from_fn(|_| None);
|
||||
let descriptors = (&mut textures, &mut samplers);
|
||||
|
||||
let output_size = output.output.size()?;
|
||||
let viewport_size = viewport.output.size()?;
|
||||
|
||||
self.build_semantics(
|
||||
pass_index,
|
||||
parent,
|
||||
output.mvp,
|
||||
frame_count,
|
||||
options,
|
||||
output.output.size,
|
||||
viewport.output.size,
|
||||
output_size,
|
||||
viewport_size,
|
||||
descriptors,
|
||||
original,
|
||||
source,
|
||||
|
@ -241,15 +245,16 @@ impl FilterPass {
|
|||
std::mem::size_of::<Option<windows::core::IUnknown>>()
|
||||
== std::mem::size_of::<windows::core::IUnknown>()
|
||||
);
|
||||
|
||||
ctx.PSSetShaderResources(0, Some(std::mem::transmute(textures.as_ref())));
|
||||
ctx.PSSetSamplers(0, Some(std::mem::transmute(samplers.as_ref())));
|
||||
|
||||
ctx.OMSetRenderTargets(Some(&[Some(output.output.handle.clone())]), None);
|
||||
ctx.OMSetRenderTargets(Some(&[Some(output.output.clone())]), None);
|
||||
ctx.RSSetViewports(Some(&[D3D11_VIEWPORT {
|
||||
TopLeftX: output.x,
|
||||
TopLeftY: output.y,
|
||||
Width: output.output.size.width as f32,
|
||||
Height: output.output.size.height as f32,
|
||||
Width: output_size.width as f32,
|
||||
Height: output_size.height as f32,
|
||||
MinDepth: 0.0,
|
||||
MaxDepth: 1.0,
|
||||
}]))
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use crate::error;
|
||||
use crate::error::{assume_d3d11_init, FilterChainError};
|
||||
use crate::texture::D3D11InputView;
|
||||
use crate::util::d3d11_get_closest_format;
|
||||
use crate::{error, D3D11OutputView};
|
||||
use librashader_common::{ImageFormat, Size};
|
||||
use librashader_presets::Scale2D;
|
||||
use librashader_runtime::scaling::{MipmapSize, ScaleFramebuffer, ViewportSize};
|
||||
|
@ -171,33 +170,25 @@ impl OwnedImage {
|
|||
Ok(rtv)
|
||||
}
|
||||
|
||||
pub fn as_output(&self) -> error::Result<D3D11OutputView> {
|
||||
let rtv = self.create_render_target_view()?;
|
||||
Ok(D3D11OutputView {
|
||||
handle: rtv,
|
||||
size: self.size,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn copy_from(
|
||||
&mut self,
|
||||
ctx: &ID3D11DeviceContext,
|
||||
image: &D3D11InputView,
|
||||
image: &ID3D11ShaderResourceView,
|
||||
) -> error::Result<()> {
|
||||
let original_resource: ID3D11Texture2D = unsafe {
|
||||
let resource = image.handle.GetResource()?;
|
||||
let resource = image.GetResource()?;
|
||||
resource.cast()?
|
||||
};
|
||||
|
||||
let format = unsafe {
|
||||
let (format, image_size) = unsafe {
|
||||
let mut desc = Default::default();
|
||||
original_resource.GetDesc(&mut desc);
|
||||
desc.Format
|
||||
(desc.Format, Size::new(desc.Width, desc.Height))
|
||||
};
|
||||
|
||||
if self.size != image.size || format != self.format {
|
||||
if self.size != image_size || format != self.format {
|
||||
// eprintln!("[history] resizing");
|
||||
self.init(image.size, ImageFormat::from(format))?;
|
||||
self.init(image_size, ImageFormat::from(format))?;
|
||||
}
|
||||
|
||||
unsafe {
|
||||
|
|
|
@ -25,5 +25,3 @@ use librashader_runtime::impl_filter_chain_parameters;
|
|||
impl_filter_chain_parameters!(FilterChainD3D11);
|
||||
|
||||
pub use filter_chain::FilterChainD3D11;
|
||||
pub use texture::D3D11InputView;
|
||||
pub use texture::D3D11OutputView;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::error;
|
||||
use crate::error::assume_d3d11_init;
|
||||
use crate::texture::InputTexture;
|
||||
use crate::{error, D3D11InputView};
|
||||
use librashader_common::{FilterMode, WrapMode};
|
||||
use librashader_runtime::image::Image;
|
||||
use librashader_runtime::scaling::MipmapSize;
|
||||
|
@ -139,10 +139,7 @@ impl LutTexture {
|
|||
handle,
|
||||
desc,
|
||||
image: InputTexture {
|
||||
view: D3D11InputView {
|
||||
handle: srv,
|
||||
size: source.size,
|
||||
},
|
||||
view: srv,
|
||||
filter,
|
||||
wrap_mode,
|
||||
},
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::error::Result;
|
||||
use crate::framebuffer::OwnedImage;
|
||||
use librashader_common::{FilterMode, Size, WrapMode};
|
||||
use librashader_common::{FilterMode, WrapMode};
|
||||
use windows::Win32::Graphics::Direct3D11::{ID3D11RenderTargetView, ID3D11ShaderResourceView};
|
||||
|
||||
/// An image view for use as a shader resource.
|
||||
|
@ -10,8 +10,6 @@ use windows::Win32::Graphics::Direct3D11::{ID3D11RenderTargetView, ID3D11ShaderR
|
|||
pub struct D3D11InputView {
|
||||
/// A handle to the shader resource view.
|
||||
pub handle: ID3D11ShaderResourceView,
|
||||
/// The size of the image.
|
||||
pub size: Size<u32>,
|
||||
}
|
||||
|
||||
/// An image view for use as a render target.
|
||||
|
@ -21,13 +19,11 @@ pub struct D3D11InputView {
|
|||
pub struct D3D11OutputView {
|
||||
/// A handle to the render target view.
|
||||
pub handle: ID3D11RenderTargetView,
|
||||
/// The size of the image.
|
||||
pub size: Size<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct InputTexture {
|
||||
pub view: D3D11InputView,
|
||||
pub view: ID3D11ShaderResourceView,
|
||||
pub filter: FilterMode,
|
||||
pub wrap_mode: WrapMode,
|
||||
}
|
||||
|
@ -39,10 +35,7 @@ impl InputTexture {
|
|||
filter: FilterMode,
|
||||
) -> Result<Self> {
|
||||
Ok(InputTexture {
|
||||
view: D3D11InputView {
|
||||
handle: fbo.create_shader_resource_view()?,
|
||||
size: fbo.size,
|
||||
},
|
||||
view: fbo.create_shader_resource_view()?,
|
||||
filter,
|
||||
wrap_mode,
|
||||
})
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
use crate::error;
|
||||
use crate::error::assume_d3d11_init;
|
||||
use librashader_common::Size;
|
||||
use std::slice;
|
||||
use windows::core::PCSTR;
|
||||
use windows::core::{ComInterface, PCSTR};
|
||||
use windows::Win32::Graphics::Direct3D::Fxc::{
|
||||
D3DCompile, D3DCOMPILE_DEBUG, D3DCOMPILE_OPTIMIZATION_LEVEL3, D3DCOMPILE_SKIP_OPTIMIZATION,
|
||||
};
|
||||
|
@ -175,3 +176,39 @@ pub fn d3d11_create_input_layout(
|
|||
Ok(input_layout)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) trait GetSize {
|
||||
fn size(&self) -> error::Result<Size<u32>>;
|
||||
}
|
||||
|
||||
impl GetSize for ID3D11RenderTargetView {
|
||||
fn size(&self) -> error::Result<Size<u32>> {
|
||||
let parent = unsafe { self.GetResource()?.cast::<ID3D11Texture2D>()? };
|
||||
|
||||
let mut desc = Default::default();
|
||||
unsafe {
|
||||
parent.GetDesc(&mut desc);
|
||||
}
|
||||
|
||||
Ok(Size {
|
||||
height: desc.Height,
|
||||
width: desc.Width,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl GetSize for ID3D11ShaderResourceView {
|
||||
fn size(&self) -> error::Result<Size<u32>> {
|
||||
let parent = unsafe { self.GetResource()?.cast::<ID3D11Texture2D>()? };
|
||||
|
||||
let mut desc = Default::default();
|
||||
unsafe {
|
||||
parent.GetDesc(&mut desc);
|
||||
}
|
||||
|
||||
Ok(Size {
|
||||
height: desc.Height,
|
||||
width: desc.Width,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -246,7 +246,6 @@ pub mod d3d11_hello_triangle {
|
|||
use librashader_runtime::image::Image;
|
||||
use librashader_runtime_d3d11::options::FilterChainOptionsD3D11;
|
||||
use librashader_runtime_d3d11::FilterChainD3D11;
|
||||
use librashader_runtime_d3d11::{D3D11InputView, D3D11OutputView};
|
||||
use std::slice;
|
||||
use std::time::Instant;
|
||||
use texture::ExampleTexture;
|
||||
|
@ -572,23 +571,11 @@ pub mod d3d11_hello_triangle {
|
|||
self.filter
|
||||
.frame(
|
||||
None,
|
||||
D3D11InputView {
|
||||
handle: srv,
|
||||
size: Size {
|
||||
width: tex2d_desc.Width,
|
||||
height: tex2d_desc.Height,
|
||||
},
|
||||
},
|
||||
&srv,
|
||||
&Viewport {
|
||||
x: 0f32,
|
||||
y: 0f32,
|
||||
output: D3D11OutputView {
|
||||
size: Size {
|
||||
width: backbuffer_desc.Width,
|
||||
height: backbuffer_desc.Height,
|
||||
},
|
||||
handle: resources.backbuffer_rtv.as_ref().unwrap().clone(),
|
||||
},
|
||||
output: resources.backbuffer_rtv.as_ref().unwrap().clone(),
|
||||
mvp: None,
|
||||
},
|
||||
resources.frame_count,
|
||||
|
|
|
@ -12,8 +12,7 @@ use librashader_runtime_d3d11::options::FilterChainOptionsD3D11;
|
|||
// const FILTER_PATH: &str =
|
||||
// "../test/Mega_Bezel_Packs/Duimon-Mega-Bezel/Presets/Advanced/Nintendo_GBA_SP/GBA_SP-[ADV]-[LCD-GRID].slangp";
|
||||
|
||||
const FILTER_PATH: &str =
|
||||
"../test/shaders_slang/bezel/Mega_Bezel/Presets/MBZ__0__SMOOTH-ADV.slangp";
|
||||
const FILTER_PATH: &str = "../test/shaders_slang/crt/crt-royale.slangp";
|
||||
|
||||
// const FILTER_PATH: &str = "../test/slang-shaders/test/history.slangp";
|
||||
// const FILTER_PATH: &str = "../test/slang-shaders/test/feedback.slangp";
|
||||
|
@ -61,7 +60,7 @@ fn triangle_d3d11() {
|
|||
FILTER_PATH,
|
||||
Some(&FilterChainOptionsD3D11 {
|
||||
force_no_mipmaps: false,
|
||||
disable_cache: false,
|
||||
disable_cache: true,
|
||||
}),
|
||||
// replace below with 'None' for the triangle
|
||||
// None,
|
||||
|
|
|
@ -259,7 +259,7 @@ pub mod runtime {
|
|||
options::{
|
||||
FilterChainOptionsD3D11 as FilterChainOptions, FrameOptionsD3D11 as FrameOptions,
|
||||
},
|
||||
D3D11InputView, D3D11OutputView, FilterChainD3D11 as FilterChain,
|
||||
FilterChainD3D11 as FilterChain,
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue