/starling/src/com/adobe/utils/AGALMiniAssembler.as

https://github.com/ltblue/Starling-Framework · ActionScript · 793 lines · 587 code · 81 blank · 125 comment · 90 complexity · d20b27d6f4039ea43dd5af8726de6f27 MD5 · raw file

  1. /*
  2. Copyright (c) 2011, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.adobe.utils
  28. {
  29. // ===========================================================================
  30. // Imports
  31. // ---------------------------------------------------------------------------
  32. import flash.display3D.*;
  33. import flash.utils.*;
  34. // ===========================================================================
  35. // Class
  36. // ---------------------------------------------------------------------------
  37. public class AGALMiniAssembler
  38. { // ======================================================================
  39. // Constants
  40. // ----------------------------------------------------------------------
  41. protected static const REGEXP_OUTER_SPACES:RegExp = /^\s+|\s+$/g;
  42. // ======================================================================
  43. // Properties
  44. // ----------------------------------------------------------------------
  45. // AGAL bytes and error buffer
  46. private var _agalcode:ByteArray = null;
  47. private var _error:String = "";
  48. private var debugEnabled:Boolean = false;
  49. private static var initialized:Boolean = false;
  50. public var verbose:Boolean = false;
  51. // ======================================================================
  52. // Getters
  53. // ----------------------------------------------------------------------
  54. public function get error():String { return _error; }
  55. public function get agalcode():ByteArray { return _agalcode; }
  56. // ======================================================================
  57. // Constructor
  58. // ----------------------------------------------------------------------
  59. public function AGALMiniAssembler( debugging:Boolean = false ):void
  60. {
  61. debugEnabled = debugging;
  62. if ( !initialized )
  63. init();
  64. }
  65. // ======================================================================
  66. // Methods
  67. // ----------------------------------------------------------------------
  68. public function assemble2( ctx3d : Context3D, version:uint, vertexsrc:String, fragmentsrc:String ) : Program3D
  69. {
  70. var agalvertex : ByteArray = assemble ( VERTEX, vertexsrc, version );
  71. var agalfragment : ByteArray = assemble ( FRAGMENT, fragmentsrc, version );
  72. var prog : Program3D = ctx3d.createProgram();
  73. prog.upload(agalvertex,agalfragment);
  74. return prog;
  75. }
  76. public function assemble( mode:String, source:String, version:uint=1, ignorelimits:Boolean=false ):ByteArray
  77. {
  78. var start:uint = getTimer();
  79. _agalcode = new ByteArray();
  80. _error = "";
  81. var isFrag:Boolean = false;
  82. if ( mode == FRAGMENT )
  83. isFrag = true;
  84. else if ( mode != VERTEX )
  85. _error = 'ERROR: mode needs to be "' + FRAGMENT + '" or "' + VERTEX + '" but is "' + mode + '".';
  86. agalcode.endian = Endian.LITTLE_ENDIAN;
  87. agalcode.writeByte( 0xa0 ); // tag version
  88. agalcode.writeUnsignedInt( version ); // AGAL version, big endian, bit pattern will be 0x01000000
  89. agalcode.writeByte( 0xa1 ); // tag program id
  90. agalcode.writeByte( isFrag ? 1 : 0 ); // vertex or fragment
  91. initregmap(version, ignorelimits);
  92. var lines:Array = source.replace( /[\f\n\r\v]+/g, "\n" ).split( "\n" );
  93. var nest:int = 0;
  94. var nops:int = 0;
  95. var i:int;
  96. var lng:int = lines.length;
  97. for ( i = 0; i < lng && _error == ""; i++ )
  98. {
  99. var line:String = new String( lines[i] );
  100. line = line.replace( REGEXP_OUTER_SPACES, "" );
  101. // remove comments
  102. var startcomment:int = line.search( "//" );
  103. if ( startcomment != -1 )
  104. line = line.slice( 0, startcomment );
  105. // grab options
  106. var optsi:int = line.search( /<.*>/g );
  107. var opts:Array;
  108. if ( optsi != -1 )
  109. {
  110. opts = line.slice( optsi ).match( /([\w\.\-\+]+)/gi );
  111. line = line.slice( 0, optsi );
  112. }
  113. // find opcode
  114. var opCode:Array = line.match( /^\w{3}/ig );
  115. if ( !opCode )
  116. {
  117. if ( line.length >= 3 )
  118. trace( "warning: bad line "+i+": "+lines[i] );
  119. continue;
  120. }
  121. var opFound:OpCode = OPMAP[ opCode[0] ];
  122. // if debug is enabled, output the opcodes
  123. if ( debugEnabled )
  124. trace( opFound );
  125. if ( opFound == null )
  126. {
  127. if ( line.length >= 3 )
  128. trace( "warning: bad line "+i+": "+lines[i] );
  129. continue;
  130. }
  131. line = line.slice( line.search( opFound.name ) + opFound.name.length );
  132. if ( ( opFound.flags & OP_VERSION2 ) && version<2 )
  133. {
  134. _error = "error: opcode requires version 2.";
  135. break;
  136. }
  137. if ( ( opFound.flags & OP_VERT_ONLY ) && isFrag )
  138. {
  139. _error = "error: opcode is only allowed in vertex programs.";
  140. break;
  141. }
  142. if ( ( opFound.flags & OP_FRAG_ONLY ) && !isFrag )
  143. {
  144. _error = "error: opcode is only allowed in fragment programs.";
  145. break;
  146. }
  147. if ( verbose )
  148. trace( "emit opcode=" + opFound );
  149. agalcode.writeUnsignedInt( opFound.emitCode );
  150. nops++;
  151. if ( nops > MAX_OPCODES )
  152. {
  153. _error = "error: too many opcodes. maximum is "+MAX_OPCODES+".";
  154. break;
  155. }
  156. // get operands, use regexp
  157. var regs:Array;
  158. // will match both syntax
  159. regs = line.match( /vc\[([vof][acostdip]?)(\d*)?(\.[xyzw](\+\d{1,3})?)?\](\.[xyzw]{1,4})?|([vof][acostdip]?)(\d*)?(\.[xyzw]{1,4})?/gi );
  160. if ( !regs || regs.length != opFound.numRegister )
  161. {
  162. _error = "error: wrong number of operands. found "+regs.length+" but expected "+opFound.numRegister+".";
  163. break;
  164. }
  165. var badreg:Boolean = false;
  166. var pad:uint = 64 + 64 + 32;
  167. var regLength:uint = regs.length;
  168. for ( var j:int = 0; j < regLength; j++ )
  169. {
  170. var isRelative:Boolean = false;
  171. var relreg:Array = regs[ j ].match( /\[.*\]/ig );
  172. if ( relreg && relreg.length > 0 )
  173. {
  174. regs[ j ] = regs[ j ].replace( relreg[ 0 ], "0" );
  175. if ( verbose )
  176. trace( "IS REL" );
  177. isRelative = true;
  178. }
  179. var res:Array = regs[j].match( /^\b[A-Za-z]{1,2}/ig );
  180. if ( !res )
  181. {
  182. _error = "error: could not parse operand "+j+" ("+regs[j]+").";
  183. badreg = true;
  184. break;
  185. }
  186. var regFound:Register = REGMAP[ res[ 0 ] ];
  187. // if debug is enabled, output the registers
  188. if ( debugEnabled )
  189. trace( regFound );
  190. if ( regFound == null )
  191. {
  192. _error = "error: could not find register name for operand "+j+" ("+regs[j]+").";
  193. badreg = true;
  194. break;
  195. }
  196. if ( isFrag )
  197. {
  198. if ( !( regFound.flags & REG_FRAG ) )
  199. {
  200. _error = "error: register operand "+j+" ("+regs[j]+") only allowed in vertex programs.";
  201. badreg = true;
  202. break;
  203. }
  204. if ( isRelative )
  205. {
  206. _error = "error: register operand "+j+" ("+regs[j]+") relative adressing not allowed in fragment programs.";
  207. badreg = true;
  208. break;
  209. }
  210. }
  211. else
  212. {
  213. if ( !( regFound.flags & REG_VERT ) )
  214. {
  215. _error = "error: register operand "+j+" ("+regs[j]+") only allowed in fragment programs.";
  216. badreg = true;
  217. break;
  218. }
  219. }
  220. regs[j] = regs[j].slice( regs[j].search( regFound.name ) + regFound.name.length );
  221. //trace( "REGNUM: " +regs[j] );
  222. var idxmatch:Array = isRelative ? relreg[0].match( /\d+/ ) : regs[j].match( /\d+/ );
  223. var regidx:uint = 0;
  224. if ( idxmatch )
  225. regidx = uint( idxmatch[0] );
  226. if ( regFound.range < regidx )
  227. {
  228. _error = "error: register operand "+j+" ("+regs[j]+") index exceeds limit of "+(regFound.range+1)+".";
  229. badreg = true;
  230. break;
  231. }
  232. var regmask:uint = 0;
  233. var maskmatch:Array = regs[j].match( /(\.[xyzw]{1,4})/ );
  234. var isDest:Boolean = ( j == 0 && !( opFound.flags & OP_NO_DEST ) );
  235. var isSampler:Boolean = ( j == 2 && ( opFound.flags & OP_SPECIAL_TEX ) );
  236. var reltype:uint = 0;
  237. var relsel:uint = 0;
  238. var reloffset:int = 0;
  239. if ( isDest && isRelative )
  240. {
  241. _error = "error: relative can not be destination";
  242. badreg = true;
  243. break;
  244. }
  245. if ( maskmatch )
  246. {
  247. regmask = 0;
  248. var cv:uint;
  249. var maskLength:uint = maskmatch[0].length;
  250. for ( var k:int = 1; k < maskLength; k++ )
  251. {
  252. cv = maskmatch[0].charCodeAt(k) - "x".charCodeAt(0);
  253. if ( cv > 2 )
  254. cv = 3;
  255. if ( isDest )
  256. regmask |= 1 << cv;
  257. else
  258. regmask |= cv << ( ( k - 1 ) << 1 );
  259. }
  260. if ( !isDest )
  261. for ( ; k <= 4; k++ )
  262. regmask |= cv << ( ( k - 1 ) << 1 ); // repeat last
  263. }
  264. else
  265. {
  266. regmask = isDest ? 0xf : 0xe4; // id swizzle or mask
  267. }
  268. if ( isRelative )
  269. {
  270. var relname:Array = relreg[0].match( /[A-Za-z]{1,2}/ig );
  271. var regFoundRel:Register = REGMAP[ relname[0]];
  272. if ( regFoundRel == null )
  273. {
  274. _error = "error: bad index register";
  275. badreg = true;
  276. break;
  277. }
  278. reltype = regFoundRel.emitCode;
  279. var selmatch:Array = relreg[0].match( /(\.[xyzw]{1,1})/ );
  280. if ( selmatch.length==0 )
  281. {
  282. _error = "error: bad index register select";
  283. badreg = true;
  284. break;
  285. }
  286. relsel = selmatch[0].charCodeAt(1) - "x".charCodeAt(0);
  287. if ( relsel > 2 )
  288. relsel = 3;
  289. var relofs:Array = relreg[0].match( /\+\d{1,3}/ig );
  290. if ( relofs.length > 0 )
  291. reloffset = relofs[0];
  292. if ( reloffset < 0 || reloffset > 255 )
  293. {
  294. _error = "error: index offset "+reloffset+" out of bounds. [0..255]";
  295. badreg = true;
  296. break;
  297. }
  298. if ( verbose )
  299. trace( "RELATIVE: type="+reltype+"=="+relname[0]+" sel="+relsel+"=="+selmatch[0]+" idx="+regidx+" offset="+reloffset );
  300. }
  301. if ( verbose )
  302. trace( " emit argcode="+regFound+"["+regidx+"]["+regmask+"]" );
  303. if ( isDest )
  304. {
  305. agalcode.writeShort( regidx );
  306. agalcode.writeByte( regmask );
  307. agalcode.writeByte( regFound.emitCode );
  308. pad -= 32;
  309. } else
  310. {
  311. if ( isSampler )
  312. {
  313. if ( verbose )
  314. trace( " emit sampler" );
  315. var samplerbits:uint = 5; // type 5
  316. var optsLength:uint = opts == null ? 0 : opts.length;
  317. var bias:Number = 0;
  318. for ( k = 0; k<optsLength; k++ )
  319. {
  320. if ( verbose )
  321. trace( " opt: "+opts[k] );
  322. var optfound:Sampler = SAMPLEMAP [opts[k]];
  323. if ( optfound == null )
  324. {
  325. // todo check that it's a number...
  326. //trace( "Warning, unknown sampler option: "+opts[k] );
  327. bias = Number(opts[k]);
  328. if ( verbose )
  329. trace( " bias: " + bias );
  330. }
  331. else
  332. {
  333. if ( optfound.flag != SAMPLER_SPECIAL_SHIFT )
  334. samplerbits &= ~( 0xf << optfound.flag );
  335. samplerbits |= uint( optfound.mask ) << uint( optfound.flag );
  336. }
  337. }
  338. agalcode.writeShort( regidx );
  339. agalcode.writeByte(int(bias*8.0));
  340. agalcode.writeByte(0);
  341. agalcode.writeUnsignedInt( samplerbits );
  342. if ( verbose )
  343. trace( " bits: " + ( samplerbits - 5 ) );
  344. pad -= 64;
  345. }
  346. else
  347. {
  348. if ( j == 0 )
  349. {
  350. agalcode.writeUnsignedInt( 0 );
  351. pad -= 32;
  352. }
  353. agalcode.writeShort( regidx );
  354. agalcode.writeByte( reloffset );
  355. agalcode.writeByte( regmask );
  356. agalcode.writeByte( regFound.emitCode );
  357. agalcode.writeByte( reltype );
  358. agalcode.writeShort( isRelative ? ( relsel | ( 1 << 15 ) ) : 0 );
  359. pad -= 64;
  360. }
  361. }
  362. }
  363. // pad unused regs
  364. for ( j = 0; j < pad; j += 8 )
  365. agalcode.writeByte( 0 );
  366. if ( badreg )
  367. break;
  368. }
  369. if ( _error != "" )
  370. {
  371. _error += "\n at line " + i + " " + lines[i];
  372. agalcode.length = 0;
  373. trace( _error );
  374. }
  375. // trace the bytecode bytes if debugging is enabled
  376. if ( debugEnabled )
  377. {
  378. var dbgLine:String = "generated bytecode:";
  379. var agalLength:uint = agalcode.length;
  380. for ( var index:uint = 0; index < agalLength; index++ )
  381. {
  382. if ( !( index % 16 ) )
  383. dbgLine += "\n";
  384. if ( !( index % 4 ) )
  385. dbgLine += " ";
  386. var byteStr:String = agalcode[ index ].toString( 16 );
  387. if ( byteStr.length < 2 )
  388. byteStr = "0" + byteStr;
  389. dbgLine += byteStr;
  390. }
  391. trace( dbgLine );
  392. }
  393. if ( verbose )
  394. trace( "AGALMiniAssembler.assemble time: " + ( ( getTimer() - start ) / 1000 ) + "s" );
  395. return agalcode;
  396. }
  397. private function initregmap ( version:uint, ignorelimits:Boolean ) : void {
  398. // version changes limits
  399. REGMAP[ VA ] = new Register( VA, "vertex attribute", 0x0, ignorelimits?1024:7, REG_VERT | REG_READ );
  400. REGMAP[ VC ] = new Register( VC, "vertex constant", 0x1, ignorelimits?1024:(version==1?127:250), REG_VERT | REG_READ );
  401. REGMAP[ VT ] = new Register( VT, "vertex temporary", 0x2, ignorelimits?1024:(version==1?7:27), REG_VERT | REG_WRITE | REG_READ );
  402. REGMAP[ VO ] = new Register( VO, "vertex output", 0x3, ignorelimits?1024:0, REG_VERT | REG_WRITE );
  403. REGMAP[ VI ] = new Register( VI, "varying", 0x4, ignorelimits?1024:(version==1?7:11), REG_VERT | REG_FRAG | REG_READ | REG_WRITE );
  404. REGMAP[ FC ] = new Register( FC, "fragment constant", 0x1, ignorelimits?1024:(version==1?27:63), REG_FRAG | REG_READ );
  405. REGMAP[ FT ] = new Register( FT, "fragment temporary", 0x2, ignorelimits?1024:(version==1?7:27), REG_FRAG | REG_WRITE | REG_READ );
  406. REGMAP[ FS ] = new Register( FS, "texture sampler", 0x5, ignorelimits?1024:7, REG_FRAG | REG_READ );
  407. REGMAP[ FO ] = new Register( FO, "fragment output", 0x3, ignorelimits?1024:(version==1?0:3), REG_FRAG | REG_WRITE );
  408. REGMAP[ FD ] = new Register( FD, "fragment depth output",0x6, ignorelimits?1024:(version==1?-1:0), REG_FRAG | REG_WRITE );
  409. // aliases
  410. REGMAP[ "op" ] = REGMAP[ VO ];
  411. REGMAP[ "i" ] = REGMAP[ VI ];
  412. REGMAP[ "v" ] = REGMAP[ VI ];
  413. REGMAP[ "oc" ] = REGMAP[ FO ];
  414. REGMAP[ "od" ] = REGMAP[ FD ];
  415. REGMAP[ "fi" ] = REGMAP[ VI ];
  416. }
  417. static private function init():void
  418. {
  419. initialized = true;
  420. // Fill the dictionaries with opcodes and registers
  421. OPMAP[ MOV ] = new OpCode( MOV, 2, 0x00, 0 );
  422. OPMAP[ ADD ] = new OpCode( ADD, 3, 0x01, 0 );
  423. OPMAP[ SUB ] = new OpCode( SUB, 3, 0x02, 0 );
  424. OPMAP[ MUL ] = new OpCode( MUL, 3, 0x03, 0 );
  425. OPMAP[ DIV ] = new OpCode( DIV, 3, 0x04, 0 );
  426. OPMAP[ RCP ] = new OpCode( RCP, 2, 0x05, 0 );
  427. OPMAP[ MIN ] = new OpCode( MIN, 3, 0x06, 0 );
  428. OPMAP[ MAX ] = new OpCode( MAX, 3, 0x07, 0 );
  429. OPMAP[ FRC ] = new OpCode( FRC, 2, 0x08, 0 );
  430. OPMAP[ SQT ] = new OpCode( SQT, 2, 0x09, 0 );
  431. OPMAP[ RSQ ] = new OpCode( RSQ, 2, 0x0a, 0 );
  432. OPMAP[ POW ] = new OpCode( POW, 3, 0x0b, 0 );
  433. OPMAP[ LOG ] = new OpCode( LOG, 2, 0x0c, 0 );
  434. OPMAP[ EXP ] = new OpCode( EXP, 2, 0x0d, 0 );
  435. OPMAP[ NRM ] = new OpCode( NRM, 2, 0x0e, 0 );
  436. OPMAP[ SIN ] = new OpCode( SIN, 2, 0x0f, 0 );
  437. OPMAP[ COS ] = new OpCode( COS, 2, 0x10, 0 );
  438. OPMAP[ CRS ] = new OpCode( CRS, 3, 0x11, 0 );
  439. OPMAP[ DP3 ] = new OpCode( DP3, 3, 0x12, 0 );
  440. OPMAP[ DP4 ] = new OpCode( DP4, 3, 0x13, 0 );
  441. OPMAP[ ABS ] = new OpCode( ABS, 2, 0x14, 0 );
  442. OPMAP[ NEG ] = new OpCode( NEG, 2, 0x15, 0 );
  443. OPMAP[ SAT ] = new OpCode( SAT, 2, 0x16, 0 );
  444. OPMAP[ M33 ] = new OpCode( M33, 3, 0x17, OP_SPECIAL_MATRIX );
  445. OPMAP[ M44 ] = new OpCode( M44, 3, 0x18, OP_SPECIAL_MATRIX );
  446. OPMAP[ M34 ] = new OpCode( M34, 3, 0x19, OP_SPECIAL_MATRIX );
  447. OPMAP[ DDX ] = new OpCode( DDX, 2, 0x1a, OP_VERSION2 | OP_FRAG_ONLY );
  448. OPMAP[ DDY ] = new OpCode( DDY, 2, 0x1b, OP_VERSION2 | OP_FRAG_ONLY );
  449. OPMAP[ IFE ] = new OpCode( IFE, 2, 0x1c, OP_NO_DEST | OP_VERSION2 | OP_INCNEST | OP_SCALAR );
  450. OPMAP[ INE ] = new OpCode( INE, 2, 0x1d, OP_NO_DEST | OP_VERSION2 | OP_INCNEST | OP_SCALAR );
  451. OPMAP[ IFG ] = new OpCode( IFG, 2, 0x1e, OP_NO_DEST | OP_VERSION2 | OP_INCNEST | OP_SCALAR );
  452. OPMAP[ IFL ] = new OpCode( IFL, 2, 0x1f, OP_NO_DEST | OP_VERSION2 | OP_INCNEST | OP_SCALAR );
  453. OPMAP[ ELS ] = new OpCode( ELS, 0, 0x20, OP_NO_DEST | OP_VERSION2 | OP_INCNEST | OP_DECNEST | OP_SCALAR );
  454. OPMAP[ EIF ] = new OpCode( EIF, 0, 0x21, OP_NO_DEST | OP_VERSION2 | OP_DECNEST | OP_SCALAR );
  455. // space
  456. OPMAP[ TED ] = new OpCode( TED, 3, 0x26, OP_FRAG_ONLY | OP_SPECIAL_TEX | OP_VERSION2);
  457. OPMAP[ KIL ] = new OpCode( KIL, 1, 0x27, OP_NO_DEST | OP_FRAG_ONLY );
  458. OPMAP[ TEX ] = new OpCode( TEX, 3, 0x28, OP_FRAG_ONLY | OP_SPECIAL_TEX );
  459. OPMAP[ SGE ] = new OpCode( SGE, 3, 0x29, 0 );
  460. OPMAP[ SLT ] = new OpCode( SLT, 3, 0x2a, 0 );
  461. OPMAP[ SGN ] = new OpCode( SGN, 2, 0x2b, 0 );
  462. OPMAP[ SEQ ] = new OpCode( SEQ, 3, 0x2c, 0 );
  463. OPMAP[ SNE ] = new OpCode( SNE, 3, 0x2d, 0 );
  464. SAMPLEMAP[ RGBA ] = new Sampler( RGBA, SAMPLER_TYPE_SHIFT, 0 );
  465. SAMPLEMAP[ DXT1 ] = new Sampler( DXT1, SAMPLER_TYPE_SHIFT, 1 );
  466. SAMPLEMAP[ DXT5 ] = new Sampler( DXT5, SAMPLER_TYPE_SHIFT, 2 );
  467. SAMPLEMAP[ VIDEO ] = new Sampler( VIDEO, SAMPLER_TYPE_SHIFT, 3 );
  468. SAMPLEMAP[ D2 ] = new Sampler( D2, SAMPLER_DIM_SHIFT, 0 );
  469. SAMPLEMAP[ D3 ] = new Sampler( D3, SAMPLER_DIM_SHIFT, 2 );
  470. SAMPLEMAP[ CUBE ] = new Sampler( CUBE, SAMPLER_DIM_SHIFT, 1 );
  471. SAMPLEMAP[ MIPNEAREST ] = new Sampler( MIPNEAREST, SAMPLER_MIPMAP_SHIFT, 1 );
  472. SAMPLEMAP[ MIPLINEAR ] = new Sampler( MIPLINEAR, SAMPLER_MIPMAP_SHIFT, 2 );
  473. SAMPLEMAP[ MIPNONE ] = new Sampler( MIPNONE, SAMPLER_MIPMAP_SHIFT, 0 );
  474. SAMPLEMAP[ NOMIP ] = new Sampler( NOMIP, SAMPLER_MIPMAP_SHIFT, 0 );
  475. SAMPLEMAP[ NEAREST ] = new Sampler( NEAREST, SAMPLER_FILTER_SHIFT, 0 );
  476. SAMPLEMAP[ LINEAR ] = new Sampler( LINEAR, SAMPLER_FILTER_SHIFT, 1 );
  477. SAMPLEMAP[ CENTROID ] = new Sampler( CENTROID, SAMPLER_SPECIAL_SHIFT, 1 << 0 );
  478. SAMPLEMAP[ SINGLE ] = new Sampler( SINGLE, SAMPLER_SPECIAL_SHIFT, 1 << 1 );
  479. SAMPLEMAP[ IGNORESAMPLER ] = new Sampler( IGNORESAMPLER, SAMPLER_SPECIAL_SHIFT, 1 << 2 );
  480. SAMPLEMAP[ REPEAT ] = new Sampler( REPEAT, SAMPLER_REPEAT_SHIFT, 1 );
  481. SAMPLEMAP[ WRAP ] = new Sampler( WRAP, SAMPLER_REPEAT_SHIFT, 1 );
  482. SAMPLEMAP[ CLAMP ] = new Sampler( CLAMP, SAMPLER_REPEAT_SHIFT, 0 );
  483. }
  484. // ======================================================================
  485. // Constants
  486. // ----------------------------------------------------------------------
  487. private static const OPMAP:Dictionary = new Dictionary();
  488. private static const REGMAP:Dictionary = new Dictionary();
  489. private static const SAMPLEMAP:Dictionary = new Dictionary();
  490. private static const MAX_NESTING:int = 4;
  491. private static const MAX_OPCODES:int = 2048;
  492. private static const FRAGMENT:String = "fragment";
  493. private static const VERTEX:String = "vertex";
  494. // masks and shifts
  495. private static const SAMPLER_TYPE_SHIFT:uint = 8;
  496. private static const SAMPLER_DIM_SHIFT:uint = 12;
  497. private static const SAMPLER_SPECIAL_SHIFT:uint = 16;
  498. private static const SAMPLER_REPEAT_SHIFT:uint = 20;
  499. private static const SAMPLER_MIPMAP_SHIFT:uint = 24;
  500. private static const SAMPLER_FILTER_SHIFT:uint = 28;
  501. // regmap flags
  502. private static const REG_WRITE:uint = 0x1;
  503. private static const REG_READ:uint = 0x2;
  504. private static const REG_FRAG:uint = 0x20;
  505. private static const REG_VERT:uint = 0x40;
  506. // opmap flags
  507. private static const OP_SCALAR:uint = 0x1;
  508. private static const OP_SPECIAL_TEX:uint = 0x8;
  509. private static const OP_SPECIAL_MATRIX:uint = 0x10;
  510. private static const OP_FRAG_ONLY:uint = 0x20;
  511. private static const OP_VERT_ONLY:uint = 0x40;
  512. private static const OP_NO_DEST:uint = 0x80;
  513. private static const OP_VERSION2:uint = 0x100;
  514. private static const OP_INCNEST:uint = 0x200;
  515. private static const OP_DECNEST:uint = 0x400;
  516. // opcodes
  517. private static const MOV:String = "mov";
  518. private static const ADD:String = "add";
  519. private static const SUB:String = "sub";
  520. private static const MUL:String = "mul";
  521. private static const DIV:String = "div";
  522. private static const RCP:String = "rcp";
  523. private static const MIN:String = "min";
  524. private static const MAX:String = "max";
  525. private static const FRC:String = "frc";
  526. private static const SQT:String = "sqt";
  527. private static const RSQ:String = "rsq";
  528. private static const POW:String = "pow";
  529. private static const LOG:String = "log";
  530. private static const EXP:String = "exp";
  531. private static const NRM:String = "nrm";
  532. private static const SIN:String = "sin";
  533. private static const COS:String = "cos";
  534. private static const CRS:String = "crs";
  535. private static const DP3:String = "dp3";
  536. private static const DP4:String = "dp4";
  537. private static const ABS:String = "abs";
  538. private static const NEG:String = "neg";
  539. private static const SAT:String = "sat";
  540. private static const M33:String = "m33";
  541. private static const M44:String = "m44";
  542. private static const M34:String = "m34";
  543. private static const DDX:String = "ddx";
  544. private static const DDY:String = "ddy";
  545. private static const IFE:String = "ife";
  546. private static const INE:String = "ine";
  547. private static const IFG:String = "ifg";
  548. private static const IFL:String = "ifl";
  549. private static const ELS:String = "els";
  550. private static const EIF:String = "eif";
  551. private static const TED:String = "ted";
  552. private static const KIL:String = "kil";
  553. private static const TEX:String = "tex";
  554. private static const SGE:String = "sge";
  555. private static const SLT:String = "slt";
  556. private static const SGN:String = "sgn";
  557. private static const SEQ:String = "seq";
  558. private static const SNE:String = "sne";
  559. // registers
  560. private static const VA:String = "va";
  561. private static const VC:String = "vc";
  562. private static const VT:String = "vt";
  563. private static const VO:String = "vo";
  564. private static const VI:String = "vi";
  565. private static const FC:String = "fc";
  566. private static const FT:String = "ft";
  567. private static const FS:String = "fs";
  568. private static const FO:String = "fo";
  569. private static const FD:String = "fd";
  570. // samplers
  571. private static const D2:String = "2d";
  572. private static const D3:String = "3d";
  573. private static const CUBE:String = "cube";
  574. private static const MIPNEAREST:String = "mipnearest";
  575. private static const MIPLINEAR:String = "miplinear";
  576. private static const MIPNONE:String = "mipnone";
  577. private static const NOMIP:String = "nomip";
  578. private static const NEAREST:String = "nearest";
  579. private static const LINEAR:String = "linear";
  580. private static const CENTROID:String = "centroid";
  581. private static const SINGLE:String = "single";
  582. private static const IGNORESAMPLER:String = "ignoresampler";
  583. private static const REPEAT:String = "repeat";
  584. private static const WRAP:String = "wrap";
  585. private static const CLAMP:String = "clamp";
  586. private static const RGBA:String = "rgba";
  587. private static const DXT1:String = "dxt1";
  588. private static const DXT5:String = "dxt5";
  589. private static const VIDEO:String = "video";
  590. }
  591. }
  592. // ================================================================================
  593. // Helper Classes
  594. // --------------------------------------------------------------------------------
  595. {
  596. // ===========================================================================
  597. // Class
  598. // ---------------------------------------------------------------------------
  599. class OpCode
  600. {
  601. // ======================================================================
  602. // Properties
  603. // ----------------------------------------------------------------------
  604. private var _emitCode:uint;
  605. private var _flags:uint;
  606. private var _name:String;
  607. private var _numRegister:uint;
  608. // ======================================================================
  609. // Getters
  610. // ----------------------------------------------------------------------
  611. public function get emitCode():uint { return _emitCode; }
  612. public function get flags():uint { return _flags; }
  613. public function get name():String { return _name; }
  614. public function get numRegister():uint { return _numRegister; }
  615. // ======================================================================
  616. // Constructor
  617. // ----------------------------------------------------------------------
  618. public function OpCode( name:String, numRegister:uint, emitCode:uint, flags:uint)
  619. {
  620. _name = name;
  621. _numRegister = numRegister;
  622. _emitCode = emitCode;
  623. _flags = flags;
  624. }
  625. // ======================================================================
  626. // Methods
  627. // ----------------------------------------------------------------------
  628. public function toString():String
  629. {
  630. return "[OpCode name=\""+_name+"\", numRegister="+_numRegister+", emitCode="+_emitCode+", flags="+_flags+"]";
  631. }
  632. }
  633. // ===========================================================================
  634. // Class
  635. // ---------------------------------------------------------------------------
  636. class Register
  637. {
  638. // ======================================================================
  639. // Properties
  640. // ----------------------------------------------------------------------
  641. private var _emitCode:uint;
  642. private var _name:String;
  643. private var _longName:String;
  644. private var _flags:uint;
  645. private var _range:uint;
  646. // ======================================================================
  647. // Getters
  648. // ----------------------------------------------------------------------
  649. public function get emitCode():uint { return _emitCode; }
  650. public function get longName():String { return _longName; }
  651. public function get name():String { return _name; }
  652. public function get flags():uint { return _flags; }
  653. public function get range():uint { return _range; }
  654. // ======================================================================
  655. // Constructor
  656. // ----------------------------------------------------------------------
  657. public function Register( name:String, longName:String, emitCode:uint, range:uint, flags:uint)
  658. {
  659. _name = name;
  660. _longName = longName;
  661. _emitCode = emitCode;
  662. _range = range;
  663. _flags = flags;
  664. }
  665. // ======================================================================
  666. // Methods
  667. // ----------------------------------------------------------------------
  668. public function toString():String
  669. {
  670. return "[Register name=\""+_name+"\", longName=\""+_longName+"\", emitCode="+_emitCode+", range="+_range+", flags="+ _flags+"]";
  671. }
  672. }
  673. // ===========================================================================
  674. // Class
  675. // ---------------------------------------------------------------------------
  676. class Sampler
  677. {
  678. // ======================================================================
  679. // Properties
  680. // ----------------------------------------------------------------------
  681. private var _flag:uint;
  682. private var _mask:uint;
  683. private var _name:String;
  684. // ======================================================================
  685. // Getters
  686. // ----------------------------------------------------------------------
  687. public function get flag():uint { return _flag; }
  688. public function get mask():uint { return _mask; }
  689. public function get name():String { return _name; }
  690. // ======================================================================
  691. // Constructor
  692. // ----------------------------------------------------------------------
  693. public function Sampler( name:String, flag:uint, mask:uint )
  694. {
  695. _name = name;
  696. _flag = flag;
  697. _mask = mask;
  698. }
  699. // ======================================================================
  700. // Methods
  701. // ----------------------------------------------------------------------
  702. public function toString():String
  703. {
  704. return "[Sampler name=\""+_name+"\", flag=\""+_flag+"\", mask="+mask+"]";
  705. }
  706. }
  707. }