Skip to content

WebGLRenderer: Allow copyTextureToTexture3D to copy 2D textures to and from 3d texture layers #29688

@gkjohnson

Description

@gkjohnson

Description

Right now in order to copy a 2d texture (render target or otherwise) into a slide of a 3d texture or an array texture you have to upload the texture and render it into the 3d texture slice. This requires an unnecessary texture data allocation and running shaders.

And as mentioned in #29677, generating mipmaps for 3d textures is extremely expensive since it always requires regenerating data for all layers. Instead I would like to be able to generate mip maps for a 2d texture, then copy the data from all mipmap levels to the levels for the target layer in the array texture, avoiding a full mipmap generation.

The concrete use case for this is when dynamically adding and removing textures from a 3d array texture for use with BatchedMesh rendering. Specifically 3d tiles in this case. Since multiple textures can suddenly be added in a single frame it's important to keep the upload of data and mipmap generation process as quick as possible.

Solution

I think a clean API would be to allow for passing 2d textures in as arguments to the copyTextureToTexture3D function. The function implementation would then effectively be able to treat 2d textures as 3d textures with a depth of 1 and copy individual layers to and from a 3d texture.

The signature would change to something like so:

copyTextureToTexture3D(
  src: Texture2D | Texture3D,
  dst: Texture2D | Texture3D,
  srcRegion: Box3,
  dstPosition: Vector3,
  level: Number
): void;

If the implementation is successful we could replace the near-duplicate implementation of copyTextureToTexture2D with this one.

cc @Mugen87 I'm wondering what your thoughts are on this.

Alternatives

Render the 2d data into a 3d texture with a depth of 1, generate mip maps, then copy the layers to the target texture array (which would be slower than necessary). Or don't use mipmaps.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions