Skip to content

Commit 0c66c79

Browse files
committed
work on src
1 parent 7dea976 commit 0c66c79

File tree

4 files changed

+235
-134
lines changed

4 files changed

+235
-134
lines changed

src/nodes/TSL.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export * from './core/MRTNode.js';
2020

2121
// math
2222
export * from './math/BitcastNode.js';
23+
export * from './math/BitcountNode.js';
2324
export * from './math/Hash.js';
2425
export * from './math/MathUtils.js';
2526
export * from './math/TriNoise3D.js';

src/nodes/math/BitcountNode.js

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
import TempNode from '../core/TempNode.js';
2+
import { addMethodChaining, Fn, nodeProxyIntent, uint } from '../tsl/TSLCore.js';
3+
import { bitcast } from './BitcastNode.js';
4+
import MathNode from './MathNode.js';
5+
/**
6+
* This node represents an operation that reinterprets the bit representation of a value
7+
* in one type as a value in another type.
8+
*
9+
* @augments TempNode
10+
*/
11+
class BitcountNode extends MathNode {
12+
13+
static get type() {
14+
15+
return 'BitcountNode';
16+
17+
}
18+
19+
/**
20+
* Constructs a new math node.
21+
*
22+
* @param {string} method - The method name.
23+
* @param {Node} aNode - The first input.
24+
*/
25+
constructor( method, aNode ) {
26+
27+
super( method, aNode );
28+
29+
/**
30+
* This flag can be used for type testing.
31+
*
32+
* @type {boolean}
33+
* @readonly
34+
* @default true
35+
*/
36+
this.isBitcountNode = true;
37+
38+
}
39+
40+
getNodeType( /*builder*/ ) {
41+
42+
return 'uint';
43+
44+
}
45+
46+
/**
47+
* Constructs a new math node.
48+
*
49+
* @param {NodeBuilder} builder - The method name.
50+
* @return {?Node} The output node.
51+
*/
52+
setup( builder ) {
53+
54+
const { method, aNode } = this;
55+
56+
const { renderer } = builder;
57+
58+
if ( renderer.backend.isWebGPUBackend ) {
59+
60+
return super.setup( builder );
61+
62+
}
63+
64+
const inputType = this.getInputType();
65+
const elementType = builder.getElementType( inputType );
66+
67+
const typeLength = builder.getTypeLength();
68+
69+
console.log( inputType, elementType, typeLength );
70+
71+
72+
if ( method === BitcountNode.COUNT_LEADING_ZEROS ) {
73+
74+
75+
76+
77+
} else if ( method === BitcountNode.COUNT_TRAILING_ZEROS ) {
78+
79+
80+
81+
} else if ( method === BitcountNode.COUNT_ONE_BITS ) {
82+
83+
const bitCountBase = Fn( ( [ value ] ) => {
84+
85+
const v = uint( 0.0 );
86+
87+
if ( elementType === 'int' ) {
88+
89+
v.assign( bitcast( value, 'uint' ) );
90+
91+
}
92+
93+
v.assign( v.sub( v.shiftRight( uint( 1 ) ).bitAnd( uint( 0x55555555 ) ) ) );
94+
v.assign( v.bitAnd( uint( 0x33333333 ) ).add( v.shiftRight( uint( 2 ) ).bitAnd( uint( 0x33333333 ) ) ) );
95+
96+
return v.add( v.shiftRight( uint( 4 ) ) ).bitAnd( uint( 0xF0F0F0F ) ).mul( uint( 0x1010101 ) ).shiftRight( uint( 24 ) );
97+
98+
} ).setLayout( {
99+
name: `bitcount_${elementType}`,
100+
type: 'uint',
101+
inputs: [
102+
{ name: 'value', type: elementType }
103+
]
104+
} );
105+
106+
107+
const bitCountFn = Fn( ( [ value ] ) => {
108+
109+
const v = uint( 0.0 );
110+
111+
if ( typeLength === 1 ) {
112+
113+
v.addAssign( bitCountBase( value ) );
114+
115+
} else {
116+
117+
const components = [ 'x', 'y', 'z', 'w' ];
118+
119+
for ( let i = 0; i < typeLength; i ++ ) {
120+
121+
const component = components[ i ];
122+
123+
v.addAssign( bitCountBase( value[ component ] ) );
124+
125+
}
126+
127+
}
128+
129+
return v;
130+
131+
} ).setLayout( {
132+
name: `bitcount_main_${this.nodeType}`,
133+
type: 'uint',
134+
inputs: [
135+
{ name: 'value', type: inputType }
136+
]
137+
} );
138+
139+
console.log( 'test' );
140+
141+
const exec = bitCountFn( aNode ).toVar( 'testVar' );
142+
143+
return exec;
144+
145+
}
146+
147+
}
148+
149+
generate( builder ) {
150+
151+
this.method += 'test';
152+
153+
super.generate( builder );
154+
155+
156+
157+
}
158+
159+
}
160+
161+
export default BitcountNode;
162+
163+
BitcountNode.COUNT_TRAILING_ZEROS = 'countTrailingZeros';
164+
BitcountNode.COUNT_LEADING_ZEROS = 'countLeadingZeros';
165+
BitcountNode.COUNT_ONE_BITS = 'countOneBits';
166+
167+
/**
168+
* Finds the number of consecutive 0 bits from the least significant bit of the input value,
169+
* which is also the index of the least significant bit of the input value.
170+
*
171+
* Can only be used with {@link WebGPURenderer} and a WebGPU backend.
172+
*
173+
* @tsl
174+
* @function
175+
* @param {Node | number} x - The input value.
176+
* @returns {Node}
177+
*/
178+
export const countTrailingZeros = /*@__PURE__*/ nodeProxyIntent( BitcountNode, BitcountNode.COUNT_TRAILING_ZEROS ).setParameterLength( 1 );
179+
180+
/**
181+
* Finds the number of consecutive 0 bits starting from the most significant bit of the input value.
182+
*
183+
* Can only be used with {@link WebGPURenderer} and a WebGPU backend.
184+
*
185+
* @tsl
186+
* @function
187+
* @param {Node | number} x - The input value.
188+
* @returns {Node}
189+
*/
190+
export const countLeadingZeros = /*@__PURE__*/ nodeProxyIntent( BitcountNode, BitcountNode.COUNT_LEADING_ZEROS ).setParameterLength( 1 );
191+
192+
/**
193+
* Finds the number of '1' bits set in the input value
194+
*
195+
* Can only be used with {@link WebGPURenderer} and a WebGPU backend.
196+
*
197+
* @tsl
198+
* @function
199+
* @returns {Node}
200+
*/
201+
export const countOneBits = /*@__PURE__*/ nodeProxyIntent( BitcountNode, BitcountNode.COUNT_ONE_BITS ).setParameterLength( 1 );
202+
203+
// GLSL alias function
204+
205+
export const findLSB = countTrailingZeros;
206+
export const findMSB = countLeadingZeros;
207+
export const bitCount = countOneBits;
208+
209+
addMethodChaining( 'countTrailingZeros', countTrailingZeros );
210+
addMethodChaining( 'countLeadingZeros', countLeadingZeros );
211+
addMethodChaining( 'countOneBits', countOneBits );

