Skip to content

Commit c38f1b7

Browse files
committed
Test.NativeLaunch can now render mods
- mods are flat shaded usually, see big comment - can also a render a spinning cube
1 parent 8bfe297 commit c38f1b7

File tree

14 files changed

+820
-118
lines changed

14 files changed

+820
-118
lines changed

Test.NativeLaunch/Cargo.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Test.NativeLaunch/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ winapi = { version = "0.3", features = ["libloaderapi",
1616
"dinput", "sysinfoapi", "errhandlingapi"] }
1717
anyhow = "*"
1818
rand = "*"
19-
aho-corasick = "0.7.20"
19+
aho-corasick = "0.7.20"
20+
glam = "*"
-608 Bytes
Binary file not shown.

Test.NativeLaunch/basic_vshader.hlsl

Lines changed: 0 additions & 19 deletions
This file was deleted.

Test.NativeLaunch/runmm.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,19 @@ set -e
22

33
mmdir=../Native
44

5+
RELEASE=0
6+
57
# change into mm native dir, check target symlink and build mm dll
6-
(cd $mmdir && rm -f $mmdir/target && sh ./setjunc.sh >/dev/null && cargo build)
7-
cp -v $mmdir/target/debug/hook_core.dll target/debug/d3d11.dll
8-
cargo run -- $@
8+
if [ "$RELEASE" -eq 1 ]; then
9+
(cd $mmdir && rm -f $mmdir/target && sh ./setjunc.sh >/dev/null && cargo build --release)
10+
cp -v $mmdir/target/release/hook_core.dll target/release/d3d11.dll
11+
RUST_BACKTRACE=full cargo run --release -- $@
12+
else
13+
(cd $mmdir && rm -f $mmdir/target && sh ./setjunc.sh >/dev/null && cargo build)
14+
cp -v $mmdir/target/debug/hook_core.dll target/debug/d3d11.dll
15+
RUST_BACKTRACE=full cargo run -- $@
16+
fi
17+
918

