WebGLBackend.js 20 KB

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