WebGPURenderPipelines.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import WebGPURenderPipeline from './WebGPURenderPipeline.js';
  2. import WebGPUProgrammableStage from './WebGPUProgrammableStage.js';
  3. class WebGPURenderPipelines {
  4. constructor( device, nodes, utils ) {
  5. this.device = device;
  6. this.nodes = nodes;
  7. this.utils = utils;
  8. this.bindings = null;
  9. this.pipelines = [];
  10. this.objectCache = new WeakMap();
  11. this.stages = {
  12. vertex: new Map(),
  13. fragment: new Map()
  14. };
  15. }
  16. get( object ) {
  17. const device = this.device;
  18. const cache = this._getCache( object );
  19. let currentPipeline;
  20. if ( this._needsUpdate( object, cache ) ) {
  21. const material = object.material;
  22. // release previous cache
  23. if ( cache.currentPipeline !== undefined ) {
  24. this._releaseObject( object );
  25. }
  26. // get shader
  27. const nodeBuilder = this.nodes.get( object );
  28. // programmable stages
  29. let stageVertex = this.stages.vertex.get( nodeBuilder.vertexShader );
  30. if ( stageVertex === undefined ) {
  31. stageVertex = new WebGPUProgrammableStage( device, nodeBuilder.vertexShader, 'vertex' );
  32. this.stages.vertex.set( nodeBuilder.vertexShader, stageVertex );
  33. }
  34. let stageFragment = this.stages.fragment.get( nodeBuilder.fragmentShader );
  35. if ( stageFragment === undefined ) {
  36. stageFragment = new WebGPUProgrammableStage( device, nodeBuilder.fragmentShader, 'fragment' );
  37. this.stages.fragment.set( nodeBuilder.fragmentShader, stageFragment );
  38. }
  39. // determine render pipeline
  40. currentPipeline = this._acquirePipeline( stageVertex, stageFragment, object, nodeBuilder );
  41. cache.currentPipeline = currentPipeline;
  42. // keep track of all used times
  43. currentPipeline.usedTimes ++;
  44. stageVertex.usedTimes ++;
  45. stageFragment.usedTimes ++;
  46. // events
  47. material.addEventListener( 'dispose', cache.dispose );
  48. } else {
  49. currentPipeline = cache.currentPipeline;
  50. }
  51. return currentPipeline;
  52. }
  53. dispose() {
  54. this.pipelines = [];
  55. this.objectCache = new WeakMap();
  56. this.shaderModules = {
  57. vertex: new Map(),
  58. fragment: new Map()
  59. };
  60. }
  61. _acquirePipeline( stageVertex, stageFragment, object, nodeBuilder ) {
  62. let pipeline;
  63. const pipelines = this.pipelines;
  64. // check for existing pipeline
  65. const cacheKey = this._computeCacheKey( stageVertex, stageFragment, object );
  66. for ( let i = 0, il = pipelines.length; i < il; i ++ ) {
  67. const preexistingPipeline = pipelines[ i ];
  68. if ( preexistingPipeline.cacheKey === cacheKey ) {
  69. pipeline = preexistingPipeline;
  70. break;
  71. }
  72. }
  73. if ( pipeline === undefined ) {
  74. pipeline = new WebGPURenderPipeline( this.device, this.utils );
  75. pipeline.init( cacheKey, stageVertex, stageFragment, object, nodeBuilder );
  76. pipelines.push( pipeline );
  77. }
  78. return pipeline;
  79. }
  80. _computeCacheKey( stageVertex, stageFragment, object ) {
  81. const material = object.material;
  82. const utils = this.utils;
  83. const parameters = [
  84. stageVertex.id, stageFragment.id,
  85. material.transparent, material.blending, material.premultipliedAlpha,
  86. material.blendSrc, material.blendDst, material.blendEquation,
  87. material.blendSrcAlpha, material.blendDstAlpha, material.blendEquationAlpha,
  88. material.colorWrite,
  89. material.depthWrite, material.depthTest, material.depthFunc,
  90. material.stencilWrite, material.stencilFunc,
  91. material.stencilFail, material.stencilZFail, material.stencilZPass,
  92. material.stencilFuncMask, material.stencilWriteMask,
  93. material.side,
  94. utils.getSampleCount(),
  95. utils.getCurrentEncoding(), utils.getCurrentColorFormat(), utils.getCurrentDepthStencilFormat(),
  96. utils.getPrimitiveTopology( object )
  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 utils = this.utils;
  167. const sampleCount = utils.getSampleCount();
  168. const encoding = utils.getCurrentEncoding();
  169. const colorFormat = utils.getCurrentColorFormat();
  170. const depthStencilFormat = utils.getCurrentDepthStencilFormat();
  171. if ( cache.sampleCount !== sampleCount || cache.encoding !== encoding ||
  172. cache.colorFormat !== colorFormat || cache.depthStencilFormat !== depthStencilFormat ) {
  173. cache.sampleCount = sampleCount;
  174. cache.encoding = encoding;
  175. cache.colorFormat = colorFormat;
  176. cache.depthStencilFormat = depthStencilFormat;
  177. needsUpdate = true;
  178. }
  179. return needsUpdate;
  180. }
  181. }
  182. export default WebGPURenderPipelines;