src/nodes/math/MathNode.js

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ class MathNode extends TempNode {
217217

218218
generate( builder, output ) {
219219

220+
console.log( this.method );
221+
220222
const properties = builder.getNodeProperties( this );
221223

222224
if ( properties.outputNode ) {
@@ -364,9 +366,6 @@ MathNode.FWIDTH = 'fwidth';
364366
MathNode.TRANSPOSE = 'transpose';
365367
MathNode.DETERMINANT = 'determinant';
366368
MathNode.INVERSE = 'inverse';
367-
MathNode.COUNT_TRAILING_ZEROS = 'countTrailingZeros';
368-
MathNode.COUNT_LEADING_ZEROS = 'countLeadingZeros';
369-
MathNode.COUNT_ONE_BITS = 'countOneBits';
370369

371370
// 2 inputs
372371

@@ -1103,49 +1102,7 @@ export const atan2 = ( y, x ) => { // @deprecated, r172
11031102
};
11041103

11051104

1106-
/**
1107-
* Finds the number of consecutive 0 bits from the least significant bit of the input value,
1108-
* which is also the index of the least significant bit of the input value.
1109-
*
1110-
* Can only be used with {@link WebGPURenderer} and a WebGPU backend.
1111-
*
1112-
* @tsl
1113-
* @function
1114-
* @param {Node | number} x - The input value.
1115-
* @returns {Node}
1116-
*/
1117-
export const countTrailingZeros = /*@__PURE__*/ nodeProxyIntent( MathNode, MathNode.COUNT_TRAILING_ZEROS ).setParameterLength( 1 );
1118-
1119-
/**
1120-
* Finds the number of consecutive 0 bits starting from the most significant bit of the input value.
1121-
*
1122-
* Can only be used with {@link WebGPURenderer} and a WebGPU backend.
1123-
*
1124-
* @tsl
1125-
* @function
1126-
* @param {Node | number} x - The input value.
1127-
* @returns {Node}
1128-
*/
1129-
export const countLeadingZeros = /*@__PURE__*/ nodeProxyIntent( MathNode, MathNode.COUNT_LEADING_ZEROS ).setParameterLength( 1 );
1130-
1131-
/**
1132-
* Finds the number of '1' bits set in the input value
1133-
*
1134-
* Can only be used with {@link WebGPURenderer} and a WebGPU backend.
1135-
*
1136-
* @tsl
1137-
* @function
1138-
* @returns {Node}
1139-
*/
1140-
export const countOneBits = /*@__PURE__*/ nodeProxyIntent( MathNode, MathNode.COUNT_ONE_BITS ).setParameterLength( 1 );
1141-
1142-
// GLSL alias function
11431105

1144-
export const faceforward = faceForward;
1145-
export const inversesqrt = inverseSqrt;
1146-
export const findLSB = countTrailingZeros;
1147-
export const findMSB = countLeadingZeros;
1148-
export const bitCount = countOneBits;
11491106

11501107
// Method chaining
11511108

@@ -1208,6 +1165,3 @@ addMethodChaining( 'transpose', transpose );
12081165
addMethodChaining( 'determinant', determinant );
12091166
addMethodChaining( 'inverse', inverse );
12101167
addMethodChaining( 'rand', rand );
1211-
addMethodChaining( 'countTrailingZeros', countTrailingZeros );
1212-
addMethodChaining( 'countLeadingZeros', countLeadingZeros );
1213-
addMethodChaining( 'countOneBits', countOneBits );

0 commit comments

Comments
 (0)