Skip to content

Commit

Permalink
Automatically send Render buffer if too large or before any GLX call
Browse files Browse the repository at this point in the history
  • Loading branch information
sidorares committed Dec 19, 2012
1 parent a951aa2 commit 8e83314
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions lib/x11/ext/glxrender.js
Expand Up @@ -2,10 +2,24 @@

var constants = require('./glxconstants');

var MAX_SMALL_RENDER=65000;

module.exports = function(GLX, ctx) {
buffers = [];
var currentLength = 0;

function commandBuffer(opcode, len) {
if (currentLength + len > MAX_SMALL_RENDER) {
console.log("Flushing buffer ", currentLength);
render();
}
if (len > MAX_SMALL_RENDER)
{
throw Error('Buffer too big. Please implement RenderLarge request');
// renderLarge();
}

currentLength += len;
var res = Buffer(len);
res.writeUInt16LE(len, 0);
res.writeUInt16LE(opcode, 2);
Expand Down Expand Up @@ -91,13 +105,17 @@ module.exports = function(GLX, ctx) {
buffers.push(res);
}

function render(ctxLocal) {
if (!ctxLocal) // ctxLocal overrides ctx passed during creation of renderContext
ctxLocal = ctx;
console.log("render called");
GLX.Render(ctxLocal, buffers);
buffers = [];
currentLength = 0;
}

var renderContext = {
render: function(ctxLocal) {
if (!ctxLocal) // ctxLocalOverrides ctx passed during creation of renderContext
ctxLocal = ctx;
GLX.Render(ctx, buffers);
buffers = [];
},
Render: render,
Begin: function(what) {
serialize1i(4, what);
},
Expand Down Expand Up @@ -132,12 +150,18 @@ module.exports = function(GLX, ctx) {
Vertex3f: function(x, y, z) {
serialize3fv(70, x, y, z);
},
Vertex3fv: function(v) {
serialize3fv(70, v[0], v[1], v[2]);
},
Color3f: function(r, g, b) {
serialize3fv(8, r, g, b);
},
Normal3f: function(x, y, z) {
serialize3fv(30, x, y, z);
},
Normal3fv: function(v) {
serialize3fv(70, v[0], v[1], v[2]);
},
Color4f: function(r, g, b, a) {
serialize4fv(16, r, g, b, a);
},
Expand Down Expand Up @@ -191,7 +215,15 @@ module.exports = function(GLX, ctx) {

// bind some glx functions
'NewList EndList GenLists SwapBuffers Finish'.split(' ').forEach(function(name) {
renderContext[name] = GLX[name].bind(GLX, ctx);
// todo: small camelCase ? to be consistent with webgl api
//renderContext[name] = GLX[name].bind(GLX, ctx);

// flush render buffer before glx requests
renderContext[name] = function(p1, p2, p3, p4, p5, p6, p7, p8) {
console.log(name);
renderContext.render();
GLX[name](ctx, p1, p2, p3, p4, p5, p6, p7, p8);
}
});

return renderContext;
Expand Down

0 comments on commit 8e83314

Please sign in to comment.