<?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>Christian Schenk&#187; opengl</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/opengl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:08:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Profiling with CPU-Sampler</title>
		<link>http://www.christianschenk.org/blog/profiling-cpu-sampler/</link>
		<comments>http://www.christianschenk.org/blog/profiling-cpu-sampler/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 10:12:40 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[cpu sampler]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=734</guid>
		<description><![CDATA[Apple&#8217;s Xcode comes with a neat profiling tool called CPU Sampler. It helps you identifying time consuming code blocks in your software and is really handy if you need to optimize for optimal performance.
Just recently I tried rewriting OpenGL&#8217;s routines that handle matrix calculations as part of a lecture at the university. Pretty soon I [...]]]></description>
			<content:encoded><![CDATA[<p>Apple&#8217;s Xcode comes with a neat profiling tool called <em>CPU Sampler</em>. It helps you identifying time consuming code blocks in your software and is really handy if you need to optimize for optimal performance.</p>
<p>Just recently I tried rewriting OpenGL&#8217;s routines that handle matrix calculations as part of a lecture at the university. Pretty soon I came to the conclusion, that I had to optimize my code if I wanted to compete with the implementation provided by OpenGL.</p>
<p>This post presents at quick look at <em>CPU Sampler</em> which helped me making well-founded decisions resulting in a faster implementation.</p>
<p><span id="more-734"></span></p>
<h2>Howto</h2>
<p>Once you&#8217;ve developed your software with Xcode and want to profile it using <em>CPU Sampler</em>, all you have to do is to click on Run &#8211; Run with Performance Tool &#8211; CPU Sampler. This will start the tool and run your application; you can start and stop the recording as you see fit.</p>
<p>As soon as you end your application you can have a look at the samples collected during the last run. This is the interesting part because you can review how long a certain function was running and the percentage tells you how long this took compared to the overall run.</p>
<p>Try playing around with the various options. In most situations you can stick with the defaults but there&#8217;re more interesting things to look at if you want to. For example try changing the <em>Active thread</em> from <em>All threads</em> to something else and the view will show the corresponding samples only.</p>
<h2>Example</h2>
<p>As I said, I tried rewriting OpenGL&#8217;s routines that handle matrix calculations. This was meant as an exercise to understand the operations needed that changed the matrices. Of course, the implementation was way slower than OpenGL and I should try speeding it up.</p>
<p>Using <em>CPU Sampler</em> I immediately identified a bottleneck and a major difference between my implementation and the one that comes with OpenGL. The slow part of my code could be found in repeated calls to <code>cos</code> and <code>sin</code>. Since OpenGL seems to work with the libraries of my graphics card directly I guess it tries to do certain computations there.</p>
<p>What I now did was substituting the calls to <code>cos</code> and <code>sin</code> with code that used a lookup table to retrieve precalculated values. This resulted in a major performance boost &#8211; still, OpenGL&#8217;s code is twice as fast as my one.</p>
<p>Have a look at the following screenshots. They show a run using OpenGL&#8217;s implementation of matrix manipulations, my own and an optimized version of my own implementation. Comparing the naive and optimized version of my code clearly shows that the optimized variant doesn&#8217;t spent much time calculating <code>cos</code> and <code>sin</code>.</p>
<div style="white-space:nowrap">
<a href="/wp-content/uploads/opengl.png" title="OpenGL" class="thickbox" rel="gallery-opengl"><img src="/wp-content/uploads/opengl-150x150.png" alt="OpenGL" /></a><a href="/wp-content/uploads/our_impl.png" title="Own implementation" class="thickbox" rel="gallery-opengl"><img src="/wp-content/uploads/our_impl-150x150.png" alt="Own implementation" /></a><a href="/wp-content/uploads/our_impl_opt.png" title="Own implementation optimized" class="thickbox" rel="gallery-opengl"><img src="/wp-content/uploads/our_impl_opt-150x150.png" alt="Own implementation optimized" /></a>
</div>
<h2>Conclusion</h2>
<p>I wanted to show that using a profiling tool like <em>CPU Sampler</em> that comes with Apple&#8217;s Xcode may make your life much easier if you&#8217;re trying to optimize your application. This helps you to make wise decisions when it comes to optimizing code so you don&#8217;t have to obfuscate your software with hard to read code blocks that have a very little effect regarding performance.</p>
<p>It&#8217;s nice to see that Apple ships tools like this with its IDE. Have a look at the other performance tools and you&#8217;ll be amazed how many cools things Xcode is capable of &#8211; things you wouldn&#8217;t even dream about in other IDE&#8217;s.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/profiling-cpu-sampler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenGL: triangle with white center</title>
		<link>http://www.christianschenk.org/blog/opengl-triangle-white-center/</link>
		<comments>http://www.christianschenk.org/blog/opengl-triangle-white-center/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 06:05:30 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[center]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[triangle]]></category>
		<category><![CDATA[white]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=717</guid>
		<description><![CDATA[Just recently I&#8217;ve learned how to create a triangle with OpenGL that has a white center and colored corners. Since I haven&#8217;t found a site explaining how to do this I thought about sharing my solution here.
It&#8217;s pretty easy as soon as you know that OpenGL blends the color between vertexes horizontally. Just setting the [...]]]></description>
			<content:encoded><![CDATA[<p>Just recently I&#8217;ve learned how to create a triangle with OpenGL that has a white center and colored corners. Since I haven&#8217;t found a site explaining how to do this I thought about sharing my solution here.</p>
<p>It&#8217;s pretty easy as soon as you know that OpenGL blends the color between vertexes horizontally. Just setting the corners of the triangle to the three fundamental colors (RGB) hoping that the center gets white won&#8217;t cut it.</p>
<p><span id="more-717"></span></p>
<h2>The goal</h2>
<p>The result should display a triangle with a white center and corners showing different colors. Have a look at it here.</p>
<p><img src="/wp-content/uploads/triangle.jpg" alt="Triangle" title="Triangle" width="380" height="352" class="size-full wp-image-718" /></p>
<h2>Solution</h2>
<p>One solution is splitting the triangle into smaller ones. This way you can set the color of the vertex in the center to white &#8211; a vertex all three sub-triangles have in common &#8211; and change the color of the corners as you see fit.</p>
<p>You can have a look at the code <a href="http://data.christianschenk.org/opengl-triangle-white-center/triangle.cpp">here</a>. Basically, it consists of some boilerplate code for OpenGL and a display function that contains the <em>magic</em>.</p>
<p>I&#8217;ve used a <code>GL_TRIANGLE_FAN</code> to construct the triangle. The vertexes are created in this order: center, top, bottom left, bottom right and top again. Right before every vertex definition the color is set like I wanted it.</p>
<h2>Conclusion</h2>
<p>In the end it&#8217;s simple to create a triangle with a white center. I hope this was helpful in understanding how shading works in OpenGL. Note that I&#8217;ve commented out a line in the code, try uncommenting it to see how the triangle really looks like.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/opengl-triangle-white-center/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
