WebGPURenderer.js 20 KB

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