FXAA.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*global define*/
  2. define([
  3. '../Core/Cartesian2',
  4. '../Core/Color',
  5. '../Core/defined',
  6. '../Core/destroyObject',
  7. '../Core/PixelFormat',
  8. '../Renderer/ClearCommand',
  9. '../Renderer/PixelDatatype',
  10. '../Renderer/RenderbufferFormat',
  11. '../Shaders/PostProcessFilters/FXAA'
  12. ], function(
  13. Cartesian2,
  14. Color,
  15. defined,
  16. destroyObject,
  17. PixelFormat,
  18. ClearCommand,
  19. PixelDatatype,
  20. RenderbufferFormat,
  21. FXAAFS) {
  22. "use strict";
  23. /**
  24. * @private
  25. */
  26. var FXAA = function(context) {
  27. this._texture = undefined;
  28. this._depthTexture = undefined;
  29. this._depthRenderbuffer = undefined;
  30. this._fbo = undefined;
  31. this._command = undefined;
  32. var clearCommand = new ClearCommand({
  33. color : new Color(0.0, 0.0, 0.0, 0.0),
  34. depth : 1.0,
  35. owner : this
  36. });
  37. this._clearCommand = clearCommand;
  38. };
  39. function destroyResources(fxaa) {
  40. fxaa._fbo = fxaa._fbo && fxaa._fbo.destroy();
  41. fxaa._texture = fxaa._texture && fxaa._texture.destroy();
  42. fxaa._depthTexture = fxaa._depthTexture && fxaa._depthTexture.destroy();
  43. fxaa._depthRenderbuffer = fxaa._depthRenderbuffer && fxaa._depthRenderbuffer.destroy();
  44. fxaa._fbo = undefined;
  45. fxaa._texture = undefined;
  46. fxaa._depthTexture = undefined;
  47. fxaa._depthRenderbuffer = undefined;
  48. if (defined(fxaa._command)) {
  49. fxaa._command.shaderProgram = fxaa._command.shaderProgram && fxaa._command.shaderProgram.destroy();
  50. fxaa._command = undefined;
  51. }
  52. }
  53. FXAA.prototype.update = function(context) {
  54. var width = context.drawingBufferWidth;
  55. var height = context.drawingBufferHeight;
  56. var fxaaTexture = this._texture;
  57. var textureChanged = !defined(fxaaTexture) || fxaaTexture.width !== width || fxaaTexture.height !== height;
  58. if (textureChanged) {
  59. this._texture = this._texture && this._texture.destroy();
  60. this._depthTexture = this._depthTexture && this._depthTexture.destroy();
  61. this._depthRenderbuffer = this._depthRenderbuffer && this._depthRenderbuffer.destroy();
  62. this._texture = context.createTexture2D({
  63. width : width,
  64. height : height,
  65. pixelFormat : PixelFormat.RGBA,
  66. pixelDatatype : PixelDatatype.UNSIGNED_BYTE
  67. });
  68. if (context.depthTexture) {
  69. this._depthTexture = context.createTexture2D({
  70. width : width,
  71. height : height,
  72. pixelFormat : PixelFormat.DEPTH_COMPONENT,
  73. pixelDatatype : PixelDatatype.UNSIGNED_SHORT
  74. });
  75. } else {
  76. this._depthRenderbuffer = context.createRenderbuffer({
  77. width : width,
  78. height : height,
  79. format : RenderbufferFormat.DEPTH_COMPONENT16
  80. });
  81. }
  82. }
  83. if (!defined(this._fbo) || textureChanged) {
  84. this._fbo = this._fbo && this._fbo.destroy();
  85. this._fbo = context.createFramebuffer({
  86. colorTextures : [this._texture],
  87. depthTexture : this._depthTexture,
  88. depthRenderbuffer : this._depthRenderbuffer,
  89. destroyAttachments : false
  90. });
  91. }
  92. if (!defined(this._command)) {
  93. this._command = context.createViewportQuadCommand(FXAAFS, {
  94. renderState : context.createRenderState(),
  95. owner : this
  96. });
  97. }
  98. if (textureChanged) {
  99. var that = this;
  100. var step = new Cartesian2(1.0 / this._texture.width, 1.0 / this._texture.height);
  101. this._command.uniformMap = {
  102. u_texture : function() {
  103. return that._texture;
  104. },
  105. u_step : function() {
  106. return step;
  107. }
  108. };
  109. }
  110. };
  111. FXAA.prototype.execute = function(context, passState) {
  112. this._command.execute(context, passState);
  113. };
  114. FXAA.prototype.clear = function(context, passState, clearColor) {
  115. var framebuffer = passState.framebuffer;
  116. passState.framebuffer = this._fbo;
  117. Color.clone(clearColor, this._clearCommand.color);
  118. this._clearCommand.execute(context, passState);
  119. passState.framebuffer = framebuffer;
  120. };
  121. FXAA.prototype.getColorFramebuffer = function() {
  122. return this._fbo;
  123. };
  124. FXAA.prototype.isDestroyed = function() {
  125. return false;
  126. };
  127. FXAA.prototype.destroy = function() {
  128. destroyResources(this);
  129. return destroyObject(this);
  130. };
  131. return FXAA;
  132. });