low-WeakReference diet

This commit is contained in:
Jakub Valtar
2015-09-25 23:58:10 -04:00
parent fd40e11180
commit c61bc0072f
6 changed files with 245 additions and 403 deletions

View File

@@ -28,7 +28,6 @@ import processing.core.PApplet;
import processing.core.PConstants;
import processing.opengl.PGraphicsOpenGL.GLResourceFrameBuffer;
import java.lang.ref.WeakReference;
import java.nio.IntBuffer;
/**
@@ -43,7 +42,7 @@ import java.nio.IntBuffer;
*/
public class FrameBuffer implements PConstants {
protected WeakReference<PGraphicsOpenGL> pg;
protected PGraphicsOpenGL pg;
protected PGL pgl;
protected int context; // The context that created this framebuffer.
@@ -73,7 +72,7 @@ public class FrameBuffer implements PConstants {
FrameBuffer(PGraphicsOpenGL pg) {
this.pg = new WeakReference<PGraphicsOpenGL>(pg);
this.pg = pg;
pgl = pg.pgl;
context = pgl.createEmptyContext();
}
@@ -153,17 +152,15 @@ public class FrameBuffer implements PConstants {
public void clear() {
PGraphicsOpenGL g = pg.get();
if (g == null) return;
g.pushFramebuffer();
g.setFramebuffer(this);
pg.pushFramebuffer();
pg.setFramebuffer(this);
pgl.clearDepth(1);
pgl.clearStencil(0);
pgl.clearColor(0, 0, 0, 0);
pgl.clear(PGL.DEPTH_BUFFER_BIT |
PGL.STENCIL_BUFFER_BIT |
PGL.COLOR_BUFFER_BIT);
g.popFramebuffer();
pg.popFramebuffer();
}
public void copyColor(FrameBuffer dest) {
@@ -179,14 +176,12 @@ public class FrameBuffer implements PConstants {
}
public void copy(FrameBuffer dest, int mask) {
PGraphicsOpenGL g = pg.get();
if (g == null) return;
pgl.bindFramebufferImpl(PGL.READ_FRAMEBUFFER, this.glFbo);
pgl.bindFramebufferImpl(PGL.DRAW_FRAMEBUFFER, dest.glFbo);
pgl.blitFramebuffer(0, 0, this.width, this.height,
0, 0, dest.width, dest.height, mask, PGL.NEAREST);
pgl.bindFramebufferImpl(PGL.READ_FRAMEBUFFER, g.getCurrentFB().glFbo);
pgl.bindFramebufferImpl(PGL.DRAW_FRAMEBUFFER, g.getCurrentFB().glFbo);
pgl.bindFramebufferImpl(PGL.READ_FRAMEBUFFER, pg.getCurrentFB().glFbo);
pgl.bindFramebufferImpl(PGL.DRAW_FRAMEBUFFER, pg.getCurrentFB().glFbo);
}
public void bind() {
@@ -198,11 +193,9 @@ public class FrameBuffer implements PConstants {
}
public void finish() {
PGraphicsOpenGL g = pg.get();
if (g == null) return;
if (noDepth) {
// No need to clear depth buffer because depth testing was disabled.
if (g.getHint(ENABLE_DEPTH_TEST)) {
if (pg.getHint(ENABLE_DEPTH_TEST)) {
pgl.enable(PGL.DEPTH_TEST);
} else {
pgl.disable(PGL.DEPTH_TEST);
@@ -258,8 +251,6 @@ public class FrameBuffer implements PConstants {
public void setColorBuffers(Texture[] textures, int n) {
PGraphicsOpenGL g = pg.get();
if (g == null) return;
if (screenFb) return;
if (numColorBuffers != PApplet.min(n, textures.length)) {
@@ -271,8 +262,8 @@ public class FrameBuffer implements PConstants {
colorBufferTex[i] = textures[i];
}
g.pushFramebuffer();
g.setFramebuffer(this);
pg.pushFramebuffer();
pg.setFramebuffer(this);
// Making sure nothing is attached.
for (int i = 0; i < numColorBuffers; i++) {
@@ -288,14 +279,11 @@ public class FrameBuffer implements PConstants {
pgl.validateFramebuffer();
g.popFramebuffer();
pg.popFramebuffer();
}
public void swapColorBuffers() {
PGraphicsOpenGL g = pg.get();
if (g == null) return;
for (int i = 0; i < numColorBuffers - 1; i++) {
int i1 = (i + 1);
Texture tmp = colorBufferTex[i];
@@ -303,8 +291,8 @@ public class FrameBuffer implements PConstants {
colorBufferTex[i1] = tmp;
}
g.pushFramebuffer();
g.setFramebuffer(this);
pg.pushFramebuffer();
pg.setFramebuffer(this);
for (int i = 0; i < numColorBuffers; i++) {
pgl.framebufferTexture2D(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0 + i,
colorBufferTex[i].glTarget,
@@ -312,7 +300,7 @@ public class FrameBuffer implements PConstants {
}
pgl.validateFramebuffer();
g.popFramebuffer();
pg.popFramebuffer();
}
@@ -433,12 +421,10 @@ public class FrameBuffer implements PConstants {
protected void initColorBufferMultisample() {
PGraphicsOpenGL g = pg.get();
if (g == null) return;
if (screenFb) return;
g.pushFramebuffer();
g.setFramebuffer(this);
pg.pushFramebuffer();
pg.setFramebuffer(this);
// glMultisample = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
pgl.bindRenderbuffer(PGL.RENDERBUFFER, glMultisample);
@@ -447,21 +433,19 @@ public class FrameBuffer implements PConstants {
pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.COLOR_ATTACHMENT0,
PGL.RENDERBUFFER, glMultisample);
g.popFramebuffer();
pg.popFramebuffer();
}
protected void initPackedDepthStencilBuffer() {
PGraphicsOpenGL g = pg.get();
if (g == null) return;
if (screenFb) return;
if (width == 0 || height == 0) {
throw new RuntimeException("PFramebuffer: size undefined.");
}
g.pushFramebuffer();
g.setFramebuffer(this);
pg.pushFramebuffer();
pg.setFramebuffer(this);
// glDepthStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
pgl.bindRenderbuffer(PGL.RENDERBUFFER, glDepthStencil);
@@ -479,21 +463,19 @@ public class FrameBuffer implements PConstants {
pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.STENCIL_ATTACHMENT,
PGL.RENDERBUFFER, glDepthStencil);
g.popFramebuffer();
pg.popFramebuffer();
}
protected void initDepthBuffer() {
PGraphicsOpenGL g = pg.get();
if (g == null) return;
if (screenFb) return;
if (width == 0 || height == 0) {
throw new RuntimeException("PFramebuffer: size undefined.");
}
g.pushFramebuffer();
g.setFramebuffer(this);
pg.pushFramebuffer();
pg.setFramebuffer(this);
// glDepth = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
pgl.bindRenderbuffer(PGL.RENDERBUFFER, glDepth);
@@ -517,21 +499,19 @@ public class FrameBuffer implements PConstants {
pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.DEPTH_ATTACHMENT,
PGL.RENDERBUFFER, glDepth);
g.popFramebuffer();
pg.popFramebuffer();
}
protected void initStencilBuffer() {
PGraphicsOpenGL g = pg.get();
if (g == null) return;
if (screenFb) return;
if (width == 0 || height == 0) {
throw new RuntimeException("PFramebuffer: size undefined.");
}
g.pushFramebuffer();
g.setFramebuffer(this);
pg.pushFramebuffer();
pg.setFramebuffer(this);
// glStencil = PGraphicsOpenGL.createRenderBufferObject(context, pgl);
pgl.bindRenderbuffer(PGL.RENDERBUFFER, glStencil);
@@ -554,7 +534,7 @@ public class FrameBuffer implements PConstants {
pgl.framebufferRenderbuffer(PGL.FRAMEBUFFER, PGL.STENCIL_ATTACHMENT,
PGL.RENDERBUFFER, glStencil);
g.popFramebuffer();
pg.popFramebuffer();
}