flutter3d
Showcase Changelog 62 packages API reference

Compressing a texture: the source

since 0.7.0 Formats and I/O

Regions the guide quotes: source (line 36), encode (line 52), mips (line 57), report (line 83), check (line 112).

1/// Compressing a texture: four block encoders, a mip chain, and a KTX2
2/// container to hold the result.
3///
4/// Quoted by `texture_compression.md` and shown whole in the Source tab.
5library;
6
7import 'dart:typed_data';
8
9import 'package:flutter/widgets.dart';
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_core/formats.dart'
12 show
13 Rgba8Image,
14 VkFormat,
15 buildMipChain,
16 encodeAstc4x4,
17 encodeBc1,
18 encodeBc3,
19 encodeEtc2Rgb8,
20 writeKtx2;
21import 'package:flutter3d_showcase/src/demo/demo.dart';
23/// The `texture-compression` page: a block wearing a texture run through the
24/// block encoders and packed into KTX2. Its check is that a compressed block
25/// is smaller than the raw one and that a KTX2 file came out.
26final class TextureCompressionDemo extends ShowcaseDemo {
27 late final Rgba8Image _source;
28 late final Uint8List _bc1;
29 late final Uint8List _bc3;
30 late final Uint8List _etc2;
31 late final Uint8List _astc;
32 late final Uint8List _ktx2;
34 @override
35 Scene build(DemoContext context) {
36 // A 16x16 checkerboard, whole 4x4 blocks in both directions, which
37 // every encoder here requires.
38 final pixels = Uint8List(16 * 16 * 4);
39 for (var y = 0; y < 16; y++) {
40 for (var x = 0; x < 16; x++) {
41 final lit = ((x ~/ 4) + (y ~/ 4)).isEven;
42 final o = (y * 16 + x) * 4;
43 final value = lit ? 220 : 40;
44 pixels[o] = value;
45 pixels[o + 1] = value;
46 pixels[o + 2] = lit ? 60 : 200;
47 pixels[o + 3] = 255;
48 }
49 }
50 _source = Rgba8Image(width: 16, height: 16, pixels: pixels);
52 _bc1 = encodeBc1(_source);
53 _bc3 = encodeBc3(_source);
54 _etc2 = encodeEtc2Rgb8(_source);
55 _astc = encodeAstc4x4(_source);
57 // A block encoder needs whole 4x4 blocks, so the chain stops at the
58 // last level that still is one: 16, 8, 4, and no further.
59 final chain = buildMipChain(
60 _source,
61 ).where((level) => level.width % 4 == 0 && level.height % 4 == 0);
62 _ktx2 = writeKtx2(
63 vkFormat: VkFormat.bc1RgbaUNormBlock,
64 pixelWidth: _source.width,
65 pixelHeight: _source.height,
66 levels: <Uint8List>[for (final level in chain) encodeBc1(level)],
67 );
69 return Scene()
70 ..add(
71 MeshNode(
72 DeviceMesh.upload(context.device, CuboidShape().build()),
73 RenderMaterial(baseColor: LinearColor.fromSrgb(0.7, 0.7, 0.75, 1.0)),
74 name: 'block',
75 ),
76 )
77 ..add(
78 LightNode(name: 'sun', intensity: 3.0 * LightNode.legacyUnit)
79 ..setLocalForward(Vector3(-0.4, -1.0, -0.3)),
80 );
81 }
83 int get _rawBytes => _source.pixels.length;
85 String _report() =>
86 'raw RGBA8: $_rawBytes bytes\n'
87 'BC1: ${_bc1.length} bytes (${(_rawBytes / _bc1.length).toStringAsFixed(1)}x)\n'
88 'BC3: ${_bc3.length} bytes (${(_rawBytes / _bc3.length).toStringAsFixed(1)}x)\n'
89 'ETC2: ${_etc2.length} bytes (${(_rawBytes / _etc2.length).toStringAsFixed(1)}x)\n'
90 'ASTC 4x4: ${_astc.length} bytes '
91 '(${(_rawBytes / _astc.length).toStringAsFixed(1)}x)\n'
92 'KTX2 with a mip chain: ${_ktx2.length} bytes';
94 @override
95 Widget? customBody(BuildContext buildContext, DemoContext context) =>
96 Container(
97 color: const Color(0xFF14161A),
98 padding: const EdgeInsets.all(24),
99 alignment: Alignment.topLeft,
100 child: DefaultTextStyle(
101 style: const TextStyle(
102 color: Color(0xFFE8E8EC),
103 fontSize: 16,
104 fontFamily: 'monospace',
105 ),
106 child: Text(_report()),
107 ),
108 );
110 @override
111 void verify(Scene scene, FrameResult frame) {
112 // Every block format above packs a 4x4 patch of pixels into fewer
113 // bytes than the raw pixels take, or it is not compression.
114 if (_bc1.length >= _rawBytes ||
115 _bc3.length >= _rawBytes ||
116 _etc2.length >= _rawBytes ||
117 _astc.length >= _rawBytes) {
118 throw StateError('a compressed block is not smaller than the raw one');
119 }
120 if (_ktx2.length < 12) {
121 throw StateError('writeKtx2 did not produce a file');
122 }
123 if (frame.drawCalls < 1) {
124 throw StateError('the block was not drawn');
125 }
126 }