@wildshard/shard-format 0.82.3 → 0.82.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wildshard/shard-format",
3
- "version": "0.82.3",
3
+ "version": "0.82.4",
4
4
  "description": "The raw shardfile format: the spec, the validator and a validate CLI. The public, versioned contract every other repo builds on.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
@@ -0,0 +1,144 @@
1
+ import type { Shardfile } from './shardSchema.ts'
2
+
3
+ /** STANDALONE-SHARDFILES SF5g (J525): a shardfile carries images only, and the cook encodes each texture image to the
4
+ * KTX2 a page reads in its place (blobKtx2), so the memory estimate charges an image as that KTX2. An image row holds no
5
+ * width or height, only `gpu`, its RGBA texels (`stillImage` envelope); textures are square or 2:1, so the size is read
6
+ * back from it, and any other count is charged as the square that holds it. Pine Hollow 0.0.18 was refused at a 1.67 GB
7
+ * phone peak counted as source RGBA, where its hand-made KTX2 sets (0.0.17) had been 1.31 GB. */
8
+
9
+ const RGBA_BYTES = 4
10
+ const WIDE = 2
11
+ const NO_BYTES = 0
12
+ const BLOCK_BYTES = 16
13
+ const BC_BLOCK = 4
14
+ const ASTC_PHONE_BLOCK = 6
15
+ const TEXEL = 1
16
+
17
+ interface Chain {
18
+ readonly block: number
19
+ readonly bytes: number
20
+ }
21
+ const RGBA_CHAIN: Chain = { block: TEXEL, bytes: RGBA_BYTES }
22
+ const BLOCK_CHAIN: Chain = { block: BC_BLOCK, bytes: BLOCK_BYTES }
23
+ const PHONE_CHAIN: Chain = { block: ASTC_PHONE_BLOCK, bytes: BLOCK_BYTES }
24
+
25
+ /** The width and height of an image whose RGBA texels cost `rgba`. */
26
+ function imageSize(rgba: number): readonly [number, number] {
27
+ const texels = Math.ceil(rgba / RGBA_BYTES)
28
+ const side = Math.round(Math.sqrt(texels))
29
+ const wide = Math.round(Math.sqrt(texels * WIDE))
30
+ if (side * side === texels) {
31
+ return [side, side]
32
+ }
33
+ if (wide % WIDE === NO_BYTES && wide * (wide / WIDE) === texels) {
34
+ return [wide, wide / WIDE]
35
+ }
36
+ const square = Math.ceil(Math.sqrt(texels))
37
+ return [square, square]
38
+ }
39
+
40
+ /** A full mip chain's bytes, each level in the chain's blocks (a block of one 4-byte texel for RGBA). */
41
+ function mipChainBytes(rgba: number, chain: Chain): number {
42
+ const [width, height] = imageSize(rgba)
43
+ let total = NO_BYTES
44
+ let levelWidth = width
45
+ let levelHeight = height
46
+ for (;;) {
47
+ total +=
48
+ Math.ceil(levelWidth / chain.block) *
49
+ Math.ceil(levelHeight / chain.block) *
50
+ chain.bytes
51
+ if (levelWidth === TEXEL && levelHeight === TEXEL) {
52
+ return total
53
+ }
54
+ levelWidth = Math.max(TEXEL, Math.floor(levelWidth / WIDE))
55
+ levelHeight = Math.max(TEXEL, Math.floor(levelHeight / WIDE))
56
+ }
57
+ }
58
+
59
+ /** What a library image no longer charges once charged as the KTX2 the cook makes from it (`parseKtx2`): its decoded
60
+ * copy, and its GPU bytes become a full mip chain, RGBA without a transcode, 4x4 blocks of 16 bytes with one. */
61
+ function cookedImageSaving(
62
+ file: Readonly<{ decoded: number; gpu: number }>,
63
+ transcodes: boolean,
64
+ ): number {
65
+ const cooked = mipChainBytes(file.gpu, transcodes ? BLOCK_CHAIN : RGBA_CHAIN)
66
+ return Math.max(NO_BYTES, file.decoded + file.gpu - cooked)
67
+ }
68
+
69
+ type ShardFile = Shardfile['files'][number]
70
+
71
+ /** The shard's files by hash. */
72
+ function filesByHash(
73
+ // oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- format's shard row, only read
74
+ source: Shardfile,
75
+ ): Map<string, ShardFile> {
76
+ const files = new Map<string, ShardFile>()
77
+ for (const file of source.files) {
78
+ files.set(file.hash, file)
79
+ }
80
+ return files
81
+ }
82
+
83
+ /** A file once: the first ask has it, later asks get none. */
84
+ function take(
85
+ files: Map<string, ShardFile>,
86
+ ref: string,
87
+ ): ShardFile | undefined {
88
+ const file = files.get(ref)
89
+ files.delete(ref)
90
+ return file
91
+ }
92
+
93
+ /** The library images the shard reaches, its library and their dependencies, each once. */
94
+ function libraryImages(
95
+ // oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- format's shard row, only read
96
+ source: Shardfile,
97
+ ): ShardFile[] {
98
+ const files = filesByHash(source)
99
+ const pending = [...source.library]
100
+ const images: ShardFile[] = []
101
+ for (let ref = pending.pop(); ref !== undefined; ref = pending.pop()) {
102
+ const file = take(files, ref)
103
+ pending.push(...(file?.dependencies ?? []))
104
+ if (file?.kind === 'image') {
105
+ images.push(file)
106
+ }
107
+ }
108
+ return images
109
+ }
110
+
111
+ /** The shard with every library image charged as the KTX2 the cook makes from it (`cookedImageSaving`, no transcode):
112
+ * what the offline memory lines and the validation receipt judge, as admission's `textureLibrarySource` does. */
113
+ function cookedImageSource(
114
+ // oxlint-disable-next-line typescript/prefer-readonly-parameter-types -- format's shard row, only read
115
+ source: Shardfile,
116
+ ): Shardfile {
117
+ let saving = NO_BYTES
118
+ for (const file of libraryImages(source)) {
119
+ saving += cookedImageSaving(file, false)
120
+ }
121
+ if (saving === NO_BYTES) {
122
+ return source
123
+ }
124
+ const { library } = source.budgets
125
+ return {
126
+ ...source,
127
+ budgets: {
128
+ ...source.budgets,
129
+ library: {
130
+ ...library,
131
+ resident: Math.max(NO_BYTES, library.resident - saving),
132
+ },
133
+ },
134
+ }
135
+ }
136
+
137
+ /** The GPU bytes of the phone file the cook makes from an image whose RGBA texels cost `rgba` (an image row's `gpu`):
138
+ * ASTC 6x6, 16 bytes a 6x6 block, with its mip chain. Charged at the image's own size, though the cook trims most phone
139
+ * rows to 512 (`phoneTierSize`): a ground layer or a sky keeps its size, and a row can't tell them apart. */
140
+ function cookedPhoneGpu(rgba: number): number {
141
+ return mipChainBytes(rgba, PHONE_CHAIN)
142
+ }
143
+
144
+ export { cookedImageSaving, cookedImageSource, cookedPhoneGpu }
@@ -4,6 +4,7 @@ import { worstContentCostSteps, type worstContentCost } from './budget.ts';
4
4
  import type { Shardfile } from './shardSchema.ts';
