equirect_to_cube.slang

cross platform rendering playground

src/shaders/ibl/equirect_to_cube.slang

1.05 KB
#include "shared.h"
#include "renderer_types.h"
#include "cubemap.slang"

[[vk::push_constant]]
IblConstants pc;

// convention: panorama center (u = 0.5) faces +Y, u increases toward +X
// sources with a different forward requires re-yawing, not editing this
// no clue if equirects are standardized at all
// or if I'll need to re-yaw for each new source
vec2 SampleSphericalMap(vec3 dir) {
    dir = normalize(dir);
    vec2 uv;
    uv.x = atan2(dir.x, dir.y) * 0.15915494309 + 0.5;
    uv.y = 0.5 - asin(dir.z) * 0.31830988618;
    return uv;
}

[shader("vertex")]
VtxOut vsMain(u32 vertexID: SV_VertexID) {
    VtxOut o;
    vec2 positions[3] = { vec2(-1.0, -1.0), vec2(3.0, -1.0), vec2(-1.0, 3.0) };
    vec2 pos = positions[vertexID];
    o.clip_pos = vec4(pos, 0.0, 1.0);
    o.uv = pos * 0.5 + 0.5;
    return o;
}

[shader("fragment")]
vec4 fsMain(VtxOut input) : SV_Target0 {
    vec3 dir = CubemapFaceDirection(pc.faceidx, input.uv);
    vec2 hdrUV = SampleSphericalMap(dir);
    vec3 color = pc.hdrTexture.Sample(pc.samp, hdrUV).rgb;
    return vec4(color, 1.0);
}