WebGPURenderer.js 23 KB

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