engine.multiRender.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. import { InternalTexture, InternalTextureSource } from '../../Materials/Textures/internalTexture';
  2. import { IMultiRenderTargetOptions } from '../../Materials/Textures/multiRenderTarget';
  3. import { Logger } from '../../Misc/logger';
  4. import { Nullable } from '../../types';
  5. import { Constants } from '../constants';
  6. import { ThinEngine } from '../thinEngine';
  7. import { IColor4Like } from '../../Maths/math.like';
  8. declare module "../../Engines/thinEngine" {
  9. export interface ThinEngine {
  10. /**
  11. * Unbind a list of render target textures from the webGL context
  12. * This is used only when drawBuffer extension or webGL2 are active
  13. * @param count number of color textures
  14. * @param textures defines the render target textures to unbind
  15. * @param disableGenerateMipMaps defines a boolean indicating that mipmaps must not be generated
  16. * @param onBeforeUnbind defines a function which will be called before the effective unbind
  17. */
  18. unBindMultiColorAttachmentFramebuffer(textures: InternalTexture[], disableGenerateMipMaps: boolean, onBeforeUnbind?: () => void): void;
  19. /**
  20. * Create a multi render target texture
  21. * @see http://doc.babylonjs.com/features/webgl2#multiple-render-target
  22. * @param size defines the size of the texture
  23. * @param options defines the creation options
  24. * @returns the cube texture as an InternalTexture
  25. */
  26. createMultipleRenderTarget(size: any, options: IMultiRenderTargetOptions): InternalTexture[];
  27. /**
  28. * Update the sample count for a given multiple render target texture
  29. * @see http://doc.babylonjs.com/features/webgl2#multisample-render-targets
  30. * @param count number of color textures
  31. * @param textures defines the textures to update
  32. * @param samples defines the sample count to set
  33. * @returns the effective sample count (could be 0 if multisample render targets are not supported)
  34. */
  35. updateMultipleRenderTargetTextureSampleCount(textures: Nullable<InternalTexture[]>, samples: number): number;
  36. /**
  37. * Clears attachments
  38. * @param texture texture to clear
  39. * @param attachments attachments to clear
  40. * @param color Clear color
  41. */
  42. clearColorAttachments(texture: InternalTexture, attachments: number[], color?: IColor4Like) : void;
  43. /**
  44. * Select a subsets of attachments to draw to.
  45. * @param attachments gl attachments
  46. */
  47. renderToAttachments(attachments: any[]) : void;
  48. }
  49. }
  50. ThinEngine.prototype.renderToAttachments = function(attachments: any[]): void {
  51. const gl = this._gl;
  52. gl.drawBuffers(attachments);
  53. };
  54. ThinEngine.prototype.clearColorAttachments = function(texture: InternalTexture, attachments: number[], color?: IColor4Like): void {
  55. // Default clear everything to transparent black
  56. const gl = this._gl;
  57. // We don't clear the first attachments which is cleared with the user clear color
  58. gl.drawBuffers(attachments);
  59. if (color) {
  60. gl.clearColor(color.r, color.g, color.b, color.a);
  61. } else {
  62. gl.clearColor(0, 0, 0, 0);
  63. }
  64. gl.clear(gl.COLOR_BUFFER_BIT);
  65. // Restore default attachments
  66. gl.drawBuffers(texture._attachments!);
  67. };
  68. ThinEngine.prototype.unBindMultiColorAttachmentFramebuffer = function(textures: InternalTexture[], disableGenerateMipMaps: boolean = false, onBeforeUnbind?: () => void): void {
  69. this._currentRenderTarget = null;
  70. // If MSAA, we need to bitblt back to main texture
  71. var gl = this._gl;
  72. if (textures[0]._MSAAFramebuffer) {
  73. gl.bindFramebuffer(gl.READ_FRAMEBUFFER, textures[0]._MSAAFramebuffer);
  74. gl.bindFramebuffer(gl.DRAW_FRAMEBUFFER, textures[0]._framebuffer);
  75. var attachments = textures[0]._attachments!;
  76. var count = attachments.length;
  77. for (var i = 0; i < count; i++) {
  78. var texture = textures[i];
  79. for (var j = 0; j < attachments.length; j++) {
  80. attachments[j] = gl.NONE;
  81. }
  82. attachments[i] = (<any>gl)[this.webGLVersion > 1 ? "COLOR_ATTACHMENT" + i : "COLOR_ATTACHMENT" + i + "_WEBGL"];
  83. gl.readBuffer(attachments[i]);
  84. gl.drawBuffers(attachments);
  85. gl.blitFramebuffer(0, 0, texture.width, texture.height,
  86. 0, 0, texture.width, texture.height,
  87. gl.COLOR_BUFFER_BIT, gl.NEAREST);
  88. }
  89. for (var i = 0; i < attachments.length; i++) {
  90. attachments[i] = (<any>gl)[this.webGLVersion > 1 ? "COLOR_ATTACHMENT" + i : "COLOR_ATTACHMENT" + i + "_WEBGL"];
  91. }
  92. gl.drawBuffers(attachments);
  93. }
  94. for (var i = 0; i < textures[0]._attachments!.length; i++) {
  95. var texture = textures[i];
  96. if (texture.generateMipMaps && !disableGenerateMipMaps && !texture.isCube) {
  97. this._bindTextureDirectly(gl.TEXTURE_2D, texture, true);
  98. gl.generateMipmap(gl.TEXTURE_2D);
  99. this._bindTextureDirectly(gl.TEXTURE_2D, null);
  100. }
  101. }
  102. if (onBeforeUnbind) {
  103. if (textures[0]._MSAAFramebuffer) {
  104. // Bind the correct framebuffer
  105. this._bindUnboundFramebuffer(textures[0]._framebuffer);
  106. }
  107. onBeforeUnbind();
  108. }
  109. this._bindUnboundFramebuffer(null);
  110. };
  111. ThinEngine.prototype.createMultipleRenderTarget = function(size: any, options: IMultiRenderTargetOptions): InternalTexture[] {
  112. var generateMipMaps = false;
  113. var generateDepthBuffer = true;
  114. var generateStencilBuffer = false;
  115. var generateDepthTexture = false;
  116. var textureCount = 1;
  117. var defaultType = Constants.TEXTURETYPE_UNSIGNED_INT;
  118. var defaultSamplingMode = Constants.TEXTURE_TRILINEAR_SAMPLINGMODE;
  119. var types = new Array<number>();
  120. var samplingModes = new Array<number>();
  121. if (options !== undefined) {
  122. generateMipMaps = options.generateMipMaps === undefined ? false : options.generateMipMaps;
  123. generateDepthBuffer = options.generateDepthBuffer === undefined ? true : options.generateDepthBuffer;
  124. generateStencilBuffer = options.generateStencilBuffer === undefined ? false : options.generateStencilBuffer;
  125. generateDepthTexture = options.generateDepthTexture === undefined ? false : options.generateDepthTexture;
  126. textureCount = options.textureCount || 1;
  127. if (options.types) {
  128. types = options.types;
  129. }
  130. if (options.samplingModes) {
  131. samplingModes = options.samplingModes;
  132. }
  133. }
  134. var gl = this._gl;
  135. // Create the framebuffer
  136. var framebuffer = gl.createFramebuffer();
  137. this._bindUnboundFramebuffer(framebuffer);
  138. var width = size.width || size;
  139. var height = size.height || size;
  140. var textures = [];
  141. var attachments = [];
  142. var depthStencilBuffer = this._setupFramebufferDepthAttachments(generateStencilBuffer, generateDepthBuffer, width, height);
  143. for (var i = 0; i < textureCount; i++) {
  144. var samplingMode = samplingModes[i] || defaultSamplingMode;
  145. var type = types[i] || defaultType;
  146. if (type === Constants.TEXTURETYPE_FLOAT && !this._caps.textureFloatLinearFiltering) {
  147. // if floating point linear (gl.FLOAT) then force to NEAREST_SAMPLINGMODE
  148. samplingMode = Constants.TEXTURE_NEAREST_SAMPLINGMODE;
  149. }
  150. else if (type === Constants.TEXTURETYPE_HALF_FLOAT && !this._caps.textureHalfFloatLinearFiltering) {
  151. // if floating point linear (HALF_FLOAT) then force to NEAREST_SAMPLINGMODE
  152. samplingMode = Constants.TEXTURE_NEAREST_SAMPLINGMODE;
  153. }
  154. var filters = this._getSamplingParameters(samplingMode, generateMipMaps);
  155. if (type === Constants.TEXTURETYPE_FLOAT && !this._caps.textureFloat) {
  156. type = Constants.TEXTURETYPE_UNSIGNED_INT;
  157. Logger.Warn("Float textures are not supported. Render target forced to TEXTURETYPE_UNSIGNED_BYTE type");
  158. }
  159. var texture = new InternalTexture(this, InternalTextureSource.MultiRenderTarget);
  160. var attachment = (<any>gl)[this.webGLVersion > 1 ? "COLOR_ATTACHMENT" + i : "COLOR_ATTACHMENT" + i + "_WEBGL"];
  161. textures.push(texture);
  162. attachments.push(attachment);
  163. gl.activeTexture((<any>gl)["TEXTURE" + i]);
  164. gl.bindTexture(gl.TEXTURE_2D, texture._webGLTexture);
  165. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filters.mag);
  166. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filters.min);
  167. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  168. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  169. gl.texImage2D(gl.TEXTURE_2D, 0, this._getRGBABufferInternalSizedFormat(type), width, height, 0, gl.RGBA, this._getWebGLTextureType(type), null);
  170. gl.framebufferTexture2D(gl.DRAW_FRAMEBUFFER, attachment, gl.TEXTURE_2D, texture._webGLTexture, 0);
  171. if (generateMipMaps) {
  172. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  173. }
  174. // Unbind
  175. this._bindTextureDirectly(gl.TEXTURE_2D, null);
  176. texture._framebuffer = framebuffer;
  177. texture._depthStencilBuffer = depthStencilBuffer;
  178. texture.baseWidth = width;
  179. texture.baseHeight = height;
  180. texture.width = width;
  181. texture.height = height;
  182. texture.isReady = true;
  183. texture.samples = 1;
  184. texture.generateMipMaps = generateMipMaps;
  185. texture.samplingMode = samplingMode;
  186. texture.type = type;
  187. texture._generateDepthBuffer = generateDepthBuffer;
  188. texture._generateStencilBuffer = generateStencilBuffer;
  189. texture._attachments = attachments;
  190. texture._textureArray = textures;
  191. this._internalTexturesCache.push(texture);
  192. }
  193. if (generateDepthTexture && this._caps.depthTextureExtension) {
  194. // Depth texture
  195. var depthTexture = new InternalTexture(this, InternalTextureSource.MultiRenderTarget);
  196. gl.activeTexture(gl.TEXTURE0);
  197. gl.bindTexture(gl.TEXTURE_2D, depthTexture._webGLTexture);
  198. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  199. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  200. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  201. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  202. gl.texImage2D(
  203. gl.TEXTURE_2D,
  204. 0,
  205. this.webGLVersion < 2 ? gl.DEPTH_COMPONENT : gl.DEPTH_COMPONENT16,
  206. width,
  207. height,
  208. 0,
  209. gl.DEPTH_COMPONENT,
  210. gl.UNSIGNED_SHORT,
  211. null
  212. );
  213. gl.framebufferTexture2D(
  214. gl.FRAMEBUFFER,
  215. gl.DEPTH_ATTACHMENT,
  216. gl.TEXTURE_2D,
  217. depthTexture._webGLTexture,
  218. 0
  219. );
  220. depthTexture._framebuffer = framebuffer;
  221. depthTexture.baseWidth = width;
  222. depthTexture.baseHeight = height;
  223. depthTexture.width = width;
  224. depthTexture.height = height;
  225. depthTexture.isReady = true;
  226. depthTexture.samples = 1;
  227. depthTexture.generateMipMaps = generateMipMaps;
  228. depthTexture.samplingMode = gl.NEAREST;
  229. depthTexture._generateDepthBuffer = generateDepthBuffer;
  230. depthTexture._generateStencilBuffer = generateStencilBuffer;
  231. textures.push(depthTexture);
  232. this._internalTexturesCache.push(depthTexture);
  233. }
  234. gl.drawBuffers(attachments);
  235. this._bindUnboundFramebuffer(null);
  236. this.resetTextureCache();
  237. return textures;
  238. };
  239. ThinEngine.prototype.updateMultipleRenderTargetTextureSampleCount = function(textures: Nullable<InternalTexture[]>, samples: number): number {
  240. if (this.webGLVersion < 2 || !textures) {
  241. return 1;
  242. }
  243. if (textures[0].samples === samples) {
  244. return samples;
  245. }
  246. var count = textures[0]._attachments!.length;
  247. if (count === 0) {
  248. return 1;
  249. }
  250. var gl = this._gl;
  251. samples = Math.min(samples, this.getCaps().maxMSAASamples);
  252. // Dispose previous render buffers
  253. if (textures[0]._depthStencilBuffer) {
  254. gl.deleteRenderbuffer(textures[0]._depthStencilBuffer);
  255. textures[0]._depthStencilBuffer = null;
  256. }
  257. if (textures[0]._MSAAFramebuffer) {
  258. gl.deleteFramebuffer(textures[0]._MSAAFramebuffer);
  259. textures[0]._MSAAFramebuffer = null;
  260. }
  261. for (var i = 0; i < count; i++) {
  262. if (textures[i]._MSAARenderBuffer) {
  263. gl.deleteRenderbuffer(textures[i]._MSAARenderBuffer);
  264. textures[i]._MSAARenderBuffer = null;
  265. }
  266. }
  267. if (samples > 1 && gl.renderbufferStorageMultisample) {
  268. let framebuffer = gl.createFramebuffer();
  269. if (!framebuffer) {
  270. throw new Error("Unable to create multi sampled framebuffer");
  271. }
  272. this._bindUnboundFramebuffer(framebuffer);
  273. let depthStencilBuffer = this._setupFramebufferDepthAttachments(textures[0]._generateStencilBuffer, textures[0]._generateDepthBuffer, textures[0].width, textures[0].height, samples);
  274. var attachments = [];
  275. for (var i = 0; i < count; i++) {
  276. var texture = textures[i];
  277. var attachment = (<any>gl)[this.webGLVersion > 1 ? "COLOR_ATTACHMENT" + i : "COLOR_ATTACHMENT" + i + "_WEBGL"];
  278. var colorRenderbuffer = gl.createRenderbuffer();
  279. if (!colorRenderbuffer) {
  280. throw new Error("Unable to create multi sampled framebuffer");
  281. }
  282. gl.bindRenderbuffer(gl.RENDERBUFFER, colorRenderbuffer);
  283. gl.renderbufferStorageMultisample(gl.RENDERBUFFER, samples, this._getRGBAMultiSampleBufferFormat(texture.type), texture.width, texture.height);
  284. gl.framebufferRenderbuffer(gl.FRAMEBUFFER, attachment, gl.RENDERBUFFER, colorRenderbuffer);
  285. texture._MSAAFramebuffer = framebuffer;
  286. texture._MSAARenderBuffer = colorRenderbuffer;
  287. texture.samples = samples;
  288. texture._depthStencilBuffer = depthStencilBuffer;
  289. gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  290. attachments.push(attachment);
  291. }
  292. gl.drawBuffers(attachments);
  293. } else {
  294. this._bindUnboundFramebuffer(textures[0]._framebuffer);
  295. }
  296. this._bindUnboundFramebuffer(null);
  297. return samples;
  298. };