WebGPURenderPipelines.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import WebGPURenderPipeline from './WebGPURenderPipeline.js';
  2. import WebGPUProgrammableStage from './WebGPUProgrammableStage.js';
  3. class WebGPURenderPipelines {
  4. constructor( renderer, properties, device, glslang, sampleCount, nodes ) {
  5. this.renderer = renderer;
  6. this.properties = properties;
  7. this.device = device;
  8. this.glslang = glslang;
  9. this.sampleCount = sampleCount;
  10. this.nodes = nodes;
  11. this.pipelines = [];
  12. this.objectCache = new WeakMap();
  13. this.stages = {
  14. vertex: new Map(),
  15. fragment: new Map()
  16. };
  17. }
  18. get( object ) {
  19. const device = this.device;
  20. const glslang = this.glslang;
  21. const properties = this.properties;
  22. const material = object.material;
  23. const materialProperties = properties.get( material );
  24. const objectProperties = properties.get( object );
  25. let currentPipeline = objectProperties.currentPipeline;
  26. if ( this._needsUpdate( object ) ) {
  27. // get shader
  28. const nodeBuilder = this.nodes.get( object );
  29. // programmable stages
  30. let stageVertex = this.stages.vertex.get( nodeBuilder.vertexShader );
  31. if ( stageVertex === undefined ) {
  32. stageVertex = new WebGPUProgrammableStage( device, glslang, nodeBuilder.vertexShader, 'vertex' );
  33. this.stages.vertex.set( nodeBuilder.vertexShader, stageVertex );
  34. }
  35. let stageFragment = this.stages.fragment.get( nodeBuilder.fragmentShader );
  36. if ( stageFragment === undefined ) {
  37. stageFragment = new WebGPUProgrammableStage( device, glslang, nodeBuilder.fragmentShader, 'fragment' );
  38. this.stages.fragment.set( nodeBuilder.fragmentShader, stageFragment );
  39. }
  40. stageVertex.usedTimes ++;
  41. stageFragment.usedTimes ++;
  42. // determine render pipeline
  43. currentPipeline = this._acquirePipeline( stageVertex, stageFragment, object, nodeBuilder );
  44. objectProperties.currentPipeline = currentPipeline;
  45. // keep track of all pipelines which are used by a material
  46. let materialPipelines = materialProperties.pipelines;
  47. if ( materialPipelines === undefined ) {
  48. materialPipelines = new Set();
  49. materialProperties.pipelines = materialPipelines;
  50. }
  51. if ( materialPipelines.has( currentPipeline ) === false ) {
  52. materialPipelines.add( currentPipeline );
  53. currentPipeline.usedTimes ++;
  54. }
  55. // dispose
  56. if ( materialProperties.disposeCallback === undefined ) {
  57. const disposeCallback = onMaterialDispose.bind( this );
  58. materialProperties.disposeCallback = disposeCallback;
  59. material.addEventListener( 'dispose', disposeCallback );
  60. }
  61. }
  62. return currentPipeline;
  63. }
  64. dispose() {
  65. this.pipelines = [];
  66. this.objectCache = new WeakMap();
  67. this.shaderModules = {
  68. vertex: new Map(),
  69. fragment: new Map()
  70. };
  71. }
  72. _acquirePipeline( stageVertex, stageFragment, object, nodeBuilder ) {
  73. let pipeline;
  74. const pipelines = this.pipelines;
  75. // check for existing pipeline
  76. const cacheKey = this._computeCacheKey( stageVertex, stageFragment, object );
  77. for ( let i = 0, il = pipelines.length; i < il; i ++ ) {
  78. const preexistingPipeline = pipelines[ i ];
  79. if ( preexistingPipeline.cacheKey === cacheKey ) {
  80. pipeline = preexistingPipeline;
  81. break;
  82. }
  83. }
  84. if ( pipeline === undefined ) {
  85. pipeline = new WebGPURenderPipeline( this.device, this.renderer, this.sampleCount );
  86. pipeline.init( cacheKey, stageVertex, stageFragment, object, nodeBuilder );
  87. pipelines.push( pipeline );
  88. }
  89. return pipeline;
  90. }
  91. _computeCacheKey( stageVertex, stageFragment, object ) {
  92. const material = object.material;
  93. const renderer = this.renderer;
  94. const parameters = [
  95. stageVertex.id, stageFragment.id,
  96. material.transparent, material.blending, material.premultipliedAlpha,
  97. material.blendSrc, material.blendDst, material.blendEquation,
  98. material.blendSrcAlpha, material.blendDstAlpha, material.blendEquationAlpha,
  99. material.colorWrite,
  100. material.depthWrite, material.depthTest, material.depthFunc,
  101. material.stencilWrite, material.stencilFunc,
  102. material.stencilFail, material.stencilZFail, material.stencilZPass,
  103. material.stencilFuncMask, material.stencilWriteMask,
  104. material.side,
  105. this.sampleCount,
  106. renderer.getCurrentEncoding(), renderer.getCurrentColorFormat(), renderer.getCurrentDepthStencilFormat()
  107. ];
  108. return parameters.join();
  109. }
  110. _releasePipeline( pipeline ) {
  111. if ( -- pipeline.usedTimes === 0 ) {
  112. const pipelines = this.pipelines;
  113. const i = pipelines.indexOf( pipeline );
  114. pipelines[ i ] = pipelines[ pipelines.length - 1 ];
  115. pipelines.pop();
  116. this._releaseStage( pipeline.stageVertex );
  117. this._releaseStage( pipeline.stageFragment );
  118. }
  119. }
  120. _releaseStage( stage ) {
  121. if ( -- stage.usedTimes === 0 ) {
  122. const code = stage.code;
  123. const type = stage.type;
  124. this.stages[ type ].delete( code );
  125. }
  126. }
  127. _needsUpdate( object ) {
  128. let cache = this.objectCache.get( object );
  129. if ( cache === undefined ) {
  130. cache = {};
  131. this.objectCache.set( object, cache );
  132. }
  133. const material = object.material;
  134. let needsUpdate = false;
  135. // check material state
  136. if ( cache.material !== material || cache.materialVersion !== material.version ||
  137. cache.transparent !== material.transparent || cache.blending !== material.blending || cache.premultipliedAlpha !== material.premultipliedAlpha ||
  138. cache.blendSrc !== material.blendSrc || cache.blendDst !== material.blendDst || cache.blendEquation !== material.blendEquation ||
  139. cache.blendSrcAlpha !== material.blendSrcAlpha || cache.blendDstAlpha !== material.blendDstAlpha || cache.blendEquationAlpha !== material.blendEquationAlpha ||
  140. cache.colorWrite !== material.colorWrite ||
  141. cache.depthWrite !== material.depthWrite || cache.depthTest !== material.depthTest || cache.depthFunc !== material.depthFunc ||
  142. cache.stencilWrite !== material.stencilWrite || cache.stencilFunc !== material.stencilFunc ||
  143. cache.stencilFail !== material.stencilFail || cache.stencilZFail !== material.stencilZFail || cache.stencilZPass !== material.stencilZPass ||
  144. cache.stencilFuncMask !== material.stencilFuncMask || cache.stencilWriteMask !== material.stencilWriteMask ||
  145. cache.side !== material.side
  146. ) {
  147. cache.material = material; cache.materialVersion = material.version;
  148. cache.transparent = material.transparent; cache.blending = material.blending; cache.premultipliedAlpha = material.premultipliedAlpha;
  149. cache.blendSrc = material.blendSrc; cache.blendDst = material.blendDst; cache.blendEquation = material.blendEquation;
  150. cache.blendSrcAlpha = material.blendSrcAlpha; cache.blendDstAlpha = material.blendDstAlpha; cache.blendEquationAlpha = material.blendEquationAlpha;
  151. cache.colorWrite = material.colorWrite;
  152. cache.depthWrite = material.depthWrite; cache.depthTest = material.depthTest; cache.depthFunc = material.depthFunc;
  153. cache.stencilWrite = material.stencilWrite; cache.stencilFunc = material.stencilFunc;
  154. cache.stencilFail = material.stencilFail; cache.stencilZFail = material.stencilZFail; cache.stencilZPass = material.stencilZPass;
  155. cache.stencilFuncMask = material.stencilFuncMask; cache.stencilWriteMask = material.stencilWriteMask;
  156. cache.side = material.side;
  157. needsUpdate = true;
  158. }
  159. // check renderer state
  160. const renderer = this.renderer;
  161. const encoding = renderer.getCurrentEncoding();
  162. const colorFormat = renderer.getCurrentColorFormat();
  163. const depthStencilFormat = renderer.getCurrentDepthStencilFormat();
  164. if ( cache.sampleCount !== this.sampleCount || cache.encoding !== encoding ||
  165. cache.colorFormat !== colorFormat || cache.depthStencilFormat !== depthStencilFormat ) {
  166. cache.sampleCount = this.sampleCount;
  167. cache.encoding = encoding;
  168. cache.colorFormat = colorFormat;
  169. cache.depthStencilFormat = depthStencilFormat;
  170. needsUpdate = true;
  171. }
  172. return needsUpdate;
  173. }
  174. }
  175. function onMaterialDispose( event ) {
  176. const properties = this.properties;
  177. const material = event.target;
  178. const materialProperties = properties.get( material );
  179. material.removeEventListener( 'dispose', materialProperties.disposeCallback );
  180. properties.remove( material );
  181. // remove references to pipelines
  182. const pipelines = materialProperties.pipelines;
  183. if ( pipelines !== undefined ) {
  184. for ( const pipeline of pipelines ) {
  185. this._releasePipeline( pipeline );
  186. }
  187. }
  188. }
  189. export default WebGPURenderPipelines;