<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Der Schmale - David Lenaerts's blog</title>
	
	<link>http://www.derschmale.com</link>
	<description>Flash Platform Experiments</description>
	<lastBuildDate>Sun, 24 Mar 2013 23:37:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/derschmale/QrhM" /><feedburner:info uri="derschmale/qrhm" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Dealing with the virality of memory alignment</title>
		<link>http://feedproxy.google.com/~r/derschmale/QrhM/~3/5bVZF84gytQ/</link>
		<comments>http://www.derschmale.com/2013/03/25/dealing-with-the-virality-of-memory-alignment/#comments</comments>
		<pubDate>Sun, 24 Mar 2013 23:37:27 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Helix]]></category>
		<category><![CDATA[align]]></category>
		<category><![CDATA[declspec]]></category>
		<category><![CDATA[helix]]></category>
		<category><![CDATA[memory alignment]]></category>
		<category><![CDATA[SIMD]]></category>
		<category><![CDATA[SSE]]></category>

		<guid isPermaLink="false">http://www.derschmale.com/?p=690</guid>
		<description><![CDATA[A C++-related post for a change! I refuse to spend whatever scraps of free time I can find on Flash these days, instead preferring to play around with DirectX 11 and tinkering away on a playground engine dubbed &#8220;Helix&#8221;. Only recently it came to my attention that there&#8217;s already a 3D game engine named &#8220;Helix&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p>A C++-related post for a change! I refuse to spend whatever scraps of free time I can find on Flash these days, instead preferring to play around with DirectX 11 and tinkering away on a playground engine dubbed &#8220;Helix&#8221;. Only recently it came to my attention that there&#8217;s already a 3D game engine named &#8220;Helix&#8221; on Google Code. It hasn&#8217;t been updated in 5 years and since I currently have no intention on publishing any of my stuff aside from the occasional snippet, I decided to go ahead and not care about names. Otherwise, there&#8217;s always <a title="Namingway" href="http://finalfantasy.wikia.com/wiki/Namingway" target="_blank">Namingway</a>! Anyway, I digress.. I&#8217;m having fun being forced to learn new things, and I hope to share a few things now and then (reminds me of the olden days of this blog!). On to memory alignment issues&#8230;</p>
<p>A quick alignment intro. Certain data types have strict memory alignment requirements, especially when using <a title="Introduction to SSE Programming" href="http://www.codeproject.com/Articles/4522/Introduction-to-SSE-Programming" target="_blank">SSE intrinsics</a>. In this particular case, types such as <em>__m128</em> require objects to be 16-byte aligned. This is automatically the case for variables on the stack on x86 and x64 architectures, but when your variable is a member of a class or allocated from the heap there&#8217;s no such guarantee. In MSVC, you can force the compiler to align your variables (or an entire class) correctly with respect to the class layout using the <em><a title="__declspec(align(#))" href="http://msdn.microsoft.com/en-us/library/83ythb65.aspx" target="_blank">__declspec(align(#))</a></em>. As long as the container class is aligned, the member will be as well.</p>
<p>Whenever there&#8217;s an alignment requirement and the object is in fact a class member, it imposes the same alignment requirement on the container class. See what happens if the container doesn&#8217;t share the alignment on the image below. This can easily start affecting large parts of your code-base, even in places that ostensibly have nothing to do with such an alignment. Alignment bugs are irregular, they don&#8217;t tell you they&#8217;re alignment issues (usually as an seemingly unrelated access violation) and as such they&#8217;re very hard to track down. If (no: when) any class up the containment chain is not correctly aligned, you&#8217;ll be hunting down strange illegal access errors all day.</p>
<div id="attachment_691" class="wp-caption aligncenter" style="width: 410px"><a href="http://www.derschmale.com/blog/wp-content/misalignment.gif"><img class=" wp-image-691    " title="misalignment" src="http://www.derschmale.com/blog/wp-content/misalignment.gif" alt="" width="400" height="223" /></a><p class="wp-caption-text">When class Container is allocated with a different alignment (at location 0&#215;24), member &#8220;aligned&#8221; is no longer 16-byte aligned (residing at address 0&#215;40).</p></div>
<p><em>__declspec(align(#))</em> does not affect dynamic allocations, so you&#8217;d need to implement <a title="Memory allocation and data alignment" href="http://flyeater.wordpress.com/2010/11/29/memory-allocation-and-data-alignment-custom-mallocfree/" target="_blank">a custom allocation scheme</a> to make sure the object is created at the correct location, and this for any class that is thus virally affected. To simplify dynamic allocations, it would be tempting to overload the global new and delete operators to always assure alignment whether it&#8217;s necessary or not. However, you may not want to overload the global operators; there&#8217;s always 16 byte of wasted padding for every allocated object and overriding global operators may be generally undesirable as it affects unrelated code. Besides, this only handles dynamic allocations; you still need to use <em>__declspec(align(#))</em> all over the place to assure static alignment.</p>
<p>To reduce mistakes and bug hunts, we wish to restrict the alignment requirements to where they are directly needed. The solution is pretty trivial, but since I couldn&#8217;t find any useful articles after a quick Google session, I decided to share my approach. I created a proxy template class simply called &#8220;Aligned&#8221;. See below: (I&#8217;m skipping out on some best practices, etc, feel free to complain about that <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> )</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#ifndef __HELIX_ALIGNED__</span><br />
<span style="color: #339900;">#define __HELIX_ALIGNED__</span><br />
<br />
<span style="color: #0000ff;">namespace</span> helix<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #ff0000; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* A wrapper for class properties that have alignment requirements<br />
&nbsp; &nbsp; &nbsp;* Type: The type of the aligned object <br />
&nbsp; &nbsp; &nbsp;* alignment: The alignment in bytes (defaults to 16)<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Type, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> alignment <span style="color: #000080;">=</span> <span style="color: #0000dd;">16</span><span style="color: #000080;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">class</span> Aligned<br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Aligned<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Aligned<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Type<span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>&nbsp; &nbsp; <span style="color: #666666;">// allow construction from non-wrapped objects</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Aligned<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ~Aligned<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Aligned<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// allow assignment of non-wrapped objects</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Aligned<span style="color: #000040;">&amp;</span> operator<span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Type<span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// dereference operator to get to actual object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">inline</span> Type<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span>object<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">const</span> Type<span style="color: #000040;">&amp;</span> operator<span style="color: #000040;">*</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span>object<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// member access operator for base object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">inline</span> Type<span style="color: #000040;">*</span> operator<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> object<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">const</span> Type<span style="color: #000040;">*</span> operator<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> object<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">private</span><span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// allocate statically to keep class layout coherent, we only need as much padding as the alignment value</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">char</span> block<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>Type<span style="color: #008000;">&#41;</span> <span style="color: #000040;">+</span> alignment<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Type<span style="color: #000040;">*</span> object<span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> GetAlignedPointer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Type, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> alignment<span style="color: #000080;">&gt;</span><br />
&nbsp; &nbsp; Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">Aligned</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; object<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr <span style="color: #000080;">=</span> GetAlignedPointer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; object <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span> Type<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Type, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> alignment<span style="color: #000080;">&gt;</span><br />
&nbsp; &nbsp; Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">Aligned</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Type<span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; object<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr <span style="color: #000080;">=</span> GetAlignedPointer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; object <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span> Type<span style="color: #008000;">&#40;</span>source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Type, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> alignment<span style="color: #000080;">&gt;</span><br />
&nbsp; &nbsp; Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">Aligned</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; object<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span> &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> ptr <span style="color: #000080;">=</span> GetAlignedPointer<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; object <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #008000;">&#40;</span>ptr<span style="color: #008000;">&#41;</span> Type<span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>source<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Type, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> alignment<span style="color: #000080;">&gt;</span><br />
&nbsp; &nbsp; Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span>~Aligned<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; object<span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span>~Type<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Type, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> alignment<span style="color: #000080;">&gt;</span><br />
&nbsp; &nbsp; Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000040;">*</span>object <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>source<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Type, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> alignment<span style="color: #000080;">&gt;</span><br />
&nbsp; &nbsp; Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #000040;">&amp;</span> Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">operator</span><span style="color: #000080;">=</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> Type<span style="color: #000040;">&amp;</span> source<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000040;">*</span>object <span style="color: #000080;">=</span> source<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> <span style="color: #000040;">*</span><span style="color: #0000dd;">this</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #0000ff;">template</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> Type, <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> alignment<span style="color: #000080;">&gt;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">void</span><span style="color: #000040;">*</span> Aligned<span style="color: #000080;">&lt;</span>Type, alignment<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">GetAlignedPointer</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666;">// offset to next 16-byte aligned object</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">int</span> padding <span style="color: #000080;">=</span> alignment <span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">size_t</span><span style="color: #008000;">&#40;</span>block<span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span> <span style="color: #008000;">&#40;</span>alignment <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">return</span> block <span style="color: #000040;">+</span> padding<span style="color: #008080;">;</span> <br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #339900;">#endif</span></div></div>
<p>The class is used as follows (not passing the alignment value assumes a default value of 16):</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000ff;">class</span> ContainerClass<br />
<span style="color: #008000;">&#123;</span><br />
<span style="color: #666666;">// ...</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">int</span> someObject<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; Aligned<span style="color: #000080;">&lt;</span>Type, <span style="color: #0000dd;">16</span><span style="color: #000080;">&gt;</span> aligned<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>Access to the aligned object is the same as with pointers through the dereference operator (<em>*aligned</em>) and the member access operator (<em>aligned-&gt;member</em>).</p>
<p>The Aligned class creates a memory block to contain the aligned object and some padding. Upon construction, the object checks the block&#8217;s address and finds the next correctly aligned address. No matter how it was created, the resulting location will be safe to construct the to-be-aligned object. The distance to the next aligned byte can&#8217;t obviously be larger than the alignment itself which is why we use that for padding.</p>
<p>Note that the block of memory is defined statically rather than creating it with <em>new</em>. This assures that stack-based object remain stack-based and that the location of the aligned object is still coherent with the container class&#8217;s layout. There will be less chance of a cache miss.</p>
<p>Since every usage of Aligned introduces some padding, it&#8217;d still be a waste if it happens more than once for a single container. You can group together objects with the same requirement in a struct, and wrap that in an Aligned proxy:</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666;">// ContainerClass is free to use whatever alignment it pleases</span><br />
<span style="color: #0000ff;">class</span> ContainerClass<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">struct</span> Properties <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// assuming Type is declared using __declspec(align(16))</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Type obj1<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Type obj2<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Type obj3<span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666;">// ...</span><br />
&nbsp; &nbsp; Aligned<span style="color: #000080;">&lt;</span>Properties, <span style="color: #0000dd;">16</span><span style="color: #000080;">&gt;</span> props<span style="color: #008080;">;</span><br />
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></div></div>
<p>If you already adhere to the <a title="Pimpl judiciously!" href="http://en.wikipedia.org/wiki/Opaque_pointer#C.2B.2B" target="_blank">pimpl idiom</a>, things should become easy enough by simply replacing the implementation struct with Aligned&lt;Impl&gt;.</p>
<p>It would make sense to create a similar solution for arrays, so you wouldn&#8217;t create an array of Aligned with padding waste per element. This should be a trivial variation.</p>
<p>I hope the post was useful for some. Until next time!</p>
<img src="http://feeds.feedburner.com/~r/derschmale/QrhM/~4/5bVZF84gytQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.derschmale.com/2013/03/25/dealing-with-the-virality-of-memory-alignment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.derschmale.com/2013/03/25/dealing-with-the-virality-of-memory-alignment/</feedburner:origLink></item>
		<item>
		<title>Speaking at FMX 2013</title>
		<link>http://feedproxy.google.com/~r/derschmale/QrhM/~3/tyRbUANqoJQ/</link>
		<comments>http://www.derschmale.com/2013/03/21/speaking-at-fmx-2013/#comments</comments>
		<pubDate>Thu, 21 Mar 2013 13:35:30 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[FMX]]></category>
		<category><![CDATA[gpu]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[Session]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[shading]]></category>

		<guid isPermaLink="false">http://www.derschmale.com/?p=676</guid>
		<description><![CDATA[Albeit with trembling knees, I&#8217;m very excited to announce I&#8217;ll be speaking at FMX this year, &#8216;Europe&#8217;s most renowned conference on digital entertainment&#8217;. It&#8217;s an intimidating place to be, with many of my heroes either in the organisation or in the speaker list! Luckily I won&#8217;t be alone; the session will be part of the [...]]]></description>
			<content:encoded><![CDATA[<p><a title="FMX 2013" href="http://www.fmx.de/program.html#!/event/924" target="_blank"><img class="alignleft size-full wp-image-677" title="fmx2013" src="http://www.derschmale.com/blog/wp-content/fmx2013.jpg" alt="" width="300" height="219" /></a>Albeit with trembling knees, I&#8217;m very excited to announce I&#8217;ll be speaking at <a title="FMX" href="http://www.fmx.de" target="_blank">FMX</a> this year, &#8216;Europe&#8217;s most renowned conference on digital entertainment&#8217;. It&#8217;s an intimidating place to be, with many of my heroes either in the organisation or in the speaker list! Luckily I won&#8217;t be alone; the session will be part of the <a title="Procedural Animation" href="http://www.fmx.de/program.html#!/list?t=126" target="_blank">Procedural Animation track</a>, where a couple of dear friends are lined up as well. Curated by good ol&#8217; <a title="Frank Reitberger" href="http://www.prinzipiell.com/" target="_blank">Frank</a>, the track is focused on rendering and animation outside the realms of gaming and film.</p>
<p>I&#8217;ll be presenting an adapted version of A Trick of Light that I tried out on Reasons to be Creative, which deals with an introduction to the world of programming shading and lighting without touching too much on the practical programming side of things. So no code but concepts, intuition and of course a wee bit of maths. This session is not meant for the shader programming veteran, but to provide a starting point for anyone interested in introducing some light to their procedural work.</p>
<p>I hope to see you there, <a title="FMX Tickets" href="http://www.fmx.de/tickets.html" target="_blank">conference passes</a> are ridiculously cheap. I know I&#8217;ll be gawking at all the other presentations!</p>
<p><strong>Some other news</strong></p>
<p>While I&#8217;m at it, here&#8217;s some other updates! I&#8217;ve recently joined the motley crew of <a title="Psykosoft" href="http://www.psykosoft.net/" target="_blank">Psykosoft</a> to work on some upcoming projects. I&#8217;m keeping tight-lipped about the details for a while, but maybe some people can already imagine some of the things I&#8217;m having fun with.</p>
<p>Alongside that, I&#8217;m also working on the <a title="Away3D 4.1 Beta" href="http://away3d.com/comments/away_foundation_roadmap_releases" target="_blank">Away3D 4.1 beta</a> to implement some optimizations, features and bug fixes. So keep an eye out for that!</p>
<p>As you can guess, busy times!</p>
<img src="http://feeds.feedburner.com/~r/derschmale/QrhM/~4/tyRbUANqoJQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.derschmale.com/2013/03/21/speaking-at-fmx-2013/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.derschmale.com/2013/03/21/speaking-at-fmx-2013/</feedburner:origLink></item>
		<item>
		<title>Another Take on Skin Rendering</title>
		<link>http://feedproxy.google.com/~r/derschmale/QrhM/~3/poC_nwFibh8/</link>
		<comments>http://www.derschmale.com/2012/11/16/another-take-on-skin-rendering/#comments</comments>
		<pubDate>Fri, 16 Nov 2012 18:30:23 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[shading]]></category>
		<category><![CDATA[shadow mapping]]></category>
		<category><![CDATA[Stage3D]]></category>

		<guid isPermaLink="false">http://www.derschmale.com/?p=649</guid>
		<description><![CDATA[It&#8217;s been a while since I&#8217;ve made a blog post about something I&#8217;ve played around with just for the heck of it.  With Away3D 4.1 Alpha pushed out yesterday, I decided to spend the day revisiting an old friend: skin rendering. Remember the blog post of skin rendering when Away3D 4 was still codenamed Broomstick [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Skin rendering" href="http://www.derschmale.com/demo/away3d/shadedHead2/MonsterHead.html" target="_blank"><img class="alignleft size-full wp-image-650" title="skinrendering" src="http://www.derschmale.com/blog/wp-content/skinrendering.jpg" alt="" width="300" height="219" /></a>It&#8217;s been a while since I&#8217;ve made a blog post about something I&#8217;ve played around with just for the heck of it.  With <a title="Away3D 4.1 Alpha Release" href="http://away3d.com/comments/away3d_4.1_alpha_release">Away3D 4.1 Alpha</a> pushed out yesterday, I decided to spend the day revisiting an old friend: skin rendering. Remember the <a title="Subsurface Scattering and Advanced Skin Rendering in Away3d 4.0 Broomstick" href="http://www.derschmale.com/2011/04/22/subsurface-scattering-and-advanced-skin-rendering-in-away3d-4-0-broomstick/" target="_blank">blog post</a> of skin rendering when Away3D 4 was still codenamed Broomstick over a year and a half ago? (ugh, time flies!) A lot has changed in the engine, and many new features have been added. It made sense to therefore build a new but similar example showing some newer additions, and highlighting how things were put together. There&#8217;s two variations of the demo:</p>
<ul>
<li><a title="Lee Perry-Smith" href="http://www.derschmale.com/demo/away3d/shadedHead2/LeePerrySmith.html" target="_blank">With a familiar face</a>: Using the same <a title="Lee Perry-Smith" href="http://www.ir-ltd.net/infinite-3d-head-scan-released" target="_blank">Lee Perry-Smith model</a> as before, for direct comparison (and because it&#8217;s free and I&#8217;m a stingy wretch)</li>
<li><a title="Monster head!" href="http://www.derschmale.com/demo/away3d/shadedHead2/MonsterHead.html" target="_blank">With a unfamiliar face</a>: Using a free (and insanely detailed) model from <a title="TurboSquid" href="http://www.turbosquid.com" target="_blank">TurboSquid</a> because I&#8217;m getting a bit sick of the old head (no offence intended to Mr. Perry-Smith) And again, it&#8217;s free and I&#8217;m a stingy wretch!</li>
</ul>
<p>As usual, click+drag to move the camera.</p>
<p>There&#8217;s source <a title="Source for skin rendering" href="http://www.derschmale.com/demo/away3d/shadedHead2/source.zip" target="_blank">right here</a>. There&#8217;s a bunch of boiler plate in there, but the only thing of interest in this case should be the <em>initMaterial</em> method in <em>Main.as</em>. I&#8217;ll highlight the material setup here.</p>
<p><strong>Multi-pass materials</strong></p>
<p>The demo is using a <a title="Multi-pass Rendering and Cascaded Shadow Mapping" href="http://www.derschmale.com/2012/11/15/multi-pass-rendering-and-cascaded-shadow-mapping/" target="_blank">multi-pass material</a> for a few reasons. It allows the shadow mapping to more complex, and it prevents the non-casting lights from being masked by the shadow map, allowing for more dramatic lighting effects.</p>
<p><strong>Soft Shadows</strong></p>
<p>With a SoftShadowMapMethod assigned to the material, it&#8217;s possible to &#8211; as the name suggests &#8211; cast soft shadows on the skin. The class was updated in Away3D 4.1 along with some other shadow map methods, allowing more samples to be taken in the shadow map. This way, the results are much smoother which is especially important when tweaking the method&#8217;s <em>range</em> property. It sets the distance with which shadow map samples are taken, effectively creating softer shadows (a setting you can tweak in the demos). Of course, the more samples you take, the more demanding your shader will become.</p>
<p><strong>Fresnel Specular Highlights</strong></p>
<p>This is identical to the old demo. It uses <em>FresnelSpecularMethod</em> to achieve the <a title="Fresnel Effect" href="http://www.3drender.com/glossary/fresneleffect.htm" target="_blank">fresnel effect</a>, causing stronger highlights at glancing viewing angles. The <em>fresnelPower</em> can be tweaked to change the viewing angle fall-off: higher values increase the fresnel effect.</p>
<p><strong>Subsurface Scattering Through Gradient Diffuse Lighting</strong></p>
<p>By using GradientDiffuseMethod, you can acchieve a very crude approximation to subsurface scattering. It allows you to pass a gradient image to define the colour and strength of the diffuse reflection for each light/normal-angle. The left of the texture (x = 0) contains the reflection for surfaces pointing away from the light, the right is the reflection when completely facing the light. This allows darker lighting to be made a bit lighter. In this case, by introducing a little red in the mid-values (see &#8220;embeds/diffuseGradient.jpg&#8221;), we can simulate the addition of scattered light into the final shaded colour. While it doesn&#8217;t create true translucency effects similar to <em>SubsurfaceScatteringDiffuseMethod</em>, it does create an organic softness when viewing the surface head-on, an effect that is as subtle as it is effective.</p>
<p><strong>Lighting Set-up</strong></p>
<p>The light set-up is very traditional, a directional light coming from above and the front (by default) with a bright blue point light to the right and a red point light to the left. It&#8217;s worth noting however that I&#8217;ve set the directional light&#8217;s <em>specular</em> property to a much lower value to prevent it from creating strong highlights. Much like a softbox, I&#8217;d imagine.</p>
<p>Enjoy tweaking!</p>
<img src="http://feeds.feedburner.com/~r/derschmale/QrhM/~4/poC_nwFibh8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.derschmale.com/2012/11/16/another-take-on-skin-rendering/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<feedburner:origLink>http://www.derschmale.com/2012/11/16/another-take-on-skin-rendering/</feedburner:origLink></item>
		<item>
		<title>Multi-pass Rendering and Cascaded Shadow Mapping</title>
		<link>http://feedproxy.google.com/~r/derschmale/QrhM/~3/p_BNkW6z0pE/</link>
		<comments>http://www.derschmale.com/2012/11/15/multi-pass-rendering-and-cascaded-shadow-mapping/#comments</comments>
		<pubDate>Thu, 15 Nov 2012 16:10:32 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[gpu]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[shading]]></category>
		<category><![CDATA[shadow mapping]]></category>

		<guid isPermaLink="false">http://www.derschmale.com/?p=619</guid>
		<description><![CDATA[If you&#8217;ve been paying attention to the Away3D blog, you probably already know that the 4.1 alpha has been released today, which has been my main fixation since September (when I wasn&#8217;t getting radiation poisoning). As mentioned in the release post, one of the new features is &#8220;multipass shading&#8221;. If you&#8217;re not into 3D rendering programming, [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Sponza Multi-pass Demo" href="http://infiniteturtles.co.uk/projects/away3d4/examples/Advanced_MultiPassSponzaDemo/Advanced_MultiPassSponzaDemo.html" target="_blank"><img class="alignleft size-full wp-image-628" title="sponzaDemo" src="http://www.derschmale.com/blog/wp-content/sponzaDemo.jpg" alt="" width="300" height="219" /></a>If you&#8217;ve been paying attention to the Away3D blog, you probably already know that the <a title="Away3D 4.1 Alpha release" href="http://away3d.com/comments/away3d_4.1_alpha_release" target="_blank">4.1 alpha has been released</a> today, which has been my main fixation since September (when I wasn&#8217;t getting <a href="https://twitter.com/DerSchmale/status/261146154992037888/photo/1" target="_blank">radiation poisoning</a>). As mentioned in the release post, one of the new features is &#8220;multipass shading&#8221;. If you&#8217;re not into 3D rendering programming, you may be asking yourself: &#8220;¿Qué?&#8221;, so I would like to go a bit more in-depth into the whats, whys, and hows.</p>
<p><strong>Multi-pass shading</strong></p>
<p>Multipass rendering is simply executing different render calls (&#8220;passes&#8221;) for a single geometry. Strictly speaking, Away3D 4.0 has supported multiple passes since its inception; SubSurfaceScatteringDiffuseMethod caused a depth map pass to be added, and OutlineMethod introduces a new pass to render the outline seperately from the normal geometry. However, all lighting &#8211; and methods contributing to the colour of the final output pixel such as fog, rim lighting, etc &#8211; happened in a single pass. With shaders being limited in number of instructions and amount of registers, so are the amount of lights that can affect a surface as well as the complexity of any other piece of code, shadow mapping being a common victim. This is where 4.1 introduces multiple passes (hence multi-pass <em>shading</em>) in the form of <em>TextureMultiPassMaterial</em> and <em>ColorMultiPassMaterial</em>. For all intents and purposes, they work identical to their single-pass counterparts (<em>TextureMaterial</em> and <em>ColorMaterial</em>, respectively), but they automatically split up into different passes depending on the amount of lights and the use of shadow mapping. The results are blended together to form a correctly shaded surface. This way, there&#8217;s no strict upper limit for lights. Of course, drawing the geometry multiple times comes with its own cost, and you should take care not to go gung-ho just because you can!</p>
<p><strong>Cascade Shadow Mapping</strong></p>
<p>Having shadows in a separate pass greatly increases what you can do to increase shadow quality. Getting shadows to look great is always a bit of a challenge, and one of the techniques that&#8217;s popular is Cascaded Shadow Maps (a great explanation <a title="Cascaded Shadow Maps" href="http://msdn.microsoft.com/en-us/library/windows/desktop/ee416307(v=vs.85).aspx" target="_blank">here</a>). If you&#8217;re too lazy to read the msdn article, here&#8217;s the gist of it. Due to perspective projection, fragments close to the viewer generally suffer shadow aliasing, because a texel in the shadow map covers much more screen space closer to the camera than it does farther away. In fact, for distant fragments, the shadow map resolution is often too large since several shadow map texels map to the same screen fragment. So there&#8217;s not enough resolution in the front, and possibly too much in the back! Cascaded Shadow Maps try to solve this by splitting up the view frustum and rendering separate shadow maps for each segment. The closer to the viewer, the smaller the frustum segment should be so as to increase the projected resolution of the shadow map.</p>
<p><strong>CSM in Away3D 4.1</strong></p>
<p>As you&#8217;ll see in demo code, it&#8217;s pretty easy to get this technique to work. Just be sure to use a multi-pass material and assign CascadeShadowMapper to the light and a CascadeShadowMapMethod to the material (this is identical to how NearDirectionalShadowMapper and NearFieldShadowMapMethod work).</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900; font-style: italic;">// can pass a value up to 4 in CascadeShadowMapper</span><br />
cascadeShadowMapper = <span style="color: #0033ff; font-weight: bold;">new</span> CascadeShadowMapper<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
cascadeShadowMapper<span style="color: #000066; font-weight: bold;">.</span>lightOffset = <span style="color: #000000; font-weight:bold;">10000</span><span style="color: #000066; font-weight: bold;">;</span><br />
directionalLight = <span style="color: #0033ff; font-weight: bold;">new</span> DirectionalLight<span style="color: #000000;">&#40;</span><span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">-</span><span style="color: #000000; font-weight:bold;">15</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
directionalLight<span style="color: #000066; font-weight: bold;">.</span>shadowMapper = _cascadeShadowMapper<span style="color: #000066; font-weight: bold;">;</span><br />
scene<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addChild</span><span style="color: #000000;">&#40;</span>_directionalLight<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
multiMaterial = <span style="color: #0033ff; font-weight: bold;">new</span> TextureMultiPassMaterial<span style="color: #000000;">&#40;</span>texture<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
multiMaterial<span style="color: #000066; font-weight: bold;">.</span>lightPicker = <span style="color: #0033ff; font-weight: bold;">new</span> StaticLightPicker<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>light<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #009900; font-style: italic;">// you can also use HardShadowMapMethod, SoftShadowMapMethod, DitheredShadowMapMethod as base method</span><br />
baseShadowMethod = <span style="color: #0033ff; font-weight: bold;">new</span> FilteredShadowMapMethod<span style="color: #000000;">&#40;</span>light<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
multiMaterial<span style="color: #000066; font-weight: bold;">.</span>shadowMapMethod = <span style="color: #0033ff; font-weight: bold;">new</span> CascadeShadowMapMethod<span style="color: #000000;">&#40;</span>baseShadowMethod<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>Away3D splits up the view frustum automatically, but it&#8217;s often a good idea to play around with different split ratios yourself. The values with the best results depend heavily on the scene, how close you allow the camera to get to shaded surfaces, and so on. This is done by simply specifying the ratio in the view frustum for each cascade level to reach, &#8220;0&#8243; being the frustum&#8217;s near plane, &#8220;1&#8243; being the far plane. Generally, you&#8217;ll want the last cascade level to reach to the far plane, thus passing &#8220;1&#8243;.</p>
<p>For example, when using 4 cascade levels, you could specify the splits as follows:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">cascadeShadowMapper<span style="color: #000066; font-weight: bold;">.</span>setSplitRatio<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">.</span>1<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
cascadeShadowMapper<span style="color: #000066; font-weight: bold;">.</span>setSplitRatio<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">1</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">.</span>2<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
cascadeShadowMapper<span style="color: #000066; font-weight: bold;">.</span>setSplitRatio<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000066; font-weight: bold;">.</span>4<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
cascadeShadowMapper<span style="color: #000066; font-weight: bold;">.</span>setSplitRatio<span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">3</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #000000; font-weight:bold;">1.0</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>A small detail to note: because texture memory is a precious resource, Away3D internally only uses a single texture for all (up to 4) shadow maps.</p>
<p><strong>The demo and source</strong></p>
<p><a title="Away3D Sponza Multipass Demo" href="http://infiniteturtles.co.uk/projects/away3d4/examples/Advanced_MultiPassSponzaDemo/Advanced_MultiPassSponzaDemo.html" target="_blank">The demo that was built</a> to demonstrate the multi-pass materials features the Sponza model built and made available by Crytek. You can find the original at their <a title="Crytek CryEngine Downloads" href="http://www.crytek.com/cryengine/cryengine3/downloads">download site</a>. It&#8217;s a bit of a behemoth, so give it some time to load <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  It allows you to play around with some of the shadow map settings, which should be pretty straightforward. At least, they will be when I get around to writing an upcoming blog post about shadow map filter types. Source for the demo can be found <a title="Sponza demo source" href="https://github.com/away3d/away3d-examples-fp11/blob/dev/src/Advanced_MultiPassSponzaDemo.as" target="_blank">on Github</a>.</p>
<p>If you were at my presentation at Reasons to be Creative (what were you doing, missing out on the great weather outside!) you may recognize the set-up. It&#8217;s the same as I used for the deferred rendering demo; however, this is <em>not</em> deferred rendering: we merely re-purposed the demo <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a title="Away3D 4.1a on Github" href="https://github.com/away3d/away3d-core-fp11/tree/dev" target="_blank">Get the Away3D 4.1 alpha version in the dev branch on Github.</a></p>
<img src="http://feeds.feedburner.com/~r/derschmale/QrhM/~4/p_BNkW6z0pE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.derschmale.com/2012/11/15/multi-pass-rendering-and-cascaded-shadow-mapping/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.derschmale.com/2012/11/15/multi-pass-rendering-and-cascaded-shadow-mapping/</feedburner:origLink></item>
		<item>
		<title>Away3D 4.1 (dev) Dynamic Reflections</title>
		<link>http://feedproxy.google.com/~r/derschmale/QrhM/~3/-p-R6b11g6Y/</link>
		<comments>http://www.derschmale.com/2012/09/10/away3d-4-1-dev-dynamic-reflections/#comments</comments>
		<pubDate>Mon, 10 Sep 2012 19:21:06 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[effects]]></category>
		<category><![CDATA[environment mapping]]></category>
		<category><![CDATA[reflections]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[shading]]></category>
		<category><![CDATA[Stage3D]]></category>

		<guid isPermaLink="false">http://www.derschmale.com/?p=600</guid>
		<description><![CDATA[One of the features we considered important for the next release of the Away3D engine (4.1) were real-time dynamic reflections, allowing for more realism and precision than the common static environment maps. In the dev branch of the engine, you can now find two flavours: reflections based on dynamic environment maps, and planar reflections. Dynamic Environment Maps [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-601" title="reflections" src="http://www.derschmale.com/blog/wp-content/reflections.jpg" alt="R2D2*2" width="300" height="219" />One of the features we considered important for the next release of the <a title="Away3D" href="http://www.away3d.com" target="_blank">Away3D</a> engine (4.1) were real-time dynamic reflections, allowing for more realism and precision than the common static environment maps. In the <a title="Away3D dev branch" href="https://github.com/away3d/away3d-core-fp11/tree/dev" target="_blank">dev branch</a> of the engine, you can now find two flavours: reflections based on dynamic environment maps, and planar reflections.</p>
<p><strong>Dynamic Environment Maps</strong></p>
<p>This technique simply uses cube maps that are rendered to on the fly. Usually, they&#8217;re used for any non-planar surfaces, and while they can look convincing enough for complex models they do suffer the same flaws as normal environment maps. Since the scene is rendered for each face of the cube from a single point, the calculated reflections would obviously only be correct for that single point in space, but using it for relatively small and complex objects can look convincing enough. However, since it renders the scene 6 times, it can be slow for more complex situations.</p>
<p>The necessary functionality is exposed through CubeReflectionTexture, a class that can be used wherever a CubeTextureBase is expected: EnvMapMethod and variations, or even as a Skybox texture. I have yet to come up with a use for the latter case, though <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  To get the best results, it&#8217;s usually a good idea to set the CubeReflectionTexture&#8217;s position to the centre of your reflective object. The cube map will be generated from this point and on average will yield the best results for all other points.</p>
<p><a title="Dynamic cube reflections" href="http://derschmale.com/demo/away3d/rtreflections/cube.html" target="_blank">Check out the demo.</a><br />
<a title="Dynamic Environment Maps Source" href="https://github.com/away3d/away3d-examples-fp11/blob/dev/src/Intermediate_RealTimeEnvMap.as" target="_blank">Source in the &#8220;dev&#8221; examples reposi</a><a title="Source" href="https://github.com/away3d/away3d-examples-fp11/blob/dev/src/Intermediate_RealTimeEnvMap.as" target="_blank">tory</a></p>
<p><strong>Planar Reflections</strong></p>
<p>For planar surfaces, a much cheaper approach and one that is very precise can be used. This means normal flat mirrors, polished floors, water, &#8230; which are quite common in games can be rendered much more effectively. Since the rules for reflection for the entire surface are the same, we can simply render the scene from a mirrored camera perspective. The only thing we need to make sure is that objects (or parts of objects) behind the mirror aren&#8217;t being rendered in this way. Usually, in OpenGL or DirectX, you&#8217;d simply introduce a new user-defined clip plane. Flash, however, doesn&#8217;t support anything of the sort. Instead, the projection matrix needs to be adapted so that the near plane becomes oblique; aligned with the mirrored plane, effectively clipping any straddling geometry along the mirror. Unfortunately, this also wreaks havoc on the far plane, which will starting cutting geometry that is in the mirrored view due to being at a different angle. <a title="Eric Lengyel" href="http://www.terathon.com/lengyel/" target="_blank">Eric Lengyel</a> effectively describes the issue and how he cleverly solves it in his <a title="Oblique View Frustum" href="http://www.terathon.com/lengyel/Lengyel-Oblique.pdf" target="_blank">Oblique View Frustum</a> paper. <em>(And while I&#8217;m at it, <a title="Mathematics for 3D Game Programming &amp; Computer Graphics" href="http://www.amazon.com/Mathematics-Programming-Computer-Graphics-Edition/dp/1435458869/ref=dp_ob_title_bk" target="_blank">his book</a> is awesome too.)</em></p>
<p>The texture target is provided as PlanarReflectionTexture. Similar to the cube maps, they need some information about where their respective reflective surfaces are. In this case, it&#8217;s the <em>plane</em> property; referencing a Plane3D object. Furthermore, it has a &#8220;<em>scale</em>&#8221; property that lets you define how much the texture should be scaled down to control quality vs rendering speed. Due to the different math and texture types involved, PlanarReflectionTexture can only be used with specific material methods. Currently, these are PlanarReflectionMethod and FresnelPlanarReflectionMethod. Except for internals and texture type, these function pretty much identical to their EnvMap counterparts.</p>
<p><a title="Planar Reflections demo" href="http://www.derschmale.com/demo/away3d/rtreflections/planar.html" target="_blank">Check out the demo</a><br />
<a title="Planar Reflections Source" href="https://github.com/away3d/away3d-examples-fp11/blob/dev/src/Intermediate_PlanarReflections.as" target="_blank">Source in the &#8220;dev&#8221; examples repository</a>.</p>
<p>In closing, this is of course only in the dev branch, which means it&#8217;s still subject to change!</p>
<img src="http://feeds.feedburner.com/~r/derschmale/QrhM/~4/-p-R6b11g6Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.derschmale.com/2012/09/10/away3d-4-1-dev-dynamic-reflections/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		<feedburner:origLink>http://www.derschmale.com/2012/09/10/away3d-4-1-dev-dynamic-reflections/</feedburner:origLink></item>
		<item>
		<title>Speaking at Reasons to be Creative 2012!</title>
		<link>http://feedproxy.google.com/~r/derschmale/QrhM/~3/5n5jYk8ZrV4/</link>
		<comments>http://www.derschmale.com/2012/08/10/speaking-at-reasons-to-be-creative-2012/#comments</comments>
		<pubDate>Fri, 10 Aug 2012 11:18:14 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[conference]]></category>
		<category><![CDATA[effects]]></category>
		<category><![CDATA[Flash on the Beach]]></category>
		<category><![CDATA[Flash Player 11]]></category>
		<category><![CDATA[Molehill]]></category>
		<category><![CDATA[Reasons to be Creative]]></category>
		<category><![CDATA[Session]]></category>
		<category><![CDATA[Shaders]]></category>
		<category><![CDATA[shading]]></category>
		<category><![CDATA[Stage3D]]></category>

		<guid isPermaLink="false">http://www.derschmale.com/?p=577</guid>
		<description><![CDATA[Oh boy, it shames me to see my last post has already been from December last year! Not that I haven&#8217;t been keeping busy; some binned personal projects, some that aren&#8217;t quite &#8220;there&#8221; yet, and a 3-month stint near San Francisco (what a city!). So yes, I&#8217;m happy to have a reason for another update, [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-578" title="reasons" src="http://www.derschmale.com/blog/wp-content/reasons.jpg" alt="" width="300" height="219" />Oh boy, it shames me to see my last post has already been from December last year! Not that I haven&#8217;t been keeping busy; some binned personal projects, some that aren&#8217;t quite &#8220;there&#8221; yet, and a 3-month stint near San Francisco (what a city!). So yes, I&#8217;m happy to have a reason for another update, and what a Reasons it is!</p>
<p>After presenting at the awesome Flash on the Beach last year, I have the unexpected honour of having been asked to come back for another round this year with the conference being restyled as Reasons to be Creative. Sweet! My session this year is called <a title="Session description" href="http://www.reasonstobecreative.com/speakers/index.php?id=davidlenaerts" target="_blank">&#8220;A Trick of Light&#8221;</a>, and will primarily deal with 3D shading: simulating lighting for 3D rendering. I touched upon this subject briefly last year, but I felt such an interesting subject deserves full-time attention. I&#8217;m working hard on explaining things in an intuitive way, something many books forget to do, while still providing some maths. Just a little and in a very unrigorous way, so don&#8217;t be afraid if you&#8217;re a mathophobe <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  I won&#8217;t be digging into any actual code as I find that relatively pointless compared to understanding the actual concepts. Furthermore, I wish to present these in as much a platform-independent way as possible. However, all my examples will obviously be built using <a title="Away3D" href="http://www.away3d.com/" target="_blank">Away3D</a> and I&#8217;ll be plugging that now and then, or what did you expect <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>The line-up is looking great again this year and I&#8217;m glad to see many friends represented in the speaker list! Check out  the likes of <a title="Rob Bateman" href="http://www.reasonstobecreative.com/speakers/index.php?id=robbateman" target="_blank">Rob &#8220;I like turtles&#8221; Bateman</a>, <a title="Frank Reitberger" href="http://www.reasonstobecreative.com/speakers/index.php?id=frankreitberger" target="_blank">Frank &#8220;wob-wob&#8221; Reitberger</a>, <a title="Eugene Zatepyakin" href="http://www.reasonstobecreative.com/speakers/index.php?id=eugenezatepyakin" target="_blank">Eugene &#8220;buy-me-a-beer&#8221; Zatepyakin</a>, <a title="Joa Ebert" href="http://www.reasonstobecreative.com/speakers/index.php?id=joaebert" target="_blank">Joa &#8220;who needs semi-colons anyway&#8221; Ebert</a>, <a title="Jon Howard" href="http://www.reasonstobecreative.com/speakers/index.php?id=jonhoward" target="_blank">Jon Howard</a>, <a title="Andre Michelle" href="http://www.reasonstobecreative.com/speakers/index.php?id=andremichelle" target="_blank">Andre Michelle</a>, <a title="Mario Klingemann" href="http://www.reasonstobecreative.com/speakers/index.php?id=marioklingemann" target="_blank">Mario Klingemann</a>, <a title="Reasons to be Creative" href="http://www.reasonstobecreative.com/speakers/" target="_blank">et al</a>&#8230; Do yourself a favour and don&#8217;t miss it! I myself am looking forward to hanging out with old and new friends and get some inspiration shots, so do come over and have some beers with us!</p>
<p>Well, it&#8217;s time for me to lock myself up inside again with some 80s thrash metal and get that presentation done! See you all in September!</p>
<p>&nbsp;</p>
<img src="http://feeds.feedburner.com/~r/derschmale/QrhM/~4/5n5jYk8ZrV4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.derschmale.com/2012/08/10/speaking-at-reasons-to-be-creative-2012/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.derschmale.com/2012/08/10/speaking-at-reasons-to-be-creative-2012/</feedburner:origLink></item>
		<item>
		<title>Convolution Light Probes in Away3D 4.0</title>
		<link>http://feedproxy.google.com/~r/derschmale/QrhM/~3/qyOUAvdTVKA/</link>
		<comments>http://www.derschmale.com/2011/12/27/convolution-light-probes-in-away3d-4-0/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 17:07:55 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[Away3D 4.0]]></category>
		<category><![CDATA[light probes]]></category>
		<category><![CDATA[materials]]></category>
		<category><![CDATA[shading]]></category>
		<category><![CDATA[shadow mapping]]></category>

		<guid isPermaLink="false">http://www.derschmale.com/?p=562</guid>
		<description><![CDATA[As mentioned in the previous blog post, the dev branch of Away3D 4.0 now has light probes. Light probes are essentially a special sort of light source that contain global lighting info for the scene location they&#8217;re placed at, encoded as cube maps. For diffuse lighting, these cube maps are simply indexed by the surface [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Light Probes Demo" href="http://www.derschmale.com/demo/away3d/lightProbes/" target="_blank"><img class="alignleft size-full wp-image-563" title="lightProbes" src="http://www.derschmale.com/blog/wp-content/lightProbes.png" alt="" width="300" height="219" /></a>As mentioned in the previous blog post, the <a title="Away3D 4.0 Dev Branch" href="https://github.com/away3d/away3d-core-fp11/tree/dev" target="_blank">dev branch</a> of Away3D 4.0 now has light probes. Light probes are essentially a special sort of light source that contain global lighting info for the scene location they&#8217;re placed at, encoded as cube maps. For diffuse lighting, these cube maps are simply indexed by the surface normal of the shaded fragment. For specular lighting, the view reflection vector is used. As such they&#8217;re a fast basic way of faking global illumination. The fact that it&#8217;s using cube maps means that the lighting would only be physically correct at the precise point where the light was sampled, but the results can still be convincing regardless. Several light probes can be placed in a scene; the material&#8217;s light picker will assign weights to each to determine the importance in the lighting step based on the rendered object&#8217;s position relative to each.</p>
<p>You can use normal lights alongside the light probes, and in case of the default material system, you can specify which lighting component (diffuse and specular) is affected by which light types (<em>material.diffuseLightSources</em> and <em>material.specularLightSources </em>accepting <em>LightSources.LIGHTS</em>, <em>LightSources.PROBES</em>,  or <em>LightSources.ALL</em>). This can be useful if you want to use light probes for diffuse global lighting, but want specular lights from traditional light sources without those affecting the diffuse light.</p>
<p>&nbsp;</p>
<p><strong>Pre-process</strong><strong> step</strong></p>
<p>A downside of this approach over conventional light sources is that it requires a pre-process step to generate the cube maps. To start with, you need to generate (HDR) cube map renders for every point where you&#8217;ll want to put a light probe. From this, two process steps can be applied: a diffuse convolution and a specular convolution, depending on which type of lighting the probe needs to support. These can be generated in the eminent Paul Debevec&#8217;s <a title="HDRShop" href="http://projects.ict.usc.edu/graphics/HDRShop/" target="_blank">HDRShop</a>. I&#8217;ll warn you, however, that these convolutions can be very time-consuming &#8211; but the results are very accurate. If you&#8217;re willing to sacrifice some accuracy and get your hands dirty, you can write your own &#8220;convolutor&#8221;. Be warned however, that Flash doesn&#8217;t support HDR in BitmapData or textures, so if you&#8217;re using BitmapData and Stage3D, you&#8217;ll end up with under-lit results due to the low dynamic range of &#8220;bright&#8221; light. Having said that, I&#8217;ll quickly outline the principles behind it, just in case it interests anyone.</p>
<p>Lighting functions are spherical functions (the domain is the unit sphere, being mapped to scalar values), so you&#8217;ll need to integrate the diffuse or specular lighting function over the unit sphere of surface normals/view reflections. In fact, integrating over the hemisphere is usually enough since there&#8217;s no light coming from inside objects in our case. But as I was lazy and it was much easier to just implement it for the entire sphere I&#8217;ll use that approach <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  For basic (Lambertian) diffuse reflections, this semi-formally boils down to:</p>
<p><a href="http://www.derschmale.com/blog/wp-content/diffuseConvolution.gif"><img class="aligncenter size-full wp-image-564" title="diffuseConvolution" src="http://www.derschmale.com/blog/wp-content/diffuseConvolution.gif" alt="" width="260" height="60" /></a>Where N is the surface normal, &#8220;<em><em>ω_</em>i</em>&#8221; is the incident light vector, and L(<em>ω_i</em>) is the light colour coming from the direction <em>ω_i. </em>The part &#8220;max(N.<em>ω_i, 0) L(<em>ω_i)&#8221;, </em></em>is in fact just the same as the diffuse lighting calculation done for point and directional lights (where L is a constant function). In this case, <em>L(<em>ω_i) </em></em>is simply a sample from the HDR cube map we rendered before. The integral just means that we&#8217;re taking the diffuse contributions for all directions (ie: all vectors <em>ω_i </em>in the unit sphere S) and accumulating them. Finally, to be able to sample the incoming light for each normal in an environment map, we would need to perform this calculation for every point in that map (remember, cube maps coordinates are simply 3D vectors, in this case representing the surface normal N).</p>
<p>In theory, the integral means we&#8217;d be summing up infinite amounts of infinitesimal samples. In practice, we&#8217;ll be using discrete steps to solve this. That&#8217;s okay, since we only have finite amount of samples to work with anyway; 6*n² pixels to be exact, n being the size of the HDR cube map. So, the most accurate way of doing things would be to add up all the contributions from the source map, and do that for each pixel in the destination map. If we&#8217;re rendering to a cube map of size <em>m, </em>that means it&#8217;s an order of <em>6*m² * 6*n² = 36m²n² </em>(6 sides of <em>m</em>*<em>m, </em>each pixel sampling 6 sides of <em>n*n</em>). This can easily become a very expensive operation. Luckily, there are some numerical methods to solve this faster (at the cost of accuracy, or what did you expect). The most useful in this case is definitely Monte Carlo integration, which allows less samples from the source material per pixel. There are many articles written about the subject, and a great introduction is found <a title="Spherical Harmonic Lighting" href="http://www.research.scea.com/gdc2003/spherical-harmonic-lighting.pdf" target="_blank">in this article by Robin Green</a>.</p>
<p>Enough to get you cracking, I think <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>&nbsp;</p>
<p><strong>Demo</strong></p>
<p><a title="Away3D Light Probe Demo" href="http://www.derschmale.com/demo/away3d/lightProbes/" target="_blank">See the Cornell Boxness of it all!</a></p>
<p>Use the arrow keys to move the head, space bar to toggle the texture (shows the lighting impact), and finally click and drag to rotate the head.</p>
<p>The head model and textures are still by <a title="Infinite-Realities" href="http://www.ir-ltd.net/" target="_blank">Lee Perry-Smith</a> (who by now must feel pretty awkward appearing in random tech demos). Source for the demo can be found <a href="https://github.com/away3d/away3d-examples-fp11/blob/texturerefactor/src/Intermediate_LightProbes.as" target="_blank">in the texture-refactor branch of the examples repo</a> (which will be merged to master together with the dev branch).</p>
<p>The environment maps were made as outlined before using a custom generation tool, and placed close to each corner of the box where they were generated.  I was using Stage3D (and thus a low dynamic range) to generate them, so I had to do some touch-ups in Photoshop to get them to look right. I&#8217;m not done investigating the options for further development of such map generation tools, but who knows they&#8217;ll make it public some day. In any case, HDRShop is more reliable! Diffuse lighting is set to use light probes only, while specular lighting uses the point light only.</p>
<p>The demo also shows how to use point light shadows, rim lighting and light mapping for added static shading.</p>
<img src="http://feeds.feedburner.com/~r/derschmale/QrhM/~4/qyOUAvdTVKA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.derschmale.com/2011/12/27/convolution-light-probes-in-away3d-4-0/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<feedburner:origLink>http://www.derschmale.com/2011/12/27/convolution-light-probes-in-away3d-4-0/</feedburner:origLink></item>
		<item>
		<title>The Away3D “dev” Branch</title>
		<link>http://feedproxy.google.com/~r/derschmale/QrhM/~3/-XSom6Cl_qg/</link>
		<comments>http://www.derschmale.com/2011/12/27/the-away3d-dev-branch/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 13:13:36 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[Flash Player 11]]></category>
		<category><![CDATA[gpu]]></category>
		<category><![CDATA[Stage3D]]></category>
		<category><![CDATA[updates]]></category>

		<guid isPermaLink="false">http://www.derschmale.com/?p=549</guid>
		<description><![CDATA[If you&#8217;ve been following the updates on the Away3D blog, you&#8217;ll have noticed the updated roadmap mentioning the dev branch on Github. This branch is basically where we&#8217;re working to get things towards Beta, so take a look if you&#8217;re curious to see where we&#8217;re headed. I personally advise anyone to use this branch if [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve been following the updates on the Away3D blog, you&#8217;ll have noticed the <a title="Updated roadmap for Away3D 4.0" href="http://away3d.com/comments/updated_roadmap_for_away3d_4.0" target="_blank">updated roadmap</a> mentioning the <a title="Away3D dev branch" href="https://github.com/away3d/away3d-core-fp11/tree/dev" target="_blank">dev branch</a> on Github. This branch is basically where we&#8217;re working to get things towards Beta, so take a look if you&#8217;re curious to see where we&#8217;re headed. I personally advise anyone to use this branch if you&#8217;re starting out with a new project but <strong>make a snapshot</strong> of the revision you&#8217;re working with. Some things will still change and might very well break your code <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>We&#8217;ll put up some proper blog posts introducing new features as we get to Beta, but for now I&#8217;ll quickly explain what I&#8217;ve personally been working on, and how I broke your existing code <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  I&#8217;ve also went ahead and updated the source code for the demos in previous blog posts to match the dev branch, so you can see how it differs concretely.</p>
<p>&nbsp;</p>
<p><strong>Death to BitmapMaterial! All hail TextureMaterial!</strong></p>
<p>One of the biggest changes, I think. Favouring composition over inheritance, we deprecated <em>BitmapMaterial</em>, <em>VideoMaterial</em>, <em>MovieMaterial</em> etc. They have been replaced by a single<em> TextureMaterial</em>, which accepts a <em>Texture2DBase</em> object. In practice, this will for instance be a <em>BitmapTexture</em> (<em>VideoTexture</em> etc are still in development). This way, when supporting new texture types you don&#8217;t need to extend every type of material that needs to extend this material type, which is especially a mess when you start creating custom materials (and need to extend/duplicate those to support all possible texture types). The same applies for cube maps (fe: <em>BitmapCubeTexture</em>), the normal and specular maps that are set on the default materials, and the view&#8217;s backgroundImage (now simply <em>View3D::background</em>). It&#8217;s a big change that might annoy many of you, but it&#8217;s necessary. With things like ATF support coming up (and who knows what else), things would simply become unmanageable and error-prone otherwise.</p>
<p>Furthermore, there are some &#8220;special&#8221; convenience textures that can be used for the default materials. For instance, these materials expect a specular map to have the specular strength on the red channel, and the specular power (gloss) on the green. <em>SpecularBitmapTexture</em> allows you to pass two greyscale <em>BitmapData</em> objects (gloss is optional) and composites the data correctly. When using texture splatting, you can use <em>SplatBlendBitmapTexture</em>, which takes 3 <em>BitmapData</em> objects containing the blending value for each splatting layer and places each on the red, blue and green channels as expected by <em>TerrainDiffuseMethod</em>. However, in both cases, it&#8217;s usually better to do these things in your offline asset preparation stage and use simpler texture objects instead.</p>
<p>&nbsp;</p>
<p><strong>Light Picking</strong></p>
<p>Another rather big change for those working with lights, but one that offers great benefits. Instead of assigning the lights as a static array as before, materials now expect a <em>LightPickerBase</em> object. Right now, that means <em>StaticLightPicker</em>, which in turn accepts the array of lights. Why this extra object? This way, you can create a <em>DynamicLightPicker</em> object that selects the most important lights from the <em>EntityCollector, </em>allowing different lights to be used without reassigning the light array (and as a result potentially invalidating the material). As light collection is often game-engine dependent, providing our own dynamic light picker is not the highest priority. At least it&#8217;s good to know the possibility is there, right? <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>&nbsp;</p>
<p><strong>Disposal</strong></p>
<p>Before, most <em>dispose</em>(&#8230;) methods accepted a &#8220;deep&#8221; parameter, which caused &#8220;sub-assets&#8221; (<em>Geometry</em> of a <em>Mesh</em>, <em>BitmapData</em> of a <em>BitmapTexture</em>, etc) to be disposed as well. While in some cases this saves you from writing a trivial few lines of disposal code, in many cases it just is an open door to invalid state (accidentally disposing a <em>BitmapData</em> used by another <em>BitmapTexture</em>) and ambiguity (what will be cleared, and what will remain uncleaned?). Allowing an object to destroy objects it did not create is poor resource management and generally bad practice in my opinion. Hence the new credo: you clean up what you create!</p>
<p>&nbsp;</p>
<p><strong>Ambient lighting</strong></p>
<p>There have been some changes in how ambient lighting works. Before, the <em>ambient </em>and <em>ambientColor</em> of the materials dictated how much ambient light is coming from the lights, rather than how much ambient light is actually reflected by the material. That has been changed, and lights now have <em>ambient</em> and <em>ambientColor</em> properties as well. As a result, ambient light now functions similar to diffuse and specular light. The lights <em>emit</em> it, the material <em>reflects</em> it. It&#8217;s usually best to only set ambient light properties on <em>DirectionalLight</em> objects, since ambient light is global. <em>PointLight </em>objects can get culled if they&#8217;re not affecting anything in the frustum and their ambient contribution would be dropped as a result.</p>
<p>&nbsp;</p>
<p><strong>Other changes and new features</strong></p>
<ul>
<li>Big refactor on how the materials and animations are compiled to facilitate building custom materials</li>
<li>Slight restructuring of the render flow to allow more intricate custom renderers</li>
<li>Light probes (default materials only support convoluted cube maps at this point)</li>
<li>Reduced shadow swimming with shadow mapping</li>
<li><em>RefractionEnvMapMethod</em> for environment-map based refraction mapping</li>
<li><em>AnisotropicSpecularMethod </em>for <a title="Heidrich–Seidel anisotropic distribution" href="http://en.wikipedia.org/wiki/Specular_highlight#Heidrich.E2.80.93Seidel_anisotropic_distribution" target="_blank">anisotropic specular highlights</a></li>
<li>Shadow mapping for point lights (<em>HardShadowMapMethod</em> only)</li>
<li><em>AlphaMaskMethod</em> to influence the material&#8217;s alpha, optionally based on a secondary UV set</li>
<li>Light map methods: <em>DiffuseLightMapMethod</em> to apply light maps as diffuse light, and <em>LightMapMethod</em> to apply it on the final shaded result</li>
<li><em>WrapDiffuseMethod </em>to perform a very cheap fake way for subsurface scattering, as outlined in <a href="http://http.developer.nvidia.com/GPUGems/gpugems_ch16.html" target="_blank">GPU Gems #1</a></li>
</ul>
<div>I hope to scrape together some time to write some things about these new features as soon as possible. A recurring promise, I know, but bear with me here <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </div>
<div><strong>Update: </strong>Forgot to mention that there are also updated official Away3D demos in the examples&#8217; <a title="texture-refactor branch" href="https://github.com/away3d/away3d-examples-fp11/tree/texturerefactor">texture-refactor</a> branch (the name is due to a previous branch).</div>
<img src="http://feeds.feedburner.com/~r/derschmale/QrhM/~4/-XSom6Cl_qg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.derschmale.com/2011/12/27/the-away3d-dev-branch/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.derschmale.com/2011/12/27/the-away3d-dev-branch/</feedburner:origLink></item>
		<item>
		<title>Away3D 4.0 Demo for Flash on the Beach 2011</title>
		<link>http://feedproxy.google.com/~r/derschmale/QrhM/~3/uaOEfv9CzjY/</link>
		<comments>http://www.derschmale.com/2011/10/06/away3d-4-0-demo-for-flash-on-the-beach-2011/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 15:26:53 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[Cube map]]></category>
		<category><![CDATA[dynamic reflections]]></category>
		<category><![CDATA[environment mapping]]></category>
		<category><![CDATA[Flash Player 11]]></category>
		<category><![CDATA[Molehill]]></category>
		<category><![CDATA[refractions]]></category>
		<category><![CDATA[Stage3D]]></category>

		<guid isPermaLink="false">http://www.derschmale.com/?p=539</guid>
		<description><![CDATA[Oh such busy times! I&#8217;m not keeping up with my planned blog posts as much as I&#8217;d like, but between work and Away3D 4.0 updates there&#8217;s not as much time left to actually write about it. But, I did want to share a video from my presentation on Flash on the Beach 2011, showcasing some [...]]]></description>
			<content:encoded><![CDATA[<p>Oh such busy times! I&#8217;m not keeping up with my planned blog posts as much as I&#8217;d like, but between work and Away3D 4.0 updates there&#8217;s not as much time left to actually write about it. But, I did want to share a video from my presentation on Flash on the Beach 2011, showcasing some of the things that can be done relatively easily with Away3D 4.0 by compositing several shadow methods and so on. It also showcases some tricks that aren&#8217;t in the engine <em>yet</em>, but will be after some refactoring. It&#8217;s very much a coder&#8217;s demo, so don&#8217;t expect too much artistic sense <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p><iframe src="http://player.vimeo.com/video/30135275" width="500" height="281" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe></p>
<p>Some of the things shown that I&#8217;d like the point out:</p>
<ul>
<li>Subsurface scattering for ice and gelatinous materials</li>
<li>Large amount of independently moving scene graph objects (1000 cubes)</li>
<li>Terrain texture splatting</li>
<li>Partial shadow mapping for large view frustra (coming soon)</li>
<li>Dynamic cube map refraction + colour aberration (coming soon)</li>
<li>Dynamic cube map reflections (coming soon)</li>
</ul>
<p>I&#8217;ll elaborate on some more once they&#8217;re available in the engine, I promise! <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The Away3D team also recently joined forces with Evoflash (in particular <a href="http://www.simppa.fi/blog/" target="_blank">Simo</a>) again to create a real scene demo for Adobe MAX. Read all about that one <a href="http://www.simppa.fi/blog/sparkle-a-moment/" target="_blank">Simo&#8217;s blog</a>. Great job guys!</p>
<img src="http://feeds.feedburner.com/~r/derschmale/QrhM/~4/uaOEfv9CzjY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.derschmale.com/2011/10/06/away3d-4-0-demo-for-flash-on-the-beach-2011/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.derschmale.com/2011/10/06/away3d-4-0-demo-for-flash-on-the-beach-2011/</feedburner:origLink></item>
		<item>
		<title>Building efficient content in Away3D 4.0: Sharing Materials &amp; Geometries</title>
		<link>http://feedproxy.google.com/~r/derschmale/QrhM/~3/PN0DOR8Ff54/</link>
		<comments>http://www.derschmale.com/2011/07/25/building-efficient-content-in-away3d-4-0-sharing-materials-geometries/#comments</comments>
		<pubDate>Mon, 25 Jul 2011 19:42:29 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Away3D]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[Molehill]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[scene graph]]></category>
		<category><![CDATA[Stage3D]]></category>

		<guid isPermaLink="false">http://www.derschmale.com/?p=508</guid>
		<description><![CDATA[Time to dig deeper into Away3D 4.0 and see how you can structure and manage your content to get the best performance! If you haven&#8217;t read the previous posts, it wouldn&#8217;t hurt to do so now, since this one will be all about Materials (on the Mesh side of things) and Geometries. In any sort [...]]]></description>
			<content:encoded><![CDATA[<p>Time to dig deeper into Away3D 4.0 and see how you can structure and manage your content to get the best performance! If you haven&#8217;t read the previous posts, it wouldn&#8217;t hurt to do so now, since this one will be all about Materials (on the Mesh side of things) and Geometries.</p>
<p>In any sort of project, if you have data collections that use a lot of memory, it makes sense not to duplicate them if you want to share the same data across several clients (<em>clients</em> simply meaning &#8220;users of the data&#8221;). Instead, you make all clients refer to the same data object, or <em>model</em>. Similarly, you probably wouldn&#8217;t want to perform the same expensive operations on this data for every client. The shared model can take care of that only once for all clients. In the Away3D scene graph structure, the Geometry is essentially your model, and a Mesh refers to it. Many Meshes can make use of the same Geometry. Similarly, Materials can be shared across Meshes as well. This all sounds very logical, yet for <em>some reason </em>I can&#8217;t fathom, this is a principle that&#8217;s often violated against. Even though Away3D internally tries to minimize memory use and tries to share things as best as it can (it caches Program3D instances, Texture objects, etc., across material instances), I can&#8217;t stress this enough: <strong>reuse Material and Geometry instances whenever possible. </strong>Not only does it limit CPU and GPU memory usage, it&#8217;s also essential for performance. I&#8217;ll explain why.</p>
<p><strong>Reusing Materials</strong></p>
<p>Every material uses a &#8220;shader program&#8221;, which is represented in Actionscript by a Program3D object. It&#8217;s a combination of a vertex shader and a fragment shader. I won&#8217;t go into the subject of shader programs in detail, but if you&#8217;re interested, <a href="http://en.wikipedia.org/wiki/Shader" target="_blank">check wikipedia</a> for starters. A large task of the shader program is to define the colour of every pixel that&#8217;s being drawn. In other words, it controls how your rendered object will be coloured, textured, lit, shaded, &#8230; If it&#8217;s on your screen, it passed through a shader program. As a result, a different material means it needs a different Program3D. When drawing objects, the gpu needs to be told to use a different program every time for every material. Unfortunately, this switch is typically very expensive and something you&#8217;d want to prevent as much as possible. To this end, Away3D sorts all the objects that need to be rendered (the so called &#8220;renderables&#8221;) primarily according to their materials so all objects with the same material will be rendered consecutively. If you&#8217;re using different material instances for things that look exactly the same, it means more programs need to be switched needlessly.</p>
<div id="attachment_516" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.derschmale.com/blog/wp-content/renderPerMaterial.jpg"><img class="size-medium wp-image-516" title="renderPerMaterial" src="http://www.derschmale.com/blog/wp-content/renderPerMaterial-300x256.jpg" alt="Sharing materials allows more renderables (here SubGeometry objects) to be rendered with less switches" width="300" height="256" /></a><p class="wp-caption-text">Sharing materials allows more renderables (here SubGeometry objects) to be rendered with less switches</p></div>
<p>Okay, I&#8217;ll admit, that isn&#8217;t <em>entirely</em> true. For those interested, I&#8217;ll explain what really happens. Whenever a material is created or changed so that it needs to create a new shader program, the vertex and fragment code is regenerated. A Program3D is requested from a central cache, but if a Program3D object already exists for that exact code combination, the existing object is returned. Different materials using the same code also use the same programs, which means there won&#8217;t be any program switching. However, it&#8217;s easy enough to accidentally have small differences between materials so that the code &#8211; and in turn the program instance &#8211; changes. And the following is still true regardless:</p>
<p>Every time a material changes, certain data needs to be uploaded to the GPU: Textures, lighting properties, &#8230; No caching of Program3D objects is going to prevent that. If you use a new material instance for every object, these uploads may occur for every object. Share a material, and it only happens once for all the renderables using it.</p>
<p>Note: if you really need several material instances, but you&#8217;re using the same BitmapData objects as textures or normal maps, at least try to share those across materials unless you have a very good reason not to. RAM&#8217;s not for wasting! Furthermore, there&#8217;s a limit to the amount of Texture objects (a buffer object representing the image data on the GPU) that can be created, and for every BitmapData, a Texture is created. You wouldn&#8217;t want to go over the limit!</p>
<p><strong>Reusing Geometries</strong></p>
<p>Similarly, Meshes that use the same data should all refer to a single Geometry instance. A very common example is a game in which there are several occurences of the same monster type spread throughout the game map. You can have one &#8220;beer demon&#8221; Geometry, and place many beer demons in your level by making the different Mesh objects refer to that single Geometry instance. Not only does this use much less memory, it also causes less instances of VertexBuffer3D to be created (these are buffers representing the vertex data on the GPU side of things). There&#8217;s a limit on how many can exist at any given time and if you exceed it, an error will be thrown and your game will crash. Sharing Geometry objects can also result in better performance in some cases, because if the same Geometry was used for the previously rendered object, it may not have to reupload its data to the GPU!</p>
<p><strong>Material/Geometry Tandems</strong></p>
<p>In many cases, especially in games, you&#8217;ll have a collection of game object &#8220;types&#8221;, such as different monsters, a barrel, weapon/health/power-up pick-ups. Each of these objects will probably have their own specific Geometry and Material objects. Makes sense, since you typically wouldn&#8217;t want to use a barrel geometry for a monster, or a shotgun texture on a health boost pick-up, would you? This one-on-one mapping is a pretty fortunate situation, because it means that the rendering for these objects can be batched very efficiently. Only when different objects will be rendered, does any material or geometry data need to be uploaded, with the exception of scene-graph-specific data such as transformation matrices.</p>
<div id="attachment_514" class="wp-caption aligncenter" style="width: 310px"><a href="http://www.derschmale.com/blog/wp-content/tandemMatGeom.jpg"><img class="size-medium wp-image-514" title="tandemMatGeom" src="http://www.derschmale.com/blog/wp-content/tandemMatGeom-300x249.jpg" alt="Rendering objects with one-on-one Material/Geometry combinations" width="300" height="249" /></a><p class="wp-caption-text">Rendering objects with one-on-one Material/Geometry combinations</p></div>
<p>Making sure that the same Geometry and Material instances are used across all instances for the same game object type is easy. The Mesh class contains a clone() method that creates a new Mesh instance that refers to the same Geometry and Material. This way, you can build a library of reference Meshes that are not actually added to the scene. Instead, you populate your scene with these clone objects. You can dispose of them whenever they&#8217;re picked up (health pack) or killed (monster), while the reference mesh and its material and geometry can be disposed when everything is unloaded. For this reference library, you can use Away3D&#8217;s AssetLibrary, which will take care of all your loading and management needs <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>For example</strong></p>
<p>Here&#8217;s a comparison:</p>
<p><a href="http://www.derschmale.com/demo/away3d/geomMatReuse/noreuse.html" target="_blank">Demo NOT reusing anything (warning, can be very slow!)</a></p>
<p><a href="http://www.derschmale.com/demo/away3d/geomMatReuse/allreuse.html" target="_blank">Demo reusing both Geometry and Materials</a></p>
<p><a href="http://www.derschmale.com/demo/away3d/geomMatReuse/ReuseDemo.zip" target="_blank">Source for both</a></p>
<p>I&#8217;ll admit, a large cost in the first demo is the creation of the objects rather than their use. This creation is a constant cost, however, and you can see the performance drop drastically over a short time. So&#8230; Enough incentive? <img src='http://www.derschmale.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>&nbsp;</p>
<p><strong>Update</strong></p>
<p>In the &#8220;dev&#8221; branch (and when we go Beta it will also be in the master branch), BitmapMaterial was removed in favour of TextureMaterial, accepting a texture rather than a BitmapData. It goes without saying that you should share these texture objects whenever possible rather than creating new textures with the same content.</p>
<p>All sources for this post have been updated to use said dev branch: <a href="https://github.com/away3d/away3d-core-fp11/tree/dev">https://github.com/away3d/away3d-core-fp11/tree/dev</a></p>
<img src="http://feeds.feedburner.com/~r/derschmale/QrhM/~4/PN0DOR8Ff54" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.derschmale.com/2011/07/25/building-efficient-content-in-away3d-4-0-sharing-materials-geometries/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		<feedburner:origLink>http://www.derschmale.com/2011/07/25/building-efficient-content-in-away3d-4-0-sharing-materials-geometries/</feedburner:origLink></item>
	</channel>
</rss>
