www.fgks.org   »   [go: up one dir, main page]

Thursday, October 04, 2007

The obligatory post on Hydra

Now that MAX 2007 in Chicago is over and the Flash Player team is somewhat settling for the final landing of Flash Player 9 Update 3 I've got to talk a little about Adobe Image Foundation, code named Hydra.

I have not been involved in this project as much as I would have liked to, with Bob Archer and Kevin Goldsmith driving most of the specifications and communication so far, nevertheless some technical questions in the context of Flash have been asked I should be able to answer. But let me make this post a little more approachable and explain the basics.

What is Hydra? If I would have to explain it in one sentence I would say: It is a shader language specifically tuned for 2D pixel graphics.

What is a shader language? Most of you will not know the answer to this. Let me try to put it into terms you might understand, with the assumption that you already have played with BitmapData objects in ActionScript 2.

Scenario: You have a bitmap and you want to exchange the red channel with the green channel. Easy enough, in Flash 8 or newer you can actually use the ColorMatrixFilter to do this. Lets say you do not have that filter, how would you do it? Well, this is what I would write in ActionScript 2:
import flash.display.*;

// create a green bitmap
var bitmap:BitmapData = new BitmapData(550,440,false,0x00FF00);

// go through the whole bitmap pixel by pixel
for(var y:Number=0; y<bitmap.height; y++) {
for(var x:Number=0; x<bitmap.width; x++) {

// get a single pixel from our bitmap
var pixel:Number = bitmap.getPixel(x, y);

var red:Number = (pixel>>16)&0xFF; // extract red
var green:Number = (pixel>> 8)&0xFF; // extract green
var blue:Number = (pixel )&0xFF; // extract blue

// create a new pixel with the red and green channels flipped
var newpixel:Number = (green<<16)|
(red << 8)|
(blue );

// replace the ol' one with the new one
bitmap.setPixel(x, y, newpixel);
}
}
// now the bitmap is pure red

// show it
this.createEmptyMovieClip("bitmap_mc",this.getNextHighestDepth());
bitmap_mc.attachBitmap(bitmap, 2, "auto", true);
That sure looks ugly and slow. Now lets express the same as above using Hydra, and believe me, it will do exactly the same as the above AS2 code:

kernel FlipRedGreen
{
void evaluatePixel(in image3 source, out pixel3 result)
{
result.grb = sample(source, outCoord());
}
}
There are several things you will notice:

  • There are no for loops to go over the pixels. That's right, shader languages only express what it inside the loop and assume that this is the operation you want to perform on every pixel.

  • There are a bunch of strange types here! Not really: image3 stands for a bitmap with RGB data and pixel3 stands for an individual RGB pixel. If you would want to include alpha you could say image4 and pixel4.

  • What is sample()? It is the same as BitmapData.getPixel() and returns a pixel, in this case a pixel3 type.

  • What is outCoord()? It is the same as the x and y positions in the AS2 code but it returns a single value containing x and y without the need to specify them seperately.

  • How does the actual flip work? Now that is some of the beauty which comes with using a shader language. You can perform all kinds of operations directly with the component identifiers.

    In this case we say 'result.grb =' which will flip the red and green channel. I could write 'result =' or 'result.rgb =' to just copy the pixel without a change. Or I could write any other combination like .bgr, .gbr to flip the different components.

    If you really want to get fancy you can also write something like this:
    'result = sample(source, outCoord()).rrr;' or
    'result = sample(source, outCoord()).bbr;'.
    In this case we make the component choice on the other side of the assigment.

    In essence, no more bit fiddling anymore, you have a high level access to the actual component data.

This is one of the most simple examples I can think of, Hydra is much more powerful and expressive than that. So I invite you to take a look and get comfortable with it, this is something which will stay with us for a long time to come. And possibly not only for graphics, but let me talk about that in a future post. :-) I also promise that I will have another post specifically talking about the technical aspects of the Flash Player implementation. I can think of those and more:

  • How will I be able to access this in my ActionScript code?

  • How much faster will it really be than ActionScript code?

  • Where will I be able to use Hydra? Just filters and blend modes, or more?

  • Will Flash Authoring support Hydra?

  • Will the Flash Player take advantage of hardware acceleration?

  • Will this work in the Flash Player even if there is no hardware acceleration available?

I know you are clamoring for answers, though for most questions we have not figured them out ourselves just yet.

