babylon.effect.ts 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. module BABYLON {
  2. export class EffectFallbacks {
  3. private _defines: {[key: string]: Array<String>} = {};
  4. private _currentRank = 32;
  5. private _maxRank = -1;
  6. private _mesh: AbstractMesh;
  7. private _meshRank: number;
  8. public unBindMesh() {
  9. this._mesh = null;
  10. }
  11. public addFallback(rank: number, define: string): void {
  12. if (!this._defines[rank]) {
  13. if (rank < this._currentRank) {
  14. this._currentRank = rank;
  15. }
  16. if (rank > this._maxRank) {
  17. this._maxRank = rank;
  18. }
  19. this._defines[rank] = new Array<String>();
  20. }
  21. this._defines[rank].push(define);
  22. }
  23. public addCPUSkinningFallback(rank: number, mesh: BABYLON.AbstractMesh) {
  24. this._meshRank = rank;
  25. this._mesh = mesh;
  26. if (rank < this._currentRank) {
  27. this._currentRank = rank;
  28. }
  29. if (rank > this._maxRank) {
  30. this._maxRank = rank;
  31. }
  32. }
  33. public get isMoreFallbacks(): boolean {
  34. return this._currentRank <= this._maxRank;
  35. }
  36. public reduce(currentDefines: string): string {
  37. // First we try to switch to CPU skinning
  38. if (this._mesh && this._mesh.computeBonesUsingShaders && this._mesh.numBoneInfluencers > 0) {
  39. this._mesh.computeBonesUsingShaders = false;
  40. currentDefines = currentDefines.replace("#define NUM_BONE_INFLUENCERS " + this._mesh.numBoneInfluencers, "#define NUM_BONE_INFLUENCERS 0");
  41. Tools.Log("Falling back to CPU skinning for " + this._mesh.name);
  42. var scene = this._mesh.getScene();
  43. for (var index = 0; index < scene.meshes.length; index++) {
  44. var otherMesh = scene.meshes[index];
  45. if (otherMesh.material === this._mesh.material && otherMesh.computeBonesUsingShaders && otherMesh.numBoneInfluencers > 0) {
  46. otherMesh.computeBonesUsingShaders = false;
  47. }
  48. }
  49. }
  50. else {
  51. var currentFallbacks = this._defines[this._currentRank];
  52. if (currentFallbacks) {
  53. for (var index = 0; index < currentFallbacks.length; index++) {
  54. currentDefines = currentDefines.replace("#define " + currentFallbacks[index], "");
  55. }
  56. }
  57. this._currentRank++;
  58. }
  59. return currentDefines;
  60. }
  61. }
  62. export class EffectCreationOptions {
  63. public attributes: string[];
  64. public uniformsNames: string[];
  65. public uniformBuffersNames: string[];
  66. public samplers: string[];
  67. public defines: any;
  68. public fallbacks: EffectFallbacks;
  69. public onCompiled: (effect: Effect) => void;
  70. public onError: (effect: Effect, errors: string) => void;
  71. public indexParameters: any;
  72. public maxSimultaneousLights: number;
  73. }
  74. export class Effect {
  75. public name: any;
  76. public defines: string;
  77. public onCompiled: (effect: Effect) => void;
  78. public onError: (effect: Effect, errors: string) => void;
  79. public onBind: (effect: Effect) => void;
  80. public uniqueId = 0;
  81. public onCompileObservable = new Observable<Effect>();
  82. public onErrorObservable = new Observable<Effect>();
  83. public onBindObservable = new Observable<Effect>();
  84. private static _uniqueIdSeed = 0;
  85. private _engine: Engine;
  86. private _uniformBuffersNames: { [key: string]: number } = {};
  87. private _uniformsNames: string[];
  88. private _samplers: string[];
  89. private _isReady = false;
  90. private _compilationError = "";
  91. private _attributesNames: string[];
  92. private _attributes: number[];
  93. private _uniforms: WebGLUniformLocation[];
  94. public _key: string;
  95. private _indexParameters: any;
  96. private _fallbacks: EffectFallbacks;
  97. private _vertexSourceCode: string;
  98. private _fragmentSourceCode: string;
  99. private _vertexSourceCodeOverride: string;
  100. private _fragmentSourceCodeOverride: string;
  101. public _program: WebGLProgram;
  102. private _valueCache: { [key: string]: any };
  103. private static _baseCache: { [key: number]: WebGLBuffer } = {};
  104. constructor(baseName: any, attributesNamesOrOptions: string[] | EffectCreationOptions, uniformsNamesOrEngine: string[] | Engine, samplers?: string[], engine?: Engine, defines?: string, fallbacks?: EffectFallbacks, onCompiled?: (effect: Effect) => void, onError?: (effect: Effect, errors: string) => void, indexParameters?: any) {
  105. this.name = baseName;
  106. if ((<EffectCreationOptions>attributesNamesOrOptions).attributes) {
  107. var options = <EffectCreationOptions>attributesNamesOrOptions;
  108. this._engine = <Engine>uniformsNamesOrEngine;
  109. this._attributesNames = options.attributes;
  110. this._uniformsNames = options.uniformsNames.concat(options.samplers);
  111. this._samplers = options.samplers;
  112. this.defines = options.defines;
  113. this.onError = options.onError;
  114. this.onCompiled = options.onCompiled;
  115. this._fallbacks = options.fallbacks;
  116. this._indexParameters = options.indexParameters;
  117. if (options.uniformBuffersNames) {
  118. for (var i = 0; i < options.uniformBuffersNames.length; i++) {
  119. this._uniformBuffersNames[options.uniformBuffersNames[i]] = i;
  120. }
  121. }
  122. } else {
  123. this._engine = engine;
  124. this.defines = defines;
  125. this._uniformsNames = (<string[]>uniformsNamesOrEngine).concat(samplers);
  126. this._samplers = samplers;
  127. this._attributesNames = (<string[]>attributesNamesOrOptions);
  128. this.onError = onError;
  129. this.onCompiled = onCompiled;
  130. this._indexParameters = indexParameters;
  131. this._fallbacks = fallbacks;
  132. }
  133. this.uniqueId = Effect._uniqueIdSeed++;
  134. var vertexSource: any;
  135. var fragmentSource: any;
  136. if (baseName.vertexElement) {
  137. vertexSource = document.getElementById(baseName.vertexElement);
  138. if (!vertexSource) {
  139. vertexSource = baseName.vertexElement;
  140. }
  141. } else {
  142. vertexSource = baseName.vertex || baseName;
  143. }
  144. if (baseName.fragmentElement) {
  145. fragmentSource = document.getElementById(baseName.fragmentElement);
  146. if (!fragmentSource) {
  147. fragmentSource = baseName.fragmentElement;
  148. }
  149. } else {
  150. fragmentSource = baseName.fragment || baseName;
  151. }
  152. this._loadVertexShader(vertexSource, vertexCode => {
  153. this._processIncludes(vertexCode, vertexCodeWithIncludes => {
  154. this._processShaderConversion(vertexCodeWithIncludes, false, migratedVertexCode => {
  155. this._loadFragmentShader(fragmentSource, (fragmentCode) => {
  156. this._processIncludes(fragmentCode, fragmentCodeWithIncludes => {
  157. this._processShaderConversion(fragmentCodeWithIncludes, true, migratedFragmentCode => {
  158. if (baseName) {
  159. var vertex = baseName.vertexElement || baseName.vertex || baseName;
  160. var fragment = baseName.fragmentElement || baseName.fragment || baseName;
  161. this._vertexSourceCode = "#define SHADER_NAME vertex:" + vertex + "\n" + migratedVertexCode;
  162. this._fragmentSourceCode = "#define SHADER_NAME fragment:" + fragment + "\n" + migratedFragmentCode;
  163. } else {
  164. this._vertexSourceCode = migratedVertexCode;
  165. this._fragmentSourceCode = migratedFragmentCode;
  166. }
  167. this._prepareEffect();
  168. });
  169. });
  170. });
  171. });
  172. });
  173. });
  174. }
  175. public get key(): string {
  176. return this._key;
  177. }
  178. // Properties
  179. public isReady(): boolean {
  180. return this._isReady;
  181. }
  182. public getEngine(): Engine {
  183. return this._engine;
  184. }
  185. public getProgram(): WebGLProgram {
  186. return this._program;
  187. }
  188. public getAttributesNames(): string[] {
  189. return this._attributesNames;
  190. }
  191. public getAttributeLocation(index: number): number {
  192. return this._attributes[index];
  193. }
  194. public getAttributeLocationByName(name: string): number {
  195. var index = this._attributesNames.indexOf(name);
  196. return this._attributes[index];
  197. }
  198. public getAttributesCount(): number {
  199. return this._attributes.length;
  200. }
  201. public getUniformIndex(uniformName: string): number {
  202. return this._uniformsNames.indexOf(uniformName);
  203. }
  204. public getUniform(uniformName: string): WebGLUniformLocation {
  205. return this._uniforms[this._uniformsNames.indexOf(uniformName)];
  206. }
  207. public getSamplers(): string[] {
  208. return this._samplers;
  209. }
  210. public getCompilationError(): string {
  211. return this._compilationError;
  212. }
  213. // Methods
  214. public executeWhenCompiled(func: (effect: Effect) => void): void {
  215. if (this.isReady()) {
  216. func(this);
  217. return;
  218. }
  219. this.onCompileObservable.add((effect) => {
  220. func(effect);
  221. });
  222. }
  223. public _loadVertexShader(vertex: any, callback: (data: any) => void): void {
  224. if (Tools.IsWindowObjectExist()) {
  225. // DOM element ?
  226. if (vertex instanceof HTMLElement) {
  227. var vertexCode = Tools.GetDOMTextContent(vertex);
  228. callback(vertexCode);
  229. return;
  230. }
  231. }
  232. // Base64 encoded ?
  233. if (vertex.substr(0, 7) === "base64:") {
  234. var vertexBinary = window.atob(vertex.substr(7));
  235. callback(vertexBinary);
  236. return;
  237. }
  238. // Is in local store ?
  239. if (Effect.ShadersStore[vertex + "VertexShader"]) {
  240. callback(Effect.ShadersStore[vertex + "VertexShader"]);
  241. return;
  242. }
  243. var vertexShaderUrl;
  244. if (vertex[0] === "." || vertex[0] === "/" || vertex.indexOf("http") > -1) {
  245. vertexShaderUrl = vertex;
  246. } else {
  247. vertexShaderUrl = Engine.ShadersRepository + vertex;
  248. }
  249. // Vertex shader
  250. Tools.LoadFile(vertexShaderUrl + ".vertex.fx", callback);
  251. }
  252. public _loadFragmentShader(fragment: any, callback: (data: any) => void): void {
  253. if (Tools.IsWindowObjectExist()) {
  254. // DOM element ?
  255. if (fragment instanceof HTMLElement) {
  256. var fragmentCode = Tools.GetDOMTextContent(fragment);
  257. callback(fragmentCode);
  258. return;
  259. }
  260. }
  261. // Base64 encoded ?
  262. if (fragment.substr(0, 7) === "base64:") {
  263. var fragmentBinary = window.atob(fragment.substr(7));
  264. callback(fragmentBinary);
  265. return;
  266. }
  267. // Is in local store ?
  268. if (Effect.ShadersStore[fragment + "PixelShader"]) {
  269. callback(Effect.ShadersStore[fragment + "PixelShader"]);
  270. return;
  271. }
  272. if (Effect.ShadersStore[fragment + "FragmentShader"]) {
  273. callback(Effect.ShadersStore[fragment + "FragmentShader"]);
  274. return;
  275. }
  276. var fragmentShaderUrl;
  277. if (fragment[0] === "." || fragment[0] === "/" || fragment.indexOf("http") > -1) {
  278. fragmentShaderUrl = fragment;
  279. } else {
  280. fragmentShaderUrl = Engine.ShadersRepository + fragment;
  281. }
  282. // Fragment shader
  283. Tools.LoadFile(fragmentShaderUrl + ".fragment.fx", callback);
  284. }
  285. private _dumpShadersSource(vertexCode: string, fragmentCode: string, defines: string): void {
  286. // Rebuild shaders source code
  287. var shaderVersion = (this._engine.webGLVersion > 1) ? "#version 300 es\n" : "";
  288. var prefix = shaderVersion + (defines ? defines + "\n" : "");
  289. vertexCode = prefix + vertexCode;
  290. fragmentCode = prefix + fragmentCode;
  291. // Number lines of shaders source code
  292. var i = 2;
  293. var regex = /\n/gm;
  294. var formattedVertexCode = "\n1\t" + vertexCode.replace(regex, function() { return "\n" + (i++) + "\t"; });
  295. i = 2;
  296. var formattedFragmentCode = "\n1\t" + fragmentCode.replace(regex, function() { return "\n" + (i++) + "\t"; });
  297. // Dump shaders name and formatted source code
  298. if (this.name.vertexElement) {
  299. BABYLON.Tools.Error("Vertex shader: " + this.name.vertexElement + formattedVertexCode);
  300. BABYLON.Tools.Error("Fragment shader: " + this.name.fragmentElement + formattedFragmentCode);
  301. }
  302. else if (this.name.vertex) {
  303. BABYLON.Tools.Error("Vertex shader: " + this.name.vertex + formattedVertexCode);
  304. BABYLON.Tools.Error("Fragment shader: " + this.name.fragment + formattedFragmentCode);
  305. }
  306. else {
  307. BABYLON.Tools.Error("Vertex shader: " + this.name + formattedVertexCode);
  308. BABYLON.Tools.Error("Fragment shader: " + this.name + formattedFragmentCode);
  309. }
  310. };
  311. private _processShaderConversion(sourceCode: string, isFragment: boolean, callback: (data: any) => void): void {
  312. var preparedSourceCode = this._processPrecision(sourceCode);
  313. if (this._engine.webGLVersion == 1) {
  314. callback(preparedSourceCode);
  315. return;
  316. }
  317. // Already converted
  318. if (preparedSourceCode.indexOf("#version 3") !== -1) {
  319. callback(preparedSourceCode.replace("#version 300 es", ""));
  320. return;
  321. }
  322. var hasDrawBuffersExtension = preparedSourceCode.search(/#extension.+GL_EXT_draw_buffers.+require/) !== -1;
  323. // Remove extensions
  324. // #extension GL_OES_standard_derivatives : enable
  325. // #extension GL_EXT_shader_texture_lod : enable
  326. // #extension GL_EXT_frag_depth : enable
  327. // #extension GL_EXT_draw_buffers : require
  328. var regex = /#extension.+(GL_OES_standard_derivatives|GL_EXT_shader_texture_lod|GL_EXT_frag_depth|GL_EXT_draw_buffers).+(enable|require)/g;
  329. var result = preparedSourceCode.replace(regex, "");
  330. // Migrate to GLSL v300
  331. result = result.replace(/varying(?![\n\r])\s/g, isFragment ? "in " : "out ");
  332. result = result.replace(/attribute[ \t]/g, "in ");
  333. result = result.replace(/[ \t]attribute/g, " in");
  334. if (isFragment) {
  335. result = result.replace(/texture2DLodEXT\s*\(/g, "textureLod(");
  336. result = result.replace(/textureCubeLodEXT\s*\(/g, "textureLod(");
  337. result = result.replace(/texture2D\s*\(/g, "texture(");
  338. result = result.replace(/textureCube\s*\(/g, "texture(");
  339. result = result.replace(/gl_FragDepthEXT/g, "gl_FragDepth");
  340. result = result.replace(/gl_FragColor/g, "glFragColor");
  341. result = result.replace(/gl_FragData/g, "glFragData");
  342. result = result.replace(/void\s+?main\s*\(/g, (hasDrawBuffersExtension ? "" : "out vec4 glFragColor;\n") + "void main(");
  343. }
  344. callback(result);
  345. }
  346. private _processIncludes(sourceCode: string, callback: (data: any) => void): void {
  347. var regex = /#include<(.+)>(\((.*)\))*(\[(.*)\])*/g;
  348. var match = regex.exec(sourceCode);
  349. var returnValue = new String(sourceCode);
  350. while (match != null) {
  351. var includeFile = match[1];
  352. // Uniform declaration
  353. if (includeFile.indexOf("__decl__") !== -1) {
  354. includeFile = includeFile.replace(/__decl__/, "");
  355. if (this._engine.supportsUniformBuffers) {
  356. includeFile = includeFile.replace(/Vertex/, "Ubo");
  357. includeFile = includeFile.replace(/Fragment/, "Ubo");
  358. }
  359. includeFile = includeFile + "Declaration";
  360. }
  361. if (Effect.IncludesShadersStore[includeFile]) {
  362. // Substitution
  363. var includeContent = Effect.IncludesShadersStore[includeFile];
  364. if (match[2]) {
  365. var splits = match[3].split(",");
  366. for (var index = 0; index < splits.length; index += 2) {
  367. var source = new RegExp(splits[index], "g");
  368. var dest = splits[index + 1];
  369. includeContent = includeContent.replace(source, dest);
  370. }
  371. }
  372. if (match[4]) {
  373. var indexString = match[5];
  374. if (indexString.indexOf("..") !== -1) {
  375. var indexSplits = indexString.split("..");
  376. var minIndex = parseInt(indexSplits[0]);
  377. var maxIndex = parseInt(indexSplits[1]);
  378. var sourceIncludeContent = includeContent.slice(0);
  379. includeContent = "";
  380. if (isNaN(maxIndex)) {
  381. maxIndex = this._indexParameters[indexSplits[1]];
  382. }
  383. for (var i = minIndex; i < maxIndex; i++) {
  384. if (!this._engine.supportsUniformBuffers) {
  385. // Ubo replacement
  386. sourceIncludeContent = sourceIncludeContent.replace(/light\{X\}.(\w*)/g, (str: string, p1: string) => {
  387. return p1 + "{X}";
  388. });
  389. }
  390. includeContent += sourceIncludeContent.replace(/\{X\}/g, i.toString()) + "\n";
  391. }
  392. } else {
  393. if (!this._engine.supportsUniformBuffers) {
  394. // Ubo replacement
  395. includeContent = includeContent.replace(/light\{X\}.(\w*)/g, (str: string, p1: string) => {
  396. return p1 + "{X}";
  397. });
  398. }
  399. includeContent = includeContent.replace(/\{X\}/g, indexString);
  400. }
  401. }
  402. // Replace
  403. returnValue = returnValue.replace(match[0], includeContent);
  404. } else {
  405. var includeShaderUrl = Engine.ShadersRepository + "ShadersInclude/" + includeFile + ".fx";
  406. Tools.LoadFile(includeShaderUrl, (fileContent) => {
  407. Effect.IncludesShadersStore[includeFile] = fileContent;
  408. this._processIncludes(<string>returnValue, callback);
  409. });
  410. return;
  411. }
  412. match = regex.exec(sourceCode);
  413. }
  414. callback(returnValue);
  415. }
  416. private _processPrecision(source: string): string {
  417. if (source.indexOf("precision highp float") === -1) {
  418. if (!this._engine.getCaps().highPrecisionShaderSupported) {
  419. source = "precision mediump float;\n" + source;
  420. } else {
  421. source = "precision highp float;\n" + source;
  422. }
  423. } else {
  424. if (!this._engine.getCaps().highPrecisionShaderSupported) { // Moving highp to mediump
  425. source = source.replace("precision highp float", "precision mediump float");
  426. }
  427. }
  428. return source;
  429. }
  430. public _rebuildProgram(vertexSourceCode: string, fragmentSourceCode: string, onCompiled: (program: WebGLProgram) => void, onError: (message: string) => void) {
  431. this._isReady = false;
  432. this._vertexSourceCodeOverride = vertexSourceCode;
  433. this._fragmentSourceCodeOverride = fragmentSourceCode;
  434. this.onError = (effect, error) => {
  435. if (onError) {
  436. onError(error);
  437. }
  438. };
  439. this.onCompiled = () => {
  440. var scenes = this.getEngine().scenes;
  441. for (var i = 0; i < scenes.length; i++) {
  442. scenes[i].markAllMaterialsAsDirty(Material.TextureDirtyFlag);
  443. }
  444. if (onCompiled) {
  445. onCompiled(this._program);
  446. }
  447. };
  448. this._fallbacks = null;
  449. this._prepareEffect();
  450. }
  451. public _prepareEffect() {
  452. let attributesNames = this._attributesNames;
  453. let defines = this.defines;
  454. let fallbacks = this._fallbacks;
  455. this._valueCache = {};
  456. var previousProgram = this._program;
  457. try {
  458. var engine = this._engine;
  459. if (this._vertexSourceCodeOverride && this._fragmentSourceCodeOverride) {
  460. this._program = engine.createRawShaderProgram(this._vertexSourceCodeOverride, this._fragmentSourceCodeOverride);
  461. }
  462. else {
  463. this._program = engine.createShaderProgram(this._vertexSourceCode, this._fragmentSourceCode, defines);
  464. }
  465. this._program.__SPECTOR_rebuildProgram = this._rebuildProgram.bind(this);
  466. if (engine.webGLVersion > 1) {
  467. for (var name in this._uniformBuffersNames) {
  468. this.bindUniformBlock(name, this._uniformBuffersNames[name]);
  469. }
  470. }
  471. this._uniforms = engine.getUniforms(this._program, this._uniformsNames);
  472. this._attributes = engine.getAttributes(this._program, attributesNames);
  473. var index: number;
  474. for (index = 0; index < this._samplers.length; index++) {
  475. var sampler = this.getUniform(this._samplers[index]);
  476. if (sampler == null) {
  477. this._samplers.splice(index, 1);
  478. index--;
  479. }
  480. }
  481. engine.bindSamplers(this);
  482. this._compilationError = "";
  483. this._isReady = true;
  484. if (this.onCompiled) {
  485. this.onCompiled(this);
  486. }
  487. this.onCompileObservable.notifyObservers(this);
  488. this.onCompileObservable.clear();
  489. // Unbind mesh reference in fallbacks
  490. if (this._fallbacks) {
  491. this._fallbacks.unBindMesh();
  492. }
  493. if (previousProgram) {
  494. this.getEngine()._deleteProgram(previousProgram);
  495. }
  496. } catch (e) {
  497. this._compilationError = e.message;
  498. // Let's go through fallbacks then
  499. Tools.Error("Unable to compile effect:");
  500. BABYLON.Tools.Error("Uniforms: " + this._uniformsNames.map(function(uniform) {
  501. return " " + uniform;
  502. }));
  503. BABYLON.Tools.Error("Attributes: " + attributesNames.map(function(attribute) {
  504. return " " + attribute;
  505. }));
  506. this._dumpShadersSource(this._vertexSourceCode, this._fragmentSourceCode, defines);
  507. Tools.Error("Error: " + this._compilationError);
  508. if (previousProgram) {
  509. this._program = previousProgram;
  510. this._isReady = true;
  511. if (this.onError) {
  512. this.onError(this, this._compilationError);
  513. }
  514. this.onErrorObservable.notifyObservers(this);
  515. }
  516. if (fallbacks && fallbacks.isMoreFallbacks) {
  517. Tools.Error("Trying next fallback.");
  518. this.defines = fallbacks.reduce(this.defines);
  519. this._prepareEffect();
  520. } else { // Sorry we did everything we can
  521. if (this.onError) {
  522. this.onError(this, this._compilationError);
  523. }
  524. this.onErrorObservable.notifyObservers(this);
  525. this.onErrorObservable.clear();
  526. // Unbind mesh reference in fallbacks
  527. if (this._fallbacks) {
  528. this._fallbacks.unBindMesh();
  529. }
  530. }
  531. }
  532. }
  533. public get isSupported(): boolean {
  534. return this._compilationError === "";
  535. }
  536. public _bindTexture(channel: string, texture: InternalTexture): void {
  537. this._engine._bindTexture(this._samplers.indexOf(channel), texture);
  538. }
  539. public setTexture(channel: string, texture: BaseTexture): void {
  540. this._engine.setTexture(this._samplers.indexOf(channel), this.getUniform(channel), texture);
  541. }
  542. public setTextureArray(channel: string, textures: BaseTexture[]): void {
  543. if (this._samplers.indexOf(channel + "Ex") === -1) {
  544. var initialPos = this._samplers.indexOf(channel);
  545. for (var index = 1; index < textures.length; index++) {
  546. this._samplers.splice(initialPos + index, 0, channel + "Ex");
  547. }
  548. }
  549. this._engine.setTextureArray(this._samplers.indexOf(channel), this.getUniform(channel), textures);
  550. }
  551. public setTextureFromPostProcess(channel: string, postProcess: PostProcess): void {
  552. this._engine.setTextureFromPostProcess(this._samplers.indexOf(channel), postProcess);
  553. }
  554. public _cacheMatrix(uniformName: string, matrix: Matrix): boolean {
  555. var cache = this._valueCache[uniformName];
  556. var flag = matrix.updateFlag;
  557. if (cache !== undefined && cache === flag) {
  558. return false;
  559. }
  560. this._valueCache[uniformName] = flag;
  561. return true;
  562. }
  563. public _cacheFloat2(uniformName: string, x: number, y: number): boolean {
  564. var cache = this._valueCache[uniformName];
  565. if (!cache) {
  566. cache = [x, y];
  567. this._valueCache[uniformName] = cache;
  568. return true;
  569. }
  570. var changed = false;
  571. if (cache[0] !== x) {
  572. cache[0] = x;
  573. changed = true;
  574. }
  575. if (cache[1] !== y) {
  576. cache[1] = y;
  577. changed = true;
  578. }
  579. return changed;
  580. }
  581. public _cacheFloat3(uniformName: string, x: number, y: number, z: number): boolean {
  582. var cache = this._valueCache[uniformName];
  583. if (!cache) {
  584. cache = [x, y, z];
  585. this._valueCache[uniformName] = cache;
  586. return true;
  587. }
  588. var changed = false;
  589. if (cache[0] !== x) {
  590. cache[0] = x;
  591. changed = true;
  592. }
  593. if (cache[1] !== y) {
  594. cache[1] = y;
  595. changed = true;
  596. }
  597. if (cache[2] !== z) {
  598. cache[2] = z;
  599. changed = true;
  600. }
  601. return changed;
  602. }
  603. public _cacheFloat4(uniformName: string, x: number, y: number, z: number, w: number): boolean {
  604. var cache = this._valueCache[uniformName];
  605. if (!cache) {
  606. cache = [x, y, z, w];
  607. this._valueCache[uniformName] = cache;
  608. return true;
  609. }
  610. var changed = false;
  611. if (cache[0] !== x) {
  612. cache[0] = x;
  613. changed = true;
  614. }
  615. if (cache[1] !== y) {
  616. cache[1] = y;
  617. changed = true;
  618. }
  619. if (cache[2] !== z) {
  620. cache[2] = z;
  621. changed = true;
  622. }
  623. if (cache[3] !== w) {
  624. cache[3] = w;
  625. changed = true;
  626. }
  627. return changed;
  628. }
  629. public bindUniformBuffer(buffer: WebGLBuffer, name: string): void {
  630. if (Effect._baseCache[this._uniformBuffersNames[name]] === buffer) {
  631. return;
  632. }
  633. Effect._baseCache[this._uniformBuffersNames[name]] = buffer;
  634. this._engine.bindUniformBufferBase(buffer, this._uniformBuffersNames[name]);
  635. }
  636. public bindUniformBlock(blockName: string, index: number): void {
  637. this._engine.bindUniformBlock(this._program, blockName, index);
  638. }
  639. public setIntArray(uniformName: string, array: Int32Array): Effect {
  640. this._valueCache[uniformName] = null;
  641. this._engine.setIntArray(this.getUniform(uniformName), array);
  642. return this;
  643. }
  644. public setIntArray2(uniformName: string, array: Int32Array): Effect {
  645. this._valueCache[uniformName] = null;
  646. this._engine.setIntArray2(this.getUniform(uniformName), array);
  647. return this;
  648. }
  649. public setIntArray3(uniformName: string, array: Int32Array): Effect {
  650. this._valueCache[uniformName] = null;
  651. this._engine.setIntArray3(this.getUniform(uniformName), array);
  652. return this;
  653. }
  654. public setIntArray4(uniformName: string, array: Int32Array): Effect {
  655. this._valueCache[uniformName] = null;
  656. this._engine.setIntArray4(this.getUniform(uniformName), array);
  657. return this;
  658. }
  659. public setFloatArray(uniformName: string, array: Float32Array): Effect {
  660. this._valueCache[uniformName] = null;
  661. this._engine.setFloatArray(this.getUniform(uniformName), array);
  662. return this;
  663. }
  664. public setFloatArray2(uniformName: string, array: Float32Array): Effect {
  665. this._valueCache[uniformName] = null;
  666. this._engine.setFloatArray2(this.getUniform(uniformName), array);
  667. return this;
  668. }
  669. public setFloatArray3(uniformName: string, array: Float32Array): Effect {
  670. this._valueCache[uniformName] = null;
  671. this._engine.setFloatArray3(this.getUniform(uniformName), array);
  672. return this;
  673. }
  674. public setFloatArray4(uniformName: string, array: Float32Array): Effect {
  675. this._valueCache[uniformName] = null;
  676. this._engine.setFloatArray4(this.getUniform(uniformName), array);
  677. return this;
  678. }
  679. public setArray(uniformName: string, array: number[]): Effect {
  680. this._valueCache[uniformName] = null;
  681. this._engine.setArray(this.getUniform(uniformName), array);
  682. return this;
  683. }
  684. public setArray2(uniformName: string, array: number[]): Effect {
  685. this._valueCache[uniformName] = null;
  686. this._engine.setArray2(this.getUniform(uniformName), array);
  687. return this;
  688. }
  689. public setArray3(uniformName: string, array: number[]): Effect {
  690. this._valueCache[uniformName] = null;
  691. this._engine.setArray3(this.getUniform(uniformName), array);
  692. return this;
  693. }
  694. public setArray4(uniformName: string, array: number[]): Effect {
  695. this._valueCache[uniformName] = null;
  696. this._engine.setArray4(this.getUniform(uniformName), array);
  697. return this;
  698. }
  699. public setMatrices(uniformName: string, matrices: Float32Array): Effect {
  700. if (!matrices) {
  701. return this;
  702. }
  703. this._valueCache[uniformName] = null;
  704. this._engine.setMatrices(this.getUniform(uniformName), matrices);
  705. return this;
  706. }
  707. public setMatrix(uniformName: string, matrix: Matrix): Effect {
  708. if (this._cacheMatrix(uniformName, matrix)) {
  709. this._engine.setMatrix(this.getUniform(uniformName), matrix);
  710. }
  711. return this;
  712. }
  713. public setMatrix3x3(uniformName: string, matrix: Float32Array): Effect {
  714. this._valueCache[uniformName] = null;
  715. this._engine.setMatrix3x3(this.getUniform(uniformName), matrix);
  716. return this;
  717. }
  718. public setMatrix2x2(uniformName: string, matrix: Float32Array): Effect {
  719. this._valueCache[uniformName] = null;
  720. this._engine.setMatrix2x2(this.getUniform(uniformName), matrix);
  721. return this;
  722. }
  723. public setFloat(uniformName: string, value: number): Effect {
  724. var cache = this._valueCache[uniformName];
  725. if (cache !== undefined && cache === value)
  726. return this;
  727. this._valueCache[uniformName] = value;
  728. this._engine.setFloat(this.getUniform(uniformName), value);
  729. return this;
  730. }
  731. public setBool(uniformName: string, bool: boolean): Effect {
  732. var cache = this._valueCache[uniformName];
  733. if (cache !== undefined && cache === bool)
  734. return this;
  735. this._valueCache[uniformName] = bool;
  736. this._engine.setBool(this.getUniform(uniformName), bool ? 1 : 0);
  737. return this;
  738. }
  739. public setVector2(uniformName: string, vector2: Vector2): Effect {
  740. if (this._cacheFloat2(uniformName, vector2.x, vector2.y)) {
  741. this._engine.setFloat2(this.getUniform(uniformName), vector2.x, vector2.y);
  742. }
  743. return this;
  744. }
  745. public setFloat2(uniformName: string, x: number, y: number): Effect {
  746. if (this._cacheFloat2(uniformName, x, y)) {
  747. this._engine.setFloat2(this.getUniform(uniformName), x, y);
  748. }
  749. return this;
  750. }
  751. public setVector3(uniformName: string, vector3: Vector3): Effect {
  752. if (this._cacheFloat3(uniformName, vector3.x, vector3.y, vector3.z)) {
  753. this._engine.setFloat3(this.getUniform(uniformName), vector3.x, vector3.y, vector3.z);
  754. }
  755. return this;
  756. }
  757. public setFloat3(uniformName: string, x: number, y: number, z: number): Effect {
  758. if (this._cacheFloat3(uniformName, x, y, z)) {
  759. this._engine.setFloat3(this.getUniform(uniformName), x, y, z);
  760. }
  761. return this;
  762. }
  763. public setVector4(uniformName: string, vector4: Vector4): Effect {
  764. if (this._cacheFloat4(uniformName, vector4.x, vector4.y, vector4.z, vector4.w)) {
  765. this._engine.setFloat4(this.getUniform(uniformName), vector4.x, vector4.y, vector4.z, vector4.w);
  766. }
  767. return this;
  768. }
  769. public setFloat4(uniformName: string, x: number, y: number, z: number, w: number): Effect {
  770. if (this._cacheFloat4(uniformName, x, y, z, w)) {
  771. this._engine.setFloat4(this.getUniform(uniformName), x, y, z, w);
  772. }
  773. return this;
  774. }
  775. public setColor3(uniformName: string, color3: Color3): Effect {
  776. if (this._cacheFloat3(uniformName, color3.r, color3.g, color3.b)) {
  777. this._engine.setColor3(this.getUniform(uniformName), color3);
  778. }
  779. return this;
  780. }
  781. public setColor4(uniformName: string, color3: Color3, alpha: number): Effect {
  782. if (this._cacheFloat4(uniformName, color3.r, color3.g, color3.b, alpha)) {
  783. this._engine.setColor4(this.getUniform(uniformName), color3, alpha);
  784. }
  785. return this;
  786. }
  787. // Statics
  788. public static ShadersStore: { [key: string]: string } = {};
  789. public static IncludesShadersStore: { [key: string]: string } = {};
  790. public static ResetCache() {
  791. Effect._baseCache = {};
  792. }
  793. }
  794. }