shaders: Implement HLSL for lessons 2-8

This commit is contained in:
2025-06-16 00:12:59 +10:00
parent cdf06bfa26
commit c0c55c06bb
16 changed files with 202 additions and 1 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -213,7 +213,7 @@ def compile_dxbc_shader(shader: Shader, type: str, flags: list[str] | None = Non
def compile_shaders() -> None:
build_spirv = True
build_metal = True
build_dxil = False
build_dxil = True
build_dxbc = False
lessons = [ "lesson2", "lesson3", "lesson6", "lesson7", "lesson8", "lesson9" ]
src_dir = Path("src/shaders")

31
src/shaders/lesson2.hlsl Normal file
View File

@@ -0,0 +1,31 @@
/*
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
* SPDX-License-Identifier: Zlib
*/
struct VertexInput
{
float3 position : TEXCOORD0;
};
cbuffer VertexUniform : register(b0, space1)
{
float4x4 viewproj : packoffset(c0);
};
struct Vertex2Pixel
{
float4 position : SV_Position;
};
Vertex2Pixel VertexMain(VertexInput input)
{
Vertex2Pixel output;
output.position = mul(viewproj, float4(input.position, 1.0));
return output;
}
half4 PixelMain(Vertex2Pixel input) : SV_Target0
{
return half4(1.0, 1.0, 1.0, 1.0);
}

34
src/shaders/lesson3.hlsl Normal file
View File

@@ -0,0 +1,34 @@
/*
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
* SPDX-License-Identifier: Zlib
*/
struct VertexInput
{
float3 position : TEXCOORD0;
float4 color : TEXCOORD1;
};
cbuffer VertexUniform : register(b0, space1)
{
float4x4 viewproj : packoffset(c0);
};
struct Vertex2Pixel
{
float4 position : SV_Position;
half4 color : COLOR0;
};
Vertex2Pixel VertexMain(VertexInput input)
{
Vertex2Pixel output;
output.position = mul(viewproj, float4(input.position, 1.0));
output.color = half4(input.color);
return output;
}
half4 PixelMain(Vertex2Pixel input) : SV_Target0
{
return input.color;
}

37
src/shaders/lesson6.hlsl Normal file
View File

@@ -0,0 +1,37 @@
/*
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
* SPDX-License-Identifier: Zlib
*/
struct VertexInput
{
float3 position : TEXCOORD0;
float2 texcoord : TEXCOORD1;
};
cbuffer VertexUniform : register(b0, space1)
{
float4x4 viewproj : packoffset(c0);
};
struct Vertex2Pixel
{
float4 position : SV_Position;
float2 texcoord : TEXCOORD0;
};
Vertex2Pixel VertexMain(VertexInput input)
{
Vertex2Pixel output;
output.position = mul(viewproj, float4(input.position, 1.0));
output.texcoord = input.texcoord;
return output;
}
Texture2D<half4> Texture : register(t0, space2);
SamplerState Sampler : register(s0, space2);
half4 PixelMain(Vertex2Pixel input) : SV_Target0
{
return Texture.Sample(Sampler, input.texcoord);
}

59
src/shaders/lesson7.hlsl Normal file
View File

@@ -0,0 +1,59 @@
/*
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
* SPDX-License-Identifier: Zlib
*/
struct VertexInput
{
float3 position : TEXCOORD0;
float2 texcoord : TEXCOORD1;
float3 normal : TEXCOORD2;
};
cbuffer VertexUniform : register(b0, space1)
{
float4x4 modelView : packoffset(c0);
float4x4 projection : packoffset(c4);
};
cbuffer LightUniform : register(b1, space1)
{
float4 lightAmbient : packoffset(c0);
float4 lightDiffuse : packoffset(c1);
float4 lightPosition : packoffset(c2);
};
struct Vertex2Pixel
{
float4 position : SV_Position;
float2 texcoord : TEXCOORD0;
half4 color : COLOR0;
};
Vertex2Pixel VertexMain(VertexInput input)
{
const float4 position = mul(modelView, float4(input.position, 1.0));
const float3 normal = normalize(mul(modelView, float4(input.normal, 0.0))).xyz;
const float3 lightVec = lightPosition.xyz - position.xyz;
const float lightDist2 = dot(lightVec, lightVec);
const float3 dir = rsqrt(lightDist2) * lightVec;
const float lambert = max(0.0, dot(normal, dir));
const half3 ambient = 0.04 + 0.2 * half3(lightAmbient.rgb);
const half3 diffuse = 0.8 * half3(lightDiffuse.rgb);
Vertex2Pixel output;
output.position = mul(projection, position);
output.texcoord = input.texcoord;
output.color = half4(ambient + lambert * diffuse, 1.0);
return output;
}
Texture2D<half4> Texture : register(t0, space2);
SamplerState Sampler : register(s0, space2);
half4 PixelMain(Vertex2Pixel input) : SV_Target0
{
return input.color * Texture.Sample(Sampler, input.texcoord);
}

40
src/shaders/lesson8.hlsl Normal file
View File

@@ -0,0 +1,40 @@
/*
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
* SPDX-License-Identifier: Zlib
*/
struct VertexInput
{
float3 position : TEXCOORD0;
float2 texcoord : TEXCOORD1;
};
cbuffer VertexUniform : register(b0, space1)
{
float4x4 viewproj : packoffset(c0);
float4 color : packoffset(c4);
};
struct Vertex2Pixel
{
float4 position : SV_Position;
float2 texcoord : TEXCOORD0;
half4 color : COLOR0;
};
Vertex2Pixel VertexMain(VertexInput input)
{
Vertex2Pixel output;
output.position = mul(viewproj, float4(input.position, 1.0));
output.texcoord = input.texcoord;
output.color = half4(color);
return output;
}
Texture2D<half4> Texture : register(t0, space2);
SamplerState Sampler : register(s0, space2);
half4 PixelMain(Vertex2Pixel input) : SV_Target0
{
return input.color * Texture.Sample(Sampler, input.texcoord);
}