WebGPURenderPipelines.js 9.3 KB

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