nullEngine.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. import { Logger } from "Misc/logger";
  2. import { Nullable, FloatArray, IndicesArray } from "types";
  3. import { Scene } from "scene";
  4. import { Matrix, Color3, Color4, Viewport } from "Maths/math";
  5. import { Engine, EngineCapabilities, RenderTargetCreationOptions } from "Engines/engine";
  6. import { VertexBuffer } from "Meshes/buffer";
  7. import { InternalTexture } from "Materials/Textures/internalTexture";
  8. import { Effect } from "Materials/effect";
  9. import { _TimeToken } from "Instrumentation/timeToken";
  10. import { _DepthCullingState, _StencilState, _AlphaState } from "States";
  11. import { Constants } from "./constants";
  12. /**
  13. * Options to create the null engine
  14. */
  15. export class NullEngineOptions {
  16. /**
  17. * Render width (Default: 512)
  18. */
  19. public renderWidth = 512;
  20. /**
  21. * Render height (Default: 256)
  22. */
  23. public renderHeight = 256;
  24. /**
  25. * Texture size (Default: 512)
  26. */
  27. public textureSize = 512;
  28. /**
  29. * If delta time between frames should be constant
  30. * @see https://doc.babylonjs.com/babylon101/animations#deterministic-lockstep
  31. */
  32. public deterministicLockstep = false;
  33. /**
  34. * Maximum about of steps between frames (Default: 4)
  35. * @see https://doc.babylonjs.com/babylon101/animations#deterministic-lockstep
  36. */
  37. public lockstepMaxSteps = 4;
  38. }
  39. /**
  40. * The null engine class provides support for headless version of babylon.js.
  41. * This can be used in server side scenario or for testing purposes
  42. */
  43. export class NullEngine extends Engine {
  44. private _options: NullEngineOptions;
  45. /**
  46. * @see https://doc.babylonjs.com/babylon101/animations#deterministic-lockstep
  47. */
  48. public isDeterministicLockStep(): boolean {
  49. return this._options.deterministicLockstep;
  50. }
  51. /** @see https://doc.babylonjs.com/babylon101/animations#deterministic-lockstep */
  52. public getLockstepMaxSteps(): number {
  53. return this._options.lockstepMaxSteps;
  54. }
  55. /**
  56. * Sets hardware scaling, used to save performance if needed
  57. * @see https://doc.babylonjs.com/how_to/how_to_use_sceneoptimizer
  58. */
  59. public getHardwareScalingLevel(): number {
  60. return 1.0;
  61. }
  62. public constructor(options: NullEngineOptions = new NullEngineOptions()) {
  63. super(null);
  64. if (options.deterministicLockstep === undefined) {
  65. options.deterministicLockstep = false;
  66. }
  67. if (options.lockstepMaxSteps === undefined) {
  68. options.lockstepMaxSteps = 4;
  69. }
  70. this._options = options;
  71. // Init caps
  72. // We consider we are on a webgl1 capable device
  73. this._caps = new EngineCapabilities();
  74. this._caps.maxTexturesImageUnits = 16;
  75. this._caps.maxVertexTextureImageUnits = 16;
  76. this._caps.maxTextureSize = 512;
  77. this._caps.maxCubemapTextureSize = 512;
  78. this._caps.maxRenderTextureSize = 512;
  79. this._caps.maxVertexAttribs = 16;
  80. this._caps.maxVaryingVectors = 16;
  81. this._caps.maxFragmentUniformVectors = 16;
  82. this._caps.maxVertexUniformVectors = 16;
  83. // Extensions
  84. this._caps.standardDerivatives = false;
  85. this._caps.astc = null;
  86. this._caps.s3tc = null;
  87. this._caps.pvrtc = null;
  88. this._caps.etc1 = null;
  89. this._caps.etc2 = null;
  90. this._caps.textureAnisotropicFilterExtension = null;
  91. this._caps.maxAnisotropy = 0;
  92. this._caps.uintIndices = false;
  93. this._caps.fragmentDepthSupported = false;
  94. this._caps.highPrecisionShaderSupported = true;
  95. this._caps.colorBufferFloat = false;
  96. this._caps.textureFloat = false;
  97. this._caps.textureFloatLinearFiltering = false;
  98. this._caps.textureFloatRender = false;
  99. this._caps.textureHalfFloat = false;
  100. this._caps.textureHalfFloatLinearFiltering = false;
  101. this._caps.textureHalfFloatRender = false;
  102. this._caps.textureLOD = false;
  103. this._caps.drawBuffersExtension = false;
  104. this._caps.depthTextureExtension = false;
  105. this._caps.vertexArrayObject = false;
  106. this._caps.instancedArrays = false;
  107. Logger.Log(`Babylon.js v${Engine.Version} - Null engine`);
  108. // Wrappers
  109. if (typeof URL === "undefined") {
  110. (<any>URL) = {
  111. createObjectURL: function() { },
  112. revokeObjectURL: function() { }
  113. };
  114. }
  115. if (typeof Blob === "undefined") {
  116. (<any>Blob) = function() { };
  117. }
  118. }
  119. public createVertexBuffer(vertices: FloatArray): WebGLBuffer {
  120. return {
  121. capacity: 0,
  122. references: 1,
  123. is32Bits: false
  124. };
  125. }
  126. public createIndexBuffer(indices: IndicesArray): WebGLBuffer {
  127. return {
  128. capacity: 0,
  129. references: 1,
  130. is32Bits: false
  131. };
  132. }
  133. public clear(color: Color4, backBuffer: boolean, depth: boolean, stencil: boolean = false): void {
  134. }
  135. public getRenderWidth(useScreen = false): number {
  136. if (!useScreen && this._currentRenderTarget) {
  137. return this._currentRenderTarget.width;
  138. }
  139. return this._options.renderWidth;
  140. }
  141. public getRenderHeight(useScreen = false): number {
  142. if (!useScreen && this._currentRenderTarget) {
  143. return this._currentRenderTarget.height;
  144. }
  145. return this._options.renderHeight;
  146. }
  147. public setViewport(viewport: Viewport, requiredWidth?: number, requiredHeight?: number): void {
  148. this._cachedViewport = viewport;
  149. }
  150. public createShaderProgram(vertexCode: string, fragmentCode: string, defines: string, context?: WebGLRenderingContext): WebGLProgram {
  151. return {
  152. transformFeedback: null,
  153. __SPECTOR_rebuildProgram: null,
  154. isParallelCompiled: false
  155. };
  156. }
  157. public getUniforms(shaderProgram: WebGLProgram, uniformsNames: string[]): WebGLUniformLocation[] {
  158. return [];
  159. }
  160. public getAttributes(shaderProgram: WebGLProgram, attributesNames: string[]): number[] {
  161. return [];
  162. }
  163. public bindSamplers(effect: Effect): void {
  164. this._currentEffect = null;
  165. }
  166. public enableEffect(effect: Effect): void {
  167. this._currentEffect = effect;
  168. if (effect.onBind) {
  169. effect.onBind(effect);
  170. }
  171. if (effect._onBindObservable) {
  172. effect._onBindObservable.notifyObservers(effect);
  173. }
  174. }
  175. public setState(culling: boolean, zOffset: number = 0, force?: boolean, reverseSide = false): void {
  176. }
  177. public setIntArray(uniform: WebGLUniformLocation, array: Int32Array): void {
  178. }
  179. public setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): void {
  180. }
  181. public setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): void {
  182. }
  183. public setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): void {
  184. }
  185. public setFloatArray(uniform: WebGLUniformLocation, array: Float32Array): void {
  186. }
  187. public setFloatArray2(uniform: WebGLUniformLocation, array: Float32Array): void {
  188. }
  189. public setFloatArray3(uniform: WebGLUniformLocation, array: Float32Array): void {
  190. }
  191. public setFloatArray4(uniform: WebGLUniformLocation, array: Float32Array): void {
  192. }
  193. public setArray(uniform: WebGLUniformLocation, array: number[]): void {
  194. }
  195. public setArray2(uniform: WebGLUniformLocation, array: number[]): void {
  196. }
  197. public setArray3(uniform: WebGLUniformLocation, array: number[]): void {
  198. }
  199. public setArray4(uniform: WebGLUniformLocation, array: number[]): void {
  200. }
  201. public setMatrices(uniform: WebGLUniformLocation, matrices: Float32Array): void {
  202. }
  203. public setMatrix(uniform: WebGLUniformLocation, matrix: Matrix): void {
  204. }
  205. public setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  206. }
  207. public setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  208. }
  209. public setFloat(uniform: WebGLUniformLocation, value: number): void {
  210. }
  211. public setFloat2(uniform: WebGLUniformLocation, x: number, y: number): void {
  212. }
  213. public setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): void {
  214. }
  215. public setBool(uniform: WebGLUniformLocation, bool: number): void {
  216. }
  217. public setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): void {
  218. }
  219. public setColor3(uniform: WebGLUniformLocation, color3: Color3): void {
  220. }
  221. public setColor4(uniform: WebGLUniformLocation, color3: Color3, alpha: number): void {
  222. }
  223. public setAlphaMode(mode: number, noDepthWriteChange: boolean = false): void {
  224. if (this._alphaMode === mode) {
  225. return;
  226. }
  227. this._alphaState.alphaBlend = (mode !== Constants.ALPHA_DISABLE);
  228. if (!noDepthWriteChange) {
  229. this.setDepthWrite(mode === Constants.ALPHA_DISABLE);
  230. }
  231. this._alphaMode = mode;
  232. }
  233. public bindBuffers(vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: WebGLBuffer, effect: Effect): void {
  234. }
  235. public wipeCaches(bruteForce?: boolean): void {
  236. if (this.preventCacheWipeBetweenFrames) {
  237. return;
  238. }
  239. this.resetTextureCache();
  240. this._currentEffect = null;
  241. if (bruteForce) {
  242. this._currentProgram = null;
  243. this._stencilState.reset();
  244. this._depthCullingState.reset();
  245. this._alphaState.reset();
  246. }
  247. this._cachedVertexBuffers = null;
  248. this._cachedIndexBuffer = null;
  249. this._cachedEffectForVertexBuffers = null;
  250. }
  251. public draw(useTriangles: boolean, indexStart: number, indexCount: number, instancesCount?: number): void {
  252. }
  253. public drawElementsType(fillMode: number, indexStart: number, indexCount: number, instancesCount?: number): void {
  254. }
  255. public drawArraysType(fillMode: number, verticesStart: number, verticesCount: number, instancesCount?: number): void {
  256. }
  257. /** @hidden */
  258. public _createTexture(): WebGLTexture {
  259. return {};
  260. }
  261. /** @hidden */
  262. public _releaseTexture(texture: InternalTexture): void {
  263. }
  264. public createTexture(urlArg: string, noMipmap: boolean, invertY: boolean, scene: Scene, samplingMode: number = Constants.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 {
  265. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_URL);
  266. var url = String(urlArg);
  267. texture.url = url;
  268. texture.generateMipMaps = !noMipmap;
  269. texture.samplingMode = samplingMode;
  270. texture.invertY = invertY;
  271. texture.baseWidth = this._options.textureSize;
  272. texture.baseHeight = this._options.textureSize;
  273. texture.width = this._options.textureSize;
  274. texture.height = this._options.textureSize;
  275. if (format) {
  276. texture.format = format;
  277. }
  278. texture.isReady = true;
  279. if (onLoad) {
  280. onLoad();
  281. }
  282. this._internalTexturesCache.push(texture);
  283. return texture;
  284. }
  285. public createRenderTargetTexture(size: any, options: boolean | RenderTargetCreationOptions): InternalTexture {
  286. let fullOptions = new RenderTargetCreationOptions();
  287. if (options !== undefined && typeof options === "object") {
  288. fullOptions.generateMipMaps = options.generateMipMaps;
  289. fullOptions.generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  290. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && options.generateStencilBuffer;
  291. fullOptions.type = options.type === undefined ? Constants.TEXTURETYPE_UNSIGNED_INT : options.type;
  292. fullOptions.samplingMode = options.samplingMode === undefined ? Constants.TEXTURE_TRILINEAR_SAMPLINGMODE : options.samplingMode;
  293. } else {
  294. fullOptions.generateMipMaps = <boolean>options;
  295. fullOptions.generateDepthBuffer = true;
  296. fullOptions.generateStencilBuffer = false;
  297. fullOptions.type = Constants.TEXTURETYPE_UNSIGNED_INT;
  298. fullOptions.samplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
  299. }
  300. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RENDERTARGET);
  301. var width = size.width || size;
  302. var height = size.height || size;
  303. texture._depthStencilBuffer = {};
  304. texture._framebuffer = {};
  305. texture.baseWidth = width;
  306. texture.baseHeight = height;
  307. texture.width = width;
  308. texture.height = height;
  309. texture.isReady = true;
  310. texture.samples = 1;
  311. texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
  312. texture.samplingMode = fullOptions.samplingMode;
  313. texture.type = fullOptions.type;
  314. texture._generateDepthBuffer = fullOptions.generateDepthBuffer;
  315. texture._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;
  316. this._internalTexturesCache.push(texture);
  317. return texture;
  318. }
  319. public updateTextureSamplingMode(samplingMode: number, texture: InternalTexture): void {
  320. texture.samplingMode = samplingMode;
  321. }
  322. public bindFramebuffer(texture: InternalTexture, faceIndex?: number, requiredWidth?: number, requiredHeight?: number, forceFullscreenViewport?: boolean): void {
  323. if (this._currentRenderTarget) {
  324. this.unBindFramebuffer(this._currentRenderTarget);
  325. }
  326. this._currentRenderTarget = texture;
  327. this._currentFramebuffer = texture._MSAAFramebuffer ? texture._MSAAFramebuffer : texture._framebuffer;
  328. if (this._cachedViewport && !forceFullscreenViewport) {
  329. this.setViewport(this._cachedViewport, requiredWidth, requiredHeight);
  330. }
  331. }
  332. public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
  333. this._currentRenderTarget = null;
  334. if (onBeforeUnbind) {
  335. if (texture._MSAAFramebuffer) {
  336. this._currentFramebuffer = texture._framebuffer;
  337. }
  338. onBeforeUnbind();
  339. }
  340. this._currentFramebuffer = null;
  341. }
  342. public createDynamicVertexBuffer(vertices: FloatArray): WebGLBuffer {
  343. var vbo = {
  344. capacity: 1,
  345. references: 1,
  346. is32Bits: false
  347. };
  348. return vbo;
  349. }
  350. public updateDynamicTexture(texture: Nullable<InternalTexture>, canvas: HTMLCanvasElement, invertY: boolean, premulAlpha: boolean = false, format?: number): void {
  351. }
  352. public areAllEffectsReady(): boolean {
  353. return true;
  354. }
  355. /**
  356. * @hidden
  357. * Get the current error code of the webGL context
  358. * @returns the error code
  359. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/getError
  360. */
  361. public getError(): number {
  362. return 0;
  363. }
  364. /** @hidden */
  365. public _getUnpackAlignement(): number {
  366. return 1;
  367. }
  368. /** @hidden */
  369. public _unpackFlipY(value: boolean) {
  370. }
  371. public updateDynamicIndexBuffer(indexBuffer: WebGLBuffer, indices: IndicesArray, offset: number = 0): void {
  372. }
  373. /**
  374. * Updates a dynamic vertex buffer.
  375. * @param vertexBuffer the vertex buffer to update
  376. * @param data the data used to update the vertex buffer
  377. * @param byteOffset the byte offset of the data (optional)
  378. * @param byteLength the byte length of the data (optional)
  379. */
  380. public updateDynamicVertexBuffer(vertexBuffer: WebGLBuffer, vertices: FloatArray, byteOffset?: number, byteLength?: number): void {
  381. }
  382. protected _bindTextureDirectly(target: number, texture: InternalTexture): boolean {
  383. if (this._boundTexturesCache[this._activeChannel] !== texture) {
  384. this._boundTexturesCache[this._activeChannel] = texture;
  385. return true;
  386. }
  387. return false;
  388. }
  389. /** @hidden */
  390. public _bindTexture(channel: number, texture: InternalTexture): void {
  391. if (channel < 0) {
  392. return;
  393. }
  394. this._bindTextureDirectly(0, texture);
  395. }
  396. /** @hidden */
  397. public _releaseBuffer(buffer: WebGLBuffer): boolean {
  398. buffer.references--;
  399. if (buffer.references === 0) {
  400. return true;
  401. }
  402. return false;
  403. }
  404. public releaseEffects() {
  405. }
  406. public displayLoadingUI(): void {
  407. }
  408. public hideLoadingUI(): void {
  409. }
  410. /** @hidden */
  411. public _uploadCompressedDataToTextureDirectly(texture: InternalTexture, internalFormat: number, width: number, height: number, data: ArrayBufferView, faceIndex: number = 0, lod: number = 0) {
  412. }
  413. /** @hidden */
  414. public _uploadDataToTextureDirectly(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
  415. }
  416. /** @hidden */
  417. public _uploadArrayBufferViewToTexture(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
  418. }
  419. /** @hidden */
  420. public _uploadImageToTexture(texture: InternalTexture, image: HTMLImageElement, faceIndex: number = 0, lod: number = 0) {
  421. }
  422. }