power_play/res/shaders/texture.hlsl

92 lines
2.3 KiB
HLSL

#include "shaders/common.hlsl"
struct vs_instance {
struct xform xf;
float2 uv0;
float2 uv1;
uint tint_srgb;
};
struct ps_input {
DESV(float4, screen_pos, SV_POSITION);
DECL(float2, uv);
DECL(float4, tint_lin);
};
/* ========================== *
* Globals
* ========================== */
SamplerState G_sampler : register(s0);
Texture2D G_Texture : register(t0);
StructuredBuffer<vs_instance> G_instance_buffer : register(t1);
cbuffer constants : register(b0)
{
float4x4 G_projection;
uint G_instance_offset;
};
/* ========================== *
* Vertex shader
* ========================== */
ps_input vs_main(uint instance_id : SV_InstanceID, uint vertex_id : SV_VertexID)
{
// static float2 quad[4] = {
// float2(-0.5f, -0.5f),
// float2( 0.5f, -0.5f),
// float2( 0.5f, 0.5f),
// float2(-0.5f, 0.5f)
// };
static const uint indices[6] = { 0, 1, 2, 2, 3, 0 };
static const float2 vertices[4] = {
float2(-0.5f, -0.5f),
float2( 0.5f, -0.5f),
float2( 0.5f, 0.5f),
float2(-0.5f, 0.5f)
};
vs_instance instance = G_instance_buffer[instance_id + G_instance_offset];
int index = indices[vertex_id];
int top = index == 0 || index == 1;
int right = index == 1 || index == 2;
int bottom = index == 2 || index == 3;
int left = index == 3 || index == 0;
//int top = vertex_id == 0 || vertex_id == 2;
//int left = vertex_id == 0 || vertex_id == 1;
//int bottom = vertex_id == 1 || vertex_id == 3;
//int right = vertex_id == 2 || vertex_id == 3;
float2 vert = vertices[index];
float2 world_pos = xform_mul(instance.xf, vert);
float4 screen_pos = mul(G_projection, float4(world_pos, 0, 1));
float uv_x = instance.uv0.x * left;
uv_x += instance.uv1.x * right;
float uv_y = instance.uv0.y * top;
uv_y += instance.uv1.y * bottom;
ps_input output;
output.screen_pos = screen_pos;
output.uv = float2(uv_x, uv_y);
output.tint_lin = linear_from_srgb32(instance.tint_srgb);
return output;
}
/* ========================== *
* Pixel shader
* ========================== */
float4 ps_main(ps_input input) : SV_TARGET
{
float4 color = G_Texture.Sample(G_sampler, input.uv) * input.tint_lin;
return color;
}