2
0

WebGPURenderer.js 24 KB

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