<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>webr3.org &#187; animation</title>
	<atom:link href="http://webr3.org/blog/tag/animation/feed/" rel="self" type="application/rss+xml" />
	<link>http://webr3.org/blog</link>
	<description>brain&#039;s on fire!</description>
	<lastBuildDate>Tue, 19 Jul 2011 15:38:29 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Something about practical usage of flash.Memory in HaXe</title>
		<link>http://webr3.org/blog/haxe/something-about-practical-usage-of-flash-memory-in-haxe/</link>
		<comments>http://webr3.org/blog/haxe/something-about-practical-usage-of-flash-memory-in-haxe/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 17:17:18 +0000</pubDate>
		<dc:creator>nathan</dc:creator>
				<category><![CDATA[Flash 10]]></category>
		<category><![CDATA[haXe]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[3D computer graphics]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[ByteArray]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Fast Memory]]></category>
		<category><![CDATA[flash player]]></category>
		<category><![CDATA[Integer]]></category>
		<category><![CDATA[Lookup table]]></category>
		<category><![CDATA[ram]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">http://webr3.org/blog/?p=79</guid>
		<description><![CDATA[It's dawned on me that I've completely negated to mention why the haXe implementation of flash.Memory (flash player 10's new opcodes / fastmemory support) is so good.
Other than the really obvious bit that its direct access to a block of ram/fast memory which works extremely quickly, there is one other small but vital detail that's [...]]]></description>
			<content:encoded><![CDATA[<p>It's dawned on me that I've completely negated to mention why the haXe implementation of flash.Memory (flash player 10's new opcodes / fastmemory support) is so good.</p>
<p>Other than the really obvious bit that its direct access to a block of ram/fast memory which works extremely quickly, there is one other small but vital detail that's haXe specific and makes a vast difference between alchemy and haxe.</p>
<h2>The Big Difference</h2>
<p>You can think of flash.Memory as a static ByteArray, thus it is accessible from <em>both</em> static class and instance methods.</p>
<p>Now, another feature which offers huge speed increases in haXe is inlining.</p>
<p>Spelling it out - You can only inline static private methods, flash.Memory is all static access. So stick all your data (or big chunks of it) in flash.Memory and then inline  most of your heavy number crunching class.</p>
<p><strong>All inlined, all using fast memory = very very quick swfs!</strong></p>
<h2>Ideas and Tricks</h2>
<p>First of all, get the thought of only being able to have one chunk or type of data in fast memory at a time, you can partition it up and load everything in there.</p>
<p>A common setup I use is to have the first 1024 bytes as fast storage for float, int, byte, uint variables, the next xxx-thousand bytes as raw bitmap data, then all the other data I may want - like a block of 500k x,y,z floats for 3d data, followed by a few lookup charts.</p>
<p>the first 1024 bytes as fast storage for float, int, byte, uint variables - <em>because</em> you can then access the most commonly used vars using small int offsets (0-255) - and since its a small int your using it avoids all stacks completely, the bytecode will only contain the opcode to get the value from fast memory followed by the offset actually in the bytecode - for anything 256 and over its going to hit the int stack to get the offset first (which is barely noticable, but you know every fraction counts)</p>
<p>the next xxx-thousand bytes as raw bitmap data - <em>because </em>you can then work on your bitmap data ultra fast and write it back to the BitmapData instance using setPixels; very fast indeed, faster than a fixed sized vector even.</p>
<p>then all the other data - this is partially obvious, what may not be is the use of lookup charts, but I'll cover that in a moment.</p>
<h3>Accessing..</h3>
<p>Remember flash.Memory differs from a byte array in that you have to add in the offset for each value you want to grab</p>
<ul>
<li>Byte = 1 Byte</li>
<li>Int = 4 Bytes</li>
<li>Float = 4 Bytes</li>
<li>Double = 8 Bytes</li>
</ul>
<p>To use flash.Memory with functions which require a ByteArray.. [ like BitmapData.getPixels() -  Sound.extract() ] .. you can access it directly from flash.system.ApplicationDomain.currentDomain.domainMemory for example:</p>
<pre>// set position to start of the data we need
flash.system.ApplicationDomain.currentDomain.domainMemory.position = SOME_OFFSET;
// use the fast memory
bitmapData.setPixels( bitmapData.rect , flash.system.ApplicationDomain.currentDomain.domainMemory );</pre>
<p><strong>note:</strong> don't be fooled though, you can't simply call the functions on the bytearray in currentDomain, although <em>it is the data</em> that is in fast memory, it does not use the opcodes, which are what speed it up!</p>
<p><strong>Lookup Charts</strong></p>
<p>Often you'll find yourself calling the same code over and over and over; perhaps without realising just how much; consider we make an animation which uses perlin noise to generate 2d or 3d data - like this example - the example reads about 40k uint values from perlin noise, then converts each pixel to individual r,g,b values, then turns it in to a float -1 to 1 using a simple calculation. Thing is.. that means the same calculation runs 3x40000 times per frame at 50 frames a second.. 6 million times a second!</p>
<p>In this scenario, there are only 256 possible inputs to the calculation and 256 results - so rather than working it out 6 million times we can load it all in to a lookup chart, stick the lookup chart in fast memory and then lookup values rather than calculating - often you can really speed things up by doing this. <a href="http://webr3.org/experiments/perlin-particles/light-cloud/PerlinParticleEffects.hx">Practical example with comments here</a>.</p>
<p>Hope this is of use to somebody and clears things up a bit; I use the methods often so you'll see them in the source of my experiments.</p>
<p><img class="alignnone size-full wp-image-80" title="nopic" src="http://webr3.org/blog/wp-content/uploads/2009/07/nopic.jpg" alt="nopic" width="600" height="250" /></p>
]]></content:encoded>
			<wfw:commentRss>http://webr3.org/blog/haxe/something-about-practical-usage-of-flash-memory-in-haxe/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

