PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/barliesque/agal/EasyAGAL.as

http://easy-agal.googlecode.com/
ActionScript | 266 lines | 116 code | 40 blank | 110 comment | 3 complexity | 6604db5ea1e9e246add509f45cd09b7e MD5 | raw file
  1. package com.barliesque.agal {
  2. /**
  3. * An easy-reading and easy-writing alternative to bare naked AGAL.
  4. * @author David Barlia, david@barliesque.com
  5. *
  6. * This project is licensed under the Apache Open Source License, ver.2
  7. * http://www.apache.org/licenses/LICENSE-2.0.html
  8. *
  9. * Basically, it's yours to code with for all your Molehill needs, on projects
  10. * personal or commercial, so long as the source itself is not sold as your own work.
  11. * If you're overcome with a need to give me credit, I will not be offended, but
  12. * no such act is required. If you come up with improvements, I ask that you
  13. * drop me an update. In any case, this header should stay where it is,
  14. * unalterred, wherever you may wish to spread it.
  15. */
  16. public class EasyAGAL extends EasyBase {
  17. /**
  18. * @param debug Set to true to enable comments to be added to opcode, and opcode trace upon rejection of program upload.
  19. * @param assemblyDebug Set to true for opcode output from AGALMiniAssembler
  20. */
  21. public function EasyAGAL(debug:Boolean = true, assemblyDebug:Boolean = false) {
  22. super(debug, assemblyDebug);
  23. }
  24. /**
  25. * Move: Copy the contents of one register or attribute into another
  26. * @param dest
  27. * @param source
  28. */
  29. static protected function mov(dest:IField, source:IField):void {
  30. Assembler.append("mov " + dest["reg"] + ", " + source["reg"]);
  31. }
  32. /// Add: destination = source1 + source2, componentwise
  33. static protected function add(dest:IField, source1:IField, source2:IField):void {
  34. Assembler.append("add " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  35. }
  36. /// Subtract: destination = source1 - source2, componentwise
  37. static protected function sub(dest:IField, source1:IField, source2:IField):void {
  38. Assembler.append("sub " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  39. }
  40. /// Multiply: destination = source1 * source2, componentwise
  41. static protected function mul(dest:IField, source1:IField, source2:IField):void {
  42. Assembler.append("mul " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  43. }
  44. /// Divide: destination = source1 / source2, componentwise
  45. static protected function div(dest:IField, source1:IField, source2:IField):void {
  46. Assembler.append("div " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  47. }
  48. /// Reciprocal: destination = 1/source1, componentwise
  49. static protected function rcp(dest:IField, source:IField):void {
  50. Assembler.append("rcp " + dest["reg"] + ", " + source["reg"]);
  51. }
  52. /// Minimum: destination = minimum(source1,source2), componentwise
  53. static protected function min(dest:IField, source1:IField, source2:IField):void {
  54. Assembler.append("min " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  55. }
  56. /// Maximum: destination = maximum(source1,source2), componentwise
  57. static protected function max(dest:IField, source1:IField, source2:IField):void {
  58. Assembler.append("max " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  59. }
  60. /// Fractional: destination = source1 - (float)floor(source), componentwise
  61. static protected function frc(dest:IField, source:IField):void {
  62. Assembler.append("frc " + dest["reg"] + ", " + source["reg"]);
  63. }
  64. /// Square Root: destination = sqrt(source), componentwise
  65. static protected function sqt(dest:IField, source:IField):void {
  66. Assembler.append("sqt " + dest["reg"] + ", " + source["reg"]);
  67. }
  68. /// Reciprocal Square Root: destination = 1/sqrt(source), componentwise
  69. static protected function rsq(dest:IField, source:IField):void {
  70. Assembler.append("rsq " + dest["reg"] + ", " + source["reg"]);
  71. }
  72. /// Power: destination = source1 to the power of source2, componentwise
  73. static protected function pow(dest:IField, source1:IField, source2:IField):void {
  74. Assembler.append("pow " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  75. }
  76. /// Logarithm: destination = log_2(source), componentwise
  77. static protected function log(dest:IField, source:IField):void {
  78. Assembler.append("log " + dest["reg"] + ", " + source["reg"]);
  79. }
  80. /// Exponential: destination = 2^source, componentwise
  81. static protected function exp(dest:IField, source:IField):void {
  82. Assembler.append("exp " + dest["reg"] + ", " + source["reg"]);
  83. }
  84. /// Normalize: destination = normalize(source)
  85. static protected function nrm(dest:IField, source:IField):void {
  86. Assembler.append("nrm " + dest["reg"] + ", " + source["reg"]);
  87. }
  88. /// Sine: destination = sin(source), componentwise
  89. static protected function sin(dest:IField, source:IField):void {
  90. Assembler.append("sin " + dest["reg"] + ", " + source["reg"]);
  91. }
  92. /// Cosine: destination = cos(source), componentwise
  93. static protected function cos(dest:IField, source:IField):void {
  94. Assembler.append("cos " + dest["reg"] + ", " + source["reg"]);
  95. }
  96. /// Absolute Value: destination = abs(source), componentwise
  97. static protected function abs(dest:IField, source:IField):void {
  98. Assembler.append("abs " + dest["reg"] + ", " + source["reg"]);
  99. }
  100. /// Negate: destination = -source, componentwise
  101. static protected function neg(dest:IField, source:IField):void {
  102. Assembler.append("neg " + dest["reg"] + ", " + source["reg"]);
  103. }
  104. /// Saturate: destination = max(min(source,1),0), componentwise
  105. /// Clamp the source value to between 0 and 1
  106. static protected function sat(dest:IField, source:IField):void {
  107. Assembler.append("sat " + dest["reg"] + ", " + source["reg"]);
  108. }
  109. /// Set If Greater or Equal
  110. /// destination = (source1 >= source2) ? 1 : 0, componentwise
  111. static protected function sge(dest:IField, source1:IField, source2:IField):void {
  112. Assembler.append("sge " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  113. }
  114. /// Set If Less Than
  115. /// destination = (source1 < source2) ? 1 : 0, componentwise
  116. static protected function slt(dest:IField, source1:IField, source2:IField):void {
  117. Assembler.append("slt " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  118. }
  119. /*
  120. /// Set If Equal
  121. /// destination = (source1 == source2) ? 1 : 0, componentwise
  122. static protected function seq(dest:IField, source1:IField, source2:IField):void {
  123. Assembler.append("seq " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  124. }
  125. /// Set If Not Equal
  126. /// destination = (source1 != source2) ? 1 : 0, componentwise
  127. static protected function sne(dest:IField, source1:IField, source2:IField):void {
  128. Assembler.append("sne " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  129. }
  130. */
  131. /**
  132. * [seq*] *Substitute solution for the unsupported comparison opcode. (Contains 3 instructions)
  133. * destination = (source1 == source2) ? 1 : 0, componentwise
  134. * @param temp A temporary register which will be utilized for this comparison
  135. */
  136. static public function setIf_Equal(dest:IField, source1:IField, source2:IField, temp:IRegister):void {
  137. sge(dest, source1, source2); // Is source1 >= source2?
  138. sge(temp, source2, source1); // Is source2 >= source1?
  139. min(dest, dest, temp); // If both of the above are true, then they must be equal
  140. }
  141. /**
  142. * [sne*] *Substitute solution for the unsupported comparison opcode. (Contains 3 instructions)
  143. * destination = (source1 != source2) ? 1 : 0, componentwise
  144. * @param temp A temporary register which will be utilized for this comparison
  145. */
  146. static public function setIf_NotEqual(dest:IField, source1:IField, source2:IField, temp:IRegister):void {
  147. slt(dest, source1, source2); // Is source1 < source2?
  148. slt(temp, source2, source1); // Is source2 < source1?
  149. max(dest, dest, temp); // If either of the above are true, then they must not be equal
  150. }
  151. /// Cross Product
  152. /// Find the cross product of two 3-component vectors, and store the resulting 3-component vector in destination.
  153. static protected function crs(dest:IField, source1:IField, source2:IField):void {
  154. Assembler.append("crs " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  155. }
  156. /** Dot Product 3
  157. * Find the dot product of two 3-component vectors, and store the result in destination.
  158. * destination = source1.x*source2.x + source1.y*source2.y + source1.z*source2.z
  159. */
  160. static protected function dp3(dest:IField, source1:IField, source2:IField):void {
  161. Assembler.append("dp3 " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  162. }
  163. /** Dot Product 4
  164. * Find the dot product of two 4-component vectors, and store the result in destination.
  165. * destination = source1.x*source2.x + source1.y*source2.y + source1.z*source2.z + source1.w*source2.w
  166. */
  167. static protected function dp4(dest:IField, source1:IField, source2:IField):void {
  168. Assembler.append("dp4 " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  169. }
  170. /**
  171. * Matrix multiply a 3-component vector with a 3x3 matrix, and store the resulting vector in destination.
  172. * @param dest Destination register
  173. * @param source1 The vector to be multiplied
  174. * @param source2 The first of three consecutive registers, forming a 3x3 matrix
  175. */
  176. static protected function m33(dest:IField, source1:IField, source2:IField):void {
  177. Assembler.append("m33 " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  178. }
  179. /**
  180. * Matrix multiply a 4-component vector with a 4x4 matrix, and store the resulting vector in destination.
  181. * @param dest Destination register
  182. * @param source1 The vector to be multiplied
  183. * @param source2 The first of four consecutive registers, forming a 4x4 matrix
  184. */
  185. static protected function m44(dest:IField, source1:IField, source2:IField):void {
  186. Assembler.append("m44 " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  187. }
  188. /**
  189. * Matrix multiply a 4-component vector with a 3x4 matrix, and store the resulting vector in destination.
  190. * @param dest Destination register
  191. * @param source1 The vector to be multiplied
  192. * @param source2 The first of three consecutive registers, forming a 3x4 matrix
  193. */
  194. static protected function m34(dest:IField, source1:IField, source2:IField):void {
  195. Assembler.append("m34 " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"]);
  196. }
  197. /**
  198. * If any component of the source is less than zero, the fragment is discarded and not drawn to the frame buffer.
  199. * @param dest The destination register must be all 0.
  200. * @param source A color value.
  201. */
  202. static protected function kil(dest:IField, source:IField):void {
  203. Assembler.append("kil " + dest["reg"] + ", " + source["reg"]);
  204. }
  205. /**
  206. * Sample Texture: Get an interpolated pixel color value
  207. * @param dest Destination register will be set to a color value
  208. * @param source1 Coordinates of the pixel to sample
  209. * @param source2 A Texture Sampler register that is linked to the texture to be sampled
  210. * @param flags Refer to the TextureFlag class
  211. */
  212. static protected function tex(dest:IField, source1:IField, source2:ISampler, flags:Array = null):void {
  213. if (Assembler.assemblingVertex) throw new Error("sampleTexture() is only available in vertex shaders.");
  214. var code:String = "tex " + dest["reg"] + ", " + source1["reg"] + ", " + source2["reg"];
  215. if (flags) {
  216. code += " <";
  217. for each (var flag:String in flags) {
  218. code += flag + ",";
  219. }
  220. code = code.substr(0, code.length - 1) + ">";
  221. }
  222. Assembler.append(code);
  223. }
  224. }
  225. }