nullEngine.ts 18 KB

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