WebGPURenderer.js 23 KB

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