babylon.nullEngine.ts 19 KB

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