WebGLBackend.js 19 KB

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