WebGLBackend.js 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. 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. const { gl } = this;
  338. const { textureGPU } = this.get( texture );
  339. gl.deleteTexture( textureGPU );
  340. this.delete( texture );
  341. }
  342. destroySampler() {}
  343. copyTextureToBuffer( texture, x, y, width, height ) {
  344. return this.textureUtils.copyTextureToBuffer( texture, x, y, width, height );
  345. }
  346. // node builder
  347. createNodeBuilder( object, renderer, scene = null ) {
  348. return new GLSLNodeBuilder( object, renderer, scene );
  349. }
  350. // program
  351. createProgram( program ) {
  352. const gl = this.gl;
  353. const { stage, code } = program;
  354. const shader = stage === 'vertex' ? gl.createShader( gl.VERTEX_SHADER ) : gl.createShader( gl.FRAGMENT_SHADER );
  355. gl.shaderSource( shader, code );
  356. gl.compileShader( shader );
  357. this.set( program, {
  358. shaderGPU: shader
  359. } );
  360. }
  361. destroyProgram( /*program*/ ) {
  362. console.warn( 'Abstract class.' );
  363. }
  364. createRenderPipeline( renderObject ) {
  365. const gl = this.gl;
  366. const pipeline = renderObject.pipeline;
  367. // Program
  368. const { fragmentProgram, vertexProgram } = pipeline;
  369. const programGPU = gl.createProgram();
  370. const fragmentShader = this.get( fragmentProgram ).shaderGPU;
  371. const vertexShader = this.get( vertexProgram ).shaderGPU;
  372. gl.attachShader( programGPU, fragmentShader );
  373. gl.attachShader( programGPU, vertexShader );
  374. gl.linkProgram( programGPU );
  375. if ( gl.getProgramParameter( programGPU, gl.LINK_STATUS ) === false ) {
  376. console.error( 'THREE.WebGLBackend:', gl.getProgramInfoLog( programGPU ) );
  377. console.error( 'THREE.WebGLBackend:', gl.getShaderInfoLog( fragmentShader ) );
  378. console.error( 'THREE.WebGLBackend:', gl.getShaderInfoLog( vertexShader ) );
  379. }
  380. gl.useProgram( programGPU );
  381. // Bindings
  382. const bindings = renderObject.getBindings();
  383. for ( const binding of bindings ) {
  384. const bindingData = this.get( binding );
  385. const index = bindingData.index;
  386. if ( binding.isUniformsGroup || binding.isUniformBuffer ) {
  387. const location = gl.getUniformBlockIndex( programGPU, binding.name );
  388. gl.uniformBlockBinding( programGPU, location, index );
  389. } else if ( binding.isSampledTexture ) {
  390. const location = gl.getUniformLocation( programGPU, binding.name );
  391. gl.uniform1i( location, index );
  392. }
  393. }
  394. // VAO
  395. const vaoGPU = gl.createVertexArray();
  396. const index = renderObject.getIndex();
  397. const attributes = renderObject.getAttributes();
  398. gl.bindVertexArray( vaoGPU );
  399. if ( index !== null ) {
  400. const indexData = this.get( index );
  401. gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, indexData.bufferGPU );
  402. }
  403. for ( let i = 0; i < attributes.length; i ++ ) {
  404. const attribute = attributes[ i ];
  405. const attributeData = this.get( attribute );
  406. gl.bindBuffer( gl.ARRAY_BUFFER, attributeData.bufferGPU );
  407. gl.enableVertexAttribArray( i );
  408. let stride, offset;
  409. if ( attribute.isInterleavedBufferAttribute === true ) {
  410. stride = attribute.data.stride * attributeData.bytesPerElement;
  411. offset = attribute.offset * attributeData.bytesPerElement;
  412. } else {
  413. stride = 0;
  414. offset = 0;
  415. }
  416. if ( attributeData.isInteger ) {
  417. gl.vertexAttribIPointer( i, attribute.itemSize, attributeData.type, stride, offset );
  418. } else {
  419. gl.vertexAttribPointer( i, attribute.itemSize, attributeData.type, attribute.normalized, stride, offset );
  420. }
  421. if ( attribute.isInstancedBufferAttribute && ! attribute.isInterleavedBufferAttribute ) {
  422. gl.vertexAttribDivisor( i, attribute.meshPerAttribute );
  423. } else if ( attribute.isInterleavedBufferAttribute && attribute.data.isInstancedInterleavedBuffer ) {
  424. gl.vertexAttribDivisor( i, attribute.data.meshPerAttribute );
  425. }
  426. }
  427. gl.bindVertexArray( null );
  428. //
  429. this.set( pipeline, {
  430. programGPU,
  431. vaoGPU
  432. } );
  433. }
  434. createComputePipeline( /*computePipeline, bindings*/ ) {
  435. console.warn( 'Abstract class.' );
  436. }
  437. createBindings( bindings ) {
  438. this.updateBindings( bindings );
  439. }
  440. updateBindings( bindings ) {
  441. const { gl } = this;
  442. let groupIndex = 0;
  443. let textureIndex = 0;
  444. for ( const binding of bindings ) {
  445. if ( binding.isUniformsGroup || binding.isUniformBuffer ) {
  446. const bufferGPU = gl.createBuffer();
  447. const data = binding.buffer;
  448. gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
  449. gl.bufferData( gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW );
  450. gl.bindBufferBase( gl.UNIFORM_BUFFER, groupIndex, bufferGPU );
  451. this.set( binding, {
  452. index: groupIndex ++,
  453. bufferGPU
  454. } );
  455. } else if ( binding.isSampledTexture ) {
  456. const { textureGPU, glTextureType } = this.get( binding.texture );
  457. this.set( binding, {
  458. index: textureIndex ++,
  459. textureGPU,
  460. glTextureType
  461. } );
  462. }
  463. }
  464. }
  465. updateBinding( binding ) {
  466. const gl = this.gl;
  467. if ( binding.isUniformsGroup || binding.isUniformBuffer ) {
  468. const bindingData = this.get( binding );
  469. const bufferGPU = bindingData.bufferGPU;
  470. const data = binding.buffer;
  471. gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
  472. gl.bufferData( gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW );
  473. }
  474. }
  475. // attributes
  476. createIndexAttribute( attribute ) {
  477. const gl = this.gl;
  478. this.attributeUtils.createAttribute( attribute, gl.ELEMENT_ARRAY_BUFFER );
  479. }
  480. createAttribute( attribute ) {
  481. const gl = this.gl;
  482. this.attributeUtils.createAttribute( attribute, gl.ARRAY_BUFFER );
  483. }
  484. createStorageAttribute( /*attribute*/ ) {
  485. console.warn( 'Abstract class.' );
  486. }
  487. updateAttribute( attribute ) {
  488. this.attributeUtils.updateAttribute( attribute );
  489. }
  490. destroyAttribute( /*attribute*/ ) {
  491. console.warn( 'Abstract class.' );
  492. }
  493. updateSize() {
  494. //console.warn( 'Abstract class.' );
  495. }
  496. hasFeature( /*name*/ ) {
  497. return true;
  498. }
  499. getMaxAnisotropy() {
  500. return this.capabilities.getMaxAnisotropy();
  501. }
  502. copyFramebufferToTexture( texture, renderContext ) {
  503. const { gl } = this;
  504. const { textureGPU } = this.get( texture );
  505. const width = texture.image.width;
  506. const height = texture.image.height;
  507. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, null );
  508. if ( texture.isDepthTexture ) {
  509. const fb = gl.createFramebuffer();
  510. gl.bindFramebuffer( gl.DRAW_FRAMEBUFFER, fb );
  511. gl.framebufferTexture2D( gl.DRAW_FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, textureGPU, 0 );
  512. gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, gl.DEPTH_BUFFER_BIT, gl.NEAREST );
  513. gl.deleteFramebuffer( fb );
  514. } else {
  515. gl.bindTexture( gl.TEXTURE_2D, textureGPU );
  516. gl.copyTexSubImage2D( gl.TEXTURE_2D, 0, 0, 0, 0, 0, width, height );
  517. gl.bindTexture( gl.TEXTURE_2D, null );
  518. }
  519. if ( texture.generateMipmaps ) this.generateMipmaps( texture );
  520. this._setFramebuffer( renderContext );
  521. }
  522. _setFramebuffer( renderContext ) {
  523. const { gl } = this;
  524. if ( renderContext.textures !== null ) {
  525. const renderContextData = this.get( renderContext.renderTarget );
  526. let fb = renderContextData.framebuffer;
  527. if ( fb === undefined ) {
  528. fb = gl.createFramebuffer();
  529. gl.bindFramebuffer( gl.FRAMEBUFFER, fb );
  530. const textures = renderContext.textures;
  531. const drawBuffers = [];
  532. for ( let i = 0; i < textures.length; i ++ ) {
  533. const texture = textures[ i ];
  534. const { textureGPU } = this.get( texture );
  535. const attachment = gl.COLOR_ATTACHMENT0 + i;
  536. gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, textureGPU, 0 );
  537. drawBuffers.push( attachment );
  538. }
  539. gl.drawBuffers( drawBuffers );
  540. if ( renderContext.depthTexture !== null ) {
  541. const { textureGPU } = this.get( renderContext.depthTexture );
  542. gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, textureGPU, 0 );
  543. }
  544. renderContextData.framebuffer = fb;
  545. } else {
  546. gl.bindFramebuffer( gl.FRAMEBUFFER, fb );
  547. }
  548. } else {
  549. gl.bindFramebuffer( gl.FRAMEBUFFER, null );
  550. }
  551. }
  552. }
  553. export default WebGLBackend;