babylon.nullEngine.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 null engine (v" + Engine.Version + ") launched");
  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. };
  145. }
  146. public getUniforms(shaderProgram: WebGLProgram, uniformsNames: string[]): WebGLUniformLocation[] {
  147. return [];
  148. }
  149. public getAttributes(shaderProgram: WebGLProgram, attributesNames: string[]): number[] {
  150. return [];
  151. }
  152. public bindSamplers(effect: Effect): void {
  153. this._currentEffect = null;
  154. }
  155. public enableEffect(effect: Effect): void {
  156. this._currentEffect = effect;
  157. if (effect.onBind) {
  158. effect.onBind(effect);
  159. }
  160. if (effect._onBindObservable) {
  161. effect._onBindObservable.notifyObservers(effect);
  162. }
  163. }
  164. public setState(culling: boolean, zOffset: number = 0, force?: boolean, reverseSide = false): void {
  165. }
  166. public setIntArray(uniform: WebGLUniformLocation, array: Int32Array): void {
  167. }
  168. public setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): void {
  169. }
  170. public setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): void {
  171. }
  172. public setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): void {
  173. }
  174. public setFloatArray(uniform: WebGLUniformLocation, array: Float32Array): void {
  175. }
  176. public setFloatArray2(uniform: WebGLUniformLocation, array: Float32Array): void {
  177. }
  178. public setFloatArray3(uniform: WebGLUniformLocation, array: Float32Array): void {
  179. }
  180. public setFloatArray4(uniform: WebGLUniformLocation, array: Float32Array): void {
  181. }
  182. public setArray(uniform: WebGLUniformLocation, array: number[]): void {
  183. }
  184. public setArray2(uniform: WebGLUniformLocation, array: number[]): void {
  185. }
  186. public setArray3(uniform: WebGLUniformLocation, array: number[]): void {
  187. }
  188. public setArray4(uniform: WebGLUniformLocation, array: number[]): void {
  189. }
  190. public setMatrices(uniform: WebGLUniformLocation, matrices: Float32Array): void {
  191. }
  192. public setMatrix(uniform: WebGLUniformLocation, matrix: Matrix): void {
  193. }
  194. public setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  195. }
  196. public setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  197. }
  198. public setFloat(uniform: WebGLUniformLocation, value: number): void {
  199. }
  200. public setFloat2(uniform: WebGLUniformLocation, x: number, y: number): void {
  201. }
  202. public setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): void {
  203. }
  204. public setBool(uniform: WebGLUniformLocation, bool: number): void {
  205. }
  206. public setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): void {
  207. }
  208. public setColor3(uniform: WebGLUniformLocation, color3: Color3): void {
  209. }
  210. public setColor4(uniform: WebGLUniformLocation, color3: Color3, alpha: number): void {
  211. }
  212. public setAlphaMode(mode: number, noDepthWriteChange: boolean = false): void {
  213. if (this._alphaMode === mode) {
  214. return;
  215. }
  216. this._alphaState.alphaBlend = (mode !== Engine.ALPHA_DISABLE);
  217. if (!noDepthWriteChange) {
  218. this.setDepthWrite(mode === Engine.ALPHA_DISABLE);
  219. }
  220. this._alphaMode = mode;
  221. }
  222. public bindBuffers(vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: WebGLBuffer, effect: Effect): void {
  223. }
  224. public wipeCaches(bruteForce?: boolean): void {
  225. if (this.preventCacheWipeBetweenFrames) {
  226. return;
  227. }
  228. this.resetTextureCache();
  229. this._currentEffect = null;
  230. if (bruteForce) {
  231. this._currentProgram = null;
  232. this._stencilState.reset();
  233. this._depthCullingState.reset();
  234. this._alphaState.reset();
  235. }
  236. this._cachedVertexBuffers = null;
  237. this._cachedIndexBuffer = null;
  238. this._cachedEffectForVertexBuffers = null;
  239. }
  240. public draw(useTriangles: boolean, indexStart: number, indexCount: number, instancesCount?: number): void {
  241. }
  242. public drawElementsType(fillMode: number, indexStart: number, indexCount: number, instancesCount?: number): void {
  243. }
  244. public drawArraysType(fillMode: number, verticesStart: number, verticesCount: number, instancesCount?: number): void {
  245. }
  246. /** @hidden */
  247. public _createTexture(): WebGLTexture {
  248. return {};
  249. }
  250. /** @hidden */
  251. public _releaseTexture(texture: InternalTexture): void {
  252. }
  253. 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 {
  254. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_URL);
  255. var url = String(urlArg);
  256. texture.url = url;
  257. texture.generateMipMaps = !noMipmap;
  258. texture.samplingMode = samplingMode;
  259. texture.invertY = invertY;
  260. texture.baseWidth = this._options.textureSize;
  261. texture.baseHeight = this._options.textureSize;
  262. texture.width = this._options.textureSize;
  263. texture.height = this._options.textureSize;
  264. if (format) {
  265. texture.format = format;
  266. }
  267. texture.isReady = true;
  268. if (onLoad) {
  269. onLoad();
  270. }
  271. this._internalTexturesCache.push(texture);
  272. return texture;
  273. }
  274. public createRenderTargetTexture(size: any, options: boolean | RenderTargetCreationOptions): InternalTexture {
  275. let fullOptions = new RenderTargetCreationOptions();
  276. if (options !== undefined && typeof options === "object") {
  277. fullOptions.generateMipMaps = options.generateMipMaps;
  278. fullOptions.generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  279. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && options.generateStencilBuffer;
  280. fullOptions.type = options.type === undefined ? Engine.TEXTURETYPE_UNSIGNED_INT : options.type;
  281. fullOptions.samplingMode = options.samplingMode === undefined ? Texture.TRILINEAR_SAMPLINGMODE : options.samplingMode;
  282. } else {
  283. fullOptions.generateMipMaps = <boolean>options;
  284. fullOptions.generateDepthBuffer = true;
  285. fullOptions.generateStencilBuffer = false;
  286. fullOptions.type = Engine.TEXTURETYPE_UNSIGNED_INT;
  287. fullOptions.samplingMode = Texture.TRILINEAR_SAMPLINGMODE;
  288. }
  289. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RENDERTARGET);
  290. var width = size.width || size;
  291. var height = size.height || size;
  292. texture._depthStencilBuffer = {};
  293. texture._framebuffer = {};
  294. texture.baseWidth = width;
  295. texture.baseHeight = height;
  296. texture.width = width;
  297. texture.height = height;
  298. texture.isReady = true;
  299. texture.samples = 1;
  300. texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
  301. texture.samplingMode = fullOptions.samplingMode;
  302. texture.type = fullOptions.type;
  303. texture._generateDepthBuffer = fullOptions.generateDepthBuffer;
  304. texture._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;
  305. this._internalTexturesCache.push(texture);
  306. return texture;
  307. }
  308. public updateTextureSamplingMode(samplingMode: number, texture: InternalTexture): void {
  309. texture.samplingMode = samplingMode;
  310. }
  311. public bindFramebuffer(texture: InternalTexture, faceIndex?: number, requiredWidth?: number, requiredHeight?: number, forceFullscreenViewport?: boolean): void {
  312. if (this._currentRenderTarget) {
  313. this.unBindFramebuffer(this._currentRenderTarget);
  314. }
  315. this._currentRenderTarget = texture;
  316. this._currentFramebuffer = texture._MSAAFramebuffer ? texture._MSAAFramebuffer : texture._framebuffer;
  317. if (this._cachedViewport && !forceFullscreenViewport) {
  318. this.setViewport(this._cachedViewport, requiredWidth, requiredHeight);
  319. }
  320. }
  321. public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
  322. this._currentRenderTarget = null;
  323. if (onBeforeUnbind) {
  324. if (texture._MSAAFramebuffer) {
  325. this._currentFramebuffer = texture._framebuffer;
  326. }
  327. onBeforeUnbind();
  328. }
  329. this._currentFramebuffer = null;
  330. }
  331. public createDynamicVertexBuffer(vertices: FloatArray): WebGLBuffer {
  332. var vbo = {
  333. capacity: 1,
  334. references: 1,
  335. is32Bits: false
  336. }
  337. return vbo;
  338. }
  339. public updateDynamicTexture(texture: Nullable<InternalTexture>, canvas: HTMLCanvasElement, invertY: boolean, premulAlpha: boolean = false, format?: number): void {
  340. }
  341. /**
  342. * @hidden
  343. * Get the current error code of the webGL context
  344. * @returns the error code
  345. * @see https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/getError
  346. */
  347. public getError(): number {
  348. return 0;
  349. }
  350. /** @hidden */
  351. public _getUnpackAlignement(): number {
  352. return 1;
  353. }
  354. /** @hidden */
  355. public _unpackFlipY(value: boolean) {
  356. }
  357. public updateDynamicIndexBuffer(indexBuffer: WebGLBuffer, indices: IndicesArray, offset: number = 0): void {
  358. }
  359. /**
  360. * Updates a dynamic vertex buffer.
  361. * @param vertexBuffer the vertex buffer to update
  362. * @param data the data used to update the vertex buffer
  363. * @param byteOffset the byte offset of the data (optional)
  364. * @param byteLength the byte length of the data (optional)
  365. */
  366. public updateDynamicVertexBuffer(vertexBuffer: WebGLBuffer, vertices: FloatArray, byteOffset?: number, byteLength?: number): void {
  367. }
  368. protected _bindTextureDirectly(target: number, texture: InternalTexture): boolean {
  369. if (this._boundTexturesCache[this._activeChannel] !== texture) {
  370. this._boundTexturesCache[this._activeChannel] = texture;
  371. return true;
  372. }
  373. return false;
  374. }
  375. /** @hidden */
  376. public _bindTexture(channel: number, texture: InternalTexture): void {
  377. if (channel < 0) {
  378. return;
  379. }
  380. this._bindTextureDirectly(0, texture);
  381. }
  382. /** @hidden */
  383. public _releaseBuffer(buffer: WebGLBuffer): boolean {
  384. buffer.references--;
  385. if (buffer.references === 0) {
  386. return true;
  387. }
  388. return false;
  389. }
  390. public releaseEffects() {
  391. }
  392. public displayLoadingUI(): void {
  393. }
  394. public hideLoadingUI(): void {
  395. }
  396. /** @hidden */
  397. public _uploadCompressedDataToTextureDirectly(texture: InternalTexture, internalFormat: number, width: number, height: number, data: ArrayBufferView, faceIndex: number = 0, lod: number = 0) {
  398. }
  399. /** @hidden */
  400. public _uploadDataToTextureDirectly(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
  401. }
  402. /** @hidden */
  403. public _uploadArrayBufferViewToTexture(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
  404. }
  405. /** @hidden */
  406. public _uploadImageToTexture(texture: InternalTexture, image: HTMLImageElement, faceIndex: number = 0, lod: number = 0) {
  407. }
  408. }
  409. }