Skip to content

Commit

Permalink
rt(d3d9): experimental d3d9 runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyran committed Mar 4, 2024
1 parent 1cffa3d commit e19eb7c
Show file tree
Hide file tree
Showing 20 changed files with 1,727 additions and 10 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ members = [
"librashader-runtime-wgpu",
"librashader-cache",
"librashader-capi",
"librashader-build-script"]
"librashader-build-script", "librashader-runtime-d3d9"]
resolver = "2"

[workspace.dependencies]
Expand Down
2 changes: 2 additions & 0 deletions librashader-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ description = "RetroArch shaders for all."
[features]
default = []
opengl = ["gl"]
d3d9 = ["windows"]
d3d11 = ["windows", "dxgi"]
d3d12 = ["windows", "dxgi"]
dxgi = ["windows"]
Expand All @@ -36,6 +37,7 @@ features = [
"Win32_Foundation",
"Win32_Graphics_Dxgi_Common",
"Win32_Graphics_Direct3D",
"Win32_Graphics_Direct3D9",
"Win32_Graphics_Direct3D11",
"Win32_Graphics_Direct3D12",
]
Expand Down
101 changes: 101 additions & 0 deletions librashader-common/src/d3d9.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use crate::{FilterMode, ImageFormat, WrapMode};
use windows::Win32::Graphics::Direct3D9;
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT;
//
impl From<ImageFormat> for Direct3D9::D3DFORMAT {
fn from(format: ImageFormat) -> Self {
match format {
ImageFormat::Unknown => Direct3D9::D3DFMT_UNKNOWN,
ImageFormat::R8Unorm => Direct3D9::D3DFMT_R8G8B8,
ImageFormat::R8Uint => Direct3D9::D3DFMT_R8G8B8,
ImageFormat::R8Sint => Direct3D9::D3DFMT_R8G8B8,
ImageFormat::R8G8B8A8Unorm => Direct3D9::D3DFMT_A8B8G8R8,
ImageFormat::R8G8B8A8Uint => Direct3D9::D3DFMT_A8B8G8R8,
ImageFormat::R8G8B8A8Sint => Direct3D9::D3DFMT_A8B8G8R8,
ImageFormat::R8G8B8A8Srgb => Direct3D9::D3DFMT_A8B8G8R8,
ImageFormat::A2B10G10R10UnormPack32 => Direct3D9::D3DFMT_A2B10G10R10,
ImageFormat::A2B10G10R10UintPack32 => Direct3D9::D3DFMT_A2B10G10R10,
ImageFormat::R16Sfloat => Direct3D9::D3DFMT_R16F,
ImageFormat::R16G16Uint => Direct3D9::D3DFMT_G16R16,
ImageFormat::R16G16Sint => Direct3D9::D3DFMT_G16R16,
ImageFormat::R16G16Sfloat => Direct3D9::D3DFMT_G16R16F,
ImageFormat::R16G16B16A16Uint => Direct3D9::D3DFMT_A16B16G16R16,
ImageFormat::R16G16B16A16Sint => Direct3D9::D3DFMT_A16B16G16R16,
ImageFormat::R16G16B16A16Sfloat => Direct3D9::D3DFMT_A16B16G16R16F,
ImageFormat::R32Sfloat => Direct3D9::D3DFMT_R32F,
_ => Direct3D9::D3DFMT_UNKNOWN
}
}
}
//
// impl From<Direct3D9::D3DFORMAT> for ImageFormat {
// fn from(format: DXGI_FORMAT) -> Self {
// match format {
// dxgi::DXGI_FORMAT_UNKNOWN => ImageFormat::Unknown,
// dxgi::DXGI_FORMAT_R8_UNORM => ImageFormat::R8Unorm,
// dxgi::DXGI_FORMAT_R8_UINT => ImageFormat::R8Uint,
// dxgi::DXGI_FORMAT_R8_SINT => ImageFormat::R8Sint,
// dxgi::DXGI_FORMAT_R8G8_UNORM => ImageFormat::R8G8Unorm,
// dxgi::DXGI_FORMAT_R8G8_UINT => ImageFormat::R8G8Uint,
// dxgi::DXGI_FORMAT_R8G8_SINT => ImageFormat::R8G8Sint,
// dxgi::DXGI_FORMAT_R8G8B8A8_UNORM => ImageFormat::R8G8B8A8Unorm,
// dxgi::DXGI_FORMAT_R8G8B8A8_UINT => ImageFormat::R8G8B8A8Uint,
// dxgi::DXGI_FORMAT_R8G8B8A8_SINT => ImageFormat::R8G8B8A8Sint,
// dxgi::DXGI_FORMAT_R8G8B8A8_UNORM_SRGB => ImageFormat::R8G8B8A8Srgb,
// dxgi::DXGI_FORMAT_R10G10B10A2_UNORM => ImageFormat::A2B10G10R10UnormPack32,
// dxgi::DXGI_FORMAT_R10G10B10A2_UINT => ImageFormat::A2B10G10R10UintPack32,
// dxgi::DXGI_FORMAT_R16_UINT => ImageFormat::R16Uint,
// dxgi::DXGI_FORMAT_R16_SINT => ImageFormat::R16Sint,
// dxgi::DXGI_FORMAT_R16_FLOAT => ImageFormat::R16Sfloat,
// dxgi::DXGI_FORMAT_R16G16_UINT => ImageFormat::R16G16Uint,
// dxgi::DXGI_FORMAT_R16G16_SINT => ImageFormat::R16G16Sint,
// dxgi::DXGI_FORMAT_R16G16_FLOAT => ImageFormat::R16G16Sfloat,
// dxgi::DXGI_FORMAT_R16G16B16A16_UINT => ImageFormat::R16G16B16A16Uint,
// dxgi::DXGI_FORMAT_R16G16B16A16_SINT => ImageFormat::R16G16B16A16Sint,
// dxgi::DXGI_FORMAT_R16G16B16A16_FLOAT => ImageFormat::R16G16B16A16Sfloat,
// dxgi::DXGI_FORMAT_R32_UINT => ImageFormat::R32Uint,
// dxgi::DXGI_FORMAT_R32_SINT => ImageFormat::R32Sint,
// dxgi::DXGI_FORMAT_R32_FLOAT => ImageFormat::R32Sfloat,
// dxgi::DXGI_FORMAT_R32G32_UINT => ImageFormat::R32G32Uint,
// dxgi::DXGI_FORMAT_R32G32_SINT => ImageFormat::R32G32Sint,
// dxgi::DXGI_FORMAT_R32G32_FLOAT => ImageFormat::R32G32Sfloat,
// dxgi::DXGI_FORMAT_R32G32B32A32_UINT => ImageFormat::R32G32B32A32Uint,
// dxgi::DXGI_FORMAT_R32G32B32A32_SINT => ImageFormat::R32G32B32A32Sint,
// dxgi::DXGI_FORMAT_R32G32B32A32_FLOAT => ImageFormat::R32G32B32A32Sfloat,
// _ => ImageFormat::Unknown,
// }
// }
// }
//

