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

@@ -1,349 +1,349 @@
/** /**
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
* If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. * If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice.
* You may add additional accurate notices of copyright ownership. * You may add additional accurate notices of copyright ownership.
* *
* 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 { package alternativa.engine3d.materials {
import alternativa.engine3d.alternativa3d; import alternativa.engine3d.alternativa3d;
import alternativa.engine3d.materials.compiler.CommandType; import alternativa.engine3d.materials.compiler.CommandType;
import alternativa.engine3d.materials.compiler.VariableType; import alternativa.engine3d.materials.compiler.VariableType;
import avmplus.getQualifiedSuperclassName; import avmplus.getQualifiedSuperclassName;
import flash.display3D.Context3D; import flash.display3D.Context3D;
import flash.display3D.Context3DTextureFormat; import flash.display3D.Context3DTextureFormat;
import flash.display3D.IndexBuffer3D; import flash.display3D.IndexBuffer3D;
import flash.display3D.VertexBuffer3D; import flash.display3D.VertexBuffer3D;
import flash.display3D.textures.Texture; import flash.display3D.textures.Texture;
import flash.geom.Point; import flash.geom.Point;
import flash.utils.ByteArray; import flash.utils.ByteArray;
import flash.utils.Dictionary; import flash.utils.Dictionary;
import flash.utils.Endian; import flash.utils.Endian;
import flash.utils.getDefinitionByName; import flash.utils.getDefinitionByName;
/** /**
* @private * @private
*/ */
public class A3DUtils { public class A3DUtils {
public static const NONE:int = 0; public static const NONE:int = 0;
public static const DXT1:int = 1; public static const DXT1:int = 1;
public static const ETC1:int = 2; public static const ETC1:int = 2;
public static const PVRTC:int = 3; public static const PVRTC:int = 3;
private static const DXT1Data:ByteArray = getDXT1(); private static const DXT1Data:ByteArray = getDXT1();
private static const PVRTCData:ByteArray = getPVRTC(); private static const PVRTCData:ByteArray = getPVRTC();
private static const ETC1Data:ByteArray = getETC1(); private static const ETC1Data:ByteArray = getETC1();
private static function getDXT1():ByteArray { private static function getDXT1():ByteArray {
var DXT1Data:Vector.<int> = Vector.<int>([65,84,70,0,2,71,2,2,2,3,0,0,12,0,0,0,16,0,0,85,105,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,10,87,77,80,72,79,84,79,0,25,0,192,122,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,224,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,16,0,0,85,105,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,10,87,77,80,72,79,84,79,0,25,0,192,122,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,224,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,16,0,0,85,105,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,10,87,77,80,72,79,84,79,0,25,0,192,122,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,224,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,7,143,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]); var DXT1Data:Vector.<int> = Vector.<int>([65,84,70,0,2,71,2,2,2,3,0,0,12,0,0,0,16,0,0,85,105,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,10,87,77,80,72,79,84,79,0,25,0,192,122,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,224,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,16,0,0,85,105,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,10,87,77,80,72,79,84,79,0,25,0,192,122,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,224,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,16,0,0,85,105,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,10,87,77,80,72,79,84,79,0,25,0,192,122,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,224,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,7,143,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]);
return getData(DXT1Data); return getData(DXT1Data);
} }
private static function getETC1():ByteArray { private static function getETC1():ByteArray {
var ETC1Data:Vector.<int> = Vector.<int>([65,84,70,0,2,104,2,2,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,16,0,0,0,255,252,0,0,0,0,12,0,0,0,16,0,0,127,233,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,208,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,7,143,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,16,0,0,0,255,252,0,0,0,0,12,0,0,0,16,0,0,127,233,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,208,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,16,0,0,0,255,252,0,0,0,0,12,0,0,0,16,0,0,127,233,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,208,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,4,0]); var ETC1Data:Vector.<int> = Vector.<int>([65,84,70,0,2,104,2,2,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,16,0,0,0,255,252,0,0,0,0,12,0,0,0,16,0,0,127,233,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,208,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,7,143,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,16,0,0,0,255,252,0,0,0,0,12,0,0,0,16,0,0,127,233,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,208,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,0,0,0,16,0,0,0,255,252,0,0,0,0,12,0,0,0,16,0,0,127,233,56,0,0,0,0,0,157,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,1,0,0,0,129,188,4,0,1,0,0,0,2,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,66,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,0,0,1,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,208,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,114,0,4,0]);
return getData(ETC1Data); return getData(ETC1Data);
} }
private static function getPVRTC():ByteArray { private static function getPVRTC():ByteArray {
var PVRTCData:Vector.<int> = Vector.<int>([65,84,70,0,2,173,2,2,2,3,0,0,0,0,0,0,0,0,13,0,0,0,16,0,0,0,104,190,153,255,0,0,0,0,15,91,0,0,16,0,0,102,12,228,2,255,225,0,0,0,0,0,223,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,2,0,0,0,129,188,4,0,1,0,0,0,4,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,132,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,1,0,3,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,165,192,0,7,227,99,186,53,197,40,185,134,182,32,130,98,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,143,192,120,64,6,16,34,52,192,196,65,132,90,98,68,16,17,68,60,91,8,48,76,35,192,97,132,71,76,33,164,97,1,2,194,12,19,8,240,29,132,24,38,17,224,48,194,35,166,16,210,48,128,128,24,68,121,132,52,204,32,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,16,0,0,0,233,56,90,0,0,0,0,12,0,0,0,16,0,0,127,237,210,0,0,0,0,0,155,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,2,0,0,0,129,188,4,0,1,0,0,0,4,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,64,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,1,0,3,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,188,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,200,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,16,0,0,0,233,56,90,0,0,0,0,12,0,0,0,16,0,0,127,237,210,0,0,0,0,0,155,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,2,0,0,0,129,188,4,0,1,0,0,0,4,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,64,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,1,0,3,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,188,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,200,0,0,0,0,0,0,0,0,0,0]); var PVRTCData:Vector.<int> = Vector.<int>([65,84,70,0,2,173,2,2,2,3,0,0,0,0,0,0,0,0,13,0,0,0,16,0,0,0,104,190,153,255,0,0,0,0,15,91,0,0,16,0,0,102,12,228,2,255,225,0,0,0,0,0,223,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,2,0,0,0,129,188,4,0,1,0,0,0,4,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,132,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,1,0,3,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,165,192,0,7,227,99,186,53,197,40,185,134,182,32,130,98,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,143,192,120,64,6,16,34,52,192,196,65,132,90,98,68,16,17,68,60,91,8,48,76,35,192,97,132,71,76,33,164,97,1,2,194,12,19,8,240,29,132,24,38,17,224,48,194,35,166,16,210,48,128,128,24,68,121,132,52,204,32,32,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,16,0,0,0,233,56,90,0,0,0,0,12,0,0,0,16,0,0,127,237,210,0,0,0,0,0,155,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,2,0,0,0,129,188,4,0,1,0,0,0,4,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,64,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,1,0,3,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,188,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,200,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,16,0,0,0,233,56,90,0,0,0,0,12,0,0,0,16,0,0,127,237,210,0,0,0,0,0,155,73,73,188,1,8,0,0,0,5,0,1,188,1,0,16,0,0,0,74,0,0,0,128,188,4,0,1,0,0,0,2,0,0,0,129,188,4,0,1,0,0,0,4,0,0,0,192,188,4,0,1,0,0,0,90,0,0,0,193,188,4,0,1,0,0,0,64,0,0,0,0,0,0,0,36,195,221,111,3,78,254,75,177,133,61,119,118,141,201,9,87,77,80,72,79,84,79,0,25,0,192,120,0,1,0,3,96,0,160,0,10,0,0,160,0,0,0,4,111,255,0,1,0,0,1,0,188,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,17,200,0,0,0,0,0,0,0,0,0,0]);
return getData(PVRTCData); return getData(PVRTCData);
} }
private static function getData(source:Vector.<int>):ByteArray { private static function getData(source:Vector.<int>):ByteArray {
var result:ByteArray = new ByteArray(); var result:ByteArray = new ByteArray();
for (var i:int = 0, length:int = source.length; i < length; i++) { for (var i:int = 0, length:int = source.length; i < length; i++) {
result.writeByte(source[i]); result.writeByte(source[i]);
} }
return result; return result;
} }
public static function getSizeFromATF(byteArray:ByteArray, size:Point):void { public static function getSizeFromATF(byteArray:ByteArray, size:Point):void {
byteArray.position = 7; byteArray.position = 7;
var w:int = byteArray.readByte(); var w:int = byteArray.readByte();
var h:int = byteArray.readByte(); var h:int = byteArray.readByte();
size.x = 1 << w; size.x = 1 << w;
size.y = 1 << h; size.y = 1 << h;
byteArray.position = 0; byteArray.position = 0;
} }
public static function getSupportedTextureFormat(context3D:Context3D):int { public static function getSupportedTextureFormat(context3D:Context3D):int {
var testTexture:Texture = context3D.createTexture(4, 4, Context3DTextureFormat.COMPRESSED, false); var testTexture:Texture = context3D.createTexture(4, 4, Context3DTextureFormat.COMPRESSED, false);
var result:int = NONE; var result:int = NONE;
try { try {
testTexture.uploadCompressedTextureFromByteArray(DXT1Data, 0); testTexture.uploadCompressedTextureFromByteArray(DXT1Data, 0);
result = DXT1; result = DXT1;
} catch(e:Error) { } catch(e:Error) {
result = NONE; result = NONE;
} }
if (result == NONE) { if (result == NONE) {
try { try {
testTexture.uploadCompressedTextureFromByteArray(PVRTCData, 0); testTexture.uploadCompressedTextureFromByteArray(PVRTCData, 0);
result = PVRTC; result = PVRTC;
} catch(e:Error) { } catch(e:Error) {
result = NONE; result = NONE;
} }
} }
if (result == NONE) { if (result == NONE) {
try { try {
testTexture.uploadCompressedTextureFromByteArray(ETC1Data, 0); testTexture.uploadCompressedTextureFromByteArray(ETC1Data, 0);
result = ETC1; result = ETC1;
} catch(e:Error) { } catch(e:Error) {
result = NONE; result = NONE;
} }
} }
testTexture.dispose(); testTexture.dispose();
return result; return result;
} }
public static function vectorNumberToByteArray(vector:Vector.<Number>):ByteArray { public static function vectorNumberToByteArray(vector:Vector.<Number>):ByteArray {
var result:ByteArray = new ByteArray(); var result:ByteArray = new ByteArray();
result.endian = Endian.LITTLE_ENDIAN; result.endian = Endian.LITTLE_ENDIAN;
for (var i:int = 0; i < vector.length; i++) { for (var i:int = 0; i < vector.length; i++) {
result.writeFloat(vector[i]); result.writeFloat(vector[i]);
} }
result.position = 0; result.position = 0;
return result; return result;
} }
public static function byteArrayToVectorUint(byteArray:ByteArray):Vector.<uint> { public static function byteArrayToVectorUint(byteArray:ByteArray):Vector.<uint> {
var result:Vector.<uint> = new Vector.<uint>(); var result:Vector.<uint> = new Vector.<uint>();
var length:uint = 0; var length:uint = 0;
byteArray.position = 0; byteArray.position = 0;
byteArray.endian = Endian.LITTLE_ENDIAN; byteArray.endian = Endian.LITTLE_ENDIAN;
while (byteArray.bytesAvailable > 0) { while (byteArray.bytesAvailable > 0) {
result[length++] = byteArray.readUnsignedShort(); result[length++] = byteArray.readUnsignedShort();
} }
return result; return result;
} }
public static function createVertexBufferFromByteArray(context:Context3D, byteArray:ByteArray, numVertices:uint, stride:uint = 3):VertexBuffer3D { public static function createVertexBufferFromByteArray(context:Context3D, byteArray:ByteArray, numVertices:uint, stride:uint = 3):VertexBuffer3D {
if (context == null) { if (context == null) {
throw new ReferenceError("context is not set"); throw new ReferenceError("context is not set");
} }
var buffer:VertexBuffer3D = context.createVertexBuffer(numVertices, stride); var buffer:VertexBuffer3D = context.createVertexBuffer(numVertices, stride);
buffer.uploadFromByteArray(byteArray, 0, 0, numVertices); buffer.uploadFromByteArray(byteArray, 0, 0, numVertices);
return buffer; return buffer;
} }
public static function createVertexBufferFromVector(context:Context3D, vector:Vector.<Number>, numVertices:uint, stride:uint = 3):VertexBuffer3D { public static function createVertexBufferFromVector(context:Context3D, vector:Vector.<Number>, numVertices:uint, stride:uint = 3):VertexBuffer3D {
if (context == null) { if (context == null) {
throw new ReferenceError("context is not set"); throw new ReferenceError("context is not set");
} }
var buffer:VertexBuffer3D = context.createVertexBuffer(numVertices, stride); var buffer:VertexBuffer3D = context.createVertexBuffer(numVertices, stride);
var byteArray:ByteArray = A3DUtils.vectorNumberToByteArray(vector); var byteArray:ByteArray = A3DUtils.vectorNumberToByteArray(vector);
buffer.uploadFromByteArray(byteArray, 0, 0, numVertices); buffer.uploadFromByteArray(byteArray, 0, 0, numVertices);
return buffer; return buffer;
} }
public static function createTextureFromByteArray(context:Context3D, byteArray:ByteArray, width:Number, height:Number, format:String):Texture { public static function createTextureFromByteArray(context:Context3D, byteArray:ByteArray, width:Number, height:Number, format:String):Texture {
if (context == null) { if (context == null) {
throw new ReferenceError("context is not set"); throw new ReferenceError("context is not set");
} }
var texture:Texture = context.createTexture(width, height, format, false); var texture:Texture = context.createTexture(width, height, format, false);
texture.uploadCompressedTextureFromByteArray(byteArray, 0); texture.uploadCompressedTextureFromByteArray(byteArray, 0);
return texture; return texture;
} }
public static function createIndexBufferFromByteArray(context:Context3D, byteArray:ByteArray, numIndices:uint):IndexBuffer3D { public static function createIndexBufferFromByteArray(context:Context3D, byteArray:ByteArray, numIndices:uint):IndexBuffer3D {
if (context == null) { if (context == null) {
throw new ReferenceError("context is not set"); throw new ReferenceError("context is not set");
} }
var buffer:IndexBuffer3D = context.createIndexBuffer(numIndices); var buffer:IndexBuffer3D = context.createIndexBuffer(numIndices);
buffer.uploadFromByteArray(byteArray, 0, 0, numIndices); buffer.uploadFromByteArray(byteArray, 0, 0, numIndices);
return buffer; return buffer;
} }
public static function createIndexBufferFromVector(context:Context3D, vector:Vector.<uint>, numIndices:int = -1):IndexBuffer3D { public static function createIndexBufferFromVector(context:Context3D, vector:Vector.<uint>, numIndices:int = -1):IndexBuffer3D {
if (context == null) { if (context == null) {
throw new ReferenceError("context is not set"); throw new ReferenceError("context is not set");
} }
var count:uint = numIndices > 0 ? numIndices : vector.length; var count:uint = numIndices > 0 ? numIndices : vector.length;
var buffer:IndexBuffer3D = context.createIndexBuffer(count); var buffer:IndexBuffer3D = context.createIndexBuffer(count);
buffer.uploadFromVector(vector, 0, count); buffer.uploadFromVector(vector, 0, count);
var byteArray:ByteArray = new ByteArray(); var byteArray:ByteArray = new ByteArray();
byteArray.endian = Endian.LITTLE_ENDIAN; byteArray.endian = Endian.LITTLE_ENDIAN;
for (var i:int = 0; i < count; i++) { for (var i:int = 0; i < count; i++) {
byteArray.writeInt(vector[i]); byteArray.writeInt(vector[i]);
} }
byteArray.position = 0; byteArray.position = 0;
buffer.uploadFromVector(vector, 0, count); buffer.uploadFromVector(vector, 0, count);
return buffer; return buffer;
} }
// Disassembler // Disassembler
private static var programType:Vector.<String> = Vector.<String>(["VERTEX", "FRAGMENT"]); private static var programType:Vector.<String> = Vector.<String>(["VERTEX", "FRAGMENT"]);
private static var samplerDimension:Vector.<String> = Vector.<String>(["2D", "cube", "3D"]); private static var samplerDimension:Vector.<String> = Vector.<String>(["2D", "cube", "3D"]);
private static var samplerWraping:Vector.<String> = Vector.<String>(["clamp", "repeat"]); private static var samplerWraping:Vector.<String> = Vector.<String>(["clamp", "repeat"]);
private static var samplerMipmap:Vector.<String> = Vector.<String>(["mipnone", "mipnearest", "miplinear"]); private static var samplerMipmap:Vector.<String> = Vector.<String>(["mipnone", "mipnearest", "miplinear"]);
private static var samplerFilter:Vector.<String> = Vector.<String>(["nearest", "linear"]); private static var samplerFilter:Vector.<String> = Vector.<String>(["nearest", "linear"]);
private static var swizzleType:Vector.<String> = Vector.<String>(["x", "y", "z", "w"]); private static var swizzleType:Vector.<String> = Vector.<String>(["x", "y", "z", "w"]);
private static var twoOperandsCommands:Dictionary; private static var twoOperandsCommands:Dictionary;
// TODO: option to turn off auto-prefixes // TODO: option to turn off auto-prefixes
public static function disassemble(byteCode:ByteArray):String { public static function disassemble(byteCode:ByteArray):String {
if (!twoOperandsCommands) { if (!twoOperandsCommands) {
twoOperandsCommands = new Dictionary(); twoOperandsCommands = new Dictionary();
twoOperandsCommands[0x1] = true; twoOperandsCommands[0x1] = true;
twoOperandsCommands[0x2] = true; twoOperandsCommands[0x2] = true;
twoOperandsCommands[0x3] = true; twoOperandsCommands[0x3] = true;
twoOperandsCommands[0x4] = true; twoOperandsCommands[0x4] = true;
twoOperandsCommands[0x6] = true; twoOperandsCommands[0x6] = true;
twoOperandsCommands[0xb] = true; twoOperandsCommands[0xb] = true;
twoOperandsCommands[0x11] = true; twoOperandsCommands[0x11] = true;
twoOperandsCommands[0x12] = true; twoOperandsCommands[0x12] = true;
twoOperandsCommands[0x13] = true; twoOperandsCommands[0x13] = true;
twoOperandsCommands[0x17] = true; twoOperandsCommands[0x17] = true;
twoOperandsCommands[0x18] = true; twoOperandsCommands[0x18] = true;
twoOperandsCommands[0x19] = true; twoOperandsCommands[0x19] = true;
twoOperandsCommands[0x26] = true; twoOperandsCommands[0x26] = true;
twoOperandsCommands[0x28] = true; twoOperandsCommands[0x28] = true;
twoOperandsCommands[0x29] = true; twoOperandsCommands[0x29] = true;
twoOperandsCommands[0x2a] = true; twoOperandsCommands[0x2a] = true;
twoOperandsCommands[0x2c] = true; twoOperandsCommands[0x2c] = true;
twoOperandsCommands[0x2d] = true; twoOperandsCommands[0x2d] = true;
} }
var res:String = ""; var res:String = "";
byteCode.position = 0; byteCode.position = 0;
if (byteCode.bytesAvailable < 7) { if (byteCode.bytesAvailable < 7) {
return "error in byteCode header"; return "error in byteCode header";
} }
res += "magic = " + byteCode.readUnsignedByte().toString(16); res += "magic = " + byteCode.readUnsignedByte().toString(16);
res += "\nversion = " + byteCode.readInt().toString(10); res += "\nversion = " + byteCode.readInt().toString(10);
res += "\nshadertypeid = " + byteCode.readUnsignedByte().toString(16); res += "\nshadertypeid = " + byteCode.readUnsignedByte().toString(16);
var pType:String = programType[byteCode.readByte()]; var pType:String = programType[byteCode.readByte()];
res += "\nshadertype = " + pType; res += "\nshadertype = " + pType;
res += "\nsource\n"; res += "\nsource\n";
pType = pType.substring(0, 1).toLowerCase(); pType = pType.substring(0, 1).toLowerCase();
var lineNumber:uint = 1; var lineNumber:uint = 1;
while (byteCode.bytesAvailable - 24 >= 0) { while (byteCode.bytesAvailable - 24 >= 0) {
res += (lineNumber++).toString() + ": " + getCommand(byteCode, pType) + "\n"; res += (lineNumber++).toString() + ": " + getCommand(byteCode, pType) + "\n";
} }
if (byteCode.bytesAvailable > 0) { if (byteCode.bytesAvailable > 0) {
res += "\nunexpected byteCode length. extra bytes:" + byteCode.bytesAvailable; res += "\nunexpected byteCode length. extra bytes:" + byteCode.bytesAvailable;
} }
return res; return res;
} }
private static function getCommand(byteCode:ByteArray, programType:String):String { private static function getCommand(byteCode:ByteArray, programType:String):String {
var cmd:uint = byteCode.readUnsignedInt(); var cmd:uint = byteCode.readUnsignedInt();
var command:String = CommandType.COMMAND_NAMES[cmd]; var command:String = CommandType.COMMAND_NAMES[cmd];
var result:String; var result:String;
var destNumber:uint = byteCode.readUnsignedShort(); var destNumber:uint = byteCode.readUnsignedShort();
var swizzle:uint = byteCode.readByte(); var swizzle:uint = byteCode.readByte();
var s:String = ""; var s:String = "";
var destSwizzle:uint = 4; var destSwizzle:uint = 4;
if (swizzle < 15) { if (swizzle < 15) {
s += "."; s += ".";
s += ((swizzle & 0x1) > 0) ? "x" : ""; s += ((swizzle & 0x1) > 0) ? "x" : "";
s += ((swizzle & 0x2) > 0) ? "y" : ""; s += ((swizzle & 0x2) > 0) ? "y" : "";
s += ((swizzle & 0x4) > 0) ? "z" : ""; s += ((swizzle & 0x4) > 0) ? "z" : "";
s += ((swizzle & 0x8) > 0) ? "w" : ""; s += ((swizzle & 0x8) > 0) ? "w" : "";
destSwizzle = s.length - 1; destSwizzle = s.length - 1;
} }
var sourceSwizzleLimit:int = destSwizzle; var sourceSwizzleLimit:int = destSwizzle;
if (cmd == CommandType.TEX) { if (cmd == CommandType.TEX) {
sourceSwizzleLimit = 2; sourceSwizzleLimit = 2;
} else if (cmd == CommandType.DP3) { } else if (cmd == CommandType.DP3) {
sourceSwizzleLimit = 3; sourceSwizzleLimit = 3;
} else if (cmd == CommandType.DP4) { } else if (cmd == CommandType.DP4) {
sourceSwizzleLimit = 4; sourceSwizzleLimit = 4;
} }
var destType:String = VariableType.TYPE_NAMES[byteCode.readUnsignedByte()].charAt(0); var destType:String = VariableType.TYPE_NAMES[byteCode.readUnsignedByte()].charAt(0);
result = command + " " + attachProgramPrefix(destType, programType) + destNumber.toString() + s + ", "; result = command + " " + attachProgramPrefix(destType, programType) + destNumber.toString() + s + ", ";
result += attachProgramPrefix(getSourceVariable(byteCode, sourceSwizzleLimit), programType); result += attachProgramPrefix(getSourceVariable(byteCode, sourceSwizzleLimit), programType);
if (twoOperandsCommands[cmd]) { if (twoOperandsCommands[cmd]) {
if (cmd == CommandType.TEX || cmd == CommandType.TED) { if (cmd == CommandType.TEX || cmd == CommandType.TED) {
result += ", " + attachProgramPrefix(getSamplerVariable(byteCode), programType); result += ", " + attachProgramPrefix(getSamplerVariable(byteCode), programType);
} else { } else {
result += ", " + attachProgramPrefix(getSourceVariable(byteCode, sourceSwizzleLimit), programType); result += ", " + attachProgramPrefix(getSourceVariable(byteCode, sourceSwizzleLimit), programType);
} }
} else { } else {
byteCode.readDouble(); byteCode.readDouble();
} }
if (cmd == CommandType.ELS || cmd == CommandType.EIF) { if (cmd == CommandType.ELS || cmd == CommandType.EIF) {
result = " " + command; result = " " + command;
} }
return result; return result;
} }
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)) {
return "o"+variable; return "o"+variable;
} else if (char != "v".charCodeAt(0)) { } else if (char != "v".charCodeAt(0)) {
return programType + variable; return programType + variable;
} }
return variable; return variable;
} }
private static function getSamplerVariable(byteCode:ByteArray):String { private static function getSamplerVariable(byteCode:ByteArray):String {
var number:uint = byteCode.readUnsignedInt(); var number:uint = byteCode.readUnsignedInt();
byteCode.readByte(); byteCode.readByte();
var dim:uint = byteCode.readByte() >> 4; var dim:uint = byteCode.readByte() >> 4;
var wraping:uint = byteCode.readByte() >> 4; var wraping:uint = byteCode.readByte() >> 4;
var n:uint = byteCode.readByte(); var n:uint = byteCode.readByte();
return "s" + number.toString() + " <" + samplerDimension[dim] + ", " + samplerWraping[wraping] return "s" + number.toString() + " <" + samplerDimension[dim] + ", " + samplerWraping[wraping]
+ ", " + samplerFilter[(n >> 4) & 0xf] + ", " + samplerMipmap[n & 0xf] + ">"; + ", " + samplerFilter[(n >> 4) & 0xf] + ", " + samplerMipmap[n & 0xf] + ">";
} }
private static function getSourceVariable(byteCode:ByteArray, swizzleLimit:uint):String { private static function getSourceVariable(byteCode:ByteArray, swizzleLimit:uint):String {
var s1Number:uint = byteCode.readUnsignedShort(); var s1Number:uint = byteCode.readUnsignedShort();
var offset:uint = byteCode.readUnsignedByte(); var offset:uint = byteCode.readUnsignedByte();
var s:String = getSourceSwizzle(byteCode.readUnsignedByte(), swizzleLimit); var s:String = getSourceSwizzle(byteCode.readUnsignedByte(), swizzleLimit);
var s1Type:String = VariableType.TYPE_NAMES[byteCode.readUnsignedByte()].charAt(0); var s1Type:String = VariableType.TYPE_NAMES[byteCode.readUnsignedByte()].charAt(0);
var indexType:String = VariableType.TYPE_NAMES[byteCode.readUnsignedByte()].charAt(0); var indexType:String = VariableType.TYPE_NAMES[byteCode.readUnsignedByte()].charAt(0);
var comp:String = swizzleType[byteCode.readUnsignedByte()]; var comp:String = swizzleType[byteCode.readUnsignedByte()];
if (byteCode.readUnsignedByte() > 0) { if (byteCode.readUnsignedByte() > 0) {
return s1Type + "[" + indexType + s1Number.toString() + "." + comp + ((offset > 0) ? ("+" + offset.toString()) : "") + "]" + s; return s1Type + "[" + indexType + s1Number.toString() + "." + comp + ((offset > 0) ? ("+" + offset.toString()) : "") + "]" + s;
} }
return s1Type + s1Number.toString() + s; return s1Type + s1Number.toString() + s;
} }
private static function getSourceSwizzle(swizzle:uint, swizzleLimit:uint = 4):String { private static function getSourceSwizzle(swizzle:uint, swizzleLimit:uint = 4):String {
var s:String = ""; var s:String = "";
if (swizzle != 0xe4) { if (swizzle != 0xe4) {
s += "."; s += ".";
s += swizzleType[(swizzle & 0x3)]; s += swizzleType[(swizzle & 0x3)];
s += swizzleType[(swizzle >> 2) & 0x3]; s += swizzleType[(swizzle >> 2) & 0x3];
s += swizzleType[(swizzle >> 4) & 0x3]; s += swizzleType[(swizzle >> 4) & 0x3];
s += swizzleType[(swizzle >> 6) & 0x3]; s += swizzleType[(swizzle >> 6) & 0x3];
s = swizzleLimit < 4 ? s.substring(0, swizzleLimit + 1) : s; s = swizzleLimit < 4 ? s.substring(0, swizzleLimit + 1) : s;
} }
return s; return s;
} }
alternativa3d static function checkParent(child:Class, parent:Class):Boolean { alternativa3d static function checkParent(child:Class, parent:Class):Boolean {
var current:Class = child; var current:Class = child;
if (parent == null) return true; if (parent == null) return true;
while (true) { while (true) {
if (current == parent) return true; if (current == parent) return true;
var className:String = getQualifiedSuperclassName(current); var className:String = getQualifiedSuperclassName(current);
if (className != null) { if (className != null) {
current = getDefinitionByName(className) as Class; current = getDefinitionByName(className) as Class;
} else return false; } else return false;
} }
return false; return false;
} }
} }
} }

