Quick answer: Pack your uniform array into vec4[] (one slot per 4 floats). For larger sets, encode into a Texture and sample with texelFetch. SSBOs only work on Vulkan; for mobile portability, use the texture-as-buffer technique.
You declare uniform float weights[256]; and the shader compiles fine on PC, fails on Android with “too many uniforms.” Mobile GPUs have constant slot limits and float arrays burn through them fast.
The Symptom
Shader compile error on mobile referencing uniform count, register limit, or constant slot exhaustion. Same shader works on desktop. Adding more uniforms accelerates the failure.
What Causes This
GLES 3 standard requires only 256 vec4 fragment uniforms. Many devices have more, but you can’t rely on it. A float[256] consumes 256 slots (each float pads to a vec4). vec4[64] covers the same 256 floats in 64 slots.
The Fix
Pattern 1: Pack into vec4 arrays.
uniform vec4 weights[64]; // 256 floats packed
void fragment() {
int i = ...;
float w = weights[i / 4][i % 4];
...
}
Set from script:
var arr := PackedFloat32Array()
arr.resize(256)
for i in 256: arr[i] = 0.5
material.set_shader_parameter("weights", arr)
Pattern 2: Encode in a texture (data buffer).
# GDScript: build a texture buffer
var img := Image.create(256, 1, false, Image.FORMAT_RGBAF)
for i in 256: img.set_pixel(i, 0, Color(value, 0, 0, 0))
var tex := ImageTexture.create_from_image(img)
material.set_shader_parameter("data", tex)
// Shader: read by index
uniform sampler2D data : filter_nearest, repeat_disable;
void fragment() {
float v = texelFetch(data, ivec2(42, 0), 0).r;
}
Texture data scales to thousands of entries; the only limit is texture size, which is much higher than uniform slot count.
Filter Nearest, No Repeat
For data buffers, set filter_nearest so sampling at integer indices doesn’t blend with neighbors. Disable repeat to avoid wraparound.
Verifying
Build to Android. Shader should compile. Run; values should match what you set per-pixel. Use OS.has_feature(“mobile”) to switch to the texture path only on mobile if you want both code paths.
“Pack vec4. Texture for large data. Mobile compiles.”
Related Issues
For shader uniform not updating, see uniform update. For mobile particle flicker, see mobile particles.
Pack tight. Texture as buffer. Mobile happy.