/* Copyright (c) V-Nova International Limited 2026. All rights reserved.
 * This software is licensed under the BSD-3-Clause-Clear License by V-Nova Limited.
 * No patent licenses are granted under this license. For enquiries about patent licenses,
 * please contact legal@v-nova.com.
 * The LCEVCdec software is a stand-alone project and is NOT A CONTRIBUTION to any other project.
 * If the software is incorporated into another project, THE TERMS OF THE BSD-3-CLAUSE-CLEAR LICENSE
 * AND THE ADDITIONAL LICENSING INFORMATION CONTAINED IN THIS FILE MUST BE MAINTAINED, AND THE
 * SOFTWARE DOES NOT AND MUST NOT ADOPT THE LICENSE OF THE INCORPORATING PROJECT. However, the
 * software may be incorporated into a project under a compatible license provided the requirements
 * of the BSD-3-Clause-Clear license are respected, and V-Nova Limited remains
 * licensor of the software ONLY UNDER the BSD-3-Clause-Clear license (not the compatible license).
 * ANY ONWARD DISTRIBUTION, WHETHER STAND-ALONE OR AS PART OF ANY OTHER PROJECT, REMAINS SUBJECT TO
 * THE EXCLUSION OF PATENT LICENSES PROVISION OF THE BSD-3-CLAUSE-CLEAR LICENSE. */

// Vulkan input format conversion shader

#version 450

#include "specialization_ids.h"

// Specialized workgropup size
layout(local_size_x_id = SPECID_LOCAL_SIZE_X,
       local_size_y_id = SPECID_LOCAL_SIZE_Y,
       local_size_z_id = SPECID_LOCAL_SIZE_Z) in;

// Specialized build time configuration
layout (constant_id = SPECID_8_BIT) const bool bit8 = false;
layout (constant_id = SPECID_NV12) const bool nv12 = false;

layout(std430, set = 0, binding = 0) buffer restrict readonly SrcImageBuffer {
    uint samples[];
} src;

layout(std430, set = 0, binding = 1) buffer restrict writeonly DstImageBuffer {
    uint samples[];
} dst;

layout(push_constant) uniform consts {
    int width;
    int height;
    int containerStrideIn;
    int containerOffsetIn;
    int containerStrideOut;
    int containerOffsetOut;
    int containerStrideV; // used to check for nv12 and as input or output stride for V-plane
    int containerOffsetV; // used with nv12 as input or output offset for V-plane
    int shift;     // 5 for 10bit, 3 for 12bit, and 1 for 14bit
    int sharpenStrength; // 0 disables sharpening, otherwise U16 strength (in-direction: unused)
    int nv21Swap; // (in-direction: unused; declared for layout parity with convert_out.comp)
};

#include "common.glsl"

ivec2 readSrc(uint x, uint y) {
    return  unpack16bitInternals(src.samples[containerOffsetIn + y * containerStrideIn + x]);
}

void writeDst(uint x, uint y, ivec2 pels)
{
    dst.samples[containerOffsetOut + y * containerStrideOut + x]  = pack16bitInternals(pels);
}

int loadInternalValue(int px, int py)
{
    const uint packed = src.samples[uint(containerOffsetIn + (px >> 1) + py * containerStrideIn)];
    const ivec2 vals = unpack16bitInternals(packed);
    return ((px & 1) == 0) ? vals.x : vals.y;
}

int toOutputValue(int internalVal)
{
    const int shiftN = bit8 ? 7 : shift;
    const int halfv = (1 << shiftN) / 2;
    const int maxv = 32767 >> shiftN;
    return clamp(((internalVal + 0x4000 + halfv) >> shiftN), 0, maxv);
}

int sharpenPixel(int px, int py)
{
    int center = toOutputValue(loadInternalValue(px, py));
    if (sharpenStrength == 0) {
        return center;
    }
    if (px <= 0 || px + 1 >= width || py <= 0 || py + 1 >= height) {
        return center;
    }
    const int left = toOutputValue(loadInternalValue(px - 1, py));
    const int right = toOutputValue(loadInternalValue(px + 1, py));
    const int top = toOutputValue(loadInternalValue(px, py - 1));
    const int bottom = toOutputValue(loadInternalValue(px, py + 1));
    const int weight = (center << 2) - (left + right + top + bottom);
    const int coeff = (sharpenStrength * weight + 32768) >> 16;
    const int maxv = bit8 ? (32767 >> 7) : (32767 >> shift);
    return clamp(center + coeff, 0, maxv);
}

void main()
{
    const uint x = gl_GlobalInvocationID.x;
    const uint y = gl_GlobalInvocationID.y;

    if(y >= height) {
        return;
    }

    if(x* (bit8?4:2) >= width) {
        return;
    }

    if (bit8) {
        if (nv12 && containerStrideV != 0) {
            if (4 * x < width) {
                const ivec4 pixels = toInternalFrom8bit(unpack8bit(src.samples[containerOffsetIn + x + y * containerStrideIn]));
                dst.samples[containerOffsetOut + x + y * containerStrideOut] = pack16bitInternals(ivec2(pixels.x, pixels.z));
                dst.samples[containerOffsetV + x + y * containerStrideV] = pack16bitInternals(ivec2(pixels.y, pixels.w));
            }
        } else {
            const ivec4 pixels = toInternalFrom8bit(unpack8bit(src.samples[containerOffsetIn + x + y * containerStrideIn]));
            dst.samples[containerOffsetOut + 2 * x + y * containerStrideOut] = pack16bitInternals(pixels.xy);
            if(4 * x + 2 < width) {
                dst.samples[containerOffsetOut + 2 * x + 1 + y * containerStrideOut] = pack16bitInternals(pixels.zw);
            }
        }
    } else {
        const ivec2 pixels = toInternalFrom16bit(unpack16bit(src.samples[containerOffsetIn + x + y * containerStrideIn]), shift);
        dst.samples[containerOffsetOut + x + y * containerStrideOut] = pack16bitInternals(pixels);
    }

}
