#include <metal_stdlib>

using namespace metal;
float textureSampleCompareLevel_2ad2b1(depth2d<float, access::sample> tint_symbol_1, sampler tint_symbol_2) {
  float2 arg_2 = float2(1.0f);
  float arg_3 = 1.0f;
  float res = tint_symbol_1.sample_compare(tint_symbol_2, arg_2, arg_3, level(0));
  return res;
}

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

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

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

