WebGPURenderPipelines.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. import { GPUPrimitiveTopology, GPUIndexFormat, GPUTextureFormat, GPUCompareFunction, GPUFrontFace, GPUCullMode, GPUVertexFormat, GPUBlendFactor, GPUBlendOperation, BlendColorFactor, OneMinusBlendColorFactor, GPUColorWriteFlags, GPUStencilOperation } from './constants.js';
  2. import {
  3. FrontSide, BackSide, DoubleSide,
  4. NeverDepth, AlwaysDepth, LessDepth, LessEqualDepth, EqualDepth, GreaterEqualDepth, GreaterDepth, NotEqualDepth,
  5. NeverStencilFunc, AlwaysStencilFunc, LessStencilFunc, LessEqualStencilFunc, EqualStencilFunc, GreaterEqualStencilFunc, GreaterStencilFunc, NotEqualStencilFunc,
  6. KeepStencilOp, ZeroStencilOp, ReplaceStencilOp, InvertStencilOp, IncrementStencilOp, DecrementStencilOp, IncrementWrapStencilOp, DecrementWrapStencilOp,
  7. NoBlending, NormalBlending, AdditiveBlending, SubtractiveBlending, MultiplyBlending, CustomBlending,
  8. AddEquation, SubtractEquation, ReverseSubtractEquation, MinEquation, MaxEquation,
  9. ZeroFactor, OneFactor, SrcColorFactor, OneMinusSrcColorFactor, SrcAlphaFactor, OneMinusSrcAlphaFactor, DstAlphaFactor, OneMinusDstAlphaFactor, DstColorFactor, OneMinusDstColorFactor, SrcAlphaSaturateFactor
  10. } from '../../../../build/three.module.js';
  11. class WebGPURenderPipelines {
  12. constructor( renderer, properties, device, glslang, sampleCount ) {
  13. this.renderer = renderer;
  14. this.properties = properties;
  15. this.device = device;
  16. this.glslang = glslang;
  17. this.sampleCount = sampleCount;
  18. this.pipelines = new WeakMap();
  19. this.shaderAttributes = new WeakMap();
  20. this.shaderModules = {
  21. vertex: new WeakMap(),
  22. fragment: new WeakMap()
  23. };
  24. }
  25. get( object ) {
  26. let pipeline = this.pipelines.get( object );
  27. if ( pipeline === undefined ) {
  28. const device = this.device;
  29. const material = object.material;
  30. // shader source
  31. let shader;
  32. if ( material.isMeshBasicMaterial ) {
  33. shader = ShaderLib.mesh_basic;
  34. } else if ( material.isPointsMaterial ) {
  35. shader = ShaderLib.points_basic;
  36. } else if ( material.isLineBasicMaterial ) {
  37. shader = ShaderLib.line_basic;
  38. } else {
  39. console.error( 'THREE.WebGPURenderer: Unknwon shader type.' );
  40. }
  41. // shader modules
  42. const glslang = this.glslang;
  43. let moduleVertex = this.shaderModules.vertex.get( shader );
  44. if ( moduleVertex === undefined ) {
  45. const byteCodeVertex = glslang.compileGLSL( shader.vertexShader, 'vertex' );
  46. moduleVertex = {
  47. module: device.createShaderModule( { code: byteCodeVertex } ),
  48. entryPoint: 'main'
  49. };
  50. this.shaderModules.vertex.set( shader, moduleVertex );
  51. }
  52. let moduleFragment = this.shaderModules.fragment.get( shader );
  53. if ( moduleFragment === undefined ) {
  54. const byteCodeFragment = glslang.compileGLSL( shader.fragmentShader, 'fragment' );
  55. moduleFragment = {
  56. module: device.createShaderModule( { code: byteCodeFragment } ),
  57. entryPoint: 'main'
  58. };
  59. this.shaderModules.fragment.set( shader, moduleFragment );
  60. }
  61. // determine shader attributes
  62. const shaderAttributes = this._parseShaderAttributes( shader.vertexShader );
  63. // vertex buffers
  64. const vertexBuffers = [];
  65. for ( const attribute of shaderAttributes ) {
  66. vertexBuffers.push( {
  67. arrayStride: attribute.arrayStride,
  68. attributes: [ { shaderLocation: attribute.slot, offset: 0, format: attribute.format } ]
  69. } );
  70. }
  71. //
  72. const geometry = object.geometry;
  73. let indexFormat;
  74. if ( object.isLine ) {
  75. const count = ( geometry.index ) ? geometry.index.count : geometry.attributes.position.count;
  76. indexFormat = ( count > 65535 ) ? GPUIndexFormat.Uint32 : GPUIndexFormat.Uint16; // define data type for primitive restart value
  77. }
  78. //
  79. let alphaBlend = {};
  80. let colorBlend = {};
  81. if ( material.transparent === true && material.blending !== NoBlending ) {
  82. alphaBlend = this._getAlphaBlend( material );
  83. colorBlend = this._getColorBlend( material );
  84. }
  85. //
  86. let stencilFront = {};
  87. if ( material.stencilWrite === true ) {
  88. stencilFront = {
  89. compare: this._getStencilCompare( material ),
  90. failOp: this._getStencilOperation( material.stencilFail ),
  91. depthFailOp: this._getStencilOperation( material.stencilZFail ),
  92. passOp: this._getStencilOperation( material.stencilZPass )
  93. };
  94. }
  95. // pipeline
  96. const primitiveTopology = this._getPrimitiveTopology( object );
  97. const rasterizationState = this._getRasterizationStateDescriptor( material );
  98. const colorWriteMask = this._getColorWriteMask( material );
  99. const depthCompare = this._getDepthCompare( material );
  100. const colorFormat = this._getColorFormat( this.renderer );
  101. const depthStencilFormat = this._getDepthStencilFormat( this.renderer );
  102. pipeline = device.createRenderPipeline( {
  103. vertexStage: moduleVertex,
  104. fragmentStage: moduleFragment,
  105. primitiveTopology: primitiveTopology,
  106. rasterizationState: rasterizationState,
  107. colorStates: [ {
  108. format: colorFormat,
  109. alphaBlend: alphaBlend,
  110. colorBlend: colorBlend,
  111. writeMask: colorWriteMask
  112. } ],
  113. depthStencilState: {
  114. format: depthStencilFormat,
  115. depthWriteEnabled: material.depthWrite,
  116. depthCompare: depthCompare,
  117. stencilFront: stencilFront,
  118. stencilBack: {}, // three.js does not provide an API to configure the back function (gl.stencilFuncSeparate() was never used)
  119. stencilReadMask: material.stencilFuncMask,
  120. stencilWriteMask: material.stencilWriteMask
  121. },
  122. vertexState: {
  123. indexFormat: indexFormat,
  124. vertexBuffers: vertexBuffers
  125. },
  126. sampleCount: this.sampleCount
  127. } );
  128. this.pipelines.set( object, pipeline );
  129. this.shaderAttributes.set( pipeline, shaderAttributes );
  130. }
  131. return pipeline;
  132. }
  133. getShaderAttributes( pipeline ) {
  134. return this.shaderAttributes.get( pipeline );
  135. }
  136. dispose() {
  137. this.pipelines = new WeakMap();
  138. this.shaderAttributes = new WeakMap();
  139. this.shaderModules = {
  140. vertex: new WeakMap(),
  141. fragment: new WeakMap()
  142. };
  143. }
  144. _getArrayStride( type ) {
  145. // @TODO: This code is GLSL specific. We need to update when we switch to WGSL.
  146. if ( type === 'float' ) return 4;
  147. if ( type === 'vec2' ) return 8;
  148. if ( type === 'vec3' ) return 12;
  149. if ( type === 'vec4' ) return 16;
  150. if ( type === 'int' ) return 4;
  151. if ( type === 'ivec2' ) return 8;
  152. if ( type === 'ivec3' ) return 12;
  153. if ( type === 'ivec4' ) return 16;
  154. if ( type === 'uint' ) return 4;
  155. if ( type === 'uvec2' ) return 8;
  156. if ( type === 'uvec3' ) return 12;
  157. if ( type === 'uvec4' ) return 16;
  158. console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.', type );
  159. }
  160. _getAlphaBlend( material ) {
  161. const blending = material.blending;
  162. const premultipliedAlpha = material.premultipliedAlpha;
  163. let alphaBlend = undefined;
  164. switch ( blending ) {
  165. case NormalBlending:
  166. if ( premultipliedAlpha === false ) {
  167. alphaBlend = {
  168. srcFactor: GPUBlendFactor.One,
  169. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  170. operation: GPUBlendOperation.Add
  171. };
  172. }
  173. break;
  174. case AdditiveBlending:
  175. // no alphaBlend settings
  176. break;
  177. case SubtractiveBlending:
  178. if ( premultipliedAlpha === true ) {
  179. alphaBlend = {
  180. srcFactor: GPUBlendFactor.OneMinusSrcColor,
  181. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  182. operation: GPUBlendOperation.Add
  183. };
  184. }
  185. break;
  186. case MultiplyBlending:
  187. if ( premultipliedAlpha === true ) {
  188. alphaBlend = {
  189. srcFactor: GPUBlendFactor.Zero,
  190. dstFactor: GPUBlendFactor.SrcAlpha,
  191. operation: GPUBlendOperation.Add
  192. };
  193. }
  194. break;
  195. case CustomBlending:
  196. const blendSrcAlpha = material.blendSrcAlpha;
  197. const blendDstAlpha = material.blendDstAlpha;
  198. const blendEquationAlpha = material.blendEquationAlpha;
  199. if ( blendSrcAlpha !== null && blendDstAlpha !== null && blendEquationAlpha !== null ) {
  200. alphaBlend = {
  201. srcFactor: this._getBlendFactor( blendSrcAlpha ),
  202. dstFactor: this._getBlendFactor( blendDstAlpha ),
  203. operation: this._getBlendOperation( blendEquationAlpha )
  204. };
  205. }
  206. break;
  207. default:
  208. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  209. }
  210. return alphaBlend;
  211. }
  212. _getBlendFactor( blend ) {
  213. let blendFactor;
  214. switch ( blend ) {
  215. case ZeroFactor:
  216. blendFactor = GPUBlendFactor.Zero;
  217. break;
  218. case OneFactor:
  219. blendFactor = GPUBlendFactor.One;
  220. break;
  221. case SrcColorFactor:
  222. blendFactor = GPUBlendFactor.SrcColor;
  223. break;
  224. case OneMinusSrcColorFactor:
  225. blendFactor = GPUBlendFactor.OneMinusSrcColor;
  226. break;
  227. case SrcAlphaFactor:
  228. blendFactor = GPUBlendFactor.SrcAlpha;
  229. break;
  230. case OneMinusSrcAlphaFactor:
  231. blendFactor = GPUBlendFactor.OneMinusSrcAlpha;
  232. break;
  233. case DstColorFactor:
  234. blendFactor = GPUBlendFactor.DstColor;
  235. break;
  236. case OneMinusDstColorFactor:
  237. blendFactor = GPUBlendFactor.OneMinusDstColor;
  238. break;
  239. case DstAlphaFactor:
  240. blendFactor = GPUBlendFactor.DstAlpha;
  241. break;
  242. case OneMinusDstAlphaFactor:
  243. blendFactor = GPUBlendFactor.OneMinusDstAlpha;
  244. break;
  245. case SrcAlphaSaturateFactor:
  246. blendFactor = GPUBlendFactor.SrcAlphaSaturated;
  247. break;
  248. case BlendColorFactor:
  249. blendFactor = GPUBlendFactor.BlendColor;
  250. break;
  251. case OneMinusBlendColorFactor:
  252. blendFactor = GPUBlendFactor.OneMinusBlendColor;
  253. break;
  254. default:
  255. console.error( 'THREE.WebGPURenderer: Blend factor not supported.', blend );
  256. }
  257. return blendFactor;
  258. }
  259. _getBlendOperation( blendEquation ) {
  260. let blendOperation;
  261. switch ( blendEquation ) {
  262. case AddEquation:
  263. blendOperation = GPUBlendOperation.Add;
  264. break;
  265. case SubtractEquation:
  266. blendOperation = GPUBlendOperation.Subtract;
  267. break;
  268. case ReverseSubtractEquation:
  269. blendOperation = GPUBlendOperation.ReverseSubtract;
  270. break;
  271. case MinEquation:
  272. blendOperation = GPUBlendOperation.Min;
  273. break;
  274. case MaxEquation:
  275. blendOperation = GPUBlendOperation.Max;
  276. break;
  277. default:
  278. console.error( 'THREE.WebGPURenderer: Blend equation not supported.', blendEquation );
  279. }
  280. return blendOperation;
  281. }
  282. _getColorBlend( material ) {
  283. const blending = material.blending;
  284. const premultipliedAlpha = material.premultipliedAlpha;
  285. const colorBlend = {
  286. srcFactor: null,
  287. dstFactor: null,
  288. operation: null
  289. };
  290. switch ( blending ) {
  291. case NormalBlending:
  292. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  293. colorBlend.dstFactor = GPUBlendFactor.OneMinusSrcAlpha;
  294. colorBlend.operation = GPUBlendOperation.Add;
  295. break;
  296. case AdditiveBlending:
  297. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  298. colorBlend.operation = GPUBlendOperation.Add;
  299. break;
  300. case SubtractiveBlending:
  301. colorBlend.srcFactor = GPUBlendFactor.Zero;
  302. colorBlend.dstFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.Zero : GPUBlendFactor.OneMinusSrcColor;
  303. colorBlend.operation = GPUBlendOperation.Add;
  304. break;
  305. case MultiplyBlending:
  306. colorBlend.srcFactor = GPUBlendFactor.Zero;
  307. colorBlend.dstFactor = GPUBlendFactor.SrcColor;
  308. colorBlend.operation = GPUBlendOperation.Add;
  309. break;
  310. case CustomBlending:
  311. colorBlend.srcFactor = this._getBlendFactor( material.blendSrc );
  312. colorBlend.dstFactor = this._getBlendFactor( material.blendDst );
  313. colorBlend.operation = this._getBlendOperation( material.blendEquation );
  314. break;
  315. default:
  316. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  317. }
  318. return colorBlend;
  319. }
  320. _getColorFormat( renderer ) {
  321. let format;
  322. const renderTarget = renderer.getRenderTarget();
  323. if ( renderTarget !== null ) {
  324. const renderTargetProperties = this.properties.get( renderTarget );
  325. format = renderTargetProperties.colorTextureFormat;
  326. } else {
  327. format = GPUTextureFormat.BRGA8Unorm; // default swap chain format
  328. }
  329. return format;
  330. }
  331. _getColorWriteMask( material ) {
  332. return ( material.colorWrite === true ) ? GPUColorWriteFlags.All : GPUColorWriteFlags.None;
  333. }
  334. _getDepthCompare( material ) {
  335. let depthCompare;
  336. if ( material.depthTest === false ) {
  337. depthCompare = GPUCompareFunction.Always;
  338. } else {
  339. const depthFunc = material.depthFunc;
  340. switch ( depthFunc ) {
  341. case NeverDepth:
  342. depthCompare = GPUCompareFunction.Never;
  343. break;
  344. case AlwaysDepth:
  345. depthCompare = GPUCompareFunction.Always;
  346. break;
  347. case LessDepth:
  348. depthCompare = GPUCompareFunction.Less;
  349. break;
  350. case LessEqualDepth:
  351. depthCompare = GPUCompareFunction.LessEqual;
  352. break;
  353. case EqualDepth:
  354. depthCompare = GPUCompareFunction.Equal;
  355. break;
  356. case GreaterEqualDepth:
  357. depthCompare = GPUCompareFunction.GreaterEqual;
  358. break;
  359. case GreaterDepth:
  360. depthCompare = GPUCompareFunction.Greater;
  361. break;
  362. case NotEqualDepth:
  363. depthCompare = GPUCompareFunction.NotEqual;
  364. break;
  365. default:
  366. console.error( 'THREE.WebGPURenderer: Invalid depth function.', depthFunc );
  367. }
  368. }
  369. return depthCompare;
  370. }
  371. _getDepthStencilFormat( renderer ) {
  372. let format;
  373. const renderTarget = renderer.getRenderTarget();
  374. if ( renderTarget !== null ) {
  375. const renderTargetProperties = this.properties.get( renderTarget );
  376. format = renderTargetProperties.depthTextureFormat;
  377. } else {
  378. format = GPUTextureFormat.Depth24PlusStencil8;
  379. }
  380. return format;
  381. }
  382. _getPrimitiveTopology( object ) {
  383. if ( object.isMesh ) return GPUPrimitiveTopology.TriangleList;
  384. else if ( object.isPoints ) return GPUPrimitiveTopology.PointList;
  385. else if ( object.isLine ) return GPUPrimitiveTopology.LineStrip;
  386. else if ( object.isLineSegments ) return GPUPrimitiveTopology.LineList;
  387. }
  388. _getRasterizationStateDescriptor( material ) {
  389. const descriptor = {};
  390. switch ( material.side ) {
  391. case FrontSide:
  392. descriptor.frontFace = GPUFrontFace.CCW;
  393. descriptor.cullMode = GPUCullMode.Back;
  394. break;
  395. case BackSide:
  396. descriptor.frontFace = GPUFrontFace.CW;
  397. descriptor.cullMode = GPUCullMode.Back;
  398. break;
  399. case DoubleSide:
  400. descriptor.frontFace = GPUFrontFace.CCW;
  401. descriptor.cullMode = GPUCullMode.None;
  402. break;
  403. default:
  404. console.error( 'THREE.WebGPURenderer: Unknown Material.side value.', material.side );
  405. break;
  406. }
  407. return descriptor;
  408. }
  409. _getStencilCompare( material ) {
  410. let stencilCompare;
  411. const stencilFunc = material.stencilFunc;
  412. switch ( stencilFunc ) {
  413. case NeverStencilFunc:
  414. stencilCompare = GPUCompareFunction.Never;
  415. break;
  416. case AlwaysStencilFunc:
  417. stencilCompare = GPUCompareFunction.Always;
  418. break;
  419. case LessStencilFunc:
  420. stencilCompare = GPUCompareFunction.Less;
  421. break;
  422. case LessEqualStencilFunc:
  423. stencilCompare = GPUCompareFunction.LessEqual;
  424. break;
  425. case EqualStencilFunc:
  426. stencilCompare = GPUCompareFunction.Equal;
  427. break;
  428. case GreaterEqualStencilFunc:
  429. stencilCompare = GPUCompareFunction.GreaterEqual;
  430. break;
  431. case GreaterStencilFunc:
  432. stencilCompare = GPUCompareFunction.Greater;
  433. break;
  434. case NotEqualStencilFunc:
  435. stencilCompare = GPUCompareFunction.NotEqual;
  436. break;
  437. default:
  438. console.error( 'THREE.WebGPURenderer: Invalid stencil function.', stencilFunc );
  439. }
  440. return stencilCompare;
  441. }
  442. _getStencilOperation( op ) {
  443. let stencilOperation;
  444. switch ( op ) {
  445. case KeepStencilOp:
  446. stencilOperation = GPUStencilOperation.Keep;
  447. break;
  448. case ZeroStencilOp:
  449. stencilOperation = GPUStencilOperation.Zero;
  450. break;
  451. case ReplaceStencilOp:
  452. stencilOperation = GPUStencilOperation.Replace;
  453. break;
  454. case InvertStencilOp:
  455. stencilOperation = GPUStencilOperation.Invert;
  456. break;
  457. case IncrementStencilOp:
  458. stencilOperation = GPUStencilOperation.IncrementClamp;
  459. break;
  460. case DecrementStencilOp:
  461. stencilOperation = GPUStencilOperation.DecrementClamp;
  462. break;
  463. case IncrementWrapStencilOp:
  464. stencilOperation = GPUStencilOperation.IncrementWrap;
  465. break;
  466. case DecrementWrapStencilOp:
  467. stencilOperation = GPUStencilOperation.DecrementWrap;
  468. break;
  469. default:
  470. console.error( 'THREE.WebGPURenderer: Invalid stencil operation.', stencilOperation );
  471. }
  472. return stencilOperation;
  473. }
  474. _getVertexFormat( type ) {
  475. // @TODO: This code is GLSL specific. We need to update when we switch to WGSL.
  476. if ( type === 'float' ) return GPUVertexFormat.Float;
  477. if ( type === 'vec2' ) return GPUVertexFormat.Float2;
  478. if ( type === 'vec3' ) return GPUVertexFormat.Float3;
  479. if ( type === 'vec4' ) return GPUVertexFormat.Float4;
  480. if ( type === 'int' ) return GPUVertexFormat.Int;
  481. if ( type === 'ivec2' ) return GPUVertexFormat.Int2;
  482. if ( type === 'ivec3' ) return GPUVertexFormat.Int3;
  483. if ( type === 'ivec4' ) return GPUVertexFormat.Int4;
  484. if ( type === 'uint' ) return GPUVertexFormat.UInt;
  485. if ( type === 'uvec2' ) return GPUVertexFormat.UInt2;
  486. if ( type === 'uvec3' ) return GPUVertexFormat.UInt3;
  487. if ( type === 'uvec4' ) return GPUVertexFormat.UInt4;
  488. console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.', type );
  489. }
  490. _parseShaderAttributes( shader ) {
  491. // find "layout (location = num) in type name" in vertex shader
  492. const regex = /^\s*layout\s*\(\s*location\s*=\s*(?<location>[0-9]+)\s*\)\s*in\s+(?<type>\w+)\s+(?<name>\w+)\s*;/gmi;
  493. let shaderAttribute = null;
  494. const attributes = [];
  495. while ( shaderAttribute = regex.exec( shader ) ) {
  496. const shaderLocation = parseInt( shaderAttribute.groups.location );
  497. const arrayStride = this._getArrayStride( shaderAttribute.groups.type );
  498. const vertexFormat = this._getVertexFormat( shaderAttribute.groups.type );
  499. attributes.push( {
  500. name: shaderAttribute.groups.name,
  501. arrayStride: arrayStride,
  502. slot: shaderLocation,
  503. format: vertexFormat
  504. } );
  505. }
  506. // the sort ensures to setup vertex buffers in the correct order
  507. return attributes.sort( function ( a, b ) {
  508. return a.slot - b.slot;
  509. } );
  510. }
  511. }
  512. const ShaderLib = {
  513. mesh_basic: {
  514. vertexShader: `#version 450
  515. layout(location = 0) in vec3 position;
  516. layout(location = 1) in vec2 uv;
  517. layout(location = 0) out vec2 vUv;
  518. layout(set = 0, binding = 0) uniform ModelUniforms {
  519. mat4 modelMatrix;
  520. mat4 modelViewMatrix;
  521. mat3 normalMatrix;
  522. } modelUniforms;
  523. layout(set = 0, binding = 1) uniform CameraUniforms {
  524. mat4 projectionMatrix;
  525. mat4 viewMatrix;
  526. } cameraUniforms;
  527. void main(){
  528. vUv = uv;
  529. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  530. }`,
  531. fragmentShader: `#version 450
  532. layout(set = 0, binding = 2) uniform OpacityUniforms {
  533. float opacity;
  534. } opacityUniforms;
  535. layout(set = 0, binding = 3) uniform sampler mySampler;
  536. layout(set = 0, binding = 4) uniform texture2D myTexture;
  537. layout(location = 0) in vec2 vUv;
  538. layout(location = 0) out vec4 outColor;
  539. void main() {
  540. outColor = texture( sampler2D( myTexture, mySampler ), vUv );
  541. outColor.a *= opacityUniforms.opacity;
  542. }`
  543. },
  544. points_basic: {
  545. vertexShader: `#version 450
  546. layout(location = 0) in vec3 position;
  547. layout(set = 0, binding = 0) uniform ModelUniforms {
  548. mat4 modelMatrix;
  549. mat4 modelViewMatrix;
  550. } modelUniforms;
  551. layout(set = 0, binding = 1) uniform CameraUniforms {
  552. mat4 projectionMatrix;
  553. mat4 viewMatrix;
  554. } cameraUniforms;
  555. void main(){
  556. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  557. }`,
  558. fragmentShader: `#version 450
  559. layout(location = 0) out vec4 outColor;
  560. void main() {
  561. outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
  562. }`
  563. },
  564. line_basic: {
  565. vertexShader: `#version 450
  566. layout(location = 0) in vec3 position;
  567. layout(set = 0, binding = 0) uniform ModelUniforms {
  568. mat4 modelMatrix;
  569. mat4 modelViewMatrix;
  570. } modelUniforms;
  571. layout(set = 0, binding = 1) uniform CameraUniforms {
  572. mat4 projectionMatrix;
  573. mat4 viewMatrix;
  574. } cameraUniforms;
  575. void main(){
  576. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  577. }`,
  578. fragmentShader: `#version 450
  579. layout(location = 0) out vec4 outColor;
  580. void main() {
  581. outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
  582. }`
  583. }
  584. };
  585. export default WebGPURenderPipelines;