//"use strict";
var LibraryGL = {
$GL: {
hashtable: function(name) {
if (!this._hashtables) {
this._hashtables = {};
}
if (!(name in this._hashtables)) {
this._hashtables[name] = {
table: {},
counter: 1,
add: function(obj) {
var id = this.counter++;
this.table[id] = obj;
return id;
},
get: function(id) {
if( id == 0 ) return null;
#if ASSERTIONS
assert(id < this.counter, "Invalid id " + id + " for the hashtable " + name);
#endif
return this.table[id];
},
id: function(obj) {
for (var i = 1; i < this.counter; ++i) {
if (obj == this.table[i]) {
return i;
}
}
return 0;
},
remove: function(id) {
if( id == 0 ) return;
#if ASSERTIONS
assert(id < this.counter, "Invalid id " + id + " for the hashtable " + name);
#endif
delete this.table[id];
},
lookup: function(v) {
for (var i = 1; i < this.counter; i++)
if (this.table[i] == v)
return i;
return 0;
},
};
}
return this._hashtables[name];
},
},
glGetString: function(name_) {
switch(name_) {
case 0x1F00 /* GL_VENDOR */:
case 0x1F01 /* GL_RENDERER */:
case 0x1F02 /* GL_VERSION */:
return allocate(intArrayFromString(Module.ctx.getParameter(name_)), 'i8', ALLOC_NORMAL);
case 0x1F03 /* GL_EXTENSIONS */:
return allocate(intArrayFromString(Module.ctx.getSupportedExtensions().join(' ')), 'i8', ALLOC_NORMAL);
default:
throw 'Failure: Invalid glGetString value: ' + name_;
}
},
glGetIntegerv__deps: ['$GL'],
glGetIntegerv: function(name_, p) {
var result = Module.ctx.getParameter(name_);
switch (typeof(result)) {
case "number":
{{{ makeSetValue('p', '0', 'result', 'i32') }}};
break;
case "boolean":
{{{ makeSetValue('p', '0', 'result ? 1 : 0', 'i8') }}};
break;
case "string":
throw 'Native code calling glGetIntegerv(' + name_ + ') on a name which returns a string!';
case "object":
if (result === null) {
{{{ makeSetValue('p', '0', '0', 'i32') }}};
} else if (result instanceof Float32Array ||
result instanceof Uint32Array ||
result instanceof Int32Array ||
result instanceof Array) {
for (var i = 0; i < result.length; ++i) {
{{{ makeSetValue('p', 'i*4', 'result[i]', 'i32') }}};
}
} else if (result instanceof WebGLBuffer) {
{{{ makeSetValue('p', '0', 'GL.hashtable("buffer").id(result)', 'i32') }}};
} else if (result instanceof WebGLProgram) {
{{{ makeSetValue('p', '0', 'GL.hashtable("program").id(result)', 'i32') }}};
} else if (result instanceof WebGLFramebuffer) {
{{{ makeSetValue('p', '0', 'GL.hashtable("framebuffer").id(result)', 'i32') }}};
} else if (result instanceof WebGLRenderbuffer) {
{{{ makeSetValue('p', '0', 'gl.hashtable("renderbuffer").id(result)', 'i32') }}};
} else if (result instanceof WebGLTexture) {
{{{ makeSetValue('p', '0', 'gl.hashtable("texture").id(result)', 'i32') }}};
} else {
throw 'Unknown object returned from WebGL getParameter';
}
break;
case "undefined":
throw 'Native code calling glGetIntegerv(' + name_ + ') and it returns undefined';
default:
throw 'Why did we hit the default case?';
}
},
glGetFloatv__deps: ['$GL'],
glGetFloatv: function(name_, p) {
var result = Module.ctx.getParameter(name_);
switch (typeof(result)) {
case "number":
{{{ makeSetValue('p', '0', 'result', 'float') }}};
break;
case "boolean":
{{{ makeSetValue('p', '0', 'result ? 1.0 : 0.0', 'float') }}};
break;
case "string":
{{{ makeSetValue('p', '0', '0', 'float') }}};
case "object":
if (result === null) {
throw 'Native code calling glGetFloatv(' + name_ + ') and it returns null';
} else if (result instanceof Float32Array ||
result instanceof Uint32Array ||
result instanceof Int32Array