@carbonenginejs/runtime-resource 0.12.0 → 0.12.1
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/dist/CjsResMan.js +4 -4
- package/dist/CjsResMan.js.map +1 -1
- package/dist/_virtual/_rollupPluginBabelHelpers.js +1 -4
- package/dist/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
- package/dist/format/CjsByteReader.js +310 -0
- package/dist/format/CjsByteReader.js.map +1 -0
- package/dist/format/CjsByteWriter.js +242 -0
- package/dist/format/CjsByteWriter.js.map +1 -0
- package/dist/format/CjsFormatError.js +41 -0
- package/dist/format/CjsFormatError.js.map +1 -0
- package/dist/format/CjsStringTable.js +268 -0
- package/dist/format/CjsStringTable.js.map +1 -0
- package/dist/format/carbonEffect/CjsCarbonEffectReader.js +361 -0
- package/dist/format/carbonEffect/CjsCarbonEffectReader.js.map +1 -0
- package/dist/format/carbonEffect/CjsCarbonEffectWriter.js +373 -0
- package/dist/format/carbonEffect/CjsCarbonEffectWriter.js.map +1 -0
- package/dist/format/carbonEffect/carbonDescriptionFromPortable.js +372 -0
- package/dist/format/carbonEffect/carbonDescriptionFromPortable.js.map +1 -0
- package/dist/format/carbonEffect/carbonEffectBackendBlock.js +427 -0
- package/dist/format/carbonEffect/carbonEffectBackendBlock.js.map +1 -0
- package/dist/format/carbonEffect/carbonEffectRecords.js +955 -0
- package/dist/format/carbonEffect/carbonEffectRecords.js.map +1 -0
- package/dist/format/compareUtf8.js +36 -0
- package/dist/format/compareUtf8.js.map +1 -0
- package/dist/format/index.js +11 -0
- package/dist/format/index.js.map +1 -0
- package/dist/formats/bnk/CjsBnkFormat.js +10 -4
- package/dist/formats/bnk/CjsBnkFormat.js.map +1 -1
- package/dist/formats/bnk/core/eventAction.js +305 -0
- package/dist/formats/bnk/core/eventAction.js.map +1 -0
- package/dist/formats/bnk/core/helpers.js +11 -2
- package/dist/formats/bnk/core/helpers.js.map +1 -1
- package/dist/formats/bnk/core/nodeBase.js +532 -0
- package/dist/formats/bnk/core/nodeBase.js.map +1 -0
- package/dist/formats/bnk/core/sfxNodes.js +252 -152
- package/dist/formats/bnk/core/sfxNodes.js.map +1 -1
- package/dist/formats/hlsl/core/HlslReader.js +7 -257
- package/dist/formats/hlsl/core/HlslReader.js.map +1 -1
- package/dist/formats/hlsl/core/carbonDescriptionToRuntime.js +781 -0
- package/dist/formats/hlsl/core/carbonDescriptionToRuntime.js.map +1 -0
- package/dist/formats/hlsl/core/tr2/resources/HlslEffectRes.js +21 -1
- package/dist/formats/hlsl/core/tr2/resources/HlslEffectRes.js.map +1 -1
- package/dist/formats/index.js +1 -1
- package/dist/formats/webgl/core/cewg/CewgPackage.js +2 -1
- package/dist/formats/webgl/core/cewg/CewgPackage.js.map +1 -1
- package/dist/formats/webgl/core/cewg/binary.js +11 -93
- package/dist/formats/webgl/core/cewg/binary.js.map +1 -1
- package/dist/formats/webgl/core/effectPackage.js +1 -1
- package/dist/formats/webgpu/CjsWebgpuFormat.js +4 -23
- package/dist/formats/webgpu/CjsWebgpuFormat.js.map +1 -1
- package/dist/formats/webgpu/core/buildCarbonEffectContainer.js +197 -0
- package/dist/formats/webgpu/core/buildCarbonEffectContainer.js.map +1 -0
- package/dist/formats/webgpu/core/cewgpu/CewgpuContainer.js +368 -0
- package/dist/formats/webgpu/core/cewgpu/CewgpuContainer.js.map +1 -0
- package/dist/formats/webgpu/core/cewgpu/containerViews.js +355 -0
- package/dist/formats/webgpu/core/cewgpu/containerViews.js.map +1 -0
- package/dist/formats/webgpu/core/cewgpu/validateContainer.js +90 -0
- package/dist/formats/webgpu/core/cewgpu/validateContainer.js.map +1 -0
- package/dist/formats/webgpu/core/effectBackendBodySet.js +21 -20
- package/dist/formats/webgpu/core/effectBackendBodySet.js.map +1 -1
- package/dist/formats/webgpu/core/helpers.js +87 -92
- package/dist/formats/webgpu/core/helpers.js.map +1 -1
- package/dist/formats/webgpu/core/ir/analyzeRegisterValues.js +14 -13
- package/dist/formats/webgpu/core/ir/analyzeRegisterValues.js.map +1 -1
- package/dist/formats/webgpu/core/ir/inferValueTypes.js +10 -8
- package/dist/formats/webgpu/core/ir/inferValueTypes.js.map +1 -1
- package/dist/formats/webgpu/core/packageEffect.js +94 -9
- package/dist/formats/webgpu/core/packageEffect.js.map +1 -1
- package/dist/formats/webgpu/core/packageMetadata.js +10 -1
- package/dist/formats/webgpu/core/packageMetadata.js.map +1 -1
- package/dist/formats/webgpu/core/wgsl/buildResourceTransformPlan.js +17 -15
- package/dist/formats/webgpu/core/wgsl/buildResourceTransformPlan.js.map +1 -1
- package/dist/formats/webgpu/core/wgsl/buildWgslBindingPlan.js +13 -12
- package/dist/formats/webgpu/core/wgsl/buildWgslBindingPlan.js.map +1 -1
- package/dist/formats/webgpu/core/wgsl/buildWgslSet.js +9 -8
- package/dist/formats/webgpu/core/wgsl/buildWgslSet.js.map +1 -1
- package/dist/formats/webgpu/core/wgsl/lowerComputeProgram.js +23 -22
- package/dist/formats/webgpu/core/wgsl/lowerComputeProgram.js.map +1 -1
- package/dist/formats/webgpu/core/wgsl/selectionPlans.js +46 -44
- package/dist/formats/webgpu/core/wgsl/selectionPlans.js.map +1 -1
- package/dist/resource/CjsResource.js +700 -684
- package/dist/resource/CjsResource.js.map +1 -1
- package/dist/resource/Tr2LightProfileRes.js +18 -27
- package/dist/resource/Tr2LightProfileRes.js.map +1 -1
- package/dist/resource/audio/CjsAudioBufferRes.js +2 -2
- package/dist/resource/audio/CjsAudioBufferRes.js.map +1 -1
- package/dist/resource/audio/CjsAudioRes.js +2 -2
- package/dist/resource/audio/CjsAudioRes.js.map +1 -1
- package/dist/resource/geometry/TriGeometryRes.js +605 -586
- package/dist/resource/geometry/TriGeometryRes.js.map +1 -1
- package/dist/resource/geometry/granny/Tr2GrannyStateRes.js +21 -30
- package/dist/resource/geometry/granny/Tr2GrannyStateRes.js.map +1 -1
- package/dist/resource/geometry/granny/TriGrannyRes.js +21 -30
- package/dist/resource/geometry/granny/TriGrannyRes.js.map +1 -1
- package/dist/resource/shader/Tr2EffectRes.js +9 -7
- package/dist/resource/shader/Tr2EffectRes.js.map +1 -1
- package/dist/resource/shader/Tr2MaterialArea.js +5 -3
- package/dist/resource/shader/Tr2MaterialArea.js.map +1 -1
- package/dist/resource/shader/Tr2MaterialMesh.js +4 -2
- package/dist/resource/shader/Tr2MaterialMesh.js.map +1 -1
- package/dist/resource/shader/Tr2MaterialRes.js +5 -3
- package/dist/resource/shader/Tr2MaterialRes.js.map +1 -1
- package/dist/resource/shader/Tr2Shader.js +26 -21
- package/dist/resource/shader/Tr2Shader.js.map +1 -1
- package/dist/resource/shader/Tr2ShaderPermutation.js +8 -6
- package/dist/resource/shader/Tr2ShaderPermutation.js.map +1 -1
- package/dist/resource/shader/reflection/Tr2EffectDefine.js +5 -3
- package/dist/resource/shader/reflection/Tr2EffectDefine.js.map +1 -1
- package/dist/resource/shader/reflection/Tr2EffectDescription.js +5 -3
- package/dist/resource/shader/reflection/Tr2EffectDescription.js.map +1 -1
- package/dist/resource/shader/reflection/Tr2EffectLibrary.js +17 -15
- package/dist/resource/shader/reflection/Tr2EffectLibrary.js.map +1 -1
- package/dist/resource/shader/reflection/Tr2EffectParameterAnnotation.js +11 -9
- package/dist/resource/shader/reflection/Tr2EffectParameterAnnotation.js.map +1 -1
- package/dist/resource/shader/reflection/Tr2EffectResource.js +8 -6
- package/dist/resource/shader/reflection/Tr2EffectResource.js.map +1 -1
- package/dist/resource/shader/reflection/Tr2EffectStageInput.js +15 -13
- package/dist/resource/shader/reflection/Tr2EffectStageInput.js.map +1 -1
- package/dist/resource/shader/reflection/Tr2EffectTechnique.js +7 -5
- package/dist/resource/shader/reflection/Tr2EffectTechnique.js.map +1 -1
- package/dist/resource/shader/reflection/Tr2Pass.js +11 -9
- package/dist/resource/shader/reflection/Tr2Pass.js.map +1 -1
- package/dist/resource/shader/sampler/Tr2SamplerSetup.js +8 -6
- package/dist/resource/shader/sampler/Tr2SamplerSetup.js.map +1 -1
- package/dist/resource/texture/CjsTextureArrayRes.js +400 -411
- package/dist/resource/texture/CjsTextureArrayRes.js.map +1 -1
- package/dist/resource/texture/Tr2ImageRes.js +99 -95
- package/dist/resource/texture/Tr2ImageRes.js.map +1 -1
- package/dist/resource/texture/TriTextureRes.js +312 -273
- package/dist/resource/texture/TriTextureRes.js.map +1 -1
- package/docs/README.md +13 -1
- package/docs/architecture.md +3 -3
- package/docs/concepts/resource-lifecycle.md +9 -2
- package/docs/concepts/shader-resource-model.md +114 -0
- package/docs/concepts/writing-an-engine-adapter.md +115 -0
- package/docs/formats/README.md +24 -1
- package/docs/formats/carbon-effect-container.md +452 -0
- package/docs/formats/dxbc/reference/classes/README.md +5 -72
- package/docs/formats/hlsl/reference/api.md +5 -4
- package/docs/formats/hlsl/reference/classes/README.md +6 -11
- package/docs/formats/provenance.md +23 -13
- package/docs/formats/webgl/reference/classes/README.md +5 -92
- package/docs/formats/webgpu/README.md +19 -16
- package/docs/formats/webgpu/architecture.md +15 -12
- package/docs/formats/webgpu/formats/cewgpu.md +175 -438
- package/docs/formats/webgpu/guides/effect-packaging.md +132 -140
- package/docs/formats/webgpu/reference/api.md +131 -131
- package/docs/formats/webgpu/reference/classes/README.md +5 -62
- package/docs/formats/webgpu/reference/wgsl-compatibility.md +146 -45
- package/docs/formats/wwise.md +34 -8
- package/docs/reference/classes/core.md +80 -0
- package/docs/reference/classes/formats.md +18 -28
- package/docs/reference/events.md +25 -0
- package/docs/reference/motherlode-cache.md +22 -8
- package/docs/reference/workers.md +5 -5
- package/docs/roadmap.md +61 -41
- package/package.json +2 -1
- package/dist/formats/webgpu/core/cewgpu/CewgpuPackage.js +0 -415
- package/dist/formats/webgpu/core/cewgpu/CewgpuPackage.js.map +0 -1
- package/dist/formats/webgpu/core/cewgpu/CewgpuPackageBuilder.js +0 -100
- package/dist/formats/webgpu/core/cewgpu/CewgpuPackageBuilder.js.map +0 -1
- package/dist/formats/webgpu/core/cewgpu/binary.js +0 -93
- package/dist/formats/webgpu/core/cewgpu/binary.js.map +0 -1
- package/dist/formats/webgpu/core/cewgpu/tags.js +0 -17
- package/dist/formats/webgpu/core/cewgpu/tags.js.map +0 -1
- package/dist/formats/webgpu/core/effectPackageValidation.js +0 -1078
- package/dist/formats/webgpu/core/effectPackageValidation.js.map +0 -1
- package/docs/formats/hlsl/reference/classes/carbon-compatibility.md +0 -66
- package/docs/formats/hlsl/reference/classes/public-api.md +0 -26
- package/docs/formats/hlsl/reference/classes/tr2-effect-model.md +0 -150
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CjsFormatError.js","sources":["../../../src/format/CjsFormatError.js"],"sourcesContent":["/**\n * Error raised when shared binary format bytes cannot be decoded safely.\n *\n * Format-specific readers substitute their own error class through the\n * `ReadError` static on `CjsByteReader`, so this type is the fallback for\n * readers that do not need a distinguishable name.\n */\nexport class CjsFormatReadError extends Error\n{\n /**\n * Creates a read error with structured reader-state details.\n *\n * @param {string} message Human-readable failure reason.\n * @param {object} [details] Extra reader state such as source, offset, or size.\n */\n constructor(message, details = {})\n {\n super(message);\n this.name = \"CjsFormatReadError\";\n this.details = details;\n }\n}\n\n/**\n * Error raised when shared binary format bytes cannot be encoded safely.\n */\nexport class CjsFormatWriteError extends Error\n{\n /**\n * Creates a write error with structured writer-state details.\n *\n * @param {string} message Human-readable failure reason.\n * @param {object} [details] Extra writer state such as offset or value.\n */\n constructor(message, details = {})\n {\n super(message);\n this.name = \"CjsFormatWriteError\";\n this.code = \"CJS_FORMAT_WRITE_ERROR\";\n this.details = details;\n }\n}\n\nexport default CjsFormatReadError;\n"],"names":["CjsFormatReadError","Error","constructor","message","details","name","CjsFormatWriteError","code"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,kBAAkB,SAASC,KAAK,CAC7C;AACI;AACJ;AACA;AACA;AACA;AACA;AACIC,EAAAA,WAAWA,CAACC,OAAO,EAAEC,OAAO,GAAG,EAAE,EACjC;IACI,KAAK,CAACD,OAAO,CAAC;IACd,IAAI,CAACE,IAAI,GAAG,oBAAoB;IAChC,IAAI,CAACD,OAAO,GAAGA,OAAO;AAC1B,EAAA;AACJ;;AAEA;AACA;AACA;AACO,MAAME,mBAAmB,SAASL,KAAK,CAC9C;AACI;AACJ;AACA;AACA;AACA;AACA;AACIC,EAAAA,WAAWA,CAACC,OAAO,EAAEC,OAAO,GAAG,EAAE,EACjC;IACI,KAAK,CAACD,OAAO,CAAC;IACd,IAAI,CAACE,IAAI,GAAG,qBAAqB;IACjC,IAAI,CAACE,IAAI,GAAG,wBAAwB;IACpC,IAAI,CAACH,OAAO,GAAGA,OAAO;AAC1B,EAAA;AACJ;;;;"}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { CjsFormatWriteError } from './CjsFormatError.js';
|
|
2
|
+
|
|
3
|
+
// Source: trinity/shadercompiler/StringTable.cpp, trinity/shadercompiler/StringTable.h
|
|
4
|
+
|
|
5
|
+
const textEncoder = new TextEncoder();
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The null arena reference. `StringTable::GetOffset` returns this for an
|
|
9
|
+
* invalid or unknown reference (`StringTable.cpp:52-68`), and Carbon's reader
|
|
10
|
+
* tolerates it at exactly one wire position: a stage's default-constant-value
|
|
11
|
+
* offset when the accompanying size is zero
|
|
12
|
+
* (`Tr2EffectDescription.cpp:80-91`). Everywhere else it fails the load.
|
|
13
|
+
*/
|
|
14
|
+
const CJS_STRING_TABLE_NULL_REFERENCE = 0xffffffff;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Compares two byte runs the way Carbon's `Blob::operator<` does: `memcmp`
|
|
18
|
+
* over the shorter length, then shorter-wins on a tie
|
|
19
|
+
* (`StringTable.h:109-122`).
|
|
20
|
+
*
|
|
21
|
+
* @param {Uint8Array} a First byte run.
|
|
22
|
+
* @param {Uint8Array} b Second byte run.
|
|
23
|
+
* @returns {number} Negative, zero, or positive ordering result.
|
|
24
|
+
*/
|
|
25
|
+
function compareTableBlobs(a, b) {
|
|
26
|
+
const shared = Math.min(a.length, b.length);
|
|
27
|
+
for (let index = 0; index < shared; index += 1) {
|
|
28
|
+
if (a[index] !== b[index]) return a[index] - b[index];
|
|
29
|
+
}
|
|
30
|
+
return a.length - b.length;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* FNV-1a over every byte, used only to bucket dedupe candidates.
|
|
35
|
+
*
|
|
36
|
+
* Carbon hashes a stride-sampled subset (`StringTable.h:124-134`); hashing all
|
|
37
|
+
* bytes costs the same asymptotically and collides less. Either way the
|
|
38
|
+
* decision is made by the full byte comparison that follows, so the choice of
|
|
39
|
+
* hash cannot change the emitted arena.
|
|
40
|
+
*
|
|
41
|
+
* @param {Uint8Array} bytes Byte run to hash.
|
|
42
|
+
* @returns {number} 32-bit hash.
|
|
43
|
+
*/
|
|
44
|
+
function hashBytes(bytes) {
|
|
45
|
+
let hash = 0x811c9dc5;
|
|
46
|
+
for (let index = 0; index < bytes.length; index += 1) {
|
|
47
|
+
hash ^= bytes[index];
|
|
48
|
+
hash = Math.imul(hash, 0x01000193);
|
|
49
|
+
}
|
|
50
|
+
return hash >>> 0;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Carbon's compiled-effect string table: a deduplicated blob arena whose
|
|
55
|
+
* offsets are assigned by a bytewise sort rather than by insertion order.
|
|
56
|
+
*
|
|
57
|
+
* The arena holds three kinds of entry, distinguished only by how the wire
|
|
58
|
+
* refers to them:
|
|
59
|
+
*
|
|
60
|
+
* - NUL-terminated strings, added with the terminator (`strlen + 1`,
|
|
61
|
+
* `StringTable.cpp:18-21`), referenced by a bare `u32` offset;
|
|
62
|
+
* - sized blobs — shader bytecode, program source, default constant values —
|
|
63
|
+
* added with exactly their own bytes and no terminator
|
|
64
|
+
* (`StringTable.cpp:23-41`), referenced by a `{u32 size, u32 offset}` pair;
|
|
65
|
+
* - nothing else. Carbon persists no manifest: `StringTable::Write` emits a
|
|
66
|
+
* `u32` payload size and the payload (`StringTable.cpp:110-143`), and every
|
|
67
|
+
* reference site resolves its own entry.
|
|
68
|
+
*
|
|
69
|
+
* Two deliberate departures from Carbon, both of which make byte-identical
|
|
70
|
+
* output more likely rather than less:
|
|
71
|
+
*
|
|
72
|
+
* - `m_size` is initialised. Carbon's constructor leaves it indeterminate
|
|
73
|
+
* (`StringTable.cpp:9-12`, `StringTable.h:170`) and gets away with it only
|
|
74
|
+
* because the one instance is a zero-initialised global.
|
|
75
|
+
* - adding after an offset has been handed out is an error rather than a silent
|
|
76
|
+
* resort. In Carbon, `GetOffset` re-sorts whenever the table is dirty
|
|
77
|
+
* (`StringTable.cpp:59-62`), which reassigns *every* offset — including
|
|
78
|
+
* offsets already baked into packed bodies. Carbon avoids the corruption by
|
|
79
|
+
* interning all late strings before the packing pass; we fail closed instead.
|
|
80
|
+
*/
|
|
81
|
+
class CjsStringTable {
|
|
82
|
+
#blobs = [];
|
|
83
|
+
#buckets = new Map();
|
|
84
|
+
#byteLength = 0;
|
|
85
|
+
#offsets = null;
|
|
86
|
+
#handedOutOffsets = false;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns the arena payload byte count, excluding the `u32` size prefix.
|
|
90
|
+
*
|
|
91
|
+
* This is Carbon's `m_size` and the value the reader bounds-checks every
|
|
92
|
+
* offset against.
|
|
93
|
+
*
|
|
94
|
+
* @returns {number} Payload byte count.
|
|
95
|
+
*/
|
|
96
|
+
get byteLength() {
|
|
97
|
+
return this.#byteLength;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Returns the byte count the arena occupies in the file, including its own
|
|
102
|
+
* `u32` size prefix.
|
|
103
|
+
*
|
|
104
|
+
* This is Carbon's `GetSize()` (`StringTable.cpp:82-85`), the term used by
|
|
105
|
+
* the body-offset base arithmetic.
|
|
106
|
+
*
|
|
107
|
+
* @returns {number} On-disk byte count.
|
|
108
|
+
*/
|
|
109
|
+
get containerSize() {
|
|
110
|
+
return this.#byteLength + 4;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Returns the number of distinct entries in the arena.
|
|
115
|
+
*
|
|
116
|
+
* @returns {number} Deduplicated entry count.
|
|
117
|
+
*/
|
|
118
|
+
get entryCount() {
|
|
119
|
+
return this.#blobs.length;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Interns a NUL-terminated string and returns its reference.
|
|
124
|
+
*
|
|
125
|
+
* @param {string} value Text to intern, stored with a trailing NUL.
|
|
126
|
+
* @returns {number} Arena reference for later `offsetOf`.
|
|
127
|
+
*/
|
|
128
|
+
addString(value) {
|
|
129
|
+
const text = textEncoder.encode(String(value));
|
|
130
|
+
const bytes = new Uint8Array(text.length + 1);
|
|
131
|
+
bytes.set(text, 0);
|
|
132
|
+
return this.#add(bytes);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Interns exact bytes with no terminator and returns their reference.
|
|
137
|
+
*
|
|
138
|
+
* @param {ArrayBufferView|Uint8Array} value Bytes to intern.
|
|
139
|
+
* @returns {number} Arena reference for later `offsetOf`.
|
|
140
|
+
*/
|
|
141
|
+
addBytes(value) {
|
|
142
|
+
const source = value instanceof Uint8Array ? value : new Uint8Array(value.buffer, value.byteOffset, value.byteLength);
|
|
143
|
+
return this.#add(Uint8Array.from(source));
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Assigns every entry its final offset by Carbon's bytewise sort.
|
|
148
|
+
*
|
|
149
|
+
* Idempotent while the table is unchanged. Returns this table so it can be
|
|
150
|
+
* chained onto construction.
|
|
151
|
+
*
|
|
152
|
+
* @returns {CjsStringTable} This table.
|
|
153
|
+
*/
|
|
154
|
+
finish() {
|
|
155
|
+
if (this.#offsets) return this;
|
|
156
|
+
const order = this.#blobs.map((blob, index) => index);
|
|
157
|
+
order.sort((a, b) => compareTableBlobs(this.#blobs[a], this.#blobs[b]));
|
|
158
|
+
const offsets = new Array(this.#blobs.length);
|
|
159
|
+
let cursor = 0;
|
|
160
|
+
for (const index of order) {
|
|
161
|
+
offsets[index] = cursor;
|
|
162
|
+
cursor += this.#blobs[index].length;
|
|
163
|
+
}
|
|
164
|
+
this.#offsets = offsets;
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Resolves an arena reference to its byte offset.
|
|
170
|
+
*
|
|
171
|
+
* `null` and `undefined` resolve to the `0xffffffff` null reference, so a
|
|
172
|
+
* caller can pass an absent optional field straight through.
|
|
173
|
+
*
|
|
174
|
+
* @param {number|null|undefined} reference Reference from `addString`/`addBytes`.
|
|
175
|
+
* @returns {number} Byte offset, or `0xffffffff` for a null reference.
|
|
176
|
+
*/
|
|
177
|
+
offsetOf(reference) {
|
|
178
|
+
if (reference === null || reference === undefined) {
|
|
179
|
+
return CJS_STRING_TABLE_NULL_REFERENCE;
|
|
180
|
+
}
|
|
181
|
+
if (!Number.isInteger(reference) || reference < 0 || reference >= this.#blobs.length) {
|
|
182
|
+
throw new CjsFormatWriteError("Unknown string-table reference", {
|
|
183
|
+
reference,
|
|
184
|
+
entryCount: this.#blobs.length
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
this.finish();
|
|
188
|
+
this.#handedOutOffsets = true;
|
|
189
|
+
return this.#offsets[reference];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Returns the interned bytes behind a reference.
|
|
194
|
+
*
|
|
195
|
+
* @param {number} reference Reference from `addString`/`addBytes`.
|
|
196
|
+
* @returns {Uint8Array} View over the interned bytes.
|
|
197
|
+
*/
|
|
198
|
+
bytesOf(reference) {
|
|
199
|
+
if (!Number.isInteger(reference) || reference < 0 || reference >= this.#blobs.length) {
|
|
200
|
+
throw new CjsFormatWriteError("Unknown string-table reference", {
|
|
201
|
+
reference,
|
|
202
|
+
entryCount: this.#blobs.length
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
return this.#blobs[reference];
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Materialises the arena payload in sorted order.
|
|
210
|
+
*
|
|
211
|
+
* @returns {Uint8Array} Arena payload, without the `u32` size prefix.
|
|
212
|
+
*/
|
|
213
|
+
toBytes() {
|
|
214
|
+
this.finish();
|
|
215
|
+
const out = new Uint8Array(this.#byteLength);
|
|
216
|
+
for (let index = 0; index < this.#blobs.length; index += 1) {
|
|
217
|
+
out.set(this.#blobs[index], this.#offsets[index]);
|
|
218
|
+
}
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Writes the arena exactly as `StringTable::Write` does: a `u32` payload
|
|
224
|
+
* size followed by the payload (`StringTable.cpp:110-143`).
|
|
225
|
+
*
|
|
226
|
+
* @param {import("./CjsByteWriter.js").CjsByteWriter} writer Target writer.
|
|
227
|
+
*/
|
|
228
|
+
write(writer) {
|
|
229
|
+
this.finish();
|
|
230
|
+
writer.u32(this.#byteLength);
|
|
231
|
+
if (this.#byteLength === 0) return;
|
|
232
|
+
writer.bytes(this.toBytes());
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Interns a byte run, returning an existing reference when the bytes match
|
|
237
|
+
* exactly. Dedupe is on full content and length, with no suffix merging —
|
|
238
|
+
* Carbon's `Blob::operator==` (`StringTable.h:104-107`).
|
|
239
|
+
*
|
|
240
|
+
* @param {Uint8Array} bytes Owned bytes to intern.
|
|
241
|
+
* @returns {number} Arena reference.
|
|
242
|
+
*/
|
|
243
|
+
#add(bytes) {
|
|
244
|
+
const key = hashBytes(bytes);
|
|
245
|
+
const bucket = this.#buckets.get(key);
|
|
246
|
+
if (bucket) {
|
|
247
|
+
for (const candidate of bucket) {
|
|
248
|
+
if (compareTableBlobs(this.#blobs[candidate], bytes) === 0) {
|
|
249
|
+
return candidate;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
if (this.#handedOutOffsets) {
|
|
254
|
+
throw new CjsFormatWriteError("String-table entry added after offsets were resolved; every offset already handed out would shift", {
|
|
255
|
+
byteLength: bytes.length
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
const reference = this.#blobs.length;
|
|
259
|
+
this.#blobs.push(bytes);
|
|
260
|
+
this.#byteLength += bytes.length;
|
|
261
|
+
this.#offsets = null;
|
|
262
|
+
if (bucket) bucket.push(reference);else this.#buckets.set(key, [reference]);
|
|
263
|
+
return reference;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export { CJS_STRING_TABLE_NULL_REFERENCE, CjsStringTable, compareTableBlobs };
|
|
268
|
+
//# sourceMappingURL=CjsStringTable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CjsStringTable.js","sources":["../../../src/format/CjsStringTable.js"],"sourcesContent":["import { CjsFormatWriteError } from \"./CjsFormatError.js\";\n\n// Source: trinity/shadercompiler/StringTable.cpp, trinity/shadercompiler/StringTable.h\n\nconst textEncoder = new TextEncoder();\n\n/**\n * The null arena reference. `StringTable::GetOffset` returns this for an\n * invalid or unknown reference (`StringTable.cpp:52-68`), and Carbon's reader\n * tolerates it at exactly one wire position: a stage's default-constant-value\n * offset when the accompanying size is zero\n * (`Tr2EffectDescription.cpp:80-91`). Everywhere else it fails the load.\n */\nexport const CJS_STRING_TABLE_NULL_REFERENCE = 0xffffffff;\n\n/**\n * Compares two byte runs the way Carbon's `Blob::operator<` does: `memcmp`\n * over the shorter length, then shorter-wins on a tie\n * (`StringTable.h:109-122`).\n *\n * @param {Uint8Array} a First byte run.\n * @param {Uint8Array} b Second byte run.\n * @returns {number} Negative, zero, or positive ordering result.\n */\nexport function compareTableBlobs(a, b)\n{\n const shared = Math.min(a.length, b.length);\n for (let index = 0; index < shared; index += 1)\n {\n if (a[index] !== b[index]) return a[index] - b[index];\n }\n return a.length - b.length;\n}\n\n/**\n * FNV-1a over every byte, used only to bucket dedupe candidates.\n *\n * Carbon hashes a stride-sampled subset (`StringTable.h:124-134`); hashing all\n * bytes costs the same asymptotically and collides less. Either way the\n * decision is made by the full byte comparison that follows, so the choice of\n * hash cannot change the emitted arena.\n *\n * @param {Uint8Array} bytes Byte run to hash.\n * @returns {number} 32-bit hash.\n */\nfunction hashBytes(bytes)\n{\n let hash = 0x811c9dc5;\n for (let index = 0; index < bytes.length; index += 1)\n {\n hash ^= bytes[index];\n hash = Math.imul(hash, 0x01000193);\n }\n return hash >>> 0;\n}\n\n/**\n * Carbon's compiled-effect string table: a deduplicated blob arena whose\n * offsets are assigned by a bytewise sort rather than by insertion order.\n *\n * The arena holds three kinds of entry, distinguished only by how the wire\n * refers to them:\n *\n * - NUL-terminated strings, added with the terminator (`strlen + 1`,\n * `StringTable.cpp:18-21`), referenced by a bare `u32` offset;\n * - sized blobs — shader bytecode, program source, default constant values —\n * added with exactly their own bytes and no terminator\n * (`StringTable.cpp:23-41`), referenced by a `{u32 size, u32 offset}` pair;\n * - nothing else. Carbon persists no manifest: `StringTable::Write` emits a\n * `u32` payload size and the payload (`StringTable.cpp:110-143`), and every\n * reference site resolves its own entry.\n *\n * Two deliberate departures from Carbon, both of which make byte-identical\n * output more likely rather than less:\n *\n * - `m_size` is initialised. Carbon's constructor leaves it indeterminate\n * (`StringTable.cpp:9-12`, `StringTable.h:170`) and gets away with it only\n * because the one instance is a zero-initialised global.\n * - adding after an offset has been handed out is an error rather than a silent\n * resort. In Carbon, `GetOffset` re-sorts whenever the table is dirty\n * (`StringTable.cpp:59-62`), which reassigns *every* offset — including\n * offsets already baked into packed bodies. Carbon avoids the corruption by\n * interning all late strings before the packing pass; we fail closed instead.\n */\nexport class CjsStringTable\n{\n #blobs = [];\n #buckets = new Map();\n #byteLength = 0;\n #offsets = null;\n #handedOutOffsets = false;\n\n /**\n * Returns the arena payload byte count, excluding the `u32` size prefix.\n *\n * This is Carbon's `m_size` and the value the reader bounds-checks every\n * offset against.\n *\n * @returns {number} Payload byte count.\n */\n get byteLength()\n {\n return this.#byteLength;\n }\n\n /**\n * Returns the byte count the arena occupies in the file, including its own\n * `u32` size prefix.\n *\n * This is Carbon's `GetSize()` (`StringTable.cpp:82-85`), the term used by\n * the body-offset base arithmetic.\n *\n * @returns {number} On-disk byte count.\n */\n get containerSize()\n {\n return this.#byteLength + 4;\n }\n\n /**\n * Returns the number of distinct entries in the arena.\n *\n * @returns {number} Deduplicated entry count.\n */\n get entryCount()\n {\n return this.#blobs.length;\n }\n\n /**\n * Interns a NUL-terminated string and returns its reference.\n *\n * @param {string} value Text to intern, stored with a trailing NUL.\n * @returns {number} Arena reference for later `offsetOf`.\n */\n addString(value)\n {\n const text = textEncoder.encode(String(value));\n const bytes = new Uint8Array(text.length + 1);\n bytes.set(text, 0);\n return this.#add(bytes);\n }\n\n /**\n * Interns exact bytes with no terminator and returns their reference.\n *\n * @param {ArrayBufferView|Uint8Array} value Bytes to intern.\n * @returns {number} Arena reference for later `offsetOf`.\n */\n addBytes(value)\n {\n const source = value instanceof Uint8Array\n ? value\n : new Uint8Array(value.buffer, value.byteOffset, value.byteLength);\n return this.#add(Uint8Array.from(source));\n }\n\n /**\n * Assigns every entry its final offset by Carbon's bytewise sort.\n *\n * Idempotent while the table is unchanged. Returns this table so it can be\n * chained onto construction.\n *\n * @returns {CjsStringTable} This table.\n */\n finish()\n {\n if (this.#offsets) return this;\n\n const order = this.#blobs.map((blob, index) => index);\n order.sort((a, b) => compareTableBlobs(this.#blobs[a], this.#blobs[b]));\n\n const offsets = new Array(this.#blobs.length);\n let cursor = 0;\n for (const index of order)\n {\n offsets[index] = cursor;\n cursor += this.#blobs[index].length;\n }\n this.#offsets = offsets;\n return this;\n }\n\n /**\n * Resolves an arena reference to its byte offset.\n *\n * `null` and `undefined` resolve to the `0xffffffff` null reference, so a\n * caller can pass an absent optional field straight through.\n *\n * @param {number|null|undefined} reference Reference from `addString`/`addBytes`.\n * @returns {number} Byte offset, or `0xffffffff` for a null reference.\n */\n offsetOf(reference)\n {\n if (reference === null || reference === undefined)\n {\n return CJS_STRING_TABLE_NULL_REFERENCE;\n }\n if (!Number.isInteger(reference) || reference < 0 || reference >= this.#blobs.length)\n {\n throw new CjsFormatWriteError(\"Unknown string-table reference\", {\n reference,\n entryCount: this.#blobs.length\n });\n }\n this.finish();\n this.#handedOutOffsets = true;\n return this.#offsets[reference];\n }\n\n /**\n * Returns the interned bytes behind a reference.\n *\n * @param {number} reference Reference from `addString`/`addBytes`.\n * @returns {Uint8Array} View over the interned bytes.\n */\n bytesOf(reference)\n {\n if (!Number.isInteger(reference) || reference < 0 || reference >= this.#blobs.length)\n {\n throw new CjsFormatWriteError(\"Unknown string-table reference\", {\n reference,\n entryCount: this.#blobs.length\n });\n }\n return this.#blobs[reference];\n }\n\n /**\n * Materialises the arena payload in sorted order.\n *\n * @returns {Uint8Array} Arena payload, without the `u32` size prefix.\n */\n toBytes()\n {\n this.finish();\n const out = new Uint8Array(this.#byteLength);\n for (let index = 0; index < this.#blobs.length; index += 1)\n {\n out.set(this.#blobs[index], this.#offsets[index]);\n }\n return out;\n }\n\n /**\n * Writes the arena exactly as `StringTable::Write` does: a `u32` payload\n * size followed by the payload (`StringTable.cpp:110-143`).\n *\n * @param {import(\"./CjsByteWriter.js\").CjsByteWriter} writer Target writer.\n */\n write(writer)\n {\n this.finish();\n writer.u32(this.#byteLength);\n if (this.#byteLength === 0) return;\n writer.bytes(this.toBytes());\n }\n\n /**\n * Interns a byte run, returning an existing reference when the bytes match\n * exactly. Dedupe is on full content and length, with no suffix merging —\n * Carbon's `Blob::operator==` (`StringTable.h:104-107`).\n *\n * @param {Uint8Array} bytes Owned bytes to intern.\n * @returns {number} Arena reference.\n */\n #add(bytes)\n {\n const key = hashBytes(bytes);\n const bucket = this.#buckets.get(key);\n if (bucket)\n {\n for (const candidate of bucket)\n {\n if (compareTableBlobs(this.#blobs[candidate], bytes) === 0)\n {\n return candidate;\n }\n }\n }\n\n if (this.#handedOutOffsets)\n {\n throw new CjsFormatWriteError(\n \"String-table entry added after offsets were resolved; every offset already handed out would shift\",\n { byteLength: bytes.length }\n );\n }\n\n const reference = this.#blobs.length;\n this.#blobs.push(bytes);\n this.#byteLength += bytes.length;\n this.#offsets = null;\n if (bucket) bucket.push(reference);\n else this.#buckets.set(key, [ reference ]);\n return reference;\n }\n}\n\nexport default CjsStringTable;\n"],"names":["textEncoder","TextEncoder","CJS_STRING_TABLE_NULL_REFERENCE","compareTableBlobs","a","b","shared","Math","min","length","index","hashBytes","bytes","hash","imul","CjsStringTable","Map","byteLength","containerSize","entryCount","addString","value","text","encode","String","Uint8Array","set","addBytes","source","buffer","byteOffset","from","finish","order","map","blob","sort","offsets","Array","cursor","offsetOf","reference","undefined","Number","isInteger","CjsFormatWriteError","bytesOf","toBytes","out","write","writer","u32","#add","key","bucket","get","candidate","push"],"mappings":";;AAEA;;AAEA,MAAMA,WAAW,GAAG,IAAIC,WAAW,EAAE;;AAErC;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMC,+BAA+B,GAAG;;AAE/C;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,iBAAiBA,CAACC,CAAC,EAAEC,CAAC,EACtC;AACI,EAAA,MAAMC,MAAM,GAAGC,IAAI,CAACC,GAAG,CAACJ,CAAC,CAACK,MAAM,EAAEJ,CAAC,CAACI,MAAM,CAAC;AAC3C,EAAA,KAAK,IAAIC,KAAK,GAAG,CAAC,EAAEA,KAAK,GAAGJ,MAAM,EAAEI,KAAK,IAAI,CAAC,EAC9C;AACI,IAAA,IAAIN,CAAC,CAACM,KAAK,CAAC,KAAKL,CAAC,CAACK,KAAK,CAAC,EAAE,OAAON,CAAC,CAACM,KAAK,CAAC,GAAGL,CAAC,CAACK,KAAK,CAAC;AACzD,EAAA;AACA,EAAA,OAAON,CAAC,CAACK,MAAM,GAAGJ,CAAC,CAACI,MAAM;AAC9B;;AAE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GAAG,IAAI,CAAC,MAAM,CAAChC,MAAM;AACpC,IAAA,IAAI,CAAC,MAAM,CAACgD,IAAI,CAAC7C,KAAK,CAAC;AACvB,IAAA,IAAI,CAAC,WAAW,IAAIA,KAAK,CAACH,MAAM;AAChC,IAAA,IAAI,CAAC,QAAQ,GAAG,IAAI;IACpB,IAAI6C,MAAM,EAAEA,MAAM,CAACG,IAAI,CAAChB,SAAS,CAAC,CAAC,KAC9B,IAAI,CAAC,QAAQ,CAACf,GAAG,CAAC2B,GAAG,EAAE,CAAEZ,SAAS,CAAE,CAAC;AAC1C,IAAA,OAAOA,SAAS;AACpB,EAAA;AACJ;;;;"}
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
import { CjsByteReader } from '../CjsByteReader.js';
|
|
2
|
+
import { CjsFormatReadError } from '../CjsFormatError.js';
|
|
3
|
+
import { CARBON_EFFECT_DATA_VERSION, readEffectDescription } from './carbonEffectRecords.js';
|
|
4
|
+
|
|
5
|
+
// Source: trinity/trinity/Resources/Tr2EffectRes.cpp (DoLoad, GetShader)
|
|
6
|
+
// Source: trinity/shadercompiler/ShaderCompiler.cpp:746-845 (file assembly)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
/** Byte count of the ASCII-hex MD5 source hash in the v15 header. */
|
|
10
|
+
const CARBON_EFFECT_SOURCE_HASH_BYTES = 32;
|
|
11
|
+
|
|
12
|
+
/** Byte count of one offset-table row: `{u32 index, u32 offset, u32 size}`. */
|
|
13
|
+
const CARBON_EFFECT_RECORD_BYTES = 12;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Plain byte cursor over one description blob, carrying the Carbon effect error
|
|
17
|
+
* class and message.
|
|
18
|
+
*
|
|
19
|
+
* Separate from the container reader because the container reader parses its
|
|
20
|
+
* header in the constructor; a body needs a cursor, not a second header parse.
|
|
21
|
+
*/
|
|
22
|
+
class CjsCarbonEffectBodyReader extends CjsByteReader {
|
|
23
|
+
static ReadError = CjsFormatReadError;
|
|
24
|
+
static endOfDataMessage = "Unexpected end of Carbon effect data";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Reader for Carbon's compiled-effect container at version 15.
|
|
29
|
+
*
|
|
30
|
+
* The header is (`ShaderCompiler.cpp:822-831`):
|
|
31
|
+
*
|
|
32
|
+
* ```
|
|
33
|
+
* u32 version = 15
|
|
34
|
+
* u8[4] shaderCompilerVersion <- four bytes, not a u32
|
|
35
|
+
* char[32] sourceHash <- ASCII hex MD5 of the HLSL inputs
|
|
36
|
+
* u32 stringTableSize | arena
|
|
37
|
+
* u8 permutationCount | permutation records
|
|
38
|
+
* u32 recordCount | recordCount x { u32 index, u32 offset, u32 size }
|
|
39
|
+
* description blobs
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* Only version 15 is accepted. Carbon's own reader takes 2..15
|
|
43
|
+
* (`Tr2EffectRes.cpp:209`) but marks the v13/v14 field-order boundaries
|
|
44
|
+
* unverified, and every file in the shipped dx11/dx12 corpus is v15, so a
|
|
45
|
+
* lower version here means something unexpected rather than something old.
|
|
46
|
+
*/
|
|
47
|
+
class CjsCarbonEffectReader extends CjsCarbonEffectBodyReader {
|
|
48
|
+
/** Cursor class used for description blobs. */
|
|
49
|
+
static BodyReader = CjsCarbonEffectBodyReader;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Reads the container header, arena, permutation axes and offset table.
|
|
53
|
+
*
|
|
54
|
+
* @param {ArrayBuffer|ArrayBufferView|Uint8Array} bytes Container bytes.
|
|
55
|
+
* @param {object} [options] Read options.
|
|
56
|
+
* @param {string} [options.source] Source name used in error details.
|
|
57
|
+
* @param {boolean} [options.permissive] Accept a sparse or misordered offset
|
|
58
|
+
* table instead of rejecting it. For forensic inspection of a file already
|
|
59
|
+
* known to be malformed; never for a load path.
|
|
60
|
+
*/
|
|
61
|
+
constructor(bytes, options = {}) {
|
|
62
|
+
super(bytes, options);
|
|
63
|
+
this.version = this.readUint32();
|
|
64
|
+
if (this.version !== CARBON_EFFECT_DATA_VERSION) {
|
|
65
|
+
throw this._error(`Unsupported Carbon effect version ${this.version}; expected ${CARBON_EFFECT_DATA_VERSION}`, {
|
|
66
|
+
version: this.version
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
this.compilerVersion = Array.from(this.readRaw(4));
|
|
70
|
+
this.sourceHash = Uint8Array.from(this.readRaw(CARBON_EFFECT_SOURCE_HASH_BYTES));
|
|
71
|
+
this.stringTableSize = this.readUint32();
|
|
72
|
+
if (this.offset + this.stringTableSize > this.bytes.length) {
|
|
73
|
+
throw this._error("Invalid Carbon effect string-table size", {
|
|
74
|
+
stringTableSize: this.stringTableSize,
|
|
75
|
+
byteLength: this.bytes.length
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
this.stringTableBytes = this.bytes.subarray(this.offset, this.offset + this.stringTableSize);
|
|
79
|
+
this.skip(this.stringTableSize);
|
|
80
|
+
this.setStringTable(this.stringTableBytes, this.stringTableSize);
|
|
81
|
+
this.permutations = this.#readPermutations();
|
|
82
|
+
this.records = this.#readRecords();
|
|
83
|
+
this.headerEnd = this.offset;
|
|
84
|
+
this.diagnostics = this.#inspect();
|
|
85
|
+
if (!options.permissive) {
|
|
86
|
+
this.requireDensePermutationTable();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Returns the product of the permutation axes' option counts — the number of
|
|
92
|
+
* offset-table rows a fully populated file has.
|
|
93
|
+
*
|
|
94
|
+
* @returns {number} Permutation count.
|
|
95
|
+
*/
|
|
96
|
+
get permutationProduct() {
|
|
97
|
+
let product = 1;
|
|
98
|
+
for (const permutation of this.permutations) {
|
|
99
|
+
product *= permutation.options.length;
|
|
100
|
+
}
|
|
101
|
+
return product;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Throws unless the offset table is dense and positionally indexed.
|
|
106
|
+
*
|
|
107
|
+
* Carbon indexes `m_offsets` positionally and never reads the stored `index`
|
|
108
|
+
* field (`Tr2EffectRes.cpp:121-126`), so a sparse or misordered table does
|
|
109
|
+
* not fail — it silently returns the wrong shader body. Carbon gets density
|
|
110
|
+
* incidentally, from `g_compiledEffects` being a `std::map` densely keyed by
|
|
111
|
+
* `AddPermutationsToWorkQueue`, and promises it nowhere.
|
|
112
|
+
*
|
|
113
|
+
* Measured across the whole shipped corpus at build 3444265 — 4833 files and
|
|
114
|
+
* 78,498 rows across `effect.dx11`, `effect.dx12` and `effect.metal` — both
|
|
115
|
+
* conditions hold in every file, so this runs by default.
|
|
116
|
+
*
|
|
117
|
+
* `--ignore-permutations` does make CCP's compiler emit only key 0 while
|
|
118
|
+
* declaring every axis, so a sparse file is producible. That argues for an
|
|
119
|
+
* escape hatch, not for permissiveness: because Carbon indexes positionally,
|
|
120
|
+
* accepting such a file returns the wrong permutation's shader silently, which
|
|
121
|
+
* is exactly the failure class this container port exists to close.
|
|
122
|
+
* `{ permissive: true }` is the opt-in, for inspecting a file already known to
|
|
123
|
+
* be malformed.
|
|
124
|
+
*/
|
|
125
|
+
requireDensePermutationTable() {
|
|
126
|
+
if (!this.diagnostics.dense) {
|
|
127
|
+
throw this._error("Carbon effect offset table is sparse", {
|
|
128
|
+
recordCount: this.records.length,
|
|
129
|
+
permutationProduct: this.permutationProduct
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
if (!this.diagnostics.indicesMatchPosition) {
|
|
133
|
+
throw this._error("Carbon effect offset table is not positionally indexed", {
|
|
134
|
+
firstMismatch: this.diagnostics.firstIndexMismatch
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Reads and parses one permutation's description blob.
|
|
141
|
+
*
|
|
142
|
+
* `backend` is **optional**, and omitting it is the interesting case. A
|
|
143
|
+
* Carbon file ends each pass at the render states; ours adds one eight-byte
|
|
144
|
+
* block reference. Nothing in the header distinguishes the two, and we
|
|
145
|
+
* deliberately do not add anything — no envelope, no version of our own,
|
|
146
|
+
* because CCP owns the version space and a container that announces itself
|
|
147
|
+
* is an invention nothing requires.
|
|
148
|
+
*
|
|
149
|
+
* Instead the blob describes itself, using a rule the format already has.
|
|
150
|
+
* Every sized record must parse to exactly its declared end (Rule 1), and a
|
|
151
|
+
* body's declared size is in the offset table. So the wrong interpretation
|
|
152
|
+
* either throws or leaves the cursor somewhere other than the end, and one
|
|
153
|
+
* retry settles it.
|
|
154
|
+
*
|
|
155
|
+
* Pass `backend` explicitly when the caller knows — the loader does, because
|
|
156
|
+
* backend selection is by resource path, mirroring Carbon's
|
|
157
|
+
* `effect.dx11`/`effect.dx12`/`effect.metal`. Auto-detection is for bytes
|
|
158
|
+
* that arrive without that context: tooling, caches, inspection.
|
|
159
|
+
*
|
|
160
|
+
* @param {number} index Permutation index, positional in the offset table.
|
|
161
|
+
* @param {object} [options] Read options.
|
|
162
|
+
* @param {boolean} [options.backend] Expect our optional per-pass trailing
|
|
163
|
+
* block. Omit to detect it from the blob's declared end.
|
|
164
|
+
* @returns {object} Description record tree.
|
|
165
|
+
*/
|
|
166
|
+
readDescription(index, options = {}) {
|
|
167
|
+
const record = this.records[index];
|
|
168
|
+
if (!record) {
|
|
169
|
+
throw this._error(`No Carbon effect body at permutation index ${index}`, {
|
|
170
|
+
index,
|
|
171
|
+
recordCount: this.records.length
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
if (options.backend !== undefined) {
|
|
175
|
+
return this.#readDescriptionAs(record, options.backend === true);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Plain Carbon first: it is the larger population, and it is the reading
|
|
179
|
+
// that must stay cheap.
|
|
180
|
+
try {
|
|
181
|
+
return this.#readDescriptionAs(record, false);
|
|
182
|
+
} catch (withoutBlocks) {
|
|
183
|
+
try {
|
|
184
|
+
return this.#readDescriptionAs(record, true);
|
|
185
|
+
} catch {
|
|
186
|
+
// Report the plain-Carbon failure. If neither reading works the
|
|
187
|
+
// blob is malformed, and the Carbon diagnosis is the useful one;
|
|
188
|
+
// the backend attempt's error would describe a misparse of bytes
|
|
189
|
+
// that were never a backend block.
|
|
190
|
+
throw withoutBlocks;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Parses one body under an exact backend-block assumption.
|
|
197
|
+
*
|
|
198
|
+
* @param {object} record Offset-table row.
|
|
199
|
+
* @param {boolean} backend Whether to expect the per-pass block.
|
|
200
|
+
* @returns {object} Description record tree.
|
|
201
|
+
*/
|
|
202
|
+
#readDescriptionAs(record, backend) {
|
|
203
|
+
const reader = new this.constructor.BodyReader(this.bytes, {
|
|
204
|
+
source: this.source,
|
|
205
|
+
offset: record.offset,
|
|
206
|
+
end: record.offset + record.size,
|
|
207
|
+
stringTable: this.stringTableBytes,
|
|
208
|
+
stringTableSize: this.stringTableSize
|
|
209
|
+
});
|
|
210
|
+
return readEffectDescription(reader, {
|
|
211
|
+
backend
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Returns the raw description-blob bytes for one permutation.
|
|
217
|
+
*
|
|
218
|
+
* @param {number} index Permutation index, positional in the offset table.
|
|
219
|
+
* @returns {Uint8Array} View over the body bytes.
|
|
220
|
+
*/
|
|
221
|
+
bodyBytes(index) {
|
|
222
|
+
const record = this.records[index];
|
|
223
|
+
if (!record) {
|
|
224
|
+
throw this._error(`No Carbon effect body at permutation index ${index}`, {
|
|
225
|
+
index
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
return this.bytes.subarray(record.offset, record.offset + record.size);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Reads the permutation axis records (`ShaderCompiler.cpp:769-795`,
|
|
233
|
+
* `Tr2EffectRes.cpp:263-292`).
|
|
234
|
+
*
|
|
235
|
+
* @returns {object[]} Permutation axes with arena references retained.
|
|
236
|
+
*/
|
|
237
|
+
#readPermutations() {
|
|
238
|
+
const permutations = [];
|
|
239
|
+
const count = this.readUint8();
|
|
240
|
+
for (let index = 0; index < count; index += 1) {
|
|
241
|
+
const nameOffset = this.readUint32();
|
|
242
|
+
const name = this.readStringAt(nameOffset);
|
|
243
|
+
const defaultOption = this.readUint8();
|
|
244
|
+
const descriptionOffset = this.readUint32();
|
|
245
|
+
const description = this.readStringAt(descriptionOffset);
|
|
246
|
+
const type = this.readUint8();
|
|
247
|
+
const options = [];
|
|
248
|
+
const optionCount = this.readUint8();
|
|
249
|
+
for (let optionIndex = 0; optionIndex < optionCount; optionIndex += 1) {
|
|
250
|
+
const offset = this.readUint32();
|
|
251
|
+
options.push({
|
|
252
|
+
offset,
|
|
253
|
+
value: this.readStringAt(offset)
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
permutations.push({
|
|
257
|
+
name: {
|
|
258
|
+
offset: nameOffset,
|
|
259
|
+
value: name
|
|
260
|
+
},
|
|
261
|
+
defaultOption,
|
|
262
|
+
description: {
|
|
263
|
+
offset: descriptionOffset,
|
|
264
|
+
value: description
|
|
265
|
+
},
|
|
266
|
+
type,
|
|
267
|
+
options
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
return permutations;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Reads the offset table (`ShaderCompiler.cpp:800-820`,
|
|
275
|
+
* `Tr2EffectRes.cpp:294-311`).
|
|
276
|
+
*
|
|
277
|
+
* @returns {object[]} Offset-table rows.
|
|
278
|
+
*/
|
|
279
|
+
#readRecords() {
|
|
280
|
+
const count = this.readUint32();
|
|
281
|
+
if (count === 0) {
|
|
282
|
+
throw this._error("Carbon effect contains no compiled bodies");
|
|
283
|
+
}
|
|
284
|
+
if (this.offset + count * CARBON_EFFECT_RECORD_BYTES > this.bytes.length) {
|
|
285
|
+
throw this._error("Carbon effect offset table runs past the end of the file", {
|
|
286
|
+
recordCount: count,
|
|
287
|
+
byteLength: this.bytes.length
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
const records = [];
|
|
291
|
+
for (let index = 0; index < count; index += 1) {
|
|
292
|
+
records.push({
|
|
293
|
+
index: this.readUint32(),
|
|
294
|
+
offset: this.readUint32(),
|
|
295
|
+
size: this.readUint32()
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
return records;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Validates every row's byte range and collects the structural diagnostics
|
|
303
|
+
* Carbon relies on without stating.
|
|
304
|
+
*
|
|
305
|
+
* @returns {object} Structural diagnostics.
|
|
306
|
+
*/
|
|
307
|
+
#inspect() {
|
|
308
|
+
const headerEnd = this.headerEnd;
|
|
309
|
+
const byteLength = this.bytes.length;
|
|
310
|
+
let firstIndexMismatch = null;
|
|
311
|
+
const uniqueOffsets = new Set();
|
|
312
|
+
for (let index = 0; index < this.records.length; index += 1) {
|
|
313
|
+
const record = this.records[index];
|
|
314
|
+
if (record.index !== index && firstIndexMismatch === null) {
|
|
315
|
+
firstIndexMismatch = {
|
|
316
|
+
position: index,
|
|
317
|
+
storedIndex: record.index
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
const end = record.offset + record.size;
|
|
321
|
+
if (record.offset < headerEnd || end > byteLength || record.size === 0) {
|
|
322
|
+
throw this._error(`Carbon effect body record ${index} is out of range`, {
|
|
323
|
+
position: index,
|
|
324
|
+
offset: record.offset,
|
|
325
|
+
size: record.size,
|
|
326
|
+
headerEnd,
|
|
327
|
+
byteLength
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
uniqueOffsets.add(record.offset);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// The body region must begin exactly where the header ends. Carbon computes
|
|
334
|
+
// its first body offset as `4 + 4 + 32 + headerSize + stringTable.GetSize()`
|
|
335
|
+
// (`ShaderCompiler.cpp:801`), so a gap or an overlap means our reconstruction
|
|
336
|
+
// of that arithmetic disagrees with the writer's — which would silently
|
|
337
|
+
// misread every body rather than fail. Carbon's own reader does not check
|
|
338
|
+
// this; measured across 4833 shipped files, every one starts its body region
|
|
339
|
+
// flush against the header with no slack.
|
|
340
|
+
const firstBody = Math.min(...this.records.map(record => record.offset));
|
|
341
|
+
if (firstBody !== headerEnd) {
|
|
342
|
+
throw this._error("Carbon effect body region does not start where the header ends", {
|
|
343
|
+
firstBody,
|
|
344
|
+
headerEnd,
|
|
345
|
+
slack: firstBody - headerEnd
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
return {
|
|
349
|
+
recordCount: this.records.length,
|
|
350
|
+
permutationProduct: this.permutationProduct,
|
|
351
|
+
dense: this.records.length === this.permutationProduct,
|
|
352
|
+
indicesMatchPosition: firstIndexMismatch === null,
|
|
353
|
+
firstIndexMismatch,
|
|
354
|
+
uniqueBodyCount: uniqueOffsets.size,
|
|
355
|
+
aliasedRowCount: this.records.length - uniqueOffsets.size
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export { CARBON_EFFECT_RECORD_BYTES, CARBON_EFFECT_SOURCE_HASH_BYTES, CjsCarbonEffectBodyReader, CjsCarbonEffectReader };
|
|
361
|
+
//# sourceMappingURL=CjsCarbonEffectReader.js.map
|