Skip to content

WebGL: flush and rebatch when texture units are exhausted instead of throwing #1280

@obiot

Description

@obiot

Description

When all GPU texture units are occupied and a new texture needs to be bound, the texture cache currently throws an exception:

Texture cache overflow: N texture units available for this GPU.

This is overly restrictive. Instead, the renderer should transparently handle texture unit exhaustion by flushing the current batch (issuing a draw call for everything buffered so far), resetting the texture unit assignments, and continuing with the new texture bound to unit 0.

Proposed solution

In TextureCache.allocateTextureUnit(), instead of throwing when no units are available:

  1. Flush the current compositor (compositor.flush())
  2. Reset/clear all texture unit assignments in the cache
  3. Allocate unit 0 for the new texture and return it

This results in an additional draw call, which is the expected trade-off when a scene uses more unique textures than the GPU has units. The behavior is transparent to the user — no errors, no texture merging complexity.

Impact

  • Games with many unique textures will work without errors (at the cost of additional draw calls)
  • Games within the texture unit limit see no change in behavior
  • No API changes required

Location: src/video/texture/cache.jsallocateTextureUnit()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions