ComputeEngine.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. var renderStateScratch;
  21. var drawCommandScratch = new DrawCommand({
  22. primitiveType : PrimitiveType.TRIANGLES
  23. });
  24. var 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 ((!defined(renderStateScratch)) ||
  47. (renderStateScratch.viewport.width !== width) ||
  48. (renderStateScratch.viewport.height !== height)) {
  49. renderStateScratch = RenderState.fromCache({
  50. viewport : new BoundingRectangle(0, 0, width, height)
  51. });
  52. }
  53. return renderStateScratch;
  54. }
  55. ComputeEngine.prototype.execute = function(computeCommand) {
  56. //>>includeStart('debug', pragmas.debug);
  57. Check.defined('computeCommand', computeCommand);
  58. //>>includeEnd('debug');
  59. // This may modify the command's resources, so do error checking afterwards
  60. if (defined(computeCommand.preExecute)) {
  61. computeCommand.preExecute(computeCommand);
  62. }
  63. //>>includeStart('debug', pragmas.debug);
  64. if (!defined(computeCommand.fragmentShaderSource) && !defined(computeCommand.shaderProgram)) {
  65. throw new DeveloperError('computeCommand.fragmentShaderSource or computeCommand.shaderProgram is required.');
  66. }
  67. Check.defined('computeCommand.outputTexture', computeCommand.outputTexture);
  68. //>>includeEnd('debug');
  69. var outputTexture = computeCommand.outputTexture;
  70. var width = outputTexture.width;
  71. var height = outputTexture.height;
  72. var context = this._context;
  73. var vertexArray = defined(computeCommand.vertexArray) ? computeCommand.vertexArray : context.getViewportQuadVertexArray();
  74. var shaderProgram = defined(computeCommand.shaderProgram) ? computeCommand.shaderProgram : createViewportQuadShader(context, computeCommand.fragmentShaderSource);
  75. var framebuffer = createFramebuffer(context, outputTexture);
  76. var renderState = createRenderState(width, height);
  77. var uniformMap = computeCommand.uniformMap;
  78. var clearCommand = clearCommandScratch;
  79. clearCommand.framebuffer = framebuffer;
  80. clearCommand.renderState = renderState;
  81. clearCommand.execute(context);
  82. var drawCommand = drawCommandScratch;
  83. drawCommand.vertexArray = vertexArray;
  84. drawCommand.renderState = renderState;
  85. drawCommand.shaderProgram = shaderProgram;
  86. drawCommand.uniformMap = uniformMap;
  87. drawCommand.framebuffer = framebuffer;
  88. drawCommand.execute(context);
  89. framebuffer.destroy();
  90. if (!computeCommand.persists) {
  91. shaderProgram.destroy();
  92. if (defined(computeCommand.vertexArray)) {
  93. vertexArray.destroy();
  94. }
  95. }
  96. if (defined(computeCommand.postExecute)) {
  97. computeCommand.postExecute(outputTexture);
  98. }
  99. };
  100. ComputeEngine.prototype.isDestroyed = function() {
  101. return false;
  102. };
  103. ComputeEngine.prototype.destroy = function() {
  104. return destroyObject(this);
  105. };
  106. export default ComputeEngine;