#include <metal_stdlib>
using namespace metal;

struct tint_module_vars_struct {
  device float4* prevent_dce;
  texturecube<float, access::sample> arg_0;
  sampler arg_1;
};

struct VertexOutput {
  float4 pos;
  float4 prevent_dce;
};

struct vertex_main_outputs {
  float4 VertexOutput_pos [[position]];
  float4 VertexOutput_prevent_dce [[user(locn0)]] [[flat]];
};

float4 textureSampleLevel_c32df7(tint_module_vars_struct tint_module_vars) {
  float4 res = tint_module_vars.arg_0.sample(tint_module_vars.arg_1, float3(1.0f), level(1.0f));
  return res;
}

fragment void fragment_main(device float4* prevent_dce [[buffer(0)]], texturecube<float, access::sample> arg_0 [[texture(0)]], sampler arg_1 [[sampler(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.prevent_dce=prevent_dce, .arg_0=arg_0, .arg_1=arg_1};
  (*tint_module_vars.prevent_dce) = textureSampleLevel_c32df7(tint_module_vars);
}

kernel void compute_main(device float4* prevent_dce [[buffer(0)]], texturecube<float, access::sample> arg_0 [[texture(0)]], sampler arg_1 [[sampler(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.prevent_dce=prevent_dce, .arg_0=arg_0, .arg_1=arg_1};
  (*tint_module_vars.prevent_dce) = textureSampleLevel_c32df7(tint_module_vars);
}

VertexOutput vertex_main_inner(tint_module_vars_struct tint_module_vars) {
  VertexOutput out = {};
  out.pos = float4(0.0f);
  out.prevent_dce = textureSampleLevel_c32df7(tint_module_vars);
  return out;
}

vertex vertex_main_outputs vertex_main(texturecube<float, access::sample> arg_0 [[texture(0)]], sampler arg_1 [[sampler(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.arg_0=arg_0, .arg_1=arg_1};
  VertexOutput const v = vertex_main_inner(tint_module_vars);
  vertex_main_outputs tint_wrapper_result = {};
  tint_wrapper_result.VertexOutput_pos = v.pos;
  tint_wrapper_result.VertexOutput_prevent_dce = v.prevent_dce;
  return tint_wrapper_result;
}
