WebGPURenderPipelines.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. import { GPUPrimitiveTopology, GPUIndexFormat, GPUTextureFormat, GPUCompareFunction, GPUFrontFace, GPUCullMode, GPUVertexFormat, GPUBlendFactor, GPUBlendOperation, BlendColorFactor, OneMinusBlendColorFactor, GPUColorWriteFlags } from './constants.js';
  2. import {
  3. FrontSide, BackSide, DoubleSide,
  4. NeverDepth, AlwaysDepth, LessDepth, LessEqualDepth, EqualDepth, GreaterEqualDepth, GreaterDepth, NotEqualDepth,
  5. NoBlending, NormalBlending, AdditiveBlending, SubtractiveBlending, MultiplyBlending, CustomBlending,
  6. AddEquation, SubtractEquation, ReverseSubtractEquation, MinEquation, MaxEquation,
  7. ZeroFactor, OneFactor, SrcColorFactor, OneMinusSrcColorFactor, SrcAlphaFactor, OneMinusSrcAlphaFactor, DstAlphaFactor, OneMinusDstAlphaFactor, DstColorFactor, OneMinusDstColorFactor, SrcAlphaSaturateFactor
  8. } from '../../../../build/three.module.js';
  9. class WebGPURenderPipelines {
  10. constructor( device, glslang, bindings, sampleCount ) {
  11. this.device = device;
  12. this.glslang = glslang;
  13. this.bindings = bindings;
  14. this.sampleCount = sampleCount;
  15. this.pipelines = new WeakMap();
  16. this.shaderAttributes = new WeakMap();
  17. this.shaderModules = {
  18. vertex: new WeakMap(),
  19. fragment: new WeakMap()
  20. };
  21. }
  22. get( object ) {
  23. let pipeline = this.pipelines.get( object );
  24. if ( pipeline === undefined ) {
  25. const device = this.device;
  26. const material = object.material;
  27. // shader source
  28. let shader;
  29. if ( material.isMeshBasicMaterial ) {
  30. shader = ShaderLib.mesh_basic;
  31. } else if ( material.isPointsMaterial ) {
  32. shader = ShaderLib.points_basic;
  33. } else if ( material.isLineBasicMaterial ) {
  34. shader = ShaderLib.line_basic;
  35. } else {
  36. console.error( 'THREE.WebGPURenderer: Unknwon shader type.' );
  37. }
  38. // shader modules
  39. const glslang = this.glslang;
  40. let moduleVertex = this.shaderModules.vertex.get( shader );
  41. if ( moduleVertex === undefined ) {
  42. const byteCodeVertex = glslang.compileGLSL( shader.vertexShader, 'vertex' );
  43. moduleVertex = {
  44. module: device.createShaderModule( { code: byteCodeVertex } ),
  45. entryPoint: 'main'
  46. };
  47. this.shaderModules.vertex.set( shader, moduleVertex );
  48. }
  49. let moduleFragment = this.shaderModules.fragment.get( shader );
  50. if ( moduleFragment === undefined ) {
  51. const byteCodeFragment = glslang.compileGLSL( shader.fragmentShader, 'fragment' );
  52. moduleFragment = {
  53. module: device.createShaderModule( { code: byteCodeFragment } ),
  54. entryPoint: 'main'
  55. };
  56. this.shaderModules.fragment.set( shader, moduleFragment );
  57. }
  58. // layout
  59. const bindLayout = this.bindings.get( object ).layout;
  60. const layout = device.createPipelineLayout( { bindGroupLayouts: [ bindLayout ] } );
  61. // vertex buffers
  62. const vertexBuffers = [];
  63. const shaderAttributes = [];
  64. // find "layout (location = num) in type name" in vertex shader
  65. const regex = /^\s*layout\s*\(\s*location\s*=\s*(?<location>[0-9]+)\s*\)\s*in\s+(?<type>\w+)\s+(?<name>\w+)\s*;/gmi;
  66. let shaderAttribute = null;
  67. while ( shaderAttribute = regex.exec( shader.vertexShader ) ) {
  68. const shaderLocation = parseInt( shaderAttribute.groups.location );
  69. const arrayStride = this._getArrayStride( shaderAttribute.groups.type );
  70. const vertexFormat = this._getVertexFormat( shaderAttribute.groups.type );
  71. shaderAttributes.push( { name: shaderAttribute.groups.name, slot: shaderLocation } );
  72. vertexBuffers.push( {
  73. arrayStride: arrayStride,
  74. attributes: [ { shaderLocation: shaderLocation, offset: 0, format: vertexFormat } ]
  75. } );
  76. }
  77. const geometry = object.geometry;
  78. let indexFormat;
  79. if ( object.isLine ) {
  80. const count = ( geometry.index ) ? geometry.index.count : geometry.attributes.position.count;
  81. indexFormat = ( count > 65535 ) ? GPUIndexFormat.Uint32 : GPUIndexFormat.Uint16; // define data type for primitive restart value
  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. // pipeline
  90. const primitiveTopology = this._getPrimitiveTopology( object );
  91. const rasterizationState = this._getRasterizationStateDescriptor( material );
  92. const depthCompare = this._getDepthCompare( material );
  93. const colorWriteMask = this._getColorWriteMask( material );
  94. pipeline = device.createRenderPipeline( {
  95. layout: layout,
  96. vertexStage: moduleVertex,
  97. fragmentStage: moduleFragment,
  98. primitiveTopology: primitiveTopology,
  99. rasterizationState: rasterizationState,
  100. colorStates: [ {
  101. format: GPUTextureFormat.BRGA8Unorm,
  102. alphaBlend: alphaBlend,
  103. colorBlend: colorBlend,
  104. writeMask: colorWriteMask
  105. } ],
  106. depthStencilState: {
  107. depthWriteEnabled: material.depthWrite,
  108. depthCompare: depthCompare,
  109. format: GPUTextureFormat.Depth24PlusStencil8,
  110. },
  111. vertexState: {
  112. indexFormat: indexFormat,
  113. vertexBuffers: vertexBuffers
  114. },
  115. sampleCount: this.sampleCount
  116. } );
  117. this.pipelines.set( object, pipeline );
  118. this.shaderAttributes.set( pipeline, shaderAttributes );
  119. }
  120. return pipeline;
  121. }
  122. getShaderAttributes( pipeline ) {
  123. return this.shaderAttributes.get( pipeline );
  124. }
  125. dispose() {
  126. this.pipelines = new WeakMap();
  127. this.shaderAttributes = new WeakMap();
  128. this.shaderModules = {
  129. vertex: new WeakMap(),
  130. fragment: new WeakMap()
  131. };
  132. }
  133. _getArrayStride( type ) {
  134. // @TODO: This code is GLSL specific. We need to update when we switch to WGSL.
  135. if ( type === 'float' ) return 4;
  136. if ( type === 'vec2' ) return 8;
  137. if ( type === 'vec3' ) return 12;
  138. if ( type === 'vec4' ) return 16;
  139. if ( type === 'int' ) return 4;
  140. if ( type === 'ivec2' ) return 8;
  141. if ( type === 'ivec3' ) return 12;
  142. if ( type === 'ivec4' ) return 16;
  143. if ( type === 'uint' ) return 4;
  144. if ( type === 'uvec2' ) return 8;
  145. if ( type === 'uvec3' ) return 12;
  146. if ( type === 'uvec4' ) return 16;
  147. console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.', type );
  148. }
  149. _getAlphaBlend( material ) {
  150. const blending = material.blending;
  151. const premultipliedAlpha = material.premultipliedAlpha;
  152. let alphaBlend = undefined;
  153. switch ( blending ) {
  154. case NormalBlending:
  155. if ( premultipliedAlpha === false ) {
  156. alphaBlend = {
  157. srcFactor: GPUBlendFactor.One,
  158. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  159. operation: GPUBlendOperation.Add
  160. };
  161. }
  162. break;
  163. case AdditiveBlending:
  164. // no alphaBlend settings
  165. break;
  166. case SubtractiveBlending:
  167. if ( premultipliedAlpha === true ) {
  168. alphaBlend = {
  169. srcFactor: GPUBlendFactor.OneMinusSrcColor,
  170. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  171. operation: GPUBlendOperation.Add
  172. };
  173. }
  174. break;
  175. case MultiplyBlending:
  176. if ( premultipliedAlpha === true ) {
  177. alphaBlend = {
  178. srcFactor: GPUBlendFactor.Zero,
  179. dstFactor: GPUBlendFactor.SrcAlpha,
  180. operation: GPUBlendOperation.Add
  181. };
  182. }
  183. break;
  184. case CustomBlending:
  185. const blendSrcAlpha = material.blendSrcAlpha;
  186. const blendDstAlpha = material.blendDstAlpha;
  187. const blendEquationAlpha = material.blendEquationAlpha;
  188. if ( blendSrcAlpha !== null && blendDstAlpha !== null && blendEquationAlpha !== null ) {
  189. alphaBlend = {
  190. srcFactor: this._getBlendFactor( blendSrcAlpha ),
  191. dstFactor: this._getBlendFactor( blendDstAlpha ),
  192. operation: this._getBlendOperation( blendEquationAlpha )
  193. };
  194. }
  195. break;
  196. default:
  197. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  198. }
  199. return alphaBlend;
  200. }
  201. _getBlendFactor( blend ) {
  202. let blendFactor;
  203. switch ( blend ) {
  204. case ZeroFactor:
  205. blendFactor = GPUBlendFactor.Zero;
  206. break;
  207. case OneFactor:
  208. blendFactor = GPUBlendFactor.One;
  209. break;
  210. case SrcColorFactor:
  211. blendFactor = GPUBlendFactor.SrcColor;
  212. break;
  213. case OneMinusSrcColorFactor:
  214. blendFactor = GPUBlendFactor.OneMinusSrcColor;
  215. break;
  216. case SrcAlphaFactor:
  217. blendFactor = GPUBlendFactor.SrcAlpha;
  218. break;
  219. case OneMinusSrcAlphaFactor:
  220. blendFactor = GPUBlendFactor.OneMinusSrcAlpha;
  221. break;
  222. case DstColorFactor:
  223. blendFactor = GPUBlendFactor.DstColor;
  224. break;
  225. case OneMinusDstColorFactor:
  226. blendFactor = GPUBlendFactor.OneMinusDstColor;
  227. break;
  228. case DstAlphaFactor:
  229. blendFactor = GPUBlendFactor.DstAlpha;
  230. break;
  231. case OneMinusDstAlphaFactor:
  232. blendFactor = GPUBlendFactor.OneMinusDstAlpha;
  233. break;
  234. case SrcAlphaSaturateFactor:
  235. blendFactor = GPUBlendFactor.SrcAlphaSaturated;
  236. break;
  237. case BlendColorFactor:
  238. blendFactor = GPUBlendFactor.BlendColor;
  239. break;
  240. case OneMinusBlendColorFactor:
  241. blendFactor = GPUBlendFactor.OneMinusBlendColor;
  242. break;
  243. default:
  244. console.error( 'THREE.WebGPURenderer: Blend factor not supported.', blend );
  245. }
  246. return blendFactor;
  247. }
  248. _getBlendOperation( blendEquation ) {
  249. let blendOperation;
  250. switch ( blendEquation ) {
  251. case AddEquation:
  252. blendOperation = GPUBlendOperation.Add;
  253. break;
  254. case SubtractEquation:
  255. blendOperation = GPUBlendOperation.Subtract;
  256. break;
  257. case ReverseSubtractEquation:
  258. blendOperation = GPUBlendOperation.ReverseSubtract;
  259. break;
  260. case MinEquation:
  261. blendOperation = GPUBlendOperation.Min;
  262. break;
  263. case MaxEquation:
  264. blendOperation = GPUBlendOperation.Max;
  265. break;
  266. default:
  267. console.error( 'THREE.WebGPURenderer: Blend equation not supported.', blendEquation );
  268. }
  269. return blendOperation;
  270. }
  271. _getColorBlend( material ) {
  272. const blending = material.blending;
  273. const premultipliedAlpha = material.premultipliedAlpha;
  274. const colorBlend = {
  275. srcFactor: null,
  276. dstFactor: null,
  277. operation: null
  278. };
  279. switch ( blending ) {
  280. case NormalBlending:
  281. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  282. colorBlend.dstFactor = GPUBlendFactor.OneMinusSrcAlpha;
  283. colorBlend.operation = GPUBlendOperation.Add;
  284. break;
  285. case AdditiveBlending:
  286. colorBlend.srcFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.One : GPUBlendFactor.SrcAlpha;
  287. colorBlend.operation = GPUBlendOperation.Add;
  288. break;
  289. case SubtractiveBlending:
  290. colorBlend.srcFactor = GPUBlendFactor.Zero;
  291. colorBlend.dstFactor = ( premultipliedAlpha === true ) ? GPUBlendFactor.Zero : GPUBlendFactor.OneMinusSrcColor;
  292. colorBlend.operation = GPUBlendOperation.Add;
  293. break;
  294. case MultiplyBlending:
  295. colorBlend.srcFactor = GPUBlendFactor.Zero;
  296. colorBlend.dstFactor = GPUBlendFactor.SrcColor;
  297. colorBlend.operation = GPUBlendOperation.Add;
  298. break;
  299. case CustomBlending:
  300. colorBlend.srcFactor = this._getBlendFactor( material.blendSrc );
  301. colorBlend.dstFactor = this._getBlendFactor( material.blendDst );
  302. colorBlend.operation = this._getBlendOperation( material.blendEquation );
  303. break;
  304. default:
  305. console.error( 'THREE.WebGPURenderer: Blending not supported.', blending );
  306. }
  307. return colorBlend;
  308. }
  309. _getColorWriteMask( material ) {
  310. return ( material.colorWrite === true ) ? GPUColorWriteFlags.All : GPUColorWriteFlags.None;
  311. }
  312. _getDepthCompare( material ) {
  313. let depthCompare;
  314. if ( material.depthTest === false ) {
  315. depthCompare = GPUCompareFunction.Always;
  316. } else {
  317. const depthFunc = material.depthFunc;
  318. switch ( depthFunc ) {
  319. case NeverDepth:
  320. depthCompare = GPUCompareFunction.Never;
  321. break;
  322. case AlwaysDepth:
  323. depthCompare = GPUCompareFunction.Always;
  324. break;
  325. case LessDepth:
  326. depthCompare = GPUCompareFunction.Less;
  327. break;
  328. case LessEqualDepth:
  329. depthCompare = GPUCompareFunction.LessEqual;
  330. break;
  331. case EqualDepth:
  332. depthCompare = GPUCompareFunction.Equal;
  333. break;
  334. case GreaterEqualDepth:
  335. depthCompare = GPUCompareFunction.GreaterEqual;
  336. break;
  337. case GreaterDepth:
  338. depthCompare = GPUCompareFunction.Greater;
  339. break;
  340. case NotEqualDepth:
  341. depthCompare = GPUCompareFunction.NotEqual;
  342. break;
  343. default:
  344. console.error( 'THREE.WebGPURenderer: Invalid depth function.', depthFunc );
  345. }
  346. }
  347. return depthCompare;
  348. }
  349. _getPrimitiveTopology( object ) {
  350. if ( object.isMesh ) return GPUPrimitiveTopology.TriangleList;
  351. else if ( object.isPoints ) return GPUPrimitiveTopology.PointList;
  352. else if ( object.isLine ) return GPUPrimitiveTopology.LineStrip;
  353. else if ( object.isLineSegments ) return GPUPrimitiveTopology.LineList;
  354. }
  355. _getRasterizationStateDescriptor( material ) {
  356. const descriptor = {};
  357. switch ( material.side ) {
  358. case FrontSide:
  359. descriptor.frontFace = GPUFrontFace.CCW;
  360. descriptor.cullMode = GPUCullMode.Back;
  361. break;
  362. case BackSide:
  363. descriptor.frontFace = GPUFrontFace.CW;
  364. descriptor.cullMode = GPUCullMode.Back;
  365. break;
  366. case DoubleSide:
  367. descriptor.frontFace = GPUFrontFace.CCW;
  368. descriptor.cullMode = GPUCullMode.None;
  369. break;
  370. default:
  371. console.error( 'THREE.WebGPURenderer: Unknown Material.side value.', material.side );
  372. break;
  373. }
  374. return descriptor;
  375. }
  376. _getVertexFormat( type ) {
  377. // @TODO: This code is GLSL specific. We need to update when we switch to WGSL.
  378. if ( type === 'float' ) return GPUVertexFormat.Float;
  379. if ( type === 'vec2' ) return GPUVertexFormat.Float2;
  380. if ( type === 'vec3' ) return GPUVertexFormat.Float3;
  381. if ( type === 'vec4' ) return GPUVertexFormat.Float4;
  382. if ( type === 'int' ) return GPUVertexFormat.Int;
  383. if ( type === 'ivec2' ) return GPUVertexFormat.Int2;
  384. if ( type === 'ivec3' ) return GPUVertexFormat.Int3;
  385. if ( type === 'ivec4' ) return GPUVertexFormat.Int4;
  386. if ( type === 'uint' ) return GPUVertexFormat.UInt;
  387. if ( type === 'uvec2' ) return GPUVertexFormat.UInt2;
  388. if ( type === 'uvec3' ) return GPUVertexFormat.UInt3;
  389. if ( type === 'uvec4' ) return GPUVertexFormat.UInt4;
  390. console.error( 'THREE.WebGPURenderer: Shader variable type not supported yet.', type );
  391. }
  392. }
  393. const ShaderLib = {
  394. mesh_basic: {
  395. vertexShader: `#version 450
  396. layout(location = 0) in vec3 position;
  397. layout(location = 1) in vec2 uv;
  398. layout(location = 0) out vec2 vUv;
  399. layout(set = 0, binding = 0) uniform ModelUniforms {
  400. mat4 modelMatrix;
  401. mat4 modelViewMatrix;
  402. } modelUniforms;
  403. layout(set = 0, binding = 1) uniform CameraUniforms {
  404. mat4 projectionMatrix;
  405. mat4 viewMatrix;
  406. } cameraUniforms;
  407. void main(){
  408. vUv = uv;
  409. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  410. }`,
  411. fragmentShader: `#version 450
  412. layout(set = 0, binding = 2) uniform OpacityUniforms {
  413. float opacity;
  414. } opacityUniforms;
  415. layout(set = 0, binding = 3) uniform sampler mySampler;
  416. layout(set = 0, binding = 4) uniform texture2D myTexture;
  417. layout(location = 0) in vec2 vUv;
  418. layout(location = 0) out vec4 outColor;
  419. void main() {
  420. outColor = texture( sampler2D( myTexture, mySampler ), vUv );
  421. outColor.a *= opacityUniforms.opacity;
  422. }`
  423. },
  424. points_basic: {
  425. vertexShader: `#version 450
  426. layout(location = 0) in vec3 position;
  427. layout(set = 0, binding = 0) uniform ModelUniforms {
  428. mat4 modelMatrix;
  429. mat4 modelViewMatrix;
  430. } modelUniforms;
  431. layout(set = 0, binding = 1) uniform CameraUniforms {
  432. mat4 projectionMatrix;
  433. mat4 viewMatrix;
  434. } cameraUniforms;
  435. void main(){
  436. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  437. }`,
  438. fragmentShader: `#version 450
  439. layout(location = 0) out vec4 outColor;
  440. void main() {
  441. outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
  442. }`
  443. },
  444. line_basic: {
  445. vertexShader: `#version 450
  446. layout(location = 0) in vec3 position;
  447. layout(set = 0, binding = 0) uniform ModelUniforms {
  448. mat4 modelMatrix;
  449. mat4 modelViewMatrix;
  450. } modelUniforms;
  451. layout(set = 0, binding = 1) uniform CameraUniforms {
  452. mat4 projectionMatrix;
  453. mat4 viewMatrix;
  454. } cameraUniforms;
  455. void main(){
  456. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  457. }`,
  458. fragmentShader: `#version 450
  459. layout(location = 0) out vec4 outColor;
  460. void main() {
  461. outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
  462. }`
  463. }
  464. };
  465. export default WebGPURenderPipelines;