WebGPURenderer.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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, LinearEncoding } from 'three';
  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.outputEncoding = LinearEncoding;
  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.requiredFeatures = ( parameters.requiredFeatures === undefined ) ? [] : parameters.requiredFeatures;
  99. this._parameters.requiredLimits = ( parameters.requiredLimits === undefined ) ? {} : parameters.requiredLimits;
  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. if ( adapter === null ) {
  108. throw new Error( 'WebGPURenderer: Unable to create WebGPU adapter.' );
  109. }
  110. const deviceDescriptor = {
  111. requiredFeatures: parameters.requiredFeatures,
  112. requiredLimits: parameters.requiredLimits
  113. };
  114. const device = await adapter.requestDevice( deviceDescriptor );
  115. const context = ( parameters.context !== undefined ) ? parameters.context : this.domElement.getContext( 'webgpu' );
  116. const swapChain = context.configure( {
  117. device: device,
  118. format: GPUTextureFormat.BGRA8Unorm // this is the only valid swap chain format right now (r121)
  119. } );
  120. this._adapter = adapter;
  121. this._device = device;
  122. this._context = context;
  123. this._swapChain = swapChain;
  124. this._info = new WebGPUInfo();
  125. this._properties = new WebGPUProperties();
  126. this._attributes = new WebGPUAttributes( device );
  127. this._geometries = new WebGPUGeometries( this._attributes, this._info );
  128. this._textures = new WebGPUTextures( device, this._properties, this._info );
  129. this._objects = new WebGPUObjects( this._geometries, this._info );
  130. this._nodes = new WebGPUNodes( this );
  131. this._renderPipelines = new WebGPURenderPipelines( this, this._properties, device, parameters.sampleCount, this._nodes );
  132. this._computePipelines = new WebGPUComputePipelines( device );
  133. this._bindings = new WebGPUBindings( device, this._info, this._properties, this._textures, this._renderPipelines, this._computePipelines, this._attributes, this._nodes );
  134. this._renderLists = new WebGPURenderLists();
  135. this._background = new WebGPUBackground( this );
  136. //
  137. this._renderPassDescriptor = {
  138. colorAttachments: [ {
  139. view: null
  140. } ],
  141. depthStencilAttachment: {
  142. view: null,
  143. depthStoreOp: GPUStoreOp.Store,
  144. stencilStoreOp: GPUStoreOp.Store
  145. }
  146. };
  147. this._setupColorBuffer();
  148. this._setupDepthBuffer();
  149. }
  150. render( scene, camera ) {
  151. // @TODO: move this to animation loop
  152. this._nodes.updateFrame();
  153. //
  154. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  155. if ( camera.parent === null ) camera.updateMatrixWorld();
  156. if ( this._info.autoReset === true ) this._info.reset();
  157. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  158. _frustum.setFromProjectionMatrix( _projScreenMatrix );
  159. this._currentRenderList = this._renderLists.get( scene, camera );
  160. this._currentRenderList.init();
  161. this._projectObject( scene, camera, 0 );
  162. this._currentRenderList.finish();
  163. if ( this.sortObjects === true ) {
  164. this._currentRenderList.sort( this._opaqueSort, this._transparentSort );
  165. }
  166. // prepare render pass descriptor
  167. const colorAttachment = this._renderPassDescriptor.colorAttachments[ 0 ];
  168. const depthStencilAttachment = this._renderPassDescriptor.depthStencilAttachment;
  169. const renderTarget = this._renderTarget;
  170. if ( renderTarget !== null ) {
  171. // @TODO: Support RenderTarget with antialiasing.
  172. const renderTargetProperties = this._properties.get( renderTarget );
  173. colorAttachment.view = renderTargetProperties.colorTextureGPU.createView();
  174. depthStencilAttachment.view = renderTargetProperties.depthTextureGPU.createView();
  175. } else {
  176. if ( this._parameters.antialias === true ) {
  177. colorAttachment.view = this._colorBuffer.createView();
  178. colorAttachment.resolveTarget = this._context.getCurrentTexture().createView();
  179. } else {
  180. colorAttachment.view = this._context.getCurrentTexture().createView();
  181. colorAttachment.resolveTarget = undefined;
  182. }
  183. depthStencilAttachment.view = this._depthBuffer.createView();
  184. }
  185. //
  186. this._background.update( scene );
  187. // start render pass
  188. const device = this._device;
  189. const cmdEncoder = device.createCommandEncoder( {} );
  190. const passEncoder = cmdEncoder.beginRenderPass( this._renderPassDescriptor );
  191. // global rasterization settings for all renderable objects
  192. const vp = this._viewport;
  193. if ( vp !== null ) {
  194. const width = Math.floor( vp.width * this._pixelRatio );
  195. const height = Math.floor( vp.height * this._pixelRatio );
  196. passEncoder.setViewport( vp.x, vp.y, width, height, vp.minDepth, vp.maxDepth );
  197. }
  198. const sc = this._scissor;
  199. if ( sc !== null ) {
  200. const width = Math.floor( sc.width * this._pixelRatio );
  201. const height = Math.floor( sc.height * this._pixelRatio );
  202. passEncoder.setScissorRect( sc.x, sc.y, width, height );
  203. }
  204. // process render lists
  205. const opaqueObjects = this._currentRenderList.opaque;
  206. const transparentObjects = this._currentRenderList.transparent;
  207. if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, passEncoder );
  208. if ( transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, passEncoder );
  209. // finish render pass
  210. passEncoder.endPass();
  211. device.queue.submit( [ cmdEncoder.finish() ] );
  212. }
  213. getContext() {
  214. return this._context;
  215. }
  216. getPixelRatio() {
  217. return this._pixelRatio;
  218. }
  219. getDrawingBufferSize( target ) {
  220. return target.set( this._width * this._pixelRatio, this._height * this._pixelRatio ).floor();
  221. }
  222. getSize( target ) {
  223. return target.set( this._width, this._height );
  224. }
  225. setPixelRatio( value = 1 ) {
  226. this._pixelRatio = value;
  227. this.setSize( this._width, this._height, false );
  228. }
  229. setDrawingBufferSize( width, height, pixelRatio ) {
  230. this._width = width;
  231. this._height = height;
  232. this._pixelRatio = pixelRatio;
  233. this.domElement.width = Math.floor( width * pixelRatio );
  234. this.domElement.height = Math.floor( height * pixelRatio );
  235. this._configureContext();
  236. this._setupColorBuffer();
  237. this._setupDepthBuffer();
  238. }
  239. setSize( width, height, updateStyle = true ) {
  240. this._width = width;
  241. this._height = height;
  242. this.domElement.width = Math.floor( width * this._pixelRatio );
  243. this.domElement.height = Math.floor( height * this._pixelRatio );
  244. if ( updateStyle === true ) {
  245. this.domElement.style.width = width + 'px';
  246. this.domElement.style.height = height + 'px';
  247. }
  248. this._configureContext();
  249. this._setupColorBuffer();
  250. this._setupDepthBuffer();
  251. }
  252. setOpaqueSort( method ) {
  253. this._opaqueSort = method;
  254. }
  255. setTransparentSort( method ) {
  256. this._transparentSort = method;
  257. }
  258. getScissor( target ) {
  259. const scissor = this._scissor;
  260. target.x = scissor.x;
  261. target.y = scissor.y;
  262. target.width = scissor.width;
  263. target.height = scissor.height;
  264. return target;
  265. }
  266. setScissor( x, y, width, height ) {
  267. if ( x === null ) {
  268. this._scissor = null;
  269. } else {
  270. this._scissor = {
  271. x: x,
  272. y: y,
  273. width: width,
  274. height: height
  275. };
  276. }
  277. }
  278. getViewport( target ) {
  279. const viewport = this._viewport;
  280. target.x = viewport.x;
  281. target.y = viewport.y;
  282. target.width = viewport.width;
  283. target.height = viewport.height;
  284. target.minDepth = viewport.minDepth;
  285. target.maxDepth = viewport.maxDepth;
  286. return target;
  287. }
  288. setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) {
  289. if ( x === null ) {
  290. this._viewport = null;
  291. } else {
  292. this._viewport = {
  293. x: x,
  294. y: y,
  295. width: width,
  296. height: height,
  297. minDepth: minDepth,
  298. maxDepth: maxDepth
  299. };
  300. }
  301. }
  302. getCurrentEncoding() {
  303. const renderTarget = this.getRenderTarget();
  304. return ( renderTarget !== null ) ? renderTarget.texture.encoding : this.outputEncoding;
  305. }
  306. getCurrentColorFormat() {
  307. let format;
  308. const renderTarget = this.getRenderTarget();
  309. if ( renderTarget !== null ) {
  310. const renderTargetProperties = this._properties.get( renderTarget );
  311. format = renderTargetProperties.colorTextureFormat;
  312. } else {
  313. format = GPUTextureFormat.BGRA8Unorm; // default swap chain format
  314. }
  315. return format;
  316. }
  317. getCurrentDepthStencilFormat() {
  318. let format;
  319. const renderTarget = this.getRenderTarget();
  320. if ( renderTarget !== null ) {
  321. const renderTargetProperties = this._properties.get( renderTarget );
  322. format = renderTargetProperties.depthTextureFormat;
  323. } else {
  324. format = GPUTextureFormat.Depth24PlusStencil8;
  325. }
  326. return format;
  327. }
  328. getClearColor( target ) {
  329. return target.copy( this._clearColor );
  330. }
  331. setClearColor( color, alpha = 1 ) {
  332. this._clearColor.set( color );
  333. this._clearAlpha = alpha;
  334. }
  335. getClearAlpha() {
  336. return this._clearAlpha;
  337. }
  338. setClearAlpha( alpha ) {
  339. this._clearAlpha = alpha;
  340. }
  341. getClearDepth() {
  342. return this._clearDepth;
  343. }
  344. setClearDepth( depth ) {
  345. this._clearDepth = depth;
  346. }
  347. getClearStencil() {
  348. return this._clearStencil;
  349. }
  350. setClearStencil( stencil ) {
  351. this._clearStencil = stencil;
  352. }
  353. clear() {
  354. this._background.clear();
  355. }
  356. dispose() {
  357. this._objects.dispose();
  358. this._properties.dispose();
  359. this._renderPipelines.dispose();
  360. this._computePipelines.dispose();
  361. this._nodes.dispose();
  362. this._bindings.dispose();
  363. this._info.dispose();
  364. this._renderLists.dispose();
  365. this._textures.dispose();
  366. }
  367. setRenderTarget( renderTarget ) {
  368. this._renderTarget = renderTarget;
  369. if ( renderTarget !== null ) {
  370. this._textures.initRenderTarget( renderTarget );
  371. }
  372. }
  373. compute( computeParams ) {
  374. const device = this._device;
  375. const cmdEncoder = device.createCommandEncoder( {} );
  376. const passEncoder = cmdEncoder.beginComputePass();
  377. for ( const param of computeParams ) {
  378. // pipeline
  379. const pipeline = this._computePipelines.get( param );
  380. passEncoder.setPipeline( pipeline );
  381. // bind group
  382. const bindGroup = this._bindings.getForCompute( param ).group;
  383. this._bindings.update( param );
  384. passEncoder.setBindGroup( 0, bindGroup );
  385. passEncoder.dispatch( param.num );
  386. }
  387. passEncoder.endPass();
  388. device.queue.submit( [ cmdEncoder.finish() ] );
  389. }
  390. getRenderTarget() {
  391. return this._renderTarget;
  392. }
  393. _projectObject( object, camera, groupOrder ) {
  394. const currentRenderList = this._currentRenderList;
  395. if ( object.visible === false ) return;
  396. const visible = object.layers.test( camera.layers );
  397. if ( visible ) {
  398. if ( object.isGroup ) {
  399. groupOrder = object.renderOrder;
  400. } else if ( object.isLOD ) {
  401. if ( object.autoUpdate === true ) object.update( camera );
  402. } else if ( object.isLight ) {
  403. //currentRenderState.pushLight( object );
  404. if ( object.castShadow ) {
  405. //currentRenderState.pushShadow( object );
  406. }
  407. } else if ( object.isSprite ) {
  408. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  409. if ( this.sortObjects === true ) {
  410. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  411. }
  412. const geometry = object.geometry;
  413. const material = object.material;
  414. if ( material.visible ) {
  415. currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  416. }
  417. }
  418. } else if ( object.isLineLoop ) {
  419. console.error( 'THREE.WebGPURenderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  420. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  421. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  422. if ( this.sortObjects === true ) {
  423. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  424. }
  425. const geometry = object.geometry;
  426. const material = object.material;
  427. if ( Array.isArray( material ) ) {
  428. const groups = geometry.groups;
  429. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  430. const group = groups[ i ];
  431. const groupMaterial = material[ group.materialIndex ];
  432. if ( groupMaterial && groupMaterial.visible ) {
  433. currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  434. }
  435. }
  436. } else if ( material.visible ) {
  437. currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  438. }
  439. }
  440. }
  441. }
  442. const children = object.children;
  443. for ( let i = 0, l = children.length; i < l; i ++ ) {
  444. this._projectObject( children[ i ], camera, groupOrder );
  445. }
  446. }
  447. _renderObjects( renderList, camera, passEncoder ) {
  448. // process renderable objects
  449. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  450. const renderItem = renderList[ i ];
  451. // @TODO: Add support for multiple materials per object. This will require to extract
  452. // the material from the renderItem object and pass it with its group data to _renderObject().
  453. const object = renderItem.object;
  454. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  455. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  456. this._objects.update( object );
  457. if ( camera.isArrayCamera ) {
  458. const cameras = camera.cameras;
  459. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  460. const camera2 = cameras[ j ];
  461. if ( object.layers.test( camera2.layers ) ) {
  462. const vp = camera2.viewport;
  463. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  464. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  465. passEncoder.setViewport( vp.x, vp.y, vp.width, vp.height, minDepth, maxDepth );
  466. this._nodes.update( object, camera2 );
  467. this._bindings.update( object );
  468. this._renderObject( object, passEncoder );
  469. }
  470. }
  471. } else {
  472. this._nodes.update( object, camera );
  473. this._bindings.update( object );
  474. this._renderObject( object, passEncoder );
  475. }
  476. }
  477. }
  478. _renderObject( object, passEncoder ) {
  479. const info = this._info;
  480. // pipeline
  481. const renderPipeline = this._renderPipelines.get( object );
  482. passEncoder.setPipeline( renderPipeline.pipeline );
  483. // bind group
  484. const bindGroup = this._bindings.get( object ).group;
  485. passEncoder.setBindGroup( 0, bindGroup );
  486. // index
  487. const geometry = object.geometry;
  488. const index = geometry.index;
  489. const hasIndex = ( index !== null );
  490. if ( hasIndex === true ) {
  491. this._setupIndexBuffer( index, passEncoder );
  492. }
  493. // vertex buffers
  494. this._setupVertexBuffers( geometry.attributes, passEncoder, renderPipeline );
  495. // draw
  496. const drawRange = geometry.drawRange;
  497. const firstVertex = drawRange.start;
  498. const instanceCount = ( geometry.isInstancedBufferGeometry ) ? geometry.instanceCount : 1;
  499. if ( hasIndex === true ) {
  500. const indexCount = ( drawRange.count !== Infinity ) ? drawRange.count : index.count;
  501. passEncoder.drawIndexed( indexCount, instanceCount, firstVertex, 0, 0 );
  502. info.update( object, indexCount, instanceCount );
  503. } else {
  504. const positionAttribute = geometry.attributes.position;
  505. const vertexCount = ( drawRange.count !== Infinity ) ? drawRange.count : positionAttribute.count;
  506. passEncoder.draw( vertexCount, instanceCount, firstVertex, 0 );
  507. info.update( object, vertexCount, instanceCount );
  508. }
  509. }
  510. _setupIndexBuffer( index, encoder ) {
  511. const buffer = this._attributes.get( index ).buffer;
  512. const indexFormat = ( index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
  513. encoder.setIndexBuffer( buffer, indexFormat );
  514. }
  515. _setupVertexBuffers( geometryAttributes, encoder, renderPipeline ) {
  516. const shaderAttributes = renderPipeline.shaderAttributes;
  517. for ( const shaderAttribute of shaderAttributes ) {
  518. const name = shaderAttribute.name;
  519. const slot = shaderAttribute.slot;
  520. const attribute = geometryAttributes[ name ];
  521. if ( attribute !== undefined ) {
  522. const buffer = this._attributes.get( attribute ).buffer;
  523. encoder.setVertexBuffer( slot, buffer );
  524. }
  525. }
  526. }
  527. _setupColorBuffer() {
  528. const device = this._device;
  529. if ( device ) {
  530. if ( this._colorBuffer ) this._colorBuffer.destroy();
  531. this._colorBuffer = this._device.createTexture( {
  532. size: {
  533. width: Math.floor( this._width * this._pixelRatio ),
  534. height: Math.floor( this._height * this._pixelRatio ),
  535. depthOrArrayLayers: 1
  536. },
  537. sampleCount: this._parameters.sampleCount,
  538. format: GPUTextureFormat.BGRA8Unorm,
  539. usage: GPUTextureUsage.RENDER_ATTACHMENT
  540. } );
  541. }
  542. }
  543. _setupDepthBuffer() {
  544. const device = this._device;
  545. if ( device ) {
  546. if ( this._depthBuffer ) this._depthBuffer.destroy();
  547. this._depthBuffer = this._device.createTexture( {
  548. size: {
  549. width: Math.floor( this._width * this._pixelRatio ),
  550. height: Math.floor( this._height * this._pixelRatio ),
  551. depthOrArrayLayers: 1
  552. },
  553. sampleCount: this._parameters.sampleCount,
  554. format: GPUTextureFormat.Depth24PlusStencil8,
  555. usage: GPUTextureUsage.RENDER_ATTACHMENT
  556. } );
  557. }
  558. }
  559. _configureContext() {
  560. const device = this._device;
  561. if ( device ) {
  562. this._context.configure( {
  563. device: device,
  564. format: GPUTextureFormat.BGRA8Unorm,
  565. usage: GPUTextureUsage.RENDER_ATTACHMENT,
  566. size: {
  567. width: Math.floor( this._width * this._pixelRatio ),
  568. height: Math.floor( this._height * this._pixelRatio ),
  569. depthOrArrayLayers: 1
  570. },
  571. } );
  572. }
  573. }
  574. _createCanvasElement() {
  575. const canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
  576. canvas.style.display = 'block';
  577. return canvas;
  578. }
  579. }
  580. WebGPURenderer.prototype.isWebGPURenderer = true;
  581. export default WebGPURenderer;