#include <metal_stdlib>

using namespace metal;
float textureSampleLevel_1bf73e(depth2d_array<float, access::sample> tint_symbol_1, sampler tint_symbol_2) {
  float res = tint_symbol_1.sample(tint_symbol_2, float2(1.0f), max(0, 1), level(1));
  return res;
}

fragment void fragment_main(device float* tint_symbol_3 [[buffer(0)]], depth2d_array<float, access::sample> tint_symbol_4 [[texture(0)]], sampler tint_symbol_5 [[sampler(0)]]) {
  *(tint_symbol_3) = textureSampleLevel_1bf73e(tint_symbol_4, tint_symbol_5);
  return;
}

kernel void compute_main(device float* tint_symbol_6 [[buffer(0)]], depth2d_array<float, access::sample> tint_symbol_7 [[texture(0)]], sampler tint_symbol_8 [[sampler(0)]]) {
  *(tint_symbol_6) = textureSampleLevel_1bf73e(tint_symbol_7, tint_symbol_8);
  return;
}

struct VertexOutput {
  float4 pos;
  float prevent_dce;
};

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

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

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

