babylon.nullEngine.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. module BABYLON {
  2. export class NullEngineOptions {
  3. public renderWidth = 512;
  4. public renderHeight = 256;
  5. public textureSize = 512;
  6. public deterministicLockstep = false;
  7. public lockstepMaxSteps = 4;
  8. }
  9. /**
  10. * The null engine class provides support for headless version of babylon.js.
  11. * This can be used in server side scenario or for testing purposes
  12. */
  13. export class NullEngine extends Engine {
  14. private _options: NullEngineOptions;
  15. public isDeterministicLockStep(): boolean {
  16. return this._options.deterministicLockstep;
  17. }
  18. public getLockstepMaxSteps(): number {
  19. return this._options.lockstepMaxSteps;
  20. }
  21. public constructor(options: NullEngineOptions = new NullEngineOptions()) {
  22. super(null);
  23. if (options.deterministicLockstep === undefined) {
  24. options.deterministicLockstep = false;
  25. }
  26. if (options.lockstepMaxSteps === undefined) {
  27. options.lockstepMaxSteps = 4;
  28. }
  29. this._options = options;
  30. // Init caps
  31. // We consider we are on a webgl1 capable device
  32. this._caps = new EngineCapabilities();
  33. this._caps.maxTexturesImageUnits = 16;
  34. this._caps.maxVertexTextureImageUnits = 16;
  35. this._caps.maxTextureSize = 512;
  36. this._caps.maxCubemapTextureSize = 512;
  37. this._caps.maxRenderTextureSize = 512;
  38. this._caps.maxVertexAttribs = 16;
  39. this._caps.maxVaryingVectors = 16;
  40. this._caps.maxFragmentUniformVectors = 16;
  41. this._caps.maxVertexUniformVectors = 16;
  42. // Extensions
  43. this._caps.standardDerivatives = false;
  44. this._caps.astc = null;
  45. this._caps.s3tc = null;
  46. this._caps.pvrtc = null;
  47. this._caps.etc1 = null;
  48. this._caps.etc2 = null;
  49. this._caps.textureAnisotropicFilterExtension = null;
  50. this._caps.maxAnisotropy = 0;
  51. this._caps.uintIndices = false;
  52. this._caps.fragmentDepthSupported = false;
  53. this._caps.highPrecisionShaderSupported = true;
  54. this._caps.colorBufferFloat = false;
  55. this._caps.textureFloat = false;
  56. this._caps.textureFloatLinearFiltering = false;
  57. this._caps.textureFloatRender = false;
  58. this._caps.textureHalfFloat = false;
  59. this._caps.textureHalfFloatLinearFiltering = false;
  60. this._caps.textureHalfFloatRender = false;
  61. this._caps.textureLOD = false;
  62. this._caps.drawBuffersExtension = false;
  63. this._caps.depthTextureExtension = false;
  64. this._caps.vertexArrayObject = false;
  65. this._caps.instancedArrays = false;
  66. Tools.Log("Babylon.js null engine (v" + Engine.Version + ") launched");
  67. // Wrappers
  68. if (typeof URL === "undefined") {
  69. (<any>URL) = {
  70. createObjectURL: function () { },
  71. revokeObjectURL: function () { }
  72. }
  73. }
  74. if (typeof Blob === "undefined") {
  75. (<any>Blob) = function () { };
  76. }
  77. }
  78. public createVertexBuffer(vertices: FloatArray): WebGLBuffer {
  79. return {
  80. capacity: 0,
  81. references: 1,
  82. is32Bits: false
  83. };
  84. }
  85. public createIndexBuffer(indices: IndicesArray): WebGLBuffer {
  86. return {
  87. capacity: 0,
  88. references: 1,
  89. is32Bits: false
  90. };
  91. }
  92. public clear(color: Color4, backBuffer: boolean, depth: boolean, stencil: boolean = false): void {
  93. }
  94. public getRenderWidth(useScreen = false): number {
  95. if (!useScreen && this._currentRenderTarget) {
  96. return this._currentRenderTarget.width;
  97. }
  98. return this._options.renderWidth;
  99. }
  100. public getRenderHeight(useScreen = false): number {
  101. if (!useScreen && this._currentRenderTarget) {
  102. return this._currentRenderTarget.height;
  103. }
  104. return this._options.renderHeight;
  105. }
  106. public setViewport(viewport: Viewport, requiredWidth?: number, requiredHeight?: number): void {
  107. this._cachedViewport = viewport;
  108. }
  109. public createShaderProgram(vertexCode: string, fragmentCode: string, defines: string, context?: WebGLRenderingContext): WebGLProgram {
  110. return {
  111. transformFeedback: null,
  112. __SPECTOR_rebuildProgram: null
  113. };
  114. }
  115. public getUniforms(shaderProgram: WebGLProgram, uniformsNames: string[]): WebGLUniformLocation[] {
  116. return [];
  117. }
  118. public getAttributes(shaderProgram: WebGLProgram, attributesNames: string[]): number[] {
  119. return [];
  120. }
  121. public bindSamplers(effect: Effect): void {
  122. this._currentEffect = null;
  123. }
  124. public enableEffect(effect: Effect): void {
  125. this._currentEffect = effect;
  126. if (effect.onBind) {
  127. effect.onBind(effect);
  128. }
  129. effect.onBindObservable.notifyObservers(effect);
  130. }
  131. public setState(culling: boolean, zOffset: number = 0, force?: boolean, reverseSide = false): void {
  132. }
  133. public setIntArray(uniform: WebGLUniformLocation, array: Int32Array): void {
  134. }
  135. public setIntArray2(uniform: WebGLUniformLocation, array: Int32Array): void {
  136. }
  137. public setIntArray3(uniform: WebGLUniformLocation, array: Int32Array): void {
  138. }
  139. public setIntArray4(uniform: WebGLUniformLocation, array: Int32Array): void {
  140. }
  141. public setFloatArray(uniform: WebGLUniformLocation, array: Float32Array): void {
  142. }
  143. public setFloatArray2(uniform: WebGLUniformLocation, array: Float32Array): void {
  144. }
  145. public setFloatArray3(uniform: WebGLUniformLocation, array: Float32Array): void {
  146. }
  147. public setFloatArray4(uniform: WebGLUniformLocation, array: Float32Array): void {
  148. }
  149. public setArray(uniform: WebGLUniformLocation, array: number[]): void {
  150. }
  151. public setArray2(uniform: WebGLUniformLocation, array: number[]): void {
  152. }
  153. public setArray3(uniform: WebGLUniformLocation, array: number[]): void {
  154. }
  155. public setArray4(uniform: WebGLUniformLocation, array: number[]): void {
  156. }
  157. public setMatrices(uniform: WebGLUniformLocation, matrices: Float32Array): void {
  158. }
  159. public setMatrix(uniform: WebGLUniformLocation, matrix: Matrix): void {
  160. }
  161. public setMatrix3x3(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  162. }
  163. public setMatrix2x2(uniform: WebGLUniformLocation, matrix: Float32Array): void {
  164. }
  165. public setFloat(uniform: WebGLUniformLocation, value: number): void {
  166. }
  167. public setFloat2(uniform: WebGLUniformLocation, x: number, y: number): void {
  168. }
  169. public setFloat3(uniform: WebGLUniformLocation, x: number, y: number, z: number): void {
  170. }
  171. public setBool(uniform: WebGLUniformLocation, bool: number): void {
  172. }
  173. public setFloat4(uniform: WebGLUniformLocation, x: number, y: number, z: number, w: number): void {
  174. }
  175. public setColor3(uniform: WebGLUniformLocation, color3: Color3): void {
  176. }
  177. public setColor4(uniform: WebGLUniformLocation, color3: Color3, alpha: number): void {
  178. }
  179. public setAlphaMode(mode: number, noDepthWriteChange: boolean = false): void {
  180. if (this._alphaMode === mode) {
  181. return;
  182. }
  183. this._alphaState.alphaBlend = (mode !== Engine.ALPHA_DISABLE);
  184. if (!noDepthWriteChange) {
  185. this.setDepthWrite(mode === Engine.ALPHA_DISABLE);
  186. }
  187. this._alphaMode = mode;
  188. }
  189. public bindBuffers(vertexBuffers: { [key: string]: VertexBuffer; }, indexBuffer: WebGLBuffer, effect: Effect): void {
  190. }
  191. public wipeCaches(bruteForce?: boolean): void {
  192. if (this.preventCacheWipeBetweenFrames) {
  193. return;
  194. }
  195. this.resetTextureCache();
  196. this._currentEffect = null;
  197. if (bruteForce) {
  198. this._currentProgram = null;
  199. this._stencilState.reset();
  200. this._depthCullingState.reset();
  201. this._alphaState.reset();
  202. }
  203. this._cachedVertexBuffers = null;
  204. this._cachedIndexBuffer = null;
  205. this._cachedEffectForVertexBuffers = null;
  206. }
  207. public draw(useTriangles: boolean, indexStart: number, indexCount: number, instancesCount?: number): void {
  208. }
  209. public drawElementsType(fillMode: number, indexStart: number, indexCount: number, instancesCount?: number): void {
  210. }
  211. public drawArraysType(fillMode: number, verticesStart: number, verticesCount: number, instancesCount?: number): void {
  212. }
  213. public _createTexture(): WebGLTexture {
  214. return {};
  215. }
  216. 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 {
  217. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_URL);
  218. var url = String(urlArg);
  219. texture.url = url;
  220. texture.generateMipMaps = !noMipmap;
  221. texture.samplingMode = samplingMode;
  222. texture.invertY = invertY;
  223. texture.baseWidth = this._options.textureSize;
  224. texture.baseHeight = this._options.textureSize;
  225. texture.width = this._options.textureSize;
  226. texture.height = this._options.textureSize;
  227. if (format) {
  228. texture.format = format;
  229. }
  230. texture.isReady = true;
  231. if (onLoad) {
  232. onLoad();
  233. }
  234. return texture;
  235. }
  236. public createRenderTargetTexture(size: any, options: boolean | RenderTargetCreationOptions): InternalTexture {
  237. let fullOptions = new RenderTargetCreationOptions();
  238. if (options !== undefined && typeof options === "object") {
  239. fullOptions.generateMipMaps = options.generateMipMaps;
  240. fullOptions.generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  241. fullOptions.generateStencilBuffer = fullOptions.generateDepthBuffer && options.generateStencilBuffer;
  242. fullOptions.type = options.type === undefined ? Engine.TEXTURETYPE_UNSIGNED_INT : options.type;
  243. fullOptions.samplingMode = options.samplingMode === undefined ? Texture.TRILINEAR_SAMPLINGMODE : options.samplingMode;
  244. } else {
  245. fullOptions.generateMipMaps = <boolean>options;
  246. fullOptions.generateDepthBuffer = true;
  247. fullOptions.generateStencilBuffer = false;
  248. fullOptions.type = Engine.TEXTURETYPE_UNSIGNED_INT;
  249. fullOptions.samplingMode = Texture.TRILINEAR_SAMPLINGMODE;
  250. }
  251. var texture = new InternalTexture(this, InternalTexture.DATASOURCE_RENDERTARGET);
  252. var width = size.width || size;
  253. var height = size.height || size;
  254. texture._depthStencilBuffer = {};
  255. texture._framebuffer = {};
  256. texture.baseWidth = width;
  257. texture.baseHeight = height;
  258. texture.width = width;
  259. texture.height = height;
  260. texture.isReady = true;
  261. texture.samples = 1;
  262. texture.generateMipMaps = fullOptions.generateMipMaps ? true : false;
  263. texture.samplingMode = fullOptions.samplingMode;
  264. texture.type = fullOptions.type;
  265. texture._generateDepthBuffer = fullOptions.generateDepthBuffer;
  266. texture._generateStencilBuffer = fullOptions.generateStencilBuffer ? true : false;
  267. return texture;
  268. }
  269. public updateTextureSamplingMode(samplingMode: number, texture: InternalTexture): void {
  270. texture.samplingMode = samplingMode;
  271. }
  272. public bindFramebuffer(texture: InternalTexture, faceIndex?: number, requiredWidth?: number, requiredHeight?: number, forceFullscreenViewport?: boolean): void {
  273. if (this._currentRenderTarget) {
  274. this.unBindFramebuffer(this._currentRenderTarget);
  275. }
  276. this._currentRenderTarget = texture;
  277. this._currentFramebuffer = texture._MSAAFramebuffer ? texture._MSAAFramebuffer : texture._framebuffer;
  278. if (this._cachedViewport && !forceFullscreenViewport) {
  279. this.setViewport(this._cachedViewport, requiredWidth, requiredHeight);
  280. }
  281. }
  282. public unBindFramebuffer(texture: InternalTexture, disableGenerateMipMaps = false, onBeforeUnbind?: () => void): void {
  283. this._currentRenderTarget = null;
  284. if (onBeforeUnbind) {
  285. if (texture._MSAAFramebuffer) {
  286. this._currentFramebuffer = texture._framebuffer;
  287. }
  288. onBeforeUnbind();
  289. }
  290. this._currentFramebuffer = null;
  291. }
  292. public createDynamicVertexBuffer(vertices: FloatArray): WebGLBuffer {
  293. var vbo = {
  294. capacity: 1,
  295. references: 1,
  296. is32Bits: false
  297. }
  298. return vbo;
  299. }
  300. public updateDynamicIndexBuffer(indexBuffer: WebGLBuffer, indices: IndicesArray, offset: number = 0): void {
  301. }
  302. public updateDynamicVertexBuffer(vertexBuffer: WebGLBuffer, vertices: FloatArray, offset?: number, count?: number): void {
  303. }
  304. protected _bindTextureDirectly(target: number, texture: InternalTexture): void {
  305. if (this._boundTexturesCache[this._activeChannel] !== texture) {
  306. this._boundTexturesCache[this._activeChannel] = texture;
  307. }
  308. }
  309. public _bindTexture(channel: number, texture: InternalTexture): void {
  310. if (channel < 0) {
  311. return;
  312. }
  313. this._bindTextureDirectly(0, texture);
  314. }
  315. public _releaseBuffer(buffer: WebGLBuffer): boolean {
  316. buffer.references--;
  317. if (buffer.references === 0) {
  318. return true;
  319. }
  320. return false;
  321. }
  322. }
  323. }