├── LICENSES ├── CC-BY-4.0.txt └── Apache-2.0.txt ├── .gitignore ├── .gitattributes ├── assembly ├── tsconfig.json ├── generator.ts └── decoder.ts ├── .github └── workflows │ ├── reuse.yml │ └── ci.yml ├── .reuse └── dep5 ├── CODE_OF_CONDUCT.md ├── package.json ├── README.md ├── test └── test.ts └── LICENSE.txt /LICENSES/CC-BY-4.0.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/KhronosGroup/BC6H-Decoder-WASM/HEAD/LICENSES/CC-BY-4.0.txt -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Copyright 2020 The Khronos Group Inc. 2 | # 3 | # SPDX-License-Identifier: Apache-2.0 4 | 5 | node_modules 6 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Copyright 2020 The Khronos Group Inc. 2 | # 3 | # SPDX-License-Identifier: Apache-2.0 4 | 5 | *.wat text eol=lf 6 | -------------------------------------------------------------------------------- /assembly/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "assemblyscript/std/assembly.json", 3 | "include": [ 4 | "./**/*.ts" 5 | ] 6 | } -------------------------------------------------------------------------------- /.github/workflows/reuse.yml: -------------------------------------------------------------------------------- 1 | # Copyright 2020 The Khronos Group Inc. 2 | # 3 | # SPDX-License-Identifier: Apache-2.0 4 | 5 | name: REUSE Compliance 6 | 7 | on: [push, pull_request] 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v2 14 | - name: REUSE Compliance Check 15 | uses: fsfe/reuse-action@v1 16 | -------------------------------------------------------------------------------- /.reuse/dep5: -------------------------------------------------------------------------------- 1 | Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ 2 | Upstream-Name: BC6H-Decoder-WASM 3 | Source: https://github.com/KhronosGroup/BC6H-Decoder-WASM 4 | 5 | Files: build/* 6 | Copyright: Copyright 2020 The Khronos Group Inc. 7 | License: Apache-2.0 8 | 9 | Files: *.json 10 | Copyright: Copyright 2020 The Khronos Group Inc. 11 | License: Apache-2.0 12 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | # Code of Conduct 6 | 7 | A reminder that this issue tracker is managed by the Khronos Group. 8 | Interactions here should follow the 9 | https://www.khronos.org/developers/code-of-conduct[Khronos Code of Conduct], 10 | which prohibits aggressive or derogatory language. Please keep the 11 | discussion friendly and civil. 12 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # Copyright 2020 The Khronos Group Inc. 2 | # 3 | # SPDX-License-Identifier: Apache-2.0 4 | 5 | name: CI 6 | 7 | on: [push, pull_request] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | strategy: 15 | matrix: 16 | node-version: [12.x, 14.x, 15.x] 17 | 18 | steps: 19 | - uses: actions/checkout@v2 20 | - name: Use Node.js ${{ matrix.node-version }} 21 | uses: actions/setup-node@v1 22 | with: 23 | node-version: ${{ matrix.node-version }} 24 | - run: npm install 25 | - run: npm run asbuild 26 | - run: npm test 27 | env: 28 | CI: true 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bc6h-decoder-wasm", 3 | "version": "0.1.0", 4 | "description": "WebAssembly BC6H decoder.", 5 | "keywords": [ 6 | "gpu", 7 | "webgl", 8 | "wasm", 9 | "hdr", 10 | "bc6h", 11 | "texture", 12 | "ibl", 13 | "ktx", 14 | "dds" 15 | ], 16 | "author": "The Khronos Group, Inc.", 17 | "license": "Apache-2.0", 18 | "devDependencies": { 19 | "@types/node": "^14.14.7", 20 | "@typescript-eslint/eslint-plugin": "^4.7.0", 21 | "@typescript-eslint/parser": "^4.7.0", 22 | "assemblyscript": "0.17.2", 23 | "eslint": "^7.13.0", 24 | "eslint-plugin-unicorn": "^23.0.0", 25 | "tap-spec": "^5.0.0", 26 | "tape": "^5.0.1", 27 | "ts-node": "^9.0.0", 28 | "typescript": "^4.0.5" 29 | }, 30 | "scripts": { 31 | "asbuild:generator": "asc assembly/generator.ts -b build/generator.wasm -t build/generator.wat --optimize --converge --runtime none --noAssert --importMemory --memoryBase 63488", 32 | "asbuild:bc6hu-rgba16f": "asc assembly/decoder.ts -u MODE=0 -b build/bc6hu-rgba16f.wasm -t build/bc6hu-rgba16f.wat --optimize --converge --runtime none --noAssert --importMemory", 33 | "asbuild:bc6hu-rgb9e5": "asc assembly/decoder.ts -u MODE=1 -b build/bc6hu-rgb9e5.wasm -t build/bc6hu-rgb9e5.wat --optimize --converge --runtime none --noAssert --importMemory", 34 | "asbuild:bc6hu-r11fg11fb10f_round": "asc assembly/decoder.ts -u MODE=2 -b build/bc6hu-r11fg11fb10f_round.wasm -t build/bc6hu-r11fg11fb10f_round.wat --optimize --converge --runtime none --noAssert --importMemory", 35 | "asbuild:bc6hu-r11fg11fb10f_trunc": "asc assembly/decoder.ts -u MODE=3 -b build/bc6hu-r11fg11fb10f_trunc.wasm -t build/bc6hu-r11fg11fb10f_trunc.wat --optimize --converge --runtime none --noAssert --importMemory", 36 | "asbuild:bc6hu-r11fg11fb10f_round_noclamp": "asc assembly/decoder.ts -u MODE=4 -b build/bc6hu-r11fg11fb10f_round_noclamp.wasm -t build/bc6hu-r11fg11fb10f_round_noclamp.wat --optimize --converge --runtime none --noAssert --importMemory", 37 | "asbuild:bc6hu-rgb32f": "asc assembly/decoder.ts -u MODE=5 -b build/bc6hu-rgb32f.wasm -t build/bc6hu-rgb32f.wat --optimize --converge --runtime none --noAssert --importMemory", 38 | "asbuild:decoders": "npm run asbuild:bc6hu-rgba16f && npm run asbuild:bc6hu-rgb9e5 && npm run asbuild:bc6hu-r11fg11fb10f_round && npm run asbuild:bc6hu-r11fg11fb10f_trunc && npm run asbuild:bc6hu-r11fg11fb10f_round_noclamp && npm run asbuild:bc6hu-rgb32f", 39 | "asbuild": "npm run asbuild:generator && npm run asbuild:decoders", 40 | "test": "ts-node node_modules/tape/bin/tape test/test.ts | tap-spec" 41 | }, 42 | "eslintConfig": { 43 | "parser": "@typescript-eslint/parser", 44 | "plugins": [ 45 | "@typescript-eslint", 46 | "unicorn" 47 | ], 48 | "extends": [ 49 | "eslint:recommended", 50 | "plugin:@typescript-eslint/recommended" 51 | ], 52 | "rules": { 53 | "semi": "off", 54 | "@typescript-eslint/ban-ts-comment": "off", 55 | "@typescript-eslint/semi": [ 56 | "error", 57 | "always" 58 | ], 59 | "unicorn/number-literal-case": "error" 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # WebAssembly BC6H Decoder 5 | 6 | ![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg) 7 | [![CI](https://github.com/KhronosGroup/BC6H-Decoder-WASM/workflows/CI/badge.svg?branch=main&event=push)](https://github.com/KhronosGroup/BC6H-Decoder-WASM/actions?query=workflow%3ACI) 8 | 9 | A collection of optimized WebAssembly decoders for [BC6H unsigned](https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#bptc_bc6h) GPU compressed HDR texture format. 10 | 11 | These decoders are intended to be used in environments where hardware decompression of BC6H data is not available, e.g. in CLI applications or when the [`EXT_texture_compression_bptc`](https://www.khronos.org/registry/webgl/extensions/EXT_texture_compression_bptc/) WebGL extension is unsupported. 12 | 13 | These decoders accept only low-level compressed payloads. Containers such as `.dds` or `.ktx2` ([KTX](https://github.khronos.org/KTX-Specification/)) should first be parsed by other means, then decompressed with this library. 14 | 15 | ## Build 16 | 17 | Pre-built WebAssembly binary modules (`*.wasm`) are maintained in the `build/` directory of this repository. To rebuild them from the source files, follow these steps. 18 | 19 | 1. Ensure that [Node.js](https://nodejs.org/) runtime is installed. 20 | 21 | 2. After cloning this repository, run 22 | ``` 23 | $ npm install 24 | ``` 25 | 26 | 3. After all the dependencies are fetched, run 27 | ``` 28 | $ npm run asbuild 29 | ``` 30 | 31 | 4. Built decoders will be available in the `build/` directory. 32 | 33 | ## Overview 34 | 35 | Decoders accept only raw BC6H blocks. Zstandard or zlib/deflate compression (if present) must be decoded in advance. 36 | 37 | Each 16-byte BC6H block is decoded to 64-192 bytes of uncompressed floating-point data, depending on a chosen target format. An application should choose the target format based on the runtime requirements and capabilities. Available targets include: 38 | 39 | ### Half-Precision Float (RGBA16F) 40 | 41 | * 8 bytes per pixel uncompressed floating-point RGBA format. 42 | * Alpha channel is set to `1.0` for all pixels. 43 | * Decoded data could be used in WebGL 2.0 or in WebGL 1.0 with [`OES_texture_half_float`](https://www.khronos.org/registry/webgl/extensions/OES_texture_half_float/) extension. 44 | * Decoding to this format fully preserves original BC6H precision and range (`[0.0 … 65504.0]`). 45 | * Due to high memory usage, this target format should be used only when other decode targets are not available (i.e. on WebGL 1.0) or when lossless decoding is absolutely required. 46 | 47 | ### Packed Float (R11FG11FB10F) 48 | 49 | * 4 bytes per pixel packed floating-point RGB format. 50 | * Decoded data could be used only in WebGL 2.0 contexts. 51 | * This format has slightly smaller ranges and reduced precision compared to the original BC6H data. 52 | * Due to the asymmetry of color channels, grayscale pixels may yield visible discoloration (blue/yellow) starting around `0.5`. 53 | * Three decoder modules are available with slightly different tradeoffs: 54 | - `bc6hu-r11fg11fb10f_trunc.wasm` 55 | - Fast processing at a cost of less accurate conversion. 56 | - Red and green values are clamped to `[0.0 … 65024.0]`; blue values are clamped to `[0.0 … 64512.0]`. 57 | - `bc6hu-r11fg11fb10f_round.wasm` 58 | - Accurate conversion at a cost of slower processing. 59 | - Red and green values are clamped to `[0.0 … 65024.0]`; blue values are clamped to `[0.0 … 64512.0]`. 60 | - `bc6hu-r11fg11fb10f_round_noclamp.wasm` 61 | - Faster conversion than the previous option at a cost of skipped clamping step. 62 | - Red and green values greater than `65024.0` or blue values greater than `64512.0` are mapped to positive infinity. 63 | - This is the best option when the compressed data stays within the aforementioned ranges or when the renderer is resilient to infinite values. 64 | 65 | ### Packed Shared Exponent Float (RGB9E5) 66 | 67 | * 4 bytes per pixel packed floating-point RGB format. 68 | * Decoded data could be used only in WebGL 2.0 contexts. 69 | * Original BC6H values are clamped to `[0.0 … 65408.0]`. 70 | * Due to shared exponent encoding, effective per-pixel precision depends on a channel having the maximum value. 71 | * Although this target format takes more time to decode to, it may provide better quality than R11FG11FB10F. 72 | 73 | ### Single-Precision Float (RGB32F) 74 | 75 | * 12 bytes per pixel uncompressed floating-point RGB format. 76 | * Decoding to this format fully preserves original BC6H precision and range (`[0.0 … 65504.0]`). 77 | * Although it's possible to use the decoded data in any WebGL context with [`OES_texture_float`](https://www.khronos.org/registry/webgl/extensions/OES_texture_float/) extension, this target format should only be used for CPU-side processing (e.g. via mapping the decompressed data with a [`Float32Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array)) as it provides no benefits over RGBA16F on the GPU side. 78 | 79 | ## Usage 80 | 81 | 1. For a single texture, the amount of the required memory should be calculated as: 82 | ```js 83 | const xBlocks = (width + 3) >> 2; 84 | const yBlocks = (height + 3) >> 2; 85 | const compressedByteLength = xBlocks * yBlocks * 16; 86 | 87 | // RGBA16F needs 8, RGB32F needs 12. 88 | const pixelByteLength = 4; 89 | 90 | // Uncompressed texture padded to multiple-of-4 height 91 | const uncompressedByteLength = width * yBlocks * 4 * pixelByteLength; 92 | const totalByteLength = compressedByteLength + uncompressedByteLength; 93 | ``` 94 | 95 | 2. Create a [`WebAssembly.Memory`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory) object large enough to contain both the compressed and the uncompressed data. Its size is given in pages, each page is 65536 bytes. The zeroth page is reserved for the decoder's internal use, so the total amount of pages should be computed as: 96 | ```js 97 | const texMemoryPages = (totalByteLength + 65535) >> 16; 98 | const memory = new WebAssembly.Memory({ initial: texMemoryPages + 1 }); 99 | ``` 100 | 101 | 3. Create a view into the memory region that will be used for transferring compressed texture data. This step must be repeated after calling [`memory.grow`]( 102 | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory/grow), such as when allocating space for another, larger, texture. 103 | 104 | ```js 105 | let compressedTextureView = new Uint8Array(memory.buffer, 65536, compressedByteLength); 106 | ``` 107 | 108 | 4. The memory could be populated with the BC6H data even before the decoder is ready. 109 | ```js 110 | compressedTextureView.set(compressedData /* Uint8Array */); 111 | ``` 112 | 113 | 5. Create a view into the memory region that will be used for transferring the decoded texture data. This step must be repeated after calling [`memory.grow`]( 114 | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory/grow), such as when allocating space for another, larger, texture. 115 | 116 | ```js 117 | const textureByteLength = width * height * pixelByteLength; 118 | let decodedTextureView = new Uint8Array(memory.buffer, 65536 + compressedByteLength, textureByteLength); 119 | ``` 120 | 121 | 6. Fetch and instantiate the decoder with the created memory. Note, that the example code uses [Fetch](https://fetch.spec.whatwg.org/) and [WebAssembly Web](https://webassembly.github.io/spec/web-api/index.html) APIs. Other JavaScript environments (such as Node.js) would need slightly different steps. 122 | ```js 123 | const decoder = ( 124 | await WebAssembly.instantiateStreaming( 125 | fetch('bc6hu-rgb9e5.wasm'), 126 | { env: { memory } } 127 | ) 128 | ).instance.exports; 129 | ``` 130 | 131 | 7. For each new texture, call the exported `decode` function passing the texture dimensions. If they are negative or exceed the available memory, the function returns `1`. Otherwise, it performs the decoding and returns `0`. The decoded texture data will be available through the `decodedTextureView` memory view. 132 | ```js 133 | compressedTextureView.set(compressedData); 134 | if (decoder.decode(width, height) === 0) { 135 | // Use decodedTextureView data 136 | } else { 137 | // Wrong dimensions 138 | } 139 | ``` 140 | 141 | 7. In a case when a new texture does not fit into the existing memory, the latter could be expanded by calling [`memory.grow`]( 142 | https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory/grow). Note that the memory views would need to be recreated afterwards, by repeating step (3) and repeating step (5) in this case. 143 | -------------------------------------------------------------------------------- /test/test.ts: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Khronos Group Inc. 2 | // 3 | // SPDX-License-Identifier: Apache-2.0 4 | 5 | import * as fs from 'fs'; 6 | import * as test from 'tape'; 7 | import * as crypto from 'crypto'; 8 | 9 | const WIDTH = 8192; 10 | const HEIGHT = 8192; 11 | 12 | const modeChecksums = { 13 | "0": { 14 | bc6h: "63649c22a56e93a93fa2bbdb299e4410b7976054821bbd0e210a2ec2c59a329c", 15 | rgba16f: "a1d51f083201861f444e9b8507a1951ee9d9ba4c47d74692b38ee0c02f96a105" 16 | }, 17 | "1": { 18 | bc6h: "29ab9bb7069402defd5a359460cee351405a8431bb4a692d0e7a7745b43f648c", 19 | rgba16f: "b58bc9cb50894e18f7b4704056d9fedb8e54acba209524142b648a5dd3486042" 20 | }, 21 | "2": { 22 | bc6h: "b2cc06730141f23ff971ca958b16e46f9491a88a68a22278cb4b07e05065c1ee", 23 | rgba16f: "f81ac469bd5ab12354bbc22794468db7b4af394757080ba9a62e3760b48a06ce" 24 | }, 25 | "6": { 26 | bc6h: "6ea51e7f8b6366b9b2cba9ecf3f00723871db4633051e45bc7be2cbafe7f58bb", 27 | rgba16f: "c63e908242825bae8cbe4af54b0a3d72ccd3317c4d09557b928cf99363d39cdd" 28 | }, 29 | "10": { 30 | bc6h: "6abb7f59941cf7c291d8dbd4f673e57b8bd34e56fd3de949ea3af8eea5fda78d", 31 | rgba16f: "a25746a3a7af4387466afced1374f7d5e4602f4568a0de251f2159a9ce5846e9" 32 | }, 33 | "14": { 34 | bc6h: "a52128e835c314207b01e3f36776997b8285119bd08a4e1ff6a7b5ed5b4dec6c", 35 | rgba16f: "288af432a33380d446d0a07696ef74ddccd940b817323eef8cc6df507a94578e" 36 | }, 37 | "18": { 38 | bc6h: "854abcaadefb93a012cc448773ff2e5cb024ddcf6ee8024b6b8b6083ba4aed31", 39 | rgba16f: "7737f401427cc3f832023f8134776ee14080f0072945d9da5040ddd076fb4f30" 40 | }, 41 | "22": { 42 | bc6h: "9e246c47b0d727ff68c8d64befbcb552f5b3c2ec75a95cdf40909802a839c36a", 43 | rgba16f: "b48ef3a8fe1115fbfd963da20f5e169434faca323952beee75235cfabead6871" 44 | }, 45 | "26": { 46 | bc6h: "5ea960b0dcf4a30b11cb4cc077c31901a6794f00f9224814d638235f98bbdd86", 47 | rgba16f: "06611b8c45dcfa303a04c909ad8ee4094dfaf6916ed72b10ac2362290907e643" 48 | }, 49 | "30": { 50 | bc6h: "b7380d85abed2c190b085a2753b68fbcbf934cceba9931f8e98ea392c077e4cf", 51 | rgba16f: "23bb81a1c2f6fb4383940d0ee02bcd63ad93abb45b5a8c9f1691f8ad7f71be60" 52 | }, 53 | "3": { 54 | bc6h: "6f0eb1c28c0a0e27153a57e51072aa8c3ccb25c3d074dbc9fa45d10229455ecd", 55 | rgba16f: "97cb8255d41134e84941d9f7aadf32a62cbc7ad8514c67a88935a0eda671b3bb" 56 | }, 57 | "7": { 58 | bc6h: "b59924b592ab4316270535723bba79402752403a3d32585c5d7be0c6da82ff28", 59 | rgba16f: "08c718e02ecba0fca19fb26c64700c394ecb28af5320414baba068af614949a8" 60 | }, 61 | "11": { 62 | bc6h: "b025d88e5ea001a80c0b064e34db54870458bba6d0d5336414a1365dd7fe227d", 63 | rgba16f: "8768db1e1fe91eac80cecc468836f85c76aefee84916f08fe8dbe8abf7f4ef0a" 64 | }, 65 | "15": { 66 | bc6h: "9d5ac6f01bde258e50bc148f9f9b78e67048b69d01941e19b28c09e23ee3869c", 67 | rgba16f: "0fe336c72371ccb581f63eeb6bda3d8cf47f81e24bd83b4453872290742bcde6" 68 | }, 69 | "19": { 70 | bc6h: "c5cbb95e965585bea4f8d728f0be403f3ab3ac5bcc1ab5f9c22e940e7714a70c", 71 | rgba16f: "8c7671118d7035c6f82e6931252131eeea6eab91420a0dbae085a07583c1e9dc" 72 | }, 73 | "23": { 74 | bc6h: "8a4a88a4c72bf2b36a1b189edb1967655be370e14c4769d646605b61e73c3b89", 75 | rgba16f: "8c7671118d7035c6f82e6931252131eeea6eab91420a0dbae085a07583c1e9dc" 76 | }, 77 | "27": { 78 | bc6h: "486e4077750cd16a302b91766d41d736905e14c62881bf65548e347f6ba845a8", 79 | rgba16f: "8c7671118d7035c6f82e6931252131eeea6eab91420a0dbae085a07583c1e9dc" 80 | }, 81 | "31": { 82 | bc6h: "3cef1e895be8b66113efb4118ce1883747a72dc171782496480f281843d8bdd5", 83 | rgba16f: "8c7671118d7035c6f82e6931252131eeea6eab91420a0dbae085a07583c1e9dc" 84 | } 85 | }; 86 | 87 | const mixedChecksums = { 88 | full: { 89 | bc6h: "18009f7b34482248ef27b4d5174bfdd954a5ba870533256ffb833cd1eaa5634a", 90 | rgba16f: "622ab1e1310dc41b5bee667a0702339221836008309d6f545c74a0a91e1b5c0b", 91 | rgb9e5: "8a20f895bbb62b8a7f77395de12229dceaf1ee7925e540502c0eb9347a3379e3", 92 | r11fg11fb10f_round: "1dc0848c6402bbd3d02d4320c1ae2e28933b5fde86c5368ac892b6aa16603d26", 93 | r11fg11fb10f_trunc: "7bfe0f27fac13e4da4f743818f6bdb788e9270f5055805432aee6ee510c7b846", 94 | r11fg11fb10f_round_noclamp: "a8c5064bcd734997bc2fd939bc80a23ef471d4232134d888c0c9e0b76fe9cc6a", 95 | rgb32f: "1f2d03bdb24c9852d8b0dffabb6c96da97c7a8113ec895d2419d015dc6d0747f" 96 | }, 97 | cropped: { 98 | bc6h: "18009f7b34482248ef27b4d5174bfdd954a5ba870533256ffb833cd1eaa5634a", 99 | rgba16f: "ab643e8bee38b859575f23a229631bf7f05f297831d50eaa8cd2c79edbd5b611", 100 | rgb9e5: "1f71648a5af72ef3a74014e41c0dd413612047cc7c1f264d684163cf81536c12", 101 | r11fg11fb10f_round: "86c58b5e24143e44d27cdbf7ff4da355c7e51451eaadc7b9dbd7eff70c3aae58", 102 | r11fg11fb10f_trunc: "f33b6cff8fe1591d343a00696d4fbc4a1aa028875d1db95cc42071df5b9ac91b", 103 | r11fg11fb10f_round_noclamp: "2b9776757999429027bdb28b6ca8797680d4415108464fa3cf1d5cecc2d7f7f6", 104 | rgb32f: "58a1d1a7fb0fa22e661fa4b9f0b5ca827dafdc11f27684f77738b1793444170f" 105 | } 106 | }; 107 | 108 | let numBlocks: number; 109 | let compressedView: Uint8Array; 110 | let uncompressedView: Uint8Array; 111 | 112 | let generator: IGenerator; 113 | 114 | const kUserDataOffset = 65536; 115 | 116 | async function setup(wasmPath: string, pixelSize: number, width = WIDTH, height = HEIGHT): Promise { 117 | const yBlocks = ((height + 3) >> 2); 118 | numBlocks = ((width + 3) >> 2) * yBlocks; 119 | 120 | const compressedByteLength = numBlocks * 16; 121 | const uncompressedByteLength = width * height * pixelSize; 122 | const totalByteLength = compressedByteLength + width * (4 * yBlocks) * pixelSize; 123 | const memory = new WebAssembly.Memory({ initial: (kUserDataOffset + totalByteLength + 65535) >> 16 }); 124 | 125 | compressedView = new Uint8Array(memory.buffer, kUserDataOffset, compressedByteLength); 126 | uncompressedView = new Uint8Array(memory.buffer, kUserDataOffset + compressedByteLength, uncompressedByteLength); 127 | 128 | // Generator 129 | const generatorModule = new WebAssembly.Module(fs.readFileSync('build/generator.wasm')); 130 | generator = new WebAssembly.Instance(generatorModule, { env: { memory: memory } }).exports as unknown as IGenerator; 131 | 132 | // Decoder 133 | const module = new WebAssembly.Module(fs.readFileSync(wasmPath)); 134 | return new WebAssembly.Instance(module, { env: { memory: memory } }).exports as unknown as IDecoder; 135 | } 136 | 137 | test('Individual Modes, RGBA16F', async (t) => { 138 | const decoder = await setup('build/bc6hu-rgba16f.wasm', 8); 139 | 140 | for (let m = 0; m < 18; m++) { 141 | const mode = generator.generate(m, numBlocks); 142 | const hashes = modeChecksums[mode.toString()]; 143 | 144 | const genHash = crypto.createHash('sha256').update(compressedView).digest('hex'); 145 | t.equals(genHash, hashes.bc6h, `mode ${mode.toString().padStart(2, '0')}: generator`); 146 | 147 | decoder.decode(WIDTH, HEIGHT); 148 | const decHash = crypto.createHash('sha256').update(uncompressedView).digest('hex'); 149 | t.equals(decHash, hashes.rgba16f, `mode ${mode.toString().padStart(2, '0')}: decoder`); 150 | } 151 | 152 | t.end(); 153 | }); 154 | 155 | test('Mixed Modes', async (t) => { 156 | const modules = { 157 | rgba16f: 8, 158 | rgb9e5: 4, 159 | r11fg11fb10f_round: 4, 160 | r11fg11fb10f_trunc: 4, 161 | r11fg11fb10f_round_noclamp: 4, 162 | rgb32f: 12 163 | }; 164 | 165 | for (const module in modules) { 166 | const pixelSize = modules[module]; 167 | const decoder = await setup(`build/bc6hu-${module}.wasm`, pixelSize); 168 | 169 | generator.generateAll(numBlocks); 170 | const hashes = mixedChecksums.full; 171 | 172 | const genHash = crypto.createHash('sha256').update(compressedView).digest('hex'); 173 | t.equals(genHash, hashes.bc6h, `all modes: generator`); 174 | 175 | decoder.decode(WIDTH, HEIGHT); 176 | const decHash = crypto.createHash('sha256').update(uncompressedView).digest('hex'); 177 | t.equals(decHash, hashes[module], `all modes: ${module} decoder`); 178 | } 179 | 180 | t.end(); 181 | }); 182 | 183 | test('Mixed Modes - odd dimensions', async (t) => { 184 | const modules = { 185 | rgba16f: 8, 186 | rgb9e5: 4, 187 | r11fg11fb10f_round: 4, 188 | r11fg11fb10f_trunc: 4, 189 | r11fg11fb10f_round_noclamp: 4, 190 | rgb32f: 12 191 | }; 192 | 193 | for (const module in modules) { 194 | const pixelSize = modules[module]; 195 | const decoder = await setup(`build/bc6hu-${module}.wasm`, pixelSize, WIDTH - 1, HEIGHT - 1); 196 | 197 | generator.generateAll(numBlocks); 198 | const hashes = mixedChecksums.cropped; 199 | 200 | const genHash = crypto.createHash('sha256').update(compressedView).digest('hex'); 201 | t.equals(genHash, hashes.bc6h, `all modes: generator`); 202 | 203 | decoder.decode(WIDTH - 1, HEIGHT - 1); 204 | const decHash = crypto.createHash('sha256').update(uncompressedView).digest('hex'); 205 | t.equals(decHash, hashes[module], `all modes: ${module} decoder`); 206 | } 207 | 208 | t.end(); 209 | }); 210 | 211 | interface IDecoder { 212 | decode(width: number, height: number): number; 213 | } 214 | 215 | interface IGenerator { 216 | generate(mode: number, nBlocks: number): number; 217 | generateAll(nBlocks: number): number; 218 | } -------------------------------------------------------------------------------- /LICENSES/Apache-2.0.txt: -------------------------------------------------------------------------------- 1 | Apache License 2 | 3 | Version 2.0, January 2004 4 | 5 | http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, 6 | AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | 11 | 12 | "License" shall mean the terms and conditions for use, reproduction, and distribution 13 | as defined by Sections 1 through 9 of this document. 14 | 15 | 16 | 17 | "Licensor" shall mean the copyright owner or entity authorized by the copyright 18 | owner that is granting the License. 19 | 20 | 21 | 22 | "Legal Entity" shall mean the union of the acting entity and all other entities 23 | that control, are controlled by, or are under common control with that entity. 24 | For the purposes of this definition, "control" means (i) the power, direct 25 | or indirect, to cause the direction or management of such entity, whether 26 | by contract or otherwise, or (ii) ownership of fifty percent (50%) or more 27 | of the outstanding shares, or (iii) beneficial ownership of such entity. 28 | 29 | 30 | 31 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions 32 | granted by this License. 33 | 34 | 35 | 36 | "Source" form shall mean the preferred form for making modifications, including 37 | but not limited to software source code, documentation source, and configuration 38 | files. 39 | 40 | 41 | 42 | "Object" form shall mean any form resulting from mechanical transformation 43 | or translation of a Source form, including but not limited to compiled object 44 | code, generated documentation, and conversions to other media types. 45 | 46 | 47 | 48 | "Work" shall mean the work of authorship, whether in Source or Object form, 49 | made available under the License, as indicated by a copyright notice that 50 | is included in or attached to the work (an example is provided in the Appendix 51 | below). 52 | 53 | 54 | 55 | "Derivative Works" shall mean any work, whether in Source or Object form, 56 | that is based on (or derived from) the Work and for which the editorial revisions, 57 | annotations, elaborations, or other modifications represent, as a whole, an 58 | original work of authorship. For the purposes of this License, Derivative 59 | Works shall not include works that remain separable from, or merely link (or 60 | bind by name) to the interfaces of, the Work and Derivative Works thereof. 61 | 62 | 63 | 64 | "Contribution" shall mean any work of authorship, including the original version 65 | of the Work and any modifications or additions to that Work or Derivative 66 | Works thereof, that is intentionally submitted to Licensor for inclusion in 67 | the Work by the copyright owner or by an individual or Legal Entity authorized 68 | to submit on behalf of the copyright owner. For the purposes of this definition, 69 | "submitted" means any form of electronic, verbal, or written communication 70 | sent to the Licensor or its representatives, including but not limited to 71 | communication on electronic mailing lists, source code control systems, and 72 | issue tracking systems that are managed by, or on behalf of, the Licensor 73 | for the purpose of discussing and improving the Work, but excluding communication 74 | that is conspicuously marked or otherwise designated in writing by the copyright 75 | owner as "Not a Contribution." 76 | 77 | 78 | 79 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf 80 | of whom a Contribution has been received by Licensor and subsequently incorporated 81 | within the Work. 82 | 83 | 2. Grant of Copyright License. Subject to the terms and conditions of this 84 | License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, 85 | no-charge, royalty-free, irrevocable copyright license to reproduce, prepare 86 | Derivative Works of, publicly display, publicly perform, sublicense, and distribute 87 | the Work and such Derivative Works in Source or Object form. 88 | 89 | 3. Grant of Patent License. Subject to the terms and conditions of this License, 90 | each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, 91 | no-charge, royalty-free, irrevocable (except as stated in this section) patent 92 | license to make, have made, use, offer to sell, sell, import, and otherwise 93 | transfer the Work, where such license applies only to those patent claims 94 | licensable by such Contributor that are necessarily infringed by their Contribution(s) 95 | alone or by combination of their Contribution(s) with the Work to which such 96 | Contribution(s) was submitted. If You institute patent litigation against 97 | any entity (including a cross-claim or counterclaim in a lawsuit) alleging 98 | that the Work or a Contribution incorporated within the Work constitutes direct 99 | or contributory patent infringement, then any patent licenses granted to You 100 | under this License for that Work shall terminate as of the date such litigation 101 | is filed. 102 | 103 | 4. Redistribution. You may reproduce and distribute copies of the Work or 104 | Derivative Works thereof in any medium, with or without modifications, and 105 | in Source or Object form, provided that You meet the following conditions: 106 | 107 | (a) You must give any other recipients of the Work or Derivative Works a copy 108 | of this License; and 109 | 110 | (b) You must cause any modified files to carry prominent notices stating that 111 | You changed the files; and 112 | 113 | (c) You must retain, in the Source form of any Derivative Works that You distribute, 114 | all copyright, patent, trademark, and attribution notices from the Source 115 | form of the Work, excluding those notices that do not pertain to any part 116 | of the Derivative Works; and 117 | 118 | (d) If the Work includes a "NOTICE" text file as part of its distribution, 119 | then any Derivative Works that You distribute must include a readable copy 120 | of the attribution notices contained within such NOTICE file, excluding those 121 | notices that do not pertain to any part of the Derivative Works, in at least 122 | one of the following places: within a NOTICE text file distributed as part 123 | of the Derivative Works; within the Source form or documentation, if provided 124 | along with the Derivative Works; or, within a display generated by the Derivative 125 | Works, if and wherever such third-party notices normally appear. The contents 126 | of the NOTICE file are for informational purposes only and do not modify the 127 | License. You may add Your own attribution notices within Derivative Works 128 | that You distribute, alongside or as an addendum to the NOTICE text from the 129 | Work, provided that such additional attribution notices cannot be construed 130 | as modifying the License. 131 | 132 | You may add Your own copyright statement to Your modifications and may provide 133 | additional or different license terms and conditions for use, reproduction, 134 | or distribution of Your modifications, or for any such Derivative Works as 135 | a whole, provided Your use, reproduction, and distribution of the Work otherwise 136 | complies with the conditions stated in this License. 137 | 138 | 5. Submission of Contributions. Unless You explicitly state otherwise, any 139 | Contribution intentionally submitted for inclusion in the Work by You to the 140 | Licensor shall be under the terms and conditions of this License, without 141 | any additional terms or conditions. Notwithstanding the above, nothing herein 142 | shall supersede or modify the terms of any separate license agreement you 143 | may have executed with Licensor regarding such Contributions. 144 | 145 | 6. Trademarks. This License does not grant permission to use the trade names, 146 | trademarks, service marks, or product names of the Licensor, except as required 147 | for reasonable and customary use in describing the origin of the Work and 148 | reproducing the content of the NOTICE file. 149 | 150 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to 151 | in writing, Licensor provides the Work (and each Contributor provides its 152 | Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 153 | KIND, either express or implied, including, without limitation, any warranties 154 | or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR 155 | A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness 156 | of using or redistributing the Work and assume any risks associated with Your 157 | exercise of permissions under this License. 158 | 159 | 8. Limitation of Liability. In no event and under no legal theory, whether 160 | in tort (including negligence), contract, or otherwise, unless required by 161 | applicable law (such as deliberate and grossly negligent acts) or agreed to 162 | in writing, shall any Contributor be liable to You for damages, including 163 | any direct, indirect, special, incidental, or consequential damages of any 164 | character arising as a result of this License or out of the use or inability 165 | to use the Work (including but not limited to damages for loss of goodwill, 166 | work stoppage, computer failure or malfunction, or any and all other commercial 167 | damages or losses), even if such Contributor has been advised of the possibility 168 | of such damages. 169 | 170 | 9. Accepting Warranty or Additional Liability. While redistributing the Work 171 | or Derivative Works thereof, You may choose to offer, and charge a fee for, 172 | acceptance of support, warranty, indemnity, or other liability obligations 173 | and/or rights consistent with this License. However, in accepting such obligations, 174 | You may act only on Your own behalf and on Your sole responsibility, not on 175 | behalf of any other Contributor, and only if You agree to indemnify, defend, 176 | and hold each Contributor harmless for any liability incurred by, or claims 177 | asserted against, such Contributor by reason of your accepting any such warranty 178 | or additional liability. END OF TERMS AND CONDITIONS 179 | 180 | APPENDIX: How to apply the Apache License to your work. 181 | 182 | To apply the Apache License to your work, attach the following boilerplate 183 | notice, with the fields enclosed by brackets "[]" replaced with your own identifying 184 | information. (Don't include the brackets!) The text should be enclosed in 185 | the appropriate comment syntax for the file format. We also recommend that 186 | a file or class name and description of purpose be included on the same "printed 187 | page" as the copyright notice for easier identification within third-party 188 | archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | 194 | you may not use this file except in compliance with the License. 195 | 196 | You may obtain a copy of the License at 197 | 198 | http://www.apache.org/licenses/LICENSE-2.0 199 | 200 | Unless required by applicable law or agreed to in writing, software 201 | 202 | distributed under the License is distributed on an "AS IS" BASIS, 203 | 204 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 205 | 206 | See the License for the specific language governing permissions and 207 | 208 | limitations under the License. 209 | -------------------------------------------------------------------------------- /assembly/generator.ts: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Khronos Group Inc. 2 | // 3 | // SPDX-License-Identifier: Apache-2.0 4 | 5 | const kUserDataOffset = 65536; 6 | 7 | /** 8 | * Generate single-mode BC6H data 9 | * @param mode - Compressed BC6H Mode Index. Input [0..13] range is mapped to [0, 1, 2, 6, 10, 14, 18, 22, 26, 30, 3, 7, 11, 15, 19, 23, 27, 31] actual BC6H modes. 10 | * @param nBlocks - The total number of BC6H blocks to generate. 11 | * @returns Actual BC6H mode index or -1 on failure. 12 | */ 13 | export function generate(mode: i32, nBlocks: i32): i32 { 14 | if (mode > 17 || mode < 0 || nBlocks < 0) return -1; 15 | 16 | const totalBytes = nBlocks * 16; 17 | if ((memory.size() - 1) * 65536 < totalBytes) return -1; 18 | 19 | const actualMode = getActualMode(mode); 20 | for (let seed = 0; seed < nBlocks; seed++) { 21 | writeBlock(actualMode, seed); 22 | } 23 | return actualMode; 24 | } 25 | 26 | /** 27 | * Generate multi-mode BC6H data 28 | * @param nBlocks - The total number of BC6H blocks. 29 | * @returns Zero on success or -1 on failure. 30 | */ 31 | export function generateAll(nBlocks: i32): i32 { 32 | if (nBlocks < 0) return -1; 33 | 34 | const totalBytes = nBlocks * 16; 35 | if ((memory.size() - 1) * 65536 < totalBytes) return -1; 36 | 37 | for (let seed = 0; seed < nBlocks; seed++) { 38 | // Restrict reserved modes 39 | const actualMode = getActualMode((seed * 3 + seed / 14 + 1) % 14); 40 | writeBlock(actualMode, seed); 41 | } 42 | return 0; 43 | } 44 | 45 | /** 46 | * Generate multi-mode BC6H data containing only single-subset blocks. 47 | * @param nBlocks - The total number of BC6H blocks. 48 | * @returns Zero on success or -1 on failure. 49 | */ 50 | export function generateAllSingleSubset(nBlocks: u32): i32 { 51 | const totalBytes = nBlocks * 16; 52 | if ((memory.size() - 1) * 65536 < totalBytes) return -1; 53 | 54 | for (let seed: u64 = 0; seed < nBlocks; seed++) { 55 | const actualMode = getActualMode(10 + ((seed * 3 + seed / 4 + 1) % 4)); 56 | writeBlock(actualMode, seed); 57 | } 58 | return 0; 59 | } 60 | 61 | /** 62 | * Generate multi-mode BC6H data containing only dual-subset blocks. 63 | * @param nBlocks - The total number of BC6H blocks. 64 | * @returns Zero on success or -1 on failure. 65 | */ 66 | export function generateAllDualSubset(nBlocks: u32): i32 { 67 | const totalBytes = nBlocks * 16; 68 | if ((memory.size() - 1) * 65536 < totalBytes) return -1; 69 | 70 | for (let seed: u64 = 0; seed < nBlocks; seed++) { 71 | const actualMode = getActualMode((seed * 3 + seed / 10 + 1) % 10); 72 | writeBlock(actualMode, seed); 73 | } 74 | return 0; 75 | } 76 | 77 | function writeBlock(mode: i32, seed: u64): void { 78 | let r0: u64 = mode; 79 | r0 |= crc64(0, seed) << ((mode < 2) ? 2 : 5); 80 | 81 | let r1 = crc64(r0, seed); 82 | if ((mode & 3) != 3) { 83 | const pattern = (seed & 0x1F) << 13; 84 | r1 = (r1 & 0xFFFFFFFFFFFC1FFF) | pattern; 85 | } 86 | 87 | const offset = kUserDataOffset + seed * 16; 88 | store(offset, r0, 0); 89 | store(offset, r1, 8); 90 | } 91 | 92 | function getActualMode(mode: i32): i32 { 93 | switch (mode) { 94 | case 0: return 0; 95 | case 1: return 1; 96 | case 2: return 2; 97 | case 3: return 6; 98 | case 4: return 10; 99 | case 5: return 14; 100 | case 6: return 18; 101 | case 7: return 22; 102 | case 8: return 26; 103 | case 9: return 30; 104 | case 10: return 3; 105 | case 11: return 7; 106 | case 12: return 11; 107 | case 13: return 15; 108 | case 14: return 19; 109 | case 15: return 23; 110 | case 16: return 27; 111 | case 17: return 31; 112 | default: return unreachable(); 113 | } 114 | } 115 | 116 | const crcOffset = memory.data([ 117 | 0x0000000000000000, 118 | 0x7AD870C830358979, 119 | 0xF5B0E190606B12F2, 120 | 0x8F689158505E9B8B, 121 | 0xC038E5739841B68F, 122 | 0xBAE095BBA8743FF6, 123 | 0x358804E3F82AA47D, 124 | 0x4F50742BC81F2D04, 125 | 0xAB28ECB46814FE75, 126 | 0xD1F09C7C5821770C, 127 | 0x5E980D24087FEC87, 128 | 0x24407DEC384A65FE, 129 | 0x6B1009C7F05548FA, 130 | 0x11C8790FC060C183, 131 | 0x9EA0E857903E5A08, 132 | 0xE478989FA00BD371, 133 | 0x7D08FF3B88BE6F81, 134 | 0x07D08FF3B88BE6F8, 135 | 0x88B81EABE8D57D73, 136 | 0xF2606E63D8E0F40A, 137 | 0xBD301A4810FFD90E, 138 | 0xC7E86A8020CA5077, 139 | 0x4880FBD87094CBFC, 140 | 0x32588B1040A14285, 141 | 0xD620138FE0AA91F4, 142 | 0xACF86347D09F188D, 143 | 0x2390F21F80C18306, 144 | 0x594882D7B0F40A7F, 145 | 0x1618F6FC78EB277B, 146 | 0x6CC0863448DEAE02, 147 | 0xE3A8176C18803589, 148 | 0x997067A428B5BCF0, 149 | 0xFA11FE77117CDF02, 150 | 0x80C98EBF2149567B, 151 | 0x0FA11FE77117CDF0, 152 | 0x75796F2F41224489, 153 | 0x3A291B04893D698D, 154 | 0x40F16BCCB908E0F4, 155 | 0xCF99FA94E9567B7F, 156 | 0xB5418A5CD963F206, 157 | 0x513912C379682177, 158 | 0x2BE1620B495DA80E, 159 | 0xA489F35319033385, 160 | 0xDE51839B2936BAFC, 161 | 0x9101F7B0E12997F8, 162 | 0xEBD98778D11C1E81, 163 | 0x64B116208142850A, 164 | 0x1E6966E8B1770C73, 165 | 0x8719014C99C2B083, 166 | 0xFDC17184A9F739FA, 167 | 0x72A9E0DCF9A9A271, 168 | 0x08719014C99C2B08, 169 | 0x4721E43F0183060C, 170 | 0x3DF994F731B68F75, 171 | 0xB29105AF61E814FE, 172 | 0xC849756751DD9D87, 173 | 0x2C31EDF8F1D64EF6, 174 | 0x56E99D30C1E3C78F, 175 | 0xD9810C6891BD5C04, 176 | 0xA3597CA0A188D57D, 177 | 0xEC09088B6997F879, 178 | 0x96D1784359A27100, 179 | 0x19B9E91B09FCEA8B, 180 | 0x636199D339C963F2, 181 | 0xDF7ADABD7A6E2D6F, 182 | 0xA5A2AA754A5BA416, 183 | 0x2ACA3B2D1A053F9D, 184 | 0x50124BE52A30B6E4, 185 | 0x1F423FCEE22F9BE0, 186 | 0x659A4F06D21A1299, 187 | 0xEAF2DE5E82448912, 188 | 0x902AAE96B271006B, 189 | 0x74523609127AD31A, 190 | 0x0E8A46C1224F5A63, 191 | 0x81E2D7997211C1E8, 192 | 0xFB3AA75142244891, 193 | 0xB46AD37A8A3B6595, 194 | 0xCEB2A3B2BA0EECEC, 195 | 0x41DA32EAEA507767, 196 | 0x3B024222DA65FE1E, 197 | 0xA2722586F2D042EE, 198 | 0xD8AA554EC2E5CB97, 199 | 0x57C2C41692BB501C, 200 | 0x2D1AB4DEA28ED965, 201 | 0x624AC0F56A91F461, 202 | 0x1892B03D5AA47D18, 203 | 0x97FA21650AFAE693, 204 | 0xED2251AD3ACF6FEA, 205 | 0x095AC9329AC4BC9B, 206 | 0x7382B9FAAAF135E2, 207 | 0xFCEA28A2FAAFAE69, 208 | 0x8632586ACA9A2710, 209 | 0xC9622C4102850A14, 210 | 0xB3BA5C8932B0836D, 211 | 0x3CD2CDD162EE18E6, 212 | 0x460ABD1952DB919F, 213 | 0x256B24CA6B12F26D, 214 | 0x5FB354025B277B14, 215 | 0xD0DBC55A0B79E09F, 216 | 0xAA03B5923B4C69E6, 217 | 0xE553C1B9F35344E2, 218 | 0x9F8BB171C366CD9B, 219 | 0x10E3202993385610, 220 | 0x6A3B50E1A30DDF69, 221 | 0x8E43C87E03060C18, 222 | 0xF49BB8B633338561, 223 | 0x7BF329EE636D1EEA, 224 | 0x012B592653589793, 225 | 0x4E7B2D0D9B47BA97, 226 | 0x34A35DC5AB7233EE, 227 | 0xBBCBCC9DFB2CA865, 228 | 0xC113BC55CB19211C, 229 | 0x5863DBF1E3AC9DEC, 230 | 0x22BBAB39D3991495, 231 | 0xADD33A6183C78F1E, 232 | 0xD70B4AA9B3F20667, 233 | 0x985B3E827BED2B63, 234 | 0xE2834E4A4BD8A21A, 235 | 0x6DEBDF121B863991, 236 | 0x1733AFDA2BB3B0E8, 237 | 0xF34B37458BB86399, 238 | 0x8993478DBB8DEAE0, 239 | 0x06FBD6D5EBD3716B, 240 | 0x7C23A61DDBE6F812, 241 | 0x3373D23613F9D516, 242 | 0x49ABA2FE23CC5C6F, 243 | 0xC6C333A67392C7E4, 244 | 0xBC1B436E43A74E9D, 245 | 0x95AC9329AC4BC9B5, 246 | 0xEF74E3E19C7E40CC, 247 | 0x601C72B9CC20DB47, 248 | 0x1AC40271FC15523E, 249 | 0x5594765A340A7F3A, 250 | 0x2F4C0692043FF643, 251 | 0xA02497CA54616DC8, 252 | 0xDAFCE7026454E4B1, 253 | 0x3E847F9DC45F37C0, 254 | 0x445C0F55F46ABEB9, 255 | 0xCB349E0DA4342532, 256 | 0xB1ECEEC59401AC4B, 257 | 0xFEBC9AEE5C1E814F, 258 | 0x8464EA266C2B0836, 259 | 0x0B0C7B7E3C7593BD, 260 | 0x71D40BB60C401AC4, 261 | 0xE8A46C1224F5A634, 262 | 0x927C1CDA14C02F4D, 263 | 0x1D148D82449EB4C6, 264 | 0x67CCFD4A74AB3DBF, 265 | 0x289C8961BCB410BB, 266 | 0x5244F9A98C8199C2, 267 | 0xDD2C68F1DCDF0249, 268 | 0xA7F41839ECEA8B30, 269 | 0x438C80A64CE15841, 270 | 0x3954F06E7CD4D138, 271 | 0xB63C61362C8A4AB3, 272 | 0xCCE411FE1CBFC3CA, 273 | 0x83B465D5D4A0EECE, 274 | 0xF96C151DE49567B7, 275 | 0x76048445B4CBFC3C, 276 | 0x0CDCF48D84FE7545, 277 | 0x6FBD6D5EBD3716B7, 278 | 0x15651D968D029FCE, 279 | 0x9A0D8CCEDD5C0445, 280 | 0xE0D5FC06ED698D3C, 281 | 0xAF85882D2576A038, 282 | 0xD55DF8E515432941, 283 | 0x5A3569BD451DB2CA, 284 | 0x20ED197575283BB3, 285 | 0xC49581EAD523E8C2, 286 | 0xBE4DF122E51661BB, 287 | 0x3125607AB548FA30, 288 | 0x4BFD10B2857D7349, 289 | 0x04AD64994D625E4D, 290 | 0x7E7514517D57D734, 291 | 0xF11D85092D094CBF, 292 | 0x8BC5F5C11D3CC5C6, 293 | 0x12B5926535897936, 294 | 0x686DE2AD05BCF04F, 295 | 0xE70573F555E26BC4, 296 | 0x9DDD033D65D7E2BD, 297 | 0xD28D7716ADC8CFB9, 298 | 0xA85507DE9DFD46C0, 299 | 0x273D9686CDA3DD4B, 300 | 0x5DE5E64EFD965432, 301 | 0xB99D7ED15D9D8743, 302 | 0xC3450E196DA80E3A, 303 | 0x4C2D9F413DF695B1, 304 | 0x36F5EF890DC31CC8, 305 | 0x79A59BA2C5DC31CC, 306 | 0x037DEB6AF5E9B8B5, 307 | 0x8C157A32A5B7233E, 308 | 0xF6CD0AFA9582AA47, 309 | 0x4AD64994D625E4DA, 310 | 0x300E395CE6106DA3, 311 | 0xBF66A804B64EF628, 312 | 0xC5BED8CC867B7F51, 313 | 0x8AEEACE74E645255, 314 | 0xF036DC2F7E51DB2C, 315 | 0x7F5E4D772E0F40A7, 316 | 0x05863DBF1E3AC9DE, 317 | 0xE1FEA520BE311AAF, 318 | 0x9B26D5E88E0493D6, 319 | 0x144E44B0DE5A085D, 320 | 0x6E963478EE6F8124, 321 | 0x21C640532670AC20, 322 | 0x5B1E309B16452559, 323 | 0xD476A1C3461BBED2, 324 | 0xAEAED10B762E37AB, 325 | 0x37DEB6AF5E9B8B5B, 326 | 0x4D06C6676EAE0222, 327 | 0xC26E573F3EF099A9, 328 | 0xB8B627F70EC510D0, 329 | 0xF7E653DCC6DA3DD4, 330 | 0x8D3E2314F6EFB4AD, 331 | 0x0256B24CA6B12F26, 332 | 0x788EC2849684A65F, 333 | 0x9CF65A1B368F752E, 334 | 0xE62E2AD306BAFC57, 335 | 0x6946BB8B56E467DC, 336 | 0x139ECB4366D1EEA5, 337 | 0x5CCEBF68AECEC3A1, 338 | 0x2616CFA09EFB4AD8, 339 | 0xA97E5EF8CEA5D153, 340 | 0xD3A62E30FE90582A, 341 | 0xB0C7B7E3C7593BD8, 342 | 0xCA1FC72BF76CB2A1, 343 | 0x45775673A732292A, 344 | 0x3FAF26BB9707A053, 345 | 0x70FF52905F188D57, 346 | 0x0A2722586F2D042E, 347 | 0x854FB3003F739FA5, 348 | 0xFF97C3C80F4616DC, 349 | 0x1BEF5B57AF4DC5AD, 350 | 0x61372B9F9F784CD4, 351 | 0xEE5FBAC7CF26D75F, 352 | 0x9487CA0FFF135E26, 353 | 0xDBD7BE24370C7322, 354 | 0xA10FCEEC0739FA5B, 355 | 0x2E675FB4576761D0, 356 | 0x54BF2F7C6752E8A9, 357 | 0xCDCF48D84FE75459, 358 | 0xB71738107FD2DD20, 359 | 0x387FA9482F8C46AB, 360 | 0x42A7D9801FB9CFD2, 361 | 0x0DF7ADABD7A6E2D6, 362 | 0x772FDD63E7936BAF, 363 | 0xF8474C3BB7CDF024, 364 | 0x829F3CF387F8795D, 365 | 0x66E7A46C27F3AA2C, 366 | 0x1C3FD4A417C62355, 367 | 0x935745FC4798B8DE, 368 | 0xE98F353477AD31A7, 369 | 0xA6DF411FBFB21CA3, 370 | 0xDC0731D78F8795DA, 371 | 0x536FA08FDFD90E51, 372 | 0x29B7D047EFEC8728, 373 | ]); 374 | 375 | function crc64(crc: u64, seed: u64): u64 { 376 | let byte: usize; 377 | 378 | byte = usize((crc ^ (seed & 0xFF)) & 0xFF); 379 | crc = load(byte, crcOffset) ^ (crc >> 8); 380 | 381 | byte = usize((crc ^ (seed >> 8 & 0xFF)) & 0xFF); 382 | crc = load(byte, crcOffset) ^ (crc >> 8); 383 | 384 | byte = usize((crc ^ (seed >> 16 & 0xFF)) & 0xFF); 385 | crc = load(byte, crcOffset) ^ (crc >> 8); 386 | 387 | byte = usize((crc ^ (seed >> 24 & 0xFF)) & 0xFF); 388 | crc = load(byte, crcOffset) ^ (crc >> 8); 389 | 390 | byte = usize((crc ^ (seed >> 32 & 0xFF)) & 0xFF); 391 | crc = load(byte, crcOffset) ^ (crc >> 8); 392 | 393 | byte = usize((crc ^ (seed >> 40 & 0xFF)) & 0xFF); 394 | crc = load(byte, crcOffset) ^ (crc >> 8); 395 | 396 | byte = usize((crc ^ (seed >> 48 & 0xFF)) & 0xFF); 397 | crc = load(byte, crcOffset) ^ (crc >> 8); 398 | 399 | byte = usize((crc ^ (seed >> 58)) & 0xFF); 400 | crc = load(byte, crcOffset) ^ (crc >> 8); 401 | 402 | return crc; 403 | } 404 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /assembly/decoder.ts: -------------------------------------------------------------------------------- 1 | // Copyright 2020 The Khronos Group Inc. 2 | // 3 | // SPDX-License-Identifier: Apache-2.0 4 | 5 | // @ts-ignore 6 | const kIsRGBA16F = MODE == 0; 7 | // @ts-ignore 8 | const kIsRGB9E5 = MODE == 1; 9 | // @ts-ignore 10 | const kIsRoundRG11FB10F = MODE == 2; 11 | // @ts-ignore 12 | const kIsTruncRG11FB10F = MODE == 3; 13 | // @ts-ignore 14 | const kIsRoundNoClampRG11FB10F = MODE == 4; 15 | // @ts-ignore 16 | const kIsRGB32F = MODE == 5; 17 | 18 | if (!kIsRGBA16F && 19 | !kIsRGB9E5 && 20 | !kIsTruncRG11FB10F && 21 | !kIsRoundRG11FB10F && 22 | !kIsRoundNoClampRG11FB10F && 23 | !kIsRGB32F) { 24 | ERROR('Decode mode is unspecified'); 25 | } 26 | 27 | const kPixelLength = kIsRGBA16F ? 8 : (kIsRGB32F ? 12 : 4); 28 | 29 | let firstRun = true; 30 | 31 | /** 32 | * Decode BC6H data as RGBA16F 33 | * @param width - Texture width in pixels 34 | * @param height - Texture height in pixels 35 | */ 36 | export function decode(width: i32, height: i32): i32 { 37 | // Fail on negative dimensions 38 | if ((width | height) < 0) return 1; 39 | 40 | const xBlocks = (width + 3) >> 2; 41 | const yBlocks = (height + 3) >> 2; 42 | 43 | // Fail on insufficient memory 44 | const compressedByteLength = xBlocks * yBlocks * 16; 45 | const decompressedByteLength = width * (4 * yBlocks) * kPixelLength; 46 | const totalMemoryLength = compressedByteLength + decompressedByteLength; 47 | if ((memory.size() - 1) * 65536 < totalMemoryLength) { 48 | return 1; 49 | } 50 | 51 | const kUserDataOffset = 65536; 52 | 53 | if (firstRun) { 54 | // 4-bit weights 55 | // [0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64] 56 | store(0, 0x1E1A15110D090400); 57 | store(8, 0x403C37332F2B2622); 58 | 59 | // 3-bit weights 60 | // [0, 9, 18, 27, 37, 46, 55, 64] 61 | store(16, 0x40372E251B120900); 62 | 63 | // Partition patterns 64 | store(32, 0xECC8EEEE8888CCCC, 0); 65 | store(32, 0xEC80FEC8FEECC880, 8); 66 | store(32, 0xE800FE80FFECC800, 16); 67 | store(32, 0xF000FFF0FF00FFE8, 24); 68 | store(32, 0x08CE7100008EF710, 32); 69 | store(32, 0x8CCE31007310008C, 40); 70 | store(32, 0x366C66663110088C, 48); 71 | store(32, 0x399C718E0FF017E8, 56); 72 | 73 | // Bit-reverse 6 bits for Mode 15 74 | store(96, 0x3818280830102000, 0); 75 | store(96, 0x3C1C2C0C34142404, 8); 76 | store(96, 0x3A1A2A0A32122202, 16); 77 | store(96, 0x3E1E2E0E36162606, 24); 78 | store(96, 0x3919290931112101, 32); 79 | store(96, 0x3D1D2D0D35152505, 40); 80 | store(96, 0x3B1B2B0B33132303, 48); 81 | store(96, 0x3F1F2F0F37172707, 56); 82 | 83 | // Precompute unquantized endpoints 84 | for (let w: u32 = 6, offset = 160; w < 13; w++) { 85 | const n: u32 = 1 << w; 86 | const lo: u32 = 1 << (15 - w); 87 | const hi: u32 = (((1 << w) - 1) << (16 - w)) | lo; 88 | for (let i: u32 = 0; i < n; i++) { 89 | let value: u32 = i << (16 - w); 90 | value |= value ? lo : 0; 91 | value = (value == hi) ? 65535 : value; 92 | store(offset + i * 2, value); 93 | } 94 | offset += n * 2; 95 | } 96 | 97 | firstRun = false; 98 | } 99 | 100 | const completeXBlocks = width >> 2; 101 | const extraByteLength = (width & 3) * kPixelLength; 102 | const uncompressedLineByteStride = width * kPixelLength; 103 | const writeSkip = uncompressedLineByteStride * 3 + extraByteLength; 104 | 105 | const compressedBlockSkip = extraByteLength ? 16 : 0; 106 | 107 | let readOffset = kUserDataOffset; 108 | let writeOffset = readOffset + compressedByteLength; 109 | 110 | for (let y = 0; y < yBlocks; y++) { 111 | for (let x = 0; x < completeXBlocks; x++) { 112 | decodeBlock(readOffset, writeOffset, uncompressedLineByteStride); 113 | readOffset += 16; 114 | writeOffset += 4 * kPixelLength; 115 | } 116 | readOffset += compressedBlockSkip; 117 | writeOffset += writeSkip; 118 | } 119 | 120 | // Image width is not a multiple of 4. 121 | if (extraByteLength) { 122 | readOffset = kUserDataOffset + completeXBlocks * 16; 123 | writeOffset = kUserDataOffset + compressedByteLength + 124 | completeXBlocks * 4 * kPixelLength; 125 | 126 | const compressedBlockLineByteStride = xBlocks * 16; 127 | const uncompressedBlockLineByteStride = uncompressedLineByteStride * 4; 128 | 129 | for (let y = 0; y < yBlocks; y++) { 130 | const kOffset = 32768; 131 | // Decode to a temporary memory location and copy only needed pixels. 132 | decodeBlock(readOffset, kOffset, kPixelLength * 4); 133 | for (let x = 0; x < extraByteLength; x += kPixelLength) { 134 | let s = writeOffset; 135 | if (kPixelLength == 8) { 136 | store(s + x, load(x, kOffset)); 137 | s += uncompressedLineByteStride; 138 | store(s + x, load(x, kOffset + kPixelLength * 4)); 139 | s += uncompressedLineByteStride; 140 | store(s + x, load(x, kOffset + kPixelLength * 8)); 141 | s += uncompressedLineByteStride; 142 | store(s + x, load(x, kOffset + kPixelLength * 12)); 143 | } else if (kPixelLength == 4) { 144 | store(s + x, load(x, kOffset)); 145 | s += uncompressedLineByteStride; 146 | store(s + x, load(x, kOffset + kPixelLength * 4)); 147 | s += uncompressedLineByteStride; 148 | store(s + x, load(x, kOffset + kPixelLength * 8)); 149 | s += uncompressedLineByteStride; 150 | store(s + x, load(x, kOffset + kPixelLength * 12)); 151 | } else if (kPixelLength == 12) { 152 | store(s + x, load(x, kOffset)); 153 | store(s + x, load(x, kOffset + 4), 4); 154 | store(s + x, load(x, kOffset + 8), 8); 155 | s += uncompressedLineByteStride; 156 | store(s + x, load(x, kOffset + kPixelLength * 4)); 157 | store(s + x, load(x, kOffset + kPixelLength * 4 + 4), 4); 158 | store(s + x, load(x, kOffset + kPixelLength * 4 + 8), 8); 159 | s += uncompressedLineByteStride; 160 | store(s + x, load(x, kOffset + kPixelLength * 8)); 161 | store(s + x, load(x, kOffset + kPixelLength * 8 + 4), 4); 162 | store(s + x, load(x, kOffset + kPixelLength * 8 + 8), 8); 163 | s += uncompressedLineByteStride; 164 | store(s + x, load(x, kOffset + kPixelLength * 12)); 165 | store(s + x, load(x, kOffset + kPixelLength * 12 + 4), 4); 166 | store(s + x, load(x, kOffset + kPixelLength * 12 + 8), 8); 167 | } else { 168 | ERROR('Invalid kPixelLength.'); 169 | } 170 | } 171 | readOffset += compressedBlockLineByteStride; 172 | writeOffset += uncompressedBlockLineByteStride; 173 | } 174 | } 175 | return 0; 176 | } 177 | 178 | function decodeBlock(readOffset: i32, offset: i32, stride: i32): void { 179 | const q0 = load(readOffset, 0); 180 | const q1 = load(readOffset, 8); 181 | 182 | if (3 == (q0 & 3)) { 183 | decodeOneSubsetBlock(q0, q1, offset, stride); 184 | } else { 185 | decodeTwoSubsetBlock(q0, q1, offset, stride); 186 | } 187 | } 188 | 189 | // @ts-ignore 190 | @inline 191 | function decodeOneSubsetBlock(q0: u64, q1: u64, offset: i32, stride: i32): void { 192 | // One-subset modes always use lower 5 bits 193 | const mode = q0 & 0x1F; 194 | 195 | // 32-bit views into 64-bit inputs 196 | const d0 = q0; 197 | const d1 = (q0 >> 32); 198 | const d2 = q1; 199 | 200 | let r0: u32 = 0; 201 | let g0: u32 = 0; 202 | let b0: u32 = 0; 203 | 204 | let r1: u32 = 0; 205 | let g1: u32 = 0; 206 | let b1: u32 = 0; 207 | 208 | switch (mode) { 209 | case 3: 210 | // rgb0 - 10 bits 211 | // rgb1 - 10 bits 212 | 213 | r0 = (d0 >> 5) & 0x3FF; 214 | g0 = (d0 >> 15) & 0x3FF; 215 | b0 = ((q0 >> 25) & 0x3FF); 216 | 217 | r1 = (d1 >> 3) & 0x3FF; 218 | g1 = (d1 >> 13) & 0x3FF; 219 | b1 = ((d2 << 9) & 0x200) | (d1 >> 23); 220 | 221 | r0 = unq(10, r0); 222 | g0 = unq(10, g0); 223 | b0 = unq(10, b0); 224 | 225 | r1 = unq(10, r1); 226 | g1 = unq(10, g1); 227 | b1 = unq(10, b1); 228 | break; 229 | case 7: 230 | // rgb0 - 11 bits 231 | // rgb1 - 9 bits (delta) 232 | 233 | r0 = ((d1 >> 2) & 0x400) | ((d0 >> 5) & 0x3FF); 234 | g0 = ((d1 >> 12) & 0x400) | ((d0 >> 15) & 0x3FF); 235 | b0 = ((d2 << 10) & 0x400) | ((q0 >> 25) & 0x3FF); 236 | 237 | r1 = (r0 + signExtend(9, d1 >> 3)) & 0x7FF; 238 | g1 = (g0 + signExtend(9, d1 >> 13)) & 0x7FF; 239 | b1 = (b0 + signExtend(9, d1 >> 23)) & 0x7FF; 240 | 241 | r0 = unq(11, r0); 242 | g0 = unq(11, g0); 243 | b0 = unq(11, b0); 244 | 245 | r1 = unq(11, r1); 246 | g1 = unq(11, g1); 247 | b1 = unq(11, b1); 248 | break; 249 | case 11: 250 | // rgb0 - 12 bits 251 | // rgb1 - 8 bits (delta) 252 | 253 | r0 = ( 254 | ((d1) & 0x800) | 255 | ((d1 >> 2) & 0x400) | 256 | ((d0 >> 5) & 0x3FF) 257 | ); 258 | g0 = ( 259 | ((d1 >> 10) & 0x800) | 260 | ((d1 >> 12) & 0x400) | 261 | ((d0 >> 15) & 0x3FF) 262 | ); 263 | b0 = ( 264 | ((d1 >> 20) & 0x800) | 265 | ((d2 << 10) & 0x400) | 266 | ((q0 >> 25) & 0x3FF) 267 | ); 268 | 269 | r1 = (r0 + signExtend(8, d1 >> 3)) & 0xFFF; 270 | g1 = (g0 + signExtend(8, d1 >> 13)) & 0xFFF; 271 | b1 = (b0 + signExtend(8, d1 >> 23)) & 0xFFF; 272 | 273 | r0 = unq(12, r0); 274 | g0 = unq(12, g0); 275 | b0 = unq(12, b0); 276 | 277 | r1 = unq(12, r1); 278 | g1 = unq(12, g1); 279 | b1 = unq(12, b1); 280 | break; 281 | case 15: 282 | // rgb0 - 16 bits 283 | // rgb1 - 4 bits (delta) 284 | 285 | r0 = ( 286 | (reverse6((d1 >> 7) & 0x3F) << 10) | 287 | ((d0 >> 5) & 0x3FF) 288 | ); 289 | g0 = ( 290 | (reverse6((d1 >> 17) & 0x3F) << 10) | 291 | ((d0 >> 15) & 0x3FF) 292 | ); 293 | b0 = ( 294 | ((reverse6(d1 >> 27) | (d2 & 1)) << 10) | 295 | ((q0 >> 25) & 0x3FF) 296 | ); 297 | 298 | r1 = (r0 + signExtend(4, d1 >> 3)) & 0xFFFF; 299 | g1 = (g0 + signExtend(4, d1 >> 13)) & 0xFFFF; 300 | b1 = (b0 + signExtend(4, d1 >> 23)) & 0xFFFF; 301 | 302 | // No need to unquantize 16-bit endpoints 303 | break; 304 | } 305 | 306 | let weights = (q1 & 0xFFFFFFFFFFFFFFF0) | ((q1 >> 1) & 7); 307 | 308 | const rs0 = r0 * 64 + 32; 309 | const gs0 = g0 * 64 + 32; 310 | const bs0 = b0 * 64 + 32; 311 | const rd0 = r1 - r0; 312 | const gd0 = g1 - g0; 313 | const bd0 = b1 - b0; 314 | 315 | for (let i = 0; i < 4; i++) { 316 | if (kIsRGBA16F) { 317 | store(offset, lerp1(rs0, gs0, bs0, rd0, gd0, bd0, 318 | weights & 0xF)); 319 | } else if (kIsRGB32F) { 320 | const w = weight4(weights & 0xF); 321 | store(offset, lerpF32(rs0, rd0, w)); 322 | store(offset, lerpF32(gs0, gd0, w), 4); 323 | store(offset, lerpF32(bs0, bd0, w), 8); 324 | } else { 325 | store(offset, lerp1(rs0, gs0, bs0, rd0, gd0, bd0, 326 | weights & 0xF)); 327 | } 328 | weights >>= 4; 329 | 330 | if (kIsRGBA16F) { 331 | store(offset, lerp1(rs0, gs0, bs0, rd0, gd0, bd0, 332 | weights & 0xF), kPixelLength); 333 | } else if (kIsRGB32F) { 334 | const w = weight4(weights & 0xF); 335 | store(offset, lerpF32(rs0, rd0, w), kPixelLength); 336 | store(offset, lerpF32(gs0, gd0, w), kPixelLength + 4); 337 | store(offset, lerpF32(bs0, bd0, w), kPixelLength + 8); 338 | } else { 339 | store(offset, lerp1(rs0, gs0, bs0, rd0, gd0, bd0, 340 | weights & 0xF), kPixelLength); 341 | } 342 | weights >>= 4; 343 | 344 | if (kIsRGBA16F) { 345 | store(offset, lerp1(rs0, gs0, bs0, rd0, gd0, bd0, 346 | weights & 0xF), kPixelLength * 2); 347 | } else if (kIsRGB32F) { 348 | const w = weight4(weights & 0xF); 349 | store(offset, lerpF32(rs0, rd0, w), kPixelLength * 2); 350 | store(offset, lerpF32(gs0, gd0, w), kPixelLength * 2 + 4); 351 | store(offset, lerpF32(bs0, bd0, w), kPixelLength * 2 + 8); 352 | } else { 353 | store(offset, lerp1(rs0, gs0, bs0, rd0, gd0, bd0, 354 | weights & 0xF), kPixelLength * 2); 355 | } 356 | weights >>= 4; 357 | 358 | if (kIsRGBA16F) { 359 | store(offset, lerp1(rs0, gs0, bs0, rd0, gd0, bd0, 360 | weights & 0xF), kPixelLength * 3); 361 | } else if (kIsRGB32F) { 362 | const w = weight4(weights & 0xF); 363 | store(offset, lerpF32(rs0, rd0, w), kPixelLength * 3); 364 | store(offset, lerpF32(gs0, gd0, w), kPixelLength * 3 + 4); 365 | store(offset, lerpF32(bs0, bd0, w), kPixelLength * 3 + 8); 366 | } else { 367 | store(offset, lerp1(rs0, gs0, bs0, rd0, gd0, bd0, 368 | weights & 0xF), kPixelLength * 3); 369 | } 370 | weights >>= 4; 371 | offset += stride; 372 | } 373 | } 374 | 375 | // @ts-ignore 376 | @inline 377 | function decodeTwoSubsetBlock(q0: u64, q1: u64, offset: i32, stride: i32): void { 378 | const mode = q0 & ((q0 & 2) ? 0x1F : 3); 379 | 380 | // 32-bit views into 64-bit inputs 381 | const d0 = q0; 382 | const d1 = (q0 >> 32); 383 | const d2 = q1; 384 | 385 | let r0: u32 = 0; 386 | let g0: u32 = 0; 387 | let b0: u32 = 0; 388 | 389 | let r1: u32 = 0; 390 | let g1: u32 = 0; 391 | let b1: u32 = 0; 392 | 393 | let r2: u32 = 0; 394 | let g2: u32 = 0; 395 | let b2: u32 = 0; 396 | 397 | let r3: u32 = 0; 398 | let g3: u32 = 0; 399 | let b3: u32 = 0; 400 | 401 | switch (mode) { 402 | case 0: 403 | // rgb0 - 10 bits 404 | // rgb1, rgb2, rgb3 - 5 bits (delta) 405 | 406 | r0 = (d0 >> 5) & 0x3FF; 407 | g0 = (d0 >> 15) & 0x3FF; 408 | b0 = ((q0 >> 25) & 0x3FF); 409 | 410 | r1 = r0 + signExtend(5, d1 >> 3); 411 | g1 = g0 + signExtend(5, d1 >> 13); 412 | b1 = b0 + signExtend(5, d1 >> 23); 413 | 414 | r2 = r0 + signExtend(5, d2 >> 1); 415 | g2 = g0 + signExtend(5, ((d0 << 2) & 0x10) | ((d1 >> 9) & 0xF)); 416 | b2 = b0 + signExtend(5, ( 417 | ((d0 << 1) & 0x10) | 418 | ((d2 << 3) & 0x08) | 419 | ((d1 >> 29)) 420 | )); 421 | 422 | r3 = r0 + signExtend(5, d2 >> 7); 423 | g3 = g0 + signExtend(5, ((d1 >> 4) & 0x10) | ((d1 >> 19) & 0xF)); 424 | b3 = b0 + signExtend(5, ( 425 | ((d0 ) & 0x10) | 426 | ((d2 >> 9) & 0x08) | 427 | ((d2 >> 4) & 0x04) | 428 | ((d1 >> 27) & 0x02) | 429 | ((d1 >> 18) & 0x01) 430 | )); 431 | 432 | r0 = unq(10, r0); 433 | g0 = unq(10, g0); 434 | b0 = unq(10, b0); 435 | 436 | r1 = unq(10, r1 & 0x3FF); 437 | g1 = unq(10, g1 & 0x3FF); 438 | b1 = unq(10, b1 & 0x3FF); 439 | 440 | r2 = unq(10, r2 & 0x3FF); 441 | g2 = unq(10, g2 & 0x3FF); 442 | b2 = unq(10, b2 & 0x3FF); 443 | 444 | r3 = unq(10, r3 & 0x3FF); 445 | g3 = unq(10, g3 & 0x3FF); 446 | b3 = unq(10, b3 & 0x3FF); 447 | break; 448 | case 1: 449 | // rgb0 - 7 bits 450 | // rgb1, rgb2, rgb3 - 6 bits (delta) 451 | 452 | r0 = (d0 >> 5) & 0x7F; 453 | g0 = (d0 >> 15) & 0x7F; 454 | b0 = (d0 >> 25); 455 | 456 | r1 = r0 + signExtend(6, d1 >> 3); 457 | g1 = g0 + signExtend(6, d1 >> 13); 458 | b1 = b0 + signExtend(6, d1 >> 23); 459 | 460 | r2 = r0 + signExtend(6, d2 >> 1); 461 | g2 = g0 + signExtend(6, ( 462 | ((d0 << 3) & 0x20) | 463 | ((d0 >> 20) & 0x10) | 464 | ((d1 >> 9) & 0x0F) 465 | )); 466 | b2 = b0 + signExtend(6, ( 467 | ((d0 >> 17) & 0x20) | 468 | ((d0 >> 10) & 0x10) | 469 | ((d2 << 3) & 0x08) | 470 | ((d1 >> 29)) 471 | )); 472 | 473 | r3 = r0 + signExtend(6, d2 >> 7); 474 | g3 = g0 + signExtend(6, ((d0 << 1) & 0x30) | ((d1 >> 19) & 0xF)); 475 | b3 = b0 + signExtend(6, ( 476 | ((d1 << 4) & 0x20) | 477 | ((d1 << 2) & 0x10) | 478 | ((d1 << 3) & 0x08) | 479 | ((d0 >> 21) & 0x04) | 480 | ((d0 >> 12) & 0x03) 481 | )); 482 | 483 | r0 = unq(7, r0); 484 | g0 = unq(7, g0); 485 | b0 = unq(7, b0); 486 | 487 | r1 = unq(7, r1 & 0x7F); 488 | g1 = unq(7, g1 & 0x7F); 489 | b1 = unq(7, b1 & 0x7F); 490 | 491 | r2 = unq(7, r2 & 0x7F); 492 | g2 = unq(7, g2 & 0x7F); 493 | b2 = unq(7, b2 & 0x7F); 494 | 495 | r3 = unq(7, r3 & 0x7F); 496 | g3 = unq(7, g3 & 0x7F); 497 | b3 = unq(7, b3 & 0x7F); 498 | break; 499 | case 2: 500 | // rgb0 - 11 bits 501 | // r1, r2, r3 - 5 bits (delta) 502 | // g1, g2, g3 - 4 bits (delta) 503 | // b1, b2, b3 - 4 bits (delta) 504 | 505 | r0 = ((d1 << 2) & 0x400) | ((d0 >> 5) & 0x3FF); 506 | g0 = ((d1 >> 7) & 0x400) | ((d0 >> 15) & 0x3FF); 507 | b0 = ((d1 >> 17) & 0x400) | ((q0 >> 25) & 0x3FF); 508 | 509 | r1 = r0 + signExtend(5, d1 >> 3); 510 | g1 = g0 + signExtend(4, d1 >> 13); 511 | b1 = b0 + signExtend(4, d1 >> 23); 512 | 513 | r2 = r0 + signExtend(5, d2 >> 1); 514 | g2 = g0 + signExtend(4, d1 >> 9); 515 | b2 = b0 + signExtend(4, ((d2 << 3) & 8) | (d1 >> 29)); 516 | 517 | r3 = r0 + signExtend(5, d2 >> 7); 518 | g3 = g0 + signExtend(4, d1 >> 19); 519 | b3 = b0 + signExtend(4, ( 520 | ((d2 >> 9) & 8) | 521 | ((d2 >> 4) & 4) | 522 | ((d1 >> 27) & 2) | 523 | ((d1 >> 18) & 1) 524 | )); 525 | 526 | r0 = unq(11, r0); 527 | g0 = unq(11, g0); 528 | b0 = unq(11, b0); 529 | 530 | r1 = unq(11, r1 & 0x7FF); 531 | g1 = unq(11, g1 & 0x7FF); 532 | b1 = unq(11, b1 & 0x7FF); 533 | 534 | r2 = unq(11, r2 & 0x7FF); 535 | g2 = unq(11, g2 & 0x7FF); 536 | b2 = unq(11, b2 & 0x7FF); 537 | 538 | r3 = unq(11, r3 & 0x7FF); 539 | g3 = unq(11, g3 & 0x7FF); 540 | b3 = unq(11, b3 & 0x7FF); 541 | break; 542 | case 6: 543 | // rgb0 - 11 bits 544 | // r1, r2, r3 - 4 bits (delta) 545 | // g1, g2, g3 - 5 bits (delta) 546 | // b1, b2, b3 - 4 bits (delta) 547 | 548 | r0 = (((q0 >> 29) & 0x400) | ((q0 >> 5) & 0x3FF)); 549 | g0 = (((q0 >> 40) & 0x400) | ((q0 >> 15) & 0x3FF)); 550 | b0 = (((q0 >> 49) & 0x400) | ((q0 >> 25) & 0x3FF)); 551 | 552 | r1 = r0 + signExtend(4, d1 >> 3); 553 | g1 = g0 + signExtend(5, d1 >> 13); 554 | b1 = b0 + signExtend(4, d1 >> 23); 555 | 556 | r2 = r0 + signExtend(4, d2 >> 1); 557 | g2 = g0 + signExtend(5, ((d2 >> 7) & 0x10) | ((d1 >> 9) & 0x0F)); 558 | b2 = b0 + signExtend(4, ((d2 << 3) & 0x08) | ((d1 >> 29))); 559 | 560 | r3 = r0 + signExtend(4, d2 >> 7); 561 | g3 = g0 + signExtend(5, ((d1 >> 4) & 0x10) | ((d1 >> 19) & 0x0F)); 562 | b3 = b0 + signExtend(4, ( 563 | ((d2 >> 9) & 8) | 564 | ((d2 >> 4) & 4) | 565 | ((d1 >> 27) & 2) | 566 | ((d2 >> 5) & 1) 567 | )); 568 | 569 | r0 = unq(11, r0); 570 | g0 = unq(11, g0); 571 | b0 = unq(11, b0); 572 | 573 | r1 = unq(11, r1 & 0x7FF); 574 | g1 = unq(11, g1 & 0x7FF); 575 | b1 = unq(11, b1 & 0x7FF); 576 | 577 | r2 = unq(11, r2 & 0x7FF); 578 | g2 = unq(11, g2 & 0x7FF); 579 | b2 = unq(11, b2 & 0x7FF); 580 | 581 | r3 = unq(11, r3 & 0x7FF); 582 | g3 = unq(11, g3 & 0x7FF); 583 | b3 = unq(11, b3 & 0x7FF); 584 | break; 585 | case 10: 586 | // rgb0 - 11 bits 587 | // r1, r2, r3 - 4 bits (delta) 588 | // g1, g2, g3 - 4 bits (delta) 589 | // b1, b2, b3 - 5 bits (delta) 590 | 591 | r0 = ((d1 << 3) & 0x400) | ((d0 >> 5) & 0x3FF); 592 | g0 = ((d1 >> 7) & 0x400) | ((d0 >> 15) & 0x3FF); 593 | b0 = ((d1 >> 18) & 0x400) | ((q0 >> 25) & 0x3FF); 594 | 595 | r1 = r0 + signExtend(4, d1 >> 3); 596 | g1 = g0 + signExtend(4, d1 >> 13); 597 | b1 = b0 + signExtend(5, d1 >> 23); 598 | 599 | r2 = r0 + signExtend(4, d2 >> 1); 600 | g2 = g0 + signExtend(4, d1 >> 9); 601 | b2 = b0 + signExtend(5, ((d1 >> 4) & 0x10) | ((d2 << 3) & 8) | (d1 >> 29)); 602 | 603 | r3 = r0 + signExtend(4, d2 >> 7); 604 | g3 = g0 + signExtend(4, d1 >> 19); 605 | b3 = b0 + signExtend(5, ( 606 | ((d2 >> 7) & 0x10) | 607 | ((d2 >> 9) & 0x08) | 608 | ((d2 >> 4) & 0x06) | 609 | ((d1 >> 18) & 0x01) 610 | )); 611 | 612 | r0 = unq(11, r0); 613 | g0 = unq(11, g0); 614 | b0 = unq(11, b0); 615 | 616 | r1 = unq(11, r1 & 0x7FF); 617 | g1 = unq(11, g1 & 0x7FF); 618 | b1 = unq(11, b1 & 0x7FF); 619 | 620 | r2 = unq(11, r2 & 0x7FF); 621 | g2 = unq(11, g2 & 0x7FF); 622 | b2 = unq(11, b2 & 0x7FF); 623 | 624 | r3 = unq(11, r3 & 0x7FF); 625 | g3 = unq(11, g3 & 0x7FF); 626 | b3 = unq(11, b3 & 0x7FF); 627 | break; 628 | case 14: 629 | // rgb0 - 9 bits 630 | // rgb1, rgb2, rgb3 - 5 bits (delta) 631 | 632 | r0 = (d0 >> 5) & 0x1FF; 633 | g0 = (d0 >> 15) & 0x1FF; 634 | b0 = ((q0 >> 25) & 0x1FF); 635 | 636 | r1 = r0 + signExtend(5, d1 >> 3); 637 | g1 = g0 + signExtend(5, d1 >> 13); 638 | b1 = b0 + signExtend(5, d1 >> 23); 639 | 640 | r2 = r0 + signExtend(5, d2 >> 1); 641 | g2 = g0 + signExtend(5, ((d0 >> 20) & 0x10) | ((d1 >> 9) & 0xF)); 642 | b2 = b0 + signExtend(5, ((d0 >> 10) & 0x10) | ((d2 << 3) & 8) | (d1 >> 29)); 643 | 644 | r3 = r0 + signExtend(5, d2 >> 7); 645 | g3 = g0 + signExtend(5, ((d1 >> 4) & 0x10) | ((d1 >> 19) & 0xF)); 646 | b3 = b0 + signExtend(5, ( 647 | ((d1 << 2) & 0x10) | 648 | ((d2 >> 9) & 0x08) | 649 | ((d2 >> 4) & 0x04) | 650 | ((d1 >> 27) & 0x02) | 651 | ((d1 >> 18) & 0x01) 652 | )); 653 | 654 | r0 = unq(9, r0); 655 | g0 = unq(9, g0); 656 | b0 = unq(9, b0); 657 | 658 | r1 = unq(9, r1 & 0x1FF); 659 | g1 = unq(9, g1 & 0x1FF); 660 | b1 = unq(9, b1 & 0x1FF); 661 | 662 | r2 = unq(9, r2 & 0x1FF); 663 | g2 = unq(9, g2 & 0x1FF); 664 | b2 = unq(9, b2 & 0x1FF); 665 | 666 | r3 = unq(9, r3 & 0x1FF); 667 | g3 = unq(9, g3 & 0x1FF); 668 | b3 = unq(9, b3 & 0x1FF); 669 | break; 670 | case 18: 671 | // rgb0 - 8 bits 672 | // r1, r2, r3 - 6 bits (delta) 673 | // g1, g2, g3 - 5 bits (delta) 674 | // b1, b2, b3 - 5 bits (delta) 675 | 676 | r0 = (d0 >> 5) & 0xFF; 677 | g0 = (d0 >> 15) & 0xFF; 678 | b0 = ((q0 >> 25) & 0xFF); 679 | 680 | r1 = r0 + signExtend(6, d1 >> 3); 681 | g1 = g0 + signExtend(5, d1 >> 13); 682 | b1 = b0 + signExtend(5, d1 >> 23); 683 | 684 | r2 = r0 + signExtend(6, d2 >> 1); 685 | g2 = g0 + signExtend(5, ((d0 >> 20) & 0x10) | ((d1 >> 9) & 0xF)); 686 | b2 = b0 + signExtend(5, ((d0 >> 10) & 0x10) | ((d2 << 3) & 8) | (d1 >> 29)); 687 | 688 | r3 = r0 + signExtend(6, d2 >> 7); 689 | g3 = g0 + signExtend(5, ((d0 >> 9) & 0x10) | ((d1 >> 19) & 0xF)); 690 | b3 = b0 + signExtend(5, ( 691 | ((d1 << 2) & 0x18) | 692 | ((d0 >> 21) & 0x04) | 693 | ((d1 >> 27) & 0x02) | 694 | ((d1 >> 18) & 0x01) 695 | )); 696 | 697 | r0 = unq(8, r0); 698 | g0 = unq(8, g0); 699 | b0 = unq(8, b0); 700 | 701 | r1 = unq(8, r1 & 0xFF); 702 | g1 = unq(8, g1 & 0xFF); 703 | b1 = unq(8, b1 & 0xFF); 704 | 705 | r2 = unq(8, r2 & 0xFF); 706 | g2 = unq(8, g2 & 0xFF); 707 | b2 = unq(8, b2 & 0xFF); 708 | 709 | r3 = unq(8, r3 & 0xFF); 710 | g3 = unq(8, g3 & 0xFF); 711 | b3 = unq(8, b3 & 0xFF); 712 | break; 713 | case 22: 714 | // rgb0 - 8 bits 715 | // r1, r2, r3 - 5 bits (delta) 716 | // g1, g2, g3 - 6 bits (delta) 717 | // b1, b2, b3 - 5 bits (delta) 718 | 719 | r0 = (d0 >> 5) & 0xFF; 720 | g0 = (d0 >> 15) & 0xFF; 721 | b0 = ((q0 >> 25) & 0xFF); 722 | 723 | r1 = r0 + signExtend(5, d1 >> 3); 724 | g1 = g0 + signExtend(6, d1 >> 13); 725 | b1 = b0 + signExtend(5, d1 >> 23); 726 | 727 | r2 = r0 + signExtend(5, d2 >> 1); 728 | g2 = g0 + signExtend(6, ( 729 | ((d0 >> 18) & 0x20) | 730 | ((d0 >> 20) & 0x10) | 731 | ((d1 >> 9) & 0xF) 732 | )); 733 | b2 = b0 + signExtend(5, ( 734 | ((d0 >> 10) & 0x10) | 735 | ((d2 << 3) & 0x08) | 736 | ((d1 >> 29)) 737 | )); 738 | 739 | r3 = r0 + signExtend(5, d2 >> 7); 740 | g3 = g0 + signExtend(6, ( 741 | ((d1 << 4) & 0x20) | 742 | ((d1 >> 4) & 0x10) | 743 | ((d1 >> 19) & 0x0F) 744 | )); 745 | b3 = b0 + signExtend(5, ( 746 | ((d1 << 2) & 0x10) | 747 | ((d2 >> 9) & 0x08) | 748 | ((d2 >> 4) & 0x04) | 749 | ((d1 >> 27) & 0x02) | 750 | ((d0 >> 13) & 0x01) 751 | )); 752 | 753 | r0 = unq(8, r0); 754 | g0 = unq(8, g0); 755 | b0 = unq(8, b0); 756 | 757 | r1 = unq(8, r1 & 0xFF); 758 | g1 = unq(8, g1 & 0xFF); 759 | b1 = unq(8, b1 & 0xFF); 760 | 761 | r2 = unq(8, r2 & 0xFF); 762 | g2 = unq(8, g2 & 0xFF); 763 | b2 = unq(8, b2 & 0xFF); 764 | 765 | r3 = unq(8, r3 & 0xFF); 766 | g3 = unq(8, g3 & 0xFF); 767 | b3 = unq(8, b3 & 0xFF); 768 | break; 769 | case 26: 770 | // rgb0 - 8 bits 771 | // r1, r2, r3 - 5 bits (delta) 772 | // g1, g2, g3 - 5 bits (delta) 773 | // b1, b2, b3 - 6 bits (delta) 774 | 775 | r0 = (d0 >> 5) & 0xFF; 776 | g0 = (d0 >> 15) & 0xFF; 777 | b0 = ((q0 >> 25) & 0xFF); 778 | 779 | r1 = r0 + signExtend(5, d1 >> 3); 780 | g1 = g0 + signExtend(5, d1 >> 13); 781 | b1 = b0 + signExtend(6, d1 >> 23); 782 | 783 | r2 = r0 + signExtend(5, d2 >> 1); 784 | g2 = g0 + signExtend(5, ((d0 >> 20) & 0x10) | ((d1 >> 9) & 0xF)); 785 | b2 = b0 + signExtend(6, ( 786 | ((d0 >> 18) & 0x20) | 787 | ((d0 >> 10) & 0x10) | 788 | ((d2 << 3) & 0x08) | 789 | ((d1 >> 29)) 790 | )); 791 | 792 | r3 = r0 + signExtend(5, d2 >> 7); 793 | g3 = g0 + signExtend(5, ((d1 >> 4) & 0x10) | ((d1 >> 19) & 0xF)); 794 | b3 = b0 + signExtend(6, ( 795 | ((d1 << 4) & 0x20) | 796 | ((d1 << 2) & 0x10) | 797 | ((d2 >> 9) & 0x08) | 798 | ((d2 >> 4) & 0x04) | 799 | ((d0 >> 12) & 0x02) | 800 | ((d1 >> 18) & 0x01) 801 | )); 802 | 803 | r0 = unq(8, r0); 804 | g0 = unq(8, g0); 805 | b0 = unq(8, b0); 806 | 807 | r1 = unq(8, r1 & 0xFF); 808 | g1 = unq(8, g1 & 0xFF); 809 | b1 = unq(8, b1 & 0xFF); 810 | 811 | r2 = unq(8, r2 & 0xFF); 812 | g2 = unq(8, g2 & 0xFF); 813 | b2 = unq(8, b2 & 0xFF); 814 | 815 | r3 = unq(8, r3 & 0xFF); 816 | g3 = unq(8, g3 & 0xFF); 817 | b3 = unq(8, b3 & 0xFF); 818 | break; 819 | case 30: 820 | // rgb0 - 6 bits 821 | // rgb1 - 6 bits 822 | // rgb2 - 6 bits 823 | // rgb3 - 6 bits 824 | 825 | r0 = (d0 >> 5) & 0x3F; 826 | g0 = (d0 >> 15) & 0x3F; 827 | b0 = (d0 >> 25) & 0x3F; 828 | 829 | r1 = (d1 >> 3) & 0x3F; 830 | g1 = (d1 >> 13) & 0x3F; 831 | b1 = (d1 >> 23) & 0x3F; 832 | 833 | r2 = (d2 >> 1) & 0x3F; 834 | g2 = ((d0 >> 16) & 0x20) | ((d0 >> 20) & 0x10) | ((d1 >> 9) & 0xF); 835 | b2 = ( 836 | ((d0 >> 17) & 0x20) | 837 | ((d0 >> 10) & 0x10) | 838 | ((d2 << 3) & 0x08) | 839 | ((d1 >> 29)) 840 | ); 841 | 842 | r3 = (d2 >> 7) & 0x3F; 843 | g3 = ((d0 >> 26) & 0x20) | ((d0 >> 7) & 0x10) | ((d1 >> 19) & 0xF); 844 | b3 = ( 845 | ((d1 << 4) & 0x20) | 846 | ((d1 << 2) & 0x10) | 847 | ((d1 << 3) & 0x08) | 848 | ((d0 >> 21) & 0x04) | 849 | ((d0 >> 12) & 0x03) 850 | ); 851 | 852 | r0 = unq(6, r0); 853 | g0 = unq(6, g0); 854 | b0 = unq(6, b0); 855 | 856 | r1 = unq(6, r1); 857 | g1 = unq(6, g1); 858 | b1 = unq(6, b1); 859 | 860 | r2 = unq(6, r2); 861 | g2 = unq(6, g2); 862 | b2 = unq(6, b2); 863 | 864 | r3 = unq(6, r3); 865 | g3 = unq(6, g3); 866 | b3 = unq(6, b3); 867 | break; 868 | } 869 | 870 | const patternIndex = (q1 >> 13) & 0x1F; 871 | 872 | const anchor = getAnchor(patternIndex) * 3; 873 | const hiMask = 0xFFFFFFFFFFFFFFFF << (anchor + 3); 874 | const loMask = (0xFFFFFFFFFFFFFFFF >> (65 - anchor)) << 3; 875 | let weights = ((q1 >> 16) & hiMask) | ((q1 >> 17) & loMask) | ((q1 >> 18) & 3); 876 | 877 | const rs0 = r0 * 64 + 32; 878 | const gs0 = g0 * 64 + 32; 879 | const bs0 = b0 * 64 + 32; 880 | const rs1 = r2 * 64 + 32; 881 | const gs1 = g2 * 64 + 32; 882 | const bs1 = b2 * 64 + 32; 883 | const rd0 = r1 - r0; 884 | const gd0 = g1 - g0; 885 | const bd0 = b1 - b0; 886 | const rd1 = r3 - r2; 887 | const gd1 = g3 - g2; 888 | const bd1 = b3 - b2; 889 | 890 | let pat = pattern(patternIndex); 891 | 892 | for (let i = 0; i < 4; i++) { 893 | if (kIsRGBA16F) { 894 | store(offset, lerp2(rs0, rs1, gs0, gs1, bs0, bs1, 895 | rd0, rd1, gd0, gd1, bd0, bd1, 896 | pat & 1, (weights & 7))); 897 | } else if (kIsRGB32F) { 898 | const w = weight3(weights & 7); 899 | if (pat & 1) { 900 | store(offset, lerpF32(rs1, rd1, w), 0); 901 | store(offset, lerpF32(gs1, gd1, w), 4); 902 | store(offset, lerpF32(bs1, bd1, w), 8); 903 | } else { 904 | store(offset, lerpF32(rs0, rd0, w), 0); 905 | store(offset, lerpF32(gs0, gd0, w), 4); 906 | store(offset, lerpF32(bs0, bd0, w), 8); 907 | } 908 | } else { 909 | store(offset, lerp2(rs0, rs1, gs0, gs1, bs0, bs1, 910 | rd0, rd1, gd0, gd1, bd0, bd1, 911 | pat & 1, (weights & 7))); 912 | } 913 | weights >>= 3; 914 | pat >>= 1; 915 | 916 | if (kIsRGBA16F) { 917 | store(offset, lerp2(rs0, rs1, gs0, gs1, bs0, bs1, 918 | rd0, rd1, gd0, gd1, bd0, bd1, 919 | pat & 1, (weights & 7)), 920 | kPixelLength); 921 | } else if (kIsRGB32F) { 922 | const w = weight3(weights & 7); 923 | if (pat & 1) { 924 | store(offset, lerpF32(rs1, rd1, w), kPixelLength + 0); 925 | store(offset, lerpF32(gs1, gd1, w), kPixelLength + 4); 926 | store(offset, lerpF32(bs1, bd1, w), kPixelLength + 8); 927 | } else { 928 | store(offset, lerpF32(rs0, rd0, w), kPixelLength + 0); 929 | store(offset, lerpF32(gs0, gd0, w), kPixelLength + 4); 930 | store(offset, lerpF32(bs0, bd0, w), kPixelLength + 8); 931 | } 932 | } else { 933 | store(offset, lerp2(rs0, rs1, gs0, gs1, bs0, bs1, 934 | rd0, rd1, gd0, gd1, bd0, bd1, 935 | pat & 1, (weights & 7)), 936 | kPixelLength); 937 | } 938 | weights >>= 3; 939 | pat >>= 1; 940 | 941 | if (kIsRGBA16F) { 942 | store(offset, lerp2(rs0, rs1, gs0, gs1, bs0, bs1, 943 | rd0, rd1, gd0, gd1, bd0, bd1, 944 | pat & 1, (weights & 7)), 945 | kPixelLength * 2); 946 | } else if (kIsRGB32F) { 947 | const w = weight3(weights & 7); 948 | if (pat & 1) { 949 | store(offset, lerpF32(rs1, rd1, w), kPixelLength * 2 + 0); 950 | store(offset, lerpF32(gs1, gd1, w), kPixelLength * 2 + 4); 951 | store(offset, lerpF32(bs1, bd1, w), kPixelLength * 2 + 8); 952 | } else { 953 | store(offset, lerpF32(rs0, rd0, w), kPixelLength * 2 + 0); 954 | store(offset, lerpF32(gs0, gd0, w), kPixelLength * 2 + 4); 955 | store(offset, lerpF32(bs0, bd0, w), kPixelLength * 2 + 8); 956 | } 957 | } else { 958 | store(offset, lerp2(rs0, rs1, gs0, gs1, bs0, bs1, 959 | rd0, rd1, gd0, gd1, bd0, bd1, 960 | pat & 1, (weights & 7)), 961 | kPixelLength * 2); 962 | } 963 | weights >>= 3; 964 | pat >>= 1; 965 | 966 | if (kIsRGBA16F) { 967 | store(offset, lerp2(rs0, rs1, gs0, gs1, bs0, bs1, 968 | rd0, rd1, gd0, gd1, bd0, bd1, 969 | pat & 1, (weights & 7)), 970 | kPixelLength * 3); 971 | } else if (kIsRGB32F) { 972 | const w = weight3(weights & 7); 973 | if (pat & 1) { 974 | store(offset, lerpF32(rs1, rd1, w), kPixelLength * 3 + 0); 975 | store(offset, lerpF32(gs1, gd1, w), kPixelLength * 3 + 4); 976 | store(offset, lerpF32(bs1, bd1, w), kPixelLength * 3 + 8); 977 | } else { 978 | store(offset, lerpF32(rs0, rd0, w), kPixelLength * 3 + 0); 979 | store(offset, lerpF32(gs0, gd0, w), kPixelLength * 3 + 4); 980 | store(offset, lerpF32(bs0, bd0, w), kPixelLength * 3 + 8); 981 | } 982 | } else { 983 | store(offset, lerp2(rs0, rs1, gs0, gs1, bs0, bs1, 984 | rd0, rd1, gd0, gd1, bd0, bd1, 985 | pat & 1, (weights & 7)), 986 | kPixelLength * 3); 987 | } 988 | weights >>= 3; 989 | pat >>= 1; 990 | 991 | offset += stride; 992 | } 993 | 994 | } 995 | 996 | /** 997 | * Linear interpolation and subsequent final unquantization. 998 | */ 999 | // @ts-ignore 1000 | @inline 1001 | function lerp1( 1002 | rs0: u32, gs0: u32, bs0: u32, 1003 | rd0: u32, gd0: u32, bd0: u32, 1004 | w: u32 1005 | ): T { 1006 | w = weight4(w); 1007 | const r = (rs0 + w * rd0) >> 6; 1008 | const g = (gs0 + w * gd0) >> 6; 1009 | const b = (bs0 + w * bd0) >> 6; 1010 | if (kIsRGBA16F) { 1011 | return unquantizeRGBA16F(r, g, b) as T; 1012 | } 1013 | if (kIsRGB9E5) { 1014 | return unquantizeRGB9E5(r, g, b) as T; 1015 | } 1016 | if (kIsRoundRG11FB10F) { 1017 | return unquantizeRoundRG11FB10F(r, g, b) as T; 1018 | } 1019 | if (kIsTruncRG11FB10F) { 1020 | return unquantizeTruncRG11FB10F(r, g, b) as T; 1021 | } 1022 | if (kIsRoundNoClampRG11FB10F) { 1023 | return unquantizeRoundUnsafeRG11FB10F(r, g, b) as T; 1024 | } 1025 | return unreachable(); 1026 | } 1027 | 1028 | /** 1029 | * Partitioned linear interpolation and subsequent final unquantization. 1030 | */ 1031 | // @ts-ignore 1032 | @inline 1033 | function lerp2( 1034 | rs0: u32, rs1: u32, 1035 | gs0: u32, gs1: u32, 1036 | bs0: u32, bs1: u32, 1037 | rd0: u32, rd1: u32, 1038 | gd0: u32, gd1: u32, 1039 | bd0: u32, bd1: u32, 1040 | p: u32, w: u32 1041 | ): T { 1042 | let r: u32 = 0; 1043 | let g: u32 = 0; 1044 | let b: u32 = 0; 1045 | w = weight3(w); 1046 | if (p) { 1047 | r = (rs1 + w * rd1) >> 6; 1048 | g = (gs1 + w * gd1) >> 6; 1049 | b = (bs1 + w * bd1) >> 6; 1050 | } else { 1051 | r = (rs0 + w * rd0) >> 6; 1052 | g = (gs0 + w * gd0) >> 6; 1053 | b = (bs0 + w * bd0) >> 6; 1054 | } 1055 | if (kIsRGBA16F) { 1056 | return unquantizeRGBA16F(r, g, b) as T; 1057 | } 1058 | if (kIsRGB9E5) { 1059 | return unquantizeRGB9E5(r, g, b) as T; 1060 | } 1061 | if (kIsRoundRG11FB10F) { 1062 | return unquantizeRoundRG11FB10F(r, g, b) as T; 1063 | } 1064 | if (kIsTruncRG11FB10F) { 1065 | return unquantizeTruncRG11FB10F(r, g, b) as T; 1066 | } 1067 | if (kIsRoundNoClampRG11FB10F) { 1068 | return unquantizeRoundUnsafeRG11FB10F(r, g, b) as T; 1069 | } 1070 | return unreachable(); 1071 | } 1072 | 1073 | /** 1074 | * Load interpolation weight for a 4-bit index. 1075 | */ 1076 | // @ts-ignore 1077 | @inline 1078 | function weight4(i: u32): u32 { 1079 | return load(i); 1080 | } 1081 | 1082 | /** 1083 | * Load interpolation weight for a 3-bit index. 1084 | */ 1085 | // @ts-ignore 1086 | @inline 1087 | function weight3(i: u32): u32 { 1088 | return load(i, 16); 1089 | } 1090 | 1091 | /** 1092 | * Load 16-bit pattern for a 5-bit index. 1093 | */ 1094 | // @ts-ignore 1095 | @inline 1096 | function pattern(p: u32): u32 { 1097 | return load(p << 1, 32); 1098 | } 1099 | 1100 | /** 1101 | * Load reversed 6-bit value. 1102 | */ 1103 | // @ts-ignore 1104 | @inline 1105 | function reverse6(n: u32): u32 { 1106 | return load(n, 96); 1107 | } 1108 | 1109 | /** 1110 | * Load unquantized value. 1111 | */ 1112 | // @ts-ignore 1113 | @inline 1114 | function unq(width: i32, value: u32): u32 { 1115 | const base = 160; 1116 | switch (width) { 1117 | case 6: return load(value * 2, base); 1118 | case 7: return load(value * 2, base + 128 * ((1 << 1) - 1)); 1119 | case 8: return load(value * 2, base + 128 * ((1 << 2) - 1)); 1120 | case 9: return load(value * 2, base + 128 * ((1 << 3) - 1)); 1121 | case 10: return load(value * 2, base + 128 * ((1 << 4) - 1)); 1122 | case 11: return load(value * 2, base + 128 * ((1 << 5) - 1)); 1123 | case 12: return load(value * 2, base + 128 * ((1 << 6) - 1)); 1124 | default: return unreachable(); 1125 | } 1126 | } 1127 | 1128 | /** 1129 | * Sign-extend w-bit value. 1130 | */ 1131 | // @ts-ignore 1132 | @inline 1133 | function signExtend(w: u32, value: i32): i32 { 1134 | const s = 32 - w; 1135 | return (value << s) >> s; 1136 | } 1137 | 1138 | /** 1139 | * Get anchor index for a given pattern index. 1140 | */ 1141 | // @ts-ignore 1142 | @inline 1143 | function getAnchor(p: u32): u32 { 1144 | if (p < 16) return 15; 1145 | return (0x22882282F882282F >> ((p & 0xF) << 2)) & 0xF; 1146 | } 1147 | 1148 | /** 1149 | * Scale to 15 bits and pack to 64 bits. 1150 | */ 1151 | // @ts-ignore 1152 | @inline 1153 | function unquantizeRGBA16F(r: u32, g: u32, b: u32): u64 { 1154 | const ru: u64 = (r * 31) >> 6; 1155 | const gu: u64 = (g * 31) >> 6; 1156 | const bu: u64 = (b * 31) >> 6; 1157 | return 0x3C00000000000000 | (bu << 32) | (gu << 16) | ru; 1158 | } 1159 | 1160 | /** 1161 | * Downscale float16 values to 9 bits with a shared 5-bit exponent. 1162 | */ 1163 | // @ts-ignore 1164 | @inline 1165 | function unquantizeRGB9E5(r: u32, g: u32, b: u32): u32 { 1166 | // Regular BC6H_U 16-to-15 unquantization. After it, 1167 | // half-float reinterpretations are guaranteed to be in [0.0, 65504.0] 1168 | // thus implicitly avoiding NaNs and Inf. 1169 | r = (r * 31) >> 6; 1170 | g = (g * 31) >> 6; 1171 | b = (b * 31) >> 6; 1172 | 1173 | // Extract exponents from the higher 5 bits. 1174 | const rExp = r >> 10; 1175 | const gExp = g >> 10; 1176 | const bExp = b >> 10; 1177 | 1178 | // Extract mantissas from the lower 10 bits. 1179 | // As per the float16 spec, add 1024 when the exponent is not zero. 1180 | const rM = (r & 0x3FF) | (rExp ? 0x400 : 0); 1181 | const gM = (g & 0x3FF) | (gExp ? 0x400 : 0); 1182 | const bM = (b & 0x3FF) | (bExp ? 0x400 : 0); 1183 | 1184 | if (rExp | gExp | bExp) { 1185 | // When at least one of RGB exponents is not zero, 1186 | // use the largest one +1 (to even the multiplier) for the packed value. 1187 | const sharedExp = 1 + max(rExp, max(gExp, bExp)); 1188 | 1189 | // Translate the exponent differences into 1190 | // the number of bits to truncate the mantissas by. 1191 | // TODO: Evaluate accuracy and speed with rounding semantics. 1192 | const rShift = 1 + sharedExp - (rExp ? rExp : 1); 1193 | const gShift = 1 + sharedExp - (gExp ? gExp : 1); 1194 | const bShift = 1 + sharedExp - (bExp ? bExp : 1); 1195 | return (sharedExp << 27) | 1196 | (bM >> bShift << 18) | 1197 | (gM >> gShift << 9) | 1198 | (rM >> rShift); 1199 | } else { 1200 | // All three values are float16 denorms. If all mantissas belong 1201 | // to 0..511 range, they could be copied directly with zero exponent. 1202 | // Otherwise, truncate to 9 bits and set the shared exponent to 1. 1203 | const msb = ((rM | gM | bM) >> 9) & 1; 1204 | return (msb << 27) | 1205 | (bM >> msb << 18) | 1206 | (gM >> msb << 9) | 1207 | (rM >> msb); 1208 | } 1209 | } 1210 | 1211 | /** 1212 | * Clamp & scale to 10-11 bits with rounding and pack to 32 bits. 1213 | */ 1214 | // @ts-ignore 1215 | @inline 1216 | function unquantizeRoundRG11FB10F(r: u32, g: u32, b: u32): u32 { 1217 | // Clamp inputs to avoid 10- or 11-bit infinity values 1218 | r = min(r, 65519); 1219 | g = min(g, 65519); 1220 | b = min(b, 65502); 1221 | 1222 | // Rescale floating-point representations afterwards 1223 | r = (r * 0x001F + 0x000200) >> 10; 1224 | g = (g * 0x003E + 0x000400) & 0x003FF800; 1225 | b = (b * 0xF800 + 0x200000) & 0xFFC00000; 1226 | return b | g | r; 1227 | } 1228 | 1229 | /** 1230 | * Scale to 10-11 bits with truncation and pack to 32 bits. 1231 | */ 1232 | // @ts-ignore 1233 | @inline 1234 | function unquantizeTruncRG11FB10F(r: u32, g: u32, b: u32): u32 { 1235 | // Copy exponents and trunc mantissas 1236 | r = (r * 0x001F) >> 10; 1237 | g = (g * 0x003E) & 0x003FF800; 1238 | b = (b * 0xF800) & 0xFFC00000; 1239 | return b | g | r; 1240 | } 1241 | 1242 | /** 1243 | * Scale to 10-11 bits with rounding and pack to 32 bits. 1244 | * Values greater than finite maximums for 10- and 11-bit 1245 | * floats will be set to Infinity. 1246 | */ 1247 | // @ts-ignore 1248 | @inline 1249 | function unquantizeRoundUnsafeRG11FB10F(r: u32, g: u32, b: u32): u32 { 1250 | r = (r * 0x001F + 0x000200) >> 10; 1251 | g = (g * 0x003E + 0x000400) & 0x003FF800; 1252 | b = (b * 0xF800 + 0x200000) & 0xFFC00000; 1253 | return b | g | r; 1254 | } 1255 | 1256 | /** 1257 | * Apply BC6H_U interpolation, final unquantization, 1258 | * and convert the result to a single-precision float. 1259 | */ 1260 | // @ts-ignore 1261 | @inline 1262 | function lerpF32(cs0: u32, cd0: u32, w: u32): u32 { 1263 | const c = (((cs0 + w * cd0) >> 6) * 31) >> 6; 1264 | 1265 | if (c == 0) return 0; 1266 | 1267 | // Normalized 1268 | if (c > 1023) return (c << 13) + 0x38000000; 1269 | 1270 | // Half-float denorms 1271 | const clzC = clz(c); 1272 | return ((c << (clzC - 8)) ^ 0x800000) + 0x43000000 - (clzC << 23); 1273 | } 1274 | --------------------------------------------------------------------------------