uniform.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Mat3, Mat4, Vec2, Vec3, Vec4 } from '../../mol-math/linear-algebra';
  7. import { ValueCell } from '../../mol-util';
  8. import { GLRenderingContext } from './compat';
  9. import { RenderableSchema } from '../../mol-gl/renderable/schema';
  10. import { ValueOf } from '../../mol-util/type-helpers';
  11. import { deepClone } from '../../mol-util/object';
  12. export type UniformKindValue = {
  13. 'b': boolean; 'b[]': boolean[]
  14. 'f': number; 'f[]': number[]
  15. 'i': number; 'i[]': number[]
  16. 'v2': Vec2; 'v2[]': number[]
  17. 'v3': Vec3; 'v3[]': number[]
  18. 'v4': Vec4; 'v4[]': number[]
  19. 'm3': Mat3; 'm3[]': number[]
  20. 'm4': Mat4; 'm4[]': number[]
  21. 't': number; 't[]': number[]
  22. }
  23. export type UniformKind = keyof UniformKindValue
  24. export type UniformType = ValueOf<UniformKindValue>
  25. export type UniformValues = { [k: string]: ValueCell<UniformType> }
  26. export type UniformsList = [string, ValueCell<UniformType>][]
  27. export function getUniformType(gl: GLRenderingContext, kind: UniformKind) {
  28. switch (kind) {
  29. case 'b': case 'b[]': return gl.BOOL;
  30. case 'f': case 'f[]': return gl.FLOAT;
  31. case 'i': case 'i[]': return gl.INT;
  32. case 'v2': case 'v2[]': return gl.FLOAT_VEC2;
  33. case 'v3': case 'v3[]': return gl.FLOAT_VEC3;
  34. case 'v4': case 'v4[]': return gl.FLOAT_VEC4;
  35. case 'm3': case 'm3[]': return gl.FLOAT_MAT3;
  36. case 'm4': case 'm4[]': return gl.FLOAT_MAT4;
  37. default: console.error(`unknown uniform kind '${kind}'`);
  38. }
  39. }
  40. export function isArrayUniform(kind: UniformKind) {
  41. return kind.endsWith('[]');
  42. }
  43. export type UniformSetter = (gl: GLRenderingContext, location: number, value: any) => void
  44. export type UniformSetters = { [k: string]: UniformSetter }
  45. function uniform1f(gl: GLRenderingContext, location: number, value: any) { gl.uniform1f(location, value); }
  46. function uniform1fv(gl: GLRenderingContext, location: number, value: any) { gl.uniform1fv(location, value); }
  47. function uniform1i(gl: GLRenderingContext, location: number, value: any) { gl.uniform1i(location, value); }
  48. function uniform1iv(gl: GLRenderingContext, location: number, value: any) { gl.uniform1iv(location, value); }
  49. function uniform2fv(gl: GLRenderingContext, location: number, value: any) { gl.uniform2fv(location, value); }
  50. function uniform3fv(gl: GLRenderingContext, location: number, value: any) { gl.uniform3fv(location, value); }
  51. function uniform4fv(gl: GLRenderingContext, location: number, value: any) { gl.uniform4fv(location, value); }
  52. function uniformMatrix3fv(gl: GLRenderingContext, location: number, value: any) { gl.uniformMatrix3fv(location, false, value); }
  53. function uniformMatrix4fv(gl: GLRenderingContext, location: number, value: any) { gl.uniformMatrix4fv(location, false, value); }
  54. function getUniformSetter(kind: UniformKind): UniformSetter {
  55. switch (kind) {
  56. case 'f': return uniform1f;
  57. case 'f[]': return uniform1fv;
  58. case 'i': case 't': case 'b': return uniform1i;
  59. case 'i[]': case 't[]': case 'b[]': return uniform1iv;
  60. case 'v2': case 'v2[]': return uniform2fv;
  61. case 'v3': case 'v3[]': return uniform3fv;
  62. case 'v4': case 'v4[]': return uniform4fv;
  63. case 'm3': case 'm3[]': return uniformMatrix3fv;
  64. case 'm4': case 'm4[]': return uniformMatrix4fv;
  65. }
  66. }
  67. export function getUniformSetters(schema: RenderableSchema) {
  68. const setters: UniformSetters = {};
  69. Object.keys(schema).forEach(k => {
  70. const spec = schema[k];
  71. if (spec.type === 'uniform') {
  72. setters[k] = getUniformSetter(spec.kind);
  73. } else if (spec.type === 'texture') {
  74. setters[k] = getUniformSetter('t');
  75. }
  76. });
  77. return setters;
  78. }
  79. export function getUniformGlslType(kind: UniformKind): string {
  80. switch (kind) {
  81. case 'f': return 'float';
  82. case 'i': return 'int';
  83. case 't': return 'sampler2D';
  84. case 'b': return 'bool';
  85. case 'v2': return 'vec2';
  86. case 'v3': return 'vec3';
  87. case 'v4': return 'vec4';
  88. case 'm3': return 'mat3';
  89. case 'm4': return 'mat4';
  90. }
  91. throw new Error(`${kind} has no primitive GLSL type.`);
  92. }
  93. export function isUniformValueScalar(kind: UniformKind): boolean {
  94. switch (kind) {
  95. case 'f':
  96. case 'i':
  97. case 'b':
  98. return true;
  99. default:
  100. return false;
  101. }
  102. }
  103. export function cloneUniformValues(uniformValues: UniformValues): UniformValues {
  104. const clonedValues: UniformValues = {};
  105. Object.keys(uniformValues).forEach(k => {
  106. clonedValues[k] = ValueCell.create(deepClone(uniformValues[k].ref.value));
  107. });
  108. return clonedValues;
  109. }