WebGPURenderPipelines.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import { GPUPrimitiveTopology, GPUIndexFormat, GPUTextureFormat, GPUCompareFunction, GPUFrontFace, GPUCullMode, GPUVertexFormat, GPUBlendFactor, GPUBlendOperation } from './constants.js';
  2. import { FrontSide, BackSide, DoubleSide } from '../../../../build/three.module.js';
  3. class WebGPURenderPipelines {
  4. constructor( device, glslang, bindings ) {
  5. this.device = device;
  6. this.glslang = glslang;
  7. this.bindings = bindings;
  8. this.pipelines = new WeakMap();
  9. this.shaderAttributes = new WeakMap();
  10. this.shaderModules = {
  11. vertex: new WeakMap(),
  12. fragment: new WeakMap()
  13. };
  14. }
  15. get( object ) {
  16. let pipeline = this.pipelines.get( object );
  17. if ( pipeline === undefined ) {
  18. const device = this.device;
  19. const material = object.material;
  20. // shader source
  21. let shader;
  22. if ( material.isMeshBasicMaterial ) {
  23. shader = ShaderLib.mesh_basic;
  24. } else if ( material.isPointsMaterial ) {
  25. shader = ShaderLib.points_basic;
  26. } else if ( material.isLineBasicMaterial ) {
  27. shader = ShaderLib.line_basic;
  28. } else {
  29. console.error( 'WebGPURenderer: Unknwon shader type' );
  30. }
  31. // shader modules
  32. const glslang = this.glslang;
  33. let moduleVertex = this.shaderModules.vertex.get( shader );
  34. if ( moduleVertex === undefined ) {
  35. const byteCodeVertex = glslang.compileGLSL( shader.vertexShader, 'vertex' );
  36. moduleVertex = {
  37. module: device.createShaderModule( { code: byteCodeVertex } ),
  38. entryPoint: 'main'
  39. };
  40. this.shaderModules.vertex.set( shader, moduleVertex );
  41. }
  42. let moduleFragment = this.shaderModules.fragment.get( shader );
  43. if ( moduleFragment === undefined ) {
  44. const byteCodeFragment = glslang.compileGLSL( shader.fragmentShader, 'fragment' );
  45. moduleFragment = {
  46. module: device.createShaderModule( { code: byteCodeFragment } ),
  47. entryPoint: 'main'
  48. };
  49. this.shaderModules.fragment.set( shader, moduleFragment );
  50. }
  51. // layout
  52. const bindLayout = this.bindings.get( object ).layout;
  53. const layout = device.createPipelineLayout( { bindGroupLayouts: [ bindLayout ] } );
  54. // vertex buffers
  55. const vertexBuffers = [];
  56. const shaderAttributes = [];
  57. // Find "layout (location = num) in type name" in vertex shader
  58. const regex = /^\s*layout\s*\(\s*location\s*=\s*(?<location>[0-9]+)\s*\)\s*in\s+(?<type>\w+)\s+(?<name>\w+)\s*;/gmi;
  59. let shaderAttribute = null;
  60. while ( shaderAttribute = regex.exec( shader.vertexShader ) ) {
  61. const shaderLocation = parseInt( shaderAttribute.groups.location );
  62. const arrayStride = this._getArrayStride( shaderAttribute.groups.type );
  63. const vertexFormat = this._getVertexFormat( shaderAttribute.groups.type );
  64. shaderAttributes.push( { name: shaderAttribute.groups.name, slot: shaderLocation } );
  65. vertexBuffers.push( {
  66. arrayStride: arrayStride,
  67. attributes: [ { shaderLocation: shaderLocation, offset: 0, format: vertexFormat } ]
  68. } );
  69. }
  70. const geometry = object.geometry;
  71. let indexFormat;
  72. if ( object.isLine ) {
  73. const count = ( geometry.index ) ? geometry.index.count : geometry.attributes.position.count;
  74. indexFormat = ( count > 65535 ) ? GPUIndexFormat.Uint32 : GPUIndexFormat.Uint16; // define data type the primitive restart value
  75. }
  76. let colorBlend;
  77. if ( material.transparent ) {
  78. // @TODO: Should be customizable with material.blend* properties.
  79. colorBlend = {
  80. srcFactor: GPUBlendFactor.SrcAlpha,
  81. dstFactor: GPUBlendFactor.OneMinusSrcAlpha,
  82. operation: GPUBlendOperation.Add
  83. };
  84. }
  85. // pipeline
  86. const primitiveTopology = this._getPrimitiveTopology( object );
  87. const rasterizationState = this._getRasterizationStateDescriptor( object );
  88. pipeline = device.createRenderPipeline( {
  89. layout: layout,
  90. vertexStage: moduleVertex,
  91. fragmentStage: moduleFragment,
  92. primitiveTopology: primitiveTopology,
  93. rasterizationState: rasterizationState,
  94. colorStates: [ {
  95. format: GPUTextureFormat.BRGA8Unorm,
  96. colorBlend: colorBlend
  97. } ],
  98. depthStencilState: {
  99. depthWriteEnabled: material.depthWrite,
  100. depthCompare: GPUCompareFunction.Less,
  101. format: GPUTextureFormat.Depth24PlusStencil8,
  102. },
  103. vertexState: {
  104. indexFormat: indexFormat,
  105. vertexBuffers: vertexBuffers
  106. }
  107. } );
  108. this.pipelines.set( object, pipeline );
  109. this.shaderAttributes.set( pipeline, shaderAttributes );
  110. }
  111. return pipeline;
  112. }
  113. getShaderAttributes( pipeline ) {
  114. return this.shaderAttributes.get( pipeline );
  115. }
  116. dispose() {
  117. this.pipelines = new WeakMap();
  118. this.shaderAttributes = new WeakMap();
  119. this.shaderModules = {
  120. vertex: new WeakMap(),
  121. fragment: new WeakMap()
  122. };
  123. }
  124. _getArrayStride( type ) {
  125. // This code is GLSL specific. We need to update when we switch to WGSL.
  126. if ( type === 'float' ) return 4;
  127. if ( type === 'vec2' ) return 8;
  128. if ( type === 'vec3' ) return 12;
  129. if ( type === 'vec4' ) return 16;
  130. if ( type === 'int' ) return 4;
  131. if ( type === 'ivec2' ) return 8;
  132. if ( type === 'ivec3' ) return 12;
  133. if ( type === 'ivec4' ) return 16;
  134. if ( type === 'uint' ) return 4;
  135. if ( type === 'uvec2' ) return 8;
  136. if ( type === 'uvec3' ) return 12;
  137. if ( type === 'uvec4' ) return 16;
  138. console.error( 'WebGPURenderer: no this shader variable type support yet.', type );
  139. }
  140. _getPrimitiveTopology( object ) {
  141. if ( object.isMesh ) return GPUPrimitiveTopology.TriangleList;
  142. else if ( object.isPoints ) return GPUPrimitiveTopology.PointList;
  143. else if ( object.isLine ) return GPUPrimitiveTopology.LineStrip;
  144. else if ( object.isLineSegments ) return GPUPrimitiveTopology.LineList;
  145. }
  146. _getRasterizationStateDescriptor( object ) {
  147. const descriptor = {};
  148. const material = object.material;
  149. switch ( material.side ) {
  150. case FrontSide:
  151. descriptor.frontFace = GPUFrontFace.CCW;
  152. descriptor.cullMode = GPUCullMode.Back;
  153. break;
  154. case BackSide:
  155. descriptor.frontFace = GPUFrontFace.CW;
  156. descriptor.cullMode = GPUCullMode.Back;
  157. break;
  158. case DoubleSide:
  159. descriptor.frontFace = GPUFrontFace.CCW;
  160. descriptor.cullMode = GPUCullMode.None;
  161. break;
  162. default:
  163. console.warn( 'WebGPURenderer: Unknown material.side value.', material.side );
  164. break;
  165. }
  166. return descriptor;
  167. }
  168. _getVertexFormat( type ) {
  169. // This code is GLSL specific. We need to update when we switch to WGSL.
  170. if ( type === 'float' ) return GPUVertexFormat.Float;
  171. if ( type === 'vec2' ) return GPUVertexFormat.Float2;
  172. if ( type === 'vec3' ) return GPUVertexFormat.Float3;
  173. if ( type === 'vec4' ) return GPUVertexFormat.Float4;
  174. if ( type === 'int' ) return GPUVertexFormat.Int;
  175. if ( type === 'ivec2' ) return GPUVertexFormat.Int2;
  176. if ( type === 'ivec3' ) return GPUVertexFormat.Int3;
  177. if ( type === 'ivec4' ) return GPUVertexFormat.Int4;
  178. if ( type === 'uint' ) return GPUVertexFormat.UInt;
  179. if ( type === 'uvec2' ) return GPUVertexFormat.UInt2;
  180. if ( type === 'uvec3' ) return GPUVertexFormat.UInt3;
  181. if ( type === 'uvec4' ) return GPUVertexFormat.UInt4;
  182. console.error( 'WebGPURenderer: no this shader variable type support yet.', type );
  183. }
  184. }
  185. const ShaderLib = {
  186. mesh_basic: {
  187. vertexShader: `#version 450
  188. layout(location = 0) in vec3 position;
  189. layout(location = 1) in vec2 uv;
  190. layout(location = 0) out vec2 vUv;
  191. layout(set = 0, binding = 0) uniform ModelUniforms {
  192. mat4 modelMatrix;
  193. mat4 modelViewMatrix;
  194. } modelUniforms;
  195. layout(set = 0, binding = 1) uniform CameraUniforms {
  196. mat4 projectionMatrix;
  197. mat4 viewMatrix;
  198. } cameraUniforms;
  199. void main(){
  200. vUv = uv;
  201. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  202. }`,
  203. fragmentShader: `#version 450
  204. layout(set = 0, binding = 2) uniform OpacityUniforms {
  205. float opacity;
  206. } opacityUniforms;
  207. layout(set = 0, binding = 3) uniform sampler mySampler;
  208. layout(set = 0, binding = 4) uniform texture2D myTexture;
  209. layout(location = 0) in vec2 vUv;
  210. layout(location = 0) out vec4 outColor;
  211. void main() {
  212. outColor = texture( sampler2D( myTexture, mySampler ), vUv );
  213. outColor.a *= opacityUniforms.opacity;
  214. }`
  215. },
  216. points_basic: {
  217. vertexShader: `#version 450
  218. layout(location = 0) in vec3 position;
  219. layout(set = 0, binding = 0) uniform ModelUniforms {
  220. mat4 modelMatrix;
  221. mat4 modelViewMatrix;
  222. } modelUniforms;
  223. layout(set = 0, binding = 1) uniform CameraUniforms {
  224. mat4 projectionMatrix;
  225. mat4 viewMatrix;
  226. } cameraUniforms;
  227. void main(){
  228. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  229. }`,
  230. fragmentShader: `#version 450
  231. layout(location = 0) out vec4 outColor;
  232. void main() {
  233. outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
  234. }`
  235. },
  236. line_basic: {
  237. vertexShader: `#version 450
  238. layout(location = 0) in vec3 position;
  239. layout(set = 0, binding = 0) uniform ModelUniforms {
  240. mat4 modelMatrix;
  241. mat4 modelViewMatrix;
  242. } modelUniforms;
  243. layout(set = 0, binding = 1) uniform CameraUniforms {
  244. mat4 projectionMatrix;
  245. mat4 viewMatrix;
  246. } cameraUniforms;
  247. void main(){
  248. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  249. }`,
  250. fragmentShader: `#version 450
  251. layout(location = 0) out vec4 outColor;
  252. void main() {
  253. outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
  254. }`
  255. }
  256. };
  257. export default WebGPURenderPipelines;