@carbonenginejs/runtime-resource 0.12.2 → 0.13.0

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.
Files changed (541) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +32 -32
  3. package/README.md +129 -108
  4. package/dist/CjsMotherLode.js +1271 -1271
  5. package/dist/CjsResMan.js +3858 -3494
  6. package/dist/CjsResMan.js.map +1 -1
  7. package/dist/CjsResManFetchProvider.js +94 -94
  8. package/dist/CjsResManWorkQueue.js +301 -301
  9. package/dist/_virtual/_rollupPluginBabelHelpers.js +150 -150
  10. package/dist/format/CjsBlueReader.js +358 -358
  11. package/dist/format/CjsByteReader.js +310 -310
  12. package/dist/format/CjsByteWriter.js +242 -242
  13. package/dist/format/CjsFormat.js +223 -223
  14. package/dist/format/CjsFormatError.js +41 -41
  15. package/dist/format/CjsReader.js +22 -22
  16. package/dist/format/CjsResourceProbe.js +279 -279
  17. package/dist/format/CjsStringTable.js +268 -268
  18. package/dist/format/carbonEffect/CjsCarbonEffectReader.js +408 -361
  19. package/dist/format/carbonEffect/CjsCarbonEffectReader.js.map +1 -1
  20. package/dist/format/carbonEffect/CjsCarbonEffectWriter.js +373 -373
  21. package/dist/format/carbonEffect/buildCarbonEffectContainer.js +182 -0
  22. package/dist/format/carbonEffect/buildCarbonEffectContainer.js.map +1 -0
  23. package/dist/format/carbonEffect/carbonEffectBackendBlock.js +321 -427
  24. package/dist/format/carbonEffect/carbonEffectBackendBlock.js.map +1 -1
  25. package/dist/format/carbonEffect/carbonEffectRecords.js +1147 -955
  26. package/dist/format/carbonEffect/carbonEffectRecords.js.map +1 -1
  27. package/dist/format/carbonEffect/carbonEffectResourceTransform.js +197 -0
  28. package/dist/format/carbonEffect/carbonEffectResourceTransform.js.map +1 -0
  29. package/dist/format/compareUtf8.js +36 -36
  30. package/dist/format/effect/effectBodyInventory.js +146 -0
  31. package/dist/format/effect/effectBodyInventory.js.map +1 -0
  32. package/dist/format/effect/effectPermutationGraph.js +257 -257
  33. package/dist/format/effect/effectPermutationGraph.js.map +1 -1
  34. package/dist/format/effect/sha256.js +114 -114
  35. package/dist/format/index.js +11 -11
  36. package/dist/format/payloadContract.js +193 -193
  37. package/dist/formats/black/CjsBlackFormat.js +310 -292
  38. package/dist/formats/black/CjsBlackFormat.js.map +1 -1
  39. package/dist/formats/black/core/CjsBlackBinaryReader.js +260 -260
  40. package/dist/formats/black/core/CjsBlackPropertyReaders.js +448 -448
  41. package/dist/formats/black/core/CjsBlackReader.js +764 -764
  42. package/dist/formats/black/core/CjsBlackSchemaRegistry.js +540 -540
  43. package/dist/formats/black/core/black-schema-v1-2026-07-23.json.js +4 -4
  44. package/dist/formats/black/core/blackConstants.js +7 -7
  45. package/dist/formats/black/core/blackDefinitions.js +10 -10
  46. package/dist/formats/black/core/blackEnums.js +6 -6
  47. package/dist/formats/black/core/blackSchema.js +3 -3
  48. package/dist/formats/black/core/blackVersion.js +22 -22
  49. package/dist/formats/black/core/helpers.js +199 -199
  50. package/dist/formats/black/core/schema.js +4 -4
  51. package/dist/formats/black/index.js +2 -2
  52. package/dist/formats/bnk/CjsBnkFormat.js +175 -156
  53. package/dist/formats/bnk/CjsBnkFormat.js.map +1 -1
  54. package/dist/formats/bnk/core/busNodes.js +252 -0
  55. package/dist/formats/bnk/core/busNodes.js.map +1 -0
  56. package/dist/formats/bnk/core/effectNodes.js +147 -0
  57. package/dist/formats/bnk/core/effectNodes.js.map +1 -0
  58. package/dist/formats/bnk/core/eventAction.js +416 -305
  59. package/dist/formats/bnk/core/eventAction.js.map +1 -1
  60. package/dist/formats/bnk/core/globalSettings.js +215 -0
  61. package/dist/formats/bnk/core/globalSettings.js.map +1 -0
  62. package/dist/formats/bnk/core/graph.js +137 -137
  63. package/dist/formats/bnk/core/helpers.js +512 -480
  64. package/dist/formats/bnk/core/helpers.js.map +1 -1
  65. package/dist/formats/bnk/core/musicNodes.js +533 -509
  66. package/dist/formats/bnk/core/musicNodes.js.map +1 -1
  67. package/dist/formats/bnk/core/nodeBase.js +553 -532
  68. package/dist/formats/bnk/core/nodeBase.js.map +1 -1
  69. package/dist/formats/bnk/core/sfxNodes.js +632 -632
  70. package/dist/formats/bnk/core/soundbanksInfo.js +209 -209
  71. package/dist/formats/bnk/index.js +2 -2
  72. package/dist/formats/cmf/CjsCmfFormat.js +497 -497
  73. package/dist/formats/cmf/core/binary.js +194 -194
  74. package/dist/formats/cmf/core/buffers.js +237 -237
  75. package/dist/formats/cmf/core/constants.js +47 -47
  76. package/dist/formats/cmf/core/gr2Anim.js +453 -453
  77. package/dist/formats/cmf/core/helpers.js +318 -318
  78. package/dist/formats/cmf/core/pack.js +276 -276
  79. package/dist/formats/cmf/core/schema.js +374 -374
  80. package/dist/formats/cmf/core/shared.js +277 -277
  81. package/dist/formats/cmf/core/writer.js +571 -571
  82. package/dist/formats/cmf/index.js +2 -2
  83. package/dist/formats/dds/CjsDdsFormat.js +200 -200
  84. package/dist/formats/dds/core/bc6h.js +298 -298
  85. package/dist/formats/dds/core/bc7.js +272 -272
  86. package/dist/formats/dds/core/helpers.js +862 -862
  87. package/dist/formats/dds/index.js +2 -2
  88. package/dist/formats/dxbc/CjsDxbcFormat.js +142 -142
  89. package/dist/formats/dxbc/core/DxbcReader.js +266 -266
  90. package/dist/formats/dxbc/core/container.js +169 -169
  91. package/dist/formats/dxbc/core/decoder.js +789 -789
  92. package/dist/formats/dxbc/core/errors.js +19 -19
  93. package/dist/formats/dxbc/core/helpers.js +220 -220
  94. package/dist/formats/dxbc/core/opcodes.js +45 -45
  95. package/dist/formats/dxbc/core/program.js +91 -91
  96. package/dist/formats/dxbc/core/signature.js +172 -172
  97. package/dist/formats/dxbc/index.js +2 -2
  98. package/dist/formats/fbx/CjsFbxFormat.js +266 -266
  99. package/dist/formats/fbx/core/helpers.js +3932 -3932
  100. package/dist/formats/fbx/index.js +2 -2
  101. package/dist/formats/flac/CjsFlacFormat.js +142 -142
  102. package/dist/formats/flac/core/helpers.js +315 -315
  103. package/dist/formats/flac/index.js +2 -2
  104. package/dist/formats/gif/CjsGifFormat.js +141 -141
  105. package/dist/formats/gif/core/helpers.js +380 -380
  106. package/dist/formats/gif/index.js +2 -2
  107. package/dist/formats/gltf/CjsGltfFormat.js +290 -290
  108. package/dist/formats/gltf/core/helpers.js +307 -307
  109. package/dist/formats/gltf/core/json.js +79 -79
  110. package/dist/formats/gltf/core/parser.js +679 -679
  111. package/dist/formats/gltf/core/targets.js +173 -173
  112. package/dist/formats/gltf/index.js +2 -2
  113. package/dist/formats/gr2/CjsGr2Format.js +289 -289
  114. package/dist/formats/gr2/core/bitknit2.js +282 -282
  115. package/dist/formats/gr2/core/curves.js +1047 -1047
  116. package/dist/formats/gr2/core/gsf.js +72 -72
  117. package/dist/formats/gr2/core/helpers.js +352 -352
  118. package/dist/formats/gr2/core/json.js +622 -622
  119. package/dist/formats/gr2/core/oodle1.js +388 -388
  120. package/dist/formats/gr2/core/tangents.js +48 -48
  121. package/dist/formats/gr2/core/targets.js +361 -361
  122. package/dist/formats/gr2/index.js +2 -2
  123. package/dist/formats/hlsl/CjsHlslFormat.js +254 -254
  124. package/dist/formats/hlsl/core/HlslBinaryUtils.js +15 -15
  125. package/dist/formats/hlsl/core/HlslEffectReadError.js +19 -19
  126. package/dist/formats/hlsl/core/HlslEffectStateManager.js +130 -130
  127. package/dist/formats/hlsl/core/HlslRenderStateSetup.js +37 -37
  128. package/dist/formats/hlsl/core/HlslResourceSetDescription.js +94 -94
  129. package/dist/formats/hlsl/core/HlslShaderBytecode.js +44 -44
  130. package/dist/formats/hlsl/core/analysis.js +51 -51
  131. package/dist/formats/hlsl/core/carbonDescriptionToRuntime.js +850 -781
  132. package/dist/formats/hlsl/core/carbonDescriptionToRuntime.js.map +1 -1
  133. package/dist/formats/hlsl/core/detailMapFamily.js +128 -0
  134. package/dist/formats/hlsl/core/detailMapFamily.js.map +1 -0
  135. package/dist/formats/hlsl/core/helpers.js +270 -270
  136. package/dist/formats/hlsl/core/json.js +284 -284
  137. package/dist/formats/hlsl/core/localLightFamily.js +133 -0
  138. package/dist/formats/hlsl/core/localLightFamily.js.map +1 -0
  139. package/dist/formats/hlsl/core/metadata.js +327 -327
  140. package/dist/formats/hlsl/core/render-states.js +280 -280
  141. package/dist/formats/hlsl/core/tr2/HlslRenderContextEnum.js +43 -43
  142. package/dist/formats/hlsl/core/tr2/resources/HlslEffectRes.js +320 -314
  143. package/dist/formats/hlsl/core/tr2/resources/HlslEffectRes.js.map +1 -1
  144. package/dist/formats/hlsl/core/tr2/resources/HlslShaderPermutation.js +33 -33
  145. package/dist/formats/hlsl/core/tr2/shader/HlslEffectBindingManifest.js +416 -416
  146. package/dist/formats/hlsl/core/tr2/shader/HlslEffectConstant.js +40 -40
  147. package/dist/formats/hlsl/core/tr2/shader/HlslEffectDescription.js +62 -742
  148. package/dist/formats/hlsl/core/tr2/shader/HlslEffectDescription.js.map +1 -1
  149. package/dist/formats/hlsl/core/tr2/shader/HlslEffectLibrary.js +52 -52
  150. package/dist/formats/hlsl/core/tr2/shader/HlslEffectParameterAnnotation.js +38 -38
  151. package/dist/formats/hlsl/core/tr2/shader/HlslEffectResource.js +50 -50
  152. package/dist/formats/hlsl/core/tr2/shader/HlslEffectStageInput.js +86 -86
  153. package/dist/formats/hlsl/core/tr2/shader/HlslEffectTechnique.js +31 -31
  154. package/dist/formats/hlsl/core/tr2/shader/HlslPass.js +42 -42
  155. package/dist/formats/hlsl/core/tr2/shader/HlslSamplerDescription.js +55 -55
  156. package/dist/formats/hlsl/core/tr2/shader/HlslSamplerSetup.js +29 -29
  157. package/dist/formats/hlsl/core/tr2/shader/HlslShader.js +220 -220
  158. package/dist/formats/hlsl/core/tr2/shader/HlslShaderOption.js +30 -30
  159. package/dist/formats/hlsl/index.js +3 -3
  160. package/dist/formats/index.js +33 -31
  161. package/dist/formats/index.js.map +1 -1
  162. package/dist/formats/jpeg/CjsJpegFormat.js +212 -212
  163. package/dist/formats/jpeg/core/helpers.js +402 -402
  164. package/dist/formats/jpeg/core/jpeg.js +480 -480
  165. package/dist/formats/jpeg/index.js +2 -2
  166. package/dist/formats/mp3/CjsMp3Format.js +197 -197
  167. package/dist/formats/mp3/core/helpers.js +375 -375
  168. package/dist/formats/mp3/index.js +2 -2
  169. package/dist/formats/mp4/CjsMp4Format.js +197 -197
  170. package/dist/formats/mp4/core/helpers.js +484 -484
  171. package/dist/formats/mp4/index.js +2 -2
  172. package/dist/formats/obj/CjsObjFormat.js +253 -253
  173. package/dist/formats/obj/core/helpers.js +573 -573
  174. package/dist/formats/obj/core/json.js +64 -64
  175. package/dist/formats/obj/core/parser.js +321 -321
  176. package/dist/formats/obj/index.js +2 -2
  177. package/dist/formats/ogg/CjsOggFormat.js +143 -143
  178. package/dist/formats/ogg/core/helpers.js +410 -410
  179. package/dist/formats/ogg/core/imdct.js +178 -178
  180. package/dist/formats/ogg/core/vorbis.js +1017 -1017
  181. package/dist/formats/ogg/index.js +2 -2
  182. package/dist/formats/pickle/CjsPickleFormat.js +214 -0
  183. package/dist/formats/pickle/CjsPickleFormat.js.map +1 -0
  184. package/dist/formats/pickle/core/CjsPickleProtocol0Reader.js +551 -0
  185. package/dist/formats/pickle/core/CjsPickleProtocol0Reader.js.map +1 -0
  186. package/dist/formats/pickle/index.js +2 -0
  187. package/dist/formats/pickle/index.js.map +1 -0
  188. package/dist/formats/png/CjsPngFormat.js +201 -201
  189. package/dist/formats/png/core/helpers.js +635 -635
  190. package/dist/formats/png/index.js +2 -2
  191. package/dist/formats/red/CjsRedFormat.js +263 -263
  192. package/dist/formats/red/core/CjsRedReader.js +246 -246
  193. package/dist/formats/red/core/blackDefinitions.js +3 -3
  194. package/dist/formats/red/core/helpers.js +158 -158
  195. package/dist/formats/red/core/redGraph.js +71 -68
  196. package/dist/formats/red/core/redGraph.js.map +1 -1
  197. package/dist/formats/red/core/schema.js +4 -4
  198. package/dist/formats/red/index.js +2 -2
  199. package/dist/formats/stl/CjsStlFormat.js +365 -365
  200. package/dist/formats/stl/core/helpers.js +261 -261
  201. package/dist/formats/stl/core/json.js +51 -51
  202. package/dist/formats/stl/core/stl.js +642 -642
  203. package/dist/formats/stl/core/targets.js +173 -173
  204. package/dist/formats/stl/index.js +2 -2
  205. package/dist/formats/tga/CjsTgaFormat.js +197 -197
  206. package/dist/formats/tga/core/helpers.js +493 -493
  207. package/dist/formats/tga/index.js +2 -2
  208. package/dist/formats/wav/CjsWavFormat.js +198 -198
  209. package/dist/formats/wav/core/helpers.js +365 -365
  210. package/dist/formats/wav/index.js +2 -2
  211. package/dist/formats/webgl/CjsWebglFormat.js +199 -221
  212. package/dist/formats/webgl/CjsWebglFormat.js.map +1 -1
  213. package/dist/formats/webgl/core/buildGlslEffectContainer.js +70 -0
  214. package/dist/formats/webgl/core/buildGlslEffectContainer.js.map +1 -0
  215. package/dist/formats/webgl/core/effectPackage.js +900 -792
  216. package/dist/formats/webgl/core/effectPackage.js.map +1 -1
  217. package/dist/formats/webgl/core/errors.js +27 -27
  218. package/dist/formats/webgl/core/errors.js.map +1 -1
  219. package/dist/formats/webgl/core/glsl/DxbcGlslEmitter.js +2820 -2678
  220. package/dist/formats/webgl/core/glsl/DxbcGlslEmitter.js.map +1 -1
  221. package/dist/formats/webgl/core/glsl/DxbcGlslHelpers.js +89 -89
  222. package/dist/formats/webgl/core/glsl/DxbcGlslOperandFormatter.js +486 -428
  223. package/dist/formats/webgl/core/glsl/DxbcGlslOperandFormatter.js.map +1 -1
  224. package/dist/formats/webgl/core/glsl/packedLightFixups.js +98 -0
  225. package/dist/formats/webgl/core/glsl/packedLightFixups.js.map +1 -0
  226. package/dist/formats/webgl/core/glslBackendBlock.js +552 -0
  227. package/dist/formats/webgl/core/glslBackendBlock.js.map +1 -0
  228. package/dist/formats/webgl/core/glslBackendBodySet.js +243 -0
  229. package/dist/formats/webgl/core/glslBackendBodySet.js.map +1 -0
  230. package/dist/formats/webgl/core/{cewgCompleteness.js → glslEffectCompleteness.js} +85 -81
  231. package/dist/formats/webgl/core/glslEffectCompleteness.js.map +1 -0
  232. package/dist/formats/webgl/core/helpers.js +167 -306
  233. package/dist/formats/webgl/core/helpers.js.map +1 -1
  234. package/dist/formats/webgl/core/inspectGlslEffectContainer.js +122 -0
  235. package/dist/formats/webgl/core/inspectGlslEffectContainer.js.map +1 -0
  236. package/dist/formats/webgl/core/readGlslEffectContainer.js +303 -0
  237. package/dist/formats/webgl/core/readGlslEffectContainer.js.map +1 -0
  238. package/dist/formats/webgl/index.js +2 -2
  239. package/dist/formats/webgpu/CjsWebgpuFormat.js +357 -357
  240. package/dist/formats/webgpu/CjsWebgpuFormat.js.map +1 -1
  241. package/dist/formats/webgpu/core/buildCarbonEffectContainer.js +89 -197
  242. package/dist/formats/webgpu/core/buildCarbonEffectContainer.js.map +1 -1
  243. package/dist/formats/webgpu/core/{cewgpu/CewgpuContainer.js → carbonWebgpu/CarbonWebgpuContainer.js} +354 -368
  244. package/dist/formats/webgpu/core/carbonWebgpu/CarbonWebgpuContainer.js.map +1 -0
  245. package/dist/formats/webgpu/core/{cewgpu → carbonWebgpu}/containerViews.js +355 -355
  246. package/dist/formats/webgpu/core/carbonWebgpu/containerViews.js.map +1 -0
  247. package/dist/formats/webgpu/core/{cewgpu → carbonWebgpu}/validateContainer.js +90 -90
  248. package/dist/formats/webgpu/core/carbonWebgpu/validateContainer.js.map +1 -0
  249. package/dist/formats/webgpu/core/effectAnalysis.js +82 -82
  250. package/dist/formats/webgpu/core/effectBackendBodySet.js +289 -289
  251. package/dist/formats/webgpu/core/effectBackendBodySet.js.map +1 -1
  252. package/dist/formats/webgpu/core/errors.js +20 -20
  253. package/dist/formats/webgpu/core/errors.js.map +1 -1
  254. package/dist/formats/webgpu/core/helpers.js +443 -443
  255. package/dist/formats/webgpu/core/helpers.js.map +1 -1
  256. package/dist/formats/webgpu/core/ir/analyzeRegisterValues.js +212 -212
  257. package/dist/formats/webgpu/core/ir/buildControlFlow.js +220 -220
  258. package/dist/formats/webgpu/core/ir/indexableTemps.js +137 -137
  259. package/dist/formats/webgpu/core/ir/inferValueTypes.js +449 -449
  260. package/dist/formats/webgpu/core/ir/lowerDxbcToIr.js +494 -494
  261. package/dist/formats/webgpu/core/ir/resolveRegisterFlow.js +177 -177
  262. package/dist/formats/webgpu/core/ir/sourceLanes.js +61 -61
  263. package/dist/formats/webgpu/core/packageEffect.js +381 -385
  264. package/dist/formats/webgpu/core/packageEffect.js.map +1 -1
  265. package/dist/formats/webgpu/core/packageEffectSelection.js +164 -164
  266. package/dist/formats/webgpu/core/packageEffectSelection.js.map +1 -1
  267. package/dist/formats/webgpu/core/packageMetadata.js +17 -17
  268. package/dist/formats/webgpu/core/schema.js +4 -4
  269. package/dist/formats/webgpu/core/wgsl/buildResourceTransformPlan.js +263 -263
  270. package/dist/formats/webgpu/core/wgsl/buildResourceTransformPlan.js.map +1 -1
  271. package/dist/formats/webgpu/core/wgsl/buildWgslBindingPlan.js +172 -172
  272. package/dist/formats/webgpu/core/wgsl/buildWgslSet.js +320 -320
  273. package/dist/formats/webgpu/core/wgsl/buildWgslSet.js.map +1 -1
  274. package/dist/formats/webgpu/core/wgsl/emitWgsl.js +356 -356
  275. package/dist/formats/webgpu/core/wgsl/hoistEscapingValues.js +77 -77
  276. package/dist/formats/webgpu/core/wgsl/lowerBindingLayout.js +451 -451
  277. package/dist/formats/webgpu/core/wgsl/lowerComputeProgram.js +735 -735
  278. package/dist/formats/webgpu/core/wgsl/lowerCreateHistogramsComputeProgram.js +457 -457
  279. package/dist/formats/webgpu/core/wgsl/lowerFragmentProgram.js +1572 -1572
  280. package/dist/formats/webgpu/core/wgsl/lowerMergeHistogramsComputeProgram.js +659 -659
  281. package/dist/formats/webgpu/core/wgsl/lowerParticleClearComputePrograms.js +734 -734
  282. package/dist/formats/webgpu/core/wgsl/lowerParticleEmitComputeProgram.js +583 -583
  283. package/dist/formats/webgpu/core/wgsl/lowerSkinVerticesComputeProgram.js +621 -621
  284. package/dist/formats/webgpu/core/wgsl/lowerSortComputeProgram.js +824 -824
  285. package/dist/formats/webgpu/core/wgsl/lowerSortInnerComputeProgram.js +697 -697
  286. package/dist/formats/webgpu/core/wgsl/lowerSortStepComputeProgram.js +559 -559
  287. package/dist/formats/webgpu/core/wgsl/lowerVertexProgram.js +1328 -1328
  288. package/dist/formats/webgpu/core/wgsl/particleEmitSemanticDigest.js +110 -110
  289. package/dist/formats/webgpu/core/wgsl/precisionControls.js +55 -55
  290. package/dist/formats/webgpu/core/wgsl/selectionPlans.js +717 -717
  291. package/dist/formats/webgpu/core/wgsl/uniformity.js +78 -78
  292. package/dist/formats/webgpu/core/wgsl/validateExactComputeIr.js +196 -196
  293. package/dist/formats/webgpu/core/wgsl/validateHandleOperand.js +37 -37
  294. package/dist/formats/webgpu/index.js +2 -2
  295. package/dist/formats/webm/CjsWebmFormat.js +197 -197
  296. package/dist/formats/webm/core/helpers.js +572 -572
  297. package/dist/formats/webm/index.js +2 -2
  298. package/dist/formats/webp/CjsWebpFormat.js +140 -140
  299. package/dist/formats/webp/core/helpers.js +237 -237
  300. package/dist/formats/webp/index.js +2 -2
  301. package/dist/formats/wem/CjsWemFormat.js +250 -250
  302. package/dist/formats/wem/core/bitStream.js +261 -261
  303. package/dist/formats/wem/core/codebookLibrary.js +164 -164
  304. package/dist/formats/wem/core/helpers.js +437 -437
  305. package/dist/formats/wem/core/packedCodebooksAotuv603.js +30 -30
  306. package/dist/formats/wem/core/ptadpcm.js +77 -77
  307. package/dist/formats/wem/core/resolve.js +121 -121
  308. package/dist/formats/wem/core/wemToOgg.js +485 -485
  309. package/dist/formats/wem/index.js +2 -2
  310. package/dist/formats/yaml/CjsYamlFormat.js +134 -134
  311. package/dist/formats/yaml/CjsYamlFormat.js.map +1 -1
  312. package/dist/formats/yaml/core/CjsYamlReader.js +400 -402
  313. package/dist/formats/yaml/core/CjsYamlReader.js.map +1 -1
  314. package/dist/formats/yaml/core/helpers.js +196 -170
  315. package/dist/formats/yaml/core/helpers.js.map +1 -1
  316. package/dist/formats/yaml/index.js +2 -2
  317. package/dist/index.js +64 -62
  318. package/dist/index.js.map +1 -1
  319. package/dist/resource/CjsLoadingObject.js +19 -0
  320. package/dist/resource/CjsLoadingObject.js.map +1 -0
  321. package/dist/resource/CjsResource.js +801 -797
  322. package/dist/resource/CjsResource.js.map +1 -1
  323. package/dist/resource/ResourceHandlerMode.js +14 -0
  324. package/dist/resource/ResourceHandlerMode.js.map +1 -0
  325. package/dist/resource/Tr2LightProfileRes.js +32 -32
  326. package/dist/resource/audio/AudioGeometryResData.js +47 -47
  327. package/dist/resource/audio/CjsAudioBufferRes.js +86 -86
  328. package/dist/resource/audio/CjsAudioRes.js +213 -213
  329. package/dist/resource/audio/index.js +4 -4
  330. package/dist/resource/geometry/MeshDecalData.js +37 -37
  331. package/dist/resource/geometry/MeshDecalLodData.js +34 -34
  332. package/dist/resource/geometry/TriGeometryRes.js +675 -675
  333. package/dist/resource/geometry/TriGeometryResAreaData.js +59 -59
  334. package/dist/resource/geometry/TriGeometryResJointData.js +38 -38
  335. package/dist/resource/geometry/TriGeometryResLodData.js +88 -88
  336. package/dist/resource/geometry/TriGeometryResMeshData.js +63 -63
  337. package/dist/resource/geometry/TriGeometryResSkeletonData.js +34 -34
  338. package/dist/resource/geometry/TriJointBinding.js +38 -38
  339. package/dist/resource/geometry/TriMorphTargetGeometryConstants.js +46 -46
  340. package/dist/resource/geometry/TriRtGeometryConstants.js +88 -88
  341. package/dist/resource/geometry/granny/GStateBindingCallbackData.js +31 -31
  342. package/dist/resource/geometry/granny/Tr2GrannyIntersectionResult.js +60 -60
  343. package/dist/resource/geometry/granny/Tr2GrannyStateRes.js +36 -36
  344. package/dist/resource/geometry/granny/TriGrannyRes.js +35 -35
  345. package/dist/resource/geometry/granny/enums.js +10 -10
  346. package/dist/resource/geometry/granny/index.js +6 -6
  347. package/dist/resource/geometry/index.js +17 -17
  348. package/dist/resource/index.js +54 -52
  349. package/dist/resource/index.js.map +1 -1
  350. package/dist/resource/resourceBoundary.js +64 -64
  351. package/dist/resource/shader/Tr2EffectRes.js +336 -431
  352. package/dist/resource/shader/Tr2EffectRes.js.map +1 -1
  353. package/dist/resource/shader/Tr2MaterialArea.js +31 -31
  354. package/dist/resource/shader/Tr2MaterialMesh.js +27 -27
  355. package/dist/resource/shader/Tr2MaterialRes.js +31 -31
  356. package/dist/resource/shader/Tr2Shader.js +283 -261
  357. package/dist/resource/shader/Tr2Shader.js.map +1 -1
  358. package/dist/resource/shader/Tr2ShaderPermutation.js +43 -43
  359. package/dist/resource/shader/index.js +17 -17
  360. package/dist/resource/shader/reflection/Tr2EffectConstant.js +143 -135
  361. package/dist/resource/shader/reflection/Tr2EffectConstant.js.map +1 -1
  362. package/dist/resource/shader/reflection/Tr2EffectDefine.js +30 -30
  363. package/dist/resource/shader/reflection/Tr2EffectDescription.js +114 -109
  364. package/dist/resource/shader/reflection/Tr2EffectDescription.js.map +1 -1
  365. package/dist/resource/shader/reflection/Tr2EffectLibrary.js +168 -147
  366. package/dist/resource/shader/reflection/Tr2EffectLibrary.js.map +1 -1
  367. package/dist/resource/shader/reflection/Tr2EffectParameterAnnotation.js +125 -105
  368. package/dist/resource/shader/reflection/Tr2EffectParameterAnnotation.js.map +1 -1
  369. package/dist/resource/shader/reflection/Tr2EffectResource.js +120 -86
  370. package/dist/resource/shader/reflection/Tr2EffectResource.js.map +1 -1
  371. package/dist/resource/shader/reflection/Tr2EffectStageInput.js +372 -241
  372. package/dist/resource/shader/reflection/Tr2EffectStageInput.js.map +1 -1
  373. package/dist/resource/shader/reflection/Tr2EffectTechnique.js +78 -68
  374. package/dist/resource/shader/reflection/Tr2EffectTechnique.js.map +1 -1
  375. package/dist/resource/shader/reflection/Tr2Pass.js +168 -111
  376. package/dist/resource/shader/reflection/Tr2Pass.js.map +1 -1
  377. package/dist/resource/shader/reflection/carbonRecordFields.js +159 -0
  378. package/dist/resource/shader/reflection/carbonRecordFields.js.map +1 -0
  379. package/dist/resource/shader/reflection/shaderStage.js +22 -0
  380. package/dist/resource/shader/reflection/shaderStage.js.map +1 -0
  381. package/dist/resource/shader/sampler/Tr2SamplerSetup.js +135 -77
  382. package/dist/resource/shader/sampler/Tr2SamplerSetup.js.map +1 -1
  383. package/dist/resource/texture/CjsTextureArrayRes.js +472 -472
  384. package/dist/resource/texture/CjsTextureArrayResParameterProxy.js +179 -179
  385. package/dist/resource/texture/Tr2ImageRes.js +120 -120
  386. package/dist/resource/texture/Tr2TextureLodManager.js +82 -82
  387. package/dist/resource/texture/Tr2TextureLodUpdateRequest.js +37 -37
  388. package/dist/resource/texture/Tr2TexturePackChannel.js +37 -37
  389. package/dist/resource/texture/Tr2TexturePipeline.js +54 -54
  390. package/dist/resource/texture/Tr2TexturePipelineParams.js +34 -34
  391. package/dist/resource/texture/Tr2TexturePipelineStepCompress.js +40 -40
  392. package/dist/resource/texture/Tr2TexturePipelineStepGenerateMips.js +22 -22
  393. package/dist/resource/texture/Tr2TexturePipelineStepLimitSize.js +34 -34
  394. package/dist/resource/texture/Tr2TexturePipelineStepLoad.js +31 -31
  395. package/dist/resource/texture/Tr2TexturePipelineStepPack.js +43 -43
  396. package/dist/resource/texture/TriTextureRes.js +359 -359
  397. package/dist/resource/texture/index.js +15 -15
  398. package/dist/resource/texture/texturePipelineBehavior.js +308 -308
  399. package/dist/worker/CjsResManMainThreadLoader.js +89 -87
  400. package/dist/worker/CjsResManMainThreadLoader.js.map +1 -1
  401. package/dist/worker/CjsResManWorker.js +218 -217
  402. package/dist/worker/CjsResManWorker.js.map +1 -1
  403. package/dist/worker/CjsResManWorkerLoader.js +437 -434
  404. package/dist/worker/CjsResManWorkerLoader.js.map +1 -1
  405. package/dist/worker/protocol.js +12 -12
  406. package/docs/README.md +98 -98
  407. package/docs/architecture.md +118 -109
  408. package/docs/concepts/resource-lifecycle.md +226 -225
  409. package/docs/concepts/shader-resource-model.md +111 -114
  410. package/docs/concepts/writing-an-engine-adapter.md +115 -115
  411. package/docs/formats/README.md +138 -136
  412. package/docs/formats/carbon-effect-container.md +553 -452
  413. package/docs/formats/dxbc/README.md +68 -68
  414. package/docs/formats/dxbc/architecture.md +80 -80
  415. package/docs/formats/dxbc/reference/api.md +77 -77
  416. package/docs/formats/dxbc/reference/classes/README.md +9 -9
  417. package/docs/formats/dxbc/reference/decoded-output.md +122 -122
  418. package/docs/formats/gr2.md +160 -160
  419. package/docs/formats/hlsl/README.md +54 -54
  420. package/docs/formats/hlsl/architecture.md +65 -67
  421. package/docs/formats/hlsl/guides/hydrating-json-output.md +60 -62
  422. package/docs/formats/hlsl/guides/reading-effects.md +64 -64
  423. package/docs/formats/hlsl/reference/advanced-analysis.md +61 -66
  424. package/docs/formats/hlsl/reference/api.md +92 -98
  425. package/docs/formats/hlsl/reference/classes/README.md +11 -11
  426. package/docs/formats/hlsl/reference/json-graph.md +97 -100
  427. package/docs/formats/pickle.md +82 -0
  428. package/docs/formats/provenance.md +196 -196
  429. package/docs/formats/stl.md +37 -37
  430. package/docs/formats/webgl/README.md +115 -57
  431. package/docs/formats/webgl/architecture.md +69 -70
  432. package/docs/formats/webgl/carbon-constant-layouts.md +326 -326
  433. package/docs/formats/webgl/decl-io.md +1234 -1234
  434. package/docs/formats/webgl/memory-structured.md +890 -871
  435. package/docs/formats/webgl/reference/classes/README.md +9 -9
  436. package/docs/formats/webgl/texture-sample.md +964 -964
  437. package/docs/formats/webgpu/README.md +84 -84
  438. package/docs/formats/webgpu/architecture.md +95 -96
  439. package/docs/formats/webgpu/formats/{cewgpu.md → carbon-webgpu.md} +215 -216
  440. package/docs/formats/webgpu/guides/effect-packaging.md +189 -191
  441. package/docs/formats/webgpu/reference/api.md +196 -197
  442. package/docs/formats/webgpu/reference/classes/README.md +9 -9
  443. package/docs/formats/webgpu/reference/wgsl-compatibility.md +1546 -1543
  444. package/docs/formats/wwise.md +146 -85
  445. package/docs/reference/classes/README.md +35 -35
  446. package/docs/reference/classes/audio.md +30 -30
  447. package/docs/reference/classes/core.md +216 -206
  448. package/docs/reference/classes/dropped.md +46 -46
  449. package/docs/reference/classes/formats.md +944 -952
  450. package/docs/reference/classes/resources.md +456 -456
  451. package/docs/reference/classes/texture.md +26 -26
  452. package/docs/reference/events.md +117 -117
  453. package/docs/reference/motherlode-cache.md +275 -275
  454. package/docs/reference/queues.md +194 -110
  455. package/docs/reference/reload.md +107 -107
  456. package/docs/reference/texture-arrays.md +113 -113
  457. package/docs/reference/texture-pipeline.md +53 -53
  458. package/docs/reference/workers.md +142 -135
  459. package/docs/roadmap.md +150 -150
  460. package/format-notices/black/LICENSE +21 -21
  461. package/format-notices/black/NOTICE +47 -47
  462. package/format-notices/bnk/LICENSE +21 -21
  463. package/format-notices/bnk/NOTICE +21 -21
  464. package/format-notices/cmf/LICENSE +21 -21
  465. package/format-notices/cmf/NOTICE +36 -36
  466. package/format-notices/dds/LICENSE +21 -21
  467. package/format-notices/dds/NOTICE +14 -14
  468. package/format-notices/dxbc/LICENSE +21 -21
  469. package/format-notices/dxbc/NOTICE +20 -20
  470. package/format-notices/fbx/LICENSE +21 -21
  471. package/format-notices/fbx/NOTICE +14 -14
  472. package/format-notices/flac/LICENSE +21 -21
  473. package/format-notices/flac/NOTICE +14 -14
  474. package/format-notices/gif/LICENSE +21 -21
  475. package/format-notices/gif/NOTICE +14 -14
  476. package/format-notices/gltf/LICENSE +21 -21
  477. package/format-notices/gltf/NOTICE +27 -27
  478. package/format-notices/gr2/LICENSE +21 -21
  479. package/format-notices/gr2/NOTICE +60 -60
  480. package/format-notices/gr2/THIRD-PARTY-NOTICES.md +93 -93
  481. package/format-notices/hlsl/LICENSE +21 -21
  482. package/format-notices/hlsl/NOTICE +25 -25
  483. package/format-notices/jpeg/LICENSE +21 -21
  484. package/format-notices/jpeg/NOTICE +14 -14
  485. package/format-notices/mp3/LICENSE +21 -21
  486. package/format-notices/mp3/NOTICE +14 -14
  487. package/format-notices/mp4/LICENSE +21 -21
  488. package/format-notices/mp4/NOTICE +14 -14
  489. package/format-notices/obj/LICENSE +21 -21
  490. package/format-notices/obj/NOTICE +26 -26
  491. package/format-notices/ogg/LICENSE +21 -21
  492. package/format-notices/ogg/NOTICE +28 -28
  493. package/format-notices/png/LICENSE +21 -21
  494. package/format-notices/png/NOTICE +14 -14
  495. package/format-notices/red/LICENSE +21 -21
  496. package/format-notices/red/NOTICE +31 -31
  497. package/format-notices/stl/LICENSE +21 -21
  498. package/format-notices/stl/NOTICE +21 -21
  499. package/format-notices/tga/LICENSE +21 -21
  500. package/format-notices/tga/NOTICE +14 -14
  501. package/format-notices/wav/LICENSE +21 -21
  502. package/format-notices/wav/NOTICE +14 -14
  503. package/format-notices/webgl/LICENSE +21 -21
  504. package/format-notices/webgl/NOTICE +35 -35
  505. package/format-notices/webgpu/LICENSE +21 -21
  506. package/format-notices/webgpu/NOTICE +31 -31
  507. package/format-notices/webm/LICENSE +21 -21
  508. package/format-notices/webm/NOTICE +14 -14
  509. package/format-notices/webp/LICENSE +21 -21
  510. package/format-notices/webp/NOTICE +14 -14
  511. package/format-notices/wem/LICENSE +57 -57
  512. package/format-notices/wem/NOTICE +33 -33
  513. package/format-notices/yaml/LICENSE +21 -21
  514. package/format-notices/yaml/NOTICE +44 -44
  515. package/package.json +63 -63
  516. package/dist/format/carbonEffect/carbonDescriptionFromPortable.js +0 -372
  517. package/dist/format/carbonEffect/carbonDescriptionFromPortable.js.map +0 -1
  518. package/dist/format/effect/effectReflectionPackage.js +0 -636
  519. package/dist/format/effect/effectReflectionPackage.js.map +0 -1
  520. package/dist/formats/hlsl/core/HlslReader.js +0 -16
  521. package/dist/formats/hlsl/core/HlslReader.js.map +0 -1
  522. package/dist/formats/hlsl/core/portableReflection.js +0 -848
  523. package/dist/formats/hlsl/core/portableReflection.js.map +0 -1
  524. package/dist/formats/hlsl/portable.js +0 -2
  525. package/dist/formats/hlsl/portable.js.map +0 -1
  526. package/dist/formats/webgl/core/cewg/CewgPackage.js +0 -340
  527. package/dist/formats/webgl/core/cewg/CewgPackage.js.map +0 -1
  528. package/dist/formats/webgl/core/cewg/CewgPackageBuilder.js +0 -104
  529. package/dist/formats/webgl/core/cewg/CewgPackageBuilder.js.map +0 -1
  530. package/dist/formats/webgl/core/cewg/binary.js +0 -19
  531. package/dist/formats/webgl/core/cewg/binary.js.map +0 -1
  532. package/dist/formats/webgl/core/cewgCompleteness.js.map +0 -1
  533. package/dist/formats/webgl/core/effectPackageValidation.js +0 -331
  534. package/dist/formats/webgl/core/effectPackageValidation.js.map +0 -1
  535. package/dist/formats/webgpu/core/cewgpu/CewgpuContainer.js.map +0 -1
  536. package/dist/formats/webgpu/core/cewgpu/containerViews.js.map +0 -1
  537. package/dist/formats/webgpu/core/cewgpu/validateContainer.js.map +0 -1
  538. package/dist/resource/shader/portable.js +0 -33
  539. package/dist/resource/shader/portable.js.map +0 -1
  540. package/docs/formats/hlsl/reference/portable-reflection.md +0 -141
  541. package/docs/formats/webgl/effect-reflection.md +0 -127
