WebGPURenderPipelines.js 8.7 KB

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