// Laser Display
#version 450


// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
layout(set = 0, binding = 150, std140) uniform Vk3DParams
{
    vec4 stereo;
    vec4 custom_params;
} vk3d_params;

layout(set = 1, binding = 0, std140) uniform _instanceUniforms_ubo
{
    vec4 modelmatrixx;
    vec4 modelmatrixy;
    vec4 modelmatrixz;
    vec4 modelmatrixw;
    vec4 select;
    vec4 lkw_ammo;
    vec4 lkw_charge;
    vec4 lkw_readytofire;
    vec4 lkw_aimcuttable;
    vec4 colorscale;
} _instanceUniforms;

layout(set = 0, binding = 0, std140) uniform _viewUniforms_ubo
{
    vec4 viewprojectionmatrixx;
    vec4 viewprojectionmatrixy;
    vec4 viewprojectionmatrixz;
    vec4 viewprojectionmatrixw;
    vec4 time;
} _viewUniforms;

layout(push_constant, std430) uniform PushConstants
{
    int lodLevel;
    int modelIndex;
} pushConstants;

layout(set = 2, binding = 1) uniform sampler2D samp_transmap1;
layout(set = 2, binding = 2) uniform sampler2D samp_transmap;
layout(set = 2, binding = 3) uniform sampler2D samp_transmap2;

layout(location = 0) in vec4 in_Position;
layout(location = 2) out vec4 vofi_TexCoord0;
layout(location = 1) in vec2 in_TexCoord;

mat4 MatrixMul(mat4 m1, mat4 m2)
{
    return m1 * m2;
}

float dot4(vec4 a, vec4 b)
{
    return dot(a, b);
}

float dot4(vec2 a, vec4 b)
{
    return dot(vec4(a, 0.0, 1.0), b);
}

void main()
{
    vec4 localPosition = vec4(in_Position.xyz, 1.0);
    mat4 modelMatrix = mat4(vec4(_instanceUniforms.modelmatrixx), vec4(_instanceUniforms.modelmatrixy), vec4(_instanceUniforms.modelmatrixz), vec4(_instanceUniforms.modelmatrixw));
    mat4 viewProjectionMatrix = mat4(vec4(_viewUniforms.viewprojectionmatrixx), vec4(_viewUniforms.viewprojectionmatrixy), vec4(_viewUniforms.viewprojectionmatrixz), vec4(_viewUniforms.viewprojectionmatrixw));
    mat4 param = modelMatrix;
    mat4 param_1 = viewProjectionMatrix;
    mat4 mvpMatrixT = MatrixMul(param, param_1);
    vec4 mvpMatrixX = mvpMatrixT[0];
    vec4 mvpMatrixY = mvpMatrixT[1];
    vec4 mvpMatrixZ = mvpMatrixT[2];
    vec4 mvpMatrixW = mvpMatrixT[3];
    vec4 param_2 = localPosition;
    vec4 param_3 = mvpMatrixX;
    gl_Position.x = dot4(param_2, param_3);
    vec4 param_4 = localPosition;
    vec4 param_5 = mvpMatrixY;
    gl_Position.y = dot4(param_4, param_5);
    vec4 param_6 = localPosition;
    vec4 param_7 = mvpMatrixZ;
    gl_Position.z = dot4(param_6, param_7);
    vec4 param_8 = localPosition;
    vec4 param_9 = mvpMatrixW;
    gl_Position.w = dot4(param_8, param_9);
    vec2 param_10 = in_TexCoord;
    vec4 param_11 = vec4(1.0, 0.0, 0.0, 0.0);
    vofi_TexCoord0.x = dot4(param_10, param_11);
    vec2 param_12 = in_TexCoord;
    vec4 param_13 = vec4(0.0, 1.0, 0.0, 0.0);
    vofi_TexCoord0.y = 1.0 - dot4(param_12, param_13);
    if (_instanceUniforms.select.x == 2.0)
    {
        float fTimeX = fract(_viewUniforms.time.x * _instanceUniforms.select.w);
        float fTimeY = fract((_viewUniforms.time.x * 0.25) * _instanceUniforms.select.w);
        float panX = floor(fTimeX * _instanceUniforms.select.y) / _instanceUniforms.select.y;
        float panY = floor(fTimeY * _instanceUniforms.select.z) / _instanceUniforms.select.z;
        vec2 param_14 = in_TexCoord;
        vec4 param_15 = vec4(1.0 / _instanceUniforms.select.y, 0.0, 0.0, panX);
        vofi_TexCoord0.x = dot4(param_14, param_15);
        vec2 param_16 = in_TexCoord;
        vec4 param_17 = vec4(0.0, 1.0 / _instanceUniforms.select.z, 0.0, panY);
        vofi_TexCoord0.y = dot4(param_16, param_17);
    }
    gl_Position.y = -gl_Position.y;

// Vk3DVision (http://3dsurroundgaming.com/) - Stereo Injection
    if(vk3d_params.custom_params.z != 0)
    {
        if (gl_Position.w > vk3d_params.custom_params.z)
            gl_Position.x += vk3d_params.stereo.x * (gl_Position.w - vk3d_params.stereo.y); 
        else
            gl_Position.x += vk3d_params.stereo.x * (gl_Position.w - vk3d_params.custom_params.w) *0.75;
    }
    else
    {
        gl_Position.x += vk3d_params.stereo.x * (gl_Position.w - vk3d_params.stereo.y); 
    }
}