heyx3 / Bplus.jl

A modern OpenGL 4.6 rendering framework, written in Julia.
Other
72 stars 3 forks source link

Define structs to unambiguously describe the packed depth/stencil formats #26

Open heyx3 opened 1 year ago

heyx3 commented 1 year ago

This existed in the original C++ version of B+. You can find it if you dig really deep into the history of the code-base, but I pasted the relevant C++ code below so you don't have to do that.

It might make sense to define these as primitive types, like I did for GL handles.

struct Unpacked_Depth24uStencil8u
{
    unsigned Depth : 24;
    unsigned Stencil : 8;
};
struct Unpacked_Depth32fStencil8u
{
    float   Depth;
    uint8_t Stencil;
    Unpacked_Depth32fStencil8u(float depth, uint8_t stencil)
        : Depth(depth), Stencil(stencil) { }
};

inline uint32_t Pack_DepthStencil(Unpacked_Depth24uStencil8u depthStencilValues)
{
    return (((uint32_t)depthStencilValues.Depth) << 8) |
           depthStencilValues.Stencil;
}
inline uint64_t Pack_DepthStencil(Unpacked_Depth32fStencil8u depthStencilValues)
{
    uint32_t depthAsInt = Reinterpret<float, uint32_t>(depthStencilValues.Depth),
             stencil = (uint32_t)depthStencilValues.Stencil;

    uint64_t result = 0;
    result |= depthAsInt;
    result <<= 32;
    result |= stencil;

    return result;
}

inline Unpacked_Depth24uStencil8u Unpack_DepthStencil(uint32_t packed)
{
    return {
             ((packed & 0xffffff00) >> 8),
             (packed & 0x000000ff)
     };
}
inline Unpacked_Depth32fStencil8u Unpack_DepthStencil(uint64_t packed)
{
    uint32_t depthAsInt = (uint32_t)((packed & 0xffffffff00000000) >> 32);
    uint8_t stencil = (uint8_t)(packed & 0x00000000000000ff);
    return {
               Reinterpret<uint32_t, float>(depthAsInt),
               stencil
           };
}