WebGLBackend.js 12 KB

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