-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathshader_lib.ts
More file actions
651 lines (616 loc) · 16.8 KB
/
shader_lib.ts
File metadata and controls
651 lines (616 loc) · 16.8 KB
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
/**
* @license
* Copyright 2016 Google Inc.
* 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.
*/
import {
DATA_TYPE_BYTES,
DATA_TYPE_SIGNED,
DataType,
} from "#src/util/data_type.js";
import type {
AttributeIndex,
ShaderBuilder,
ShaderCodePart,
} from "#src/webgl/shader.js";
export const glsl_mixLinear = `
float mixLinear(float x, float y, float a) { return mix(x, y, a); }
`;
// Hue, saturation, and value are in [0, 1] range.
export const glsl_hsvToRgb = `
vec3 hueToRgb(float hue) {
float hue6 = hue * 6.0;
float r = abs(hue6 - 3.0) - 1.0;
float g = 2.0 - abs(hue6 - 2.0);
float b = 2.0 - abs(hue6 - 4.0);
return clamp(vec3(r, g, b), 0.0, 1.0);
}
vec3 hsvToRgb(vec3 c) {
vec3 hueRgb = hueToRgb(c.x);
return c.z * ((hueRgb - 1.0) * c.y + 1.0);
}
`;
export const glsl_uint64 = `
struct uint64_t {
highp uvec2 value;
};
struct uint64x2_t {
highp uvec4 value;
};
uint64_t mixLinear(uint64_t x, uint64_t y, float a) {
return x;
}
uint64_t toUint64(uint64_t x) { return x; }
`;
export const glsl_unpackUint64leFromUint32 = [
glsl_uint64,
`
uint64_t unpackUint64leFromUint32(highp uvec2 x) {
uint64_t result;
result.value = x;
return result;
}
uint64x2_t unpackUint64leFromUint32(highp uvec4 x) {
uint64x2_t result;
result.value = x;
return result;
}
`,
];
export const glsl_equalUint64 = [
glsl_uint64,
`
bool equals(uint64_t a, uint64_t b) {
return a.value == b.value;
}
`,
];
export const glsl_compareLessThanUint64 = [
glsl_uint64,
`
bool compareLessThan(uint64_t a, uint64_t b) {
return (a.value[1] < b.value[1])||
(a.value[1] == b.value[1] && a.value[0] < b.value[0]);
}
`,
];
export const glsl_subtractUint64 = [
glsl_uint64,
`
uint64_t subtract(uint64_t a, uint64_t b) {
if (a.value[0] < b.value[0]) {
--a.value[1];
}
a.value -= b.value;
return a;
}
`,
];
export const glsl_addUint64 = [
glsl_uint64,
`
uint64_t add(uint64_t a, uint64_t b) {
a.value[0] += b.value[0];
if (a.value[0] < b.value[0]) {
++a.value[1];
}
a.value[1] += b.value[1];
return a;
}
`,
];
export const glsl_addSaturateUint64 = [
glsl_addUint64,
glsl_compareLessThanUint64,
`
uint64_t addSaturate(uint64_t a, uint64_t b) {
a = add(a, b);
if (compareLessThan(a, b)) {
a.value = uvec2(0xffffffffu, 0xffffffffu);
}
return a;
}
`,
];
export const glsl_subtractSaturateUint64 = [
glsl_subtractUint64,
glsl_compareLessThanUint64,
`
uint64_t subtractSaturate(uint64_t a, uint64_t b) {
b = subtract(a, b);
if (compareLessThan(a, b)) {
b.value = uvec2(0u, 0u);
}
return b;
}
`,
];
export const glsl_shiftRightUint64 = [
glsl_uint64,
`
uint64_t shiftRight(uint64_t a, int shift) {
if (shift >= 32) {
return uint64_t(uvec2(a.value[1] >> (shift - 32), 0u));
} else if (shift == 0) {
return a;
} else {
return uint64_t(uvec2((a.value[0] >> shift) | (a.value[1] << (32 - shift)), a.value[1] >> shift));
}
}
`,
];
export const glsl_shiftLeftUint64 = [
glsl_uint64,
`
uint64_t shiftLeft(uint64_t a, int shift) {
if (shift >= 32) {
return uint64_t(uvec2(0u, a.value[0] << (shift - 32)));
} else if (shift == 0) {
return a;
} else {
return uint64_t(uvec2(a.value[0] << shift, (a.value[1] << shift) | (a.value[0] >> (32 - shift))));
}
}
`,
];
export const glsl_uint8 = [
glsl_uint64,
`
struct uint8_t {
highp uint value;
};
struct uint8x2_t {
highp uvec2 value;
};
struct uint8x3_t {
highp uvec3 value;
};
struct uint8x4_t {
highp uvec4 value;
};
uint8_t mixLinear(uint8_t x, uint8_t y, highp float a) {
return uint8_t(uint(round(mix(float(x.value), float(y.value), a))));
}
highp uint toRaw(uint8_t x) { return x.value; }
highp float toNormalized(uint8_t x) { return float(x.value) / 255.0; }
highp uvec2 toRaw(uint8x2_t x) { return x.value; }
highp vec2 toNormalized(uint8x2_t x) { return vec2(x.value) / 255.0; }
highp uvec3 toRaw(uint8x3_t x) { return x.value; }
vec3 toNormalized(uint8x3_t x) { return vec3(x.value) / 255.0; }
highp uvec4 toRaw(uint8x4_t x) { return x.value; }
vec4 toNormalized(uint8x4_t x) { return vec4(x.value) / 255.0; }
uint64_t toUint64(uint8_t x) {
uint64_t result;
result.value[0] = x.value;
result.value[1] = 0u;
return result;
}
uint8_t uint8FromFloat(highp float x) {
return uint8_t(uint(clamp(x, 0.0, 255.0)));
}
`,
];
export const glsl_int8 = [
glsl_uint64,
`
struct int8_t {
highp int value;
};
struct int8x2_t {
highp ivec2 value;
};
struct int8x3_t {
highp ivec3 value;
};
struct int8x4_t {
highp ivec4 value;
};
int8_t mixLinear(int8_t x, int8_t y, highp float a) {
return int8_t(int(round(mix(float(x.value), float(y.value), a))));
}
highp int toRaw(int8_t x) { return x.value; }
highp ivec2 toRaw(int8x2_t x) { return x.value; }
highp ivec3 toRaw(int8x3_t x) { return x.value; }
highp ivec4 toRaw(int8x4_t x) { return x.value; }
uint64_t toUint64(int8_t x) {
uint64_t result;
result.value[0] = uint(x.value);
result.value[1] = uint(x.value >> 31);
return result;
}
int8_t int8FromFloat(highp float x) {
return int8_t(int(clamp(x, -128.0, 127.0)));
}
`,
];
export const glsl_float = `
highp float toRaw(highp float x) { return x; }
highp float toNormalized(highp float x) { return x; }
vec2 toRaw(vec2 x) { return x; }
vec2 toNormalized(vec2 x) { return x; }
vec3 toRaw(vec3 x) { return x; }
vec3 toNormalized(vec3 x) { return x; }
vec4 toRaw(vec4 x) { return x; }
vec4 toNormalized(vec4 x) { return x; }
`;
export const glsl_uint16 = [
glsl_uint64,
`
struct uint16_t {
highp uint value;
};
struct uint16x2_t {
highp uvec2 value;
};
uint16_t mixLinear(uint16_t x, uint16_t y, highp float a) {
return uint16_t(uint(round(mix(float(x.value), float(y.value), a))));
}
highp uint toRaw(uint16_t x) { return x.value; }
highp float toNormalized(uint16_t x) { return float(toRaw(x)) / 65535.0; }
highp uvec2 toRaw(uint16x2_t x) { return x.value; }
highp vec2 toNormalized(uint16x2_t x) { return vec2(toRaw(x)) / 65535.0; }
uint64_t toUint64(uint16_t x) {
uint64_t result;
result.value[0] = x.value;
result.value[1] = 0u;
return result;
}
uint16_t uint16FromFloat(highp float x) {
return uint16_t(uint(clamp(x, 0.0, 65535.0)));
}
`,
];
export const glsl_int16 = [
glsl_uint64,
`
struct int16_t {
highp int value;
};
struct int16x2_t {
highp ivec2 value;
};
int16_t mixLinear(int16_t x, int16_t y, highp float a) {
return int16_t(int(round(mix(float(x.value), float(y.value), a))));
}
highp int toRaw(int16_t x) { return x.value; }
highp ivec2 toRaw(int16x2_t x) { return x.value; }
uint64_t toUint64(int16_t x) {
uint64_t result;
result.value[0] = uint(x.value);
result.value[1] = uint(x.value >> 31);
return result;
}
int16_t int16FromFloat(highp float x) {
return int16_t(int(clamp(x, -32768.0, 32767.0)));
}
`,
];
export const glsl_uint32 = [
glsl_uint64,
`
struct uint32_t {
highp uint value;
};
uint32_t mixLinear(uint32_t x, uint32_t y, highp float a) {
return uint32_t(uint(round(mix(float(x.value), float(y.value), a))));
}
highp float toNormalized(uint32_t x) { return float(x.value) / 4294967295.0; }
highp uint toRaw(uint32_t x) { return x.value; }
uint64_t toUint64(uint32_t x) {
uint64_t result;
result.value[0] = x.value;
result.value[1] = 0u;
return result;
}
uint32_t uint32FromFloat(highp float x) {
return uint32_t(uint(clamp(x, 0.0, 4294967295.0)));
}
`,
];
export const glsl_int32 = [
glsl_uint64,
`
struct int32_t {
highp int value;
};
int32_t mixLinear(int32_t x, int32_t y, highp float a) {
return int32_t(int(round(mix(float(x.value), float(y.value), a))));
}
highp int toRaw(int32_t x) { return x.value; }
uint64_t toUint64(int32_t x) {
uint64_t result;
result.value[0] = uint(x.value);
result.value[1] = uint(x.value >> 31);
return result;
}
int32_t int32FromFloat(highp float x) {
return int32_t(int(clamp(x, 2147483648.0, 2147483647.0)));
}
`,
];
export const glsl_getFortranOrderIndex = `
highp int getFortranOrderIndex(ivec3 subscripts, ivec3 size) {
return subscripts.x + size.x * (subscripts.y + size.y * subscripts.z);
}
`;
export const glsl_log2Exact = `
highp uint log2Exact(highp uint i) {
highp uint r;
r = uint((i & 0xAAAAAAAAu) != 0u);
r |= uint((i & 0xFFFF0000u) != 0u) << 4;
r |= uint((i & 0xFF00FF00u) != 0u) << 3;
r |= uint((i & 0xF0F0F0F0u) != 0u) << 2;
r |= uint((i & 0xCCCCCCCCu) != 0u) << 1;
return r;
}
`;
// Clip line endpoints to the OpenGL viewing volume depth range.
// https://www.khronos.org/opengl/wiki/Vertex_Post-Processing#Clipping
//
// This is similar to the clipping that the OpenGL implementation itself would do for lines, except
// that we only clip based on `z`.
export const glsl_clipLineToDepthRange = `
bool clipLineToDepthRange(inout highp vec4 a, inout highp vec4 b) {
highp float tmin = 0.0, tmax = 1.0;
highp float k1 = b.w - a.w + a.z - b.z;
highp float k2 = a.w - b.w + a.z - b.z;
highp float q1 = (a.z - a.w) / k1;
highp float q2 = (a.z + a.w) / k2;
if (k1 > 0.0) tmin = max(tmin, q1);
else if (k1 < 0.0) tmax = min(tmax, q1);
if (k2 > 0.0) tmax = min(tmax, q2);
else if (k2 < 0.0) tmin = max(tmin, q2);
if (tmin <= tmax) {
highp vec4 tempA = a;
highp vec4 tempB = b;
a = mix(tempA, tempB, tmin);
b = mix(tempA, tempB, tmax);
return true;
}
return false;
}
`;
// https://stackoverflow.com/questions/4200224/random-noise-functions-for-glsl
export const glsl_simpleFloatHash = `
highp float simpleFloatHash(highp vec2 co) {
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
`;
export const glsl_shiftLeftSaturateUint32 = `
highp uint shiftLeftSaturate(highp uint x, int shiftAmount) {
highp uint result = x << shiftAmount;
if ((result >> shiftAmount) != x) return 0xffffffffu;
return result;
}
`;
export const glsl_addSaturateUint32 = `
highp uint addSaturate(highp uint x, highp uint y) {
highp uint result = x + y;
if (result < x) return 0xffffffffu;
return result;
}
`;
export const glsl_subtractSaturateUint32 = `
highp uint subtractSaturate(highp uint x, highp uint y) {
highp uint result = x - y;
if (result > x) return 0u;
return result;
}
`;
export const glsl_addSaturateInt32 = [
glsl_addSaturateUint32,
`
highp int addSaturate(highp int x, highp uint y) {
if (x >= 0) {
return int(min(addSaturate(y, uint(x)), 0x7fffffffu));
} else if (y >= uint(-x)) {
return int(min(y - uint(-x), 0x7fffffffu));
} else {
return -int(min(uint(-x) - y, 0x80000000u));
}
}
`,
];
export const glsl_subtractSaturateInt32 = [
glsl_addSaturateUint32,
`
highp int subtractSaturate(highp int x, highp uint y) {
if (x < 0) {
return -int(min(addSaturate(uint(-x), uint(y)), 0x80000000u));
} else if (uint(x) >= y) {
return x - int(y);
} else {
return -int(min(y - uint(x), 0x80000000u));
}
}
`,
];
export const glsl_modelToPixels = `
float modelToPixels(highp vec3 vertex, mat4 projection, mat4 viewModel, float projectionWidthPixels) {
float viewDistance = 1.0;
vec4 vertexViewOrigin = viewModel * vec4(vertex, 1.0);
vec4 vertexViewOffset = vertexViewOrigin + vec4(viewDistance, 0.0, 0.0, 0.0);
vec4 vertexClipOrigin = projection * vertexViewOrigin;
vec4 vertexClipOffset = projection * vertexViewOffset;
float projectionDistancePixels = abs(vertexClipOrigin.x - vertexClipOffset.x) * projectionWidthPixels;
float viewModalScale = length(viewModel[0].xyz);
float projectionScale = projectionDistancePixels / viewDistance;
float perspectiveScale = 1.0 / vertexClipOrigin.w;
return viewModalScale * projectionScale * perspectiveScale;
}
`;
export function getShaderType(dataType: DataType, numComponents = 1) {
switch (dataType) {
case DataType.FLOAT32:
if (numComponents === 1) {
return "float";
}
if (numComponents > 1 && numComponents <= 4) {
return `vec${numComponents}`;
}
break;
case DataType.UINT8:
case DataType.INT8:
case DataType.UINT16:
case DataType.INT16:
case DataType.UINT32:
case DataType.INT32:
case DataType.UINT64: {
const prefix = DATA_TYPE_SIGNED[dataType] ? "" : "u";
const bits = DATA_TYPE_BYTES[dataType] * 8;
if (numComponents === 1) {
return `${prefix}int${bits}_t`;
}
if (numComponents > 1 && numComponents * bits <= 32) {
return `${prefix}int${bits}x${numComponents}_t`;
}
break;
}
}
throw new Error(
`No shader type for ${DataType[dataType]}[${numComponents}].`,
);
}
export const dataTypeShaderDefinition: Record<DataType, ShaderCodePart> = {
[DataType.UINT8]: glsl_uint8,
[DataType.INT8]: glsl_int8,
[DataType.UINT16]: glsl_uint16,
[DataType.INT16]: glsl_int16,
[DataType.UINT32]: glsl_uint32,
[DataType.INT32]: glsl_int32,
[DataType.UINT64]: glsl_uint64,
[DataType.FLOAT32]: glsl_float,
};
export function getShaderVectorType(
typeName: "float" | "int" | "uint",
n: number,
) {
if (n === 1) return typeName;
if (typeName === "float") return `vec${n}`;
return `${typeName[0]}vec${n}`;
}
export const webglTypeSizeInBytes: { [webglType: number]: number } = {
[WebGL2RenderingContext.UNSIGNED_BYTE]: 1,
[WebGL2RenderingContext.BYTE]: 1,
[WebGL2RenderingContext.UNSIGNED_SHORT]: 2,
[WebGL2RenderingContext.SHORT]: 2,
[WebGL2RenderingContext.FLOAT]: 4,
[WebGL2RenderingContext.INT]: 4,
[WebGL2RenderingContext.UNSIGNED_INT]: 4,
};
export function defineVectorArrayVertexShaderInput(
builder: ShaderBuilder,
typeName: "float" | "int" | "uint",
attributeType: number,
normalized: boolean,
name: string,
vectorRank: number,
arraySize = 1,
) {
let numAttributes = 0;
let n = vectorRank * arraySize;
while (n > 0) {
const components = Math.min(4, n);
const t = getShaderVectorType(typeName, components);
n -= components;
builder.addAttribute("highp " + t, `a${name}${numAttributes}`);
++numAttributes;
}
n = vectorRank * arraySize;
let code = "";
for (let arrayIndex = 0; arrayIndex < arraySize; ++arrayIndex) {
code += `highp ${typeName}[${vectorRank}] get${name}${arrayIndex}() {
highp ${typeName}[${vectorRank}] result;
`;
for (let vectorIndex = 0; vectorIndex < vectorRank; ++vectorIndex) {
const i = arrayIndex * vectorRank + vectorIndex;
const attributeIndex = Math.floor(i / 4);
const componentIndex = i % 4;
code += ` result[${vectorIndex}] = a${name}${attributeIndex}`;
if (componentIndex !== 0 || i !== n - 1) {
code += `[${componentIndex}]`;
}
code += ";\n";
}
code += " return result;\n";
code += "}\n";
}
builder.addVertexCode(code);
const elementSize = webglTypeSizeInBytes[attributeType];
builder.addInitializer((shader) => {
const locations: AttributeIndex[] = [];
for (
let attributeIndex = 0;
attributeIndex < numAttributes;
++attributeIndex
) {
locations[attributeIndex] = shader.attribute(`a${name}${attributeIndex}`);
}
shader.vertexShaderInputBinders[name] = {
enable(divisor: number) {
const { gl } = shader;
for (
let attributeIndex = 0;
attributeIndex < numAttributes;
++attributeIndex
) {
const location = locations[attributeIndex];
gl.enableVertexAttribArray(location);
gl.vertexAttribDivisor(location, divisor);
}
},
disable() {
const { gl } = shader;
for (
let attributeIndex = 0;
attributeIndex < numAttributes;
++attributeIndex
) {
const location = locations[attributeIndex];
gl.vertexAttribDivisor(location, 0);
gl.disableVertexAttribArray(location);
}
},
bind(stride: number, offset: number) {
const { gl } = shader;
for (
let attributeIndex = 0;
attributeIndex < numAttributes;
++attributeIndex
) {
const location = locations[attributeIndex];
const numComponents = Math.min(4, n - 4 * attributeIndex);
if (typeName === "float") {
gl.vertexAttribPointer(
location,
/*size=*/ numComponents,
attributeType,
normalized,
stride,
offset,
);
} else {
gl.vertexAttribIPointer(
location,
/*size=*/ Math.min(4, n - 4 * attributeIndex),
attributeType,
stride,
offset,
);
}
offset += elementSize * numComponents;
}
},
};
});
}