WebGPUBackend.js 19 KB

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