WebGPURenderer.js 22 KB

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