From 4c4a918b9f97184010155c4149655eb91fcf7efa Mon Sep 17 00:00:00 2001 From: chyyran Date: Sat, 26 Nov 2022 15:55:14 -0500 Subject: [PATCH] d3d11: gen samplers --- Cargo.lock | 1 + librashader-common/Cargo.toml | 11 +++-- librashader-common/src/d3d11.rs | 61 ++++++++++++++++++++++++ librashader-common/src/dx.rs | 39 --------------- librashader-common/src/lib.rs | 10 ++-- librashader-runtime-dx11/Cargo.toml | 2 +- librashader-runtime-dx11/src/lib.rs | 1 + librashader-runtime-dx11/src/samplers.rs | 52 ++++++++++++++++++++ librashader-runtime-dx11/src/texture.rs | 3 +- 9 files changed, 130 insertions(+), 50 deletions(-) create mode 100644 librashader-common/src/d3d11.rs delete mode 100644 librashader-common/src/dx.rs create mode 100644 librashader-runtime-dx11/src/samplers.rs diff --git a/Cargo.lock b/Cargo.lock index 1f188e4..18a5763 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -427,6 +427,7 @@ dependencies = [ "gl", "image", "num-traits", + "windows", ] [[package]] diff --git a/librashader-common/Cargo.toml b/librashader-common/Cargo.toml index eed6ec1..2f70cfb 100644 --- a/librashader-common/Cargo.toml +++ b/librashader-common/Cargo.toml @@ -8,12 +8,15 @@ edition = "2021" [features] default = [] opengl = ["gl"] +d3d11 = ["windows"] [dependencies] gl = { version = "0.14.0", optional = true } image = "0.24.5" num-traits = "0.2.15" -# -#[dependencies.windows] -#optional = true -#version = "0.43.0" + +[dependencies.windows] +optional = true +version = "0.43.0" +features = [ +] \ No newline at end of file diff --git a/librashader-common/src/d3d11.rs b/librashader-common/src/d3d11.rs new file mode 100644 index 0000000..f29741c --- /dev/null +++ b/librashader-common/src/d3d11.rs @@ -0,0 +1,61 @@ +use crate::{FilterMode, ShaderFormat, WrapMode}; +use windows::Win32::Graphics::Direct3D11; +use windows::Win32::Graphics::Dxgi::Common as dxgi; + +impl From for dxgi::DXGI_FORMAT { + fn from(format: ShaderFormat) -> Self { + match format { + ShaderFormat::Unknown => dxgi::DXGI_FORMAT_UNKNOWN, + ShaderFormat::R8Unorm => dxgi::DXGI_FORMAT_R8_UNORM, + ShaderFormat::R8Uint => dxgi::DXGI_FORMAT_R8_UINT, + ShaderFormat::R8Sint => dxgi::DXGI_FORMAT_R8_SINT, + ShaderFormat::R8G8Unorm => dxgi::DXGI_FORMAT_R8G8_UNORM, + ShaderFormat::R8G8Uint => dxgi::DXGI_FORMAT_R8G8_UINT, + ShaderFormat::R8G8Sint => dxgi::DXGI_FORMAT_R8G8_SINT, + ShaderFormat::R8G8B8A8Unorm => dxgi::DXGI_FORMAT_R8G8B8A8_UNORM, + ShaderFormat::R8G8B8A8Uint => dxgi::DXGI_FORMAT_R8G8B8A8_UINT, + ShaderFormat::R8G8B8A8Sint => dxgi::DXGI_FORMAT_R8G8B8A8_SINT, + ShaderFormat::R8G8B8A8Srgb => dxgi::DXGI_FORMAT_R8G8B8A8_UNORM_SRGB, + ShaderFormat::A2B10G10R10UnormPack32 => dxgi::DXGI_FORMAT_R10G10B10A2_UNORM, + ShaderFormat::A2B10G10R10UintPack32 => dxgi::DXGI_FORMAT_R10G10B10A2_UINT, + ShaderFormat::R16Uint => dxgi::DXGI_FORMAT_R16_UINT, + ShaderFormat::R16Sint => dxgi::DXGI_FORMAT_R16_SINT, + ShaderFormat::R16Sfloat => dxgi::DXGI_FORMAT_R16_FLOAT, + ShaderFormat::R16G16Uint => dxgi::DXGI_FORMAT_R16G16_UINT, + ShaderFormat::R16G16Sint => dxgi::DXGI_FORMAT_R16G16_SINT, + ShaderFormat::R16G16Sfloat => dxgi::DXGI_FORMAT_R16G16_FLOAT, + ShaderFormat::R16G16B16A16Uint => dxgi::DXGI_FORMAT_R16G16B16A16_UINT, + ShaderFormat::R16G16B16A16Sint => dxgi::DXGI_FORMAT_R16G16B16A16_SINT, + ShaderFormat::R16G16B16A16Sfloat => dxgi::DXGI_FORMAT_R16G16B16A16_FLOAT, + ShaderFormat::R32Uint => dxgi::DXGI_FORMAT_R32_UINT, + ShaderFormat::R32Sint =>dxgi::DXGI_FORMAT_R32_SINT, + ShaderFormat::R32Sfloat => dxgi::DXGI_FORMAT_R32_FLOAT, + ShaderFormat::R32G32Uint => dxgi::DXGI_FORMAT_R32G32_UINT, + ShaderFormat::R32G32Sint => dxgi::DXGI_FORMAT_R32G32_SINT, + ShaderFormat::R32G32Sfloat => dxgi::DXGI_FORMAT_R32G32_FLOAT, + ShaderFormat::R32G32B32A32Uint => dxgi::DXGI_FORMAT_R32G32B32A32_UINT, + ShaderFormat::R32G32B32A32Sint => dxgi::DXGI_FORMAT_R32G32B32A32_SINT, + ShaderFormat::R32G32B32A32Sfloat => dxgi::DXGI_FORMAT_R32G32B32A32_FLOAT, + } + } +} + +impl From for Direct3D11::D3D11_TEXTURE_ADDRESS_MODE { + fn from(value: WrapMode) -> Self { + match value { + WrapMode::ClampToBorder => Direct3D11::D3D11_TEXTURE_ADDRESS_BORDER, + WrapMode::ClampToEdge => Direct3D11::D3D11_TEXTURE_ADDRESS_CLAMP, + WrapMode::Repeat => Direct3D11::D3D11_TEXTURE_ADDRESS_WRAP, + WrapMode::MirroredRepeat => Direct3D11::D3D11_TEXTURE_ADDRESS_MIRROR, + } + } +} + +impl From for Direct3D11::D3D11_FILTER { + fn from(value: FilterMode) -> Self { + match value { + FilterMode::Linear => Direct3D11::D3D11_FILTER_MIN_MAG_MIP_LINEAR, + _ => Direct3D11::D3D11_FILTER_MIN_MAG_MIP_POINT + } + } +} diff --git a/librashader-common/src/dx.rs b/librashader-common/src/dx.rs deleted file mode 100644 index e414d39..0000000 --- a/librashader-common/src/dx.rs +++ /dev/null @@ -1,39 +0,0 @@ -use crate::{FilterMode, ShaderFormat, WrapMode}; -// -// impl From for gl::types::GLenum { -// fn from(format: ShaderFormat) -> Self { -// match format { -// ShaderFormat::Unknown => 0 as gl::types::GLenum, -// ShaderFormat::R8Unorm => gl::R8, -// ShaderFormat::R8Uint => gl::R8UI, -// ShaderFormat::R8Sint => gl::R8I, -// ShaderFormat::R8G8Unorm => gl::RG8, -// ShaderFormat::R8G8Uint => gl::RG8UI, -// ShaderFormat::R8G8Sint => gl::RG8I, -// ShaderFormat::R8G8B8A8Unorm => gl::RGBA8, -// ShaderFormat::R8G8B8A8Uint => gl::RGBA8UI, -// ShaderFormat::R8G8B8A8Sint => gl::RGBA8I, -// ShaderFormat::R8G8B8A8Srgb => gl::SRGB8_ALPHA8, -// ShaderFormat::A2B10G10R10UnormPack32 => gl::RGB10_A2, -// ShaderFormat::A2B10G10R10UintPack32 => gl::RGB10_A2UI, -// ShaderFormat::R16Uint => gl::R16UI, -// ShaderFormat::R16Sint => gl::R16I, -// ShaderFormat::R16Sfloat => gl::R16F, -// ShaderFormat::R16G16Uint => gl::RG16UI, -// ShaderFormat::R16G16Sint => gl::RG16I, -// ShaderFormat::R16G16Sfloat => gl::RG16F, -// ShaderFormat::R16G16B16A16Uint => gl::RGBA16UI, -// ShaderFormat::R16G16B16A16Sint => gl::RGBA16I, -// ShaderFormat::R16G16B16A16Sfloat => gl::RGBA16F, -// ShaderFormat::R32Uint => gl::R32UI, -// ShaderFormat::R32Sint => gl::R32I, -// ShaderFormat::R32Sfloat => gl::R32F, -// ShaderFormat::R32G32Uint => gl::RG32UI, -// ShaderFormat::R32G32Sint => gl::RG32I, -// ShaderFormat::R32G32Sfloat => gl::RG32F, -// ShaderFormat::R32G32B32A32Uint => gl::RGBA32UI, -// ShaderFormat::R32G32B32A32Sint => gl::RGBA32I, -// ShaderFormat::R32G32B32A32Sfloat => gl::RGBA32F, -// } -// } -// } \ No newline at end of file diff --git a/librashader-common/src/lib.rs b/librashader-common/src/lib.rs index f7128a3..357d912 100644 --- a/librashader-common/src/lib.rs +++ b/librashader-common/src/lib.rs @@ -1,7 +1,7 @@ #[cfg(feature = "opengl")] pub mod gl; -#[cfg(feature = "dxgi")] -pub mod dx; +#[cfg(feature = "d3d11")] +pub mod d3d11; pub mod image; pub mod runtime; @@ -12,7 +12,7 @@ use std::str::FromStr; use num_traits::AsPrimitive; #[repr(u32)] -#[derive(Default, Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Default, Copy, Clone, Debug, Eq, PartialEq, Hash)] pub enum ShaderFormat { #[default] Unknown = 0, @@ -57,7 +57,7 @@ pub enum ShaderFormat { } #[repr(i32)] -#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Hash)] pub enum FilterMode { #[default] Linear = 0, @@ -80,7 +80,7 @@ impl FromStr for WrapMode { } #[repr(i32)] -#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)] +#[derive(Copy, Clone, Default, Debug, Eq, PartialEq, Hash)] pub enum WrapMode { #[default] ClampToBorder = 0, diff --git a/librashader-runtime-dx11/Cargo.toml b/librashader-runtime-dx11/Cargo.toml index e2031a7..23c82d0 100644 --- a/librashader-runtime-dx11/Cargo.toml +++ b/librashader-runtime-dx11/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -"librashader-common" = { path = "../librashader-common" } +"librashader-common" = { path = "../librashader-common", features = ["d3d11"] } "librashader-presets" = { path = "../librashader-presets" } "librashader-preprocess" = { path = "../librashader-preprocess" } "librashader-reflect" = { path = "../librashader-reflect" } diff --git a/librashader-runtime-dx11/src/lib.rs b/librashader-runtime-dx11/src/lib.rs index 6d79da6..ca3e3f8 100644 --- a/librashader-runtime-dx11/src/lib.rs +++ b/librashader-runtime-dx11/src/lib.rs @@ -22,6 +22,7 @@ mod filter_pass; mod hello_triangle; mod texture; mod util; +mod samplers; #[cfg(test)] mod tests { diff --git a/librashader-runtime-dx11/src/samplers.rs b/librashader-runtime-dx11/src/samplers.rs new file mode 100644 index 0000000..e39ef93 --- /dev/null +++ b/librashader-runtime-dx11/src/samplers.rs @@ -0,0 +1,52 @@ +use rustc_hash::FxHashMap; +use windows::Win32::Graphics::Direct3D11::{D3D11_COMPARISON_NEVER, D3D11_FLOAT32_MAX, D3D11_SAMPLER_DESC, D3D11_TEXTURE_ADDRESS_MODE, ID3D11Device, ID3D11SamplerState}; +use librashader_common::{FilterMode, WrapMode}; +use crate::util::Result; +pub struct SamplerSet { + samplers: FxHashMap<(WrapMode, FilterMode), ID3D11SamplerState> +} + +impl SamplerSet { + pub fn new(device: &ID3D11Device) -> Result { + let mut samplers = FxHashMap::default(); + let wrap_modes = + &[WrapMode::ClampToBorder, WrapMode::ClampToEdge, WrapMode::Repeat, WrapMode::MirroredRepeat]; + for wrap_mode in wrap_modes { + unsafe { + let linear = device.CreateSamplerState(&D3D11_SAMPLER_DESC { + Filter: FilterMode::Linear.into(), + AddressU: D3D11_TEXTURE_ADDRESS_MODE::from(*wrap_mode), + AddressV: D3D11_TEXTURE_ADDRESS_MODE::from(*wrap_mode), + AddressW: D3D11_TEXTURE_ADDRESS_MODE::from(*wrap_mode), + MipLODBias: 0.0, + MaxAnisotropy: 1, + ComparisonFunc: D3D11_COMPARISON_NEVER, + BorderColor: [0.0, 0.0, 0.0, 0.0], + MinLOD: -D3D11_FLOAT32_MAX, + MaxLOD: D3D11_FLOAT32_MAX, + })?; + + let nearest = device.CreateSamplerState(&D3D11_SAMPLER_DESC { + Filter: FilterMode::Nearest.into(), + AddressU: D3D11_TEXTURE_ADDRESS_MODE::from(*wrap_mode), + AddressV: D3D11_TEXTURE_ADDRESS_MODE::from(*wrap_mode), + AddressW: D3D11_TEXTURE_ADDRESS_MODE::from(*wrap_mode), + MipLODBias: 0.0, + MaxAnisotropy: 1, + ComparisonFunc: D3D11_COMPARISON_NEVER, + BorderColor: [0.0, 0.0, 0.0, 0.0], + MinLOD: -D3D11_FLOAT32_MAX, + MaxLOD: D3D11_FLOAT32_MAX, + })?; + + samplers.insert((*wrap_mode, FilterMode::Linear), linear); + samplers.insert((*wrap_mode, FilterMode::Nearest), nearest); + } + } + + Ok(SamplerSet { + samplers + }) + } +} + diff --git a/librashader-runtime-dx11/src/texture.rs b/librashader-runtime-dx11/src/texture.rs index 0f080ea..bf06a90 100644 --- a/librashader-runtime-dx11/src/texture.rs +++ b/librashader-runtime-dx11/src/texture.rs @@ -113,7 +113,8 @@ impl OwnedTexture { SysMemSlicePitch: 0 }))?; - // todo: do format conversion (leverage image crate..? is this necessary tbh)... + // todo: do format conversion (leverage image crate..? + // is this necessary with CopySubresourceRegion)... context.CopySubresourceRegion(&handle, 0, 0, 0, 0, &staging, 0, Some(&D3D11_BOX {