WebGLBackend.js 21 KB

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