#3790·ebiten

internal/shader: check the total uniform size against every backend's limit

Author: hajimehoshiCreated Sep 26, 2026Updated Sep 26, 2026
Labelsfeatureos:linuxos:windowsos:macosos:androidos:iosos:freebsdos:js

Operating System

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • OpenBSD
  • Android
  • iOS
  • Nintendo Switch
  • PlayStation 5
  • Xbox
  • Web Browsers

What feature would you like to be added?

Check the total size of a shader's uniform variables at compile time, and make NewShader return an error when it does not fit every backend.

Nothing checks the total uniform size today. maxArrayLength (internal/shader/type.go, 75e60168c) caps an array at 4096 elements, but that bounds code generation, not uniform capacity. The budgets differ by backend, and the same uniform takes a different amount of space on each:

  • DirectX 11: a constant buffer holds at most 4096 16-byte registers. The HLSL layout (hlsl.UniformVariableOffsetsInDwords) starts every array element on a register boundary, so an array takes at least one register per element. The engine's own uniforms share the same cbuffer Uniforms : register(b0).
  • Metal: uniforms are passed with setVertexBytes and setFragmentBytes (internal/graphicsdriver/metal), which Apple documents for data smaller than 4 KB. appendUniformVariables packs a bool into one byte and a float into four.
  • OpenGL ES 3 and WebGL 2: MAX_FRAGMENT_UNIFORM_VECTORS is only guaranteed to be at least 224 vec4s.
  • PlayStation 5, Nintendo Switch and Xbox: the limits still need to be checked.

For example, var U [4096]float compiles today (TestCompileArrayLengthLimit even asserts it). On DirectX 11 it needs all 4096 registers before the engine's uniforms are counted, on Metal it is 16 KB, and it is far over the WebGL 2 minimum. What happens at run time then depends on the backend and has not been checked.

A possible approach is to compute the total size with each backend's layout rules, including the engine's reserved uniforms, and to report a compile error when it exceeds the smallest guaranteed budget among the supported backends. The var U [4096]float case in TestCompileArrayLengthLimit would need to change then.

Why is this needed?

So that a shader NewShader accepts works on every platform. Today NewShader accepts uniforms that fit one backend's budget but exceed another's. For example, var U [2048]float fits the DirectX 11 register limit, but is 8 KB on Metal and far over the WebGL 2 minimum.


Filed by Claude (Claude Code), on behalf of @hajimehoshi.