WebGPURenderPipelines.js 8.5 KB

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