@@ -119,12 +119,8 @@ function getPackedSamplerFromInInfo(inInfo: InputInfo): string {
119
119
return getPackedSampler2D ( inInfo ) ;
120
120
case 3 :
121
121
return getPackedSampler3D ( inInfo ) ;
122
- case 4 :
123
- return getPackedSampler4D ( inInfo ) ;
124
122
default :
125
- throw new Error (
126
- `Packed ${ shape . length } -D input sampling` +
127
- ` is not yet supported` ) ;
123
+ return getPackedSamplerND ( inInfo ) ;
128
124
}
129
125
}
130
126
@@ -165,13 +161,8 @@ function getPackedOutputSamplingSnippet(
165
161
case 3 :
166
162
return getOutputPacked3DCoords (
167
163
outShape as [ number , number , number ] , outTexShape ) ;
168
- case 4 :
169
- return getOutputPacked4DCoords (
170
- outShape as [ number , number , number , number ] , outTexShape ) ;
171
164
default :
172
- throw new Error (
173
- `${ outShape . length } -D packed output ` +
174
- `coordinate fetching is not yet supported` ) ;
165
+ return getOutputPackedNDCoords ( outShape , outTexShape ) ;
175
166
}
176
167
}
177
168
@@ -312,7 +303,6 @@ function getShaderPrefix(glsl: GLSL): string {
312
303
${ SAMPLE_1D_SNIPPET }
313
304
${ SAMPLE_2D_SNIPPET }
314
305
${ SAMPLE_3D_SNIPPET }
315
- ${ SAMPLE_4D_SNIPPET }
316
306
${ SAMPLE_5D_SNIPPET }
317
307
${ SAMPLE_6D_SNIPPET }
318
308
` ;
@@ -355,18 +345,6 @@ vec2 packedUVfrom3D(int texNumR, int texNumC,
355
345
}
356
346
` ;
357
347
358
- const SAMPLE_4D_SNIPPET = `
359
- vec2 packedUVfrom4D(int texNumR, int texNumC, int texelsInBatch2,
360
- int texelsInBatch, int texelsInLogicalRow, int b2, int b,
361
- int row, int col) {
362
- int index = b2 * texelsInBatch2 + b * texelsInBatch +
363
- (row / 2) * texelsInLogicalRow + (col / 2);
364
- int texR = index / texNumC;
365
- int texC = index - texR * texNumC;
366
- return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
367
- }
368
- ` ;
369
-
370
348
const SAMPLE_5D_SNIPPET = `
371
349
vec2 UVfrom5D(int texNumR, int texNumC, int stride0,
372
350
int stride1, int stride2, int stride3, int row, int col, int depth,
@@ -508,32 +486,42 @@ function getOutput3DCoords(
508
486
` ;
509
487
}
510
488
511
- function getOutputPacked4DCoords (
512
- shape : [ number , number , number , number ] ,
513
- texShape : [ number , number ] ) : string {
489
+ function getOutputPackedNDCoords (
490
+ shape : number [ ] , texShape : [ number , number ] ) : string {
514
491
const packedTexShape =
515
492
[ Math . ceil ( texShape [ 0 ] / 2 ) , Math . ceil ( texShape [ 1 ] / 2 ) ] ;
516
493
517
- const texelsInLogicalRow = Math . ceil ( shape [ 3 ] / 2 ) ;
518
- const texelsInBatch = texelsInLogicalRow * Math . ceil ( shape [ 2 ] / 2 ) ;
519
- const texelsInBatch2 = texelsInBatch * shape [ 1 ] ;
494
+ const texelsInLogicalRow = Math . ceil ( shape [ shape . length - 1 ] / 2 ) ;
495
+ const texelsInBatch =
496
+ texelsInLogicalRow * Math . ceil ( shape [ shape . length - 2 ] / 2 ) ;
497
+ let texelsInBatchN = texelsInBatch ;
498
+ let batches = `` ;
499
+ let coords = 'b, r, c' ;
500
+
501
+ for ( let b = 2 ; b < shape . length - 1 ; b ++ ) {
502
+ texelsInBatchN *= shape [ shape . length - b - 1 ] ;
503
+ batches = `
504
+ int b${ b } = index / ${ texelsInBatchN } ;
505
+ index -= b${ b } * ${ texelsInBatchN } ;
506
+ ` + batches ;
507
+ coords = `b${ b } , ` + coords ;
508
+ }
520
509
521
510
return `
522
- ivec4 getOutputCoords() {
511
+ ivec ${ shape . length } getOutputCoords() {
523
512
ivec2 resTexRC = ivec2(resultUV.yx *
524
513
vec2(${ packedTexShape [ 0 ] } , ${ packedTexShape [ 1 ] } ));
525
514
int index = resTexRC.x * ${ packedTexShape [ 1 ] } + resTexRC.y;
526
-
527
- int b2 = index / ${ texelsInBatch2 } ;
528
- index -= b2 * ${ texelsInBatch2 } ;
515
+
516
+ ${ batches }
529
517
530
518
int b = index / ${ texelsInBatch } ;
531
519
index -= b * ${ texelsInBatch } ;
532
520
533
521
int r = 2 * (index / ${ texelsInLogicalRow } );
534
522
int c = imod(index, ${ texelsInLogicalRow } ) * 2;
535
523
536
- return ivec4(b2, b, r, c );
524
+ return ivec ${ shape . length } ( ${ coords } );
537
525
}
538
526
` ;
539
527
}
@@ -996,8 +984,9 @@ function getSampler3D(inputInfo: InputInfo): string {
996
984
` ;
997
985
}
998
986
999
- function getPackedSampler4D ( inputInfo : InputInfo ) : string {
987
+ function getPackedSamplerND ( inputInfo : InputInfo ) : string {
1000
988
const shape = inputInfo . shapeInfo . logicalShape ;
989
+ const rank = shape . length ;
1001
990
const texName = inputInfo . name ;
1002
991
const funcName = 'get' + texName . charAt ( 0 ) . toUpperCase ( ) + texName . slice ( 1 ) ;
1003
992
const texShape = inputInfo . shapeInfo . texShape ;
@@ -1006,17 +995,23 @@ function getPackedSampler4D(inputInfo: InputInfo): string {
1006
995
const texNumR = packedTexShape [ 0 ] ;
1007
996
const texNumC = packedTexShape [ 1 ] ;
1008
997
1009
- const valuesPerRow = Math . ceil ( shape [ 3 ] / 2 ) ;
1010
- const texelsInBatch = valuesPerRow * Math . ceil ( shape [ 2 ] / 2 ) ;
1011
- const texelsInBatch2 = texelsInBatch * shape [ 1 ] ;
998
+ const valuesPerRow = Math . ceil ( shape [ rank - 1 ] / 2 ) ;
999
+ let texelsInBatch = valuesPerRow * Math . ceil ( shape [ rank - 2 ] / 2 ) ;
1000
+ let params = `int b, int row, int col` ;
1001
+ let index = `b * ${ texelsInBatch } + (row / 2) * ${ valuesPerRow } + (col / 2)` ;
1002
+ for ( let b = 2 ; b < rank - 1 ; b ++ ) {
1003
+ params = `int b${ b } , ` + params ;
1004
+ texelsInBatch *= shape [ rank - b - 1 ] ;
1005
+ index = `b${ b } * ${ texelsInBatch } + ` + index ;
1006
+ }
1012
1007
const glsl = getGlslDifferences ( ) ;
1013
-
1014
1008
return `
1015
- vec4 ${ funcName } (int b2, int b, int row, int col) {
1016
- vec2 uv = packedUVfrom4D(
1017
- ${ texNumR } , ${ texNumC } , ${ texelsInBatch2 } ,
1018
- ${ texelsInBatch } , ${ valuesPerRow } , b2, b, row, col);
1019
- return ${ glsl . texture2D } (${ texName } , uv);
1009
+ vec4 ${ funcName } (${ params } ) {
1010
+ int index = ${ index } ;
1011
+ int texR = index / ${ texNumC } ;
1012
+ int texC = index - texR * ${ texNumC } ;
1013
+ vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${ texNumC } , ${ texNumR } );
1014
+ return ${ glsl . texture2D } (${ texName } , uv);
1020
1015
}
1021
1016
` ;
1022
1017
}
0 commit comments