Skip to content

gpu: software device decodes BC7 only for mode 6 and ASTC 4x4 only for void-extent blocks #2336

Backlog

Description

@houko

Context

#1195 added compressed texture formats, and the changelog entry for it says they come with "pure-Go headless decoding". The software device reports them all as supported: DefaultSoftwareGPUFeatures in internal/driver/gpu.go:680-690 sets TextureCompressionBC, TextureCompressionETC2 and TextureCompressionASTC to true. So gpu.NewSoftwareRenderer and nsgotest accept BC7 and ASTC 4x4 uploads and decode them through decodeCompressedToRGBA (internal/driver/gpu_compressed.go:66, called from decodeCompressedIntoTexture in internal/driver/gpu_software.go:831).

BC1, BC3, ETC2 RGB8/RGBA8 and BC7 mode 6 are decoded as the specifications describe, but two decoders on main handle only part of their format:

  • decodeBC7Block (internal/driver/gpu_compressed.go:237) decodes mode 6 (lines 246-284) and the reserved mode. For modes 0-5 and 7 (lines 298-309), it fills the whole 4x4 block with one colour taken from raw block bytes block[1], block[2], block[3] (and block[4] as alpha for modes 4 and up). These bytes are not endpoints: they mix the partition, rotation and endpoint bit fields. The doc comment at line 236 says the other modes "decode deterministically from their endpoints", which does not match the code.
  • decodeASTC4x4Block (internal/driver/gpu_compressed.go:580) decodes only 2D LDR void-extent (solid colour) blocks (lines 582-594). Every other block (lines 596-606) becomes one colour taken from block[4..7], so weights, partitions and colour endpoint modes are all ignored.

No document under docs/ says that these decoders are approximations, and TestCompressedBlockDecoders in internal/driver/gpu_compressed_test.go:138 covers only the exact paths.

Why it matters

Real encoders (bc7enc, Compressonator, ispc_texcomp, astcenc, basisu transcoding to BC7/ASTC) use mainly BC7 modes other than 6 and ordinary ASTC blocks. On the software device, a texture encoded this way samples as flat 4x4 squares with unrelated colours, and no error or warning is raised. This means that:

  • pixel readback through nsgotest or the software renderer cannot test that real BC7 or ASTC data renders correctly, because the test passes or fails based on the fake colour, not on the texture;
  • an app that selects BC7/ASTC because the software device advertises TextureCompressionBC/TextureCompressionASTC renders different results headlessly than on a GPU, and golden images taken headlessly do not match the GPU.

A game layer built on NSGo (nsgo-dev/game) hit this while adding KTX2 texture tests. Its current workaround uses only single-colour 4x4 blocks, so the encoders emit BC7 mode 6 and ASTC void-extent blocks. It checks full decoding separately on the CPU with its own BC7 decoder, so the GPU path is never tested with realistic data.

Proposal

  • Implement every BC7 mode (0-7) in decodeBC7Block: the two- and three-subset partition tables, anchor indices, P-bits/shared P-bits, rotation and index selection, and 2/3/4-bit weights, following the BC7 format specification (Direct3D 11 / Khronos Data Format Specification).
  • Implement ASTC 4x4 LDR decoding in decodeASTC4x4Block: the block mode, integer sequence encoding (trits/quints), weight unquantisation and infill (trivial at 4x4 with a 4x4 grid, but grids smaller than the block must be supported), partitions with the partition hash, dual-plane, and the LDR colour endpoint modes. HDR endpoint modes and errors should produce the spec's error colour (magenta).
  • Fix the doc comments at lines 236 and 579 to describe what is decoded.
  • If full ASTC decoding is not done soon, the software device should stop reporting TextureCompressionASTC (or the limitation should be documented in docs/architecture/gpu.md and next to Features), so apps do not use ASTC headlessly without knowing the results are fake.

Acceptance

  • decodeBC7Block matches a reference decoder (for example bcdec or DirectXTex) bit for bit on blocks of all eight modes, including two- and three-subset partitions and mode 4/5 rotation.
  • decodeASTC4x4Block matches a reference decoder (for example astcenc in LDR mode) on astcenc-encoded 4x4 blocks covering single and multiple partitions, dual-plane, and several weight and endpoint quantisation levels, and returns the error colour for illegal and HDR blocks.
  • A software-device test uploads a non-trivial (gradient or photo) BC7 and ASTC 4x4 texture, draws it, and checks the read-back pixels against the reference decode.
  • Doc comments and docs/ describe the software device's compressed-format support accurately.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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