//
// fragment_main
//
#include <metal_stdlib>

using namespace metal;
uint textureNumLayers_52dfc5(texture2d_array<int, access::read> tint_symbol) {
  uint res = tint_symbol.get_array_size();
  return res;
}

struct VertexOutput {
  float4 pos;
  uint prevent_dce;
};

fragment void fragment_main(device uint* tint_symbol_1 [[buffer(0)]], texture2d_array<int, access::read> tint_symbol_2 [[texture(0)]]) {
  *(tint_symbol_1) = textureNumLayers_52dfc5(tint_symbol_2);
  return;
}

//
// compute_main
//
#include <metal_stdlib>

using namespace metal;
uint textureNumLayers_52dfc5(texture2d_array<int, access::read> tint_symbol) {
  uint res = tint_symbol.get_array_size();
  return res;
}

struct VertexOutput {
  float4 pos;
  uint prevent_dce;
};

kernel void compute_main(device uint* tint_symbol_1 [[buffer(0)]], texture2d_array<int, access::read> tint_symbol_2 [[texture(0)]]) {
  *(tint_symbol_1) = textureNumLayers_52dfc5(tint_symbol_2);
  return;
}

//
// vertex_main
//
#include <metal_stdlib>

using namespace metal;
uint textureNumLayers_52dfc5(texture2d_array<int, access::read> tint_symbol_1) {
  uint res = tint_symbol_1.get_array_size();
  return res;
}

struct VertexOutput {
  float4 pos;
  uint prevent_dce;
};

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

VertexOutput vertex_main_inner(texture2d_array<int, access::read> tint_symbol_2) {
  VertexOutput out = {};
  out.pos = float4(0.0f);
  out.prevent_dce = textureNumLayers_52dfc5(tint_symbol_2);
  return out;
}

vertex tint_symbol vertex_main(texture2d_array<int, access::read> tint_symbol_3 [[texture(0)]]) {
  VertexOutput const inner_result = vertex_main_inner(tint_symbol_3);
  tint_symbol wrapper_result = {};
  wrapper_result.pos = inner_result.pos;
  wrapper_result.prevent_dce = inner_result.prevent_dce;
  return wrapper_result;
}

