flush method
Flushes all batched drawing commands to the GPU.
This method sends all data to WebGL for rendering. It is called automatically when necessary (e.g., when changing blend modes, textures, or when the batch buffer is full), but can also be called manually to ensure all pending draw operations are executed.
The application loop in App typically calls this once per frame
after onRender
.
Implementation
void flush() {
if (_vertexCount == 0) {
return;
}
if (_renderState._primitiveType == null) {
die("State error: Batch started without a _PrimitiveType set!");
}
final int dataSizeInBytes = _vertexCount * _vertexSizeInBytes;
gl
..useProgram(_program)
..bindVertexArray(_vao)
..bindBuffer(GL.ARRAY_BUFFER, _interleavedBuffer)
..bufferData(GL.ARRAY_BUFFER, _batchCapacityInBytes.toJS, GL.STREAM_DRAW)
..bufferSubData(GL.ARRAY_BUFFER, 0, _batchedInterleavedData.buffer.asUint8List(0, dataSizeInBytes).toJS);
switch (_renderState._primitiveType) {
case _PrimitiveType.quads:
final numQuadsInBatch = _vertexCount ~/ 4;
if (_vertexCount % 4 != 0) {
die("Warning: Drawing QUAD batch with vertex count $_vertexCount not divisible by 4.");
return;
}
final numIndicesToDraw = numQuadsInBatch * 6;
gl
..bindBuffer(GL.ELEMENT_ARRAY_BUFFER, _quadIndexBuffer)
..drawElements(GL.TRIANGLES, numIndicesToDraw, GL.UNSIGNED_SHORT, 0)
..bindBuffer(GL.ELEMENT_ARRAY_BUFFER, null);
break;
case _PrimitiveType.points:
case _PrimitiveType.lines:
case _PrimitiveType.lineStrip:
case _PrimitiveType.triangles:
case _PrimitiveType.triangleFan:
gl.drawArrays(_renderState._primitiveType!.toGLPrimitive(), 0, _vertexCount);
break;
default:
die("Unknown batch primitive type: $_renderState.primitiveType");
}
_vertexCount = 0;
}