#include <metal_stdlib>
using namespace metal;

struct tint_module_vars_struct {
  device packed_uint3* prevent_dce;
  texture3d<float, access::read> arg_0;
};

struct VertexOutput {
  float4 pos;
  uint3 prevent_dce;
};

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

uint3 textureDimensions_2ff32a(tint_module_vars_struct tint_module_vars) {
  uint3 res = uint3(tint_module_vars.arg_0.get_width(0u), tint_module_vars.arg_0.get_height(0u), tint_module_vars.arg_0.get_depth(0u));
  return res;
}

fragment void fragment_main(device packed_uint3* prevent_dce [[buffer(0)]], texture3d<float, access::read> arg_0 [[texture(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.prevent_dce=prevent_dce, .arg_0=arg_0};
  (*tint_module_vars.prevent_dce) = packed_uint3(textureDimensions_2ff32a(tint_module_vars));
}

kernel void compute_main(device packed_uint3* prevent_dce [[buffer(0)]], texture3d<float, access::read> arg_0 [[texture(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.prevent_dce=prevent_dce, .arg_0=arg_0};
  (*tint_module_vars.prevent_dce) = packed_uint3(textureDimensions_2ff32a(tint_module_vars));
}

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

vertex vertex_main_outputs vertex_main(texture3d<float, access::read> arg_0 [[texture(0)]]) {
  tint_module_vars_struct const tint_module_vars = tint_module_vars_struct{.arg_0=arg_0};
  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;
}
