WebGPURenderer.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  1. import { GPUIndexFormat, GPUTextureFormat } from './constants.js';
  2. import WebGPUAnimation from './WebGPUAnimation.js';
  3. import WebGPURenderObjects from './WebGPURenderObjects.js';
  4. import WebGPUAttributes from './WebGPUAttributes.js';
  5. import WebGPUGeometries from './WebGPUGeometries.js';
  6. import WebGPUInfo from './WebGPUInfo.js';
  7. import WebGPUProperties from './WebGPUProperties.js';
  8. import WebGPURenderPipelines from './WebGPURenderPipelines.js';
  9. import WebGPUComputePipelines from './WebGPUComputePipelines.js';
  10. import WebGPUBindings from './WebGPUBindings.js';
  11. import WebGPURenderLists from './WebGPURenderLists.js';
  12. import WebGPURenderStates from './WebGPURenderStates.js';
  13. import WebGPUTextures from './WebGPUTextures.js';
  14. import WebGPUBackground from './WebGPUBackground.js';
  15. import WebGPUNodes from './nodes/WebGPUNodes.js';
  16. import WebGPUUtils from './WebGPUUtils.js';
  17. import { Frustum, Matrix4, Vector3, Color, SRGBColorSpace, NoToneMapping, DepthFormat } from 'three';
  18. console.info( 'THREE.WebGPURenderer: Modified Matrix4.makePerspective() and Matrix4.makeOrtographic() to work with WebGPU, see https://github.com/mrdoob/three.js/issues/20276.' );
  19. Matrix4.prototype.makePerspective = function ( left, right, top, bottom, near, far ) {
  20. const te = this.elements;
  21. const x = 2 * near / ( right - left );
  22. const y = 2 * near / ( top - bottom );
  23. const a = ( right + left ) / ( right - left );
  24. const b = ( top + bottom ) / ( top - bottom );
  25. const c = - far / ( far - near );
  26. const d = - far * near / ( far - near );
  27. te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0;
  28. te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0;
  29. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d;
  30. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0;
  31. return this;
  32. };
  33. Matrix4.prototype.makeOrthographic = function ( left, right, top, bottom, near, far ) {
  34. const te = this.elements;
  35. const w = 1.0 / ( right - left );
  36. const h = 1.0 / ( top - bottom );
  37. const p = 1.0 / ( far - near );
  38. const x = ( right + left ) * w;
  39. const y = ( top + bottom ) * h;
  40. const z = near * p;
  41. te[ 0 ] = 2 * w; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = - x;
  42. te[ 1 ] = 0; te[ 5 ] = 2 * h; te[ 9 ] = 0; te[ 13 ] = - y;
  43. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = - 1 * p; te[ 14 ] = - z;
  44. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1;
  45. return this;
  46. };
  47. Frustum.prototype.setFromProjectionMatrix = function ( m ) {
  48. const planes = this.planes;
  49. const me = m.elements;
  50. const me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ];
  51. const me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ];
  52. const me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ];
  53. const me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ];
  54. planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize();
  55. planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize();
  56. planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize();
  57. planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize();
  58. planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize();
  59. planes[ 5 ].setComponents( me2, me6, me10, me14 ).normalize();
  60. return this;
  61. };
  62. const _frustum = new Frustum();
  63. const _projScreenMatrix = new Matrix4();
  64. const _vector3 = new Vector3();
  65. class WebGPURenderer {
  66. constructor( parameters = {} ) {
  67. this.isWebGPURenderer = true;
  68. // public
  69. this.domElement = ( parameters.canvas !== undefined ) ? parameters.canvas : this._createCanvasElement();
  70. this.autoClear = true;
  71. this.autoClearColor = true;
  72. this.autoClearDepth = true;
  73. this.autoClearStencil = true;
  74. this.outputColorSpace = SRGBColorSpace;
  75. this.toneMapping = NoToneMapping;
  76. this.toneMappingExposure = 1.0;
  77. this.sortObjects = true;
  78. // internals
  79. this._parameters = Object.assign( {}, parameters );
  80. this._pixelRatio = 1;
  81. this._width = this.domElement.width;
  82. this._height = this.domElement.height;
  83. this._viewport = null;
  84. this._scissor = null;
  85. this._adapter = null;
  86. this._device = null;
  87. this._context = null;
  88. this._colorBuffer = null;
  89. this._depthBuffer = null;
  90. this._info = null;
  91. this._properties = null;
  92. this._attributes = null;
  93. this._geometries = null;
  94. this._nodes = null;
  95. this._bindings = null;
  96. this._objects = null;
  97. this._renderPipelines = null;
  98. this._computePipelines = null;
  99. this._renderLists = null;
  100. this._renderStates = null;
  101. this._textures = null;
  102. this._background = null;
  103. this._animation = new WebGPUAnimation();
  104. this._currentRenderState = null;
  105. this._currentRenderList = null;
  106. this._opaqueSort = null;
  107. this._transparentSort = null;
  108. this._clearAlpha = 1;
  109. this._clearColor = new Color( 0x000000 );
  110. this._clearDepth = 1;
  111. this._clearStencil = 0;
  112. this._renderTarget = null;
  113. this._initialized = false;
  114. // some parameters require default values other than "undefined"
  115. this._parameters.antialias = ( parameters.antialias === true );
  116. if ( this._parameters.antialias === true ) {
  117. this._parameters.sampleCount = ( parameters.sampleCount === undefined ) ? 4 : parameters.sampleCount;
  118. } else {
  119. this._parameters.sampleCount = 1;
  120. }
  121. this._parameters.requiredFeatures = ( parameters.requiredFeatures === undefined ) ? [] : parameters.requiredFeatures;
  122. this._parameters.requiredLimits = ( parameters.requiredLimits === undefined ) ? {} : parameters.requiredLimits;
  123. // backwards compatibility
  124. this.shadow = {
  125. shadowMap: {}
  126. };
  127. }
  128. async init() {
  129. if ( this._initialized === true ) {
  130. throw new Error( 'WebGPURenderer: Device has already been initialized.' );
  131. }
  132. const parameters = this._parameters;
  133. const adapterOptions = {
  134. powerPreference: parameters.powerPreference
  135. };
  136. const adapter = await navigator.gpu.requestAdapter( adapterOptions );
  137. if ( adapter === null ) {
  138. throw new Error( 'WebGPURenderer: Unable to create WebGPU adapter.' );
  139. }
  140. const deviceDescriptor = {
  141. requiredFeatures: parameters.requiredFeatures,
  142. requiredLimits: parameters.requiredLimits
  143. };
  144. const device = await adapter.requestDevice( deviceDescriptor );
  145. const context = ( parameters.context !== undefined ) ? parameters.context : this.domElement.getContext( 'webgpu' );
  146. context.configure( {
  147. device,
  148. format: GPUTextureFormat.BGRA8Unorm, // this is the only valid context format right now (r121)
  149. alphaMode: 'premultiplied'
  150. } );
  151. this._adapter = adapter;
  152. this._device = device;
  153. this._context = context;
  154. this._info = new WebGPUInfo();
  155. this._properties = new WebGPUProperties();
  156. this._attributes = new WebGPUAttributes( device );
  157. this._geometries = new WebGPUGeometries( this._attributes, this._properties, this._info );
  158. this._textures = new WebGPUTextures( device, this._properties, this._info );
  159. this._utils = new WebGPUUtils( this );
  160. this._nodes = new WebGPUNodes( this, this._properties );
  161. this._objects = new WebGPURenderObjects( this, this._nodes, this._geometries, this._info );
  162. this._computePipelines = new WebGPUComputePipelines( device, this._nodes );
  163. this._renderPipelines = new WebGPURenderPipelines( device, this._nodes, this._utils );
  164. this._bindings = this._renderPipelines.bindings = new WebGPUBindings( device, this._info, this._properties, this._textures, this._renderPipelines, this._computePipelines, this._attributes, this._nodes );
  165. this._renderLists = new WebGPURenderLists();
  166. this._renderStates = new WebGPURenderStates();
  167. this._background = new WebGPUBackground( this, this._properties );
  168. //
  169. this._setupColorBuffer();
  170. this._setupDepthBuffer();
  171. this._animation.setNodes( this._nodes );
  172. this._animation.start();
  173. this._initialized = true;
  174. }
  175. async render( scene, camera ) {
  176. if ( this._initialized === false ) await this.init();
  177. //
  178. const nodeFrame = this._nodes.nodeFrame;
  179. let previousRenderId = nodeFrame.renderId;
  180. nodeFrame.renderId ++;
  181. if ( this._animation.isAnimating === false ) nodeFrame.update();
  182. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  183. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  184. if ( this._info.autoReset === true ) this._info.reset();
  185. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  186. _frustum.setFromProjectionMatrix( _projScreenMatrix );
  187. this._currentRenderList = this._renderLists.get( scene, camera );
  188. this._currentRenderList.init();
  189. this._currentRenderState = this._renderStates.get( scene, camera );
  190. this._currentRenderState.init();
  191. this._projectObject( scene, camera, 0 );
  192. this._currentRenderList.finish();
  193. if ( this.sortObjects === true ) {
  194. this._currentRenderList.sort( this._opaqueSort, this._transparentSort );
  195. }
  196. // prepare render pass descriptor
  197. const renderPassDescriptor = {
  198. colorAttachments: [ {
  199. view: null
  200. } ],
  201. depthStencilAttachment: {
  202. view: null
  203. }
  204. };
  205. const renderAttachments = {
  206. depth: true,
  207. stencil: true
  208. };
  209. const colorAttachment = renderPassDescriptor.colorAttachments[ 0 ];
  210. const depthStencilAttachment = renderPassDescriptor.depthStencilAttachment;
  211. const renderTarget = this._renderTarget;
  212. if ( renderTarget !== null ) {
  213. this._textures.initRenderTarget( renderTarget );
  214. // @TODO: Support RenderTarget with antialiasing.
  215. const renderTargetProperties = this._properties.get( renderTarget );
  216. colorAttachment.view = renderTargetProperties.colorTextureGPU.createView();
  217. depthStencilAttachment.view = renderTargetProperties.depthTextureGPU.createView();
  218. renderAttachments.stencil = renderTarget.depthTexture ? renderTarget.depthTexture.format !== DepthFormat : true;
  219. } else {
  220. if ( this._parameters.antialias === true ) {
  221. colorAttachment.view = this._colorBuffer.createView();
  222. colorAttachment.resolveTarget = this._context.getCurrentTexture().createView();
  223. } else {
  224. colorAttachment.view = this._context.getCurrentTexture().createView();
  225. colorAttachment.resolveTarget = undefined;
  226. }
  227. depthStencilAttachment.view = this._depthBuffer.createView();
  228. }
  229. //
  230. this._nodes.updateEnvironment( scene );
  231. this._nodes.updateFog( scene );
  232. this._nodes.updateBackground( scene );
  233. this._nodes.updateToneMapping();
  234. //
  235. this._background.update( this._currentRenderList, scene, renderPassDescriptor, renderAttachments );
  236. // start render pass
  237. const device = this._device;
  238. const cmdEncoder = device.createCommandEncoder( {} );
  239. const passEncoder = cmdEncoder.beginRenderPass( renderPassDescriptor );
  240. // global rasterization settings for all renderable objects
  241. const vp = this._viewport;
  242. if ( vp !== null ) {
  243. const width = Math.floor( vp.width * this._pixelRatio );
  244. const height = Math.floor( vp.height * this._pixelRatio );
  245. passEncoder.setViewport( vp.x, vp.y, width, height, vp.minDepth, vp.maxDepth );
  246. }
  247. const sc = this._scissor;
  248. if ( sc !== null ) {
  249. const width = Math.floor( sc.width * this._pixelRatio );
  250. const height = Math.floor( sc.height * this._pixelRatio );
  251. passEncoder.setScissorRect( sc.x, sc.y, width, height );
  252. }
  253. // lights node
  254. const lightsNode = this._currentRenderState.getLightsNode();
  255. // process render lists
  256. const opaqueObjects = this._currentRenderList.opaque;
  257. const transparentObjects = this._currentRenderList.transparent;
  258. if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, scene, lightsNode, passEncoder );
  259. if ( transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, scene, lightsNode, passEncoder );
  260. // finish render pass
  261. passEncoder.end();
  262. device.queue.submit( [ cmdEncoder.finish() ] );
  263. nodeFrame.renderId = previousRenderId;
  264. }
  265. setAnimationLoop( callback ) {
  266. if ( this._initialized === false ) this.init();
  267. const animation = this._animation;
  268. animation.setAnimationLoop( callback );
  269. ( callback === null ) ? animation.stop() : animation.start();
  270. }
  271. async getArrayBuffer( attribute ) {
  272. return await this._attributes.getArrayBuffer( attribute );
  273. }
  274. getContext() {
  275. return this._context;
  276. }
  277. getPixelRatio() {
  278. return this._pixelRatio;
  279. }
  280. getDrawingBufferSize( target ) {
  281. return target.set( this._width * this._pixelRatio, this._height * this._pixelRatio ).floor();
  282. }
  283. getSize( target ) {
  284. return target.set( this._width, this._height );
  285. }
  286. setPixelRatio( value = 1 ) {
  287. this._pixelRatio = value;
  288. this.setSize( this._width, this._height, false );
  289. }
  290. setDrawingBufferSize( width, height, pixelRatio ) {
  291. this._width = width;
  292. this._height = height;
  293. this._pixelRatio = pixelRatio;
  294. this.domElement.width = Math.floor( width * pixelRatio );
  295. this.domElement.height = Math.floor( height * pixelRatio );
  296. this._configureContext();
  297. this._setupColorBuffer();
  298. this._setupDepthBuffer();
  299. }
  300. setSize( width, height, updateStyle = true ) {
  301. this._width = width;
  302. this._height = height;
  303. this.domElement.width = Math.floor( width * this._pixelRatio );
  304. this.domElement.height = Math.floor( height * this._pixelRatio );
  305. if ( updateStyle === true ) {
  306. this.domElement.style.width = width + 'px';
  307. this.domElement.style.height = height + 'px';
  308. }
  309. this._configureContext();
  310. this._setupColorBuffer();
  311. this._setupDepthBuffer();
  312. }
  313. setOpaqueSort( method ) {
  314. this._opaqueSort = method;
  315. }
  316. setTransparentSort( method ) {
  317. this._transparentSort = method;
  318. }
  319. getScissor( target ) {
  320. const scissor = this._scissor;
  321. target.x = scissor.x;
  322. target.y = scissor.y;
  323. target.width = scissor.width;
  324. target.height = scissor.height;
  325. return target;
  326. }
  327. setScissor( x, y, width, height ) {
  328. if ( x === null ) {
  329. this._scissor = null;
  330. } else {
  331. this._scissor = {
  332. x: x,
  333. y: y,
  334. width: width,
  335. height: height
  336. };
  337. }
  338. }
  339. getViewport( target ) {
  340. const viewport = this._viewport;
  341. target.x = viewport.x;
  342. target.y = viewport.y;
  343. target.width = viewport.width;
  344. target.height = viewport.height;
  345. target.minDepth = viewport.minDepth;
  346. target.maxDepth = viewport.maxDepth;
  347. return target;
  348. }
  349. setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) {
  350. if ( x === null ) {
  351. this._viewport = null;
  352. } else {
  353. this._viewport = {
  354. x: x,
  355. y: y,
  356. width: width,
  357. height: height,
  358. minDepth: minDepth,
  359. maxDepth: maxDepth
  360. };
  361. }
  362. }
  363. getClearColor( target ) {
  364. return target.copy( this._clearColor );
  365. }
  366. setClearColor( color, alpha = 1 ) {
  367. this._clearColor.set( color );
  368. this._clearAlpha = alpha;
  369. }
  370. getClearAlpha() {
  371. return this._clearAlpha;
  372. }
  373. setClearAlpha( alpha ) {
  374. this._clearAlpha = alpha;
  375. }
  376. getClearDepth() {
  377. return this._clearDepth;
  378. }
  379. setClearDepth( depth ) {
  380. this._clearDepth = depth;
  381. }
  382. getClearStencil() {
  383. return this._clearStencil;
  384. }
  385. setClearStencil( stencil ) {
  386. this._clearStencil = stencil;
  387. }
  388. clear() {
  389. if ( this._background ) this._background.clear();
  390. }
  391. dispose() {
  392. this._objects.dispose();
  393. this._properties.dispose();
  394. this._renderPipelines.dispose();
  395. this._computePipelines.dispose();
  396. this._nodes.dispose();
  397. this._bindings.dispose();
  398. this._info.dispose();
  399. this._renderLists.dispose();
  400. this._renderStates.dispose();
  401. this._textures.dispose();
  402. this.setRenderTarget( null );
  403. this.setAnimationLoop( null );
  404. }
  405. setRenderTarget( renderTarget ) {
  406. this._renderTarget = renderTarget;
  407. }
  408. async compute( ...computeNodes ) {
  409. if ( this._initialized === false ) await this.init();
  410. const device = this._device;
  411. const computePipelines = this._computePipelines;
  412. const cmdEncoder = device.createCommandEncoder( {} );
  413. const passEncoder = cmdEncoder.beginComputePass();
  414. for ( const computeNode of computeNodes ) {
  415. // onInit
  416. if ( computePipelines.has( computeNode ) === false ) {
  417. computeNode.onInit( { renderer: this } );
  418. }
  419. // pipeline
  420. const pipeline = computePipelines.get( computeNode );
  421. passEncoder.setPipeline( pipeline );
  422. // node
  423. //this._nodes.update( computeNode );
  424. // bind group
  425. const bindGroup = this._bindings.getForCompute( computeNode ).group;
  426. this._bindings.update( computeNode );
  427. passEncoder.setBindGroup( 0, bindGroup );
  428. passEncoder.dispatchWorkgroups( computeNode.dispatchCount );
  429. }
  430. passEncoder.end();
  431. device.queue.submit( [ cmdEncoder.finish() ] );
  432. }
  433. getRenderTarget() {
  434. return this._renderTarget;
  435. }
  436. _projectObject( object, camera, groupOrder ) {
  437. const currentRenderList = this._currentRenderList;
  438. const currentRenderState = this._currentRenderState;
  439. if ( object.visible === false ) return;
  440. const visible = object.layers.test( camera.layers );
  441. if ( visible ) {
  442. if ( object.isGroup ) {
  443. groupOrder = object.renderOrder;
  444. } else if ( object.isLOD ) {
  445. if ( object.autoUpdate === true ) object.update( camera );
  446. } else if ( object.isLight ) {
  447. currentRenderState.pushLight( object );
  448. if ( object.castShadow ) {
  449. //currentRenderState.pushShadow( object );
  450. }
  451. } else if ( object.isSprite ) {
  452. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  453. if ( this.sortObjects === true ) {
  454. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  455. }
  456. const geometry = object.geometry;
  457. const material = object.material;
  458. if ( material.visible ) {
  459. currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  460. }
  461. }
  462. } else if ( object.isLineLoop ) {
  463. console.error( 'THREE.WebGPURenderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  464. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  465. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  466. if ( this.sortObjects === true ) {
  467. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  468. }
  469. const geometry = object.geometry;
  470. const material = object.material;
  471. if ( Array.isArray( material ) ) {
  472. const groups = geometry.groups;
  473. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  474. const group = groups[ i ];
  475. const groupMaterial = material[ group.materialIndex ];
  476. if ( groupMaterial && groupMaterial.visible ) {
  477. currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  478. }
  479. }
  480. } else if ( material.visible ) {
  481. currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  482. }
  483. }
  484. }
  485. }
  486. const children = object.children;
  487. for ( let i = 0, l = children.length; i < l; i ++ ) {
  488. this._projectObject( children[ i ], camera, groupOrder );
  489. }
  490. }
  491. _renderObjects( renderList, camera, scene, lightsNode, passEncoder ) {
  492. // process renderable objects
  493. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  494. const renderItem = renderList[ i ];
  495. // @TODO: Add support for multiple materials per object. This will require to extract
  496. // the material from the renderItem object and pass it with its group data to _renderObject().
  497. const { object, geometry, material, group } = renderItem;
  498. if ( camera.isArrayCamera ) {
  499. const cameras = camera.cameras;
  500. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  501. const camera2 = cameras[ j ];
  502. if ( object.layers.test( camera2.layers ) ) {
  503. const vp = camera2.viewport;
  504. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  505. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  506. passEncoder.setViewport( vp.x, vp.y, vp.width, vp.height, minDepth, maxDepth );
  507. this._renderObject( object, scene, camera2, geometry, material, group, lightsNode, passEncoder );
  508. }
  509. }
  510. } else {
  511. this._renderObject( object, scene, camera, geometry, material, group, lightsNode, passEncoder );
  512. }
  513. }
  514. }
  515. _renderObject( object, scene, camera, geometry, material, group, lightsNode, passEncoder ) {
  516. const info = this._info;
  517. material = scene.overrideMaterial !== null ? scene.overrideMaterial : material;
  518. //
  519. object.onBeforeRender( this, scene, camera, geometry, material, group );
  520. //
  521. const renderObject = this._getRenderObject( object, material, scene, camera, lightsNode );
  522. //
  523. this._nodes.updateBefore( renderObject );
  524. //
  525. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  526. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  527. // updates
  528. this._nodes.update( renderObject );
  529. this._geometries.update( renderObject );
  530. this._bindings.update( renderObject );
  531. // pipeline
  532. const renderPipeline = this._renderPipelines.get( renderObject );
  533. passEncoder.setPipeline( renderPipeline.pipeline );
  534. // bind group
  535. const bindGroup = this._bindings.get( renderObject ).group;
  536. passEncoder.setBindGroup( 0, bindGroup );
  537. // index
  538. const index = this._geometries.getIndex( renderObject );
  539. const hasIndex = ( index !== null );
  540. if ( hasIndex === true ) {
  541. this._setupIndexBuffer( index, passEncoder );
  542. }
  543. // vertex buffers
  544. this._setupVertexBuffers( geometry.attributes, passEncoder, renderPipeline );
  545. // draw
  546. const drawRange = geometry.drawRange;
  547. const firstVertex = drawRange.start;
  548. const instanceCount = geometry.isInstancedBufferGeometry ? geometry.instanceCount : ( object.isInstancedMesh ? object.count : 1 );
  549. if ( hasIndex === true ) {
  550. const indexCount = ( drawRange.count !== Infinity ) ? drawRange.count : index.count;
  551. passEncoder.drawIndexed( indexCount, instanceCount, firstVertex, 0, 0 );
  552. info.update( object, indexCount, instanceCount );
  553. } else {
  554. const positionAttribute = geometry.attributes.position;
  555. const vertexCount = ( drawRange.count !== Infinity ) ? drawRange.count : positionAttribute.count;
  556. passEncoder.draw( vertexCount, instanceCount, firstVertex, 0 );
  557. info.update( object, vertexCount, instanceCount );
  558. }
  559. }
  560. _getRenderObject( object, material, scene, camera, lightsNode ) {
  561. const renderObject = this._objects.get( object, material, scene, camera, lightsNode );
  562. const renderObjectProperties = this._properties.get( renderObject );
  563. if ( renderObjectProperties.initialized !== true ) {
  564. renderObjectProperties.initialized = true;
  565. const dispose = () => {
  566. this._renderPipelines.remove( renderObject );
  567. this._nodes.remove( renderObject );
  568. this._properties.remove( renderObject );
  569. this._objects.remove( object, material, scene, camera, lightsNode );
  570. renderObject.material.removeEventListener( 'dispose', dispose );
  571. };
  572. renderObject.material.addEventListener( 'dispose', dispose );
  573. }
  574. const cacheKey = renderObject.getCacheKey();
  575. if ( renderObjectProperties.cacheKey !== cacheKey ) {
  576. renderObjectProperties.cacheKey = cacheKey;
  577. this._renderPipelines.remove( renderObject );
  578. this._nodes.remove( renderObject );
  579. }
  580. return renderObject;
  581. }
  582. _setupIndexBuffer( index, encoder ) {
  583. const buffer = this._attributes.get( index ).buffer;
  584. const indexFormat = ( index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
  585. encoder.setIndexBuffer( buffer, indexFormat );
  586. }
  587. _setupVertexBuffers( geometryAttributes, encoder, renderPipeline ) {
  588. const shaderAttributes = renderPipeline.shaderAttributes;
  589. for ( const shaderAttribute of shaderAttributes ) {
  590. const name = shaderAttribute.name;
  591. const slot = shaderAttribute.slot;
  592. const attribute = geometryAttributes[ name ];
  593. if ( attribute !== undefined ) {
  594. const buffer = this._attributes.get( attribute ).buffer;
  595. encoder.setVertexBuffer( slot, buffer );
  596. }
  597. }
  598. }
  599. _setupColorBuffer() {
  600. const device = this._device;
  601. if ( device ) {
  602. if ( this._colorBuffer ) this._colorBuffer.destroy();
  603. this._colorBuffer = this._device.createTexture( {
  604. label: 'colorBuffer',
  605. size: {
  606. width: Math.floor( this._width * this._pixelRatio ),
  607. height: Math.floor( this._height * this._pixelRatio ),
  608. depthOrArrayLayers: 1
  609. },
  610. sampleCount: this._parameters.sampleCount,
  611. format: GPUTextureFormat.BGRA8Unorm,
  612. usage: GPUTextureUsage.RENDER_ATTACHMENT
  613. } );
  614. }
  615. }
  616. _setupDepthBuffer() {
  617. const device = this._device;
  618. if ( device ) {
  619. if ( this._depthBuffer ) this._depthBuffer.destroy();
  620. this._depthBuffer = this._device.createTexture( {
  621. label: 'depthBuffer',
  622. size: {
  623. width: Math.floor( this._width * this._pixelRatio ),
  624. height: Math.floor( this._height * this._pixelRatio ),
  625. depthOrArrayLayers: 1
  626. },
  627. sampleCount: this._parameters.sampleCount,
  628. format: GPUTextureFormat.Depth24PlusStencil8,
  629. usage: GPUTextureUsage.RENDER_ATTACHMENT
  630. } );
  631. }
  632. }
  633. _configureContext() {
  634. const device = this._device;
  635. if ( device ) {
  636. this._context.configure( {
  637. device: device,
  638. format: GPUTextureFormat.BGRA8Unorm,
  639. usage: GPUTextureUsage.RENDER_ATTACHMENT,
  640. alphaMode: 'premultiplied'
  641. } );
  642. }
  643. }
  644. _createCanvasElement() {
  645. const canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
  646. canvas.style.display = 'block';
  647. return canvas;
  648. }
  649. }
  650. export default WebGPURenderer;