View File

@@ -1,148 +1,148 @@
/** /**
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
* If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. * If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice.
* You may add additional accurate notices of copyright ownership. * You may add additional accurate notices of copyright ownership.
* *
* 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
*/ */
public class CommandType { public class CommandType {
public static const MOV : uint = 0x00; public static const MOV : uint = 0x00;
public static const ADD : uint = 0x01; public static const ADD : uint = 0x01;
public static const SUB : uint = 0x02; public static const SUB : uint = 0x02;
public static const MUL : uint = 0x03; public static const MUL : uint = 0x03;
public static const DIV : uint = 0x04; public static const DIV : uint = 0x04;
public static const RCP : uint = 0x05; public static const RCP : uint = 0x05;
public static const MIN : uint = 0x06; public static const MIN : uint = 0x06;
public static const MAX : uint = 0x07; public static const MAX : uint = 0x07;
public static const FRC : uint = 0x08; public static const FRC : uint = 0x08;
public static const SQT : uint = 0x09; public static const SQT : uint = 0x09;
public static const RSQ : uint = 0x0a; public static const RSQ : uint = 0x0a;
public static const POW : uint = 0x0b; public static const POW : uint = 0x0b;
public static const LOG : uint = 0x0c; public static const LOG : uint = 0x0c;
public static const EXP : uint = 0x0d; public static const EXP : uint = 0x0d;
public static const NRM : uint = 0x0e; public static const NRM : uint = 0x0e;
public static const SIN : uint = 0x0f; public static const SIN : uint = 0x0f;
public static const COS : uint = 0x10; public static const COS : uint = 0x10;
public static const CRS : uint = 0x11; public static const CRS : uint = 0x11;
public static const DP3 : uint = 0x12; public static const DP3 : uint = 0x12;
public static const DP4 : uint = 0x13; public static const DP4 : uint = 0x13;
public static const ABS : uint = 0x14; public static const ABS : uint = 0x14;
public static const NEG : uint = 0x15; public static const NEG : uint = 0x15;
public static const SAT : uint = 0x16; public static const SAT : uint = 0x16;
public static const M33 : uint = 0x17; public static const M33 : uint = 0x17;
public static const M44 : uint = 0x18; public static const M44 : uint = 0x18;
public static const M34 : uint = 0x19; public static const M34 : uint = 0x19;
public static const DDX : uint = 0x1a; public static const DDX : uint = 0x1a;
public static const DDY : uint = 0x1b; public static const DDY : uint = 0x1b;
public static const IFE : uint = 0x1c; public static const IFE : uint = 0x1c;
public static const INE : uint = 0x1d; public static const INE : uint = 0x1d;
public static const IFG : uint = 0x1e; public static const IFG : uint = 0x1e;
public static const IFL : uint = 0x1f; public static const IFL : uint = 0x1f;
public static const ELS : uint = 0x20; public static const ELS : uint = 0x20;
public static const EIF : uint = 0x21; public static const EIF : uint = 0x21;
public static const TED : uint = 0x26; public static const TED : uint = 0x26;
public static const KIL : uint = 0x27; public static const KIL : uint = 0x27;
public static const TEX : uint = 0x28; public static const TEX : uint = 0x28;
// set if greater equal // set if greater equal
public static const SGE : uint = 0x29; public static const SGE : uint = 0x29;
// set if less than // set if less than
public static const SLT : uint = 0x2a; public static const SLT : uint = 0x2a;
// set if greater than // set if greater than
public static const SGN : uint = 0x2b; public static const SGN : uint = 0x2b;
// set if equal // set if equal
public static const SEQ : uint = 0x2c; public static const SEQ : uint = 0x2c;
// 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";
COMMAND_NAMES[MUL] = "mul"; COMMAND_NAMES[MUL] = "mul";
COMMAND_NAMES[DIV] = "div"; COMMAND_NAMES[DIV] = "div";
COMMAND_NAMES[RCP] = "rcp"; COMMAND_NAMES[RCP] = "rcp";
COMMAND_NAMES[MIN] = "min"; COMMAND_NAMES[MIN] = "min";
COMMAND_NAMES[MAX] = "max"; COMMAND_NAMES[MAX] = "max";
COMMAND_NAMES[FRC] = "frc"; COMMAND_NAMES[FRC] = "frc";
COMMAND_NAMES[SQT] = "sqt"; COMMAND_NAMES[SQT] = "sqt";
COMMAND_NAMES[RSQ] = "rsq"; COMMAND_NAMES[RSQ] = "rsq";
COMMAND_NAMES[POW] = "pow"; COMMAND_NAMES[POW] = "pow";
COMMAND_NAMES[LOG] = "log"; COMMAND_NAMES[LOG] = "log";
COMMAND_NAMES[EXP] = "exp"; COMMAND_NAMES[EXP] = "exp";
COMMAND_NAMES[NRM] = "nrm"; COMMAND_NAMES[NRM] = "nrm";
COMMAND_NAMES[SIN] = "sin"; COMMAND_NAMES[SIN] = "sin";
COMMAND_NAMES[COS] = "cos"; COMMAND_NAMES[COS] = "cos";
COMMAND_NAMES[CRS] = "crs"; COMMAND_NAMES[CRS] = "crs";
COMMAND_NAMES[DP3] = "dp3"; COMMAND_NAMES[DP3] = "dp3";
COMMAND_NAMES[DP4] = "dp4"; COMMAND_NAMES[DP4] = "dp4";
COMMAND_NAMES[ABS] = "abs"; COMMAND_NAMES[ABS] = "abs";
COMMAND_NAMES[NEG] = "neg"; COMMAND_NAMES[NEG] = "neg";
COMMAND_NAMES[SAT] = "sat"; COMMAND_NAMES[SAT] = "sat";
COMMAND_NAMES[M33] = "m33"; COMMAND_NAMES[M33] = "m33";
COMMAND_NAMES[M44] = "m44"; COMMAND_NAMES[M44] = "m44";
COMMAND_NAMES[M34] = "m34"; COMMAND_NAMES[M34] = "m34";
COMMAND_NAMES[DDX] = "ddx"; COMMAND_NAMES[DDX] = "ddx";
COMMAND_NAMES[DDY] = "ddy"; COMMAND_NAMES[DDY] = "ddy";
COMMAND_NAMES[IFE] = "ife"; COMMAND_NAMES[IFE] = "ife";
COMMAND_NAMES[INE] = "ine"; COMMAND_NAMES[INE] = "ine";
COMMAND_NAMES[IFG] = "ifg"; COMMAND_NAMES[IFG] = "ifg";
COMMAND_NAMES[IFL] = "ifl"; COMMAND_NAMES[IFL] = "ifl";
COMMAND_NAMES[ELS] = "els"; COMMAND_NAMES[ELS] = "els";
COMMAND_NAMES[EIF] = "eif"; COMMAND_NAMES[EIF] = "eif";
COMMAND_NAMES[TED] = "ted"; COMMAND_NAMES[TED] = "ted";
COMMAND_NAMES[KIL] = "kil"; COMMAND_NAMES[KIL] = "kil";
COMMAND_NAMES[TEX] = "tex"; COMMAND_NAMES[TEX] = "tex";
COMMAND_NAMES[SGE] = "sge"; COMMAND_NAMES[SGE] = "sge";
COMMAND_NAMES[SLT] = "slt"; COMMAND_NAMES[SLT] = "slt";
COMMAND_NAMES[SGN] = "sgn"; COMMAND_NAMES[SGN] = "sgn";
COMMAND_NAMES[SEQ] = "seq"; COMMAND_NAMES[SEQ] = "seq";
COMMAND_NAMES[SNE] = "sne"; COMMAND_NAMES[SNE] = "sne";
} }
} }

