-
-
Notifications
You must be signed in to change notification settings - Fork 401
Expand file tree
/
Copy pathPreprocessor.ts
More file actions
197 lines (175 loc) · 6.31 KB
/
Preprocessor.ts
File metadata and controls
197 lines (175 loc) · 6.31 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
import { Logger, ShaderPass } from "@galacean/engine";
/** @ts-ignore */
import { ShaderLib } from "@galacean/engine";
export enum MacroValueType {
Number, // 1, 1.1
Symbol, // variable name
MemberAccess, // member access, e.g. input.v_uv, v.rgb
FunctionCall, // function call, e.g. clamp(a, 0.0, 1.0)
Other // shaderLab does not check this
}
export interface MacroDefineInfo {
isFunction: boolean;
name: string;
value: string;
valueType: MacroValueType;
params: string[];
functionCallName: string;
}
export interface MacroDefineList {
[macroName: string]: MacroDefineInfo[];
}
export class Preprocessor {
private static readonly _includeReg = /^[ \t]*#include +"([\w\d./]+)"/gm;
private static readonly _macroRegex =
/^\s*#define\s+(\w+)[ ]*(\(([^)]*)\))?[ ]+(\(?\w+\)?.*?)(?:\/\/.*|\/\*.*?\*\/)?\s*$/gm;
private static readonly _symbolReg = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
private static readonly _memberAccessReg = /^([a-zA-Z_][a-zA-Z0-9_]*)(\.[a-zA-Z_][a-zA-Z0-9_]*)+$/;
private static readonly _funcCallReg = /^([a-zA-Z_][a-zA-Z0-9_]*)\s*\((.*)\)$/;
private static readonly _macroDefineIncludeMap = new Map<string, MacroDefineList>();
/**
* @internal
*/
static _repeatIncludeSet = new Set<string>();
static parse(
source: string,
basePathForIncludeKey: string,
outMacroDefineList: MacroDefineList,
parseMacro = true
): string {
if (parseMacro) {
this._parseMacroDefines(source, outMacroDefineList);
}
return source.replace(this._includeReg, (_, includeName) =>
this._replace(includeName, basePathForIncludeKey, outMacroDefineList)
);
}
static getReferenceSymbolNames(macroDefineList: MacroDefineList, macroName: string, out: string[]): void {
out.length = 0;
const infos = macroDefineList[macroName];
if (!infos) return;
for (let i = 0; i < infos.length; i++) {
const info = infos[i];
const valueType = info.valueType;
if (valueType === MacroValueType.FunctionCall || valueType === MacroValueType.Symbol) {
const referencedName = valueType === MacroValueType.FunctionCall ? info.functionCallName : info.value;
if (info.params.indexOf(referencedName) !== -1) continue;
if (out.indexOf(referencedName) === -1) out.push(referencedName);
} else if (valueType === MacroValueType.MemberAccess) {
// Extract root symbol: "input.v_uv" → "input"
const rootName = info.value.substring(0, info.value.indexOf("."));
if (out.indexOf(rootName) === -1) out.push(rootName);
} else if (valueType === MacroValueType.Other) {
// #if _VERBOSE
Logger.warn(
`Warning: Macro "${info.name}" has an unrecognized value "${info.value}". ShaderLab does not validate this type.`
);
// #endif
}
}
}
private static _isNumber(str: string): boolean {
return !isNaN(Number(str));
}
private static _isExist(list: MacroDefineInfo[], item: MacroDefineInfo): boolean {
return list.some(
(e) =>
e.valueType === item.valueType &&
e.value === item.value &&
e.isFunction === item.isFunction &&
e.functionCallName === item.functionCallName &&
e.params.length === item.params.length &&
e.params.every((p, i) => p === item.params[i])
);
}
private static _parseMacroDefines(source: string, outMacroList: MacroDefineList): void {
let match: RegExpExecArray | null;
this._macroRegex.lastIndex = 0;
while ((match = this._macroRegex.exec(source)) !== null) {
const [, name, paramsGroup, paramsStr, valueRaw] = match;
const isFunction = !!paramsGroup && !!valueRaw;
const params =
isFunction && paramsStr
? paramsStr
.split(",")
.map((p) => p.trim())
.filter(Boolean)
: [];
const value = valueRaw ? valueRaw.trim() : "";
let valueType = MacroValueType.Other;
let functionCallName = "";
if (this._isNumber(value)) {
valueType = MacroValueType.Number;
} else if (this._symbolReg.test(value)) {
valueType = MacroValueType.Symbol;
} else if (this._memberAccessReg.test(value)) {
valueType = MacroValueType.MemberAccess;
} else {
const callMatch = this._funcCallReg.exec(value);
if (callMatch) {
valueType = MacroValueType.FunctionCall;
functionCallName = callMatch[1];
}
}
const info: MacroDefineInfo = {
isFunction,
name,
value,
valueType,
params,
functionCallName
};
const arr = outMacroList[name];
if (arr) {
if (!this._isExist(arr, info)) arr.push(info);
} else {
outMacroList[name] = [info];
}
}
}
private static _mergeMacroDefineLists(from: MacroDefineList, to: MacroDefineList): void {
for (const macroName in from) {
if (to[macroName]) {
const target = to[macroName];
const src = from[macroName];
for (let i = 0; i < src.length; i++) {
const info = src[i];
if (!this._isExist(target, info)) target.push(info);
}
} else {
to[macroName] = from[macroName];
}
}
}
private static _replace(
includeName: string,
basePathForIncludeKey: string,
outMacroDefineList: MacroDefineList
): string {
let path: string;
if (includeName[0] === ".") {
// @ts-ignore
path = new URL(includeName, basePathForIncludeKey).href.substring(ShaderPass._shaderRootPath.length);
} else {
path = includeName;
}
const chunk = (ShaderLib as any)[path];
if (!chunk) {
Logger.error(`Shader slice "${path}" not founded.`);
return "";
}
if (this._repeatIncludeSet.has(path)) {
Logger.warn(`Shader slice "${path}" is included multiple times.`);
}
this._repeatIncludeSet.add(path);
if (this._macroDefineIncludeMap.has(path)) {
this._mergeMacroDefineLists(this._macroDefineIncludeMap.get(path)!, outMacroDefineList);
} else {
const chunkMacroDefineList: MacroDefineList = {};
this._parseMacroDefines(chunk, chunkMacroDefineList);
this._macroDefineIncludeMap.set(path, chunkMacroDefineList);
this._mergeMacroDefineLists(chunkMacroDefineList, outMacroDefineList);
}
return this.parse(chunk, basePathForIncludeKey, outMacroDefineList, false);
}
}