[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Public WebGL] For review: ANGLE_timer_query extension



So I have made the argument that callbacks would be no worse than polling, I'd like to illustrate that in code. Below is an example of how thousands of queries would be used but only one callback per frame is attached.

// create a 10k query cache
var queryPool = [];
for(var i=0; i<1024*10; i++){
  queryPool.push(ext.createQueryANGLE());
}

// frame object to hold queries
var Frame = function(){ this.queries = []; }

// called at the start of a frame, returnes used queries
Frame.prototype.start = function(frameCount){
  this.frameCount = frameCount;
  while(this.queries.length > 0){
    queryPool.push(this.queries.pop());
  }
}

// called during a frame to obtain queries from the pool
Frame.prototype.createQuery = function(){
  var query = queryPool.pop();
  query.>null;
  this.queries.push(query);
  return query;
}

// called at the end of a frame, attaches callback to the last query of the frame
Frame.prototype.finish = function(callback){
  this.queries[this.queries.length-1].>
}

// create a 10 frame cache
var framePool = [];
for(var i=0; i<10; i++){
   framePool.push(new Frame());
}

// passed to frame.finish as callback
var >function(frame){
  // whatever processing on frame.queries, they're all ready
  framePool.push(frame); // return frame to the frame pool
}

var frameCounter = 0;
var draw = function(){
  // start a frame
  var frame = framePool.pop();
  frame.start(frameCounter++);
  
  // work with your thousands of query objects  
  for(var i=0; i<1000; i++){
   var query = frame.createQuery();
   ext.beginQueryANGLE(ext.TIME_ELAPSED_ANGLE, query);
   gl.drawElements(...);
   ext.endQueryANGLE(ext.TIME_ELAPSED_ANGLE);
  }

  // attach one callback per frame
  frame.finish(onFrameMeasured);

  // wash rhinse repeat
  requestAnimationFrame(draw);
}

requestAnimationFrame(draw);