2
0

WebGPURenderPipelines.js 20 KB

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