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 {
var char : uint = variable.charCodeAt(0);
var char:uint = variable.charCodeAt(0);
if (char == "o".charCodeAt(0)) {
return variable + (programType == "f" ? "c" : "p");
} 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/
* */
package alternativa.engine3d.materials.compiler {
import flash.utils.Dictionary;
/**
* @private
@@ -101,7 +101,7 @@ package alternativa.engine3d.materials.compiler {
// set if not equal
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[ADD] = "add";
COMMAND_NAMES[SUB] = "sub";

View File

@@ -40,26 +40,26 @@ package alternativa.engine3d.materials.compiler {
switch(strType){
case "t":
lowerCode |= 0x2000000;
type = 2;
type = VariableType.TEMPORARY;
break;
case "o":
lowerCode |= 0x3000000;
type = 3;
type = VariableType.OUTPUT;
break;
case "v":
lowerCode |= 0x4000000;
type = 4;
type = VariableType.VARYING;
break;
case "d":
lowerCode |= 0x6000000;
type = 6;
type = VariableType.DEPTH;
break;
case "i":
lowerCode |= 0x7000000;
type = 7;
type = VariableType.INPUT;
break;
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;
}
}

View File

@@ -51,7 +51,7 @@ package alternativa.engine3d.materials.compiler {
alternativa3d var _linkedVariables:Object;
// 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.
private var _inputParams:Dictionary = new Dictionary();
@@ -61,9 +61,9 @@ package alternativa.engine3d.materials.compiler {
// Counters of variables by types
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.
@@ -81,8 +81,8 @@ package alternativa.engine3d.materials.compiler {
data = null;
_locals[0] = _locals[1] = _locals[2] = _locals[3] = _locals[4] = _locals[5] = _locals[6] = 0;
procedures.length = 0;
_varyings = new Object();
samplers = new Object();
_varyings = {};
samplers = {};
commandsCount = 0;
slotsCount = 0;
@@ -218,7 +218,7 @@ package alternativa.engine3d.materials.compiler {
if (data != null) return;
var v:Variable;
var variables:Object = _linkedVariables = new Object();
var variables:Object = _linkedVariables = {};
var p:Procedure;
var i:int, j:int;
var nv:Variable;
@@ -259,7 +259,8 @@ package alternativa.engine3d.materials.compiler {
for (j = 0; j < jLength; j++) {
v = vector[j];
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());
}
nv = variables[v.name] = new Variable();
@@ -291,7 +292,7 @@ package alternativa.engine3d.materials.compiler {
if (p.variablesUsages[7].length > j) {
var inParam:Variable = p.variablesUsages[7][j];
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) {
v.index = _locals[v.type];
@@ -302,7 +303,6 @@ package alternativa.engine3d.materials.compiler {
inParam = inParam.next;
}
}
}
}
if (output != null) {
@@ -316,7 +316,7 @@ package alternativa.engine3d.materials.compiler {
// Output variable
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.type != 2) {
@@ -340,7 +340,7 @@ package alternativa.engine3d.materials.compiler {
v = vars[j];
if (v == null) continue;
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;
}
}

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/
* */
package alternativa.engine3d.materials.compiler {
import alternativa.engine3d.alternativa3d;
import flash.display3D.Context3DProgramType;
@@ -19,20 +20,20 @@ package alternativa.engine3d.materials.compiler {
*/
public class Procedure {
// Name of procedure
public var name : String;
public var name:String;
/**
* @private
*/
alternativa3d static const crc32Table : Vector.<uint> = createCRC32Table();
alternativa3d static const crc32Table:Vector.<uint> = createCRC32Table();
private static function createCRC32Table() : Vector.<uint> {
var crc_table : Vector.<uint> = new Vector.<uint>(256);
var crc : uint, i : int, j : int;
private static function createCRC32Table():Vector.<uint> {
var crc_table:Vector.<uint> = new Vector.<uint>(256);
var crc:uint, i:int, j:int;
for (i = 0; i < 256; i++) {
crc = i;
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;
}
@@ -42,36 +43,36 @@ package alternativa.engine3d.materials.compiler {
/**
* @private
*/
alternativa3d var crc32 : uint = 0;
alternativa3d var crc32:uint = 0;
/**
* 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.
*/
public var slotsCount : int = 0;
public var slotsCount:int = 0;
/**
* Number of strings in a procedure.
*/
public var commandsCount : int = 0;
public var commandsCount:int = 0;
/**
* @private
*/
alternativa3d var reservedConstants : uint = 0;
alternativa3d var reservedConstants:uint = 0;
/**
* Creates a new Procedure instance.
*
* @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;
this.name = name;
if (array != null) {
@@ -79,23 +80,23 @@ package alternativa.engine3d.materials.compiler {
}
}
public function getByteCode(type : String, version:uint = 1) : ByteArray {
var result : ByteArray = new ByteArray();
public function getByteCode(type:String, version:uint = 1):ByteArray {
var result:ByteArray = new ByteArray();
result.endian = Endian.LITTLE_ENDIAN;
result.writeByte(0xa0);
result.writeUnsignedInt(version);
// AGAL version, big endian, bit pattern will be 0x01000000
result.writeByte(0xa1);
// tag program id
result.writeByte((type == Context3DProgramType.FRAGMENT) ? 1 : 0);
result.writeByte((type == Context3DProgramType.FRAGMENT) ? 1:0);
// vertex or fragment
result.writeBytes(byteCode);
return result;
}
private function addVariableUsage(v : Variable) : void {
var vars : Vector.<Variable> = variablesUsages[v.type];
var index : int = v.index;
private function addVariableUsage(v:Variable):void {
var vars:Vector.<Variable> = variablesUsages[v.type];
var index:int = v.index;
if (index >= vars.length) {
vars.length = index + 1;
} else {
@@ -114,8 +115,8 @@ package alternativa.engine3d.materials.compiler {
*
* @see VariableType
*/
public function assignVariableName(type : uint, index : uint, name : String, size : uint = 1) : void {
var v : Variable = variablesUsages[type][index];
public function assignVariableName(type:uint, index:uint, name:String, size:uint = 1):void {
var v:Variable = variablesUsages[type][index];
while (v != null) {
v.size = size;
v.name = name;
@@ -126,28 +127,28 @@ package alternativa.engine3d.materials.compiler {
/**
* Compiles shader from the string.
*/
public function compileFromString(source : String) : void {
var commands : Array = source.split("\n");
public function compileFromString(source:String):void {
var commands:Array = source.split("\n");
compileFromArray(commands);
}
/**
* Compiles shader from the array of strings.
*/
public function compileFromArray(source : Array) : void {
for (var i : int = 0; i < 8; i++) {
public function compileFromArray(source:Array):void {
for (var i:int = 0; i < 8; i++) {
variablesUsages[i] = new Vector.<Variable>();
}
byteCode.length = 0;
commandsCount = 0;
slotsCount = 0;
const decPattern : RegExp = /# *[acvs]\d{1,3} *= *[a-zA-Z0-9_]*/i;
var declarationStrings : Vector.<String> = new Vector.<String>();
var count : int = source.length;
const decPattern:RegExp = /# *[acvs]\d{1,3} *= *[a-zA-Z0-9_]*/i;
var declarationStrings:Vector.<String> = new Vector.<String>();
var count:int = source.length;
for (i = 0; i < count; i++) {
var cmd : String = source[i];
var declaration : Array = cmd.match(decPattern);
var cmd:String = source[i];
var declaration:Array = cmd.match(decPattern);
if (declaration != null && declaration.length > 0) {
declarationStrings.push(declaration[0]);
} else {
@@ -155,10 +156,10 @@ package alternativa.engine3d.materials.compiler {
}
}
for (i = 0,count = declarationStrings.length; i < count; i++) {
var decArray : Array = declarationStrings[i].split("=");
var regType : String = decArray[0].match(/[acvs]/i);
var varIndex : int = int(decArray[0].match(/\d{1,3}/i));
var varName : String = decArray[1].match(/[a-zA-Z0-9]*/i);
var decArray:Array = declarationStrings[i].split("=");
var regType:String = decArray[0].match(/[acvs]/i);
var varIndex:int = int(decArray[0].match(/\d{1,3}/i));
var varName:String = decArray[1].match(/[a-zA-Z0-9]*/i);
switch (regType.toLowerCase()) {
case "a":
assignVariableName(VariableType.ATTRIBUTE, varIndex, varName);
@@ -177,14 +178,14 @@ package alternativa.engine3d.materials.compiler {
crc32 = createCRC32(byteCode);
}
public function assignConstantsArray(registersCount : uint = 1) : void {
public function assignConstantsArray(registersCount:uint = 1):void {
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 {
var commentIndex : int = source.indexOf("//");
private function writeAGALExpression(source:String):void {
var commentIndex:int = source.indexOf("//");
if (commentIndex >= 0) {
source = source.substr(0, commentIndex);
}
@@ -219,15 +220,12 @@ package alternativa.engine3d.materials.compiler {
// -- too many interpolated values
// 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
// Determine the size of constant
var opCode : String = operands[0];
var destination : DestinationVariable;
var source1 : SourceVariable;
var source2 : Variable;
var opCode:String = operands[0];
var destination:DestinationVariable;
var source1:SourceVariable;
var source2:Variable;
if (opCode == "kil" || opCode == "ife" || opCode == "ine" || opCode == "ifg" || opCode == "ifl") {//no dist
source1 = new SourceVariable(operands[1]);
addVariableUsage(source1);
@@ -240,7 +238,7 @@ package alternativa.engine3d.materials.compiler {
source1 = new SourceVariable(operands[2]);
addVariableUsage(source1);
}
var type : uint;
var type:uint;
switch (opCode) {
case "mov":
type = CommandType.MOV;
@@ -461,7 +459,6 @@ package alternativa.engine3d.materials.compiler {
break;
}
// Fill of byteCode of command
byteCode.writeUnsignedInt(type);
if (destination != null) {
@@ -483,7 +480,7 @@ package alternativa.engine3d.materials.compiler {
}
if (source2 != null) {
source2.position = byteCode.position;
var s2v : SourceVariable = source2 as SourceVariable;
var s2v:SourceVariable = source2 as SourceVariable;
if (s2v != null && s2v.relative != null) {
addVariableUsage(s2v.relative);
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.
*/
public static function compileFromArray(source : Array, name : String = null) : Procedure {
var proc : Procedure = new Procedure(source, name);
public static function compileFromArray(source:Array, name:String = null):Procedure {
var proc:Procedure = new Procedure(source, name);
return proc;
}
/**
* Creates and returns an instance of procedure from string.
*/
public static function compileFromString(source : String, name : String = null) : Procedure {
var proc : Procedure = new Procedure(null, name);
public static function compileFromString(source:String, name:String = null):Procedure {
var proc:Procedure = new Procedure(null, name);
proc.compileFromString(source);
return proc;
}
@@ -516,8 +513,8 @@ package alternativa.engine3d.materials.compiler {
/**
* Create an instance of procedure.
*/
public function newInstance() : Procedure {
var res : Procedure = new Procedure();
public function newInstance():Procedure {
var res:Procedure = new Procedure();
res.byteCode = this.byteCode;
res.variablesUsages = this.variablesUsages;
res.slotsCount = this.slotsCount;
@@ -530,12 +527,12 @@ package alternativa.engine3d.materials.compiler {
/**
* @private
*/
alternativa3d static function createCRC32(byteCode : ByteArray) : uint {
alternativa3d static function createCRC32(byteCode:ByteArray):uint {
byteCode.position = 0;
var len : uint = byteCode.length;
var crc : uint = 0xFFFFFFFF;
var len:uint = byteCode.length;
var crc:uint = 0xFFFFFFFF;
while (len--) {
var byte : int = byteCode.readByte();
var byte:int = byteCode.readByte();
crc = crc32Table[(crc ^ byte) & 0xFF] ^ (crc >> 8);
}
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/
* */
package alternativa.engine3d.materials.compiler {
import flash.utils.ByteArray;
/**
* @private
*/
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) {
return 0;
}
return super.size;
}
public function SourceVariable(source : String) {
var strType : String = String(source.match(/[catsoivd]/g)[0]);
public function SourceVariable(source:String) {
var strType:String = String(source.match(/[catsoiv]/g)[0]);
var regmask : uint;
var regmask:uint;
var relreg : Array = source.match(/\[.*\]/g);
var isRel : Boolean = relreg.length > 0;
var relreg:Array = source.match(/\[.*\]/g);
var isRel:Boolean = relreg.length > 0;
if (isRel) {
source = source.replace(relreg[0], "0");
} else {
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) {
regmask = 0;
var cv : int;
var maskLength : uint = maskmatch.length;
for (var i : int = 1; i < maskLength; i++) {
var cv:int;
var maskLength:uint = maskmatch.length;
for (var i:int = 1; i < maskLength; i++) {
cv = maskmatch.charCodeAt(i) - X_CHAR_CODE;
if (cv == -1) cv = 3;
regmask |= cv << ( ( i - 1 ) << 1 );
@@ -74,11 +75,8 @@ package alternativa.engine3d.materials.compiler {
case "i":
type = VariableType.INPUT;
break;
case "d":
type = VariableType.DEPTH;
break;
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;
}
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) {
super.writeToByteArray(byteCode, newIndex, newType, offset);
} else {
@@ -99,5 +97,6 @@ package alternativa.engine3d.materials.compiler {
byteCode.position = position + offset + 4;
byteCode.writeByte(newType);
}
}
}