WebGPURenderPipelines.js 8.4 KB

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