WebGLBackend.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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, depth } = 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.isDataArrayTexture ) {
  280. gl.texStorage3D( gl.TEXTURE_2D_ARRAY, levels, glInternalFormat, width, height, depth );
  281. } else if ( ! texture.isVideoTexture ) {
  282. gl.texStorage2D( glTextureType, levels, glInternalFormat, width, height );
  283. }
  284. this.set( texture, {
  285. textureGPU,
  286. glTextureType,
  287. glFormat,
  288. glType,
  289. glInternalFormat
  290. } );
  291. }
  292. updateTexture( texture, options ) {
  293. const { gl } = this;
  294. const { width, height } = options;
  295. const { textureGPU, glTextureType, glFormat, glType, glInternalFormat } = this.get( texture );
  296. const getImage = ( source ) => {
  297. if ( source.isDataTexture ) {
  298. return source.image.data;
  299. } else if ( source instanceof ImageBitmap || source instanceof OffscreenCanvas || source instanceof HTMLImageElement || source instanceof HTMLCanvasElement ) {
  300. return source;
  301. }
  302. return source.data;
  303. };
  304. gl.bindTexture( glTextureType, textureGPU );
  305. if ( texture.isCubeTexture ) {
  306. const images = options.images;
  307. for ( let i = 0; i < 6; i ++ ) {
  308. const image = getImage( images[ i ] );
  309. gl.texSubImage2D( gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, width, height, glFormat, glType, image );
  310. }
  311. } else if ( texture.isDataArrayTexture ) {
  312. const image = options.image;
  313. gl.texSubImage3D( gl.TEXTURE_2D_ARRAY, 0, 0, 0, 0, image.width, image.height, image.depth, glFormat, glType, image.data );
  314. } else if ( texture.isVideoTexture ) {
  315. texture.update();
  316. gl.texImage2D( glTextureType, 0, glInternalFormat, glFormat, glType, options.image );
  317. } else {
  318. const image = getImage( options.image );
  319. gl.texSubImage2D( glTextureType, 0, 0, 0, width, height, glFormat, glType, image );
  320. }
  321. }
  322. generateMipmaps( texture ) {
  323. const { gl } = this;
  324. const { textureGPU, glTextureType } = this.get( texture );
  325. gl.bindTexture( glTextureType, textureGPU );
  326. gl.generateMipmap( glTextureType );
  327. }
  328. destroyTexture( /*texture*/ ) {
  329. console.warn( 'Abstract class.' );
  330. }
  331. copyTextureToBuffer( /*texture, x, y, width, height*/ ) {
  332. console.warn( 'Abstract class.' );
  333. }
  334. // node builder
  335. createNodeBuilder( object, renderer, scene = null ) {
  336. return new GLSLNodeBuilder( object, renderer, scene );
  337. }
  338. // program
  339. createProgram( program ) {
  340. const gl = this.gl;
  341. const { stage, code } = program;
  342. const shader = stage === 'vertex' ? gl.createShader( gl.VERTEX_SHADER ) : gl.createShader( gl.FRAGMENT_SHADER );
  343. gl.shaderSource( shader, code );
  344. gl.compileShader( shader );
  345. this.set( program, {
  346. shaderGPU: shader
  347. } );
  348. }
  349. destroyProgram( /*program*/ ) {
  350. console.warn( 'Abstract class.' );
  351. }
  352. createRenderPipeline( renderObject ) {
  353. const gl = this.gl;
  354. const pipeline = renderObject.pipeline;
  355. // Program
  356. const { fragmentProgram, vertexProgram } = pipeline;
  357. const programGPU = gl.createProgram();
  358. const fragmentShader = this.get( fragmentProgram ).shaderGPU;
  359. const vertexShader = this.get( vertexProgram ).shaderGPU;
  360. gl.attachShader( programGPU, fragmentShader );
  361. gl.attachShader( programGPU, vertexShader );
  362. gl.linkProgram( programGPU );
  363. if ( gl.getProgramParameter( programGPU, gl.LINK_STATUS ) === false ) {
  364. console.error( 'THREE.WebGLBackend:', gl.getProgramInfoLog( programGPU ) );
  365. console.error( 'THREE.WebGLBackend:', gl.getShaderInfoLog( fragmentShader ) );
  366. console.error( 'THREE.WebGLBackend:', gl.getShaderInfoLog( vertexShader ) );
  367. }
  368. gl.useProgram( programGPU );
  369. // Bindings
  370. const bindings = renderObject.getBindings();
  371. for ( const binding of bindings ) {
  372. const bindingData = this.get( binding );
  373. const index = bindingData.index;
  374. if ( binding.isUniformsGroup || binding.isUniformBuffer ) {
  375. const location = gl.getUniformBlockIndex( programGPU, binding.name );
  376. gl.uniformBlockBinding( programGPU, location, index );
  377. } else if ( binding.isSampledTexture ) {
  378. const location = gl.getUniformLocation( programGPU, binding.name );
  379. gl.uniform1i( location, index );
  380. }
  381. }
  382. // VAO
  383. const vaoGPU = gl.createVertexArray();
  384. const index = renderObject.getIndex();
  385. const attributes = renderObject.getAttributes();
  386. gl.bindVertexArray( vaoGPU );
  387. if ( index !== null ) {
  388. const indexData = this.get( index );
  389. gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, indexData.bufferGPU );
  390. }
  391. for ( let i = 0; i < attributes.length; i ++ ) {
  392. const attribute = attributes[ i ];
  393. const attributeData = this.get( attribute );
  394. gl.bindBuffer( gl.ARRAY_BUFFER, attributeData.bufferGPU );
  395. gl.enableVertexAttribArray( i );
  396. let stride, offset;
  397. if ( attribute.isInterleavedBufferAttribute === true ) {
  398. stride = attribute.data.stride * attributeData.bytesPerElement;
  399. offset = attribute.offset * attributeData.bytesPerElement;
  400. } else {
  401. stride = 0;
  402. offset = 0;
  403. }
  404. if ( attributeData.isInteger ) {
  405. gl.vertexAttribIPointer( i, attribute.itemSize, attributeData.type, stride, offset );
  406. } else {
  407. gl.vertexAttribPointer( i, attribute.itemSize, attributeData.type, attribute.normalized, stride, offset );
  408. }
  409. if ( attribute.isInstancedBufferAttribute && ! attribute.isInterleavedBufferAttribute ) {
  410. gl.vertexAttribDivisor( i, attribute.meshPerAttribute );
  411. } else if ( attribute.isInterleavedBufferAttribute && attribute.data.isInstancedInterleavedBuffer ) {
  412. gl.vertexAttribDivisor( i, attribute.data.meshPerAttribute );
  413. }
  414. }
  415. gl.bindVertexArray( null );
  416. //
  417. this.set( pipeline, {
  418. programGPU,
  419. vaoGPU
  420. } );
  421. }
  422. createComputePipeline( /*computePipeline, bindings*/ ) {
  423. console.warn( 'Abstract class.' );
  424. }
  425. createBindings( bindings ) {
  426. this.updateBindings( bindings );
  427. }
  428. updateBindings( bindings ) {
  429. const { gl } = this;
  430. let groupIndex = 0;
  431. let textureIndex = 0;
  432. for ( const binding of bindings ) {
  433. if ( binding.isUniformsGroup || binding.isUniformBuffer ) {
  434. const bufferGPU = gl.createBuffer();
  435. const data = binding.buffer;
  436. gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
  437. gl.bufferData( gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW );
  438. gl.bindBufferBase( gl.UNIFORM_BUFFER, groupIndex, bufferGPU );
  439. this.set( binding, {
  440. index: groupIndex ++,
  441. bufferGPU
  442. } );
  443. } else if ( binding.isSampledTexture ) {
  444. const { textureGPU, glTextureType } = this.get( binding.texture );
  445. this.set( binding, {
  446. index: textureIndex ++,
  447. textureGPU,
  448. glTextureType
  449. } );
  450. }
  451. }
  452. }
  453. updateBinding( binding ) {
  454. const gl = this.gl;
  455. if ( binding.isUniformsGroup || binding.isUniformBuffer ) {
  456. const bindingData = this.get( binding );
  457. const bufferGPU = bindingData.bufferGPU;
  458. const data = binding.buffer;
  459. gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
  460. gl.bufferData( gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW );
  461. }
  462. }
  463. // attributes
  464. createIndexAttribute( attribute ) {
  465. const gl = this.gl;
  466. this.attributeUtils.createAttribute( attribute, gl.ELEMENT_ARRAY_BUFFER );
  467. }
  468. createAttribute( attribute ) {
  469. const gl = this.gl;
  470. this.attributeUtils.createAttribute( attribute, gl.ARRAY_BUFFER );
  471. }
  472. createStorageAttribute( /*attribute*/ ) {
  473. console.warn( 'Abstract class.' );
  474. }
  475. updateAttribute( attribute ) {
  476. this.attributeUtils.updateAttribute( attribute );
  477. }
  478. destroyAttribute( /*attribute*/ ) {
  479. console.warn( 'Abstract class.' );
  480. }
  481. updateSize() {
  482. //console.warn( 'Abstract class.' );
  483. }
  484. hasFeature( name ) {
  485. return true;
  486. }
  487. copyFramebufferToTexture( texture, renderContext ) {
  488. const { gl } = this;
  489. const { textureGPU } = this.get( texture );
  490. const width = texture.image.width;
  491. const height = texture.image.height;
  492. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, null );
  493. if ( texture.isDepthTexture ) {
  494. const fb = gl.createFramebuffer();
  495. gl.bindFramebuffer( gl.DRAW_FRAMEBUFFER, fb );
  496. gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, textureGPU, 0 );
  497. gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, gl.DEPTH_BUFFER_BIT, gl.NEAREST );
  498. gl.deleteFramebuffer( fb );
  499. } else {
  500. gl.bindTexture( gl.TEXTURE_2D, textureGPU );
  501. gl.copyTexSubImage2D( gl.TEXTURE_2D, 0, 0, 0, 0, 0, width, height );
  502. gl.bindTexture( gl.TEXTURE_2D, null );
  503. }
  504. if ( texture.generateMipmaps ) this.generateMipmaps( texture );
  505. this._setFramebuffer( renderContext );
  506. }
  507. _setFramebuffer( renderContext ) {
  508. const { gl } = this;
  509. if ( renderContext.textures !== null ) {
  510. const renderContextData = this.get( renderContext );
  511. let fb = renderContextData.framebuffer;
  512. if ( fb === undefined ) {
  513. fb = gl.createFramebuffer();
  514. gl.bindFramebuffer( gl.FRAMEBUFFER, fb );
  515. const textures = renderContext.textures;
  516. const drawBuffers = [];
  517. for ( let i = 0; i < textures.length; i ++ ) {
  518. const texture = textures[ i ];
  519. const { textureGPU } = this.get( texture );
  520. const attachment = gl.COLOR_ATTACHMENT0 + i;
  521. gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, textureGPU, 0 );
  522. drawBuffers.push( attachment );
  523. }
  524. gl.drawBuffers( drawBuffers );
  525. if ( renderContext.depthTexture !== null ) {
  526. const { textureGPU } = this.get( renderContext.depthTexture );
  527. gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, textureGPU, 0 );
  528. }
  529. renderContextData.framebuffer = fb;
  530. } else {
  531. gl.bindFramebuffer( gl.FRAMEBUFFER, fb );
  532. }
  533. } else {
  534. gl.bindFramebuffer( gl.FRAMEBUFFER, null );
  535. }
  536. }
  537. }
  538. export default WebGLBackend;