Files
LNXSDK/leenkx/Shaders/std/gbuffer.glsl

189 lines
5.0 KiB
Plaintext
Raw Normal View History

2025-01-22 16:18:30 +01:00
#ifndef _GBUFFER_GLSL_
#define _GBUFFER_GLSL_
2026-02-24 11:44:01 -08:00
vec2 octahedronWrap(vec2 v) {
2025-01-22 16:18:30 +01:00
return (1.0 - abs(v.yx)) * (vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0));
}
2026-02-24 11:44:01 -08:00
vec3 getNor(vec2 enc) {
2025-01-22 16:18:30 +01:00
vec3 n;
n.z = 1.0 - abs(enc.x) - abs(enc.y);
n.xy = n.z >= 0.0 ? enc.xy : octahedronWrap(enc.xy);
n = normalize(n);
return n;
}
2026-02-24 11:44:01 -08:00
vec3 getPosView(vec3 viewRay, float depth, vec2 cameraProj) {
2025-01-22 16:18:30 +01:00
float linearDepth = cameraProj.y / (cameraProj.x - depth);
//float linearDepth = cameraProj.y / ((depth * 0.5 + 0.5) - cameraProj.x);
return viewRay * linearDepth;
}
2026-02-24 11:44:01 -08:00
vec3 getPos(vec3 eye, vec3 eyeLook, vec3 viewRay, float depth, vec2 cameraProj) {
2025-01-22 16:18:30 +01:00
// eyeLook, viewRay should be normalized
float linearDepth = cameraProj.y / ((depth * 0.5 + 0.5) - cameraProj.x);
float viewZDist = dot(eyeLook, viewRay);
vec3 wposition = eye + viewRay * (linearDepth / viewZDist);
return wposition;
}
2026-02-24 11:44:01 -08:00
vec3 getPosNoEye(vec3 eyeLook, vec3 viewRay, float depth, vec2 cameraProj) {
2025-01-22 16:18:30 +01:00
// eyeLook, viewRay should be normalized
float linearDepth = cameraProj.y / ((depth * 0.5 + 0.5) - cameraProj.x);
float viewZDist = dot(eyeLook, viewRay);
vec3 wposition = viewRay * (linearDepth / viewZDist);
return wposition;
}
#if defined(HLSL) || defined(METAL)
2026-02-24 11:44:01 -08:00
vec3 getPos2(mat4 invVP, float depth, vec2 coord) {
2025-01-22 16:18:30 +01:00
coord.y = 1.0 - coord.y;
#else
2026-02-24 11:44:01 -08:00
vec3 getPos2(mat4 invVP, float depth, vec2 coord) {
2025-01-22 16:18:30 +01:00
#endif
vec4 pos = vec4(coord * 2.0 - 1.0, depth, 1.0);
pos = invVP * pos;
pos.xyz /= pos.w;
return pos.xyz;
}
#if defined(HLSL) || defined(METAL)
2026-02-24 11:44:01 -08:00
vec3 getPosView2(mat4 invP, float depth, vec2 coord) {
2025-01-22 16:18:30 +01:00
coord.y = 1.0 - coord.y;
#else
2026-02-24 11:44:01 -08:00
vec3 getPosView2(mat4 invP, float depth, vec2 coord) {
2025-01-22 16:18:30 +01:00
#endif
vec4 pos = vec4(coord * 2.0 - 1.0, depth, 1.0);
pos = invP * pos;
pos.xyz /= pos.w;
return pos.xyz;
}
#if defined(HLSL) || defined(METAL)
2026-02-24 11:44:01 -08:00
vec3 getPos2NoEye(vec3 eye, mat4 invVP, float depth, vec2 coord) {
2025-01-22 16:18:30 +01:00
coord.y = 1.0 - coord.y;
#else
2026-02-24 11:44:01 -08:00
vec3 getPos2NoEye(vec3 eye, mat4 invVP, float depth, vec2 coord) {
2025-01-22 16:18:30 +01:00
#endif
vec4 pos = vec4(coord * 2.0 - 1.0, depth, 1.0);
pos = invVP * pos;
pos.xyz /= pos.w;
return pos.xyz - eye;
}
2026-02-24 11:44:01 -08:00
float packFloat(float f1, float f2) {
2025-01-22 16:18:30 +01:00
return floor(f1 * 100.0) + min(f2, 1.0 - 1.0 / 100.0);
}
2026-02-24 11:44:01 -08:00
vec2 unpackFloat(float f) {
2025-01-22 16:18:30 +01:00
return vec2(floor(f) / 100.0, fract(f));
}
2026-02-24 11:44:01 -08:00
float packFloat2(float f1, float f2) {
2025-01-22 16:18:30 +01:00
// Higher f1 = less precise f2
return floor(f1 * 255.0) + min(f2, 1.0 - 1.0 / 100.0);
}
2026-02-24 11:44:01 -08:00
vec2 unpackFloat2(float f) {
2025-01-22 16:18:30 +01:00
return vec2(floor(f) / 255.0, fract(f));
}
2026-02-24 11:44:01 -08:00
vec4 encodeRGBM(vec3 rgb) {
2025-01-22 16:18:30 +01:00
const float maxRange = 6.0;
float maxRGB = max(rgb.x, max(rgb.g, rgb.b));
float m = maxRGB / maxRange;
m = ceil(m * 255.0) / 255.0;
return vec4(rgb / (m * maxRange), m);
}
2026-02-24 11:44:01 -08:00
vec3 decodeRGBM(vec4 rgbm) {
2025-01-22 16:18:30 +01:00
const float maxRange = 6.0;
return rgbm.rgb * rgbm.a * maxRange;
}
vec2 signNotZero(vec2 v)
{
return vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0);
}
vec2 encode_oct(vec3 v)
{
// Project the sphere onto the octahedron, and then onto the xy plane
vec2 p = v.xy * (1.0 / (abs(v.x) + abs(v.y) + abs(v.z)));
// Reflect the folds of the lower hemisphere over the diagonals
return (v.z <= 0.0) ? ((1.0 - abs(p.yx)) * signNotZero(p)) : p;
}
vec3 decode_oct(vec2 e)
{
vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y));
if (v.z < 0) v.xy = (1.0 - abs(v.yx)) * signNotZero(v.xy);
return normalize(v);
}
uint encNor(vec3 n) {
ivec3 nor = ivec3(n * 255.0f);
uvec3 norSigns;
norSigns.x = (nor.x >> 5) & 0x04000000;
norSigns.y = (nor.y >> 14) & 0x00020000;
norSigns.z = (nor.z >> 23) & 0x00000100;
nor = abs(nor);
uint val = norSigns.x | (nor.x << 18) | norSigns.y | (nor.y << 9) | norSigns.z | nor.z;
return val;
}
vec3 decNor(uint val) {
uvec3 nor;
nor.x = (val >> 18) & 0x000000ff;
nor.y = (val >> 9) & 0x000000ff;
nor.z = val & 0x000000ff;
uvec3 norSigns;
norSigns.x = (val >> 25) & 0x00000002;
norSigns.y = (val >> 16) & 0x00000002;
norSigns.z = (val >> 7) & 0x00000002;
norSigns = 1 - norSigns;
vec3 normal = vec3(nor) / 255.0f;
normal *= norSigns;
return normal;
}
/**
Packs a float in [0, 1] and an integer in [0..15] into a single 16 bit float value.
**/
2026-02-24 11:44:01 -08:00
float packFloatInt16(float f, uint i) {
2025-01-22 16:18:30 +01:00
const uint numBitFloat = 12;
const float maxValFloat = float((1 << numBitFloat) - 1);
const uint bitsInt = i << numBitFloat;
const uint bitsFloat = uint(f * maxValFloat);
return float(bitsInt | bitsFloat);
}
2026-02-24 11:44:01 -08:00
void unpackFloatInt16(float val, out float f, out uint i) {
2025-01-22 16:18:30 +01:00
const uint numBitFloat = 12;
const float maxValFloat = float((1 << numBitFloat) - 1);
const uint bitsValue = uint(val);
i = bitsValue >> numBitFloat;
f = (bitsValue & ~(0xF << numBitFloat)) / maxValFloat;
}
2026-07-27 12:10:11 -07:00
#ifdef _ExtBRDF
// extended material parameters by material slot ID returns vec4s (28 floats) of extended BRDF parameters
void getMaterialParams(uint matid, out vec4 p0, out vec4 p1, out vec4 p2, out vec4 p3,
out vec4 p4, out vec4 p5, out vec4 p6) {
uint base = matid * 8u;
p0 = materialParams[base];
p1 = materialParams[base + 1u];
p2 = materialParams[base + 2u];
p3 = materialParams[base + 3u];
p4 = materialParams[base + 4u];
p5 = materialParams[base + 5u];
p6 = materialParams[base + 6u];
}
#endif
2025-01-22 16:18:30 +01:00
#endif