WebGLBackend.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. }
  58. finishRender( /*renderContext*/ ) {
  59. //console.warn( 'Abstract class.' );
  60. }
  61. updateViewport( renderContext ) {
  62. const gl = this.gl;
  63. const { x, y, width, height } = renderContext.viewportValue;
  64. gl.viewport( x, y, width, height );
  65. }
  66. clear( /*renderContext, color, depth, stencil*/ ) {
  67. console.warn( 'Abstract class.' );
  68. }
  69. beginCompute( /*computeGroup*/ ) {
  70. console.warn( 'Abstract class.' );
  71. }
  72. compute( /*computeGroup, computeNode, bindings, pipeline*/ ) {
  73. console.warn( 'Abstract class.' );
  74. }
  75. finishCompute( /*computeGroup*/ ) {
  76. console.warn( 'Abstract class.' );
  77. }
  78. draw( renderObject, info ) {
  79. const { pipeline, material } = renderObject;
  80. const { programGPU, vaoGPU } = this.get( pipeline );
  81. const { gl, state } = this;
  82. //
  83. const bindings = renderObject.getBindings();
  84. for ( const binding of bindings ) {
  85. const bindingData = this.get( binding );
  86. const index = bindingData.index;
  87. if ( binding.isUniformsGroup ) {
  88. gl.bindBufferBase( gl.UNIFORM_BUFFER, index, bindingData.bufferGPU );
  89. } else if ( binding.isSampledTexture ) {
  90. gl.activeTexture( gl.TEXTURE0 + index );
  91. gl.bindTexture( bindingData.glTextureType, bindingData.textureGPU );
  92. }
  93. }
  94. state.setMaterial( material );
  95. gl.useProgram( programGPU );
  96. gl.bindVertexArray( vaoGPU );
  97. //
  98. const index = renderObject.getIndex();
  99. const object = renderObject.object;
  100. const geometry = renderObject.geometry;
  101. const drawRange = geometry.drawRange;
  102. const firstVertex = drawRange.start;
  103. //
  104. let mode;
  105. if ( object.isPoints ) mode = gl.POINTS;
  106. else if ( object.isLine ) mode = gl.LINES;
  107. else if ( object.isLineLoop ) mode = gl.LINE_LOOP;
  108. else if ( object.isLineSegments ) mode = gl.LINES;
  109. else mode = gl.TRIANGLES;
  110. //
  111. if ( index !== null ) {
  112. const indexData = this.get( index );
  113. const indexCount = ( drawRange.count !== Infinity ) ? drawRange.count : index.count;
  114. gl.drawElements( mode, index.count, indexData.type, firstVertex );
  115. info.update( object, indexCount, 1 );
  116. } else {
  117. const positionAttribute = geometry.attributes.position;
  118. const vertexCount = ( drawRange.count !== Infinity ) ? drawRange.count : positionAttribute.count;
  119. gl.drawArrays( mode, 0, vertexCount );
  120. //gl.drawArrays( mode, vertexCount, gl.UNSIGNED_SHORT, firstVertex );
  121. info.update( object, vertexCount, 1 );
  122. }
  123. //
  124. gl.bindVertexArray( null );
  125. }
  126. needsUpdate( renderObject ) {
  127. return false;
  128. }
  129. getCacheKey( renderObject ) {
  130. return '';
  131. }
  132. // textures
  133. createSampler( /*texture*/ ) {
  134. //console.warn( 'Abstract class.' );
  135. }
  136. destroySampler( /*texture*/ ) {
  137. console.warn( 'Abstract class.' );
  138. }
  139. createDefaultTexture( texture ) {
  140. const { gl, textureUtils, defaultTextures } = this;
  141. const glTextureType = textureUtils.getGLTextureType( texture );
  142. let textureGPU = defaultTextures[ glTextureType ];
  143. if ( textureGPU === undefined ) {
  144. textureGPU = gl.createTexture();
  145. gl.bindTexture( glTextureType, textureGPU );
  146. gl.texParameteri( glTextureType, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
  147. gl.texParameteri( glTextureType, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
  148. //gl.texImage2D( target + i, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data );
  149. defaultTextures[ glTextureType ] = textureGPU;
  150. }
  151. this.set( texture, {
  152. textureGPU,
  153. glTextureType,
  154. isDefault: true
  155. } );
  156. }
  157. createTexture( texture, options ) {
  158. const { gl, utils, textureUtils } = this;
  159. const { levels, width, height } = options;
  160. const glFormat = utils.convert( texture.format, texture.colorSpace );
  161. const glType = utils.convert( texture.type );
  162. const glInternalFormat = textureUtils.getInternalFormat( texture.internalFormat, glFormat, glType, texture.colorSpace, texture.isVideoTexture );
  163. const textureGPU = gl.createTexture();
  164. const glTextureType = textureUtils.getGLTextureType( texture );
  165. gl.bindTexture( glTextureType, textureGPU );
  166. gl.pixelStorei( gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  167. gl.pixelStorei( gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  168. gl.pixelStorei( gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
  169. gl.pixelStorei( gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE );
  170. textureUtils.setTextureParameters( glTextureType, texture );
  171. gl.bindTexture( glTextureType, textureGPU );
  172. if ( ! texture.isVideoTexture ) {
  173. gl.texStorage2D( glTextureType, levels, glInternalFormat, width, height );
  174. }
  175. this.set( texture, {
  176. textureGPU,
  177. glTextureType,
  178. glFormat,
  179. glType,
  180. glInternalFormat
  181. } );
  182. }
  183. updateTexture( texture, options ) {
  184. const { gl } = this;
  185. const { width, height } = options;
  186. const { textureGPU, glTextureType, glFormat, glType, glInternalFormat } = this.get( texture );
  187. const getImage = ( source ) => {
  188. if ( source.isDataTexture ) {
  189. return source.image.data;
  190. } else if ( source instanceof ImageBitmap || source instanceof OffscreenCanvas ) {
  191. return source;
  192. }
  193. return source.data;
  194. };
  195. gl.bindTexture( glTextureType, textureGPU );
  196. if ( texture.isCubeTexture ) {
  197. const images = options.images;
  198. for ( let i = 0; i < 6; i ++ ) {
  199. const image = getImage( images[ i ] );
  200. gl.texSubImage2D( gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, 0, 0, width, height, glFormat, glType, image );
  201. }
  202. } else if ( texture.isVideoTexture ) {
  203. texture.update();
  204. gl.texImage2D( glTextureType, 0, glInternalFormat, glFormat, glType, options.image );
  205. } else {
  206. const image = getImage( options.image );
  207. gl.texSubImage2D( glTextureType, 0, 0, 0, width, height, glFormat, glType, image );
  208. }
  209. }
  210. generateMipmaps( texture ) {
  211. const { gl } = this;
  212. const { textureGPU, glTextureType } = this.get( texture );
  213. gl.bindTexture( glTextureType, textureGPU );
  214. gl.generateMipmap( glTextureType );
  215. }
  216. destroyTexture( /*texture*/ ) {
  217. console.warn( 'Abstract class.' );
  218. }
  219. copyTextureToBuffer( /*texture, x, y, width, height*/ ) {
  220. console.warn( 'Abstract class.' );
  221. }
  222. // node builder
  223. createNodeBuilder( object, renderer, scene = null ) {
  224. return new GLSLNodeBuilder( object, renderer, scene );
  225. }
  226. // program
  227. createProgram( program ) {
  228. const gl = this.gl;
  229. const { stage, code } = program;
  230. const shader = stage === 'vertex' ? gl.createShader( gl.VERTEX_SHADER ) : gl.createShader( gl.FRAGMENT_SHADER );
  231. gl.shaderSource( shader, code );
  232. gl.compileShader( shader );
  233. if ( gl.getShaderParameter( shader, gl.COMPILE_STATUS ) === false ) {
  234. console.error( 'THREE.WebGLBackend:', gl.getShaderInfoLog( shader ) );
  235. }
  236. this.set( program, {
  237. shaderGPU: shader
  238. } );
  239. }
  240. destroyProgram( /*program*/ ) {
  241. console.warn( 'Abstract class.' );
  242. }
  243. createRenderPipeline( renderObject ) {
  244. const gl = this.gl;
  245. const pipeline = renderObject.pipeline;
  246. // Program
  247. const { fragmentProgram, vertexProgram } = pipeline;
  248. const programGPU = gl.createProgram();
  249. gl.attachShader( programGPU, this.get( fragmentProgram ).shaderGPU );
  250. gl.attachShader( programGPU, this.get( vertexProgram ).shaderGPU );
  251. gl.linkProgram( programGPU );
  252. if ( gl.getProgramParameter( programGPU, gl.LINK_STATUS ) === false ) {
  253. console.error( 'THREE.WebGLBackend:', gl.getProgramInfoLog( programGPU ) );
  254. }
  255. gl.useProgram( programGPU );
  256. // Bindings
  257. const bindings = renderObject.getBindings();
  258. for ( const binding of bindings ) {
  259. const bindingData = this.get( binding );
  260. const index = bindingData.index;
  261. if ( binding.isUniformsGroup ) {
  262. const location = gl.getUniformBlockIndex( programGPU, binding.name );
  263. gl.uniformBlockBinding( programGPU, location, index );
  264. } else if ( binding.isSampledTexture ) {
  265. const location = gl.getUniformLocation( programGPU, binding.name );
  266. gl.uniform1i( location, index );
  267. }
  268. }
  269. // VAO
  270. const vaoGPU = gl.createVertexArray();
  271. const index = renderObject.getIndex();
  272. const vertexBuffers = renderObject.getVertexBuffers();
  273. gl.bindVertexArray( vaoGPU );
  274. if ( index !== null ) {
  275. const indexData = this.get( index );
  276. gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, indexData.bufferGPU );
  277. }
  278. for ( let i = 0; i < vertexBuffers.length; i ++ ) {
  279. const attribute = vertexBuffers[ i ];
  280. const attributeData = this.get( attribute );
  281. gl.bindBuffer( gl.ARRAY_BUFFER, attributeData.bufferGPU );
  282. gl.enableVertexAttribArray( i );
  283. gl.vertexAttribPointer( i, attribute.itemSize, attributeData.type, false, 0, 0 );
  284. }
  285. gl.bindVertexArray( null );
  286. //
  287. this.set( pipeline, {
  288. programGPU,
  289. vaoGPU
  290. } );
  291. }
  292. createComputePipeline( /*computePipeline, bindings*/ ) {
  293. console.warn( 'Abstract class.' );
  294. }
  295. createBindings( bindings ) {
  296. this.updateBindings( bindings );
  297. }
  298. updateBindings( bindings ) {
  299. const { gl } = this;
  300. let groupIndex = 0;
  301. let textureIndex = 0;
  302. for ( const binding of bindings ) {
  303. if ( binding.isUniformsGroup ) {
  304. const bufferGPU = gl.createBuffer();
  305. const data = binding.buffer;
  306. gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
  307. gl.bufferData( gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW );
  308. gl.bindBufferBase( gl.UNIFORM_BUFFER, groupIndex, bufferGPU );
  309. this.set( binding, {
  310. index: groupIndex ++,
  311. bufferGPU
  312. } );
  313. } else if ( binding.isSampledTexture ) {
  314. const { textureGPU, glTextureType } = this.get( binding.texture );
  315. this.set( binding, {
  316. index: textureIndex ++,
  317. textureGPU,
  318. glTextureType
  319. } );
  320. }
  321. }
  322. }
  323. updateBinding( binding ) {
  324. const gl = this.gl;
  325. if ( binding.isUniformsGroup ) {
  326. const bindingData = this.get( binding );
  327. const bufferGPU = bindingData.bufferGPU;
  328. const data = binding.buffer;
  329. gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
  330. gl.bufferData( gl.UNIFORM_BUFFER, data, gl.DYNAMIC_DRAW );
  331. }
  332. }
  333. // attributes
  334. createIndexAttribute( attribute ) {
  335. const gl = this.gl;
  336. this.attributeUtils.createAttribute( attribute, gl.ELEMENT_ARRAY_BUFFER );
  337. }
  338. createAttribute( attribute ) {
  339. const gl = this.gl;
  340. this.attributeUtils.createAttribute( attribute, gl.ARRAY_BUFFER );
  341. }
  342. createStorageAttribute( /*attribute*/ ) {
  343. console.warn( 'Abstract class.' );
  344. }
  345. updateAttribute( /*attribute*/ ) {
  346. console.warn( 'Abstract class.' );
  347. }
  348. destroyAttribute( /*attribute*/ ) {
  349. console.warn( 'Abstract class.' );
  350. }
  351. updateSize() {
  352. //console.warn( 'Abstract class.' );
  353. }
  354. hasFeature( name ) {
  355. return true;
  356. }
  357. copyFramebufferToTexture( /*texture, renderContext*/ ) {
  358. console.warn( 'Abstract class.' );
  359. }
  360. _setFramebuffer( renderContext ) {
  361. const { gl } = this;
  362. if ( renderContext.textures !== null ) {
  363. const renderContextData = this.get( renderContext );
  364. let fb = renderContextData.framebuffer;
  365. if ( fb === undefined ) {
  366. fb = gl.createFramebuffer();
  367. gl.bindFramebuffer( gl.FRAMEBUFFER, fb );
  368. const textures = renderContext.textures;
  369. const drawBuffers = [];
  370. for ( let i = 0; i < textures.length; i++ ) {
  371. const texture = textures[ i ];
  372. const { textureGPU } = this.get( texture );
  373. const attachment = gl.COLOR_ATTACHMENT0 + i;
  374. gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0 + i, gl.TEXTURE_2D, textureGPU, 0 );
  375. drawBuffers.push( attachment );
  376. }
  377. gl.drawBuffers( drawBuffers );
  378. if ( renderContext.depthTexture !== null ) {
  379. const { textureGPU } = this.get( renderContext.depthTexture );
  380. gl.framebufferTexture2D( gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, textureGPU, 0 );
  381. }
  382. renderContextData.framebuffer = fb;
  383. } else {
  384. gl.bindFramebuffer( gl.FRAMEBUFFER, fb );
  385. }
  386. } else {
  387. gl.bindFramebuffer( gl.FRAMEBUFFER, null );
  388. }
  389. }
  390. }
  391. export default WebGLBackend;