Mikemon/assets/shader/basic.slang
2025-03-20 19:45:19 +01:00

67 lines
1.6 KiB
Plaintext

#include "common.slang"
struct VertexShaderInput {
// Per Vertex
uint vertex_id : SV_VertexID;
float3 pos;
// Per Instance
float4 pos_size;
float4 uv0uv1;
float4 uv2uv3;
};
struct VertexShaderOutput {
float4 pos : SV_POSITION;
float2 uv;
};
struct PixelShaderOutput {
float4 color : SV_TARGET;
};
[shader("vertex")]
VertexShaderOutput main_vertex(VertexShaderInput input) {
VertexShaderOutput output;
float4x4 view_matrix = view(float3(camera_x, camera_y, 0), radians(camera_tilt), camera_distance);
float4x4 projection_matrix = projection(radians(fovy_degrees), aspect_ratio, NEAR_PLANE);
float4x4 view_projection_matrix = mul(projection_matrix, view_matrix);
output.pos = mul(view_projection_matrix, float4(input.pos_size.xy + input.pos.xy, 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;
}
[[vk::binding(0, 2)]]
Sampler2D<float4> tex1;
[[vk::binding(0, 3)]]
cbuffer fragment_constants {
float3 tint;
}
[shader("pixel")]
PixelShaderOutput main_fragment(VertexShaderOutput input) {
PixelShaderOutput output;
float2 texture_size;
tex1.GetDimensions(texture_size.x, texture_size.y);
float2 uv = input.uv * texture_size;
uv = floor(uv) + min(fract(uv) / fwidth(uv), 1.0) - 0.5;
uv /= texture_size;
output.color = tex1.Sample(uv);
output.color.rgb *= tint.rgb;
return output;
}