Skip to content

Commit e113f04

Browse files
committed
fix backbuffer/drawingbuffer size calc
1 parent 4e317f4 commit e113f04

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

src/utils.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,29 @@ export function glTypeToTypedArray(type) {
9797

9898
export function getDrawingbufferInfo(gl) {
9999
return {
100-
samples: gl.getParameter(gl.SAMPLES),
100+
samples: gl.getParameter(gl.SAMPLES) || 1,
101101
depthBits: gl.getParameter(gl.DEPTH_BITS),
102+
stencilBits: gl.getParameter(gl.STENCIL_BITS),
102103
contextAttributes: gl.getContextAttributes(),
103104
};
104105
}
106+
107+
function computeDepthStencilSize(drawingBufferInfo) {
108+
const {depthBits, stencilBits} = drawingBufferInfo
109+
const depthSize = (depthBits + stencilBits + 7) / 8 | 0;
110+
return depthSize === 3 ? 4 : depthSize;
111+
}
112+
105113
export function computeDrawingbufferSize(gl, drawingBufferInfo) {
106-
// this will need to change for hi-color support
107114
if (gl.isContextLost()) {
108115
return 0;
109116
}
110-
const {samples, depthBits, contextAttributes} = drawingBufferInfo;
111-
const size = gl.drawingBufferWidth * gl.drawingBufferHeight * 4 || 0;
112-
const depth = contextAttributes.depth ? 1 : 0;
113-
const stencil = contextAttributes.stencil ? 1 : 0;
114-
const depthSize = Math.min(stencil + depthBits > 16 ? 4 : 2, 4);
115-
return size + size * samples + size * depth * depthSize;
117+
const {samples} = drawingBufferInfo;
118+
// this will need to change for hi-color support
119+
const colorSize = 4;
120+
const size = gl.drawingBufferWidth * gl.drawingBufferHeight;
121+
const depthStencilSize = computeDepthStencilSize(drawingBufferInfo);
122+
return size * colorSize + size * samples * colorSize + size * depthStencilSize;
116123
}
117124

118125
// I know this is not a full check

test/tests/info-tests.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,22 @@ import {createContext, createContext2} from '../webgl.js';
55

66
describe('info tests', () => {
77
it('test base state webgl1', () => {
8-
const {ext, drawingbufferSize} = createContext();
8+
const {gl, ext, drawingbufferSize} = createContext();
99
assertTruthy(ext, 'got extension');
1010

11+
// compute this size ourselves so we can compare
12+
const size = gl.drawingBufferWidth * gl.drawingBufferHeight;
13+
const colorSize = 4;
14+
const samples = gl.getParameter(gl.SAMPLES) || 1;
15+
const depthStencilBytes = (gl.getParameter(gl.DEPTH_BITS) + gl.getParameter(gl.STENCIL_BITS) + 7) / 8 | 0;
16+
const depthStencilSize = depthStencilBytes === 3 ? 4 : depthStencilBytes;
17+
const canvasSize =
18+
size * colorSize + // display buffer
19+
size * colorSize * samples + // drawing buffer
20+
size * depthStencilSize; // depth + stencil buffer
21+
22+
assertEqual(drawingbufferSize, canvasSize);
23+
1124
const info = ext.getMemoryInfo();
1225
const {memory, resources} = info;
1326

0 commit comments

Comments
 (0)