#include <metal_stdlib>

using namespace metal;

template<typename T, size_t N>
struct tint_array {
    const constant T& operator[](size_t i) const constant { return elements[i]; }
    device T& operator[](size_t i) device { return elements[i]; }
    const device T& operator[](size_t i) const device { return elements[i]; }
    thread T& operator[](size_t i) thread { return elements[i]; }
    const thread T& operator[](size_t i) const thread { return elements[i]; }
    threadgroup T& operator[](size_t i) threadgroup { return elements[i]; }
    const threadgroup T& operator[](size_t i) const threadgroup { return elements[i]; }
    T elements[N];
};

struct tint_packed_vec3_f32_array_element {
  /* 0x0000 */ packed_float3 elements;
  /* 0x000c */ tint_array<int8_t, 4> tint_pad;
};

struct GammaTransferParams {
  /* 0x0000 */ float G;
  /* 0x0004 */ float A;
  /* 0x0008 */ float B;
  /* 0x000c */ float C;
  /* 0x0010 */ float D;
  /* 0x0014 */ float E;
  /* 0x0018 */ float F;
  /* 0x001c */ uint padding;
};

struct ExternalTextureParams_tint_packed_vec3 {
  /* 0x0000 */ uint numPlanes;
  /* 0x0004 */ uint doYuvToRgbConversionOnly;
  /* 0x0008 */ tint_array<int8_t, 8> tint_pad_1;
  /* 0x0010 */ float3x4 yuvToRgbConversionMatrix;
  /* 0x0040 */ GammaTransferParams gammaDecodeParams;
  /* 0x0060 */ GammaTransferParams gammaEncodeParams;
  /* 0x0080 */ tint_array<tint_packed_vec3_f32_array_element, 3> gamutConversionMatrix;
  /* 0x00b0 */ float3x2 sampleTransform;
  /* 0x00c8 */ float3x2 loadTransform;
  /* 0x00e0 */ float2 samplePlane0RectMin;
  /* 0x00e8 */ float2 samplePlane0RectMax;
  /* 0x00f0 */ float2 samplePlane1RectMin;
  /* 0x00f8 */ float2 samplePlane1RectMax;
  /* 0x0100 */ uint2 visibleSize;
  /* 0x0108 */ float2 plane1CoordFactor;
};

float3x3 tint_unpack_vec3_in_composite(tint_array<tint_packed_vec3_f32_array_element, 3> in) {
  float3x3 result = float3x3(float3(in[0].elements), float3(in[1].elements), float3(in[2].elements));
  return result;
}

struct ExternalTextureParams {
  uint numPlanes;
  uint doYuvToRgbConversionOnly;
  float3x4 yuvToRgbConversionMatrix;
  GammaTransferParams gammaDecodeParams;
  GammaTransferParams gammaEncodeParams;
  float3x3 gamutConversionMatrix;
  float3x2 sampleTransform;
  float3x2 loadTransform;
  float2 samplePlane0RectMin;
  float2 samplePlane0RectMax;
  float2 samplePlane1RectMin;
  float2 samplePlane1RectMax;
  uint2 visibleSize;
  float2 plane1CoordFactor;
};

ExternalTextureParams tint_unpack_vec3_in_composite_1(ExternalTextureParams_tint_packed_vec3 in) {
  ExternalTextureParams result = {};
  result.numPlanes = in.numPlanes;
  result.doYuvToRgbConversionOnly = in.doYuvToRgbConversionOnly;
  result.yuvToRgbConversionMatrix = in.yuvToRgbConversionMatrix;
  result.gammaDecodeParams = in.gammaDecodeParams;
  result.gammaEncodeParams = in.gammaEncodeParams;
  result.gamutConversionMatrix = tint_unpack_vec3_in_composite(in.gamutConversionMatrix);
  result.sampleTransform = in.sampleTransform;
  result.loadTransform = in.loadTransform;
  result.samplePlane0RectMin = in.samplePlane0RectMin;
  result.samplePlane0RectMax = in.samplePlane0RectMax;
  result.samplePlane1RectMin = in.samplePlane1RectMin;
  result.samplePlane1RectMax = in.samplePlane1RectMax;
  result.visibleSize = in.visibleSize;
  result.plane1CoordFactor = in.plane1CoordFactor;
  return result;
}

uint2 tint_ftou(float2 v) {
  return select(uint2(4294967295u), select(uint2(v), uint2(0u), (v < float2(0.0f))), (v <= float2(4294967040.0f)));
}

