|
| 1 | +import TempNode from '../core/TempNode.js'; |
| 2 | +import { nodeProxyIntent } from '../tsl/TSLCore.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * This node represents an operation that packs floating-point values of a vector into an unsigned 32-bit integer |
| 6 | + * |
| 7 | + * @augments TempNode |
| 8 | + */ |
| 9 | +class PackFloatNode extends TempNode { |
| 10 | + |
| 11 | + static get type() { |
| 12 | + |
| 13 | + return 'PackFloatNode'; |
| 14 | + |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * |
| 19 | + * @param {'snorm' | 'unorm' | 'float16'} encoding - The numeric encoding that describes how the float values are mapped to the integer range. |
| 20 | + * @param {Node} vectorNode - The vector node to be packed |
| 21 | + */ |
| 22 | + constructor( encoding, vectorNode ) { |
| 23 | + |
| 24 | + super(); |
| 25 | + |
| 26 | + /** |
| 27 | + * The vector to be packed. |
| 28 | + * |
| 29 | + * @type {Node} |
| 30 | + */ |
| 31 | + this.vectorNode = vectorNode; |
| 32 | + |
| 33 | + /** |
| 34 | + * The numeric encoding. |
| 35 | + * |
| 36 | + * @type {string} |
| 37 | + */ |
| 38 | + this.encoding = encoding; |
| 39 | + |
| 40 | + /** |
| 41 | + * This flag can be used for type testing. |
| 42 | + * |
| 43 | + * @type {boolean} |
| 44 | + * @readonly |
| 45 | + * @default true |
| 46 | + */ |
| 47 | + this.isPackFloatNode = true; |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + getNodeType() { |
| 52 | + |
| 53 | + return 'uint'; |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + generate( builder ) { |
| 58 | + |
| 59 | + const inputType = this.vectorNode.getNodeType( builder ); |
| 60 | + return `${ builder.getFloatPackingMethod( this.encoding ) }(${ this.vectorNode.build( builder, inputType )})`; |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +export default PackFloatNode; |
| 67 | + |
| 68 | +/** |
| 69 | + * Converts each component of the normalized float to 16-bit integer values. The results are packed into a single unsigned integer. |
| 70 | + * round(clamp(c, -1, +1) * 32767.0) |
| 71 | + * |
| 72 | + * @tsl |
| 73 | + * @function |
| 74 | + * @param {Node<vec2>} value - The 2-component vector to be packed |
| 75 | + * @returns {Node} |
| 76 | + */ |
| 77 | +export const packSnorm2x16 = /*@__PURE__*/ nodeProxyIntent( PackFloatNode, 'snorm' ).setParameterLength( 1 ); |
| 78 | + |
| 79 | +/** |
| 80 | + * Converts each component of the normalized float to 16-bit integer values. The results are packed into a single unsigned integer. |
| 81 | + * round(clamp(c, 0, +1) * 65535.0) |
| 82 | + * |
| 83 | + * @tsl |
| 84 | + * @function |
| 85 | + * @param {Node<vec2>} value - The 2-component vector to be packed |
| 86 | + * @returns {Node} |
| 87 | + */ |
| 88 | +export const packUnorm2x16 = /*@__PURE__*/ nodeProxyIntent( PackFloatNode, 'unorm' ).setParameterLength( 1 ); |
| 89 | + |
| 90 | +/** |
| 91 | + * Converts each component of the vec2 to 16-bit floating-point values. The results are packed into a single unsigned integer. |
| 92 | + * |
| 93 | + * @tsl |
| 94 | + * @function |
| 95 | + * @param {Node<vec2>} value - The 2-component vector to be packed |
| 96 | + * @returns {Node} |
| 97 | + */ |
| 98 | +export const packHalf2x16 = /*@__PURE__*/ nodeProxyIntent( PackFloatNode, 'float16' ).setParameterLength( 1 ); |
0 commit comments