WebGLBackend.js 21 KB

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