Pipelines.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import DataMap from './DataMap.js';
  2. import RenderPipeline from './RenderPipeline.js';
  3. import ComputePipeline from './ComputePipeline.js';
  4. import ProgrammableStage from './ProgrammableStage.js';
  5. class Pipelines extends DataMap {
  6. constructor( backend, nodes ) {
  7. super();
  8. this.backend = backend;
  9. this.nodes = nodes;
  10. this.bindings = null; // set by the bindings
  11. this.caches = new Map();
  12. this.programs = {
  13. vertex: new Map(),
  14. fragment: new Map(),
  15. compute: new Map()
  16. };
  17. }
  18. getForCompute( computeNode ) {
  19. const { backend } = this;
  20. const data = this.get( computeNode );
  21. if ( data.pipeline === undefined ) {
  22. // release previous cache
  23. this._releasePipeline( computeNode );
  24. // get shader
  25. const nodeBuilder = this.nodes.getForCompute( computeNode );
  26. // programmable stage
  27. let stageCompute = this.programs.compute.get( nodeBuilder.computeShader );
  28. if ( stageCompute === undefined ) {
  29. stageCompute = new ProgrammableStage( nodeBuilder.computeShader, 'compute' );
  30. this.programs.compute.set( nodeBuilder.computeShader, stageCompute );
  31. backend.createProgram( stageCompute );
  32. }
  33. // determine compute pipeline
  34. const pipeline = this._getComputePipeline( stageCompute );
  35. // keep track of all used times
  36. pipeline.usedTimes ++;
  37. stageCompute.usedTimes ++;
  38. //
  39. data.pipeline = pipeline;
  40. }
  41. return data.pipeline;
  42. }
  43. getForRender( renderObject ) {
  44. const { backend } = this;
  45. const data = this.get( renderObject );
  46. if ( this._needsUpdate( renderObject ) ) {
  47. // release previous cache
  48. this._releasePipeline( renderObject );
  49. // get shader
  50. const nodeBuilder = this.nodes.getForRender( renderObject );
  51. // programmable stages
  52. let stageVertex = this.programs.vertex.get( nodeBuilder.vertexShader );
  53. if ( stageVertex === undefined ) {
  54. stageVertex = new ProgrammableStage( nodeBuilder.vertexShader, 'vertex' );
  55. this.programs.vertex.set( nodeBuilder.vertexShader, stageVertex );
  56. backend.createProgram( stageVertex );
  57. }
  58. let stageFragment = this.programs.fragment.get( nodeBuilder.fragmentShader );
  59. if ( stageFragment === undefined ) {
  60. stageFragment = new ProgrammableStage( nodeBuilder.fragmentShader, 'fragment' );
  61. this.programs.fragment.set( nodeBuilder.fragmentShader, stageFragment );
  62. backend.createProgram( stageFragment );
  63. }
  64. // determine render pipeline
  65. const pipeline = this._getRenderPipeline( renderObject, stageVertex, stageFragment );
  66. // keep track of all used times
  67. pipeline.usedTimes ++;
  68. stageVertex.usedTimes ++;
  69. stageFragment.usedTimes ++;
  70. //
  71. data.pipeline = pipeline;
  72. }
  73. return data.pipeline;
  74. }
  75. delete( object ) {
  76. this._releasePipeline( object );
  77. super.delete( object );
  78. }
  79. dispose() {
  80. super.dispose();
  81. this.caches = new Map();
  82. this.programs = {
  83. vertex: new Map(),
  84. fragment: new Map(),
  85. compute: new Map()
  86. };
  87. }
  88. _getComputePipeline( stageCompute ) {
  89. // check for existing pipeline
  90. const cacheKey = 'compute:' + stageCompute.id;
  91. let pipeline = this.caches.get( cacheKey );
  92. if ( pipeline === undefined ) {
  93. pipeline = new ComputePipeline( cacheKey, stageCompute );
  94. this.caches.set( cacheKey, pipeline );
  95. this.backend.createComputePipeline( pipeline );
  96. }
  97. return pipeline;
  98. }
  99. _getRenderPipeline( renderObject, stageVertex, stageFragment ) {
  100. // check for existing pipeline
  101. const cacheKey = this._getRenderCacheKey( renderObject, stageVertex, stageFragment );
  102. let pipeline = this.caches.get( cacheKey );
  103. if ( pipeline === undefined ) {
  104. pipeline = new RenderPipeline( cacheKey, stageVertex, stageFragment );
  105. this.caches.set( cacheKey, pipeline );
  106. renderObject.pipeline = pipeline;
  107. this.backend.createRenderPipeline( renderObject );
  108. } else {
  109. // assign a shared pipeline to renderObject
  110. renderObject.pipeline = pipeline;
  111. }
  112. return pipeline;
  113. }
  114. _getRenderCacheKey( renderObject, stageVertex, stageFragment ) {
  115. const { material } = renderObject;
  116. const parameters = [
  117. stageVertex.id, stageFragment.id,
  118. material.transparent, material.blending, material.premultipliedAlpha,
  119. material.blendSrc, material.blendDst, material.blendEquation,
  120. material.blendSrcAlpha, material.blendDstAlpha, material.blendEquationAlpha,
  121. material.colorWrite,
  122. material.depthWrite, material.depthTest, material.depthFunc,
  123. material.stencilWrite, material.stencilFunc,
  124. material.stencilFail, material.stencilZFail, material.stencilZPass,
  125. material.stencilFuncMask, material.stencilWriteMask,
  126. material.side,
  127. this.backend.getCacheKey( renderObject )
  128. ];
  129. return parameters.join();
  130. }
  131. _releasePipeline( object ) {
  132. const pipeline = this.get( object ).pipeline;
  133. //this.bindings.delete( object );
  134. if ( pipeline && -- pipeline.usedTimes === 0 ) {
  135. this.caches.delete( pipeline.cacheKey );
  136. if ( pipeline.isComputePipeline ) {
  137. this._releaseProgram( pipeline.computeProgram );
  138. } else {
  139. this._releaseProgram( pipeline.vertexProgram );
  140. this._releaseProgram( pipeline.fragmentProgram );
  141. }
  142. }
  143. }
  144. _releaseProgram( program ) {
  145. if ( -- program.usedTimes === 0 ) {
  146. const code = program.code;
  147. const stage = program.stage;
  148. this.programs[ stage ].delete( code );
  149. }
  150. }
  151. _needsUpdate( renderObject ) {
  152. const data = this.get( renderObject );
  153. const material = renderObject.material;
  154. let needsUpdate = this.backend.needsUpdate( renderObject );
  155. // check material state
  156. if ( data.material !== material || data.materialVersion !== material.version ||
  157. data.transparent !== material.transparent || data.blending !== material.blending || data.premultipliedAlpha !== material.premultipliedAlpha ||
  158. data.blendSrc !== material.blendSrc || data.blendDst !== material.blendDst || data.blendEquation !== material.blendEquation ||
  159. data.blendSrcAlpha !== material.blendSrcAlpha || data.blendDstAlpha !== material.blendDstAlpha || data.blendEquationAlpha !== material.blendEquationAlpha ||
  160. data.colorWrite !== material.colorWrite ||
  161. data.depthWrite !== material.depthWrite || data.depthTest !== material.depthTest || data.depthFunc !== material.depthFunc ||
  162. data.stencilWrite !== material.stencilWrite || data.stencilFunc !== material.stencilFunc ||
  163. data.stencilFail !== material.stencilFail || data.stencilZFail !== material.stencilZFail || data.stencilZPass !== material.stencilZPass ||
  164. data.stencilFuncMask !== material.stencilFuncMask || data.stencilWriteMask !== material.stencilWriteMask ||
  165. data.side !== material.side
  166. ) {
  167. data.material = material; data.materialVersion = material.version;
  168. data.transparent = material.transparent; data.blending = material.blending; data.premultipliedAlpha = material.premultipliedAlpha;
  169. data.blendSrc = material.blendSrc; data.blendDst = material.blendDst; data.blendEquation = material.blendEquation;
  170. data.blendSrcAlpha = material.blendSrcAlpha; data.blendDstAlpha = material.blendDstAlpha; data.blendEquationAlpha = material.blendEquationAlpha;
  171. data.colorWrite = material.colorWrite;
  172. data.depthWrite = material.depthWrite; data.depthTest = material.depthTest; data.depthFunc = material.depthFunc;
  173. data.stencilWrite = material.stencilWrite; data.stencilFunc = material.stencilFunc;
  174. data.stencilFail = material.stencilFail; data.stencilZFail = material.stencilZFail; data.stencilZPass = material.stencilZPass;
  175. data.stencilFuncMask = material.stencilFuncMask; data.stencilWriteMask = material.stencilWriteMask;
  176. data.side = material.side;
  177. needsUpdate = true;
  178. }
  179. return needsUpdate || data.pipeline !== undefined;
  180. }
  181. }
  182. export default Pipelines;