Skip to content

Commit

Permalink
Back out change to FXAA to fix resize artifact.
Browse files Browse the repository at this point in the history
  • Loading branch information
bagnell committed Jul 13, 2015
1 parent d85a963 commit dc1109c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 72 deletions.
139 changes: 69 additions & 70 deletions Source/Scene/FXAA.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,98 +24,102 @@ define([
/**
* @private
*/
var FXAA = function() {
var FXAA = function(context) {
this._texture = undefined;
this._depthTexture = undefined;
this._depthRenderbuffer = undefined;
this._fbo = undefined;
this._command = undefined;
this._clearCommand = undefined;

this._step = new Cartesian2();
var clearCommand = new ClearCommand({
color : new Color(0.0, 0.0, 0.0, 0.0),
depth : 1.0,
owner : this
});
this._clearCommand = clearCommand;
};

function createResources(fxaa, context, width, height) {
function destroyResources(fxaa) {
fxaa._fbo = fxaa._fbo && fxaa._fbo.destroy();

var texture = context.createTexture2D({
width : width,
height : height,
pixelFormat : PixelFormat.RGBA,
pixelDatatype : PixelDatatype.UNSIGNED_BYTE
});

var depthTexture;
var depthRenderbuffer;

if (context.depthTexture) {
depthTexture = context.createTexture2D({
width : width,
height : height,
pixelFormat : PixelFormat.DEPTH_COMPONENT,
pixelDatatype : PixelDatatype.UNSIGNED_SHORT
});
} else {
depthRenderbuffer = context.createRenderbuffer({
width : width,
height : height,
format : RenderbufferFormat.DEPTH_COMPONENT16
});
fxaa._texture = fxaa._texture && fxaa._texture.destroy();
fxaa._depthTexture = fxaa._depthTexture && fxaa._depthTexture.destroy();
fxaa._depthRenderbuffer = fxaa._depthRenderbuffer && fxaa._depthRenderbuffer.destroy();

fxaa._fbo = undefined;
fxaa._texture = undefined;
fxaa._depthTexture = undefined;
fxaa._depthRenderbuffer = undefined;

if (defined(fxaa._command)) {
fxaa._command.shaderProgram = fxaa._command.shaderProgram && fxaa._command.shaderProgram.destroy();
fxaa._command = undefined;
}

fxaa._fbo = context.createFramebuffer({
colorTextures : [texture],
depthTexture : depthTexture,
depthRenderbuffer : depthRenderbuffer
});
}

FXAA.prototype.update = function(context, texture) {
FXAA.prototype.update = function(context) {
var width = context.drawingBufferWidth;
var height = context.drawingBufferHeight;

if (!defined(this._fbo)) {
createResources(this, context, width, height);
}
var fxaaTexture = this._texture;
var textureChanged = !defined(fxaaTexture) || fxaaTexture.width !== width || fxaaTexture.height !== height;
if (textureChanged) {
this._texture = this._texture && this._texture.destroy();
this._depthTexture = this._depthTexture && this._depthTexture.destroy();
this._depthRenderbuffer = this._depthRenderbuffer && this._depthRenderbuffer.destroy();

if (!defined(texture)) {
var fboTexture = this._fbo.getColorTexture(0);
var texWidth = fboTexture.width;
var texHeight = fboTexture.height;
this._texture = context.createTexture2D({
width : width,
height : height,
pixelFormat : PixelFormat.RGBA,
pixelDatatype : PixelDatatype.UNSIGNED_BYTE
});

if (texWidth !== width || texHeight !== height) {
createResources(this, context, width, height);
if (context.depthTexture) {
this._depthTexture = context.createTexture2D({
width : width,
height : height,
pixelFormat : PixelFormat.DEPTH_COMPONENT,
pixelDatatype : PixelDatatype.UNSIGNED_SHORT
});
} else {
this._depthRenderbuffer = context.createRenderbuffer({
width : width,
height : height,
format : RenderbufferFormat.DEPTH_COMPONENT16
});
}

this._texture = fboTexture;
} else {
this._texture = texture;
}

this._step.x = 1.0 / this._texture.width;
this._step.y = 1.0 / this._texture.height;
if (!defined(this._fbo) || textureChanged) {
this._fbo = this._fbo && this._fbo.destroy();

if (!defined(this._clearCommand)) {
this._clearCommand = new ClearCommand({
color : new Color(0.0, 0.0, 0.0, 0.0),
depth : 1.0,
owner : this
this._fbo = context.createFramebuffer({
colorTextures : [this._texture],
depthTexture : this._depthTexture,
depthRenderbuffer : this._depthRenderbuffer,
destroyAttachments : false
});
}

if (!defined(this._command)) {
var that = this;
this._command = context.createViewportQuadCommand(FXAAFS, {
renderState : context.createRenderState(),
uniformMap : {
u_texture : function() {
return that._texture;
},
u_step : function() {
return that._step;
}
},
owner : this
});
}

if (textureChanged) {
var that = this;
var step = new Cartesian2(1.0 / this._texture.width, 1.0 / this._texture.height);
this._command.uniformMap = {
u_texture : function() {
return that._texture;
},
u_step : function() {
return step;
}
};
}
};

FXAA.prototype.execute = function(context, passState) {
Expand All @@ -141,12 +145,7 @@ define([
};

FXAA.prototype.destroy = function() {
this._fbo = this._fbo && this._fbo.destroy();

if (defined(this._command)) {
this._command.shaderProgram = this._command.shaderProgram && this._command.shaderProgram.destroy();
}

destroyResources(this);
return destroyObject(this);
};

Expand Down
8 changes: 6 additions & 2 deletions Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -1370,8 +1370,7 @@ define([
// If supported, configure FXAA to use the globe depth color texture and clear the FXAA framebuffer.
var useFXAA = !picking && scene.fxaa;
if (useFXAA) {
var fxaaTexture = !useOIT && defined(scene._globeDepth) ? scene._globeDepth._colorTexture : undefined;
scene._fxaa.update(context, fxaaTexture);
scene._fxaa.update(context);
scene._fxaa.clear(context, passState, clearColor);
}

Expand Down Expand Up @@ -1520,6 +1519,11 @@ define([
}

if (useFXAA) {
if (!useOIT && useGlobeDepthFramebuffer) {
passState.framebuffer = scene._fxaa.getColorFramebuffer();
scene._globeDepth.executeCopyColor(context, passState);
}

passState.framebuffer = originalFramebuffer;
scene._fxaa.execute(context, passState);
}
Expand Down

0 comments on commit dc1109c

Please sign in to comment.