onnxruntime

Форк
0
/
glsl-function-inliner.ts 
53 строки · 1.9 Кб
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
// Licensed under the MIT License.
3

4
const INLINE_FUNC_DEF_REGEX = /@inline[\s\n\r]+(\w+)[\s\n\r]+([0-9a-zA-Z_]+)\s*\(([^)]*)\)\s*{(([^}]|[\n\r])*)}/gm;
5
const FUNC_CALL_REGEX = '(\\w+)?\\s+([_0-9a-zA-Z]+)\\s+=\\s+__FUNC__\\((.*)\\)\\s*;';
6
/**
7
 * GLSL preprocessor responsible for resolving @inline directives
8
 */
9
export function replaceInlines(script: string): string {
10
  const inlineDefs: { [name: string]: { params: Array<{ type: string; name: string } | null>; body: string } } = {};
11
  let match;
12
  while ((match = INLINE_FUNC_DEF_REGEX.exec(script)) !== null) {
13
    const params = match[3]
14
      .split(',')
15
      .map((s) => {
16
        const tokens = s.trim().split(' ');
17
        if (tokens && tokens.length === 2) {
18
          return { type: tokens[0], name: tokens[1] };
19
        }
20
        return null;
21
      })
22
      .filter((v) => v !== null);
23
    inlineDefs[match[2]] = { params, body: match[4] };
24
  }
25
  for (const name in inlineDefs) {
26
    const regexString = FUNC_CALL_REGEX.replace('__FUNC__', name);
27
    const regex = new RegExp(regexString, 'gm');
28
    while ((match = regex.exec(script)) !== null) {
29
      const type = match[1];
30
      const variable = match[2];
31
      const params = match[3].split(',');
32
      const declLine = type ? `${type} ${variable};` : '';
33
      let newBody: string = inlineDefs[name].body;
34
      let paramRedecLine = '';
35
      inlineDefs[name].params.forEach((v, i) => {
36
        if (v) {
37
          paramRedecLine += `${v.type} ${v.name} = ${params[i]};\n`;
38
        }
39
      });
40
      newBody = `${paramRedecLine}\n ${newBody}`;
41
      newBody = newBody.replace('return', `${variable} = `);
42
      const replacement = `
43
      ${declLine}
44
      {
45
        ${newBody}
46
      }
47
      `;
48
      script = script.replace(match[0], replacement);
49
    }
50
  }
51
  script = script.replace(INLINE_FUNC_DEF_REGEX, '');
52
  return script;
53
}
54

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

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

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

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