#version 310 es
precision highp float;
precision highp int;

struct tint_symbol_1 {
  uint texture_builtin_value_0;
  uint pad;
  uint pad_1;
  uint pad_2;
};

layout(binding = 0, std140) uniform tint_symbol_2_block_ubo {
  tint_symbol_1 inner;
} tint_symbol_2;

layout(binding = 0, std430) buffer prevent_dce_block_ssbo {
  uint inner;
} prevent_dce;

uint textureNumLevels_ae911c() {
  uint res = tint_symbol_2.inner.texture_builtin_value_0;
  return res;
}

struct VertexOutput {
  vec4 pos;
  uint prevent_dce;
};

void fragment_main() {
  prevent_dce.inner = textureNumLevels_ae911c();
}

void main() {
  fragment_main();
  return;
}
#version 310 es

struct tint_symbol_1 {
  uint texture_builtin_value_0;
  uint pad;
  uint pad_1;
  uint pad_2;
};

layout(binding = 0, std140) uniform tint_symbol_2_block_ubo {
  tint_symbol_1 inner;
} tint_symbol_2;

layout(binding = 0, std430) buffer prevent_dce_block_ssbo {
  uint inner;
} prevent_dce;

uint textureNumLevels_ae911c() {
  uint res = tint_symbol_2.inner.texture_builtin_value_0;
  return res;
}

struct VertexOutput {
  vec4 pos;
  uint prevent_dce;
};

void compute_main() {
  prevent_dce.inner = textureNumLevels_ae911c();
}

layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
void main() {
  compute_main();
  return;
}
#version 310 es

layout(location = 0) flat out uint prevent_dce_1;
struct tint_symbol_1 {
  uint texture_builtin_value_0;
  uint pad;
  uint pad_1;
  uint pad_2;
};

layout(binding = 0, std140) uniform tint_symbol_2_block_ubo {
  tint_symbol_1 inner;
} tint_symbol_2;

uint textureNumLevels_ae911c() {
  uint res = tint_symbol_2.inner.texture_builtin_value_0;
  return res;
}

struct VertexOutput {
  vec4 pos;
  uint prevent_dce;
};

VertexOutput vertex_main() {
  VertexOutput tint_symbol = VertexOutput(vec4(0.0f, 0.0f, 0.0f, 0.0f), 0u);
  tint_symbol.pos = vec4(0.0f);
  tint_symbol.prevent_dce = textureNumLevels_ae911c();
  return tint_symbol;
}

void main() {
  gl_PointSize = 1.0;
  VertexOutput inner_result = vertex_main();
  gl_Position = inner_result.pos;
  prevent_dce_1 = inner_result.prevent_dce;
  gl_Position.y = -(gl_Position.y);
  gl_Position.z = ((2.0f * gl_Position.z) - gl_Position.w);
  return;
}
