#include <metal_stdlib>
using namespace metal;

struct tint_module_vars_struct {
  device uint2* prevent_dce;
  texture2d<uint, access::read> arg_0;
};

struct VertexOutput {
  float4 pos;
  uint2 prevent_dce;
};

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

uint2 textureDimensions_8e15f4(tint_module_vars_struct tint_module_vars) {
  uint2 res = uint2(tint_module_vars.arg_0.get_width(0u), tint_module_vars.arg_0.get_height(0u));
  return res;
}

fragment void fragment_main(device uint2* prevent_dce [[buffer(0)]], texture2d<uint, 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) = textureDimensions_8e15f4(tint_module_vars);
}

kernel void compute_main(device uint2* prevent_dce [[buffer(0)]], texture2d<uint, 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) = textureDimensions_8e15f4(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_8e15f4(tint_module_vars);
  return out;
}

vertex vertex_main_outputs vertex_main(texture2d<uint, 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;
}