1019
# ad hoc benchmark functions for testing the load_mmobj module. intended
1120
# to be pasted into a bash shell.
1.05 KB
Binary file not shown.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
cbuffer LightingBuffer : register(b1)
2+
{
3+
float3 lightDirection; // Should be normalized in app code
4+
float padding; // For alignment (16-byte row)
5+
};
6+
7+
struct VS_OUTPUT
8+
{
9+
float4 position : SV_POSITION;
10+
float3 normal : NORMAL; // Unpacked and passed to PS
11+
};
12+
13+
float4 main(VS_OUTPUT input) : SV_Target
14+
{
15+
float3 N = normalize(input.normal);
16+
float3 L = normalize(-lightDirection); // Negated so light "comes from" the direction
17+
18+
float diffuse = saturate(dot(N, L));
19+
float ambient = 0.2;
20+
21+
float brightness = ambient + diffuse * 0.8;
22+
return float4(brightness, brightness, brightness, 1.0); // grayscale lit color
23+
24+
//return float4(1.0, 0.0, 0.0, 1.0); // bright red
25+
}
1.47 KB
Binary file not shown.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
cbuffer MatrixBuffer : register(b0)
2+
{
3+
matrix modelViewProjection;
4+
};
5+
6+
cbuffer NormalMatrixBuffer : register(b2)
7+
{
8+
float3x3 normalMatrix;
9+
};
10+
11+
struct VS_INPUT
12+
{
13+
float3 position : POSITION;
14+
int4 normal : NORMAL; // Packed as R16G16B16A16_SINT
15+
int2 binormal : BINORMAL; // Ignored for now
16+
float4 texturecoordinate0 : TEXCOORD0; // Ignored for lighting
17+
};
18+
19+
struct VS_OUTPUT
20+
{
21+
float4 position : SV_POSITION;
22+
float3 normal : NORMAL; // Unpacked and passed to PS
23+
};
24+
25+
VS_OUTPUT main(VS_INPUT input)
26+
//VS_OUTPUT main(uint vertexId : SV_VertexID)
27+
{
28+
VS_OUTPUT output;
29+
30+
// Transform position to clip space
31+
output.position = mul(float4(input.position, 1.0), modelViewProjection);
32+
33+
// Unpack normal from signed 16-bit int to float in [-1, 1]
34+
float3 normal;
35+
normal.x = input.normal.x / 32767.0;
36+
normal.y = input.normal.y / 32767.0;
37+
normal.z = input.normal.z / 32767.0;
38+
normal = normalize(normal);
39+
40+
normal = mul(normalMatrix, normal);
41+
42+
output.normal = normalize(normal); // Normalize for lighting
43+
44+
return output;
45+
}
46+
47+
// struct VS_INPUT
48+
// {
49+
// float3 position : POSITION;
50+
// };
51+
52+
// struct VS_OUTPUT
53+
// {
54+
// float4 position : SV_POSITION;
55+
// };
56+
57+
// VS_OUTPUT main(VS_INPUT input)
58+
// {
59+
// VS_OUTPUT output;
60+
// //output.position = float4(input.position, 1.0);
61+
// output.position = mul(float4(input.position, 1.0), modelViewProjection);
62+
// return output;
63+
// }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use winapi::{shared::{winerror::SUCCEEDED}, um::d3d11::{ID3D11Buffer, ID3D11Device, ID3D11DeviceContext, D3D11_BIND_CONSTANT_BUFFER, D3D11_BUFFER_DESC, D3D11_MAPPED_SUBRESOURCE, D3D11_MAP_WRITE_DISCARD, D3D11_USAGE_DYNAMIC}};
2+
3+
4+
pub unsafe fn create_constant_buffer<T>(
5+
device: *mut ID3D11Device,
6+
) -> anyhow::Result<*mut ID3D11Buffer> {
7+
let buffer_desc = D3D11_BUFFER_DESC {
8+
ByteWidth: std::mem::size_of::<T>() as u32,
9+
Usage: D3D11_USAGE_DYNAMIC,
10+
BindFlags: D3D11_BIND_CONSTANT_BUFFER,
11+
CPUAccessFlags: winapi::um::d3d11::D3D11_CPU_ACCESS_WRITE,
12+
MiscFlags: 0,
13+
StructureByteStride: 0,
14+
};
15+
16+
let mut buffer_ptr: *mut ID3D11Buffer = std::ptr::null_mut();
17+
let hr = (*device).CreateBuffer(&buffer_desc, std::ptr::null(), &mut buffer_ptr);
18+
if SUCCEEDED(hr) {
19+
Ok(buffer_ptr)
20+
} else {
21+
Err(anyhow!("failed to create constant buffer: {:X}", hr))
22+
}
23+
}
24+
25+
pub unsafe fn update_constant_buffer<T: Copy>(
26+
context: *mut ID3D11DeviceContext,
27+
buffer: *mut ID3D11Buffer,
28+
data: &T,
29+
) -> anyhow::Result<()> {
30+
let mut mapped = std::mem::zeroed::<D3D11_MAPPED_SUBRESOURCE>();
31+
let hr = (*context).Map(
32+
buffer as *mut _,
33+
0,
34+
D3D11_MAP_WRITE_DISCARD,
35+
0,
36+
&mut mapped,
37+
);
38+
39+
if SUCCEEDED(hr) {
40+
std::ptr::copy_nonoverlapping(
41+
data as *const T as *const u8,
42+
mapped.pData as *mut u8,
43+
std::mem::size_of::<T>(),
44+
);
45+
(*context).Unmap(buffer as *mut _, 0);
46+
Ok(())
47+
} else {
48+
Err(anyhow!("failed to update constant buffer: {:X}", hr))
49+
}
50+
}

0 commit comments

Comments
 (0)