#ifndef PALETTE_GLSL_
#define PALETTE_GLSL_

#include "common.glsl"
#include "svo.glsl"
#include "worldFrameData.glsl"

#ifndef VOXEL_PALETTE_DESCSET
#define VOXEL_PALETTE_DESCSET GLSL_TERRAIN_VOXEL_PALETTE_DESCRIPTOR_SET
#endif

struct Voxel
{
    uint color;
    uint lightSpec;
    uint bias;
    uint refract;
    uint frameDuration;
};

layout(
    std430,
    set = VOXEL_PALETTE_DESCSET,
    binding = 0
) readonly buffer PaletteBuffer
{
    Voxel palette[];
} paletteBuffer;

Voxel readVoxel(VoxelRef ref)
{
    uint numFrames = 1u << GLSL_SVO_VOXEL_FRAMEPOW_GET(ref);
    uint base = ref & GLSL_SVO_VOXEL_INDEX_MASK;
    uint frameDuration = paletteBuffer.palette[base].frameDuration;
    uint frame = (uFrameData.ticks / frameDuration) & (numFrames - 1);

    return paletteBuffer.palette[base + frame];
}

#endif