WebGLBackend.js 21 KB

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