nodeMaterialBuildState.ts 13 KB

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