Skip to content

Commit

Permalink
d3d9
Browse files Browse the repository at this point in the history
  • Loading branch information
chyyran committed Mar 6, 2024
1 parent d549f52 commit 570e6ea
Show file tree
Hide file tree
Showing 24 changed files with 994 additions and 117 deletions.
1 change: 0 additions & 1 deletion 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 librashader-common/src/d3d9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl From<ImageFormat> for Direct3D9::D3DFORMAT {
ImageFormat::R16G16B16A16Sint => Direct3D9::D3DFMT_A16B16G16R16,
ImageFormat::R16G16B16A16Sfloat => Direct3D9::D3DFMT_A16B16G16R16F,
ImageFormat::R32Sfloat => Direct3D9::D3DFMT_R32F,
_ => Direct3D9::D3DFMT_UNKNOWN
_ => Direct3D9::D3DFMT_UNKNOWN,
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions librashader-reflect/src/reflect/cross/hlsl.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use spirv_cross::ErrorCode;
use crate::back::hlsl::CrossHlslContext;
use crate::back::targets::HLSL;
use crate::back::{CompileShader, ShaderCompilerOutput};
use crate::error::ShaderCompileError;
use crate::reflect::cross::{CompiledAst, CompiledProgram, CrossReflect};
use spirv_cross::hlsl::ShaderModel as HlslShaderModel;
use spirv_cross::spirv::Decoration;

pub(crate) type HlslReflect = CrossReflect<spirv_cross::hlsl::Target>;

Expand All @@ -22,6 +24,39 @@ impl CompileShader<HLSL> for CrossReflect<spirv_cross::hlsl::Target> {
self.vertex.set_compiler_options(&options)?;
self.fragment.set_compiler_options(&options)?;

// todo: options
let flatten = sm == HlslShaderModel::V3_0;

let vertex_resources = self.vertex.get_shader_resources()?;
let fragment_resources = self.fragment.get_shader_resources()?;

if vertex_resources.uniform_buffers.len() > 1 {
return Err(ShaderCompileError::SpirvCrossCompileError(
ErrorCode::CompilationError(String::from(
"Cannot have more than one uniform buffer",
)),
));
}

println!("{:?}", self.vertex.get_active_interface_variables()?);

if fragment_resources.push_constant_buffers.len() > 1 {
return Err(ShaderCompileError::SpirvCrossCompileError(
ErrorCode::CompilationError(String::from(
"Cannot have more than one push constant buffer",
)),
));
}

if fragment_resources.uniform_buffers.len() > 1 {
return Err(ShaderCompileError::SpirvCrossCompileError(
ErrorCode::CompilationError(String::from(
"Cannot have more than one uniform buffer",
)),
));
}


Ok(ShaderCompilerOutput {
vertex: self.vertex.compile()?,
fragment: self.fragment.compile()?,
Expand Down
17 changes: 11 additions & 6 deletions librashader-reflect/src/reflect/cross/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,16 +741,16 @@ mod test {
use crate::reflect::ReflectShader;
use rustc_hash::FxHashMap;

use crate::back::hlsl::CrossHlslContext;
use crate::back::targets::HLSL;
use crate::back::{CompileShader, ShaderCompilerOutput};
use crate::front::{Glslang, ShaderInputCompiler};
use crate::reflect::semantics::{Semantic, ShaderSemantics, UniformSemantic, UniqueSemantics};
use librashader_common::map::FastHashMap;
use librashader_preprocess::ShaderSource;
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;
use spirv_cross::{glsl, hlsl};

#[test]
pub fn test_into() {
Expand Down Expand Up @@ -780,9 +780,14 @@ mod test {
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();
let compiled: ShaderCompilerOutput<String, CrossHlslContext> =
<CrossReflect<hlsl::Target> as CompileShader<HLSL>>::compile(
reflect,
Some(ShaderModel::V3_0),
)
.unwrap();

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

Expand Down
6 changes: 5 additions & 1 deletion librashader-runtime-d3d9/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ librashader-cache = { path = "../librashader-cache", version = "0.2.4", features

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

[target.'cfg(windows)'.dependencies.windows]
Expand All @@ -41,15 +40,20 @@ features = [
"Win32_Foundation",
"Win32_Graphics_Direct3D",
"Win32_Graphics_Direct3D9",
"Win32_Graphics_Direct3D9on12",
"Win32_Graphics_Direct3D_Fxc",
"Win32_Graphics_Gdi",
"Win32_Security",
"Win32_System_LibraryLoader",
"Win32_System_Threading",
"Win32_UI_WindowsAndMessaging",
"Win32_UI",
"Foundation_Numerics"
]

[[test]]
name = "triangle"

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

Expand Down
16 changes: 15 additions & 1 deletion librashader-runtime-d3d9/src/binding.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Debug;
use librashader_reflect::reflect::semantics::{MemberOffset, UniformMemberBlock};
use librashader_runtime::binding::ContextOffset;
use librashader_runtime::uniforms::{BindUniform, UniformScalar, UniformStorage};
Expand Down Expand Up @@ -135,6 +136,7 @@ pub(crate) struct D3D9UniformBinder;
impl<T> BindUniform<ConstantRegister, T, IDirect3DDevice9> for D3D9UniformBinder
where
T: D3D9UniformScalar,
T: Debug
{
fn bind_uniform(
block: UniformMemberBlock,
Expand All @@ -145,13 +147,15 @@ where
if let Some(location) = context.register(block) {
// need to respect packing rules.
// todo: confirm lsb vs msb
let value = [T::zeroed(), T::zeroed(), T::zeroed(), value];
let value = [value, T::zeroed(), T::zeroed(), T::zeroed()];
unsafe {
if let Err(err) = T::FACTORY_VS(&device, location, value.as_ptr(), 1) {
println!(
"[librashader-runtime-d3d9] unable to bind vertex {}: {err}",
location
);
} else {
eprintln!("set vs {location} val {value:?}")
}
}
unsafe {
Expand All @@ -160,6 +164,8 @@ where
"[librashader-runtime-d3d9] unable to bind fragment {}: {err}",
location
);
}else {
eprintln!("set ps {location} val {value:?}")
}
}
Some(())
Expand All @@ -183,6 +189,8 @@ impl BindUniform<ConstantRegister, &[f32; 4], IDirect3DDevice9> for D3D9UniformB
"[librashader-runtime-d3d9] unable to bind vertex {}: {err}",
location
);
} else {
eprintln!("set vs {location} f4 {vec4:?}")
}
}
unsafe {
Expand All @@ -191,6 +199,8 @@ impl BindUniform<ConstantRegister, &[f32; 4], IDirect3DDevice9> for D3D9UniformB
"[librashader-runtime-d3d9] unable to bind fragment {}: {err}",
location
);
}else {
eprintln!("set ps {location} f4 {vec4:?}")
}
}
Some(())
Expand All @@ -215,6 +225,8 @@ impl BindUniform<ConstantRegister, &[f32; 16], IDirect3DDevice9> for D3D9Uniform
"[librashader-runtime-d3d9] unable to bind vertex {}: {err}",
location
);
} else {
eprintln!("set vs {location} mvp {mat4:?}")
}
}
unsafe {
Expand All @@ -223,6 +235,8 @@ impl BindUniform<ConstantRegister, &[f32; 16], IDirect3DDevice9> for D3D9Uniform
"[librashader-runtime-d3d9] unable to bind fragment {}: {err}",
location
);
} else {
eprintln!("set ps {location} mvp {mat4:?}")
}
}
}
Expand Down
78 changes: 63 additions & 15 deletions librashader-runtime-d3d9/src/draw_quad.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::error;
use crate::error::{assume_d3d_init, Result};
use array_concat::concat_arrays;
use librashader_runtime::quad::{QuadType, VertexInput};
use windows::Win32::Graphics::Direct3D9::{
IDirect3DDevice9, IDirect3DVertexBuffer9, D3DFVF_TEX1, D3DFVF_XYZRHW, D3DPOOL_DEFAULT,
D3DPT_TRIANGLESTRIP,
};
use bytemuck::offset_of;
use librashader_runtime::quad::{IDENTITY_MVP, QuadType, VertexInput};
use windows::core::PCSTR;
use windows::Win32::Foundation::FALSE;
use windows::Win32::Graphics::Direct3D11::{D3D11_INPUT_ELEMENT_DESC, D3D11_INPUT_PER_VERTEX_DATA};
use windows::Win32::Graphics::Direct3D9::{IDirect3DDevice9, IDirect3DVertexBuffer9, IDirect3DVertexDeclaration9, D3DCMP_ALWAYS, D3DCULL_NONE, D3DDECLMETHOD_DEFAULT, D3DDECLTYPE_FLOAT2, D3DDECLTYPE_FLOAT4, D3DDECLTYPE_UNUSED, D3DDECLUSAGE_POSITION, D3DDECLUSAGE_TEXCOORD, D3DFVF_TEX1, D3DFVF_XYZRHW, D3DFVF_XYZW, D3DPOOL_DEFAULT, D3DPT_TRIANGLESTRIP, D3DRS_CLIPPING, D3DRS_CULLMODE, D3DRS_LIGHTING, D3DRS_ZENABLE, D3DRS_ZFUNC, D3DVERTEXELEMENT9, D3DTS_PROJECTION, D3DTS_VIEW, D3DTRANSFORMSTATETYPE};
use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32_FLOAT;

