braille_xr/sk_demo/res/shaders/dofdev.hlsli
2024-01-28 08:53:21 -05:00

82 lines
No EOL
2.1 KiB
HLSL

#ifndef _DOFDEV_HLSLI
#define _DOFDEV_HLSLI
float sum(float4 v) { // *used in firball.hlsl
return dot(v, float4(1,1,1,1));
}
// color space conversions
float4 gamma_linear(float4 gamma_col) {
return float4(pow(gamma_col.rgb, 0.454545), gamma_col.a);
}
float4 linear_gamma(float4 linear_col) {
return float4(pow(linear_col.rgb, 2.2), linear_col.a);
}
struct psOut {
float4 color : SV_TARGET;
float depth : SV_Depth;
};
// oriel
struct oriel_data {
float4x4 m4;
float4x4 m4_inv;
float3 size;
float pad;
};
cbuffer oriel_buffer : register(b3) {
oriel_data oriels[455]; // UINT16_MAX(65535) / sizeof(oriel 144) = 455
};
float2 face_dist(oriel_data oriel, float3 cam_pos, float3 dir) {
float3 box = oriel.size / 2.0;
float3 boxMin = -box;
float3 boxMax = box;
float3 tMin = (boxMin - cam_pos) / dir;
float3 tMax = (boxMax - cam_pos) / dir;
float3 t1 = min(tMin, tMax);
float3 t2 = max(tMin, tMax);
float tNear = max(max(t1.x, t1.y), t1.z);
float tFar = min(min(t2.x, t2.y), t2.z);
return float2(tNear, tFar);
};
float oriel(int id, float depth, int view_id, float3 world_pos) {
if (id == -1) return depth; // optimal?
oriel_data oriel = oriels[id];
float3 cam_pos = sk_camera_pos[view_id].xyz;
cam_pos = mul(float4(cam_pos, 1), oriel.m4).xyz;
world_pos = mul(float4(world_pos, 1), oriel.m4).xyz;
float3 dir = normalize(world_pos - cam_pos);
float2 faces = face_dist(oriel, cam_pos, dir);
clip(faces.y - faces.x); // no intersection
// is a distance call cheaper than just transforming the input depth?
float dist = distance(cam_pos, world_pos);
clip(dist - faces.x); // near clip
// far depth *flatten
float pad = 0.01; // 1cm ?
if (dist > faces.y) {
float bleed = (dist - faces.y) / (100.0 - faces.y);
dist = faces.y + (bleed * pad);
}
// gotta be a better way to do this than having two matrices...
float3 new_pos = mul(float4(cam_pos + (dir * dist), 1), oriel.m4_inv).xyz;
float4 og = mul(float4(new_pos, 1), sk_viewproj[view_id]);
depth = (og * rcp(og.w)).z;
return depth;
}
// !!!
// for the build process to include new changes:
// you need to edit another shader that uses it
#endif