#include <metal_stdlib>

using namespace metal;
uint2 textureDimensions_eafe19(depth2d_array<float, access::sample> tint_symbol_1) {
  uint arg_1 = 1u;
  uint2 res = uint2(tint_symbol_1.get_width(arg_1), tint_symbol_1.get_height(arg_1));
  return res;
}

fragment void fragment_main(device uint2* tint_symbol_2 [[buffer(0)]], depth2d_array<float, access::sample> tint_symbol_3 [[texture(0)]]) {
  *(tint_symbol_2) = textureDimensions_eafe19(tint_symbol_3);
  return;
}

kernel void compute_main(device uint2* tint_symbol_4 [[buffer(0)]], depth2d_array<float, access::sample> tint_symbol_5 [[texture(0)]]) {
  *(tint_symbol_4) = textureDimensions_eafe19(tint_symbol_5);
  return;
}

struct VertexOutput {
  float4 pos;
  uint2 prevent_dce;
};

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

VertexOutput vertex_main_inner(depth2d_array<float, access::sample> tint_symbol_6) {
  VertexOutput out = {};
  out.pos = float4(0.0f);
  out.prevent_dce = textureDimensions_eafe19(tint_symbol_6);
  return out;
}

vertex tint_symbol vertex_main(depth2d_array<float, access::sample> tint_symbol_7 [[texture(0)]]) {
  VertexOutput const inner_result = vertex_main_inner(tint_symbol_7);
  tint_symbol wrapper_result = {};
  wrapper_result.pos = inner_result.pos;
  wrapper_result.prevent_dce = inner_result.prevent_dce;
  return wrapper_result;
}

