本文并没有打算事无巨细的介绍一遍AGAL,仅仅是对现有文档的一些理解及汇总,因此请先阅读相参考文档html
参考资料spa
建议去看英文,中文翻译的很烂,要是想看英文,请在下方将语言换为英语,不然仍旧自动跳转回中文翻译
如文档中所说,AGAL(Adobe Graphics Assembly Language) 是一种接近于GPU指令的语言,同Pixel Bender3D的区别在于code
一个事先须要先编译好(Pixel Bender3D),component
另一个仅仅为一段字符串(借助AGAL Mini Assembler),能够在运行期间动态改变。orm
文档中说起的AGAL Mini Assembler是一个小类库,下载地址http://www.bytearray.org/wp-content/projects/agalassembler/com.ziphtm
主要是用于将字符串形式的AGAL转换为ByteArray。blog
在代码中最后上传这些操做给GPU时候,实际用到的方法为ip
能够在API中查到,参数类型为ByteArray,而非字符串文档
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display3D/Program3D.html
因此才须要一个小类库,将字符串转化为ByteArray
m44 op, va0, vc0 // pos to clipspace
mov v0, va1 // copy color
根据语法来讲,第一个m44 和 mov 均为指令集
mov:
moves data from source1 to destination, component-wiseadd:
destination = source1 + source2, component-wisesub:
destination = source1 – source2, component-wisemul:
destination = source1 * source2, component-wisediv:
destination = source1 / source2, component-wisedp3:
dot product (3 components) between source1 and source2dp4:
dot product (4 components) between source1 and source2m44:
multiplication between 4 components vector in source1 and 4×4 matrix in source2tex:
texture sample. Load from texture at source2 at coordinates source1.要了解后续代码的意思,须要引入寄存器的概念
AGAL doesn't use variables to store data, like ActionScript and other high level languages do. AGAL just uses registers.Registers are small memory areas in the GPU that AGAL programs (Shaders) can use during their execution. Registers are used to store both the sources and the destination of AGAL commands.
参考资料:
就我我的理解,应该能够认为Vertex Shader主要处理顶点相关的计算,如在数学上定义的一个三角形x(0,1,0),y(1,0,0),z(0,0,1),将这个三角形进行缩放,旋转等操做时候须要用到 Vertex Shader,
当该三角形完成以上所有操做后,后续的对其进行着色(UV map等)操做时候就须要用到Pixel shaders
m44 op, va0, vc0 // pos to clipspace
mov v0, va1 // copy color
根据AGAL语法:<操做指令>,<目标>,<数据源1>,<数据源2>
m44 op,va0,vc0
将va0(顶点坐标),和vc0(AS中传入的变量)执行m44(矩阵相乘),放入op(顶点计算结果)中
例子中的两行代码是否有任何关系?
由于第二行代码仅操做的 v0 和va1 和op没有任何关系,在后续中我会说一下我本身作的Demo,其中的Shader是
//compile vertex shader var vertexShader:Array = [ "dp4 op.x, va0, vc0", //4x4 matrix transform from 0 to output clipspace "dp4 op.y, va0, vc1", "dp4 op.z, va0, vc2", "dp4 op.w, va0, vc3", "mov v0, va1.xyzw" //copy texcoord from 1 to fragment program ]; //compile fragment shader var fragmentShader:Array = [ "mov ft0, v0\n", "tex ft1, ft0, fs1 <2d,clamp,linear>\n", //sample texture 1 "mov oc, ft1\n" ];
能够看出 仍旧以前先操做va0, 可是最后一行仍旧是操做的va1—> mov v0,va1.xyzw
已经在stackoverflow上面发帖,但愿可以获得解答
http://stackoverflow.com/questions/13854785/adboe-agal-confused-about-the-registers
对于疑惑的解答
后来有人在StackOverflow上面回帖了,我也本身看了一下代码,在Demo里面(后续会放出),存在以下代码
{ context.setVertexBufferAt(0, _buffer, 0, Context3DVertexBufferFormat.FLOAT_2); //xy
context.setVertexBufferAt(1, _buffer, 2, Context3DVertexBufferFormat.FLOAT_2); //uv
context.setTextureAt(1 , texture); }
该代码的意思及为,在va0中放入了顶点坐标,在va1中放入了uv坐标。
因此例子中的五行
"dp4 op.x, va0, vc0",
"dp4 op.y, va0, vc1",
"dp4 op.z, va0, vc2",
"dp4 op.w, va0, vc3",
"mov v0, va1.xyzw"
能够拆分为两部分理解,前四行操做的是vc0中的变换,最后存入op
第五行作的操做实际上是为了后续的fragmentShader操做作准备(如不太明白,能够查阅 什么是Vertex Shader,什么是Pixel shaders)
总体AGAL作的事情能够理解为:就是执行多种变换,最终结果存入op和oc 两个寄存器中