WebGLBackend.js 20 KB

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