WebGPURenderer.js 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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, NoToneMapping } 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.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._info );
  155. this._textures = new WebGPUTextures( device, this._properties, this._info );
  156. this._objects = new WebGPUObjects( this._geometries, this._info );
  157. this._utils = new WebGPUUtils( this );
  158. this._nodes = new WebGPUNodes( this, this._properties );
  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 );
  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._background.update( this._currentRenderList, scene );
  222. // start render pass
  223. const device = this._device;
  224. const cmdEncoder = device.createCommandEncoder( {} );
  225. const passEncoder = cmdEncoder.beginRenderPass( this._renderPassDescriptor );
  226. // global rasterization settings for all renderable objects
  227. const vp = this._viewport;
  228. if ( vp !== null ) {
  229. const width = Math.floor( vp.width * this._pixelRatio );
  230. const height = Math.floor( vp.height * this._pixelRatio );
  231. passEncoder.setViewport( vp.x, vp.y, width, height, vp.minDepth, vp.maxDepth );
  232. }
  233. const sc = this._scissor;
  234. if ( sc !== null ) {
  235. const width = Math.floor( sc.width * this._pixelRatio );
  236. const height = Math.floor( sc.height * this._pixelRatio );
  237. passEncoder.setScissorRect( sc.x, sc.y, width, height );
  238. }
  239. // lights node
  240. const lightsNode = this._currentRenderState.getLightsNode();
  241. // process render lists
  242. const opaqueObjects = this._currentRenderList.opaque;
  243. const transparentObjects = this._currentRenderList.transparent;
  244. if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, scene, lightsNode, passEncoder );
  245. if ( transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, scene, lightsNode, passEncoder );
  246. // finish render pass
  247. passEncoder.end();
  248. device.queue.submit( [ cmdEncoder.finish() ] );
  249. }
  250. setAnimationLoop( callback ) {
  251. if ( this._initialized === false ) this.init();
  252. const animation = this._animation;
  253. animation.setAnimationLoop( callback );
  254. ( callback === null ) ? animation.stop() : animation.start();
  255. }
  256. async getArrayBuffer( attribute ) {
  257. return await this._attributes.getArrayBuffer( attribute );
  258. }
  259. getContext() {
  260. return this._context;
  261. }
  262. getPixelRatio() {
  263. return this._pixelRatio;
  264. }
  265. getDrawingBufferSize( target ) {
  266. return target.set( this._width * this._pixelRatio, this._height * this._pixelRatio ).floor();
  267. }
  268. getSize( target ) {
  269. return target.set( this._width, this._height );
  270. }
  271. setPixelRatio( value = 1 ) {
  272. this._pixelRatio = value;
  273. this.setSize( this._width, this._height, false );
  274. }
  275. setDrawingBufferSize( width, height, pixelRatio ) {
  276. this._width = width;
  277. this._height = height;
  278. this._pixelRatio = pixelRatio;
  279. this.domElement.width = Math.floor( width * pixelRatio );
  280. this.domElement.height = Math.floor( height * pixelRatio );
  281. this._configureContext();
  282. this._setupColorBuffer();
  283. this._setupDepthBuffer();
  284. }
  285. setSize( width, height, updateStyle = true ) {
  286. this._width = width;
  287. this._height = height;
  288. this.domElement.width = Math.floor( width * this._pixelRatio );
  289. this.domElement.height = Math.floor( height * this._pixelRatio );
  290. if ( updateStyle === true ) {
  291. this.domElement.style.width = width + 'px';
  292. this.domElement.style.height = height + 'px';
  293. }
  294. this._configureContext();
  295. this._setupColorBuffer();
  296. this._setupDepthBuffer();
  297. }
  298. setOpaqueSort( method ) {
  299. this._opaqueSort = method;
  300. }
  301. setTransparentSort( method ) {
  302. this._transparentSort = method;
  303. }
  304. getScissor( target ) {
  305. const scissor = this._scissor;
  306. target.x = scissor.x;
  307. target.y = scissor.y;
  308. target.width = scissor.width;
  309. target.height = scissor.height;
  310. return target;
  311. }
  312. setScissor( x, y, width, height ) {
  313. if ( x === null ) {
  314. this._scissor = null;
  315. } else {
  316. this._scissor = {
  317. x: x,
  318. y: y,
  319. width: width,
  320. height: height
  321. };
  322. }
  323. }
  324. getViewport( target ) {
  325. const viewport = this._viewport;
  326. target.x = viewport.x;
  327. target.y = viewport.y;
  328. target.width = viewport.width;
  329. target.height = viewport.height;
  330. target.minDepth = viewport.minDepth;
  331. target.maxDepth = viewport.maxDepth;
  332. return target;
  333. }
  334. setViewport( x, y, width, height, minDepth = 0, maxDepth = 1 ) {
  335. if ( x === null ) {
  336. this._viewport = null;
  337. } else {
  338. this._viewport = {
  339. x: x,
  340. y: y,
  341. width: width,
  342. height: height,
  343. minDepth: minDepth,
  344. maxDepth: maxDepth
  345. };
  346. }
  347. }
  348. getClearColor( target ) {
  349. return target.copy( this._clearColor );
  350. }
  351. setClearColor( color, alpha = 1 ) {
  352. this._clearColor.set( color );
  353. this._clearAlpha = alpha;
  354. }
  355. getClearAlpha() {
  356. return this._clearAlpha;
  357. }
  358. setClearAlpha( alpha ) {
  359. this._clearAlpha = alpha;
  360. }
  361. getClearDepth() {
  362. return this._clearDepth;
  363. }
  364. setClearDepth( depth ) {
  365. this._clearDepth = depth;
  366. }
  367. getClearStencil() {
  368. return this._clearStencil;
  369. }
  370. setClearStencil( stencil ) {
  371. this._clearStencil = stencil;
  372. }
  373. clear() {
  374. if ( this._background ) this._background.clear();
  375. }
  376. dispose() {
  377. this._objects.dispose();
  378. this._properties.dispose();
  379. this._renderPipelines.dispose();
  380. this._computePipelines.dispose();
  381. this._nodes.dispose();
  382. this._bindings.dispose();
  383. this._info.dispose();
  384. this._renderLists.dispose();
  385. this._renderStates.dispose();
  386. this._textures.dispose();
  387. this.setRenderTarget( null );
  388. this.setAnimationLoop( null );
  389. }
  390. setRenderTarget( renderTarget ) {
  391. this._renderTarget = renderTarget;
  392. }
  393. async compute( ...computeNodes ) {
  394. if ( this._initialized === false ) await this.init();
  395. const device = this._device;
  396. const computePipelines = this._computePipelines;
  397. const cmdEncoder = device.createCommandEncoder( {} );
  398. const passEncoder = cmdEncoder.beginComputePass();
  399. for ( const computeNode of computeNodes ) {
  400. // onInit
  401. if ( computePipelines.has( computeNode ) === false ) {
  402. computeNode.onInit( { renderer: this } );
  403. }
  404. // pipeline
  405. const pipeline = computePipelines.get( computeNode );
  406. passEncoder.setPipeline( pipeline );
  407. // node
  408. //this._nodes.update( computeNode );
  409. // bind group
  410. const bindGroup = this._bindings.get( computeNode ).group;
  411. this._bindings.update( computeNode );
  412. passEncoder.setBindGroup( 0, bindGroup );
  413. passEncoder.dispatchWorkgroups( computeNode.dispatchCount );
  414. }
  415. passEncoder.end();
  416. device.queue.submit( [ cmdEncoder.finish() ] );
  417. }
  418. getRenderTarget() {
  419. return this._renderTarget;
  420. }
  421. _projectObject( object, camera, groupOrder ) {
  422. const currentRenderList = this._currentRenderList;
  423. const currentRenderState = this._currentRenderState;
  424. if ( object.visible === false ) return;
  425. const visible = object.layers.test( camera.layers );
  426. if ( visible ) {
  427. if ( object.isGroup ) {
  428. groupOrder = object.renderOrder;
  429. } else if ( object.isLOD ) {
  430. if ( object.autoUpdate === true ) object.update( camera );
  431. } else if ( object.isLight ) {
  432. currentRenderState.pushLight( object );
  433. if ( object.castShadow ) {
  434. //currentRenderState.pushShadow( object );
  435. }
  436. } else if ( object.isSprite ) {
  437. if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) {
  438. if ( this.sortObjects === true ) {
  439. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  440. }
  441. const geometry = object.geometry;
  442. const material = object.material;
  443. if ( material.visible ) {
  444. currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  445. }
  446. }
  447. } else if ( object.isLineLoop ) {
  448. console.error( 'THREE.WebGPURenderer: Objects of type THREE.LineLoop are not supported. Please use THREE.Line or THREE.LineSegments.' );
  449. } else if ( object.isMesh || object.isLine || object.isPoints ) {
  450. if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) {
  451. if ( this.sortObjects === true ) {
  452. _vector3.setFromMatrixPosition( object.matrixWorld ).applyMatrix4( _projScreenMatrix );
  453. }
  454. const geometry = object.geometry;
  455. const material = object.material;
  456. if ( Array.isArray( material ) ) {
  457. const groups = geometry.groups;
  458. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  459. const group = groups[ i ];
  460. const groupMaterial = material[ group.materialIndex ];
  461. if ( groupMaterial && groupMaterial.visible ) {
  462. currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group );
  463. }
  464. }
  465. } else if ( material.visible ) {
  466. currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null );
  467. }
  468. }
  469. }
  470. }
  471. const children = object.children;
  472. for ( let i = 0, l = children.length; i < l; i ++ ) {
  473. this._projectObject( children[ i ], camera, groupOrder );
  474. }
  475. }
  476. _renderObjects( renderList, camera, scene, lightsNode, passEncoder ) {
  477. // process renderable objects
  478. for ( let i = 0, il = renderList.length; i < il; i ++ ) {
  479. const renderItem = renderList[ i ];
  480. // @TODO: Add support for multiple materials per object. This will require to extract
  481. // the material from the renderItem object and pass it with its group data to _renderObject().
  482. const { object, geometry, material, group } = renderItem;
  483. if ( camera.isArrayCamera ) {
  484. const cameras = camera.cameras;
  485. for ( let j = 0, jl = cameras.length; j < jl; j ++ ) {
  486. const camera2 = cameras[ j ];
  487. if ( object.layers.test( camera2.layers ) ) {
  488. const vp = camera2.viewport;
  489. const minDepth = ( vp.minDepth === undefined ) ? 0 : vp.minDepth;
  490. const maxDepth = ( vp.maxDepth === undefined ) ? 1 : vp.maxDepth;
  491. passEncoder.setViewport( vp.x, vp.y, vp.width, vp.height, minDepth, maxDepth );
  492. this._renderObject( object, scene, camera2, geometry, material, group, lightsNode, passEncoder );
  493. }
  494. }
  495. } else {
  496. this._renderObject( object, scene, camera, geometry, material, group, lightsNode, passEncoder );
  497. }
  498. }
  499. }
  500. _renderObject( object, scene, camera, geometry, material, group, lightsNode, passEncoder ) {
  501. const info = this._info;
  502. // send scene properties to object
  503. const objectProperties = this._properties.get( object );
  504. objectProperties.lightsNode = lightsNode;
  505. objectProperties.scene = scene;
  506. //
  507. object.onBeforeRender( this, scene, camera, geometry, material, group );
  508. object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  509. object.normalMatrix.getNormalMatrix( object.modelViewMatrix );
  510. // updates
  511. this._nodes.update( object, camera );
  512. this._bindings.update( object );
  513. this._objects.update( object );
  514. // pipeline
  515. const renderPipeline = this._renderPipelines.get( object );
  516. passEncoder.setPipeline( renderPipeline.pipeline );
  517. // bind group
  518. const bindGroup = this._bindings.get( object ).group;
  519. passEncoder.setBindGroup( 0, bindGroup );
  520. // index
  521. const index = this._geometries.getIndex( geometry, material.wireframe === true );
  522. const hasIndex = ( index !== null );
  523. if ( hasIndex === true ) {
  524. this._setupIndexBuffer( index, passEncoder );
  525. }
  526. // vertex buffers
  527. this._setupVertexBuffers( geometry.attributes, passEncoder, renderPipeline );
  528. // draw
  529. const drawRange = geometry.drawRange;
  530. const firstVertex = drawRange.start;
  531. const instanceCount = geometry.isInstancedBufferGeometry ? geometry.instanceCount : ( object.isInstancedMesh ? object.count : 1 );
  532. if ( hasIndex === true ) {
  533. const indexCount = ( drawRange.count !== Infinity ) ? drawRange.count : index.count;
  534. passEncoder.drawIndexed( indexCount, instanceCount, firstVertex, 0, 0 );
  535. info.update( object, indexCount, instanceCount );
  536. } else {
  537. const positionAttribute = geometry.attributes.position;
  538. const vertexCount = ( drawRange.count !== Infinity ) ? drawRange.count : positionAttribute.count;
  539. passEncoder.draw( vertexCount, instanceCount, firstVertex, 0 );
  540. info.update( object, vertexCount, instanceCount );
  541. }
  542. }
  543. _setupIndexBuffer( index, encoder ) {
  544. const buffer = this._attributes.get( index ).buffer;
  545. const indexFormat = ( index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
  546. encoder.setIndexBuffer( buffer, indexFormat );
  547. }
  548. _setupVertexBuffers( geometryAttributes, encoder, renderPipeline ) {
  549. const shaderAttributes = renderPipeline.shaderAttributes;
  550. for ( const shaderAttribute of shaderAttributes ) {
  551. const name = shaderAttribute.name;
  552. const slot = shaderAttribute.slot;
  553. const attribute = geometryAttributes[ name ];
  554. if ( attribute !== undefined ) {
  555. const buffer = this._attributes.get( attribute ).buffer;
  556. encoder.setVertexBuffer( slot, buffer );
  557. }
  558. }
  559. }
  560. _setupColorBuffer() {
  561. const device = this._device;
  562. if ( device ) {
  563. if ( this._colorBuffer ) this._colorBuffer.destroy();
  564. this._colorBuffer = this._device.createTexture( {
  565. size: {
  566. width: Math.floor( this._width * this._pixelRatio ),
  567. height: Math.floor( this._height * this._pixelRatio ),
  568. depthOrArrayLayers: 1
  569. },
  570. sampleCount: this._parameters.sampleCount,
  571. format: GPUTextureFormat.BGRA8Unorm,
  572. usage: GPUTextureUsage.RENDER_ATTACHMENT
  573. } );
  574. }
  575. }
  576. _setupDepthBuffer() {
  577. const device = this._device;
  578. if ( device ) {
  579. if ( this._depthBuffer ) this._depthBuffer.destroy();
  580. this._depthBuffer = this._device.createTexture( {
  581. size: {
  582. width: Math.floor( this._width * this._pixelRatio ),
  583. height: Math.floor( this._height * this._pixelRatio ),
  584. depthOrArrayLayers: 1
  585. },
  586. sampleCount: this._parameters.sampleCount,
  587. format: GPUTextureFormat.Depth24PlusStencil8,
  588. usage: GPUTextureUsage.RENDER_ATTACHMENT
  589. } );
  590. }
  591. }
  592. _configureContext() {
  593. const device = this._device;
  594. if ( device ) {
  595. this._context.configure( {
  596. device: device,
  597. format: GPUTextureFormat.BGRA8Unorm,
  598. usage: GPUTextureUsage.RENDER_ATTACHMENT,
  599. alphaMode: 'premultiplied'
  600. } );
  601. }
  602. }
  603. _createCanvasElement() {
  604. const canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
  605. canvas.style.display = 'block';
  606. return canvas;
  607. }
  608. }
  609. export default WebGPURenderer;