WebGPURenderPipelines.js 21 KB

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