WebGLBackend.js 19 KB

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