WebGPURenderer.js 22 KB

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