Fix coding style

This commit is contained in:
Yaski
2013-01-17 22:45:43 +06:00
parent 8e7d834a22
commit 64e7463d00
6 changed files with 1640 additions and 1644 deletions

View File

@@ -283,7 +283,7 @@ package alternativa.engine3d.materials {
} }
private static function attachProgramPrefix(variable:String, programType:String):String { private static function attachProgramPrefix(variable:String, programType:String):String {
var char : uint = variable.charCodeAt(0); var char:uint = variable.charCodeAt(0);
if (char == "o".charCodeAt(0)) { if (char == "o".charCodeAt(0)) {
return variable + (programType == "f" ? "c" : "p"); return variable + (programType == "f" ? "c" : "p");
} else if (char == "d".charCodeAt(0)) { } else if (char == "d".charCodeAt(0)) {

View File

@@ -6,7 +6,7 @@
* It is desirable to notify that Covered Software was "Powered by AlternativaPlatform" with link to http://www.alternativaplatform.com/ * It is desirable to notify that Covered Software was "Powered by AlternativaPlatform" with link to http://www.alternativaplatform.com/
* */ * */
package alternativa.engine3d.materials.compiler { package alternativa.engine3d.materials.compiler {
import flash.utils.Dictionary;
/** /**
* @private * @private
@@ -101,7 +101,7 @@ package alternativa.engine3d.materials.compiler {
// set if not equal // set if not equal
public static const SNE : uint = 0x2d; public static const SNE : uint = 0x2d;
public static const COMMAND_NAMES : Dictionary = new Dictionary(); public static const COMMAND_NAMES : Array = [];
COMMAND_NAMES[MOV] = "mov"; COMMAND_NAMES[MOV] = "mov";
COMMAND_NAMES[ADD] = "add"; COMMAND_NAMES[ADD] = "add";
COMMAND_NAMES[SUB] = "sub"; COMMAND_NAMES[SUB] = "sub";

View File

@@ -40,26 +40,26 @@ package alternativa.engine3d.materials.compiler {
switch(strType){ switch(strType){
case "t": case "t":
lowerCode |= 0x2000000; lowerCode |= 0x2000000;
type = 2; type = VariableType.TEMPORARY;
break; break;
case "o": case "o":
lowerCode |= 0x3000000; lowerCode |= 0x3000000;
type = 3; type = VariableType.OUTPUT;
break; break;
case "v": case "v":
lowerCode |= 0x4000000; lowerCode |= 0x4000000;
type = 4; type = VariableType.VARYING;
break; break;
case "d": case "d":
lowerCode |= 0x6000000; lowerCode |= 0x6000000;
type = 6; type = VariableType.DEPTH;
break; break;
case "i": case "i":
lowerCode |= 0x7000000; lowerCode |= 0x7000000;
type = 7; type = VariableType.INPUT;
break; break;
default : default :
throw new ArgumentError("Wrong destination register type, must be \"t\" or \"o\" or \"v\", var = " + source); throw new ArgumentError('Wrong destination register type, must be "t" or "o" or "v" or "d", var = ' + source);
break; break;
} }
} }

View File

@@ -51,7 +51,7 @@ package alternativa.engine3d.materials.compiler {
alternativa3d var _linkedVariables:Object; alternativa3d var _linkedVariables:Object;
// Dictionary of temporary variables at this linker. Key is a name of variable, value is a variable. // Dictionary of temporary variables at this linker. Key is a name of variable, value is a variable.
private var _localVariables:Object = new Object(); private var _localVariables:Object = {};
// Key - procedure, value - array of strings. // Key - procedure, value - array of strings.
private var _inputParams:Dictionary = new Dictionary(); private var _inputParams:Dictionary = new Dictionary();
@@ -61,9 +61,9 @@ package alternativa.engine3d.materials.compiler {
// Counters of variables by types // Counters of variables by types
private var _locals:Vector.<uint> = new Vector.<uint>(7, true); private var _locals:Vector.<uint> = new Vector.<uint>(7, true);
private var samplers:Object = new Object(); private var samplers:Object = {};
private var _varyings:Object = new Object(); private var _varyings:Object = {};
/** /**
* Creates a new Linker instance. * Creates a new Linker instance.
@@ -81,8 +81,8 @@ package alternativa.engine3d.materials.compiler {
data = null; data = null;
_locals[0] = _locals[1] = _locals[2] = _locals[3] = _locals[4] = _locals[5] = _locals[6] = 0; _locals[0] = _locals[1] = _locals[2] = _locals[3] = _locals[4] = _locals[5] = _locals[6] = 0;
procedures.length = 0; procedures.length = 0;
_varyings = new Object(); _varyings = {};
samplers = new Object(); samplers = {};
commandsCount = 0; commandsCount = 0;
slotsCount = 0; slotsCount = 0;
@@ -218,7 +218,7 @@ package alternativa.engine3d.materials.compiler {
if (data != null) return; if (data != null) return;
var v:Variable; var v:Variable;
var variables:Object = _linkedVariables = new Object(); var variables:Object = _linkedVariables = {};
var p:Procedure; var p:Procedure;
var i:int, j:int; var i:int, j:int;
var nv:Variable; var nv:Variable;
@@ -259,7 +259,8 @@ package alternativa.engine3d.materials.compiler {
for (j = 0; j < jLength; j++) { for (j = 0; j < jLength; j++) {
v = vector[j]; v = vector[j];
if (v == null || v.name == null) continue; if (v == null || v.name == null) continue;
if (v.name == null && i != 2 && i != 6 && i != 3 && i != 7) { if (v.name == null && i != 2 && i != 3 && i != 6 && i != 7) {
// TODO: Never happens
throw new Error("Linkage error: Noname variable. Procedure = " + p.name + ", type = " + i.toString() + ", index = " + j.toString()); throw new Error("Linkage error: Noname variable. Procedure = " + p.name + ", type = " + i.toString() + ", index = " + j.toString());
} }
nv = variables[v.name] = new Variable(); nv = variables[v.name] = new Variable();
@@ -291,7 +292,7 @@ package alternativa.engine3d.materials.compiler {
if (p.variablesUsages[7].length > j) { if (p.variablesUsages[7].length > j) {
var inParam:Variable = p.variablesUsages[7][j]; var inParam:Variable = p.variablesUsages[7][j];
if (inParam == null) { if (inParam == null) {
throw new Error("Input parameter set, but not exist in code. paramName = " + param + ", register = i" + j.toString()); throw new Error("Input parameter set, but not used in code. paramName = " + param + ", register = i" + j.toString());
} }
if (v.index < 0) { if (v.index < 0) {
v.index = _locals[v.type]; v.index = _locals[v.type];
@@ -302,7 +303,6 @@ package alternativa.engine3d.materials.compiler {
inParam = inParam.next; inParam = inParam.next;
} }
} }
} }
} }
if (output != null) { if (output != null) {
@@ -316,7 +316,7 @@ package alternativa.engine3d.materials.compiler {
// Output variable // Output variable
continue; continue;
} }
throw new Error("Output parameter have not declared. paramName = " + param); throw new Error("Output parameter not declared. paramName = " + param);
} }
if (v.index < 0) { if (v.index < 0) {
if (v.type != 2) { if (v.type != 2) {
@@ -340,7 +340,7 @@ package alternativa.engine3d.materials.compiler {
v = vars[j]; v = vars[j];
if (v == null) continue; if (v == null) continue;
while (v != null) { while (v != null) {
v.writeToByteArray(data, v.index + _locals[2], VariableType.TEMPORARY, offset); v.writeToByteArray(data, _locals[2] + v.index, VariableType.TEMPORARY, offset);
v = v.next; v = v.next;
} }
} }

View File

@@ -6,6 +6,7 @@
* It is desirable to notify that Covered Software was "Powered by AlternativaPlatform" with link to http://www.alternativaplatform.com/ * It is desirable to notify that Covered Software was "Powered by AlternativaPlatform" with link to http://www.alternativaplatform.com/
* */ * */
package alternativa.engine3d.materials.compiler { package alternativa.engine3d.materials.compiler {
import alternativa.engine3d.alternativa3d; import alternativa.engine3d.alternativa3d;
import flash.display3D.Context3DProgramType; import flash.display3D.Context3DProgramType;
@@ -19,20 +20,20 @@ package alternativa.engine3d.materials.compiler {
*/ */
public class Procedure { public class Procedure {
// Name of procedure // Name of procedure
public var name : String; public var name:String;
/** /**
* @private * @private
*/ */
alternativa3d static const crc32Table : Vector.<uint> = createCRC32Table(); alternativa3d static const crc32Table:Vector.<uint> = createCRC32Table();
private static function createCRC32Table() : Vector.<uint> { private static function createCRC32Table():Vector.<uint> {
var crc_table : Vector.<uint> = new Vector.<uint>(256); var crc_table:Vector.<uint> = new Vector.<uint>(256);
var crc : uint, i : int, j : int; var crc:uint, i:int, j:int;
for (i = 0; i < 256; i++) { for (i = 0; i < 256; i++) {
crc = i; crc = i;
for (j = 0; j < 8; j++) for (j = 0; j < 8; j++)
crc = crc & 1 ? (crc >> 1) ^ 0xEDB88320 : crc >> 1; crc = crc & 1 ? (crc >> 1) ^ 0xEDB88320:crc >> 1;
crc_table[i] = crc; crc_table[i] = crc;
} }
@@ -42,36 +43,36 @@ package alternativa.engine3d.materials.compiler {
/** /**
* @private * @private
*/ */
alternativa3d var crc32 : uint = 0; alternativa3d var crc32:uint = 0;
/** /**
* Code of procedure. * Code of procedure.
*/ */
public var byteCode : ByteArray = new ByteArray(); public var byteCode:ByteArray = new ByteArray();
public var variablesUsages : Vector.<Vector.<Variable>> = new Vector.<Vector.<Variable>>(); public var variablesUsages:Vector.<Vector.<Variable>> = new Vector.<Vector.<Variable>>();
/** /**
* Number of instruction slots in a procedure. * Number of instruction slots in a procedure.
*/ */
public var slotsCount : int = 0; public var slotsCount:int = 0;
/** /**
* Number of strings in a procedure. * Number of strings in a procedure.
*/ */
public var commandsCount : int = 0; public var commandsCount:int = 0;
/** /**
* @private * @private
*/ */
alternativa3d var reservedConstants : uint = 0; alternativa3d var reservedConstants:uint = 0;
/** /**
* Creates a new Procedure instance. * Creates a new Procedure instance.
* *
* @param array Array of AGAL strings * @param array Array of AGAL strings
*/ */
public function Procedure(array : Array = null, name : String = null) { public function Procedure(array:Array = null, name:String = null) {
byteCode.endian = Endian.LITTLE_ENDIAN; byteCode.endian = Endian.LITTLE_ENDIAN;
this.name = name; this.name = name;
if (array != null) { if (array != null) {
@@ -79,23 +80,23 @@ package alternativa.engine3d.materials.compiler {
} }
} }
public function getByteCode(type : String, version:uint = 1) : ByteArray { public function getByteCode(type:String, version:uint = 1):ByteArray {
var result : ByteArray = new ByteArray(); var result:ByteArray = new ByteArray();
result.endian = Endian.LITTLE_ENDIAN; result.endian = Endian.LITTLE_ENDIAN;
result.writeByte(0xa0); result.writeByte(0xa0);
result.writeUnsignedInt(version); result.writeUnsignedInt(version);
// AGAL version, big endian, bit pattern will be 0x01000000 // AGAL version, big endian, bit pattern will be 0x01000000
result.writeByte(0xa1); result.writeByte(0xa1);
// tag program id // tag program id
result.writeByte((type == Context3DProgramType.FRAGMENT) ? 1 : 0); result.writeByte((type == Context3DProgramType.FRAGMENT) ? 1:0);
// vertex or fragment // vertex or fragment
result.writeBytes(byteCode); result.writeBytes(byteCode);
return result; return result;
} }
private function addVariableUsage(v : Variable) : void { private function addVariableUsage(v:Variable):void {
var vars : Vector.<Variable> = variablesUsages[v.type]; var vars:Vector.<Variable> = variablesUsages[v.type];
var index : int = v.index; var index:int = v.index;
if (index >= vars.length) { if (index >= vars.length) {
vars.length = index + 1; vars.length = index + 1;
} else { } else {
@@ -114,8 +115,8 @@ package alternativa.engine3d.materials.compiler {
* *
* @see VariableType * @see VariableType
*/ */
public function assignVariableName(type : uint, index : uint, name : String, size : uint = 1) : void { public function assignVariableName(type:uint, index:uint, name:String, size:uint = 1):void {
var v : Variable = variablesUsages[type][index]; var v:Variable = variablesUsages[type][index];
while (v != null) { while (v != null) {
v.size = size; v.size = size;
v.name = name; v.name = name;
@@ -126,28 +127,28 @@ package alternativa.engine3d.materials.compiler {
/** /**
* Compiles shader from the string. * Compiles shader from the string.
*/ */
public function compileFromString(source : String) : void { public function compileFromString(source:String):void {
var commands : Array = source.split("\n"); var commands:Array = source.split("\n");
compileFromArray(commands); compileFromArray(commands);
} }
/** /**
* Compiles shader from the array of strings. * Compiles shader from the array of strings.
*/ */
public function compileFromArray(source : Array) : void { public function compileFromArray(source:Array):void {
for (var i : int = 0; i < 8; i++) { for (var i:int = 0; i < 8; i++) {
variablesUsages[i] = new Vector.<Variable>(); variablesUsages[i] = new Vector.<Variable>();
} }
byteCode.length = 0; byteCode.length = 0;
commandsCount = 0; commandsCount = 0;
slotsCount = 0; slotsCount = 0;
const decPattern : RegExp = /# *[acvs]\d{1,3} *= *[a-zA-Z0-9_]*/i; const decPattern:RegExp = /# *[acvs]\d{1,3} *= *[a-zA-Z0-9_]*/i;
var declarationStrings : Vector.<String> = new Vector.<String>(); var declarationStrings:Vector.<String> = new Vector.<String>();
var count : int = source.length; var count:int = source.length;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
var cmd : String = source[i]; var cmd:String = source[i];
var declaration : Array = cmd.match(decPattern); var declaration:Array = cmd.match(decPattern);
if (declaration != null && declaration.length > 0) { if (declaration != null && declaration.length > 0) {
declarationStrings.push(declaration[0]); declarationStrings.push(declaration[0]);
} else { } else {
@@ -155,10 +156,10 @@ package alternativa.engine3d.materials.compiler {
} }
} }
for (i = 0,count = declarationStrings.length; i < count; i++) { for (i = 0,count = declarationStrings.length; i < count; i++) {
var decArray : Array = declarationStrings[i].split("="); var decArray:Array = declarationStrings[i].split("=");
var regType : String = decArray[0].match(/[acvs]/i); var regType:String = decArray[0].match(/[acvs]/i);
var varIndex : int = int(decArray[0].match(/\d{1,3}/i)); var varIndex:int = int(decArray[0].match(/\d{1,3}/i));
var varName : String = decArray[1].match(/[a-zA-Z0-9]*/i); var varName:String = decArray[1].match(/[a-zA-Z0-9]*/i);
switch (regType.toLowerCase()) { switch (regType.toLowerCase()) {
case "a": case "a":
assignVariableName(VariableType.ATTRIBUTE, varIndex, varName); assignVariableName(VariableType.ATTRIBUTE, varIndex, varName);
@@ -177,14 +178,14 @@ package alternativa.engine3d.materials.compiler {
crc32 = createCRC32(byteCode); crc32 = createCRC32(byteCode);
} }
public function assignConstantsArray(registersCount : uint = 1) : void { public function assignConstantsArray(registersCount:uint = 1):void {
reservedConstants = registersCount; reservedConstants = registersCount;
} }
private const agalParser : RegExp = /[A-Za-z]+(((\[.+\])|(\d+))(\.[xyzw]{1,4})?(\ *\<.*>)?)?/g; private const agalParser:RegExp = /[A-Za-z]+(((\[.+\])|(\d+))(\.[xyzw]{1,4})?(\ *\<.*>)?)?/g;
private function writeAGALExpression(source : String) : void { private function writeAGALExpression(source:String):void {
var commentIndex : int = source.indexOf("//"); var commentIndex:int = source.indexOf("//");
if (commentIndex >= 0) { if (commentIndex >= 0) {
source = source.substr(0, commentIndex); source = source.substr(0, commentIndex);
} }
@@ -219,15 +220,12 @@ package alternativa.engine3d.materials.compiler {
// -- too many interpolated values // -- too many interpolated values
// You can not use kil in fragment shader // You can not use kil in fragment shader
var operands : Array = source.match(agalParser); var operands:Array = source.match(agalParser);
// It is possible not use the input parameter. It is optimization of the linker var opCode:String = operands[0];
// Determine the size of constant var destination:DestinationVariable;
var source1:SourceVariable;
var opCode : String = operands[0]; var source2:Variable;
var destination : DestinationVariable;
var source1 : SourceVariable;
var source2 : Variable;
if (opCode == "kil" || opCode == "ife" || opCode == "ine" || opCode == "ifg" || opCode == "ifl") {//no dist if (opCode == "kil" || opCode == "ife" || opCode == "ine" || opCode == "ifg" || opCode == "ifl") {//no dist
source1 = new SourceVariable(operands[1]); source1 = new SourceVariable(operands[1]);
addVariableUsage(source1); addVariableUsage(source1);
@@ -240,7 +238,7 @@ package alternativa.engine3d.materials.compiler {
source1 = new SourceVariable(operands[2]); source1 = new SourceVariable(operands[2]);
addVariableUsage(source1); addVariableUsage(source1);
} }
var type : uint; var type:uint;
switch (opCode) { switch (opCode) {
case "mov": case "mov":
type = CommandType.MOV; type = CommandType.MOV;
@@ -461,7 +459,6 @@ package alternativa.engine3d.materials.compiler {
break; break;
} }
// Fill of byteCode of command // Fill of byteCode of command
byteCode.writeUnsignedInt(type); byteCode.writeUnsignedInt(type);
if (destination != null) { if (destination != null) {
@@ -483,7 +480,7 @@ package alternativa.engine3d.materials.compiler {
} }
if (source2 != null) { if (source2 != null) {
source2.position = byteCode.position; source2.position = byteCode.position;
var s2v : SourceVariable = source2 as SourceVariable; var s2v:SourceVariable = source2 as SourceVariable;
if (s2v != null && s2v.relative != null) { if (s2v != null && s2v.relative != null) {
addVariableUsage(s2v.relative); addVariableUsage(s2v.relative);
s2v.relative.position = s2v.position; s2v.relative.position = s2v.position;
@@ -499,16 +496,16 @@ package alternativa.engine3d.materials.compiler {
/** /**
* Creates and returns an instance of procedure from array of strings. * Creates and returns an instance of procedure from array of strings.
*/ */
public static function compileFromArray(source : Array, name : String = null) : Procedure { public static function compileFromArray(source:Array, name:String = null):Procedure {
var proc : Procedure = new Procedure(source, name); var proc:Procedure = new Procedure(source, name);
return proc; return proc;
} }
/** /**
* Creates and returns an instance of procedure from string. * Creates and returns an instance of procedure from string.
*/ */
public static function compileFromString(source : String, name : String = null) : Procedure { public static function compileFromString(source:String, name:String = null):Procedure {
var proc : Procedure = new Procedure(null, name); var proc:Procedure = new Procedure(null, name);
proc.compileFromString(source); proc.compileFromString(source);
return proc; return proc;
} }
@@ -516,8 +513,8 @@ package alternativa.engine3d.materials.compiler {
/** /**
* Create an instance of procedure. * Create an instance of procedure.
*/ */
public function newInstance() : Procedure { public function newInstance():Procedure {
var res : Procedure = new Procedure(); var res:Procedure = new Procedure();
res.byteCode = this.byteCode; res.byteCode = this.byteCode;
res.variablesUsages = this.variablesUsages; res.variablesUsages = this.variablesUsages;
res.slotsCount = this.slotsCount; res.slotsCount = this.slotsCount;
@@ -530,12 +527,12 @@ package alternativa.engine3d.materials.compiler {
/** /**
* @private * @private
*/ */
alternativa3d static function createCRC32(byteCode : ByteArray) : uint { alternativa3d static function createCRC32(byteCode:ByteArray):uint {
byteCode.position = 0; byteCode.position = 0;
var len : uint = byteCode.length; var len:uint = byteCode.length;
var crc : uint = 0xFFFFFFFF; var crc:uint = 0xFFFFFFFF;
while (len--) { while (len--) {
var byte : int = byteCode.readByte(); var byte:int = byteCode.readByte();
crc = crc32Table[(crc ^ byte) & 0xFF] ^ (crc >> 8); crc = crc32Table[(crc ^ byte) & 0xFF] ^ (crc >> 8);
} }
return crc ^ 0xFFFFFFFF; return crc ^ 0xFFFFFFFF;

View File

@@ -6,42 +6,43 @@
* It is desirable to notify that Covered Software was "Powered by AlternativaPlatform" with link to http://www.alternativaplatform.com/ * It is desirable to notify that Covered Software was "Powered by AlternativaPlatform" with link to http://www.alternativaplatform.com/
* */ * */
package alternativa.engine3d.materials.compiler { package alternativa.engine3d.materials.compiler {
import flash.utils.ByteArray; import flash.utils.ByteArray;
/** /**
* @private * @private
*/ */
public class SourceVariable extends Variable { public class SourceVariable extends Variable {
public var relative : RelativeVariable; public var relative:RelativeVariable;
override public function get size() : uint { override public function get size():uint {
if (relative) { if (relative) {
return 0; return 0;
} }
return super.size; return super.size;
} }
public function SourceVariable(source : String) { public function SourceVariable(source:String) {
var strType : String = String(source.match(/[catsoivd]/g)[0]); var strType:String = String(source.match(/[catsoiv]/g)[0]);
var regmask : uint; var regmask:uint;
var relreg : Array = source.match(/\[.*\]/g); var relreg:Array = source.match(/\[.*\]/g);
var isRel : Boolean = relreg.length > 0; var isRel:Boolean = relreg.length > 0;
if (isRel) { if (isRel) {
source = source.replace(relreg[0], "0"); source = source.replace(relreg[0], "0");
} else { } else {
index = parseInt(source.match(/\d+/g)[0], 10); index = parseInt(source.match(/\d+/g)[0], 10);
} }
var swizzle : Array = source.match(/\.[xyzw]{1,4}/); var swizzle:Array = source.match(/\.[xyzw]{1,4}/);
var maskmatch : String = swizzle ? swizzle[0] : null; var maskmatch:String = swizzle ? swizzle[0]:null;
if (maskmatch) { if (maskmatch) {
regmask = 0; regmask = 0;
var cv : int; var cv:int;
var maskLength : uint = maskmatch.length; var maskLength:uint = maskmatch.length;
for (var i : int = 1; i < maskLength; i++) { for (var i:int = 1; i < maskLength; i++) {
cv = maskmatch.charCodeAt(i) - X_CHAR_CODE; cv = maskmatch.charCodeAt(i) - X_CHAR_CODE;
if (cv == -1) cv = 3; if (cv == -1) cv = 3;
regmask |= cv << ( ( i - 1 ) << 1 ); regmask |= cv << ( ( i - 1 ) << 1 );
@@ -74,11 +75,8 @@ package alternativa.engine3d.materials.compiler {
case "i": case "i":
type = VariableType.INPUT; type = VariableType.INPUT;
break; break;
case "d":
type = VariableType.DEPTH;
break;
default : default :
throw new ArgumentError('Wrong source register type, must be "a" or "c" or "t" or "o" or "v" or "i" or "d", var = ' + source); throw new ArgumentError('Wrong source register type, must be "a" or "c" or "t" or "o" or "v" or "i", var = ' + source);
break; break;
} }
upperCode = type; upperCode = type;
@@ -90,7 +88,7 @@ package alternativa.engine3d.materials.compiler {
} }
} }
override public function writeToByteArray(byteCode : ByteArray, newIndex : int, newType : int, offset : int = 0) : void { override public function writeToByteArray(byteCode:ByteArray, newIndex:int, newType:int, offset:int = 0):void {
if (relative == null) { if (relative == null) {
super.writeToByteArray(byteCode, newIndex, newType, offset); super.writeToByteArray(byteCode, newIndex, newType, offset);
} else { } else {
@@ -99,5 +97,6 @@ package alternativa.engine3d.materials.compiler {
byteCode.position = position + offset + 4; byteCode.position = position + offset + 4;
byteCode.writeByte(newType); byteCode.writeByte(newType);
} }
} }
} }