1
+ import { dualImpl } from '../core/function/dualImpl.ts' ;
1
2
import { stitch } from '../core/resolve/stitch.ts' ;
2
- import { isSnippetNumeric , snip } from '../data/snippet .ts' ;
3
+ import type { AnyData } from '../data/dataTypes .ts' ;
3
4
import { bool , f32 } from '../data/numeric.ts' ;
5
+ import { isSnippetNumeric , snip } from '../data/snippet.ts' ;
4
6
import { vec2b , vec3b , vec4b } from '../data/vector.ts' ;
5
7
import { VectorOps } from '../data/vectorOps.ts' ;
6
8
import {
@@ -16,10 +18,8 @@ import {
16
18
type v3b ,
17
19
type v4b ,
18
20
} from '../data/wgslTypes.ts' ;
19
- import { dualImpl } from '../core/function/dualImpl.ts' ;
20
- import { sub } from './operators.ts' ;
21
- import type { AnyData } from '../data/dataTypes.ts' ;
22
21
import { $internal } from '../shared/symbols.ts' ;
22
+ import { sub } from './operators.ts' ;
23
23
24
24
function correspondingBooleanVectorSchema ( dataType : AnyData ) {
25
25
if ( dataType . type . includes ( '2' ) ) {
@@ -270,7 +270,7 @@ export const isCloseTo = dualImpl({
270
270
lhs : T ,
271
271
rhs : T ,
272
272
precision = 0.01 ,
273
- ) => {
273
+ ) : boolean => {
274
274
if ( typeof lhs === 'number' && typeof rhs === 'number' ) {
275
275
return Math . abs ( lhs - rhs ) < precision ;
276
276
}
@@ -296,16 +296,31 @@ export const isCloseTo = dualImpl({
296
296
} ,
297
297
} ) ;
298
298
299
- export type SelectOverload = {
300
- < T extends number | boolean | AnyVecInstance > ( f : T , t : T , cond : boolean ) : T ;
301
- < T extends AnyVecInstance > (
302
- f : T ,
303
- t : T ,
304
- cond : T extends AnyVec2Instance ? v2b
299
+ function cpuSelect ( f : boolean , t : boolean , cond : boolean ) : boolean ;
300
+ function cpuSelect ( f : number , t : number , cond : boolean ) : number ;
301
+ function cpuSelect < T extends AnyVecInstance > (
302
+ f : T ,
303
+ t : T ,
304
+ cond :
305
+ | boolean
306
+ | ( T extends AnyVec2Instance ? v2b
305
307
: T extends AnyVec3Instance ? v3b
306
- : v4b ,
307
- ) : T ;
308
- } ;
308
+ : v4b ) ,
309
+ ) : T ;
310
+ function cpuSelect < T extends number | boolean | AnyVecInstance > (
311
+ f : T ,
312
+ t : T ,
313
+ cond : AnyBooleanVecInstance | boolean ,
314
+ ) {
315
+ if ( typeof cond === 'boolean' ) {
316
+ return cond ? t : f ;
317
+ }
318
+ return VectorOps . select [ ( f as AnyVecInstance ) . kind ] (
319
+ f as AnyVecInstance ,
320
+ t as AnyVecInstance ,
321
+ cond ,
322
+ ) ;
323
+ }
309
324
310
325
/**
311
326
* Returns `t` if `cond` is `true`, and `f` otherwise.
@@ -316,22 +331,9 @@ export type SelectOverload = {
316
331
* select(vec2i(1, 2), vec2i(3, 4), true) // returns vec2i(3, 4)
317
332
* select(vec2i(1, 2), vec2i(3, 4), vec2b(false, true)) // returns vec2i(1, 4)
318
333
*/
319
- export const select : SelectOverload = dualImpl ( {
334
+ export const select = dualImpl ( {
320
335
name : 'select' ,
321
336
signature : ( ...argTypes ) => ( { argTypes, returnType : argTypes [ 0 ] } ) ,
322
- normalImpl < T extends number | boolean | AnyVecInstance > (
323
- f : T ,
324
- t : T ,
325
- cond : AnyBooleanVecInstance | boolean ,
326
- ) {
327
- if ( typeof cond === 'boolean' ) {
328
- return cond ? t : f ;
329
- }
330
- return VectorOps . select [ ( f as AnyVecInstance ) . kind ] (
331
- f as AnyVecInstance ,
332
- t as AnyVecInstance ,
333
- cond ,
334
- ) ;
335
- } ,
337
+ normalImpl : cpuSelect ,
336
338
codegenImpl : ( f , t , cond ) => stitch `select(${ f } , ${ t } , ${ cond } )` ,
337
339
} ) ;
0 commit comments