WebGLBackend.js 19 KB

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