ComputeEngine.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import BoundingRectangle from "../Core/BoundingRectangle.js";
  2. import Check from "../Core/Check.js";
  3. import Color from "../Core/Color.js";
  4. import defined from "../Core/defined.js";
  5. import destroyObject from "../Core/destroyObject.js";
  6. import DeveloperError from "../Core/DeveloperError.js";
  7. import PrimitiveType from "../Core/PrimitiveType.js";
  8. import ViewportQuadVS from "../Shaders/ViewportQuadVS.js";
  9. import ClearCommand from "./ClearCommand.js";
  10. import DrawCommand from "./DrawCommand.js";
  11. import Framebuffer from "./Framebuffer.js";
  12. import RenderState from "./RenderState.js";
  13. import ShaderProgram from "./ShaderProgram.js";
  14. /**
  15. * @private
  16. */
  17. function ComputeEngine(context) {
  18. this._context = context;
  19. }
  20. let renderStateScratch;
  21. const drawCommandScratch = new DrawCommand({
  22. primitiveType: PrimitiveType.TRIANGLES,
  23. });
  24. const clearCommandScratch = new ClearCommand({
  25. color: new Color(0.0, 0.0, 0.0, 0.0),
  26. });
  27. function createFramebuffer(context, outputTexture) {
  28. return new Framebuffer({
  29. context: context,
  30. colorTextures: [outputTexture],
  31. destroyAttachments: false,
  32. });
  33. }
  34. function createViewportQuadShader(context, fragmentShaderSource) {
  35. return ShaderProgram.fromCache({
  36. context: context,
  37. vertexShaderSource: ViewportQuadVS,
  38. fragmentShaderSource: fragmentShaderSource,
  39. attributeLocations: {
  40. position: 0,
  41. textureCoordinates: 1,
  42. },
  43. });
  44. }
  45. function createRenderState(width, height) {
  46. if (
  47. !defined(renderStateScratch) ||
  48. renderStateScratch.viewport.width !== width ||
  49. renderStateScratch.viewport.height !== height
  50. ) {
  51. renderStateScratch = RenderState.fromCache({
  52. viewport: new BoundingRectangle(0, 0, width, height),
  53. });
  54. }
  55. return renderStateScratch;
  56. }
  57. ComputeEngine.prototype.execute = function (computeCommand) {
  58. //>>includeStart('debug', pragmas.debug);
  59. Check.defined("computeCommand", computeCommand);
  60. //>>includeEnd('debug');
  61. // This may modify the command's resources, so do error checking afterwards
  62. if (defined(computeCommand.preExecute)) {
  63. computeCommand.preExecute(computeCommand);
  64. }
  65. //>>includeStart('debug', pragmas.debug);
  66. if (
  67. !defined(computeCommand.fragmentShaderSource) &&
  68. !defined(computeCommand.shaderProgram)
  69. ) {
  70. throw new DeveloperError(
  71. "computeCommand.fragmentShaderSource or computeCommand.shaderProgram is required."
  72. );
  73. }
  74. Check.defined("computeCommand.outputTexture", computeCommand.outputTexture);
  75. //>>includeEnd('debug');
  76. const outputTexture = computeCommand.outputTexture;
  77. const width = outputTexture.width;
  78. const height = outputTexture.height;
  79. const context = this._context;
  80. const vertexArray = defined(computeCommand.vertexArray)
  81. ? computeCommand.vertexArray
  82. : context.getViewportQuadVertexArray();
  83. const shaderProgram = defined(computeCommand.shaderProgram)
  84. ? computeCommand.shaderProgram
  85. : createViewportQuadShader(context, computeCommand.fragmentShaderSource);
  86. const framebuffer = createFramebuffer(context, outputTexture);
  87. const renderState = createRenderState(width, height);
  88. const uniformMap = computeCommand.uniformMap;
  89. const clearCommand = clearCommandScratch;
  90. clearCommand.framebuffer = framebuffer;
  91. clearCommand.renderState = renderState;
  92. clearCommand.execute(context);
  93. const drawCommand = drawCommandScratch;
  94. drawCommand.vertexArray = vertexArray;
  95. drawCommand.renderState = renderState;
  96. drawCommand.shaderProgram = shaderProgram;
  97. drawCommand.uniformMap = uniformMap;
  98. drawCommand.framebuffer = framebuffer;
  99. drawCommand.execute(context);
  100. framebuffer.destroy();
  101. if (!computeCommand.persists) {
  102. shaderProgram.destroy();
  103. if (defined(computeCommand.vertexArray)) {
  104. vertexArray.destroy();
  105. }
  106. }
  107. if (defined(computeCommand.postExecute)) {
  108. computeCommand.postExecute(outputTexture);
  109. }
  110. };
  111. ComputeEngine.prototype.isDestroyed = function () {
  112. return false;
  113. };
  114. ComputeEngine.prototype.destroy = function () {
  115. return destroyObject(this);
  116. };
  117. export default ComputeEngine;