@@ -19,8 +19,9 @@ class BitcastNode extends TempNode {
1919 *
2020 * @param {Node } valueNode - The value to convert.
2121 * @param {string } conversionType - The type to convert to.
22+ * @param {?string } [inputType = null] - The expected input data type of the bitcast operation.
2223 */
23- constructor ( valueNode , conversionType ) {
24+ constructor ( valueNode , conversionType , inputType = null ) {
2425
2526 super ( ) ;
2627
@@ -35,10 +36,19 @@ class BitcastNode extends TempNode {
3536 * The type the value will be converted to.
3637 *
3738 * @type {string }
38- * @default null
3939 */
4040 this . conversionType = conversionType ;
4141
42+
43+ /**
44+ * The expected input data type of the bitcast operation.
45+ *
46+ *
47+ * @type {string }
48+ * @default null
49+ */
50+ this . inputType = inputType ;
51+
4252 /**
4353 * This flag can be used for type testing.
4454 *
@@ -50,12 +60,17 @@ class BitcastNode extends TempNode {
5060
5161 }
5262
53- /**
54- * The node's type is defined by the conversion type.
55- *
56- * @return {string } The node type.
57- */
58- getNodeType ( ) {
63+ getNodeType ( builder ) {
64+
65+ // GLSL aliasing
66+ if ( this . inputType !== null ) {
67+
68+ const valueType = this . valueNode . getNodeType ( builder ) ;
69+ const valueLength = builder . getTypeLength ( valueType ) ;
70+
71+ return builder . getTypeFromLength ( valueLength , this . conversionType ) ;
72+
73+ }
5974
6075 return this . conversionType ;
6176
@@ -65,9 +80,22 @@ class BitcastNode extends TempNode {
6580 generate ( builder ) {
6681
6782 const type = this . getNodeType ( builder ) ;
68- const inputType = this . valueNode . getNodeType ( builder ) ;
83+ let inputType = '' ;
84+
85+ if ( this . inputType !== null ) {
86+
87+ const valueType = this . valueNode . getNodeType ( builder ) ;
88+ const valueTypeLength = builder . getTypeLength ( valueType ) ;
6989
70- return `${ builder . getBitcastMethod ( type , inputType ) } ( ${ this . valueNode . build ( builder , inputType ) } )` ;
90+ inputType = valueTypeLength === 1 ? this . inputType : builder . changeComponentType ( valueType , this . inputType ) ;
91+
92+ } else {
93+
94+ inputType = this . valueNode . getNodeType ( builder ) ;
95+
96+ }
97+
98+ return `${ builder . getBitcastMethod ( type , inputType ) } ( ${ this . valueNode . build ( builder , inputType ) } )` ;
7199
72100
73101 }
@@ -86,3 +114,43 @@ export default BitcastNode;
86114 * @returns {Node }
87115 */
88116export const bitcast = /*@__PURE__ */ nodeProxyIntent ( BitcastNode ) . setParameterLength ( 2 ) ;
117+
118+ /**
119+ * Bitcasts a float or a vector of floats to a corresponding integer type with the same element size.
120+ *
121+ * @tsl
122+ * @function
123+ * @param {Node<float> } value - The float or vector of floats to bitcast.
124+ * @returns {BitcastNode }
125+ */
126+ export const floatBitsToInt = ( value ) => new BitcastNode ( value , 'int' , 'float' ) ;
127+
128+ /**
129+ * Bitcasts a float or a vector of floats to a corresponding unsigned integer type with the same element size.
130+ *
131+ * @tsl
132+ * @function
133+ * @param {Node<float> } value - The float or vector of floats to bitcast.
134+ * @returns {BitcastNode }
135+ */
136+ export const floatBitsToUint = ( value ) => new BitcastNode ( value , 'uint' , 'float' ) ;
137+
138+ /**
139+ * Bitcasts an integer or a vector of integers to a corresponding float type with the same element size.
140+ *
141+ * @tsl
142+ * @function
143+ * @param {Node<int> } value - The integer or vector of integers to bitcast.
144+ * @returns {BitcastNode }
145+ */
146+ export const intBitsToFloat = ( value ) => new BitcastNode ( value , 'float' , 'int' ) ;
147+
148+ /**
149+ * Bitcast an unsigned integer or a vector of unsigned integers to a corresponding float type with the same element size.
150+ *
151+ * @tsl
152+ * @function
153+ * @param {Node<uint> } value - The unsigned integer or vector of unsigned integers to bitcast.
154+ * @returns {BitcastNode }
155+ */
156+ export const uintBitsToFloat = ( value ) => new BitcastNode ( value , 'float' , 'uint' ) ;
0 commit comments