shaders: Implement GLSL shaders for lessons 2-9

This commit is contained in:
2025-06-15 23:30:33 +10:00
parent 1ed13961e8
commit cdf06bfa26
19 changed files with 269 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.

Binary file not shown.

Binary file not shown.

View File

@@ -211,7 +211,7 @@ def compile_dxbc_shader(shader: Shader, type: str, flags: list[str] | None = Non
def compile_shaders() -> None:
build_spirv = False
build_spirv = True
build_metal = True
build_dxil = False
build_dxbc = False

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

@@ -0,0 +1,31 @@
/*
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
* SPDX-License-Identifier: Zlib
*/
#version 450
#ifdef VERTEX
layout(location = 0) in vec3 i_position;
layout(set = 1, binding = 0) uniform UBO
{
mat4 u_viewproj;
};
void main()
{
gl_Position = u_viewproj * vec4(i_position, 1.0);
}
#else
layout(location = 0) out vec4 o_color;
void main()
{
o_color = vec4(1.0);
}
#endif

37
src/shaders/lesson3.glsl Normal file
View File

@@ -0,0 +1,37 @@
/*
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
* SPDX-License-Identifier: Zlib
*/
#version 450
#ifdef VERTEX
layout(location = 0) in vec3 i_position;
layout(location = 1) in vec4 i_color;
layout(location = 0) out vec4 v_color;
layout(set = 1, binding = 0) uniform UBO
{
mat4 u_viewproj;
};
void main()
{
v_color = i_color;
gl_Position = u_viewproj * vec4(i_position, 1.0);
}
#else
layout(location = 0) in vec4 v_color;
layout(location = 0) out vec4 o_color;
void main()
{
o_color = v_color;
}
#endif

39
src/shaders/lesson6.glsl Normal file
View File

@@ -0,0 +1,39 @@
/*
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
* SPDX-License-Identifier: Zlib
*/
#version 450
#ifdef VERTEX
layout(location = 0) in vec3 i_position;
layout(location = 1) in vec2 i_texcoord;
layout(location = 0) out vec2 v_texcoord;
layout(set = 1, binding = 0) uniform UBO
{
mat4 u_viewproj;
};
void main()
{
v_texcoord = i_texcoord;
gl_Position = u_viewproj * vec4(i_position, 1.0);
}
#else
layout(location = 0) in vec2 v_texcoord;
layout(location = 0) out vec4 o_color;
layout(set = 2, binding = 0) uniform sampler2D u_texture;
void main()
{
o_color = texture(u_texture, v_texcoord);
}
#endif

62
src/shaders/lesson7.glsl Normal file
View File

@@ -0,0 +1,62 @@
/*
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
* SPDX-License-Identifier: Zlib
*/
#version 450
#ifdef VERTEX
layout(location = 0) in vec3 i_position;
layout(location = 1) in vec2 i_texcoord;
layout(location = 2) in vec3 i_normal;
layout(location = 0) out vec2 v_texcoord;
layout(location = 1) out vec4 v_color;
layout(set = 1, binding = 0) uniform UBO
{
mat4 u_modelView;
mat4 u_projection;
};
layout(set = 1, binding = 1) uniform Light
{
vec4 u_ambient;
vec4 u_diffuse;
vec4 u_position;
};
void main()
{
const vec4 position = u_modelView * vec4(i_position, 1.0);
const vec3 normal = normalize(u_modelView * vec4(i_normal, 0.0)).xyz;
const vec3 lightVec = u_position.xyz - position.xyz;
const float lightDest2 = dot(lightVec, lightVec);
const vec3 dir = inversesqrt(lightDest2) * lightVec;
const float lambert = max(0.0, dot(normal, dir));
const vec3 ambient = 0.04 + 0.2 * u_ambient.rgb;
const vec3 diffuse = 0.8 * u_diffuse.rgb;
v_texcoord = i_texcoord;
v_color = vec4(ambient + lambert * diffuse, 1.0);
gl_Position = u_projection * position;
}
#else
layout(location = 0) in vec2 v_texcoord;
layout(location = 1) in vec4 v_color;
layout(location = 0) out vec4 o_color;
layout(set = 2, binding = 0) uniform sampler2D u_texture;
void main()
{
o_color = v_color * texture(u_texture, v_texcoord);
}
#endif

43
src/shaders/lesson8.glsl Normal file
View File

@@ -0,0 +1,43 @@
/*
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
* SPDX-License-Identifier: Zlib
*/
#version 450
#ifdef VERTEX
layout(location = 0) in vec3 i_position;
layout(location = 1) in vec2 i_texcoord;
layout(location = 0) out vec2 v_texcoord;
layout(location = 1) out vec4 v_color;
layout(set = 1, binding = 0) uniform UBO
{
mat4 u_viewproj;
vec4 u_color;
};
void main()
{
v_texcoord = i_texcoord;
v_color = u_color;
gl_Position = u_viewproj * vec4(i_position, 1.0);
}
#else
layout(location = 0) in vec2 v_texcoord;
layout(location = 1) in vec4 v_color;
layout(location = 0) out vec4 o_color;
layout(set = 2, binding = 0) uniform sampler2D u_texture;
void main()
{
o_color = v_color * texture(u_texture, v_texcoord);
}
#endif

56
src/shaders/lesson9.glsl Normal file
View File

@@ -0,0 +1,56 @@
/*
* SPDX-FileCopyrightText: (C) 2025 a dinosaur
* SPDX-License-Identifier: Zlib
*/
#version 450
#ifdef VERTEX
layout(location = 0) in vec3 i_position;
layout(location = 1) in vec2 i_texcoord;
layout(location = 0) out vec2 v_texcoord;
layout(location = 1) out vec4 v_color;
layout(set = 1, binding = 0) uniform UBO
{
mat4 u_projection;
vec4 u_color;
};
struct Instance
{
mat4 model;
vec4 color;
};
layout(std140, set = 0, binding = 0) readonly buffer InstanceData
{
Instance instances[];
} instanceData;
void main()
{
const Instance instance = instanceData.instances[gl_InstanceIndex];
v_texcoord = i_texcoord;
v_color = instance.color;
gl_Position = u_projection * instance.model * vec4(i_position, 1.0);
}
#else
layout(location = 0) in vec2 v_texcoord;
layout(location = 1) in vec4 v_color;
layout(location = 0) out vec4 o_color;
layout(set = 2, binding = 0) uniform sampler2D u_texture;
void main()
{
o_color = v_color * texture(u_texture, v_texcoord);
}
#endif