WebGPURenderer.js 22 KB

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