nodeMaterialBuildState.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import { NodeMaterialBlockConnectionPointTypes } from "./Enums/nodeMaterialBlockConnectionPointTypes.js";
  2. import { NodeMaterialBlockTargets } from "./Enums/nodeMaterialBlockTargets.js";
  3. import { Effect } from "../effect.js";
  4. /**
  5. * Class used to store node based material build state
  6. */
  7. export class NodeMaterialBuildState {
  8. constructor() {
  9. /** Gets or sets a boolean indicating if the current state can emit uniform buffers */
  10. this.supportUniformBuffers = false;
  11. /**
  12. * Gets the list of emitted attributes
  13. */
  14. this.attributes = [];
  15. /**
  16. * Gets the list of emitted uniforms
  17. */
  18. this.uniforms = [];
  19. /**
  20. * Gets the list of emitted constants
  21. */
  22. this.constants = [];
  23. /**
  24. * Gets the list of emitted samplers
  25. */
  26. this.samplers = [];
  27. /**
  28. * Gets the list of emitted functions
  29. */
  30. this.functions = {};
  31. /**
  32. * Gets the list of emitted extensions
  33. */
  34. this.extensions = {};
  35. /**
  36. * Gets the list of emitted prePass outputs - if using the prepass
  37. */
  38. this.prePassOutput = {};
  39. /**
  40. * Gets the list of emitted counters
  41. */
  42. this.counters = {};
  43. /** @internal */
  44. this._attributeDeclaration = "";
  45. /** @internal */
  46. this._uniformDeclaration = "";
  47. /** @internal */
  48. this._constantDeclaration = "";
  49. /** @internal */
  50. this._samplerDeclaration = "";
  51. /** @internal */
  52. this._varyingTransfer = "";
  53. /** @internal */
  54. this._injectAtEnd = "";
  55. this._repeatableContentAnchorIndex = 0;
  56. /** @internal */
  57. this._builtCompilationString = "";
  58. /**
  59. * Gets the emitted compilation strings
  60. */
  61. this.compilationString = "";
  62. }
  63. /**
  64. * Finalize the compilation strings
  65. * @param state defines the current compilation state
  66. */
  67. finalize(state) {
  68. const emitComments = state.sharedData.emitComments;
  69. const isFragmentMode = this.target === NodeMaterialBlockTargets.Fragment;
  70. this.compilationString = `\n${emitComments ? "//Entry point\n" : ""}void main(void) {\n${this.compilationString}`;
  71. if (this._constantDeclaration) {
  72. this.compilationString = `\n${emitComments ? "//Constants\n" : ""}${this._constantDeclaration}\n${this.compilationString}`;
  73. }
  74. let functionCode = "";
  75. for (const functionName in this.functions) {
  76. functionCode += this.functions[functionName] + `\n`;
  77. }
  78. this.compilationString = `\n${functionCode}\n${this.compilationString}`;
  79. if (!isFragmentMode && this._varyingTransfer) {
  80. this.compilationString = `${this.compilationString}\n${this._varyingTransfer}`;
  81. }
  82. if (this._injectAtEnd) {
  83. this.compilationString = `${this.compilationString}\n${this._injectAtEnd}`;
  84. }
  85. this.compilationString = `${this.compilationString}\n}`;
  86. if (this.sharedData.varyingDeclaration) {
  87. this.compilationString = `\n${emitComments ? "//Varyings\n" : ""}${this.sharedData.varyingDeclaration}\n${this.compilationString}`;
  88. }
  89. if (this._samplerDeclaration) {
  90. this.compilationString = `\n${emitComments ? "//Samplers\n" : ""}${this._samplerDeclaration}\n${this.compilationString}`;
  91. }
  92. if (this._uniformDeclaration) {
  93. this.compilationString = `\n${emitComments ? "//Uniforms\n" : ""}${this._uniformDeclaration}\n${this.compilationString}`;
  94. }
  95. if (this._attributeDeclaration && !isFragmentMode) {
  96. this.compilationString = `\n${emitComments ? "//Attributes\n" : ""}${this._attributeDeclaration}\n${this.compilationString}`;
  97. }
  98. this.compilationString = "precision highp float;\n" + this.compilationString;
  99. this.compilationString = "#if defined(WEBGL2) || defined(WEBGPU)\nprecision highp sampler2DArray;\n#endif\n" + this.compilationString;
  100. if (isFragmentMode) {
  101. this.compilationString =
  102. "#if defined(PREPASS)\r\n#extension GL_EXT_draw_buffers : require\r\nlayout(location = 0) out highp vec4 glFragData[SCENE_MRT_COUNT];\r\nhighp vec4 gl_FragColor;\r\n#endif\r\n" +
  103. this.compilationString;
  104. }
  105. for (const extensionName in this.extensions) {
  106. const extension = this.extensions[extensionName];
  107. this.compilationString = `\n${extension}\n${this.compilationString}`;
  108. }
  109. this._builtCompilationString = this.compilationString;
  110. }
  111. /** @internal */
  112. get _repeatableContentAnchor() {
  113. return `###___ANCHOR${this._repeatableContentAnchorIndex++}___###`;
  114. }
  115. /**
  116. * @internal
  117. */
  118. _getFreeVariableName(prefix) {
  119. prefix = prefix.replace(/[^a-zA-Z_]+/g, "");
  120. if (this.sharedData.variableNames[prefix] === undefined) {
  121. this.sharedData.variableNames[prefix] = 0;
  122. // Check reserved words
  123. if (prefix === "output" || prefix === "texture") {
  124. return prefix + this.sharedData.variableNames[prefix];
  125. }
  126. return prefix;
  127. }
  128. else {
  129. this.sharedData.variableNames[prefix]++;
  130. }
  131. return prefix + this.sharedData.variableNames[prefix];
  132. }
  133. /**
  134. * @internal
  135. */
  136. _getFreeDefineName(prefix) {
  137. if (this.sharedData.defineNames[prefix] === undefined) {
  138. this.sharedData.defineNames[prefix] = 0;
  139. }
  140. else {
  141. this.sharedData.defineNames[prefix]++;
  142. }
  143. return prefix + this.sharedData.defineNames[prefix];
  144. }
  145. /**
  146. * @internal
  147. */
  148. _excludeVariableName(name) {
  149. this.sharedData.variableNames[name] = 0;
  150. }
  151. /**
  152. * @internal
  153. */
  154. _emit2DSampler(name) {
  155. if (this.samplers.indexOf(name) < 0) {
  156. this._samplerDeclaration += `uniform sampler2D ${name};\n`;
  157. this.samplers.push(name);
  158. }
  159. }
  160. /**
  161. * @internal
  162. */
  163. _emit2DArraySampler(name) {
  164. if (this.samplers.indexOf(name) < 0) {
  165. this._samplerDeclaration += `uniform sampler2DArray ${name};\n`;
  166. this.samplers.push(name);
  167. }
  168. }
  169. /**
  170. * @internal
  171. */
  172. _getGLType(type) {
  173. switch (type) {
  174. case NodeMaterialBlockConnectionPointTypes.Float:
  175. return "float";
  176. case NodeMaterialBlockConnectionPointTypes.Int:
  177. return "int";
  178. case NodeMaterialBlockConnectionPointTypes.Vector2:
  179. return "vec2";
  180. case NodeMaterialBlockConnectionPointTypes.Color3:
  181. case NodeMaterialBlockConnectionPointTypes.Vector3:
  182. return "vec3";
  183. case NodeMaterialBlockConnectionPointTypes.Color4:
  184. case NodeMaterialBlockConnectionPointTypes.Vector4:
  185. return "vec4";
  186. case NodeMaterialBlockConnectionPointTypes.Matrix:
  187. return "mat4";
  188. }
  189. return "";
  190. }
  191. /**
  192. * @internal
  193. */
  194. _emitExtension(name, extension, define = "") {
  195. if (this.extensions[name]) {
  196. return;
  197. }
  198. if (define) {
  199. extension = `#if ${define}\n${extension}\n#endif`;
  200. }
  201. this.extensions[name] = extension;
  202. }
  203. /**
  204. * @internal
  205. */
  206. _emitFunction(name, code, comments) {
  207. if (this.functions[name]) {
  208. return;
  209. }
  210. if (this.sharedData.emitComments) {
  211. code = comments + `\n` + code;
  212. }
  213. this.functions[name] = code;
  214. }
  215. /**
  216. * @internal
  217. */
  218. _emitCodeFromInclude(includeName, comments, options) {
  219. if (options && options.repeatKey) {
  220. return `#include<${includeName}>${options.substitutionVars ? "(" + options.substitutionVars + ")" : ""}[0..${options.repeatKey}]\n`;
  221. }
  222. let code = Effect.IncludesShadersStore[includeName] + "\n";
  223. if (this.sharedData.emitComments) {
  224. code = comments + `\n` + code;
  225. }
  226. if (!options) {
  227. return code;
  228. }
  229. if (options.replaceStrings) {
  230. for (let index = 0; index < options.replaceStrings.length; index++) {
  231. const replaceString = options.replaceStrings[index];
  232. code = code.replace(replaceString.search, replaceString.replace);
  233. }
  234. }
  235. return code;
  236. }
  237. /**
  238. * @internal
  239. */
  240. _emitFunctionFromInclude(includeName, comments, options, storeKey = "") {
  241. const key = includeName + storeKey;
  242. if (this.functions[key]) {
  243. return;
  244. }
  245. if (!options || (!options.removeAttributes && !options.removeUniforms && !options.removeVaryings && !options.removeIfDef && !options.replaceStrings)) {
  246. if (options && options.repeatKey) {
  247. this.functions[key] = `#include<${includeName}>${options.substitutionVars ? "(" + options.substitutionVars + ")" : ""}[0..${options.repeatKey}]\n`;
  248. }
  249. else {
  250. this.functions[key] = `#include<${includeName}>${options?.substitutionVars ? "(" + options?.substitutionVars + ")" : ""}\n`;
  251. }
  252. if (this.sharedData.emitComments) {
  253. this.functions[key] = comments + `\n` + this.functions[key];
  254. }
  255. return;
  256. }
  257. this.functions[key] = Effect.IncludesShadersStore[includeName];
  258. if (this.sharedData.emitComments) {
  259. this.functions[key] = comments + `\n` + this.functions[key];
  260. }
  261. if (options.removeIfDef) {
  262. this.functions[key] = this.functions[key].replace(/^\s*?#ifdef.+$/gm, "");
  263. this.functions[key] = this.functions[key].replace(/^\s*?#endif.*$/gm, "");
  264. this.functions[key] = this.functions[key].replace(/^\s*?#else.*$/gm, "");
  265. this.functions[key] = this.functions[key].replace(/^\s*?#elif.*$/gm, "");
  266. }
  267. if (options.removeAttributes) {
  268. this.functions[key] = this.functions[key].replace(/\s*?attribute .+?;/g, "\n");
  269. }
  270. if (options.removeUniforms) {
  271. this.functions[key] = this.functions[key].replace(/\s*?uniform .*?;/g, "\n");
  272. }
  273. if (options.removeVaryings) {
  274. this.functions[key] = this.functions[key].replace(/\s*?(varying|in) .+?;/g, "\n");
  275. }
  276. if (options.replaceStrings) {
  277. for (let index = 0; index < options.replaceStrings.length; index++) {
  278. const replaceString = options.replaceStrings[index];
  279. this.functions[key] = this.functions[key].replace(replaceString.search, replaceString.replace);
  280. }
  281. }
  282. }
  283. /**
  284. * @internal
  285. */
  286. _registerTempVariable(name) {
  287. if (this.sharedData.temps.indexOf(name) !== -1) {
  288. return false;
  289. }
  290. this.sharedData.temps.push(name);
  291. return true;
  292. }
  293. /**
  294. * @internal
  295. */
  296. _emitVaryingFromString(name, type, define = "", notDefine = false) {
  297. if (this.sharedData.varyings.indexOf(name) !== -1) {
  298. return false;
  299. }
  300. this.sharedData.varyings.push(name);
  301. if (define) {
  302. if (define.startsWith("defined(")) {
  303. this.sharedData.varyingDeclaration += `#if ${define}\n`;
  304. }
  305. else {
  306. this.sharedData.varyingDeclaration += `${notDefine ? "#ifndef" : "#ifdef"} ${define}\n`;
  307. }
  308. }
  309. this.sharedData.varyingDeclaration += `varying ${type} ${name};\n`;
  310. if (define) {
  311. this.sharedData.varyingDeclaration += `#endif\n`;
  312. }
  313. return true;
  314. }
  315. /**
  316. * @internal
  317. */
  318. _emitUniformFromString(name, type, define = "", notDefine = false) {
  319. if (this.uniforms.indexOf(name) !== -1) {
  320. return;
  321. }
  322. this.uniforms.push(name);
  323. if (define) {
  324. if (define.startsWith("defined(")) {
  325. this._uniformDeclaration += `#if ${define}\n`;
  326. }
  327. else {
  328. this._uniformDeclaration += `${notDefine ? "#ifndef" : "#ifdef"} ${define}\n`;
  329. }
  330. }
  331. this._uniformDeclaration += `uniform ${type} ${name};\n`;
  332. if (define) {
  333. this._uniformDeclaration += `#endif\n`;
  334. }
  335. }
  336. /**
  337. * @internal
  338. */
  339. _emitFloat(value) {
  340. if (value.toString() === value.toFixed(0)) {
  341. return `${value}.0`;
  342. }
  343. return value.toString();
  344. }
  345. }
  346. //# sourceMappingURL=nodeMaterialBuildState.js.map