#include <metal_stdlib>

using namespace metal;
float textureSampleLevel_ff11bc(depthcube_array<float, access::sample> tint_symbol_1, sampler tint_symbol_2) {
  float3 arg_2 = float3(1.0f);
  uint arg_3 = 1u;
  int arg_4 = 1;
  float res = tint_symbol_1.sample(tint_symbol_2, arg_2, arg_3, level(arg_4));
  return res;
}

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

kernel void compute_main(device float* tint_symbol_6 [[buffer(0)]], depthcube_array<float, access::sample> tint_symbol_7 [[texture(0)]], sampler tint_symbol_8 [[sampler(0)]]) {
  *(tint_symbol_6) = textureSampleLevel_ff11bc(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(depthcube_array<float, access::sample> tint_symbol_9, sampler tint_symbol_10) {
  VertexOutput out = {};
  out.pos = float4(0.0f);
  out.prevent_dce = textureSampleLevel_ff11bc(tint_symbol_9, tint_symbol_10);
  return out;
}

vertex tint_symbol vertex_main(depthcube_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;
}