impl From<WrapMode> for Direct3D9::D3DTEXTUREADDRESS {
fn from(value: WrapMode) -> Self {
match value {
WrapMode::ClampToBorder => Direct3D9::D3DTADDRESS_BORDER,
WrapMode::ClampToEdge => Direct3D9::D3DTADDRESS_CLAMP,
WrapMode::Repeat => Direct3D9::D3DTADDRESS_WRAP,
WrapMode::MirroredRepeat => Direct3D9::D3DTADDRESS_MIRROR,
}
}
}

impl From<FilterMode> for Direct3D9::D3DTEXTUREFILTER {
fn from(value: FilterMode) -> Self {
match value {
FilterMode::Linear => Direct3D9::D3DFILTER_LINEAR,
FilterMode::Nearest => Direct3D9::D3DFILTER_NEAREST,
}
}
}

// impl FilterMode {
// /// Get the mipmap filtering mode for the given combination.
// pub fn d3d9_mip(&self, mip: FilterMode) -> Direct3D9::D3DTEXTUREFILTER {
// match (self, mip) {
// (FilterMode::Linear, FilterMode::Linear) => Direct3D9::D3DFILTER_MIPLINEAR,
// (FilterMode::Linear, FilterMode::Nearest) => Direct3D9::D3DFILTER_LINEARMIPNEAREST,
// (FilterMode::Nearest, FilterMode::Linear) => Direct3D9::D3DFILTER_MIPNEAREST,
// _ => Direct3D9::D3DFILTER_MIPNEAREST
// }
// }
// }
4 changes: 4 additions & 0 deletions librashader-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pub mod wgpu;
#[cfg(all(target_os = "windows", feature = "dxgi"))]
pub mod dxgi;

/// Direct3D 9 common conversions.
#[cfg(all(target_os = "windows", feature = "d3d9"))]
pub mod d3d9;

