#include <metal_stdlib>

using namespace metal;
float4 textureSampleLevel_c32df7(texturecube<float, access::sample> tint_symbol_1, sampler tint_symbol_2) {
  float3 arg_2 = float3(1.0f);
  float arg_3 = 1.0f;
  float4 res = tint_symbol_1.sample(tint_symbol_2, arg_2, level(arg_3));
  return res;
}

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

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

struct VertexOutput {
  float4 pos;
  float4 prevent_dce;
};

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

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

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