View File

@@ -1,74 +1,74 @@
/** /**
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
* If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. * If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice.
* You may add additional accurate notices of copyright ownership. * You may add additional accurate notices of copyright ownership.
* *
* 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 DestinationVariable extends Variable { public class DestinationVariable extends Variable {
public function DestinationVariable(source:String) { public function DestinationVariable(source:String) {
var strType : String = source.match(/[tovid]/)[0]; var strType : String = source.match(/[tovid]/)[0];
index = parseInt(source.match(/\d+/)[0], 10); index = parseInt(source.match(/\d+/)[0], 10);
var swizzle:Array = source.match(/\.[xyzw]{1,4}/); var swizzle:Array = source.match(/\.[xyzw]{1,4}/);
var regmask:uint; var regmask:uint;
var maskmatch:String = swizzle ? swizzle[0] : null; var maskmatch:String = swizzle ? swizzle[0] : null;
if (maskmatch != null) { if (maskmatch != null) {
regmask = 0; regmask = 0;
var cv:int; var cv:int;
var maskLength:uint = maskmatch.length; var maskLength:uint = maskmatch.length;
// If first char is point, then skip // If first char is point, then skip
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 |= 1 << cv; regmask |= 1 << cv;
} }
} else { } else {
regmask = 0xf; regmask = 0xf;
// id swizzle or mask // id swizzle or mask
} }
lowerCode = (regmask << 16) | index; lowerCode = (regmask << 16) | index;
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;
} }
} }
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 {
byteCode.position = position + offset; byteCode.position = position + offset;
byteCode.writeUnsignedInt((lowerCode & ~(0xf00ffff)) | newIndex | (newType << 24)); byteCode.writeUnsignedInt((lowerCode & ~(0xf00ffff)) | newIndex | (newType << 24));
} }
} }
} }

View File

@@ -1,426 +1,426 @@
/** /**
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
* If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. * If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice.
* You may add additional accurate notices of copyright ownership. * You may add additional accurate notices of copyright ownership.
* *
* 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;
import flash.utils.ByteArray; import flash.utils.ByteArray;
import flash.utils.Dictionary; import flash.utils.Dictionary;
import flash.utils.Endian; import flash.utils.Endian;
use namespace alternativa3d; use namespace alternativa3d;
/** /**
* @private * @private
* Dynamic shader linker * Dynamic shader linker
*/ */
public class Linker { public class Linker {
/** /**
* Data after linking. * Data after linking.
*/ */
public var data:ByteArray = null; public var data:ByteArray = null;
/** /**
* Number of used slots. * Number of used slots.
*/ */
public var slotsCount:int = 0; public var slotsCount:int = 0;
/** /**
* Number of lines of the shader code. * Number of lines of the shader code.
*/ */
public var commandsCount:int = 0; public var commandsCount:int = 0;
/** /**
* Linker type. Can be vertex of fragment. * Linker type. Can be vertex of fragment.
*/ */
public var type:String; public var type:String;
private var procedures:Vector.<Procedure> = new Vector.<Procedure>(); private var procedures:Vector.<Procedure> = new Vector.<Procedure>();
/** /**
* @private * @private
* Variables after linking. * Variables after linking.
*/ */
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();
// Key - procedure, value - array of strings. // Key - procedure, value - array of strings.
private var _outputParams:Dictionary = new Dictionary(); private var _outputParams:Dictionary = new Dictionary();
// 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.
* *
* @param programType Type of shader. * @param programType Type of shader.
*/ */
public function Linker(programType:String) { public function Linker(programType:String) {
type = programType; type = programType;
} }
/** /**
* Clears a content. * Clears a content.
*/ */
public function clear():void { public function clear():void {
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;
_linkedVariables = null; _linkedVariables = null;
_inputParams = new Dictionary(); _inputParams = new Dictionary();
_outputParams = new Dictionary(); _outputParams = new Dictionary();
} }
/** /**
* Adds a new shader procedure. * Adds a new shader procedure.
* *
* @param procedure Procedure to add. * @param procedure Procedure to add.
* *
* @see Procedure * @see Procedure
*/ */
public function addProcedure(procedure:Procedure, ...args):void { public function addProcedure(procedure:Procedure, ...args):void {
for each(var v:Variable in procedure.variablesUsages[VariableType.VARYING]) { for each(var v:Variable in procedure.variablesUsages[VariableType.VARYING]) {
if (v == null) continue; if (v == null) continue;
var nv:Variable = _varyings[v.name] = new Variable(); var nv:Variable = _varyings[v.name] = new Variable();
nv.name = v.name; nv.name = v.name;
nv.type = v.type; nv.type = v.type;
nv.index = -1; nv.index = -1;
} }
procedures.push(procedure); procedures.push(procedure);
_inputParams[procedure] = args; _inputParams[procedure] = args;
data = null; data = null;
} }
/** /**
* Declaration of variable of given type. * Declaration of variable of given type.
* *
* @param name Name of variable * @param name Name of variable
* @param type Type of variable. Should be one of the VariableType constants. The default value is Temporary variable. * @param type Type of variable. Should be one of the VariableType constants. The default value is Temporary variable.
* *
* @see VariableType * @see VariableType
*/ */
public function declareVariable(name:String, type:uint = 2):void { public function declareVariable(name:String, type:uint = 2):void {
var v:Variable = new Variable(); var v:Variable = new Variable();
v.index = -1; v.index = -1;
v.type = type; v.type = type;
v.name = name; v.name = name;
_localVariables[name] = v; _localVariables[name] = v;
if (v.type == VariableType.VARYING) { if (v.type == VariableType.VARYING) {
_varyings[v.name] = v; _varyings[v.name] = v;
} }
data = null; data = null;
} }
public function declareSampler(output:String, uv:String, sampler:String, options:String):void { public function declareSampler(output:String, uv:String, sampler:String, options:String):void {
if (_localVariables[uv] == null) { if (_localVariables[uv] == null) {
throw new ArgumentError("Undefined variable " + uv); throw new ArgumentError("Undefined variable " + uv);
} }
if (_localVariables[sampler] == null) { if (_localVariables[sampler] == null) {
throw new ArgumentError("Undefined variable " + sampler); throw new ArgumentError("Undefined variable " + sampler);
} }
if (_localVariables[output] == null) { if (_localVariables[output] == null) {
declareVariable(output, 2); declareVariable(output, 2);
} }
data = null; data = null;
} }
/** /**
* Setting of input parameters of procedure. * Setting of input parameters of procedure.
* *
* @param procedure A procedure to which parameters will be set. * @param procedure A procedure to which parameters will be set.
* @param args Names of variables, separated by the comma, that are passed into the procedure. * @param args Names of variables, separated by the comma, that are passed into the procedure.
* Variables must be previously declared, using the method <code>declareVariable()</code>. * Variables must be previously declared, using the method <code>declareVariable()</code>.
* *
* @see #declareVariable() * @see #declareVariable()
*/ */
public function setInputParams(procedure:Procedure, ...args):void { public function setInputParams(procedure:Procedure, ...args):void {
_inputParams[procedure] = args; _inputParams[procedure] = args;
data = null; data = null;
} }
/** /**
* Setting of output parameters of procedure. * Setting of output parameters of procedure.
* *
* @param procedure A procedure to which parameters will be set. * @param procedure A procedure to which parameters will be set.
* @param args Names of variables, separated by the comma, that are passed into the procedure. * @param args Names of variables, separated by the comma, that are passed into the procedure.
* Variables must be previously declared, using the method declareVariable(). * Variables must be previously declared, using the method declareVariable().
* *
* @see #declareVariable() * @see #declareVariable()
*/ */
public function setOutputParams(procedure:Procedure, ...args):void { public function setOutputParams(procedure:Procedure, ...args):void {
_outputParams[procedure] = args; _outputParams[procedure] = args;
data = null; data = null;
} }
/** /**
* Returns of index of variable after the linking. * Returns of index of variable after the linking.
* *
* @param name Name of variable. * @param name Name of variable.
* @return Its index for sending to Context3D * @return Its index for sending to Context3D
*/ */
public function getVariableIndex(name:String):int { public function getVariableIndex(name:String):int {
if (_linkedVariables == null) throw new Error("Not linked"); if (_linkedVariables == null) throw new Error("Not linked");
var variable:Variable = _linkedVariables[name]; var variable:Variable = _linkedVariables[name];
if (variable == null) { if (variable == null) {
throw new Error('Variable "' + name + '" not found'); throw new Error('Variable "' + name + '" not found');
} }
return variable.index; return variable.index;
} }
/** /**
* Returns index of variable or <code>-1</code> there is no variable with such name. * Returns index of variable or <code>-1</code> there is no variable with such name.
*/ */
public function findVariable(name:String):int { public function findVariable(name:String):int {
if (_linkedVariables == null) throw new Error("Has not linked"); if (_linkedVariables == null) throw new Error("Has not linked");
var variable:Variable = _linkedVariables[name]; var variable:Variable = _linkedVariables[name];
if (variable == null) { if (variable == null) {
return -1; return -1;
} }
return variable.index; return variable.index;
} }
/** /**
* Returns the existence of this variable in linked code. * Returns the existence of this variable in linked code.
* @param name Name of variable * @param name Name of variable
*/ */
public function containsVariable(name:String):Boolean { public function containsVariable(name:String):Boolean {
if (_linkedVariables == null) throw new Error("Not linked"); if (_linkedVariables == null) throw new Error("Not linked");
return _linkedVariables[name] != null; return _linkedVariables[name] != null;
} }
/** /**
* Linking of procedures to one shader. * Linking of procedures to one shader.
*/ */
public function link(version:uint = 1):void { public function link(version:uint = 1):void {
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;
for each (v in _localVariables) { for each (v in _localVariables) {
nv = variables[v.name] = new Variable(); nv = variables[v.name] = new Variable();
nv.index = -1; nv.index = -1;
nv.type = v.type; nv.type = v.type;
nv.name = v.name; nv.name = v.name;
nv.size = v.size; nv.size = v.size;
} }
data = new ByteArray(); data = new ByteArray();
data.endian = Endian.LITTLE_ENDIAN; data.endian = Endian.LITTLE_ENDIAN;
data.writeByte(0xa0); data.writeByte(0xa0);
// tag version // tag version
data.writeUnsignedInt(version); data.writeUnsignedInt(version);
// AGAL version, big endian, bit pattern will be 0x01000000 // AGAL version, big endian, bit pattern will be 0x01000000
data.writeByte(0xa1); data.writeByte(0xa1);
// tag program id // tag program id
data.writeByte((type == Context3DProgramType.FRAGMENT) ? 1 : 0); // vertex or fragment data.writeByte((type == Context3DProgramType.FRAGMENT) ? 1 : 0); // vertex or fragment
commandsCount = 0; commandsCount = 0;
slotsCount = 0; slotsCount = 0;
_locals[0] = 0; _locals[0] = 0;
_locals[1] = 0; _locals[1] = 0;
_locals[2] = 0; _locals[2] = 0;
_locals[3] = 0; _locals[3] = 0;
_locals[4] = 0; _locals[4] = 0;
_locals[5] = 0; _locals[5] = 0;
_locals[6] = 0; _locals[6] = 0;
// First iteration - collecting of variables. // First iteration - collecting of variables.
for each (p in procedures) { for each (p in procedures) {
_locals[1] += p.reservedConstants; _locals[1] += p.reservedConstants;
var iLength:int = p.variablesUsages.length; var iLength:int = p.variablesUsages.length;
for (i = 0; i < iLength; i++) { for (i = 0; i < iLength; i++) {
var vector:Vector.<Variable> = p.variablesUsages[i]; var vector:Vector.<Variable> = p.variablesUsages[i];
var jLength:int = vector.length; var jLength:int = vector.length;
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) {
throw new Error("Linkage error: Noname variable. Procedure = " + p.name + ", type = " + i.toString() + ", index = " + j.toString()); // 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(); }
nv.index = -1; nv = variables[v.name] = new Variable();
nv.type = v.type; nv.index = -1;
nv.name = v.name; nv.type = v.type;
nv.size = v.size; nv.name = v.name;
} nv.size = v.size;
} }
} }
}
for each (p in procedures) {
// Changing of inputs for each (p in procedures) {
var offset:int = data.length; // Changing of inputs
data.position = data.length; var offset:int = data.length;
data.writeBytes(p.byteCode, 0, p.byteCode.length); data.position = data.length;
var input:Array = _inputParams[p]; data.writeBytes(p.byteCode, 0, p.byteCode.length);
var output:Array = _outputParams[p]; var input:Array = _inputParams[p];
var param:String; var output:Array = _outputParams[p];
var numParams:int; var param:String;
if (input != null) { var numParams:int;
numParams = input.length; if (input != null) {
for (j = 0; j < numParams; j++) { numParams = input.length;
param = input[j]; for (j = 0; j < numParams; j++) {
v = variables[param]; param = input[j];
if (v == null) { v = variables[param];
throw new Error("Input parameter not set. paramName = " + param); if (v == null) {
} throw new Error("Input parameter not set. paramName = " + param);
if (p.variablesUsages[7].length > j) { }
var inParam:Variable = p.variablesUsages[7][j]; if (p.variablesUsages[7].length > j) {
if (inParam == null) { var inParam:Variable = p.variablesUsages[7][j];
throw new Error("Input parameter set, but not exist in code. paramName = " + param + ", register = i" + j.toString()); if (inParam == null) {
} 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]; if (v.index < 0) {
_locals[v.type] += v.size; v.index = _locals[v.type];
} _locals[v.type] += v.size;
while (inParam != null) { }
inParam.writeToByteArray(data, v.index, v.type, offset); while (inParam != null) {
inParam = inParam.next; inParam.writeToByteArray(data, v.index, v.type, offset);
} inParam = inParam.next;
} }
}
} }
} }
if (output != null) { if (output != null) {
// Output parameters // Output parameters
numParams = output.length; numParams = output.length;
for (j = 0; j < numParams; j++) { for (j = 0; j < numParams; j++) {
param = output[j]; param = output[j];
v = variables[param]; v = variables[param];
if (v == null) { if (v == null) {
if (j == 0 && (i == procedures.length - 1)) { if (j == 0 && (i == procedures.length - 1)) {
// 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) {
throw new Error("Wrong output type:" + VariableType.TYPE_NAMES[v.type]); throw new Error("Wrong output type:" + VariableType.TYPE_NAMES[v.type]);
} }
v.index = _locals[v.type]; v.index = _locals[v.type];
_locals[v.type] += v.size; _locals[v.type] += v.size;
} }
var outParam:Variable = p.variablesUsages[3][j]; var outParam:Variable = p.variablesUsages[3][j];
if (outParam == null) { if (outParam == null) {
throw new Error("Output parameter set, but not exist in code. paramName = " + param + ", register = i" + j.toString()); throw new Error("Output parameter set, but not exist in code. paramName = " + param + ", register = i" + j.toString());
} }
while (outParam != null) { while (outParam != null) {
outParam.writeToByteArray(data, v.index, v.type, offset); outParam.writeToByteArray(data, v.index, v.type, offset);
outParam = outParam.next; outParam = outParam.next;
} }
} }
} }
var vars:Vector.<Variable> = p.variablesUsages[2]; var vars:Vector.<Variable> = p.variablesUsages[2];
for (j = 0; j < vars.length; j++) { for (j = 0; j < vars.length; j++) {
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;
} }
} }
resolveVariablesUsages(data, variables, p.variablesUsages[0], VariableType.ATTRIBUTE, offset); resolveVariablesUsages(data, variables, p.variablesUsages[0], VariableType.ATTRIBUTE, offset);
resolveVariablesUsages(data, variables, p.variablesUsages[1], VariableType.CONSTANT, offset); resolveVariablesUsages(data, variables, p.variablesUsages[1], VariableType.CONSTANT, offset);
resolveVariablesUsages(data, _varyings, p.variablesUsages[4], VariableType.VARYING, offset); resolveVariablesUsages(data, _varyings, p.variablesUsages[4], VariableType.VARYING, offset);
resolveVariablesUsages(data, variables, p.variablesUsages[5], VariableType.SAMPLER, offset); resolveVariablesUsages(data, variables, p.variablesUsages[5], VariableType.SAMPLER, offset);
commandsCount += p.commandsCount; commandsCount += p.commandsCount;
slotsCount += p.slotsCount; slotsCount += p.slotsCount;
} }
} }
private function resolveVariablesUsages(code:ByteArray, variables:Object, variableUsages:Vector.<Variable>, type:uint, offset:int):void { private function resolveVariablesUsages(code:ByteArray, variables:Object, variableUsages:Vector.<Variable>, type:uint, offset:int):void {
for (var j:int = 0; j < variableUsages.length; j++) { for (var j:int = 0; j < variableUsages.length; j++) {
var vUsage:Variable = variableUsages[j]; var vUsage:Variable = variableUsages[j];
if (vUsage == null) continue; if (vUsage == null) continue;
if (vUsage.isRelative) continue; if (vUsage.isRelative) continue;
var variable:Variable = variables[vUsage.name]; var variable:Variable = variables[vUsage.name];
if (variable.index < 0) { if (variable.index < 0) {
variable.index = _locals[type]; variable.index = _locals[type];
_locals[type] += variable.size; _locals[type] += variable.size;
} }
while (vUsage != null) { while (vUsage != null) {
vUsage.writeToByteArray(code, variable.index, variable.type, offset); vUsage.writeToByteArray(code, variable.index, variable.type, offset);
vUsage = vUsage.next; vUsage = vUsage.next;
} }
} }
} }
/** /**
* Returns description of procedures: name, size, input and output parameters. * Returns description of procedures: name, size, input and output parameters.
* @return * @return
*/ */
public function describeLinkageInfo():String { public function describeLinkageInfo():String {
var str:String; var str:String;
var result:String = "LINKER:\n"; var result:String = "LINKER:\n";
var totalCodes:uint = 0; var totalCodes:uint = 0;
var totalCommands:uint = 0; var totalCommands:uint = 0;
for (var i:int = 0; i < procedures.length; i++) { for (var i:int = 0; i < procedures.length; i++) {
var p:Procedure = procedures[i]; var p:Procedure = procedures[i];
if (p.name != null) { if (p.name != null) {
result += p.name + "("; result += p.name + "(";
} else { } else {
result += "#" + i.toString() + "("; result += "#" + i.toString() + "(";
} }
var args:* = _inputParams[p]; var args:* = _inputParams[p];
if (args != null) { if (args != null) {
for each (str in args) { for each (str in args) {
result += str + ","; result += str + ",";
} }
result = result.substr(0, result.length - 1); result = result.substr(0, result.length - 1);
} }
result += ")"; result += ")";
args = _outputParams[p]; args = _outputParams[p];
if (args != null) { if (args != null) {
result += "->("; result += "->(";
for each (str in args) { for each (str in args) {
result += str + ","; result += str + ",";
} }
result = result.substr(0, result.length - 1); result = result.substr(0, result.length - 1);
result += ")"; result += ")";
} }
result += " [IS:" + p.slotsCount.toString() + ", CMDS:" + p.commandsCount.toString() + "]\n"; result += " [IS:" + p.slotsCount.toString() + ", CMDS:" + p.commandsCount.toString() + "]\n";
totalCodes += p.slotsCount; totalCodes += p.slotsCount;
totalCommands += p.commandsCount; totalCommands += p.commandsCount;
} }
result += "[IS:" + totalCodes.toString() + ", CMDS:" + totalCommands.toString() + "]\n"; result += "[IS:" + totalCodes.toString() + ", CMDS:" + totalCommands.toString() + "]\n";
return result; return result;
} }
public function get varyings():Object { public function get varyings():Object {
return _varyings; return _varyings;
} }
public function set varyings(value:Object):void { public function set varyings(value:Object):void {
_varyings = value; _varyings = value;
data = null; data = null;
} }
} }
} }

