forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshader_compiler.ts
1915 lines (1745 loc) · 59.5 KB
/
shader_compiler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright 2017 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
// Please make sure the shaker key in makeShaderKey in gpgpu_math.ts is well
// mapped if any shader source code is changed in this file.
import {backend_util, util} from '@tensorflow/tfjs-core';
const {getBroadcastDims} = backend_util;
import {getGlslDifferences, GLSL} from './glsl_version';
import * as shader_util from './shader_compiler_util';
export type ShapeInfo = {
logicalShape: number[],
texShape: [number, number],
isUniform: boolean,
isPacked: boolean,
flatOffset: number
};
export type InputInfo = {
name: string,
shapeInfo: ShapeInfo
};
export type UniformType =
'float'|'vec2'|'vec3'|'vec4'|'int'|'ivec2'|'ivec3'|'ivec4';
interface ProgramParams {
userCode: string;
enableShapeUniforms?: boolean;
packedInputs?: boolean;
customUniforms?:
Array<{name: string; arrayIndex?: number; type: UniformType;}>;
}
export function makeShader(
inputsInfo: InputInfo[], outputShape: ShapeInfo,
program: ProgramParams): string {
const prefixSnippets: string[] = [];
inputsInfo.forEach(x => {
const size = util.sizeFromShape(x.shapeInfo.logicalShape);
// Snippet when we decided to upload the values as uniform.
if (x.shapeInfo.isUniform) {
prefixSnippets.push(
`uniform float ${x.name}${size > 1 ? `[${size}]` : ''};`);
} else {
prefixSnippets.push(`uniform sampler2D ${x.name};`);
prefixSnippets.push(`uniform int offset${x.name};`);
}
if (program.enableShapeUniforms) {
const {uniformShape} = getUniformInfoFromShape(
program.packedInputs, x.shapeInfo.logicalShape, x.shapeInfo.texShape);
switch (uniformShape.length) {
case 1:
prefixSnippets.push(`uniform int ${x.name}Shape;`);
break;
case 2:
prefixSnippets.push(`uniform ivec2 ${x.name}Shape;`);
break;
case 3:
prefixSnippets.push(`uniform ivec3 ${x.name}Shape;`);
break;
case 4:
prefixSnippets.push(`uniform ivec4 ${x.name}Shape;`);
break;
default:
break;
}
prefixSnippets.push(`uniform ivec2 ${x.name}TexShape;`);
}
});
if (program.enableShapeUniforms) {
switch (outputShape.logicalShape.length) {
case 1:
prefixSnippets.push(`uniform int outShape;`);
break;
case 2:
prefixSnippets.push(`uniform ivec2 outShape;`);
prefixSnippets.push(`uniform int outShapeStrides;`);
break;
case 3:
prefixSnippets.push(`uniform ivec3 outShape;`);
prefixSnippets.push(`uniform ivec2 outShapeStrides;`);
break;
case 4:
prefixSnippets.push(`uniform ivec4 outShape;`);
prefixSnippets.push(`uniform ivec3 outShapeStrides;`);
break;
default:
break;
}
prefixSnippets.push(`uniform ivec2 outTexShape;`);
}
if (program.customUniforms) {
program.customUniforms.forEach((d) => {
prefixSnippets.push(`uniform ${d.type} ${d.name}${
d.arrayIndex ? `[${d.arrayIndex}]` : ''};`);
});
}
const inputPrefixSnippet = prefixSnippets.join('\n');
const inputSamplingSnippet = inputsInfo
.map(
x => getInputSamplingSnippet(
x, outputShape, program.packedInputs,
program.enableShapeUniforms))
.join('\n');
const outTexShape = outputShape.texShape;
const glsl = getGlslDifferences();
const floatTextureSampleSnippet = getFloatTextureSampleSnippet(glsl);
let outputSamplingSnippet: string;
let floatTextureSetOutputSnippet: string;
let shaderPrefix = getShaderPrefix(glsl);
if (outputShape.isPacked) {
outputSamplingSnippet = getPackedOutputSamplingSnippet(
outputShape.logicalShape, outTexShape, program.enableShapeUniforms);
floatTextureSetOutputSnippet = getFloatTextureSetRGBASnippet(glsl);
} else {
outputSamplingSnippet = getOutputSamplingSnippet(
outputShape.logicalShape, outTexShape, program.enableShapeUniforms);
floatTextureSetOutputSnippet = getFloatTextureSetRSnippet(glsl);
}
if (program.packedInputs) {
shaderPrefix += SHADER_PACKED_PREFIX;
}
const source = [
shaderPrefix, floatTextureSampleSnippet, floatTextureSetOutputSnippet,
inputPrefixSnippet, outputSamplingSnippet, inputSamplingSnippet,
program.userCode
].join('\n');
return source;
}
function getSamplerFromInInfo(
inInfo: InputInfo, enableShapeUniforms = false): string {
const shape = inInfo.shapeInfo.logicalShape;
switch (shape.length) {
case 0:
return getSamplerScalar(inInfo, enableShapeUniforms);
case 1:
return getSampler1D(inInfo, enableShapeUniforms);
case 2:
return getSampler2D(inInfo, enableShapeUniforms);
case 3:
return getSampler3D(inInfo, enableShapeUniforms);
case 4:
return getSampler4D(inInfo, enableShapeUniforms);
case 5:
return getSampler5D(inInfo);
case 6:
return getSampler6D(inInfo);
default:
throw new Error(
`${shape.length}-D input sampling` +
` is not yet supported`);
}
}
function getPackedSamplerFromInInfo(
inInfo: InputInfo, enableShapeUniforms: boolean): string {
const shape = inInfo.shapeInfo.logicalShape;
switch (shape.length) {
case 0:
return getPackedSamplerScalar(inInfo);
case 1:
return getPackedSampler1D(inInfo, enableShapeUniforms);
case 2:
return getPackedSampler2D(inInfo, enableShapeUniforms);
case 3:
return getPackedSampler3D(inInfo, enableShapeUniforms);
default:
return getPackedSamplerND(inInfo, enableShapeUniforms);
}
}
function getInputSamplingSnippet(
inInfo: InputInfo, outShapeInfo: ShapeInfo, usesPackedTextures = false,
enableShapeUniforms: boolean): string {
let res = '';
if (usesPackedTextures) {
res += getPackedSamplerFromInInfo(inInfo, enableShapeUniforms);
} else {
res += getSamplerFromInInfo(inInfo, enableShapeUniforms);
}
const inShape = inInfo.shapeInfo.logicalShape;
const outShape = outShapeInfo.logicalShape;
if (inShape.length <= outShape.length) {
if (usesPackedTextures) {
res += getPackedSamplerAtOutputCoords(inInfo, outShapeInfo);
} else {
res += getSamplerAtOutputCoords(inInfo, outShapeInfo);
}
}
return res;
}
function getPackedOutputSamplingSnippet(
outShape: number[], outTexShape: [number, number],
enableShapeUniforms: boolean): string {
switch (outShape.length) {
case 0:
return getOutputScalarCoords();
case 1:
return getOutputPacked1DCoords(
outShape as [number], outTexShape, enableShapeUniforms);
case 2:
return getOutputPacked2DCoords(
outShape as [number, number], outTexShape, enableShapeUniforms);
case 3:
return getOutputPacked3DCoords(
outShape as [number, number, number], outTexShape,
enableShapeUniforms);
default:
return getOutputPackedNDCoords(
outShape, outTexShape, enableShapeUniforms);
}
}
function getOutputSamplingSnippet(
outShape: number[], outTexShape: [number, number],
enableShapeUniforms: boolean): string {
switch (outShape.length) {
case 0:
return getOutputScalarCoords();
case 1:
return getOutput1DCoords(
outShape as [number], outTexShape, enableShapeUniforms);
case 2:
return getOutput2DCoords(
outShape as [number, number], outTexShape, enableShapeUniforms);
case 3:
return getOutput3DCoords(
outShape as [number, number, number], outTexShape,
enableShapeUniforms);
case 4:
return getOutput4DCoords(
outShape as [number, number, number, number], outTexShape,
enableShapeUniforms);
case 5:
return getOutput5DCoords(
outShape as [number, number, number, number, number], outTexShape);
case 6:
return getOutput6DCoords(
outShape as [number, number, number, number, number, number],
outTexShape);
default:
throw new Error(
`${outShape.length}-D output sampling is not yet supported`);
}
}
function getFloatTextureSampleSnippet(glsl: GLSL): string {
return `
float sampleTexture(sampler2D textureSampler, vec2 uv) {
return ${glsl.texture2D}(textureSampler, uv).r;
}
`;
}
function getFloatTextureSetRSnippet(glsl: GLSL): string {
return `
void setOutput(float val) {
${glsl.output} = vec4(val, 0, 0, 0);
}
`;
}
function getFloatTextureSetRGBASnippet(glsl: GLSL): string {
return `
void setOutput(vec4 val) {
${glsl.output} = val;
}
`;
}
function getShaderPrefix(glsl: GLSL): string {
const SHADER_PREFIX = `${glsl.version}
precision highp float;
precision highp int;
precision highp sampler2D;
${glsl.varyingFs} vec2 resultUV;
${glsl.defineOutput}
const vec2 halfCR = vec2(0.5, 0.5);
struct ivec5
{
int x;
int y;
int z;
int w;
int u;
};
struct ivec6
{
int x;
int y;
int z;
int w;
int u;
int v;
};
uniform float NAN;
${glsl.defineSpecialNaN}
${glsl.defineSpecialInf}
${glsl.defineRound}
int imod(int x, int y) {
return x - y * (x / y);
}
int idiv(int a, int b, float sign) {
int res = a / b;
int mod = imod(a, b);
if (sign < 0. && mod != 0) {
res -= 1;
}
return res;
}
//Based on the work of Dave Hoskins
//https://www.shadertoy.com/view/4djSRW
#define HASHSCALE1 443.8975
float random(float seed){
vec2 p = resultUV * seed;
vec3 p3 = fract(vec3(p.xyx) * HASHSCALE1);
p3 += dot(p3, p3.yzx + 19.19);
return fract((p3.x + p3.y) * p3.z);
}
${SAMPLE_1D_SNIPPET}
${SAMPLE_2D_SNIPPET}
${SAMPLE_3D_SNIPPET}
`;
return SHADER_PREFIX;
}
const SAMPLE_1D_SNIPPET = `
vec2 uvFromFlat(int texNumR, int texNumC, int index) {
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
vec2 packedUVfrom1D(int texNumR, int texNumC, int index) {
int texelIndex = index / 2;
int texR = texelIndex / texNumC;
int texC = texelIndex - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
`;
const SAMPLE_2D_SNIPPET = `
vec2 packedUVfrom2D(int texelsInLogicalRow, int texNumR,
int texNumC, int row, int col) {
int texelIndex = (row / 2) * texelsInLogicalRow + (col / 2);
int texR = texelIndex / texNumC;
int texC = texelIndex - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
`;
const SAMPLE_3D_SNIPPET = `
vec2 packedUVfrom3D(int texNumR, int texNumC,
int texelsInBatch, int texelsInLogicalRow, int b,
int row, int col) {
int index = b * texelsInBatch + (row / 2) * texelsInLogicalRow + (col / 2);
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
`;
const SHADER_PACKED_PREFIX = `
float getChannel(vec4 frag, vec2 innerDims) {
vec2 modCoord = mod(innerDims, 2.);
return modCoord.x == 0. ?
(modCoord.y == 0. ? frag.r : frag.g) :
(modCoord.y == 0. ? frag.b : frag.a);
}
float getChannel(vec4 frag, int dim) {
float modCoord = mod(float(dim), 2.);
return modCoord == 0. ? frag.r : frag.g;
}
`;
function getOutputScalarCoords() {
return `
int getOutputCoords() {
return 0;
}
`;
}
function getOutputPacked1DCoords(
shape: [number], texShape: [number, number],
enableShapeUniforms: boolean): string {
const packedTexShape =
[Math.ceil(texShape[0] / 2), Math.ceil(texShape[1] / 2)];
if (packedTexShape[0] === 1) {
if (enableShapeUniforms) {
return `
int getOutputCoords() {
return 2 * int(resultUV.x * ceil(float(outTexShape[1]) / 2.0));
}
`;
}
return `
int getOutputCoords() {
return 2 * int(resultUV.x * ${packedTexShape[1]}.0);
}
`;
}
if (packedTexShape[1] === 1) {
if (enableShapeUniforms) {
return `
int getOutputCoords() {
return 2 * int(resultUV.y * ceil(float(outTexShape[0]) / 2.0));
}
`;
}
return `
int getOutputCoords() {
return 2 * int(resultUV.y * ${packedTexShape[0]}.0);
}
`;
}
if (enableShapeUniforms) {
return `
int getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
return 2 * (resTexRC.x * packedTexShape[1] + resTexRC.y);
}
`;
}
return `
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${packedTexShape[0]}, ${packedTexShape[1]}));
return 2 * (resTexRC.x * ${packedTexShape[1]} + resTexRC.y);
}
`;
}
function getOutput1DCoords(
shape: [number], texShape: [number, number],
enableShapeUniforms: boolean): string {
if (texShape[0] === 1) {
if (enableShapeUniforms) {
return `
int getOutputCoords() {
return int(resultUV.x * float(outTexShape[1]));
}
`;
}
return `
int getOutputCoords() {
return int(resultUV.x * ${texShape[1]}.0);
}
`;
}
if (texShape[1] === 1) {
if (enableShapeUniforms) {
return `
int getOutputCoords() {
return int(resultUV.y * float(outTexShape[0]));
}
`;
}
return `
int getOutputCoords() {
return int(resultUV.y * ${texShape[0]}.0);
}
`;
}
if (enableShapeUniforms) {
return `
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
return resTexRC.x * outTexShape[1] + resTexRC.y;
}
`;
}
return `
int getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${texShape[0]}, ${texShape[1]}));
return resTexRC.x * ${texShape[1]} + resTexRC.y;
}
`;
}
function getOutputPacked3DCoords(
shape: [number, number, number], texShape: [number, number],
enableShapeUniforms: boolean): string {
if (enableShapeUniforms) {
return `
ivec3 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
int texelsInLogicalRow = int(ceil(float(outShape[2]) / 2.0));
int texelsInBatch = texelsInLogicalRow * int(ceil(float(outShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int b = index / texelsInBatch;
index -= b * texelsInBatch;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec3(b, r, c);
}
`;
}
const packedTexShape =
[Math.ceil(texShape[0] / 2), Math.ceil(texShape[1] / 2)];
const texelsInLogicalRow = Math.ceil(shape[2] / 2);
const texelsInBatch = texelsInLogicalRow * Math.ceil(shape[1] / 2);
return `
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${packedTexShape[0]}, ${packedTexShape[1]}));
int index = resTexRC.x * ${packedTexShape[1]} + resTexRC.y;
int b = index / ${texelsInBatch};
index -= b * ${texelsInBatch};
int r = 2 * (index / ${texelsInLogicalRow});
int c = imod(index, ${texelsInLogicalRow}) * 2;
return ivec3(b, r, c);
}
`;
}
function getOutput3DCoords(
shape: [number, number, number], texShape: [number, number],
enableShapeUniforms: boolean): string {
if (enableShapeUniforms) {
const coordsFromIndexSnippet =
shader_util.getOutputLogicalCoordinatesFromFlatIndexByUniform(
['r', 'c', 'd'], shape);
return `
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
${coordsFromIndexSnippet}
return ivec3(r, c, d);
}
`;
}
const coordsFromIndexSnippet =
shader_util.getLogicalCoordinatesFromFlatIndex(['r', 'c', 'd'], shape);
return `
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${texShape[0]}, ${texShape[1]}));
int index = resTexRC.x * ${texShape[1]} + resTexRC.y;
${coordsFromIndexSnippet}
return ivec3(r, c, d);
}
`;
}
function getOutputPackedNDCoords(
shape: number[], texShape: [number, number],
enableShapeUniforms: boolean): string {
if (enableShapeUniforms) {
// TODO: support 5d and 6d
return `
ivec4 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int texelsInLogicalRow = int(ceil(float(outShape[3]) / 2.0));
int texelsInBatch = texelsInLogicalRow * int(ceil(float(outShape[2]) / 2.0));
int texelsInBatchN = texelsInBatch * outShape[1];
int b2 = index / texelsInBatchN;
index -= b2 * texelsInBatchN;
int b = index / texelsInBatch;
index -= b * texelsInBatch;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec4(b2, b, r, c);
}
`;
}
const packedTexShape =
[Math.ceil(texShape[0] / 2), Math.ceil(texShape[1] / 2)];
const texelsInLogicalRow = Math.ceil(shape[shape.length - 1] / 2);
const texelsInBatch =
texelsInLogicalRow * Math.ceil(shape[shape.length - 2] / 2);
let texelsInBatchN = texelsInBatch;
let batches = ``;
let coords = 'b, r, c';
for (let b = 2; b < shape.length - 1; b++) {
texelsInBatchN *= shape[shape.length - b - 1];
batches = `
int b${b} = index / ${texelsInBatchN};
index -= b${b} * ${texelsInBatchN};
` + batches;
coords = `b${b}, ` + coords;
}
return `
ivec${shape.length} getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${packedTexShape[0]}, ${packedTexShape[1]}));
int index = resTexRC.x * ${packedTexShape[1]} + resTexRC.y;
${batches}
int b = index / ${texelsInBatch};
index -= b * ${texelsInBatch};
int r = 2 * (index / ${texelsInLogicalRow});
int c = imod(index, ${texelsInLogicalRow}) * 2;
return ivec${shape.length}(${coords});
}
`;
}
function getOutput4DCoords(
shape: [number, number, number, number], texShape: [number, number],
enableShapeUniforms: boolean): string {
if (enableShapeUniforms) {
const coordsFromIndexSnippet =
shader_util.getOutputLogicalCoordinatesFromFlatIndexByUniform(
['r', 'c', 'd', 'd2'], shape);
return `
ivec4 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
${coordsFromIndexSnippet}
return ivec4(r, c, d, d2);
}
`;
}
const coordsFromIndexSnippet = shader_util.getLogicalCoordinatesFromFlatIndex(
['r', 'c', 'd', 'd2'], shape);
return `
ivec4 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${texShape[0]}, ${texShape[1]}));
int index = resTexRC.x * ${texShape[1]} + resTexRC.y;
${coordsFromIndexSnippet}
return ivec4(r, c, d, d2);
}
`;
}
function getOutput5DCoords(
shape: [number, number, number, number, number],
texShape: [number, number]): string {
const coordsFromIndexSnippet = shader_util.getLogicalCoordinatesFromFlatIndex(
['r', 'c', 'd', 'd2', 'd3'], shape);
return `
ivec5 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx * vec2(${texShape[0]},
${texShape[1]}));
int index = resTexRC.x * ${texShape[1]} + resTexRC.y;
${coordsFromIndexSnippet}
ivec5 outShape = ivec5(r, c, d, d2, d3);
return outShape;
}
`;
}
function getOutput6DCoords(
shape: [number, number, number, number, number, number],
texShape: [number, number]): string {
const coordsFromIndexSnippet = shader_util.getLogicalCoordinatesFromFlatIndex(
['r', 'c', 'd', 'd2', 'd3', 'd4'], shape);
return `
ivec6 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${texShape[0]}, ${texShape[1]}));
int index = resTexRC.x * ${texShape[1]} + resTexRC.y;
${coordsFromIndexSnippet}
ivec6 result = ivec6(r, c, d, d2, d3, d4);
return result;
}
`;
}
function getOutputPacked2DCoords(
shape: [number, number], texShape: [number, number],
enableShapeUniforms: boolean): string {
const packedTexShape =
[Math.ceil(texShape[0] / 2), Math.ceil(texShape[1] / 2)];
if (util.arraysEqual(shape, texShape)) {
if (enableShapeUniforms) {
return `
ivec2 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
return 2 * ivec2(resultUV.yx * vec2(packedTexShape[0], packedTexShape[1]));
}
`;
}
return `
ivec2 getOutputCoords() {
return 2 * ivec2(resultUV.yx * vec2(${packedTexShape[0]}, ${
packedTexShape[1]}));
}
`;
}
// texels needed to accommodate a logical row
const texelsInLogicalRow = Math.ceil(shape[1] / 2);
/**
* getOutputCoords
*
* resTexRC: The rows and columns of the texels. If you move over one
* texel to the right in the packed texture, you are moving over one column
* (not two).
*
* index: The texel index
*/
if (enableShapeUniforms) {
return `
ivec2 getOutputCoords() {
ivec2 packedTexShape = ivec2(ceil(float(outTexShape[0]) / 2.0), ceil(float(outTexShape[1]) / 2.0));
int texelsInLogicalRow = int(ceil(float(outShape[1]) / 2.0));
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(packedTexShape[0], packedTexShape[1]));
int index = resTexRC.x * packedTexShape[1] + resTexRC.y;
int r = 2 * (index / texelsInLogicalRow);
int c = imod(index, texelsInLogicalRow) * 2;
return ivec2(r, c);
}
`;
}
return `
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${packedTexShape[0]}, ${packedTexShape[1]}));
int index = resTexRC.x * ${packedTexShape[1]} + resTexRC.y;
int r = 2 * (index / ${texelsInLogicalRow});
int c = imod(index, ${texelsInLogicalRow}) * 2;
return ivec2(r, c);
}
`;
}
function getOutput2DCoords(
shape: [number, number], texShape: [number, number],
enableShapeUniforms: boolean): string {
if (util.arraysEqual(shape, texShape)) {
if (enableShapeUniforms) {
return `
ivec2 getOutputCoords() {
return ivec2(resultUV.yx * vec2(outTexShape[0], outTexShape[1]));
}
`;
}
return `
ivec2 getOutputCoords() {
return ivec2(resultUV.yx * vec2(${texShape[0]}, ${texShape[1]}));
}
`;
}
if (shape[1] === 1) {
if (enableShapeUniforms) {
return `
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
return ivec2(index, 0);
}
`;
}
return `
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${texShape[0]}, ${texShape[1]}));
int index = resTexRC.x * ${texShape[1]} + resTexRC.y;
return ivec2(index, 0);
}
`;
}
if (shape[0] === 1) {
if (enableShapeUniforms) {
return `
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
return ivec2(0, index);
}
`;
}
return `
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${texShape[0]}, ${texShape[1]}));
int index = resTexRC.x * ${texShape[1]} + resTexRC.y;
return ivec2(0, index);
}
`;
}
if (enableShapeUniforms) {
return `
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(outTexShape[0], outTexShape[1]));
int index = resTexRC.x * outTexShape[1] + resTexRC.y;
int r = index / outShape[1];
int c = index - r * outShape[1];
return ivec2(r, c);
}
`;
}
return `
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(resultUV.yx *
vec2(${texShape[0]}, ${texShape[1]}));
int index = resTexRC.x * ${texShape[1]} + resTexRC.y;
int r = index / ${shape[1]};
int c = index - r * ${shape[1]};
return ivec2(r, c);
}
`;
}
function getFlatOffsetUniformName(texName: string): string {
return `offset${texName}`;
}
function getPackedSamplerScalar(inputInfo: InputInfo): string {
const texName = inputInfo.name;
const funcName = 'get' + texName.charAt(0).toUpperCase() + texName.slice(1);
const glsl = getGlslDifferences();
return `
vec4 ${funcName}() {
return ${glsl.texture2D}(${texName}, halfCR);
}
`;
}
function getSamplerScalar(
inputInfo: InputInfo, enableShapeUniforms: boolean): string {
const texName = inputInfo.name;
const funcName = 'get' + texName.charAt(0).toUpperCase() + texName.slice(1);
if (inputInfo.shapeInfo.isUniform) {
return `float ${funcName}() {return ${texName};}`;
}
const [texNumR, texNumC] = inputInfo.shapeInfo.texShape;
if (texNumR === 1 && texNumC === 1) {
return `
float ${funcName}() {
return sampleTexture(${texName}, halfCR);
}
`;
}
const offset = getFlatOffsetUniformName(texName);
if (enableShapeUniforms) {
return `
float ${funcName}() {
vec2 uv = uvFromFlat(${texName}TexShape[0], ${texName}TexShape[1], ${
offset});
return sampleTexture(${texName}, uv);
}
`;
}
const [tNumR, tNumC] = inputInfo.shapeInfo.texShape;
return `
float ${funcName}() {
vec2 uv = uvFromFlat(${tNumR}, ${tNumC}, ${offset});
return sampleTexture(${texName}, uv);
}
`;
}
function getPackedSampler1D(
inputInfo: InputInfo, enableShapeUniforms: boolean): string {
const texName = inputInfo.name;
const funcName = 'get' + texName.charAt(0).toUpperCase() + texName.slice(1);
const texShape = inputInfo.shapeInfo.texShape;
const glsl = getGlslDifferences();
if (enableShapeUniforms) {
return `
vec4 ${funcName}(int index) {
ivec2 packedTexShape = ivec2(ceil(float(${
texName}TexShape[0]) / 2.0), ceil(float(${texName}TexShape[1]) / 2.0));
vec2 uv = packedUVfrom1D(
packedTexShape[0], packedTexShape[1], index);
return ${glsl.texture2D}(${texName}, uv);
}
`;
}
const packedTexShape =
[Math.ceil(texShape[0] / 2), Math.ceil(texShape[1] / 2)];
return `
vec4 ${funcName}(int index) {
vec2 uv = packedUVfrom1D(
${packedTexShape[0]}, ${packedTexShape[1]}, index);
return ${glsl.texture2D}(${texName}, uv);
}
`;
}
function getSampler1D(
inputInfo: InputInfo, enableShapeUniforms: boolean): string {
const texName = inputInfo.name;
const funcName = 'get' + texName.charAt(0).toUpperCase() + texName.slice(1);
if (inputInfo.shapeInfo.isUniform) {
// Uniform arrays will be less than 65505 (no risk of float16 overflow).
return `
float ${funcName}(int index) {
${getUniformSampler(inputInfo)}
}
`;
}
const texShape = inputInfo.shapeInfo.texShape;
const tNumR = texShape[0];
const tNumC = texShape[1];
if (tNumC === 1 && tNumR === 1) {
return `
float ${funcName}(int index) {
return sampleTexture(${texName}, halfCR);
}
`;
}
const offset = getFlatOffsetUniformName(texName);
if (tNumC === 1) {
if (enableShapeUniforms) {
return `
float ${funcName}(int index) {
vec2 uv = vec2(0.5, (float(index + ${offset}) + 0.5) / float(${
texName}TexShape[0]));
return sampleTexture(${texName}, uv);
}