WebGLBackend.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. import { WebGLCoordinateSystem } from 'three';
  2. import GLSLNodeBuilder from './nodes/GLSLNodeBuilder.js';
  3. import Backend from '../common/Backend.js';
  4. import WebGLAttributeUtils from './utils/WebGLAttributeUtils.js';
  5. import WebGLState from './utils/WebGLState.js';
  6. import WebGLUtils from './utils/WebGLUtils.js';
  7. import WebGLTextureUtils from './utils/WebGLTextureUtils.js';
  8. import WebGLExtensions from './utils/WebGLExtensions.js';
  9. import WebGLCapabilities from './utils/WebGLCapabilities.js';
  10. import { GLFeatureName } from './utils/WebGLConstants.js';
  11. //
  12. class WebGLBackend extends Backend {
  13. constructor( parameters = {} ) {
  14. super( parameters );
  15. this.isWebGLBackend = true;
  16. }
  17. init( renderer ) {
  18. super.init( renderer );
  19. //
  20. const parameters = this.parameters;
  21. const glContext = ( parameters.context !== undefined ) ? parameters.context : renderer.domElement.getContext( 'webgl2' );
  22. this.gl = glContext;
  23. this.extensions = new WebGLExtensions( this );
  24. this.capabilities = new WebGLCapabilities( this );
  25. this.attributeUtils = new WebGLAttributeUtils( this );
  26. this.textureUtils = new WebGLTextureUtils( this );
  27. this.state = new WebGLState( this );
  28. this.utils = new WebGLUtils( this );
  29. this.extensions.get( 'EXT_color_buffer_float' );
  30. this._currentContext = null;
  31. }
  32. get coordinateSystem() {
  33. return WebGLCoordinateSystem;
  34. }
  35. async getArrayBufferAsync( attribute ) {
  36. return await this.attributeUtils.getArrayBufferAsync( attribute );
  37. }
  38. getContext() {
  39. return this.gl;
  40. }
  41. beginRender( renderContext ) {
  42. const { gl } = this;
  43. const renderContextData = this.get( renderContext );
  44. //
  45. //
  46. renderContextData.previousContext = this._currentContext;
  47. this._currentContext = renderContext;
  48. this._setFramebuffer( renderContext );
  49. this.clear( renderContext.clearColor, renderContext.clearDepth, renderContext.clearStencil, renderContext );
  50. //
  51. if ( renderContext.viewport ) {
  52. this.updateViewport( renderContext );
  53. } else {
  54. gl.viewport( 0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight );
  55. }
  56. const occlusionQueryCount = renderContext.occlusionQueryCount;
  57. if ( occlusionQueryCount > 0 ) {
  58. // Get a reference to the array of objects with queries. The renderContextData property
  59. // can be changed by another render pass before the async reading of all previous queries complete
  60. renderContextData.currentOcclusionQueries = renderContextData.occlusionQueries;
  61. renderContextData.currentOcclusionQueryObjects = renderContextData.occlusionQueryObjects;
  62. renderContextData.lastOcclusionObject = null;
  63. renderContextData.occlusionQueries = new Array( occlusionQueryCount );
  64. renderContextData.occlusionQueryObjects = new Array( occlusionQueryCount );
  65. renderContextData.occlusionQueryIndex = 0;
  66. }
  67. }
  68. finishRender( renderContext ) {
  69. const { gl } = this;
  70. const renderContextData = this.get( renderContext );
  71. const previousContext = renderContextData.previousContext;
  72. this._currentContext = previousContext;
  73. if ( renderContext.textures !== null && renderContext.renderTarget ) {
  74. const renderTargetContextData = this.get( renderContext.renderTarget );
  75. const { samples, stencilBuffer } = renderContext.renderTarget;
  76. const fb = renderTargetContextData.framebuffer;
  77. if ( samples > 0 ) {
  78. const invalidationArray = [];
  79. const depthStyle = stencilBuffer ? gl.DEPTH_STENCIL_ATTACHMENT : gl.DEPTH_ATTACHMENT;
  80. invalidationArray.push( gl.COLOR_ATTACHMENT0 );
  81. if ( renderTargetContextData.depthBuffer ) {
  82. invalidationArray.push( depthStyle );
  83. }
  84. // TODO For loop support MRT
  85. const msaaFrameBuffer = renderTargetContextData.msaaFrameBuffer;
  86. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, msaaFrameBuffer );
  87. gl.bindFramebuffer( gl.DRAW_FRAMEBUFFER, fb );
  88. gl.blitFramebuffer( 0, 0, renderContext.width, renderContext.height, 0, 0, renderContext.width, renderContext.height, gl.COLOR_BUFFER_BIT, gl.NEAREST );
  89. gl.invalidateFramebuffer( gl.READ_FRAMEBUFFER, invalidationArray );
  90. }
  91. }
  92. if ( previousContext !== null ) {
  93. this._setFramebuffer( previousContext );
  94. if ( previousContext.viewport ) {
  95. this.updateViewport( previousContext );
  96. } else {
  97. const gl = this.gl;
  98. gl.viewport( 0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight );
  99. }
  100. }
  101. const occlusionQueryCount = renderContext.occlusionQueryCount;
  102. if ( occlusionQueryCount > 0 ) {
  103. const renderContextData = this.get( renderContext );
  104. if ( occlusionQueryCount > renderContextData.occlusionQueryIndex ) {
  105. const { gl } = this;
  106. gl.endQuery( gl.ANY_SAMPLES_PASSED );
  107. }
  108. this.resolveOccludedAsync( renderContext );
  109. }
  110. }
  111. resolveOccludedAsync( renderContext ) {
  112. const renderContextData = this.get( renderContext );
  113. // handle occlusion query results
  114. const { currentOcclusionQueries, currentOcclusionQueryObjects } = renderContextData;
  115. if ( currentOcclusionQueries && currentOcclusionQueryObjects ) {
  116. const occluded = new WeakSet();
  117. const { gl } = this;
  118. renderContextData.currentOcclusionQueryObjects = null;
  119. renderContextData.currentOcclusionQueries = null;
  120. const check = () => {
  121. let completed = 0;
  122. // check all queries and requeue as appropriate
  123. for ( let i = 0; i < currentOcclusionQueries.length; i ++ ) {
  124. const query = currentOcclusionQueries[ i ];
  125. if ( query === null ) continue;
  126. if ( gl.getQueryParameter( query, gl.QUERY_RESULT_AVAILABLE ) ) {
  127. if ( gl.getQueryParameter( query, gl.QUERY_RESULT ) > 0 ) occluded.add( currentOcclusionQueryObjects[ i ] );
  128. currentOcclusionQueries[ i ] = null;
  129. gl.deleteQuery( query );
  130. completed ++;
  131. }
  132. }
  133. if ( completed < currentOcclusionQueries.length ) {
  134. requestAnimationFrame( check );
  135. } else {
  136. renderContextData.occluded = occluded;
  137. }
  138. };
  139. check();
  140. }
  141. }
  142. isOccluded( renderContext, object ) {
  143. const renderContextData = this.get( renderContext );
  144. return renderContextData.occluded && renderContextData.occluded.has( object );
  145. }
  146. updateViewport( renderContext ) {
  147. const gl = this.gl;
  148. const { x, y, width, height } = renderContext.viewportValue;
  149. gl.viewport( x, y, width, height );
  150. }
  151. clear( color, depth, stencil, descriptor = null ) {
  152. const { gl } = this;
  153. if ( descriptor === null ) {
  154. descriptor = {
  155. textures: null,
  156. clearColorValue: this.getClearColor()
  157. };
  158. }
  159. //
  160. let clear = 0;
  161. if ( color ) clear |= gl.COLOR_BUFFER_BIT;
  162. if ( depth ) clear |= gl.DEPTH_BUFFER_BIT;
  163. if ( stencil ) clear |= gl.STENCIL_BUFFER_BIT;
  164. if ( clear !== 0 ) {
  165. const clearColor = descriptor.clearColorValue;
  166. if ( depth ) this.state.setDepthMask( true );
  167. if ( descriptor.textures === null ) {
  168. gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearColor.a );
  169. gl.clear( clear );
  170. } else {
  171. if ( color ) {
  172. for ( let i = 0; i < descriptor.textures.length; i ++ ) {
  173. gl.clearBufferfv( gl.COLOR, i, [ clearColor.r, clearColor.g, clearColor.b, clearColor.a ] );
  174. }
  175. }
  176. if ( depth && stencil ) {
  177. gl.clearBufferfi( gl.DEPTH_STENCIL, 0, 1, 0 );
  178. } else if ( depth ) {
  179. gl.clearBufferfv( gl.DEPTH, 0, [ 1.0 ] );
  180. } else if ( stencil ) {
  181. gl.clearBufferiv( gl.STENCIL, 0, [ 0 ] );
  182. }
  183. }
  184. }
  185. }
  186. beginCompute( /*computeGroup*/ ) {
  187. console.warn( 'Abstract class.' );
  188. }
  189. compute( /*computeGroup, computeNode, bindings, pipeline*/ ) {
  190. console.warn( 'Abstract class.' );
  191. }
  192. finishCompute( /*computeGroup*/ ) {
  193. console.warn( 'Abstract class.' );
  194. }
  195. draw( renderObject, info ) {
  196. const { pipeline, material, context } = renderObject;
  197. const { programGPU, vaoGPU } = this.get( pipeline );
  198. const { gl, state } = this;
  199. const contextData = this.get( context );
  200. //
  201. const bindings = renderObject.getBindings();
  202. for ( const binding of bindings ) {
  203. const bindingData = this.get( binding );
  204. const index = bindingData.index;
  205. if ( binding.isUniformsGroup || binding.isUniformBuffer ) {
  206. gl.bindBufferBase( gl.UNIFORM_BUFFER, index, bindingData.bufferGPU );
  207. } else if ( binding.isSampledTexture ) {
  208. state.bindTexture( bindingData.glTextureType, bindingData.textureGPU, gl.TEXTURE0 + index );
  209. }
  210. }
  211. state.setMaterial( material );
  212. gl.useProgram( programGPU );
  213. gl.bindVertexArray( vaoGPU );
  214. //
  215. const index = renderObject.getIndex();
  216. const object = renderObject.object;
  217. const geometry = renderObject.geometry;
  218. const drawRange = geometry.drawRange;
  219. const firstVertex = drawRange.start;
  220. //
  221. const lastObject = contextData.lastOcclusionObject;
  222. if ( lastObject !== object && lastObject !== undefined ) {
  223. if ( lastObject !== null && lastObject.occlusionTest === true ) {
  224. gl.endQuery( gl.ANY_SAMPLES_PASSED );
  225. contextData.occlusionQueryIndex ++;
  226. }
  227. if ( object.occlusionTest === true ) {
  228. const query = gl.createQuery();
  229. gl.beginQuery( gl.ANY_SAMPLES_PASSED, query );
  230. contextData.occlusionQueries[ contextData.occlusionQueryIndex ] = query;
  231. contextData.occlusionQueryObjects[ contextData.occlusionQueryIndex ] = object;
  232. }
  233. contextData.lastOcclusionObject = object;
  234. }
  235. //
  236. let mode;
  237. if ( object.isPoints ) mode = gl.POINTS;
  238. else if ( object.isLineSegments ) mode = gl.LINES;
  239. else if ( object.isLine ) mode = gl.LINE_STRIP;
  240. else if ( object.isLineLoop ) mode = gl.LINE_LOOP;
  241. else {
  242. if ( material.wireframe === true ) {
  243. state.setLineWidth( material.wireframeLinewidth * this.renderer.getPixelRatio() );
  244. mode = gl.LINES;
  245. } else {
  246. mode = gl.TRIANGLES;
  247. }
  248. }
  249. //
  250. const instanceCount = this.getInstanceCount( renderObject );
  251. if ( index !== null ) {
  252. const indexData = this.get( index );
  253. const indexCount = ( drawRange.count !== Infinity ) ? drawRange.count : index.count;
  254. if ( instanceCount > 1 ) {
  255. gl.drawElementsInstanced( mode, index.count, indexData.type, firstVertex, instanceCount );
  256. } else {
  257. gl.drawElements( mode, index.count, indexData.type, firstVertex );
  258. }
  259. info.update( object, indexCount, 1 );
  260. } else {
  261. const positionAttribute = geometry.attributes.position;
  262. const vertexCount = ( drawRange.count !== Infinity ) ? drawRange.count : positionAttribute.count;
  263. if ( instanceCount > 1 ) {
  264. gl.drawArraysInstanced( mode, 0, vertexCount, instanceCount );
  265. } else {
  266. gl.drawArrays( mode, 0, vertexCount );
  267. }
  268. //gl.drawArrays( mode, vertexCount, gl.UNSIGNED_SHORT, firstVertex );
  269. info.update( object, vertexCount, 1 );
  270. }
  271. //
  272. gl.bindVertexArray( null );
  273. }
  274. needsRenderUpdate( /*renderObject*/ ) {
  275. return false;
  276. }
  277. getRenderCacheKey( renderObject ) {
  278. return renderObject.id;
  279. }
  280. // textures
  281. createDefaultTexture( texture ) {
  282. this.textureUtils.createDefaultTexture( texture );
  283. }
  284. createTexture( texture, options ) {
  285. this.textureUtils.createTexture( texture, options );
  286. }
  287. updateTexture( texture, options ) {
  288. this.textureUtils.updateTexture( texture, options );
  289. }
  290. generateMipmaps( texture ) {
  291. this.textureUtils.generateMipmaps( texture );
  292. }
  293. destroyTexture( texture ) {
  294. this.textureUtils.destroyTexture( texture );
  295. }
  296. copyTextureToBuffer( texture, x, y, width, height ) {
  297. return this.textureUtils.copyTextureToBuffer( texture, x, y, width, height );
  298. }
  299. createSampler( /*texture*/ ) {
  300. //console.warn( 'Abstract class.' );
  301. }
  302. destroySampler() {}
  303. // node builder
  304. createNodeBuilder( object, renderer, scene = null ) {
  305. return new GLSLNodeBuilder( object, renderer, scene );
  306. }
  307. // program
  308. createProgram( program ) {
  309. const gl = this.gl;
  310. const { stage, code } = program;
  311. const shader = stage === 'vertex' ? gl.createShader( gl.VERTEX_SHADER ) : gl.createShader( gl.FRAGMENT_SHADER );
  312. gl.shaderSource( shader, code );
  313. gl.compileShader( shader );
  314. this.set( program, {
  315. shaderGPU: shader
  316. } );
  317. }
  318. destroyProgram( /*program*/ ) {
  319. console.warn( 'Abstract class.' );
  320. }
  321. createRenderPipeline( renderObject ) {
  322. const gl = this.gl;
  323. const pipeline = renderObject.pipeline;
  324. // Program
  325. const { fragmentProgram, vertexProgram } = pipeline;
  326. const programGPU = gl.createProgram();
  327. const fragmentShader = this.get( fragmentProgram ).shaderGPU;
  328. const vertexShader = this.get( vertexProgram ).shaderGPU;
  329. gl.attachShader( programGPU, fragmentShader );
  330. gl.attachShader( programGPU, vertexShader );
  331. gl.linkProgram( programGPU );
  332. if ( gl.getProgramParameter( programGPU, gl.LINK_STATUS ) === false ) {
  333. console.error( 'THREE.WebGLBackend:', gl.getProgramInfoLog( programGPU ) );
  334. console.error( 'THREE.WebGLBackend:', gl.getShaderInfoLog( fragmentShader ) );
  335. console.error( 'THREE.WebGLBackend:', gl.getShaderInfoLog( vertexShader ) );
  336. }
  337. gl.useProgram( programGPU );
  338. // Bindings
  339. const bindings = renderObject.getBindings();
  340. for ( const binding of bindings ) {
  341. const bindingData = this.get( binding );
  342. const index = bindingData.index;
  343. if ( binding.isUniformsGroup || binding.isUniformBuffer ) {
  344. const location = gl.getUniformBlockIndex( programGPU, binding.name );
  345. gl.uniformBlockBinding( programGPU, location, index );
  346. } else if ( binding.isSampledTexture ) {
  347. const location = gl.getUniformLocation( programGPU, binding.name );
  348. gl.uniform1i( location, index );
  349. }
  350. }
  351. // VAO
  352. const vaoGPU = gl.createVertexArray();
  353. const index = renderObject.getIndex();
  354. const attributes = renderObject.getAttributes();
  355. gl.bindVertexArray( vaoGPU );
  356. if ( index !== null ) {
  357. const indexData = this.get( index );
  358. gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, indexData.bufferGPU );
  359. }
  360. for ( let i = 0; i < attributes.length; i ++ ) {
  361. const attribute = attributes[ i ];
  362. const attributeData = this.get( attribute );
  363. gl.bindBuffer( gl.ARRAY_BUFFER, attributeData.bufferGPU );
  364. gl.enableVertexAttribArray( i );
  365. let stride, offset;
  366. if ( attribute.isInterleavedBufferAttribute === true ) {
  367. stride = attribute.data.stride * attributeData.bytesPerElement;
  368. offset = attribute.offset * attributeData.bytesPerElement;
  369. } else {
  370. stride = 0;
  371. offset = 0;
  372. }
  373. if ( attributeData.isInteger ) {
  374. gl.vertexAttribIPointer( i, attribute.itemSize, attributeData.type, stride, offset );
  375. } else {
  376. gl.vertexAttribPointer( i, attribute.itemSize, attributeData.type, attribute.normalized, stride, offset );
  377. }
  378. if ( attribute.isInstancedBufferAttribute && ! attribute.isInterleavedBufferAttribute ) {
  379. gl.vertexAttribDivisor( i, attribute.meshPerAttribute );
  380. } else if ( attribute.isInterleavedBufferAttribute && attribute.data.isInstancedInterleavedBuffer ) {
  381. gl.vertexAttribDivisor( i, attribute.data.meshPerAttribute );
  382. }
  383. }
  384. gl.bindVertexArray( null );
  385. //
  386. this.set( pipeline, {
  387. programGPU,
  388. vaoGPU
  389. } );
  390. }
  391. createComputePipeline( /*computePipeline, bindings*/ ) {
  392. console.warn( 'Abstract class.' );
  393. }
  394. createBindings( bindings ) {
  395. this.updateBindings( bindings );
  396. }
  397. updateBindings( bindings ) {
  398. const { gl } = this;
  399. let groupIndex = 0;
  400. let textureIndex = 0;
  401. for ( const binding of bindings ) {
  402. if ( binding.isUniformsGroup || binding.isUniformBuffer ) {
  403. const bufferGPU = gl.createBuffer();
  404. const data = binding.buffer;
  405. gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
  406. gl.bufferData( gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW );
  407. gl.bindBufferBase( gl.UNIFORM_BUFFER, groupIndex, bufferGPU );
  408. this.set( binding, {
  409. index: groupIndex ++,
  410. bufferGPU
  411. } );
  412. } else if ( binding.isSampledTexture ) {
  413. const { textureGPU, glTextureType } = this.get( binding.texture );
  414. this.set( binding, {
  415. index: textureIndex ++,
  416. textureGPU,
  417. glTextureType
  418. } );
  419. }
  420. }
  421. }
  422. updateBinding( binding ) {
  423. const gl = this.gl;
  424. if ( binding.isUniformsGroup || binding.isUniformBuffer ) {
  425. const bindingData = this.get( binding );
  426. const bufferGPU = bindingData.bufferGPU;
  427. const data = binding.buffer;
  428. gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
  429. gl.bufferData( gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW );
  430. }
  431. }
  432. // attributes
  433. createIndexAttribute( attribute ) {
  434. const gl = this.gl;
  435. this.attributeUtils.createAttribute( attribute, gl.ELEMENT_ARRAY_BUFFER );
  436. }
  437. createAttribute( attribute ) {
  438. const gl = this.gl;
  439. this.attributeUtils.createAttribute( attribute, gl.ARRAY_BUFFER );
  440. }
  441. createStorageAttribute( /*attribute*/ ) {
  442. console.warn( 'Abstract class.' );
  443. }
  444. updateAttribute( attribute ) {
  445. this.attributeUtils.updateAttribute( attribute );
  446. }
  447. destroyAttribute( /*attribute*/ ) {
  448. console.warn( 'Abstract class.' );
  449. }
  450. updateSize() {
  451. //console.warn( 'Abstract class.' );
  452. }
  453. hasFeature( name ) {
  454. const keysMatching = Object.keys( GLFeatureName ).filter( key => GLFeatureName[ key ] === name );
  455. const extensions = this.extensions;
  456. for ( let i = 0; i < keysMatching.length; i ++ ) {
  457. if ( extensions.has( keysMatching[ i ] ) ) return true;
  458. }
  459. return false;
  460. }
  461. getMaxAnisotropy() {
  462. return this.capabilities.getMaxAnisotropy();
  463. }
  464. copyFramebufferToTexture( texture, renderContext ) {
  465. this.textureUtils.copyFramebufferToTexture( texture, renderContext );
  466. }
  467. _setFramebuffer( renderContext ) {
  468. const { gl, state } = this;
  469. let fb = null;
  470. let currentFrameBuffer = null;
  471. if ( renderContext.textures !== null ) {
  472. const renderTargetContextData = this.get( renderContext.renderTarget );
  473. const { samples } = renderContext.renderTarget;
  474. fb = renderTargetContextData.framebuffer;
  475. let msaaFb = renderTargetContextData.msaaFrameBuffer;
  476. let depthRenderbuffer = renderTargetContextData.depthRenderbuffer;
  477. if ( fb === undefined ) {
  478. fb = gl.createFramebuffer();
  479. state.bindFramebuffer( gl.FRAMEBUFFER, fb );
  480. const textures = renderContext.textures;
  481. for ( let i = 0; i < textures.length; i ++ ) {
  482. const texture = textures[ i ];
  483. const textureData = this.get( texture );
  484. textureData.renderTarget = renderContext.renderTarget;
  485. const attachment = gl.COLOR_ATTACHMENT0 + i;
  486. gl.framebufferTexture2D( gl.FRAMEBUFFER, attachment, gl.TEXTURE_2D, textureData.textureGPU, 0 );
  487. }
  488. if ( renderContext.depthTexture !== null ) {
  489. const textureData = this.get( renderContext.depthTexture );
  490. gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, textureData.textureGPU, 0 );
  491. }
  492. renderTargetContextData.framebuffer = fb;
  493. state.drawBuffers( renderContext, fb );
  494. }
  495. if ( samples > 0 ) {
  496. if ( msaaFb === undefined ) {
  497. msaaFb = gl.createFramebuffer();
  498. state.bindFramebuffer( gl.FRAMEBUFFER, msaaFb );
  499. // TODO For loop support MRT
  500. const msaaRenderbuffer = gl.createRenderbuffer();
  501. gl.bindRenderbuffer( gl.RENDERBUFFER, msaaRenderbuffer );
  502. const texture = renderContext.textures[ 0 ];
  503. const textureData = this.get( texture );
  504. gl.renderbufferStorageMultisample( gl.RENDERBUFFER, samples, textureData.glInternalFormat, renderContext.width, renderContext.height );
  505. gl.framebufferRenderbuffer( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.RENDERBUFFER, msaaRenderbuffer );
  506. renderTargetContextData.msaaRenderbuffer = msaaRenderbuffer;
  507. renderTargetContextData.msaaFrameBuffer = msaaFb;
  508. if ( depthRenderbuffer === undefined ) {
  509. depthRenderbuffer = gl.createRenderbuffer();
  510. this.textureUtils.setupRenderBufferStorage( depthRenderbuffer, renderContext );
  511. renderTargetContextData.depthRenderbuffer = depthRenderbuffer;
  512. }
  513. }
  514. currentFrameBuffer = renderTargetContextData.msaaFrameBuffer;
  515. } else {
  516. currentFrameBuffer = fb;
  517. }
  518. }
  519. state.bindFramebuffer( gl.FRAMEBUFFER, currentFrameBuffer );
  520. }
  521. }
  522. export default WebGLBackend;