WebGPURenderPipelines.js 13 KB

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