#include <metal_stdlib>

using namespace metal;
uint2 textureDimensions_49a067(texturecube<float, access::sample> tint_symbol_1) {
  uint2 res = uint2(tint_symbol_1.get_width(1), tint_symbol_1.get_height(1));
  return res;
}

fragment void fragment_main(device uint2* tint_symbol_2 [[buffer(0)]], texturecube<float, access::sample> tint_symbol_3 [[texture(0)]]) {
  *(tint_symbol_2) = textureDimensions_49a067(tint_symbol_3);
  return;
}

kernel void compute_main(device uint2* tint_symbol_4 [[buffer(0)]], texturecube<float, access::sample> tint_symbol_5 [[texture(0)]]) {
  *(tint_symbol_4) = textureDimensions_49a067(tint_symbol_5);
  return;
}

struct VertexOutput {
  float4 pos;
  uint2 prevent_dce;
};

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

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

vertex tint_symbol vertex_main(texturecube<float, access::sample> tint_symbol_7 [[texture(0)]]) {
  VertexOutput const inner_result = vertex_main_inner(tint_symbol_7);
  tint_symbol wrapper_result = {};
  wrapper_result.pos = inner_result.pos;
  wrapper_result.prevent_dce = inner_result.prevent_dce;
  return wrapper_result;
}

