WebGPURenderPipelines.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. import { GPUPrimitiveTopology, GPUIndexFormat } 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. let shaderLocation = 0;
  58. for ( const name in attributes ) {
  59. const attribute = attributes[ name ];
  60. const arrayStride = this._getArrayStride( attribute );
  61. const vertexFormat = this._getVertexFormat( attribute );
  62. vertexBuffers.push( {
  63. arrayStride: arrayStride,
  64. attributes: [ { shaderLocation: shaderLocation, offset: 0, format: vertexFormat } ]
  65. } );
  66. shaderLocation ++;
  67. }
  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: 'bgra8unorm' } ],
  83. depthStencilState: {
  84. depthWriteEnabled: material.depthWrite,
  85. depthCompare: "less",
  86. format: 'depth24plus-stencil8',
  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( attribute ) {
  105. const array = attribute.array;
  106. if ( array instanceof Float32Array || array instanceof Uint32Array || array instanceof Int32Array ) {
  107. return attribute.itemSize * 4;
  108. }
  109. }
  110. _getPrimitiveTopology( object ) {
  111. if ( object.isMesh ) return GPUPrimitiveTopology.TriangleList;
  112. else if ( object.isPoints ) return GPUPrimitiveTopology.PointList;
  113. else if ( object.isLine ) return GPUPrimitiveTopology.LineStrip;
  114. else if ( object.isLineSegments ) return GPUPrimitiveTopology.LineList;
  115. }
  116. _getRasterizationStateDescriptor( object ) {
  117. const descriptor = {};
  118. const material = object.material;
  119. switch ( material.side ) {
  120. case FrontSide:
  121. descriptor.frontFace = 'ccw';
  122. descriptor.cullMode = 'back';
  123. break;
  124. case BackSide:
  125. descriptor.frontFace = 'cw';
  126. descriptor.cullMode = 'back';
  127. break;
  128. case DoubleSide:
  129. descriptor.frontFace = 'ccw';
  130. descriptor.cullMode = 'none';
  131. break;
  132. default:
  133. console.warn( 'WebGPURenderer: Unknown material.side value.', material.side );
  134. break;
  135. }
  136. return descriptor;
  137. }
  138. _getVertexFormat( attribute ) {
  139. const array = attribute.array;
  140. if ( array instanceof Float32Array ) {
  141. if ( attribute.itemSize === 1 ) return 'float';
  142. if ( attribute.itemSize === 2 ) return 'float2';
  143. if ( attribute.itemSize === 3 ) return 'float3';
  144. if ( attribute.itemSize === 4 ) return 'float4';
  145. } else if ( array instanceof Uint32Array ) {
  146. if ( attribute.itemSize === 1 ) return 'uint';
  147. if ( attribute.itemSize === 2 ) return 'uint2';
  148. if ( attribute.itemSize === 3 ) return 'uint3';
  149. if ( attribute.itemSize === 4 ) return 'uint4';
  150. } else if ( array instanceof Int32Array ) {
  151. if ( attribute.itemSize === 1 ) return 'int';
  152. if ( attribute.itemSize === 2 ) return 'int2';
  153. if ( attribute.itemSize === 3 ) return 'int3';
  154. if ( attribute.itemSize === 4 ) return 'int4';
  155. }
  156. }
  157. }
  158. const ShaderLib = {
  159. mesh_basic: {
  160. vertexShader: `#version 450
  161. layout(location = 0) in vec3 position;
  162. layout(location = 1) in vec3 normal;
  163. layout(location = 2) in vec2 uv;
  164. layout(location = 0) out vec2 vUv;
  165. layout(set = 0, binding = 0) uniform ModelUniforms {
  166. mat4 modelMatrix;
  167. mat4 modelViewMatrix;
  168. } modelUniforms;
  169. layout(set = 0, binding = 1) uniform CameraUniforms {
  170. mat4 projectionMatrix;
  171. mat4 viewMatrix;
  172. } cameraUniforms;
  173. void main(){
  174. vUv = uv;
  175. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  176. }`,
  177. fragmentShader: `#version 450
  178. layout(set = 0, binding = 2) uniform sampler mySampler;
  179. layout(set = 0, binding = 3) uniform texture2D myTexture;
  180. layout(location = 0) in vec2 vUv;
  181. layout(location = 0) out vec4 outColor;
  182. void main() {
  183. outColor = texture( sampler2D( myTexture, mySampler ), vUv );
  184. }`
  185. },
  186. points_basic: {
  187. vertexShader: `#version 450
  188. layout(location = 0) in vec3 position;
  189. layout(set = 0, binding = 0) uniform ModelUniforms {
  190. mat4 modelMatrix;
  191. mat4 modelViewMatrix;
  192. } modelUniforms;
  193. layout(set = 0, binding = 1) uniform CameraUniforms {
  194. mat4 projectionMatrix;
  195. mat4 viewMatrix;
  196. } cameraUniforms;
  197. void main(){
  198. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  199. }`,
  200. fragmentShader: `#version 450
  201. layout(location = 0) out vec4 outColor;
  202. void main() {
  203. outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
  204. }`
  205. },
  206. line_basic: {
  207. vertexShader: `#version 450
  208. layout(location = 0) in vec3 position;
  209. layout(set = 0, binding = 0) uniform ModelUniforms {
  210. mat4 modelMatrix;
  211. mat4 modelViewMatrix;
  212. } modelUniforms;
  213. layout(set = 0, binding = 1) uniform CameraUniforms {
  214. mat4 projectionMatrix;
  215. mat4 viewMatrix;
  216. } cameraUniforms;
  217. void main(){
  218. gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
  219. }`,
  220. fragmentShader: `#version 450
  221. layout(location = 0) out vec4 outColor;
  222. void main() {
  223. outColor = vec4( 1.0, 0.0, 0.0, 1.0 );
  224. }`
  225. }
  226. };
  227. export default WebGPURenderPipelines;