@@ -1,452 +1,553 @@
1
- # Carbon compiled-effect container
2
-
3
- Status: Stable
4
- Visibility: Public
5
- Scope: `@carbonenginejs/runtime-resource` — `src/format/carbonEffect/`, `src/format/CjsByteReader.js`, `src/format/CjsByteWriter.js`, `src/format/CjsStringTable.js`
6
- Audience: Anyone reading or writing compiled shader effect bytes, or extending `.cewgpu`
7
- Summary: The v15 binary layout of Carbon's compiled effect files, the shared byte reader and writer that implement it, and the arena offset policy a byte-exact re-emit depends on.
8
-
9
- ## What this is
10
-
11
- Carbon's shader compiler emits one file per effect containing **every permutation**,
12
- selected at read time through an offset table. The format has three parts: a header,
13
- a deduplicated blob arena ("string table"), one dense offset-table row per
14
- permutation, and one stored description blob per distinct encoded body.
15
-
16
- This package implements it as a shared reader and writer, verified byte-exact against
17
- CCP's own shipped files. It is the wire foundation for `.cewgpu`. The `.cewg`
18
- WebGL package retains its separate `CEWG` magic and chunk layout.
19
-
20
- | module | role |
21
- |---|---|
22
- | `src/format/CjsByteReader.js` | little-endian cursor plus arena resolution primitives; shared by the HLSL, Carbon-effect, and WebGL readers |
23
- | `src/format/CjsByteWriter.js` | growable append cursor with reserve-and-patch |
24
- | `src/format/CjsStringTable.js` | the blob arena, with Carbon's bytewise-sorted offset assignment |
25
- | `src/format/carbonEffect/carbonEffectRecords.js` | the v15 description-blob record codec |
26
- | `src/format/carbonEffect/CjsCarbonEffectReader.js` | container reader and structural checks |
27
- | `src/format/carbonEffect/CjsCarbonEffectWriter.js` | container writer, offset arithmetic and alias dedupe |
28
-
29
- ## Why v15 only
30
-
31
- The reader and writer accept and emit version 15 and nothing else.
32
-
33
- Carbon's own reader accepts versions 2 through 15, but its v13/v14 branches mark
34
- the field-order boundaries as uncertain. Version 15 is the version with an
35
- authoritative writer to check against, and the entire audited shipped corpus at
36
- build 3444265 is v15 3222 files across `effect.dx11` and `effect.dx12`, plus
37
- the same 537 shaders again under `effect.metal`. Nothing older appears in that
38
- audited corpus.
39
-
40
- The v15 body is byte-identical to v14. Version 15 differs from 14 only by the 36
41
- extra header bytes: the compiler version and the source hash.
42
-
43
- ## Layout
44
-
45
- ### Header
46
-
47
- ```
48
- u32 version = 15
49
- u8[4] shaderCompilerVersion {major, minor, patch, tweak}
50
- char[32] sourceHash ASCII hex MD5 of the HLSL source inputs
51
- u32 stringTableSize | arena payload
52
- u8 permutationCount | permutation records
53
- u32 recordCount | recordCount x { u32 index, u32 offset, u32 size }
54
- description blobs
55
- ```
56
-
57
- The compiler writes this order and the runtime reads the same order.
58
-
59
- The compiler version is **four bytes, not a `u32`**, and Carbon's rebuild check
60
- compares only the first three. A shipped v15 header reads `01 02 06 00` —
61
- compiler 1.2.6.0, matching the ShaderCompiler project version. As a `u32` those
62
- bytes are `0x00060201`, which means nothing. `HlslEffectRes` historically read
63
- the field as a dword; it now also exposes `m_compilerVersionBytes`, which is the
64
- truthful reading and what new code should use. The dword form survives only
65
- because it is republished as `source.compilerVersion` in the portable
66
- reflection, where it is asserted to be an unsigned integer and covered by a
67
- package digest.
68
-
69
- The compiler's rebuild check reads the 32-byte hash, while the runtime skips it.
70
- It is provenance, not integrity.
71
-
72
- A permutation record is:
73
-
74
- ```
75
- u32 nameOffset | u8 defaultOption | u32 descriptionOffset | u8 type | u8 optionCount | u32 optionOffset[optionCount]
76
- ```
77
-
78
- Note the field order: `defaultOption` sits between the name and the description.
79
- Carbon writes that byte inside a conditional loop with no `else`, so a
80
- permutation whose declared default matches no option would emit a record one
81
- byte short and desynchronise the entire rest of the header. Our writer always
82
- emits it.
83
-
84
- ### Body-offset arithmetic
85
-
86
- ```
87
- base = 4 + 4 + 32 + headerSize + stringTable.GetSize()
88
- headerSize = (recordCount * 3 + 1) * 4 + permutationBytes
89
- permutationBytes = 1 + Σ (11 + optionCount * 4)
90
- ```
91
-
92
- `GetSize()` **includes** the arena's own `u32` length prefix, so the prefix is
93
- counted exactly once. Row offsets are absolute from byte 0 of the file.
94
- `CjsCarbonEffectWriter` asserts that the bytes it actually wrote before the
95
- first body equal this computed base, so an arithmetic error fails loudly rather
96
- than shifting every body.
97
-
98
- ### The arena
99
-
100
- `CjsStringTable` is Carbon's `StringTable`. Three properties matter:
101
-
102
- 1. **Offsets are assigned by a bytewise sort, not by insertion order.**
103
- The comparison is `memcmp` over the shorter length, then shorter-wins on a
104
- tie, before cumulative offsets are assigned. Any writer that assigns
105
- first-seen offsets produces a valid file that is not byte-identical to
106
- Carbon's.
107
- 2. **Dedupe is on exact bytes with no suffix merging.** `"red"` gets its own
108
- entry even though it is a suffix of `"shared"`.
109
- 3. **There are two kinds of entry and no manifest.** A NUL-terminated string is
110
- added with its terminator and referenced by a bare `u32` offset. A sized blob
111
- shader bytecode, program source, default constant values — is added with
112
- exactly its own bytes and referenced by a `{u32 size, u32 offset}` pair. The
113
- arena writes a `u32` payload size and the payload, nothing else; every
114
- reference site resolves its own entry.
115
-
116
- ## Two rules for anything added later
117
-
118
- Neither is visible from Carbon's own code, and both were found by implementing
119
- rather than by reading. They constrain every future addition to this format.
120
-
121
- **Rule 1: every sized record must parse to exactly its declared end.** Trailing
122
- bytes mean one of two things and both are fatal the writer knew fields this reader
123
- does not, or the writer miscounted. Enforced for the description blob
124
- (`readEffectDescription`), for the per-pass backend block (`readBackendBlock`), and
125
- for the header, whose end must equal where the body region begins.
126
-
127
- This rule carries weight that used to live elsewhere. The chunk container it replaces
128
- spent roughly 600 lines asserting that its several projections of one effect still
129
- agreed with each other, and those checks caught a malformed *tree* our writer
130
- emitting something structurally wrong — not only a malformed file. A record layout
131
- makes most of that question unaskable, because containment replaces reference and
132
- position replaces key. What remains is this: a writer bug either fails to parse,
133
- which announces itself, or it parses and leaves the cursor somewhere other than the
134
- declared end. Applying the rule to some sized records and not others is a gap that
135
- stays invisible until a writer bug hides in one of the others.
136
-
137
- **Rule 2: anything placed in the arena must be arena-independent.** An arena entry
138
- cannot contain an arena offset. Offsets are assigned by the content sort, the sort
139
- depends on every entry's bytes, so an entry that referred to the arena would have to
140
- be interned before its own contents could be computed a circular dependency with
141
- no fixed point. This is invisible in Carbon's own code because no Carbon arena blob
142
- refers to the arena: strings, bytecode and default constant values are all leaves.
143
- Our per-pass backend block is the first non-leaf candidate, and it is why that block
144
- carries inline length-prefixed strings instead of references. A test pins the
145
- property directly — the block's bytes must be identical whichever arena it is
146
- interned into. Any future arena entry must satisfy the same rule.
147
-
148
- ### One field the container cannot round-trip
149
-
150
- For a **non-dynamic sampler, the name is not preserved.** The file stores one, but
151
- Carbon's reader nulls it before any producer sees it, so a package built from
152
- our reflection carries the empty string.
153
-
154
- This is a property of the input, not a bug in the mapping: the name is unrecoverable
155
- by the time we receive the data, rather than dropped on the way out. Carbon nulls it
156
- precisely because a non-dynamic sampler is never looked up by name — `FindSamplerByName`
157
- only matters for the dynamic case. Recorded here because it will otherwise be
158
- rediscovered as a bug: a diff against the source effect will always show it.
159
-
160
- **Corollary: the container admits all six of Carbon's stage types.** `stages` is
161
- capped at `SHADER_TYPE_COUNT` = 6, and the stage-type byte uses Carbon's
162
- `InputStageType` numbering: vertex, pixel, compute, geometry, hull, domain. A
163
- backend that can only express three of those rejects the rest in its own layer;
164
- the container does not narrow on its behalf. The Carbon region is
165
- backend-invariant, and restrictions belong to the backend.
166
-
167
- `0xffffffff` is the null reference. It is legal at **exactly one wire
168
- position**: a stage's default-constant-value offset when the accompanying size
169
- is zero, which the optional-value reader consumes without dereferencing.
170
- Everywhere else a `0xffffffff` offset fails the load.
171
-
172
- Two deliberate departures from Carbon, both of which make byte-identical output
173
- more likely rather than less:
174
-
175
- - **`m_size` is initialised.** Carbon's constructor leaves it indeterminate and
176
- gets away with it only because the one instance is a zero-initialised global.
177
- - **Adding after an offset has been handed out is an error.** In Carbon,
178
- `GetOffset` re-sorts a dirty table, which reassigns *every* offset including
179
- offsets already baked into packed bodies. Carbon avoids the corruption by
180
- interning all late strings before the packing pass. `CjsCarbonEffectWriter`
181
- reproduces that discipline structurally: it runs the record walk twice, once
182
- with `collectArena` to intern and once with `internArena` to emit. Because both
183
- passes drive the same `writeEffectDescription`, they cannot drift apart.
184
-
185
- ### Description blob, v15 field order
186
-
187
- Derived independently from the writer's save order and the reader's load order,
188
- then confirmed to agree field for field. Counts are `u8` unless marked.
189
-
190
- ```
191
- u8 techniqueCount
192
- u32 name
193
- u8 passCount cap 64
194
- u8 stageCount cap 6 (SHADER_TYPE_COUNT)
195
- u8 stageType
196
- u32 shaderSize | u32 shaderDataOffset program payload, arena blob
197
- u32 threadGroupSize[3]
198
- u8 pipelineInputCount cap 64
199
- u8 usage, registerIndex, usageIndex, usedMask, type, dimension
200
- -- StageData --
201
- u8 registerCount
202
- u8 registerType | u32 registerIndex | u32 registerCount | u8 registerSpace
203
- u8 staticSamplerCount
204
- u32 registerIndex | u8 registerSpace | u8 x7 filters/address
205
- f32 mipLODBias | u8 maxAnisotropy | u8 comparisonFunc
206
- u8 borderColor enum, NOT four floats
207
- f32 minLOD | f32 maxLOD
208
- u32 constantCount u32, not u8
209
- u32 name | u32 offset | u32 size | u8 type | u8 dimension
210
- u32 elements | u8 isSRGB | u8 isAutoregister
211
- u32 defaultValuesSize | u32 defaultValuesOffset 0xffffffff legal when size 0
212
- u8 textureCount cap 64
213
- u8 registerIndex | u32 name | u8 type | u32 count | u8 isSRGB | u8 isAutoregister
214
- u8 samplerCount cap 64
215
- u8 registerIndex | u32 name | u8 x7 | f32 mipLODBias | u8 maxAnisotropy
216
- u8 comparisonFunc | f32 borderColor[4] | f32 minLOD | f32 maxLOD | u8 isDynamic
217
- u8 uavCount cap 64
218
- u8 registerIndex | u32 name | u8 type | u32 count | u8 isAutoregister
219
- u8 annotationCount
220
- u32 name | u8 type | (u32 stringOffset if type == STRING else 4 raw bytes)
221
- u8 renderStateCount cap 64
222
- u32 state | u32 value
223
- u8 libraryCount
224
- u32 payloadSize | u32 shaderSize | u32 shaderDataOffset
225
- u32 exportCount u32, not u8
226
- u8 type | u32 name
227
- u32 hitGroupName
228
- StageData globalInputs
229
- StageData localInputs
230
- u16 parameterCount cap 256
231
- u32 name | annotation map as above
232
- ```
233
-
234
- Four places this is easy to get wrong:
235
-
236
- - **At v15 the program payload comes first and the signature tables follow.**
237
- `pipelineInputs` and `registers` sit *after* `shaderCode` and `threadGroupSize`.
238
- Before v14 it was the other way round; v14 moved them, and Carbon marks its own
239
- v14 branch as uncertain.
240
- - **A UAV record is one byte shorter than a texture record** — it has no `isSRGB`.
241
- Carbon's reader hardcodes `isSRGB = false` and the writer omits it. Sharing one
242
- "resource" codec between the two silently corrupts every subsequent field.
243
- - **`borderColor` is four floats on a sampler and one byte on a static sampler**
244
- because the two records mirror different D3D binding models.
245
- - **A non-string annotation value is four raw bytes.** Carbon writes it through the
246
- `float` member of a `{float,int32_t}` union and reads it back through a different
247
- union. The bytes round-trip; applying an int/float conversion does not. The codec
248
- keeps `rawValue` as bytes for exactly this reason.
249
-
250
- Carbon writes `textures`, `samplers`, `uavs` and render states in ascending key
251
- order and sorts annotation keys by bytewise string comparison.
252
- `compareAnnotationNames` implements that comparison over UTF-8 bytes, which is
253
- *not* the same as JavaScript's UTF-16 code-unit order for names outside ASCII
254
- `"Z"` sorts before `"a"`.
255
-
256
- ### The optional trailing block
257
-
258
- Our own containers add exactly one optional block per pass, after the render-state
259
- table, referenced by a `{u32 size, u32 offset}` pair into the arena. A Carbon file
260
- ends the pass at the render states, so the reader and writer gate it on
261
- `{ backend: true }` and produce Carbon's bytes unchanged when it is closed.
262
-
263
- The block carries the two sections that are not derivable from Carbon reflection —
264
- WebGPU bind-group layouts and resource transforms in **one** unit, because they
265
- are mutually required and because "the Carbon region is backend-invariant, with
266
- exactly one optional trailing block" is the invariant worth keeping.
267
-
268
- It lives in the arena so identical layouts dedupe across bodies the way program
269
- source does; measured sharing is 30.5:1 at `(body, pass)` granularity, 22 distinct
270
- blocks across 672 pairs. That forces one property: **the block contains no arena
271
- offsets.** An offset is only known after the arena's content sort, which depends on
272
- every blob's bytes including this one, so a block referencing the arena could not be
273
- built before it was interned. Strings inside it are inline and length-prefixed.
274
-
275
- ```
276
- u8 blobVersion = 1
277
- u8 bindGroupCount
278
- u8 group | u8 bindingCount
279
- u8 resourceKind | u8 registerSpace | u8 binding | u8 visibilityMask
280
- u32 registerIndex | u32 structureStride (0xffffffff absent) | u8 arrayLayerCount (0 absent)
281
- str type | str generatedSymbol | str transformId (empty = none)
282
- u8 transformCount
283
- u8 familyCode | str id | u8 inputCount
284
- u8 registerSpace | u8 registerIndex | str parameter
285
- ```
286
-
287
- `identity` and `group` on each binding, and a transform's `kind`,
288
- `stage`, `representation`, `missingLayer`, `viewDimension`, `layerCount`,
289
- `output.identity`, `output.scopeIdentity`, `output.name`, `layoutKey` and every
290
- input's `layer` are restored on read, not stored. The family byte is what keeps them
291
- derivable without pinning the format to one recognizer. `id` and each input's
292
- `parameter` stay on the wire deliberately — `id` because a caller may supply it,
293
- `parameter` because it keeps layer identity cross-checkable rather than asserted by
294
- position.
295
-
296
- Backend-block version 1 stores visibility but not the original
297
- `scopeIdentity`. The reader reconstructs `${identity}@${visibility[0]}`. A
298
- multi-stage shared binding therefore rereads as stage-qualified rather than
299
- recovering its original bare scope. Callers must not infer that original
300
- sharing decision from the wire view.
301
-
302
- An unknown `blobVersion` reports the pass as having no backend data rather than
303
- misparsing it; the enclosing size makes it skippable.
304
-
305
- ### Count caps
306
-
307
- `CARBON_EFFECT_COUNT_CAPS` mirrors the runtime's inclusive limits. Carbon's
308
- compiler enforces none of them while its runtime rejects anything above them,
309
- so an over-large effect compiles and then fails to load; our writer checks on
310
- the way out. The caps Carbon does *not* have techniques, registers, static
311
- samplers, constants, libraries, exports, annotation counts are deliberately
312
- not invented here.
313
-
314
- ### The alias path
315
-
316
- Carbon compares packed bodies pairwise and points a duplicate's row at the
317
- surviving twin. The row is **kept**, so the offset table stays dense while the
318
- file stores each distinct body once. Across the shipped corpus 22% of files
319
- alias, at roughly 2.1 rows per distinct body.
320
-
321
- ## Offset-table density
322
-
323
- Carbon indexes the offset table **positionally** and never reads each row's
324
- stored `index` field. A sparse or misordered table therefore does not fail — it
325
- silently returns the wrong shader body.
326
-
327
- Density is incidental in Carbon: it falls out of `g_compiledEffects` being a
328
- `std::map` densely keyed by the work-queue builder, and is promised nowhere.
329
-
330
- Measured twice at build 3444265. A header-only sweep of every `.sm_hi`, `.sm_lo`
331
- and `.sm_depth` under `effect.dx11` and `effect.dx12` 3222 files, 52,332 rows
332
- and the full round-trip run below across all three backends 4833 files, 78,498
333
- rows, 40,645 distinct bodies. **Every file is dense and positionally indexed**, and
334
- every row's byte range lies inside the file and clear of the header. In every
335
- single file the body region tiles the post-header space exactly, with no leading or
336
- trailing slack.
337
-
338
- Given that, the checks are implemented as follows:
339
-
340
- - `CjsCarbonEffectReader` **always** collects `dense` and `indicesMatchPosition` as
341
- diagnostics, and **always** fails closed on an out-of-range row.
342
- - Density and positional indexing **fail closed on read by default**.
343
- `{ permissive: true }` skips the check and leaves the diagnostics in place, for
344
- forensic inspection of a file already known to be malformed. It is not a load
345
- option.
346
- - `writeCarbonEffectFile` **always** fails closed: it refuses to emit bodies that
347
- are not dense from index 0. Where we own the bytes there is no reason to be
348
- lenient.
349
-
350
- `--ignore-permutations` does make CCP's compiler emit only key 0 while declaring
351
- every axis, so a sparse file is producible. That argues for the escape hatch, not
352
- for permissive defaults: Carbon does not reject such a file, it returns the wrong
353
- permutation's shader silently, which is the failure class this port exists to
354
- close.
355
-
356
- ## Backend selection and envelope removal
357
-
358
- **Historical compatibility.** Flat-package builds once prepended
359
- `magic | containerVersion | payloadKind` before Carbon's byte-compatible
360
- layout. Current packaging emits no such prefix and the former helper is no
361
- longer part of the package.
362
-
363
- **Current WebGPU wire.** CEWGPU bytes have no envelope, magic, `payloadKind`,
364
- or independent container version. They are bare Carbon v15 records resolved
365
- from `effect.webgpu/`, with one optional per-pass backend block. `.cewg`
366
- remains a separate CEWG chunk format rather than this Carbon-record wire.
367
-
368
- `CewgpuContainer` reads the optional blocks, and the shared record reader can
369
- auto-detect them from a description's declared size. Direct conversion from
370
- those descriptions into the portable reflection envelope consumed by
371
- `Tr2EffectRes` remains an adapter boundary.
372
-
373
- Versioning remains local to what it versions: Carbon's version dword governs
374
- the Carbon region, while `blobVersion` governs the optional backend block. An
375
- unknown block version is skipped rather than misparsed. The package does not
376
- claim a version in CCP's namespace.
377
-
378
- Loose program bytes without a resource path can be identified from their
379
- payload: DXBC opens with `"DXBC"`, AIR is bitcode (`BC 0xC0DE`), and WGSL and
380
- GLSL have distinct text syntax. The stage record itself carries no language
381
- tag; program interpretation remains a backend/path responsibility.
382
-
383
- ## Verification
384
-
385
- `node --test` in this package. Two gates.
386
-
387
- **Always green.** `test/format/byte-primitives.test.js` and
388
- `test/format/carbon-effect.test.js` build a synthetic four-permutation v15
389
- container exercising every record type static samplers, UAVs, annotations of
390
- every value type, render states, a raytracing library with both stage-data blocks
391
- and assert a byte-exact write read write round trip, the arena sort order,
392
- the caps, the structural checks and, until the switchover, the legacy envelope's
393
- disjointness.
394
-
395
- **Env-gated real-file proof.** `test/format/carbon-effect-corpus.test.js`, enabled
396
- with `CARBON_EFFECT_CORPUS_DIR`. Game bytes are never committed. Supply a
397
- separately acquired corpus at pinned build 3444265. The test re-emits each file
398
- three ways:
399
-
400
- 1. every description blob through the file's own arena proves the field order;
401
- 2. the whole container from raw bodies and the source arena — proves the header
402
- order, the base arithmetic and the alias path;
403
- 3. the whole container with the arena rebuilt from the references found — proves
404
- the sorted-offset policy.
405
-
406
- Only the third can legitimately differ, because an arena may retain blobs the file
407
- no longer references. When it does differ the divergence is reported exactly and
408
- asserted to be unreferenced-blob retention; it is never downgraded to a weaker
409
- comparison such as "same strings, any order", which would look green and prove
410
- nothing.
411
-
412
- Measured result over the complete corpus — 4833 files (537 shaders × 3 variants ×
413
- 3 backends), 78,498 offset-table rows, 40,645 distinct description bodies:
414
- **all three modes byte-exact, with zero arena-rebuild divergences, zero sparse
415
- tables and zero misordered tables.** Not one shipped file retains an unreferenced
416
- arena blob, so the sorted-offset policy reproduces CCP's arena exactly.
417
- Supply a separately acquired corpus and set `CARBON_EFFECT_CORPUS_DIR`; no
418
- corpus data ships with the package.
419
-
420
- That result is the container port's central evidence. The same reader and the same
421
- writer reproduce, byte for byte, files whose program payloads are DXBC in two
422
- dialects and AIR — with no language field anywhere in the format. The metadata
423
- region is backend-invariant as a measured fact rather than an argument from the
424
- writer. Backend selection therefore belongs at the resource-path boundary, not
425
- in an envelope or per-stage record.
426
-
427
- `effect.gles2` is deliberately not a validation target for **this package**: those
428
- shaders are v8, and nothing in the container port reads or writes them.
429
-
430
- **Do not read that as "obsolete".** `effect.gles2` is the shader tree ccpwgl
431
- actually renders with today it is the only one that currently works end to end.
432
-
433
- The two statements coexist because **v15-only constrains what we write and
434
- validate against, not what a reader may accept.** Version-branching is the
435
- format's own mechanism. A reader that wants all supported generations branches
436
- on the version dword v2..8 legacy gles2, v15 everything current — which is one
437
- reader, not a bespoke path per format.
438
-
439
- **Our containers are v15, not a version of our own.** A "v16" was considered for
440
- the variant carrying the per-pass backend block and **rejected**: CCP owns that
441
- number space, so claiming 16 would collide with any real v16 they ship, in the one
442
- field whose entire job is telling a reader how to parse. It also failed the rule
443
- the rest of this format is held to invent something only because it *has to*
444
- exist, never because we think it should.
445
-
446
- The container needs no new version. Each description blob carries a declared
447
- size in the offset table, and [Rule 1](#two-rules-for-anything-added-later)
448
- already requires it to parse to exactly that end. A reader parses a blob without
449
- blocks and re-parses with them if the cursor misses the declared end, so the
450
- presence of the block is **self-describing** with no new field, no container
451
- version and no out-of-band flag. `blobVersion` inside the block versions the
452
- extension itself.
1
+ # Carbon compiled-effect container
2
+
3
+ Status: Stable
4
+ Visibility: Public
5
+ Scope: `@carbonenginejs/runtime-resource` — `src/format/carbonEffect/`, `src/format/CjsByteReader.js`, `src/format/CjsByteWriter.js`, `src/format/CjsStringTable.js`
6
+ Audience: Anyone reading or writing compiled shader effect bytes, or extending `.carbonwebgpu`
7
+ Summary: The v15 binary layout of Carbon's compiled effect files, the shared byte reader and writer that implement it, and the arena offset policy a byte-exact re-emit depends on.
8
+
9
+ ## What this is
10
+
11
+ Carbon's shader compiler emits one file per effect containing **every permutation**,
12
+ selected at read time through an offset table. The format has three parts: a header,
13
+ a deduplicated blob arena ("string table"), one dense offset-table row per
14
+ permutation, and one stored description blob per distinct encoded body.
15
+
16
+ This package implements it as a shared reader and writer, verified byte-exact against
17
+ CCP's own shipped files. It is the wire foundation for **both** browser backends:
18
+ `.carbonwebgpu` and `.carbonwebgl` are the same Carbon v15 container, differing
19
+ only in the program text occupying each stage's slot and in the optional
20
+ per-pass backend block. Neither carries a private magic or chunk layout.
21
+
22
+ | module | role |
23
+ |---|---|
24
+ | `src/format/CjsByteReader.js` | little-endian cursor plus arena resolution primitives; shared by the HLSL, Carbon-effect, and WebGL readers |
25
+ | `src/format/CjsByteWriter.js` | growable append cursor with reserve-and-patch |
26
+ | `src/format/CjsStringTable.js` | the blob arena, with Carbon's bytewise-sorted offset assignment |
27
+ | `src/format/carbonEffect/carbonEffectRecords.js` | the v15 description-blob record codec |
28
+ | `src/format/carbonEffect/CjsCarbonEffectReader.js` | container reader and structural checks |
29
+ | `src/format/carbonEffect/CjsCarbonEffectWriter.js` | container writer, offset arithmetic and alias dedupe |
30
+
31
+ ## Versioning: how Carbon does it, and what this reader does instead
32
+
33
+ ### Carbon validates the version, then threads it through parsing
34
+
35
+ Carbon reads the version dword once, in `Tr2EffectRes`, and rejects anything
36
+ outside 2..15 (`Tr2EffectRes.cpp:209`). Inside that range it never branches to
37
+ accept or refuse again the version becomes a *parsing input*. It is first used
38
+ by the container reader itself, then **passed as a parameter** into the effect
39
+ description's read call (`Tr2EffectRes.cpp:128`), alongside the buffer, its
40
+ size, and the string table.
41
+
42
+ From there it is threaded through every record reader in
43
+ `Tr2EffectDescription.cpp` — `ReadConstant`, `ReadResource`,
44
+ `ReadPipelineInputs`, `ReadRegisters`, `ReadInput`, and the pass/technique loop
45
+ which branch on it at 22 conditional sites using 16 distinct comparisons:
46
+
47
+ | Threshold | What it decides |
48
+ | --- | --- |
49
+ | `> 8` | Whether a stage carries a register signature **at all** |
50
+ | `< 14` | Where pipeline inputs are read, and whether registers are read beside them |
51
+ | `< 11` | How a constant's type byte is decoded — older files remap a legacy enum, so the byte count is unchanged but its meaning is not |
52
+ | `>= 13` | Whether a resource carries `arrayElements` at all; below 13 it is absent and defaults to 1 |
53
+ | `< 5`, `< 4`, `>= 3`, `>= 4` | Stage-input presence and ordering |
54
+ | `> 6`, `> 9`, `> 10`, `> 12`, `>= 8` | Per-record field presence |
55
+ | `> 13`, `>= 14` | Library and pipeline-input placement |
56
+
57
+ The consequence worth internalising: **the version determines the byte layout at
58
+ nearly every level of the reflection graph**, not just the header. A v8 file and
59
+ a v15 file are not the same records with a different preamble.
60
+
61
+ Note where the version does *not* reach: `Tr2Shader` itself has no version field
62
+ and no version-dependent branch. Container/header parsing and description
63
+ parsing between them absorb every version difference before the graph reaches
64
+ `Tr2Shader` the container reader has its own branches (`Tr2EffectRes.cpp`
65
+ handles `< 5` for the legacy header size, `>= 15` for the compiler version and
66
+ source hash, and `> 5` for the permutation type), and the description reader
67
+ handles the rest. What emerges carries no version, so consumers above that
68
+ boundary are version-free. A port that preserves the same separation stays
69
+ consistent with Carbon.
70
+
71
+ ### What this reader does: constant-folded to 15
72
+
73
+ `readEffectDescription` takes no version parameter. This reader is
74
+ constant-folded to version 15 and rejects everything else outright.
75
+
76
+ That is a deliberate narrowing, and it is the missing piece if an older version
77
+ is ever needed. Supporting v8 does **not** mean writing a second reader; it
78
+ means restoring the parameter Carbon always had and implementing the branches
79
+ above. The writer is unaffected reading an old version does not imply emitting
80
+ one, and this package emits v15 only.
81
+
82
+ Rejecting is the correct failure mode rather than a limitation to route around.
83
+ Applying v15 rules to an older layout can misalign fields — the comparisons
84
+ above change which fields are present and how their bytes are interpreted — and
85
+ must not be attempted. How such a read would fail in practice is not documented
86
+ here, because no legacy container has been examined to establish it.
87
+
88
+ The reader rejects with the version it read, and does not classify it further.
89
+ A leading dword of 2..14 is a *recognized legacy version number*, not evidence
90
+ that the bytes are an effect container: arbitrary data can begin with a small
91
+ integer, and the version dword alone carries no magic or checksum to confirm it.
92
+ Anything that reaches this rejection needs identifying by other means before it
93
+ is treated as a porting question rather than a wrong file.
94
+
95
+ ### Why 15 is the version that got implemented
96
+
97
+ Version 15 is the one with an authoritative writer to check against, and the
98
+ entire audited shipped corpus at build 3444265 is v15 — 3222 files across
99
+ `effect.dx11` and `effect.dx12`, plus the same 537 shaders again under
100
+ `effect.metal`. Nothing older appears in that audited corpus. Carbon's own
101
+ v13/v14 branches additionally mark their field-order boundaries as uncertain.
102
+
103
+ The v15 body is byte-identical to v14. Version 15 differs from 14 only by the 36
104
+ extra header bytes: the compiler version and the source hash.
105
+
106
+ ## Layout
107
+
108
+ ### Header
109
+
110
+ ```
111
+ u32 version = 15
112
+ u8[4] shaderCompilerVersion {major, minor, patch, tweak}
113
+ char[32] sourceHash ASCII hex MD5 of the HLSL source inputs
114
+ u32 stringTableSize | arena payload
115
+ u8 permutationCount | permutation records
116
+ u32 recordCount | recordCount x { u32 index, u32 offset, u32 size }
117
+ description blobs
118
+ ```
119
+
120
+ The compiler writes this order and the runtime reads the same order.
121
+
122
+ The compiler version is **four bytes, not a `u32`**, and Carbon's rebuild check
123
+ compares only the first three. A shipped v15 header reads `01 02 06 00` —
124
+ compiler 1.2.6.0, matching the ShaderCompiler project version. As a `u32` those
125
+ bytes are `0x00060201`, which means nothing. `HlslEffectRes` historically read
126
+ the field as a dword; it now also exposes `m_compilerVersionBytes`, which is the
127
+ truthful reading and what new code should use.
128
+
129
+ The compiler's rebuild check reads the 32-byte hash, while the runtime skips it.
130
+ It is provenance, not integrity.
131
+
132
+ A permutation record is:
133
+
134
+ ```
135
+ u32 nameOffset | u8 defaultOption | u32 descriptionOffset | u8 type | u8 optionCount | u32 optionOffset[optionCount]
136
+ ```
137
+
138
+ Note the field order: `defaultOption` sits between the name and the description.
139
+ Carbon writes that byte inside a conditional loop with no `else`, so a
140
+ permutation whose declared default matches no option would emit a record one
141
+ byte short and desynchronise the entire rest of the header. Our writer always
142
+ emits it.
143
+
144
+ ### Body-offset arithmetic
145
+
146
+ ```
147
+ base = 4 + 4 + 32 + headerSize + stringTable.GetSize()
148
+ headerSize = (recordCount * 3 + 1) * 4 + permutationBytes
149
+ permutationBytes = 1 + Σ (11 + optionCount * 4)
150
+ ```
151
+
152
+ `GetSize()` **includes** the arena's own `u32` length prefix, so the prefix is
153
+ counted exactly once. Row offsets are absolute from byte 0 of the file.
154
+ `CjsCarbonEffectWriter` asserts that the bytes it actually wrote before the
155
+ first body equal this computed base, so an arithmetic error fails loudly rather
156
+ than shifting every body.
157
+
158
+ ### The arena
159
+
160
+ `CjsStringTable` is Carbon's `StringTable`. Three properties matter:
161
+
162
+ 1. **Offsets are assigned by a bytewise sort, not by insertion order.**
163
+ The comparison is `memcmp` over the shorter length, then shorter-wins on a
164
+ tie, before cumulative offsets are assigned. Any writer that assigns
165
+ first-seen offsets produces a valid file that is not byte-identical to
166
+ Carbon's.
167
+ 2. **Dedupe is on exact bytes with no suffix merging.** `"red"` gets its own
168
+ entry even though it is a suffix of `"shared"`.
169
+ 3. **There are two kinds of entry and no manifest.** A NUL-terminated string is
170
+ added with its terminator and referenced by a bare `u32` offset. A sized blob
171
+ — shader bytecode, program source, default constant values — is added with
172
+ exactly its own bytes and referenced by a `{u32 size, u32 offset}` pair. The
173
+ arena writes a `u32` payload size and the payload, nothing else; every
174
+ reference site resolves its own entry.
175
+
176
+ ## Two rules for anything added later
177
+
178
+ Neither is visible from Carbon's own code, and both were found by implementing
179
+ rather than by reading. They constrain every future addition to this format.
180
+
181
+ **Rule 1: every sized record must parse to exactly its declared end.** Trailing
182
+ bytes mean one of two things and both are fatal the writer knew fields this reader
183
+ does not, or the writer miscounted. Enforced for the description blob
184
+ (`readEffectDescription`), for the per-pass backend block (`readBackendBlock`), and
185
+ for the header, whose end must equal where the body region begins.
186
+
187
+ This rule carries weight that used to live elsewhere. The chunk container it replaces
188
+ spent roughly 600 lines asserting that its several projections of one effect still
189
+ agreed with each other, and those checks caught a malformed *tree* — our writer
190
+ emitting something structurally wrong — not only a malformed file. A record layout
191
+ makes most of that question unaskable, because containment replaces reference and
192
+ position replaces key. What remains is this: a writer bug either fails to parse,
193
+ which announces itself, or it parses and leaves the cursor somewhere other than the
194
+ declared end. Applying the rule to some sized records and not others is a gap that
195
+ stays invisible until a writer bug hides in one of the others.
196
+
197
+ **Rule 2: anything placed in the arena must be arena-independent.** An arena entry
198
+ cannot contain an arena offset. Offsets are assigned by the content sort, the sort
199
+ depends on every entry's bytes, so an entry that referred to the arena would have to
200
+ be interned before its own contents could be computed — a circular dependency with
201
+ no fixed point. This is invisible in Carbon's own code because no Carbon arena blob
202
+ refers to the arena: strings, bytecode and default constant values are all leaves.
203
+ Our per-pass backend block is the first non-leaf candidate, and it is why that block
204
+ carries inline length-prefixed strings instead of references. A test pins the
205
+ property directly the block's bytes must be identical whichever arena it is
206
+ interned into. Any future arena entry must satisfy the same rule.
207
+
208
+ ### One field the container cannot round-trip
209
+
210
+ For a **non-dynamic sampler, the name is not preserved.** The file stores one, but
211
+ Carbon's reader nulls it before any producer sees it, so a package built from
212
+ our reflection carries the empty string.
213
+
214
+ This is a property of the input, not a bug in the mapping: the name is unrecoverable
215
+ by the time we receive the data, rather than dropped on the way out. Carbon nulls it
216
+ precisely because a non-dynamic sampler is never looked up by name `FindSamplerByName`
217
+ only matters for the dynamic case. Recorded here because it will otherwise be
218
+ rediscovered as a bug: a diff against the source effect will always show it.
219
+
220
+ **Corollary: the container admits all six of Carbon's stage types.** `stages` is
221
+ capped at `SHADER_TYPE_COUNT` = 6, and the stage-type byte uses Carbon's
222
+ `InputStageType` numbering: vertex, pixel, compute, geometry, hull, domain. A
223
+ backend that can only express three of those rejects the rest in its own layer;
224
+ the container does not narrow on its behalf. The Carbon region is
225
+ backend-invariant, and restrictions belong to the backend.
226
+
227
+ `0xffffffff` is the null reference. It is legal at **exactly one wire
228
+ position**: a stage's default-constant-value offset when the accompanying size
229
+ is zero, which the optional-value reader consumes without dereferencing.
230
+ Everywhere else a `0xffffffff` offset fails the load.
231
+
232
+ Two deliberate departures from Carbon, both of which make byte-identical output
233
+ more likely rather than less:
234
+
235
+ - **`m_size` is initialised.** Carbon's constructor leaves it indeterminate and
236
+ gets away with it only because the one instance is a zero-initialised global.
237
+ - **Adding after an offset has been handed out is an error.** In Carbon,
238
+ `GetOffset` re-sorts a dirty table, which reassigns *every* offset including
239
+ offsets already baked into packed bodies. Carbon avoids the corruption by
240
+ interning all late strings before the packing pass. `CjsCarbonEffectWriter`
241
+ reproduces that discipline structurally: it runs the record walk twice, once
242
+ with `collectArena` to intern and once with `internArena` to emit. Because both
243
+ passes drive the same `writeEffectDescription`, they cannot drift apart.
244
+
245
+ ### Description blob, v15 field order
246
+
247
+ Derived independently from the writer's save order and the reader's load order,
248
+ then confirmed to agree field for field. Counts are `u8` unless marked.
249
+
250
+ ```
251
+ u8 techniqueCount
252
+ u32 name
253
+ u8 passCount cap 64
254
+ u8 stageCount cap 6 (SHADER_TYPE_COUNT)
255
+ u8 stageType
256
+ u32 shaderSize | u32 shaderDataOffset program payload, arena blob
257
+ u32 threadGroupSize[3]
258
+ u8 pipelineInputCount cap 64
259
+ u8 usage, registerIndex, usageIndex, usedMask, type, dimension
260
+ -- StageData --
261
+ u8 registerCount
262
+ u8 registerType | u32 registerIndex | u32 registerCount | u8 registerSpace
263
+ u8 staticSamplerCount
264
+ u32 registerIndex | u8 registerSpace | u8 x7 filters/address
265
+ f32 mipLODBias | u8 maxAnisotropy | u8 comparisonFunc
266
+ u8 borderColor enum, NOT four floats
267
+ f32 minLOD | f32 maxLOD
268
+ u32 constantCount u32, not u8
269
+ u32 name | u32 offset | u32 size | u8 type | u8 dimension
270
+ u32 elements | u8 isSRGB | u8 isAutoregister
271
+ u32 defaultValuesSize | u32 defaultValuesOffset 0xffffffff legal when size 0
272
+ u8 textureCount cap 64
273
+ u8 registerIndex | u32 name | u8 type | u32 count | u8 isSRGB | u8 isAutoregister
274
+ u8 samplerCount cap 64
275
+ u8 registerIndex | u32 name | u8 x7 | f32 mipLODBias | u8 maxAnisotropy
276
+ u8 comparisonFunc | f32 borderColor[4] | f32 minLOD | f32 maxLOD | u8 isDynamic
277
+ u8 uavCount cap 64
278
+ u8 registerIndex | u32 name | u8 type | u32 count | u8 isAutoregister
279
+ u8 annotationCount
280
+ u32 name | u8 type | (u32 stringOffset if type == STRING else 4 raw bytes)
281
+ u8 renderStateCount cap 64
282
+ u32 state | u32 value
283
+ u8 libraryCount
284
+ u32 payloadSize | u32 shaderSize | u32 shaderDataOffset
285
+ u32 exportCount u32, not u8
286
+ u8 type | u32 name
287
+ u32 hitGroupName
288
+ StageData globalInputs
289
+ StageData localInputs
290
+ u16 parameterCount cap 256
291
+ u32 name | annotation map as above
292
+ ```
293
+
294
+ Four places this is easy to get wrong:
295
+
296
+ - **At v15 the program payload comes first and the signature tables follow.**
297
+ `pipelineInputs` and `registers` sit *after* `shaderCode` and `threadGroupSize`.
298
+ Before v14 it was the other way round; v14 moved them, and Carbon marks its own
299
+ v14 branch as uncertain.
300
+ - **A UAV record is one byte shorter than a texture record** — it has no `isSRGB`.
301
+ Carbon's reader hardcodes `isSRGB = false` and the writer omits it. Sharing one
302
+ "resource" codec between the two silently corrupts every subsequent field.
303
+ - **`borderColor` is four floats on a sampler and one byte on a static sampler**
304
+ because the two records mirror different D3D binding models.
305
+ - **A non-string annotation value is four raw bytes.** Carbon writes it through the
306
+ `float` member of a `{float,int32_t}` union and reads it back through a different
307
+ union. The bytes round-trip; applying an int/float conversion does not. The codec
308
+ keeps `rawValue` as bytes for exactly this reason.
309
+
310
+ Carbon writes `textures`, `samplers`, `uavs` and render states in ascending key
311
+ order and sorts annotation keys by bytewise string comparison.
312
+ `compareAnnotationNames` implements that comparison over UTF-8 bytes, which is
313
+ *not* the same as JavaScript's UTF-16 code-unit order for names outside ASCII
314
+ `"Z"` sorts before `"a"`.
315
+
316
+ ### The optional trailing block
317
+
318
+ Our own containers add exactly one optional block per pass, after the render-state
319
+ table, referenced by a `{u32 size, u32 offset}` pair into the arena. A Carbon file
320
+ ends the pass at the render states, so the reader and writer gate it on
321
+ `{ backend: true }` and produce Carbon's bytes unchanged when it is closed.
322
+
323
+ The block carries the two sections that are not derivable from Carbon reflection —
324
+ WebGPU bind-group layouts and resource transforms in **one** unit, because they
325
+ are mutually required and because "the Carbon region is backend-invariant, with
326
+ exactly one optional trailing block" is the invariant worth keeping.
327
+
328
+ It lives in the arena so identical layouts dedupe across bodies the way program
329
+ source does; measured sharing is 30.5:1 at `(body, pass)` granularity, 22 distinct
330
+ blocks across 672 pairs. That forces one property: **the block contains no arena
331
+ offsets.** An offset is only known after the arena's content sort, which depends on
332
+ every blob's bytes including this one, so a block referencing the arena could not be
333
+ built before it was interned. Strings inside it are inline and length-prefixed.
334
+
335
+ ```
336
+ u8 blobVersion = 1
337
+ u8 bindGroupCount
338
+ u8 group | u8 bindingCount
339
+ u8 resourceKind | u8 registerSpace | u8 binding | u8 visibilityMask
340
+ u32 registerIndex | u32 structureStride (0xffffffff absent) | u8 arrayLayerCount (0 absent)
341
+ str type | str generatedSymbol | str transformId (empty = none)
342
+ u8 transformCount
343
+ u8 familyCode | str id | u8 inputCount
344
+ u8 registerSpace | u8 registerIndex | str parameter
345
+ ```
346
+
347
+ `identity` and `group` on each binding, and a transform's `kind`,
348
+ `stage`, `representation`, `missingLayer`, `viewDimension`, `layerCount`,
349
+ `output.identity`, `output.scopeIdentity`, `output.name`, `layoutKey` and every
350
+ input's `layer` are restored on read, not stored. The family byte is what keeps them
351
+ derivable without pinning the format to one recognizer. `id` and each input's
352
+ `parameter` stay on the wire deliberately `id` because a caller may supply it,
353
+ `parameter` because it keeps layer identity cross-checkable rather than asserted by
354
+ position.
355
+
356
+ Backend-block version 1 stores visibility but not the original
357
+ `scopeIdentity`. The reader reconstructs `${identity}@${visibility[0]}`. A
358
+ multi-stage shared binding therefore rereads as stage-qualified rather than
359
+ recovering its original bare scope. Callers must not infer that original
360
+ sharing decision from the wire view.
361
+
362
+ An unknown `blobVersion` reports the pass as having no backend data rather than
363
+ misparsing it; the enclosing size makes it skippable.
364
+
365
+ ### Count caps
366
+
367
+ `CARBON_EFFECT_COUNT_CAPS` mirrors the runtime's inclusive limits. Carbon's
368
+ compiler enforces none of them while its runtime rejects anything above them,
369
+ so an over-large effect compiles and then fails to load; our writer checks on
370
+ the way out. The caps Carbon does *not* have — techniques, registers, static
371
+ samplers, constants, libraries, exports, annotation counts — are deliberately
372
+ not invented here.
373
+
374
+ ### The alias path
375
+
376
+ Carbon compares packed bodies pairwise and points a duplicate's row at the
377
+ surviving twin. The row is **kept**, so the offset table stays dense while the
378
+ file stores each distinct body once. Across the shipped corpus 22% of files
379
+ alias, at roughly 2.1 rows per distinct body.
380
+
381
+ `enumerateUniqueEffectBodies` recovers that grouping without decoding a single
382
+ body. It inspects source records and bytes only, and returns
383
+ first-occurrence-ordered groups, each holding one canonical
384
+ `permutationIndex`/`sourceRecord` plus every byte-identical alias. Exact range
385
+ aliases are the fast path; distinct ranges are fingerprinted and then compared
386
+ byte for byte. It caps the Cartesian body table at 65,536 records
387
+ (`EFFECT_BODY_COUNT_MAX`) and rejects partial overlaps, because a partial
388
+ overlap means two bodies claim the same bytes. It decodes nothing: it reads
389
+ `m_offsets` and slices `m_data` directly, so a backend packager can inventory an
390
+ effect without disturbing anything a later read depends on. It is internal
391
+ (`src/format/effect/`), not a published export; the backend body set is its one
392
+ consumer.
393
+
394
+ ## Offset-table density
395
+
396
+ Carbon indexes the offset table **positionally** and never reads each row's
397
+ stored `index` field. A sparse or misordered table therefore does not fail — it
398
+ silently returns the wrong shader body.
399
+
400
+ Density is incidental in Carbon: it falls out of `g_compiledEffects` being a
401
+ `std::map` densely keyed by the work-queue builder, and is promised nowhere.
402
+
403
+ Measured twice at build 3444265. A header-only sweep of every `.sm_hi`, `.sm_lo`
404
+ and `.sm_depth` under `effect.dx11` and `effect.dx12` — 3222 files, 52,332 rows —
405
+ and the full round-trip run below across all three backends — 4833 files, 78,498
406
+ rows, 40,645 distinct bodies. **Every file is dense and positionally indexed**, and
407
+ every row's byte range lies inside the file and clear of the header. In every
408
+ single file the body region tiles the post-header space exactly, with no leading or
409
+ trailing slack.
410
+
411
+ Given that, the checks are implemented as follows:
412
+
413
+ - `CjsCarbonEffectReader` **always** collects `dense` and `indicesMatchPosition` as
414
+ diagnostics, and **always** fails closed on an out-of-range row.
415
+ - Density and positional indexing **fail closed on read by default**.
416
+ `{ permissive: true }` skips the check and leaves the diagnostics in place, for
417
+ forensic inspection of a file already known to be malformed. It is not a load
418
+ option.
419
+ - `writeCarbonEffectFile` **always** fails closed: it refuses to emit bodies that
420
+ are not dense from index 0. Where we own the bytes there is no reason to be
421
+ lenient.
422
+
423
+ `--ignore-permutations` does make CCP's compiler emit only key 0 while declaring
424
+ every axis, so a sparse file is producible. That argues for the escape hatch, not
425
+ for permissive defaults: Carbon does not reject such a file, it returns the wrong
426
+ permutation's shader silently, which is the failure class this port exists to
427
+ close.
428
+
429
+ ## Backend selection and envelope removal
430
+
431
+ **Historical compatibility.** Flat-package builds once prepended
432
+ `magic | containerVersion | payloadKind` before Carbon's byte-compatible
433
+ layout. Current packaging emits no such prefix and the former helper is no
434
+ longer part of the package.
435
+
436
+ **Current WebGPU wire.** Carbon WebGPU bytes have no envelope, magic, `payloadKind`,
437
+ or independent container version. They are bare Carbon v15 records resolved
438
+ from `effect.webgpu/`, with one optional per-pass backend block. `.carbonwebgl`
439
+ remains a separate Carbon WebGL chunk format rather than this Carbon-record wire.
440
+
441
+ `CarbonWebgpuContainer` reads the optional blocks, and the shared record reader can
442
+ auto-detect them from a description's declared size. There is no adapter
443
+ boundary left between them and the runtime: `Tr2EffectRes.DoLoad` retains a
444
+ `CjsCarbonEffectReader` over the same bytes and `Tr2Shader.fromCarbonBinary`
445
+ builds the device-free graph from one description record.
446
+
447
+ Versioning remains local to what it versions: Carbon's version dword governs
448
+ the Carbon region, while `blobVersion` governs the optional backend block. An
449
+ unknown block version is skipped rather than misparsed. The package does not
450
+ claim a version in CCP's namespace.
451
+
452
+ Loose program bytes without a resource path can be identified from their
453
+ payload: DXBC opens with `"DXBC"`, AIR is bitcode (`BC 0xC0DE`), and WGSL and
454
+ GLSL have distinct text syntax. The stage record itself carries no language
455
+ tag; program interpretation remains a backend/path responsibility.
456
+
457
+ ## Verification
458
+
459
+ `node --test` in this package. Two gates.
460
+
461
+ **Always green.** `test/format/byte-primitives.test.js` and
462
+ `test/format/carbon-effect.test.js` build a synthetic four-permutation v15
463
+ container exercising every record type — static samplers, UAVs, annotations of
464
+ every value type, render states, a raytracing library with both stage-data blocks
465
+ — and assert a byte-exact write → read → write round trip, the arena sort order,
466
+ the caps, the structural checks and, until the switchover, the legacy envelope's
467
+ disjointness.
468
+
469
+ **Env-gated real-file proof.** `test/format/carbon-effect-corpus.test.js`, enabled
470
+ with `CARBON_EFFECT_CORPUS_DIR`. Game bytes are never committed. Supply a
471
+ separately acquired corpus at pinned build 3444265.
472
+
473
+ `CARBON_EFFECT_CORPUS_DIR` must point at a **materialised tree of source
474
+ effects** — real `.sm_hi` / `.sm_lo` / `.sm_depth` filenames under
475
+ `effect.dx11`, `effect.dx12`, or `effect.gles2`, which is what the walker
476
+ filters on. Two nearby directories look like corpora and are not:
477
+
478
+ - a content-addressed resource store (hash-named files, no extensions) matches
479
+ nothing and fails as `no compiled effect files found` — the walker is
480
+ extension-driven, so an unextracted cache silently yields zero files;
481
+ - a directory of **our own translated output** keeps the `.sm_*` names but
482
+ holds `Carbon WebGL`/`Carbon WebGPU` containers, and fails with
483
+ `Unsupported Carbon effect version 1196901699` — that number is the ASCII
484
+ `Carbon WebGL` magic read as a version dword.
485
+
486
+ Both failures are loud, which is the intended behaviour; neither is a defect.
487
+ The test re-emits each file three ways:
488
+
489
+ 1. every description blob through the file's own arena — proves the field order;
490
+ 2. the whole container from raw bodies and the source arena — proves the header
491
+ order, the base arithmetic and the alias path;
492
+ 3. the whole container with the arena rebuilt from the references found — proves
493
+ the sorted-offset policy.
494
+
495
+ Only the third can legitimately differ, because an arena may retain blobs the file
496
+ no longer references. When it does differ the divergence is reported exactly and
497
+ asserted to be unreferenced-blob retention; it is never downgraded to a weaker
498
+ comparison such as "same strings, any order", which would look green and prove
499
+ nothing.
500
+
501
+ Measured result over the complete corpus — 4833 files (537 shaders × 3 variants ×
502
+ 3 backends), 78,498 offset-table rows, 40,645 distinct description bodies:
503
+ **all three modes byte-exact, with zero arena-rebuild divergences, zero sparse
504
+ tables and zero misordered tables.** Not one shipped file retains an unreferenced
505
+ arena blob, so the sorted-offset policy reproduces CCP's arena exactly.
506
+ Supply a separately acquired corpus and set `CARBON_EFFECT_CORPUS_DIR`; no
507
+ corpus data ships with the package.
508
+
509
+ **The same proof, one level up.** `test/resource/effect-res-corpus.test.js`
510
+ closes the loop through the resource classes rather than the records: bytes →
511
+ `Tr2EffectRes.DoLoad` → `Tr2Shader.fromCarbonBinary` → the device-free graph →
512
+ `toCarbonBinary()` → records → bytes. Running the record-level round trip on the
513
+ same files with no classes involved is the negative control; when the two
514
+ disagree, the difference is the class layer's, and that is how the three
515
+ fidelity losses it once had were found and closed — non-dynamic sampler names
516
+ (1,631 files), authored pass-stage order (21 files), and the offset word of a
517
+ zero-size blob (150 files), which is not always the `0xffffffff` sentinel.
518
+ Carbon's *runtime* discards the first two; the file does not, so re-emitting the
519
+ file it came from means keeping them.
520
+
521
+ That result is the container port's central evidence. The same reader and the same
522
+ writer reproduce, byte for byte, files whose program payloads are DXBC in two
523
+ dialects and AIR — with no language field anywhere in the format. The metadata
524
+ region is backend-invariant as a measured fact rather than an argument from the
525
+ writer. Backend selection therefore belongs at the resource-path boundary, not
526
+ in an envelope or per-stage record.
527
+
528
+ `effect.gles2` is deliberately not a validation target for **this package**: those
529
+ shaders are v8, and nothing in the container port reads or writes them.
530
+
531
+ **Do not read that as "obsolete".** `effect.gles2` is the shader tree ccpwgl
532
+ actually renders with today — it is the only one that currently works end to end.
533
+
534
+ The two statements coexist because **v15-only constrains what we write and
535
+ validate against, not what a reader may accept.** Version-branching is the
536
+ format's own mechanism. A reader that wants all supported generations branches
537
+ on the version dword — v2..8 legacy gles2, v15 everything current — which is one
538
+ reader, not a bespoke path per format.
539
+
540
+ **Our containers are v15, not a version of our own.** A "v16" was considered for
541
+ the variant carrying the per-pass backend block and **rejected**: CCP owns that
542
+ number space, so claiming 16 would collide with any real v16 they ship, in the one
543
+ field whose entire job is telling a reader how to parse. It also failed the rule
544
+ the rest of this format is held to — invent something only because it *has to*
545
+ exist, never because we think it should.
546
+
547
+ The container needs no new version. Each description blob carries a declared
548
+ size in the offset table, and [Rule 1](#two-rules-for-anything-added-later)
549
+ already requires it to parse to exactly that end. A reader parses a blob without
550
+ blocks and re-parses with them if the cursor misses the declared end, so the
551
+ presence of the block is **self-describing** with no new field, no container
552
+ version and no out-of-band flag. `blobVersion` inside the block versions the
553
+ extension itself.