Skip to content

Commit 1346423

Browse files
committed
Add GPU lighting shaders to D3D11 renderer
Move lighting calculations from CPU to GPU via new PositionColorLit and PositionTextureLit HLSL shaders. Add light constant buffer, separate dynamic vertex buffers for colored/textured geometry, and dirty-state tracking to reduce redundant D3D11 calls. Disable vsync for uncapped frame rates. Made-with: Cursor
1 parent 18eac65 commit 1346423

4 files changed

Lines changed: 466 additions & 184 deletions

File tree

VorticeD3D/MatterHackers.VorticeD3D.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
<ItemGroup>
2626
<EmbeddedResource Include="Shaders\PositionColor.hlsl" />
2727
<EmbeddedResource Include="Shaders\PositionTexture.hlsl" />
28+
<EmbeddedResource Include="Shaders\PositionColorLit.hlsl" />
29+
<EmbeddedResource Include="Shaders\PositionTextureLit.hlsl" />
2830
</ItemGroup>
2931

3032
</Project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
cbuffer TransformBuffer : register(b0)
2+
{
3+
row_major float4x4 ModelView;
4+
row_major float4x4 Projection;
5+
};
6+
7+
cbuffer LightBuffer : register(b1)
8+
{
9+
float4 Light0Position;
10+
float4 Light0Ambient;
11+
float4 Light0Diffuse;
12+
float4 Light1Position;
13+
float4 Light1Ambient;
14+
float4 Light1Diffuse;
15+
float4 LightFlags; // x = light0On, y = light1On
16+
};
17+
18+
struct VS_INPUT
19+
{
20+
float3 Position : POSITION;
21+
float3 Normal : NORMAL;
22+
float4 Color : COLOR;
23+
};
24+
25+
struct PS_INPUT
26+
{
27+
float4 Position : SV_POSITION;
28+
float4 Color : COLOR;
29+
};
30+
31+
PS_INPUT VS(VS_INPUT input)
32+
{
33+
PS_INPUT output;
34+
float4 worldPos = mul(float4(input.Position, 1.0), ModelView);
35+
output.Position = mul(worldPos, Projection);
36+
37+
float3 n = normalize(input.Normal);
38+
float3 litColor = float3(0, 0, 0);
39+
40+
if (LightFlags.x > 0.5)
41+
{
42+
float3 lightDir = normalize(Light0Position.xyz);
43+
float ndotl = max(0, dot(n, lightDir));
44+
litColor += Light0Ambient.rgb + Light0Diffuse.rgb * ndotl;
45+
}
46+
47+
if (LightFlags.y > 0.5)
48+
{
49+
float3 lightDir = normalize(Light1Position.xyz);
50+
float ndotl = max(0, dot(n, lightDir));
51+
litColor += Light1Ambient.rgb + Light1Diffuse.rgb * ndotl;
52+
}
53+
54+
output.Color = float4(min(1, input.Color.rgb * litColor), input.Color.a);
55+
return output;
56+
}
57+
58+
float4 PS(PS_INPUT input) : SV_TARGET
59+
{
60+
return input.Color;
61+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
cbuffer TransformBuffer : register(b0)
2+
{
3+
row_major float4x4 ModelView;
4+
row_major float4x4 Projection;
5+
};
6+
7+
cbuffer LightBuffer : register(b1)
8+
{
9+
float4 Light0Position;
10+
float4 Light0Ambient;
11+
float4 Light0Diffuse;
12+
float4 Light1Position;
13+
float4 Light1Ambient;
14+
float4 Light1Diffuse;
15+
float4 LightFlags; // x = light0On, y = light1On
16+
};
17+
18+
Texture2D diffuseTexture : register(t0);
19+
SamplerState textureSampler : register(s0);
20+
21+
struct VS_INPUT
22+
{
23+
float3 Position : POSITION;
24+
float3 Normal : NORMAL;
25+
float2 TexCoord : TEXCOORD0;
26+
float4 Color : COLOR;
27+
};
28+
29+
struct PS_INPUT
30+
{
31+
float4 Position : SV_POSITION;
32+
float2 TexCoord : TEXCOORD0;
33+
float4 Color : COLOR;
34+
};
35+
36+
PS_INPUT VS(VS_INPUT input)
37+
{
38+
PS_INPUT output;
39+
float4 worldPos = mul(float4(input.Position, 1.0), ModelView);
40+
output.Position = mul(worldPos, Projection);
41+
output.TexCoord = input.TexCoord;
42+
43+
float3 n = normalize(input.Normal);
44+
float3 litColor = float3(0, 0, 0);
45+
46+
if (LightFlags.x > 0.5)
47+
{
48+
float3 lightDir = normalize(Light0Position.xyz);
49+
float ndotl = max(0, dot(n, lightDir));
50+
litColor += Light0Ambient.rgb + Light0Diffuse.rgb * ndotl;
51+
}
52+
53+
if (LightFlags.y > 0.5)
54+
{
55+
float3 lightDir = normalize(Light1Position.xyz);
56+
float ndotl = max(0, dot(n, lightDir));
57+
litColor += Light1Ambient.rgb + Light1Diffuse.rgb * ndotl;
58+
}
59+
60+
output.Color = float4(min(1, input.Color.rgb * litColor), input.Color.a);
61+
return output;
62+
}
63+
64+
float4 PS(PS_INPUT input) : SV_TARGET
65+
{
66+
float4 texColor = diffuseTexture.Sample(textureSampler, input.TexCoord);
67+
return texColor * input.Color;
68+
}

0 commit comments

Comments
 (0)