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

<channel>
	<title>ScapeCode</title>
	<atom:link href="http://scapecode.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://scapecode.com</link>
	<description>Anime, tentacles, and software development.</description>
	<lastBuildDate>Thu, 26 Aug 2010 22:32:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Building a SlimDX MiniTriangle sample with Direct3D11 and IronPython</title>
		<link>http://scapecode.com/2010/08/building-a-slimdx-minitriangle-sample-with-direct3d11-and-ironpython/</link>
		<comments>http://scapecode.com/2010/08/building-a-slimdx-minitriangle-sample-with-direct3d11-and-ironpython/#comments</comments>
		<pubDate>Thu, 26 Aug 2010 22:27:21 +0000</pubDate>
		<dc:creator>Washu</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SlimDX]]></category>

		<guid isPermaLink="false">https://scapecode.com/2010/08/building-a-slimdx-minitriangle-sample-with-direct3d11-and-ironpython/</guid>
		<description><![CDATA[I generally don&#8217;t post huge code dumps, mainly because I find them more annoying and less helpful than some books/authors might. But you know, I&#8217;ve been playing with IronPython/SlimDX recently and decided to do up another SlimDX Sample (demonstrating DX11), except in IronPython this time. This will be in the SlimDX samples sometime soon! import [...]]]></description>
			<content:encoded><![CDATA[<p>I generally don&#8217;t post huge code dumps, mainly because I find them more annoying and less helpful than some books/authors might. But you know, I&#8217;ve been playing with IronPython/SlimDX recently and decided to do up another SlimDX Sample (demonstrating DX11), except in IronPython this time. This will be in the SlimDX samples sometime soon!</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> clr
clr.<span style="color: black;">AddReference</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'System.Windows.Forms'</span><span style="color: black;">&#41;</span>
clr.<span style="color: black;">AddReference</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'System.Drawing'</span><span style="color: black;">&#41;</span>
clr.<span style="color: black;">AddReference</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'SlimDX'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> System <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> System.<span style="color: black;">Drawing</span> <span style="color: #ff7700;font-weight:bold;">import</span> Size
<span style="color: #ff7700;font-weight:bold;">from</span> System.<span style="color: black;">Windows</span>.<span style="color: black;">Forms</span> <span style="color: #ff7700;font-weight:bold;">import</span> Form, Application, MessageBox, FormBorderStyle
<span style="color: #ff7700;font-weight:bold;">from</span> SlimDX <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> SlimDX.<span style="color: black;">Direct3D11</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> SlimDX.<span style="color: black;">DXGI</span> <span style="color: #ff7700;font-weight:bold;">import</span> SwapChainDescription, SwapChainFlags, ModeDescription, SampleDescription, Usage, SwapEffect, Format, PresentFlags, Factory, WindowAssociationFlags
<span style="color: #ff7700;font-weight:bold;">from</span> SlimDX.<span style="color: black;">D3DCompiler</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #ff7700;font-weight:bold;">from</span> SlimDX.<span style="color: black;">Windows</span> <span style="color: #ff7700;font-weight:bold;">import</span> MessagePump
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> GameObject:
    <span style="color: #ff7700;font-weight:bold;">def</span> Render<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">pass</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> Tick<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> GraphicsDevice<span style="color: black;">&#40;</span>IDisposable<span style="color: black;">&#41;</span>:
    Context = <span style="color: #008000;">property</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: #008000;">self</span>: <span style="color: #008000;">self</span>.<span style="color: black;">context</span><span style="color: black;">&#41;</span>
    Device = <span style="color: #008000;">property</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: #008000;">self</span>: <span style="color: #008000;">self</span>.<span style="color: black;">device</span><span style="color: black;">&#41;</span>
    SwapChain = <span style="color: #008000;">property</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: #008000;">self</span>: <span style="color: #008000;">self</span>.<span style="color: black;">swapChain</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, control, fullscreen<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">fullscreen</span> = fullscreen
        <span style="color: #008000;">self</span>.<span style="color: black;">control</span> = control
&nbsp;
        control.<span style="color: black;">Resize</span> += <span style="color: #ff7700;font-weight:bold;">lambda</span> sender, args: <span style="color: #008000;">self</span>.<span style="color: black;">Resize</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        swapChainDesc = <span style="color: #008000;">self</span>.<span style="color: black;">CreateSwapChainDescription</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
        success,<span style="color: #008000;">self</span>.<span style="color: black;">device</span>,<span style="color: #008000;">self</span>.<span style="color: black;">swapChain</span> = Device.<span style="color: black;">CreateWithSwapChain</span><span style="color: black;">&#40;</span>DriverType.<span style="color: black;">Hardware</span>, DeviceCreationFlags.<span style="color: #008000;">None</span>, Array<span style="color: black;">&#91;</span>FeatureLevel<span style="color: black;">&#93;</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>FeatureLevel.<span style="color: black;">Level_11_0</span>, FeatureLevel.<span style="color: black;">Level_10_1</span>, FeatureLevel.<span style="color: black;">Level_10_0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, swapChainDesc<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">context</span> = <span style="color: #008000;">self</span>.<span style="color: black;">Device</span>.<span style="color: black;">ImmediateContext</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">self</span>.<span style="color: black;">swapChain</span>.<span style="color: black;">GetParent</span><span style="color: black;">&#91;</span>Factory<span style="color: black;">&#93;</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> factory:
            factory.<span style="color: black;">SetWindowAssociation</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">control</span>.<span style="color: black;">Handle</span>, WindowAssociationFlags.<span style="color: black;">IgnoreAll</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">with</span> Resource.<span style="color: black;">FromSwapChain</span><span style="color: black;">&#91;</span>Texture2D<span style="color: black;">&#93;</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">swapChain</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> backBuffer:
            <span style="color: #008000;">self</span>.<span style="color: black;">backBufferRTV</span> = RenderTargetView<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">Device</span>, backBuffer<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">Resize</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>        
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> CreateSwapChainDescription<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        swapChainDesc = SwapChainDescription<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        swapChainDesc.<span style="color: black;">IsWindowed</span> = <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>.<span style="color: black;">fullscreen</span>
        swapChainDesc.<span style="color: black;">BufferCount</span> = <span style="color: #ff4500;">1</span>
        swapChainDesc.<span style="color: black;">ModeDescription</span> = ModeDescription<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">control</span>.<span style="color: black;">ClientSize</span>.<span style="color: black;">Width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">control</span>.<span style="color: black;">ClientSize</span>.<span style="color: black;">Height</span>, Rational<span style="color: black;">&#40;</span><span style="color: #ff4500;">60</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>, Format.<span style="color: black;">R8G8B8A8_UNorm</span><span style="color: black;">&#41;</span>
        swapChainDesc.<span style="color: black;">Flags</span> = SwapChainFlags.<span style="color: #008000;">None</span>
        swapChainDesc.<span style="color: black;">SwapEffect</span> = SwapEffect.<span style="color: black;">Discard</span>
        swapChainDesc.<span style="color: black;">Usage</span> = Usage.<span style="color: black;">RenderTargetOutput</span>
        swapChainDesc.<span style="color: black;">SampleDescription</span> = SampleDescription<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        swapChainDesc.<span style="color: black;">OutputHandle</span> = <span style="color: #008000;">self</span>.<span style="color: black;">control</span>.<span style="color: black;">Handle</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> swapChainDesc
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> Resize<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">Context</span>.<span style="color: black;">ClearState</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">backBufferRTV</span>.<span style="color: black;">Dispose</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">swapChain</span>.<span style="color: black;">ResizeBuffers</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #008000;">self</span>.<span style="color: black;">control</span>.<span style="color: black;">ClientSize</span>.<span style="color: black;">Width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">control</span>.<span style="color: black;">ClientSize</span>.<span style="color: black;">Height</span>, Format.<span style="color: black;">R8G8B8A8_UNorm</span>, SwapChainFlags.<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">with</span> Resource.<span style="color: black;">FromSwapChain</span><span style="color: black;">&#91;</span>Texture2D<span style="color: black;">&#93;</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">swapChain</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> backBuffer:
            <span style="color: #008000;">self</span>.<span style="color: black;">backBufferRTV</span> = RenderTargetView<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">Device</span>, backBuffer<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Context</span>.<span style="color: black;">Rasterizer</span>.<span style="color: black;">SetViewports</span><span style="color: black;">&#40;</span>Viewport<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #008000;">self</span>.<span style="color: black;">control</span>.<span style="color: black;">ClientSize</span>.<span style="color: black;">Width</span>, <span style="color: #008000;">self</span>.<span style="color: black;">control</span>.<span style="color: black;">ClientSize</span>.<span style="color: black;">Height</span>, <span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> BeginRender<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">Context</span>.<span style="color: black;">ClearRenderTargetView</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">backBufferRTV</span>, Color4<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">Context</span>.<span style="color: black;">OutputMerger</span>.<span style="color: black;">SetTargets</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">backBufferRTV</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> EndRender<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">swapChain</span>.<span style="color: black;">Present</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, PresentFlags.<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> Dispose<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">backBufferRTV</span>.<span style="color: black;">Dispose</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">swapChain</span>.<span style="color: black;">Dispose</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">device</span>.<span style="color: black;">Dispose</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> TriangleObject<span style="color: black;">&#40;</span>GameObject<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, game<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">game</span> = game
        device = game.<span style="color: black;">GraphicsDevice</span>.<span style="color: black;">Device</span>
        context = game.<span style="color: black;">GraphicsDevice</span>.<span style="color: black;">Context</span>
&nbsp;
        err = clr.<span style="color: black;">Reference</span><span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#93;</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">with</span> ShaderBytecode.<span style="color: black;">CompileFromFile</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SimpleTriangle10.fx&quot;</span>, <span style="color: #483d8b;">&quot;fx_5_0&quot;</span>, ShaderFlags.<span style="color: #008000;">None</span>, EffectFlags.<span style="color: #008000;">None</span>, <span style="color: #008000;">None</span>, <span style="color: #008000;">None</span>, err<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> shaderByteCode:
            <span style="color: #008000;">self</span>.<span style="color: black;">effect</span> = Effect<span style="color: black;">&#40;</span>device, shaderByteCode<span style="color: black;">&#41;</span>
&nbsp;
        shaderTechnique = <span style="color: #008000;">self</span>.<span style="color: black;">effect</span>.<span style="color: black;">GetTechniqueByIndex</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">shaderPass</span> = shaderTechnique.<span style="color: black;">GetPassByIndex</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
        sig = <span style="color: #008000;">self</span>.<span style="color: black;">shaderPass</span>.<span style="color: black;">Description</span>.<span style="color: black;">Signature</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">inputLayout</span> = InputLayout<span style="color: black;">&#40;</span>device, sig, Array<span style="color: black;">&#91;</span>InputElement<span style="color: black;">&#93;</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>InputElement<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;POSITION&quot;</span>, <span style="color: #ff4500;">0</span>, Format.<span style="color: black;">R32G32B32A32_Float</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>, InputElement<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;COLOR&quot;</span>, <span style="color: #ff4500;">0</span>, Format.<span style="color: black;">R32G32B32A32_Float</span>, <span style="color: #ff4500;">16</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        bufferDesc = BufferDescription<span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">32</span>, ResourceUsage.<span style="color: black;">Dynamic</span>, BindFlags.<span style="color: black;">VertexBuffer</span>, CpuAccessFlags.<span style="color: black;">Write</span>, ResourceOptionFlags.<span style="color: #008000;">None</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">vertexBuffer</span> = Buffer<span style="color: black;">&#40;</span>device, bufferDesc<span style="color: black;">&#41;</span>
&nbsp;
        stream = context.<span style="color: black;">MapSubresource</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">vertexBuffer</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">3</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">32</span>, MapMode.<span style="color: black;">WriteDiscard</span>, MapFlags.<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>.<span style="color: black;">Data</span>
        data = Array<span style="color: black;">&#91;</span>Vector4<span style="color: black;">&#93;</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>
            Vector4<span style="color: black;">&#40;</span><span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">0.5</span>, <span style="color: #ff4500;">0.5</span>, <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span>, Vector4<span style="color: black;">&#40;</span><span style="color: #ff4500;">1.0</span>, <span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span>,
            Vector4<span style="color: black;">&#40;</span><span style="color: #ff4500;">0.5</span>, -<span style="color: #ff4500;">0.5</span>, <span style="color: #ff4500;">0.5</span>, <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span>, Vector4<span style="color: black;">&#40;</span><span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">1.0</span>, <span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span>,
            Vector4<span style="color: black;">&#40;</span>-<span style="color: #ff4500;">0.5</span>, -<span style="color: #ff4500;">0.5</span>, <span style="color: #ff4500;">0.5</span>, <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span>, Vector4<span style="color: black;">&#40;</span><span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">0.0</span>, <span style="color: #ff4500;">1.0</span>, <span style="color: #ff4500;">1.0</span><span style="color: black;">&#41;</span>
        <span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        stream.<span style="color: black;">WriteRange</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>
        context.<span style="color: black;">UnmapSubresource</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">vertexBuffer</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> Render<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        context = <span style="color: #008000;">self</span>.<span style="color: black;">game</span>.<span style="color: black;">GraphicsDevice</span>.<span style="color: black;">Context</span>
        context.<span style="color: black;">InputAssembler</span>.<span style="color: black;">InputLayout</span> = <span style="color: #008000;">self</span>.<span style="color: black;">inputLayout</span>
        context.<span style="color: black;">InputAssembler</span>.<span style="color: black;">PrimitiveTopology</span> = PrimitiveTopology.<span style="color: black;">TriangleList</span>
        context.<span style="color: black;">InputAssembler</span>.<span style="color: black;">SetVertexBuffers</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, VertexBufferBinding<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">vertexBuffer</span>, <span style="color: #ff4500;">32</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">shaderPass</span>.<span style="color: black;">Apply</span><span style="color: black;">&#40;</span>context<span style="color: black;">&#41;</span>
        context.<span style="color: black;">Draw</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> Dispose<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">effect</span>.<span style="color: black;">Dispose</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">inputLayout</span>.<span style="color: black;">Dispose</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">vertexBuffer</span>.<span style="color: black;">Dispose</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Game<span style="color: black;">&#40;</span>IDisposable<span style="color: black;">&#41;</span>:
    GraphicsDevice = <span style="color: #008000;">property</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: #008000;">self</span>: <span style="color: #008000;">self</span>.<span style="color: black;">graphicsDevice</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, width, height, fullscreen = <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">fullscreen</span> = fullscreen
        <span style="color: #008000;">self</span>.<span style="color: black;">form</span> = GameForm<span style="color: black;">&#40;</span>width, height, fullscreen<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">form</span>.<span style="color: black;">Visible</span> = <span style="color: #008000;">True</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">graphicsDevice</span> = GraphicsDevice<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">form</span>, <span style="color: #008000;">self</span>.<span style="color: black;">fullscreen</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">gameObjects</span> = <span style="color: black;">&#91;</span>TriangleObject<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> Run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        Application.<span style="color: black;">Idle</span> += <span style="color: #008000;">self</span>.<span style="color: black;">OnIdle</span>
        Application.<span style="color: black;">Run</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">form</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> OnIdle<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, ea, sender<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">while</span> MessagePump.<span style="color: black;">IsApplicationIdle</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">Update</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">Render</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> Update<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">gameObjects</span>:
            i.<span style="color: black;">Tick</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> Render<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">GraphicsDevice</span>.<span style="color: black;">BeginRender</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">gameObjects</span>:
            i.<span style="color: black;">Render</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">GraphicsDevice</span>.<span style="color: black;">EndRender</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> Dispose<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">GraphicsDevice</span>.<span style="color: black;">Dispose</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">gameObjects</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'Dispose'</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">dir</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>:
                i.<span style="color: black;">Dispose</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">form</span>.<span style="color: black;">Dispose</span><span style="color: black;">&#40;</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> GameForm<span style="color: black;">&#40;</span>Form<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, width, height, fullscreen<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">ClientSize</span> = Size<span style="color: black;">&#40;</span>width, height<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> fullscreen:
            <span style="color: #008000;">self</span>.<span style="color: black;">FormBorderStyle</span> = FormBorderStyle.<span style="color: #008000;">None</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        <span style="color: #ff7700;font-weight:bold;">with</span> Game<span style="color: black;">&#40;</span><span style="color: #ff4500;">640</span>, <span style="color: #ff4500;">480</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> game:
            game.<span style="color: black;">Run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span> <span style="color: #ff7700;font-weight:bold;">as</span> e:
        MessageBox.<span style="color: black;">Show</span><span style="color: black;">&#40;</span>e.<span style="color: black;">ToString</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://scapecode.com/2010/08/building-a-slimdx-minitriangle-sample-with-direct3d11-and-ironpython/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is It Really A Bug For A Beginner To Be Using C-Strings In C++?</title>
		<link>http://scapecode.com/2010/04/is-it-really-a-bug-for-a-beginner-to-be-using-c-string-in-c/</link>
		<comments>http://scapecode.com/2010/04/is-it-really-a-bug-for-a-beginner-to-be-using-c-string-in-c/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 01:54:25 +0000</pubDate>
		<dc:creator>Washu</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C-strings]]></category>
		<category><![CDATA[The C++ Standard Library]]></category>

		<guid isPermaLink="false">http://scapecode.com/2010/04/is-it-really-a-bug-for-a-beginner-to-be-using-c-string-in-c/</guid>
		<description><![CDATA[Depends, but probably yes. A beginning programmer should be focusing on learning to program. That is: the process of taking a concept and turning it into an application. Problem solving, in other words. Learning to program is not the same thing as learning a programming language. Learning a programming language is about learning the syntax [...]]]></description>
			<content:encoded><![CDATA[<p>Depends, but probably yes.</p>
<p>A beginning programmer should be focusing on learning to program. That is: the process of taking a concept and turning it into an application. Problem solving, in other words. Learning to program is not the same thing as learning a programming language. Learning a programming language is about learning the syntax and standard library that comes with said programming language, it may involve the process of problem solving, but that is not its primary concern.</p>
<p>Given that, one can quickly see that the best way to introduce a beginning programmer to programming is to get them to use a language that is quick and easy to get up and running in. There are many languages that are quick and easy to get up and running in, and they all tend to share a rather similar component… which is verbosity. <a href="http://python.org/">Python</a> and <a href="http://www.ruby-lang.org/en/">Ruby</a> are two prime examples, both of which have a very simple language syntax which allows for a lot of leeway for the programmer, without all the extra clutter that many other languages have (C++ *cough*). Another good choice, in my opinion, is C# which, when combined with <a href="http://msdn.microsoft.com/en-us/vcsharp/default.aspx">Microsoft Visual C#</a>, provides a very robust but easy to learn language. These languages all have many key features which make them easy to learn and use: All of them are generally garbage collected, they all have fairly simple syntax with few (if any) corner cases, and all of them have huge standard libraries that provide for a great deal of quick and easy to use functionality with minimal programmer effort.</p>
<p>C++ has almost none of those things. While there is <a href="http://msdn.microsoft.com/en-us/visualc/default.aspx">Visual Studio</a> for it, the IntelliSense is still not perfect, even with the help of tools like <a href="http://www.wholetomato.com/">WholeTomato’s VAX</a>. The standard library is quite small, dealing mainly with file and console IO, and some minimal containers. It leaves the rest of the work up to the developer. This means that for any sufficiently complex project you will either end up implementing a majority of the behaviors needed yourself, or having to dig up third party libraries and APIs for said behavior. Even the recent C++0x work hasn’t really alleviated the problem. Then you have the language complexity of which I’ve <a href="http://scapecode.com/2009/10/a-simple-c-quiz/">commented on previously</a>…</p>
<p>However, the C++ standard library does provide some features that should be in every developers pocketbook… such as std::string. std::string behaves a lot more like what a beginning programmer expects a primitive type to work. They’ve learned that you can add integers and floats together, so why can’t they add strings together? Well, with std::string they can, but with c-strings they can’t. They’ve learned to compare integers and floats using the standard == operator, so why can’t they do that with strings? With std::string they can, but with c-strings they can’t (well, they “can”, but the behavior is not what they want). They’ve learned how to read in integers and floats from std::cin, so why can’t they do the same with strings? They can with std::string, but with c-strings they have to be careful of the length and also that they’ve pre-allocated it, which has hazards of its own… such as stack space issues when they try to create a char array of 5000 characters.</p>
<p>C-strings do not behave intuitively. They have no inherit length, instead preferring to use null terminators to indicate the end of the string. They cannot be trivially concatenated, instead requiring the user to ensure that appropriate space is available, and then they have to use various function calls to copy the string, and then they have to ensure that those string functions had the space required to copy the null terminator (which the strncpy and other functions MAY omit if there isn’t enough space in the destination). Comparison requires the use functionality like strcmp, which doesn’t return true/false, but instead returns an integer indicating the string difference, with 0 being no differences. In a language where the user has been taught that 0/null generally means failure, remembering to test for 0 in that one off corner case is rather strange.</p>
<p>For a beginner, all that strangeness doesn’t equate to extra power or better performance. Instead it equates to extra confusion, and strange crashes. Had they been taught std::string first, they would have been free and clear, able to use the familiar operators they are used to, while being safe and secure in the bosom that is std::string. In fact, it generally gets worst than that, as c-strings are usually taught before pointers! This makes it even more confusing for the poor beginner, because then they’re introduced to arrays and pointers (instead of say std::vector), and now have a whole slew of new functionality to basically kill themselves with. </p>
<p>Thus, in conclusion, if you see a c-string in a beginners code, it probably means they have a bug somewhere in their code.</p>
]]></content:encoded>
			<wfw:commentRss>http://scapecode.com/2010/04/is-it-really-a-bug-for-a-beginner-to-be-using-c-string-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SlimDX Direct3D10 X Loader</title>
		<link>http://scapecode.com/2009/11/slimdx-direct3d10-x-loader/</link>
		<comments>http://scapecode.com/2009/11/slimdx-direct3d10-x-loader/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 22:49:10 +0000</pubDate>
		<dc:creator>Washu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://scapecode.com/?p=127</guid>
		<description><![CDATA[Here&#8217;s a useful class for loading X files (using SlimDX) into a Direct3D10 Mesh object. This is based off of Jack Hoxley&#8217;s C++ code from his journal post on GameDev.Net. A few things to note about it: It doesn&#8217;t handle multiple materials (or materials at all). To handle that would require you to be sure [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a useful class for loading X files (using SlimDX) into a Direct3D10 Mesh object. This is based off of Jack Hoxley&#8217;s C++ code from his journal post on GameDev.Net.</p>
<p>A few things to note about it: It doesn&#8217;t handle multiple materials (or materials at all). To handle that would require you to be sure to optimize the D3D9 mesh in place, then harvest the EffectInstance&#8217;s and also the materials. That way you could load the appropriate textures and bind them during rendering of the appropriate attributes. For simple X meshes this isn&#8217;t an issue, but some (like the Airplane model that comes with the DirectX SDK) have multiple textures.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Windows.Forms</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">DXGI </span><span style="color: #008000;">=</span> SlimDX.<span style="color: #0000FF;">DXGI</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">D3D9 </span><span style="color: #008000;">=</span> SlimDX.<span style="color: #0000FF;">Direct3D9</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">SlimDX.Direct3D10</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> XMeshLoader <span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">class</span> XLoader <span style="color: #008000;">:</span> IDisposable <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">public</span> XLoader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            CreateNullDevice<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#region IDisposable</span>
        ~XLoader<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            Dispose<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            Dispose<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">bool</span> disposeManagedObjects<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>disposeManagedObjects<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                device9.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                form.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #008080;">#endregion</span>
&nbsp;
        <span style="color: #0600FF;">public</span> Mesh CreateMesh<span style="color: #000000;">&#40;</span>Device device, D3D9.<span style="color: #0000FF;">Mesh</span> mesh9, <span style="color: #0600FF;">out</span> InputElement<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> outDecls<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            var inDecls <span style="color: #008000;">=</span> mesh9.<span style="color: #0000FF;">GetDeclaration</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            outDecls <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> InputElement<span style="color: #000000;">&#91;</span>inDecls.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
            ConvertDecleration<span style="color: #000000;">&#40;</span>inDecls, outDecls<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            var flags <span style="color: #008000;">=</span> MeshFlags.<span style="color: #0000FF;">None</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>mesh9.<span style="color: #0000FF;">CreationOptions</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;</span> D3D9.<span style="color: #0000FF;">MeshFlags</span>.<span style="color: #0000FF;">Use32Bit</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                flags <span style="color: #008000;">=</span> MeshFlags.<span style="color: #0000FF;">Has32BitIndices</span><span style="color: #008000;">;</span>
&nbsp;
            var mesh <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Mesh<span style="color: #000000;">&#40;</span>device, outDecls, D3D9.<span style="color: #0000FF;">DeclarationUsage</span>.<span style="color: #0000FF;">Position</span>.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToUpper</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, mesh9.<span style="color: #0000FF;">VertexCount</span>, mesh9.<span style="color: #0000FF;">FaceCount</span>, flags<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            ConvertIndexBuffer<span style="color: #000000;">&#40;</span>mesh9, mesh<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ConvertVertexBuffer<span style="color: #000000;">&#40;</span>mesh9, mesh<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            ConfigureAttributeTable<span style="color: #000000;">&#40;</span>mesh9, mesh<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            mesh.<span style="color: #0000FF;">GenerateAdjacencyAndPointRepresentation</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            mesh.<span style="color: #0000FF;">Optimize</span><span style="color: #000000;">&#40;</span>MeshOptimizeFlags.<span style="color: #0000FF;">Compact</span> <span style="color: #008000;">|</span> MeshOptimizeFlags.<span style="color: #0000FF;">AttributeSort</span> <span style="color: #008000;">|</span> MeshOptimizeFlags.<span style="color: #0000FF;">VertexCache</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            mesh.<span style="color: #0000FF;">Commit</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">return</span> mesh<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> Mesh LoadFile<span style="color: #000000;">&#40;</span>Device device, <span style="color: #FF0000;">string</span> filename, <span style="color: #0600FF;">out</span> InputElement<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> outDecls<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var mesh9 <span style="color: #008000;">=</span> D3D9.<span style="color: #0000FF;">Mesh</span>.<span style="color: #0000FF;">FromFile</span><span style="color: #000000;">&#40;</span>device9, filename, D3D9.<span style="color: #0000FF;">MeshFlags</span>.<span style="color: #0000FF;">SystemMemory</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">return</span> CreateMesh<span style="color: #000000;">&#40;</span>device, mesh9, <span style="color: #0600FF;">out</span> outDecls<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #008080;">#region Implementation Details</span>
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ConfigureAttributeTable<span style="color: #000000;">&#40;</span>D3D9.<span style="color: #0000FF;">BaseMesh</span> inMesh, Mesh outMesh<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            var inAttribTable <span style="color: #008000;">=</span> inMesh.<span style="color: #0000FF;">GetAttributeTable</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>inAttribTable <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span> <span style="color: #008000;">||</span> inAttribTable.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                outMesh.<span style="color: #0000FF;">SetAttributeTable</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #008000;">new</span> MeshAttributeRange <span style="color: #000000;">&#123;</span>
                    FaceCount <span style="color: #008000;">=</span> outMesh.<span style="color: #0000FF;">FaceCount</span>,
                    FaceStart <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>,
                    Id <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>,
                    VertexCount <span style="color: #008000;">=</span> outMesh.<span style="color: #0000FF;">VertexCount</span>,
                    VertexStart <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>
                <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">else</span> <span style="color: #000000;">&#123;</span>
                var outAttribTable <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MeshAttributeRange<span style="color: #000000;">&#91;</span>inAttribTable.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>var i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> inAttribTable.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                    outAttribTable<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Id</span> <span style="color: #008000;">=</span> inAttribTable<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AttribId</span><span style="color: #008000;">;</span>
                    outAttribTable<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">FaceCount</span> <span style="color: #008000;">=</span> inAttribTable<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">FaceCount</span><span style="color: #008000;">;</span>
                    outAttribTable<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">FaceStart</span> <span style="color: #008000;">=</span> inAttribTable<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">FaceStart</span><span style="color: #008000;">;</span>
                    outAttribTable<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">VertexCount</span> <span style="color: #008000;">=</span> inAttribTable<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">VertexCount</span><span style="color: #008000;">;</span>
                    outAttribTable<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">VertexStart</span> <span style="color: #008000;">=</span> inAttribTable<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">VertexStart</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                outMesh.<span style="color: #0000FF;">SetAttributeTable</span><span style="color: #000000;">&#40;</span>outAttribTable<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            outMesh.<span style="color: #0000FF;">GenerateAttributeBufferFromTable</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ConvertIndexBuffer<span style="color: #000000;">&#40;</span>D3D9.<span style="color: #0000FF;">BaseMesh</span> inMesh, Mesh outMesh<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var inStream <span style="color: #008000;">=</span> inMesh.<span style="color: #0000FF;">LockIndexBuffer</span><span style="color: #000000;">&#40;</span>D3D9.<span style="color: #0000FF;">LockFlags</span>.<span style="color: #0000FF;">None</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var outBuffer <span style="color: #008000;">=</span> outMesh.<span style="color: #0000FF;">GetIndexBuffer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var outStream <span style="color: #008000;">=</span> outBuffer.<span style="color: #0000FF;">Map</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                    <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>outMesh.<span style="color: #0000FF;">Flags</span> <span style="color: #008000;">&amp;</span>amp<span style="color: #008000;">;</span> MeshFlags.<span style="color: #0000FF;">Has32BitIndices</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
                        outStream.<span style="color: #0000FF;">WriteRange</span><span style="color: #000000;">&#40;</span>inStream.<span style="color: #0000FF;">ReadRange</span><span style="color: #000000;">&#40;</span>inMesh.<span style="color: #0000FF;">FaceCount</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                    <span style="color: #0600FF;">else</span>
                        outStream.<span style="color: #0000FF;">WriteRange</span><span style="color: #000000;">&#40;</span>inStream.<span style="color: #0000FF;">ReadRange</span><span style="color: #000000;">&#40;</span>inMesh.<span style="color: #0000FF;">FaceCount</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                outBuffer.<span style="color: #0000FF;">Unmap</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            inMesh.<span style="color: #0000FF;">UnlockIndexBuffer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ConvertVertexBuffer<span style="color: #000000;">&#40;</span>D3D9.<span style="color: #0000FF;">BaseMesh</span> inMesh, Mesh outMesh<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var inStream <span style="color: #008000;">=</span> inMesh.<span style="color: #0000FF;">LockVertexBuffer</span><span style="color: #000000;">&#40;</span>D3D9.<span style="color: #0000FF;">LockFlags</span>.<span style="color: #0000FF;">None</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var outBuffer <span style="color: #008000;">=</span> outMesh.<span style="color: #0000FF;">GetVertexBuffer</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var outStream <span style="color: #008000;">=</span> outBuffer.<span style="color: #0000FF;">Map</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                    outStream.<span style="color: #0000FF;">WriteRange</span><span style="color: #000000;">&#40;</span>inStream.<span style="color: #0000FF;">ReadRange</span><span style="color: #000000;">&#40;</span>inMesh.<span style="color: #0000FF;">VertexCount</span> <span style="color: #008000;">*</span> inMesh.<span style="color: #0000FF;">BytesPerVertex</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
                outBuffer.<span style="color: #0000FF;">Unmap</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
            inMesh.<span style="color: #0000FF;">UnlockIndexBuffer</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> ConvertDecleration<span style="color: #000000;">&#40;</span>D3D9.<span style="color: #0000FF;">VertexElement</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> inDecls, InputElement<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> outDecls<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>var i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&amp;</span>lt<span style="color: #008000;">;</span> inDecls.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                outDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">SemanticName</span> <span style="color: #008000;">=</span> ConvertSemanticName<span style="color: #000000;">&#40;</span>inDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Usage</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                outDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">SemanticIndex</span> <span style="color: #008000;">=</span> inDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">UsageIndex</span><span style="color: #008000;">;</span>
                outDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">AlignedByteOffset</span> <span style="color: #008000;">=</span> inDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Offset</span><span style="color: #008000;">;</span>
                outDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Slot</span> <span style="color: #008000;">=</span> inDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Stream</span><span style="color: #008000;">;</span>
                outDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Classification</span> <span style="color: #008000;">=</span> InputClassification.<span style="color: #0000FF;">PerVertexData</span><span style="color: #008000;">;</span>
                outDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">InstanceDataStepRate</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                outDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Format</span> <span style="color: #008000;">=</span> ConvertFormat<span style="color: #000000;">&#40;</span>inDecls<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Type</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">string</span> ConvertSemanticName<span style="color: #000000;">&#40;</span>D3D9.<span style="color: #0000FF;">DeclarationUsage</span> usage<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">switch</span> <span style="color: #000000;">&#40;</span>usage<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationUsage</span>.<span style="color: #0000FF;">TextureCoordinate</span><span style="color: #008000;">:</span>
                    <span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;TEXCOORD&quot;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationUsage</span>.<span style="color: #0000FF;">PositionTransformed</span><span style="color: #008000;">:</span>
                    <span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;POSITIONT&quot;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationUsage</span>.<span style="color: #0000FF;">TessellateFactor</span><span style="color: #008000;">:</span>
                    <span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;TESSFACTOR&quot;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationUsage</span>.<span style="color: #0000FF;">PointSize</span><span style="color: #008000;">:</span>
                    <span style="color: #0600FF;">return</span> <span style="color: #666666;">&quot;PSIZE&quot;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">default</span><span style="color: #008000;">:</span>
                    <span style="color: #0600FF;">return</span> usage.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToUpper</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> DXGI.<span style="color: #0000FF;">Format</span> ConvertFormat<span style="color: #000000;">&#40;</span>D3D9.<span style="color: #0000FF;">DeclarationType</span> type<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">switch</span> <span style="color: #000000;">&#40;</span>type<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">Float1</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R32_Float</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">Float2</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R32G32_Float</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">Float3</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R32G32B32_Float</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">Float4</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R32G32B32A32_Float</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">Color</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R8G8B8A8_UNorm</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">Ubyte4</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R8G8B8A8_UInt</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">Short2</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R16G16_SInt</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">Short4</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R16G16B16A16_SInt</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">UByte4N</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R8G8B8A8_UNorm</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">Short2N</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R16G16_SNorm</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">Short4N</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R16G16B16A16_SNorm</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">UShort2N</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R16G16_UNorm</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">UShort4N</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R16G16B16A16_UNorm</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">UDec3</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R10G10B10A2_UInt</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">Dec3N</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R10G10B10A2_UNorm</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">HalfTwo</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R16G16_Float</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">case</span> D3D9.<span style="color: #0000FF;">DeclarationType</span>.<span style="color: #0000FF;">HalfFour</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">R16G16B16A16_Float</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">default</span><span style="color: #008000;">:</span> <span style="color: #0600FF;">return</span> DXGI.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">Unknown</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> CreateNullDevice<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            form <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Form<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var direct3D <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> D3D9.<span style="color: #0000FF;">Direct3D</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
                device9 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> D3D9.<span style="color: #0000FF;">Device</span><span style="color: #000000;">&#40;</span>direct3D, <span style="color: #FF0000;">0</span>, D3D9.<span style="color: #0000FF;">DeviceType</span>.<span style="color: #0000FF;">NullReference</span>, form.<span style="color: #0000FF;">Handle</span>, D3D9.<span style="color: #0000FF;">CreateFlags</span>.<span style="color: #0000FF;">HardwareVertexProcessing</span>, <span style="color: #008000;">new</span> D3D9.<span style="color: #0000FF;">PresentParameters</span> <span style="color: #000000;">&#123;</span>
                    BackBufferCount <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>,
                    BackBufferFormat <span style="color: #008000;">=</span> D3D9.<span style="color: #0000FF;">Format</span>.<span style="color: #0000FF;">A8R8G8B8</span>,
                    BackBufferHeight <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>,
                    BackBufferWidth <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>,
                    SwapEffect <span style="color: #008000;">=</span> D3D9.<span style="color: #0000FF;">SwapEffect</span>.<span style="color: #0000FF;">Copy</span>,
                    Windowed <span style="color: #008000;">=</span> <span style="color: #0600FF;">true</span>
                <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">private</span> Form form<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">private</span> D3D9.<span style="color: #0000FF;">Device</span> device9<span style="color: #008000;">;</span>
        <span style="color: #008080;">#endregion</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://scapecode.com/2009/11/slimdx-direct3d10-x-loader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Simple C++ Quiz</title>
		<link>http://scapecode.com/2009/10/a-simple-c-quiz/</link>
		<comments>http://scapecode.com/2009/10/a-simple-c-quiz/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 22:16:35 +0000</pubDate>
		<dc:creator>Washu</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Quizzes]]></category>

		<guid isPermaLink="false">http://scapecode.com/?p=107</guid>
		<description><![CDATA[Recently some people have been pestering me to post back up my C++ quizzes. So…without further ado here is the first one. The answers will be posted later. Given the following three lines of code, answer these questions int* p = new int[10]; int* j = p + 11; int* k = p + 10; [...]]]></description>
			<content:encoded><![CDATA[<p>Recently some people have been pestering me to post back up my C++ quizzes. So…without further ado here is the first one. The answers will be posted later.</p>
<ol>
<li>Given the following three lines of code, answer these questions<br />
<code>int* p = new int[10];<br />
int* j = p + 11;<br />
int* k = p + 10;</code></p>
<ol>
<li>Is the second line well defined behavior?</li>
<li>If the second line is well defined, where does the pointer point to?</li>
<li>What are some of the legal operations that can be performed on the third pointer?</li>
</ol>
</li>
<li>What output should the following lines of code produce?<br />
<code>int a = 10;<br />
std::cout&lt;&lt;a&lt;&lt;a++&lt;&lt;--a;</code></li>
<li>Assuming the function called in the following block of code has no default parameters, and that no operators are overloaded, how many parameters does it take? Which objects are passed to it?<br />
<code>f((a, b, c), d, e, ((g, h), i));</code></li>
<li>Assuming the function called in the following block of code takes an A* and a B*, what is potentially wrong with the code?<br />
<code>f(new A(), new B());</code></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://scapecode.com/2009/10/a-simple-c-quiz/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SlimGen and You, Part ADD EAX, [EAX] of N</title>
		<link>http://scapecode.com/2009/08/slimgen-and-you-part-add-eax-eax-of-n-2/</link>
		<comments>http://scapecode.com/2009/08/slimgen-and-you-part-add-eax-eax-of-n-2/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 17:22:47 +0000</pubDate>
		<dc:creator>Washu</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[SlimDX]]></category>
		<category><![CDATA[SlimGen]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://scapecode.com/?p=73</guid>
		<description><![CDATA[So far I’ve covered how SlimGen works and the difficulties in doing what it does, including calling convention issues that one must be made aware of when writing replacement methods for use with SlimGen. So the next question arises, just how much of a difference can using SlimGen make? Well, a lot of that will [...]]]></description>
			<content:encoded><![CDATA[<p>So far I’ve covered how SlimGen works and the difficulties in doing what it does, including calling convention issues that one must be made aware of when writing replacement methods for use with SlimGen.</p>
<p>So the next question arises, just how much of a difference can using SlimGen make? Well, a lot of that will depend on the developer and their skill level. But we also were pretty curious about this and so we slapped together a test sample that runs through a series of matrix multiplications and times it. It uses three arrays to perform the multiplications, two of the arrays contains 100,000 randomly generated matrixes, with the third being used as the destinations for the results. Both matrix multiplications (the SlimGen one and the .Net one) assume that a source can also be used as a destination, and so they are overlap safe.</p>
<p>The timing results will vary, of course, from machine to machine depending on the processor in the machine, how much ram you have and also on what you’re doing at the time. Running the results against my Phenom 9850 I get:</p>
<pre>Total Matrix Count Per Run:  100,000
Multiply        Total Ticks: 2,001,059
SlimGenMultiply Total Ticks: 1,269,200
Improvement:                 36.57 % </pre>
<p>While when I run it against my T8300 Core2 Duo laptop I get:</p>
<pre>Total Matrix Count Per Run:  100,000
Multiply        Total Ticks: 2,175,380
SlimGenMultiply Total Ticks: 1,621,830
Improvement:                 25.45 %</pre>
<p>Still, 25-35% improvement over the FPU based multiply is quite significant. Since X64 support hasn’t been fully hammered out (in that it “works” but hasn’t been sufficiently verified as working), those numbers are unavailable at the moment. However, they should be available in the near future as we finalize error handling and ensure that there are no bugs in the x64 assembly handling.</p>
<p>So why the great difference in performance? Well, part of it is the method size, the .Net method is 566 bytes of pure code, that’s over half a kilobyte of code that has to be walked through by the processor, code which needs to be brought into the instruction-cache on the CPU and executed, meanwhile the SSE2 method is around half that size, at 266 bytes. The smaller your footprint in the I-cache, the fewer hits you take and the more likely your code is to actually be IN the I-cache. Then there’s the instructions, SSE2 has been around for a while, and so it has had plenty of time to be wrangled around with by CPU manufacturers to ensure optimal performance. Finally there’s the memory hit issue, the SSE2 based code hits memory a minimal number of times, reducing the chances of cache misses, after the first read/write, except for a few cases.</p>
<p>Finally there’s how it deals with storage of the temporary results. The .Net FPU based version allocates a Matrix type on the stack, calls the constructor (which 0 initializes it), and then proceeds to overwrite those entries one by one with the results of each set of dot products. At the end of the method it does what amounts to a memcpy, and copies the temporary matrix over the result matrix. The SSE2 version however doesn’t bother with initializing the stack and only stores three of the results on the stack, opting to write out the final result directly to the destination. The three other rows are then moved back into XMM registers and then back out to the destination.</p>
<p>The SSE2 source code, followed by the .Net source code, note that both are functionally equivalent:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">start<span style="color: #339933;">:</span>      <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esp</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">4</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm4</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm5</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x10</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm6</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x20</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm7</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x30</span><span style="color: #009900; font-weight: bold;">&#93;</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ecx</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x00</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x55</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xAA</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xFF</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm4</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm5</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm6</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm7</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esp</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">0x20</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span> <span style="color: #666666; font-style: italic;">; store row 0 of new matrix</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ecx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x10</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x00</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x55</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xAA</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xFF</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm4</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm5</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm6</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm7</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esp</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">0x30</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span> <span style="color: #666666; font-style: italic;">; store row 1 of new matrix</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ecx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x20</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x00</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x55</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xAA</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xFF</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm4</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm5</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm6</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm7</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esp</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">0x40</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span> <span style="color: #666666; font-style: italic;">; store row 2 of new matrix</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ecx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x30</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x00</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x55</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xAA</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xFF</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm4</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm5</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm6</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm7</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">eax</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x30</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span> <span style="color: #666666; font-style: italic;">; store row 3 of new matrix</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esp</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">0x40</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">eax</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x20</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esp</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">0x30</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">eax</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x10</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esp</span> <span style="color: #339933;">-</span> <span style="color: #0000ff;">0x20</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">eax</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">ret</span>     <span style="color: #0000ff;">4</span></pre></div></div>

<p>The .Net matrix multiplication source code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Multiply<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> Matrix left, <span style="color: #0600FF;">ref</span> Matrix right, <span style="color: #0600FF;">out</span> Matrix result<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
    Matrix r<span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M11</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M11</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M11</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M12</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M21</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M13</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M31</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M14</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M41</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M12</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M11</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M12</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M12</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M22</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M13</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M32</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M14</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M42</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M13</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M11</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M13</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M12</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M23</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M13</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M33</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M14</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M43</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M14</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M11</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M14</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M12</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M24</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M13</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M34</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M14</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M44</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M21</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M21</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M11</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M22</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M21</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M23</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M31</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M24</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M41</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M22</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M21</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M12</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M22</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M22</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M23</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M32</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M24</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M42</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M23</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M21</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M13</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M22</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M23</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M23</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M33</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M24</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M43</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M24</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M21</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M14</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M22</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M24</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M23</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M34</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M24</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M44</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M31</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M31</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M11</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M32</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M21</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M33</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M31</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M34</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M41</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M32</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M31</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M12</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M32</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M22</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M33</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M32</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M34</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M42</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M33</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M31</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M13</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M32</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M23</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M33</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M33</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M34</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M43</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M34</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M31</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M14</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M32</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M24</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M33</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M34</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M34</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M44</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M41</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M41</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M11</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M42</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M21</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M43</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M31</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M44</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M41</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M42</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M41</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M12</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M42</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M22</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M43</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M32</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M44</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M42</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M43</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M41</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M13</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M42</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M23</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M43</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M33</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M44</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M43</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    r.<span style="color: #0000FF;">M44</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M41</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M14</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M42</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M24</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M43</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M34</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #000000;">&#40;</span>left.<span style="color: #0000FF;">M44</span> <span style="color: #008000;">*</span> right.<span style="color: #0000FF;">M44</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    result <span style="color: #008000;">=</span> r<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://scapecode.com/2009/08/slimgen-and-you-part-add-eax-eax-of-n-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SlimGen and You, Part ADD AL, [RAX] of N</title>
		<link>http://scapecode.com/2009/08/slimgen-and-you-part-add-al-rax-of-n/</link>
		<comments>http://scapecode.com/2009/08/slimgen-and-you-part-add-al-rax-of-n/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 20:40:23 +0000</pubDate>
		<dc:creator>Washu</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[SlimDX]]></category>
		<category><![CDATA[SlimGen]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://scapecode.com/?p=72</guid>
		<description><![CDATA[The question does arise though, when using SlimGen and writing your SSE replacement methods, what kind of calling convention does the CLR use? The CLR uses a version of fastcall. On x86 processors this means that the first two parameters (that are DWORD or smaller) are passed in ECX and EDX. However, and this is [...]]]></description>
			<content:encoded><![CDATA[<p>The question does arise though, when using SlimGen and writing your SSE replacement methods, what kind of calling convention does the CLR use?</p>
<p>The CLR uses a version of fastcall. On x86 processors this means that the first two parameters (that are DWORD or smaller) are passed in ECX and EDX. However, and this is where the CLR differs from standard fastcall, the parameters after the first two are pushed onto the stack from left to right, not right to left. This is important to remember, especially for functions that take a variable number of arguments. So a call like: <tt>X(‘c’, 2, 3.0f, “Hello”);</tt> becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">X<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #7f007f;">'c'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">3.0f</span><span style="color: #339933;">,</span> <span style="color: #7f007f;">&quot;Hello&quot;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
<span style="color: #adadad; font-style: italic;">00000025</span>  <span style="color: #00007f; font-weight: bold;">push</span>        <span style="color: #0000ff;">40400000h</span> <span style="color: #666666; font-style: italic;">; 3.0f</span>
<span style="color: #adadad; font-style: italic;">0000002a</span>  <span style="color: #00007f; font-weight: bold;">push</span>        <span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #00007f;">ds</span><span style="color: #339933;">:</span><span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #0000ff;">03402088h</span><span style="color: #009900; font-weight: bold;">&#93;</span> <span style="color: #666666; font-style: italic;">;Address of &quot;Hello&quot;</span>
<span style="color: #adadad; font-style: italic;">00000030</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">edx</span><span style="color: #339933;">,</span><span style="color: #0000ff;">2</span> 
<span style="color: #adadad; font-style: italic;">00000035</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span><span style="color: #0000ff;">63h</span> <span style="color: #666666; font-style: italic;">;'c'</span>
<span style="color: #adadad; font-style: italic;">0000003a</span>  <span style="color: #00007f; font-weight: bold;">call</span>        FFB8B040</pre></div></div>

<p>The situation is the same for member functions as well, except with this being passed in ECX, which leaves only EDX to hold an additional parameter. The rest are passed on the stack as before:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">p<span style="color: #339933;">.</span>Y<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">3.0f</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
<span style="color: #adadad; font-style: italic;">0000006d</span>  <span style="color: #00007f; font-weight: bold;">push</span>        <span style="color: #0000ff;">40400000h</span>  <span style="color: #666666; font-style: italic;">; 3.0f</span>
<span style="color: #adadad; font-style: italic;">00000072</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">ecx</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ebp</span><span style="color: #339933;">-</span><span style="color: #0000ff;">40h</span><span style="color: #009900; font-weight: bold;">&#93;</span> <span style="color: #666666; font-style: italic;">;this</span>
<span style="color: #adadad; font-style: italic;">00000075</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">edx</span><span style="color: #339933;">,</span><span style="color: #0000ff;">2</span>
<span style="color: #adadad; font-style: italic;">0000007c</span>  <span style="color: #00007f; font-weight: bold;">call</span>        FFA1B048</pre></div></div>

<p>So this all seems clear enough, but it’s important to note these differences, especially when you’re poking around in the low level bowels of the CLR or when you’re doing what SlimGen does: which is replacing actual method bodies.</p>
<p>So this does beget the question, what about on the x64 platform? Well, again, the calling convention is fastcall with a few differences. The first four parameters are in RCX, RDX, R8 and R9 (or smaller registers), unless those parameters are floating point types, in which case they are passed using XMM registers.&#160;</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">Z<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #7f007f;">'c'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">3.0f</span><span style="color: #339933;">,</span> <span style="color: #7f007f;">&quot;Hello&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">1.0</span><span style="color: #339933;">,</span> pa<span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
<span style="color: #adadad; font-style: italic;">000000c0</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         r9<span style="color: #339933;">,</span><span style="color: #0000ff;">124D3100h</span> 
<span style="color: #adadad; font-style: italic;">000000ca</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         r9<span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">qword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>r9<span style="color: #009900; font-weight: bold;">&#93;</span> <span style="color: #666666; font-style: italic;">; &quot;Hello&quot;</span>
<span style="color: #adadad; font-style: italic;">000000cd</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         rax<span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">qword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rsp<span style="color: #339933;">+</span><span style="color: #0000ff;">38h</span><span style="color: #009900; font-weight: bold;">&#93;</span> <span style="color: #666666; font-style: italic;">;pa (IntPtr[])</span>
<span style="color: #adadad; font-style: italic;">000000d2</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #000000; font-weight: bold;">qword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rsp<span style="color: #339933;">+</span><span style="color: #0000ff;">28h</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span>rax <span style="color: #666666; font-style: italic;">;pa - stack spill</span>
<span style="color: #adadad; font-style: italic;">000000d7</span>  <span style="color: #00007f; font-weight: bold;">movsd</span>       <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span>mmword <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #0000ff;">00000118h</span><span style="color: #009900; font-weight: bold;">&#93;</span> <span style="color: #666666; font-style: italic;">;1.0</span>
<span style="color: #adadad; font-style: italic;">000000df</span>  <span style="color: #00007f; font-weight: bold;">movsd</span>       mmword <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rsp<span style="color: #339933;">+</span><span style="color: #0000ff;">20h</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span><span style="color: #00007f;">xmm0</span> <span style="color: #666666; font-style: italic;">;1.0 - stack spill</span>
<span style="color: #adadad; font-style: italic;">000000e5</span>  movss       <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #0000ff;">00000110h</span><span style="color: #009900; font-weight: bold;">&#93;</span> <span style="color: #666666; font-style: italic;">;3.0f</span>
<span style="color: #adadad; font-style: italic;">000000ed</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">edx</span><span style="color: #339933;">,</span><span style="color: #0000ff;">2</span> <span style="color: #666666; font-style: italic;">;int (2)</span>
<span style="color: #adadad; font-style: italic;">000000f2</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">cx</span><span style="color: #339933;">,</span><span style="color: #0000ff;">63h</span> <span style="color: #666666; font-style: italic;">;'c' </span>
<span style="color: #adadad; font-style: italic;">000000f6</span>  <span style="color: #00007f; font-weight: bold;">call</span>        FFFFFFFFFFEC9300</pre></div></div>

<p>Whew, that looks pretty nasty doesn’t it? But if you notice, pretty much every single parameter to that function is passed in a register. The stack spillage is part of the calling convention to allow for variables to be spilled into memory (or read back from memory) when the register needs to be used. Calling an instance method follows pretty much the same rules, except the this pointer is passed in RCX first.</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">p<span style="color: #339933;">.</span>Q<span style="color: #009900; font-weight: bold;">&#40;</span>~0L<span style="color: #339933;">,</span> ~1L<span style="color: #339933;">,</span> ~2L<span style="color: #339933;">,</span> ~<span style="color: #0000ff;">3</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
<span style="color: #adadad; font-style: italic;">0000010a</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         rcx<span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">qword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rsp<span style="color: #339933;">+</span><span style="color: #0000ff;">30h</span><span style="color: #009900; font-weight: bold;">&#93;</span> <span style="color: #666666; font-style: italic;">; this pointer</span>
<span style="color: #adadad; font-style: italic;">0000010f</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #000000; font-weight: bold;">qword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rsp<span style="color: #339933;">+</span><span style="color: #0000ff;">20h</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">0FFFFFFFFFFFFFFFCh</span> <span style="color: #666666; font-style: italic;">;~3L, spilled to stack</span>
<span style="color: #adadad; font-style: italic;">00000118</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         r9<span style="color: #339933;">,</span><span style="color: #0000ff;">0FFFFFFFFFFFFFFFDh</span> <span style="color: #666666; font-style: italic;">;~2L</span>
<span style="color: #adadad; font-style: italic;">0000011f</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         r8<span style="color: #339933;">,</span><span style="color: #0000ff;">0FFFFFFFFFFFFFFFEh</span> <span style="color: #666666; font-style: italic;">;~1L</span>
<span style="color: #adadad; font-style: italic;">00000126</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         rdx<span style="color: #339933;">,</span><span style="color: #0000ff;">0FFFFFFFFFFFFFFFFh</span> <span style="color: #666666; font-style: italic;">;~0L</span>
<span style="color: #adadad; font-style: italic;">0000012d</span>  <span style="color: #00007f; font-weight: bold;">call</span>        FFFFFFFFFFEC9310&lt;<span style="color: #339933;">/</span>p&gt;</pre></div></div>

<p>Calling a function and passing something larger than a register can store does pose an interesting problem, the CLR deals with it by moving the entire data onto the stack, and passing it (hence call by value)</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">var v = new Vector<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
p<span style="color: #339933;">.</span>R<span style="color: #009900; font-weight: bold;">&#40;</span>v<span style="color: #009900; font-weight: bold;">&#41;</span><span style="color: #666666; font-style: italic;">;</span>
<span style="color: #adadad; font-style: italic;">00000169</span>  <span style="color: #00007f; font-weight: bold;">lea</span>         rcx<span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">&#91;</span>rsp<span style="color: #339933;">+</span><span style="color: #0000ff;">40h</span><span style="color: #009900; font-weight: bold;">&#93;</span> 
<span style="color: #adadad; font-style: italic;">0000016e</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         rax<span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">qword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rcx<span style="color: #009900; font-weight: bold;">&#93;</span> 
<span style="color: #adadad; font-style: italic;">00000171</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #000000; font-weight: bold;">qword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rsp<span style="color: #339933;">+</span><span style="color: #0000ff;">50h</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span>rax 
<span style="color: #adadad; font-style: italic;">00000176</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         rax<span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">qword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rcx<span style="color: #339933;">+</span><span style="color: #0000ff;">8</span><span style="color: #009900; font-weight: bold;">&#93;</span> 
<span style="color: #adadad; font-style: italic;">0000017a</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #000000; font-weight: bold;">qword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span>rsp<span style="color: #339933;">+</span><span style="color: #0000ff;">58h</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span>rax 
<span style="color: #adadad; font-style: italic;">0000017f</span>  <span style="color: #00007f; font-weight: bold;">lea</span>         rdx<span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">&#91;</span>rsp<span style="color: #339933;">+</span><span style="color: #0000ff;">50h</span><span style="color: #009900; font-weight: bold;">&#93;</span> 
<span style="color: #adadad; font-style: italic;">00000184</span>  <span style="color: #00007f; font-weight: bold;">mov</span>         rcx<span style="color: #339933;">,</span>r8 
<span style="color: #adadad; font-style: italic;">00000187</span>  <span style="color: #00007f; font-weight: bold;">call</span>        FFFFFFFFFFEC9318</pre></div></div>

<p>As you can see, it copies the data from the vector onto the stack, stores the this pointer in RCX, and then calls to the function. This is why pass by reference is the preferred method (for fast code) to move around structures that are non-trivial.</p>
<p>All of this goes into calcuating our matrix multiplication method (which assumes the output is not one of the inputs):</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">BITS        <span style="color: #0000ff;">32</span>
<span style="color: #000000; font-weight: bold;">ORG</span>         <span style="color: #0000ff;">0x59f0</span>
<span style="color: #666666; font-style: italic;">;           void Multiply(ref Matrix, ref Matrix, out Matrix)</span>
start<span style="color: #339933;">:</span>      <span style="color: #00007f; font-weight: bold;">mov</span>     <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esp</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">4</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm4</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm5</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x10</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm6</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x20</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm7</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x30</span><span style="color: #009900; font-weight: bold;">&#93;</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ecx</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x00</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x55</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xAA</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xFF</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm4</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm5</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm6</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm7</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">eax</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span> <span style="color: #666666; font-style: italic;">; Calculate row 0 of new matrix</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ecx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x10</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x00</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x55</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xAA</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xFF</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm4</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm5</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm6</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm7</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">eax</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x10</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span> <span style="color: #666666; font-style: italic;">; Calculate row 1 of new matrix</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ecx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x20</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x00</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x55</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xAA</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xFF</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm4</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm5</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm6</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm7</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">eax</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x20</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span> <span style="color: #666666; font-style: italic;">; Calculate row 2 of new matrix</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ecx</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x30</span><span style="color: #009900; font-weight: bold;">&#93;</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">movaps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x00</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0x55</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xAA</span>
            <span style="color: #00007f; font-weight: bold;">shufps</span>  <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">0xFF</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm4</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm5</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm2</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm6</span>
            <span style="color: #00007f; font-weight: bold;">mulps</span>   <span style="color: #00007f;">xmm3</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm7</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm2</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm3</span>
            <span style="color: #00007f; font-weight: bold;">addps</span>   <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm1</span>
&nbsp;
            <span style="color: #00007f; font-weight: bold;">movups</span>  <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">eax</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">0x30</span><span style="color: #009900; font-weight: bold;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #00007f;">xmm0</span> <span style="color: #666666; font-style: italic;">; Calculate row 3 of new matrix</span>
            <span style="color: #00007f; font-weight: bold;">ret</span>     <span style="color: #0000ff;">4</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://scapecode.com/2009/08/slimgen-and-you-part-add-al-rax-of-n/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SlimGen and You, Part ADD [EAX], EAX of N</title>
		<link>http://scapecode.com/2009/08/slimgen-and-you-part-add-eax-eax-of-n/</link>
		<comments>http://scapecode.com/2009/08/slimgen-and-you-part-add-eax-eax-of-n/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 23:00:16 +0000</pubDate>
		<dc:creator>Washu</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[SlimDX]]></category>
		<category><![CDATA[SlimGen]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://scapecode.com/?p=70</guid>
		<description><![CDATA[So previously we delved into one of the nastier performance corners on the .Net framework. Today I’m going to introduce you to a tool, that is in development currently, which allows you to take those slow math functions of yours and replace them with high performance SSE optimized methods. We’ve called it SlimGen, which although [...]]]></description>
			<content:encoded><![CDATA[<p>So previously we delved into one of the nastier performance corners on the .Net framework. Today I’m going to introduce you to a tool, that is in development currently, which allows you to take those slow math functions of yours and replace them with high performance SSE optimized methods.</p>
<p>We’ve called it <a href="http://code.google.com/p/slimgen/">SlimGen</a>, which although not exactly accurate, does fit nicely in with the other Slim projects currently underway including <a href="http://ventspace.wordpress.com/">SlimTune</a>, and the flagship that started it all, <a href="http://slimdx.org">SlimDX</a>.</p>
<p>So what does SlimGen do? Well, you pass it a .Net assembly and it replaces the native method bodies, which are generated using NGEN, with replacement ones written in assembly (for now). This modified assembly then replaces the original assembly that was stored in the native image store. SlimGen can operate on signed and unsigned assemblies alike, as the native image is not signed, more on this later though.</p>
<p>Managed PE files contain a great deal of metadata stored in tables. You can enumerate these tables and parse them yourself, for instance if you were writing your own <a href="http://scientificninja.com/tag/clr">CLR</a>. Thankfully though, the .Net framework comes with several COM interfaces that are very helpful in accessing these tables without having to manually parse them out of the PE file, this is especially useful since the table rows are are not a fixed format. Specifically, indexes in the tables can be either a 2 bytes or 4 bytes in size depending on the size of the dataset indexed. In the case of SlimGen we use the <a href="http://msdn.microsoft.com/en-us/library/ms232953.aspx">IMetaDataImport2 interface</a> for accessing the metadata.</p>
<p>Of course, the managed metadata does not contain all of the information we need. NGEN manipulates the managed assembly and introduces pre-jitted versions of the functions contained within the assembly. However, their managed counterparts remain in the assembly and are what the metadata tables reference to. So how does one go from a managed method and its IL to the associated unmanaged code? Well, the CLR header of a PE file does contain a pointer to a table for a native header. However the exact format of that table is undocumented and as such it makes it hard to parse it and find the information we need. Therefore we have to use an alternative method…</p>
<p>When you load up an assembly the CLR generates, using the metadata and other information found in the PE file, a set of runtime tables that it uses to indicate information about where things are in memory, and their current state. For instance, it can tell if its jitted a method or not. When you load up an assembly that’s been NGENed, it checks the native images for an associated copy, assuming your assembly validates, and will load up the NGENed assembly and parse out the appropriate information from that. Therefore we need some way of gaining access to these runtime generated tables. Enter the debugger.</p>
<p>The .Net framework exposes debugging interfaces that are quite trivial to implement, but more important, they give you access to all of the runtime information available to the CLR. In the case of SlimGen what we do is load up your assembly (not run) into a host process and then simply have the host process execute a debugger breakpoint. The SlimGen analyzer first initializes its self as a debugger and then executes the host process as the attached debugger. When the breakpoint is hit, it breaks into the analyzer, which can then begin the work of processing the loaded assemblies. Since SlimGen knows which assembly it fed to the host, it is able to filter out all of the other assemblies that have been loaded and focus in on the one we care about. First we check and see if a native version of the assembly has been loaded, for if one hasn’t been loaded there is no point in continuing. if not then we simply report an error and cleanup. Assuming there is a native version of the assembly loaded then we use the aforementioned metadata interfaces to walk the assembly and find all of the methods that have been marked for replacement. Each method is examined to ensure that it has a native counterpart, and if it doesn’t another warning is issued and the method is skipped.</p>
<p>Now comes the annoying part. In .Net 1.x the framework had each method exist within a singular code chunk, which made extracting that code quite easy. However in .Net 2.x and forward the framework allows a method to have multiple code chunks, each with a different base address and length. This is theoretically to allow an optimizer to spread work its magic, but it does make extracting methods harder. SlimGen will generate an assembly file per chunk and all of the associated binaries for each chunk, generated from the assembly files, must be present for the method to be replaced. No dangling chunks please. The SlimGen analyzer extracts each base address from each chunk, along with the module base address. Using that information we can then calculate the relative virtual address of each method’s native counterpart within the NGENed file.</p>
<p>Using that information the SlimGen client simply walks a copy of the native image performing the replacement of each method, and then when done (and assuming no errors), copies it back over the original NGEN image. Tada, you now have your highly optimized SSE code running in a managed application with no managed –&gt; unmanaged transitions in sight.</p>
]]></content:encoded>
			<wfw:commentRss>http://scapecode.com/2009/08/slimgen-and-you-part-add-eax-eax-of-n/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SlimGen and You, Part ADD [EAX], AL of N</title>
		<link>http://scapecode.com/2009/07/slimgen-and-you-part-add-eax-al-of-n/</link>
		<comments>http://scapecode.com/2009/07/slimgen-and-you-part-add-eax-al-of-n/#comments</comments>
		<pubDate>Sat, 01 Aug 2009 05:52:05 +0000</pubDate>
		<dc:creator>Washu</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[SlimDX]]></category>
		<category><![CDATA[SlimGen]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://scapecode.com/?p=68</guid>
		<description><![CDATA[Imagine you could have the safety of managed code, and the speed of SIMD all in one? Sounds like one of those weird dreams Trent has, or perhaps you are already thinking of using C++/CLI to wrap SIMD methods to help reduce the unmanaged transition overhead. You might also be thinking about pinvoking DLL methods [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine you could have the safety of managed code, and the speed of SIMD all in one? Sounds like one of those weird dreams <a href="http://polycat.net/" target="_blank">Trent</a> has, or perhaps you are already thinking of using C++/CLI to wrap SIMD methods to help reduce the unmanaged transition overhead. You might also be thinking about pinvoking DLL methods such as those used in the D3DX framework to take advantage of its SIMD capabilities.</p>
<p>While all of those are quite possible, and for sufficiently large problems quite efficient too, they also have a relatively high cost of invocation. Managed to unmanaged transitions, even in the best of cases, costs a pretty penny. Registers have to be saved, marshalling of non-fundamental types has to be performed, and in many cases an interop thunk has to be created/jitted. This is a case where the best option is to do as much work as you can in one area before transitioning to the next.</p>
<p>But you can’t always do tons of work at once, a prime example is that of managing your game state. You’ll have discrete transformations of objects, but batching up those transformations to perform them all at once because a management nightmare. You have to craft special data-structures to avoid marshalling, use pinned arrays, and in general you end up doing a lot of work maintaining the two, will spend plenty of time debugging your interface, and may actually not gain anything speed wise still.</p>
<p>If you’re wondering just how bad the interop transition is, you can take a look at my <a href="http://scapecode.com/?cat=3" target="_blank">previous entries</a>, where I explored the topic in some detail.</p>
<p>In the .Net framework, most code runs almost as fast, as fast, or faster than the comparable native counterparts. There are cases where the framework is significantly faster, and cases where it loses out at about 10% in the worst case. 10% isn’t a horrible loss, and it’s not a consistent loss either. The cost will vary depending on factors such as: is JITing required, is memory allocation performed, are you doing FPU math that would be vectorized in native code?</p>
<p>In fact, that 10% figure isn’t accurate either: If a method requires JITting the first time it is called, which could cost you 10% on the first invocation, future invocations will not need JITing and so the cost may end up being the same as its native counterpart henceforth. If the method is called a thousand times, then that’s only an additional .01% cost over the entire set of invocations.</p>
<p>The only real area that the .Net framework seriously loses out to unmanaged code is in the math department. The inability to use vectorization can significantly increase the cost of managed math over that of unamanged math code, that 10% figure rears its ugly head here. On the integer math side of things managed code is almost on equal footing with unmanaged code, although there are some vectorized operations you can perform that will enhance integer operations quite significantly, but in general the two add up to be about the same. However when it comes to floating point performance managed code loses out due to its dependency on the FPU or single float SSE instructions. The ability to vectorize large chunks of floating point math can work wonders for unmanaged code.</p>
<p>Well, all is not lost for those of us who love the managed world… SlimGen is here. Exactly what SlimGen is will be delved into later, but here’s a sample preview of what it can do:</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">SlimDX<span style="color: #339933;">.</span>Matrix<span style="color: #339933;">.</span>Multiply<span style="color: #009900; font-weight: bold;">&#40;</span>SlimDX<span style="color: #339933;">.</span>Matrix ByRef<span style="color: #339933;">,</span> SlimDX<span style="color: #339933;">.</span>Matrix ByRef<span style="color: #339933;">,</span> SlimDX<span style="color: #339933;">.</span>Matrix ByRef<span style="color: #009900; font-weight: bold;">&#41;</span>
Begin 5a856e64<span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">size</span> <span style="color: #0000ff;">293</span>
<span style="color: #adadad; font-style: italic;">5A856E64</span> 8B442404         <span style="color: #00007f; font-weight: bold;">mov</span>         <span style="color: #00007f;">eax</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">dword</span> <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">esp</span><span style="color: #339933;">+</span><span style="color: #0000ff;">4</span><span style="color: #009900; font-weight: bold;">&#93;</span>
<span style="color: #adadad; font-style: italic;">5A856E68</span> 0F1022           <span style="color: #00007f; font-weight: bold;">movups</span>      <span style="color: #00007f;">xmm4</span><span style="color: #339933;">,</span>xmmword <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span><span style="color: #009900; font-weight: bold;">&#93;</span>
<span style="color: #adadad; font-style: italic;">5A856E6B</span> 0F106A10         <span style="color: #00007f; font-weight: bold;">movups</span>      <span style="color: #00007f;">xmm5</span><span style="color: #339933;">,</span>xmmword <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span><span style="color: #339933;">+</span><span style="color: #0000ff;">10h</span><span style="color: #009900; font-weight: bold;">&#93;</span>
<span style="color: #adadad; font-style: italic;">5A856E6F</span> 0F107220         <span style="color: #00007f; font-weight: bold;">movups</span>      <span style="color: #00007f;">xmm6</span><span style="color: #339933;">,</span>xmmword <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span><span style="color: #339933;">+</span><span style="color: #0000ff;">20h</span><span style="color: #009900; font-weight: bold;">&#93;</span>
<span style="color: #adadad; font-style: italic;">5A856E73</span> 0F107A30         <span style="color: #00007f; font-weight: bold;">movups</span>      <span style="color: #00007f;">xmm7</span><span style="color: #339933;">,</span>xmmword <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">edx</span><span style="color: #339933;">+</span><span style="color: #0000ff;">30h</span><span style="color: #009900; font-weight: bold;">&#93;</span>
<span style="color: #adadad; font-style: italic;">5A856E77</span> 0F1001           <span style="color: #00007f; font-weight: bold;">movups</span>      <span style="color: #00007f;">xmm0</span><span style="color: #339933;">,</span>xmmword <span style="color: #000000; font-weight: bold;">ptr</span> <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #00007f;">ecx</span><span style="color: #009900; font-weight: bold;">&#93;</span>
<span style="color: #adadad; font-style: italic;">5A856E7A</span> 0F28C8           <span style="color: #00007f; font-weight: bold;">movaps</span>      <span style="color: #00007f;">xmm1</span><span style="color: #339933;">,</span><span style="color: #00007f;">xmm0</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://scapecode.com/2009/07/slimgen-and-you-part-add-eax-al-of-n/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VMWare Server 2.x</title>
		<link>http://scapecode.com/2009/06/vmware-server-2-x/</link>
		<comments>http://scapecode.com/2009/06/vmware-server-2-x/#comments</comments>
		<pubDate>Thu, 25 Jun 2009 02:49:43 +0000</pubDate>
		<dc:creator>Washu</dc:creator>
				<category><![CDATA[Virtualization]]></category>

		<guid isPermaLink="false">http://scapecode.com/?p=48</guid>
		<description><![CDATA[Virtualization has become quite the business hot topic (and buzzword too), now days. It offers the promise of server consolidation, ease of management, personal reduction, monetary savings in miscellaneous fields (such as power consumption). Of course, there is always the question of if it actually delivers on any those promises. I&#8217;ve been using VMWare Server [...]]]></description>
			<content:encoded><![CDATA[<p>Virtualization has become quite the business hot topic (and buzzword too), now days. It offers the promise of server consolidation, ease of management, personal reduction, monetary savings in miscellaneous fields (such as power consumption). Of course, there is always the question of if it actually delivers on any those promises.</p>
<p>I&#8217;ve been using VMWare Server for quite some time, and have been pleased with the product overall. Well, up until I tried out version 2.x.</p>
<p>It was quite a disappointment.</p>
<p>First things first, the web interface, it’s rather slow and clunky. It does display more information than you used to get out of VMWare Server 1.x, such as memory usage and CPU usage, but you have to constantly refresh the pages to get more up to date information. There is a somewhat “ajax” feel to the interface in some areas, and in others you can definitely feel the page refreshes. You also have to install a console plugin to even view your VM running, which is major annoying since the installation only works on a select subset of browsers (Firefox and IE basically). The final issue, which is a real killer, with the web interface is the memory usage. Running two VMs, with neither console open, they were taking up their respective amounts of RAM. Meanwhile the web interface was hogging another half gig of ram doing nothing (I was not even on the interface at the time, just checking it via host OS tools). That’s a rather large chunk of overhead for something you shouldn’t have to touch much. The good news is that you can turn off the web interface, if you’re willing to edit some batch files. The bad news is, you then have to buy VMWare vCenter if you want to manage it without the web interface.</p>
<p>Then there are the speed issues. While running an x64 VM, attempting to install Windows Server x64, with the guest being allocated 2GB of RAM and two processor cores, the machine ran inordinately slow. The installation of Windows Server x64 took almost 3 HOURS just to get it far enough along that it would be usable. It was about this point in time that I started to wonder if v2.x of VMWare Server was worth it. Research indicated that others had experienced similar issues with VMWare Server 2.x and multicore installations. After disabling one of the cores it was noticeably faster, but still quite slow. Read, and especially writes, to the virtual disk were noticeable, and patching the operating system took longer than I cared to wait.</p>
<p>The last thing that really made me decide against using VMWare Server 2.x was the feature removal. For instance, you can no longer trivially create a virtual machine that has it’s backing storage being a physical disk. While the virtualization subsystem DOES support this, you have to craft a handmade (or use third party tools) VMDK just to mount it up. At which point the web interface has problems managing the virtual machine, as it’s not designed to deal with such capabilities, and will typically display an error message when attempting to manage other features of that virtual machine. Considering the rather significant performance advantages using actual backing disks can have, and also considering that this was a rather simple feature of VMWare Server 1.x to use… it’s quite annoying to see it vanish.</p>
]]></content:encoded>
			<wfw:commentRss>http://scapecode.com/2009/06/vmware-server-2-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Server outage</title>
		<link>http://scapecode.com/2009/06/server-outage/</link>
		<comments>http://scapecode.com/2009/06/server-outage/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 17:43:17 +0000</pubDate>
		<dc:creator>Washu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://scapecode.com/?p=3</guid>
		<description><![CDATA[Well, sites back up, on WordPress now&#8230; Server crashed something spectacularily the other day. Both drives in the RAID 1 decided to take a nice vacation, and so I&#8217;m having to restore from backups I&#8217;ve kept. Thanks to google I&#8217;ve got most of my old posts, and will be dumping them up here through the [...]]]></description>
			<content:encoded><![CDATA[<p>Well, sites back up, on WordPress now&#8230;</p>
<p>Server crashed something spectacularily the other day. Both drives in the RAID 1 decided to take a nice vacation, and so I&#8217;m having to restore from backups I&#8217;ve kept.</p>
<p>Thanks to google I&#8217;ve got most of my old posts, and will be dumping them up here through the day as I edit them and put them together again.</p>
]]></content:encoded>
			<wfw:commentRss>http://scapecode.com/2009/06/server-outage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
