Skip to content

Refactor TextureCache and Batcher base class for renderer-agnostic abstraction #1410

@obiot

Description

@obiot

Summary

Prepare the texture cache and batcher systems for a future WebGPU backend by extracting renderer-agnostic parts from the current WebGL-specific implementations.

Completed

Buffer extraction (PR #1414)

  • VertexArrayBuffer: moved from video/webgl/buffer/ to video/buffer/ — zero GL dependency, pure typed array management
  • IndexBuffer: split into renderer-agnostic IndexBuffer base class (data accumulation, quad index patterns) and WebGLIndexBuffer subclass (GL buffer bind/upload)

Deferred — TextureCache unit management

Moving units/usedUnits/allocateTextureUnit/etc. from TextureCache to MaterialBatcher was attempted but causes rendering regressions.

Root causes identified

  1. boundTextures is per-batcher, but GL texture units are global. When unit management lives on the batcher, each batcher (QuadBatcher, MeshBatcher, PrimitiveBatcher) independently allocates units. MeshBatcher assigns a texture to unit 0, then QuadBatcher also assigns a different texture to unit 0 — they conflict because GL units are shared.

  2. blitTexture and beginPostEffect bypass unbindTexture2D. They do raw delete this.boundTextures[0] which desyncs the source→unit map from the unit→GLTexture map. Fixed by routing through unbindTexture2D, but the global vs per-batcher issue (point 1) remains.

  3. The cache's units Map must be shared across all batchers. This is by design — cache.units maps texture sources to GL units at the renderer level, while batcher.boundTextures tracks which GL texture object is bound to each unit within that batcher. These are different levels of abstraction that cannot be merged.

Correct approach (future)

The source→unit mapping should stay on a shared object (renderer or cache). Options:

  • Rename cache.units/usedUnits to something clearer (e.g. textureBindings) and keep on cache
  • Create a dedicated TextureUnitAllocator on the renderer that both the cache and batchers access
  • Keep the current architecture but document the intentional coupling between cache and batcher

Not planned (wait for WebGPU)

  • GLShader / ShaderEffect — GLSL vs WGSL is too fundamental to abstract without the actual WebGPU implementation
  • WebGLRenderer itself — 2000+ lines of direct GL, would be a parallel WebGPURenderer
  • Specific batchers (QuadBatcher, PrimitiveBatcher, MeshBatcher) — depend on the batcher base

Already done (prior PRs)

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