There is obviously one more question: "Instead of working on new features, when do you get your act together and ship a 64-bit version of the player?" :-) I always have a good laugh when we get this one, especially since it has been answered many times already.

11 Comments:

Blogger Matthew said...

I look forward to seeing the answers to the other questions !

Nice article :)

Friday, October 05, 2007 3:08:00 PM  
Blogger hyperphonic said...

> and possibly not only for graphics ...

I've already been thinking about using the GPU for performing audio effects, along the lines of Sean Whalen's paper:

www.node99.org/projects/gpuaudio/gpuaudio.pdf

Any hints? (-;

Cheers,

- Ian

Friday, October 05, 2007 3:54:00 PM  
Anonymous Anonymous said...

Obligatory question: but will it be GPU accelerated or not when ran on Astro?

Friday, October 05, 2007 6:44:00 PM  
Blogger drawk said...

Thanks Tinic. First of all hopefully you get mor einvolved with this. Secondly, this is great to bring the world of GLSL and HLSL type technology to Flash. It now reminds me a bit of processing and Cg tools that were very fun, but being able to use at least some of these in Flash 2d is excellent!

Thanks for the more technical look at Hydra it is a very exciting technology. Shaders have really changed the landscape of 3d native gaming and I suspect that they will at the "next level" for Flash as well.

Now maybe you can look into Director 3d and see where that is going? I ask many at Adobe and it is pretty much stagnant.

Saturday, October 06, 2007 3:25:00 AM  
Anonymous Tek said...

Tinic, your example and your explanation about what is a shader language rocks ! It convinced me to download AIF Toolkit and launch my first tests.

Saturday, October 06, 2007 5:13:00 AM  
Anonymous Phil Douglas said...

Is there a reason Hydra isn't written in ECMAScript?

Saturday, October 06, 2007 10:03:00 AM  
Anonymous Brian Sexton said...

The question of why Adobe's Flash developers are working on new features rather than a 64-bit version of Flash Player may have "been answered many times already", but not everyone has seen those answers, so would you be kind enough to provide a link or two? Personally, I have seen some assertions that Adobe is working on 64-bit conversion of non-platform-specific Flash Player code for release at some unspecified time in the future, but I do not recall reading anything about why Adobe is prioritizing new features above 64-bit support (i.e., developing new features at all when those developers could be working on 64-bit support first).

Sunday, October 07, 2007 2:21:00 AM  
Blogger cageRattler said...

Tinic: you post wonderfull info all the time, thanks. Is it OK if I use some of your published code verbatim, and if so, where would you like me to link to when I credit you?

Tuesday, October 09, 2007 6:56:00 AM  
Blogger Nick said...

Hi Tinic, Sorry to use your blog in this way but I think this is a real issue for Flash and the louder I shout, the better for all. Plus I'm a major fan of what you do and I believe you can sort this out.

As you can see from my site http://www.idealhome.tv/gallery.php, we’re trying to get the property market going with interactive flash Tours and have been doing well under XP (reasonably smooth playback) but to my HORROR when I upgraded to the Business version of Vista our movies are incredibly jerky – under IE7 and Firefox, even using the Moviestar version of Flash. Audio frames are also being skipped.

What we need is Vista Flash to perform at least as well as XP Flash … if not better.

We run at 60 fps (but CPU is generally very low) and I can’t believe that Flash in Vista would have a problem with this when Flash 7 in Windows ME was fine with it!

Worse still clients have seen it and it could jeoprdise all the work we’ve done unless we can roll out a fix quick! Clients have even mentioned Silverlight as an alternative!

I’m a major fan of Flash and just want the experience to get better and better and not to drop the ball like this.

Can you please get this info into the hands of the right people and get a fix released just asap.

Many thanks, and please keep doing what you’re doing. You probably don’t realise how important you are to a number of us.

All the best,

Nick

Wednesday, October 10, 2007 4:15:00 PM  
Anonymous Anonymous said...

Awesome,

I had written something similar in AS2, it was with a base class iterating over pixels and subclasses implementing the individual pixel calculations. I had wave, scatter, blur and a couple more filters but it was terribly slow. So now it will be pure goodness :)

Tuesday, October 16, 2007 12:54:00 AM  
Anonymous spender said...

Tinic, can't you tell us all about the other treats Hydra might bring us? I was already thinking about subverting it for use in audio, but maybe you guys are on it?

Tuesday, October 16, 2007 8:18:00 PM  

Post a Comment

<< Home