WebGLBackend.js 17 KB

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