nodeMaterialBuildState.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import { NodeMaterialBlockConnectionPointTypes } from './nodeMaterialBlockConnectionPointTypes';
  2. import { NodeMaterialBlockTargets } from './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 uniform buffers
  22. */
  23. public uniformBuffers = 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 target of the compilation state
  34. */
  35. public target: NodeMaterialBlockTargets;
  36. /**
  37. * Gets the list of emitted counters
  38. */
  39. public counters: { [key: string]: number } = {};
  40. /**
  41. * Shared data between multiple NodeMaterialBuildState instances
  42. */
  43. public sharedData: NodeMaterialBuildStateSharedData;
  44. /** @hidden */
  45. public _vertexState: NodeMaterialBuildState;
  46. /** @hidden */
  47. public _attributeDeclaration = "";
  48. /** @hidden */
  49. public _uniformDeclaration = "";
  50. /** @hidden */
  51. public _samplerDeclaration = "";
  52. /** @hidden */
  53. public _varyingTransfer = "";
  54. private _repeatableContentAnchorIndex = 0;
  55. /** @hidden */
  56. public _builtCompilationString = "";
  57. /**
  58. * Gets the emitted compilation strings
  59. */
  60. public compilationString = "";
  61. /**
  62. * Finalize the compilation strings
  63. * @param state defines the current compilation state
  64. */
  65. public finalize(state: NodeMaterialBuildState) {
  66. let emitComments = state.sharedData.emitComments;
  67. let isFragmentMode = (this.target === NodeMaterialBlockTargets.Fragment);
  68. this.compilationString = `\r\n${emitComments ? "//Entry point\r\n" : ""}void main(void) {\r\n${this.compilationString}`;
  69. let functionCode = "";
  70. for (var functionName in this.functions) {
  71. functionCode += this.functions[functionName] + `\r\n`;
  72. }
  73. this.compilationString = `\r\n${functionCode}\r\n${this.compilationString}`;
  74. if (!isFragmentMode && this._varyingTransfer) {
  75. this.compilationString = `${this.compilationString}\r\n${this._varyingTransfer}`;
  76. }
  77. this.compilationString = `${this.compilationString}\r\n}`;
  78. if (this.sharedData.varyingDeclaration) {
  79. this.compilationString = `\r\n${emitComments ? "//Varyings\r\n" : ""}${this.sharedData.varyingDeclaration}\r\n${this.compilationString}`;
  80. }
  81. if (this._samplerDeclaration) {
  82. this.compilationString = `\r\n${emitComments ? "//Samplers\r\n" : ""}${this._samplerDeclaration}\r\n${this.compilationString}`;
  83. }
  84. if (this._uniformDeclaration) {
  85. this.compilationString = `\r\n${emitComments ? "//Uniforms\r\n" : ""}${this._uniformDeclaration}\r\n${this.compilationString}`;
  86. }
  87. if (this._attributeDeclaration && !isFragmentMode) {
  88. this.compilationString = `\r\n${emitComments ? "//Attributes\r\n" : ""}${this._attributeDeclaration}\r\n${this.compilationString}`;
  89. }
  90. this._builtCompilationString = this.compilationString;
  91. }
  92. /** @hidden */
  93. public get _repeatableContentAnchor(): string {
  94. return `###___ANCHOR${this._repeatableContentAnchorIndex++}___###`;
  95. }
  96. /** @hidden */
  97. public _getFreeVariableName(prefix: string): string {
  98. if (this.sharedData.variableNames[prefix] === undefined) {
  99. this.sharedData.variableNames[prefix] = 0;
  100. // Check reserved words
  101. if (prefix === "output" || prefix === "texture") {
  102. return prefix + this.sharedData.variableNames[prefix];
  103. }
  104. return prefix;
  105. } else {
  106. this.sharedData.variableNames[prefix]++;
  107. }
  108. return prefix + this.sharedData.variableNames[prefix];
  109. }
  110. /** @hidden */
  111. public _getFreeDefineName(prefix: string): string {
  112. if (this.sharedData.defineNames[prefix] === undefined) {
  113. this.sharedData.defineNames[prefix] = 0;
  114. } else {
  115. this.sharedData.defineNames[prefix]++;
  116. }
  117. return prefix + this.sharedData.defineNames[prefix];
  118. }
  119. /** @hidden */
  120. public _excludeVariableName(name: string) {
  121. this.sharedData.variableNames[name] = 0;
  122. }
  123. /** @hidden */
  124. public _getGLType(type: NodeMaterialBlockConnectionPointTypes): string {
  125. switch (type) {
  126. case NodeMaterialBlockConnectionPointTypes.Float:
  127. return "float";
  128. case NodeMaterialBlockConnectionPointTypes.Int:
  129. return "int";
  130. case NodeMaterialBlockConnectionPointTypes.Vector2:
  131. return "vec2";
  132. case NodeMaterialBlockConnectionPointTypes.Color3:
  133. case NodeMaterialBlockConnectionPointTypes.Vector3:
  134. return "vec3";
  135. case NodeMaterialBlockConnectionPointTypes.Color4:
  136. case NodeMaterialBlockConnectionPointTypes.Vector4:
  137. return "vec4";
  138. case NodeMaterialBlockConnectionPointTypes.Matrix:
  139. return "mat4";
  140. }
  141. return "";
  142. }
  143. /** @hidden */
  144. public _emitFunction(name: string, code: string, comments: string) {
  145. if (this.functions[name]) {
  146. return;
  147. }
  148. if (this.sharedData.emitComments) {
  149. code = comments + `\r\n` + code;
  150. }
  151. this.functions[name] = code;
  152. }
  153. /** @hidden */
  154. public _emitCodeFromInclude(includeName: string, comments: string, options?: {
  155. replaceStrings?: { search: RegExp, replace: string }[],
  156. repeatKey?: string
  157. }) {
  158. if (options && options.repeatKey) {
  159. return `#include<${includeName}>[0..${options.repeatKey}]\r\n`;
  160. }
  161. let code = Effect.IncludesShadersStore[includeName] + "\r\n";
  162. if (this.sharedData.emitComments) {
  163. code = comments + `\r\n` + code;
  164. }
  165. if (!options) {
  166. return code;
  167. }
  168. if (options.replaceStrings) {
  169. for (var index = 0; index < options.replaceStrings.length; index++) {
  170. let replaceString = options.replaceStrings[index];
  171. code = code.replace(replaceString.search, replaceString.replace);
  172. }
  173. }
  174. return code;
  175. }
  176. /** @hidden */
  177. public _emitFunctionFromInclude(includeName: string, comments: string, options?: {
  178. repeatKey?: string,
  179. removeAttributes?: boolean,
  180. removeUniforms?: boolean,
  181. removeVaryings?: boolean,
  182. removeIfDef?: boolean,
  183. replaceStrings?: { search: RegExp, replace: string }[],
  184. }, storeKey: string = "") {
  185. let key = includeName + storeKey;
  186. if (this.functions[key]) {
  187. return;
  188. }
  189. if (!options || (!options.removeAttributes && !options.removeUniforms && !options.removeVaryings && !options.removeIfDef && !options.replaceStrings)) {
  190. if (options && options.repeatKey) {
  191. this.functions[key] = `#include<${includeName}>[0..${options.repeatKey}]\r\n`;
  192. } else {
  193. this.functions[key] = `#include<${includeName}>\r\n`;
  194. }
  195. if (this.sharedData.emitComments) {
  196. this.functions[key] = comments + `\r\n` + this.functions[key];
  197. }
  198. return;
  199. }
  200. this.functions[key] = Effect.IncludesShadersStore[includeName];
  201. if (this.sharedData.emitComments) {
  202. this.functions[key] = comments + `\r\n` + this.functions[key];
  203. }
  204. if (options.removeIfDef) {
  205. this.functions[key] = this.functions[key].replace(/^\s*?#ifdef.+$/gm, "");
  206. this.functions[key] = this.functions[key].replace(/^\s*?#endif.*$/gm, "");
  207. this.functions[key] = this.functions[key].replace(/^\s*?#else.*$/gm, "");
  208. this.functions[key] = this.functions[key].replace(/^\s*?#elif.*$/gm, "");
  209. }
  210. if (options.removeAttributes) {
  211. this.functions[key] = this.functions[key].replace(/^\s*?attribute.+$/gm, "");
  212. }
  213. if (options.removeUniforms) {
  214. this.functions[key] = this.functions[key].replace(/^\s*?uniform.+$/gm, "");
  215. }
  216. if (options.removeVaryings) {
  217. this.functions[key] = this.functions[key].replace(/^\s*?varying.+$/gm, "");
  218. }
  219. if (options.replaceStrings) {
  220. for (var index = 0; index < options.replaceStrings.length; index++) {
  221. let replaceString = options.replaceStrings[index];
  222. this.functions[key] = this.functions[key].replace(replaceString.search, replaceString.replace);
  223. }
  224. }
  225. }
  226. /** @hidden */
  227. public _registerTempVariable(name: string) {
  228. if (this.sharedData.temps.indexOf(name) !== -1) {
  229. return false;
  230. }
  231. this.sharedData.temps.push(name);
  232. return true;
  233. }
  234. /** @hidden */
  235. public _emitVaryingFromString(name: string, type: string, define: string = "", notDefine = false) {
  236. if (this.sharedData.varyings.indexOf(name) !== -1) {
  237. return false;
  238. }
  239. this.sharedData.varyings.push(name);
  240. if (define) {
  241. if (StringTools.StartsWith(define, "defined(")) {
  242. this.sharedData.varyingDeclaration += `#if ${define}\r\n`;
  243. } else {
  244. this.sharedData.varyingDeclaration += `${notDefine ? "#ifndef" : "#ifdef"} ${define}\r\n`;
  245. }
  246. }
  247. this.sharedData.varyingDeclaration += `varying ${type} ${name};\r\n`;
  248. if (define) {
  249. this.sharedData.varyingDeclaration += `#endif\r\n`;
  250. }
  251. return true;
  252. }
  253. /** @hidden */
  254. public _emitUniformFromString(name: string, type: string, define: string = "", notDefine = false) {
  255. if (this.uniforms.indexOf(name) !== -1) {
  256. return;
  257. }
  258. this.uniforms.push(name);
  259. if (define) {
  260. this._uniformDeclaration += `${notDefine ? "#ifndef" : "#ifdef"} ${define}\r\n`;
  261. }
  262. this._uniformDeclaration += `uniform ${type} ${name};\r\n`;
  263. if (define) {
  264. this._uniformDeclaration += `#endif\r\n`;
  265. }
  266. }
  267. }