Go: AS3 call GPU Rendering

Source: Internet
Author: User

Content Transfer from Heaven

As code (TESTSHADER.AS):

First look at the code ... And then elaborate.

 Package2 { 3ImportFlash.display.Sprite;4ImportFlash.display.BitmapData;5ImportFlash.display.Loader;6ImportFlash.display.Shader;7ImportFlash.display.StageScaleMode;8Importflash.display.StageAlign;9Importflash.events.Event;10Importflash.events.TimerEvent;11ImportFlash.net.URLStream;12Importflash.net.URLRequest;13ImportFlash.utils.ByteArray;14ImportFlash.utils.Timer;15 16 Public classTestshaderextendsSprite17         {18Private var_bitmapdata:bitmapdata;19Private var_shader:shader =NewShader ();20Private var_sl:urlstream=NewURLStream ();21 22 Public functionTestshader ()23                 {24 This. stage.scalemode=Stagescalemode.no_scale;25 This. stage.align=Stagealign.top_left;26 This. stage.showdefaultcontextmenu=false;27//===========================================28 29//Loading Pictures30varImgloader:loader=NewLoader ();31Imgloader.contentLoaderInfo.addEventListener (event.complete,imgcomplete);Imgloader.load (NewURLRequest ("Image.jpg"));33                 }34 35/**36 * Loading picture complete PNS * @param event38 **/40Private functionImgcomplete (event:event):void41                 {_bitmapdata=Event.target.content.bitmapData;43_sl.addeventlistener (event.complete,shaderloaded);_sl.load (NewURLRequest ("OPENGLTEST.PBJ"));45                 }46 47/**48 * Load GLSL code Complete * @param event50 **/52Private functionShaderloaded (event:event):void53                 {54varsls:urlstream=Event.target as URLStream;55if(sls==NULL){56Throw NewError ("GLSL data is empty or load Error");57                         }58varbyarr:bytearray=NewByteArray ();59sls.readbytes (Byarr);60 61//Start Coloring Rendering_shader.bytecode =Byarr;_shader.data.src.input =_bitmapdata;64Changeshader ();65 66 67//time interval68varTime:timer=NewTimer (1000);69Time.addeventlistener (timerevent.timer,ontime);70Time.start ();71                 }72 73/**74 * time interval, dynamically changing shading rendering * @param event76 **/78Private functionOnTime (event:timerevent):void79                 {80Changeshader ();81                 }82 83/**84 * Change coloring render **/87Private functionChangeshader ():void88                 {_shader.data.exposure.value = [0.5-math.random ()];90 91graphics.clear ();                         Graphics.beginshaderfill (_shader); Graphics.drawrect (0,0,300,300); }         } }

Pixel Bender Code (OPENGLTEST.PBK):

<languageVersion:1.0;>kernel Exposurefilter<namespace:"TB"; Vendor:"Common"; Version:1; Description:"Picture Exposure";>{parameterfloatExposure<MinValue:float(-0.5); MaxValue:float(0.5); DefaultValue:float(0.0); Description:"Exposure"; >;    Input image4 src;    Output PIXEL4 DST; voidEvaluatepixel () {float4 Inputcolor=samplenearest (SRC, Outcoord ()); Dst.rgb= Pow (Inputcolor.rgb, FLOAT3 (1.0-exposure)); DST.A=Inputcolor.a; }}

some knowledge of OpenGL (for FP10 knowledge):
OpenGL is a professional 3D program interface, is a powerful, easy to call the bottom 3D graphics library. The current mainstream graphics card is support for OpenGL 2.0, while FP10 supports OpenGL 2.0.
Baidu Encyclopedia Information: [Url=http://baike.baidu.com/view/9222.htm]http://baike.baidu.com/view/9222.htm[/url].

GLSL is the development language of OpenGL, which is used for GPU computing.
Pixel Bender is built on GLSL based on the language, in fact, Adobe's own dedicated, the official said is effective on the CPU or GPU operation (how to feel more intelligent than GLSL, haha).
PBK is the suffix name of the code source file for the development pixel Bender of the Pixel Bender Toolkit development tool.
PBJ is pixel Bender Toolkit release the compiled pixel Bender code file suffix name to the FP10 called file.

Because said GLSL more smooth, the following are expressed with GLSL, do not pixel bender:loveliness:
examples of simple solutions:

As part:
Flash.display.Shader: Shader, call GLSL on her.
Flash.display.Shader: There is a data property in the class, and it is the GLSL code that is used to invoke the code in GLSL;

_shader.bytecode = Byarr: Gets the GLSL code in byte stream.

_shader.data.src.input = _bitmapdata: The src in this sentence corresponds to input image4 src in GLSL, which means that the bitmap data is passed to the GLSL code processing.
_shader.data.exposure.value = [0.5-math.random ()]: The exposure in this sentence is the exposure attribute in the GLSL code, and value is the GLSL that is assigned to the exposure code.

GLSL section:
<languageversion:1.0;&gt: Pixel Bender Toolkit Compile the required file headers.
Kernel Exposurefilter:kernel basic Graphics Rendering core unit, if you do not understand that you can first understand as the Class,exposurefilter render unit name, you can first understand as in the custom class name, must.
void Evaluatepixel (): This can be understood as the main entry run function, equivalent to the C language of main, must be.
Parameter: The attribute declared to the as call.
Input Image4 SRC:GLSL code gets the AS-passed bitmap data, SRC is the custom attribute name, Image4 is the SRC type, and input declares src as input.
Output Pixel4 DST:GLSL code is processed and exported to the Flash.display.Shader shaded display. DST is the custom property name, Pixel4 is the type of DST, and output declares DST for the outputs.

attention to detail:
The properties called by the shader.data.*** in the as are corresponding to the properties provided in the GLSL;
The attribute assigned to GLSL in as is given by [] instead of direct =, such as: _shader.data.exposure.value = [0.5];
Specific types refer to static properties in the Shaderparametertype class:

 PackageFlash.display {Final  Public classShaderparametertypeextendsObject {Static  Publicvar bool:string = "BOOL"; Static  Publicvar bool2:string = "Bool2"; Static  Publicvar bool3:string = "Bool3"; Static  Publicvar bool4:string = "Bool4"; Static  Publicvar float:string = "FLOAT"; Static  Publicvar float2:string = "Float2"; Static  Publicvar float3:string = "FLOAT3"; Static  Publicvar float4:string = "Float4"; Static  Publicvar int:string = "INT"; Static  Publicvar int2:string = "Int2"; Static  Publicvar int3:string = "Int3"; Static  Publicvar int4:string = "Int4"; Static  Publicvar matrix2x2:string = "matrix2x2"; Static  Publicvar matrix3x3:string = "matrix3x3"; Static  Publicvar matrix4x4:string = "matrix4x4";  Publicfunction Shaderparametertype (); }}

If the graphics driver does not support OpenGL 2.0, the program will use CPU operations instead of using GPU operations, without the graphics hardware acceleration effect. Please update the latest graphics driver to support OpenGL 2.0.

It is believed that in the near future, it will be useful for pixel Bender to develop a shaded rendering engine for the as call.

Go: AS3 call GPU Rendering

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.