File diff suppressed because it is too large Load Diff

View File

@@ -1,103 +1,102 @@
/** /**
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
* If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. * If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice.
* You may add additional accurate notices of copyright ownership. * You may add additional accurate notices of copyright ownership.
* *
* 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 var relative : RelativeVariable; public class SourceVariable extends Variable {
public var relative:RelativeVariable;
override public function get size() : uint {
if (relative) { override public function get size():uint {
return 0; if (relative) {
} return 0;
return super.size; }
} 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);
if (isRel) { var isRel:Boolean = relreg.length > 0;
source = source.replace(relreg[0], "0"); if (isRel) {
} else { source = source.replace(relreg[0], "0");
index = parseInt(source.match(/\d+/g)[0], 10); } 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;
if (maskmatch) { var maskmatch:String = swizzle ? swizzle[0]:null;
regmask = 0; if (maskmatch) {
var cv : int; regmask = 0;
var maskLength : uint = maskmatch.length; var cv:int;
for (var i : int = 1; i < maskLength; i++) { var maskLength:uint = maskmatch.length;
cv = maskmatch.charCodeAt(i) - X_CHAR_CODE; for (var i:int = 1; i < maskLength; i++) {
if (cv == -1) cv = 3; cv = maskmatch.charCodeAt(i) - X_CHAR_CODE;
regmask |= cv << ( ( i - 1 ) << 1 ); if (cv == -1) cv = 3;
} regmask |= cv << ( ( i - 1 ) << 1 );
for ( ; i <= 4; i++ ) }
regmask |= cv << ( ( i - 1 ) << 1 ); for ( ; i <= 4; i++ )
// repeat last regmask |= cv << ( ( i - 1 ) << 1 );
} else { // repeat last
regmask = 0xe4; } else {
// id swizzle or mask regmask = 0xe4;
} // id swizzle or mask
lowerCode = (regmask << 24) | index; }
lowerCode = (regmask << 24) | index;
switch(strType) {
case "a": switch(strType) {
type = VariableType.ATTRIBUTE; case "a":
break; type = VariableType.ATTRIBUTE;
case "c": break;
type = VariableType.CONSTANT; case "c":
break; type = VariableType.CONSTANT;
case "t": break;
type = VariableType.TEMPORARY; case "t":
break; type = VariableType.TEMPORARY;
case "o": break;
type = VariableType.OUTPUT; case "o":
break; type = VariableType.OUTPUT;
case "v": break;
type = VariableType.VARYING; case "v":
break; type = VariableType.VARYING;
case "i": break;
type = VariableType.INPUT; case "i":
break; type = VariableType.INPUT;
case "d": break;
type = VariableType.DEPTH; default :
break; throw new ArgumentError('Wrong source register type, must be "a" or "c" or "t" or "o" or "v" or "i", var = ' + source);
default : break;
throw new ArgumentError('Wrong source register type, must be "a" or "c" or "t" or "o" or "v" or "i" or "d", var = ' + source); }
break; upperCode = type;
} if (isRel) {
upperCode = type; relative = new RelativeVariable(relreg[0]);
if (isRel) { lowerCode |= relative.lowerCode;
relative = new RelativeVariable(relreg[0]); upperCode |= relative.upperCode;
lowerCode |= relative.lowerCode; isRelative = true;
upperCode |= relative.upperCode; }
isRelative = true; }
}
} override public function writeToByteArray(byteCode:ByteArray, newIndex:int, newType:int, offset:int = 0):void {
if (relative == null) {
override public function writeToByteArray(byteCode : ByteArray, newIndex : int, newType : int, offset : int = 0) : void { super.writeToByteArray(byteCode, newIndex, newType, offset);
if (relative == null) { } else {
super.writeToByteArray(byteCode, newIndex, newType, offset); byteCode.position = position + 2;
} else { }
byteCode.position = position + 2; byteCode.position = position + offset + 4;
} byteCode.writeByte(newType);
byteCode.position = position + offset + 4; }
byteCode.writeByte(newType);
} }
} }
}