Spaces:
Sleeping
Sleeping
File size: 5,841 Bytes
e7b2eb4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | // @flow
import assert from 'assert';
import type Program from './program';
import type VertexBuffer from '../gl/vertex_buffer';
import type IndexBuffer from '../gl/index_buffer';
import type Context from '../gl/context';
class VertexArrayObject {
context: Context;
boundProgram: ?Program<*>;
boundLayoutVertexBuffer: ?VertexBuffer;
boundPaintVertexBuffers: Array<VertexBuffer>;
boundIndexBuffer: ?IndexBuffer;
boundVertexOffset: ?number;
boundDynamicVertexBuffer: ?VertexBuffer;
boundDynamicVertexBuffer2: ?VertexBuffer;
vao: any;
constructor() {
this.boundProgram = null;
this.boundLayoutVertexBuffer = null;
this.boundPaintVertexBuffers = [];
this.boundIndexBuffer = null;
this.boundVertexOffset = null;
this.boundDynamicVertexBuffer = null;
this.vao = null;
}
bind(context: Context,
program: Program<*>,
layoutVertexBuffer: VertexBuffer,
paintVertexBuffers: Array<VertexBuffer>,
indexBuffer: ?IndexBuffer,
vertexOffset: ?number,
dynamicVertexBuffer: ?VertexBuffer,
dynamicVertexBuffer2: ?VertexBuffer) {
this.context = context;
let paintBuffersDiffer = this.boundPaintVertexBuffers.length !== paintVertexBuffers.length;
for (let i = 0; !paintBuffersDiffer && i < paintVertexBuffers.length; i++) {
if (this.boundPaintVertexBuffers[i] !== paintVertexBuffers[i]) {
paintBuffersDiffer = true;
}
}
const isFreshBindRequired = (
!this.vao ||
this.boundProgram !== program ||
this.boundLayoutVertexBuffer !== layoutVertexBuffer ||
paintBuffersDiffer ||
this.boundIndexBuffer !== indexBuffer ||
this.boundVertexOffset !== vertexOffset ||
this.boundDynamicVertexBuffer !== dynamicVertexBuffer ||
this.boundDynamicVertexBuffer2 !== dynamicVertexBuffer2
);
if (!context.extVertexArrayObject || isFreshBindRequired) {
this.freshBind(program, layoutVertexBuffer, paintVertexBuffers, indexBuffer, vertexOffset, dynamicVertexBuffer, dynamicVertexBuffer2);
} else {
context.bindVertexArrayOES.set(this.vao);
if (dynamicVertexBuffer) {
// The buffer may have been updated. Rebind to upload data.
dynamicVertexBuffer.bind();
}
if (indexBuffer && indexBuffer.dynamicDraw) {
indexBuffer.bind();
}
if (dynamicVertexBuffer2) {
dynamicVertexBuffer2.bind();
}
}
}
freshBind(program: Program<*>,
layoutVertexBuffer: VertexBuffer,
paintVertexBuffers: Array<VertexBuffer>,
indexBuffer: ?IndexBuffer,
vertexOffset: ?number,
dynamicVertexBuffer: ?VertexBuffer,
dynamicVertexBuffer2: ?VertexBuffer) {
let numPrevAttributes;
const numNextAttributes = program.numAttributes;
const context = this.context;
const gl = context.gl;
if (context.extVertexArrayObject) {
if (this.vao) this.destroy();
this.vao = context.extVertexArrayObject.createVertexArrayOES();
context.bindVertexArrayOES.set(this.vao);
numPrevAttributes = 0;
// store the arguments so that we can verify them when the vao is bound again
this.boundProgram = program;
this.boundLayoutVertexBuffer = layoutVertexBuffer;
this.boundPaintVertexBuffers = paintVertexBuffers;
this.boundIndexBuffer = indexBuffer;
this.boundVertexOffset = vertexOffset;
this.boundDynamicVertexBuffer = dynamicVertexBuffer;
this.boundDynamicVertexBuffer2 = dynamicVertexBuffer2;
} else {
numPrevAttributes = context.currentNumAttributes || 0;
// Disable all attributes from the previous program that aren't used in
// the new program. Note: attribute indices are *not* program specific!
for (let i = numNextAttributes; i < numPrevAttributes; i++) {
// WebGL breaks if you disable attribute 0.
// http://stackoverflow.com/questions/20305231
assert(i !== 0);
gl.disableVertexAttribArray(i);
}
}
layoutVertexBuffer.enableAttributes(gl, program);
for (const vertexBuffer of paintVertexBuffers) {
vertexBuffer.enableAttributes(gl, program);
}
if (dynamicVertexBuffer) {
dynamicVertexBuffer.enableAttributes(gl, program);
}
if (dynamicVertexBuffer2) {
dynamicVertexBuffer2.enableAttributes(gl, program);
}
layoutVertexBuffer.bind();
layoutVertexBuffer.setVertexAttribPointers(gl, program, vertexOffset);
for (const vertexBuffer of paintVertexBuffers) {
vertexBuffer.bind();
vertexBuffer.setVertexAttribPointers(gl, program, vertexOffset);
}
if (dynamicVertexBuffer) {
dynamicVertexBuffer.bind();
dynamicVertexBuffer.setVertexAttribPointers(gl, program, vertexOffset);
}
if (indexBuffer) {
indexBuffer.bind();
}
if (dynamicVertexBuffer2) {
dynamicVertexBuffer2.bind();
dynamicVertexBuffer2.setVertexAttribPointers(gl, program, vertexOffset);
}
context.currentNumAttributes = numNextAttributes;
}
destroy() {
if (this.vao) {
this.context.extVertexArrayObject.deleteVertexArrayOES(this.vao);
this.vao = null;
}
}
}
export default VertexArrayObject;
|