d3d11: gen samplers

This commit is contained in:
chyyran 2022-11-26 15:55:14 -05:00
parent 5cd30ade02
commit 4c4a918b9f
9 changed files with 130 additions and 50 deletions

1
Cargo.lock generated
View file

@ -427,6 +427,7 @@ dependencies = [
"gl",
"image",
"num-traits",
"windows",
]
[[package]]

View file

@ -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 = [
]

View file

@ -0,0 +1,61 @@
use crate::{FilterMode, ShaderFormat, WrapMode};
use windows::Win32::Graphics::Direct3D11;
use windows::Win32::Graphics::Dxgi::Common as dxgi;
impl From<ShaderFormat> 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<WrapMode> 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<FilterMode> 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
}
}
}

View file

@ -1,39 +0,0 @@
use crate::{FilterMode, ShaderFormat, WrapMode};
//
// impl From<ShaderFormat> 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,
// }
// }
// }

View file

@ -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,

View file

@ -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" }

View file

@ -22,6 +22,7 @@ mod filter_pass;
mod hello_triangle;
mod texture;
mod util;
mod samplers;
#[cfg(test)]
mod tests {

View file

@ -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<SamplerSet> {
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
})
}
}

View file

@ -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 {