Skip to content

Commit

Permalink
New JITed vtables - interpolation code still needed
Browse files Browse the repository at this point in the history
  • Loading branch information
benvanik committed Aug 3, 2012
1 parent 6f6023a commit a96d07f
Show file tree
Hide file tree
Showing 6 changed files with 507 additions and 422 deletions.
2 changes: 1 addition & 1 deletion anvil_rules/templates/simstate_js.mako
Expand Up @@ -23,7 +23,7 @@ goog.require('${state.super}');
*/
${state.name} = function(entity, opt_variableTable) {
var variableTable = opt_variableTable || gf.sim.EntityState.getVariableTable(
${state.name}.declareVariables);
${state.name}.declareVariables, this);
goog.base(this, entity, variableTable);
% for i, var in enumerate(state.vars):

Expand Down
60 changes: 60 additions & 0 deletions src/gf/net/packetreader.js
Expand Up @@ -292,6 +292,21 @@ gf.net.PacketReader.prototype.readVec3 = function(value) {
};


/**
* Reads a value from the buffer.
* The result of this function will be reset on the next read operation and must
* only be used to copy the value to some other structure.
* @return {!goog.vec.Vec3.Float32} Value pointer.
*/
gf.net.PacketReader.prototype.readVec3Temp = function() {
goog.asserts.assert(this.offset + 3 * 4 <= this.buffer.length);
for (var n = 0; n < 3 * 4; n++) {
this.float32byte_[n] = this.buffer[this.offset++];
}
return this.float32_;
};


/**
* Reads a value from the buffer.
* @param {!goog.vec.Vec4.Float32} value Value to receive the contents.
Expand All @@ -305,6 +320,21 @@ gf.net.PacketReader.prototype.readVec4 = function(value) {
};


/**
* Reads a value from the buffer.
* The result of this function will be reset on the next read operation and must
* only be used to copy the value to some other structure.
* @return {!goog.vec.Vec3.Float32} Value pointer.
*/
gf.net.PacketReader.prototype.readVec4Temp = function() {
goog.asserts.assert(this.offset + 4 * 4 <= this.buffer.length);
for (var n = 0; n < 4 * 4; n++) {
this.float32byte_[n] = this.buffer[this.offset++];
}
return this.float32_;
};


/**
* Reads a value from the buffer.
* @param {!goog.vec.Mat3.Type} value Value to receive the contents.
Expand All @@ -318,6 +348,21 @@ gf.net.PacketReader.prototype.readMat3 = function(value) {
};


/**
* Reads a value from the buffer.
* The result of this function will be reset on the next read operation and must
* only be used to copy the value to some other structure.
* @return {!goog.vec.Vec3.Float32} Value pointer.
*/
gf.net.PacketReader.prototype.readMat3Temp = function() {
goog.asserts.assert(this.offset + 3 * 3 * 4 <= this.buffer.length);
for (var n = 0; n < 3 * 3 * 4; n++) {
this.float32byte_[n] = this.buffer[this.offset++];
}
return this.float32_;
};


/**
* Reads a value from the buffer.
* @param {!goog.vec.Mat4.Type} value Value to receive the contents.
Expand All @@ -331,6 +376,21 @@ gf.net.PacketReader.prototype.readMat4 = function(value) {
};


/**
* Reads a value from the buffer.
* The result of this function will be reset on the next read operation and must
* only be used to copy the value to some other structure.
* @return {!goog.vec.Vec3.Float32} Value pointer.
*/
gf.net.PacketReader.prototype.readMat4Temp = function() {
goog.asserts.assert(this.offset + 4 * 4 * 4 <= this.buffer.length);
for (var n = 0; n < 4 * 4 * 4; n++) {
this.float32byte_[n] = this.buffer[this.offset++];
}
return this.float32_;
};


