#include <metal_stdlib>

using namespace metal;
float4 textureSampleLevel_aab3b9(texturecube_array<float, access::sample> tint_symbol_1, sampler tint_symbol_2) {
  float4 res = tint_symbol_1.sample(tint_symbol_2, float3(1.0f), 1u, level(1.0f));
  return res;
}

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

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

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

