WebGPURenderer.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. import { GPUIndexFormat, GPUTextureFormat, GPUStoreOp } from './constants.js';
  2. import WebGPUObjects from './WebGPUObjects.js';
  3. import WebGPUAttributes from './WebGPUAttributes.js';
  4. import WebGPUGeometries from './WebGPUGeometries.js';
  5. import WebGPUInfo from './WebGPUInfo.js';
  6. import WebGPUProperties from './WebGPUProperties.js';
  7. import WebGPURenderPipelines from './WebGPURenderPipelines.js';
  8. import WebGPUComputePipelines from './WebGPUComputePipelines.js';
  9. import WebGPUBindings from './WebGPUBindings.js';
  10. import WebGPURenderLists from './WebGPURenderLists.js';
  11. import WebGPUTextures from './WebGPUTextures.js';
  12. import WebGPUBackground from './WebGPUBackground.js';
  13. import WebGPUNodes from './nodes/WebGPUNodes.js';
  14. import { Frustum, Matrix4, Vector3, Color } from '../../../../build/three.module.js';
  15. console.info( 'THREE.WebGPURenderer: Modified Matrix4.makePerspective() and Matrix4.makeOrtographic() to work with WebGPU, see https://github.com/mrdoob/three.js/issues/20276.' );
  16. Matrix4.prototype.makePerspective = function ( left, right, top, bottom, near, far ) {
  17. const te = this.elements;
  18. const x = 2 * near / ( right - left );
  19. const y = 2 * near / ( top - bottom );
  20. const a = ( right + left ) / ( right - left );
  21. const b = ( top + bottom ) / ( top - bottom );
  22. const c = - far / ( far - near );
  23. const d = - far * near / ( far - near );
  24. te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0;
  25. te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0;
  26. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d;
  27. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0;
  28. return this;
  29. };
  30. Matrix4.prototype.makeOrthographic = function ( left, right, top, bottom, near, far ) {
  31. const te = this.elements;
  32. const w = 1.0 / ( right - left );
  33. const h = 1.0 / ( top - bottom );
  34. const p = 1.0 / ( far - near );
  35. const x = ( right + left ) * w;
  36. const y = ( top + bottom ) * h;
  37. const z = near * p;
  38. te[ 0 ] = 2 * w; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = - x;
  39. te[ 1 ] = 0; te[ 5 ] = 2 * h; te[ 9 ] = 0; te[ 13 ] = - y;
  40. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = - 1 * p; te[ 14 ] = - z;
  41. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1;
  42. return this;
  43. };
  44. const _frustum = new Frustum();
  45. const _projScreenMatrix = new Matrix4();
  46. const _vector3 = new Vector3();
  47. class WebGPURenderer {
  48. constructor( parameters = {} ) {
  49. // public
  50. this.domElement = ( parameters.canvas !== undefined ) ? parameters.canvas : this._createCanvasElement();
  51. this.autoClear = true;
  52. this.autoClearColor = true;
  53. this.autoClearDepth = true;
  54. this.autoClearStencil = true;
  55. this.sortObjects = true;
  56. // internals
  57. this._parameters = Object.assign( {}, parameters );
  58. this._pixelRatio = 1;
  59. this._width = this.domElement.width;
  60. this._height = this.domElement.height;
  61. this._viewport = null;
  62. this._scissor = null;
  63. this._adapter = null;
  64. this._device = null;
  65. this._context = null;
  66. this._swapChain = null;
  67. this._colorBuffer = null;
  68. this._depthBuffer = null;
  69. this._info = null;
  70. this._properties = null;
  71. this._attributes = null;
  72. this._geometries = null;
  73. this._nodes = null;
  74. this._bindings = null;
  75. this._objects = null;
  76. this._renderPipelines = null;
  77. this._computePipelines = null;
  78. this._renderLists = null;
  79. this._textures = null;
  80. this._background = null;
  81. this._renderPassDescriptor = null;
  82. this._currentRenderList = null;
  83. this._opaqueSort = null;
  84. this._transparentSort = null;
  85. this._clearAlpha = 1;
  86. this._clearColor = new Color( 0x000000 );
  87. this._clearDepth = 1;
  88. this._clearStencil = 0;
  89. this._renderTarget = null;
  90. // some parameters require default values other than "undefined"
  91. this._parameters.antialias = ( parameters.antialias === true );
  92. if ( this._parameters.antialias === true ) {
  93. this._parameters.sampleCount = ( parameters.sampleCount === undefined ) ? 4 : parameters.sampleCount;
  94. } else {
  95. this._parameters.sampleCount = 1;
  96. }
  97. this._parameters.extensions = ( parameters.extensions === undefined ) ? [] : parameters.extensions;
  98. this._parameters.limits = ( parameters.limits === undefined ) ? {} : parameters.limits;
  99. }
  100. async init() {
  101. const parameters = this._parameters;
  102. const adapterOptions = {
  103. powerPreference: parameters.powerPreference
  104. };
  105. const adapter = await navigator.gpu.requestAdapter( adapterOptions );
  106. const deviceDescriptor = {
  107. extensions: parameters.extensions,
  108. limits: parameters.limits
  109. };
  110. const device = await adapter.requestDevice( deviceDescriptor );
  111. // https://cdn.jsdelivr.net/npm/@webgpu/[email protected]/dist/web-devel/glslang.js
  112. const glslang = await import( '../../libs/glslang.js' );
  113. const compiler = await glslang.default();
  114. const context = ( parameters.context !== undefined ) ? parameters.context : this.domElement.getContext( 'gpupresent' );
  115. const swapChain = context.configureSwapChain( {
  116. device: device,
  117. format: GPUTextureFormat.BRGA8Unorm // this is the only valid swap chain format right now (r121)
  118. } );
  119. this._adapter = adapter;
  120. this._device = device;
  121. this._context = context;
  122. this._swapChain = swapChain;
  123. this._info = new WebGPUInfo();
  124. this._properties = new WebGPUProperties();
  125. this._attributes = new WebGPUAttributes( device );
  126. this._geometries = new WebGPUGeometries( this._attributes, this._info );
  127. this._textures = new WebGPUTextures( device, this._properties, this._info, compiler );
  128. this._objects = new WebGPUObjects( this._geometries, this._info );
  129. this._nodes = new WebGPUNodes( this );
  130. this._renderPipelines = new WebGPURenderPipelines( this, this._properties, device, compiler, parameters.sampleCount, this._nodes );
  131. this._computePipelines = new WebGPUComputePipelines( device, compiler );
  132. this._bindings = new WebGPUBindings( device, this._info, this._properties, this._textures, this._renderPipelines, this._computePipelines, this._attributes, this._nodes );
  133. this._renderLists = new WebGPURenderLists();
  134. this._background = new WebGPUBackground( this );
  135. //
  136. this._renderPassDescriptor = {
  137. colorAttachments: [ {
  138. attachment: null
  139. } ],
  140. depthStencilAttachment: {
  141. attachment: null,
  142. depthStoreOp: GPUStoreOp.Store,
  143. stencilStoreOp: GPUStoreOp.Store
  144. }
  145. };
  146. this._setupColorBuffer();
  147. this._setupDepthBuffer();
  148. }
  149. render( scene, camera ) {
  150. // @TODO: move this to animation loop
  151. this._nodes.updateFrame();
  152. //
  153. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  154. if ( camera.parent === null ) camera.updateMatrixWorld();
  155. if ( this._info.autoReset === true ) this._info.reset();
  156. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  157. _frustum.setFromProjectionMatrix( _projScreenMatrix );
  158. this._currentRenderList = this._renderLists.get( scene, camera );
  159. this._currentRenderList.init();
  160. this._projectObject( scene, camera, 0 );
  161. this._currentRenderList.finish();
  162. if ( this.sortObjects === true ) {
  163. this._currentRenderList.sort( this._opaqueSort, this._transparentSort );
  164. }
  165. // prepare render pass descriptor
  166. const colorAttachment = this._renderPassDescriptor.colorAttachments[ 0 ];
  167. const depthStencilAttachment = this._renderPassDescriptor.depthStencilAttachment;
  168. const renderTarget = this._renderTarget;
  169. if ( renderTarget !== null ) {
  170. // @TODO: Support RenderTarget with antialiasing.
  171. const renderTargetProperties = this._properties.get( renderTarget );
  172. colorAttachment.attachment = renderTargetProperties.colorTextureGPU.createView();
  173. depthStencilAttachment.attachment = renderTargetProperties.depthTextureGPU.createView();
  174. } else {
  175. if ( this._parameters.antialias === true ) {
  176. colorAttachment.attachment = this._colorBuffer.createView();
  177. colorAttachment.resolveTarget = this._swapChain.getCurrentTexture().createView();
  178. } else {
  179. colorAttachment.attachment = this._swapChain.getCurrentTexture().createView();
  180. colorAttachment.resolveTarget = undefined;
  181. }
  182. depthStencilAttachment.attachment = this._depthBuffer.createView();
  183. }
  184. //
  185. this._background.update( scene );
  186. // start render pass
  187. const device = this._device;
  188. const cmdEncoder = device.createCommandEncoder( {} );
  189. const passEncoder = cmdEncoder.beginRenderPass( this._renderPassDescriptor );
  190. // global rasterization settings for all renderable objects
  191. const vp = this._viewport;
  192. if ( vp !== null ) {
  193. const width = Math.floor( vp.width * this._pixelRatio );
  194. const height = Math.floor( vp.height * this._pixelRatio );
  195. passEncoder.setViewport( vp.x, vp.y, width, height, vp.minDepth, vp.maxDepth );
  196. }
  197. const sc = this._scissor;
  198. if ( sc !== null ) {
  199. const width = Math.floor( sc.width * this._pixelRatio );
  200. const height = Math.floor( sc.height * this._pixelRatio );
  201. passEncoder.setScissorRect( sc.x, sc.y, width, height );
  202. }
  203. // process render lists
  204. const opaqueObjects = this._currentRenderList.opaque;
  205. const transparentObjects = this._currentRenderList.transparent;
  206. if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, passEncoder );
  207. if ( transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, passEncoder );
  208. // finish render pass
  209. passEncoder.endPass();
  210. device.defaultQueue.submit( [ cmdEncoder.finish() ] );
  211. }
  212. getContext() {
  213. return this._context;
  214. }
  215. getPixelRatio() {
  216. return this._pixelRatio;
  217. }
  218. getDrawingBufferSize( target ) {
  219. return target.set( this._width * this._pixelRatio, this._height * this._pixelRatio ).floor();
  220. }
  221. getSize( target ) {
  222. return target.set( this._width, this._height );
  223. }
  224. setPixelRatio( value = 1 ) {
  225. this._pixelRatio = value;
  226. this.setSize( this._width, this._height, false );
  227. }
  228. setDrawingBufferSize( width, height, pixelRatio ) {
  229. this._width = width;
  230. this._height = height;
  231. this._pixelRatio = pixelRatio;
  232. this.domElement.width = Math.floor( width * pixelRatio );
  233. this.domElement.height = Math.floor( height * pixelRatio );
  234. this._setupColorBuffer();
  235. this._setupDepthBuffer();
  236. }
  237. setSize( width, height, updateStyle = true ) {
  238. this._width = width;
  239. this._height = height;
  240. this.domElement.width = Math.floor( width * this._pixelRatio );
  241. this.domElement.height = Math.floor( height * this._pixelRatio );
  242. if ( updateStyle === true ) {
  243. this.domElement.style.width = width + 'px';
  244. this.domElement.style.height = height + 'px';
  245. }
  246. this._setupColorBuffer();
  247. this._setupDepthBuffer();
  248. }
  249. setOpaqueSort( method ) {
  250. this._opaqueSort = method;
  251. }
  252. setTransparentSort( method ) {
  253. this._transparentSort = method;
  254. }
  255. getScissor( target ) {
  256. const scissor = this._scissor;
  257. target.x = scissor.x;
  258. target.y = scissor.y;
  259. target.width = scissor.width;
  260. target.height = scissor.height;
  261. return target;
  262. }
  263. setScissor( x, y, width, height ) {
  264. if ( x === null ) {
  265. this._scissor = null;
  266. } else {
  267. this._scissor = {
  268. x: x,
  269. y: y,
  270. width: width,
  271. height: height
  272. };
  273. }
  274. }
  275. getViewport( target ) {
  276. const viewport = this._viewport;
  277. target.x = viewport.x;
  278. target.y = viewport.y;
  279. target.width = viewport.width;
  280. target.height = viewport.height;
  281. target.minDepth = viewport.minDepth;
  282. target.maxDepth = viewport.maxDepth;
  283. return target;
  284. }
  285. setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) {
  286. if ( x === null ) {
  287. this._viewport = null;
  288. } else {
  289. this._viewport = {
  290. x: x,
  291. y: y,
  292. width: width,
  293. height: height,
  294. minDepth: minDepth,
  295. maxDepth: maxDepth
  296. };
  297. }
  298. }
  299. getClearColor( target ) {
  300. return target.copy( this._clearColor );
  301. }
  302. setClearColor( color, alpha = 1 ) {
  303. this._clearColor.set( color );
  304. this._clearAlpha = alpha;
  305. }
  306. getClearAlpha() {
  307. return this._clearAlpha;
  308. }
  309. setClearAlpha( alpha ) {
  310. this._clearAlpha = alpha;
  311. }
  312. getClearDepth() {
  313. return this._clearDepth;
  314. }
  315. setClearDepth( depth ) {
  316. this._clearDepth = depth;
  317. }
  318. getClearStencil() {
  319. return this._clearStencil;
  320. }
  321. setClearStencil( stencil ) {
  322. this._clearStencil = stencil;
  323. }
  324. clear() {
  325. this._background.clear();
  326. }
  327. dispose() {
  328. this._objects.dispose();
  329. this._properties.dispose();
  330. this._renderPipelines.dispose();
  331. this._computePipelines.dispose();
  332. this._nodes.dispose();
  333. this._bindings.dispose();
  334. this._info.dispose();
  335. this._renderLists.dispose();
  336. this._textures.dispose();
  337. }
  338. setRenderTarget( renderTarget ) {
  339. this._renderTarget = renderTarget;
  340. if ( renderTarget !== null ) {
  341. this._textures.initRenderTarget( renderTarget );
  342. }
  343. }
  344. compute( computeParams ) {
  345. const device = this._device;
  346. const cmdEncoder = device.createCommandEncoder( {} );
  347. const passEncoder = cmdEncoder.beginComputePass();
  348. for ( const param of computeParams ) {
  349. // pipeline
  350. const pipeline = this._computePipelines.get( param );
  351. passEncoder.setPipeline( pipeline );
  352. // bind group
  353. const bindGroup = this._bindings.getForCompute( param ).group;
  354. this._bindings.update( param );
  355. passEncoder.setBindGroup( 0, bindGroup );
  356. passEncoder.dispatch( param.num );
  357. }
  358. passEncoder.endPass();
  359. device.defaultQueue.submit( [ cmdEncoder.finish() ] );
  360. }
  361. getRenderTarget() {
  362. return this._renderTarget;
  363. }
  364. _projectObject( object, camera, groupOrder ) {
  365. const info = this._info;
  366. const currentRenderList = this._currentRenderList;
  367. if ( object.visible === false ) return;
  368. const visible = object.layers.test( camera.layers );
  369. if ( visible ) {
  370. if ( object.isGroup ) {
  371. groupOrder = object.renderOrder;
  372. } else if ( object.isLOD ) {
  373. if ( object.autoUpdate === true ) object.update( camera );
  374. } else if ( object.isLight ) {
  375. //currentRenderState.pushLight( object );
  376. if ( object.castShadow ) {
  377. //currentRenderState.pushShadow( object );
  378. }
  379. } else if ( object.isSprite ) {
  380. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  381. if ( this.sortObjects === true ) {
  382. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  383. }
  384. const geometry = object.geometry;
  385. const material = object.material;
  386. if ( material.visible ) {
  387. currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  388. }
  389. }
  390. } else if ( object.isLineLoop ) {
  391. console.error( 'THREE.WebGPURenderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  392. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  393. if ( object.isSkinnedMesh ) {
  394. // update skeleton only once in a frame
  395. if ( object.skeleton.frame !== info.render.frame ) {
  396. object.skeleton.update();
  397. object.skeleton.frame = info.render.frame;
  398. }
  399. }
  400. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  401. if ( this.sortObjects === true ) {
  402. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  403. }
  404. const geometry = object.geometry;
  405. const material = object.material;
  406. if ( Array.isArray( material ) ) {
  407. const groups = geometry.groups;
  408. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  409. const group = groups[ i ];
  410. const groupMaterial = material[ group.materialIndex ];
  411. if ( groupMaterial && groupMaterial.visible ) {
  412. currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  413. }
  414. }
  415. } else if ( material.visible ) {
  416. currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  417. }
  418. }
  419. }
  420. }
  421. const children = object.children;
  422. for ( let i = 0, l = children.length; i < l; i ++ ) {
  423. this._projectObject( children[ i ], camera, groupOrder );
  424. }
  425. }
  426. _renderObjects( renderList, camera, passEncoder ) {
  427. // process renderable objects
  428. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  429. const renderItem = renderList[ i ];
  430. // @TODO: Add support for multiple materials per object. This will require to extract
  431. // the material from the renderItem object and pass it with its group data to _renderObject().
  432. const object = renderItem.object;
  433. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  434. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  435. this._objects.update( object );
  436. if ( camera.isArrayCamera ) {
  437. const cameras = camera.cameras;
  438. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  439. const camera2 = cameras[ j ];
  440. if ( object.layers.test( camera2.layers ) ) {
  441. const vp = camera2.viewport;
  442. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  443. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  444. passEncoder.setViewport( vp.x, vp.y, vp.width, vp.height, minDepth, maxDepth );
  445. this._nodes.update( object, camera2 );
  446. this._bindings.update( object, camera2 );
  447. this._renderObject( object, passEncoder );
  448. }
  449. }
  450. } else {
  451. this._nodes.update( object, camera );
  452. this._bindings.update( object, camera );
  453. this._renderObject( object, passEncoder );
  454. }
  455. }
  456. }
  457. _renderObject( object, passEncoder ) {
  458. const info = this._info;
  459. // pipeline
  460. const pipeline = this._renderPipelines.get( object );
  461. passEncoder.setPipeline( pipeline );
  462. // bind group
  463. const bindGroup = this._bindings.get( object ).group;
  464. passEncoder.setBindGroup( 0, bindGroup );
  465. // index
  466. const geometry = object.geometry;
  467. const index = geometry.index;
  468. const hasIndex = ( index !== null );
  469. if ( hasIndex === true ) {
  470. this._setupIndexBuffer( index, passEncoder );
  471. }
  472. // vertex buffers
  473. this._setupVertexBuffers( geometry.attributes, passEncoder, pipeline );
  474. // draw
  475. const drawRange = geometry.drawRange;
  476. const firstVertex = drawRange.start;
  477. const instanceCount = ( geometry.isInstancedBufferGeometry ) ? geometry.instanceCount : 1;
  478. if ( hasIndex === true ) {
  479. const indexCount = ( drawRange.count !== Infinity ) ? drawRange.count : index.count;
  480. passEncoder.drawIndexed( indexCount, instanceCount, firstVertex, 0, 0 );
  481. info.update( object, indexCount, instanceCount );
  482. } else {
  483. const positionAttribute = geometry.attributes.position;
  484. const vertexCount = ( drawRange.count !== Infinity ) ? drawRange.count : positionAttribute.count;
  485. passEncoder.draw( vertexCount, instanceCount, firstVertex, 0 );
  486. info.update( object, vertexCount, instanceCount );
  487. }
  488. }
  489. _setupIndexBuffer( index, encoder ) {
  490. const buffer = this._attributes.get( index ).buffer;
  491. const indexFormat = ( index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
  492. encoder.setIndexBuffer( buffer, indexFormat );
  493. }
  494. _setupVertexBuffers( geometryAttributes, encoder, pipeline ) {
  495. const shaderAttributes = this._renderPipelines.getShaderAttributes( pipeline );
  496. for ( const shaderAttribute of shaderAttributes ) {
  497. const name = shaderAttribute.name;
  498. const slot = shaderAttribute.slot;
  499. const attribute = geometryAttributes[ name ];
  500. if ( attribute !== undefined ) {
  501. const buffer = this._attributes.get( attribute ).buffer;
  502. encoder.setVertexBuffer( slot, buffer );
  503. }
  504. }
  505. }
  506. _setupColorBuffer() {
  507. const device = this._device;
  508. if ( device ) {
  509. if ( this._colorBuffer ) this._colorBuffer.destroy();
  510. this._colorBuffer = this._device.createTexture( {
  511. size: {
  512. width: this._width * this._pixelRatio,
  513. height: this._height * this._pixelRatio,
  514. depth: 1
  515. },
  516. sampleCount: this._parameters.sampleCount,
  517. format: GPUTextureFormat.BRGA8Unorm,
  518. usage: GPUTextureUsage.OUTPUT_ATTACHMENT
  519. } );
  520. }
  521. }
  522. _setupDepthBuffer() {
  523. const device = this._device;
  524. if ( device ) {
  525. if ( this._depthBuffer ) this._depthBuffer.destroy();
  526. this._depthBuffer = this._device.createTexture( {
  527. size: {
  528. width: this._width * this._pixelRatio,
  529. height: this._height * this._pixelRatio,
  530. depth: 1
  531. },
  532. sampleCount: this._parameters.sampleCount,
  533. format: GPUTextureFormat.Depth24PlusStencil8,
  534. usage: GPUTextureUsage.OUTPUT_ATTACHMENT
  535. } );
  536. }
  537. }
  538. _createCanvasElement() {
  539. const canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
  540. canvas.style.display = 'block';
  541. return canvas;
  542. }
  543. }
  544. export default WebGPURenderer;