/**
* Reads a value from the buffer.
* @param {Uint8Array=} opt_target Target value, used if the size matches.
Expand Down
124 changes: 65 additions & 59 deletions src/gf/sim/entitystate.js
Expand Up @@ -20,6 +20,7 @@

goog.provide('gf.sim.EntityState');

goog.require('gf.sim.Variable');
goog.require('gf.sim.VariableTable');
goog.require('goog.asserts');

Expand Down Expand Up @@ -92,11 +93,12 @@ gf.sim.EntityState = function(entity, variableTable) {
* Entity state variable declaration function.
* @return {!gf.sim.VariableTable} A shared variable table.
*/
gf.sim.EntityState.getVariableTable = function(declarationFunction) {
gf.sim.EntityState.getVariableTable = function(declarationFunction, obj) {
if (!declarationFunction.variableTable_) {
var variableList = [];
declarationFunction(variableList);
declarationFunction.variableTable_ = new gf.sim.VariableTable(variableList);
declarationFunction.variableTable_ = new gf.sim.VariableTable(
variableList, obj);
}
return declarationFunction.variableTable_;
};
Expand Down Expand Up @@ -155,37 +157,15 @@ gf.sim.EntityState.prototype.read = function(reader) {
*/
gf.sim.EntityState.prototype.readDelta = function(reader) {
// Read the first 32 variables
this.readDeltaVariables_(reader, 0);
var presentBits00_31 = reader.readVarUint();
this.variableTable_.readPresentVariables(
0, presentBits00_31, this, reader);

// Write the next 32, if present
if (this.variableTable_.getCount() > 31) {
this.readDeltaVariables_(reader, 32);
}
};


/**
* Reads a range of delta variables.
* This function is designed to be called on a subset of the variable range.
* For example, the first 32 variables, second 32, etc.
* @private
* @param {!gf.net.PacketReader} reader Packet reader.
* @param {number} startingOrdinal Ordinal this range starts at.
*/
gf.sim.EntityState.prototype.readDeltaVariables_ = function(
reader, startingOrdinal) {
// Read bits indicating which variables are present
var presentBits = reader.readVarUint();

// For each bit that is present, read the value
var ordinal = startingOrdinal;
while (presentBits) {
if (presentBits & 1) {
// Variable at <ordinal> is present and needs reading
this.variableTable_.readVariable(ordinal, this, reader);
}
presentBits >>= 1;
ordinal++;
var presentBits32_63 = reader.readVarUint();
this.variableTable_.readPresentVariables(
32, presentBits32_63, this, reader);
}
};

Expand Down Expand Up @@ -214,38 +194,15 @@ gf.sim.EntityState.prototype.writeDelta = function(writer) {
// delta

// Write the first 32 variables
this.writeDeltaVariables_(writer, this.dirtyBits00_31_, 0);
writer.writeVarUint(this.dirtyBits00_31_);
this.variableTable_.writePresentVariables(
0, this.dirtyBits00_31_, this, writer);

// Write the next 32, if present
if (this.dirtyBits32_63_ && this.variableTable_.getCount() > 31) {
this.writeDeltaVariables_(writer, this.dirtyBits32_63_, 32);
}
};


/**
* Writes a range of delta variables.
* This function is designed to be called on a subset of the variable range.
* For example, the first 32 variables, second 32, etc.
* @private
* @param {!gf.net.PacketWriter} writer Packet writer.
* @param {number} presentBits Bit field indicating which variables are present.
* @param {number} startingOrdinal Ordinal this range starts at.
*/
gf.sim.EntityState.prototype.writeDeltaVariables_ = function(
writer, presentBits, startingOrdinal) {
// Write dirty bits
writer.writeVarUint(presentBits);

// For each bit that is dirty, write the value
var ordinal = startingOrdinal;
while (presentBits) {
if (presentBits & 1) {
// Variable at <ordinal> is dirty and needs writing
this.variableTable_.writeVariable(ordinal, this, writer);
}
presentBits >>= 1;
ordinal++;
writer.writeVarUint(this.dirtyBits32_63_);
this.variableTable_.writePresentVariables(
32, this.dirtyBits32_63_, this, writer);
}
};

Expand Down Expand Up @@ -300,3 +257,52 @@ gf.sim.EntityState.prototype.interpolate = function(
vtable.interpolateVariables(sourceState, targetState, t, this);
}
};


// TODO(benvanik): find a way to remove these - point at an indirection table?
/**
* Scratch Vec3 for math.
* This is currently used by the variable table system.
* @protected
* @type {!goog.vec.Vec3.Float32}
*/
gf.sim.EntityState.prototype.tmpVec3 = gf.sim.Variable.tmpVec3;


/**
* Scratch Quaternion for math.
* This is currently used by the variable table system.
* @protected
* @type {!goog.vec.Quaternion.Float32}
*/
gf.sim.EntityState.prototype.tmpQuat = gf.sim.Variable.tmpQuat;


/**
* Quaternion slerp.
* This is currently used by the variable table system.
* @protected
* @type {!Function}
*/
gf.sim.EntityState.prototype.qslerp = goog.vec.Quaternion.slerp;


/**
* Color lerp.
* This is currently used by the variable table system.
* @protected
* @type {!Function}
*/
gf.sim.EntityState.prototype.colorLerp = gf.vec.Color.lerpUint32;

// HACK: ensure things are included
goog.scope(function() {
gf.sim.EntityState.prototype.tmpVec3[0] =
gf.sim.EntityState.prototype.tmpVec3[1];
gf.sim.EntityState.prototype.qslerp(
gf.sim.EntityState.prototype.tmpQuat,
gf.sim.EntityState.prototype.tmpQuat,
0,
gf.sim.EntityState.prototype.tmpQuat);
gf.sim.EntityState.prototype.colorLerp(0, 0, 0);
});

0 comments on commit a96d07f

Please sign in to comment.