#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) {
  int2 arg_1 = int2(1);
  float4 res = textureLoadExternal(tint_symbol_1, tint_symbol_2, arg_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;
}

