Mikemon/assets/shader/basic_vertex_shader.slang
2025-02-28 16:05:51 +01:00

66 lines
1.3 KiB
Plaintext

struct VertexShaderInput {
// Per Vertex
float4 pos : VERTEX_POSITION;
float2 uv : UV_VERTEX;
uint vertex_id : SV_VertexID;
// Per Instance
float4 pos_size : INSTANCE_POSITION_SIZE;
uint tile_type : TILE_TYPE;
float4 uv0uv1 : UV_INSTANCE;
float4 uv2uv3 : UV_INSTANCE2;
};
[[vk::binding(0, 1)]]
cbuffer constants {
float aspect_ratio;
};
struct VertexShaderOutput {
float4 pos : SV_POSITION;
float2 uv : COORDINATES;
};
[shader("vertex")]
VertexShaderOutput main(VertexShaderInput input) {
VertexShaderOutput output;
float3x3 coord_sys = {
2, 0, -1,
0, -2, 1,
0, 0, 1
};
input.pos_size.xy = mul(coord_sys, float3(input.pos_size.xy, 1)).xy;
float3x3 pos = {
1, 0, input.pos_size.x,
0, 1, input.pos_size.y,
0, 0, 1
};
float3x3 size = {
input.pos_size.z, 0, 0,
0, input.pos_size.w, 0,
0, 0, 1
};
output.pos.xy = mul(pos, mul(size, input.pos.xyz)).xy;
float correction_factor = aspect_ratio / (16.0 / 9.0);
if(correction_factor < 1)
output.pos.y *= correction_factor;
else
output.pos.x /= correction_factor;
output.pos.zw = float2(0, 1);
switch(input.vertex_id) {
case 0: output.uv = input.uv0uv1.xy; break;
case 1: output.uv = input.uv0uv1.zw; break;
case 2: output.uv = input.uv2uv3.xy; break;
case 3: output.uv = input.uv2uv3.zw; break;
}
return output;
}