WebGPURenderPipelines.js 21 KB

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