const OFFSCREEN_VBO_DATA: [VertexInput; 4] = [
VertexInput {
Expand Down Expand Up @@ -50,6 +52,7 @@ static VBO_DATA: &[VertexInput; 8] = &concat_arrays!(OFFSCREEN_VBO_DATA, FINAL_V
pub(crate) struct DrawQuad {
stride: u32,
vbo: IDirect3DVertexBuffer9,
vao: IDirect3DVertexDeclaration9,
}

impl DrawQuad {
Expand All @@ -59,7 +62,7 @@ impl DrawQuad {
device.CreateVertexBuffer(
2 * std::mem::size_of::<[VertexInput; 4]>() as u32,
0,
D3DFVF_XYZRHW | D3DFVF_TEX1,
0,
D3DPOOL_DEFAULT,
&mut vbo,
std::ptr::null_mut(),
Expand All @@ -76,31 +79,76 @@ impl DrawQuad {
)?;
std::ptr::copy_nonoverlapping(VBO_DATA.as_ptr(), ptr.cast::<VertexInput>(), 8);
vbo.Unlock()?;

let vao = device.CreateVertexDeclaration(Self::get_spirv_cross_vbo_desc().as_ptr())?;
Ok(DrawQuad {
vbo,
stride: std::mem::size_of::<VertexInput>() as u32,
vao,
})
}
}

pub fn bind_vbo_for_frame(&self, device: &IDirect3DDevice9) -> Result<()> {
unsafe {
device.SetStreamSource(0, &self.vbo, 0, std::mem::size_of::<VertexInput>() as u32)?;
device.SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1)?;
Ok(())
}
}

pub fn draw_quad(&self, device: &IDirect3DDevice9, vbo_type: QuadType) -> Result<()> {
pub fn draw_quad(&self, device: &IDirect3DDevice9, vbo_type: QuadType, mvp: &[f32; 16]) -> Result<()> {
let offset = match vbo_type {
QuadType::Offscreen => 0,
QuadType::Final => 4,
QuadType::Final => 0,
};

unsafe {
device
.SetTransform(D3DTS_PROJECTION, mvp.as_ptr().cast())?;
device
.SetTransform(D3DTS_VIEW, mvp.as_ptr().cast())?;
device
.SetTransform(D3DTRANSFORMSTATETYPE(256), mvp.as_ptr().cast())?;


device.SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE.0 as u32)?;
device.SetRenderState(D3DRS_CLIPPING, FALSE.0 as u32)?;
device.SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS.0 as u32)?;
device.SetRenderState(D3DRS_ZENABLE, FALSE.0 as u32)?;
device.SetRenderState(D3DRS_LIGHTING, FALSE.0 as u32)?;

device.BeginScene()?;
device.SetStreamSource(0, &self.vbo, 0, std::mem::size_of::<VertexInput>() as u32)?;
// device.SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1)?;
device.SetVertexDeclaration(&self.vao)?;

device.DrawPrimitive(D3DPT_TRIANGLESTRIP, offset, 2)?;
device.EndScene()?;
}

Ok(())
}

pub fn get_spirv_cross_vbo_desc() -> [D3DVERTEXELEMENT9; 3] {
[
D3DVERTEXELEMENT9 {
Stream: 0,
Offset: offset_of!(VertexInput, position) as u16,
Type: D3DDECLTYPE_FLOAT4.0 as u8,
Method: D3DDECLMETHOD_DEFAULT.0 as u8,
Usage: D3DDECLUSAGE_TEXCOORD.0 as u8,
UsageIndex: 0,
},
D3DVERTEXELEMENT9 {
Stream: 0,
Offset: offset_of!(VertexInput, texcoord) as u16,
Type: D3DDECLTYPE_FLOAT2.0 as u8,
Method: D3DDECLMETHOD_DEFAULT.0 as u8,
Usage: D3DDECLUSAGE_TEXCOORD.0 as u8,
UsageIndex: 1,
},
D3DVERTEXELEMENT9 {
Stream: 0xFF,
Offset: 0,
Type: D3DDECLTYPE_UNUSED.0 as u8,
Method: 0,
Usage: 0,
UsageIndex: 0,
},
]
}
}
Loading

0 comments on commit 570e6ea

Please sign in to comment.