nullEngine.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. /* eslint-disable @typescript-eslint/no-unused-vars */
  2. import { Logger } from "../Misc/logger.js";
  3. import { Engine } from "../Engines/engine.js";
  4. import { InternalTexture, InternalTextureSource } from "../Materials/Textures/internalTexture.js";
  5. import { DataBuffer } from "../Buffers/dataBuffer.js";
  6. import { PerformanceConfigurator } from "./performanceConfigurator.js";
  7. import { RenderTargetWrapper } from "./renderTargetWrapper.js";
  8. import { IsWrapper } from "../Materials/drawWrapper.functions.js";
  9. /**
  10. * Options to create the null engine
  11. */
  12. export class NullEngineOptions {
  13. constructor() {
  14. /**
  15. * Render width (Default: 512)
  16. */
  17. this.renderWidth = 512;
  18. /**
  19. * Render height (Default: 256)
  20. */
  21. this.renderHeight = 256;
  22. /**
  23. * Texture size (Default: 512)
  24. */
  25. this.textureSize = 512;
  26. /**
  27. * If delta time between frames should be constant
  28. * @see https://doc.babylonjs.com/features/featuresDeepDive/animation/advanced_animations#deterministic-lockstep
  29. */
  30. this.deterministicLockstep = false;
  31. /**
  32. * Maximum about of steps between frames (Default: 4)
  33. * @see https://doc.babylonjs.com/features/featuresDeepDive/animation/advanced_animations#deterministic-lockstep
  34. */
  35. this.lockstepMaxSteps = 4;
  36. }
  37. }
  38. /**
  39. * The null engine class provides support for headless version of babylon.js.
  40. * This can be used in server side scenario or for testing purposes
  41. */
  42. export class NullEngine extends Engine {
  43. /**
  44. * Gets a boolean indicating that the engine is running in deterministic lock step mode
  45. * @see https://doc.babylonjs.com/features/featuresDeepDive/animation/advanced_animations#deterministic-lockstep
  46. * @returns true if engine is in deterministic lock step mode
  47. */
  48. isDeterministicLockStep() {
  49. return this._options.deterministicLockstep;
  50. }
  51. /**
  52. * Gets the max steps when engine is running in deterministic lock step
  53. * @see https://doc.babylonjs.com/features/featuresDeepDive/animation/advanced_animations#deterministic-lockstep
  54. * @returns the max steps
  55. */
  56. getLockstepMaxSteps() {
  57. return this._options.lockstepMaxSteps;
  58. }
  59. /**
  60. * Gets the current hardware scaling level.
  61. * By default the hardware scaling level is computed from the window device ratio.
  62. * if level = 1 then the engine will render at the exact resolution of the canvas. If level = 0.5 then the engine will render at twice the size of the canvas.
  63. * @returns a number indicating the current hardware scaling level
  64. */
  65. getHardwareScalingLevel() {
  66. return 1.0;
  67. }
  68. constructor(options = new NullEngineOptions()) {
  69. super(null);
  70. if (options.deterministicLockstep === undefined) {
  71. options.deterministicLockstep = false;
  72. }
  73. if (options.timeStep !== undefined) {
  74. this._timeStep = options.timeStep;
  75. }
  76. if (options.lockstepMaxSteps === undefined) {
  77. options.lockstepMaxSteps = 4;
  78. }
  79. this._options = options;
  80. PerformanceConfigurator.SetMatrixPrecision(!!options.useHighPrecisionMatrix);
  81. // Init caps
  82. // We consider we are on a webgl1 capable device
  83. this._caps = {
  84. maxTexturesImageUnits: 16,
  85. maxVertexTextureImageUnits: 16,
  86. maxCombinedTexturesImageUnits: 32,
  87. maxTextureSize: 512,
  88. maxCubemapTextureSize: 512,
  89. maxRenderTextureSize: 512,
  90. maxVertexAttribs: 16,
  91. maxVaryingVectors: 16,
  92. maxFragmentUniformVectors: 16,
  93. maxVertexUniformVectors: 16,
  94. standardDerivatives: false,
  95. astc: null,
  96. pvrtc: null,
  97. etc1: null,
  98. etc2: null,
  99. bptc: null,
  100. maxAnisotropy: 0,
  101. uintIndices: false,
  102. fragmentDepthSupported: false,
  103. highPrecisionShaderSupported: true,
  104. colorBufferFloat: false,
  105. supportFloatTexturesResolve: false,
  106. rg11b10ufColorRenderable: false,
  107. textureFloat: false,
  108. textureFloatLinearFiltering: false,
  109. textureFloatRender: false,
  110. textureHalfFloat: false,
  111. textureHalfFloatLinearFiltering: false,
  112. textureHalfFloatRender: false,
  113. textureLOD: false,
  114. texelFetch: false,
  115. drawBuffersExtension: false,
  116. depthTextureExtension: false,
  117. vertexArrayObject: false,
  118. instancedArrays: false,
  119. supportOcclusionQuery: false,
  120. canUseTimestampForTimerQuery: false,
  121. maxMSAASamples: 1,
  122. blendMinMax: false,
  123. canUseGLInstanceID: false,
  124. canUseGLVertexID: false,
  125. supportComputeShaders: false,
  126. supportSRGBBuffers: false,
  127. supportTransformFeedbacks: false,
  128. textureMaxLevel: false,
  129. texture2DArrayMaxLayerCount: 128,
  130. disableMorphTargetTexture: false,
  131. };
  132. this._features = {
  133. forceBitmapOverHTMLImageElement: false,
  134. supportRenderAndCopyToLodForFloatTextures: false,
  135. supportDepthStencilTexture: false,
  136. supportShadowSamplers: false,
  137. uniformBufferHardCheckMatrix: false,
  138. allowTexturePrefiltering: false,
  139. trackUbosInFrame: false,
  140. checkUbosContentBeforeUpload: false,
  141. supportCSM: false,
  142. basisNeedsPOT: false,
  143. support3DTextures: false,
  144. needTypeSuffixInShaderConstants: false,
  145. supportMSAA: false,
  146. supportSSAO2: false,
  147. supportExtendedTextureFormats: false,
  148. supportSwitchCaseInShader: false,
  149. supportSyncTextureRead: false,
  150. needsInvertingBitmap: false,
  151. useUBOBindingCache: false,
  152. needShaderCodeInlining: false,
  153. needToAlwaysBindUniformBuffers: false,
  154. supportRenderPasses: true,
  155. supportSpriteInstancing: false,
  156. forceVertexBufferStrideAndOffsetMultiple4Bytes: false,
  157. _collectUbosUpdatedInFrame: false,
  158. };
  159. Logger.Log(`Babylon.js v${Engine.Version} - Null engine`);
  160. // Wrappers
  161. const theCurrentGlobal = typeof self !== "undefined" ? self : typeof global !== "undefined" ? global : window;
  162. if (typeof URL === "undefined") {
  163. theCurrentGlobal.URL = {
  164. createObjectURL: function () { },
  165. revokeObjectURL: function () { },
  166. };
  167. }
  168. if (typeof Blob === "undefined") {
  169. theCurrentGlobal.Blob = function () { };
  170. }
  171. }
  172. /**
  173. * Creates a vertex buffer
  174. * @param vertices the data for the vertex buffer
  175. * @returns the new WebGL static buffer
  176. */
  177. createVertexBuffer(vertices) {
  178. const buffer = new DataBuffer();
  179. buffer.references = 1;
  180. return buffer;
  181. }
  182. /**
  183. * Creates a new index buffer
  184. * @param indices defines the content of the index buffer
  185. * @returns a new webGL buffer
  186. */
  187. createIndexBuffer(indices) {
  188. const buffer = new DataBuffer();
  189. buffer.references = 1;
  190. return buffer;
  191. }
  192. /**
  193. * Clear the current render buffer or the current render target (if any is set up)
  194. * @param color defines the color to use
  195. * @param backBuffer defines if the back buffer must be cleared
  196. * @param depth defines if the depth buffer must be cleared
  197. * @param stencil defines if the stencil buffer must be cleared
  198. */
  199. clear(color, backBuffer, depth, stencil = false) { }
  200. /**
  201. * Gets the current render width
  202. * @param useScreen defines if screen size must be used (or the current render target if any)
  203. * @returns a number defining the current render width
  204. */
  205. getRenderWidth(useScreen = false) {
  206. if (!useScreen && this._currentRenderTarget) {
  207. return this._currentRenderTarget.width;
  208. }
  209. return this._options.renderWidth;
  210. }
  211. /**
  212. * Gets the current render height
  213. * @param useScreen defines if screen size must be used (or the current render target if any)
  214. * @returns a number defining the current render height
  215. */
  216. getRenderHeight(useScreen = false) {
  217. if (!useScreen && this._currentRenderTarget) {
  218. return this._currentRenderTarget.height;
  219. }
  220. return this._options.renderHeight;
  221. }
  222. /**
  223. * Set the WebGL's viewport
  224. * @param viewport defines the viewport element to be used
  225. * @param requiredWidth defines the width required for rendering. If not provided the rendering canvas' width is used
  226. * @param requiredHeight defines the height required for rendering. If not provided the rendering canvas' height is used
  227. */
  228. setViewport(viewport, requiredWidth, requiredHeight) {
  229. this._cachedViewport = viewport;
  230. }
  231. createShaderProgram(pipelineContext, vertexCode, fragmentCode, defines, context) {
  232. return {
  233. // eslint-disable-next-line @typescript-eslint/naming-convention
  234. __SPECTOR_rebuildProgram: null,
  235. };
  236. }
  237. /**
  238. * Gets the list of webGL uniform locations associated with a specific program based on a list of uniform names
  239. * @param pipelineContext defines the pipeline context to use
  240. * @param uniformsNames defines the list of uniform names
  241. * @returns an array of webGL uniform locations
  242. */
  243. getUniforms(pipelineContext, uniformsNames) {
  244. return [];
  245. }
  246. /**
  247. * Gets the lsit of active attributes for a given webGL program
  248. * @param pipelineContext defines the pipeline context to use
  249. * @param attributesNames defines the list of attribute names to get
  250. * @returns an array of indices indicating the offset of each attribute
  251. */
  252. getAttributes(pipelineContext, attributesNames) {
  253. return [];
  254. }
  255. /**
  256. * Binds an effect to the webGL context
  257. * @param effect defines the effect to bind
  258. */
  259. bindSamplers(effect) {
  260. this._currentEffect = null;
  261. }
  262. /**
  263. * Activates an effect, making it the current one (ie. the one used for rendering)
  264. * @param effect defines the effect to activate
  265. */
  266. enableEffect(effect) {
  267. effect = effect !== null && IsWrapper(effect) ? effect.effect : effect; // get only the effect, we don't need a Wrapper in the WebGL engine
  268. this._currentEffect = effect;
  269. if (!effect) {
  270. return;
  271. }
  272. if (effect.onBind) {
  273. effect.onBind(effect);
  274. }
  275. if (effect._onBindObservable) {
  276. effect._onBindObservable.notifyObservers(effect);
  277. }
  278. }
  279. /**
  280. * Set various states to the webGL context
  281. * @param culling defines culling state: true to enable culling, false to disable it
  282. * @param zOffset defines the value to apply to zOffset (0 by default)
  283. * @param force defines if states must be applied even if cache is up to date
  284. * @param reverseSide defines if culling must be reversed (CCW if false, CW if true)
  285. * @param cullBackFaces true to cull back faces, false to cull front faces (if culling is enabled)
  286. * @param stencil stencil states to set
  287. * @param zOffsetUnits defines the value to apply to zOffsetUnits (0 by default)
  288. */
  289. setState(culling, zOffset = 0, force, reverseSide = false, cullBackFaces, stencil, zOffsetUnits = 0) { }
  290. /**
  291. * Set the value of an uniform to an array of int32
  292. * @param uniform defines the webGL uniform location where to store the value
  293. * @param array defines the array of int32 to store
  294. * @returns true if value was set
  295. */
  296. setIntArray(uniform, array) {
  297. return true;
  298. }
  299. /**
  300. * Set the value of an uniform to an array of int32 (stored as vec2)
  301. * @param uniform defines the webGL uniform location where to store the value
  302. * @param array defines the array of int32 to store
  303. * @returns true if value was set
  304. */
  305. setIntArray2(uniform, array) {
  306. return true;
  307. }
  308. /**
  309. * Set the value of an uniform to an array of int32 (stored as vec3)
  310. * @param uniform defines the webGL uniform location where to store the value
  311. * @param array defines the array of int32 to store
  312. * @returns true if value was set
  313. */
  314. setIntArray3(uniform, array) {
  315. return true;
  316. }
  317. /**
  318. * Set the value of an uniform to an array of int32 (stored as vec4)
  319. * @param uniform defines the webGL uniform location where to store the value
  320. * @param array defines the array of int32 to store
  321. * @returns true if value was set
  322. */
  323. setIntArray4(uniform, array) {
  324. return true;
  325. }
  326. /**
  327. * Set the value of an uniform to an array of float32
  328. * @param uniform defines the webGL uniform location where to store the value
  329. * @param array defines the array of float32 to store
  330. * @returns true if value was set
  331. */
  332. setFloatArray(uniform, array) {
  333. return true;
  334. }
  335. /**
  336. * Set the value of an uniform to an array of float32 (stored as vec2)
  337. * @param uniform defines the webGL uniform location where to store the value
  338. * @param array defines the array of float32 to store
  339. * @returns true if value was set
  340. */
  341. setFloatArray2(uniform, array) {
  342. return true;
  343. }
  344. /**
  345. * Set the value of an uniform to an array of float32 (stored as vec3)
  346. * @param uniform defines the webGL uniform location where to store the value
  347. * @param array defines the array of float32 to store
  348. * @returns true if value was set
  349. */
  350. setFloatArray3(uniform, array) {
  351. return true;
  352. }
  353. /**
  354. * Set the value of an uniform to an array of float32 (stored as vec4)
  355. * @param uniform defines the webGL uniform location where to store the value
  356. * @param array defines the array of float32 to store
  357. * @returns true if value was set
  358. */
  359. setFloatArray4(uniform, array) {
  360. return true;
  361. }
  362. /**
  363. * Set the value of an uniform to an array of number
  364. * @param uniform defines the webGL uniform location where to store the value
  365. * @param array defines the array of number to store
  366. * @returns true if value was set
  367. */
  368. setArray(uniform, array) {
  369. return true;
  370. }
  371. /**
  372. * Set the value of an uniform to an array of number (stored as vec2)
  373. * @param uniform defines the webGL uniform location where to store the value
  374. * @param array defines the array of number to store
  375. * @returns true if value was set
  376. */
  377. setArray2(uniform, array) {
  378. return true;
  379. }
  380. /**
  381. * Set the value of an uniform to an array of number (stored as vec3)
  382. * @param uniform defines the webGL uniform location where to store the value
  383. * @param array defines the array of number to store
  384. * @returns true if value was set
  385. */
  386. setArray3(uniform, array) {
  387. return true;
  388. }
  389. /**
  390. * Set the value of an uniform to an array of number (stored as vec4)
  391. * @param uniform defines the webGL uniform location where to store the value
  392. * @param array defines the array of number to store
  393. * @returns true if value was set
  394. */
  395. setArray4(uniform, array) {
  396. return true;
  397. }
  398. /**
  399. * Set the value of an uniform to an array of float32 (stored as matrices)
  400. * @param uniform defines the webGL uniform location where to store the value
  401. * @param matrices defines the array of float32 to store
  402. * @returns true if value was set
  403. */
  404. setMatrices(uniform, matrices) {
  405. return true;
  406. }
  407. /**
  408. * Set the value of an uniform to a matrix (3x3)
  409. * @param uniform defines the webGL uniform location where to store the value
  410. * @param matrix defines the Float32Array representing the 3x3 matrix to store
  411. * @returns true if value was set
  412. */
  413. setMatrix3x3(uniform, matrix) {
  414. return true;
  415. }
  416. /**
  417. * Set the value of an uniform to a matrix (2x2)
  418. * @param uniform defines the webGL uniform location where to store the value
  419. * @param matrix defines the Float32Array representing the 2x2 matrix to store
  420. * @returns true if value was set
  421. */
  422. setMatrix2x2(uniform, matrix) {
  423. return true;
  424. }
  425. /**
  426. * Set the value of an uniform to a number (float)
  427. * @param uniform defines the webGL uniform location where to store the value
  428. * @param value defines the float number to store
  429. * @returns true if value was set
  430. */
  431. setFloat(uniform, value) {
  432. return true;
  433. }
  434. /**
  435. * Set the value of an uniform to a vec2
  436. * @param uniform defines the webGL uniform location where to store the value
  437. * @param x defines the 1st component of the value
  438. * @param y defines the 2nd component of the value
  439. * @returns true if value was set
  440. */
  441. setFloat2(uniform, x, y) {
  442. return true;
  443. }
  444. /**
  445. * Set the value of an uniform to a vec3
  446. * @param uniform defines the webGL uniform location where to store the value
  447. * @param x defines the 1st component of the value
  448. * @param y defines the 2nd component of the value
  449. * @param z defines the 3rd component of the value
  450. * @returns true if value was set
  451. */
  452. setFloat3(uniform, x, y, z) {
  453. return true;
  454. }
  455. /**
  456. * Set the value of an uniform to a boolean
  457. * @param uniform defines the webGL uniform location where to store the value
  458. * @param bool defines the boolean to store
  459. * @returns true if value was set
  460. */
  461. setBool(uniform, bool) {
  462. return true;
  463. }
  464. /**
  465. * Set the value of an uniform to a vec4
  466. * @param uniform defines the webGL uniform location where to store the value
  467. * @param x defines the 1st component of the value
  468. * @param y defines the 2nd component of the value
  469. * @param z defines the 3rd component of the value
  470. * @param w defines the 4th component of the value
  471. * @returns true if value was set
  472. */
  473. setFloat4(uniform, x, y, z, w) {
  474. return true;
  475. }
  476. /**
  477. * Sets the current alpha mode
  478. * @param mode defines the mode to use (one of the Engine.ALPHA_XXX)
  479. * @param noDepthWriteChange defines if depth writing state should remains unchanged (false by default)
  480. * @see https://doc.babylonjs.com/features/featuresDeepDive/materials/advanced/transparent_rendering
  481. */
  482. setAlphaMode(mode, noDepthWriteChange = false) {
  483. if (this._alphaMode === mode) {
  484. return;
  485. }
  486. this.alphaState.alphaBlend = mode !== 0;
  487. if (!noDepthWriteChange) {
  488. this.setDepthWrite(mode === 0);
  489. }
  490. this._alphaMode = mode;
  491. }
  492. /**
  493. * Bind webGl buffers directly to the webGL context
  494. * @param vertexBuffers defines the vertex buffer to bind
  495. * @param indexBuffer defines the index buffer to bind
  496. * @param effect defines the effect associated with the vertex buffer
  497. */
  498. bindBuffers(vertexBuffers, indexBuffer, effect) { }
  499. /**
  500. * Force the entire cache to be cleared
  501. * You should not have to use this function unless your engine needs to share the webGL context with another engine
  502. * @param bruteForce defines a boolean to force clearing ALL caches (including stencil, detoh and alpha states)
  503. */
  504. wipeCaches(bruteForce) {
  505. if (this.preventCacheWipeBetweenFrames) {
  506. return;
  507. }
  508. this.resetTextureCache();
  509. this._currentEffect = null;
  510. if (bruteForce) {
  511. this._currentProgram = null;
  512. this._stencilStateComposer.reset();
  513. this.depthCullingState.reset();
  514. this.alphaState.reset();
  515. }
  516. this._cachedVertexBuffers = null;
  517. this._cachedIndexBuffer = null;
  518. this._cachedEffectForVertexBuffers = null;
  519. }
  520. /**
  521. * Send a draw order
  522. * @param useTriangles defines if triangles must be used to draw (else wireframe will be used)
  523. * @param indexStart defines the starting index
  524. * @param indexCount defines the number of index to draw
  525. * @param instancesCount defines the number of instances to draw (if instantiation is enabled)
  526. */
  527. draw(useTriangles, indexStart, indexCount, instancesCount) { }
  528. /**
  529. * Draw a list of indexed primitives
  530. * @param fillMode defines the primitive to use
  531. * @param indexStart defines the starting index
  532. * @param indexCount defines the number of index to draw
  533. * @param instancesCount defines the number of instances to draw (if instantiation is enabled)
  534. */
  535. drawElementsType(fillMode, indexStart, indexCount, instancesCount) { }
  536. /**
  537. * Draw a list of unindexed primitives
  538. * @param fillMode defines the primitive to use
  539. * @param verticesStart defines the index of first vertex to draw
  540. * @param verticesCount defines the count of vertices to draw
  541. * @param instancesCount defines the number of instances to draw (if instantiation is enabled)
  542. */
  543. drawArraysType(fillMode, verticesStart, verticesCount, instancesCount) { }
  544. /** @internal */
  545. _createTexture() {
  546. return {};
  547. }
  548. /**
  549. * @internal
  550. */
  551. _releaseTexture(texture) { }
  552. /**
  553. * Usually called from Texture.ts.
  554. * Passed information to create a WebGLTexture
  555. * @param urlArg defines a value which contains one of the following:
  556. * * A conventional http URL, e.g. 'http://...' or 'file://...'
  557. * * A base64 string of in-line texture data, e.g. 'data:image/jpg;base64,/...'
  558. * * An indicator that data being passed using the buffer parameter, e.g. 'data:mytexture.jpg'
  559. * @param noMipmap defines a boolean indicating that no mipmaps shall be generated. Ignored for compressed textures. They must be in the file
  560. * @param invertY when true, image is flipped when loaded. You probably want true. Certain compressed textures may invert this if their default is inverted (eg. ktx)
  561. * @param scene needed for loading to the correct scene
  562. * @param samplingMode mode with should be used sample / access the texture (Default: Texture.TRILINEAR_SAMPLINGMODE)
  563. * @param onLoad optional callback to be called upon successful completion
  564. * @param onError optional callback to be called upon failure
  565. * @param buffer a source of a file previously fetched as either a base64 string, an ArrayBuffer (compressed or image format), HTMLImageElement (image format), or a Blob
  566. * @param fallback an internal argument in case the function must be called again, due to etc1 not having alpha capabilities
  567. * @param format internal format. Default: RGB when extension is '.jpg' else RGBA. Ignored for compressed textures
  568. * @param forcedExtension defines the extension to use to pick the right loader
  569. * @param mimeType defines an optional mime type
  570. * @returns a InternalTexture for assignment back into BABYLON.Texture
  571. */
  572. createTexture(urlArg, noMipmap, invertY, scene, samplingMode = 3, onLoad = null, onError = null, buffer = null, fallback = null, format = null, forcedExtension = null, mimeType) {
  573. const texture = new InternalTexture(this, InternalTextureSource.Url);
  574. const url = String(urlArg);
  575. texture.url = url;
  576. texture.generateMipMaps = !noMipmap;
  577. texture.samplingMode = samplingMode;
  578. texture.invertY = invertY;
  579. texture.baseWidth = this._options.textureSize;
  580. texture.baseHeight = this._options.textureSize;
  581. texture.width = this._options.textureSize;
  582. texture.height = this._options.textureSize;
  583. if (format) {
  584. texture.format = format;
  585. }
  586. texture.isReady = true;
  587. if (onLoad) {
  588. setTimeout(() => {
  589. onLoad(texture);
  590. });
  591. }
  592. this._internalTexturesCache.push(texture);
  593. return texture;
  594. }
  595. /**
  596. * @internal
  597. */
  598. _createHardwareRenderTargetWrapper(isMulti, isCube, size) {
  599. const rtWrapper = new RenderTargetWrapper(isMulti, isCube, size, this);
  600. this._renderTargetWrapperCache.push(rtWrapper);
  601. return rtWrapper;
  602. }
  603. /**
  604. * Creates a new render target wrapper
  605. * @param size defines the size of the texture
  606. * @param options defines the options used to create the texture
  607. * @returns a new render target wrapper
  608. */
  609. createRenderTargetTexture(size, options) {
  610. const rtWrapper = this._createHardwareRenderTargetWrapper(false, false, size);
  611. const fullOptions = {};
  612. if (options !== undefined && typeof options === "object") {
  613. fullOptions.generateMipMaps = options.generateMipMaps;
  614. fullOptions.generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  615. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && options.generateStencilBuffer;
  616. fullOptions.type = options.type === undefined ? 0 : options.type;
  617. fullOptions.samplingMode = options.samplingMode === undefined ? 3 : options.samplingMode;
  618. }
  619. else {
  620. fullOptions.generateMipMaps = options;
  621. fullOptions.generateDepthBuffer = true;
  622. fullOptions.generateStencilBuffer = false;
  623. fullOptions.type = 0;
  624. fullOptions.samplingMode = 3;
  625. }
  626. const texture = new InternalTexture(this, InternalTextureSource.RenderTarget);
  627. const width = size.width || size;
  628. const height = size.height || size;
  629. rtWrapper._generateDepthBuffer = fullOptions.generateDepthBuffer;
  630. rtWrapper._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;
  631. texture.baseWidth = width;
  632. texture.baseHeight = height;
  633. texture.width = width;
  634. texture.height = height;
  635. texture.isReady = true;
  636. texture.samples = 1;
  637. texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
  638. texture.samplingMode = fullOptions.samplingMode;
  639. texture.type = fullOptions.type;
  640. this._internalTexturesCache.push(texture);
  641. return rtWrapper;
  642. }
  643. /**
  644. * Creates a new render target wrapper
  645. * @param size defines the size of the texture
  646. * @param options defines the options used to create the texture
  647. * @returns a new render target wrapper
  648. */
  649. createRenderTargetCubeTexture(size, options) {
  650. const rtWrapper = this._createHardwareRenderTargetWrapper(false, true, size);
  651. const fullOptions = {
  652. generateMipMaps: true,
  653. generateDepthBuffer: true,
  654. generateStencilBuffer: false,
  655. type: 0,
  656. samplingMode: 3,
  657. format: 5,
  658. ...options,
  659. };
  660. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && fullOptions.generateStencilBuffer;
  661. if (fullOptions.type === 1 && !this._caps.textureFloatLinearFiltering) {
  662. // if floating point linear (gl.FLOAT) then force to NEAREST_SAMPLINGMODE
  663. fullOptions.samplingMode = 1;
  664. }
  665. else if (fullOptions.type === 2 && !this._caps.textureHalfFloatLinearFiltering) {
  666. // if floating point linear (HALF_FLOAT) then force to NEAREST_SAMPLINGMODE
  667. fullOptions.samplingMode = 1;
  668. }
  669. rtWrapper._generateDepthBuffer = fullOptions.generateDepthBuffer;
  670. rtWrapper._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;
  671. const texture = new InternalTexture(this, InternalTextureSource.RenderTarget);
  672. texture.baseWidth = size;
  673. texture.baseHeight = size;
  674. texture.width = size;
  675. texture.height = size;
  676. texture.isReady = true;
  677. texture.isCube = true;
  678. texture.samples = 1;
  679. texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
  680. texture.samplingMode = fullOptions.samplingMode;
  681. texture.type = fullOptions.type;
  682. this._internalTexturesCache.push(texture);
  683. return rtWrapper;
  684. }
  685. /**
  686. * Update the sampling mode of a given texture
  687. * @param samplingMode defines the required sampling mode
  688. * @param texture defines the texture to update
  689. */
  690. updateTextureSamplingMode(samplingMode, texture) {
  691. texture.samplingMode = samplingMode;
  692. }
  693. /**
  694. * Creates a raw texture
  695. * @param data defines the data to store in the texture
  696. * @param width defines the width of the texture
  697. * @param height defines the height of the texture
  698. * @param format defines the format of the data
  699. * @param generateMipMaps defines if the engine should generate the mip levels
  700. * @param invertY defines if data must be stored with Y axis inverted
  701. * @param samplingMode defines the required sampling mode (Texture.NEAREST_SAMPLINGMODE by default)
  702. * @param compression defines the compression used (null by default)
  703. * @param type defines the type fo the data (Engine.TEXTURETYPE_UNSIGNED_INT by default)
  704. * @param creationFlags specific flags to use when creating the texture (1 for storage textures, for eg)
  705. * @param useSRGBBuffer defines if the texture must be loaded in a sRGB GPU buffer (if supported by the GPU).
  706. * @returns the raw texture inside an InternalTexture
  707. */
  708. createRawTexture(data, width, height, format, generateMipMaps, invertY, samplingMode, compression = null, type = 0, creationFlags = 0, useSRGBBuffer = false) {
  709. const texture = new InternalTexture(this, InternalTextureSource.Raw);
  710. texture.baseWidth = width;
  711. texture.baseHeight = height;
  712. texture.width = width;
  713. texture.height = height;
  714. texture.format = format;
  715. texture.generateMipMaps = generateMipMaps;
  716. texture.samplingMode = samplingMode;
  717. texture.invertY = invertY;
  718. texture._compression = compression;
  719. texture.type = type;
  720. texture._useSRGBBuffer = useSRGBBuffer;
  721. if (!this._doNotHandleContextLost) {
  722. texture._bufferView = data;
  723. }
  724. return texture;
  725. }
  726. /**
  727. * Update a raw texture
  728. * @param texture defines the texture to update
  729. * @param data defines the data to store in the texture
  730. * @param format defines the format of the data
  731. * @param invertY defines if data must be stored with Y axis inverted
  732. * @param compression defines the compression used (null by default)
  733. * @param type defines the type fo the data (Engine.TEXTURETYPE_UNSIGNED_INT by default)
  734. * @param useSRGBBuffer defines if the texture must be loaded in a sRGB GPU buffer (if supported by the GPU).
  735. */
  736. updateRawTexture(texture, data, format, invertY, compression = null, type = 0, useSRGBBuffer = false) {
  737. if (texture) {
  738. texture._bufferView = data;
  739. texture.format = format;
  740. texture.invertY = invertY;
  741. texture._compression = compression;
  742. texture.type = type;
  743. texture._useSRGBBuffer = useSRGBBuffer;
  744. }
  745. }
  746. /**
  747. * Binds the frame buffer to the specified texture.
  748. * @param rtWrapper The render target wrapper to render to
  749. * @param faceIndex The face of the texture to render to in case of cube texture
  750. * @param requiredWidth The width of the target to render to
  751. * @param requiredHeight The height of the target to render to
  752. * @param forceFullscreenViewport Forces the viewport to be the entire texture/screen if true
  753. */
  754. bindFramebuffer(rtWrapper, faceIndex, requiredWidth, requiredHeight, forceFullscreenViewport) {
  755. if (this._currentRenderTarget) {
  756. this.unBindFramebuffer(this._currentRenderTarget);
  757. }
  758. this._currentRenderTarget = rtWrapper;
  759. this._currentFramebuffer = null;
  760. if (this._cachedViewport && !forceFullscreenViewport) {
  761. this.setViewport(this._cachedViewport, requiredWidth, requiredHeight);
  762. }
  763. }
  764. /**
  765. * Unbind the current render target texture from the webGL context
  766. * @param rtWrapper defines the render target wrapper to unbind
  767. * @param disableGenerateMipMaps defines a boolean indicating that mipmaps must not be generated
  768. * @param onBeforeUnbind defines a function which will be called before the effective unbind
  769. */
  770. unBindFramebuffer(rtWrapper, disableGenerateMipMaps = false, onBeforeUnbind) {
  771. this._currentRenderTarget = null;
  772. if (onBeforeUnbind) {
  773. onBeforeUnbind();
  774. }
  775. this._currentFramebuffer = null;
  776. }
  777. /**
  778. * Creates a dynamic vertex buffer
  779. * @param vertices the data for the dynamic vertex buffer
  780. * @returns the new WebGL dynamic buffer
  781. */
  782. createDynamicVertexBuffer(vertices) {
  783. const buffer = new DataBuffer();
  784. buffer.references = 1;
  785. buffer.capacity = 1;
  786. return buffer;
  787. }
  788. /**
  789. * Update the content of a dynamic texture
  790. * @param texture defines the texture to update
  791. * @param canvas defines the canvas containing the source
  792. * @param invertY defines if data must be stored with Y axis inverted
  793. * @param premulAlpha defines if alpha is stored as premultiplied
  794. * @param format defines the format of the data
  795. */
  796. updateDynamicTexture(texture, canvas, invertY, premulAlpha = false, format) { }
  797. /**
  798. * Gets a boolean indicating if all created effects are ready
  799. * @returns true if all effects are ready
  800. */
  801. areAllEffectsReady() {
  802. return true;
  803. }
  804. /**
  805. * @internal
  806. * Get the current error code of the webGL context
  807. * @returns the error code
  808. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/getError
  809. */
  810. getError() {
  811. return 0;
  812. }
  813. /** @internal */
  814. _getUnpackAlignement() {
  815. return 1;
  816. }
  817. /**
  818. * @internal
  819. */
  820. _unpackFlipY(value) { }
  821. /**
  822. * Update a dynamic index buffer
  823. * @param indexBuffer defines the target index buffer
  824. * @param indices defines the data to update
  825. * @param offset defines the offset in the target index buffer where update should start
  826. */
  827. updateDynamicIndexBuffer(indexBuffer, indices, offset = 0) { }
  828. /**
  829. * Updates a dynamic vertex buffer.
  830. * @param vertexBuffer the vertex buffer to update
  831. * @param vertices the data used to update the vertex buffer
  832. * @param byteOffset the byte offset of the data (optional)
  833. * @param byteLength the byte length of the data (optional)
  834. */
  835. updateDynamicVertexBuffer(vertexBuffer, vertices, byteOffset, byteLength) { }
  836. /**
  837. * @internal
  838. */
  839. _bindTextureDirectly(target, texture) {
  840. if (this._boundTexturesCache[this._activeChannel] !== texture) {
  841. this._boundTexturesCache[this._activeChannel] = texture;
  842. return true;
  843. }
  844. return false;
  845. }
  846. /**
  847. * @internal
  848. */
  849. _bindTexture(channel, texture) {
  850. if (channel < 0) {
  851. return;
  852. }
  853. this._bindTextureDirectly(0, texture);
  854. }
  855. _deleteBuffer(buffer) { }
  856. /**
  857. * Force the engine to release all cached effects. This means that next effect compilation will have to be done completely even if a similar effect was already compiled
  858. */
  859. releaseEffects() { }
  860. displayLoadingUI() { }
  861. hideLoadingUI() { }
  862. set loadingUIText(_) { }
  863. /**
  864. * @internal
  865. */
  866. _uploadCompressedDataToTextureDirectly(texture, internalFormat, width, height, data, faceIndex = 0, lod = 0) { }
  867. /**
  868. * @internal
  869. */
  870. _uploadDataToTextureDirectly(texture, imageData, faceIndex = 0, lod = 0) { }
  871. /**
  872. * @internal
  873. */
  874. _uploadArrayBufferViewToTexture(texture, imageData, faceIndex = 0, lod = 0) { }
  875. /**
  876. * @internal
  877. */
  878. _uploadImageToTexture(texture, image, faceIndex = 0, lod = 0) { }
  879. }
  880. //# sourceMappingURL=nullEngine.js.map