float3 gammaCorrection(float3 v, GammaTransferParams params) {
  bool3 const cond = (fabs(v) < float3(params.D));
  float3 const t = (sign(v) * ((params.C * fabs(v)) + params.F));
  float3 const f = (sign(v) * (powr(((params.A * fabs(v)) + params.B), float3(params.G)) + params.E));
  return select(f, t, cond);
}

float4 textureLoadExternal(texture2d<float, access::sample> plane0, texture2d<float, access::sample> plane1, int2 coord, ExternalTextureParams params) {
  uint2 const clampedCoords = min(uint2(coord), params.visibleSize);
  uint2 const plane0_clamped = tint_ftou(rint((params.loadTransform * float3(float2(clampedCoords), 1.0f))));
  float4 color = 0.0f;
  if ((params.numPlanes == 1u)) {
    color = plane0.read(uint2(plane0_clamped), 0).rgba;
  } else {
    uint2 const plane1_clamped = tint_ftou((float2(plane0_clamped) * params.plane1CoordFactor));
    color = float4((float4(plane0.read(uint2(plane0_clamped), 0)[0], plane1.read(uint2(plane1_clamped), 0).rg, 1.0f) * params.yuvToRgbConversionMatrix), 1.0f);
  }
  if ((params.doYuvToRgbConversionOnly == 0u)) {
    color = float4(gammaCorrection(color.rgb, params.gammaDecodeParams), color[3]);
    color = float4((params.gamutConversionMatrix * color.rgb), color[3]);
    color = float4(gammaCorrection(color.rgb, params.gammaEncodeParams), color[3]);
  }
  return color;
}

float4 textureLoad_8acf41(texture2d<float, access::sample> tint_symbol_1, texture2d<float, access::sample> tint_symbol_2, const constant ExternalTextureParams_tint_packed_vec3* const tint_symbol_3) {
  float4 res = textureLoadExternal(tint_symbol_1, tint_symbol_2, int2(1), tint_unpack_vec3_in_composite_1(*(tint_symbol_3)));
  return res;
}

fragment void fragment_main(device float4* tint_symbol_4 [[buffer(0)]], texture2d<float, access::sample> tint_symbol_5 [[texture(0)]], texture2d<float, access::sample> tint_symbol_6 [[texture(1)]], const constant ExternalTextureParams_tint_packed_vec3* tint_symbol_7 [[buffer(2)]]) {
  *(tint_symbol_4) = textureLoad_8acf41(tint_symbol_5, tint_symbol_6, tint_symbol_7);
  return;
}

kernel void compute_main(device float4* tint_symbol_8 [[buffer(0)]], texture2d<float, access::sample> tint_symbol_9 [[texture(0)]], texture2d<float, access::sample> tint_symbol_10 [[texture(1)]], const constant ExternalTextureParams_tint_packed_vec3* tint_symbol_11 [[buffer(2)]]) {
  *(tint_symbol_8) = textureLoad_8acf41(tint_symbol_9, tint_symbol_10, tint_symbol_11);
  return;
}

struct VertexOutput {
  float4 pos;
  float4 prevent_dce;
};

struct tint_symbol {
  float4 prevent_dce [[user(locn0)]] [[flat]];
  float4 pos [[position]];
};

VertexOutput vertex_main_inner(texture2d<float, access::sample> tint_symbol_12, texture2d<float, access::sample> tint_symbol_13, const constant ExternalTextureParams_tint_packed_vec3* const tint_symbol_14) {
  VertexOutput out = {};
  out.pos = float4(0.0f);
  out.prevent_dce = textureLoad_8acf41(tint_symbol_12, tint_symbol_13, tint_symbol_14);
  return out;
}

vertex tint_symbol vertex_main(texture2d<float, access::sample> tint_symbol_15 [[texture(0)]], texture2d<float, access::sample> tint_symbol_16 [[texture(1)]], const constant ExternalTextureParams_tint_packed_vec3* tint_symbol_17 [[buffer(2)]]) {
  VertexOutput const inner_result = vertex_main_inner(tint_symbol_15, tint_symbol_16, tint_symbol_17);
  tint_symbol wrapper_result = {};
  wrapper_result.pos = inner_result.pos;
  wrapper_result.prevent_dce = inner_result.prevent_dce;
  return wrapper_result;
}

