WebGLBackend.js 20 KB

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