/// Direct3D 11 common conversions.
#[cfg(all(target_os = "windows", feature = "d3d11"))]
pub mod d3d11;
Expand Down
23 changes: 16 additions & 7 deletions librashader-reflect/src/reflect/cross/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,12 @@ mod test {
use crate::reflect::semantics::{Semantic, ShaderSemantics, UniformSemantic, UniqueSemantics};
use librashader_common::map::FastHashMap;
use librashader_preprocess::ShaderSource;
use spirv_cross::glsl;
use spirv_cross::{glsl, hlsl};
use spirv_cross::glsl::{CompilerOptions, Version};
use spirv_cross::hlsl::ShaderModel;
use crate::back::hlsl::CrossHlslContext;
use crate::back::{CompileShader, ShaderCompilerOutput};
use crate::back::targets::HLSL;

#[test]
pub fn test_into() {
Expand All @@ -763,8 +767,8 @@ mod test {
);
}
let spirv = Glslang::compile(&result).unwrap();
let mut reflect = CrossReflect::<glsl::Target>::try_from(&spirv).unwrap();
let _shader_reflection = reflect
let mut reflect = CrossReflect::<hlsl::Target>::try_from(&spirv).unwrap();
let shader_reflection = reflect
.reflect(
0,
&ShaderSemantics {
Expand All @@ -773,10 +777,15 @@ mod test {
},
)
.unwrap();
let mut opts = CompilerOptions::default();
opts.version = Version::V4_60;
opts.enable_420_pack_extension = false;
// let compiled: ShaderCompilerOutput<String, CrossWgslContext> = <CrossReflect<glsl::Target> as CompileShader<WGSL>>::compile(reflect, Version::V3_30).unwrap();
let mut opts = hlsl::CompilerOptions::default();
opts.shader_model = ShaderModel::V3_0;

let compiled: ShaderCompilerOutput<String, CrossHlslContext> = <CrossReflect<hlsl::Target> as CompileShader<HLSL>>::compile(reflect, Some(ShaderModel::V3_0)).unwrap();

println!("{:?}",shader_reflection.meta);
println!("{}", compiled.fragment);
println!("{}", compiled.vertex);

// // eprintln!("{shader_reflection:#?}");
// eprintln!("{}", compiled.fragment)
// let mut loader = rspirv::dr::Loader::new();
Expand Down
57 changes: 57 additions & 0 deletions librashader-runtime-d3d9/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[package]
name = "librashader-runtime-d3d9"
edition = "2021"

license = "MPL-2.0 OR GPL-3.0-only"
version = "0.2.4"
authors = ["Ronny Chan <ronny@ronnychan.ca>"]
repository = "https://github.com/SnowflakePowered/librashader"
readme = "../README.md"
categories = ["emulators", "compilers", "graphics"]
keywords = ["shader", "retroarch", "SPIR-V"]
description = "RetroArch shaders for all."

[dependencies]
librashader-common = { path = "../librashader-common", features = ["d3d9", "d3d11"], version = "0.2.4" }
librashader-presets = { path = "../librashader-presets", version = "0.2.4" }
librashader-preprocess = { path = "../librashader-preprocess", version = "0.2.4" }
librashader-reflect = { path = "../librashader-reflect", version = "0.2.4" }
librashader-runtime = { path = "../librashader-runtime", version = "0.2.4" }
librashader-cache = { path = "../librashader-cache", version = "0.2.4", features = ["d3d"] }

thiserror = "1.0.37"
bytemuck = "1.12.3"
rayon = "1.6.1"
array-concat = "0.5.2"

[target.'cfg(windows)'.dependencies.windows]
workspace = true
features = [
"Win32_Foundation",
"Win32_Graphics_Direct3D",
"Win32_Graphics_Direct3D9",
"Win32_Graphics_Direct3D_Fxc",
"Win32_System_Threading",
"Win32_Security",
]

[target.'cfg(windows)'.dev-dependencies.windows]
workspace = true
features = [
"Win32_Foundation",
"Win32_Graphics_Direct3D",
"Win32_Graphics_Direct3D9",
"Win32_Graphics_Direct3D_Fxc",
"Win32_Graphics_Gdi",
"Win32_Security",
"Win32_System_LibraryLoader",
"Win32_System_Threading",
"Win32_UI_WindowsAndMessaging",
"Win32_UI",
]

[dev-dependencies]
gfx-maths = "0.2.8"

[package.metadata.docs.rs]
features = ["librashader-cache/docsrs"]
Loading

0 comments on commit e19eb7c

Please sign in to comment.