WebGPURenderPipelines.js 7.6 KB

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