Pipelines.js 7.9 KB

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