babylon.nativeEngineWrapper.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. module BABYLON {
  2. /** @hidden */
  3. export interface INativeEngineInterop {
  4. requestAnimationFrame(callback: () => void): void;
  5. createIndexBuffer(indices: ArrayBuffer, is32Bits: boolean): WebGLBuffer;
  6. bindIndexBuffer(buffer: WebGLBuffer) : void;
  7. createVertexBuffer(vertices: Float32Array): WebGLBuffer;
  8. bindVertexBuffer(buffer: WebGLBuffer, indx: number, size: number, type: number, normalized: boolean, stride: number, offset: number): void;
  9. createProgram(vertexShader: string, fragmentShader: string): WebGLProgram;
  10. getUniforms(shaderProgram: WebGLProgram, uniformsNames: string[]): WebGLUniformLocation[];
  11. setProgram(program: WebGLProgram): void;
  12. setState(culling: boolean, zOffset: number, reverseSide: boolean): void;
  13. setMatrix(uniform: WebGLUniformLocation, matrix: Float32Array): void;
  14. setIntArray(uniform: WebGLUniformLocation, array: Int32Array): void;
  15. setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): void;
  16. setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): void;
  17. setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): void;
  18. setFloatArray(uniform: WebGLUniformLocation, array: Float32Array | number[]): void;
  19. setFloatArray2(uniform: WebGLUniformLocation, array: Float32Array | number[]): void;
  20. setFloatArray3(uniform: WebGLUniformLocation, array: Float32Array | number[]): void;
  21. setFloatArray4(uniform: WebGLUniformLocation, array: Float32Array | number[]): void;
  22. setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): void;
  23. setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): void;
  24. setFloat(uniform: WebGLUniformLocation, value: number): void;
  25. setFloat2(uniform: WebGLUniformLocation, x: number, y: number): void;
  26. setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): void;
  27. setBool(uniform: WebGLUniformLocation, bool: number): void;
  28. setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): void;
  29. drawIndexed(fillMode: number, indexStart: number, indexCount: number): void;
  30. draw(fillMode: number, vertexStart: number, vertexCount: number): void;
  31. clear(r: number, g: number, b: number, a: number, backBuffer: boolean, depth: boolean, stencil: boolean): void;
  32. }
  33. /** @hidden */
  34. declare var nativeEngineInterop: INativeEngineInterop;
  35. /** @hidden */
  36. export class NativeEngineWrapperOptions {
  37. public renderWidth = 1024;
  38. public renderHeight = 768;
  39. public textureSize = 512;
  40. public deterministicLockstep = false;
  41. public lockstepMaxSteps = 4;
  42. }
  43. /** @hidden */
  44. export class NativeEngineWrapper extends Engine {
  45. private _options: NativeEngineWrapperOptions;
  46. public isDeterministicLockStep(): boolean {
  47. return this._options.deterministicLockstep;
  48. }
  49. public getLockstepMaxSteps(): number {
  50. return this._options.lockstepMaxSteps;
  51. }
  52. public getHardwareScalingLevel(): number {
  53. return 1.0;
  54. }
  55. // INativeEngineInterop
  56. //
  57. public constructor(private readonly _interop: INativeEngineInterop = nativeEngineInterop, options: NativeEngineWrapperOptions = new NativeEngineWrapperOptions()) {
  58. super(null);
  59. if (options.deterministicLockstep === undefined) {
  60. options.deterministicLockstep = false;
  61. }
  62. if (options.lockstepMaxSteps === undefined) {
  63. options.lockstepMaxSteps = 4;
  64. }
  65. this._options = options;
  66. // Init caps
  67. // We consider we are on a webgl1 capable device
  68. this._caps = new EngineCapabilities();
  69. this._caps.maxTexturesImageUnits = 16;
  70. this._caps.maxVertexTextureImageUnits = 16;
  71. this._caps.maxTextureSize = 512;
  72. this._caps.maxCubemapTextureSize = 512;
  73. this._caps.maxRenderTextureSize = 512;
  74. this._caps.maxVertexAttribs = 16;
  75. this._caps.maxVaryingVectors = 16;
  76. this._caps.maxFragmentUniformVectors = 16;
  77. this._caps.maxVertexUniformVectors = 16;
  78. // Extensions
  79. this._caps.standardDerivatives = false;
  80. this._caps.astc = null;
  81. this._caps.s3tc = null;
  82. this._caps.pvrtc = null;
  83. this._caps.etc1 = null;
  84. this._caps.etc2 = null;
  85. this._caps.textureAnisotropicFilterExtension = null;
  86. this._caps.maxAnisotropy = 0;
  87. this._caps.uintIndices = false;
  88. this._caps.fragmentDepthSupported = false;
  89. this._caps.highPrecisionShaderSupported = true;
  90. this._caps.colorBufferFloat = false;
  91. this._caps.textureFloat = false;
  92. this._caps.textureFloatLinearFiltering = false;
  93. this._caps.textureFloatRender = false;
  94. this._caps.textureHalfFloat = false;
  95. this._caps.textureHalfFloatLinearFiltering = false;
  96. this._caps.textureHalfFloatRender = false;
  97. this._caps.textureLOD = false;
  98. this._caps.drawBuffersExtension = false;
  99. this._caps.depthTextureExtension = false;
  100. this._caps.vertexArrayObject = false;
  101. this._caps.instancedArrays = false;
  102. Tools.Log("Babylon.js null engine (v" + Engine.Version + ") launched");
  103. // Wrappers
  104. if (typeof URL === "undefined") {
  105. (<any>URL) = {
  106. createObjectURL: function () { },
  107. revokeObjectURL: function () { }
  108. }
  109. }
  110. if (typeof Blob === "undefined") {
  111. (<any>Blob) = function () { };
  112. }
  113. }
  114. /**
  115. * Can be used to override the current requestAnimationFrame requester.
  116. * @hidden
  117. */
  118. protected _queueNewFrame(bindedRenderFunction: any, requester: any): number {
  119. this._interop.requestAnimationFrame(bindedRenderFunction);
  120. return 0;
  121. }
  122. public clear(color: Color4, backBuffer: boolean, depth: boolean, stencil: boolean = false): void {
  123. this._interop.clear(color.r, color.g, color.b, color.a, backBuffer, depth, stencil);
  124. }
  125. public createIndexBuffer(indices: IndicesArray): WebGLBuffer {
  126. var arrayBuffer: ArrayBuffer;
  127. var is32Bits;
  128. if (indices instanceof Uint16Array) {
  129. arrayBuffer = indices.buffer;
  130. is32Bits = false;
  131. } else if (indices instanceof Uint32Array) {
  132. arrayBuffer = indices.buffer;
  133. is32Bits = true;
  134. } else {
  135. arrayBuffer = new Uint32Array(indices).buffer;
  136. is32Bits = true;
  137. }
  138. const buffer = this._interop.createIndexBuffer(arrayBuffer, is32Bits);
  139. buffer.capacity = indices.length;
  140. buffer.references = 1;
  141. buffer.is32Bits = is32Bits;
  142. return buffer;
  143. }
  144. public createVertexBuffer(data: DataArray): WebGLBuffer {
  145. var floatArray: Float32Array;
  146. if (data instanceof Array ||
  147. data instanceof ArrayBuffer) {
  148. floatArray = new Float32Array(data);
  149. } else {
  150. floatArray = new Float32Array((<ArrayBufferView>data).buffer);
  151. }
  152. const buffer = this._interop.createVertexBuffer(floatArray);
  153. buffer.capacity = floatArray.length;
  154. buffer.references = 1;
  155. buffer.is32Bits = true;
  156. return buffer;
  157. }
  158. // BUFFERS.
  159. public bindBuffers(vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: WebGLBuffer, effect: Effect): void {
  160. // Index
  161. if (indexBuffer) {
  162. this._interop.bindIndexBuffer(indexBuffer);
  163. }
  164. // Vertex
  165. var attributes = effect.getAttributesNames();
  166. for (var index = 0; index < attributes.length; index++) {
  167. var order = effect.getAttributeLocation(index);
  168. if (order >= 0) {
  169. var vertexBuffer = vertexBuffers[attributes[index]];
  170. if (!vertexBuffer) {
  171. continue;
  172. }
  173. var buffer = vertexBuffer.getBuffer();
  174. if (buffer) {
  175. this._interop.bindVertexBuffer(buffer, order, vertexBuffer.getSize(), vertexBuffer.type, vertexBuffer.normalized, vertexBuffer.byteStride, vertexBuffer.byteOffset);
  176. }
  177. }
  178. }
  179. }
  180. public getAttributes(shaderProgram: WebGLProgram, attributesNames: string[]): number[] {
  181. var results = [];
  182. for (var index = 0; index < attributesNames.length; index++) {
  183. switch(attributesNames[index]) {
  184. case VertexBuffer.PositionKind:
  185. results.push(0);
  186. break;
  187. case VertexBuffer.NormalKind:
  188. results.push(1);
  189. break;
  190. // case VertexBuffer.ColorKind:
  191. // results.push(2);
  192. // break;
  193. default:
  194. results.push(-1);
  195. }
  196. }
  197. return results;
  198. }
  199. /**
  200. * Draw a list of indexed primitives
  201. * @param fillMode defines the primitive to use
  202. * @param indexStart defines the starting index
  203. * @param indexCount defines the number of index to draw
  204. * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
  205. */
  206. public drawElementsType(fillMode: number, indexStart: number, indexCount: number, instancesCount?: number): void {
  207. // Apply states
  208. // this.applyStates();
  209. this._drawCalls.addCount(1, false);
  210. // Render
  211. //var indexFormat = this._uintIndicesCurrentlySet ? this._gl.UNSIGNED_INT : this._gl.UNSIGNED_SHORT;
  212. //var mult = this._uintIndicesCurrentlySet ? 4 : 2;
  213. // if (instancesCount) {
  214. // this._gl.drawElementsInstanced(drawMode, indexCount, indexFormat, indexStart * mult, instancesCount);
  215. // } else {
  216. this._interop.drawIndexed(fillMode, indexStart, indexCount);
  217. // }
  218. }
  219. /**
  220. * Draw a list of unindexed primitives
  221. * @param fillMode defines the primitive to use
  222. * @param verticesStart defines the index of first vertex to draw
  223. * @param verticesCount defines the count of vertices to draw
  224. * @param instancesCount defines the number of instances to draw (if instanciation is enabled)
  225. */
  226. public drawArraysType(fillMode: number, verticesStart: number, verticesCount: number, instancesCount?: number): void {
  227. // Apply states
  228. // this.applyStates();
  229. this._drawCalls.addCount(1, false);
  230. // if (instancesCount) {
  231. // this._gl.drawArraysInstanced(drawMode, verticesStart, verticesCount, instancesCount);
  232. // } else {
  233. this._interop.draw(fillMode, verticesStart, verticesCount);
  234. // }
  235. }
  236. /**
  237. * Directly creates a webGL program
  238. * @param vertexCode defines the vertex shader code to use
  239. * @param fragmentCode defines the fragment shader code to use
  240. * @param context defines the webGL context to use (if not set, the current one will be used)
  241. * @param transformFeedbackVaryings defines the list of transform feedback varyings to use
  242. * @returns the new webGL program
  243. */
  244. public createRawShaderProgram(vertexCode: string, fragmentCode: string, context?: WebGLRenderingContext, transformFeedbackVaryings: Nullable<string[]> = null): WebGLProgram {
  245. return this._interop.createProgram(vertexCode, fragmentCode);
  246. }
  247. /**
  248. * Creates a webGL program
  249. * @param vertexCode defines the vertex shader code to use
  250. * @param fragmentCode defines the fragment shader code to use
  251. * @param defines defines the string containing the defines to use to compile the shaders
  252. * @param context defines the webGL context to use (if not set, the current one will be used)
  253. * @param transformFeedbackVaryings defines the list of transform feedback varyings to use
  254. * @returns the new webGL program
  255. */
  256. public createShaderProgram(vertexCode: string, fragmentCode: string, defines: Nullable<string>, context?: WebGLRenderingContext, transformFeedbackVaryings: Nullable<string[]> = null): WebGLProgram {
  257. this.onBeforeShaderCompilationObservable.notifyObservers(this);
  258. var shaderVersion = (this._webGLVersion > 1) ? "#version 300 es\n#define WEBGL2 \n" : "";
  259. var vertexCodeConcat = Engine._concatenateShader(vertexCode, defines, shaderVersion);
  260. var fragmentCodeConcat = Engine._concatenateShader(fragmentCode, defines, shaderVersion);
  261. var program = this.createRawShaderProgram(vertexCodeConcat, fragmentCodeConcat);
  262. program.transformFeedback = null;
  263. program.__SPECTOR_rebuildProgram = null;
  264. this.onAfterShaderCompilationObservable.notifyObservers(this);
  265. return program;
  266. }
  267. /**
  268. * Binds an effect to the webGL context
  269. * @param effect defines the effect to bind
  270. */
  271. public bindSamplers(effect: Effect): void {
  272. var program = effect.getProgram();
  273. if (this._currentProgram !== program) {
  274. this._interop.setProgram(effect.getProgram());
  275. this._currentProgram = program;
  276. }
  277. // var samplers = effect.getSamplers();
  278. // for (var index = 0; index < samplers.length; index++) {
  279. // var uniform = effect.getUniform(samplers[index]);
  280. // if (uniform) {
  281. // this._boundUniforms[index] = uniform;
  282. // }
  283. // }
  284. this._currentEffect = null;
  285. }
  286. public getUniforms(shaderProgram: WebGLProgram, uniformsNames: string[]): WebGLUniformLocation[] {
  287. return this._interop.getUniforms(shaderProgram, uniformsNames);
  288. }
  289. public setMatrix(uniform: WebGLUniformLocation, matrix: Matrix): void {
  290. if (!uniform)
  291. return;
  292. this._interop.setMatrix(uniform, matrix.toArray());
  293. }
  294. public getRenderWidth(useScreen = false): number {
  295. if (!useScreen && this._currentRenderTarget) {
  296. return this._currentRenderTarget.width;
  297. }
  298. return this._options.renderWidth;
  299. }
  300. public getRenderHeight(useScreen = false): number {
  301. if (!useScreen && this._currentRenderTarget) {
  302. return this._currentRenderTarget.height;
  303. }
  304. return this._options.renderHeight;
  305. }
  306. public setViewport(viewport: Viewport, requiredWidth?: number, requiredHeight?: number): void {
  307. this._cachedViewport = viewport;
  308. }
  309. public setState(culling: boolean, zOffset: number = 0, force?: boolean, reverseSide = false): void {
  310. this._interop.setState(culling, zOffset, reverseSide);
  311. }
  312. public setIntArray(uniform: WebGLUniformLocation, array: Int32Array): void {
  313. if (!uniform)
  314. return;
  315. this._interop.setIntArray(uniform, array);
  316. }
  317. public setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): void {
  318. if (!uniform)
  319. return;
  320. this._interop.setIntArray2(uniform, array);
  321. }
  322. public setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): void {
  323. if (!uniform)
  324. return;
  325. this._interop.setIntArray3(uniform, array);
  326. }
  327. public setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): void {
  328. if (!uniform)
  329. return;
  330. this._interop.setIntArray4(uniform, array);
  331. }
  332. public setFloatArray(uniform: WebGLUniformLocation, array: Float32Array): void {
  333. if (!uniform)
  334. return;
  335. this._interop.setFloatArray(uniform, array);
  336. }
  337. public setFloatArray2(uniform: WebGLUniformLocation, array: Float32Array): void {
  338. if (!uniform)
  339. return;
  340. this._interop.setFloatArray2(uniform, array);
  341. }
  342. public setFloatArray3(uniform: WebGLUniformLocation, array: Float32Array): void {
  343. if (!uniform)
  344. return;
  345. this._interop.setFloatArray3(uniform, array);
  346. }
  347. public setFloatArray4(uniform: WebGLUniformLocation, array: Float32Array): void {
  348. if (!uniform)
  349. return;
  350. this._interop.setFloatArray4(uniform, array);
  351. }
  352. public setArray(uniform: WebGLUniformLocation, array: number[]): void {
  353. if (!uniform)
  354. return;
  355. this._interop.setFloatArray(uniform, array);
  356. }
  357. public setArray2(uniform: WebGLUniformLocation, array: number[]): void {
  358. if (!uniform)
  359. return;
  360. this._interop.setFloatArray2(uniform, array);
  361. }
  362. public setArray3(uniform: WebGLUniformLocation, array: number[]): void {
  363. if (!uniform)
  364. return;
  365. this._interop.setFloatArray3(uniform, array);
  366. }
  367. public setArray4(uniform: WebGLUniformLocation, array: number[]): void {
  368. if (!uniform)
  369. return;
  370. this._interop.setFloatArray4(uniform, array);
  371. }
  372. public setMatrices(uniform: WebGLUniformLocation, matrices: Float32Array): void {
  373. if (!uniform)
  374. return;
  375. this._interop.setMatrix(uniform, matrices);
  376. }
  377. public setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  378. if (!uniform)
  379. return;
  380. this._interop.setMatrix3x3(uniform, matrix);
  381. }
  382. public setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  383. if (!uniform)
  384. return;
  385. this._interop.setMatrix2x2(uniform, matrix);
  386. }
  387. public setFloat(uniform: WebGLUniformLocation, value: number): void {
  388. if (!uniform)
  389. return;
  390. this._interop.setFloat(uniform, value);
  391. }
  392. public setFloat2(uniform: WebGLUniformLocation, x: number, y: number): void {
  393. if (!uniform)
  394. return;
  395. this._interop.setFloat2(uniform, x, y);
  396. }
  397. public setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): void {
  398. if (!uniform)
  399. return;
  400. this._interop.setFloat3(uniform, x, y, z);
  401. }
  402. public setBool(uniform: WebGLUniformLocation, bool: number): void {
  403. if (!uniform)
  404. return;
  405. this._interop.setBool(uniform, bool);
  406. }
  407. public setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): void {
  408. if (!uniform)
  409. return;
  410. this._interop.setFloat4(uniform, x, y, z, w);
  411. }
  412. public setColor3(uniform: WebGLUniformLocation, color3: Color3): void {
  413. if (!uniform)
  414. return;
  415. this._interop.setFloat3(uniform, color3.r, color3.g, color3.b);
  416. }
  417. public setColor4(uniform: WebGLUniformLocation, color3: Color3, alpha: number): void {
  418. if (!uniform)
  419. return;
  420. this._interop.setFloat4(uniform, color3.r, color3.g, color3.b, alpha);
  421. }
  422. public setAlphaMode(mode: number, noDepthWriteChange: boolean = false): void {
  423. if (this._alphaMode === mode) {
  424. return;
  425. }
  426. this._alphaState.alphaBlend = (mode !== Engine.ALPHA_DISABLE);
  427. if (!noDepthWriteChange) {
  428. this.setDepthWrite(mode === Engine.ALPHA_DISABLE);
  429. }
  430. this._alphaMode = mode;
  431. }
  432. public wipeCaches(bruteForce?: boolean): void {
  433. if (this.preventCacheWipeBetweenFrames) {
  434. return;
  435. }
  436. this.resetTextureCache();
  437. this._currentEffect = null;
  438. if (bruteForce) {
  439. this._currentProgram = null;
  440. this._stencilState.reset();
  441. this._depthCullingState.reset();
  442. this._alphaState.reset();
  443. }
  444. this._cachedVertexBuffers = null;
  445. this._cachedIndexBuffer = null;
  446. this._cachedEffectForVertexBuffers = null;
  447. }
  448. public _createTexture(): WebGLTexture {
  449. return {};
  450. }
  451. public _releaseTexture(texture: InternalTexture): void {
  452. }
  453. public createTexture(urlArg: string, noMipmap: boolean, invertY: boolean, scene: Scene, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: Nullable<() => void> = null, onError: Nullable<(message: string, exception: any) => void> = null, buffer: Nullable<ArrayBuffer | HTMLImageElement> = null, fallBack?: InternalTexture, format?: number): InternalTexture {
  454. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_URL);
  455. var url = String(urlArg);
  456. texture.url = url;
  457. texture.generateMipMaps = !noMipmap;
  458. texture.samplingMode = samplingMode;
  459. texture.invertY = invertY;
  460. texture.baseWidth = this._options.textureSize;
  461. texture.baseHeight = this._options.textureSize;
  462. texture.width = this._options.textureSize;
  463. texture.height = this._options.textureSize;
  464. if (format) {
  465. texture.format = format;
  466. }
  467. texture.isReady = true;
  468. if (onLoad) {
  469. onLoad();
  470. }
  471. this._internalTexturesCache.push(texture);
  472. return texture;
  473. }
  474. public createRenderTargetTexture(size: any, options: boolean | RenderTargetCreationOptions): InternalTexture {
  475. let fullOptions = new RenderTargetCreationOptions();
  476. if (options !== undefined && typeof options === "object") {
  477. fullOptions.generateMipMaps = options.generateMipMaps;
  478. fullOptions.generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  479. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && options.generateStencilBuffer;
  480. fullOptions.type = options.type === undefined ? Engine.TEXTURETYPE_UNSIGNED_INT : options.type;
  481. fullOptions.samplingMode = options.samplingMode === undefined ? Texture.TRILINEAR_SAMPLINGMODE : options.samplingMode;
  482. } else {
  483. fullOptions.generateMipMaps = <boolean>options;
  484. fullOptions.generateDepthBuffer = true;
  485. fullOptions.generateStencilBuffer = false;
  486. fullOptions.type = Engine.TEXTURETYPE_UNSIGNED_INT;
  487. fullOptions.samplingMode = Texture.TRILINEAR_SAMPLINGMODE;
  488. }
  489. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RENDERTARGET);
  490. var width = size.width || size;
  491. var height = size.height || size;
  492. texture._depthStencilBuffer = {};
  493. texture._framebuffer = {};
  494. texture.baseWidth = width;
  495. texture.baseHeight = height;
  496. texture.width = width;
  497. texture.height = height;
  498. texture.isReady = true;
  499. texture.samples = 1;
  500. texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
  501. texture.samplingMode = fullOptions.samplingMode;
  502. texture.type = fullOptions.type;
  503. texture._generateDepthBuffer = fullOptions.generateDepthBuffer;
  504. texture._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;
  505. this._internalTexturesCache.push(texture);
  506. return texture;
  507. }
  508. public updateTextureSamplingMode(samplingMode: number, texture: InternalTexture): void {
  509. texture.samplingMode = samplingMode;
  510. }
  511. public bindFramebuffer(texture: InternalTexture, faceIndex?: number, requiredWidth?: number, requiredHeight?: number, forceFullscreenViewport?: boolean): void {
  512. if (this._currentRenderTarget) {
  513. this.unBindFramebuffer(this._currentRenderTarget);
  514. }
  515. this._currentRenderTarget = texture;
  516. this._currentFramebuffer = texture._MSAAFramebuffer ? texture._MSAAFramebuffer : texture._framebuffer;
  517. if (this._cachedViewport && !forceFullscreenViewport) {
  518. this.setViewport(this._cachedViewport, requiredWidth, requiredHeight);
  519. }
  520. }
  521. public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
  522. this._currentRenderTarget = null;
  523. if (onBeforeUnbind) {
  524. if (texture._MSAAFramebuffer) {
  525. this._currentFramebuffer = texture._framebuffer;
  526. }
  527. onBeforeUnbind();
  528. }
  529. this._currentFramebuffer = null;
  530. }
  531. public createDynamicVertexBuffer(vertices: FloatArray): WebGLBuffer {
  532. var vbo = {
  533. capacity: 1,
  534. references: 1,
  535. is32Bits: false
  536. }
  537. return vbo;
  538. }
  539. public updateDynamicIndexBuffer(indexBuffer: WebGLBuffer, indices: IndicesArray, offset: number = 0): void {
  540. }
  541. /**
  542. * Updates a dynamic vertex buffer.
  543. * @param vertexBuffer the vertex buffer to update
  544. * @param data the data used to update the vertex buffer
  545. * @param byteOffset the byte offset of the data (optional)
  546. * @param byteLength the byte length of the data (optional)
  547. */
  548. public updateDynamicVertexBuffer(vertexBuffer: WebGLBuffer, vertices: FloatArray, byteOffset?: number, byteLength?: number): void {
  549. }
  550. protected _bindTextureDirectly(target: number, texture: Nullable<InternalTexture>, forTextureDataUpdate = false, force = false): boolean {
  551. if (this._boundTexturesCache[this._activeChannel] !== texture) {
  552. this._boundTexturesCache[this._activeChannel] = texture;
  553. return false;
  554. } else {
  555. return true;
  556. }
  557. }
  558. public _bindTexture(channel: number, texture: InternalTexture): void {
  559. if (channel < 0) {
  560. return;
  561. }
  562. this._bindTextureDirectly(0, texture);
  563. }
  564. public _releaseBuffer(buffer: WebGLBuffer): boolean {
  565. buffer.references--;
  566. if (buffer.references === 0) {
  567. return true;
  568. }
  569. return false;
  570. }
  571. public releaseEffects() {
  572. }
  573. }
  574. }