Skip to content

Commit

Permalink
simpler fbo layer code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeanticode committed Sep 12, 2015
1 parent 9d819b8 commit 99039bf
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 64 deletions.
78 changes: 42 additions & 36 deletions core/src/processing/opengl/PGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public abstract class PGL {

// Parameters

protected static boolean USE_FBOLAYER_BY_DEFAULT = false;
public static int REQUESTED_DEPTH_BITS = 24;
public static int REQUESTED_STENCIL_BITS = 8;
public static int REQUESTED_ALPHA_BITS = 8;
Expand Down Expand Up @@ -128,16 +127,17 @@ public abstract class PGL {
* order to make sure the lines are always on top of the fill geometry */
protected static float STROKE_DISPLACEMENT = 0.999f;

protected static boolean DOUBLE_BUFFERED = true;

// ........................................................

// FBO layer

protected boolean requestedFBOLayer = false;
protected boolean requestedFBOLayerReset = false;
protected boolean fboLayerEnabled = false;
protected boolean fboLayerCreated = false;
protected boolean fboLayerInUse = false;
protected boolean firstFrame = true;
protected boolean fboLayerEnabledReq = false;
protected boolean fboLayerDisableReq = false;
protected boolean fbolayerResetReq = false;
public int reqNumSamples;
protected int numSamples;

Expand Down Expand Up @@ -442,10 +442,6 @@ public PGL(PGraphicsOpenGL pg) {
glMultiDepthStencil = allocateIntBuffer(1);
glMultiDepth = allocateIntBuffer(1);
glMultiStencil = allocateIntBuffer(1);

fboLayerCreated = false;
fboLayerInUse = false;
firstFrame = false;
}

byteBuffer = allocateByteBuffer(1);
Expand Down Expand Up @@ -494,39 +490,44 @@ static public int smoothToSamples(int smooth) {


protected int getReadFramebuffer() {
return fboLayerInUse ? glColorFbo.get(0) : 0;
return fboLayerEnabled ? glColorFbo.get(0) : 0;
}


protected int getDrawFramebuffer() {
if (fboLayerInUse) return 1 < numSamples ? glMultiFbo.get(0) :
glColorFbo.get(0);
if (fboLayerEnabled) return 1 < numSamples ? glMultiFbo.get(0) :
glColorFbo.get(0);
else return 0;
}


protected int getDefaultDrawBuffer() {
return fboLayerInUse ? COLOR_ATTACHMENT0 : BACK;
return fboLayerEnabled ? COLOR_ATTACHMENT0 : BACK;
}


protected int getDefaultReadBuffer() {
return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT;
return fboLayerEnabled ? COLOR_ATTACHMENT0 : FRONT;
}


protected boolean isFBOBacked() {;
return fboLayerInUse;
return fboLayerEnabled;
}


public void enableFBOLayer() {
fboLayerEnabledReq = true;
}


public void requestFBOLayer() {
requestedFBOLayer = true;
public void disableFBOLayer() {
fboLayerDisableReq = true;
}


public void requestFBOLayerReset() {
requestedFBOLayerReset = true;
public void resetFBOLayer() {
fbolayerResetReq = true;
}


Expand Down Expand Up @@ -639,7 +640,7 @@ public void initPresentMode(float x, float y) {
presentMode = true;
presentX = x;
presentY = y;
requestFBOLayer();
enableFBOLayer();
}


Expand Down Expand Up @@ -693,12 +694,17 @@ protected void beginRender() {
pclearColor = clearColor;
clearColor = false;

if (requestedFBOLayer) {
if (requestedFBOLayerReset) {
if (fboLayerEnabledReq) {
fboLayerEnabled = true;
fboLayerEnabledReq = false;
}

if (fboLayerEnabled) {
if (fbolayerResetReq) {
destroyFBOLayer();
requestedFBOLayerReset = false;
fbolayerResetReq = false;
}
if (!fboLayerCreated) {
if (!fboLayerCreated && DOUBLE_BUFFERED) {
createFBOLayer();
}

Expand All @@ -711,7 +717,7 @@ protected void beginRender() {
bindFramebufferImpl(FRAMEBUFFER, glMultiFbo.get(0));
}

if (firstFrame) {
if (sketch.frameCount == 0) {
// No need to draw back color buffer because we are in the first frame.
int argb = graphics.backgroundColor;
float a = ((argb >> 24) & 0xff) / 255.0f;
Expand All @@ -735,18 +741,12 @@ protected void beginRender() {
0, 0, (int)(scale * graphics.width), (int)(scale * graphics.height),
0, 0, graphics.width, graphics.height);
}

fboLayerInUse = true;
} else {
fboLayerInUse = false;
}

firstFrame = false;
}


protected void endRender(int windowColor) {
if (fboLayerInUse) {
if (fboLayerEnabled) {
syncBackTexture();

// Draw the contents of the back texture to the screen framebuffer.
Expand Down Expand Up @@ -804,8 +804,17 @@ protected void endRender(int windowColor) {
int temp = frontTex;
frontTex = backTex;
backTex = temp;

if (fboLayerDisableReq) {
fboLayerEnabled = false;
fboLayerDisableReq = false;
}
} else if (!clearColor && 0 < sketch.frameCount || !sketch.isLooping()) {
requestFBOLayer();
enableFBOLayer();
}

if (fboLayerEnabledReq && !fboLayerCreated && !DOUBLE_BUFFERED) {
createFBOLayer();
}
}

Expand Down Expand Up @@ -944,10 +953,7 @@ protected void destroyFBOLayer() {
deleteRenderbuffers(1, glMultiDepth);
deleteRenderbuffers(1, glMultiStencil);
}

fboLayerCreated = false;
fboLayerInUse = false;
// firstFrame = false;
}


Expand Down
45 changes: 21 additions & 24 deletions core/src/processing/opengl/PGLES.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public class PGLES extends PGL {
// GLES

static {
DOUBLE_BUFFERED = false;

MIN_DIRECT_BUFFER_SIZE = 1;
INDEX_TYPE = GLES20.GL_UNSIGNED_SHORT;

Expand Down Expand Up @@ -134,13 +136,7 @@ protected void setFrameRate(float fps) { }
protected void initSurface(int antialias) {
glview = (GLSurfaceView)sketch.getSurfaceView();
reqNumSamples = qualityToSamples(antialias);

registerListeners();

fboLayerCreated = false;
fboLayerInUse = false;
firstFrame = true;
setFps = false;
}


Expand Down Expand Up @@ -170,13 +166,13 @@ protected int getStencilBits() {

@Override
protected int getDefaultDrawBuffer() {
return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT;
return fboLayerEnabled ? COLOR_ATTACHMENT0 : FRONT;
}


@Override
protected int getDefaultReadBuffer() {
return fboLayerInUse ? COLOR_ATTACHMENT0 : FRONT;
return fboLayerEnabled ? COLOR_ATTACHMENT0 : FRONT;
}


Expand Down Expand Up @@ -233,26 +229,27 @@ protected void initFBOLayer() {
IntBuffer buf = null;
buf = allocateDirectIntBuffer(fboWidth * fboHeight);

// Copy the contents of the front and back screen buffers to the textures
// of the FBO, so they are properly initialized. Note that the front buffer
// of the default framebuffer (the screen) contains the previous frame:
// https://www.opengl.org/wiki/Default_Framebuffer
// so it is copied to the front texture of the FBO layer:
if (pclearColor || 0 < pgeomCount || !sketch.isLooping()) {
readBuffer(FRONT);
} else {
// ...except when the previous frame has not been cleared and nothing was
// renderered while looping. In this case the back buffer, which holds the
// initial state of the previous frame, still contains the most up-to-date
// screen state.
readBuffer(BACK);
}
// // Copy the contents of the front and back screen buffers to the textures
// // of the FBO, so they are properly initialized. Note that the front buffer
// // of the default framebuffer (the screen) contains the previous frame:
// // https://www.opengl.org/wiki/Default_Framebuffer
// // so it is copied to the front texture of the FBO layer:
// if (pclearColor || 0 < pgeomCount || !sketch.isLooping()) {
// readBuffer(FRONT);
// } else {
// // ...except when the previous frame has not been cleared and nothing was
// // renderered while looping. In this case the back buffer, which holds the
// // initial state of the previous frame, still contains the most up-to-date
// // screen state.
// readBuffer(BACK);
// }
readBuffer(FRONT);
readPixelsImpl(0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf);
bindTexture(TEXTURE_2D, glColorTex.get(frontTex));
texSubImage2D(TEXTURE_2D, 0, 0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf);

readBuffer(BACK);
readPixelsImpl(0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf);
// readBuffer(BACK);
// readPixelsImpl(0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf);
bindTexture(TEXTURE_2D, glColorTex.get(backTex));
texSubImage2D(TEXTURE_2D, 0, 0, 0, fboWidth, fboHeight, RGBA, UNSIGNED_BYTE, buf);

Expand Down
6 changes: 3 additions & 3 deletions core/src/processing/opengl/PGraphicsOpenGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ public void requestDraw() {
}


/*
/*
@Override
// Java only
public PSurface createSurface() { // ignore
Expand Down Expand Up @@ -5766,7 +5766,7 @@ public void filter(PShader shader) {

boolean needEndDraw = false;
if (primaryGraphics) {
pgl.requestFBOLayer();
pgl.enableFBOLayer();
} else if (!drawing) {
beginDraw();
needEndDraw = true;
Expand Down Expand Up @@ -5842,7 +5842,7 @@ public void filter(PShader shader) {
@Override
public void copy(int sx, int sy, int sw, int sh,
int dx, int dy, int dw, int dh) {
if (primaryGraphics) pgl.requestFBOLayer();
if (primaryGraphics) pgl.enableFBOLayer();
loadTexture();
if (filterTexture == null || filterTexture.contextIsOutdated()) {
filterTexture = new Texture(this, texture.width, texture.height,
Expand Down
2 changes: 1 addition & 1 deletion core/src/processing/opengl/PShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1320,7 +1320,7 @@ protected void unbindTyped() {
if (-1 < normalLoc) pgl.disableVertexAttribArray(normalLoc);

if (-1 < ppixelsLoc) {
pgl.requestFBOLayer();
pgl.enableFBOLayer();
pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit);
currentPG.unbindFrontTexture();
pgl.activeTexture(PGL.TEXTURE0);
Expand Down

0 comments on commit 99039bf

Please sign in to comment.