API 参考取自源码中的 Go 文档注释,以英文书写。
package ktx2
import "github.com/nsgo-dev/game/pkg/tooling/ktx2"概述
Package ktx2 provides a pure-Go KTX2 v2.0 texture container encoder, decoder, supercompression decoder (BasisLZ, Zstandard, ZLIB), mipmap chain generator, Basis Universal ETC1S and UASTC 4x4 codecs, and block texture transcoder (BC1, BC3, BC7, ETC2 RGB8/RGBA8, ASTC 4x4, RGBA8).
索引
- 常量
- 变量
- func DecodeHeader(data []byte) (Header, []LevelIndex, error)
- func DecodeKVD(kvdBytes []byte) (map[string][]byte, error)
- func DecodeRGBA(data []byte, level int) (*image.NRGBA, error)
- func Encode(img image.Image, opts Options) ([]byte, error)
- func EncodeFile(inPath, outPath string, opts Options) error
- func EncodeWriter(w io.Writer, img image.Image, opts Options) error
- func GenerateMipmaps(src image.Image, srgb bool) []*image.NRGBA
- func MipDimensions(baseW, baseH, level int) (int, int)
- func MipLevelCount(width, height int) int
- func ReadLevel(data []byte, level int) (raw []byte, width, height int, err error)
- func ToNRGBA(src image.Image) *image.NRGBA
- func Validate(data []byte) error
- type DFD
- type DFDSample
- type Format
- func FormatFromHeaderDFD(h Header, dfd DFD) (format Format, srgb bool, hasAlpha bool, ok bool)
- func FormatFromVk(vk uint32) (Format, bool, bool)
- func ParseFormat(s string) (Format, error)
- func (f Format) BlockBytes() int
- func (f Format) BlockDim() int
- func (f Format) HasAlpha() bool
- func (f Format) IsBlockCompressed() bool
- func (f Format) LevelByteSize(w, h int) int
- func (f Format) String() string
- func (f Format) VkFormat(srgb bool) (uint32, error)
- type Header
- type Level
- type LevelIndex
- type Options
- type Texture
常量
const (
VkFormatUndefined uint32 = 0
VkFormatR8G8B8A8Unorm uint32 = 37
VkFormatR8G8B8A8SRGB uint32 = 43
VkFormatBC1RGBAUnormBlock uint32 = 133
VkFormatBC1RGBASRGBBlock uint32 = 134
VkFormatBC3UnormBlock uint32 = 137
VkFormatBC3SRGBBlock uint32 = 138
VkFormatBC7UnormBlock uint32 = 145
VkFormatBC7SRGBBlock uint32 = 146
VkFormatETC2R8G8B8UnormBlk uint32 = 147
VkFormatETC2R8G8B8SRGBBlk uint32 = 148
VkFormatETC2R8G8B8A8UnormBlk uint32 = 151
VkFormatETC2R8G8B8A8SRGBBlk uint32 = 152
VkFormatASTC4x4UnormBlock uint32 = 157
VkFormatASTC4x4SRGBBlock uint32 = 158
)Vulkan VkFormat constants used by this package.
const (
DFDVendorKhronos uint32 = 0
DFDDescriptorBasicFormat uint32 = 0
DFDVersionNumber uint32 = 2
DFDModelRGBSDA uint8 = 1
DFDModelBC1A uint8 = 128
DFDModelBC3 uint8 = 130
DFDModelBC7 uint8 = 134
DFDModelETC2 uint8 = 161
DFDModelASTC uint8 = 162
DFDModelETC1S uint8 = 163
DFDModelUASTC uint8 = 166
DFDPrimariesBT709 uint8 = 1
DFDTransferLinear uint8 = 1
DFDTransferSRGB uint8 = 2
DFDChannelRGBSDARed uint8 = 0
DFDChannelRGBSDAGreen uint8 = 1
DFDChannelRGBSDABlue uint8 = 2
DFDChannelRGBSDAAlpha uint8 = 15
DFDChannelBC1AColor uint8 = 0
DFDChannelBC3Color uint8 = 0
DFDChannelBC3Alpha uint8 = 15
DFDChannelBC7Color uint8 = 0
DFDChannelETC2Color uint8 = 0
DFDChannelETC2Alpha uint8 = 15
DFDChannelASTCData uint8 = 0
DFDChannelETC1SRGB uint8 = 0
DFDChannelETC1SRRR uint8 = 3
DFDChannelETC1SGGG uint8 = 4
DFDChannelETC1SAAA uint8 = 15
DFDChannelUASTCRGB uint8 = 0
DFDChannelUASTCRGBA uint8 = 3
DFDChannelUASTCRRR uint8 = 4
DFDChannelUASTCRRRG uint8 = 5
SupercompressionNone uint32 = 0
SupercompressionBasisLZ uint32 = 1
SupercompressionZstandard uint32 = 2
SupercompressionZLIB uint32 = 3
)Khronos Data Format Descriptor (DFD) v1.3 constants.
const HeaderSize = 80HeaderSize is the fixed size in bytes of the KTX2 identifier + header + index section.
const LevelIndexEntrySize = 24LevelIndexEntrySize is the byte size of one entry in the KTX2 level index array.
变量
var Identifier = [12]byte{0xAB, 0x4B, 0x54, 0x58, 0x20, 0x32, 0x30, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A}Identifier is the 12-byte KTX2 v2.0 magic file signature: "\xABKTX 20\xBB\r\n\x1A\n".
函数
func DecodeHeader(data []byte) (Header, []LevelIndex, error)DecodeHeader parses the KTX2 identifier, header, and level index array.
func DecodeKVD(kvdBytes []byte) (map[string][]byte, error)DecodeKVD parses the Key/Value Data section of a KTX2 file and verifies lexicographical key order.
func DecodeRGBA(data []byte, level int) (*image.NRGBA, error)DecodeRGBA decodes the specified mip level (0 = base) of a KTX2 texture into *image.NRGBA. Supports RGBA8, BC1, BC3, BC7, ETC2 RGB8/RGBA8, ASTC 4x4, Basis Universal UASTC 4x4, and BasisLZ/ETC1S with uncompressed, BasisLZ, Zstandard, or ZLIB supercompression.
func Encode(img image.Image, opts Options) ([]byte, error)Encode encodes img into a spec-compliant KTX2 v2.0 byte slice.
func EncodeFile(inPath, outPath string, opts Options) errorEncodeFile reads a PNG or JPEG image from inPath and writes a KTX2 texture to outPath.
func EncodeWriter(w io.Writer, img image.Image, opts Options) errorEncodeWriter encodes img as KTX2 and writes it to w.
func GenerateMipmaps(src image.Image, srgb bool) []*image.NRGBAGenerateMipmaps generates a complete mipmap chain down to 1x1 starting from src. When srgb is true, RGB channels are converted to linear light prior to filtering and converted back to sRGB afterward.
func MipDimensions(baseW, baseH, level int) (int, int)MipDimensions returns the width and height of mip level (0-based) for a base texture size.
func MipLevelCount(width, height int) intMipLevelCount returns the total number of mipmap levels from (width, height) down to 1x1.
func ReadLevel(data []byte, level int) (raw []byte, width, height int, err error)ReadLevel returns the raw (on-disk) bytes and pixel dimensions for mip level (0 = base level).
func ToNRGBA(src image.Image) *image.NRGBAToNRGBA converts any image.Image into a tightly-packed 0-origin *image.NRGBA.
func Validate(data []byte) errorValidate checks a KTX2 buffer for structural compliance (magic, header, DFD, KVD, SGD bounds, level index bounds, and mip level byte sizes).
类型
type DFD struct {
TotalSize uint32
VendorID uint32
DescriptorType uint32
VersionNumber uint32
DescriptorBlockSize uint32
ColorModel uint8
ColorPrimaries uint8
TransferFunction uint8
Flags uint8
TexelBlockDimensions [4]uint8 // stored as dim - 1 per Khronos DFD v1.3
BytesPlane [8]uint8
Samples []DFDSample
}DFD represents a parsed KTX2 Basic Data Format Descriptor block.
func ParseDFD(dfdBytes []byte) (DFD, error)ParseDFD decodes the Basic Data Format Descriptor block from raw DFD bytes.
type DFDSample struct {
BitOffset uint16
BitLength uint8 // stored as bit_length - 1 per Khronos DFD v1.3
ChannelType uint8
SamplePosition [4]uint8
SampleLower uint32
SampleUpper uint32
}DFDSample describes one sample block inside a KTX2 Basic Data Format Descriptor.
type Format stringFormat identifies a supported KTX2 pixel, universal, or block-compressed texture format.
const (
FormatRGBA8 Format = "rgba8"
FormatBC1 Format = "bc1"
FormatBC3 Format = "bc3"
FormatBC7 Format = "bc7"
FormatETC2 Format = "etc2"
FormatETC2RGBA Format = "etc2-rgba"
FormatASTC4x4 Format = "astc4x4"
FormatUASTC Format = "uastc"
FormatETC1S Format = "etc1s"
)func FormatFromHeaderDFD(h Header, dfd DFD) (format Format, srgb bool, hasAlpha bool, ok bool)FormatFromHeaderDFD resolves the texture format, sRGB transfer flag, and alpha presence from a KTX2 header and parsed Data Format Descriptor, including Basis Universal UASTC and ETC1S containers that set Header.VkFormat == 0.
func FormatFromVk(vk uint32) (Format, bool, bool)FormatFromVk maps a supported Vulkan VkFormat ID back to (Format, srgb, ok). For VkFormatUndefined (0), use FormatFromHeaderDFD to resolve Basis Universal UASTC or ETC1S from the Data Format Descriptor.
func ParseFormat(s string) (Format, error)ParseFormat parses a format name as game tex's -format flag and an image sidecar spell it (rgba8, bc1/dxt1, bc3/dxt5 or etc2), the formats the asset pipeline writes. The other formats this package encodes and decodes, such as FormatBC7, FormatETC2RGBA, FormatASTC4x4 and FormatUASTC, are set through Options.Format from Go.
func (f Format) BlockBytes() intBlockBytes returns the byte size of one texel block (4 for RGBA8, 8 for BC1/ETC2, 16 for BC3/BC7/ETC2RGBA/ASTC4x4/UASTC).
func (f Format) BlockDim() intBlockDim returns the square texel block dimension (4 for block formats, 1 for RGBA8).
func (f Format) HasAlpha() boolHasAlpha reports whether f carries an alpha channel.
func (f Format) IsBlockCompressed() boolIsBlockCompressed reports whether f stores 4x4 texel blocks rather than uncompressed pixels.
func (f Format) LevelByteSize(w, h int) intLevelByteSize returns the exact uncompressed byte size of a w x h mip level encoded in f.
func (f Format) VkFormat(srgb bool) (uint32, error)VkFormat returns the Vulkan VkFormat ID for f and the requested sRGB color space. Universal formats (FormatUASTC, FormatETC1S) use VkFormatUndefined (0) per KTX2 v2.0, with the pixel representation described in the DFD.
type Header struct {
VkFormat uint32
TypeSize uint32
PixelWidth uint32
PixelHeight uint32
PixelDepth uint32
LayerCount uint32
FaceCount uint32
LevelCount uint32
SupercompressionScheme uint32
DFDByteOffset uint32
DFDByteLength uint32
KVDByteOffset uint32
KVDByteLength uint32
SGDByteOffset uint64
SGDByteLength uint64
}Header represents the 80-byte KTX2 v2.0 file header (including index offsets).
type Level struct {
Width int
Height int
Data []byte
}Level holds one mip level's dimensions and de-supercompressed payload bytes.
type LevelIndex struct {
ByteOffset uint64
ByteLength uint64
UncompressedByteLength uint64
}LevelIndex describes one mip level entry in the KTX2 level index array.
type Options struct {
// Format selects pixel, universal, or block compression (default FormatRGBA8).
Format Format
// Mipmaps generates a complete mipmap chain down to 1x1 when true.
Mipmaps bool
// SRGB encodes using the corresponding Vulkan sRGB format, sRGB DFD transfer function, and sRGB-aware linear-light mipmap filtering.
SRGB bool
// Supercompression selects an optional KTX2 supercompression scheme (SupercompressionNone default, or SupercompressionZLIB).
Supercompression uint32
// KVD adds optional key/value metadata pairs to the KTX2 Key/Value Data section. Standard keys KTXorientation and KTXwriter are populated automatically if absent.
KVD map[string]string
}Options configures KTX2 texture encoding.
type Texture struct {
Header Header
DFD DFD
KVD map[string][]byte
Format Format
SRGB bool
HasAlpha bool
Width int
Height int
Levels []Level
SGD []byte
// contains filtered or unexported fields
}Texture holds a fully parsed and validated 2D KTX2 v2.0 container.
func Decode(data []byte) (*Texture, error)Decode parses, validates, and de-supercompresses a 2D KTX2 v2.0 container. Supports SupercompressionNone (0), SupercompressionBasisLZ (1), SupercompressionZstandard (2), and SupercompressionZLIB (3) across RGBA8, BC1, BC3, BC7, ETC2 RGB8/RGBA8, ASTC 4x4, Basis Universal UASTC 4x4, and BasisLZ/ETC1S.
func (t *Texture) DecodeLevelRGBA(level int) (*image.NRGBA, error)DecodeLevelRGBA decodes mip level (0 = base) of t into *image.NRGBA.
func (t *Texture) Transcode(target Format) ([]Level, error)Transcode converts every mip level of t to target format. When t.Format already equals target (and target is a concrete GPU/pixel format), level payloads are cloned directly without pixel decoding or re-encoding.