onnxruntime

Форк
0
103 строки · 2.3 Кб
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
// Licensed under the MIT License.
3

4
/**
5
 * represent a version irrelevant abstraction of for GLSL source code
6
 */
7
export interface Glsl {
8
  readonly version: string;
9
  readonly attribute: string;
10
  readonly varyingVertex: string;
11
  readonly varyingFrag: string;
12
  readonly texture2D: string;
13
  readonly output: string;
14
  readonly outputDeclaration: string;
15
}
16

17
const GLSL_ES_2_0: Glsl = {
18
  version: '',
19
  attribute: 'attribute',
20
  varyingVertex: 'varying',
21
  varyingFrag: 'varying',
22
  texture2D: 'texture2D',
23
  output: 'gl_FragColor',
24
  outputDeclaration: '',
25
};
26
const GLSL_ES_3_0: Glsl = {
27
  version: '#version 300 es',
28
  attribute: 'in',
29
  varyingVertex: 'out',
30
  varyingFrag: 'in',
31
  texture2D: 'texture',
32
  output: 'outputColor',
33
  outputDeclaration: 'out vec4 outputColor;',
34
};
35

36
export function getGlsl(version: 1 | 2) {
37
  return version === 1 ? GLSL_ES_2_0 : GLSL_ES_3_0;
38
}
39

40
export function getVertexShaderSource(version: 1 | 2): string {
41
  const glsl = getGlsl(version);
42
  return `${glsl.version}
43
      precision highp float;
44
      ${glsl.attribute} vec3 position;
45
      ${glsl.attribute} vec2 textureCoord;
46

47
      ${glsl.varyingVertex} vec2 TexCoords;
48

49
      void main()
50
      {
51
          gl_Position = vec4(position, 1.0);
52
          TexCoords = textureCoord;
53
      }`;
54
}
55

56
export function getFragShaderPreamble(version: 1 | 2): string {
57
  const glsl = getGlsl(version);
58
  return `${glsl.version}
59
    precision highp float;
60
    precision highp int;
61
    precision highp sampler2D;
62
    ${glsl.varyingFrag} vec2 TexCoords;
63
    ${glsl.outputDeclaration}
64
    const vec2 halfCR = vec2(0.5, 0.5);
65

66
    // Custom vector types to handle higher dimenalities.
67
    struct ivec5
68
    {
69
      int x;
70
      int y;
71
      int z;
72
      int w;
73
      int u;
74
    };
75

76
    struct ivec6
77
    {
78
      int x;
79
      int y;
80
      int z;
81
      int w;
82
      int u;
83
      int v;
84
    };
85

86
    int imod(int x, int y) {
87
      return x - y * (x / y);
88
    }
89

90
    `;
91
}
92

93
export function getDefaultFragShaderMain(version: 1 | 2, outputShapeLength: number): string {
94
  const glsl = getGlsl(version);
95
  return `
96
  void main() {
97
    int indices[${outputShapeLength}];
98
    toVec(TexCoords, indices);
99
    vec4 result = vec4(process(indices));
100
    ${glsl.output} = result;
101
  }
102
  `;
103
}
104

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.