Skip to content

Commit 20742ba

Browse files
committed
adjusted parameters in depth algorithm so it works with 16-bit depth buffer
1 parent d82f12d commit 20742ba

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

core/src/processing/opengl/PGraphics2DX.java

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,20 +1285,23 @@ protected void end2D() {
12851285
// SHADER FILTER
12861286

12871287

1288+
12881289
@Override
12891290
public void filter(PShader shader) {
12901291
// The filter method needs to use the geometry-generation in the base class.
12911292
// We could re-implement it here, but this is easier.
1292-
if (!useParentImpl) {
1293-
useOldP2D();
1294-
super.filter(shader);
1295-
useNewP2D();
1296-
} else {
1297-
super.filter(shader);
1298-
}
1293+
// if (!useParentImpl) {
1294+
// useOldP2D();
1295+
// super.filter(shader);
1296+
// useNewP2D();
1297+
// } else {
1298+
// super.filter(shader);
1299+
// }
1300+
super.filter(shader);
12991301
}
13001302

13011303

1304+
13021305
//////////////////////////////////////////////////////////////
13031306

13041307
// SHADER API
@@ -1531,28 +1534,31 @@ private void init() {
15311534
private int vbo;
15321535
private int texWidth, texHeight;
15331536

1537+
// Determination of the smallest increments and largest-greater-than-minus-one
1538+
// https://en.wikipedia.org/wiki/Half-precision_floating-point_format
1539+
1540+
// Using the smallest positive normal number in half (16-bit) precision, which is how the depth
1541+
// buffer is initialized in mobile
1542+
private float smallestDepthIncrement = (float)Math.pow(2, -14);
1543+
1544+
// As the limit for the depth increase, we take the minus the largest number less than one in
1545+
// half (16-bit) precision
1546+
private float largestNumberLessThanOne = 1 - (float)Math.pow(2, -11);
15341547

15351548
private void incrementDepth() {
1536-
//by resetting the depth buffer when needed, we are able to have arbitrarily many
1537-
//layers, unlimited by depth buffer precision. in practice, the precision of this
1538-
//algorithm seems to be very good (~1,000,000 layers), so it pretty much won't happen
1539-
//unless you're drawing enough geometry per frame to set your computer on fire
1540-
if (depth < -0.9999f) {
1549+
// By resetting the depth buffer when needed, we are able to have arbitrarily many
1550+
// layers, unlimited by depth buffer precision. In practice, the precision of this
1551+
// algorithm seems to be acceptable (exactly (1 + 1 - pow(2, -11))/pow(2, -14) = 32,760 layers)
1552+
// for mobile.
1553+
if (depth < -largestNumberLessThanOne) {
15411554
flushBuffer();
15421555
pgl.clear(PGL.DEPTH_BUFFER_BIT);
1543-
//depth test will fail at depth = 1.0 after clearing the depth buffer,
1544-
//but since we always increment before drawing anything, this should be okay
1556+
// Depth test will fail at depth = 1.0 after clearing the depth buffer,
1557+
// But since we always increment before drawing anything, this should be okay
15451558
depth = 1.0f;
15461559
}
15471560

1548-
//found to be a small but reliable increment value for a 24-bit depth buffer
1549-
//through trial and error. as numbers approach zero, absolute floating point
1550-
//precision increases, while absolute fixed point precision stays the same,
1551-
//so regardless of representation, this value should work for all depths in
1552-
//range (-1, 1), as long as it works for depths at either end of the range
1553-
depth -= 0.000001f;
1554-
1555-
//TODO: use an increment value based on good math instead of lazy trial-and-error
1561+
depth -= smallestDepthIncrement;
15561562
}
15571563

15581564

@@ -1694,6 +1700,10 @@ private PShader getShader() {
16941700
return shader;
16951701
}
16961702

1703+
@Override
1704+
protected PShader getPolyShader(boolean lit, boolean tex) {
1705+
return super.getPolyShader(lit, tex);
1706+
}
16971707

16981708
private void setAttribs() {
16991709
pgl.vertexAttribPointer(positionLoc, 3, PGL.FLOAT, false, vertSize, 0);

0 commit comments

Comments
 (0)