5
5
  import type { MemoryAdmission } from './memoryAdmission.ts';
6
6
  import { parseKtx2, ktx2DeviceGpu, type Ktx2DeviceCosts } from './ktx2Cost.ts';
7
+ import { cookedImageSaving } from './cooked-image-cost.ts';
7
8
 
8
9
  /** Apply the existing total-memory policy to declared or measured costs. */
9
10
  export function acceptsShardfileCost(memory: MemoryAdmission | undefined, source: Shardfile, stage: 'declared' | 'actual', cost: ReturnType<typeof worstContentCost>): boolean {
@@ -30,6 +31,9 @@ export function textureLibrarySource(source: Shardfile, assets: ReadonlyMap<stri
30
31
  seen.add(ref);
31
32
  const file = files.get(ref); if (file === undefined) continue;
32
33
  pending.push(...file.dependencies);
34
+ if (file.kind === 'image') {
35
+ saving += cookedImageSaving(file, transcodes);
36
+ }
33
37
  if (file.kind !== 'ktx2') continue;
34
38
  saving += file.decoded;
35
39
  if (!transcodes) continue;
@@ -2,6 +2,7 @@ import { parse } from 'valibot'
2
2
 
3
3
  import { bootMemoryCosts } from './boot-memory-cost.ts'
4
4
  import { worstContentCost } from './budget.ts'
5
+ import { cookedImageSource } from './cooked-image-cost.ts'
5
6
  import {
6
7
  ARMED_LINES,
7
8
  armedZone,
@@ -139,7 +140,8 @@ function offlineMemoryIssues(
139
140
  (sum, row): number => sum + row.decoded + row.gpu,
140
141
  EMPTY_BYTES,
141
142
  )
142
- const cost = worstContentCost(source, {
143
+ // SF5g: a library image is charged as the KTX2 the cook makes from it (J525).
144
+ const cost = worstContentCost(cookedImageSource(source), {
143
145
  commons: runtime + commons,
144
146
  boot: bootMemoryCosts(source, sections, { mountedImages, index }),
145
147
  })
@@ -1,4 +1,5 @@
1
1
  // MEMORY-GATES MG6 epoch 5 (mini-em11b): the files a phone never fetches, for the offline estimate's terms.
2
+ import { cookedPhoneGpu } from './cooked-image-cost.ts'
2
3
  import { tierLibrarySteps } from './tierSelection.ts'
3
4
 
4
5
  import type { Shardfile } from './shardSchema.ts'
@@ -51,8 +52,12 @@ function contentBytes(
51
52
  let bytes = mountedImages
52
53
  for (const file of shard.files) {
53
54
  if (!inactive.has(file.hash)) {
55
+ // SF5g (J525): the cook encodes an image to the KTX2 a phone uploads, so the phone never decodes it.
54
56
  bytes +=
55
- file.compressed + (file.kind === 'audio' ? NO_BYTES : file.decoded)
57
+ file.compressed +
58
+ (file.kind === 'audio' || file.kind === 'image'
59
+ ? NO_BYTES
60
+ : file.decoded)
56
61
  }
57
62
  }
58
63
  for (const wire of Object.values(shard.requires.commonsWire)) {
@@ -74,7 +79,8 @@ function gpuBytes(
74
79
  let bytes = NO_BYTES
75
80
  for (const file of shard.files) {
76
81
  if (kinds.has(file.kind) && !inactive.has(file.hash)) {
77
- bytes += file.gpu
82
+ // SF5g (J525): an image uploads as the cook's phone ASTC 6x6 file, not as RGBA.
83
+ bytes += file.kind === 'image' ? cookedPhoneGpu(file.gpu) : file.gpu
78
84
  }
79
85
  }
80
86
  return bytes
@@ -19,6 +19,7 @@ import type { Judged } from './policy-verdict.ts';
19
19
  import { staleRuntimeIssues } from './memory-epoch.ts';
20
20
  import { MEMORY_HARD_BYTES, memoryLineIssues, runtimeOfflineBytes } from './memory-line.ts';
21
21
  import { bootMemoryCosts } from './boot-memory-cost.ts';
22
+ import { cookedImageSource } from './cooked-image-cost.ts';
22
23
  import type { TierIndex } from './phone-files.ts';
23
24
  import { VIEW_DRAWS, viewDraws } from './view-draws.ts';
24
25
  import { DEFAULT_GRAPH_BUDGET } from "./materialGraph.ts";
@@ -35,7 +36,9 @@ interface PerformanceOptions { readonly policy: PerformancePolicy; readonly sect
35
36
  function reportMemory(source: Shardfile, nativeBytes: number, options: Readonly<{ sections: ReadonlyMap<string, unknown>; mountedImages?: number; index?: TierIndex }>): PerformanceReport['memory'] {
36
37
  const commons = nativeBytes + Object.values(source.requires.commonsCosts).reduce((sum, row) => sum + row.decoded + row.gpu, 0);
37
38
  const budget = { commons, boot: bootMemoryCosts(source, options.sections, options) };
38
- return { near: nearbyContentCost(source, source.spawn.x, source.spawn.z, budget), worst: worstContentCost(source, budget), playingLimit: ARMED_LINES.playing, loadingLimit: ARMED_LINES.loading };
39
+ // SF5g: a library image is charged as the KTX2 the cook makes from it (J525).
40
+ const cooked = cookedImageSource(source);
41
+ return { near: nearbyContentCost(cooked, source.spawn.x, source.spawn.z, budget), worst: worstContentCost(cooked, budget), playingLimit: ARMED_LINES.playing, loadingLimit: ARMED_LINES.loading };
39
42
  }
40
43
  /** A performance issue's code and its metric words. */
41
44
  type Named = readonly [code: IssueCodeOf<'performance'>, metric: string];