diff --git a/lib/ext/glxrender.js b/lib/ext/glxrender.js index 6eba8c7..014d544 100644 --- a/lib/ext/glxrender.js +++ b/lib/ext/glxrender.js @@ -138,8 +138,16 @@ module.exports = function(GLX, ctx) { } function render(ctxLocal) { + if (!ctxLocal) // ctxLocal overrides ctx passed during creation of renderContext ctxLocal = ctx; + + if (buffers.length == 0) { + buffers = []; + currentLength = 0; + return; + } + GLX.Render(ctxLocal, buffers); buffers = []; currentLength = 0; @@ -255,15 +263,20 @@ module.exports = function(GLX, ctx) { }, TexImage2D: function(target, level, internalFormat, width, height, border, format, type, data) { + render(); + var typeSize = []; typeSize[constants.FLOAT] = 4; typeSize[constants.BYTE] = 1; typeSize[constants.UNSIGNED_BYTE] = 1; - var res = commandBuffer(110, 56 + data.length*typeSize[type]); - res[4] = 0; // swapbytes - res[5] = 0; // lsbfirst - res.writeUInt16LE(0, 6); // unused + var res = new Buffer(60 + data.length*typeSize[type]); + res.writeUInt32LE(res.length, 0); + res.writeUInt32LE(110, 4); + + res[8] = 0; // swapbytes + res[9] = 0; // lsbfirst + res.writeUInt16LE(0, 10); // unused /* defaults: (from http://stackoverflow.com/questions/21563590/glteximage2d-protocol-arguments?noredirect=1#comment32577251_21563590 ) @@ -277,35 +290,58 @@ module.exports = function(GLX, ctx) { */ - res.writeUInt32LE(0, 8); // rowlength - res.writeUInt32LE(0, 12); // skiprows - res.writeUInt32LE(0, 16); // skippixels - res.writeUInt32LE(4, 20); // alignment + res.writeUInt32LE(0, 12); // rowlength + res.writeUInt32LE(0, 16); // skiprows + res.writeUInt32LE(0, 20); // skippixels + res.writeUInt32LE(4, 24); // alignment - res.writeUInt32LE(target, 24); - res.writeUInt32LE(level, 28); - res.writeUInt32LE(internalFormat, 32); - res.writeUInt32LE(width, 36); - res.writeUInt32LE(height, 40); - res.writeUInt32LE(border, 44); - res.writeUInt32LE(format, 48); - res.writeUInt32LE(type, 52); + res.writeUInt32LE(target, 28); + res.writeUInt32LE(level, 32); + res.writeUInt32LE(internalFormat, 36); + res.writeUInt32LE(width, 40); + res.writeUInt32LE(height, 44); + res.writeUInt32LE(border, 48); + res.writeUInt32LE(format, 52); + res.writeUInt32LE(type, 56); switch(type) { case constants.FLOAT: for (var i=0; i < data.length; ++i) - res.writeFloatLE(data[i], 56+i*typeSize[type]); + res.writeFloatLE(data[i], 60+i*typeSize[type]); break; case constants.BYTE: case constants.UNSIGNED_BYTE: for (var i=0; i < data.length; ++i) - res[56+i] = data[i]; + res[60+i] = data[i]; break; default: throw new Error('unsupported texture type:' + type); } - buffers.push(res); + + // bake sure buffer for glxRender request is emptied first + render(); + + var dataLen = res.length; + var maxSize = 65536*4 - 16 - 8; + var totalRequests = 1 + parseInt(dataLen / maxSize) - 1; + if (dataLen % maxSize) + totalRequests++; + + var pos = 0; + var reqNum = 1; + while(dataLen > 0) { + if (dataLen < maxSize) { + GLX.RenderLarge(ctx, reqNum, totalRequests, res.slice(pos)); + break; + } else { + GLX.RenderLarge(ctx, reqNum, totalRequests, res.slice(pos, pos + maxSize)); + pos += maxSize; + dataLen -= maxSize; + reqNum++; + } + } }, + TexCoord2f: function(x, y) { serialize2f(54, x, y); }