2
0

WebGPUBackend.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. import { GPUFeatureName, GPUTextureFormat, GPULoadOp, GPUStoreOp, GPUIndexFormat } from './utils/WebGPUConstants.js';
  2. import WebGPUNodeBuilder from './nodes/WGSLNodeBuilder.js';
  3. import Backend from '../common/Backend.js';
  4. import { Matrix4, Frustum, DepthFormat } from 'three';
  5. import WebGPUUtils from './utils/WebGPUUtils.js';
  6. import WebGPUAttributeUtils from './utils/WebGPUAttributeUtils.js';
  7. import WebGPUBindingUtils from './utils/WebGPUBindingUtils.js';
  8. import WebGPUPipelineUtils from './utils/WebGPUPipelineUtils.js';
  9. import WebGPUTextureUtils from './utils/WebGPUTextureUtils.js';
  10. // statics
  11. let _staticAdapter = null;
  12. if ( navigator.gpu !== undefined ) {
  13. _staticAdapter = await navigator.gpu.requestAdapter();
  14. }
  15. // hacks
  16. let _initializedHack = false;
  17. function _initWebGPUHack() {
  18. if ( _initializedHack ) return;
  19. console.info( 'THREE.WebGPURenderer: Modified Matrix4.makePerspective() and Matrix4.makeOrtographic() to work with WebGPU, see https://github.com/mrdoob/three.js/issues/20276.' );
  20. Matrix4.prototype.makePerspective = function ( left, right, top, bottom, near, far ) {
  21. const te = this.elements;
  22. const x = 2 * near / ( right - left );
  23. const y = 2 * near / ( top - bottom );
  24. const a = ( right + left ) / ( right - left );
  25. const b = ( top + bottom ) / ( top - bottom );
  26. const c = - far / ( far - near );
  27. const d = - far * near / ( far - near );
  28. te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0;
  29. te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0;
  30. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d;
  31. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0;
  32. return this;
  33. };
  34. Matrix4.prototype.makeOrthographic = function ( left, right, top, bottom, near, far ) {
  35. const te = this.elements;
  36. const w = 1.0 / ( right - left );
  37. const h = 1.0 / ( top - bottom );
  38. const p = 1.0 / ( far - near );
  39. const x = ( right + left ) * w;
  40. const y = ( top + bottom ) * h;
  41. const z = near * p;
  42. te[ 0 ] = 2 * w; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = - x;
  43. te[ 1 ] = 0; te[ 5 ] = 2 * h; te[ 9 ] = 0; te[ 13 ] = - y;
  44. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = - 1 * p; te[ 14 ] = - z;
  45. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1;
  46. return this;
  47. };
  48. Frustum.prototype.setFromProjectionMatrix = function ( m ) {
  49. const planes = this.planes;
  50. const me = m.elements;
  51. const me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ];
  52. const me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ];
  53. const me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ];
  54. const me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ];
  55. planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize();
  56. planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize();
  57. planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize();
  58. planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize();
  59. planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize();
  60. planes[ 5 ].setComponents( me2, me6, me10, me14 ).normalize();
  61. return this;
  62. };
  63. _initializedHack = true;
  64. }
  65. _initWebGPUHack();
  66. //
  67. class WebGPUBackend extends Backend {
  68. constructor( parameters = {} ) {
  69. super( parameters );
  70. // some parameters require default values other than "undefined"
  71. this.parameters.antialias = ( parameters.antialias === true );
  72. if ( this.parameters.antialias === true ) {
  73. this.parameters.sampleCount = ( parameters.sampleCount === undefined ) ? 4 : parameters.sampleCount;
  74. } else {
  75. this.parameters.sampleCount = 1;
  76. }
  77. this.parameters.requiredLimits = ( parameters.requiredLimits === undefined ) ? {} : parameters.requiredLimits;
  78. this.adapter = null;
  79. this.device = null;
  80. this.context = null;
  81. this.colorBuffer = null;
  82. this.depthBuffer = null;
  83. this.utils = new WebGPUUtils( this );
  84. this.attributeUtils = new WebGPUAttributeUtils( this );
  85. this.bindingUtils = new WebGPUBindingUtils( this );
  86. this.pipelineUtils = new WebGPUPipelineUtils( this );
  87. this.textureUtils = new WebGPUTextureUtils( this );
  88. }
  89. async init( renderer ) {
  90. await super.init( renderer );
  91. //
  92. const parameters = this.parameters;
  93. const adapterOptions = {
  94. powerPreference: parameters.powerPreference
  95. };
  96. const adapter = await navigator.gpu.requestAdapter( adapterOptions );
  97. if ( adapter === null ) {
  98. throw new Error( 'WebGPUBackend: Unable to create WebGPU adapter.' );
  99. }
  100. // feature support
  101. const features = Object.values( GPUFeatureName );
  102. const supportedFeatures = [];
  103. for ( const name of features ) {
  104. if ( adapter.features.has( name ) ) {
  105. supportedFeatures.push( name );
  106. }
  107. }
  108. const deviceDescriptor = {
  109. requiredFeatures: supportedFeatures,
  110. requiredLimits: parameters.requiredLimits
  111. };
  112. const device = await adapter.requestDevice( deviceDescriptor );
  113. const context = ( parameters.context !== undefined ) ? parameters.context : renderer.domElement.getContext( 'webgpu' );
  114. this.adapter = adapter;
  115. this.device = device;
  116. this.context = context;
  117. this.updateSize();
  118. }
  119. async getArrayBuffer( attribute ) {
  120. return await this.attributeUtils.getArrayBuffer( attribute );
  121. }
  122. beginRender( renderContext ) {
  123. const renderContextData = this.get( renderContext );
  124. const device = this.device;
  125. const descriptor = {
  126. colorAttachments: [ {
  127. view: null
  128. } ],
  129. depthStencilAttachment: {
  130. view: null
  131. }
  132. };
  133. const colorAttachment = descriptor.colorAttachments[ 0 ];
  134. const depthStencilAttachment = descriptor.depthStencilAttachment;
  135. const antialias = this.parameters.antialias;
  136. if ( renderContext.texture !== null ) {
  137. const textureData = this.get( renderContext.texture );
  138. const depthTextureData = this.get( renderContext.depthTexture );
  139. // @TODO: Support RenderTarget with antialiasing.
  140. colorAttachment.view = textureData.texture.createView();
  141. depthStencilAttachment.view = depthTextureData.texture.createView();
  142. if ( renderContext.stencil && renderContext.depthTexture.format === DepthFormat ) {
  143. renderContext.stencil = false;
  144. }
  145. } else {
  146. if ( antialias === true ) {
  147. colorAttachment.view = this.colorBuffer.createView();
  148. colorAttachment.resolveTarget = this.context.getCurrentTexture().createView();
  149. } else {
  150. colorAttachment.view = this.context.getCurrentTexture().createView();
  151. colorAttachment.resolveTarget = undefined;
  152. }
  153. depthStencilAttachment.view = this.depthBuffer.createView();
  154. }
  155. if ( renderContext.clearColor ) {
  156. colorAttachment.clearValue = renderContext.clearColorValue;
  157. colorAttachment.loadOp = GPULoadOp.Clear;
  158. colorAttachment.storeOp = GPUStoreOp.Store;
  159. } else {
  160. colorAttachment.loadOp = GPULoadOp.Load;
  161. colorAttachment.storeOp = GPUStoreOp.Store;
  162. }
  163. //
  164. if ( renderContext.depth ) {
  165. if ( renderContext.clearDepth ) {
  166. depthStencilAttachment.depthClearValue = renderContext.clearDepthValue;
  167. depthStencilAttachment.depthLoadOp = GPULoadOp.Clear;
  168. depthStencilAttachment.depthStoreOp = GPUStoreOp.Store;
  169. } else {
  170. depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
  171. depthStencilAttachment.depthStoreOp = GPUStoreOp.Store;
  172. }
  173. }
  174. if ( renderContext.stencil ) {
  175. if ( renderContext.clearStencil ) {
  176. depthStencilAttachment.stencilClearValue = renderContext.clearStencilValue;
  177. depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear;
  178. depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store;
  179. } else {
  180. depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
  181. depthStencilAttachment.stencilStoreOp = GPUStoreOp.Store;
  182. }
  183. }
  184. //
  185. const encoder = device.createCommandEncoder( { label: 'renderContext_' + renderContext.id } );
  186. const currentPass = encoder.beginRenderPass( descriptor );
  187. //
  188. renderContextData.descriptor = descriptor;
  189. renderContextData.encoder = encoder;
  190. renderContextData.currentPass = currentPass;
  191. //
  192. if ( renderContext.viewport ) {
  193. this.updateViewport( renderContext );
  194. }
  195. if ( renderContext.scissor ) {
  196. const { x, y, width, height } = renderContext.scissorValue;
  197. currentPass.setScissorRect( x, y, width, height );
  198. }
  199. }
  200. finishRender( renderContext ) {
  201. const renderContextData = this.get( renderContext );
  202. renderContextData.currentPass.end();
  203. this.device.queue.submit( [ renderContextData.encoder.finish() ] );
  204. }
  205. updateViewport( renderContext ) {
  206. const { currentPass } = this.get( renderContext );
  207. const { x, y, width, height, minDepth, maxDepth } = renderContext.viewportValue;
  208. currentPass.setViewport( x, y, width, height, minDepth, maxDepth );
  209. }
  210. clear( renderContext, color, depth, stencil ) {
  211. const device = this.device;
  212. const renderContextData = this.get( renderContext );
  213. const { descriptor } = renderContextData;
  214. depth = depth && renderContext.depth;
  215. stencil = stencil && renderContext.stencil;
  216. const colorAttachment = descriptor.colorAttachments[ 0 ];
  217. const antialias = this.parameters.antialias;
  218. // @TODO: Include render target in clear operation.
  219. if ( antialias === true ) {
  220. colorAttachment.view = this.colorBuffer.createView();
  221. colorAttachment.resolveTarget = this.context.getCurrentTexture().createView();
  222. } else {
  223. colorAttachment.view = this.context.getCurrentTexture().createView();
  224. colorAttachment.resolveTarget = undefined;
  225. }
  226. descriptor.depthStencilAttachment.view = this.depthBuffer.createView();
  227. if ( color ) {
  228. colorAttachment.loadOp = GPULoadOp.Clear;
  229. colorAttachment.clearValue = renderContext.clearColorValue;
  230. }
  231. if ( depth ) {
  232. descriptor.depthStencilAttachment.depthLoadOp = GPULoadOp.Clear;
  233. descriptor.depthStencilAttachment.depthClearValue = renderContext.clearDepthValue;
  234. }
  235. if ( stencil ) {
  236. descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Clear;
  237. descriptor.depthStencilAttachment.stencilClearValue = renderContext.clearStencilValue;
  238. }
  239. renderContextData.encoder = device.createCommandEncoder( {} );
  240. renderContextData.currentPass = renderContextData.encoder.beginRenderPass( descriptor );
  241. renderContextData.currentPass.end();
  242. device.queue.submit( [ renderContextData.encoder.finish() ] );
  243. }
  244. // compute
  245. beginCompute( computeGroup ) {
  246. const groupGPU = this.get( computeGroup );
  247. groupGPU.cmdEncoderGPU = this.device.createCommandEncoder( {} );
  248. groupGPU.passEncoderGPU = groupGPU.cmdEncoderGPU.beginComputePass();
  249. }
  250. compute( computeGroup, computeNode, bindings, pipeline ) {
  251. const { passEncoderGPU } = this.get( computeGroup );
  252. // pipeline
  253. const pipelineGPU = this.get( pipeline ).pipeline;
  254. passEncoderGPU.setPipeline( pipelineGPU );
  255. // bind group
  256. const bindGroupGPU = this.get( bindings ).group;
  257. passEncoderGPU.setBindGroup( 0, bindGroupGPU );
  258. passEncoderGPU.dispatchWorkgroups( computeNode.dispatchCount );
  259. }
  260. finishCompute( computeGroup ) {
  261. const groupData = this.get( computeGroup );
  262. groupData.passEncoderGPU.end();
  263. this.device.queue.submit( [ groupData.cmdEncoderGPU.finish() ] );
  264. }
  265. // render object
  266. draw( renderObject, info ) {
  267. const { object, geometry, context, pipeline } = renderObject;
  268. const bindingsData = this.get( renderObject.getBindings() );
  269. const contextData = this.get( context );
  270. const pipelineGPU = this.get( pipeline ).pipeline;
  271. // pipeline
  272. const passEncoderGPU = contextData.currentPass;
  273. passEncoderGPU.setPipeline( pipelineGPU );
  274. // bind group
  275. const bindGroupGPU = bindingsData.group;
  276. passEncoderGPU.setBindGroup( 0, bindGroupGPU );
  277. // index
  278. const index = renderObject.getIndex();
  279. const hasIndex = ( index !== null );
  280. if ( hasIndex === true ) {
  281. const buffer = this.get( index ).buffer;
  282. const indexFormat = ( index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32;
  283. passEncoderGPU.setIndexBuffer( buffer, indexFormat );
  284. }
  285. // vertex buffers
  286. const attributes = renderObject.getAttributes();
  287. for ( let i = 0, l = attributes.length; i < l; i ++ ) {
  288. const buffer = this.get( attributes[ i ] ).buffer;
  289. passEncoderGPU.setVertexBuffer( i, buffer );
  290. }
  291. // draw
  292. const drawRange = geometry.drawRange;
  293. const firstVertex = drawRange.start;
  294. const instanceCount = this.getInstanceCount( renderObject );
  295. if ( hasIndex === true ) {
  296. const indexCount = ( drawRange.count !== Infinity ) ? drawRange.count : index.count;
  297. passEncoderGPU.drawIndexed( indexCount, instanceCount, firstVertex, 0, 0 );
  298. info.update( object, indexCount, instanceCount );
  299. } else {
  300. const positionAttribute = geometry.attributes.position;
  301. const vertexCount = ( drawRange.count !== Infinity ) ? drawRange.count : positionAttribute.count;
  302. passEncoderGPU.draw( vertexCount, instanceCount, firstVertex, 0 );
  303. info.update( object, vertexCount, instanceCount );
  304. }
  305. }
  306. // cache key
  307. needsUpdate( renderObject ) {
  308. const renderObjectGPU = this.get( renderObject );
  309. const { object, material } = renderObject;
  310. const utils = this.utils;
  311. const sampleCount = utils.getSampleCount();
  312. const colorSpace = utils.getCurrentColorSpace( renderObject.context );
  313. const colorFormat = utils.getCurrentColorFormat( renderObject.context );
  314. const depthStencilFormat = utils.getCurrentDepthStencilFormat( renderObject.context );
  315. const primitiveTopology = utils.getPrimitiveTopology( object, material );
  316. let needsUpdate = false;
  317. if ( renderObjectGPU.sampleCount !== sampleCount || renderObjectGPU.colorSpace !== colorSpace ||
  318. renderObjectGPU.colorFormat !== colorFormat || renderObjectGPU.depthStencilFormat !== depthStencilFormat ||
  319. renderObjectGPU.primitiveTopology !== primitiveTopology ) {
  320. renderObjectGPU.sampleCount = sampleCount;
  321. renderObjectGPU.colorSpace = colorSpace;
  322. renderObjectGPU.colorFormat = colorFormat;
  323. renderObjectGPU.depthStencilFormat = depthStencilFormat;
  324. renderObjectGPU.primitiveTopology = primitiveTopology;
  325. needsUpdate = true;
  326. }
  327. return needsUpdate;
  328. }
  329. getCacheKey( renderObject ) {
  330. const { object, material } = renderObject;
  331. const utils = this.utils;
  332. const renderContext = renderObject.context;
  333. return [
  334. utils.getSampleCount(),
  335. utils.getCurrentColorSpace( renderContext ), utils.getCurrentColorFormat( renderContext ), utils.getCurrentDepthStencilFormat( renderContext ),
  336. utils.getPrimitiveTopology( object, material )
  337. ].join();
  338. }
  339. // textures
  340. createSampler( texture ) {
  341. this.textureUtils.createSampler( texture );
  342. }
  343. destroySampler( texture ) {
  344. this.textureUtils.destroySampler( texture );
  345. }
  346. createDefaultTexture( texture ) {
  347. this.textureUtils.createDefaultTexture( texture );
  348. }
  349. createTexture( texture ) {
  350. this.textureUtils.createTexture( texture );
  351. }
  352. updateTexture( texture ) {
  353. this.textureUtils.updateTexture( texture );
  354. }
  355. destroyTexture( texture ) {
  356. this.textureUtils.destroyTexture( texture );
  357. }
  358. // node builder
  359. createNodeBuilder( object, renderer ) {
  360. return new WebGPUNodeBuilder( object, renderer );
  361. }
  362. // program
  363. createProgram( program ) {
  364. const programGPU = this.get( program );
  365. programGPU.module = {
  366. module: this.device.createShaderModule( { code: program.code, label: program.stage } ),
  367. entryPoint: 'main'
  368. };
  369. }
  370. destroyProgram( program ) {
  371. this.delete( program );
  372. }
  373. // pipelines
  374. createRenderPipeline( renderObject ) {
  375. this.pipelineUtils.createRenderPipeline( renderObject );
  376. }
  377. createComputePipeline( computePipeline ) {
  378. this.pipelineUtils.createComputePipeline( computePipeline );
  379. }
  380. // bindings
  381. createBindings( bindings, pipeline ) {
  382. this.bindingUtils.createBindings( bindings, pipeline );
  383. }
  384. updateBindings( bindings, pipeline ) {
  385. this.bindingUtils.createBindings( bindings, pipeline );
  386. }
  387. updateBinding( binding ) {
  388. this.bindingUtils.updateBinding( binding );
  389. }
  390. // attributes
  391. createIndexAttribute( attribute ) {
  392. this.attributeUtils.createAttribute( attribute, GPUBufferUsage.INDEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST );
  393. }
  394. createAttribute( attribute ) {
  395. this.attributeUtils.createAttribute( attribute, GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST );
  396. }
  397. createStorageAttribute( attribute ) {
  398. this.attributeUtils.createAttribute( attribute, GPUBufferUsage.STORAGE | GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_SRC | GPUBufferUsage.COPY_DST );
  399. }
  400. updateAttribute( attribute ) {
  401. this.attributeUtils.updateAttribute( attribute );
  402. }
  403. destroyAttribute( attribute ) {
  404. this.attributeUtils.destroyAttribute( attribute );
  405. }
  406. // canvas
  407. updateSize() {
  408. this._configureContext();
  409. this._setupColorBuffer();
  410. this._setupDepthBuffer();
  411. }
  412. // utils public
  413. hasFeature( name ) {
  414. const adapter = this.adapter || _staticAdapter;
  415. //
  416. const features = Object.values( GPUFeatureName );
  417. if ( features.includes( name ) === false ) {
  418. throw new Error( 'THREE.WebGPURenderer: Unknown WebGPU GPU feature: ' + name );
  419. }
  420. //
  421. return adapter.features.has( name );
  422. }
  423. copyFramebufferToTexture( framebufferTexture, renderContext ) {
  424. const renderContextData = this.get( renderContext );
  425. const { encoder, descriptor } = renderContextData;
  426. const sourceGPU = this.context.getCurrentTexture();
  427. const destinationGPU = this.get( framebufferTexture ).texture;
  428. renderContextData.currentPass.end();
  429. encoder.copyTextureToTexture(
  430. {
  431. texture: sourceGPU
  432. },
  433. {
  434. texture: destinationGPU
  435. },
  436. [
  437. framebufferTexture.image.width,
  438. framebufferTexture.image.height
  439. ]
  440. );
  441. descriptor.colorAttachments[ 0 ].loadOp = GPULoadOp.Load;
  442. if ( renderContext.depth ) descriptor.depthStencilAttachment.depthLoadOp = GPULoadOp.Load;
  443. if ( renderContext.stencil ) descriptor.depthStencilAttachment.stencilLoadOp = GPULoadOp.Load;
  444. renderContextData.currentPass = encoder.beginRenderPass( descriptor );
  445. }
  446. // utils
  447. _configureContext() {
  448. this.context.configure( {
  449. device: this.device,
  450. format: GPUTextureFormat.BGRA8Unorm,
  451. usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC,
  452. alphaMode: 'premultiplied'
  453. } );
  454. }
  455. _setupColorBuffer() {
  456. if ( this.colorBuffer ) this.colorBuffer.destroy();
  457. const { width, height } = this.getDrawingBufferSize();
  458. //const format = navigator.gpu.getPreferredCanvasFormat(); // @TODO: Move to WebGPUUtils
  459. this.colorBuffer = this.device.createTexture( {
  460. label: 'colorBuffer',
  461. size: {
  462. width: width,
  463. height: height,
  464. depthOrArrayLayers: 1
  465. },
  466. sampleCount: this.parameters.sampleCount,
  467. format: GPUTextureFormat.BGRA8Unorm,
  468. usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC
  469. } );
  470. }
  471. _setupDepthBuffer() {
  472. if ( this.depthBuffer ) this.depthBuffer.destroy();
  473. const { width, height } = this.getDrawingBufferSize();
  474. this.depthBuffer = this.device.createTexture( {
  475. label: 'depthBuffer',
  476. size: {
  477. width: width,
  478. height: height,
  479. depthOrArrayLayers: 1
  480. },
  481. sampleCount: this.parameters.sampleCount,
  482. format: GPUTextureFormat.Depth24PlusStencil8,
  483. usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.COPY_SRC
  484. } );
  485. }
  486. }
  487. export default WebGPUBackend;