跪拜 Guibai
← Back to the summary

Flutter GPU Draft PRs Bring Real 3D Textures with Hardware Trilinear Filtering

Flutter GPU recently has two interesting Draft PRs: #197354 adds kTexture3D under Impeller, and #197355 exposes this capability to the Flutter GPU Dart API.

Previously, Flutter GPU already supported 2D Texture Array via #190036, allowing many 2D textures of the same size and format to be placed in one texture resource. Why then do we need a 3D texture?

The difference is, for example, a fluid simulation result has a density value at every position in space. The simplest way to store it is to slice that space along the Z axis into a stack of 2D textures, then look up values using X, Y, and Z.

Using a 2D Texture Array to store this data is fine, but when you want to draw a cross-section slanted through the volume, many sample points fall between two layers, say between layer 12 and 13, biased toward layer 13. At that point, the third sampling coordinate of a Texture Array mainly selects a layer; it won't automatically linearly interpolate between adjacent layers the way the X and Y axes do.

So in the shader you need to sample two layers separately, then blend the two results based on the point's Z position. With a 3D texture, the third coordinate can also participate in hardware linear filtering, just like the first two.

image

For example, if data has different values on eight neighboring voxels and the sample point lands between them, a 3D texture, on a format and sampler setting that support linear filtering, can let the hardware interpolate along X, Y, and Z, which is the trilinear filtering mentioned here.

Of course, this trilinear is interpolation across three spatial axes, which is a bit different from the traditional 2D texture filtering between two mip levels.

This request actually originated from #195222. The author was using Flutter GPU to do volume rendering. Besides drawing slices at arbitrary angles, they needed to sample the entire volume multiple times along rays in the fragment shader; for example, to display temperature fields, fluid density fields, voxel scan data, or SDF.

Without 3D textures, they used multiple 2D atlases, each arranged with many slices, and each RGBA8 texel packed two 16-bit scalar values.

In the shader, every voxel read required computing which atlas, which tile, which texel, then reassembling the bytes in the two channels back to the original values.

Because in this packing the same channel in adjacent texels doesn't necessarily correspond to adjacent scalars, they also had to read the surrounding eight voxels and do seven mix operations to interpolate. But with a 3D texture format that supports filtering, the shader can just write:

uniform sampler3D volume;
float density = texture(volume, vec3(u, v, w)).r;

That is, a single volume sample is handed directly to the GPU's texture sampling hardware.

So this #197354 first adds depth to Impeller's TextureDescriptor, then maps this descriptor to the native objects of different backends:

There's also a practical design: the existing slice parameter can continue to be used. For a 2D Texture Array it means an array layer, while for a 3D texture it means a slice along the Z axis.

For example, if a simulation program generates 256 slices in sequence, Flutter GPU doesn't need to assemble the whole data into a single contiguous buffer in Dart before uploading. It can create the full GPU texture first, then upload each slice to the corresponding Z position as it arrives.

The full texture on the GPU still needs to be allocated, but the CPU side doesn't need to keep all slices simultaneously in memory.

The underlying byte-size interfaces have also been adjusted:

Then #197355 connects this to the Dart layer. For example, a simplified usage according to the draft PR's API looks roughly like:

final volume = gpu.gpuContext.createTexture(
  gpu.StorageMode.hostVisible,
  width,
  height,
  depth: depth,
  textureType: gpu.TextureType.texture3D,
  enableRenderTargetUsage: false,
);
volume.overwrite(sliceBytes, slice: z);

Currently, the limitations for Flutter GPU are quite clear. First, the underlying support and the capabilities exposed at the Dart layer aren't fully the same:

Backend Impeller draft's per-axis size limit Flutter GPU Dart layer draft
Metal 2,048 Supported
Vulkan device maxImageDimension3D, spec guarantees at least 256 Supported
GLES query GL_MAX_3D_TEXTURE_SIZE when support conditions are met Not exposed

The GLES case is special: even if the underlying layer has 3D textures, Flutter GPU's current shader bundle still lacks a corresponding GLSL ES 3.0 variant.

In addition, the current PR limits 3D textures to sampling use: single mip level, single sample, no block-compressed formats, and it can't be used as a render target. So far, there's still a distance from using a 3D texture as a GPU write-accessible compute resource inside Flutter GPU.

Also, 3D texture volume grows quickly; for example, with RGBA8:

So although maxTexture3DSize tells you the limit per dimension, it currently doesn't guarantee enough memory to allocate a cube near the cap.

Still, it's visible that Flutter's 3D capabilities are getting more mature. Looking forward to other impressive follow-ups from Flutter Scene.

Comments

Top 2 from juejin.cn, machine-translated. The original thread is authoritative.

伯伯熊勞

Looking forward to a game engine.

午夜小学徒

Melon-eating bystanders.